Skip to content

Commit be7af5c

Browse files
Marek Lindnerecsv
authored andcommitted
batman-adv: refactoring gateway handling code
Signed-off-by: Marek Lindner <lindner_marek@yahoo.de> Acked-by: Antonio Quartulli <ordex@autistici.org> Signed-off-by: Sven Eckelmann <sven@narfation.org>
1 parent 25a92b1 commit be7af5c

3 files changed

Lines changed: 126 additions & 75 deletions

File tree

‎net/batman-adv/gateway_client.c‎

Lines changed: 94 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "gateway_common.h"
2626
#include "hard-interface.h"
2727
#include "originator.h"
28+
#include "translation-table.h"
2829
#include "routing.h"
2930
#include <linux/ip.h>
3031
#include <linux/ipv6.h>
@@ -572,108 +573,142 @@ static bool is_type_dhcprequest(struct sk_buff *skb, int header_len)
572573
return ret;
573574
}
574575

575-
int gw_is_target(struct bat_priv *bat_priv, struct sk_buff *skb,
576-
struct orig_node *old_gw)
576+
bool gw_is_dhcp_target(struct sk_buff *skb, unsigned int *header_len)
577577
{
578578
struct ethhdr *ethhdr;
579579
struct iphdr *iphdr;
580580
struct ipv6hdr *ipv6hdr;
581581
struct udphdr *udphdr;
582-
struct gw_node *curr_gw;
583-
struct neigh_node *neigh_curr = NULL, *neigh_old = NULL;
584-
unsigned int header_len = 0;
585-
int ret = 1;
586-
587-
if (atomic_read(&bat_priv->gw_mode) == GW_MODE_OFF)
588-
return 0;
589582

590583
/* check for ethernet header */
591-
if (!pskb_may_pull(skb, header_len + ETH_HLEN))
592-
return 0;
584+
if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
585+
return false;
593586
ethhdr = (struct ethhdr *)skb->data;
594-
header_len += ETH_HLEN;
587+
*header_len += ETH_HLEN;
595588

596589
/* check for initial vlan header */
597590
if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
598-
if (!pskb_may_pull(skb, header_len + VLAN_HLEN))
599-
return 0;
591+
if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
592+
return false;
600593
ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
601-
header_len += VLAN_HLEN;
594+
*header_len += VLAN_HLEN;
602595
}
603596

604597
/* check for ip header */
605598
switch (ntohs(ethhdr->h_proto)) {
606599
case ETH_P_IP:
607-
if (!pskb_may_pull(skb, header_len + sizeof(*iphdr)))
608-
return 0;
609-
iphdr = (struct iphdr *)(skb->data + header_len);
610-
header_len += iphdr->ihl * 4;
600+
if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
601+
return false;
602+
iphdr = (struct iphdr *)(skb->data + *header_len);
603+
*header_len += iphdr->ihl * 4;
611604

612605
/* check for udp header */
613606
if (iphdr->protocol != IPPROTO_UDP)
614-
return 0;
607+
return false;
615608

616609
break;
617610
case ETH_P_IPV6:
618-
if (!pskb_may_pull(skb, header_len + sizeof(*ipv6hdr)))
619-
return 0;
620-
ipv6hdr = (struct ipv6hdr *)(skb->data + header_len);
621-
header_len += sizeof(*ipv6hdr);
611+
if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
612+
return false;
613+
ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
614+
*header_len += sizeof(*ipv6hdr);
622615

623616
/* check for udp header */
624617
if (ipv6hdr->nexthdr != IPPROTO_UDP)
625-
return 0;
618+
return false;
626619

627620
break;
628621
default:
629-
return 0;
622+
return false;
630623
}
631624

632-
if (!pskb_may_pull(skb, header_len + sizeof(*udphdr)))
633-
return 0;
634-
udphdr = (struct udphdr *)(skb->data + header_len);
635-
header_len += sizeof(*udphdr);
625+
if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
626+
return false;
627+
udphdr = (struct udphdr *)(skb->data + *header_len);
628+
*header_len += sizeof(*udphdr);
636629

637630
/* check for bootp port */
638631
if ((ntohs(ethhdr->h_proto) == ETH_P_IP) &&
639632
(ntohs(udphdr->dest) != 67))
640-
return 0;
633+
return false;
641634

642635
if ((ntohs(ethhdr->h_proto) == ETH_P_IPV6) &&
643636
(ntohs(udphdr->dest) != 547))
644-
return 0;
637+
return false;
645638

646-
if (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER)
647-
return -1;
639+
return true;
640+
}
648641

649-
curr_gw = gw_get_selected_gw_node(bat_priv);
650-
if (!curr_gw)
651-
return 0;
652-
653-
/* If old_gw != NULL then this packet is unicast.
654-
* So, at this point we have to check the message type: if it is a
655-
* DHCPREQUEST we have to decide whether to drop it or not */
656-
if (old_gw && curr_gw->orig_node != old_gw) {
657-
if (is_type_dhcprequest(skb, header_len)) {
658-
/* If the dhcp packet has been sent to a different gw,
659-
* we have to evaluate whether the old gw is still
660-
* reliable enough */
661-
neigh_curr = find_router(bat_priv, curr_gw->orig_node,
662-
NULL);
663-
neigh_old = find_router(bat_priv, old_gw, NULL);
664-
if (!neigh_curr || !neigh_old)
665-
goto free_neigh;
666-
if (neigh_curr->tq_avg - neigh_old->tq_avg <
667-
GW_THRESHOLD)
668-
ret = -1;
669-
}
642+
bool gw_out_of_range(struct bat_priv *bat_priv,
643+
struct sk_buff *skb, struct ethhdr *ethhdr)
644+
{
645+
struct neigh_node *neigh_curr = NULL, *neigh_old = NULL;
646+
struct orig_node *orig_dst_node = NULL;
647+
struct gw_node *curr_gw = NULL;
648+
bool ret, out_of_range = false;
649+
unsigned int header_len = 0;
650+
uint8_t curr_tq_avg;
651+
652+
ret = gw_is_dhcp_target(skb, &header_len);
653+
if (!ret)
654+
goto out;
655+
656+
orig_dst_node = transtable_search(bat_priv, ethhdr->h_source,
657+
ethhdr->h_dest);
658+
if (!orig_dst_node)
659+
goto out;
660+
661+
if (!orig_dst_node->gw_flags)
662+
goto out;
663+
664+
ret = is_type_dhcprequest(skb, header_len);
665+
if (!ret)
666+
goto out;
667+
668+
switch (atomic_read(&bat_priv->gw_mode)) {
669+
case GW_MODE_SERVER:
670+
/* If we are a GW then we are our best GW. We can artificially
671+
* set the tq towards ourself as the maximum value */
672+
curr_tq_avg = TQ_MAX_VALUE;
673+
break;
674+
case GW_MODE_CLIENT:
675+
curr_gw = gw_get_selected_gw_node(bat_priv);
676+
if (!curr_gw)
677+
goto out;
678+
679+
/* packet is going to our gateway */
680+
if (curr_gw->orig_node == orig_dst_node)
681+
goto out;
682+
683+
/* If the dhcp packet has been sent to a different gw,
684+
* we have to evaluate whether the old gw is still
685+
* reliable enough */
686+
neigh_curr = find_router(bat_priv, curr_gw->orig_node, NULL);
687+
if (!neigh_curr)
688+
goto out;
689+
690+
curr_tq_avg = neigh_curr->tq_avg;
691+
break;
692+
case GW_MODE_OFF:
693+
default:
694+
goto out;
670695
}
671-
free_neigh:
696+
697+
neigh_old = find_router(bat_priv, orig_dst_node, NULL);
698+
if (!!neigh_old)
699+
goto out;
700+
701+
if (curr_tq_avg - neigh_old->tq_avg > GW_THRESHOLD)
702+
out_of_range = true;
703+
704+
out:
705+
if (orig_dst_node)
706+
orig_node_free_ref(orig_dst_node);
707+
if (curr_gw)
708+
gw_node_free_ref(curr_gw);
672709
if (neigh_old)
673710
neigh_node_free_ref(neigh_old);
674711
if (neigh_curr)
675712
neigh_node_free_ref(neigh_curr);
676-
if (curr_gw)
677-
gw_node_free_ref(curr_gw);
678-
return ret;
713+
return out_of_range;
679714
}

‎net/batman-adv/gateway_client.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ void gw_node_update(struct bat_priv *bat_priv,
3131
void gw_node_delete(struct bat_priv *bat_priv, struct orig_node *orig_node);
3232
void gw_node_purge(struct bat_priv *bat_priv);
3333
int gw_client_seq_print_text(struct seq_file *seq, void *offset);
34-
int gw_is_target(struct bat_priv *bat_priv, struct sk_buff *skb,
35-
struct orig_node *old_gw);
34+
bool gw_is_dhcp_target(struct sk_buff *skb, unsigned int *header_len);
35+
bool gw_out_of_range(struct bat_priv *bat_priv,
36+
struct sk_buff *skb, struct ethhdr *ethhdr);
3637

3738
#endif /* _NET_BATMAN_ADV_GATEWAY_CLIENT_H_ */

‎net/batman-adv/soft-interface.c‎

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -563,10 +563,10 @@ static int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
563563
struct bcast_packet *bcast_packet;
564564
struct vlan_ethhdr *vhdr;
565565
struct softif_neigh *curr_softif_neigh = NULL;
566-
struct orig_node *orig_node = NULL;
566+
unsigned int header_len = 0;
567567
int data_len = skb->len, ret;
568568
short vid = -1;
569-
bool do_bcast;
569+
bool do_bcast = false;
570570

571571
if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
572572
goto dropped;
@@ -598,17 +598,28 @@ static int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
598598
/* Register the client MAC in the transtable */
599599
tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
600600

601-
orig_node = transtable_search(bat_priv, ethhdr->h_source,
602-
ethhdr->h_dest);
603-
do_bcast = is_multicast_ether_addr(ethhdr->h_dest);
604-
if (do_bcast || (orig_node && orig_node->gw_flags)) {
605-
ret = gw_is_target(bat_priv, skb, orig_node);
601+
if (is_multicast_ether_addr(ethhdr->h_dest)) {
602+
do_bcast = true;
606603

607-
if (ret < 0)
608-
goto dropped;
609-
610-
if (ret)
611-
do_bcast = false;
604+
switch (atomic_read(&bat_priv->gw_mode)) {
605+
case GW_MODE_SERVER:
606+
/* gateway servers should not send dhcp
607+
* requests into the mesh */
608+
ret = gw_is_dhcp_target(skb, &header_len);
609+
if (ret)
610+
goto dropped;
611+
break;
612+
case GW_MODE_CLIENT:
613+
/* gateway clients should send dhcp requests
614+
* via unicast to their gateway */
615+
ret = gw_is_dhcp_target(skb, &header_len);
616+
if (ret)
617+
do_bcast = false;
618+
break;
619+
case GW_MODE_OFF:
620+
default:
621+
break;
622+
}
612623
}
613624

614625
/* ethernet packet should be broadcasted */
@@ -644,6 +655,12 @@ static int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
644655

645656
/* unicast packet */
646657
} else {
658+
if (atomic_read(&bat_priv->gw_mode) != GW_MODE_OFF) {
659+
ret = gw_out_of_range(bat_priv, skb, ethhdr);
660+
if (ret)
661+
goto dropped;
662+
}
663+
647664
ret = unicast_send_skb(skb, bat_priv);
648665
if (ret != 0)
649666
goto dropped_freed;
@@ -662,8 +679,6 @@ static int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
662679
softif_neigh_free_ref(curr_softif_neigh);
663680
if (primary_if)
664681
hardif_free_ref(primary_if);
665-
if (orig_node)
666-
orig_node_free_ref(orig_node);
667682
return NETDEV_TX_OK;
668683
}
669684

0 commit comments

Comments
 (0)