datapath: Add support for kernel 3.14.
[sliver-openvswitch.git] / datapath / linux / compat / flow_dissector.c
1 /*
2  * Copyright (c) 2007-2013 Nicira, Inc.
3  *
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.
7  *
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.
12  *
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
16  * 02110-1301, USA
17  *
18  * This code is derived from kernel flow_dissector.c
19  */
20
21 #include <linux/version.h>
22 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0)
23 #include <linux/ip.h>
24 #include <linux/ipv6.h>
25 #include <linux/if_vlan.h>
26 #include <net/ip.h>
27 #include <net/ipv6.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>
36
37
38 /* copy saddr & daddr, possibly using 64bit load/store
39  * Equivalent to :      flow->src = iph->saddr;
40  *                      flow->dst = iph->daddr;
41  */
42 static void iph_to_flow_copy_addrs(struct flow_keys *flow, const struct iphdr *iph)
43 {
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));
47 }
48
49 static __be32 skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto)
50 {
51         int poff = proto_ports_offset(ip_proto);
52
53         if (poff >= 0) {
54                 __be32 *ports, _ports;
55
56                 ports = skb_header_pointer(skb, thoff + poff,
57                                 sizeof(_ports), &_ports);
58                 if (ports)
59                         return *ports;
60         }
61
62         return 0;
63 }
64
65 static bool skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow)
66 {
67         int nhoff = skb_network_offset(skb);
68         u8 ip_proto;
69         __be16 proto = skb->protocol;
70
71         memset(flow, 0, sizeof(*flow));
72
73 again:
74         switch (proto) {
75         case __constant_htons(ETH_P_IP): {
76                 const struct iphdr *iph;
77                 struct iphdr _iph;
78 ip:
79                 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
80                 if (!iph)
81                         return false;
82
83                 if (ip_is_fragment(iph))
84                         ip_proto = 0;
85                 else
86                         ip_proto = iph->protocol;
87                 iph_to_flow_copy_addrs(flow, iph);
88                 nhoff += iph->ihl * 4;
89                 break;
90         }
91         case __constant_htons(ETH_P_IPV6): {
92                 const struct ipv6hdr *iph;
93                 struct ipv6hdr _iph;
94 ipv6:
95                 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
96                 if (!iph)
97                         return false;
98
99                 ip_proto = iph->nexthdr;
100                 flow->src = (__force __be32)ipv6_addr_hash(&iph->saddr);
101                 flow->dst = (__force __be32)ipv6_addr_hash(&iph->daddr);
102                 nhoff += sizeof(struct ipv6hdr);
103                 break;
104         }
105         case __constant_htons(ETH_P_8021AD):
106         case __constant_htons(ETH_P_8021Q): {
107                 const struct vlan_hdr *vlan;
108                 struct vlan_hdr _vlan;
109
110                 vlan = skb_header_pointer(skb, nhoff, sizeof(_vlan), &_vlan);
111                 if (!vlan)
112                         return false;
113
114                 proto = vlan->h_vlan_encapsulated_proto;
115                 nhoff += sizeof(*vlan);
116                 goto again;
117         }
118         case __constant_htons(ETH_P_PPP_SES): {
119                 struct {
120                         struct pppoe_hdr hdr;
121                         __be16 proto;
122                 } *hdr, _hdr;
123                 hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
124                 if (!hdr)
125                         return false;
126                 proto = hdr->proto;
127                 nhoff += PPPOE_SES_HLEN;
128                 switch (proto) {
129                 case __constant_htons(PPP_IP):
130                         goto ip;
131                 case __constant_htons(PPP_IPV6):
132                         goto ipv6;
133                 default:
134                         return false;
135                 }
136         }
137         default:
138                 return false;
139         }
140
141         switch (ip_proto) {
142         case IPPROTO_GRE: {
143                 struct gre_hdr {
144                         __be16 flags;
145                         __be16 proto;
146                 } *hdr, _hdr;
147
148                 hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
149                 if (!hdr)
150                         return false;
151                 /*
152                  * Only look inside GRE if version zero and no
153                  * routing
154                  */
155                 if (!(hdr->flags & (GRE_VERSION|GRE_ROUTING))) {
156                         proto = hdr->proto;
157                         nhoff += 4;
158                         if (hdr->flags & GRE_CSUM)
159                                 nhoff += 4;
160                         if (hdr->flags & GRE_KEY)
161                                 nhoff += 4;
162                         if (hdr->flags & GRE_SEQ)
163                                 nhoff += 4;
164                         if (proto == htons(ETH_P_TEB)) {
165                                 const struct ethhdr *eth;
166                                 struct ethhdr _eth;
167
168                                 eth = skb_header_pointer(skb, nhoff,
169                                                          sizeof(_eth), &_eth);
170                                 if (!eth)
171                                         return false;
172                                 proto = eth->h_proto;
173                                 nhoff += sizeof(*eth);
174                         }
175                         goto again;
176                 }
177                 break;
178         }
179         case IPPROTO_IPIP:
180                 goto again;
181         case IPPROTO_IPV6:
182                 proto = htons(ETH_P_IPV6);
183                 goto ipv6;
184         default:
185                 break;
186         }
187
188         flow->ip_proto = ip_proto;
189         flow->ports = skb_flow_get_ports(skb, nhoff, ip_proto);
190         flow->thoff = (u16) nhoff;
191
192         return true;
193 }
194
195 static u32 hashrnd __read_mostly;
196 static __always_inline void __flow_hash_secret_init(void)
197 {
198         net_get_random_once(&hashrnd, sizeof(hashrnd));
199 }
200
201 static __always_inline u32 __flow_hash_3words(u32 a, u32 b, u32 c)
202 {
203         __flow_hash_secret_init();
204         return jhash_3words(a, b, c, hashrnd);
205 }
206
207 u32 __skb_get_hash(struct sk_buff *skb)
208 {
209         struct flow_keys keys;
210         u32 hash;
211
212         if (!skb_flow_dissect(skb, &keys))
213                 return 0;
214
215         /* get a consistent hash (same value on both flow directions) */
216         if (((__force u32)keys.dst < (__force u32)keys.src) ||
217             (((__force u32)keys.dst == (__force u32)keys.src) &&
218              ((__force u16)keys.port16[1] < (__force u16)keys.port16[0]))) {
219                 swap(keys.dst, keys.src);
220                 swap(keys.port16[0], keys.port16[1]);
221         }
222
223         hash = __flow_hash_3words((__force u32)keys.dst,
224                                   (__force u32)keys.src,
225                                   (__force u32)keys.ports);
226         if (!hash)
227                 hash = 1;
228
229 #ifdef HAVE_RXHASH
230         skb->rxhash = hash;
231 #endif
232         return hash;
233 }
234 #endif