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