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