Commit 859c2012 authored by Eric Dumazet's avatar Eric Dumazet Committed by David S. Miller

net_sched: cls_flow: use skb_header_pointer()

Dan Siemon would like to add tunnelling support to cls_flow

This preliminary patch introduces use of skb_header_pointer() to help
this task, while avoiding skb head reallocation because of deep packet
inspection.
Signed-off-by: default avatarEric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 59445b6b
...@@ -65,132 +65,134 @@ static inline u32 addr_fold(void *addr) ...@@ -65,132 +65,134 @@ static inline u32 addr_fold(void *addr)
return (a & 0xFFFFFFFF) ^ (BITS_PER_LONG > 32 ? a >> 32 : 0); return (a & 0xFFFFFFFF) ^ (BITS_PER_LONG > 32 ? a >> 32 : 0);
} }
static u32 flow_get_src(struct sk_buff *skb) static u32 flow_get_src(const struct sk_buff *skb, int nhoff)
{ {
__be32 *data = NULL, hdata;
switch (skb->protocol) { switch (skb->protocol) {
case htons(ETH_P_IP): case htons(ETH_P_IP):
if (pskb_network_may_pull(skb, sizeof(struct iphdr))) data = skb_header_pointer(skb,
return ntohl(ip_hdr(skb)->saddr); nhoff + offsetof(struct iphdr,
saddr),
4, &hdata);
break; break;
case htons(ETH_P_IPV6): case htons(ETH_P_IPV6):
if (pskb_network_may_pull(skb, sizeof(struct ipv6hdr))) data = skb_header_pointer(skb,
return ntohl(ipv6_hdr(skb)->saddr.s6_addr32[3]); nhoff + offsetof(struct ipv6hdr,
saddr.s6_addr32[3]),
4, &hdata);
break; break;
} }
if (data)
return ntohl(*data);
return addr_fold(skb->sk); return addr_fold(skb->sk);
} }
static u32 flow_get_dst(struct sk_buff *skb) static u32 flow_get_dst(const struct sk_buff *skb, int nhoff)
{ {
__be32 *data = NULL, hdata;
switch (skb->protocol) { switch (skb->protocol) {
case htons(ETH_P_IP): case htons(ETH_P_IP):
if (pskb_network_may_pull(skb, sizeof(struct iphdr))) data = skb_header_pointer(skb,
return ntohl(ip_hdr(skb)->daddr); nhoff + offsetof(struct iphdr,
daddr),
4, &hdata);
break; break;
case htons(ETH_P_IPV6): case htons(ETH_P_IPV6):
if (pskb_network_may_pull(skb, sizeof(struct ipv6hdr))) data = skb_header_pointer(skb,
return ntohl(ipv6_hdr(skb)->daddr.s6_addr32[3]); nhoff + offsetof(struct ipv6hdr,
daddr.s6_addr32[3]),
4, &hdata);
break; break;
} }
if (data)
return ntohl(*data);
return addr_fold(skb_dst(skb)) ^ (__force u16)skb->protocol; return addr_fold(skb_dst(skb)) ^ (__force u16)skb->protocol;
} }
static u32 flow_get_proto(struct sk_buff *skb) static u32 flow_get_proto(const struct sk_buff *skb, int nhoff)
{ {
__u8 *data = NULL, hdata;
switch (skb->protocol) { switch (skb->protocol) {
case htons(ETH_P_IP): case htons(ETH_P_IP):
return pskb_network_may_pull(skb, sizeof(struct iphdr)) ? data = skb_header_pointer(skb,
ip_hdr(skb)->protocol : 0; nhoff + offsetof(struct iphdr,
protocol),
1, &hdata);
break;
case htons(ETH_P_IPV6): case htons(ETH_P_IPV6):
return pskb_network_may_pull(skb, sizeof(struct ipv6hdr)) ? data = skb_header_pointer(skb,
ipv6_hdr(skb)->nexthdr : 0; nhoff + offsetof(struct ipv6hdr,
default: nexthdr),
return 0; 1, &hdata);
break;
} }
if (data)
return *data;
return 0;
} }
static u32 flow_get_proto_src(struct sk_buff *skb) /* helper function to get either src or dst port */
static __be16 *flow_get_proto_common(const struct sk_buff *skb, int nhoff,
__be16 *_port, int dst)
{ {
__be16 *port = NULL;
int poff;
switch (skb->protocol) { switch (skb->protocol) {
case htons(ETH_P_IP): { case htons(ETH_P_IP): {
struct iphdr *iph; struct iphdr *iph, _iph;
int poff;
if (!pskb_network_may_pull(skb, sizeof(*iph))) iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
if (!iph)
break; break;
iph = ip_hdr(skb);
if (ip_is_fragment(iph)) if (ip_is_fragment(iph))
break; break;
poff = proto_ports_offset(iph->protocol); poff = proto_ports_offset(iph->protocol);
if (poff >= 0 && if (poff >= 0)
pskb_network_may_pull(skb, iph->ihl * 4 + 2 + poff)) { port = skb_header_pointer(skb,
iph = ip_hdr(skb); nhoff + iph->ihl * 4 + poff + dst,
return ntohs(*(__be16 *)((void *)iph + iph->ihl * 4 + sizeof(*_port), _port);
poff));
}
break; break;
} }
case htons(ETH_P_IPV6): { case htons(ETH_P_IPV6): {
struct ipv6hdr *iph; struct ipv6hdr *iph, _iph;
int poff;
if (!pskb_network_may_pull(skb, sizeof(*iph))) iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
if (!iph)
break; break;
iph = ipv6_hdr(skb);
poff = proto_ports_offset(iph->nexthdr); poff = proto_ports_offset(iph->nexthdr);
if (poff >= 0 && if (poff >= 0)
pskb_network_may_pull(skb, sizeof(*iph) + poff + 2)) { port = skb_header_pointer(skb,
iph = ipv6_hdr(skb); nhoff + sizeof(*iph) + poff + dst,
return ntohs(*(__be16 *)((void *)iph + sizeof(*iph) + sizeof(*_port), _port);
poff));
}
break; break;
} }
} }
return addr_fold(skb->sk); return port;
} }
static u32 flow_get_proto_dst(struct sk_buff *skb) static u32 flow_get_proto_src(const struct sk_buff *skb, int nhoff)
{ {
switch (skb->protocol) { __be16 _port, *port = flow_get_proto_common(skb, nhoff, &_port, 0);
case htons(ETH_P_IP): {
struct iphdr *iph;
int poff;
if (!pskb_network_may_pull(skb, sizeof(*iph))) if (port)
break; return ntohs(*port);
iph = ip_hdr(skb);
if (ip_is_fragment(iph))
break;
poff = proto_ports_offset(iph->protocol);
if (poff >= 0 &&
pskb_network_may_pull(skb, iph->ihl * 4 + 4 + poff)) {
iph = ip_hdr(skb);
return ntohs(*(__be16 *)((void *)iph + iph->ihl * 4 +
2 + poff));
}
break;
}
case htons(ETH_P_IPV6): {
struct ipv6hdr *iph;
int poff;
if (!pskb_network_may_pull(skb, sizeof(*iph))) return addr_fold(skb->sk);
break; }
iph = ipv6_hdr(skb);
poff = proto_ports_offset(iph->nexthdr); static u32 flow_get_proto_dst(const struct sk_buff *skb, int nhoff)
if (poff >= 0 && {
pskb_network_may_pull(skb, sizeof(*iph) + poff + 4)) { __be16 _port, *port = flow_get_proto_common(skb, nhoff, &_port, 2);
iph = ipv6_hdr(skb);
return ntohs(*(__be16 *)((void *)iph + sizeof(*iph) + if (port)
poff + 2)); return ntohs(*port);
}
break;
}
}
return addr_fold(skb_dst(skb)) ^ (__force u16)skb->protocol; return addr_fold(skb_dst(skb)) ^ (__force u16)skb->protocol;
} }
...@@ -223,7 +225,7 @@ static u32 flow_get_nfct(const struct sk_buff *skb) ...@@ -223,7 +225,7 @@ static u32 flow_get_nfct(const struct sk_buff *skb)
#define CTTUPLE(skb, member) \ #define CTTUPLE(skb, member) \
({ \ ({ \
enum ip_conntrack_info ctinfo; \ enum ip_conntrack_info ctinfo; \
struct nf_conn *ct = nf_ct_get(skb, &ctinfo); \ const struct nf_conn *ct = nf_ct_get(skb, &ctinfo); \
if (ct == NULL) \ if (ct == NULL) \
goto fallback; \ goto fallback; \
ct->tuplehash[CTINFO2DIR(ctinfo)].tuple.member; \ ct->tuplehash[CTINFO2DIR(ctinfo)].tuple.member; \
...@@ -236,7 +238,7 @@ static u32 flow_get_nfct(const struct sk_buff *skb) ...@@ -236,7 +238,7 @@ static u32 flow_get_nfct(const struct sk_buff *skb)
}) })
#endif #endif
static u32 flow_get_nfct_src(struct sk_buff *skb) static u32 flow_get_nfct_src(const struct sk_buff *skb, int nhoff)
{ {
switch (skb->protocol) { switch (skb->protocol) {
case htons(ETH_P_IP): case htons(ETH_P_IP):
...@@ -245,10 +247,10 @@ static u32 flow_get_nfct_src(struct sk_buff *skb) ...@@ -245,10 +247,10 @@ static u32 flow_get_nfct_src(struct sk_buff *skb)
return ntohl(CTTUPLE(skb, src.u3.ip6[3])); return ntohl(CTTUPLE(skb, src.u3.ip6[3]));
} }
fallback: fallback:
return flow_get_src(skb); return flow_get_src(skb, nhoff);
} }
static u32 flow_get_nfct_dst(struct sk_buff *skb) static u32 flow_get_nfct_dst(const struct sk_buff *skb, int nhoff)
{ {
switch (skb->protocol) { switch (skb->protocol) {
case htons(ETH_P_IP): case htons(ETH_P_IP):
...@@ -257,21 +259,21 @@ static u32 flow_get_nfct_dst(struct sk_buff *skb) ...@@ -257,21 +259,21 @@ static u32 flow_get_nfct_dst(struct sk_buff *skb)
return ntohl(CTTUPLE(skb, dst.u3.ip6[3])); return ntohl(CTTUPLE(skb, dst.u3.ip6[3]));
} }
fallback: fallback:
return flow_get_dst(skb); return flow_get_dst(skb, nhoff);
} }
static u32 flow_get_nfct_proto_src(struct sk_buff *skb) static u32 flow_get_nfct_proto_src(const struct sk_buff *skb, int nhoff)
{ {
return ntohs(CTTUPLE(skb, src.u.all)); return ntohs(CTTUPLE(skb, src.u.all));
fallback: fallback:
return flow_get_proto_src(skb); return flow_get_proto_src(skb, nhoff);
} }
static u32 flow_get_nfct_proto_dst(struct sk_buff *skb) static u32 flow_get_nfct_proto_dst(const struct sk_buff *skb, int nhoff)
{ {
return ntohs(CTTUPLE(skb, dst.u.all)); return ntohs(CTTUPLE(skb, dst.u.all));
fallback: fallback:
return flow_get_proto_dst(skb); return flow_get_proto_dst(skb, nhoff);
} }
static u32 flow_get_rtclassid(const struct sk_buff *skb) static u32 flow_get_rtclassid(const struct sk_buff *skb)
...@@ -313,17 +315,19 @@ static u32 flow_get_rxhash(struct sk_buff *skb) ...@@ -313,17 +315,19 @@ static u32 flow_get_rxhash(struct sk_buff *skb)
static u32 flow_key_get(struct sk_buff *skb, int key) static u32 flow_key_get(struct sk_buff *skb, int key)
{ {
int nhoff = skb_network_offset(skb);
switch (key) { switch (key) {
case FLOW_KEY_SRC: case FLOW_KEY_SRC:
return flow_get_src(skb); return flow_get_src(skb, nhoff);
case FLOW_KEY_DST: case FLOW_KEY_DST:
return flow_get_dst(skb); return flow_get_dst(skb, nhoff);
case FLOW_KEY_PROTO: case FLOW_KEY_PROTO:
return flow_get_proto(skb); return flow_get_proto(skb, nhoff);
case FLOW_KEY_PROTO_SRC: case FLOW_KEY_PROTO_SRC:
return flow_get_proto_src(skb); return flow_get_proto_src(skb, nhoff);
case FLOW_KEY_PROTO_DST: case FLOW_KEY_PROTO_DST:
return flow_get_proto_dst(skb); return flow_get_proto_dst(skb, nhoff);
case FLOW_KEY_IIF: case FLOW_KEY_IIF:
return flow_get_iif(skb); return flow_get_iif(skb);
case FLOW_KEY_PRIORITY: case FLOW_KEY_PRIORITY:
...@@ -333,13 +337,13 @@ static u32 flow_key_get(struct sk_buff *skb, int key) ...@@ -333,13 +337,13 @@ static u32 flow_key_get(struct sk_buff *skb, int key)
case FLOW_KEY_NFCT: case FLOW_KEY_NFCT:
return flow_get_nfct(skb); return flow_get_nfct(skb);
case FLOW_KEY_NFCT_SRC: case FLOW_KEY_NFCT_SRC:
return flow_get_nfct_src(skb); return flow_get_nfct_src(skb, nhoff);
case FLOW_KEY_NFCT_DST: case FLOW_KEY_NFCT_DST:
return flow_get_nfct_dst(skb); return flow_get_nfct_dst(skb, nhoff);
case FLOW_KEY_NFCT_PROTO_SRC: case FLOW_KEY_NFCT_PROTO_SRC:
return flow_get_nfct_proto_src(skb); return flow_get_nfct_proto_src(skb, nhoff);
case FLOW_KEY_NFCT_PROTO_DST: case FLOW_KEY_NFCT_PROTO_DST:
return flow_get_nfct_proto_dst(skb); return flow_get_nfct_proto_dst(skb, nhoff);
case FLOW_KEY_RTCLASSID: case FLOW_KEY_RTCLASSID:
return flow_get_rtclassid(skb); return flow_get_rtclassid(skb);
case FLOW_KEY_SKUID: case FLOW_KEY_SKUID:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment