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