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