bridging using libbridge
[strongswan.git] / src / dumm / bridge.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <sys/types.h>
17 #include <libbridge.h>
18
19 #include <debug.h>
20 #include <utils/linked_list.h>
21
22 #include "bridge.h"
23
24 typedef struct private_bridge_t private_bridge_t;
25
26 struct private_bridge_t {
27 /** public interface */
28 bridge_t public;
29 /** device name */
30 char *name;
31 /** list of attached interfaces */
32 linked_list_t *ifaces;
33 };
34
35 /**
36 * Implementation of bridge_t.get_name.
37 */
38 static char* get_name(private_bridge_t *this)
39 {
40 return this->name;
41 }
42
43 /**
44 * Implementation of bridge_t.create_iface_iterator.
45 */
46 static iterator_t* create_iface_iterator(private_bridge_t *this)
47 {
48 return this->ifaces->create_iterator(this->ifaces, TRUE);
49 }
50
51 /**
52 * Implementation of bridge_t.del_iface.
53 */
54 static bool del_iface(private_bridge_t *this, iface_t *iface)
55 {
56 iterator_t *iterator;
57 iface_t *current;
58 bool good = FALSE;
59
60 iterator = this->ifaces->create_iterator(this->ifaces, TRUE);
61 while (iterator->iterate(iterator, (void**)&current))
62 {
63 if (current == iface)
64 {
65 if (br_del_interface(this->name, iface->get_hostif(iface)) != 0)
66 {
67 DBG1("removing iface '%s' from bridge '%s' in kernel failed: %m",
68 iface->get_hostif(iface), this->name);
69 }
70 else
71 {
72 good = TRUE;
73 }
74 break;
75 }
76 }
77 if (iface != current)
78 {
79 DBG1("iface '%s' not found on bridge '%s'", iface->get_hostif(iface),
80 this->name);
81 }
82 iterator->destroy(iterator);
83 return good;
84 }
85
86 /**
87 * Implementation of bridge_t.add_iface.
88 */
89 static bool add_iface(private_bridge_t *this, iface_t *iface)
90 {
91 if (br_add_interface(this->name, iface->get_hostif(iface)) != 0)
92 {
93 DBG1("adding iface '%s' to bridge '%s' failed: %m",
94 iface->get_hostif(iface), this->name);
95 return FALSE;
96 }
97 this->ifaces->insert_last(this->ifaces, iface);
98 return TRUE;
99 }
100
101 /**
102 * instance counter to (de-)initialize libbridge
103 */
104 static int instances = 0;
105
106 /**
107 * Implementation of bridge_t.destroy.
108 */
109 static void destroy(private_bridge_t *this)
110 {
111 this->ifaces->destroy(this->ifaces);
112 if (br_del_bridge(this->name) != 0)
113 {
114 DBG1("deleting bridge '%s' from kernel failed: %m", this->name);
115 }
116 free(this->name);
117 free(this);
118 if (--instances == 0)
119 {
120 br_shutdown();
121 }
122 }
123
124 /**
125 * create the bridge instance
126 */
127 bridge_t *bridge_create(char *name)
128 {
129 private_bridge_t *this;
130
131 if (instances == 0)
132 {
133 if (br_init() != 0)
134 {
135 DBG1("libbridge initialization failed: %m");
136 return NULL;
137 }
138 }
139
140 this = malloc_thing(private_bridge_t);
141 this->public.get_name = (char*(*)(bridge_t*))get_name;
142 this->public.create_iface_iterator = (iterator_t*(*)(bridge_t*))create_iface_iterator;
143 this->public.del_iface = (bool(*)(bridge_t*, iface_t *iface))del_iface;
144 this->public.add_iface = (bool(*)(bridge_t*, iface_t *iface))add_iface;
145 this->public.destroy = (void*)destroy;
146
147 if (br_add_bridge(name) != 0)
148 {
149 DBG1("creating bridge '%s' failed: %m", name);
150 free(this);
151 return NULL;
152 }
153
154 this->name = strdup(name);
155 this->ifaces = linked_list_create();
156
157 instances++;
158 return &this->public;
159 }
160