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

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

Version: ~ [ linux-6.1-rc5 ] ~ [ linux-6.0.8 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.78 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.154 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.224 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.265 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.299 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.333 ] ~ [ 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         result = bpf_prog_run_pin_on_cpu(prog, ctx);
940 
941         flow_keys->nhoff = clamp_t(u16, flow_keys->nhoff, nhoff, hlen);
942         flow_keys->thoff = clamp_t(u16, flow_keys->thoff,
943                                    flow_keys->nhoff, hlen);
944 
945         return result == BPF_OK;
946 }
947 
948 /**
949  * __skb_flow_dissect - extract the flow_keys struct and return it
950  * @net: associated network namespace, derived from @skb if NULL
951  * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
952  * @flow_dissector: list of keys to dissect
953  * @target_container: target structure to put dissected values into
954  * @data: raw buffer pointer to the packet, if NULL use skb->data
955  * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
956  * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
957  * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
958  * @flags: flags that control the dissection process, e.g.
959  *         FLOW_DISSECTOR_F_STOP_AT_ENCAP.
960  *
961  * The function will try to retrieve individual keys into target specified
962  * by flow_dissector from either the skbuff or a raw buffer specified by the
963  * rest parameters.
964  *
965  * Caller must take care of zeroing target container memory.
966  */
967 bool __skb_flow_dissect(const struct net *net,
968                         const struct sk_buff *skb,
969                         struct flow_dissector *flow_dissector,
970                         void *target_container,
971                         void *data, __be16 proto, int nhoff, int hlen,
972                         unsigned int flags)
973 {
974         struct flow_dissector_key_control *key_control;
975         struct flow_dissector_key_basic *key_basic;
976         struct flow_dissector_key_addrs *key_addrs;
977         struct flow_dissector_key_tags *key_tags;
978         struct flow_dissector_key_vlan *key_vlan;
979         struct bpf_prog *attached = NULL;
980         enum flow_dissect_ret fdret;
981         enum flow_dissector_key_id dissector_vlan = FLOW_DISSECTOR_KEY_MAX;
982         int num_hdrs = 0;
983         u8 ip_proto = 0;
984         bool ret;
985 
986         if (!data) {
987                 data = skb->data;
988                 proto = skb_vlan_tag_present(skb) ?
989                          skb->vlan_proto : skb->protocol;
990                 nhoff = skb_network_offset(skb);
991                 hlen = skb_headlen(skb);
992 #if IS_ENABLED(CONFIG_NET_DSA)
993                 if (unlikely(skb->dev && netdev_uses_dsa(skb->dev) &&
994                              proto == htons(ETH_P_XDSA))) {
995                         const struct dsa_device_ops *ops;
996                         int offset = 0;
997 
998                         ops = skb->dev->dsa_ptr->tag_ops;
999                         if (ops->flow_dissect &&
1000                             !ops->flow_dissect(skb, &proto, &offset)) {
1001                                 hlen -= offset;
1002                                 nhoff += offset;
1003                         }
1004                 }
1005 #endif
1006         }
1007 
1008         /* It is ensured by skb_flow_dissector_init() that control key will
1009          * be always present.
1010          */
1011         key_control = skb_flow_dissector_target(flow_dissector,
1012                                                 FLOW_DISSECTOR_KEY_CONTROL,
1013                                                 target_container);
1014 
1015         /* It is ensured by skb_flow_dissector_init() that basic key will
1016          * be always present.
1017          */
1018         key_basic = skb_flow_dissector_target(flow_dissector,
1019                                               FLOW_DISSECTOR_KEY_BASIC,
1020                                               target_container);
1021 
1022         if (skb) {
1023                 if (!net) {
1024                         if (skb->dev)
1025                                 net = dev_net(skb->dev);
1026                         else if (skb->sk)
1027                                 net = sock_net(skb->sk);
1028                 }
1029         }
1030 
1031         WARN_ON_ONCE(!net);
1032         if (net) {
1033                 rcu_read_lock();
1034                 attached = rcu_dereference(init_net.flow_dissector_prog);
1035 
1036                 if (!attached)
1037                         attached = rcu_dereference(net->flow_dissector_prog);
1038 
1039                 if (attached) {
1040                         struct bpf_flow_keys flow_keys;
1041                         struct bpf_flow_dissector ctx = {
1042                                 .flow_keys = &flow_keys,
1043                                 .data = data,
1044                                 .data_end = data + hlen,
1045                         };
1046                         __be16 n_proto = proto;
1047 
1048                         if (skb) {
1049                                 ctx.skb = skb;
1050                                 /* we can't use 'proto' in the skb case
1051                                  * because it might be set to skb->vlan_proto
1052                                  * which has been pulled from the data
1053                                  */
1054                                 n_proto = skb->protocol;
1055                         }
1056 
1057                         ret = bpf_flow_dissect(attached, &ctx, n_proto, nhoff,
1058                                                hlen, flags);
1059                         __skb_flow_bpf_to_target(&flow_keys, flow_dissector,
1060                                                  target_container);
1061                         rcu_read_unlock();
1062                         return ret;
1063                 }
1064                 rcu_read_unlock();
1065         }
1066 
1067         if (dissector_uses_key(flow_dissector,
1068                                FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1069                 struct ethhdr *eth = eth_hdr(skb);
1070                 struct flow_dissector_key_eth_addrs *key_eth_addrs;
1071 
1072                 key_eth_addrs = skb_flow_dissector_target(flow_dissector,
1073                                                           FLOW_DISSECTOR_KEY_ETH_ADDRS,
1074                                                           target_container);
1075                 memcpy(key_eth_addrs, &eth->h_dest, sizeof(*key_eth_addrs));
1076         }
1077 
1078 proto_again:
1079         fdret = FLOW_DISSECT_RET_CONTINUE;
1080 
1081         switch (proto) {
1082         case htons(ETH_P_IP): {
1083                 const struct iphdr *iph;
1084                 struct iphdr _iph;
1085 
1086                 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
1087                 if (!iph || iph->ihl < 5) {
1088                         fdret = FLOW_DISSECT_RET_OUT_BAD;
1089                         break;
1090                 }
1091 
1092                 nhoff += iph->ihl * 4;
1093 
1094                 ip_proto = iph->protocol;
1095 
1096                 if (dissector_uses_key(flow_dissector,
1097                                        FLOW_DISSECTOR_KEY_IPV4_ADDRS)) {
1098                         key_addrs = skb_flow_dissector_target(flow_dissector,
1099                                                               FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1100                                                               target_container);
1101 
1102                         memcpy(&key_addrs->v4addrs, &iph->saddr,
1103                                sizeof(key_addrs->v4addrs));
1104                         key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1105                 }
1106 
1107                 if (ip_is_fragment(iph)) {
1108                         key_control->flags |= FLOW_DIS_IS_FRAGMENT;
1109 
1110                         if (iph->frag_off & htons(IP_OFFSET)) {
1111                                 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1112                                 break;
1113                         } else {
1114                                 key_control->flags |= FLOW_DIS_FIRST_FRAG;
1115                                 if (!(flags &
1116                                       FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) {
1117                                         fdret = FLOW_DISSECT_RET_OUT_GOOD;
1118                                         break;
1119                                 }
1120                         }
1121                 }
1122 
1123                 __skb_flow_dissect_ipv4(skb, flow_dissector,
1124                                         target_container, data, iph);
1125 
1126                 break;
1127         }
1128         case htons(ETH_P_IPV6): {
1129                 const struct ipv6hdr *iph;
1130                 struct ipv6hdr _iph;
1131 
1132                 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
1133                 if (!iph) {
1134                         fdret = FLOW_DISSECT_RET_OUT_BAD;
1135                         break;
1136                 }
1137 
1138                 ip_proto = iph->nexthdr;
1139                 nhoff += sizeof(struct ipv6hdr);
1140 
1141                 if (dissector_uses_key(flow_dissector,
1142                                        FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
1143                         key_addrs = skb_flow_dissector_target(flow_dissector,
1144                                                               FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1145                                                               target_container);
1146 
1147                         memcpy(&key_addrs->v6addrs, &iph->saddr,
1148                                sizeof(key_addrs->v6addrs));
1149                         key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1150                 }
1151 
1152                 if ((dissector_uses_key(flow_dissector,
1153                                         FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
1154                      (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
1155                     ip6_flowlabel(iph)) {
1156                         __be32 flow_label = ip6_flowlabel(iph);
1157 
1158                         if (dissector_uses_key(flow_dissector,
1159                                                FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
1160                                 key_tags = skb_flow_dissector_target(flow_dissector,
1161                                                                      FLOW_DISSECTOR_KEY_FLOW_LABEL,
1162                                                                      target_container);
1163                                 key_tags->flow_label = ntohl(flow_label);
1164                         }
1165                         if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) {
1166                                 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1167                                 break;
1168                         }
1169                 }
1170 
1171                 __skb_flow_dissect_ipv6(skb, flow_dissector,
1172                                         target_container, data, iph);
1173 
1174                 break;
1175         }
1176         case htons(ETH_P_8021AD):
1177         case htons(ETH_P_8021Q): {
1178                 const struct vlan_hdr *vlan = NULL;
1179                 struct vlan_hdr _vlan;
1180                 __be16 saved_vlan_tpid = proto;
1181 
1182                 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX &&
1183                     skb && skb_vlan_tag_present(skb)) {
1184                         proto = skb->protocol;
1185                 } else {
1186                         vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan),
1187                                                     data, hlen, &_vlan);
1188                         if (!vlan) {
1189                                 fdret = FLOW_DISSECT_RET_OUT_BAD;
1190                                 break;
1191                         }
1192 
1193                         proto = vlan->h_vlan_encapsulated_proto;
1194                         nhoff += sizeof(*vlan);
1195                 }
1196 
1197                 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX) {
1198                         dissector_vlan = FLOW_DISSECTOR_KEY_VLAN;
1199                 } else if (dissector_vlan == FLOW_DISSECTOR_KEY_VLAN) {
1200                         dissector_vlan = FLOW_DISSECTOR_KEY_CVLAN;
1201                 } else {
1202                         fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1203                         break;
1204                 }
1205 
1206                 if (dissector_uses_key(flow_dissector, dissector_vlan)) {
1207                         key_vlan = skb_flow_dissector_target(flow_dissector,
1208                                                              dissector_vlan,
1209                                                              target_container);
1210 
1211                         if (!vlan) {
1212                                 key_vlan->vlan_id = skb_vlan_tag_get_id(skb);
1213                                 key_vlan->vlan_priority = skb_vlan_tag_get_prio(skb);
1214                         } else {
1215                                 key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) &
1216                                         VLAN_VID_MASK;
1217                                 key_vlan->vlan_priority =
1218                                         (ntohs(vlan->h_vlan_TCI) &
1219                                          VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
1220                         }
1221                         key_vlan->vlan_tpid = saved_vlan_tpid;
1222                 }
1223 
1224                 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1225                 break;
1226         }
1227         case htons(ETH_P_PPP_SES): {
1228                 struct {
1229                         struct pppoe_hdr hdr;
1230                         __be16 proto;
1231                 } *hdr, _hdr;
1232                 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
1233                 if (!hdr) {
1234                         fdret = FLOW_DISSECT_RET_OUT_BAD;
1235                         break;
1236                 }
1237 
1238                 proto = hdr->proto;
1239                 nhoff += PPPOE_SES_HLEN;
1240                 switch (proto) {
1241                 case htons(PPP_IP):
1242                         proto = htons(ETH_P_IP);
1243                         fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1244                         break;
1245                 case htons(PPP_IPV6):
1246                         proto = htons(ETH_P_IPV6);
1247                         fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1248                         break;
1249                 default:
1250                         fdret = FLOW_DISSECT_RET_OUT_BAD;
1251                         break;
1252                 }
1253                 break;
1254         }
1255         case htons(ETH_P_TIPC): {
1256                 struct tipc_basic_hdr *hdr, _hdr;
1257 
1258                 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr),
1259                                            data, hlen, &_hdr);
1260                 if (!hdr) {
1261                         fdret = FLOW_DISSECT_RET_OUT_BAD;
1262                         break;
1263                 }
1264 
1265                 if (dissector_uses_key(flow_dissector,
1266                                        FLOW_DISSECTOR_KEY_TIPC)) {
1267                         key_addrs = skb_flow_dissector_target(flow_dissector,
1268                                                               FLOW_DISSECTOR_KEY_TIPC,
1269                                                               target_container);
1270                         key_addrs->tipckey.key = tipc_hdr_rps_key(hdr);
1271                         key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC;
1272                 }
1273                 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1274                 break;
1275         }
1276 
1277         case htons(ETH_P_MPLS_UC):
1278         case htons(ETH_P_MPLS_MC):
1279                 fdret = __skb_flow_dissect_mpls(skb, flow_dissector,
1280                                                 target_container, data,
1281                                                 nhoff, hlen);
1282                 break;
1283         case htons(ETH_P_FCOE):
1284                 if ((hlen - nhoff) < FCOE_HEADER_LEN) {
1285                         fdret = FLOW_DISSECT_RET_OUT_BAD;
1286                         break;
1287                 }
1288 
1289                 nhoff += FCOE_HEADER_LEN;
1290                 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1291                 break;
1292 
1293         case htons(ETH_P_ARP):
1294         case htons(ETH_P_RARP):
1295                 fdret = __skb_flow_dissect_arp(skb, flow_dissector,
1296                                                target_container, data,
1297                                                nhoff, hlen);
1298                 break;
1299 
1300         case htons(ETH_P_BATMAN):
1301                 fdret = __skb_flow_dissect_batadv(skb, key_control, data,
1302                                                   &proto, &nhoff, hlen, flags);
1303                 break;
1304 
1305         default:
1306                 fdret = FLOW_DISSECT_RET_OUT_BAD;
1307                 break;
1308         }
1309 
1310         /* Process result of proto processing */
1311         switch (fdret) {
1312         case FLOW_DISSECT_RET_OUT_GOOD:
1313                 goto out_good;
1314         case FLOW_DISSECT_RET_PROTO_AGAIN:
1315                 if (skb_flow_dissect_allowed(&num_hdrs))
1316                         goto proto_again;
1317                 goto out_good;
1318         case FLOW_DISSECT_RET_CONTINUE:
1319         case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1320                 break;
1321         case FLOW_DISSECT_RET_OUT_BAD:
1322         default:
1323                 goto out_bad;
1324         }
1325 
1326 ip_proto_again:
1327         fdret = FLOW_DISSECT_RET_CONTINUE;
1328 
1329         switch (ip_proto) {
1330         case IPPROTO_GRE:
1331                 fdret = __skb_flow_dissect_gre(skb, key_control, flow_dissector,
1332                                                target_container, data,
1333                                                &proto, &nhoff, &hlen, flags);
1334                 break;
1335 
1336         case NEXTHDR_HOP:
1337         case NEXTHDR_ROUTING:
1338         case NEXTHDR_DEST: {
1339                 u8 _opthdr[2], *opthdr;
1340 
1341                 if (proto != htons(ETH_P_IPV6))
1342                         break;
1343 
1344                 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
1345                                               data, hlen, &_opthdr);
1346                 if (!opthdr) {
1347                         fdret = FLOW_DISSECT_RET_OUT_BAD;
1348                         break;
1349                 }
1350 
1351                 ip_proto = opthdr[0];
1352                 nhoff += (opthdr[1] + 1) << 3;
1353 
1354                 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1355                 break;
1356         }
1357         case NEXTHDR_FRAGMENT: {
1358                 struct frag_hdr _fh, *fh;
1359 
1360                 if (proto != htons(ETH_P_IPV6))
1361                         break;
1362 
1363                 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
1364                                           data, hlen, &_fh);
1365 
1366                 if (!fh) {
1367                         fdret = FLOW_DISSECT_RET_OUT_BAD;
1368                         break;
1369                 }
1370 
1371                 key_control->flags |= FLOW_DIS_IS_FRAGMENT;
1372 
1373                 nhoff += sizeof(_fh);
1374                 ip_proto = fh->nexthdr;
1375 
1376                 if (!(fh->frag_off & htons(IP6_OFFSET))) {
1377                         key_control->flags |= FLOW_DIS_FIRST_FRAG;
1378                         if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
1379                                 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN;
1380                                 break;
1381                         }
1382                 }
1383 
1384                 fdret = FLOW_DISSECT_RET_OUT_GOOD;
1385                 break;
1386         }
1387         case IPPROTO_IPIP:
1388                 proto = htons(ETH_P_IP);
1389 
1390                 key_control->flags |= FLOW_DIS_ENCAPSULATION;
1391                 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1392                         fdret = FLOW_DISSECT_RET_OUT_GOOD;
1393                         break;
1394                 }
1395 
1396                 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1397                 break;
1398 
1399         case IPPROTO_IPV6:
1400                 proto = htons(ETH_P_IPV6);
1401 
1402                 key_control->flags |= FLOW_DIS_ENCAPSULATION;
1403                 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) {
1404                         fdret = FLOW_DISSECT_RET_OUT_GOOD;
1405                         break;
1406                 }
1407 
1408                 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1409                 break;
1410 
1411 
1412         case IPPROTO_MPLS:
1413                 proto = htons(ETH_P_MPLS_UC);
1414                 fdret = FLOW_DISSECT_RET_PROTO_AGAIN;
1415                 break;
1416 
1417         case IPPROTO_TCP:
1418                 __skb_flow_dissect_tcp(skb, flow_dissector, target_container,
1419                                        data, nhoff, hlen);
1420                 break;
1421 
1422         case IPPROTO_ICMP:
1423         case IPPROTO_ICMPV6:
1424                 __skb_flow_dissect_icmp(skb, flow_dissector, target_container,
1425                                         data, nhoff, hlen);
1426                 break;
1427 
1428         default:
1429                 break;
1430         }
1431 
1432         if (!(key_control->flags & FLOW_DIS_IS_FRAGMENT))
1433                 __skb_flow_dissect_ports(skb, flow_dissector, target_container,
1434                                          data, nhoff, ip_proto, hlen);
1435 
1436         /* Process result of IP proto processing */
1437         switch (fdret) {
1438         case FLOW_DISSECT_RET_PROTO_AGAIN:
1439                 if (skb_flow_dissect_allowed(&num_hdrs))
1440                         goto proto_again;
1441                 break;
1442         case FLOW_DISSECT_RET_IPPROTO_AGAIN:
1443                 if (skb_flow_dissect_allowed(&num_hdrs))
1444                         goto ip_proto_again;
1445                 break;
1446         case FLOW_DISSECT_RET_OUT_GOOD:
1447         case FLOW_DISSECT_RET_CONTINUE:
1448                 break;
1449         case FLOW_DISSECT_RET_OUT_BAD:
1450         default:
1451                 goto out_bad;
1452         }
1453 
1454 out_good:
1455         ret = true;
1456 
1457 out:
1458         key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
1459         key_basic->n_proto = proto;
1460         key_basic->ip_proto = ip_proto;
1461 
1462         return ret;
1463 
1464 out_bad:
1465         ret = false;
1466         goto out;
1467 }
1468 EXPORT_SYMBOL(__skb_flow_dissect);
1469 
1470 static siphash_key_t hashrnd __read_mostly;
1471 static __always_inline void __flow_hash_secret_init(void)
1472 {
1473         net_get_random_once(&hashrnd, sizeof(hashrnd));
1474 }
1475 
1476 static const void *flow_keys_hash_start(const struct flow_keys *flow)
1477 {
1478         BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % SIPHASH_ALIGNMENT);
1479         return &flow->FLOW_KEYS_HASH_START_FIELD;
1480 }
1481 
1482 static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
1483 {
1484         size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
1485 
1486         BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
1487 
1488         switch (flow->control.addr_type) {
1489         case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1490                 diff -= sizeof(flow->addrs.v4addrs);
1491                 break;
1492         case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1493                 diff -= sizeof(flow->addrs.v6addrs);
1494                 break;
1495         case FLOW_DISSECTOR_KEY_TIPC:
1496                 diff -= sizeof(flow->addrs.tipckey);
1497                 break;
1498         }
1499         return sizeof(*flow) - diff;
1500 }
1501 
1502 __be32 flow_get_u32_src(const struct flow_keys *flow)
1503 {
1504         switch (flow->control.addr_type) {
1505         case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1506                 return flow->addrs.v4addrs.src;
1507         case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1508                 return (__force __be32)ipv6_addr_hash(
1509                         &flow->addrs.v6addrs.src);
1510         case FLOW_DISSECTOR_KEY_TIPC:
1511                 return flow->addrs.tipckey.key;
1512         default:
1513                 return 0;
1514         }
1515 }
1516 EXPORT_SYMBOL(flow_get_u32_src);
1517 
1518 __be32 flow_get_u32_dst(const struct flow_keys *flow)
1519 {
1520         switch (flow->control.addr_type) {
1521         case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1522                 return flow->addrs.v4addrs.dst;
1523         case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1524                 return (__force __be32)ipv6_addr_hash(
1525                         &flow->addrs.v6addrs.dst);
1526         default:
1527                 return 0;
1528         }
1529 }
1530 EXPORT_SYMBOL(flow_get_u32_dst);
1531 
1532 /* Sort the source and destination IP (and the ports if the IP are the same),
1533  * to have consistent hash within the two directions
1534  */
1535 static inline void __flow_hash_consistentify(struct flow_keys *keys)
1536 {
1537         int addr_diff, i;
1538 
1539         switch (keys->control.addr_type) {
1540         case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1541                 addr_diff = (__force u32)keys->addrs.v4addrs.dst -
1542                             (__force u32)keys->addrs.v4addrs.src;
1543                 if ((addr_diff < 0) ||
1544                     (addr_diff == 0 &&
1545                      ((__force u16)keys->ports.dst <
1546                       (__force u16)keys->ports.src))) {
1547                         swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
1548                         swap(keys->ports.src, keys->ports.dst);
1549                 }
1550                 break;
1551         case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1552                 addr_diff = memcmp(&keys->addrs.v6addrs.dst,
1553                                    &keys->addrs.v6addrs.src,
1554                                    sizeof(keys->addrs.v6addrs.dst));
1555                 if ((addr_diff < 0) ||
1556                     (addr_diff == 0 &&
1557                      ((__force u16)keys->ports.dst <
1558                       (__force u16)keys->ports.src))) {
1559                         for (i = 0; i < 4; i++)
1560                                 swap(keys->addrs.v6addrs.src.s6_addr32[i],
1561                                      keys->addrs.v6addrs.dst.s6_addr32[i]);
1562                         swap(keys->ports.src, keys->ports.dst);
1563                 }
1564                 break;
1565         }
1566 }
1567 
1568 static inline u32 __flow_hash_from_keys(struct flow_keys *keys,
1569                                         const siphash_key_t *keyval)
1570 {
1571         u32 hash;
1572 
1573         __flow_hash_consistentify(keys);
1574 
1575         hash = siphash(flow_keys_hash_start(keys),
1576                        flow_keys_hash_length(keys), keyval);
1577         if (!hash)
1578                 hash = 1;
1579 
1580         return hash;
1581 }
1582 
1583 u32 flow_hash_from_keys(struct flow_keys *keys)
1584 {
1585         __flow_hash_secret_init();
1586         return __flow_hash_from_keys(keys, &hashrnd);
1587 }
1588 EXPORT_SYMBOL(flow_hash_from_keys);
1589 
1590 static inline u32 ___skb_get_hash(const struct sk_buff *skb,
1591                                   struct flow_keys *keys,
1592                                   const siphash_key_t *keyval)
1593 {
1594         skb_flow_dissect_flow_keys(skb, keys,
1595                                    FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1596 
1597         return __flow_hash_from_keys(keys, keyval);
1598 }
1599 
1600 struct _flow_keys_digest_data {
1601         __be16  n_proto;
1602         u8      ip_proto;
1603         u8      padding;
1604         __be32  ports;
1605         __be32  src;
1606         __be32  dst;
1607 };
1608 
1609 void make_flow_keys_digest(struct flow_keys_digest *digest,
1610                            const struct flow_keys *flow)
1611 {
1612         struct _flow_keys_digest_data *data =
1613             (struct _flow_keys_digest_data *)digest;
1614 
1615         BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
1616 
1617         memset(digest, 0, sizeof(*digest));
1618 
1619         data->n_proto = flow->basic.n_proto;
1620         data->ip_proto = flow->basic.ip_proto;
1621         data->ports = flow->ports.ports;
1622         data->src = flow->addrs.v4addrs.src;
1623         data->dst = flow->addrs.v4addrs.dst;
1624 }
1625 EXPORT_SYMBOL(make_flow_keys_digest);
1626 
1627 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
1628 
1629 u32 __skb_get_hash_symmetric(const struct sk_buff *skb)
1630 {
1631         struct flow_keys keys;
1632 
1633         __flow_hash_secret_init();
1634 
1635         memset(&keys, 0, sizeof(keys));
1636         __skb_flow_dissect(NULL, skb, &flow_keys_dissector_symmetric,
1637                            &keys, NULL, 0, 0, 0,
1638                            FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
1639 
1640         return __flow_hash_from_keys(&keys, &hashrnd);
1641 }
1642 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
1643 
1644 /**
1645  * __skb_get_hash: calculate a flow hash
1646  * @skb: sk_buff to calculate flow hash from
1647  *
1648  * This function calculates a flow hash based on src/dst addresses
1649  * and src/dst port numbers.  Sets hash in skb to non-zero hash value
1650  * on success, zero indicates no valid hash.  Also, sets l4_hash in skb
1651  * if hash is a canonical 4-tuple hash over transport ports.
1652  */
1653 void __skb_get_hash(struct sk_buff *skb)
1654 {
1655         struct flow_keys keys;
1656         u32 hash;
1657 
1658         __flow_hash_secret_init();
1659 
1660         hash = ___skb_get_hash(skb, &keys, &hashrnd);
1661 
1662         __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
1663 }
1664 EXPORT_SYMBOL(__skb_get_hash);
1665 
1666 __u32 skb_get_hash_perturb(const struct sk_buff *skb,
1667                            const siphash_key_t *perturb)
1668 {
1669         struct flow_keys keys;
1670 
1671         return ___skb_get_hash(skb, &keys, perturb);
1672 }
1673 EXPORT_SYMBOL(skb_get_hash_perturb);
1674 
1675 u32 __skb_get_poff(const struct sk_buff *skb, void *data,
1676                    const struct flow_keys_basic *keys, int hlen)
1677 {
1678         u32 poff = keys->control.thoff;
1679 
1680         /* skip L4 headers for fragments after the first */
1681         if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) &&
1682             !(keys->control.flags & FLOW_DIS_FIRST_FRAG))
1683                 return poff;
1684 
1685         switch (keys->basic.ip_proto) {
1686         case IPPROTO_TCP: {
1687                 /* access doff as u8 to avoid unaligned access */
1688                 const u8 *doff;
1689                 u8 _doff;
1690 
1691                 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
1692                                             data, hlen, &_doff);
1693                 if (!doff)
1694                         return poff;
1695 
1696                 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
1697                 break;
1698         }
1699         case IPPROTO_UDP:
1700         case IPPROTO_UDPLITE:
1701                 poff += sizeof(struct udphdr);
1702                 break;
1703         /* For the rest, we do not really care about header
1704          * extensions at this point for now.
1705          */
1706         case IPPROTO_ICMP:
1707                 poff += sizeof(struct icmphdr);
1708                 break;
1709         case IPPROTO_ICMPV6:
1710                 poff += sizeof(struct icmp6hdr);
1711                 break;
1712         case IPPROTO_IGMP:
1713                 poff += sizeof(struct igmphdr);
1714                 break;
1715         case IPPROTO_DCCP:
1716                 poff += sizeof(struct dccp_hdr);
1717                 break;
1718         case IPPROTO_SCTP:
1719                 poff += sizeof(struct sctphdr);
1720                 break;
1721         }
1722 
1723         return poff;
1724 }
1725 
1726 /**
1727  * skb_get_poff - get the offset to the payload
1728  * @skb: sk_buff to get the payload offset from
1729  *
1730  * The function will get the offset to the payload as far as it could
1731  * be dissected.  The main user is currently BPF, so that we can dynamically
1732  * truncate packets without needing to push actual payload to the user
1733  * space and can analyze headers only, instead.
1734  */
1735 u32 skb_get_poff(const struct sk_buff *skb)
1736 {
1737         struct flow_keys_basic keys;
1738 
1739         if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys,
1740                                               NULL, 0, 0, 0, 0))
1741                 return 0;
1742 
1743         return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
1744 }
1745 
1746 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
1747 {
1748         memset(keys, 0, sizeof(*keys));
1749 
1750         memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
1751             sizeof(keys->addrs.v6addrs.src));
1752         memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
1753             sizeof(keys->addrs.v6addrs.dst));
1754         keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1755         keys->ports.src = fl6->fl6_sport;
1756         keys->ports.dst = fl6->fl6_dport;
1757         keys->keyid.keyid = fl6->fl6_gre_key;
1758         keys->tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
1759         keys->basic.ip_proto = fl6->flowi6_proto;
1760 
1761         return flow_hash_from_keys(keys);
1762 }
1763 EXPORT_SYMBOL(__get_hash_from_flowi6);
1764 
1765 static const struct flow_dissector_key flow_keys_dissector_keys[] = {
1766         {
1767                 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1768                 .offset = offsetof(struct flow_keys, control),
1769         },
1770         {
1771                 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1772                 .offset = offsetof(struct flow_keys, basic),
1773         },
1774         {
1775                 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1776                 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1777         },
1778         {
1779                 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1780                 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1781         },
1782         {
1783                 .key_id = FLOW_DISSECTOR_KEY_TIPC,
1784                 .offset = offsetof(struct flow_keys, addrs.tipckey),
1785         },
1786         {
1787                 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1788                 .offset = offsetof(struct flow_keys, ports),
1789         },
1790         {
1791                 .key_id = FLOW_DISSECTOR_KEY_VLAN,
1792                 .offset = offsetof(struct flow_keys, vlan),
1793         },
1794         {
1795                 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
1796                 .offset = offsetof(struct flow_keys, tags),
1797         },
1798         {
1799                 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
1800                 .offset = offsetof(struct flow_keys, keyid),
1801         },
1802 };
1803 
1804 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
1805         {
1806                 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1807                 .offset = offsetof(struct flow_keys, control),
1808         },
1809         {
1810                 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1811                 .offset = offsetof(struct flow_keys, basic),
1812         },
1813         {
1814                 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1815                 .offset = offsetof(struct flow_keys, addrs.v4addrs),
1816         },
1817         {
1818                 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1819                 .offset = offsetof(struct flow_keys, addrs.v6addrs),
1820         },
1821         {
1822                 .key_id = FLOW_DISSECTOR_KEY_PORTS,
1823                 .offset = offsetof(struct flow_keys, ports),
1824         },
1825 };
1826 
1827 static const struct flow_dissector_key flow_keys_basic_dissector_keys[] = {
1828         {
1829                 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
1830                 .offset = offsetof(struct flow_keys, control),
1831         },
1832         {
1833                 .key_id = FLOW_DISSECTOR_KEY_BASIC,
1834                 .offset = offsetof(struct flow_keys, basic),
1835         },
1836 };
1837 
1838 struct flow_dissector flow_keys_dissector __read_mostly;
1839 EXPORT_SYMBOL(flow_keys_dissector);
1840 
1841 struct flow_dissector flow_keys_basic_dissector __read_mostly;
1842 EXPORT_SYMBOL(flow_keys_basic_dissector);
1843 
1844 static int __init init_default_flow_dissectors(void)
1845 {
1846         skb_flow_dissector_init(&flow_keys_dissector,
1847                                 flow_keys_dissector_keys,
1848                                 ARRAY_SIZE(flow_keys_dissector_keys));
1849         skb_flow_dissector_init(&flow_keys_dissector_symmetric,
1850                                 flow_keys_dissector_symmetric_keys,
1851                                 ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
1852         skb_flow_dissector_init(&flow_keys_basic_dissector,
1853                                 flow_keys_basic_dissector_keys,
1854                                 ARRAY_SIZE(flow_keys_basic_dissector_keys));
1855 
1856         return register_pernet_subsys(&flow_dissector_pernet_ops);
1857 }
1858 core_initcall(init_default_flow_dissectors);
1859 

~ [ 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