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

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

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

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