1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2013 Nicira, Inc. 4 */ 5 6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7 8 #include <linux/types.h> 9 #include <linux/kernel.h> 10 #include <linux/skbuff.h> 11 #include <linux/netdevice.h> 12 #include <linux/in.h> 13 #include <linux/if_arp.h> 14 #include <linux/init.h> 15 #include <linux/in6.h> 16 #include <linux/inetdevice.h> 17 #include <linux/netfilter_ipv4.h> 18 #include <linux/etherdevice.h> 19 #include <linux/if_ether.h> 20 #include <linux/if_vlan.h> 21 #include <linux/static_key.h> 22 23 #include <net/ip.h> 24 #include <net/icmp.h> 25 #include <net/protocol.h> 26 #include <net/ip_tunnels.h> 27 #include <net/ip6_tunnel.h> 28 #include <net/ip6_checksum.h> 29 #include <net/arp.h> 30 #include <net/checksum.h> 31 #include <net/dsfield.h> 32 #include <net/inet_ecn.h> 33 #include <net/xfrm.h> 34 #include <net/net_namespace.h> 35 #include <net/netns/generic.h> 36 #include <net/rtnetlink.h> 37 #include <net/dst_metadata.h> 38 #include <net/geneve.h> 39 #include <net/vxlan.h> 40 #include <net/erspan.h> 41 42 const struct ip_tunnel_encap_ops __rcu * 43 iptun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly; 44 EXPORT_SYMBOL(iptun_encaps); 45 46 const struct ip6_tnl_encap_ops __rcu * 47 ip6tun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly; 48 EXPORT_SYMBOL(ip6tun_encaps); 49 50 void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb, 51 __be32 src, __be32 dst, __u8 proto, 52 __u8 tos, __u8 ttl, __be16 df, bool xnet, 53 u16 ipcb_flags) 54 { 55 int pkt_len = skb->len - skb_inner_network_offset(skb); 56 struct net *net = dev_net(rt->dst.dev); 57 struct net_device *dev = skb->dev; 58 struct iphdr *iph; 59 int err; 60 61 if (unlikely(dev_recursion_level() > IP_TUNNEL_RECURSION_LIMIT)) { 62 if (dev) { 63 net_crit_ratelimited("Dead loop on virtual device %s (net %llu), fix it urgently!\n", 64 dev->name, dev_net(dev)->net_cookie); 65 DEV_STATS_INC(dev, tx_errors); 66 } 67 ip_rt_put(rt); 68 kfree_skb_reason(skb, SKB_DROP_REASON_RECURSION_LIMIT); 69 return; 70 } 71 72 dev_xmit_recursion_inc(); 73 74 skb_scrub_packet(skb, xnet); 75 76 skb_clear_hash_if_not_l4(skb); 77 skb_dst_set(skb, &rt->dst); 78 memset(IPCB(skb), 0, sizeof(*IPCB(skb))); 79 IPCB(skb)->flags = ipcb_flags; 80 81 /* Push down and install the IP header. */ 82 skb_push(skb, sizeof(struct iphdr)); 83 skb_reset_network_header(skb); 84 85 iph = ip_hdr(skb); 86 87 iph->version = 4; 88 iph->ihl = sizeof(struct iphdr) >> 2; 89 iph->frag_off = ip_mtu_locked(&rt->dst) ? 0 : df; 90 iph->protocol = proto; 91 iph->tos = tos; 92 iph->daddr = dst; 93 iph->saddr = src; 94 iph->ttl = ttl; 95 __ip_select_ident(net, iph, skb_shinfo(skb)->gso_segs ?: 1); 96 97 err = ip_local_out(net, sk, skb); 98 99 if (dev) { 100 if (unlikely(net_xmit_eval(err))) 101 pkt_len = 0; 102 iptunnel_xmit_stats(dev, pkt_len); 103 } 104 105 dev_xmit_recursion_dec(); 106 } 107 EXPORT_SYMBOL_GPL(iptunnel_xmit); 108 109 enum skb_drop_reason 110 __iptunnel_pull_header(struct sk_buff *skb, int hdr_len, 111 __be16 inner_proto, bool raw_proto, bool xnet) 112 { 113 enum skb_drop_reason reason; 114 115 reason = pskb_may_pull_reason(skb, hdr_len); 116 if (unlikely(reason)) 117 return reason; 118 119 skb_pull_rcsum(skb, hdr_len); 120 121 if (!raw_proto && inner_proto == htons(ETH_P_TEB)) { 122 struct ethhdr *eh; 123 124 reason = pskb_may_pull_reason(skb, ETH_HLEN); 125 if (unlikely(reason)) 126 return reason; 127 128 eh = (struct ethhdr *)skb->data; 129 if (likely(eth_proto_is_802_3(eh->h_proto))) 130 skb->protocol = eh->h_proto; 131 else 132 skb->protocol = htons(ETH_P_802_2); 133 134 } else { 135 skb->protocol = inner_proto; 136 } 137 138 skb_clear_hash_if_not_l4(skb); 139 __vlan_hwaccel_clear_tag(skb); 140 skb_set_queue_mapping(skb, 0); 141 skb_scrub_packet(skb, xnet); 142 143 if (unlikely(iptunnel_pull_offloads(skb))) 144 return SKB_DROP_REASON_NOMEM; 145 146 return SKB_NOT_DROPPED_YET; 147 } 148 EXPORT_SYMBOL_GPL(__iptunnel_pull_header); 149 150 struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md, 151 gfp_t flags) 152 { 153 IP_TUNNEL_DECLARE_FLAGS(tun_flags) = { }; 154 struct metadata_dst *res; 155 struct ip_tunnel_info *dst, *src; 156 157 if (!md || md->type != METADATA_IP_TUNNEL || 158 md->u.tun_info.mode & IP_TUNNEL_INFO_TX) 159 return NULL; 160 161 src = &md->u.tun_info; 162 res = metadata_dst_alloc(src->options_len, METADATA_IP_TUNNEL, flags); 163 if (!res) 164 return NULL; 165 166 dst = &res->u.tun_info; 167 dst->key.tun_id = src->key.tun_id; 168 if (src->mode & IP_TUNNEL_INFO_IPV6) 169 memcpy(&dst->key.u.ipv6.dst, &src->key.u.ipv6.src, 170 sizeof(struct in6_addr)); 171 else 172 dst->key.u.ipv4.dst = src->key.u.ipv4.src; 173 ip_tunnel_flags_copy(dst->key.tun_flags, src->key.tun_flags); 174 dst->mode = src->mode | IP_TUNNEL_INFO_TX; 175 ip_tunnel_info_opts_set(dst, ip_tunnel_info_opts(src), 176 src->options_len, tun_flags); 177 178 return res; 179 } 180 EXPORT_SYMBOL_GPL(iptunnel_metadata_reply); 181 182 int iptunnel_handle_offloads(struct sk_buff *skb, 183 int gso_type_mask) 184 { 185 int err; 186 187 if (likely(!skb->encapsulation)) { 188 skb_reset_inner_headers(skb); 189 skb->encapsulation = 1; 190 } 191 192 if (skb_is_gso(skb)) { 193 err = skb_header_unclone(skb, GFP_ATOMIC); 194 if (unlikely(err)) 195 return err; 196 skb_shinfo(skb)->gso_type |= gso_type_mask; 197 return 0; 198 } 199 200 if (skb->ip_summed != CHECKSUM_PARTIAL) { 201 skb->ip_summed = CHECKSUM_NONE; 202 /* We clear encapsulation here to prevent badly-written 203 * drivers potentially deciding to offload an inner checksum 204 * if we set CHECKSUM_PARTIAL on the outer header. 205 * This should go away when the drivers are all fixed. 206 */ 207 skb->encapsulation = 0; 208 } 209 210 return 0; 211 } 212 EXPORT_SYMBOL_GPL(iptunnel_handle_offloads); 213 214 /** 215 * iptunnel_pmtud_build_icmp() - Build ICMP error message for PMTUD 216 * @skb: Original packet with L2 header 217 * @mtu: MTU value for ICMP error 218 * 219 * Return: length on success, negative error code if message couldn't be built. 220 */ 221 static int iptunnel_pmtud_build_icmp(struct sk_buff *skb, int mtu) 222 { 223 const struct iphdr *iph; 224 struct icmphdr *icmph; 225 struct iphdr *niph; 226 struct ethhdr eh; 227 int len, err; 228 229 if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct iphdr))) 230 return -EINVAL; 231 232 if (skb_is_gso(skb)) 233 skb_gso_reset(skb); 234 235 skb_copy_bits(skb, skb_mac_offset(skb), &eh, ETH_HLEN); 236 pskb_pull(skb, ETH_HLEN); 237 238 err = pskb_trim(skb, 576 - sizeof(*niph) - sizeof(*icmph)); 239 if (err) 240 return err; 241 242 len = skb->len + sizeof(*icmph); 243 err = skb_cow(skb, sizeof(*niph) + sizeof(*icmph) + ETH_HLEN); 244 if (err) 245 return err; 246 iph = ip_hdr(skb); 247 icmph = skb_push(skb, sizeof(*icmph)); 248 *icmph = (struct icmphdr) { 249 .type = ICMP_DEST_UNREACH, 250 .code = ICMP_FRAG_NEEDED, 251 .checksum = 0, 252 .un.frag.__unused = 0, 253 .un.frag.mtu = htons(mtu), 254 }; 255 icmph->checksum = csum_fold(skb_checksum(skb, 0, len, 0)); 256 skb_reset_transport_header(skb); 257 258 niph = skb_push(skb, sizeof(*niph)); 259 *niph = (struct iphdr) { 260 .ihl = sizeof(*niph) / 4u, 261 .version = 4, 262 .tos = 0, 263 .tot_len = htons(len + sizeof(*niph)), 264 .id = 0, 265 .frag_off = htons(IP_DF), 266 .ttl = iph->ttl, 267 .protocol = IPPROTO_ICMP, 268 .saddr = iph->daddr, 269 .daddr = iph->saddr, 270 }; 271 ip_send_check(niph); 272 skb_reset_network_header(skb); 273 274 skb->ip_summed = CHECKSUM_NONE; 275 276 eth_header(skb, skb->dev, ntohs(eh.h_proto), eh.h_source, eh.h_dest, 0); 277 skb_reset_mac_header(skb); 278 279 if (skb_valid_dst(skb)) 280 skb_dst_drop(skb); 281 282 return skb->len; 283 } 284 285 /** 286 * iptunnel_pmtud_check_icmp() - Trigger ICMP reply if needed and allowed 287 * @skb: Buffer being sent by encapsulation, L2 headers expected 288 * @mtu: Network MTU for path 289 * 290 * Return: 0 for no ICMP reply, length if built, negative value on error. 291 */ 292 static int iptunnel_pmtud_check_icmp(struct sk_buff *skb, int mtu) 293 { 294 const struct iphdr *iph = ip_hdr(skb); 295 296 if (mtu < 576 || iph->frag_off != htons(IP_DF)) 297 return 0; 298 299 if (ipv4_is_lbcast(iph->daddr) || ipv4_is_multicast(iph->daddr) || 300 ipv4_is_zeronet(iph->saddr) || ipv4_is_loopback(iph->saddr) || 301 ipv4_is_lbcast(iph->saddr) || ipv4_is_multicast(iph->saddr)) 302 return 0; 303 304 if (iph->protocol == IPPROTO_ICMP) { 305 const struct icmphdr *icmph; 306 307 if (!pskb_network_may_pull(skb, iph->ihl * 4 + 308 offsetofend(struct icmphdr, type))) 309 return 0; 310 iph = ip_hdr(skb); 311 icmph = (void *)iph + iph->ihl * 4; 312 if (icmp_is_err(icmph->type)) 313 return 0; 314 } 315 return iptunnel_pmtud_build_icmp(skb, mtu); 316 } 317 318 #if IS_ENABLED(CONFIG_IPV6) 319 /** 320 * iptunnel_pmtud_build_icmpv6() - Build ICMPv6 error message for PMTUD 321 * @skb: Original packet with L2 header 322 * @mtu: MTU value for ICMPv6 error 323 * 324 * Return: length on success, negative error code if message couldn't be built. 325 */ 326 static int iptunnel_pmtud_build_icmpv6(struct sk_buff *skb, int mtu) 327 { 328 const struct ipv6hdr *ip6h; 329 struct icmp6hdr *icmp6h; 330 struct ipv6hdr *nip6h; 331 struct ethhdr eh; 332 int len, err; 333 __wsum csum; 334 335 if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct ipv6hdr))) 336 return -EINVAL; 337 338 if (skb_is_gso(skb)) 339 skb_gso_reset(skb); 340 341 skb_copy_bits(skb, skb_mac_offset(skb), &eh, ETH_HLEN); 342 pskb_pull(skb, ETH_HLEN); 343 344 err = pskb_trim(skb, IPV6_MIN_MTU - sizeof(*nip6h) - sizeof(*icmp6h)); 345 if (err) 346 return err; 347 348 len = skb->len + sizeof(*icmp6h); 349 err = skb_cow(skb, sizeof(*nip6h) + sizeof(*icmp6h) + ETH_HLEN); 350 if (err) 351 return err; 352 353 ip6h = ipv6_hdr(skb); 354 icmp6h = skb_push(skb, sizeof(*icmp6h)); 355 *icmp6h = (struct icmp6hdr) { 356 .icmp6_type = ICMPV6_PKT_TOOBIG, 357 .icmp6_code = 0, 358 .icmp6_cksum = 0, 359 .icmp6_mtu = htonl(mtu), 360 }; 361 skb_reset_transport_header(skb); 362 363 nip6h = skb_push(skb, sizeof(*nip6h)); 364 *nip6h = (struct ipv6hdr) { 365 .priority = 0, 366 .version = 6, 367 .flow_lbl = { 0 }, 368 .payload_len = htons(len), 369 .nexthdr = IPPROTO_ICMPV6, 370 .hop_limit = ip6h->hop_limit, 371 .saddr = ip6h->daddr, 372 .daddr = ip6h->saddr, 373 }; 374 skb_reset_network_header(skb); 375 376 csum = skb_checksum(skb, skb_transport_offset(skb), len, 0); 377 icmp6h->icmp6_cksum = csum_ipv6_magic(&nip6h->saddr, &nip6h->daddr, len, 378 IPPROTO_ICMPV6, csum); 379 380 skb->ip_summed = CHECKSUM_NONE; 381 382 eth_header(skb, skb->dev, ntohs(eh.h_proto), eh.h_source, eh.h_dest, 0); 383 skb_reset_mac_header(skb); 384 385 if (skb_valid_dst(skb)) 386 skb_dst_drop(skb); 387 388 return skb->len; 389 } 390 391 /** 392 * iptunnel_pmtud_check_icmpv6() - Trigger ICMPv6 reply if needed and allowed 393 * @skb: Buffer being sent by encapsulation, L2 headers expected 394 * @mtu: Network MTU for path 395 * 396 * Return: 0 for no ICMPv6 reply, length if built, negative value on error. 397 */ 398 static int iptunnel_pmtud_check_icmpv6(struct sk_buff *skb, int mtu) 399 { 400 const struct ipv6hdr *ip6h = ipv6_hdr(skb); 401 int stype = ipv6_addr_type(&ip6h->saddr); 402 u8 proto = ip6h->nexthdr; 403 __be16 frag_off; 404 int offset; 405 406 if (mtu < IPV6_MIN_MTU) 407 return 0; 408 409 if (stype == IPV6_ADDR_ANY || stype == IPV6_ADDR_MULTICAST || 410 stype == IPV6_ADDR_LOOPBACK) 411 return 0; 412 413 offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &proto, 414 &frag_off); 415 if (offset < 0 || (frag_off & htons(~0x7))) 416 return 0; 417 418 if (proto == IPPROTO_ICMPV6) { 419 struct icmp6hdr *icmp6h; 420 421 if (!pskb_may_pull(skb, skb_network_header(skb) + 422 offset + 1 - skb->data)) 423 return 0; 424 425 icmp6h = (struct icmp6hdr *)(skb_network_header(skb) + offset); 426 if (icmpv6_is_err(icmp6h->icmp6_type) || 427 icmp6h->icmp6_type == NDISC_REDIRECT) 428 return 0; 429 } 430 431 return iptunnel_pmtud_build_icmpv6(skb, mtu); 432 } 433 #endif /* IS_ENABLED(CONFIG_IPV6) */ 434 435 /** 436 * skb_tunnel_check_pmtu() - Check, update PMTU and trigger ICMP reply as needed 437 * @skb: Buffer being sent by encapsulation, L2 headers expected 438 * @encap_dst: Destination for tunnel encapsulation (outer IP) 439 * @headroom: Encapsulation header size, bytes 440 * @reply: Build matching ICMP or ICMPv6 message as a result 441 * 442 * L2 tunnel implementations that can carry IP and can be directly bridged 443 * (currently UDP tunnels) can't always rely on IP forwarding paths to handle 444 * PMTU discovery. In the bridged case, ICMP or ICMPv6 messages need to be built 445 * based on payload and sent back by the encapsulation itself. 446 * 447 * For routable interfaces, we just need to update the PMTU for the destination. 448 * 449 * Return: 0 if ICMP error not needed, length if built, negative value on error 450 */ 451 int skb_tunnel_check_pmtu(struct sk_buff *skb, struct dst_entry *encap_dst, 452 int headroom, bool reply) 453 { 454 u32 mtu = dst_mtu(encap_dst) - headroom; 455 456 if ((skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu)) || 457 (!skb_is_gso(skb) && (skb->len - skb_network_offset(skb)) <= mtu)) 458 return 0; 459 460 skb_dst_update_pmtu_no_confirm(skb, mtu); 461 462 if (!reply) 463 return 0; 464 465 if (skb->protocol == htons(ETH_P_IP)) 466 return iptunnel_pmtud_check_icmp(skb, mtu); 467 468 #if IS_ENABLED(CONFIG_IPV6) 469 if (skb->protocol == htons(ETH_P_IPV6)) 470 return iptunnel_pmtud_check_icmpv6(skb, mtu); 471 #endif 472 return 0; 473 } 474 EXPORT_SYMBOL(skb_tunnel_check_pmtu); 475 476 static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = { 477 [LWTUNNEL_IP_UNSPEC] = { .strict_start_type = LWTUNNEL_IP_OPTS }, 478 [LWTUNNEL_IP_ID] = { .type = NLA_U64 }, 479 [LWTUNNEL_IP_DST] = { .type = NLA_U32 }, 480 [LWTUNNEL_IP_SRC] = { .type = NLA_U32 }, 481 [LWTUNNEL_IP_TTL] = { .type = NLA_U8 }, 482 [LWTUNNEL_IP_TOS] = { .type = NLA_U8 }, 483 [LWTUNNEL_IP_FLAGS] = { .type = NLA_U16 }, 484 [LWTUNNEL_IP_OPTS] = { .type = NLA_NESTED }, 485 }; 486 487 static const struct nla_policy ip_opts_policy[LWTUNNEL_IP_OPTS_MAX + 1] = { 488 [LWTUNNEL_IP_OPTS_GENEVE] = { .type = NLA_NESTED }, 489 [LWTUNNEL_IP_OPTS_VXLAN] = { .type = NLA_NESTED }, 490 [LWTUNNEL_IP_OPTS_ERSPAN] = { .type = NLA_NESTED }, 491 }; 492 493 static const struct nla_policy 494 geneve_opt_policy[LWTUNNEL_IP_OPT_GENEVE_MAX + 1] = { 495 [LWTUNNEL_IP_OPT_GENEVE_CLASS] = { .type = NLA_U16 }, 496 [LWTUNNEL_IP_OPT_GENEVE_TYPE] = { .type = NLA_U8 }, 497 [LWTUNNEL_IP_OPT_GENEVE_DATA] = { .type = NLA_BINARY, .len = 127 }, 498 }; 499 500 static const struct nla_policy 501 vxlan_opt_policy[LWTUNNEL_IP_OPT_VXLAN_MAX + 1] = { 502 [LWTUNNEL_IP_OPT_VXLAN_GBP] = { .type = NLA_U32 }, 503 }; 504 505 static const struct nla_policy 506 erspan_opt_policy[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1] = { 507 [LWTUNNEL_IP_OPT_ERSPAN_VER] = { .type = NLA_U8 }, 508 [LWTUNNEL_IP_OPT_ERSPAN_INDEX] = { .type = NLA_U32 }, 509 [LWTUNNEL_IP_OPT_ERSPAN_DIR] = { .type = NLA_U8 }, 510 [LWTUNNEL_IP_OPT_ERSPAN_HWID] = { .type = NLA_U8 }, 511 }; 512 513 static int ip_tun_parse_opts_geneve(struct nlattr *attr, 514 struct ip_tunnel_info *info, int opts_len, 515 struct netlink_ext_ack *extack) 516 { 517 struct nlattr *tb[LWTUNNEL_IP_OPT_GENEVE_MAX + 1]; 518 int data_len, err; 519 520 err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_GENEVE_MAX, attr, 521 geneve_opt_policy, extack); 522 if (err) 523 return err; 524 525 if (!tb[LWTUNNEL_IP_OPT_GENEVE_CLASS] || 526 !tb[LWTUNNEL_IP_OPT_GENEVE_TYPE] || 527 !tb[LWTUNNEL_IP_OPT_GENEVE_DATA]) 528 return -EINVAL; 529 530 attr = tb[LWTUNNEL_IP_OPT_GENEVE_DATA]; 531 data_len = nla_len(attr); 532 if (data_len % 4) 533 return -EINVAL; 534 535 if (info) { 536 struct geneve_opt *opt = ip_tunnel_info_opts(info) + opts_len; 537 538 memcpy(opt->opt_data, nla_data(attr), data_len); 539 opt->length = data_len / 4; 540 attr = tb[LWTUNNEL_IP_OPT_GENEVE_CLASS]; 541 opt->opt_class = nla_get_be16(attr); 542 attr = tb[LWTUNNEL_IP_OPT_GENEVE_TYPE]; 543 opt->type = nla_get_u8(attr); 544 __set_bit(IP_TUNNEL_GENEVE_OPT_BIT, info->key.tun_flags); 545 } 546 547 return sizeof(struct geneve_opt) + data_len; 548 } 549 550 static int ip_tun_parse_opts_vxlan(struct nlattr *attr, 551 struct ip_tunnel_info *info, int opts_len, 552 struct netlink_ext_ack *extack) 553 { 554 struct nlattr *tb[LWTUNNEL_IP_OPT_VXLAN_MAX + 1]; 555 int err; 556 557 err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_VXLAN_MAX, attr, 558 vxlan_opt_policy, extack); 559 if (err) 560 return err; 561 562 if (!tb[LWTUNNEL_IP_OPT_VXLAN_GBP]) 563 return -EINVAL; 564 565 if (info) { 566 struct vxlan_metadata *md = 567 ip_tunnel_info_opts(info) + opts_len; 568 569 attr = tb[LWTUNNEL_IP_OPT_VXLAN_GBP]; 570 md->gbp = nla_get_u32(attr); 571 md->gbp &= VXLAN_GBP_MASK; 572 __set_bit(IP_TUNNEL_VXLAN_OPT_BIT, info->key.tun_flags); 573 } 574 575 return sizeof(struct vxlan_metadata); 576 } 577 578 static int ip_tun_parse_opts_erspan(struct nlattr *attr, 579 struct ip_tunnel_info *info, int opts_len, 580 struct netlink_ext_ack *extack) 581 { 582 struct nlattr *tb[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1]; 583 int err; 584 u8 ver; 585 586 err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_ERSPAN_MAX, attr, 587 erspan_opt_policy, extack); 588 if (err) 589 return err; 590 591 if (!tb[LWTUNNEL_IP_OPT_ERSPAN_VER]) 592 return -EINVAL; 593 594 ver = nla_get_u8(tb[LWTUNNEL_IP_OPT_ERSPAN_VER]); 595 if (ver == 1) { 596 if (!tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX]) 597 return -EINVAL; 598 } else if (ver == 2) { 599 if (!tb[LWTUNNEL_IP_OPT_ERSPAN_DIR] || 600 !tb[LWTUNNEL_IP_OPT_ERSPAN_HWID]) 601 return -EINVAL; 602 } else { 603 return -EINVAL; 604 } 605 606 if (info) { 607 struct erspan_metadata *md = 608 ip_tunnel_info_opts(info) + opts_len; 609 610 md->version = ver; 611 if (ver == 1) { 612 attr = tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX]; 613 md->u.index = nla_get_be32(attr); 614 } else { 615 attr = tb[LWTUNNEL_IP_OPT_ERSPAN_DIR]; 616 md->u.md2.dir = nla_get_u8(attr); 617 attr = tb[LWTUNNEL_IP_OPT_ERSPAN_HWID]; 618 set_hwid(&md->u.md2, nla_get_u8(attr)); 619 } 620 621 __set_bit(IP_TUNNEL_ERSPAN_OPT_BIT, info->key.tun_flags); 622 } 623 624 return sizeof(struct erspan_metadata); 625 } 626 627 static int ip_tun_parse_opts(struct nlattr *attr, struct ip_tunnel_info *info, 628 struct netlink_ext_ack *extack) 629 { 630 int err, rem, opt_len, opts_len = 0; 631 struct nlattr *nla; 632 u32 type = 0; 633 634 if (!attr) 635 return 0; 636 637 err = nla_validate(nla_data(attr), nla_len(attr), LWTUNNEL_IP_OPTS_MAX, 638 ip_opts_policy, extack); 639 if (err) 640 return err; 641 642 nla_for_each_attr(nla, nla_data(attr), nla_len(attr), rem) { 643 switch (nla_type(nla)) { 644 case LWTUNNEL_IP_OPTS_GENEVE: 645 if (type && type != IP_TUNNEL_GENEVE_OPT_BIT) 646 return -EINVAL; 647 opt_len = ip_tun_parse_opts_geneve(nla, info, opts_len, 648 extack); 649 if (opt_len < 0) 650 return opt_len; 651 opts_len += opt_len; 652 if (opts_len > IP_TUNNEL_OPTS_MAX) 653 return -EINVAL; 654 type = IP_TUNNEL_GENEVE_OPT_BIT; 655 break; 656 case LWTUNNEL_IP_OPTS_VXLAN: 657 if (type) 658 return -EINVAL; 659 opt_len = ip_tun_parse_opts_vxlan(nla, info, opts_len, 660 extack); 661 if (opt_len < 0) 662 return opt_len; 663 opts_len += opt_len; 664 type = IP_TUNNEL_VXLAN_OPT_BIT; 665 break; 666 case LWTUNNEL_IP_OPTS_ERSPAN: 667 if (type) 668 return -EINVAL; 669 opt_len = ip_tun_parse_opts_erspan(nla, info, opts_len, 670 extack); 671 if (opt_len < 0) 672 return opt_len; 673 opts_len += opt_len; 674 type = IP_TUNNEL_ERSPAN_OPT_BIT; 675 break; 676 default: 677 return -EINVAL; 678 } 679 } 680 681 return opts_len; 682 } 683 684 static int ip_tun_get_optlen(struct nlattr *attr, 685 struct netlink_ext_ack *extack) 686 { 687 return ip_tun_parse_opts(attr, NULL, extack); 688 } 689 690 static int ip_tun_set_opts(struct nlattr *attr, struct ip_tunnel_info *info, 691 int opts_len, struct netlink_ext_ack *extack) 692 { 693 /* `options_len` is the __counted_by() annotation of the `options` 694 * flexible array, it must be initialized before parsing writes 695 * into it. 696 */ 697 info->options_len = opts_len; 698 699 return ip_tun_parse_opts(attr, info, extack); 700 } 701 702 static int ip_tun_build_state(struct net *net, struct nlattr *attr, 703 unsigned int family, const void *cfg, 704 struct lwtunnel_state **ts, 705 struct netlink_ext_ack *extack) 706 { 707 struct nlattr *tb[LWTUNNEL_IP_MAX + 1]; 708 struct lwtunnel_state *new_state; 709 struct ip_tunnel_info *tun_info; 710 int err, opt_len; 711 712 err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_MAX, attr, 713 ip_tun_policy, extack); 714 if (err < 0) 715 return err; 716 717 opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP_OPTS], extack); 718 if (opt_len < 0) 719 return opt_len; 720 721 new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len); 722 if (!new_state) 723 return -ENOMEM; 724 725 new_state->type = LWTUNNEL_ENCAP_IP; 726 727 tun_info = lwt_tun_info(new_state); 728 729 err = ip_tun_set_opts(tb[LWTUNNEL_IP_OPTS], tun_info, opt_len, 730 extack); 731 if (err < 0) { 732 lwtstate_free(new_state); 733 return err; 734 } 735 736 #ifdef CONFIG_DST_CACHE 737 err = dst_cache_init(&tun_info->dst_cache, GFP_KERNEL); 738 if (err) { 739 lwtstate_free(new_state); 740 return err; 741 } 742 #endif 743 744 if (tb[LWTUNNEL_IP_ID]) 745 tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP_ID]); 746 747 if (tb[LWTUNNEL_IP_DST]) 748 tun_info->key.u.ipv4.dst = nla_get_in_addr(tb[LWTUNNEL_IP_DST]); 749 750 if (tb[LWTUNNEL_IP_SRC]) 751 tun_info->key.u.ipv4.src = nla_get_in_addr(tb[LWTUNNEL_IP_SRC]); 752 753 if (tb[LWTUNNEL_IP_TTL]) 754 tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]); 755 756 if (tb[LWTUNNEL_IP_TOS]) 757 tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]); 758 759 if (tb[LWTUNNEL_IP_FLAGS]) { 760 IP_TUNNEL_DECLARE_FLAGS(flags); 761 762 ip_tunnel_flags_from_be16(flags, 763 nla_get_be16(tb[LWTUNNEL_IP_FLAGS])); 764 ip_tunnel_clear_options_present(flags); 765 766 ip_tunnel_flags_or(tun_info->key.tun_flags, 767 tun_info->key.tun_flags, flags); 768 } 769 770 tun_info->mode = IP_TUNNEL_INFO_TX; 771 772 *ts = new_state; 773 774 return 0; 775 } 776 777 static void ip_tun_destroy_state(struct lwtunnel_state *lwtstate) 778 { 779 #ifdef CONFIG_DST_CACHE 780 struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate); 781 782 dst_cache_destroy(&tun_info->dst_cache); 783 #endif 784 } 785 786 static int ip_tun_fill_encap_opts_geneve(struct sk_buff *skb, 787 struct ip_tunnel_info *tun_info) 788 { 789 struct geneve_opt *opt; 790 struct nlattr *nest; 791 int offset = 0; 792 793 nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_GENEVE); 794 if (!nest) 795 return -ENOMEM; 796 797 while (tun_info->options_len > offset) { 798 opt = ip_tunnel_info_opts(tun_info) + offset; 799 if (nla_put_be16(skb, LWTUNNEL_IP_OPT_GENEVE_CLASS, 800 opt->opt_class) || 801 nla_put_u8(skb, LWTUNNEL_IP_OPT_GENEVE_TYPE, opt->type) || 802 nla_put(skb, LWTUNNEL_IP_OPT_GENEVE_DATA, opt->length * 4, 803 opt->opt_data)) { 804 nla_nest_cancel(skb, nest); 805 return -ENOMEM; 806 } 807 offset += sizeof(*opt) + opt->length * 4; 808 } 809 810 nla_nest_end(skb, nest); 811 return 0; 812 } 813 814 static int ip_tun_fill_encap_opts_vxlan(struct sk_buff *skb, 815 struct ip_tunnel_info *tun_info) 816 { 817 struct vxlan_metadata *md; 818 struct nlattr *nest; 819 820 nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_VXLAN); 821 if (!nest) 822 return -ENOMEM; 823 824 md = ip_tunnel_info_opts(tun_info); 825 if (nla_put_u32(skb, LWTUNNEL_IP_OPT_VXLAN_GBP, md->gbp)) { 826 nla_nest_cancel(skb, nest); 827 return -ENOMEM; 828 } 829 830 nla_nest_end(skb, nest); 831 return 0; 832 } 833 834 static int ip_tun_fill_encap_opts_erspan(struct sk_buff *skb, 835 struct ip_tunnel_info *tun_info) 836 { 837 struct erspan_metadata *md; 838 struct nlattr *nest; 839 840 nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_ERSPAN); 841 if (!nest) 842 return -ENOMEM; 843 844 md = ip_tunnel_info_opts(tun_info); 845 if (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_VER, md->version)) 846 goto err; 847 848 if (md->version == 1 && 849 nla_put_be32(skb, LWTUNNEL_IP_OPT_ERSPAN_INDEX, md->u.index)) 850 goto err; 851 852 if (md->version == 2 && 853 (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_DIR, md->u.md2.dir) || 854 nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_HWID, 855 get_hwid(&md->u.md2)))) 856 goto err; 857 858 nla_nest_end(skb, nest); 859 return 0; 860 err: 861 nla_nest_cancel(skb, nest); 862 return -ENOMEM; 863 } 864 865 static int ip_tun_fill_encap_opts(struct sk_buff *skb, int type, 866 struct ip_tunnel_info *tun_info) 867 { 868 struct nlattr *nest; 869 int err = 0; 870 871 if (!ip_tunnel_is_options_present(tun_info->key.tun_flags)) 872 return 0; 873 874 nest = nla_nest_start_noflag(skb, type); 875 if (!nest) 876 return -ENOMEM; 877 878 if (test_bit(IP_TUNNEL_GENEVE_OPT_BIT, tun_info->key.tun_flags)) 879 err = ip_tun_fill_encap_opts_geneve(skb, tun_info); 880 else if (test_bit(IP_TUNNEL_VXLAN_OPT_BIT, tun_info->key.tun_flags)) 881 err = ip_tun_fill_encap_opts_vxlan(skb, tun_info); 882 else if (test_bit(IP_TUNNEL_ERSPAN_OPT_BIT, tun_info->key.tun_flags)) 883 err = ip_tun_fill_encap_opts_erspan(skb, tun_info); 884 885 if (err) { 886 nla_nest_cancel(skb, nest); 887 return err; 888 } 889 890 nla_nest_end(skb, nest); 891 return 0; 892 } 893 894 static int ip_tun_fill_encap_info(struct sk_buff *skb, 895 struct lwtunnel_state *lwtstate) 896 { 897 struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate); 898 899 if (nla_put_be64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id, 900 LWTUNNEL_IP_PAD) || 901 nla_put_in_addr(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) || 902 nla_put_in_addr(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) || 903 nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) || 904 nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) || 905 nla_put_be16(skb, LWTUNNEL_IP_FLAGS, 906 ip_tunnel_flags_to_be16(tun_info->key.tun_flags)) || 907 ip_tun_fill_encap_opts(skb, LWTUNNEL_IP_OPTS, tun_info)) 908 return -ENOMEM; 909 910 return 0; 911 } 912 913 static int ip_tun_opts_nlsize(struct ip_tunnel_info *info) 914 { 915 int opt_len; 916 917 if (!ip_tunnel_is_options_present(info->key.tun_flags)) 918 return 0; 919 920 opt_len = nla_total_size(0); /* LWTUNNEL_IP_OPTS */ 921 if (test_bit(IP_TUNNEL_GENEVE_OPT_BIT, info->key.tun_flags)) { 922 struct geneve_opt *opt; 923 int offset = 0; 924 925 opt_len += nla_total_size(0); /* LWTUNNEL_IP_OPTS_GENEVE */ 926 while (info->options_len > offset) { 927 opt = ip_tunnel_info_opts(info) + offset; 928 opt_len += nla_total_size(2) /* OPT_GENEVE_CLASS */ 929 + nla_total_size(1) /* OPT_GENEVE_TYPE */ 930 + nla_total_size(opt->length * 4); 931 /* OPT_GENEVE_DATA */ 932 offset += sizeof(*opt) + opt->length * 4; 933 } 934 } else if (test_bit(IP_TUNNEL_VXLAN_OPT_BIT, info->key.tun_flags)) { 935 opt_len += nla_total_size(0) /* LWTUNNEL_IP_OPTS_VXLAN */ 936 + nla_total_size(4); /* OPT_VXLAN_GBP */ 937 } else if (test_bit(IP_TUNNEL_ERSPAN_OPT_BIT, info->key.tun_flags)) { 938 struct erspan_metadata *md = ip_tunnel_info_opts(info); 939 940 opt_len += nla_total_size(0) /* LWTUNNEL_IP_OPTS_ERSPAN */ 941 + nla_total_size(1) /* OPT_ERSPAN_VER */ 942 + (md->version == 1 ? nla_total_size(4) 943 /* OPT_ERSPAN_INDEX (v1) */ 944 : nla_total_size(1) + 945 nla_total_size(1)); 946 /* OPT_ERSPAN_DIR + HWID (v2) */ 947 } 948 949 return opt_len; 950 } 951 952 static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate) 953 { 954 return nla_total_size_64bit(8) /* LWTUNNEL_IP_ID */ 955 + nla_total_size(4) /* LWTUNNEL_IP_DST */ 956 + nla_total_size(4) /* LWTUNNEL_IP_SRC */ 957 + nla_total_size(1) /* LWTUNNEL_IP_TOS */ 958 + nla_total_size(1) /* LWTUNNEL_IP_TTL */ 959 + nla_total_size(2) /* LWTUNNEL_IP_FLAGS */ 960 + ip_tun_opts_nlsize(lwt_tun_info(lwtstate)); 961 /* LWTUNNEL_IP_OPTS */ 962 } 963 964 static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b) 965 { 966 struct ip_tunnel_info *info_a = lwt_tun_info(a); 967 struct ip_tunnel_info *info_b = lwt_tun_info(b); 968 969 return memcmp(info_a, info_b, sizeof(info_a->key)) || 970 info_a->mode != info_b->mode || 971 info_a->options_len != info_b->options_len || 972 memcmp(ip_tunnel_info_opts(info_a), 973 ip_tunnel_info_opts(info_b), info_a->options_len); 974 } 975 976 static const struct lwtunnel_encap_ops ip_tun_lwt_ops = { 977 .build_state = ip_tun_build_state, 978 .destroy_state = ip_tun_destroy_state, 979 .fill_encap = ip_tun_fill_encap_info, 980 .get_encap_size = ip_tun_encap_nlsize, 981 .cmp_encap = ip_tun_cmp_encap, 982 .owner = THIS_MODULE, 983 }; 984 985 static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = { 986 [LWTUNNEL_IP6_UNSPEC] = { .strict_start_type = LWTUNNEL_IP6_OPTS }, 987 [LWTUNNEL_IP6_ID] = { .type = NLA_U64 }, 988 [LWTUNNEL_IP6_DST] = { .len = sizeof(struct in6_addr) }, 989 [LWTUNNEL_IP6_SRC] = { .len = sizeof(struct in6_addr) }, 990 [LWTUNNEL_IP6_HOPLIMIT] = { .type = NLA_U8 }, 991 [LWTUNNEL_IP6_TC] = { .type = NLA_U8 }, 992 [LWTUNNEL_IP6_FLAGS] = { .type = NLA_U16 }, 993 [LWTUNNEL_IP6_OPTS] = { .type = NLA_NESTED }, 994 }; 995 996 static int ip6_tun_build_state(struct net *net, struct nlattr *attr, 997 unsigned int family, const void *cfg, 998 struct lwtunnel_state **ts, 999 struct netlink_ext_ack *extack) 1000 { 1001 struct nlattr *tb[LWTUNNEL_IP6_MAX + 1]; 1002 struct lwtunnel_state *new_state; 1003 struct ip_tunnel_info *tun_info; 1004 int err, opt_len; 1005 1006 err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP6_MAX, attr, 1007 ip6_tun_policy, extack); 1008 if (err < 0) 1009 return err; 1010 1011 opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP6_OPTS], extack); 1012 if (opt_len < 0) 1013 return opt_len; 1014 1015 new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len); 1016 if (!new_state) 1017 return -ENOMEM; 1018 1019 new_state->type = LWTUNNEL_ENCAP_IP6; 1020 1021 tun_info = lwt_tun_info(new_state); 1022 1023 err = ip_tun_set_opts(tb[LWTUNNEL_IP6_OPTS], tun_info, opt_len, 1024 extack); 1025 if (err < 0) { 1026 lwtstate_free(new_state); 1027 return err; 1028 } 1029 1030 if (tb[LWTUNNEL_IP6_ID]) 1031 tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP6_ID]); 1032 1033 if (tb[LWTUNNEL_IP6_DST]) 1034 tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]); 1035 1036 if (tb[LWTUNNEL_IP6_SRC]) 1037 tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]); 1038 1039 if (tb[LWTUNNEL_IP6_HOPLIMIT]) 1040 tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]); 1041 1042 if (tb[LWTUNNEL_IP6_TC]) 1043 tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]); 1044 1045 if (tb[LWTUNNEL_IP6_FLAGS]) { 1046 IP_TUNNEL_DECLARE_FLAGS(flags); 1047 __be16 data; 1048 1049 data = nla_get_be16(tb[LWTUNNEL_IP6_FLAGS]); 1050 ip_tunnel_flags_from_be16(flags, data); 1051 ip_tunnel_clear_options_present(flags); 1052 1053 ip_tunnel_flags_or(tun_info->key.tun_flags, 1054 tun_info->key.tun_flags, flags); 1055 } 1056 1057 tun_info->mode = IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_IPV6; 1058 1059 *ts = new_state; 1060 1061 return 0; 1062 } 1063 1064 static int ip6_tun_fill_encap_info(struct sk_buff *skb, 1065 struct lwtunnel_state *lwtstate) 1066 { 1067 struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate); 1068 1069 if (nla_put_be64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id, 1070 LWTUNNEL_IP6_PAD) || 1071 nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) || 1072 nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) || 1073 nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.tos) || 1074 nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.ttl) || 1075 nla_put_be16(skb, LWTUNNEL_IP6_FLAGS, 1076 ip_tunnel_flags_to_be16(tun_info->key.tun_flags)) || 1077 ip_tun_fill_encap_opts(skb, LWTUNNEL_IP6_OPTS, tun_info)) 1078 return -ENOMEM; 1079 1080 return 0; 1081 } 1082 1083 static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate) 1084 { 1085 return nla_total_size_64bit(8) /* LWTUNNEL_IP6_ID */ 1086 + nla_total_size(16) /* LWTUNNEL_IP6_DST */ 1087 + nla_total_size(16) /* LWTUNNEL_IP6_SRC */ 1088 + nla_total_size(1) /* LWTUNNEL_IP6_HOPLIMIT */ 1089 + nla_total_size(1) /* LWTUNNEL_IP6_TC */ 1090 + nla_total_size(2) /* LWTUNNEL_IP6_FLAGS */ 1091 + ip_tun_opts_nlsize(lwt_tun_info(lwtstate)); 1092 /* LWTUNNEL_IP6_OPTS */ 1093 } 1094 1095 static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = { 1096 .build_state = ip6_tun_build_state, 1097 .fill_encap = ip6_tun_fill_encap_info, 1098 .get_encap_size = ip6_tun_encap_nlsize, 1099 .cmp_encap = ip_tun_cmp_encap, 1100 .owner = THIS_MODULE, 1101 }; 1102 1103 void __init ip_tunnel_core_init(void) 1104 { 1105 /* If you land here, make sure whether increasing ip_tunnel_info's 1106 * options_len is a reasonable choice with its usage in front ends 1107 * (f.e., it's part of flow keys, etc). 1108 */ 1109 BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255); 1110 1111 lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP); 1112 lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6); 1113 } 1114 1115 DEFINE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt); 1116 EXPORT_SYMBOL(ip_tunnel_metadata_cnt); 1117 1118 void ip_tunnel_need_metadata(void) 1119 { 1120 static_branch_inc(&ip_tunnel_metadata_cnt); 1121 } 1122 EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata); 1123 1124 void ip_tunnel_unneed_metadata(void) 1125 { 1126 static_branch_dec(&ip_tunnel_metadata_cnt); 1127 } 1128 EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata); 1129 1130 /* Returns either the correct skb->protocol value, or 0 if invalid. */ 1131 __be16 ip_tunnel_parse_protocol(const struct sk_buff *skb) 1132 { 1133 if (skb_network_header(skb) >= skb->head && 1134 (skb_network_header(skb) + sizeof(struct iphdr)) <= skb_tail_pointer(skb) && 1135 ip_hdr(skb)->version == 4) 1136 return htons(ETH_P_IP); 1137 if (skb_network_header(skb) >= skb->head && 1138 (skb_network_header(skb) + sizeof(struct ipv6hdr)) <= skb_tail_pointer(skb) && 1139 ipv6_hdr(skb)->version == 6) 1140 return htons(ETH_P_IPV6); 1141 return 0; 1142 } 1143 EXPORT_SYMBOL(ip_tunnel_parse_protocol); 1144 1145 const struct header_ops ip_tunnel_header_ops = { .parse_protocol = ip_tunnel_parse_protocol }; 1146 EXPORT_SYMBOL(ip_tunnel_header_ops); 1147 1148 /* This function returns true when ENCAP attributes are present in the nl msg */ 1149 bool ip_tunnel_netlink_encap_parms(struct nlattr *data[], 1150 struct ip_tunnel_encap *encap) 1151 { 1152 bool ret = false; 1153 1154 memset(encap, 0, sizeof(*encap)); 1155 1156 if (!data) 1157 return ret; 1158 1159 if (data[IFLA_IPTUN_ENCAP_TYPE]) { 1160 ret = true; 1161 encap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]); 1162 } 1163 1164 if (data[IFLA_IPTUN_ENCAP_FLAGS]) { 1165 ret = true; 1166 encap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]); 1167 } 1168 1169 if (data[IFLA_IPTUN_ENCAP_SPORT]) { 1170 ret = true; 1171 encap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]); 1172 } 1173 1174 if (data[IFLA_IPTUN_ENCAP_DPORT]) { 1175 ret = true; 1176 encap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]); 1177 } 1178 1179 return ret; 1180 } 1181 EXPORT_SYMBOL_GPL(ip_tunnel_netlink_encap_parms); 1182 1183 void ip_tunnel_netlink_parms(struct nlattr *data[], 1184 struct ip_tunnel_parm_kern *parms) 1185 { 1186 if (data[IFLA_IPTUN_LINK]) 1187 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]); 1188 1189 if (data[IFLA_IPTUN_LOCAL]) 1190 parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]); 1191 1192 if (data[IFLA_IPTUN_REMOTE]) 1193 parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]); 1194 1195 if (data[IFLA_IPTUN_TTL]) { 1196 parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]); 1197 if (parms->iph.ttl) 1198 parms->iph.frag_off = htons(IP_DF); 1199 } 1200 1201 if (data[IFLA_IPTUN_TOS]) 1202 parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]); 1203 1204 if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC])) 1205 parms->iph.frag_off = htons(IP_DF); 1206 1207 if (data[IFLA_IPTUN_FLAGS]) { 1208 __be16 flags; 1209 1210 flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]); 1211 ip_tunnel_flags_from_be16(parms->i_flags, flags); 1212 } 1213 1214 if (data[IFLA_IPTUN_PROTO]) 1215 parms->iph.protocol = nla_get_u8(data[IFLA_IPTUN_PROTO]); 1216 } 1217 EXPORT_SYMBOL_GPL(ip_tunnel_netlink_parms); 1218