blob: d885e282908e91a1bba5e57b2a4c4edbb4757c5d [file] [log] [blame]
Eric Dumazet0744dd02011-11-28 05:22:18 +00001#include <linux/skbuff.h>
Jesper Dangaard Brouerc452ed72012-01-24 16:03:33 -05002#include <linux/export.h>
Eric Dumazet0744dd02011-11-28 05:22:18 +00003#include <linux/ip.h>
4#include <linux/ipv6.h>
5#include <linux/if_vlan.h>
6#include <net/ip.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +00007#include <net/ipv6.h>
Daniel Borkmannf77668d2013-03-19 06:39:30 +00008#include <linux/igmp.h>
9#include <linux/icmp.h>
10#include <linux/sctp.h>
11#include <linux/dccp.h>
Eric Dumazet0744dd02011-11-28 05:22:18 +000012#include <linux/if_tunnel.h>
13#include <linux/if_pppox.h>
14#include <linux/ppp_defs.h>
Jiri Pirko1bd758e2015-05-12 14:56:07 +020015#include <net/flow_dissector.h>
Alexander Duyck56193d12014-09-05 19:20:26 -040016#include <scsi/fc/fc_fcoe.h>
Eric Dumazet0744dd02011-11-28 05:22:18 +000017
Eric Dumazet4d77d2b2011-11-28 20:30:35 +000018/* copy saddr & daddr, possibly using 64bit load/store
19 * Equivalent to : flow->src = iph->saddr;
20 * flow->dst = iph->daddr;
21 */
22static void iph_to_flow_copy_addrs(struct flow_keys *flow, const struct iphdr *iph)
23{
24 BUILD_BUG_ON(offsetof(typeof(*flow), dst) !=
25 offsetof(typeof(*flow), src) + sizeof(flow->src));
26 memcpy(&flow->src, &iph->saddr, sizeof(flow->src) + sizeof(flow->dst));
27}
Eric Dumazet0744dd02011-11-28 05:22:18 +000028
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020029/**
WANG Cong6451b3f2014-08-25 17:03:46 -070030 * __skb_flow_get_ports - extract the upper layer ports and return them
31 * @skb: sk_buff to extract the ports from
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020032 * @thoff: transport header offset
33 * @ip_proto: protocol for which to get port offset
WANG Cong6451b3f2014-08-25 17:03:46 -070034 * @data: raw buffer pointer to the packet, if NULL use skb->data
35 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020036 *
37 * The function will try to retrieve the ports at offset thoff + poff where poff
38 * is the protocol port offset returned from proto_ports_offset
39 */
David S. Miller690e36e2014-08-23 12:13:41 -070040__be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
41 void *data, int hlen)
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020042{
43 int poff = proto_ports_offset(ip_proto);
44
David S. Miller690e36e2014-08-23 12:13:41 -070045 if (!data) {
46 data = skb->data;
47 hlen = skb_headlen(skb);
48 }
49
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020050 if (poff >= 0) {
51 __be32 *ports, _ports;
52
David S. Miller690e36e2014-08-23 12:13:41 -070053 ports = __skb_header_pointer(skb, thoff + poff,
54 sizeof(_ports), data, hlen, &_ports);
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020055 if (ports)
56 return *ports;
57 }
58
59 return 0;
60}
David S. Miller690e36e2014-08-23 12:13:41 -070061EXPORT_SYMBOL(__skb_flow_get_ports);
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020062
WANG Cong453a9402014-08-25 17:03:47 -070063/**
64 * __skb_flow_dissect - extract the flow_keys struct and return it
65 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
66 * @data: raw buffer pointer to the packet, if NULL use skb->data
67 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
68 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
69 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
70 *
71 * The function will try to retrieve the struct flow_keys from either the skbuff
72 * or a raw buffer specified by the rest parameters
73 */
74bool __skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow,
75 void *data, __be16 proto, int nhoff, int hlen)
Eric Dumazet0744dd02011-11-28 05:22:18 +000076{
Eric Dumazet0744dd02011-11-28 05:22:18 +000077 u8 ip_proto;
Eric Dumazet0744dd02011-11-28 05:22:18 +000078
David S. Miller690e36e2014-08-23 12:13:41 -070079 if (!data) {
80 data = skb->data;
WANG Cong453a9402014-08-25 17:03:47 -070081 proto = skb->protocol;
82 nhoff = skb_network_offset(skb);
David S. Miller690e36e2014-08-23 12:13:41 -070083 hlen = skb_headlen(skb);
84 }
85
Eric Dumazet0744dd02011-11-28 05:22:18 +000086 memset(flow, 0, sizeof(*flow));
87
88again:
89 switch (proto) {
Joe Perches2b8837a2014-03-12 10:04:17 -070090 case htons(ETH_P_IP): {
Eric Dumazet0744dd02011-11-28 05:22:18 +000091 const struct iphdr *iph;
92 struct iphdr _iph;
93ip:
David S. Miller690e36e2014-08-23 12:13:41 -070094 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
Jason Wang6f092342013-11-01 15:01:10 +080095 if (!iph || iph->ihl < 5)
Eric Dumazet0744dd02011-11-28 05:22:18 +000096 return false;
Eric Dumazet3797d3e2013-11-07 08:37:28 -080097 nhoff += iph->ihl * 4;
Eric Dumazet0744dd02011-11-28 05:22:18 +000098
Eric Dumazet3797d3e2013-11-07 08:37:28 -080099 ip_proto = iph->protocol;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000100 if (ip_is_fragment(iph))
101 ip_proto = 0;
Eric Dumazet3797d3e2013-11-07 08:37:28 -0800102
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700103 /* skip the address processing if skb is NULL. The assumption
104 * here is that if there is no skb we are not looking for flow
105 * info but lengths and protocols.
106 */
107 if (!skb)
108 break;
109
Eric Dumazet4d77d2b2011-11-28 20:30:35 +0000110 iph_to_flow_copy_addrs(flow, iph);
Eric Dumazet0744dd02011-11-28 05:22:18 +0000111 break;
112 }
Joe Perches2b8837a2014-03-12 10:04:17 -0700113 case htons(ETH_P_IPV6): {
Eric Dumazet0744dd02011-11-28 05:22:18 +0000114 const struct ipv6hdr *iph;
115 struct ipv6hdr _iph;
Tom Herbert19469a82014-07-01 21:33:01 -0700116 __be32 flow_label;
117
Eric Dumazet0744dd02011-11-28 05:22:18 +0000118ipv6:
David S. Miller690e36e2014-08-23 12:13:41 -0700119 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
Eric Dumazet0744dd02011-11-28 05:22:18 +0000120 if (!iph)
121 return false;
122
123 ip_proto = iph->nexthdr;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000124 nhoff += sizeof(struct ipv6hdr);
Tom Herbert19469a82014-07-01 21:33:01 -0700125
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700126 /* see comment above in IPv4 section */
Alexander Duyck56193d12014-09-05 19:20:26 -0400127 if (!skb)
128 break;
129
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700130 flow->src = (__force __be32)ipv6_addr_hash(&iph->saddr);
131 flow->dst = (__force __be32)ipv6_addr_hash(&iph->daddr);
132
Tom Herbert19469a82014-07-01 21:33:01 -0700133 flow_label = ip6_flowlabel(iph);
134 if (flow_label) {
135 /* Awesome, IPv6 packet has a flow label so we can
136 * use that to represent the ports without any
137 * further dissection.
138 */
139 flow->n_proto = proto;
140 flow->ip_proto = ip_proto;
141 flow->ports = flow_label;
142 flow->thoff = (u16)nhoff;
143
144 return true;
145 }
146
Eric Dumazet0744dd02011-11-28 05:22:18 +0000147 break;
148 }
Joe Perches2b8837a2014-03-12 10:04:17 -0700149 case htons(ETH_P_8021AD):
150 case htons(ETH_P_8021Q): {
Eric Dumazet0744dd02011-11-28 05:22:18 +0000151 const struct vlan_hdr *vlan;
152 struct vlan_hdr _vlan;
153
David S. Miller690e36e2014-08-23 12:13:41 -0700154 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), data, hlen, &_vlan);
Eric Dumazet0744dd02011-11-28 05:22:18 +0000155 if (!vlan)
156 return false;
157
158 proto = vlan->h_vlan_encapsulated_proto;
159 nhoff += sizeof(*vlan);
160 goto again;
161 }
Joe Perches2b8837a2014-03-12 10:04:17 -0700162 case htons(ETH_P_PPP_SES): {
Eric Dumazet0744dd02011-11-28 05:22:18 +0000163 struct {
164 struct pppoe_hdr hdr;
165 __be16 proto;
166 } *hdr, _hdr;
David S. Miller690e36e2014-08-23 12:13:41 -0700167 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
Eric Dumazet0744dd02011-11-28 05:22:18 +0000168 if (!hdr)
169 return false;
170 proto = hdr->proto;
171 nhoff += PPPOE_SES_HLEN;
172 switch (proto) {
Joe Perches2b8837a2014-03-12 10:04:17 -0700173 case htons(PPP_IP):
Eric Dumazet0744dd02011-11-28 05:22:18 +0000174 goto ip;
Joe Perches2b8837a2014-03-12 10:04:17 -0700175 case htons(PPP_IPV6):
Eric Dumazet0744dd02011-11-28 05:22:18 +0000176 goto ipv6;
177 default:
178 return false;
179 }
180 }
Erik Hugne08bfc9c2015-01-22 17:10:32 +0100181 case htons(ETH_P_TIPC): {
182 struct {
183 __be32 pre[3];
184 __be32 srcnode;
185 } *hdr, _hdr;
186 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
187 if (!hdr)
188 return false;
189 flow->src = hdr->srcnode;
190 flow->dst = 0;
191 flow->n_proto = proto;
192 flow->thoff = (u16)nhoff;
193 return true;
194 }
Alexander Duyck56193d12014-09-05 19:20:26 -0400195 case htons(ETH_P_FCOE):
196 flow->thoff = (u16)(nhoff + FCOE_HEADER_LEN);
197 /* fall through */
Eric Dumazet0744dd02011-11-28 05:22:18 +0000198 default:
199 return false;
200 }
201
202 switch (ip_proto) {
203 case IPPROTO_GRE: {
204 struct gre_hdr {
205 __be16 flags;
206 __be16 proto;
207 } *hdr, _hdr;
208
David S. Miller690e36e2014-08-23 12:13:41 -0700209 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
Eric Dumazet0744dd02011-11-28 05:22:18 +0000210 if (!hdr)
211 return false;
212 /*
213 * Only look inside GRE if version zero and no
214 * routing
215 */
216 if (!(hdr->flags & (GRE_VERSION|GRE_ROUTING))) {
217 proto = hdr->proto;
218 nhoff += 4;
219 if (hdr->flags & GRE_CSUM)
220 nhoff += 4;
221 if (hdr->flags & GRE_KEY)
222 nhoff += 4;
223 if (hdr->flags & GRE_SEQ)
224 nhoff += 4;
Michael Daltone1733de2013-03-11 06:52:28 +0000225 if (proto == htons(ETH_P_TEB)) {
226 const struct ethhdr *eth;
227 struct ethhdr _eth;
228
David S. Miller690e36e2014-08-23 12:13:41 -0700229 eth = __skb_header_pointer(skb, nhoff,
230 sizeof(_eth),
231 data, hlen, &_eth);
Michael Daltone1733de2013-03-11 06:52:28 +0000232 if (!eth)
233 return false;
234 proto = eth->h_proto;
235 nhoff += sizeof(*eth);
236 }
Eric Dumazet0744dd02011-11-28 05:22:18 +0000237 goto again;
238 }
239 break;
240 }
241 case IPPROTO_IPIP:
Tom Herbertfca41892013-07-29 11:07:36 -0700242 proto = htons(ETH_P_IP);
243 goto ip;
Tom Herbertb438f942013-07-29 11:07:42 -0700244 case IPPROTO_IPV6:
245 proto = htons(ETH_P_IPV6);
246 goto ipv6;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000247 default:
248 break;
249 }
250
Govindarajulu Varadarajane0f31d82014-06-23 16:07:58 +0530251 flow->n_proto = proto;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000252 flow->ip_proto = ip_proto;
Daniel Borkmann8ed78162013-03-19 06:39:29 +0000253 flow->thoff = (u16) nhoff;
254
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700255 /* unless skb is set we don't need to record port info */
256 if (skb)
257 flow->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
258 data, hlen);
259
Eric Dumazet0744dd02011-11-28 05:22:18 +0000260 return true;
261}
David S. Miller690e36e2014-08-23 12:13:41 -0700262EXPORT_SYMBOL(__skb_flow_dissect);
Cong Wang441d9d32013-01-21 00:39:24 +0000263
264static u32 hashrnd __read_mostly;
Hannes Frederic Sowa66415cf2013-10-23 20:06:00 +0200265static __always_inline void __flow_hash_secret_init(void)
266{
267 net_get_random_once(&hashrnd, sizeof(hashrnd));
268}
269
Tom Herbert50fb7992015-05-01 11:30:12 -0700270static __always_inline u32 __flow_hash_3words(u32 a, u32 b, u32 c, u32 keyval)
Hannes Frederic Sowa66415cf2013-10-23 20:06:00 +0200271{
Tom Herbert50fb7992015-05-01 11:30:12 -0700272 return jhash_3words(a, b, c, keyval);
Hannes Frederic Sowa66415cf2013-10-23 20:06:00 +0200273}
274
Tom Herbert50fb7992015-05-01 11:30:12 -0700275static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
Tom Herbert5ed20a62014-07-01 21:32:05 -0700276{
277 u32 hash;
278
279 /* get a consistent hash (same value on both flow directions) */
280 if (((__force u32)keys->dst < (__force u32)keys->src) ||
281 (((__force u32)keys->dst == (__force u32)keys->src) &&
282 ((__force u16)keys->port16[1] < (__force u16)keys->port16[0]))) {
283 swap(keys->dst, keys->src);
284 swap(keys->port16[0], keys->port16[1]);
285 }
286
287 hash = __flow_hash_3words((__force u32)keys->dst,
288 (__force u32)keys->src,
Tom Herbert50fb7992015-05-01 11:30:12 -0700289 (__force u32)keys->ports,
290 keyval);
Tom Herbert5ed20a62014-07-01 21:32:05 -0700291 if (!hash)
292 hash = 1;
293
294 return hash;
295}
296
297u32 flow_hash_from_keys(struct flow_keys *keys)
298{
Tom Herbert50fb7992015-05-01 11:30:12 -0700299 __flow_hash_secret_init();
300 return __flow_hash_from_keys(keys, hashrnd);
Tom Herbert5ed20a62014-07-01 21:32:05 -0700301}
302EXPORT_SYMBOL(flow_hash_from_keys);
303
Tom Herbert50fb7992015-05-01 11:30:12 -0700304static inline u32 ___skb_get_hash(const struct sk_buff *skb,
305 struct flow_keys *keys, u32 keyval)
306{
307 if (!skb_flow_dissect(skb, keys))
308 return 0;
309
310 return __flow_hash_from_keys(keys, keyval);
311}
312
Tom Herbert2f59e1e2015-05-01 11:30:17 -0700313struct _flow_keys_digest_data {
314 __be16 n_proto;
315 u8 ip_proto;
316 u8 padding;
317 __be32 ports;
318 __be32 src;
319 __be32 dst;
320};
321
322void make_flow_keys_digest(struct flow_keys_digest *digest,
323 const struct flow_keys *flow)
324{
325 struct _flow_keys_digest_data *data =
326 (struct _flow_keys_digest_data *)digest;
327
328 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
329
330 memset(digest, 0, sizeof(*digest));
331
332 data->n_proto = flow->n_proto;
333 data->ip_proto = flow->ip_proto;
334 data->ports = flow->ports;
335 data->src = flow->src;
336 data->dst = flow->dst;
337}
338EXPORT_SYMBOL(make_flow_keys_digest);
339
Jiri Pirkod4fd3272015-05-12 14:56:10 +0200340/**
341 * __skb_get_hash: calculate a flow hash
342 * @skb: sk_buff to calculate flow hash from
343 *
344 * This function calculates a flow hash based on src/dst addresses
Tom Herbert61b905d2014-03-24 15:34:47 -0700345 * and src/dst port numbers. Sets hash in skb to non-zero hash value
346 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
Cong Wang441d9d32013-01-21 00:39:24 +0000347 * if hash is a canonical 4-tuple hash over transport ports.
348 */
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800349void __skb_get_hash(struct sk_buff *skb)
Cong Wang441d9d32013-01-21 00:39:24 +0000350{
351 struct flow_keys keys;
Tom Herbert50fb7992015-05-01 11:30:12 -0700352 u32 hash;
Cong Wang441d9d32013-01-21 00:39:24 +0000353
Tom Herbert50fb7992015-05-01 11:30:12 -0700354 __flow_hash_secret_init();
355
356 hash = ___skb_get_hash(skb, &keys, hashrnd);
357 if (!hash)
Cong Wang441d9d32013-01-21 00:39:24 +0000358 return;
Cong Wang441d9d32013-01-21 00:39:24 +0000359 if (keys.ports)
Tom Herbert61b905d2014-03-24 15:34:47 -0700360 skb->l4_hash = 1;
Tom Herberta3b18dd2014-07-01 21:33:17 -0700361 skb->sw_hash = 1;
Tom Herbert50fb7992015-05-01 11:30:12 -0700362 skb->hash = hash;
Cong Wang441d9d32013-01-21 00:39:24 +0000363}
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800364EXPORT_SYMBOL(__skb_get_hash);
Cong Wang441d9d32013-01-21 00:39:24 +0000365
Tom Herbert50fb7992015-05-01 11:30:12 -0700366__u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
367{
368 struct flow_keys keys;
369
370 return ___skb_get_hash(skb, &keys, perturb);
371}
372EXPORT_SYMBOL(skb_get_hash_perturb);
373
Alexander Duyck56193d12014-09-05 19:20:26 -0400374u32 __skb_get_poff(const struct sk_buff *skb, void *data,
375 const struct flow_keys *keys, int hlen)
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000376{
Alexander Duyck56193d12014-09-05 19:20:26 -0400377 u32 poff = keys->thoff;
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000378
Alexander Duyck56193d12014-09-05 19:20:26 -0400379 switch (keys->ip_proto) {
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000380 case IPPROTO_TCP: {
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700381 /* access doff as u8 to avoid unaligned access */
382 const u8 *doff;
383 u8 _doff;
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000384
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700385 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
386 data, hlen, &_doff);
387 if (!doff)
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000388 return poff;
389
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700390 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000391 break;
392 }
393 case IPPROTO_UDP:
394 case IPPROTO_UDPLITE:
395 poff += sizeof(struct udphdr);
396 break;
397 /* For the rest, we do not really care about header
398 * extensions at this point for now.
399 */
400 case IPPROTO_ICMP:
401 poff += sizeof(struct icmphdr);
402 break;
403 case IPPROTO_ICMPV6:
404 poff += sizeof(struct icmp6hdr);
405 break;
406 case IPPROTO_IGMP:
407 poff += sizeof(struct igmphdr);
408 break;
409 case IPPROTO_DCCP:
410 poff += sizeof(struct dccp_hdr);
411 break;
412 case IPPROTO_SCTP:
413 poff += sizeof(struct sctphdr);
414 break;
415 }
416
417 return poff;
418}
419
Jiri Pirko0db89b82015-05-12 14:56:14 +0200420/**
421 * skb_get_poff - get the offset to the payload
422 * @skb: sk_buff to get the payload offset from
423 *
424 * The function will get the offset to the payload as far as it could
425 * be dissected. The main user is currently BPF, so that we can dynamically
Alexander Duyck56193d12014-09-05 19:20:26 -0400426 * truncate packets without needing to push actual payload to the user
427 * space and can analyze headers only, instead.
428 */
429u32 skb_get_poff(const struct sk_buff *skb)
430{
431 struct flow_keys keys;
432
433 if (!skb_flow_dissect(skb, &keys))
434 return 0;
435
436 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
437}