~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/core/flow_dissector.c

Version: ~ [ linux-6.2-rc3 ] ~ [ linux-6.1.5 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.87 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.162 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.228 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.269 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.302 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 #include <linux/kernel.h>
  3 #include <linux/skbuff.h>
  4 #include <linux/export.h>
  5 #include <linux/ip.h>
  6 #include <linux/ipv6.h>
  7 #include <linux/if_vlan.h>
  8 #include <net/dsa.h>
  9 #include <net/dst_metadata.h>
 10 #include <net/ip.h>
 11 #include <net/ipv6.h>
 12 #include <net/gre.h>
 13 #include <net/pptp.h>
 14 #include <net/tipc.h>
 15 #include <linux/igmp.h>
 16 #include <linux/icmp.h>
 17 #include <linux/sctp.h>
 18 #include <linux/dccp.h>
 19 #include <linux/if_tunnel.h>
 20 #include <linux/if_pppox.h>
 21 #include <linux/ppp_defs.h>
 22 #include <linux/stddef.h>
 23 #include <linux/if_ether.h>
 24 #include <linux/mpls.h>
 25 #include <linux/tcp.h>
 26 #include <net/flow_dissector.h>
 27 #include <scsi/fc/fc_fcoe.h>
 28 #include <uapi/linux/batadv_packet.h>
 29 #include <linux/bpf.h>
 30 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
 31 #include <net/netfilter/nf_conntrack_core.h>
 32 #include <net/netfilter/nf_conntrack_labels.h>
 33 #endif
 34 
 35 static DEFINE_MUTEX(flow_dissector_mutex);
 36 
 37 static void dissector_set_key(struct flow_dissector *flow_dissector,
 38                               enum flow_dissector_key_id key_id)
 39 {
 40         flow_dissector->used_keys |= (1 << key_id);
 41 }
 42 
 43 void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
 44                              const struct flow_dissector_key *key,
 45                              unsigned int key_count)
 46 {
 47         unsigned int i;
 48 
 49         memset(flow_dissector, 0, sizeof(*flow_dissector));
 50 
 51         for (i = 0; i < key_count; i++, key++) {
 52                 /* User should make sure that every key target offset is withing
 53                  * boundaries of unsigned short.
 54                  */
 55                 BUG_ON(key->offset > USHRT_MAX);
 56                 BUG_ON(dissector_uses_key(flow_dissector,
 57                                           key->key_id));
 58 
 59                 dissector_set_key(flow_dissector, key->key_id);
 60                 flow_dissector->offset[key->key_id] = key->offset;
 61         }
 62 
 63         /* Ensure that the dissector always includes control and basic key.
 64          * That way we are able to avoid handling lack of these in fast path.
 65          */
 66         BUG_ON(!dissector_uses_key(flow_dissector,
 67                                    FLOW_DISSECTOR_KEY_CONTROL));
 68         BUG_ON(!dissector_uses_key(flow_dissector,
 69                                    FLOW_DISSECTOR_KEY_BASIC));
 70 }
 71 EXPORT_SYMBOL(skb_flow_dissector_init);
 72 
 73 int skb_flow_dissector_prog_query(const union bpf_attr *attr,
 74                                   union bpf_attr __user *uattr)
 75 {
 76         __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
 77         u32 prog_id, prog_cnt = 0, flags = 0;
 78         struct bpf_prog *attached;
 79         struct net *net;
 80 
 81         if (attr->query.query_flags)
 82                 return -EINVAL;
 83 
 84         net = get_net_ns_by_fd(attr->query.target_fd);
 85         if (IS_ERR(net))
 86                 return PTR_ERR(net);
 87 
 88         rcu_read_lock();
 89         attached = rcu_dereference(net->flow_dissector_prog);
 90         if (attached) {
 91                 prog_cnt = 1;
 92                 prog_id = attached->aux->id;
 93         }
 94         rcu_read_unlock();
 95 
 96         put_net(net);
 97 
 98         if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
 99                 return -EFAULT;
100         if (copy_to_user(&uattr->query.prog_cnt, &prog_cnt, sizeof(prog_cnt)))
101                 return -EFAULT;
102 
103         if (!attr->query.prog_cnt || !prog_ids || !prog_cnt)
104                 return 0;
105 
106         if (copy_to_user(prog_ids, &prog_id, sizeof(u32)))
107                 return -EFAULT;
108 
109         return 0;
110 }
111 
112 int skb_flow_dissector_bpf_prog_attach(const union bpf_attr *attr,
113                                        struct bpf_prog *prog)
114 {
115         struct bpf_prog *attached;
116         struct net *net;
117         int ret = 0;
118 
119         net = current->nsproxy->net_ns;
120         mutex_lock(&flow_dissector_mutex);
121 
122         if (net == &init_net) {
123                 /* BPF flow dissector in the root namespace overrides
124                  * any per-net-namespace one. When attaching to root,
125                  * make sure we don't have any BPF program attached
126                  * to the non-root namespaces.
127                  */
128                 struct net *ns;
129 
130                 for_each_net(ns) {
131                         if (ns == &init_net)
132                                 continue;
133                         if (rcu_access_pointer(ns->flow_dissector_prog)) {
134                                 ret = -EEXIST;
135                                 goto out;
136                         }
137                 }
138         } else {
139                 /* Make sure root flow dissector is not attached
140                  * when attaching to the non-root namespace.
141                  */
142                 if (rcu_access_pointer(init_net.flow_dissector_prog)) {
143                         ret = -EEXIST;
144                         goto out;
145                 }
146         }
147 
148         attached = rcu_dereference_protected(net->flow_dissector_prog,
149                                              lockdep_is_held(&flow_dissector_mutex));
150         if (attached == prog) {
151                 /* The same program cannot be attached twice */
152                 ret = -EINVAL;
153                 goto out;
154         }
155         rcu_assign_pointer(net->flow_dissector_prog, prog);
156         if (attached)
157                 bpf_prog_put(attached);
158 out:
159         mutex_unlock(&flow_dissector_mutex);
160         return ret;
161 }
162 
163 static int flow_dissector_bpf_prog_detach(struct net *net)
164 {
165         struct bpf_prog *attached;
166 
167         mutex_lock(&flow_dissector_mutex);
168         attached = rcu_dereference_protected(net->flow_dissector_prog,
169                                              lockdep_is_held(&flow_dissector_mutex));
170         if (!attached) {
171                 mutex_unlock(&flow_dissector_mutex);
172                 return -ENOENT;
173         }
174         RCU_INIT_POINTER(net->flow_dissector_prog, NULL);
175         bpf_prog_put(attached);
176         mutex_unlock(&flow_dissector_mutex);
177         return 0;
178 }
179 
180 int skb_flow_dissector_bpf_prog_detach(const union bpf_attr *attr)
181 {
182         return flow_dissector_bpf_prog_detach(current->nsproxy->net_ns);
183 }
184 
185 static void __net_exit flow_dissector_pernet_pre_exit(struct net *net)
186 {
187         /* We're not racing with attach/detach because there are no
188          * references to netns left when pre_exit gets called.
189          */
190         if (rcu_access_pointer(net->flow_dissector_prog))
191                 flow_dissector_bpf_prog_detach(net);
192 }
193 
194 static struct pernet_operations flow_dissector_pernet_ops __net_initdata = {
195         .pre_exit = flow_dissector_pernet_pre_exit,
196 };
197 
198 /**
199  * __skb_flow_get_ports - extract the upper layer ports and return them
200  * @skb: sk_buff to extract the ports from
201  * @thoff: transport header offset
202  * @ip_proto: protocol for which to get port offset
203  * @data: raw buffer pointer to the packet, if NULL use skb->data
204  * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
205  *
206  * The function will try to retrieve the ports at offset thoff + poff where poff
207  * is the protocol port offset returned from proto_ports_offset
208  */
209 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
210                             void *data, int hlen)
211 {
212         int poff = proto_ports_offset(ip_proto);
213 
214         if (!data) {
215                 data = skb->data;
216                 hlen = skb_headlen(skb);
217         }
218 
219         if (poff >= 0) {
220                 __be32 *ports, _ports;
221 
222                 ports = __skb_header_pointer(skb, thoff + poff,
223                                              sizeof(_ports), data, hlen, &_ports);
224                 if (ports)
225                         return *ports;
226         }
227 
228         return 0;
229 }
230 EXPORT_SYMBOL(__skb_flow_get_ports);
231 
232 static bool icmp_has_id(u8 type)
233 {
234         switch (type) {
235         case ICMP_ECHO:
236         case ICMP_ECHOREPLY:
237         case ICMP_TIMESTAMP:
238         case ICMP_TIMESTAMPREPLY:
239         case ICMPV6_ECHO_REQUEST:
240         case ICMPV6_ECHO_REPLY:
241                 return true;
242         }
243 
244         return false;
245 }
246 
247 /**
248  * skb_flow_get_icmp_tci - extract ICMP(6) Type, Code and Identifier fields
249  * @skb: sk_buff to extract from
250  * @key_icmp: struct flow_dissector_key_icmp to fill
251  * @data: raw buffer pointer to the packet
252  * @thoff: offset to extract at
253  * @hlen: packet header length
254  */
255 void skb_flow_get_icmp_tci(const struct sk_buff *skb,
256                            struct flow_dissector_key_icmp *key_icmp,
257                            void *data, int thoff, int hlen)
258 {
259         struct icmphdr *ih, _ih;
260 
261         ih = __skb_header_pointer(skb, thoff, sizeof(_ih), data, hlen, &_ih);
262         if (!ih)
263                 return;
264 
265         key_icmp->type = ih->type;
266         key_icmp->code = ih->code;
267 
268         /* As we use 0 to signal that the Id field is not present,
269          * avoid confusion with packets without such field
270          */
271         if (icmp_has_id(ih->type))
272                 key_icmp->id = ih->un.echo.id ? : 1;
273         else
274                 key_icmp->id = 0;
275 }
276 EXPORT_SYMBOL(skb_flow_get_icmp_tci);
277 
278 /* If FLOW_DISSECTOR_KEY_ICMP is set, dissect an ICMP packet
279  * using skb_flow_get_icmp_tci().
280  */
281 static void __skb_flow_dissect_icmp(const struct sk_buff *skb,
282                                     struct flow_dissector *flow_dissector,
283                                     void *target_container,
284                                     void *data, int thoff, int hlen)
285 {
286         struct flow_dissector_key_icmp *key_icmp;
287 
288         if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ICMP))
289                 return;
290 
291         key_icmp = skb_flow_dissector_target(flow_dissector,
292                                              FLOW_DISSECTOR_KEY_ICMP,
293                                              target_container);
294 
295         skb_flow_get_icmp_tci(skb, key_icmp, data, thoff, hlen);
296 }
297 
298 void skb_flow_dissect_meta(const struct sk_buff *skb,
299                            struct flow_dissector *flow_dissector,
300                            void *target_container)
301 {
302         struct flow_dissector_key_meta *meta;
303 
304         if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_META))
305                 return;
306 
307         meta = skb_flow_dissector_target(flow_dissector,
308                                          FLOW_DISSECTOR_KEY_META,
309                                          target_container);
310         meta->ingress_ifindex = skb->skb_iif;
311 }
312 EXPORT_SYMBOL(skb_flow_dissect_meta);
313 
314 static void
315 skb_flow_dissect_set_enc_addr_type(enum flow_dissector_key_id type,
316                                    struct flow_dissector *flow_dissector,
317                                    void *target_container)
318 {
319         struct flow_dissector_key_control *ctrl;
320 
321         if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL))
322                 return;
323 
324         ctrl = skb_flow_dissector_target(flow_dissector,
325                                          FLOW_DISSECTOR_KEY_ENC_CONTROL,
326                                          target_container);
327         ctrl->addr_type = type;
328 }
329 
330 void
331 skb_flow_dissect_ct(const struct sk_buff *skb,
332                     struct flow_dissector *flow_dissector,
333                     void *target_container,
334                     u16 *ctinfo_map,
335                     size_t mapsize)
336 {
337 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
338         struct flow_dissector_key_ct *key;
339         enum ip_conntrack_info ctinfo;
340         struct nf_conn_labels *cl;
341         struct nf_conn *ct;
342 
343         if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_CT))
344                 return;
345 
346         ct = nf_ct_get(skb, &ctinfo);
347         if (!ct)
348                 return;
349 
350         key = skb_flow_dissector_target(flow_dissector,
351                                         FLOW_DISSECTOR_KEY_CT,
352                                         target_container);
353 
354         if (ctinfo < mapsize)
355                 key->ct_state = ctinfo_map[ctinfo];
356 #if IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)
357         key->ct_zone = ct->zone.id;
358 #endif
359 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
360         key->ct_mark = ct->mark;
361 #endif
362 
363         cl = nf_ct_labels_find(ct);
364         if (cl)
365                 memcpy(key->ct_labels, cl->bits, sizeof(key->ct_labels));
366 #endif /* CONFIG_NF_CONNTRACK */
367 }
368 EXPORT_SYMBOL(skb_flow_dissect_ct);
369 
370 void
371 skb_flow_dissect_tunnel_info(const struct sk_buff *skb,
372                              struct flow_dissector *flow_dissector,
373                              void *target_container)
374 {
375         struct ip_tunnel_info *info;
376         struct ip_tunnel_key *key;
377 
378         /* A quick check to see if there might be something to do. */
379         if (!dissector_uses_key(flow_dissector,
380                                 FLOW_DISSECTOR_KEY_ENC_KEYID) &&
381             !dissector_uses_key(flow_dissector,
382                                 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) &&
383             !dissector_uses_key(flow_dissector,
384                                 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) &&
385             !dissector_uses_key(flow_dissector,
386                                 FLOW_DISSECTOR_KEY_ENC_CONTROL) &&
387             !dissector_uses_key(flow_dissector,
388                                 FLOW_DISSECTOR_KEY_ENC_PORTS) &&
389             !dissector_uses_key(flow_dissector,
390                                 FLOW_DISSECTOR_KEY_ENC_IP) &&
391             !dissector_uses_key(flow_dissector,
392                                 FLOW_DISSECTOR_KEY_ENC_OPTS))
393                 return;
394 
395         info = skb_tunnel_info(skb);
396         if (!info)
397                 return;
398 
399         key = &info->key;
400 
401         switch (ip_tunnel_info_af(info)) {
402         case AF_INET:
403                 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV4_ADDRS,
404                                                    flow_dissector,
405                                                    target_container);
406                 if (dissector_uses_key(flow_dissector,
407                                        FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) {
408                         struct flow_dissector_key_ipv4_addrs *ipv4;
409 
410                         ipv4 = skb_flow_dissector_target(flow_dissector,
411                                                          FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
412                                                          target_container);
413                         ipv4->src = key->u.ipv4.src;
414                         ipv4->dst = key->u.ipv4.dst;
415                 }
416                 break;
417         case AF_INET6:
418                 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV6_ADDRS,
419                                                    flow_dissector,
420                                                    target_container);
421                 if (dissector_uses_key(flow_dissector,
422                                        FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS)) {
423                         struct flow_dissector_key_ipv6_addrs *ipv6;
424 
425                         ipv6 = skb_flow_dissector_target(flow_dissector,
426                                                          FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
427                                                          target_container);
428                         ipv6->src = key->u.ipv6.src;
429                         ipv6->dst = key->u.ipv6.dst;
430                 }
431                 break;
432         }
433 
434         if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
435                 struct flow_dissector_key_keyid *keyid;
436 
437                 keyid = skb_flow_dissector_target(flow_dissector,
438                                                   FLOW_DISSECTOR_KEY_ENC_KEYID,
439                                                   target_container);
440                 keyid->keyid = tunnel_id_to_key32(key->tun_id);
441         }
442 
443         if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
444                 struct flow_dissector_key_ports *tp;
445 
446                 tp = skb_flow_dissector_target(flow_dissector,
447                                                FLOW_DISSECTOR_KEY_ENC_PORTS,
448                                                target_container);
449                 tp->src = key->tp_src;
450                 tp->dst = key->tp_dst;
451         }
452 
453         if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_IP)) {
454                 struct flow_dissector_key_ip *ip;
455 
456                 ip = skb_flow_dissector_target(flow_dissector,
457                                                FLOW_DISSECTOR_KEY_ENC_IP,
458                                                target_container);
459                 ip->tos = key->tos;
460                 ip->ttl = key->ttl;
461         }
462 
463         if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_OPTS)) {
464                 struct flow_dissector_key_enc_opts *enc_opt;
465 
466                 enc_opt = skb_flow_dissector_target(flow_dissector,
467                                                     FLOW_DISSECTOR_KEY_ENC_OPTS,
468                                                     target_container);
469 
470                 if (info->options_len) {
471                         enc_opt->len = info->options_len;
472                         ip_tunnel_info_opts_get(enc_opt->data, info);
473                         enc_opt->dst_opt_type = info->key.tun_flags &
474                                                 TUNNEL_OPTIONS_PRESENT;
475                 }
476         }
477 }
478 EXPORT_SYMBOL(skb_flow_dissect_tunnel_info);
479 
480 static enum flow_dissect_ret
481 __skb_flow_dissect_mpls(const struct sk_buff *skb,
482                         struct flow_dissector *flow_dissector,
483                         void *target_container, void *data, int nhoff, int hlen)
484 {
485         struct flow_dissector_key_keyid *key_keyid;
486         struct mpls_label *hdr, _hdr[2];
487         u32 entry, label;
488 
489         if (!dissector_uses_key(flow_dissector,
490                                 FLOW_DISSECTOR_KEY_MPLS_ENTROPY) &&
491             !dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS))
492                 return FLOW_DISSECT_RET_OUT_GOOD;
493 
494         hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
495                                    hlen, &_hdr);
496         if (!hdr)
497                 return FLOW_DISSECT_RET_OUT_BAD;
498 
499         entry = ntohl(hdr[0].entry);
500         label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
501 
502         if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) {
503                 struct flow_dissector_key_mpls *key_mpls;
504 
505                 key_mpls = skb_flow_dissector_target(flow_dissector,
506                                                      FLOW_DISSECTOR_KEY_MPLS,
507                                                      target_container);
508                 key_mpls->mpls_label = label;
509                 key_mpls->mpls_ttl = (entry & MPLS_LS_TTL_MASK)
510                                         >> MPLS_LS_TTL_SHIFT;
511                 key_mpls->mpls_tc = (entry & MPLS_LS_TC_MASK)
512                                         >> MPLS_LS_TC_SHIFT;
513                 key_mpls->mpls_bos = (entry & MPLS_LS_S_MASK)
514                                         >> MPLS_LS_S_SHIFT;
515         }
516 
517         if (label == MPLS_LABEL_ENTROPY) {
518                 key_keyid = skb_flow_dissector_target(flow_dissector,
519                                                       FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
520                                                       target_container);
521                 key_keyid->keyid = hdr[1].entry & htonl(MPLS_LS_LABEL_MASK);
522         }
523         return FLOW_DISSECT_RET_OUT_GOOD;
524 }
525 
526 static enum flow_dissect_ret
527 __skb_flow_dissect_arp(const struct sk_buff *skb,
528                        struct flow_dissector *flow_dissector,
529                        void *target_container, void *data, int nhoff, int hlen)
530 {
531         struct flow_dissector_key_arp *key_arp;
532         struct {
533                 unsigned char ar_sha[ETH_ALEN];
534                 unsigned char ar_sip[4];
535                 unsigned char ar_tha[ETH_ALEN];
536                 unsigned char ar_tip[4];
537         } *arp_eth, _arp_eth;
538         const struct arphdr *arp;
539         struct arphdr _arp;
540 
541         if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ARP))
542                 return FLOW_DISSECT_RET_OUT_GOOD;
543 
544         arp = __skb_header_pointer(skb, nhoff, sizeof(_arp), data,
545                                    hlen, &_arp);
546         if (!arp)
547                 return FLOW_DISSECT_RET_OUT_BAD;
548 
549         if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
550             arp->ar_pro != htons(ETH_P_IP) ||
551             arp->ar_hln != ETH_ALEN ||
552             arp->ar_pln != 4 ||
553             (arp->ar_op != htons(ARPOP_REPLY) &&
554              arp->ar_op != htons(ARPOP_REQUEST)))
555                 return FLOW_DISSECT_RET_OUT_BAD;
556 
557         arp_eth = __skb_header_pointer(skb, nhoff + sizeof(_arp),
558                                        sizeof(_arp_eth), data,
559                                        hlen, &_arp_eth);
560         if (!arp_eth)
561                 return FLOW_DISSECT_RET_OUT_BAD;
562 
563         key_arp = skb_flow_dissector_target(flow_dissector,
564                                             FLOW_DISSECTOR_KEY_ARP,
565                                             target_container);
566 
567         memcpy(&key_arp->sip, arp_eth->ar_sip, sizeof(key_arp->sip));
568         memcpy(&key_arp->tip, arp_eth->ar_tip, sizeof(key_arp->tip));
569 
570         /* Only store the lower byte of the opcode;
571          * this covers ARPOP_REPLY and ARPOP_REQUEST.
572          */
573         key_arp->op = ntohs(arp->ar_op) & 0xff;
574 
575         ether_addr_copy(key_arp->sha, arp_eth->ar_sha);
576         ether_addr_copy(key_arp->tha, arp_eth->ar_tha);
577 
578         return FLOW_DISSECT_RET_OUT_GOOD;
579 }
580 
581 static enum flow_dissect_ret
582 __skb_flow_dissect_gre(const struct sk_buff *skb,
583                        struct flow_dissector_key_control *key_control,
584                        struct flow_dissector *flow_dissector,
585                        void *target_container, void *data,
586                        __be16 *p_proto, int *p_nhoff, int *p_hlen,
587                        unsigned int flags)
588 {
589         struct flow_dissector_key_keyid *key_keyid;
590         struct gre_base_hdr *hdr, _hdr;
591         int offset = 0;
592         u16 gre_ver;
593 
594         hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr),
595                                    data, *p_hlen, &_hdr);
596         if (!hdr)
597                 return FLOW_DISSECT_RET_OUT_BAD;
598 
599         /* Only look inside GRE without routing */
600         if (hdr->flags & GRE_ROUTING)
601                 return FLOW_DISSECT_RET_OUT_GOOD;
602 
603         /* Only look inside GRE for version 0 and 1 */
604         gre_ver = ntohs(hdr->flags & GRE_VERSION);
605         if (gre_ver > 1)
606                 return FLOW_DISSECT_RET_OUT_GOOD;
607 
608         *p_proto = hdr->protocol;
609         if (gre_ver) {
610                 /* Version1 must be PPTP, and check the flags */
611                 if (!(*p_proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY)))
612                         return FLOW_DISSECT_RET_OUT_GOOD;
613         }
614 
615         offset += sizeof(struct gre_base_hdr);
616 
617         if (hdr->flags & GRE_CSUM)
618                 offset += sizeof_field(struct gre_full_hdr, csum) +
619                           sizeof_field(struct gre_full_hdr, reserved1);
620 
621         if (hdr->flags & GRE_KEY) {
622                 const __be32 *keyid;
623                 __be32 _keyid;
624 
625                 keyid = __skb_header_pointer(skb, *p_nhoff + offset,
626                                              sizeof(_keyid),
627                                              data, *p_hlen, &_keyid);
628                 if (!keyid)
629                         return FLOW_DISSECT_RET_OUT_BAD;
630 
631                 if (dissector_uses_key(flow_dissector,
632                                        FLOW_DISSECTOR_KEY_GRE_KEYID)) {
633                         key_keyid = skb_flow_dissector_target(flow_dissector,
634                                                               FLOW_DISSECTOR_KEY_GRE_KEYID,
635                                                               target_container);
636                         if (gre_ver == 0)
637                                 key_keyid->keyid = *keyid;
638                         else
639                                 key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK;
640                 }
641                 offset += sizeof_field(struct gre_full_hdr, key);
642         }
643 
644         if (hdr->flags & GRE_SEQ)
645                 offset += sizeof_field(struct pptp_gre_header, seq);
646 
647         if (gre_ver == 0) {
648                 if (*p_proto == htons(ETH_P_TEB)) {
649                         const struct ethhdr *eth;
650                         struct ethhdr _eth;
651 
652                         eth = __skb_header_pointer(skb, *p_nhoff + offset,
653                                                    sizeof(_eth),
654                                                    data, *p_hlen, &_eth);
655                         if (!eth)
656                                 return FLOW_DISSECT_RET_OUT_BAD;
657                         *p_proto = eth->h_proto;
658                         offset += sizeof(*eth);
659 
660                         /* Cap headers that we access via pointers at the
661                          * end of the Ethernet header as our maximum alignment
662                          * at that point is only 2 bytes.
663                          */
664                         if (NET_IP_ALIGN)
665                                 *p_hlen = *p_nhoff + offset;
666                 }
667         } else { /* version 1, must be PPTP */
668                 u8 _ppp_hdr[PPP_HDRLEN];
669                 u8 *ppp_hdr;
670 
671                 if (hdr->flags & GRE_ACK)
672                         offset += sizeof_field(struct pptp_gre_header, ack);
673 
674                 ppp_hdr = __skb_header_pointer(skb, *p_nhoff + offset,
675                                                sizeof(_ppp_hdr),
676                                                data, *p_hlen, _ppp_hdr);
677                 if (!ppp_hdr)
678                         return FLOW_DISSECT_RET_OUT_BAD;
679 
680                 switch (PPP_PROTOCOL(ppp_hdr)) {
681                 case PPP_IP:
682                         *p_proto = htons(ETH_P_IP);
683                         break;
684                 case PPP_IPV6:
685                         *p_proto = htons(ETH_P_IPV6);
686                         break;
687                 default:
688                         /* Could probably catch some more like MPLS */
689                         break;
690                 }
691 
692                 offset += PPP_HDRLEN;
693         }
694 
695         *p_nhoff += offset;
696         key_control->flags |= FLOW_DIS_ENCAPSULATION;
697         if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
698                 return FLOW_DISSECT_RET_OUT_GOOD;
699 
700         return FLOW_DISSECT_RET_PROTO_AGAIN;
701 }
702 
703 /**
704  * __skb_flow_dissect_batadv() - dissect batman-adv header
705  * @skb: sk_buff to with the batman-adv header
706  * @key_control: flow dissectors control key
707  * @data: raw buffer pointer to the packet, if NULL use skb->data
708  * @p_proto: pointer used to update the protocol to process next
709  * @p_nhoff: pointer used to update inner network header offset
710  * @hlen: packet header length
711  * @flags: any combination of FLOW_DISSECTOR_F_*
712  *
713  * ETH_P_BATMAN packets are tried to be dissected. Only
714  * &struct batadv_unicast packets are actually processed because they contain an
715  * inner ethernet header and are usually followed by actual network header. This
716  * allows the flow dissector to continue processing the packet.
717  *
718  * Return: FLOW_DISSECT_RET_PROTO_AGAIN when &struct batadv_unicast was found,
719  *  FLOW_DISSECT_RET_OUT_GOOD when dissector should stop after encapsulation,
720  *  otherwise FLOW_DISSECT_RET_OUT_BAD
721  */
722 static enum flow_dissect_ret
723 __skb_flow_dissect_batadv(const struct sk_buff *skb,
724                           struct flow_dissector_key_control *key_control,
725                           void *data, __be16 *p_proto, int *p_nhoff, int hlen,
726                           unsigned int flags)
727 {
728         struct {
729                 struct batadv_unicast_packet batadv_unicast;
730                 struct ethhdr eth;
731         } *hdr, _hdr;
732 
733         hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr), data, hlen,
734                                    &_hdr);
735         if (!hdr)
736                 return FLOW_DISSECT_RET_OUT_BAD;
737 
738         if (hdr->batadv_unicast.version != BATADV_COMPAT_VERSION)
739                 return FLOW_DISSECT_RET_OUT_BAD;
740 
741         if (hdr->batadv_unicast.packet_type != BATADV_UNICAST)
742                 return FLOW_DISSECT_RET_OUT_BAD;
743 
744         *p_proto = hdr->eth.h_proto;
745         *p_nhoff += sizeof(*hdr);
746 
747         key_control->flags |= FLOW_DIS_ENCAPSULATION;
748         if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
749                 return FLOW_DISSECT_RET_OUT_GOOD;
750 
751         return FLOW_DISSECT_RET_PROTO_AGAIN;
752 }
753 
754 static void
755 __skb_flow_dissect_tcp(const struct sk_buff *skb,
756                        struct flow_dissector *flow_dissector,
757                        void *target_container, void *data, int thoff, int hlen)
758 {
759         struct flow_dissector_key_tcp *key_tcp;
760         struct tcphdr *th, _th;
761 
762         if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_TCP))
763                 return;
764 
765         th = __skb_header_pointer(skb, thoff, sizeof(_th), data, hlen, &_th);
766         if (!th)
767                 return;
768 
769         if (unlikely(__tcp_hdrlen(th) < sizeof(_th)))
770                 return;
771 
772         key_tcp = skb_flow_dissector_target(flow_dissector,
773                                             FLOW_DISSECTOR_KEY_TCP,
774                                             target_container);
775         key_tcp->flags = (*(__be16 *) &tcp_flag_word(th) & htons(0x0FFF));
776 }
777 
778 static void
779 __skb_flow_dissect_ports(const struct sk_buff *skb,
780                          struct flow_dissector *flow_dissector,
781                          void *target_container, void *data, int nhoff,
782                          u8 ip_proto, int hlen)
783 {
784         enum flow_dissector_key_id dissector_ports = FLOW_DISSECTOR_KEY_MAX;
785         struct flow_dissector_key_ports *key_ports;
786 
787         if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS))
788                 dissector_ports = FLOW_DISSECTOR_KEY_PORTS;
789         else if (dissector_uses_key(flow_dissector,
790                                     FLOW_DISSECTOR_KEY_PORTS_RANGE))
791                 dissector_ports = FLOW_DISSECTOR_KEY_PORTS_RANGE;
792 
793         if (dissector_ports == FLOW_DISSECTOR_KEY_MAX)
794                 return;
795 
796         key_ports = skb_flow_dissector_target(flow_dissector,
797                                               dissector_ports,
798                                               target_container);
799         key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
800                                                 data, hlen);
801 }
802 
803 static void
804 __skb_flow_dissect_ipv4(const struct sk_buff *skb,
805                         struct flow_dissector *flow_dissector,
806                         void *target_container, void *data, const struct iphdr *iph)
807 {
808         struct flow_dissector_key_ip *key_ip;
809 
810         if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
811                 return;
812 
813         key_ip = skb_flow_dissector_target(flow_dissector,
814                                            FLOW_DISSECTOR_KEY_IP,
815                                            target_container);
816         key_ip->tos = iph->tos;
817         key_ip->ttl = iph->ttl;
818 }
819 
820 static void
821 __skb_flow_dissect_ipv6(const struct sk_buff *skb,
822                         struct flow_dissector *flow_dissector,
823                         void *target_container, void *data, const struct ipv6hdr *iph)
824 {
825         struct flow_dissector_key_ip *key_ip;
826 
827         if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP))
828                 return;
829 
830         key_ip = skb_flow_dissector_target(flow_dissector,
831                                            FLOW_DISSECTOR_KEY_IP,
832                                            target_container);
833         key_ip->tos = ipv6_get_dsfield(iph);
834         key_ip->ttl = iph->hop_limit;
835 }
836 
837 /* Maximum number of protocol headers that can be parsed in
838  * __skb_flow_dissect
839  */
840 #define MAX_FLOW_DISSECT_HDRS   15
841 
842 static bool skb_flow_dissect_allowed(int *num_hdrs)
843 {
844         ++*num_hdrs;
845 
846         return (*num_hdrs <= MAX_FLOW_DISSECT_HDRS);
847 }
848 
849 static void __skb_flow_bpf_to_target(const struct bpf_flow_keys *flow_keys,
850                                      struct flow_dissector *flow_dissector,
851                                      void *target_container)
852 {
853         struct flow_dissector_key_ports *key_ports = NULL;
854         struct flow_dissector_key_control *key_control;
855         struct flow_dissector_key_basic *key_basic;
856         struct flow_dissector_key_addrs *key_addrs;
857         struct flow_dissector_key_tags *key_tags;
858 
859         key_control = skb_flow_dissector_target(flow_dissector,
860                                                 FLOW_DISSECTOR_KEY_CONTROL,
861                                                 target_container);
862         key_control->thoff = flow_keys->thoff;
863         if (flow_keys->is_frag)
864                 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
865         if (flow_keys->is_first_frag)
866                 key_control->flags |= FLOW_DIS_FIRST_FRAG;
867         if (flow_keys->is_encap)
868                 key_control->flags |= FLOW_DIS_ENCAPSULATION;
869 
870         key_basic = skb_flow_dissector_target(flow_dissector,
871                                               FLOW_DISSECTOR_KEY_BASIC,
872                                               target_container);
873         key_basic->n_proto = flow_keys->n_proto;
874         key_basic->ip_proto = flow_keys->ip_proto;
875 
876         if (flow_keys->addr_proto == ETH_P_IP &&
877             dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
878                 key_addrs = skb_flow_dissector_target(flow_dissector,
879                                                       FLOW_DISSECTOR_KEY_IPV4_ADDRS,
880                                                       target_container);
881                 key_addrs->v4addrs.src = flow_keys->ipv4_src;
882                 key_addrs->v4addrs.dst = flow_keys->ipv4_dst;
883                 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
884         } else if (flow_keys->addr_proto == ETH_P_IPV6 &&
885                    dissector_uses_key(flow_dissector,
886                                       FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
887                 key_addrs = skb_flow_dissector_target(flow_dissector,
888                                                       FLOW_DISSECTOR_KEY_IPV6_ADDRS,
889                                                       target_container);
890                 memcpy(&key_addrs->v6addrs, &flow_keys->ipv6_src,
891                        sizeof(key_addrs->v6addrs));
892                 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
893         }
894 
895         if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS))
896                 key_ports = skb_flow_dissector_target(flow_dissector,
897                                                       FLOW_DISSECTOR_KEY_PORTS,
898                                                       target_container);
899         else if (dissector_uses_key(flow_dissector,
900                                     FLOW_DISSECTOR_KEY_PORTS_RANGE))
901                 key_ports = skb_flow_dissector_target(flow_dissector,
902                                                       FLOW_DISSECTOR_KEY_PORTS_RANGE,
903                                                       target_container);
904 
905         if (key_ports) {
906                 key_ports->src = flow_keys->sport;
907                 key_ports->dst = flow_keys->dport;
908         }
909 
910         if (dissector_uses_key(flow_dissector,
911                                FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
912                 key_tags = skb_flow_dissector_target(flow_dissector,
913                                                      FLOW_DISSECTOR_KEY_FLOW_LABEL,
914                                                      target_container);
915                 key_tags->flow_label = ntohl(flow_keys->flow_label);
916         }
917 }
918 
919 bool bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx,
920                       __be16 proto, int nhoff, int hlen, unsigned int flags)
921 {
922         struct bpf_flow_keys *flow_keys = ctx->flow_keys;
923         u32 result;
924 
925         /* Pass parameters to the BPF program */
926         memset(flow_keys, 0, sizeof(*flow_keys));
927         flow_keys->n_proto = proto;
928         flow_keys->nhoff = nhoff;
929         flow_keys->thoff = flow_keys->nhoff;
930 
931         BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG !=
932                      (int)FLOW_DISSECTOR_F_PARSE_1ST_FRAG);
933         BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL !=
934                      (int)FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
935         BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP !=
936                      (int)FLOW_DISSECTOR_F_STOP_AT_ENCAP);
937         flow_keys->flags = flags;
938 
939         preempt_disable();
940         result = BPF_PROG_RUN(prog, ctx);
941         preempt_enable();
942 
943         flow_keys->nhoff = clamp_t(u16, flow_keys->nhoff, nhoff, hlen);
944         flow_keys->thoff = clamp_t(u16, flow_keys->thoff,
945                                    flow_keys->nhoff, hlen);
946 
947         return result == BPF_OK;
948 }
949 
950 /**
951  * __skb_flow_dissect - extract the flow_keys struct and return it
952  * @net: associated network namespace, derived from @skb if NULL
953  * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
954  * @flow_dissector: list of keys to dissect
955  * @target_container: target structure to put dissected values into
956  * @data: raw buffer pointer to the packet, if NULL use skb->data
957  * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
958  * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
959  * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
960  * @flags: flags that control the dissection process, e.g.
961  *         FLOW_DISSECTOR_F_STOP_AT_ENCAP.
962  *
963  * The function will try to retrieve individual keys into target specified
964  * by flow_dissector from either the skbuff or a raw buffer specified by the
965  * rest parameters.
966  *
967  * Caller must take care of zeroing target container memory.
968  */
969 bool __skb_flow_dissect(const struct net *net,
970                         const struct sk_buff *skb,
971                         struct flow_dissector *flow_dissector,
972                         void *target_container,
973                         void *data, __be16 proto, int nhoff, int hlen,
974                         unsigned int flags)
975 {
976         struct flow_dissector_key_control *key_control;
977         struct flow_dissector_key_basic *key_basic;
978         struct flow_dissector_key_addrs *key_addrs;
979         struct flow_dissector_key_tags *key_tags;
980         struct flow_dissector_key_vlan *key_vlan;
981         struct bpf_prog *attached = NULL;
982         enum flow_dissect_ret fdret;
983         enum flow_dissector_key_id dissector_vlan = FLOW_DISSECTOR_KEY_MAX;
984         int num_hdrs = 0;
985         u8 ip_proto = 0;
986         bool ret;
987 
988         if (!data) {
989                 data = skb->data;
990                 proto = skb_vlan_tag_present(skb) ?
991                          skb->vlan_proto : skb->protocol;
992                 nhoff = skb_network_offset(skb);
993                 hlen = skb_headlen(skb);
994 #if IS_ENABLED(CONFIG_NET_DSA)
995                 if (unlikely(skb->dev && netdev_uses_dsa(skb->dev) &&
996                              proto == htons(ETH_P_XDSA))) {
997                         const struct dsa_device_ops *ops;
998                         int offset = 0;
999 
1000                         ops = skb->dev->dsa_ptr->tag_ops;
1001                         if (ops->flow_dissect &&
1002                             !ops->flow_dissect(skb, &proto, &offset)) {
1003                                 hlen -= offset;
1004                                 nhoff += offset;
1005                         }
1006                 }
1007 #endif
1008         }
1009 
1010         /* It is ensured by skb_flow_dissector_init() that control key will
1011          * be always present.
1012          */
1013         key_control = skb_flow_dissector_target(flow_dissector,
1014                                                 FLOW_DISSECTOR_KEY_CONTROL,
1015                                                 target_container);
1016 
1017         /* It is ensured by skb_flow_dissector_init() that basic key will
1018          * be always present.
1019          */
1020         key_basic = skb_flow_dissector_target(flow_dissector,
1021                                               FLOW_DISSECTOR_KEY_BASIC,
1022                                               target_container);
1023 
1024         if (skb) {
1025                 if (!net) {
1026                         if (skb->dev)
1027                                 net = dev_net(skb->dev);
1028                         else if (skb->sk)
1029                                 net = sock_net(skb->sk);
1030                 }
1031         }
1032 
1033         WARN_ON_ONCE(!net);
1034         if (net) {
1035                 rcu_read_lock();
1036                 attached = rcu_dereference(init_net.flow_dissector_prog);
1037 
1038                 if (!attached)
1039                         attached = rcu_dereference(net->flow_dissector_prog);
1040 
1041                 if (attached) {
1042                         struct bpf_flow_keys flow_keys;
1043                         struct bpf_flow_dissector ctx = {
1044                                 .flow_keys = &flow_keys,
1045                                 .data = data,
1046                                 .data_end = data + hlen,
1047                         };
1048                         __be16 n_proto = proto;
1049 
1050                         if (skb) {
1051                                 ctx.skb = skb;
1052                                 /* we can't use 'proto' in the skb case
1053                                  * because it might be set to skb->vlan_proto
1054                                  * which has been pulled from the data
1055                                  */
1056                                 n_proto = skb->protocol;
1057                         }
1058 
1059                         ret = bpf_flow_dissect(attached, &ctx, n_proto, nhoff,
1060                                                hlen, flags);
1061                         __skb_flow_bpf_to_target(&flow_keys, flow_dissector,
1062                                                  target_container);
1063                         rcu_read_unlock();
1064                         return ret;
1065                 }
1066                 rcu_read_unlock();
1067         }
1068 
1069         if (dissector_uses_key(flow_dissector,
1070                                FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1071                 struct ethhdr *eth = eth_hdr(skb);
1072                 struct flow_dissector_key_eth_addrs *key_eth_addrs;
1073 
1074                 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
1075                                                           FLOW_DISSECTOR_KEY_ETH_ADDRS,
1076                                                           target_container);
1077                 memcpy(key_eth_addrs, &eth->h_dest, sizeof(*key_eth_addrs));
1078         }
1079 
1080 proto_again:
1081         fdret = FLOW_DISSECT_RET_CONTINUE;
1082 
1083         switch (proto) {
1084         case htons(ETH_P_IP): {
1085                 const struct iphdr *iph;
1086                 struct iphdr _iph;
1087 
1088                 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
1089                 if (!iph || iph->ihl < 5) {
1090                         fdret = FLOW_DISSECT_RET_OUT_BAD;
1091                         break;
1092                 }
1093 
1094                 nhoff += iph->ihl * 4;
1095 
1096                 ip_proto = iph->protocol;
1097 
1098                 if (dissector_uses_key(flow_dissector,
1099                                        FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
1100                         key_addrs = skb_flow_dissector_target(flow_dissector,
1101                                                               FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1102                                                               target_container);
1103 
1104                         memcpy(&key_addrs->v4addrs, &iph->saddr,
1105                                sizeof(key_addrs->v4addrs));
1106                         key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1107                 }
1108 
1109                 if (ip_is_fragment(iph)) {
1110                         key_control->flags |= FLOW_DIS_IS_FRAGMENT;
1111 
1112                         if (iph->frag_off & htons(IP_OFFSET)) {
1113                                 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1114                                 break;
1115                         } else {
1116                                 key_control->flags |= FLOW_DIS_FIRST_FRAG;
1117                                 if (!(flags &
1118                                       FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) {
1119                                         fdret = FLOW_DISSECT_RET_OUT_GOOD;
1120                                         break;
1121                                 }
1122                         }
1123                 }
1124 
1125                 __skb_flow_dissect_ipv4(skb, flow_dissector,
1126                                         target_container, data, iph);
1127 
1128                 break;
1129         }
1130         case htons(ETH_P_IPV6): {
1131                 const struct ipv6hdr *iph;
1132                 struct ipv6hdr _iph;
1133 
1134                 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
1135                 if (!iph) {
1136                         fdret = FLOW_DISSECT_RET_OUT_BAD;
1137                         break;
1138                 }
1139 
1140                 ip_proto = iph->nexthdr;
1141                 nhoff += sizeof(struct ipv6hdr);
1142 
1143                 if (dissector_uses_key(flow_dissector,
1144                                        FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
1145                         key_addrs = skb_flow_dissector_target(flow_dissector,
1146                                                               FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1147                                                               target_container);
1148 
1149                         memcpy(&key_addrs->v6addrs, &iph->saddr,
1150                                sizeof(key_addrs->v6addrs));
1151                         key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1152                 }
1153 
1154                 if ((dissector_uses_key(flow_dissector,
1155                                         FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
1156                      (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
1157                     ip6_flowlabel(iph)) {
1158                         __be32 flow_label = ip6_flowlabel(iph);
1159 
1160                         if (dissector_uses_key(flow_dissector,
1161                                                FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
1162                                 key_tags = skb_flow_dissector_target(flow_dissector,
1163                                                                      FLOW_DISSECTOR_KEY_FLOW_LABEL,
1164                                                                      target_container);
1165                                 key_tags->flow_label = ntohl(flow_label);
1166                         }
1167                         if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) {
1168                                 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1169                                 break;
1170                         }
1171                 }
1172 
1173                 __skb_flow_dissect_ipv6(skb, flow_dissector,
1174                                         target_container, data, iph);
1175 
1176                 break;
1177         }
1178         case htons(ETH_P_8021AD):
1179         case htons(ETH_P_8021Q): {
1180                 const struct vlan_hdr *vlan = NULL;
1181                 struct vlan_hdr _vlan;
1182                 __be16 saved_vlan_tpid = proto;
1183 
1184                 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX &&
1185                     skb && skb_vlan_tag_present(skb)) {
1186                         proto = skb->protocol;
1187                 } else {
1188                         vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
1189                                                     data, hlen, &_vlan);
1190                         if (!vlan) {
1191                                 fdret = FLOW_DISSECT_RET_OUT_BAD;
1192                                 break;
1193                         }
1194 
1195                         proto = vlan->h_vlan_encapsulated_proto;
1196                         nhoff += sizeof(*vlan);
1197                 }
1198 
1199                 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX) {
1200                         dissector_vlan = FLOW_DISSECTOR_KEY_VLAN;
1201                 } else if (dissector_vlan == FLOW_DISSECTOR_KEY_VLAN) {
1202                         dissector_vlan = FLOW_DISSECTOR_KEY_CVLAN;
1203                 } else {
1204                         fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1205                         break;
1206                 }
1207 
1208                 if (dissector_uses_key(flow_dissector, dissector_vlan)) {
1209                         key_vlan = skb_flow_dissector_target(flow_dissector,
1210                                                              dissector_vlan,
1211                                                              target_container);
1212 
1213                         if (!vlan) {
1214                                 key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
1215                                 key_vlan->vlan_priority = skb_vlan_tag_get_prio(skb);
1216                         } else {
1217                                 key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) &
1218                                         VLAN_VID_MASK;
1219                                 key_vlan->vlan_priority =
1220                                         (ntohs(vlan->h_vlan_TCI) &
1221                                          VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
1222                         }
1223                         key_vlan->vlan_tpid = saved_vlan_tpid;
1224                 }
1225 
1226                 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1227                 break;
1228         }
1229         case htons(ETH_P_PPP_SES): {
1230                 struct {
1231                         struct pppoe_hdr hdr;
1232                         __be16 proto;
1233                 } *hdr, _hdr;
1234                 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
1235                 if (!hdr) {
1236                         fdret = FLOW_DISSECT_RET_OUT_BAD;
1237                         break;
1238                 }
1239 
1240                 proto = hdr->proto;
1241                 nhoff += PPPOE_SES_HLEN;
1242                 switch (proto) {
1243                 case htons(PPP_IP):
1244                         proto = htons(ETH_P_IP);
1245                         fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1246                         break;
1247                 case htons(PPP_IPV6):
1248                         proto = htons(ETH_P_IPV6);
1249                         fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1250                         break;
1251                 default:
1252                         fdret = FLOW_DISSECT_RET_OUT_BAD;
1253                         break;
1254                 }
1255                 break;
1256         }
1257         case htons(ETH_P_TIPC): {
1258                 struct tipc_basic_hdr *hdr, _hdr;
1259 
1260                 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr),
1261                                            data, hlen, &_hdr);
1262                 if (!hdr) {
1263                         fdret = FLOW_DISSECT_RET_OUT_BAD;
1264                         break;
1265                 }
1266 
1267                 if (dissector_uses_key(flow_dissector,
1268                                        FLOW_DISSECTOR_KEY_TIPC)) {
1269                         key_addrs = skb_flow_dissector_target(flow_dissector,
1270                                                               FLOW_DISSECTOR_KEY_TIPC,
1271                                                               target_container);
1272                         key_addrs->tipckey.key = tipc_hdr_rps_key(hdr);
1273                         key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC;
1274                 }
1275                 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1276                 break;
1277         }
1278 
1279         case htons(ETH_P_MPLS_UC):
1280         case htons(ETH_P_MPLS_MC):
1281                 fdret = __skb_flow_dissect_mpls(skb, flow_dissector,
1282                                                 target_container, data,
1283                                                 nhoff, hlen);
1284                 break;
1285         case htons(ETH_P_FCOE):
1286                 if ((hlen - nhoff) < FCOE_HEADER_LEN) {
1287                         fdret = FLOW_DISSECT_RET_OUT_BAD;
1288                         break;
1289                 }
1290 
1291                 nhoff += FCOE_HEADER_LEN;
1292                 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1293                 break;
1294 
1295         case htons(ETH_P_ARP):
1296         case htons(ETH_P_RARP):
1297                 fdret = __skb_flow_dissect_arp(skb, flow_dissector,
1298                                                target_container, data,
1299                                                nhoff, hlen);
1300                 break;
1301 
1302         case htons(ETH_P_BATMAN):
1303                 fdret = __skb_flow_dissect_batadv(skb, key_control, data,
1304                                                   &proto, &nhoff, hlen, flags);
1305                 break;
1306 
1307         default:
1308                 fdret = FLOW_DISSECT_RET_OUT_BAD;
1309                 break;
1310         }
1311 
1312         /* Process result of proto processing */
1313         switch (fdret) {
1314         case FLOW_DISSECT_RET_OUT_GOOD:
1315                 goto out_good;
1316         case FLOW_DISSECT_RET_PROTO_AGAIN:
1317                 if (skb_flow_dissect_allowed(&num_hdrs))
1318                         goto proto_again;
1319                 goto out_good;
1320         case FLOW_DISSECT_RET_CONTINUE:
1321         case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1322                 break;
1323         case FLOW_DISSECT_RET_OUT_BAD:
1324         default:
1325                 goto out_bad;
1326         }
1327 
1328 ip_proto_again:
1329         fdret = FLOW_DISSECT_RET_CONTINUE;
1330 
1331         switch (ip_proto) {
1332         case IPPROTO_GRE:
1333                 fdret = __skb_flow_dissect_gre(skb, key_control, flow_dissector,
1334                                                target_container, data,
1335                                                &proto, &nhoff, &hlen, flags);
1336                 break;
1337 
1338         case NEXTHDR_HOP:
1339         case NEXTHDR_ROUTING:
1340         case NEXTHDR_DEST: {
1341                 u8 _opthdr[2], *opthdr;
1342 
1343                 if (proto != htons(ETH_P_IPV6))
1344                         break;
1345 
1346                 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
1347                                               data, hlen, &_opthdr);
1348                 if (!opthdr) {
1349                         fdret = FLOW_DISSECT_RET_OUT_BAD;
1350                         break;
1351                 }
1352 
1353                 ip_proto = opthdr[0];
1354                 nhoff += (opthdr[1] + 1) << 3;
1355 
1356                 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1357                 break;
1358         }
1359         case NEXTHDR_FRAGMENT: {
1360                 struct frag_hdr _fh, *fh;
1361 
1362                 if (proto != htons(ETH_P_IPV6))
1363                         break;
1364 
1365                 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
1366                                           data, hlen, &_fh);
1367 
1368                 if (!fh) {
1369                         fdret = FLOW_DISSECT_RET_OUT_BAD;
1370                         break;
1371                 }
1372 
1373                 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
1374 
1375                 nhoff += sizeof(_fh);
1376                 ip_proto = fh->nexthdr;
1377 
1378                 if (!(fh->frag_off & htons(IP6_OFFSET))) {
1379                         key_control->flags |= FLOW_DIS_FIRST_FRAG;
1380                         if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
1381                                 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1382                                 break;
1383                         }
1384                 }
1385 
1386                 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1387                 break;
1388         }
1389         case IPPROTO_IPIP:
1390                 proto = htons(ETH_P_IP);
1391 
1392                 key_control->flags |= FLOW_DIS_ENCAPSULATION;
1393                 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1394                         fdret = FLOW_DISSECT_RET_OUT_GOOD;
1395                         break;
1396                 }
1397 
1398                 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1399                 break;
1400 
1401         case IPPROTO_IPV6:
1402                 proto = htons(ETH_P_IPV6);
1403 
1404                 key_control->flags |= FLOW_DIS_ENCAPSULATION;
1405                 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1406                         fdret = FLOW_DISSECT_RET_OUT_GOOD;
1407                         break;
1408                 }
1409 
1410                 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1411                 break;
1412 
1413 
1414         case IPPROTO_MPLS:
1415                 proto = htons(ETH_P_MPLS_UC);
1416                 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1417                 break;
1418 
1419         case IPPROTO_TCP:
1420                 __skb_flow_dissect_tcp(skb, flow_dissector, target_container,
1421                                        data, nhoff, hlen);
1422                 break;
1423 
1424         case IPPROTO_ICMP:
1425         case IPPROTO_ICMPV6:
1426                 __skb_flow_dissect_icmp(skb, flow_dissector, target_container,
1427                                         data, nhoff, hlen);
1428                 break;
1429 
1430         default:
1431                 break;
1432         }
1433 
1434         if (!(key_control->flags & FLOW_DIS_IS_FRAGMENT))
1435                 __skb_flow_dissect_ports(skb, flow_dissector, target_container,
1436                                          data, nhoff, ip_proto, hlen);
1437 
1438         /* Process result of IP proto processing */
1439         switch (fdret) {
1440         case FLOW_DISSECT_RET_PROTO_AGAIN:
1441                 if (skb_flow_dissect_allowed(&num_hdrs))
1442                         goto proto_again;
1443                 break;
1444         case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1445                 if (skb_flow_dissect_allowed(&num_hdrs))
1446                         goto ip_proto_again;
1447                 break;
1448         case FLOW_DISSECT_RET_OUT_GOOD:
1449         case FLOW_DISSECT_RET_CONTINUE:
1450                 break;
1451         case FLOW_DISSECT_RET_OUT_BAD:
1452         default:
1453                 goto out_bad;
1454         }
1455 
1456 out_good:
1457         ret = true;
1458 
1459 out:
1460         key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
1461         key_basic->n_proto = proto;
1462         key_basic->ip_proto = ip_proto;
1463 
1464         return ret;
1465 
1466 out_bad:
1467         ret = false;
1468         goto out;
1469 }
1470 EXPORT_SYMBOL(__skb_flow_dissect);
1471 
1472 static siphash_key_t hashrnd __read_mostly;
1473 static __always_inline void __flow_hash_secret_init(void)
1474 {
1475         net_get_random_once(&hashrnd, sizeof(hashrnd));
1476 }
1477 
1478 static const void *flow_keys_hash_start(const struct flow_keys *flow)
1479 {
1480         BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % SIPHASH_ALIGNMENT);
1481         return &flow->FLOW_KEYS_HASH_START_FIELD;
1482 }
1483 
1484 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
1485 {
1486         size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
1487 
1488         BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
1489 
1490         switch (flow->control.addr_type) {
1491         case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1492                 diff -= sizeof(flow->addrs.v4addrs);
1493                 break;
1494         case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1495                 diff -= sizeof(flow->addrs.v6addrs);
1496                 break;
1497         case FLOW_DISSECTOR_KEY_TIPC:
1498                 diff -= sizeof(flow->addrs.tipckey);
1499                 break;
1500         }
1501         return sizeof(*flow) - diff;
1502 }
1503 
1504 __be32 flow_get_u32_src(const struct flow_keys *flow)
1505 {
1506         switch (flow->control.addr_type) {
1507         case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1508                 return flow->addrs.v4addrs.src;
1509         case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1510                 return (__force __be32)ipv6_addr_hash(
1511                         &flow->addrs.v6addrs.src);
1512         case FLOW_DISSECTOR_KEY_TIPC:
1513                 return flow->addrs.tipckey.key;
1514         default:
1515                 return 0;
1516         }
1517 }
1518 EXPORT_SYMBOL(flow_get_u32_src);
1519 
1520 __be32 flow_get_u32_dst(const struct flow_keys *flow)
1521 {
1522         switch (flow->control.addr_type) {
1523         case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1524                 return flow->addrs.v4addrs.dst;
1525         case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1526                 return (__force __be32)ipv6_addr_hash(
1527                         &flow->addrs.v6addrs.dst);
1528         default:
1529                 return 0;
1530         }
1531 }
1532 EXPORT_SYMBOL(flow_get_u32_dst);
1533 
1534 /* Sort the source and destination IP (and the ports if the IP are the same),
1535  * to have consistent hash within the two directions
1536  */
1537 static inline void __flow_hash_consistentify(struct flow_keys *keys)
1538 {
1539         int addr_diff, i;
1540 
1541         switch (keys->control.addr_type) {
1542         case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1543                 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
1544                             (__force u32)keys->addrs.v4addrs.src;
1545                 if ((addr_diff < 0) ||
1546                     (addr_diff == 0 &&
1547                      ((__force u16)keys->ports.dst <
1548                       (__force u16)keys->ports.src))) {
1549                         swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
1550                         swap(keys->ports.src, keys->ports.dst);
1551                 }
1552                 break;
1553         case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1554                 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
1555                                    &keys->addrs.v6addrs.src,
1556                                    sizeof(keys->addrs.v6addrs.dst));
1557                 if ((addr_diff < 0) ||
1558                     (addr_diff == 0 &&
1559                      ((__force u16)keys->ports.dst <
1560                       (__force u16)keys->ports.src))) {
1561                         for (i = 0; i < 4; i++)
1562                                 swap(keys->addrs.v6addrs.src.s6_addr32[i],
1563                                      keys->addrs.v6addrs.dst.s6_addr32[i]);
1564                         swap(keys->ports.src, keys->ports.dst);
1565                 }
1566                 break;
1567         }
1568 }
1569 
1570 static inline u32 __flow_hash_from_keys(struct flow_keys *keys,
1571                                         const siphash_key_t *keyval)
1572 {
1573         u32 hash;
1574 
1575         __flow_hash_consistentify(keys);
1576 
1577         hash = siphash(flow_keys_hash_start(keys),
1578                        flow_keys_hash_length(keys), keyval);
1579         if (!hash)
1580                 hash = 1;
1581 
1582         return hash;
1583 }
1584 
1585 u32 flow_hash_from_keys(struct flow_keys *keys)
1586 {
1587         __flow_hash_secret_init();
1588         return __flow_hash_from_keys(keys, &hashrnd);
1589 }
1590 EXPORT_SYMBOL(flow_hash_from_keys);
1591 
1592 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
1593                                   struct flow_keys *keys,
1594                                   const siphash_key_t *keyval)
1595 {
1596         skb_flow_dissect_flow_keys(skb, keys,
1597                                    FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1598 
1599         return __flow_hash_from_keys(keys, keyval);
1600 }
1601 
1602 struct _flow_keys_digest_data {
1603         __be16  n_proto;
1604         u8      ip_proto;
1605         u8      padding;
1606         __be32  ports;
1607         __be32  src;
1608         __be32  dst;
1609 };
1610 
1611 void make_flow_keys_digest(struct flow_keys_digest *digest,
1612                            const struct flow_keys *flow)
1613 {
1614         struct _flow_keys_digest_data *data =
1615             (struct _flow_keys_digest_data *)digest;
1616 
1617         BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
1618 
1619         memset(digest, 0, sizeof(*digest));
1620 
1621         data->n_proto = flow->basic.n_proto;
1622         data->ip_proto = flow->basic.ip_proto;
1623         data->ports = flow->ports.ports;
1624         data->src = flow->addrs.v4addrs.src;
1625         data->dst = flow->addrs.v4addrs.dst;
1626 }
1627 EXPORT_SYMBOL(make_flow_keys_digest);
1628 
1629 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
1630 
1631 u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
1632 {
1633         struct flow_keys keys;
1634 
1635         __flow_hash_secret_init();
1636 
1637         memset(&keys, 0, sizeof(keys));
1638         __skb_flow_dissect(NULL, skb, &flow_keys_dissector_symmetric,
1639                            &keys, NULL, 0, 0, 0,
1640                            FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1641 
1642         return __flow_hash_from_keys(&keys, &hashrnd);
1643 }
1644 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
1645 
1646 /**
1647  * __skb_get_hash: calculate a flow hash
1648  * @skb: sk_buff to calculate flow hash from
1649  *
1650  * This function calculates a flow hash based on src/dst addresses
1651  * and src/dst port numbers.  Sets hash in skb to non-zero hash value
1652  * on success, zero indicates no valid hash.  Also, sets l4_hash in skb
1653  * if hash is a canonical 4-tuple hash over transport ports.
1654  */
1655 void __skb_get_hash(struct sk_buff *skb)
1656 {
1657         struct flow_keys keys;
1658         u32 hash;
1659 
1660         __flow_hash_secret_init();
1661 
1662         hash = ___skb_get_hash(skb, &keys, &hashrnd);
1663 
1664         __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1665 }
1666 EXPORT_SYMBOL(__skb_get_hash);
1667 
1668 __u32 skb_get_hash_perturb(const struct sk_buff *skb,
1669                            const siphash_key_t *perturb)
1670 {
1671         struct flow_keys keys;
1672 
1673         return ___skb_get_hash(skb, &keys, perturb);
1674 }
1675 EXPORT_SYMBOL(skb_get_hash_perturb);
1676 
1677 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
1678                    const struct flow_keys_basic *keys, int hlen)
1679 {
1680         u32 poff = keys->control.thoff;
1681 
1682         /* skip L4 headers for fragments after the first */
1683         if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
1684             !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
1685                 return poff;
1686 
1687         switch (keys->basic.ip_proto) {
1688         case IPPROTO_TCP: {
1689                 /* access doff as u8 to avoid unaligned access */
1690                 const u8 *doff;
1691                 u8 _doff;
1692 
1693                 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
1694                                             data, hlen, &_doff);
1695                 if (!doff)
1696                         return poff;
1697 
1698                 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
1699                 break;
1700         }
1701         case IPPROTO_UDP:
1702         case IPPROTO_UDPLITE:
1703                 poff += sizeof(struct udphdr);
1704                 break;
1705         /* For the rest, we do not really care about header
1706          * extensions at this point for now.
1707          */
1708         case IPPROTO_ICMP:
1709                 poff += sizeof(struct icmphdr);
1710                 break;
1711         case IPPROTO_ICMPV6:
1712                 poff += sizeof(struct icmp6hdr);
1713                 break;
1714         case IPPROTO_IGMP:
1715                 poff += sizeof(struct igmphdr);
1716                 break;
1717         case IPPROTO_DCCP:
1718                 poff += sizeof(struct dccp_hdr);
1719                 break;
1720         case IPPROTO_SCTP:
1721                 poff += sizeof(struct sctphdr);
1722                 break;
1723         }
1724 
1725         return poff;
1726 }
1727 
1728 /**
1729  * skb_get_poff - get the offset to the payload
1730  * @skb: sk_buff to get the payload offset from
1731  *
1732  * The function will get the offset to the payload as far as it could
1733  * be dissected.  The main user is currently BPF, so that we can dynamically
1734  * truncate packets without needing to push actual payload to the user
1735  * space and can analyze headers only, instead.
1736  */
1737 u32 skb_get_poff(const struct sk_buff *skb)
1738 {
1739         struct flow_keys_basic keys;
1740 
1741         if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
1742                                               NULL, 0, 0, 0, 0))
1743                 return 0;
1744 
1745         return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
1746 }
1747 
1748 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
1749 {
1750         memset(keys, 0, sizeof(*keys));
1751 
1752         memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
1753             sizeof(keys->addrs.v6addrs.src));
1754         memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
1755             sizeof(keys->addrs.v6addrs.dst));
1756         keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1757         keys->ports.src = fl6->fl6_sport;
1758         keys->ports.dst = fl6->fl6_dport;
1759         keys->keyid.keyid = fl6->fl6_gre_key;
1760         keys->tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
1761         keys->basic.ip_proto = fl6->flowi6_proto;
1762 
1763         return flow_hash_from_keys(keys);
1764 }
1765 EXPORT_SYMBOL(__get_hash_from_flowi6);
1766 
1767 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
1768         {
1769                 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1770                 .offset = offsetof(struct flow_keys, control),
1771         },
1772         {
1773                 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1774                 .offset = offsetof(struct flow_keys, basic),
1775         },
1776         {
1777                 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1778                 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1779         },
1780         {
1781                 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1782                 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1783         },
1784         {
1785                 .key_id = FLOW_DISSECTOR_KEY_TIPC,
1786                 .offset = offsetof(struct flow_keys, addrs.tipckey),
1787         },
1788         {
1789                 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1790                 .offset = offsetof(struct flow_keys, ports),
1791         },
1792         {
1793                 .key_id = FLOW_DISSECTOR_KEY_VLAN,
1794                 .offset = offsetof(struct flow_keys, vlan),
1795         },
1796         {
1797                 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
1798                 .offset = offsetof(struct flow_keys, tags),
1799         },
1800         {
1801                 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
1802                 .offset = offsetof(struct flow_keys, keyid),
1803         },
1804 };
1805 
1806 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
1807         {
1808                 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1809                 .offset = offsetof(struct flow_keys, control),
1810         },
1811         {
1812                 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1813                 .offset = offsetof(struct flow_keys, basic),
1814         },
1815         {
1816                 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1817                 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1818         },
1819         {
1820                 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1821                 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1822         },
1823         {
1824                 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1825                 .offset = offsetof(struct flow_keys, ports),
1826         },
1827 };
1828 
1829 static const struct flow_dissector_key flow_keys_basic_dissector_keys[] = {
1830         {
1831                 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1832                 .offset = offsetof(struct flow_keys, control),
1833         },
1834         {
1835                 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1836                 .offset = offsetof(struct flow_keys, basic),
1837         },
1838 };
1839 
1840 struct flow_dissector flow_keys_dissector __read_mostly;
1841 EXPORT_SYMBOL(flow_keys_dissector);
1842 
1843 struct flow_dissector flow_keys_basic_dissector __read_mostly;
1844 EXPORT_SYMBOL(flow_keys_basic_dissector);
1845 
1846 static int __init init_default_flow_dissectors(void)
1847 {
1848         skb_flow_dissector_init(&flow_keys_dissector,
1849                                 flow_keys_dissector_keys,
1850                                 ARRAY_SIZE(flow_keys_dissector_keys));
1851         skb_flow_dissector_init(&flow_keys_dissector_symmetric,
1852                                 flow_keys_dissector_symmetric_keys,
1853                                 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
1854         skb_flow_dissector_init(&flow_keys_basic_dissector,
1855                                 flow_keys_basic_dissector_keys,
1856                                 ARRAY_SIZE(flow_keys_basic_dissector_keys));
1857 
1858         return register_pernet_subsys(&flow_dissector_pernet_ops);
1859 }
1860 core_initcall(init_default_flow_dissectors);
1861 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp