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