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