* [B.A.T.M.A.N.] [PATCHv2] batman-adv: h_vlan_encapsulated_proto access refactoring
@ 2013-05-12 19:51 Antonio Quartulli
2013-05-18 9:59 ` Marek Lindner
0 siblings, 1 reply; 2+ messages in thread
From: Antonio Quartulli @ 2013-05-12 19:51 UTC (permalink / raw)
To: b.a.t.m.a.n; +Cc: Antonio Quartulli
In the gateway code in case of VLAN tagged frame the ethhdr
pointer is moved forward by 4 bytes so that the offset of
h_proto in struct ethhdr matches the real
h_vlan_encapsulated_proto address in the skb.
This trick is correct but the code is not easy to understand
and may lead to bugs in case of re-use of ethhdr for other
purposes.
Do some code refactoring in order to make it easier to
understand and to change in the future.
Signed-off-by: Antonio Quartulli <ordex@autistici.org>
---
v2:
- slightly change the commit subject and message
- use __constant_htons() when comparing proto with constants
gateway_client.c | 20 +++++++++++++-------
1 file changed, 13 insertions(+), 7 deletions(-)
diff --git a/gateway_client.c b/gateway_client.c
index 973f02d..f59e58d 100644
--- a/gateway_client.c
+++ b/gateway_client.c
@@ -626,24 +626,30 @@ bool batadv_gw_is_dhcp_target(struct sk_buff *skb, unsigned int *header_len)
struct iphdr *iphdr;
struct ipv6hdr *ipv6hdr;
struct udphdr *udphdr;
+ __be16 proto;
/* check for ethernet header */
if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
return false;
ethhdr = (struct ethhdr *)skb->data;
+ proto = ethhdr->h_proto;
*header_len += ETH_HLEN;
/* check for initial vlan header */
- if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
+ if (proto == __constant_htons(ETH_P_8021Q)) {
+ struct vlan_ethhdr *vhdr;
+
if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
return false;
- ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
+
+ vhdr = (struct vlan_ethhdr *)skb->data;
+ proto = vhdr->h_vlan_encapsulated_proto;
*header_len += VLAN_HLEN;
}
/* check for ip header */
- switch (ntohs(ethhdr->h_proto)) {
- case ETH_P_IP:
+ switch (proto) {
+ case __constant_htons(ETH_P_IP):
if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
return false;
iphdr = (struct iphdr *)(skb->data + *header_len);
@@ -654,7 +660,7 @@ bool batadv_gw_is_dhcp_target(struct sk_buff *skb, unsigned int *header_len)
return false;
break;
- case ETH_P_IPV6:
+ case __constant_htons(ETH_P_IPV6):
if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
return false;
ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
@@ -675,11 +681,11 @@ bool batadv_gw_is_dhcp_target(struct sk_buff *skb, unsigned int *header_len)
*header_len += sizeof(*udphdr);
/* check for bootp port */
- if ((ntohs(ethhdr->h_proto) == ETH_P_IP) &&
+ if ((proto == __constant_htons(ETH_P_IP)) &&
(ntohs(udphdr->dest) != 67))
return false;
- if ((ntohs(ethhdr->h_proto) == ETH_P_IPV6) &&
+ if ((proto == __constant_htons(ETH_P_IPV6)) &&
(ntohs(udphdr->dest) != 547))
return false;
--
1.8.1.5
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [B.A.T.M.A.N.] [PATCHv2] batman-adv: h_vlan_encapsulated_proto access refactoring
2013-05-12 19:51 [B.A.T.M.A.N.] [PATCHv2] batman-adv: h_vlan_encapsulated_proto access refactoring Antonio Quartulli
@ 2013-05-18 9:59 ` Marek Lindner
0 siblings, 0 replies; 2+ messages in thread
From: Marek Lindner @ 2013-05-18 9:59 UTC (permalink / raw)
To: b.a.t.m.a.n; +Cc: Antonio Quartulli
On Monday, May 13, 2013 03:51:15 Antonio Quartulli wrote:
> In the gateway code in case of VLAN tagged frame the ethhdr
> pointer is moved forward by 4 bytes so that the offset of
> h_proto in struct ethhdr matches the real
> h_vlan_encapsulated_proto address in the skb.
>
> This trick is correct but the code is not easy to understand
> and may lead to bugs in case of re-use of ethhdr for other
> purposes.
>
> Do some code refactoring in order to make it easier to
> understand and to change in the future.
>
> Signed-off-by: Antonio Quartulli <ordex@autistici.org>
> ---
>
> v2:
> - slightly change the commit subject and message
> - use __constant_htons() when comparing proto with constants
>
>
>
> gateway_client.c | 20 +++++++++++++-------
> 1 file changed, 13 insertions(+), 7 deletions(-)
Applied in revision 9eff62f.
Thanks,
Marek
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2013-05-18 9:59 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-12 19:51 [B.A.T.M.A.N.] [PATCHv2] batman-adv: h_vlan_encapsulated_proto access refactoring Antonio Quartulli
2013-05-18 9:59 ` Marek Lindner
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox