GNU Linux-libre 4.9.337-gnu1
[releases.git] / net / batman-adv / gateway_client.c
1 /* Copyright (C) 2009-2016  B.A.T.M.A.N. contributors:
2  *
3  * Marek Lindner
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "gateway_client.h"
19 #include "main.h"
20
21 #include <linux/atomic.h>
22 #include <linux/byteorder/generic.h>
23 #include <linux/errno.h>
24 #include <linux/etherdevice.h>
25 #include <linux/fs.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/in.h>
29 #include <linux/ip.h>
30 #include <linux/ipv6.h>
31 #include <linux/kernel.h>
32 #include <linux/kref.h>
33 #include <linux/list.h>
34 #include <linux/lockdep.h>
35 #include <linux/netdevice.h>
36 #include <linux/netlink.h>
37 #include <linux/rculist.h>
38 #include <linux/rcupdate.h>
39 #include <linux/seq_file.h>
40 #include <linux/skbuff.h>
41 #include <linux/slab.h>
42 #include <linux/spinlock.h>
43 #include <linux/stddef.h>
44 #include <linux/udp.h>
45 #include <net/sock.h>
46 #include <uapi/linux/batman_adv.h>
47
48 #include "gateway_common.h"
49 #include "hard-interface.h"
50 #include "log.h"
51 #include "netlink.h"
52 #include "originator.h"
53 #include "packet.h"
54 #include "routing.h"
55 #include "soft-interface.h"
56 #include "sysfs.h"
57 #include "translation-table.h"
58
59 /* These are the offsets of the "hw type" and "hw address length" in the dhcp
60  * packet starting at the beginning of the dhcp header
61  */
62 #define BATADV_DHCP_HTYPE_OFFSET        1
63 #define BATADV_DHCP_HLEN_OFFSET         2
64 /* Value of htype representing Ethernet */
65 #define BATADV_DHCP_HTYPE_ETHERNET      0x01
66 /* This is the offset of the "chaddr" field in the dhcp packet starting at the
67  * beginning of the dhcp header
68  */
69 #define BATADV_DHCP_CHADDR_OFFSET       28
70
71 /**
72  * batadv_gw_node_release - release gw_node from lists and queue for free after
73  *  rcu grace period
74  * @ref: kref pointer of the gw_node
75  */
76 static void batadv_gw_node_release(struct kref *ref)
77 {
78         struct batadv_gw_node *gw_node;
79
80         gw_node = container_of(ref, struct batadv_gw_node, refcount);
81
82         batadv_orig_node_put(gw_node->orig_node);
83         kfree_rcu(gw_node, rcu);
84 }
85
86 /**
87  * batadv_gw_node_put - decrement the gw_node refcounter and possibly release it
88  * @gw_node: gateway node to free
89  */
90 void batadv_gw_node_put(struct batadv_gw_node *gw_node)
91 {
92         kref_put(&gw_node->refcount, batadv_gw_node_release);
93 }
94
95 struct batadv_gw_node *
96 batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
97 {
98         struct batadv_gw_node *gw_node;
99
100         rcu_read_lock();
101         gw_node = rcu_dereference(bat_priv->gw.curr_gw);
102         if (!gw_node)
103                 goto out;
104
105         if (!kref_get_unless_zero(&gw_node->refcount))
106                 gw_node = NULL;
107
108 out:
109         rcu_read_unlock();
110         return gw_node;
111 }
112
113 struct batadv_orig_node *
114 batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
115 {
116         struct batadv_gw_node *gw_node;
117         struct batadv_orig_node *orig_node = NULL;
118
119         gw_node = batadv_gw_get_selected_gw_node(bat_priv);
120         if (!gw_node)
121                 goto out;
122
123         rcu_read_lock();
124         orig_node = gw_node->orig_node;
125         if (!orig_node)
126                 goto unlock;
127
128         if (!kref_get_unless_zero(&orig_node->refcount))
129                 orig_node = NULL;
130
131 unlock:
132         rcu_read_unlock();
133 out:
134         if (gw_node)
135                 batadv_gw_node_put(gw_node);
136         return orig_node;
137 }
138
139 static void batadv_gw_select(struct batadv_priv *bat_priv,
140                              struct batadv_gw_node *new_gw_node)
141 {
142         struct batadv_gw_node *curr_gw_node;
143
144         spin_lock_bh(&bat_priv->gw.list_lock);
145
146         if (new_gw_node)
147                 kref_get(&new_gw_node->refcount);
148
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);
151
152         if (curr_gw_node)
153                 batadv_gw_node_put(curr_gw_node);
154
155         spin_unlock_bh(&bat_priv->gw.list_lock);
156 }
157
158 /**
159  * batadv_gw_reselect - force a gateway reselection
160  * @bat_priv: the bat priv with all the soft interface information
161  *
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.
165  *
166  * This means that invoking batadv_gw_reselect() does not guarantee a gateway
167  * change and therefore a uevent is not necessarily expected.
168  */
169 void batadv_gw_reselect(struct batadv_priv *bat_priv)
170 {
171         atomic_set(&bat_priv->gw.reselect, 1);
172 }
173
174 /**
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
177  *
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.
181  */
182 void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
183 {
184         struct batadv_gw_node *curr_gw;
185
186         if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
187                 return;
188
189         curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
190         if (!curr_gw)
191                 return;
192
193         /* deselect the current gateway so that next time that client mode is
194          * enabled a proper GW_ADD event can be sent
195          */
196         batadv_gw_select(bat_priv, NULL);
197
198         /* if batman-adv is switching the gw client mode off and a gateway was
199          * already selected, send a DEL uevent
200          */
201         batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
202
203         batadv_gw_node_put(curr_gw);
204 }
205
206 void batadv_gw_election(struct batadv_priv *bat_priv)
207 {
208         struct batadv_gw_node *curr_gw = NULL;
209         struct batadv_gw_node *next_gw = NULL;
210         struct batadv_neigh_node *router = NULL;
211         struct batadv_neigh_ifinfo *router_ifinfo = NULL;
212         char gw_addr[18] = { '\0' };
213
214         if (atomic_read(&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 (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && 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         if (curr_gw)
285                 batadv_gw_node_put(curr_gw);
286         if (next_gw)
287                 batadv_gw_node_put(next_gw);
288         if (router)
289                 batadv_neigh_node_put(router);
290         if (router_ifinfo)
291                 batadv_neigh_ifinfo_put(router_ifinfo);
292 }
293
294 void batadv_gw_check_election(struct batadv_priv *bat_priv,
295                               struct batadv_orig_node *orig_node)
296 {
297         struct batadv_orig_node *curr_gw_orig;
298
299         /* abort immediately if the routing algorithm does not support gateway
300          * election
301          */
302         if (!bat_priv->algo_ops->gw.is_eligible)
303                 return;
304
305         curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
306         if (!curr_gw_orig)
307                 goto reselect;
308
309         /* this node already is the gateway */
310         if (curr_gw_orig == orig_node)
311                 goto out;
312
313         if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig,
314                                                 orig_node))
315                 goto out;
316
317 reselect:
318         batadv_gw_reselect(bat_priv);
319 out:
320         if (curr_gw_orig)
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 soft 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  */
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(sizeof(*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.list);
357
358         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
359                    "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
360                    orig_node->orig,
361                    ntohl(gateway->bandwidth_down) / 10,
362                    ntohl(gateway->bandwidth_down) % 10,
363                    ntohl(gateway->bandwidth_up) / 10,
364                    ntohl(gateway->bandwidth_up) % 10);
365
366         /* don't return reference to new gw_node */
367         batadv_gw_node_put(gw_node);
368 }
369
370 /**
371  * batadv_gw_node_get - retrieve gateway node from list of available gateways
372  * @bat_priv: the bat priv with all the soft interface information
373  * @orig_node: originator announcing gateway capabilities
374  *
375  * Return: gateway node if found or NULL otherwise.
376  */
377 struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv,
378                                           struct batadv_orig_node *orig_node)
379 {
380         struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
381
382         rcu_read_lock();
383         hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) {
384                 if (gw_node_tmp->orig_node != orig_node)
385                         continue;
386
387                 if (!kref_get_unless_zero(&gw_node_tmp->refcount))
388                         continue;
389
390                 gw_node = gw_node_tmp;
391                 break;
392         }
393         rcu_read_unlock();
394
395         return gw_node;
396 }
397
398 /**
399  * batadv_gw_node_update - update list of available gateways with changed
400  *  bandwidth information
401  * @bat_priv: the bat priv with all the soft interface information
402  * @orig_node: originator announcing gateway capabilities
403  * @gateway: announced bandwidth information
404  */
405 void batadv_gw_node_update(struct batadv_priv *bat_priv,
406                            struct batadv_orig_node *orig_node,
407                            struct batadv_tvlv_gateway_data *gateway)
408 {
409         struct batadv_gw_node *gw_node, *curr_gw = NULL;
410
411         spin_lock_bh(&bat_priv->gw.list_lock);
412         gw_node = batadv_gw_node_get(bat_priv, orig_node);
413         if (!gw_node) {
414                 batadv_gw_node_add(bat_priv, orig_node, gateway);
415                 spin_unlock_bh(&bat_priv->gw.list_lock);
416                 goto out;
417         }
418         spin_unlock_bh(&bat_priv->gw.list_lock);
419
420         if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
421             (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
422                 goto out;
423
424         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
425                    "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
426                    orig_node->orig,
427                    gw_node->bandwidth_down / 10,
428                    gw_node->bandwidth_down % 10,
429                    gw_node->bandwidth_up / 10,
430                    gw_node->bandwidth_up % 10,
431                    ntohl(gateway->bandwidth_down) / 10,
432                    ntohl(gateway->bandwidth_down) % 10,
433                    ntohl(gateway->bandwidth_up) / 10,
434                    ntohl(gateway->bandwidth_up) % 10);
435
436         gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
437         gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
438
439         if (ntohl(gateway->bandwidth_down) == 0) {
440                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
441                            "Gateway %pM removed from gateway list\n",
442                            orig_node->orig);
443
444                 /* Note: We don't need a NULL check here, since curr_gw never
445                  * gets dereferenced.
446                  */
447                 spin_lock_bh(&bat_priv->gw.list_lock);
448                 if (!hlist_unhashed(&gw_node->list)) {
449                         hlist_del_init_rcu(&gw_node->list);
450                         batadv_gw_node_put(gw_node);
451                 }
452                 spin_unlock_bh(&bat_priv->gw.list_lock);
453
454                 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
455                 if (gw_node == curr_gw)
456                         batadv_gw_reselect(bat_priv);
457
458                 if (curr_gw)
459                         batadv_gw_node_put(curr_gw);
460         }
461
462 out:
463         if (gw_node)
464                 batadv_gw_node_put(gw_node);
465 }
466
467 void batadv_gw_node_delete(struct batadv_priv *bat_priv,
468                            struct batadv_orig_node *orig_node)
469 {
470         struct batadv_tvlv_gateway_data gateway;
471
472         gateway.bandwidth_down = 0;
473         gateway.bandwidth_up = 0;
474
475         batadv_gw_node_update(bat_priv, orig_node, &gateway);
476 }
477
478 void batadv_gw_node_free(struct batadv_priv *bat_priv)
479 {
480         struct batadv_gw_node *gw_node;
481         struct hlist_node *node_tmp;
482
483         spin_lock_bh(&bat_priv->gw.list_lock);
484         hlist_for_each_entry_safe(gw_node, node_tmp,
485                                   &bat_priv->gw.list, list) {
486                 hlist_del_init_rcu(&gw_node->list);
487                 batadv_gw_node_put(gw_node);
488         }
489         spin_unlock_bh(&bat_priv->gw.list_lock);
490 }
491
492 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
493 int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
494 {
495         struct net_device *net_dev = (struct net_device *)seq->private;
496         struct batadv_priv *bat_priv = netdev_priv(net_dev);
497         struct batadv_hard_iface *primary_if;
498
499         primary_if = batadv_seq_print_text_primary_if_get(seq);
500         if (!primary_if)
501                 return 0;
502
503         seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
504                    BATADV_SOURCE_VERSION, primary_if->net_dev->name,
505                    primary_if->net_dev->dev_addr, net_dev->name,
506                    bat_priv->algo_ops->name);
507
508         batadv_hardif_put(primary_if);
509
510         if (!bat_priv->algo_ops->gw.print) {
511                 seq_puts(seq,
512                          "No printing function for this routing protocol\n");
513                 return 0;
514         }
515
516         bat_priv->algo_ops->gw.print(bat_priv, seq);
517
518         return 0;
519 }
520 #endif
521
522 /**
523  * batadv_gw_dump - Dump gateways into a message
524  * @msg: Netlink message to dump into
525  * @cb: Control block containing additional options
526  *
527  * Return: Error code, or length of message
528  */
529 int batadv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb)
530 {
531         struct batadv_hard_iface *primary_if = NULL;
532         struct net *net = sock_net(cb->skb->sk);
533         struct net_device *soft_iface;
534         struct batadv_priv *bat_priv;
535         int ifindex;
536         int ret;
537
538         ifindex = batadv_netlink_get_ifindex(cb->nlh,
539                                              BATADV_ATTR_MESH_IFINDEX);
540         if (!ifindex)
541                 return -EINVAL;
542
543         soft_iface = dev_get_by_index(net, ifindex);
544         if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
545                 ret = -ENODEV;
546                 goto out;
547         }
548
549         bat_priv = netdev_priv(soft_iface);
550
551         primary_if = batadv_primary_if_get_selected(bat_priv);
552         if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
553                 ret = -ENOENT;
554                 goto out;
555         }
556
557         if (!bat_priv->algo_ops->gw.dump) {
558                 ret = -EOPNOTSUPP;
559                 goto out;
560         }
561
562         bat_priv->algo_ops->gw.dump(msg, cb, bat_priv);
563
564         ret = msg->len;
565
566 out:
567         if (primary_if)
568                 batadv_hardif_put(primary_if);
569         if (soft_iface)
570                 dev_put(soft_iface);
571
572         return ret;
573 }
574
575 /**
576  * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message
577  * @skb: the packet to check
578  * @header_len: a pointer to the batman-adv header size
579  * @chaddr: buffer where the client address will be stored. Valid
580  *  only if the function returns BATADV_DHCP_TO_CLIENT
581  *
582  * This function may re-allocate the data buffer of the skb passed as argument.
583  *
584  * Return:
585  * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
586  *   while parsing it
587  * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
588  * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
589  */
590 enum batadv_dhcp_recipient
591 batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
592                              u8 *chaddr)
593 {
594         enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
595         struct ethhdr *ethhdr;
596         struct iphdr *iphdr;
597         struct ipv6hdr *ipv6hdr;
598         struct udphdr *udphdr;
599         struct vlan_ethhdr *vhdr;
600         int chaddr_offset;
601         __be16 proto;
602         u8 *p;
603
604         /* check for ethernet header */
605         if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
606                 return BATADV_DHCP_NO;
607
608         ethhdr = eth_hdr(skb);
609         proto = ethhdr->h_proto;
610         *header_len += ETH_HLEN;
611
612         /* check for initial vlan header */
613         if (proto == htons(ETH_P_8021Q)) {
614                 if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
615                         return BATADV_DHCP_NO;
616
617                 vhdr = vlan_eth_hdr(skb);
618                 proto = vhdr->h_vlan_encapsulated_proto;
619                 *header_len += VLAN_HLEN;
620         }
621
622         /* check for ip header */
623         switch (proto) {
624         case htons(ETH_P_IP):
625                 if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
626                         return BATADV_DHCP_NO;
627
628                 iphdr = (struct iphdr *)(skb->data + *header_len);
629                 *header_len += iphdr->ihl * 4;
630
631                 /* check for udp header */
632                 if (iphdr->protocol != IPPROTO_UDP)
633                         return BATADV_DHCP_NO;
634
635                 break;
636         case htons(ETH_P_IPV6):
637                 if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
638                         return BATADV_DHCP_NO;
639
640                 ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
641                 *header_len += sizeof(*ipv6hdr);
642
643                 /* check for udp header */
644                 if (ipv6hdr->nexthdr != IPPROTO_UDP)
645                         return BATADV_DHCP_NO;
646
647                 break;
648         default:
649                 return BATADV_DHCP_NO;
650         }
651
652         if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
653                 return BATADV_DHCP_NO;
654
655         udphdr = (struct udphdr *)(skb->data + *header_len);
656         *header_len += sizeof(*udphdr);
657
658         /* check for bootp port */
659         switch (proto) {
660         case htons(ETH_P_IP):
661                 if (udphdr->dest == htons(67))
662                         ret = BATADV_DHCP_TO_SERVER;
663                 else if (udphdr->source == htons(67))
664                         ret = BATADV_DHCP_TO_CLIENT;
665                 break;
666         case htons(ETH_P_IPV6):
667                 if (udphdr->dest == htons(547))
668                         ret = BATADV_DHCP_TO_SERVER;
669                 else if (udphdr->source == htons(547))
670                         ret = BATADV_DHCP_TO_CLIENT;
671                 break;
672         }
673
674         chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
675         /* store the client address if the message is going to a client */
676         if (ret == BATADV_DHCP_TO_CLIENT) {
677                 if (!pskb_may_pull(skb, chaddr_offset + ETH_ALEN))
678                         return BATADV_DHCP_NO;
679
680                 /* check if the DHCP packet carries an Ethernet DHCP */
681                 p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
682                 if (*p != BATADV_DHCP_HTYPE_ETHERNET)
683                         return BATADV_DHCP_NO;
684
685                 /* check if the DHCP packet carries a valid Ethernet address */
686                 p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
687                 if (*p != ETH_ALEN)
688                         return BATADV_DHCP_NO;
689
690                 ether_addr_copy(chaddr, skb->data + chaddr_offset);
691         }
692
693         return ret;
694 }
695
696 /**
697  * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
698  * @bat_priv: the bat priv with all the soft interface information
699  * @skb: the outgoing packet
700  *
701  * Check if the skb is a DHCP request and if it is sent to the current best GW
702  * server. Due to topology changes it may be the case that the GW server
703  * previously selected is not the best one anymore.
704  *
705  * This call might reallocate skb data.
706  * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
707  *
708  * Return: true if the packet destination is unicast and it is not the best gw,
709  * false otherwise.
710  */
711 bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
712                             struct sk_buff *skb)
713 {
714         struct batadv_neigh_node *neigh_curr = NULL;
715         struct batadv_neigh_node *neigh_old = NULL;
716         struct batadv_orig_node *orig_dst_node = NULL;
717         struct batadv_gw_node *gw_node = NULL;
718         struct batadv_gw_node *curr_gw = NULL;
719         struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
720         struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
721         bool out_of_range = false;
722         u8 curr_tq_avg;
723         unsigned short vid;
724
725         vid = batadv_get_vid(skb, 0);
726
727         if (is_multicast_ether_addr(ethhdr->h_dest))
728                 goto out;
729
730         orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
731                                                  ethhdr->h_dest, vid);
732         if (!orig_dst_node)
733                 goto out;
734
735         gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
736         if (!gw_node)
737                 goto out;
738
739         switch (atomic_read(&bat_priv->gw.mode)) {
740         case BATADV_GW_MODE_SERVER:
741                 /* If we are a GW then we are our best GW. We can artificially
742                  * set the tq towards ourself as the maximum value
743                  */
744                 curr_tq_avg = BATADV_TQ_MAX_VALUE;
745                 break;
746         case BATADV_GW_MODE_CLIENT:
747                 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
748                 if (!curr_gw)
749                         goto out;
750
751                 /* packet is going to our gateway */
752                 if (curr_gw->orig_node == orig_dst_node)
753                         goto out;
754
755                 /* If the dhcp packet has been sent to a different gw,
756                  * we have to evaluate whether the old gw is still
757                  * reliable enough
758                  */
759                 neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
760                                                 NULL);
761                 if (!neigh_curr)
762                         goto out;
763
764                 curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
765                                                       BATADV_IF_DEFAULT);
766                 if (!curr_ifinfo)
767                         goto out;
768
769                 curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
770                 batadv_neigh_ifinfo_put(curr_ifinfo);
771
772                 break;
773         case BATADV_GW_MODE_OFF:
774         default:
775                 goto out;
776         }
777
778         neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
779         if (!neigh_old)
780                 goto out;
781
782         old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
783         if (!old_ifinfo)
784                 goto out;
785
786         if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
787                 out_of_range = true;
788         batadv_neigh_ifinfo_put(old_ifinfo);
789
790 out:
791         if (orig_dst_node)
792                 batadv_orig_node_put(orig_dst_node);
793         if (curr_gw)
794                 batadv_gw_node_put(curr_gw);
795         if (gw_node)
796                 batadv_gw_node_put(gw_node);
797         if (neigh_old)
798                 batadv_neigh_node_put(neigh_old);
799         if (neigh_curr)
800                 batadv_neigh_node_put(neigh_curr);
801         return out_of_range;
802 }