Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (C) B.A.T.M.A.N. contributors:
0003  *
0004  * Marek Lindner
0005  */
0006 
0007 #include "gateway_client.h"
0008 #include "main.h"
0009 
0010 #include <linux/atomic.h>
0011 #include <linux/byteorder/generic.h>
0012 #include <linux/container_of.h>
0013 #include <linux/errno.h>
0014 #include <linux/etherdevice.h>
0015 #include <linux/gfp.h>
0016 #include <linux/if_ether.h>
0017 #include <linux/if_vlan.h>
0018 #include <linux/in.h>
0019 #include <linux/ip.h>
0020 #include <linux/ipv6.h>
0021 #include <linux/kernel.h>
0022 #include <linux/kref.h>
0023 #include <linux/list.h>
0024 #include <linux/lockdep.h>
0025 #include <linux/netdevice.h>
0026 #include <linux/netlink.h>
0027 #include <linux/rculist.h>
0028 #include <linux/rcupdate.h>
0029 #include <linux/skbuff.h>
0030 #include <linux/slab.h>
0031 #include <linux/spinlock.h>
0032 #include <linux/stddef.h>
0033 #include <linux/udp.h>
0034 #include <net/sock.h>
0035 #include <uapi/linux/batadv_packet.h>
0036 #include <uapi/linux/batman_adv.h>
0037 
0038 #include "hard-interface.h"
0039 #include "log.h"
0040 #include "netlink.h"
0041 #include "originator.h"
0042 #include "routing.h"
0043 #include "soft-interface.h"
0044 #include "translation-table.h"
0045 
0046 /* These are the offsets of the "hw type" and "hw address length" in the dhcp
0047  * packet starting at the beginning of the dhcp header
0048  */
0049 #define BATADV_DHCP_HTYPE_OFFSET    1
0050 #define BATADV_DHCP_HLEN_OFFSET     2
0051 /* Value of htype representing Ethernet */
0052 #define BATADV_DHCP_HTYPE_ETHERNET  0x01
0053 /* This is the offset of the "chaddr" field in the dhcp packet starting at the
0054  * beginning of the dhcp header
0055  */
0056 #define BATADV_DHCP_CHADDR_OFFSET   28
0057 
0058 /**
0059  * batadv_gw_node_release() - release gw_node from lists and queue for free
0060  *  after rcu grace period
0061  * @ref: kref pointer of the gw_node
0062  */
0063 void batadv_gw_node_release(struct kref *ref)
0064 {
0065     struct batadv_gw_node *gw_node;
0066 
0067     gw_node = container_of(ref, struct batadv_gw_node, refcount);
0068 
0069     batadv_orig_node_put(gw_node->orig_node);
0070     kfree_rcu(gw_node, rcu);
0071 }
0072 
0073 /**
0074  * batadv_gw_get_selected_gw_node() - Get currently selected gateway
0075  * @bat_priv: the bat priv with all the soft interface information
0076  *
0077  * Return: selected gateway (with increased refcnt), NULL on errors
0078  */
0079 struct batadv_gw_node *
0080 batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
0081 {
0082     struct batadv_gw_node *gw_node;
0083 
0084     rcu_read_lock();
0085     gw_node = rcu_dereference(bat_priv->gw.curr_gw);
0086     if (!gw_node)
0087         goto out;
0088 
0089     if (!kref_get_unless_zero(&gw_node->refcount))
0090         gw_node = NULL;
0091 
0092 out:
0093     rcu_read_unlock();
0094     return gw_node;
0095 }
0096 
0097 /**
0098  * batadv_gw_get_selected_orig() - Get originator of currently selected gateway
0099  * @bat_priv: the bat priv with all the soft interface information
0100  *
0101  * Return: orig_node of selected gateway (with increased refcnt), NULL on errors
0102  */
0103 struct batadv_orig_node *
0104 batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
0105 {
0106     struct batadv_gw_node *gw_node;
0107     struct batadv_orig_node *orig_node = NULL;
0108 
0109     gw_node = batadv_gw_get_selected_gw_node(bat_priv);
0110     if (!gw_node)
0111         goto out;
0112 
0113     rcu_read_lock();
0114     orig_node = gw_node->orig_node;
0115     if (!orig_node)
0116         goto unlock;
0117 
0118     if (!kref_get_unless_zero(&orig_node->refcount))
0119         orig_node = NULL;
0120 
0121 unlock:
0122     rcu_read_unlock();
0123 out:
0124     batadv_gw_node_put(gw_node);
0125     return orig_node;
0126 }
0127 
0128 static void batadv_gw_select(struct batadv_priv *bat_priv,
0129                  struct batadv_gw_node *new_gw_node)
0130 {
0131     struct batadv_gw_node *curr_gw_node;
0132 
0133     spin_lock_bh(&bat_priv->gw.list_lock);
0134 
0135     if (new_gw_node)
0136         kref_get(&new_gw_node->refcount);
0137 
0138     curr_gw_node = rcu_replace_pointer(bat_priv->gw.curr_gw, new_gw_node,
0139                        true);
0140 
0141     batadv_gw_node_put(curr_gw_node);
0142 
0143     spin_unlock_bh(&bat_priv->gw.list_lock);
0144 }
0145 
0146 /**
0147  * batadv_gw_reselect() - force a gateway reselection
0148  * @bat_priv: the bat priv with all the soft interface information
0149  *
0150  * Set a flag to remind the GW component to perform a new gateway reselection.
0151  * However this function does not ensure that the current gateway is going to be
0152  * deselected. The reselection mechanism may elect the same gateway once again.
0153  *
0154  * This means that invoking batadv_gw_reselect() does not guarantee a gateway
0155  * change and therefore a uevent is not necessarily expected.
0156  */
0157 void batadv_gw_reselect(struct batadv_priv *bat_priv)
0158 {
0159     atomic_set(&bat_priv->gw.reselect, 1);
0160 }
0161 
0162 /**
0163  * batadv_gw_check_client_stop() - check if client mode has been switched off
0164  * @bat_priv: the bat priv with all the soft interface information
0165  *
0166  * This function assumes the caller has checked that the gw state *is actually
0167  * changing*. This function is not supposed to be called when there is no state
0168  * change.
0169  */
0170 void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
0171 {
0172     struct batadv_gw_node *curr_gw;
0173 
0174     if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
0175         return;
0176 
0177     curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
0178     if (!curr_gw)
0179         return;
0180 
0181     /* deselect the current gateway so that next time that client mode is
0182      * enabled a proper GW_ADD event can be sent
0183      */
0184     batadv_gw_select(bat_priv, NULL);
0185 
0186     /* if batman-adv is switching the gw client mode off and a gateway was
0187      * already selected, send a DEL uevent
0188      */
0189     batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
0190 
0191     batadv_gw_node_put(curr_gw);
0192 }
0193 
0194 /**
0195  * batadv_gw_election() - Elect the best gateway
0196  * @bat_priv: the bat priv with all the soft interface information
0197  */
0198 void batadv_gw_election(struct batadv_priv *bat_priv)
0199 {
0200     struct batadv_gw_node *curr_gw = NULL;
0201     struct batadv_gw_node *next_gw = NULL;
0202     struct batadv_neigh_node *router = NULL;
0203     struct batadv_neigh_ifinfo *router_ifinfo = NULL;
0204     char gw_addr[18] = { '\0' };
0205 
0206     if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
0207         goto out;
0208 
0209     if (!bat_priv->algo_ops->gw.get_best_gw_node)
0210         goto out;
0211 
0212     curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
0213 
0214     if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
0215         goto out;
0216 
0217     /* if gw.reselect is set to 1 it means that a previous call to
0218      * gw.is_eligible() said that we have a new best GW, therefore it can
0219      * now be picked from the list and selected
0220      */
0221     next_gw = bat_priv->algo_ops->gw.get_best_gw_node(bat_priv);
0222 
0223     if (curr_gw == next_gw)
0224         goto out;
0225 
0226     if (next_gw) {
0227         sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
0228 
0229         router = batadv_orig_router_get(next_gw->orig_node,
0230                         BATADV_IF_DEFAULT);
0231         if (!router) {
0232             batadv_gw_reselect(bat_priv);
0233             goto out;
0234         }
0235 
0236         router_ifinfo = batadv_neigh_ifinfo_get(router,
0237                             BATADV_IF_DEFAULT);
0238         if (!router_ifinfo) {
0239             batadv_gw_reselect(bat_priv);
0240             goto out;
0241         }
0242     }
0243 
0244     if (curr_gw && !next_gw) {
0245         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
0246                "Removing selected gateway - no gateway in range\n");
0247         batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
0248                     NULL);
0249     } else if (!curr_gw && next_gw) {
0250         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
0251                "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
0252                next_gw->orig_node->orig,
0253                next_gw->bandwidth_down / 10,
0254                next_gw->bandwidth_down % 10,
0255                next_gw->bandwidth_up / 10,
0256                next_gw->bandwidth_up % 10,
0257                router_ifinfo->bat_iv.tq_avg);
0258         batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
0259                     gw_addr);
0260     } else {
0261         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
0262                "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
0263                next_gw->orig_node->orig,
0264                next_gw->bandwidth_down / 10,
0265                next_gw->bandwidth_down % 10,
0266                next_gw->bandwidth_up / 10,
0267                next_gw->bandwidth_up % 10,
0268                router_ifinfo->bat_iv.tq_avg);
0269         batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
0270                     gw_addr);
0271     }
0272 
0273     batadv_gw_select(bat_priv, next_gw);
0274 
0275 out:
0276     batadv_gw_node_put(curr_gw);
0277     batadv_gw_node_put(next_gw);
0278     batadv_neigh_node_put(router);
0279     batadv_neigh_ifinfo_put(router_ifinfo);
0280 }
0281 
0282 /**
0283  * batadv_gw_check_election() - Elect orig node as best gateway when eligible
0284  * @bat_priv: the bat priv with all the soft interface information
0285  * @orig_node: orig node which is to be checked
0286  */
0287 void batadv_gw_check_election(struct batadv_priv *bat_priv,
0288                   struct batadv_orig_node *orig_node)
0289 {
0290     struct batadv_orig_node *curr_gw_orig;
0291 
0292     /* abort immediately if the routing algorithm does not support gateway
0293      * election
0294      */
0295     if (!bat_priv->algo_ops->gw.is_eligible)
0296         return;
0297 
0298     curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
0299     if (!curr_gw_orig)
0300         goto reselect;
0301 
0302     /* this node already is the gateway */
0303     if (curr_gw_orig == orig_node)
0304         goto out;
0305 
0306     if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig,
0307                         orig_node))
0308         goto out;
0309 
0310 reselect:
0311     batadv_gw_reselect(bat_priv);
0312 out:
0313     batadv_orig_node_put(curr_gw_orig);
0314 }
0315 
0316 /**
0317  * batadv_gw_node_add() - add gateway node to list of available gateways
0318  * @bat_priv: the bat priv with all the soft interface information
0319  * @orig_node: originator announcing gateway capabilities
0320  * @gateway: announced bandwidth information
0321  *
0322  * Has to be called with the appropriate locks being acquired
0323  * (gw.list_lock).
0324  */
0325 static void batadv_gw_node_add(struct batadv_priv *bat_priv,
0326                    struct batadv_orig_node *orig_node,
0327                    struct batadv_tvlv_gateway_data *gateway)
0328 {
0329     struct batadv_gw_node *gw_node;
0330 
0331     lockdep_assert_held(&bat_priv->gw.list_lock);
0332 
0333     if (gateway->bandwidth_down == 0)
0334         return;
0335 
0336     gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
0337     if (!gw_node)
0338         return;
0339 
0340     kref_init(&gw_node->refcount);
0341     INIT_HLIST_NODE(&gw_node->list);
0342     kref_get(&orig_node->refcount);
0343     gw_node->orig_node = orig_node;
0344     gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
0345     gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
0346 
0347     kref_get(&gw_node->refcount);
0348     hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.gateway_list);
0349     bat_priv->gw.generation++;
0350 
0351     batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
0352            "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
0353            orig_node->orig,
0354            ntohl(gateway->bandwidth_down) / 10,
0355            ntohl(gateway->bandwidth_down) % 10,
0356            ntohl(gateway->bandwidth_up) / 10,
0357            ntohl(gateway->bandwidth_up) % 10);
0358 
0359     /* don't return reference to new gw_node */
0360     batadv_gw_node_put(gw_node);
0361 }
0362 
0363 /**
0364  * batadv_gw_node_get() - retrieve gateway node from list of available gateways
0365  * @bat_priv: the bat priv with all the soft interface information
0366  * @orig_node: originator announcing gateway capabilities
0367  *
0368  * Return: gateway node if found or NULL otherwise.
0369  */
0370 struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv,
0371                       struct batadv_orig_node *orig_node)
0372 {
0373     struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
0374 
0375     rcu_read_lock();
0376     hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.gateway_list,
0377                  list) {
0378         if (gw_node_tmp->orig_node != orig_node)
0379             continue;
0380 
0381         if (!kref_get_unless_zero(&gw_node_tmp->refcount))
0382             continue;
0383 
0384         gw_node = gw_node_tmp;
0385         break;
0386     }
0387     rcu_read_unlock();
0388 
0389     return gw_node;
0390 }
0391 
0392 /**
0393  * batadv_gw_node_update() - update list of available gateways with changed
0394  *  bandwidth information
0395  * @bat_priv: the bat priv with all the soft interface information
0396  * @orig_node: originator announcing gateway capabilities
0397  * @gateway: announced bandwidth information
0398  */
0399 void batadv_gw_node_update(struct batadv_priv *bat_priv,
0400                struct batadv_orig_node *orig_node,
0401                struct batadv_tvlv_gateway_data *gateway)
0402 {
0403     struct batadv_gw_node *gw_node, *curr_gw = NULL;
0404 
0405     spin_lock_bh(&bat_priv->gw.list_lock);
0406     gw_node = batadv_gw_node_get(bat_priv, orig_node);
0407     if (!gw_node) {
0408         batadv_gw_node_add(bat_priv, orig_node, gateway);
0409         spin_unlock_bh(&bat_priv->gw.list_lock);
0410         goto out;
0411     }
0412     spin_unlock_bh(&bat_priv->gw.list_lock);
0413 
0414     if (gw_node->bandwidth_down == ntohl(gateway->bandwidth_down) &&
0415         gw_node->bandwidth_up == ntohl(gateway->bandwidth_up))
0416         goto out;
0417 
0418     batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
0419            "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
0420            orig_node->orig,
0421            gw_node->bandwidth_down / 10,
0422            gw_node->bandwidth_down % 10,
0423            gw_node->bandwidth_up / 10,
0424            gw_node->bandwidth_up % 10,
0425            ntohl(gateway->bandwidth_down) / 10,
0426            ntohl(gateway->bandwidth_down) % 10,
0427            ntohl(gateway->bandwidth_up) / 10,
0428            ntohl(gateway->bandwidth_up) % 10);
0429 
0430     gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
0431     gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
0432 
0433     if (ntohl(gateway->bandwidth_down) == 0) {
0434         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
0435                "Gateway %pM removed from gateway list\n",
0436                orig_node->orig);
0437 
0438         /* Note: We don't need a NULL check here, since curr_gw never
0439          * gets dereferenced.
0440          */
0441         spin_lock_bh(&bat_priv->gw.list_lock);
0442         if (!hlist_unhashed(&gw_node->list)) {
0443             hlist_del_init_rcu(&gw_node->list);
0444             batadv_gw_node_put(gw_node);
0445             bat_priv->gw.generation++;
0446         }
0447         spin_unlock_bh(&bat_priv->gw.list_lock);
0448 
0449         curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
0450         if (gw_node == curr_gw)
0451             batadv_gw_reselect(bat_priv);
0452 
0453         batadv_gw_node_put(curr_gw);
0454     }
0455 
0456 out:
0457     batadv_gw_node_put(gw_node);
0458 }
0459 
0460 /**
0461  * batadv_gw_node_delete() - Remove orig_node from gateway list
0462  * @bat_priv: the bat priv with all the soft interface information
0463  * @orig_node: orig node which is currently in process of being removed
0464  */
0465 void batadv_gw_node_delete(struct batadv_priv *bat_priv,
0466                struct batadv_orig_node *orig_node)
0467 {
0468     struct batadv_tvlv_gateway_data gateway;
0469 
0470     gateway.bandwidth_down = 0;
0471     gateway.bandwidth_up = 0;
0472 
0473     batadv_gw_node_update(bat_priv, orig_node, &gateway);
0474 }
0475 
0476 /**
0477  * batadv_gw_node_free() - Free gateway information from soft interface
0478  * @bat_priv: the bat priv with all the soft interface information
0479  */
0480 void batadv_gw_node_free(struct batadv_priv *bat_priv)
0481 {
0482     struct batadv_gw_node *gw_node;
0483     struct hlist_node *node_tmp;
0484 
0485     spin_lock_bh(&bat_priv->gw.list_lock);
0486     hlist_for_each_entry_safe(gw_node, node_tmp,
0487                   &bat_priv->gw.gateway_list, list) {
0488         hlist_del_init_rcu(&gw_node->list);
0489         batadv_gw_node_put(gw_node);
0490         bat_priv->gw.generation++;
0491     }
0492     spin_unlock_bh(&bat_priv->gw.list_lock);
0493 }
0494 
0495 /**
0496  * batadv_gw_dump() - Dump gateways into a message
0497  * @msg: Netlink message to dump into
0498  * @cb: Control block containing additional options
0499  *
0500  * Return: Error code, or length of message
0501  */
0502 int batadv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb)
0503 {
0504     struct batadv_hard_iface *primary_if = NULL;
0505     struct net *net = sock_net(cb->skb->sk);
0506     struct net_device *soft_iface;
0507     struct batadv_priv *bat_priv;
0508     int ifindex;
0509     int ret;
0510 
0511     ifindex = batadv_netlink_get_ifindex(cb->nlh,
0512                          BATADV_ATTR_MESH_IFINDEX);
0513     if (!ifindex)
0514         return -EINVAL;
0515 
0516     soft_iface = dev_get_by_index(net, ifindex);
0517     if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
0518         ret = -ENODEV;
0519         goto out;
0520     }
0521 
0522     bat_priv = netdev_priv(soft_iface);
0523 
0524     primary_if = batadv_primary_if_get_selected(bat_priv);
0525     if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
0526         ret = -ENOENT;
0527         goto out;
0528     }
0529 
0530     if (!bat_priv->algo_ops->gw.dump) {
0531         ret = -EOPNOTSUPP;
0532         goto out;
0533     }
0534 
0535     bat_priv->algo_ops->gw.dump(msg, cb, bat_priv);
0536 
0537     ret = msg->len;
0538 
0539 out:
0540     batadv_hardif_put(primary_if);
0541     dev_put(soft_iface);
0542 
0543     return ret;
0544 }
0545 
0546 /**
0547  * batadv_gw_dhcp_recipient_get() - check if a packet is a DHCP message
0548  * @skb: the packet to check
0549  * @header_len: a pointer to the batman-adv header size
0550  * @chaddr: buffer where the client address will be stored. Valid
0551  *  only if the function returns BATADV_DHCP_TO_CLIENT
0552  *
0553  * This function may re-allocate the data buffer of the skb passed as argument.
0554  *
0555  * Return:
0556  * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
0557  *   while parsing it
0558  * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
0559  * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
0560  */
0561 enum batadv_dhcp_recipient
0562 batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
0563                  u8 *chaddr)
0564 {
0565     enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
0566     struct ethhdr *ethhdr;
0567     struct iphdr *iphdr;
0568     struct ipv6hdr *ipv6hdr;
0569     struct udphdr *udphdr;
0570     struct vlan_ethhdr *vhdr;
0571     int chaddr_offset;
0572     __be16 proto;
0573     u8 *p;
0574 
0575     /* check for ethernet header */
0576     if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
0577         return BATADV_DHCP_NO;
0578 
0579     ethhdr = eth_hdr(skb);
0580     proto = ethhdr->h_proto;
0581     *header_len += ETH_HLEN;
0582 
0583     /* check for initial vlan header */
0584     if (proto == htons(ETH_P_8021Q)) {
0585         if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
0586             return BATADV_DHCP_NO;
0587 
0588         vhdr = vlan_eth_hdr(skb);
0589         proto = vhdr->h_vlan_encapsulated_proto;
0590         *header_len += VLAN_HLEN;
0591     }
0592 
0593     /* check for ip header */
0594     switch (proto) {
0595     case htons(ETH_P_IP):
0596         if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
0597             return BATADV_DHCP_NO;
0598 
0599         iphdr = (struct iphdr *)(skb->data + *header_len);
0600         *header_len += iphdr->ihl * 4;
0601 
0602         /* check for udp header */
0603         if (iphdr->protocol != IPPROTO_UDP)
0604             return BATADV_DHCP_NO;
0605 
0606         break;
0607     case htons(ETH_P_IPV6):
0608         if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
0609             return BATADV_DHCP_NO;
0610 
0611         ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
0612         *header_len += sizeof(*ipv6hdr);
0613 
0614         /* check for udp header */
0615         if (ipv6hdr->nexthdr != IPPROTO_UDP)
0616             return BATADV_DHCP_NO;
0617 
0618         break;
0619     default:
0620         return BATADV_DHCP_NO;
0621     }
0622 
0623     if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
0624         return BATADV_DHCP_NO;
0625 
0626     udphdr = (struct udphdr *)(skb->data + *header_len);
0627     *header_len += sizeof(*udphdr);
0628 
0629     /* check for bootp port */
0630     switch (proto) {
0631     case htons(ETH_P_IP):
0632         if (udphdr->dest == htons(67))
0633             ret = BATADV_DHCP_TO_SERVER;
0634         else if (udphdr->source == htons(67))
0635             ret = BATADV_DHCP_TO_CLIENT;
0636         break;
0637     case htons(ETH_P_IPV6):
0638         if (udphdr->dest == htons(547))
0639             ret = BATADV_DHCP_TO_SERVER;
0640         else if (udphdr->source == htons(547))
0641             ret = BATADV_DHCP_TO_CLIENT;
0642         break;
0643     }
0644 
0645     chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
0646     /* store the client address if the message is going to a client */
0647     if (ret == BATADV_DHCP_TO_CLIENT) {
0648         if (!pskb_may_pull(skb, chaddr_offset + ETH_ALEN))
0649             return BATADV_DHCP_NO;
0650 
0651         /* check if the DHCP packet carries an Ethernet DHCP */
0652         p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
0653         if (*p != BATADV_DHCP_HTYPE_ETHERNET)
0654             return BATADV_DHCP_NO;
0655 
0656         /* check if the DHCP packet carries a valid Ethernet address */
0657         p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
0658         if (*p != ETH_ALEN)
0659             return BATADV_DHCP_NO;
0660 
0661         ether_addr_copy(chaddr, skb->data + chaddr_offset);
0662     }
0663 
0664     return ret;
0665 }
0666 
0667 /**
0668  * batadv_gw_out_of_range() - check if the dhcp request destination is the best
0669  *  gateway
0670  * @bat_priv: the bat priv with all the soft interface information
0671  * @skb: the outgoing packet
0672  *
0673  * Check if the skb is a DHCP request and if it is sent to the current best GW
0674  * server. Due to topology changes it may be the case that the GW server
0675  * previously selected is not the best one anymore.
0676  *
0677  * This call might reallocate skb data.
0678  * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
0679  *
0680  * Return: true if the packet destination is unicast and it is not the best gw,
0681  * false otherwise.
0682  */
0683 bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
0684                 struct sk_buff *skb)
0685 {
0686     struct batadv_neigh_node *neigh_curr = NULL;
0687     struct batadv_neigh_node *neigh_old = NULL;
0688     struct batadv_orig_node *orig_dst_node = NULL;
0689     struct batadv_gw_node *gw_node = NULL;
0690     struct batadv_gw_node *curr_gw = NULL;
0691     struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
0692     struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
0693     bool out_of_range = false;
0694     u8 curr_tq_avg;
0695     unsigned short vid;
0696 
0697     vid = batadv_get_vid(skb, 0);
0698 
0699     if (is_multicast_ether_addr(ethhdr->h_dest))
0700         goto out;
0701 
0702     orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
0703                          ethhdr->h_dest, vid);
0704     if (!orig_dst_node)
0705         goto out;
0706 
0707     gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
0708     if (!gw_node)
0709         goto out;
0710 
0711     switch (atomic_read(&bat_priv->gw.mode)) {
0712     case BATADV_GW_MODE_SERVER:
0713         /* If we are a GW then we are our best GW. We can artificially
0714          * set the tq towards ourself as the maximum value
0715          */
0716         curr_tq_avg = BATADV_TQ_MAX_VALUE;
0717         break;
0718     case BATADV_GW_MODE_CLIENT:
0719         curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
0720         if (!curr_gw)
0721             goto out;
0722 
0723         /* packet is going to our gateway */
0724         if (curr_gw->orig_node == orig_dst_node)
0725             goto out;
0726 
0727         /* If the dhcp packet has been sent to a different gw,
0728          * we have to evaluate whether the old gw is still
0729          * reliable enough
0730          */
0731         neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
0732                         NULL);
0733         if (!neigh_curr)
0734             goto out;
0735 
0736         curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
0737                               BATADV_IF_DEFAULT);
0738         if (!curr_ifinfo)
0739             goto out;
0740 
0741         curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
0742         batadv_neigh_ifinfo_put(curr_ifinfo);
0743 
0744         break;
0745     case BATADV_GW_MODE_OFF:
0746     default:
0747         goto out;
0748     }
0749 
0750     neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
0751     if (!neigh_old)
0752         goto out;
0753 
0754     old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
0755     if (!old_ifinfo)
0756         goto out;
0757 
0758     if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
0759         out_of_range = true;
0760     batadv_neigh_ifinfo_put(old_ifinfo);
0761 
0762 out:
0763     batadv_orig_node_put(orig_dst_node);
0764     batadv_gw_node_put(curr_gw);
0765     batadv_gw_node_put(gw_node);
0766     batadv_neigh_node_put(neigh_old);
0767     batadv_neigh_node_put(neigh_curr);
0768     return out_of_range;
0769 }