]>
Commit | Line | Data |
---|---|---|
725e263f MW |
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 | ||
f05b4272 | 19 | #include <utils/debug.h> |
12642a68 | 20 | #include <collections/linked_list.h> |
725e263f MW |
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 | }; | |
a47486b5 MW |
34 | |
35 | /** | |
36 | * defined in iface.c | |
37 | */ | |
38 | bool iface_control(char *name, bool up); | |
7daf5226 | 39 | |
d6c8c0ca TB |
40 | METHOD(bridge_t, get_name, char*, |
41 | private_bridge_t *this) | |
725e263f MW |
42 | { |
43 | return this->name; | |
44 | } | |
45 | ||
d6c8c0ca TB |
46 | METHOD(bridge_t, create_iface_enumerator, enumerator_t*, |
47 | private_bridge_t *this) | |
725e263f | 48 | { |
a47486b5 | 49 | return this->ifaces->create_enumerator(this->ifaces); |
725e263f MW |
50 | } |
51 | ||
d6c8c0ca TB |
52 | METHOD(bridge_t, disconnect_iface, bool, |
53 | private_bridge_t *this, iface_t *iface) | |
725e263f | 54 | { |
a47486b5 | 55 | enumerator_t *enumerator; |
ff2d02ed | 56 | iface_t *current = NULL; |
725e263f MW |
57 | bool good = FALSE; |
58 | ||
a47486b5 MW |
59 | enumerator = this->ifaces->create_enumerator(this->ifaces); |
60 | while (enumerator->enumerate(enumerator, (void**)¤t)) | |
725e263f MW |
61 | { |
62 | if (current == iface) | |
63 | { | |
64 | if (br_del_interface(this->name, iface->get_hostif(iface)) != 0) | |
65 | { | |
8b0e0910 TB |
66 | DBG1(DBG_LIB, "removing iface '%s' from bridge '%s' in kernel" |
67 | " failed: %m", iface->get_hostif(iface), this->name); | |
725e263f MW |
68 | } |
69 | else | |
70 | { | |
7b7201be | 71 | iface->set_bridge(iface, NULL); |
ff2d02ed | 72 | this->ifaces->remove_at(this->ifaces, enumerator); |
725e263f MW |
73 | good = TRUE; |
74 | } | |
75 | break; | |
76 | } | |
77 | } | |
78 | if (iface != current) | |
79 | { | |
8b0e0910 TB |
80 | DBG1(DBG_LIB, "iface '%s' not found on bridge '%s'", |
81 | iface->get_hostif(iface), this->name); | |
725e263f | 82 | } |
a47486b5 | 83 | enumerator->destroy(enumerator); |
725e263f MW |
84 | return good; |
85 | } | |
86 | ||
d6c8c0ca TB |
87 | METHOD(bridge_t, connect_iface, bool, |
88 | private_bridge_t *this, iface_t *iface) | |
725e263f MW |
89 | { |
90 | if (br_add_interface(this->name, iface->get_hostif(iface)) != 0) | |
91 | { | |
8b0e0910 | 92 | DBG1(DBG_LIB, "adding iface '%s' to bridge '%s' failed: %m", |
725e263f MW |
93 | iface->get_hostif(iface), this->name); |
94 | return FALSE; | |
95 | } | |
7b7201be | 96 | iface->set_bridge(iface, &this->public); |
725e263f MW |
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 | ||
d6c8c0ca TB |
106 | METHOD(bridge_t, destroy, void, |
107 | private_bridge_t *this) | |
725e263f | 108 | { |
36beca7c MW |
109 | enumerator_t *enumerator; |
110 | iface_t *iface; | |
111 | ||
112 | enumerator = this->ifaces->create_enumerator(this->ifaces); | |
113 | while (enumerator->enumerate(enumerator, (void**)&iface)) | |
114 | { | |
115 | if (br_del_interface(this->name, iface->get_hostif(iface)) != 0) | |
116 | { | |
8b0e0910 TB |
117 | DBG1(DBG_LIB, "disconnecting iface '%s' failed: %m", |
118 | iface->get_hostif(iface)); | |
36beca7c MW |
119 | } |
120 | iface->set_bridge(iface, NULL); | |
121 | } | |
122 | enumerator->destroy(enumerator); | |
725e263f | 123 | this->ifaces->destroy(this->ifaces); |
a47486b5 | 124 | iface_control(this->name, FALSE); |
725e263f MW |
125 | if (br_del_bridge(this->name) != 0) |
126 | { | |
8b0e0910 TB |
127 | DBG1(DBG_LIB, "deleting bridge '%s' from kernel failed: %m", |
128 | this->name); | |
725e263f MW |
129 | } |
130 | free(this->name); | |
131 | free(this); | |
132 | if (--instances == 0) | |
133 | { | |
134 | br_shutdown(); | |
135 | } | |
136 | } | |
137 | ||
138 | /** | |
139 | * create the bridge instance | |
140 | */ | |
141 | bridge_t *bridge_create(char *name) | |
142 | { | |
143 | private_bridge_t *this; | |
7daf5226 | 144 | |
725e263f MW |
145 | if (instances == 0) |
146 | { | |
147 | if (br_init() != 0) | |
148 | { | |
8b0e0910 | 149 | DBG1(DBG_LIB, "libbridge initialization failed: %m"); |
725e263f MW |
150 | return NULL; |
151 | } | |
152 | } | |
7daf5226 | 153 | |
d6c8c0ca TB |
154 | INIT(this, |
155 | .public = { | |
156 | .get_name = _get_name, | |
157 | .create_iface_enumerator = _create_iface_enumerator, | |
158 | .disconnect_iface = _disconnect_iface, | |
159 | .connect_iface = _connect_iface, | |
160 | .destroy = _destroy, | |
161 | } | |
162 | ); | |
725e263f MW |
163 | |
164 | if (br_add_bridge(name) != 0) | |
165 | { | |
8b0e0910 | 166 | DBG1(DBG_LIB, "creating bridge '%s' failed: %m", name); |
725e263f MW |
167 | free(this); |
168 | return NULL; | |
169 | } | |
a47486b5 MW |
170 | if (!iface_control(name, TRUE)) |
171 | { | |
8b0e0910 | 172 | DBG1(DBG_LIB, "bringing bridge '%s' up failed: %m", name); |
a47486b5 | 173 | } |
725e263f MW |
174 | |
175 | this->name = strdup(name); | |
176 | this->ifaces = linked_list_create(); | |
177 | ||
178 | instances++; | |
179 | return &this->public; | |
180 | } | |
181 |