Commit | Line | Data |
---|---|---|
7db7d9f3 | 1 | // SPDX-License-Identifier: GPL-2.0 |
cfa55c6d | 2 | /* Copyright (C) B.A.T.M.A.N. contributors: |
c6c8fea2 SE |
3 | * |
4 | * Marek Lindner | |
c6c8fea2 SE |
5 | */ |
6 | ||
c6c8fea2 | 7 | #include "gateway_client.h" |
1e2c2a4f SE |
8 | #include "main.h" |
9 | ||
10 | #include <linux/atomic.h> | |
11 | #include <linux/byteorder/generic.h> | |
eb7da4f1 | 12 | #include <linux/container_of.h> |
6ecc4fd6 | 13 | #include <linux/err.h> |
d7129daf | 14 | #include <linux/errno.h> |
1e2c2a4f | 15 | #include <linux/etherdevice.h> |
b92b94ac | 16 | #include <linux/gfp.h> |
1e2c2a4f SE |
17 | #include <linux/if_ether.h> |
18 | #include <linux/if_vlan.h> | |
19 | #include <linux/in.h> | |
20 | #include <linux/ip.h> | |
21 | #include <linux/ipv6.h> | |
e7aed321 | 22 | #include <linux/kref.h> |
1e2c2a4f | 23 | #include <linux/list.h> |
dff9bc42 | 24 | #include <linux/lockdep.h> |
1e2c2a4f | 25 | #include <linux/netdevice.h> |
d7129daf | 26 | #include <linux/netlink.h> |
1e2c2a4f SE |
27 | #include <linux/rculist.h> |
28 | #include <linux/rcupdate.h> | |
1e2c2a4f SE |
29 | #include <linux/skbuff.h> |
30 | #include <linux/slab.h> | |
31 | #include <linux/spinlock.h> | |
69f9aff2 | 32 | #include <linux/sprintf.h> |
1e2c2a4f SE |
33 | #include <linux/stddef.h> |
34 | #include <linux/udp.h> | |
fec149f5 | 35 | #include <uapi/linux/batadv_packet.h> |
d7129daf | 36 | #include <uapi/linux/batman_adv.h> |
1e2c2a4f | 37 | |
c6c8fea2 | 38 | #include "hard-interface.h" |
ba412080 | 39 | #include "log.h" |
d7129daf | 40 | #include "netlink.h" |
57f0c07c | 41 | #include "originator.h" |
43676ab5 | 42 | #include "routing.h" |
1e2c2a4f | 43 | #include "translation-table.h" |
c6c8fea2 | 44 | |
6c413b1c AQ |
45 | /* These are the offsets of the "hw type" and "hw address length" in the dhcp |
46 | * packet starting at the beginning of the dhcp header | |
9cfc7bd6 | 47 | */ |
6c413b1c AQ |
48 | #define BATADV_DHCP_HTYPE_OFFSET 1 |
49 | #define BATADV_DHCP_HLEN_OFFSET 2 | |
50 | /* Value of htype representing Ethernet */ | |
51 | #define BATADV_DHCP_HTYPE_ETHERNET 0x01 | |
52 | /* This is the offset of the "chaddr" field in the dhcp packet starting at the | |
53 | * beginning of the dhcp header | |
54 | */ | |
55 | #define BATADV_DHCP_CHADDR_OFFSET 28 | |
43676ab5 | 56 | |
e7aed321 | 57 | /** |
7e9a8c2c SE |
58 | * batadv_gw_node_release() - release gw_node from lists and queue for free |
59 | * after rcu grace period | |
e7aed321 SE |
60 | * @ref: kref pointer of the gw_node |
61 | */ | |
6340dcbd | 62 | void batadv_gw_node_release(struct kref *ref) |
e7aed321 SE |
63 | { |
64 | struct batadv_gw_node *gw_node; | |
65 | ||
66 | gw_node = container_of(ref, struct batadv_gw_node, refcount); | |
67 | ||
5d967310 | 68 | batadv_orig_node_put(gw_node->orig_node); |
e7aed321 SE |
69 | kfree_rcu(gw_node, rcu); |
70 | } | |
71 | ||
ff15c27c SE |
72 | /** |
73 | * batadv_gw_get_selected_gw_node() - Get currently selected gateway | |
94433355 | 74 | * @bat_priv: the bat priv with all the mesh interface information |
ff15c27c SE |
75 | * |
76 | * Return: selected gateway (with increased refcnt), NULL on errors | |
77 | */ | |
34d99cfe | 78 | struct batadv_gw_node * |
56303d34 | 79 | batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv) |
c6c8fea2 | 80 | { |
56303d34 | 81 | struct batadv_gw_node *gw_node; |
c6c8fea2 | 82 | |
5d02b3cd | 83 | rcu_read_lock(); |
807736f6 | 84 | gw_node = rcu_dereference(bat_priv->gw.curr_gw); |
c4aac1ab | 85 | if (!gw_node) |
7b36e8ee | 86 | goto out; |
c6c8fea2 | 87 | |
e7aed321 | 88 | if (!kref_get_unless_zero(&gw_node->refcount)) |
c4aac1ab | 89 | gw_node = NULL; |
43c70ad5 | 90 | |
5d02b3cd LL |
91 | out: |
92 | rcu_read_unlock(); | |
c4aac1ab | 93 | return gw_node; |
c6c8fea2 SE |
94 | } |
95 | ||
ff15c27c SE |
96 | /** |
97 | * batadv_gw_get_selected_orig() - Get originator of currently selected gateway | |
94433355 | 98 | * @bat_priv: the bat priv with all the mesh interface information |
ff15c27c SE |
99 | * |
100 | * Return: orig_node of selected gateway (with increased refcnt), NULL on errors | |
101 | */ | |
56303d34 SE |
102 | struct batadv_orig_node * |
103 | batadv_gw_get_selected_orig(struct batadv_priv *bat_priv) | |
c6c8fea2 | 104 | { |
56303d34 SE |
105 | struct batadv_gw_node *gw_node; |
106 | struct batadv_orig_node *orig_node = NULL; | |
c6c8fea2 | 107 | |
1409a834 | 108 | gw_node = batadv_gw_get_selected_gw_node(bat_priv); |
c4aac1ab ML |
109 | if (!gw_node) |
110 | goto out; | |
111 | ||
112 | rcu_read_lock(); | |
113 | orig_node = gw_node->orig_node; | |
114 | if (!orig_node) | |
115 | goto unlock; | |
116 | ||
7c124391 | 117 | if (!kref_get_unless_zero(&orig_node->refcount)) |
c4aac1ab | 118 | orig_node = NULL; |
c6c8fea2 | 119 | |
c4aac1ab ML |
120 | unlock: |
121 | rcu_read_unlock(); | |
122 | out: | |
79a0bffb | 123 | batadv_gw_node_put(gw_node); |
c4aac1ab | 124 | return orig_node; |
c6c8fea2 SE |
125 | } |
126 | ||
56303d34 SE |
127 | static void batadv_gw_select(struct batadv_priv *bat_priv, |
128 | struct batadv_gw_node *new_gw_node) | |
c6c8fea2 | 129 | { |
56303d34 | 130 | struct batadv_gw_node *curr_gw_node; |
c6c8fea2 | 131 | |
807736f6 | 132 | spin_lock_bh(&bat_priv->gw.list_lock); |
c4aac1ab | 133 | |
a08d497d SE |
134 | if (new_gw_node) |
135 | kref_get(&new_gw_node->refcount); | |
c6c8fea2 | 136 | |
cf78bb0b AQ |
137 | curr_gw_node = rcu_replace_pointer(bat_priv->gw.curr_gw, new_gw_node, |
138 | true); | |
25b6d3c1 | 139 | |
79a0bffb | 140 | batadv_gw_node_put(curr_gw_node); |
c4aac1ab | 141 | |
807736f6 | 142 | spin_unlock_bh(&bat_priv->gw.list_lock); |
c4aac1ab ML |
143 | } |
144 | ||
4e820e72 | 145 | /** |
7e9a8c2c | 146 | * batadv_gw_reselect() - force a gateway reselection |
94433355 | 147 | * @bat_priv: the bat priv with all the mesh interface information |
4e820e72 AQ |
148 | * |
149 | * Set a flag to remind the GW component to perform a new gateway reselection. | |
150 | * However this function does not ensure that the current gateway is going to be | |
151 | * deselected. The reselection mechanism may elect the same gateway once again. | |
152 | * | |
153 | * This means that invoking batadv_gw_reselect() does not guarantee a gateway | |
154 | * change and therefore a uevent is not necessarily expected. | |
155 | */ | |
156 | void batadv_gw_reselect(struct batadv_priv *bat_priv) | |
c4aac1ab | 157 | { |
807736f6 | 158 | atomic_set(&bat_priv->gw.reselect, 1); |
c6c8fea2 SE |
159 | } |
160 | ||
c6eaa3f0 | 161 | /** |
7e9a8c2c | 162 | * batadv_gw_check_client_stop() - check if client mode has been switched off |
94433355 | 163 | * @bat_priv: the bat priv with all the mesh interface information |
c6eaa3f0 AQ |
164 | * |
165 | * This function assumes the caller has checked that the gw state *is actually | |
166 | * changing*. This function is not supposed to be called when there is no state | |
167 | * change. | |
168 | */ | |
169 | void batadv_gw_check_client_stop(struct batadv_priv *bat_priv) | |
170 | { | |
171 | struct batadv_gw_node *curr_gw; | |
172 | ||
3a24a63e | 173 | if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT) |
c6eaa3f0 AQ |
174 | return; |
175 | ||
176 | curr_gw = batadv_gw_get_selected_gw_node(bat_priv); | |
177 | if (!curr_gw) | |
178 | return; | |
179 | ||
f3163181 AQ |
180 | /* deselect the current gateway so that next time that client mode is |
181 | * enabled a proper GW_ADD event can be sent | |
182 | */ | |
183 | batadv_gw_select(bat_priv, NULL); | |
184 | ||
c6eaa3f0 AQ |
185 | /* if batman-adv is switching the gw client mode off and a gateway was |
186 | * already selected, send a DEL uevent | |
187 | */ | |
188 | batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL); | |
189 | ||
3a01743d | 190 | batadv_gw_node_put(curr_gw); |
c6eaa3f0 AQ |
191 | } |
192 | ||
ff15c27c SE |
193 | /** |
194 | * batadv_gw_election() - Elect the best gateway | |
94433355 | 195 | * @bat_priv: the bat priv with all the mesh interface information |
ff15c27c | 196 | */ |
56303d34 | 197 | void batadv_gw_election(struct batadv_priv *bat_priv) |
2265c141 | 198 | { |
4f248cff SE |
199 | struct batadv_gw_node *curr_gw = NULL; |
200 | struct batadv_gw_node *next_gw = NULL; | |
56303d34 | 201 | struct batadv_neigh_node *router = NULL; |
89652331 | 202 | struct batadv_neigh_ifinfo *router_ifinfo = NULL; |
19595e05 | 203 | char gw_addr[18] = { '\0' }; |
2265c141 | 204 | |
3a24a63e | 205 | if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT) |
2265c141 AQ |
206 | goto out; |
207 | ||
34d99cfe AQ |
208 | if (!bat_priv->algo_ops->gw.get_best_gw_node) |
209 | goto out; | |
210 | ||
1409a834 | 211 | curr_gw = batadv_gw_get_selected_gw_node(bat_priv); |
2265c141 | 212 | |
807736f6 | 213 | if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw) |
caa0bf64 ML |
214 | goto out; |
215 | ||
34d99cfe AQ |
216 | /* if gw.reselect is set to 1 it means that a previous call to |
217 | * gw.is_eligible() said that we have a new best GW, therefore it can | |
218 | * now be picked from the list and selected | |
219 | */ | |
220 | next_gw = bat_priv->algo_ops->gw.get_best_gw_node(bat_priv); | |
2265c141 AQ |
221 | |
222 | if (curr_gw == next_gw) | |
223 | goto out; | |
224 | ||
225 | if (next_gw) { | |
19595e05 AQ |
226 | sprintf(gw_addr, "%pM", next_gw->orig_node->orig); |
227 | ||
7351a482 SW |
228 | router = batadv_orig_router_get(next_gw->orig_node, |
229 | BATADV_IF_DEFAULT); | |
2265c141 | 230 | if (!router) { |
4e820e72 | 231 | batadv_gw_reselect(bat_priv); |
2265c141 AQ |
232 | goto out; |
233 | } | |
89652331 SW |
234 | |
235 | router_ifinfo = batadv_neigh_ifinfo_get(router, | |
236 | BATADV_IF_DEFAULT); | |
237 | if (!router_ifinfo) { | |
238 | batadv_gw_reselect(bat_priv); | |
239 | goto out; | |
240 | } | |
c6c8fea2 SE |
241 | } |
242 | ||
825ffe1f | 243 | if (curr_gw && !next_gw) { |
39c75a51 | 244 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 245 | "Removing selected gateway - no gateway in range\n"); |
39c75a51 SE |
246 | batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, |
247 | NULL); | |
825ffe1f | 248 | } else if (!curr_gw && next_gw) { |
39c75a51 | 249 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
414254e3 | 250 | "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n", |
1eda58bf | 251 | next_gw->orig_node->orig, |
414254e3 ML |
252 | next_gw->bandwidth_down / 10, |
253 | next_gw->bandwidth_down % 10, | |
254 | next_gw->bandwidth_up / 10, | |
89652331 SW |
255 | next_gw->bandwidth_up % 10, |
256 | router_ifinfo->bat_iv.tq_avg); | |
39c75a51 SE |
257 | batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD, |
258 | gw_addr); | |
2265c141 | 259 | } else { |
39c75a51 | 260 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
414254e3 | 261 | "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n", |
1eda58bf | 262 | next_gw->orig_node->orig, |
414254e3 ML |
263 | next_gw->bandwidth_down / 10, |
264 | next_gw->bandwidth_down % 10, | |
265 | next_gw->bandwidth_up / 10, | |
89652331 SW |
266 | next_gw->bandwidth_up % 10, |
267 | router_ifinfo->bat_iv.tq_avg); | |
39c75a51 SE |
268 | batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE, |
269 | gw_addr); | |
2265c141 AQ |
270 | } |
271 | ||
1409a834 | 272 | batadv_gw_select(bat_priv, next_gw); |
2265c141 | 273 | |
c4aac1ab | 274 | out: |
79a0bffb SE |
275 | batadv_gw_node_put(curr_gw); |
276 | batadv_gw_node_put(next_gw); | |
277 | batadv_neigh_node_put(router); | |
278 | batadv_neigh_ifinfo_put(router_ifinfo); | |
c6c8fea2 SE |
279 | } |
280 | ||
ff15c27c SE |
281 | /** |
282 | * batadv_gw_check_election() - Elect orig node as best gateway when eligible | |
94433355 | 283 | * @bat_priv: the bat priv with all the mesh interface information |
ff15c27c SE |
284 | * @orig_node: orig node which is to be checked |
285 | */ | |
56303d34 SE |
286 | void batadv_gw_check_election(struct batadv_priv *bat_priv, |
287 | struct batadv_orig_node *orig_node) | |
c6c8fea2 | 288 | { |
56303d34 | 289 | struct batadv_orig_node *curr_gw_orig; |
34d99cfe AQ |
290 | |
291 | /* abort immediately if the routing algorithm does not support gateway | |
292 | * election | |
293 | */ | |
294 | if (!bat_priv->algo_ops->gw.is_eligible) | |
295 | return; | |
c6c8fea2 | 296 | |
7cf06bc6 | 297 | curr_gw_orig = batadv_gw_get_selected_orig(bat_priv); |
57f0c07c | 298 | if (!curr_gw_orig) |
4e820e72 | 299 | goto reselect; |
c6c8fea2 | 300 | |
c6c8fea2 | 301 | /* this node already is the gateway */ |
57f0c07c | 302 | if (curr_gw_orig == orig_node) |
e1a5382f | 303 | goto out; |
c6c8fea2 | 304 | |
34d99cfe AQ |
305 | if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig, |
306 | orig_node)) | |
e1a5382f | 307 | goto out; |
5d02b3cd | 308 | |
4e820e72 AQ |
309 | reselect: |
310 | batadv_gw_reselect(bat_priv); | |
5d02b3cd | 311 | out: |
79a0bffb | 312 | batadv_orig_node_put(curr_gw_orig); |
c6c8fea2 SE |
313 | } |
314 | ||
414254e3 | 315 | /** |
7e9a8c2c | 316 | * batadv_gw_node_add() - add gateway node to list of available gateways |
94433355 | 317 | * @bat_priv: the bat priv with all the mesh interface information |
414254e3 ML |
318 | * @orig_node: originator announcing gateway capabilities |
319 | * @gateway: announced bandwidth information | |
dff9bc42 SE |
320 | * |
321 | * Has to be called with the appropriate locks being acquired | |
322 | * (gw.list_lock). | |
414254e3 | 323 | */ |
56303d34 SE |
324 | static void batadv_gw_node_add(struct batadv_priv *bat_priv, |
325 | struct batadv_orig_node *orig_node, | |
414254e3 | 326 | struct batadv_tvlv_gateway_data *gateway) |
c6c8fea2 | 327 | { |
56303d34 | 328 | struct batadv_gw_node *gw_node; |
414254e3 | 329 | |
dff9bc42 SE |
330 | lockdep_assert_held(&bat_priv->gw.list_lock); |
331 | ||
414254e3 ML |
332 | if (gateway->bandwidth_down == 0) |
333 | return; | |
c6c8fea2 | 334 | |
704509b8 | 335 | gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC); |
c3ba37a7 | 336 | if (!gw_node) |
c6c8fea2 SE |
337 | return; |
338 | ||
f665fa7e | 339 | kref_init(&gw_node->refcount); |
c6c8fea2 | 340 | INIT_HLIST_NODE(&gw_node->list); |
55db2d59 | 341 | kref_get(&orig_node->refcount); |
c6c8fea2 | 342 | gw_node->orig_node = orig_node; |
27a4d5ef SW |
343 | gw_node->bandwidth_down = ntohl(gateway->bandwidth_down); |
344 | gw_node->bandwidth_up = ntohl(gateway->bandwidth_up); | |
c6c8fea2 | 345 | |
f665fa7e | 346 | kref_get(&gw_node->refcount); |
70ea5cee | 347 | hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.gateway_list); |
9264c85c | 348 | bat_priv->gw.generation++; |
c6c8fea2 | 349 | |
39c75a51 | 350 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
414254e3 ML |
351 | "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n", |
352 | orig_node->orig, | |
353 | ntohl(gateway->bandwidth_down) / 10, | |
354 | ntohl(gateway->bandwidth_down) % 10, | |
355 | ntohl(gateway->bandwidth_up) / 10, | |
356 | ntohl(gateway->bandwidth_up) % 10); | |
f665fa7e SE |
357 | |
358 | /* don't return reference to new gw_node */ | |
359 | batadv_gw_node_put(gw_node); | |
c6c8fea2 SE |
360 | } |
361 | ||
414254e3 | 362 | /** |
7e9a8c2c | 363 | * batadv_gw_node_get() - retrieve gateway node from list of available gateways |
94433355 | 364 | * @bat_priv: the bat priv with all the mesh interface information |
414254e3 ML |
365 | * @orig_node: originator announcing gateway capabilities |
366 | * | |
62fe710f | 367 | * Return: gateway node if found or NULL otherwise. |
414254e3 | 368 | */ |
50164d8f AQ |
369 | struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv, |
370 | struct batadv_orig_node *orig_node) | |
c6c8fea2 | 371 | { |
414254e3 | 372 | struct batadv_gw_node *gw_node_tmp, *gw_node = NULL; |
c6c8fea2 SE |
373 | |
374 | rcu_read_lock(); | |
70ea5cee SE |
375 | hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.gateway_list, |
376 | list) { | |
414254e3 | 377 | if (gw_node_tmp->orig_node != orig_node) |
c6c8fea2 SE |
378 | continue; |
379 | ||
e7aed321 | 380 | if (!kref_get_unless_zero(&gw_node_tmp->refcount)) |
414254e3 | 381 | continue; |
c6c8fea2 | 382 | |
414254e3 ML |
383 | gw_node = gw_node_tmp; |
384 | break; | |
385 | } | |
386 | rcu_read_unlock(); | |
c6c8fea2 | 387 | |
414254e3 ML |
388 | return gw_node; |
389 | } | |
c6c8fea2 | 390 | |
414254e3 | 391 | /** |
7e9a8c2c | 392 | * batadv_gw_node_update() - update list of available gateways with changed |
414254e3 | 393 | * bandwidth information |
94433355 | 394 | * @bat_priv: the bat priv with all the mesh interface information |
414254e3 ML |
395 | * @orig_node: originator announcing gateway capabilities |
396 | * @gateway: announced bandwidth information | |
397 | */ | |
398 | void batadv_gw_node_update(struct batadv_priv *bat_priv, | |
399 | struct batadv_orig_node *orig_node, | |
400 | struct batadv_tvlv_gateway_data *gateway) | |
401 | { | |
402 | struct batadv_gw_node *gw_node, *curr_gw = NULL; | |
403 | ||
dff9bc42 | 404 | spin_lock_bh(&bat_priv->gw.list_lock); |
414254e3 ML |
405 | gw_node = batadv_gw_node_get(bat_priv, orig_node); |
406 | if (!gw_node) { | |
407 | batadv_gw_node_add(bat_priv, orig_node, gateway); | |
dff9bc42 | 408 | spin_unlock_bh(&bat_priv->gw.list_lock); |
414254e3 | 409 | goto out; |
c6c8fea2 | 410 | } |
dff9bc42 | 411 | spin_unlock_bh(&bat_priv->gw.list_lock); |
c6c8fea2 | 412 | |
825ffe1f SE |
413 | if (gw_node->bandwidth_down == ntohl(gateway->bandwidth_down) && |
414 | gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)) | |
414254e3 | 415 | goto out; |
c6c8fea2 | 416 | |
414254e3 ML |
417 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
418 | "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n", | |
419 | orig_node->orig, | |
420 | gw_node->bandwidth_down / 10, | |
421 | gw_node->bandwidth_down % 10, | |
422 | gw_node->bandwidth_up / 10, | |
423 | gw_node->bandwidth_up % 10, | |
424 | ntohl(gateway->bandwidth_down) / 10, | |
425 | ntohl(gateway->bandwidth_down) % 10, | |
426 | ntohl(gateway->bandwidth_up) / 10, | |
427 | ntohl(gateway->bandwidth_up) % 10); | |
428 | ||
429 | gw_node->bandwidth_down = ntohl(gateway->bandwidth_down); | |
430 | gw_node->bandwidth_up = ntohl(gateway->bandwidth_up); | |
431 | ||
414254e3 | 432 | if (ntohl(gateway->bandwidth_down) == 0) { |
414254e3 ML |
433 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
434 | "Gateway %pM removed from gateway list\n", | |
435 | orig_node->orig); | |
c4aac1ab | 436 | |
414254e3 ML |
437 | /* Note: We don't need a NULL check here, since curr_gw never |
438 | * gets dereferenced. | |
439 | */ | |
bd3524c1 | 440 | spin_lock_bh(&bat_priv->gw.list_lock); |
c18bdd01 SE |
441 | if (!hlist_unhashed(&gw_node->list)) { |
442 | hlist_del_init_rcu(&gw_node->list); | |
3a01743d | 443 | batadv_gw_node_put(gw_node); |
9264c85c | 444 | bat_priv->gw.generation++; |
c18bdd01 | 445 | } |
bd3524c1 SW |
446 | spin_unlock_bh(&bat_priv->gw.list_lock); |
447 | ||
414254e3 ML |
448 | curr_gw = batadv_gw_get_selected_gw_node(bat_priv); |
449 | if (gw_node == curr_gw) | |
4e820e72 | 450 | batadv_gw_reselect(bat_priv); |
bd3524c1 | 451 | |
79a0bffb | 452 | batadv_gw_node_put(curr_gw); |
414254e3 | 453 | } |
71e4aa9c | 454 | |
414254e3 | 455 | out: |
79a0bffb | 456 | batadv_gw_node_put(gw_node); |
c6c8fea2 SE |
457 | } |
458 | ||
ff15c27c SE |
459 | /** |
460 | * batadv_gw_node_delete() - Remove orig_node from gateway list | |
94433355 | 461 | * @bat_priv: the bat priv with all the mesh interface information |
ff15c27c SE |
462 | * @orig_node: orig node which is currently in process of being removed |
463 | */ | |
56303d34 SE |
464 | void batadv_gw_node_delete(struct batadv_priv *bat_priv, |
465 | struct batadv_orig_node *orig_node) | |
c6c8fea2 | 466 | { |
414254e3 ML |
467 | struct batadv_tvlv_gateway_data gateway; |
468 | ||
469 | gateway.bandwidth_down = 0; | |
470 | gateway.bandwidth_up = 0; | |
471 | ||
472 | batadv_gw_node_update(bat_priv, orig_node, &gateway); | |
c6c8fea2 SE |
473 | } |
474 | ||
ff15c27c | 475 | /** |
94433355 SE |
476 | * batadv_gw_node_free() - Free gateway information from mesh interface |
477 | * @bat_priv: the bat priv with all the mesh interface information | |
ff15c27c | 478 | */ |
bd3524c1 | 479 | void batadv_gw_node_free(struct batadv_priv *bat_priv) |
c6c8fea2 | 480 | { |
bd3524c1 | 481 | struct batadv_gw_node *gw_node; |
b67bfe0d | 482 | struct hlist_node *node_tmp; |
c6c8fea2 | 483 | |
807736f6 | 484 | spin_lock_bh(&bat_priv->gw.list_lock); |
b67bfe0d | 485 | hlist_for_each_entry_safe(gw_node, node_tmp, |
70ea5cee | 486 | &bat_priv->gw.gateway_list, list) { |
bd3524c1 | 487 | hlist_del_init_rcu(&gw_node->list); |
3a01743d | 488 | batadv_gw_node_put(gw_node); |
9264c85c | 489 | bat_priv->gw.generation++; |
c6c8fea2 | 490 | } |
807736f6 | 491 | spin_unlock_bh(&bat_priv->gw.list_lock); |
c6c8fea2 SE |
492 | } |
493 | ||
d7129daf | 494 | /** |
7e9a8c2c | 495 | * batadv_gw_dump() - Dump gateways into a message |
d7129daf SE |
496 | * @msg: Netlink message to dump into |
497 | * @cb: Control block containing additional options | |
498 | * | |
499 | * Return: Error code, or length of message | |
500 | */ | |
501 | int batadv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb) | |
502 | { | |
503 | struct batadv_hard_iface *primary_if = NULL; | |
94433355 | 504 | struct net_device *mesh_iface; |
d7129daf | 505 | struct batadv_priv *bat_priv; |
d7129daf SE |
506 | int ret; |
507 | ||
94433355 SE |
508 | mesh_iface = batadv_netlink_get_meshif(cb); |
509 | if (IS_ERR(mesh_iface)) | |
510 | return PTR_ERR(mesh_iface); | |
d7129daf | 511 | |
94433355 | 512 | bat_priv = netdev_priv(mesh_iface); |
d7129daf SE |
513 | |
514 | primary_if = batadv_primary_if_get_selected(bat_priv); | |
515 | if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) { | |
516 | ret = -ENOENT; | |
517 | goto out; | |
518 | } | |
519 | ||
520 | if (!bat_priv->algo_ops->gw.dump) { | |
521 | ret = -EOPNOTSUPP; | |
522 | goto out; | |
523 | } | |
524 | ||
525 | bat_priv->algo_ops->gw.dump(msg, cb, bat_priv); | |
526 | ||
527 | ret = msg->len; | |
528 | ||
529 | out: | |
79a0bffb | 530 | batadv_hardif_put(primary_if); |
94433355 | 531 | dev_put(mesh_iface); |
d7129daf SE |
532 | |
533 | return ret; | |
534 | } | |
535 | ||
6c413b1c | 536 | /** |
7e9a8c2c | 537 | * batadv_gw_dhcp_recipient_get() - check if a packet is a DHCP message |
6c413b1c AQ |
538 | * @skb: the packet to check |
539 | * @header_len: a pointer to the batman-adv header size | |
540 | * @chaddr: buffer where the client address will be stored. Valid | |
541 | * only if the function returns BATADV_DHCP_TO_CLIENT | |
542 | * | |
62fe710f SE |
543 | * This function may re-allocate the data buffer of the skb passed as argument. |
544 | * | |
545 | * Return: | |
6c413b1c AQ |
546 | * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error |
547 | * while parsing it | |
548 | * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server | |
549 | * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client | |
6c413b1c AQ |
550 | */ |
551 | enum batadv_dhcp_recipient | |
552 | batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len, | |
6b5e971a | 553 | u8 *chaddr) |
c6c8fea2 | 554 | { |
6c413b1c | 555 | enum batadv_dhcp_recipient ret = BATADV_DHCP_NO; |
c6c8fea2 SE |
556 | struct ethhdr *ethhdr; |
557 | struct iphdr *iphdr; | |
558 | struct ipv6hdr *ipv6hdr; | |
559 | struct udphdr *udphdr; | |
f7f8ed56 | 560 | struct vlan_ethhdr *vhdr; |
6c413b1c | 561 | int chaddr_offset; |
f7f8ed56 | 562 | __be16 proto; |
6b5e971a | 563 | u8 *p; |
c6c8fea2 SE |
564 | |
565 | /* check for ethernet header */ | |
be7af5cf | 566 | if (!pskb_may_pull(skb, *header_len + ETH_HLEN)) |
6c413b1c AQ |
567 | return BATADV_DHCP_NO; |
568 | ||
927c2ed7 | 569 | ethhdr = eth_hdr(skb); |
f7f8ed56 | 570 | proto = ethhdr->h_proto; |
be7af5cf | 571 | *header_len += ETH_HLEN; |
c6c8fea2 SE |
572 | |
573 | /* check for initial vlan header */ | |
f7f8ed56 | 574 | if (proto == htons(ETH_P_8021Q)) { |
be7af5cf | 575 | if (!pskb_may_pull(skb, *header_len + VLAN_HLEN)) |
6c413b1c | 576 | return BATADV_DHCP_NO; |
f7f8ed56 | 577 | |
927c2ed7 | 578 | vhdr = vlan_eth_hdr(skb); |
f7f8ed56 | 579 | proto = vhdr->h_vlan_encapsulated_proto; |
be7af5cf | 580 | *header_len += VLAN_HLEN; |
c6c8fea2 SE |
581 | } |
582 | ||
583 | /* check for ip header */ | |
f7f8ed56 AQ |
584 | switch (proto) { |
585 | case htons(ETH_P_IP): | |
be7af5cf | 586 | if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr))) |
6c413b1c AQ |
587 | return BATADV_DHCP_NO; |
588 | ||
be7af5cf ML |
589 | iphdr = (struct iphdr *)(skb->data + *header_len); |
590 | *header_len += iphdr->ihl * 4; | |
c6c8fea2 SE |
591 | |
592 | /* check for udp header */ | |
593 | if (iphdr->protocol != IPPROTO_UDP) | |
6c413b1c | 594 | return BATADV_DHCP_NO; |
c6c8fea2 SE |
595 | |
596 | break; | |
f7f8ed56 | 597 | case htons(ETH_P_IPV6): |
be7af5cf | 598 | if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr))) |
6c413b1c AQ |
599 | return BATADV_DHCP_NO; |
600 | ||
be7af5cf ML |
601 | ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len); |
602 | *header_len += sizeof(*ipv6hdr); | |
c6c8fea2 SE |
603 | |
604 | /* check for udp header */ | |
605 | if (ipv6hdr->nexthdr != IPPROTO_UDP) | |
6c413b1c | 606 | return BATADV_DHCP_NO; |
c6c8fea2 SE |
607 | |
608 | break; | |
609 | default: | |
6c413b1c | 610 | return BATADV_DHCP_NO; |
c6c8fea2 SE |
611 | } |
612 | ||
be7af5cf | 613 | if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr))) |
6c413b1c | 614 | return BATADV_DHCP_NO; |
9d2c9488 | 615 | |
be7af5cf ML |
616 | udphdr = (struct udphdr *)(skb->data + *header_len); |
617 | *header_len += sizeof(*udphdr); | |
c6c8fea2 SE |
618 | |
619 | /* check for bootp port */ | |
6c413b1c AQ |
620 | switch (proto) { |
621 | case htons(ETH_P_IP): | |
622 | if (udphdr->dest == htons(67)) | |
623 | ret = BATADV_DHCP_TO_SERVER; | |
624 | else if (udphdr->source == htons(67)) | |
625 | ret = BATADV_DHCP_TO_CLIENT; | |
626 | break; | |
627 | case htons(ETH_P_IPV6): | |
628 | if (udphdr->dest == htons(547)) | |
629 | ret = BATADV_DHCP_TO_SERVER; | |
630 | else if (udphdr->source == htons(547)) | |
631 | ret = BATADV_DHCP_TO_CLIENT; | |
632 | break; | |
633 | } | |
c6c8fea2 | 634 | |
6c413b1c AQ |
635 | chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET; |
636 | /* store the client address if the message is going to a client */ | |
303216e7 SE |
637 | if (ret == BATADV_DHCP_TO_CLIENT) { |
638 | if (!pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) | |
639 | return BATADV_DHCP_NO; | |
640 | ||
6c413b1c AQ |
641 | /* check if the DHCP packet carries an Ethernet DHCP */ |
642 | p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET; | |
643 | if (*p != BATADV_DHCP_HTYPE_ETHERNET) | |
644 | return BATADV_DHCP_NO; | |
645 | ||
646 | /* check if the DHCP packet carries a valid Ethernet address */ | |
647 | p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET; | |
648 | if (*p != ETH_ALEN) | |
649 | return BATADV_DHCP_NO; | |
650 | ||
8fdd0153 | 651 | ether_addr_copy(chaddr, skb->data + chaddr_offset); |
6c413b1c | 652 | } |
c6c8fea2 | 653 | |
6c413b1c | 654 | return ret; |
be7af5cf | 655 | } |
aa143d28 | 656 | |
bbb877ed | 657 | /** |
7e9a8c2c SE |
658 | * batadv_gw_out_of_range() - check if the dhcp request destination is the best |
659 | * gateway | |
94433355 | 660 | * @bat_priv: the bat priv with all the mesh interface information |
bbb877ed AQ |
661 | * @skb: the outgoing packet |
662 | * | |
663 | * Check if the skb is a DHCP request and if it is sent to the current best GW | |
664 | * server. Due to topology changes it may be the case that the GW server | |
665 | * previously selected is not the best one anymore. | |
666 | * | |
bbb877ed | 667 | * This call might reallocate skb data. |
6c413b1c | 668 | * Must be invoked only when the DHCP packet is going TO a DHCP SERVER. |
62fe710f SE |
669 | * |
670 | * Return: true if the packet destination is unicast and it is not the best gw, | |
671 | * false otherwise. | |
bbb877ed | 672 | */ |
56303d34 | 673 | bool batadv_gw_out_of_range(struct batadv_priv *bat_priv, |
9d2c9488 | 674 | struct sk_buff *skb) |
be7af5cf | 675 | { |
4f248cff SE |
676 | struct batadv_neigh_node *neigh_curr = NULL; |
677 | struct batadv_neigh_node *neigh_old = NULL; | |
a752c0a4 | 678 | struct batadv_orig_node *orig_dst_node = NULL; |
4f248cff SE |
679 | struct batadv_gw_node *gw_node = NULL; |
680 | struct batadv_gw_node *curr_gw = NULL; | |
89652331 | 681 | struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo; |
6c413b1c AQ |
682 | struct ethhdr *ethhdr = (struct ethhdr *)skb->data; |
683 | bool out_of_range = false; | |
6b5e971a | 684 | u8 curr_tq_avg; |
bbb877ed AQ |
685 | unsigned short vid; |
686 | ||
687 | vid = batadv_get_vid(skb, 0); | |
be7af5cf | 688 | |
a752c0a4 LL |
689 | if (is_multicast_ether_addr(ethhdr->h_dest)) |
690 | goto out; | |
691 | ||
08c36d3e | 692 | orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source, |
bbb877ed | 693 | ethhdr->h_dest, vid); |
be7af5cf ML |
694 | if (!orig_dst_node) |
695 | goto out; | |
696 | ||
414254e3 | 697 | gw_node = batadv_gw_node_get(bat_priv, orig_dst_node); |
0d164491 | 698 | if (!gw_node) |
be7af5cf ML |
699 | goto out; |
700 | ||
3a24a63e | 701 | switch (atomic_read(&bat_priv->gw.mode)) { |
cd646ab1 | 702 | case BATADV_GW_MODE_SERVER: |
be7af5cf | 703 | /* If we are a GW then we are our best GW. We can artificially |
9cfc7bd6 SE |
704 | * set the tq towards ourself as the maximum value |
705 | */ | |
42d0b044 | 706 | curr_tq_avg = BATADV_TQ_MAX_VALUE; |
be7af5cf | 707 | break; |
cd646ab1 | 708 | case BATADV_GW_MODE_CLIENT: |
1409a834 | 709 | curr_gw = batadv_gw_get_selected_gw_node(bat_priv); |
be7af5cf ML |
710 | if (!curr_gw) |
711 | goto out; | |
712 | ||
713 | /* packet is going to our gateway */ | |
714 | if (curr_gw->orig_node == orig_dst_node) | |
715 | goto out; | |
716 | ||
717 | /* If the dhcp packet has been sent to a different gw, | |
718 | * we have to evaluate whether the old gw is still | |
9cfc7bd6 SE |
719 | * reliable enough |
720 | */ | |
30d3c511 SE |
721 | neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node, |
722 | NULL); | |
be7af5cf ML |
723 | if (!neigh_curr) |
724 | goto out; | |
725 | ||
89652331 SW |
726 | curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr, |
727 | BATADV_IF_DEFAULT); | |
728 | if (!curr_ifinfo) | |
729 | goto out; | |
730 | ||
731 | curr_tq_avg = curr_ifinfo->bat_iv.tq_avg; | |
044fa3ae | 732 | batadv_neigh_ifinfo_put(curr_ifinfo); |
89652331 | 733 | |
be7af5cf | 734 | break; |
cd646ab1 | 735 | case BATADV_GW_MODE_OFF: |
be7af5cf ML |
736 | default: |
737 | goto out; | |
43676ab5 | 738 | } |
be7af5cf | 739 | |
30d3c511 | 740 | neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL); |
2ef04f47 | 741 | if (!neigh_old) |
be7af5cf ML |
742 | goto out; |
743 | ||
89652331 SW |
744 | old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT); |
745 | if (!old_ifinfo) | |
746 | goto out; | |
747 | ||
748 | if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD) | |
be7af5cf | 749 | out_of_range = true; |
044fa3ae | 750 | batadv_neigh_ifinfo_put(old_ifinfo); |
be7af5cf ML |
751 | |
752 | out: | |
79a0bffb SE |
753 | batadv_orig_node_put(orig_dst_node); |
754 | batadv_gw_node_put(curr_gw); | |
755 | batadv_gw_node_put(gw_node); | |
756 | batadv_neigh_node_put(neigh_old); | |
757 | batadv_neigh_node_put(neigh_curr); | |
be7af5cf | 758 | return out_of_range; |
c6c8fea2 | 759 | } |