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