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