2
* Copyright (c) 2007-2013 Nicira, Inc.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of version 2 of the GNU General Public
6
* License as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18
* This code is derived from kernel flow_dissector.c
21
#include <linux/version.h>
22
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
24
#include <linux/ipv6.h>
25
#include <linux/if_vlan.h>
28
#include <linux/igmp.h>
29
#include <linux/icmp.h>
30
#include <linux/sctp.h>
31
#include <linux/dccp.h>
32
#include <linux/if_tunnel.h>
33
#include <linux/if_pppox.h>
34
#include <linux/ppp_defs.h>
35
#include <net/flow_keys.h>
38
/* copy saddr & daddr, possibly using 64bit load/store
39
* Equivalent to : flow->src = iph->saddr;
40
* flow->dst = iph->daddr;
42
static void iph_to_flow_copy_addrs(struct flow_keys *flow, const struct iphdr *iph)
44
BUILD_BUG_ON(offsetof(typeof(*flow), dst) !=
45
offsetof(typeof(*flow), src) + sizeof(flow->src));
46
memcpy(&flow->src, &iph->saddr, sizeof(flow->src) + sizeof(flow->dst));
49
static bool skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow)
51
int poff, nhoff = skb_network_offset(skb);
53
__be16 proto = skb->protocol;
55
memset(flow, 0, sizeof(*flow));
59
case __constant_htons(ETH_P_IP): {
60
const struct iphdr *iph;
63
iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
67
if (ip_is_fragment(iph))
70
ip_proto = iph->protocol;
71
iph_to_flow_copy_addrs(flow, iph);
72
nhoff += iph->ihl * 4;
75
case __constant_htons(ETH_P_IPV6): {
76
const struct ipv6hdr *iph;
79
iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
83
ip_proto = iph->nexthdr;
84
flow->src = (__force __be32)ipv6_addr_hash(&iph->saddr);
85
flow->dst = (__force __be32)ipv6_addr_hash(&iph->daddr);
86
nhoff += sizeof(struct ipv6hdr);
89
case __constant_htons(ETH_P_8021Q): {
90
const struct vlan_hdr *vlan;
91
struct vlan_hdr _vlan;
93
vlan = skb_header_pointer(skb, nhoff, sizeof(_vlan), &_vlan);
97
proto = vlan->h_vlan_encapsulated_proto;
98
nhoff += sizeof(*vlan);
101
case __constant_htons(ETH_P_PPP_SES): {
103
struct pppoe_hdr hdr;
106
hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
110
nhoff += PPPOE_SES_HLEN;
112
case __constant_htons(PPP_IP):
114
case __constant_htons(PPP_IPV6):
131
hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
135
* Only look inside GRE if version zero and no
138
if (!(hdr->flags & (GRE_VERSION|GRE_ROUTING))) {
141
if (hdr->flags & GRE_CSUM)
143
if (hdr->flags & GRE_KEY)
145
if (hdr->flags & GRE_SEQ)
147
if (proto == htons(ETH_P_TEB)) {
148
const struct ethhdr *eth;
151
eth = skb_header_pointer(skb, nhoff,
152
sizeof(_eth), &_eth);
155
proto = eth->h_proto;
156
nhoff += sizeof(*eth);
168
flow->ip_proto = ip_proto;
169
poff = proto_ports_offset(ip_proto);
171
__be32 *ports, _ports;
174
ports = skb_header_pointer(skb, nhoff, sizeof(_ports), &_ports);
176
flow->ports = *ports;
179
flow->thoff = (u16) nhoff;
184
static u32 hashrnd __read_mostly;
186
static void init_hashrnd(void)
190
get_random_bytes(&hashrnd, sizeof(hashrnd));
193
u32 __skb_get_rxhash(struct sk_buff *skb)
195
struct flow_keys keys;
198
if (!skb_flow_dissect(skb, &keys))
201
/* get a consistent hash (same value on both flow directions) */
202
if (((__force u32)keys.dst < (__force u32)keys.src) ||
203
(((__force u32)keys.dst == (__force u32)keys.src) &&
204
((__force u16)keys.port16[1] < (__force u16)keys.port16[0]))) {
205
swap(keys.dst, keys.src);
206
swap(keys.port16[0], keys.port16[1]);
211
hash = jhash_3words((__force u32)keys.dst,
212
(__force u32)keys.src,
213
(__force u32)keys.ports, hashrnd);
217
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)