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