measure ping latency including qos_resume delay
[cor.git] / net / batman-adv / gateway_client.c
blob47df4c678988613f7410acb96889558eabdf396d
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2009-2019 B.A.T.M.A.N. contributors:
4 * Marek Lindner
5 */
7 #include "gateway_client.h"
8 #include "main.h"
10 #include <linux/atomic.h>
11 #include <linux/byteorder/generic.h>
12 #include <linux/errno.h>
13 #include <linux/etherdevice.h>
14 #include <linux/gfp.h>
15 #include <linux/if_ether.h>
16 #include <linux/if_vlan.h>
17 #include <linux/in.h>
18 #include <linux/ip.h>
19 #include <linux/ipv6.h>
20 #include <linux/kernel.h>
21 #include <linux/kref.h>
22 #include <linux/list.h>
23 #include <linux/lockdep.h>
24 #include <linux/netdevice.h>
25 #include <linux/netlink.h>
26 #include <linux/rculist.h>
27 #include <linux/rcupdate.h>
28 #include <linux/seq_file.h>
29 #include <linux/skbuff.h>
30 #include <linux/slab.h>
31 #include <linux/spinlock.h>
32 #include <linux/stddef.h>
33 #include <linux/udp.h>
34 #include <net/sock.h>
35 #include <uapi/linux/batadv_packet.h>
36 #include <uapi/linux/batman_adv.h>
38 #include "hard-interface.h"
39 #include "log.h"
40 #include "netlink.h"
41 #include "originator.h"
42 #include "routing.h"
43 #include "soft-interface.h"
44 #include "translation-table.h"
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
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
56 #define BATADV_DHCP_CHADDR_OFFSET 28
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
63 static void batadv_gw_node_release(struct kref *ref)
65 struct batadv_gw_node *gw_node;
67 gw_node = container_of(ref, struct batadv_gw_node, refcount);
69 batadv_orig_node_put(gw_node->orig_node);
70 kfree_rcu(gw_node, rcu);
73 /**
74 * batadv_gw_node_put() - decrement the gw_node refcounter and possibly release
75 * it
76 * @gw_node: gateway node to free
78 void batadv_gw_node_put(struct batadv_gw_node *gw_node)
80 kref_put(&gw_node->refcount, batadv_gw_node_release);
83 /**
84 * batadv_gw_get_selected_gw_node() - Get currently selected gateway
85 * @bat_priv: the bat priv with all the soft interface information
87 * Return: selected gateway (with increased refcnt), NULL on errors
89 struct batadv_gw_node *
90 batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
92 struct batadv_gw_node *gw_node;
94 rcu_read_lock();
95 gw_node = rcu_dereference(bat_priv->gw.curr_gw);
96 if (!gw_node)
97 goto out;
99 if (!kref_get_unless_zero(&gw_node->refcount))
100 gw_node = NULL;
102 out:
103 rcu_read_unlock();
104 return gw_node;
108 * batadv_gw_get_selected_orig() - Get originator of currently selected gateway
109 * @bat_priv: the bat priv with all the soft interface information
111 * Return: orig_node of selected gateway (with increased refcnt), NULL on errors
113 struct batadv_orig_node *
114 batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
116 struct batadv_gw_node *gw_node;
117 struct batadv_orig_node *orig_node = NULL;
119 gw_node = batadv_gw_get_selected_gw_node(bat_priv);
120 if (!gw_node)
121 goto out;
123 rcu_read_lock();
124 orig_node = gw_node->orig_node;
125 if (!orig_node)
126 goto unlock;
128 if (!kref_get_unless_zero(&orig_node->refcount))
129 orig_node = NULL;
131 unlock:
132 rcu_read_unlock();
133 out:
134 if (gw_node)
135 batadv_gw_node_put(gw_node);
136 return orig_node;
139 static void batadv_gw_select(struct batadv_priv *bat_priv,
140 struct batadv_gw_node *new_gw_node)
142 struct batadv_gw_node *curr_gw_node;
144 spin_lock_bh(&bat_priv->gw.list_lock);
146 if (new_gw_node)
147 kref_get(&new_gw_node->refcount);
149 curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1);
150 rcu_assign_pointer(bat_priv->gw.curr_gw, new_gw_node);
152 if (curr_gw_node)
153 batadv_gw_node_put(curr_gw_node);
155 spin_unlock_bh(&bat_priv->gw.list_lock);
159 * batadv_gw_reselect() - force a gateway reselection
160 * @bat_priv: the bat priv with all the soft interface information
162 * Set a flag to remind the GW component to perform a new gateway reselection.
163 * However this function does not ensure that the current gateway is going to be
164 * deselected. The reselection mechanism may elect the same gateway once again.
166 * This means that invoking batadv_gw_reselect() does not guarantee a gateway
167 * change and therefore a uevent is not necessarily expected.
169 void batadv_gw_reselect(struct batadv_priv *bat_priv)
171 atomic_set(&bat_priv->gw.reselect, 1);
175 * batadv_gw_check_client_stop() - check if client mode has been switched off
176 * @bat_priv: the bat priv with all the soft interface information
178 * This function assumes the caller has checked that the gw state *is actually
179 * changing*. This function is not supposed to be called when there is no state
180 * change.
182 void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
184 struct batadv_gw_node *curr_gw;
186 if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
187 return;
189 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
190 if (!curr_gw)
191 return;
193 /* deselect the current gateway so that next time that client mode is
194 * enabled a proper GW_ADD event can be sent
196 batadv_gw_select(bat_priv, NULL);
198 /* if batman-adv is switching the gw client mode off and a gateway was
199 * already selected, send a DEL uevent
201 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
203 batadv_gw_node_put(curr_gw);
207 * batadv_gw_election() - Elect the best gateway
208 * @bat_priv: the bat priv with all the soft interface information
210 void batadv_gw_election(struct batadv_priv *bat_priv)
212 struct batadv_gw_node *curr_gw = NULL;
213 struct batadv_gw_node *next_gw = NULL;
214 struct batadv_neigh_node *router = NULL;
215 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
216 char gw_addr[18] = { '\0' };
218 if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
219 goto out;
221 if (!bat_priv->algo_ops->gw.get_best_gw_node)
222 goto out;
224 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
226 if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
227 goto out;
229 /* if gw.reselect is set to 1 it means that a previous call to
230 * gw.is_eligible() said that we have a new best GW, therefore it can
231 * now be picked from the list and selected
233 next_gw = bat_priv->algo_ops->gw.get_best_gw_node(bat_priv);
235 if (curr_gw == next_gw)
236 goto out;
238 if (next_gw) {
239 sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
241 router = batadv_orig_router_get(next_gw->orig_node,
242 BATADV_IF_DEFAULT);
243 if (!router) {
244 batadv_gw_reselect(bat_priv);
245 goto out;
248 router_ifinfo = batadv_neigh_ifinfo_get(router,
249 BATADV_IF_DEFAULT);
250 if (!router_ifinfo) {
251 batadv_gw_reselect(bat_priv);
252 goto out;
256 if (curr_gw && !next_gw) {
257 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
258 "Removing selected gateway - no gateway in range\n");
259 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
260 NULL);
261 } else if (!curr_gw && next_gw) {
262 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
263 "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
264 next_gw->orig_node->orig,
265 next_gw->bandwidth_down / 10,
266 next_gw->bandwidth_down % 10,
267 next_gw->bandwidth_up / 10,
268 next_gw->bandwidth_up % 10,
269 router_ifinfo->bat_iv.tq_avg);
270 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
271 gw_addr);
272 } else {
273 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
274 "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
275 next_gw->orig_node->orig,
276 next_gw->bandwidth_down / 10,
277 next_gw->bandwidth_down % 10,
278 next_gw->bandwidth_up / 10,
279 next_gw->bandwidth_up % 10,
280 router_ifinfo->bat_iv.tq_avg);
281 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
282 gw_addr);
285 batadv_gw_select(bat_priv, next_gw);
287 out:
288 if (curr_gw)
289 batadv_gw_node_put(curr_gw);
290 if (next_gw)
291 batadv_gw_node_put(next_gw);
292 if (router)
293 batadv_neigh_node_put(router);
294 if (router_ifinfo)
295 batadv_neigh_ifinfo_put(router_ifinfo);
299 * batadv_gw_check_election() - Elect orig node as best gateway when eligible
300 * @bat_priv: the bat priv with all the soft interface information
301 * @orig_node: orig node which is to be checked
303 void batadv_gw_check_election(struct batadv_priv *bat_priv,
304 struct batadv_orig_node *orig_node)
306 struct batadv_orig_node *curr_gw_orig;
308 /* abort immediately if the routing algorithm does not support gateway
309 * election
311 if (!bat_priv->algo_ops->gw.is_eligible)
312 return;
314 curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
315 if (!curr_gw_orig)
316 goto reselect;
318 /* this node already is the gateway */
319 if (curr_gw_orig == orig_node)
320 goto out;
322 if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig,
323 orig_node))
324 goto out;
326 reselect:
327 batadv_gw_reselect(bat_priv);
328 out:
329 if (curr_gw_orig)
330 batadv_orig_node_put(curr_gw_orig);
334 * batadv_gw_node_add() - add gateway node to list of available gateways
335 * @bat_priv: the bat priv with all the soft interface information
336 * @orig_node: originator announcing gateway capabilities
337 * @gateway: announced bandwidth information
339 * Has to be called with the appropriate locks being acquired
340 * (gw.list_lock).
342 static void batadv_gw_node_add(struct batadv_priv *bat_priv,
343 struct batadv_orig_node *orig_node,
344 struct batadv_tvlv_gateway_data *gateway)
346 struct batadv_gw_node *gw_node;
348 lockdep_assert_held(&bat_priv->gw.list_lock);
350 if (gateway->bandwidth_down == 0)
351 return;
353 gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
354 if (!gw_node)
355 return;
357 kref_init(&gw_node->refcount);
358 INIT_HLIST_NODE(&gw_node->list);
359 kref_get(&orig_node->refcount);
360 gw_node->orig_node = orig_node;
361 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
362 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
364 kref_get(&gw_node->refcount);
365 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.gateway_list);
366 bat_priv->gw.generation++;
368 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
369 "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
370 orig_node->orig,
371 ntohl(gateway->bandwidth_down) / 10,
372 ntohl(gateway->bandwidth_down) % 10,
373 ntohl(gateway->bandwidth_up) / 10,
374 ntohl(gateway->bandwidth_up) % 10);
376 /* don't return reference to new gw_node */
377 batadv_gw_node_put(gw_node);
381 * batadv_gw_node_get() - retrieve gateway node from list of available gateways
382 * @bat_priv: the bat priv with all the soft interface information
383 * @orig_node: originator announcing gateway capabilities
385 * Return: gateway node if found or NULL otherwise.
387 struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv,
388 struct batadv_orig_node *orig_node)
390 struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
392 rcu_read_lock();
393 hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.gateway_list,
394 list) {
395 if (gw_node_tmp->orig_node != orig_node)
396 continue;
398 if (!kref_get_unless_zero(&gw_node_tmp->refcount))
399 continue;
401 gw_node = gw_node_tmp;
402 break;
404 rcu_read_unlock();
406 return gw_node;
410 * batadv_gw_node_update() - update list of available gateways with changed
411 * bandwidth information
412 * @bat_priv: the bat priv with all the soft interface information
413 * @orig_node: originator announcing gateway capabilities
414 * @gateway: announced bandwidth information
416 void batadv_gw_node_update(struct batadv_priv *bat_priv,
417 struct batadv_orig_node *orig_node,
418 struct batadv_tvlv_gateway_data *gateway)
420 struct batadv_gw_node *gw_node, *curr_gw = NULL;
422 spin_lock_bh(&bat_priv->gw.list_lock);
423 gw_node = batadv_gw_node_get(bat_priv, orig_node);
424 if (!gw_node) {
425 batadv_gw_node_add(bat_priv, orig_node, gateway);
426 spin_unlock_bh(&bat_priv->gw.list_lock);
427 goto out;
429 spin_unlock_bh(&bat_priv->gw.list_lock);
431 if (gw_node->bandwidth_down == ntohl(gateway->bandwidth_down) &&
432 gw_node->bandwidth_up == ntohl(gateway->bandwidth_up))
433 goto out;
435 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
436 "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
437 orig_node->orig,
438 gw_node->bandwidth_down / 10,
439 gw_node->bandwidth_down % 10,
440 gw_node->bandwidth_up / 10,
441 gw_node->bandwidth_up % 10,
442 ntohl(gateway->bandwidth_down) / 10,
443 ntohl(gateway->bandwidth_down) % 10,
444 ntohl(gateway->bandwidth_up) / 10,
445 ntohl(gateway->bandwidth_up) % 10);
447 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
448 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
450 if (ntohl(gateway->bandwidth_down) == 0) {
451 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
452 "Gateway %pM removed from gateway list\n",
453 orig_node->orig);
455 /* Note: We don't need a NULL check here, since curr_gw never
456 * gets dereferenced.
458 spin_lock_bh(&bat_priv->gw.list_lock);
459 if (!hlist_unhashed(&gw_node->list)) {
460 hlist_del_init_rcu(&gw_node->list);
461 batadv_gw_node_put(gw_node);
462 bat_priv->gw.generation++;
464 spin_unlock_bh(&bat_priv->gw.list_lock);
466 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
467 if (gw_node == curr_gw)
468 batadv_gw_reselect(bat_priv);
470 if (curr_gw)
471 batadv_gw_node_put(curr_gw);
474 out:
475 if (gw_node)
476 batadv_gw_node_put(gw_node);
480 * batadv_gw_node_delete() - Remove orig_node from gateway list
481 * @bat_priv: the bat priv with all the soft interface information
482 * @orig_node: orig node which is currently in process of being removed
484 void batadv_gw_node_delete(struct batadv_priv *bat_priv,
485 struct batadv_orig_node *orig_node)
487 struct batadv_tvlv_gateway_data gateway;
489 gateway.bandwidth_down = 0;
490 gateway.bandwidth_up = 0;
492 batadv_gw_node_update(bat_priv, orig_node, &gateway);
496 * batadv_gw_node_free() - Free gateway information from soft interface
497 * @bat_priv: the bat priv with all the soft interface information
499 void batadv_gw_node_free(struct batadv_priv *bat_priv)
501 struct batadv_gw_node *gw_node;
502 struct hlist_node *node_tmp;
504 spin_lock_bh(&bat_priv->gw.list_lock);
505 hlist_for_each_entry_safe(gw_node, node_tmp,
506 &bat_priv->gw.gateway_list, list) {
507 hlist_del_init_rcu(&gw_node->list);
508 batadv_gw_node_put(gw_node);
509 bat_priv->gw.generation++;
511 spin_unlock_bh(&bat_priv->gw.list_lock);
514 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
517 * batadv_gw_client_seq_print_text() - Print the gateway table in a seq file
518 * @seq: seq file to print on
519 * @offset: not used
521 * Return: always 0
523 int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
525 struct net_device *net_dev = (struct net_device *)seq->private;
526 struct batadv_priv *bat_priv = netdev_priv(net_dev);
527 struct batadv_hard_iface *primary_if;
529 primary_if = batadv_seq_print_text_primary_if_get(seq);
530 if (!primary_if)
531 return 0;
533 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
534 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
535 primary_if->net_dev->dev_addr, net_dev->name,
536 bat_priv->algo_ops->name);
538 batadv_hardif_put(primary_if);
540 if (!bat_priv->algo_ops->gw.print) {
541 seq_puts(seq,
542 "No printing function for this routing protocol\n");
543 return 0;
546 bat_priv->algo_ops->gw.print(bat_priv, seq);
548 return 0;
550 #endif
553 * batadv_gw_dump() - Dump gateways into a message
554 * @msg: Netlink message to dump into
555 * @cb: Control block containing additional options
557 * Return: Error code, or length of message
559 int batadv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb)
561 struct batadv_hard_iface *primary_if = NULL;
562 struct net *net = sock_net(cb->skb->sk);
563 struct net_device *soft_iface;
564 struct batadv_priv *bat_priv;
565 int ifindex;
566 int ret;
568 ifindex = batadv_netlink_get_ifindex(cb->nlh,
569 BATADV_ATTR_MESH_IFINDEX);
570 if (!ifindex)
571 return -EINVAL;
573 soft_iface = dev_get_by_index(net, ifindex);
574 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
575 ret = -ENODEV;
576 goto out;
579 bat_priv = netdev_priv(soft_iface);
581 primary_if = batadv_primary_if_get_selected(bat_priv);
582 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
583 ret = -ENOENT;
584 goto out;
587 if (!bat_priv->algo_ops->gw.dump) {
588 ret = -EOPNOTSUPP;
589 goto out;
592 bat_priv->algo_ops->gw.dump(msg, cb, bat_priv);
594 ret = msg->len;
596 out:
597 if (primary_if)
598 batadv_hardif_put(primary_if);
599 if (soft_iface)
600 dev_put(soft_iface);
602 return ret;
606 * batadv_gw_dhcp_recipient_get() - check if a packet is a DHCP message
607 * @skb: the packet to check
608 * @header_len: a pointer to the batman-adv header size
609 * @chaddr: buffer where the client address will be stored. Valid
610 * only if the function returns BATADV_DHCP_TO_CLIENT
612 * This function may re-allocate the data buffer of the skb passed as argument.
614 * Return:
615 * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
616 * while parsing it
617 * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
618 * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
620 enum batadv_dhcp_recipient
621 batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
622 u8 *chaddr)
624 enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
625 struct ethhdr *ethhdr;
626 struct iphdr *iphdr;
627 struct ipv6hdr *ipv6hdr;
628 struct udphdr *udphdr;
629 struct vlan_ethhdr *vhdr;
630 int chaddr_offset;
631 __be16 proto;
632 u8 *p;
634 /* check for ethernet header */
635 if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
636 return BATADV_DHCP_NO;
638 ethhdr = eth_hdr(skb);
639 proto = ethhdr->h_proto;
640 *header_len += ETH_HLEN;
642 /* check for initial vlan header */
643 if (proto == htons(ETH_P_8021Q)) {
644 if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
645 return BATADV_DHCP_NO;
647 vhdr = vlan_eth_hdr(skb);
648 proto = vhdr->h_vlan_encapsulated_proto;
649 *header_len += VLAN_HLEN;
652 /* check for ip header */
653 switch (proto) {
654 case htons(ETH_P_IP):
655 if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
656 return BATADV_DHCP_NO;
658 iphdr = (struct iphdr *)(skb->data + *header_len);
659 *header_len += iphdr->ihl * 4;
661 /* check for udp header */
662 if (iphdr->protocol != IPPROTO_UDP)
663 return BATADV_DHCP_NO;
665 break;
666 case htons(ETH_P_IPV6):
667 if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
668 return BATADV_DHCP_NO;
670 ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
671 *header_len += sizeof(*ipv6hdr);
673 /* check for udp header */
674 if (ipv6hdr->nexthdr != IPPROTO_UDP)
675 return BATADV_DHCP_NO;
677 break;
678 default:
679 return BATADV_DHCP_NO;
682 if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
683 return BATADV_DHCP_NO;
685 udphdr = (struct udphdr *)(skb->data + *header_len);
686 *header_len += sizeof(*udphdr);
688 /* check for bootp port */
689 switch (proto) {
690 case htons(ETH_P_IP):
691 if (udphdr->dest == htons(67))
692 ret = BATADV_DHCP_TO_SERVER;
693 else if (udphdr->source == htons(67))
694 ret = BATADV_DHCP_TO_CLIENT;
695 break;
696 case htons(ETH_P_IPV6):
697 if (udphdr->dest == htons(547))
698 ret = BATADV_DHCP_TO_SERVER;
699 else if (udphdr->source == htons(547))
700 ret = BATADV_DHCP_TO_CLIENT;
701 break;
704 chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
705 /* store the client address if the message is going to a client */
706 if (ret == BATADV_DHCP_TO_CLIENT &&
707 pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) {
708 /* check if the DHCP packet carries an Ethernet DHCP */
709 p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
710 if (*p != BATADV_DHCP_HTYPE_ETHERNET)
711 return BATADV_DHCP_NO;
713 /* check if the DHCP packet carries a valid Ethernet address */
714 p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
715 if (*p != ETH_ALEN)
716 return BATADV_DHCP_NO;
718 ether_addr_copy(chaddr, skb->data + chaddr_offset);
721 return ret;
725 * batadv_gw_out_of_range() - check if the dhcp request destination is the best
726 * gateway
727 * @bat_priv: the bat priv with all the soft interface information
728 * @skb: the outgoing packet
730 * Check if the skb is a DHCP request and if it is sent to the current best GW
731 * server. Due to topology changes it may be the case that the GW server
732 * previously selected is not the best one anymore.
734 * This call might reallocate skb data.
735 * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
737 * Return: true if the packet destination is unicast and it is not the best gw,
738 * false otherwise.
740 bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
741 struct sk_buff *skb)
743 struct batadv_neigh_node *neigh_curr = NULL;
744 struct batadv_neigh_node *neigh_old = NULL;
745 struct batadv_orig_node *orig_dst_node = NULL;
746 struct batadv_gw_node *gw_node = NULL;
747 struct batadv_gw_node *curr_gw = NULL;
748 struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
749 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
750 bool out_of_range = false;
751 u8 curr_tq_avg;
752 unsigned short vid;
754 vid = batadv_get_vid(skb, 0);
756 if (is_multicast_ether_addr(ethhdr->h_dest))
757 goto out;
759 orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
760 ethhdr->h_dest, vid);
761 if (!orig_dst_node)
762 goto out;
764 gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
765 if (!gw_node)
766 goto out;
768 switch (atomic_read(&bat_priv->gw.mode)) {
769 case BATADV_GW_MODE_SERVER:
770 /* If we are a GW then we are our best GW. We can artificially
771 * set the tq towards ourself as the maximum value
773 curr_tq_avg = BATADV_TQ_MAX_VALUE;
774 break;
775 case BATADV_GW_MODE_CLIENT:
776 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
777 if (!curr_gw)
778 goto out;
780 /* packet is going to our gateway */
781 if (curr_gw->orig_node == orig_dst_node)
782 goto out;
784 /* If the dhcp packet has been sent to a different gw,
785 * we have to evaluate whether the old gw is still
786 * reliable enough
788 neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
789 NULL);
790 if (!neigh_curr)
791 goto out;
793 curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
794 BATADV_IF_DEFAULT);
795 if (!curr_ifinfo)
796 goto out;
798 curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
799 batadv_neigh_ifinfo_put(curr_ifinfo);
801 break;
802 case BATADV_GW_MODE_OFF:
803 default:
804 goto out;
807 neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
808 if (!neigh_old)
809 goto out;
811 old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
812 if (!old_ifinfo)
813 goto out;
815 if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
816 out_of_range = true;
817 batadv_neigh_ifinfo_put(old_ifinfo);
819 out:
820 if (orig_dst_node)
821 batadv_orig_node_put(orig_dst_node);
822 if (curr_gw)
823 batadv_gw_node_put(curr_gw);
824 if (gw_node)
825 batadv_gw_node_put(gw_node);
826 if (neigh_old)
827 batadv_neigh_node_put(neigh_old);
828 if (neigh_curr)
829 batadv_neigh_node_put(neigh_curr);
830 return out_of_range;