blob: 61ad43f61c5edbffa48f4983d8bf1a7472a66cdc [file] [log] [blame]
Jiri Pirkofbff9492015-05-12 14:56:15 +02001#include <linux/kernel.h>
Eric Dumazet0744dd02011-11-28 05:22:18 +00002#include <linux/skbuff.h>
Jesper Dangaard Brouerc452ed72012-01-24 16:03:33 -05003#include <linux/export.h>
Eric Dumazet0744dd02011-11-28 05:22:18 +00004#include <linux/ip.h>
5#include <linux/ipv6.h>
6#include <linux/if_vlan.h>
7#include <net/ip.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +00008#include <net/ipv6.h>
Daniel Borkmannf77668d2013-03-19 06:39:30 +00009#include <linux/igmp.h>
10#include <linux/icmp.h>
11#include <linux/sctp.h>
12#include <linux/dccp.h>
Eric Dumazet0744dd02011-11-28 05:22:18 +000013#include <linux/if_tunnel.h>
14#include <linux/if_pppox.h>
15#include <linux/ppp_defs.h>
Jiri Pirko06635a32015-05-12 14:56:16 +020016#include <linux/stddef.h>
Jiri Pirko67a900c2015-05-12 14:56:19 +020017#include <linux/if_ether.h>
Tom Herbertb3baa0f2015-06-04 09:16:46 -070018#include <linux/mpls.h>
Jiri Pirko1bd758e2015-05-12 14:56:07 +020019#include <net/flow_dissector.h>
Alexander Duyck56193d12014-09-05 19:20:26 -040020#include <scsi/fc/fc_fcoe.h>
Eric Dumazet0744dd02011-11-28 05:22:18 +000021
David S. Miller20a17bf2015-09-01 21:19:17 -070022static void dissector_set_key(struct flow_dissector *flow_dissector,
23 enum flow_dissector_key_id key_id)
Jiri Pirkofbff9492015-05-12 14:56:15 +020024{
25 flow_dissector->used_keys |= (1 << key_id);
26}
27
Jiri Pirkofbff9492015-05-12 14:56:15 +020028void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
29 const struct flow_dissector_key *key,
30 unsigned int key_count)
31{
32 unsigned int i;
33
34 memset(flow_dissector, 0, sizeof(*flow_dissector));
35
36 for (i = 0; i < key_count; i++, key++) {
37 /* User should make sure that every key target offset is withing
38 * boundaries of unsigned short.
39 */
40 BUG_ON(key->offset > USHRT_MAX);
David S. Miller20a17bf2015-09-01 21:19:17 -070041 BUG_ON(dissector_uses_key(flow_dissector,
42 key->key_id));
Jiri Pirkofbff9492015-05-12 14:56:15 +020043
David S. Miller20a17bf2015-09-01 21:19:17 -070044 dissector_set_key(flow_dissector, key->key_id);
Jiri Pirkofbff9492015-05-12 14:56:15 +020045 flow_dissector->offset[key->key_id] = key->offset;
46 }
47
Tom Herbert42aecaa2015-06-04 09:16:39 -070048 /* Ensure that the dissector always includes control and basic key.
49 * That way we are able to avoid handling lack of these in fast path.
Jiri Pirkofbff9492015-05-12 14:56:15 +020050 */
David S. Miller20a17bf2015-09-01 21:19:17 -070051 BUG_ON(!dissector_uses_key(flow_dissector,
52 FLOW_DISSECTOR_KEY_CONTROL));
53 BUG_ON(!dissector_uses_key(flow_dissector,
54 FLOW_DISSECTOR_KEY_BASIC));
Jiri Pirkofbff9492015-05-12 14:56:15 +020055}
56EXPORT_SYMBOL(skb_flow_dissector_init);
57
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020058/**
WANG Cong6451b3f2014-08-25 17:03:46 -070059 * __skb_flow_get_ports - extract the upper layer ports and return them
60 * @skb: sk_buff to extract the ports from
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020061 * @thoff: transport header offset
62 * @ip_proto: protocol for which to get port offset
WANG Cong6451b3f2014-08-25 17:03:46 -070063 * @data: raw buffer pointer to the packet, if NULL use skb->data
64 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020065 *
66 * The function will try to retrieve the ports at offset thoff + poff where poff
67 * is the protocol port offset returned from proto_ports_offset
68 */
David S. Miller690e36e2014-08-23 12:13:41 -070069__be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
70 void *data, int hlen)
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020071{
72 int poff = proto_ports_offset(ip_proto);
73
David S. Miller690e36e2014-08-23 12:13:41 -070074 if (!data) {
75 data = skb->data;
76 hlen = skb_headlen(skb);
77 }
78
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020079 if (poff >= 0) {
80 __be32 *ports, _ports;
81
David S. Miller690e36e2014-08-23 12:13:41 -070082 ports = __skb_header_pointer(skb, thoff + poff,
83 sizeof(_ports), data, hlen, &_ports);
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020084 if (ports)
85 return *ports;
86 }
87
88 return 0;
89}
David S. Miller690e36e2014-08-23 12:13:41 -070090EXPORT_SYMBOL(__skb_flow_get_ports);
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020091
WANG Cong453a9402014-08-25 17:03:47 -070092/**
93 * __skb_flow_dissect - extract the flow_keys struct and return it
94 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
Jiri Pirko06635a32015-05-12 14:56:16 +020095 * @flow_dissector: list of keys to dissect
96 * @target_container: target structure to put dissected values into
WANG Cong453a9402014-08-25 17:03:47 -070097 * @data: raw buffer pointer to the packet, if NULL use skb->data
98 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
99 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
100 * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
101 *
Jiri Pirko06635a32015-05-12 14:56:16 +0200102 * The function will try to retrieve individual keys into target specified
103 * by flow_dissector from either the skbuff or a raw buffer specified by the
104 * rest parameters.
105 *
106 * Caller must take care of zeroing target container memory.
WANG Cong453a9402014-08-25 17:03:47 -0700107 */
Jiri Pirko06635a32015-05-12 14:56:16 +0200108bool __skb_flow_dissect(const struct sk_buff *skb,
109 struct flow_dissector *flow_dissector,
110 void *target_container,
Tom Herbertcd79a232015-09-01 09:24:27 -0700111 void *data, __be16 proto, int nhoff, int hlen,
112 unsigned int flags)
Eric Dumazet0744dd02011-11-28 05:22:18 +0000113{
Tom Herbert42aecaa2015-06-04 09:16:39 -0700114 struct flow_dissector_key_control *key_control;
Jiri Pirko06635a32015-05-12 14:56:16 +0200115 struct flow_dissector_key_basic *key_basic;
116 struct flow_dissector_key_addrs *key_addrs;
117 struct flow_dissector_key_ports *key_ports;
Tom Herbertd34af822015-06-04 09:16:43 -0700118 struct flow_dissector_key_tags *key_tags;
Tom Herbert1fdd5122015-06-04 09:16:45 -0700119 struct flow_dissector_key_keyid *key_keyid;
Geert Uytterhoeven8e690ff2015-06-25 15:10:32 +0200120 u8 ip_proto = 0;
Tom Herberta6e544b2015-09-01 09:24:26 -0700121 bool ret = false;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000122
David S. Miller690e36e2014-08-23 12:13:41 -0700123 if (!data) {
124 data = skb->data;
WANG Cong453a9402014-08-25 17:03:47 -0700125 proto = skb->protocol;
126 nhoff = skb_network_offset(skb);
David S. Miller690e36e2014-08-23 12:13:41 -0700127 hlen = skb_headlen(skb);
128 }
129
Tom Herbert42aecaa2015-06-04 09:16:39 -0700130 /* It is ensured by skb_flow_dissector_init() that control key will
131 * be always present.
132 */
133 key_control = skb_flow_dissector_target(flow_dissector,
134 FLOW_DISSECTOR_KEY_CONTROL,
135 target_container);
136
Jiri Pirko06635a32015-05-12 14:56:16 +0200137 /* It is ensured by skb_flow_dissector_init() that basic key will
138 * be always present.
139 */
140 key_basic = skb_flow_dissector_target(flow_dissector,
141 FLOW_DISSECTOR_KEY_BASIC,
142 target_container);
Eric Dumazet0744dd02011-11-28 05:22:18 +0000143
David S. Miller20a17bf2015-09-01 21:19:17 -0700144 if (dissector_uses_key(flow_dissector,
145 FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
Jiri Pirko67a900c2015-05-12 14:56:19 +0200146 struct ethhdr *eth = eth_hdr(skb);
147 struct flow_dissector_key_eth_addrs *key_eth_addrs;
148
149 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
150 FLOW_DISSECTOR_KEY_ETH_ADDRS,
151 target_container);
152 memcpy(key_eth_addrs, &eth->h_dest, sizeof(*key_eth_addrs));
153 }
154
Eric Dumazet0744dd02011-11-28 05:22:18 +0000155again:
156 switch (proto) {
Joe Perches2b8837a2014-03-12 10:04:17 -0700157 case htons(ETH_P_IP): {
Eric Dumazet0744dd02011-11-28 05:22:18 +0000158 const struct iphdr *iph;
159 struct iphdr _iph;
160ip:
David S. Miller690e36e2014-08-23 12:13:41 -0700161 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
Jason Wang6f092342013-11-01 15:01:10 +0800162 if (!iph || iph->ihl < 5)
Tom Herberta6e544b2015-09-01 09:24:26 -0700163 goto out_bad;
Eric Dumazet3797d3e2013-11-07 08:37:28 -0800164 nhoff += iph->ihl * 4;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000165
Eric Dumazet3797d3e2013-11-07 08:37:28 -0800166 ip_proto = iph->protocol;
Eric Dumazet3797d3e2013-11-07 08:37:28 -0800167
Alexander Duyck918c0232016-02-24 09:29:38 -0800168 if (dissector_uses_key(flow_dissector,
169 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
170 key_addrs = skb_flow_dissector_target(flow_dissector,
171 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
172 target_container);
Tom Herbertc3f83242015-06-04 09:16:40 -0700173
Alexander Duyck918c0232016-02-24 09:29:38 -0800174 memcpy(&key_addrs->v4addrs, &iph->saddr,
175 sizeof(key_addrs->v4addrs));
176 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
177 }
Tom Herbert807e1652015-09-01 09:24:28 -0700178
179 if (ip_is_fragment(iph)) {
David S. Miller4b369932015-09-01 16:46:08 -0700180 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
Tom Herbert807e1652015-09-01 09:24:28 -0700181
182 if (iph->frag_off & htons(IP_OFFSET)) {
183 goto out_good;
184 } else {
David S. Miller4b369932015-09-01 16:46:08 -0700185 key_control->flags |= FLOW_DIS_FIRST_FRAG;
Tom Herbert807e1652015-09-01 09:24:28 -0700186 if (!(flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG))
187 goto out_good;
188 }
189 }
190
Tom Herbert8306b682015-09-01 09:24:30 -0700191 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
192 goto out_good;
193
Eric Dumazet0744dd02011-11-28 05:22:18 +0000194 break;
195 }
Joe Perches2b8837a2014-03-12 10:04:17 -0700196 case htons(ETH_P_IPV6): {
Eric Dumazet0744dd02011-11-28 05:22:18 +0000197 const struct ipv6hdr *iph;
198 struct ipv6hdr _iph;
Tom Herbert19469a82014-07-01 21:33:01 -0700199
Eric Dumazet0744dd02011-11-28 05:22:18 +0000200ipv6:
David S. Miller690e36e2014-08-23 12:13:41 -0700201 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
Eric Dumazet0744dd02011-11-28 05:22:18 +0000202 if (!iph)
Tom Herberta6e544b2015-09-01 09:24:26 -0700203 goto out_bad;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000204
205 ip_proto = iph->nexthdr;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000206 nhoff += sizeof(struct ipv6hdr);
Tom Herbert19469a82014-07-01 21:33:01 -0700207
David S. Miller20a17bf2015-09-01 21:19:17 -0700208 if (dissector_uses_key(flow_dissector,
209 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
Alexander Duyckb3c31062016-02-24 09:29:57 -0800210 key_addrs = skb_flow_dissector_target(flow_dissector,
211 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
212 target_container);
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700213
Alexander Duyckb3c31062016-02-24 09:29:57 -0800214 memcpy(&key_addrs->v6addrs, &iph->saddr,
215 sizeof(key_addrs->v6addrs));
Tom Herbertc3f83242015-06-04 09:16:40 -0700216 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Jiri Pirkob9249332015-05-12 14:56:18 +0200217 }
Tom Herbert87ee9e52015-06-04 09:16:44 -0700218
Alexander Duyck461547f2016-02-09 02:49:54 -0800219 if ((dissector_uses_key(flow_dissector,
220 FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
221 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
222 ip6_flowlabel(iph)) {
223 __be32 flow_label = ip6_flowlabel(iph);
224
David S. Miller20a17bf2015-09-01 21:19:17 -0700225 if (dissector_uses_key(flow_dissector,
226 FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
Tom Herbert87ee9e52015-06-04 09:16:44 -0700227 key_tags = skb_flow_dissector_target(flow_dissector,
228 FLOW_DISSECTOR_KEY_FLOW_LABEL,
229 target_container);
230 key_tags->flow_label = ntohl(flow_label);
Jiri Pirko12c227e2015-05-22 11:05:58 +0200231 }
Tom Herbert872b1ab2015-09-01 09:24:31 -0700232 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)
233 goto out_good;
Tom Herbert19469a82014-07-01 21:33:01 -0700234 }
235
Tom Herbert8306b682015-09-01 09:24:30 -0700236 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
237 goto out_good;
238
Eric Dumazet0744dd02011-11-28 05:22:18 +0000239 break;
240 }
Joe Perches2b8837a2014-03-12 10:04:17 -0700241 case htons(ETH_P_8021AD):
242 case htons(ETH_P_8021Q): {
Eric Dumazet0744dd02011-11-28 05:22:18 +0000243 const struct vlan_hdr *vlan;
244 struct vlan_hdr _vlan;
245
David S. Miller690e36e2014-08-23 12:13:41 -0700246 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), data, hlen, &_vlan);
Eric Dumazet0744dd02011-11-28 05:22:18 +0000247 if (!vlan)
Tom Herberta6e544b2015-09-01 09:24:26 -0700248 goto out_bad;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000249
David S. Miller20a17bf2015-09-01 21:19:17 -0700250 if (dissector_uses_key(flow_dissector,
251 FLOW_DISSECTOR_KEY_VLANID)) {
Tom Herbertd34af822015-06-04 09:16:43 -0700252 key_tags = skb_flow_dissector_target(flow_dissector,
253 FLOW_DISSECTOR_KEY_VLANID,
254 target_container);
255
256 key_tags->vlan_id = skb_vlan_tag_get_id(skb);
257 }
258
Eric Dumazet0744dd02011-11-28 05:22:18 +0000259 proto = vlan->h_vlan_encapsulated_proto;
260 nhoff += sizeof(*vlan);
261 goto again;
262 }
Joe Perches2b8837a2014-03-12 10:04:17 -0700263 case htons(ETH_P_PPP_SES): {
Eric Dumazet0744dd02011-11-28 05:22:18 +0000264 struct {
265 struct pppoe_hdr hdr;
266 __be16 proto;
267 } *hdr, _hdr;
David S. Miller690e36e2014-08-23 12:13:41 -0700268 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
Eric Dumazet0744dd02011-11-28 05:22:18 +0000269 if (!hdr)
Tom Herberta6e544b2015-09-01 09:24:26 -0700270 goto out_bad;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000271 proto = hdr->proto;
272 nhoff += PPPOE_SES_HLEN;
273 switch (proto) {
Joe Perches2b8837a2014-03-12 10:04:17 -0700274 case htons(PPP_IP):
Eric Dumazet0744dd02011-11-28 05:22:18 +0000275 goto ip;
Joe Perches2b8837a2014-03-12 10:04:17 -0700276 case htons(PPP_IPV6):
Eric Dumazet0744dd02011-11-28 05:22:18 +0000277 goto ipv6;
278 default:
Tom Herberta6e544b2015-09-01 09:24:26 -0700279 goto out_bad;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000280 }
281 }
Erik Hugne08bfc9c2015-01-22 17:10:32 +0100282 case htons(ETH_P_TIPC): {
283 struct {
284 __be32 pre[3];
285 __be32 srcnode;
286 } *hdr, _hdr;
287 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
288 if (!hdr)
Tom Herberta6e544b2015-09-01 09:24:26 -0700289 goto out_bad;
Jiri Pirko06635a32015-05-12 14:56:16 +0200290
David S. Miller20a17bf2015-09-01 21:19:17 -0700291 if (dissector_uses_key(flow_dissector,
292 FLOW_DISSECTOR_KEY_TIPC_ADDRS)) {
Jiri Pirko06635a32015-05-12 14:56:16 +0200293 key_addrs = skb_flow_dissector_target(flow_dissector,
Tom Herbert9f249082015-06-04 09:16:41 -0700294 FLOW_DISSECTOR_KEY_TIPC_ADDRS,
Jiri Pirko06635a32015-05-12 14:56:16 +0200295 target_container);
Tom Herbert9f249082015-06-04 09:16:41 -0700296 key_addrs->tipcaddrs.srcnode = hdr->srcnode;
297 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC_ADDRS;
Jiri Pirko06635a32015-05-12 14:56:16 +0200298 }
Tom Herberta6e544b2015-09-01 09:24:26 -0700299 goto out_good;
Erik Hugne08bfc9c2015-01-22 17:10:32 +0100300 }
Tom Herbertb3baa0f2015-06-04 09:16:46 -0700301
302 case htons(ETH_P_MPLS_UC):
303 case htons(ETH_P_MPLS_MC): {
304 struct mpls_label *hdr, _hdr[2];
305mpls:
306 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
307 hlen, &_hdr);
308 if (!hdr)
Tom Herberta6e544b2015-09-01 09:24:26 -0700309 goto out_bad;
Tom Herbertb3baa0f2015-06-04 09:16:46 -0700310
Tom Herbert611d23c2015-06-12 09:01:05 -0700311 if ((ntohl(hdr[0].entry) & MPLS_LS_LABEL_MASK) >>
312 MPLS_LS_LABEL_SHIFT == MPLS_LABEL_ENTROPY) {
David S. Miller20a17bf2015-09-01 21:19:17 -0700313 if (dissector_uses_key(flow_dissector,
314 FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) {
Tom Herbertb3baa0f2015-06-04 09:16:46 -0700315 key_keyid = skb_flow_dissector_target(flow_dissector,
316 FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
317 target_container);
318 key_keyid->keyid = hdr[1].entry &
319 htonl(MPLS_LS_LABEL_MASK);
320 }
321
Tom Herberta6e544b2015-09-01 09:24:26 -0700322 goto out_good;
Tom Herbertb3baa0f2015-06-04 09:16:46 -0700323 }
324
Tom Herberta6e544b2015-09-01 09:24:26 -0700325 goto out_good;
Tom Herbertb3baa0f2015-06-04 09:16:46 -0700326 }
327
Alexander Duyck56193d12014-09-05 19:20:26 -0400328 case htons(ETH_P_FCOE):
Alexander Duyck224516b2016-02-24 09:29:51 -0800329 if ((hlen - nhoff) < FCOE_HEADER_LEN)
330 goto out_bad;
331
332 nhoff += FCOE_HEADER_LEN;
333 goto out_good;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000334 default:
Tom Herberta6e544b2015-09-01 09:24:26 -0700335 goto out_bad;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000336 }
337
Tom Herbert6a74fcf2015-06-12 09:01:06 -0700338ip_proto_again:
Eric Dumazet0744dd02011-11-28 05:22:18 +0000339 switch (ip_proto) {
340 case IPPROTO_GRE: {
341 struct gre_hdr {
342 __be16 flags;
343 __be16 proto;
344 } *hdr, _hdr;
345
David S. Miller690e36e2014-08-23 12:13:41 -0700346 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
Eric Dumazet0744dd02011-11-28 05:22:18 +0000347 if (!hdr)
Tom Herberta6e544b2015-09-01 09:24:26 -0700348 goto out_bad;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000349 /*
350 * Only look inside GRE if version zero and no
351 * routing
352 */
Tom Herbertce3b5352015-06-04 09:16:36 -0700353 if (hdr->flags & (GRE_VERSION | GRE_ROUTING))
354 break;
Michael Daltone1733de2013-03-11 06:52:28 +0000355
Tom Herbertce3b5352015-06-04 09:16:36 -0700356 proto = hdr->proto;
357 nhoff += 4;
358 if (hdr->flags & GRE_CSUM)
359 nhoff += 4;
Tom Herbert1fdd5122015-06-04 09:16:45 -0700360 if (hdr->flags & GRE_KEY) {
361 const __be32 *keyid;
362 __be32 _keyid;
363
364 keyid = __skb_header_pointer(skb, nhoff, sizeof(_keyid),
365 data, hlen, &_keyid);
366
367 if (!keyid)
Tom Herberta6e544b2015-09-01 09:24:26 -0700368 goto out_bad;
Tom Herbert1fdd5122015-06-04 09:16:45 -0700369
David S. Miller20a17bf2015-09-01 21:19:17 -0700370 if (dissector_uses_key(flow_dissector,
371 FLOW_DISSECTOR_KEY_GRE_KEYID)) {
Tom Herbert1fdd5122015-06-04 09:16:45 -0700372 key_keyid = skb_flow_dissector_target(flow_dissector,
373 FLOW_DISSECTOR_KEY_GRE_KEYID,
374 target_container);
375 key_keyid->keyid = *keyid;
376 }
Tom Herbertce3b5352015-06-04 09:16:36 -0700377 nhoff += 4;
Tom Herbert1fdd5122015-06-04 09:16:45 -0700378 }
Tom Herbertce3b5352015-06-04 09:16:36 -0700379 if (hdr->flags & GRE_SEQ)
380 nhoff += 4;
381 if (proto == htons(ETH_P_TEB)) {
382 const struct ethhdr *eth;
383 struct ethhdr _eth;
384
385 eth = __skb_header_pointer(skb, nhoff,
386 sizeof(_eth),
387 data, hlen, &_eth);
388 if (!eth)
Tom Herberta6e544b2015-09-01 09:24:26 -0700389 goto out_bad;
Tom Herbertce3b5352015-06-04 09:16:36 -0700390 proto = eth->h_proto;
391 nhoff += sizeof(*eth);
Alexander Duyck78565202016-02-09 06:14:43 -0800392
393 /* Cap headers that we access via pointers at the
394 * end of the Ethernet header as our maximum alignment
395 * at that point is only 2 bytes.
396 */
397 if (NET_IP_ALIGN)
398 hlen = nhoff;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000399 }
Tom Herbert823b9692015-09-01 09:24:32 -0700400
David S. Miller4b369932015-09-01 16:46:08 -0700401 key_control->flags |= FLOW_DIS_ENCAPSULATION;
Tom Herbert823b9692015-09-01 09:24:32 -0700402 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
403 goto out_good;
404
Tom Herbertce3b5352015-06-04 09:16:36 -0700405 goto again;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000406 }
Tom Herbert6a74fcf2015-06-12 09:01:06 -0700407 case NEXTHDR_HOP:
408 case NEXTHDR_ROUTING:
409 case NEXTHDR_DEST: {
410 u8 _opthdr[2], *opthdr;
411
412 if (proto != htons(ETH_P_IPV6))
413 break;
414
415 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
416 data, hlen, &_opthdr);
Eric Dumazet1e98a0f2015-06-12 19:31:32 -0700417 if (!opthdr)
Tom Herberta6e544b2015-09-01 09:24:26 -0700418 goto out_bad;
Tom Herbert6a74fcf2015-06-12 09:01:06 -0700419
Eric Dumazet1e98a0f2015-06-12 19:31:32 -0700420 ip_proto = opthdr[0];
421 nhoff += (opthdr[1] + 1) << 3;
Tom Herbert6a74fcf2015-06-12 09:01:06 -0700422
423 goto ip_proto_again;
424 }
Tom Herbertb840f282015-09-01 09:24:29 -0700425 case NEXTHDR_FRAGMENT: {
426 struct frag_hdr _fh, *fh;
427
428 if (proto != htons(ETH_P_IPV6))
429 break;
430
431 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
432 data, hlen, &_fh);
433
434 if (!fh)
435 goto out_bad;
436
David S. Miller4b369932015-09-01 16:46:08 -0700437 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
Tom Herbertb840f282015-09-01 09:24:29 -0700438
439 nhoff += sizeof(_fh);
Alexander Duyck43d2ccb2016-02-24 09:29:44 -0800440 ip_proto = fh->nexthdr;
Tom Herbertb840f282015-09-01 09:24:29 -0700441
442 if (!(fh->frag_off & htons(IP6_OFFSET))) {
David S. Miller4b369932015-09-01 16:46:08 -0700443 key_control->flags |= FLOW_DIS_FIRST_FRAG;
Alexander Duyck43d2ccb2016-02-24 09:29:44 -0800444 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG)
Tom Herbertb840f282015-09-01 09:24:29 -0700445 goto ip_proto_again;
Tom Herbertb840f282015-09-01 09:24:29 -0700446 }
447 goto out_good;
448 }
Eric Dumazet0744dd02011-11-28 05:22:18 +0000449 case IPPROTO_IPIP:
Tom Herbertfca41892013-07-29 11:07:36 -0700450 proto = htons(ETH_P_IP);
Tom Herbert823b9692015-09-01 09:24:32 -0700451
David S. Miller4b369932015-09-01 16:46:08 -0700452 key_control->flags |= FLOW_DIS_ENCAPSULATION;
Tom Herbert823b9692015-09-01 09:24:32 -0700453 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
454 goto out_good;
455
Tom Herbertfca41892013-07-29 11:07:36 -0700456 goto ip;
Tom Herbertb438f942013-07-29 11:07:42 -0700457 case IPPROTO_IPV6:
458 proto = htons(ETH_P_IPV6);
Tom Herbert823b9692015-09-01 09:24:32 -0700459
David S. Miller4b369932015-09-01 16:46:08 -0700460 key_control->flags |= FLOW_DIS_ENCAPSULATION;
Tom Herbert823b9692015-09-01 09:24:32 -0700461 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
462 goto out_good;
463
Tom Herbertb438f942013-07-29 11:07:42 -0700464 goto ipv6;
Tom Herbertb3baa0f2015-06-04 09:16:46 -0700465 case IPPROTO_MPLS:
466 proto = htons(ETH_P_MPLS_UC);
467 goto mpls;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000468 default:
469 break;
470 }
471
David S. Miller20a17bf2015-09-01 21:19:17 -0700472 if (dissector_uses_key(flow_dissector,
473 FLOW_DISSECTOR_KEY_PORTS)) {
Jiri Pirko06635a32015-05-12 14:56:16 +0200474 key_ports = skb_flow_dissector_target(flow_dissector,
475 FLOW_DISSECTOR_KEY_PORTS,
476 target_container);
477 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
478 data, hlen);
479 }
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700480
Tom Herberta6e544b2015-09-01 09:24:26 -0700481out_good:
482 ret = true;
483
484out_bad:
485 key_basic->n_proto = proto;
486 key_basic->ip_proto = ip_proto;
487 key_control->thoff = (u16)nhoff;
488
489 return ret;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000490}
David S. Miller690e36e2014-08-23 12:13:41 -0700491EXPORT_SYMBOL(__skb_flow_dissect);
Cong Wang441d9d32013-01-21 00:39:24 +0000492
493static u32 hashrnd __read_mostly;
Hannes Frederic Sowa66415cf2013-10-23 20:06:00 +0200494static __always_inline void __flow_hash_secret_init(void)
495{
496 net_get_random_once(&hashrnd, sizeof(hashrnd));
497}
498
David S. Miller20a17bf2015-09-01 21:19:17 -0700499static __always_inline u32 __flow_hash_words(const u32 *words, u32 length,
500 u32 keyval)
Hannes Frederic Sowa66415cf2013-10-23 20:06:00 +0200501{
Tom Herbert42aecaa2015-06-04 09:16:39 -0700502 return jhash2(words, length, keyval);
503}
504
David S. Miller20a17bf2015-09-01 21:19:17 -0700505static inline const u32 *flow_keys_hash_start(const struct flow_keys *flow)
Tom Herbert42aecaa2015-06-04 09:16:39 -0700506{
David S. Miller20a17bf2015-09-01 21:19:17 -0700507 const void *p = flow;
508
Tom Herbert42aecaa2015-06-04 09:16:39 -0700509 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32));
David S. Miller20a17bf2015-09-01 21:19:17 -0700510 return (const u32 *)(p + FLOW_KEYS_HASH_OFFSET);
Tom Herbert42aecaa2015-06-04 09:16:39 -0700511}
512
David S. Miller20a17bf2015-09-01 21:19:17 -0700513static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
Tom Herbert42aecaa2015-06-04 09:16:39 -0700514{
Tom Herbertc3f83242015-06-04 09:16:40 -0700515 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
Tom Herbert42aecaa2015-06-04 09:16:39 -0700516 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
Tom Herbertc3f83242015-06-04 09:16:40 -0700517 BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
518 sizeof(*flow) - sizeof(flow->addrs));
519
520 switch (flow->control.addr_type) {
521 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
522 diff -= sizeof(flow->addrs.v4addrs);
523 break;
524 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
525 diff -= sizeof(flow->addrs.v6addrs);
526 break;
Tom Herbert9f249082015-06-04 09:16:41 -0700527 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
528 diff -= sizeof(flow->addrs.tipcaddrs);
529 break;
Tom Herbertc3f83242015-06-04 09:16:40 -0700530 }
531 return (sizeof(*flow) - diff) / sizeof(u32);
532}
533
534__be32 flow_get_u32_src(const struct flow_keys *flow)
535{
536 switch (flow->control.addr_type) {
537 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
538 return flow->addrs.v4addrs.src;
539 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
540 return (__force __be32)ipv6_addr_hash(
541 &flow->addrs.v6addrs.src);
Tom Herbert9f249082015-06-04 09:16:41 -0700542 case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
543 return flow->addrs.tipcaddrs.srcnode;
Tom Herbertc3f83242015-06-04 09:16:40 -0700544 default:
545 return 0;
546 }
547}
548EXPORT_SYMBOL(flow_get_u32_src);
549
550__be32 flow_get_u32_dst(const struct flow_keys *flow)
551{
552 switch (flow->control.addr_type) {
553 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
554 return flow->addrs.v4addrs.dst;
555 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
556 return (__force __be32)ipv6_addr_hash(
557 &flow->addrs.v6addrs.dst);
558 default:
559 return 0;
560 }
561}
562EXPORT_SYMBOL(flow_get_u32_dst);
563
564static inline void __flow_hash_consistentify(struct flow_keys *keys)
565{
566 int addr_diff, i;
567
568 switch (keys->control.addr_type) {
569 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
570 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
571 (__force u32)keys->addrs.v4addrs.src;
572 if ((addr_diff < 0) ||
573 (addr_diff == 0 &&
574 ((__force u16)keys->ports.dst <
575 (__force u16)keys->ports.src))) {
576 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
577 swap(keys->ports.src, keys->ports.dst);
578 }
579 break;
580 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
581 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
582 &keys->addrs.v6addrs.src,
583 sizeof(keys->addrs.v6addrs.dst));
584 if ((addr_diff < 0) ||
585 (addr_diff == 0 &&
586 ((__force u16)keys->ports.dst <
587 (__force u16)keys->ports.src))) {
588 for (i = 0; i < 4; i++)
589 swap(keys->addrs.v6addrs.src.s6_addr32[i],
590 keys->addrs.v6addrs.dst.s6_addr32[i]);
591 swap(keys->ports.src, keys->ports.dst);
592 }
593 break;
594 }
Hannes Frederic Sowa66415cf2013-10-23 20:06:00 +0200595}
596
Tom Herbert50fb7992015-05-01 11:30:12 -0700597static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
Tom Herbert5ed20a62014-07-01 21:32:05 -0700598{
599 u32 hash;
600
Tom Herbertc3f83242015-06-04 09:16:40 -0700601 __flow_hash_consistentify(keys);
Tom Herbert5ed20a62014-07-01 21:32:05 -0700602
David S. Miller20a17bf2015-09-01 21:19:17 -0700603 hash = __flow_hash_words(flow_keys_hash_start(keys),
Tom Herbert42aecaa2015-06-04 09:16:39 -0700604 flow_keys_hash_length(keys), keyval);
Tom Herbert5ed20a62014-07-01 21:32:05 -0700605 if (!hash)
606 hash = 1;
607
608 return hash;
609}
610
611u32 flow_hash_from_keys(struct flow_keys *keys)
612{
Tom Herbert50fb7992015-05-01 11:30:12 -0700613 __flow_hash_secret_init();
614 return __flow_hash_from_keys(keys, hashrnd);
Tom Herbert5ed20a62014-07-01 21:32:05 -0700615}
616EXPORT_SYMBOL(flow_hash_from_keys);
617
Tom Herbert50fb7992015-05-01 11:30:12 -0700618static inline u32 ___skb_get_hash(const struct sk_buff *skb,
619 struct flow_keys *keys, u32 keyval)
620{
Tom Herbert6db61d72015-09-01 09:24:33 -0700621 skb_flow_dissect_flow_keys(skb, keys,
622 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
Tom Herbert50fb7992015-05-01 11:30:12 -0700623
624 return __flow_hash_from_keys(keys, keyval);
625}
626
Tom Herbert2f59e1e2015-05-01 11:30:17 -0700627struct _flow_keys_digest_data {
628 __be16 n_proto;
629 u8 ip_proto;
630 u8 padding;
631 __be32 ports;
632 __be32 src;
633 __be32 dst;
634};
635
636void make_flow_keys_digest(struct flow_keys_digest *digest,
637 const struct flow_keys *flow)
638{
639 struct _flow_keys_digest_data *data =
640 (struct _flow_keys_digest_data *)digest;
641
642 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
643
644 memset(digest, 0, sizeof(*digest));
645
Jiri Pirko06635a32015-05-12 14:56:16 +0200646 data->n_proto = flow->basic.n_proto;
647 data->ip_proto = flow->basic.ip_proto;
648 data->ports = flow->ports.ports;
Tom Herbertc3f83242015-06-04 09:16:40 -0700649 data->src = flow->addrs.v4addrs.src;
650 data->dst = flow->addrs.v4addrs.dst;
Tom Herbert2f59e1e2015-05-01 11:30:17 -0700651}
652EXPORT_SYMBOL(make_flow_keys_digest);
653
David S. Millereb70db82016-07-01 16:07:50 -0400654static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
655
656u32 __skb_get_hash_symmetric(struct sk_buff *skb)
657{
658 struct flow_keys keys;
659
660 __flow_hash_secret_init();
661
662 memset(&keys, 0, sizeof(keys));
663 __skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys,
664 NULL, 0, 0, 0,
665 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
666
667 return __flow_hash_from_keys(&keys, hashrnd);
668}
669EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
670
Jiri Pirkod4fd3272015-05-12 14:56:10 +0200671/**
672 * __skb_get_hash: calculate a flow hash
673 * @skb: sk_buff to calculate flow hash from
674 *
675 * This function calculates a flow hash based on src/dst addresses
Tom Herbert61b905d2014-03-24 15:34:47 -0700676 * and src/dst port numbers. Sets hash in skb to non-zero hash value
677 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
Cong Wang441d9d32013-01-21 00:39:24 +0000678 * if hash is a canonical 4-tuple hash over transport ports.
679 */
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800680void __skb_get_hash(struct sk_buff *skb)
Cong Wang441d9d32013-01-21 00:39:24 +0000681{
682 struct flow_keys keys;
Cong Wang441d9d32013-01-21 00:39:24 +0000683
Tom Herbert50fb7992015-05-01 11:30:12 -0700684 __flow_hash_secret_init();
685
Tom Herbert6db61d72015-09-01 09:24:33 -0700686 __skb_set_sw_hash(skb, ___skb_get_hash(skb, &keys, hashrnd),
Tom Herbertbcc83832015-09-01 09:24:24 -0700687 flow_keys_have_l4(&keys));
Cong Wang441d9d32013-01-21 00:39:24 +0000688}
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800689EXPORT_SYMBOL(__skb_get_hash);
Cong Wang441d9d32013-01-21 00:39:24 +0000690
Tom Herbert50fb7992015-05-01 11:30:12 -0700691__u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
692{
693 struct flow_keys keys;
694
695 return ___skb_get_hash(skb, &keys, perturb);
696}
697EXPORT_SYMBOL(skb_get_hash_perturb);
698
David S. Miller20a17bf2015-09-01 21:19:17 -0700699__u32 __skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6)
Tom Herbertf70ea012015-07-31 16:52:10 -0700700{
701 struct flow_keys keys;
702
703 memset(&keys, 0, sizeof(keys));
704
705 memcpy(&keys.addrs.v6addrs.src, &fl6->saddr,
706 sizeof(keys.addrs.v6addrs.src));
707 memcpy(&keys.addrs.v6addrs.dst, &fl6->daddr,
708 sizeof(keys.addrs.v6addrs.dst));
709 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
710 keys.ports.src = fl6->fl6_sport;
711 keys.ports.dst = fl6->fl6_dport;
712 keys.keyid.keyid = fl6->fl6_gre_key;
713 keys.tags.flow_label = (__force u32)fl6->flowlabel;
714 keys.basic.ip_proto = fl6->flowi6_proto;
715
Tom Herbertbcc83832015-09-01 09:24:24 -0700716 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
717 flow_keys_have_l4(&keys));
Tom Herbertf70ea012015-07-31 16:52:10 -0700718
719 return skb->hash;
720}
721EXPORT_SYMBOL(__skb_get_hash_flowi6);
722
David S. Miller20a17bf2015-09-01 21:19:17 -0700723__u32 __skb_get_hash_flowi4(struct sk_buff *skb, const struct flowi4 *fl4)
Tom Herbertf70ea012015-07-31 16:52:10 -0700724{
725 struct flow_keys keys;
726
727 memset(&keys, 0, sizeof(keys));
728
729 keys.addrs.v4addrs.src = fl4->saddr;
730 keys.addrs.v4addrs.dst = fl4->daddr;
731 keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
732 keys.ports.src = fl4->fl4_sport;
733 keys.ports.dst = fl4->fl4_dport;
734 keys.keyid.keyid = fl4->fl4_gre_key;
735 keys.basic.ip_proto = fl4->flowi4_proto;
736
Tom Herbertbcc83832015-09-01 09:24:24 -0700737 __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
738 flow_keys_have_l4(&keys));
Tom Herbertf70ea012015-07-31 16:52:10 -0700739
740 return skb->hash;
741}
742EXPORT_SYMBOL(__skb_get_hash_flowi4);
743
Alexander Duyck56193d12014-09-05 19:20:26 -0400744u32 __skb_get_poff(const struct sk_buff *skb, void *data,
745 const struct flow_keys *keys, int hlen)
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000746{
Tom Herbert42aecaa2015-06-04 09:16:39 -0700747 u32 poff = keys->control.thoff;
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000748
Alexander Duyck43d2ccb2016-02-24 09:29:44 -0800749 /* skip L4 headers for fragments after the first */
750 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
751 !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
752 return poff;
753
Jiri Pirko06635a32015-05-12 14:56:16 +0200754 switch (keys->basic.ip_proto) {
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000755 case IPPROTO_TCP: {
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700756 /* access doff as u8 to avoid unaligned access */
757 const u8 *doff;
758 u8 _doff;
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000759
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700760 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
761 data, hlen, &_doff);
762 if (!doff)
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000763 return poff;
764
Alexander Duyck5af7fb62014-10-10 12:09:12 -0700765 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000766 break;
767 }
768 case IPPROTO_UDP:
769 case IPPROTO_UDPLITE:
770 poff += sizeof(struct udphdr);
771 break;
772 /* For the rest, we do not really care about header
773 * extensions at this point for now.
774 */
775 case IPPROTO_ICMP:
776 poff += sizeof(struct icmphdr);
777 break;
778 case IPPROTO_ICMPV6:
779 poff += sizeof(struct icmp6hdr);
780 break;
781 case IPPROTO_IGMP:
782 poff += sizeof(struct igmphdr);
783 break;
784 case IPPROTO_DCCP:
785 poff += sizeof(struct dccp_hdr);
786 break;
787 case IPPROTO_SCTP:
788 poff += sizeof(struct sctphdr);
789 break;
790 }
791
792 return poff;
793}
794
Jiri Pirko0db89b82015-05-12 14:56:14 +0200795/**
796 * skb_get_poff - get the offset to the payload
797 * @skb: sk_buff to get the payload offset from
798 *
799 * The function will get the offset to the payload as far as it could
800 * be dissected. The main user is currently BPF, so that we can dynamically
Alexander Duyck56193d12014-09-05 19:20:26 -0400801 * truncate packets without needing to push actual payload to the user
802 * space and can analyze headers only, instead.
803 */
804u32 skb_get_poff(const struct sk_buff *skb)
805{
806 struct flow_keys keys;
807
Tom Herbertcd79a232015-09-01 09:24:27 -0700808 if (!skb_flow_dissect_flow_keys(skb, &keys, 0))
Alexander Duyck56193d12014-09-05 19:20:26 -0400809 return 0;
810
811 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
812}
Jiri Pirko06635a32015-05-12 14:56:16 +0200813
David S. Miller20a17bf2015-09-01 21:19:17 -0700814__u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
David S. Millera17ace92015-09-01 17:00:24 -0700815{
816 memset(keys, 0, sizeof(*keys));
817
818 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
819 sizeof(keys->addrs.v6addrs.src));
820 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
821 sizeof(keys->addrs.v6addrs.dst));
822 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
823 keys->ports.src = fl6->fl6_sport;
824 keys->ports.dst = fl6->fl6_dport;
825 keys->keyid.keyid = fl6->fl6_gre_key;
826 keys->tags.flow_label = (__force u32)fl6->flowlabel;
827 keys->basic.ip_proto = fl6->flowi6_proto;
828
829 return flow_hash_from_keys(keys);
830}
831EXPORT_SYMBOL(__get_hash_from_flowi6);
832
David S. Miller20a17bf2015-09-01 21:19:17 -0700833__u32 __get_hash_from_flowi4(const struct flowi4 *fl4, struct flow_keys *keys)
David S. Millera17ace92015-09-01 17:00:24 -0700834{
835 memset(keys, 0, sizeof(*keys));
836
837 keys->addrs.v4addrs.src = fl4->saddr;
838 keys->addrs.v4addrs.dst = fl4->daddr;
839 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
840 keys->ports.src = fl4->fl4_sport;
841 keys->ports.dst = fl4->fl4_dport;
842 keys->keyid.keyid = fl4->fl4_gre_key;
843 keys->basic.ip_proto = fl4->flowi4_proto;
844
845 return flow_hash_from_keys(keys);
846}
847EXPORT_SYMBOL(__get_hash_from_flowi4);
848
Jiri Pirko06635a32015-05-12 14:56:16 +0200849static const struct flow_dissector_key flow_keys_dissector_keys[] = {
850 {
Tom Herbert42aecaa2015-06-04 09:16:39 -0700851 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
852 .offset = offsetof(struct flow_keys, control),
853 },
854 {
Jiri Pirko06635a32015-05-12 14:56:16 +0200855 .key_id = FLOW_DISSECTOR_KEY_BASIC,
856 .offset = offsetof(struct flow_keys, basic),
857 },
858 {
859 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
Tom Herbertc3f83242015-06-04 09:16:40 -0700860 .offset = offsetof(struct flow_keys, addrs.v4addrs),
861 },
862 {
863 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
864 .offset = offsetof(struct flow_keys, addrs.v6addrs),
Jiri Pirko06635a32015-05-12 14:56:16 +0200865 },
866 {
Tom Herbert9f249082015-06-04 09:16:41 -0700867 .key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS,
868 .offset = offsetof(struct flow_keys, addrs.tipcaddrs),
869 },
870 {
Jiri Pirko06635a32015-05-12 14:56:16 +0200871 .key_id = FLOW_DISSECTOR_KEY_PORTS,
872 .offset = offsetof(struct flow_keys, ports),
873 },
Tom Herbertd34af822015-06-04 09:16:43 -0700874 {
875 .key_id = FLOW_DISSECTOR_KEY_VLANID,
876 .offset = offsetof(struct flow_keys, tags),
877 },
Tom Herbert87ee9e52015-06-04 09:16:44 -0700878 {
879 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
880 .offset = offsetof(struct flow_keys, tags),
881 },
Tom Herbert1fdd5122015-06-04 09:16:45 -0700882 {
883 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
884 .offset = offsetof(struct flow_keys, keyid),
885 },
Jiri Pirko06635a32015-05-12 14:56:16 +0200886};
887
David S. Millereb70db82016-07-01 16:07:50 -0400888static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
889 {
890 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
891 .offset = offsetof(struct flow_keys, control),
892 },
893 {
894 .key_id = FLOW_DISSECTOR_KEY_BASIC,
895 .offset = offsetof(struct flow_keys, basic),
896 },
897 {
898 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
899 .offset = offsetof(struct flow_keys, addrs.v4addrs),
900 },
901 {
902 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
903 .offset = offsetof(struct flow_keys, addrs.v6addrs),
904 },
905 {
906 .key_id = FLOW_DISSECTOR_KEY_PORTS,
907 .offset = offsetof(struct flow_keys, ports),
908 },
909};
910
Jiri Pirko06635a32015-05-12 14:56:16 +0200911static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = {
912 {
Tom Herbert42aecaa2015-06-04 09:16:39 -0700913 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
914 .offset = offsetof(struct flow_keys, control),
915 },
916 {
Jiri Pirko06635a32015-05-12 14:56:16 +0200917 .key_id = FLOW_DISSECTOR_KEY_BASIC,
918 .offset = offsetof(struct flow_keys, basic),
919 },
920};
921
922struct flow_dissector flow_keys_dissector __read_mostly;
923EXPORT_SYMBOL(flow_keys_dissector);
924
925struct flow_dissector flow_keys_buf_dissector __read_mostly;
926
927static int __init init_default_flow_dissectors(void)
928{
929 skb_flow_dissector_init(&flow_keys_dissector,
930 flow_keys_dissector_keys,
931 ARRAY_SIZE(flow_keys_dissector_keys));
David S. Millereb70db82016-07-01 16:07:50 -0400932 skb_flow_dissector_init(&flow_keys_dissector_symmetric,
933 flow_keys_dissector_symmetric_keys,
934 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
Jiri Pirko06635a32015-05-12 14:56:16 +0200935 skb_flow_dissector_init(&flow_keys_buf_dissector,
936 flow_keys_buf_dissector_keys,
937 ARRAY_SIZE(flow_keys_buf_dissector_keys));
938 return 0;
939}
940
941late_initcall_sync(init_default_flow_dissectors);