1 /* Copyright (C) 2009-2017 B.A.T.M.A.N. contributors:
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.
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.
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/>.
18 #include "gateway_client.h"
21 #include <linux/atomic.h>
22 #include <linux/byteorder/generic.h>
23 #include <linux/errno.h>
24 #include <linux/etherdevice.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.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>
46 #include <uapi/linux/batman_adv.h>
48 #include "gateway_common.h"
49 #include "hard-interface.h"
52 #include "originator.h"
55 #include "soft-interface.h"
57 #include "translation-table.h"
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
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
69 #define BATADV_DHCP_CHADDR_OFFSET 28
72 * batadv_gw_node_release - release gw_node from lists and queue for free after
74 * @ref: kref pointer of the gw_node
76 static void batadv_gw_node_release(struct kref *ref)
78 struct batadv_gw_node *gw_node;
80 gw_node = container_of(ref, struct batadv_gw_node, refcount);
82 batadv_orig_node_put(gw_node->orig_node);
83 kfree_rcu(gw_node, rcu);
87 * batadv_gw_node_put - decrement the gw_node refcounter and possibly release it
88 * @gw_node: gateway node to free
90 void batadv_gw_node_put(struct batadv_gw_node *gw_node)
92 kref_put(&gw_node->refcount, batadv_gw_node_release);
95 struct batadv_gw_node *
96 batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
98 struct batadv_gw_node *gw_node;
101 gw_node = rcu_dereference(bat_priv->gw.curr_gw);
105 if (!kref_get_unless_zero(&gw_node->refcount))
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);
124 orig_node = gw_node->orig_node;
128 if (!kref_get_unless_zero(&orig_node->refcount))
135 batadv_gw_node_put(gw_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);
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);
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
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)
189 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
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);
206 void batadv_gw_election(struct batadv_priv *bat_priv)
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' };
214 if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
217 if (!bat_priv->algo_ops->gw.get_best_gw_node)
220 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
222 if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
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
229 next_gw = bat_priv->algo_ops->gw.get_best_gw_node(bat_priv);
231 if (curr_gw == next_gw)
235 sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
237 router = batadv_orig_router_get(next_gw->orig_node,
240 batadv_gw_reselect(bat_priv);
244 router_ifinfo = batadv_neigh_ifinfo_get(router,
246 if (!router_ifinfo) {
247 batadv_gw_reselect(bat_priv);
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,
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,
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,
281 batadv_gw_select(bat_priv, next_gw);
285 batadv_gw_node_put(curr_gw);
287 batadv_gw_node_put(next_gw);
289 batadv_neigh_node_put(router);
291 batadv_neigh_ifinfo_put(router_ifinfo);
294 void batadv_gw_check_election(struct batadv_priv *bat_priv,
295 struct batadv_orig_node *orig_node)
297 struct batadv_orig_node *curr_gw_orig;
299 /* abort immediately if the routing algorithm does not support gateway
302 if (!bat_priv->algo_ops->gw.is_eligible)
305 curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
309 /* this node already is the gateway */
310 if (curr_gw_orig == orig_node)
313 if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig,
318 batadv_gw_reselect(bat_priv);
321 batadv_orig_node_put(curr_gw_orig);
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
330 * Has to be called with the appropriate locks being acquired
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)
337 struct batadv_gw_node *gw_node;
339 lockdep_assert_held(&bat_priv->gw.list_lock);
341 if (gateway->bandwidth_down == 0)
344 gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
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);
355 kref_get(&gw_node->refcount);
356 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.gateway_list);
358 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
359 "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
361 ntohl(gateway->bandwidth_down) / 10,
362 ntohl(gateway->bandwidth_down) % 10,
363 ntohl(gateway->bandwidth_up) / 10,
364 ntohl(gateway->bandwidth_up) % 10);
366 /* don't return reference to new gw_node */
367 batadv_gw_node_put(gw_node);
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
375 * Return: gateway node if found or NULL otherwise.
377 struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv,
378 struct batadv_orig_node *orig_node)
380 struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
383 hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.gateway_list,
385 if (gw_node_tmp->orig_node != orig_node)
388 if (!kref_get_unless_zero(&gw_node_tmp->refcount))
391 gw_node = gw_node_tmp;
400 * batadv_gw_node_update - update list of available gateways with changed
401 * bandwidth information
402 * @bat_priv: the bat priv with all the soft interface information
403 * @orig_node: originator announcing gateway capabilities
404 * @gateway: announced bandwidth information
406 void batadv_gw_node_update(struct batadv_priv *bat_priv,
407 struct batadv_orig_node *orig_node,
408 struct batadv_tvlv_gateway_data *gateway)
410 struct batadv_gw_node *gw_node, *curr_gw = NULL;
412 spin_lock_bh(&bat_priv->gw.list_lock);
413 gw_node = batadv_gw_node_get(bat_priv, orig_node);
415 batadv_gw_node_add(bat_priv, orig_node, gateway);
416 spin_unlock_bh(&bat_priv->gw.list_lock);
419 spin_unlock_bh(&bat_priv->gw.list_lock);
421 if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
422 (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
425 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
426 "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
428 gw_node->bandwidth_down / 10,
429 gw_node->bandwidth_down % 10,
430 gw_node->bandwidth_up / 10,
431 gw_node->bandwidth_up % 10,
432 ntohl(gateway->bandwidth_down) / 10,
433 ntohl(gateway->bandwidth_down) % 10,
434 ntohl(gateway->bandwidth_up) / 10,
435 ntohl(gateway->bandwidth_up) % 10);
437 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
438 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
440 if (ntohl(gateway->bandwidth_down) == 0) {
441 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
442 "Gateway %pM removed from gateway list\n",
445 /* Note: We don't need a NULL check here, since curr_gw never
448 spin_lock_bh(&bat_priv->gw.list_lock);
449 if (!hlist_unhashed(&gw_node->list)) {
450 hlist_del_init_rcu(&gw_node->list);
451 batadv_gw_node_put(gw_node);
453 spin_unlock_bh(&bat_priv->gw.list_lock);
455 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
456 if (gw_node == curr_gw)
457 batadv_gw_reselect(bat_priv);
460 batadv_gw_node_put(curr_gw);
465 batadv_gw_node_put(gw_node);
468 void batadv_gw_node_delete(struct batadv_priv *bat_priv,
469 struct batadv_orig_node *orig_node)
471 struct batadv_tvlv_gateway_data gateway;
473 gateway.bandwidth_down = 0;
474 gateway.bandwidth_up = 0;
476 batadv_gw_node_update(bat_priv, orig_node, &gateway);
479 void batadv_gw_node_free(struct batadv_priv *bat_priv)
481 struct batadv_gw_node *gw_node;
482 struct hlist_node *node_tmp;
484 spin_lock_bh(&bat_priv->gw.list_lock);
485 hlist_for_each_entry_safe(gw_node, node_tmp,
486 &bat_priv->gw.gateway_list, list) {
487 hlist_del_init_rcu(&gw_node->list);
488 batadv_gw_node_put(gw_node);
490 spin_unlock_bh(&bat_priv->gw.list_lock);
493 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
494 int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
496 struct net_device *net_dev = (struct net_device *)seq->private;
497 struct batadv_priv *bat_priv = netdev_priv(net_dev);
498 struct batadv_hard_iface *primary_if;
500 primary_if = batadv_seq_print_text_primary_if_get(seq);
504 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
505 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
506 primary_if->net_dev->dev_addr, net_dev->name,
507 bat_priv->algo_ops->name);
509 batadv_hardif_put(primary_if);
511 if (!bat_priv->algo_ops->gw.print) {
513 "No printing function for this routing protocol\n");
517 bat_priv->algo_ops->gw.print(bat_priv, seq);
524 * batadv_gw_dump - Dump gateways into a message
525 * @msg: Netlink message to dump into
526 * @cb: Control block containing additional options
528 * Return: Error code, or length of message
530 int batadv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb)
532 struct batadv_hard_iface *primary_if = NULL;
533 struct net *net = sock_net(cb->skb->sk);
534 struct net_device *soft_iface;
535 struct batadv_priv *bat_priv;
539 ifindex = batadv_netlink_get_ifindex(cb->nlh,
540 BATADV_ATTR_MESH_IFINDEX);
544 soft_iface = dev_get_by_index(net, ifindex);
545 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
550 bat_priv = netdev_priv(soft_iface);
552 primary_if = batadv_primary_if_get_selected(bat_priv);
553 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
558 if (!bat_priv->algo_ops->gw.dump) {
563 bat_priv->algo_ops->gw.dump(msg, cb, bat_priv);
569 batadv_hardif_put(primary_if);
577 * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message
578 * @skb: the packet to check
579 * @header_len: a pointer to the batman-adv header size
580 * @chaddr: buffer where the client address will be stored. Valid
581 * only if the function returns BATADV_DHCP_TO_CLIENT
583 * This function may re-allocate the data buffer of the skb passed as argument.
586 * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
588 * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
589 * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
591 enum batadv_dhcp_recipient
592 batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
595 enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
596 struct ethhdr *ethhdr;
598 struct ipv6hdr *ipv6hdr;
599 struct udphdr *udphdr;
600 struct vlan_ethhdr *vhdr;
605 /* check for ethernet header */
606 if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
607 return BATADV_DHCP_NO;
609 ethhdr = eth_hdr(skb);
610 proto = ethhdr->h_proto;
611 *header_len += ETH_HLEN;
613 /* check for initial vlan header */
614 if (proto == htons(ETH_P_8021Q)) {
615 if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
616 return BATADV_DHCP_NO;
618 vhdr = vlan_eth_hdr(skb);
619 proto = vhdr->h_vlan_encapsulated_proto;
620 *header_len += VLAN_HLEN;
623 /* check for ip header */
625 case htons(ETH_P_IP):
626 if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
627 return BATADV_DHCP_NO;
629 iphdr = (struct iphdr *)(skb->data + *header_len);
630 *header_len += iphdr->ihl * 4;
632 /* check for udp header */
633 if (iphdr->protocol != IPPROTO_UDP)
634 return BATADV_DHCP_NO;
637 case htons(ETH_P_IPV6):
638 if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
639 return BATADV_DHCP_NO;
641 ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
642 *header_len += sizeof(*ipv6hdr);
644 /* check for udp header */
645 if (ipv6hdr->nexthdr != IPPROTO_UDP)
646 return BATADV_DHCP_NO;
650 return BATADV_DHCP_NO;
653 if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
654 return BATADV_DHCP_NO;
656 udphdr = (struct udphdr *)(skb->data + *header_len);
657 *header_len += sizeof(*udphdr);
659 /* check for bootp port */
661 case htons(ETH_P_IP):
662 if (udphdr->dest == htons(67))
663 ret = BATADV_DHCP_TO_SERVER;
664 else if (udphdr->source == htons(67))
665 ret = BATADV_DHCP_TO_CLIENT;
667 case htons(ETH_P_IPV6):
668 if (udphdr->dest == htons(547))
669 ret = BATADV_DHCP_TO_SERVER;
670 else if (udphdr->source == htons(547))
671 ret = BATADV_DHCP_TO_CLIENT;
675 chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
676 /* store the client address if the message is going to a client */
677 if (ret == BATADV_DHCP_TO_CLIENT) {
678 if (!pskb_may_pull(skb, chaddr_offset + ETH_ALEN))
679 return BATADV_DHCP_NO;
681 /* check if the DHCP packet carries an Ethernet DHCP */
682 p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
683 if (*p != BATADV_DHCP_HTYPE_ETHERNET)
684 return BATADV_DHCP_NO;
686 /* check if the DHCP packet carries a valid Ethernet address */
687 p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
689 return BATADV_DHCP_NO;
691 ether_addr_copy(chaddr, skb->data + chaddr_offset);
698 * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
699 * @bat_priv: the bat priv with all the soft interface information
700 * @skb: the outgoing packet
702 * Check if the skb is a DHCP request and if it is sent to the current best GW
703 * server. Due to topology changes it may be the case that the GW server
704 * previously selected is not the best one anymore.
706 * This call might reallocate skb data.
707 * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
709 * Return: true if the packet destination is unicast and it is not the best gw,
712 bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
715 struct batadv_neigh_node *neigh_curr = NULL;
716 struct batadv_neigh_node *neigh_old = NULL;
717 struct batadv_orig_node *orig_dst_node = NULL;
718 struct batadv_gw_node *gw_node = NULL;
719 struct batadv_gw_node *curr_gw = NULL;
720 struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
721 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
722 bool out_of_range = false;
726 vid = batadv_get_vid(skb, 0);
728 if (is_multicast_ether_addr(ethhdr->h_dest))
731 orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
732 ethhdr->h_dest, vid);
736 gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
740 switch (atomic_read(&bat_priv->gw.mode)) {
741 case BATADV_GW_MODE_SERVER:
742 /* If we are a GW then we are our best GW. We can artificially
743 * set the tq towards ourself as the maximum value
745 curr_tq_avg = BATADV_TQ_MAX_VALUE;
747 case BATADV_GW_MODE_CLIENT:
748 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
752 /* packet is going to our gateway */
753 if (curr_gw->orig_node == orig_dst_node)
756 /* If the dhcp packet has been sent to a different gw,
757 * we have to evaluate whether the old gw is still
760 neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
765 curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
770 curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
771 batadv_neigh_ifinfo_put(curr_ifinfo);
774 case BATADV_GW_MODE_OFF:
779 neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
783 old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
787 if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
789 batadv_neigh_ifinfo_put(old_ifinfo);
793 batadv_orig_node_put(orig_dst_node);
795 batadv_gw_node_put(curr_gw);
797 batadv_gw_node_put(gw_node);
799 batadv_neigh_node_put(neigh_old);
801 batadv_neigh_node_put(neigh_curr);