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