0001
0002
0003
0004
0005
0006 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0007
0008 #include <linux/types.h>
0009 #include <linux/kernel.h>
0010 #include <linux/skbuff.h>
0011 #include <linux/netdevice.h>
0012 #include <linux/in.h>
0013 #include <linux/if_arp.h>
0014 #include <linux/init.h>
0015 #include <linux/in6.h>
0016 #include <linux/inetdevice.h>
0017 #include <linux/netfilter_ipv4.h>
0018 #include <linux/etherdevice.h>
0019 #include <linux/if_ether.h>
0020 #include <linux/if_vlan.h>
0021 #include <linux/static_key.h>
0022
0023 #include <net/ip.h>
0024 #include <net/icmp.h>
0025 #include <net/protocol.h>
0026 #include <net/ip_tunnels.h>
0027 #include <net/ip6_tunnel.h>
0028 #include <net/ip6_checksum.h>
0029 #include <net/arp.h>
0030 #include <net/checksum.h>
0031 #include <net/dsfield.h>
0032 #include <net/inet_ecn.h>
0033 #include <net/xfrm.h>
0034 #include <net/net_namespace.h>
0035 #include <net/netns/generic.h>
0036 #include <net/rtnetlink.h>
0037 #include <net/dst_metadata.h>
0038 #include <net/geneve.h>
0039 #include <net/vxlan.h>
0040 #include <net/erspan.h>
0041
0042 const struct ip_tunnel_encap_ops __rcu *
0043 iptun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
0044 EXPORT_SYMBOL(iptun_encaps);
0045
0046 const struct ip6_tnl_encap_ops __rcu *
0047 ip6tun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
0048 EXPORT_SYMBOL(ip6tun_encaps);
0049
0050 void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
0051 __be32 src, __be32 dst, __u8 proto,
0052 __u8 tos, __u8 ttl, __be16 df, bool xnet)
0053 {
0054 int pkt_len = skb->len - skb_inner_network_offset(skb);
0055 struct net *net = dev_net(rt->dst.dev);
0056 struct net_device *dev = skb->dev;
0057 struct iphdr *iph;
0058 int err;
0059
0060 skb_scrub_packet(skb, xnet);
0061
0062 skb_clear_hash_if_not_l4(skb);
0063 skb_dst_set(skb, &rt->dst);
0064 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
0065
0066
0067 skb_push(skb, sizeof(struct iphdr));
0068 skb_reset_network_header(skb);
0069
0070 iph = ip_hdr(skb);
0071
0072 iph->version = 4;
0073 iph->ihl = sizeof(struct iphdr) >> 2;
0074 iph->frag_off = ip_mtu_locked(&rt->dst) ? 0 : df;
0075 iph->protocol = proto;
0076 iph->tos = tos;
0077 iph->daddr = dst;
0078 iph->saddr = src;
0079 iph->ttl = ttl;
0080 __ip_select_ident(net, iph, skb_shinfo(skb)->gso_segs ?: 1);
0081
0082 err = ip_local_out(net, sk, skb);
0083
0084 if (dev) {
0085 if (unlikely(net_xmit_eval(err)))
0086 pkt_len = 0;
0087 iptunnel_xmit_stats(dev, pkt_len);
0088 }
0089 }
0090 EXPORT_SYMBOL_GPL(iptunnel_xmit);
0091
0092 int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
0093 __be16 inner_proto, bool raw_proto, bool xnet)
0094 {
0095 if (unlikely(!pskb_may_pull(skb, hdr_len)))
0096 return -ENOMEM;
0097
0098 skb_pull_rcsum(skb, hdr_len);
0099
0100 if (!raw_proto && inner_proto == htons(ETH_P_TEB)) {
0101 struct ethhdr *eh;
0102
0103 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
0104 return -ENOMEM;
0105
0106 eh = (struct ethhdr *)skb->data;
0107 if (likely(eth_proto_is_802_3(eh->h_proto)))
0108 skb->protocol = eh->h_proto;
0109 else
0110 skb->protocol = htons(ETH_P_802_2);
0111
0112 } else {
0113 skb->protocol = inner_proto;
0114 }
0115
0116 skb_clear_hash_if_not_l4(skb);
0117 __vlan_hwaccel_clear_tag(skb);
0118 skb_set_queue_mapping(skb, 0);
0119 skb_scrub_packet(skb, xnet);
0120
0121 return iptunnel_pull_offloads(skb);
0122 }
0123 EXPORT_SYMBOL_GPL(__iptunnel_pull_header);
0124
0125 struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
0126 gfp_t flags)
0127 {
0128 struct metadata_dst *res;
0129 struct ip_tunnel_info *dst, *src;
0130
0131 if (!md || md->type != METADATA_IP_TUNNEL ||
0132 md->u.tun_info.mode & IP_TUNNEL_INFO_TX)
0133 return NULL;
0134
0135 src = &md->u.tun_info;
0136 res = metadata_dst_alloc(src->options_len, METADATA_IP_TUNNEL, flags);
0137 if (!res)
0138 return NULL;
0139
0140 dst = &res->u.tun_info;
0141 dst->key.tun_id = src->key.tun_id;
0142 if (src->mode & IP_TUNNEL_INFO_IPV6)
0143 memcpy(&dst->key.u.ipv6.dst, &src->key.u.ipv6.src,
0144 sizeof(struct in6_addr));
0145 else
0146 dst->key.u.ipv4.dst = src->key.u.ipv4.src;
0147 dst->key.tun_flags = src->key.tun_flags;
0148 dst->mode = src->mode | IP_TUNNEL_INFO_TX;
0149 ip_tunnel_info_opts_set(dst, ip_tunnel_info_opts(src),
0150 src->options_len, 0);
0151
0152 return res;
0153 }
0154 EXPORT_SYMBOL_GPL(iptunnel_metadata_reply);
0155
0156 int iptunnel_handle_offloads(struct sk_buff *skb,
0157 int gso_type_mask)
0158 {
0159 int err;
0160
0161 if (likely(!skb->encapsulation)) {
0162 skb_reset_inner_headers(skb);
0163 skb->encapsulation = 1;
0164 }
0165
0166 if (skb_is_gso(skb)) {
0167 err = skb_header_unclone(skb, GFP_ATOMIC);
0168 if (unlikely(err))
0169 return err;
0170 skb_shinfo(skb)->gso_type |= gso_type_mask;
0171 return 0;
0172 }
0173
0174 if (skb->ip_summed != CHECKSUM_PARTIAL) {
0175 skb->ip_summed = CHECKSUM_NONE;
0176
0177
0178
0179
0180
0181 skb->encapsulation = 0;
0182 }
0183
0184 return 0;
0185 }
0186 EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
0187
0188
0189
0190
0191
0192
0193
0194
0195 static int iptunnel_pmtud_build_icmp(struct sk_buff *skb, int mtu)
0196 {
0197 const struct iphdr *iph = ip_hdr(skb);
0198 struct icmphdr *icmph;
0199 struct iphdr *niph;
0200 struct ethhdr eh;
0201 int len, err;
0202
0203 if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct iphdr)))
0204 return -EINVAL;
0205
0206 skb_copy_bits(skb, skb_mac_offset(skb), &eh, ETH_HLEN);
0207 pskb_pull(skb, ETH_HLEN);
0208 skb_reset_network_header(skb);
0209
0210 err = pskb_trim(skb, 576 - sizeof(*niph) - sizeof(*icmph));
0211 if (err)
0212 return err;
0213
0214 len = skb->len + sizeof(*icmph);
0215 err = skb_cow(skb, sizeof(*niph) + sizeof(*icmph) + ETH_HLEN);
0216 if (err)
0217 return err;
0218
0219 icmph = skb_push(skb, sizeof(*icmph));
0220 *icmph = (struct icmphdr) {
0221 .type = ICMP_DEST_UNREACH,
0222 .code = ICMP_FRAG_NEEDED,
0223 .checksum = 0,
0224 .un.frag.__unused = 0,
0225 .un.frag.mtu = htons(mtu),
0226 };
0227 icmph->checksum = ip_compute_csum(icmph, len);
0228 skb_reset_transport_header(skb);
0229
0230 niph = skb_push(skb, sizeof(*niph));
0231 *niph = (struct iphdr) {
0232 .ihl = sizeof(*niph) / 4u,
0233 .version = 4,
0234 .tos = 0,
0235 .tot_len = htons(len + sizeof(*niph)),
0236 .id = 0,
0237 .frag_off = htons(IP_DF),
0238 .ttl = iph->ttl,
0239 .protocol = IPPROTO_ICMP,
0240 .saddr = iph->daddr,
0241 .daddr = iph->saddr,
0242 };
0243 ip_send_check(niph);
0244 skb_reset_network_header(skb);
0245
0246 skb->ip_summed = CHECKSUM_NONE;
0247
0248 eth_header(skb, skb->dev, ntohs(eh.h_proto), eh.h_source, eh.h_dest, 0);
0249 skb_reset_mac_header(skb);
0250
0251 return skb->len;
0252 }
0253
0254
0255
0256
0257
0258
0259
0260
0261 static int iptunnel_pmtud_check_icmp(struct sk_buff *skb, int mtu)
0262 {
0263 const struct icmphdr *icmph = icmp_hdr(skb);
0264 const struct iphdr *iph = ip_hdr(skb);
0265
0266 if (mtu < 576 || iph->frag_off != htons(IP_DF))
0267 return 0;
0268
0269 if (ipv4_is_lbcast(iph->daddr) || ipv4_is_multicast(iph->daddr) ||
0270 ipv4_is_zeronet(iph->saddr) || ipv4_is_loopback(iph->saddr) ||
0271 ipv4_is_lbcast(iph->saddr) || ipv4_is_multicast(iph->saddr))
0272 return 0;
0273
0274 if (iph->protocol == IPPROTO_ICMP && icmp_is_err(icmph->type))
0275 return 0;
0276
0277 return iptunnel_pmtud_build_icmp(skb, mtu);
0278 }
0279
0280 #if IS_ENABLED(CONFIG_IPV6)
0281
0282
0283
0284
0285
0286
0287
0288 static int iptunnel_pmtud_build_icmpv6(struct sk_buff *skb, int mtu)
0289 {
0290 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
0291 struct icmp6hdr *icmp6h;
0292 struct ipv6hdr *nip6h;
0293 struct ethhdr eh;
0294 int len, err;
0295 __wsum csum;
0296
0297 if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct ipv6hdr)))
0298 return -EINVAL;
0299
0300 skb_copy_bits(skb, skb_mac_offset(skb), &eh, ETH_HLEN);
0301 pskb_pull(skb, ETH_HLEN);
0302 skb_reset_network_header(skb);
0303
0304 err = pskb_trim(skb, IPV6_MIN_MTU - sizeof(*nip6h) - sizeof(*icmp6h));
0305 if (err)
0306 return err;
0307
0308 len = skb->len + sizeof(*icmp6h);
0309 err = skb_cow(skb, sizeof(*nip6h) + sizeof(*icmp6h) + ETH_HLEN);
0310 if (err)
0311 return err;
0312
0313 icmp6h = skb_push(skb, sizeof(*icmp6h));
0314 *icmp6h = (struct icmp6hdr) {
0315 .icmp6_type = ICMPV6_PKT_TOOBIG,
0316 .icmp6_code = 0,
0317 .icmp6_cksum = 0,
0318 .icmp6_mtu = htonl(mtu),
0319 };
0320 skb_reset_transport_header(skb);
0321
0322 nip6h = skb_push(skb, sizeof(*nip6h));
0323 *nip6h = (struct ipv6hdr) {
0324 .priority = 0,
0325 .version = 6,
0326 .flow_lbl = { 0 },
0327 .payload_len = htons(len),
0328 .nexthdr = IPPROTO_ICMPV6,
0329 .hop_limit = ip6h->hop_limit,
0330 .saddr = ip6h->daddr,
0331 .daddr = ip6h->saddr,
0332 };
0333 skb_reset_network_header(skb);
0334
0335 csum = csum_partial(icmp6h, len, 0);
0336 icmp6h->icmp6_cksum = csum_ipv6_magic(&nip6h->saddr, &nip6h->daddr, len,
0337 IPPROTO_ICMPV6, csum);
0338
0339 skb->ip_summed = CHECKSUM_NONE;
0340
0341 eth_header(skb, skb->dev, ntohs(eh.h_proto), eh.h_source, eh.h_dest, 0);
0342 skb_reset_mac_header(skb);
0343
0344 return skb->len;
0345 }
0346
0347
0348
0349
0350
0351
0352
0353
0354 static int iptunnel_pmtud_check_icmpv6(struct sk_buff *skb, int mtu)
0355 {
0356 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
0357 int stype = ipv6_addr_type(&ip6h->saddr);
0358 u8 proto = ip6h->nexthdr;
0359 __be16 frag_off;
0360 int offset;
0361
0362 if (mtu < IPV6_MIN_MTU)
0363 return 0;
0364
0365 if (stype == IPV6_ADDR_ANY || stype == IPV6_ADDR_MULTICAST ||
0366 stype == IPV6_ADDR_LOOPBACK)
0367 return 0;
0368
0369 offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &proto,
0370 &frag_off);
0371 if (offset < 0 || (frag_off & htons(~0x7)))
0372 return 0;
0373
0374 if (proto == IPPROTO_ICMPV6) {
0375 struct icmp6hdr *icmp6h;
0376
0377 if (!pskb_may_pull(skb, skb_network_header(skb) +
0378 offset + 1 - skb->data))
0379 return 0;
0380
0381 icmp6h = (struct icmp6hdr *)(skb_network_header(skb) + offset);
0382 if (icmpv6_is_err(icmp6h->icmp6_type) ||
0383 icmp6h->icmp6_type == NDISC_REDIRECT)
0384 return 0;
0385 }
0386
0387 return iptunnel_pmtud_build_icmpv6(skb, mtu);
0388 }
0389 #endif
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407 int skb_tunnel_check_pmtu(struct sk_buff *skb, struct dst_entry *encap_dst,
0408 int headroom, bool reply)
0409 {
0410 u32 mtu = dst_mtu(encap_dst) - headroom;
0411
0412 if ((skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu)) ||
0413 (!skb_is_gso(skb) && (skb->len - skb_network_offset(skb)) <= mtu))
0414 return 0;
0415
0416 skb_dst_update_pmtu_no_confirm(skb, mtu);
0417
0418 if (!reply || skb->pkt_type == PACKET_HOST)
0419 return 0;
0420
0421 if (skb->protocol == htons(ETH_P_IP))
0422 return iptunnel_pmtud_check_icmp(skb, mtu);
0423
0424 #if IS_ENABLED(CONFIG_IPV6)
0425 if (skb->protocol == htons(ETH_P_IPV6))
0426 return iptunnel_pmtud_check_icmpv6(skb, mtu);
0427 #endif
0428 return 0;
0429 }
0430 EXPORT_SYMBOL(skb_tunnel_check_pmtu);
0431
0432 static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = {
0433 [LWTUNNEL_IP_UNSPEC] = { .strict_start_type = LWTUNNEL_IP_OPTS },
0434 [LWTUNNEL_IP_ID] = { .type = NLA_U64 },
0435 [LWTUNNEL_IP_DST] = { .type = NLA_U32 },
0436 [LWTUNNEL_IP_SRC] = { .type = NLA_U32 },
0437 [LWTUNNEL_IP_TTL] = { .type = NLA_U8 },
0438 [LWTUNNEL_IP_TOS] = { .type = NLA_U8 },
0439 [LWTUNNEL_IP_FLAGS] = { .type = NLA_U16 },
0440 [LWTUNNEL_IP_OPTS] = { .type = NLA_NESTED },
0441 };
0442
0443 static const struct nla_policy ip_opts_policy[LWTUNNEL_IP_OPTS_MAX + 1] = {
0444 [LWTUNNEL_IP_OPTS_GENEVE] = { .type = NLA_NESTED },
0445 [LWTUNNEL_IP_OPTS_VXLAN] = { .type = NLA_NESTED },
0446 [LWTUNNEL_IP_OPTS_ERSPAN] = { .type = NLA_NESTED },
0447 };
0448
0449 static const struct nla_policy
0450 geneve_opt_policy[LWTUNNEL_IP_OPT_GENEVE_MAX + 1] = {
0451 [LWTUNNEL_IP_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
0452 [LWTUNNEL_IP_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
0453 [LWTUNNEL_IP_OPT_GENEVE_DATA] = { .type = NLA_BINARY, .len = 128 },
0454 };
0455
0456 static const struct nla_policy
0457 vxlan_opt_policy[LWTUNNEL_IP_OPT_VXLAN_MAX + 1] = {
0458 [LWTUNNEL_IP_OPT_VXLAN_GBP] = { .type = NLA_U32 },
0459 };
0460
0461 static const struct nla_policy
0462 erspan_opt_policy[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1] = {
0463 [LWTUNNEL_IP_OPT_ERSPAN_VER] = { .type = NLA_U8 },
0464 [LWTUNNEL_IP_OPT_ERSPAN_INDEX] = { .type = NLA_U32 },
0465 [LWTUNNEL_IP_OPT_ERSPAN_DIR] = { .type = NLA_U8 },
0466 [LWTUNNEL_IP_OPT_ERSPAN_HWID] = { .type = NLA_U8 },
0467 };
0468
0469 static int ip_tun_parse_opts_geneve(struct nlattr *attr,
0470 struct ip_tunnel_info *info, int opts_len,
0471 struct netlink_ext_ack *extack)
0472 {
0473 struct nlattr *tb[LWTUNNEL_IP_OPT_GENEVE_MAX + 1];
0474 int data_len, err;
0475
0476 err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_GENEVE_MAX, attr,
0477 geneve_opt_policy, extack);
0478 if (err)
0479 return err;
0480
0481 if (!tb[LWTUNNEL_IP_OPT_GENEVE_CLASS] ||
0482 !tb[LWTUNNEL_IP_OPT_GENEVE_TYPE] ||
0483 !tb[LWTUNNEL_IP_OPT_GENEVE_DATA])
0484 return -EINVAL;
0485
0486 attr = tb[LWTUNNEL_IP_OPT_GENEVE_DATA];
0487 data_len = nla_len(attr);
0488 if (data_len % 4)
0489 return -EINVAL;
0490
0491 if (info) {
0492 struct geneve_opt *opt = ip_tunnel_info_opts(info) + opts_len;
0493
0494 memcpy(opt->opt_data, nla_data(attr), data_len);
0495 opt->length = data_len / 4;
0496 attr = tb[LWTUNNEL_IP_OPT_GENEVE_CLASS];
0497 opt->opt_class = nla_get_be16(attr);
0498 attr = tb[LWTUNNEL_IP_OPT_GENEVE_TYPE];
0499 opt->type = nla_get_u8(attr);
0500 info->key.tun_flags |= TUNNEL_GENEVE_OPT;
0501 }
0502
0503 return sizeof(struct geneve_opt) + data_len;
0504 }
0505
0506 static int ip_tun_parse_opts_vxlan(struct nlattr *attr,
0507 struct ip_tunnel_info *info, int opts_len,
0508 struct netlink_ext_ack *extack)
0509 {
0510 struct nlattr *tb[LWTUNNEL_IP_OPT_VXLAN_MAX + 1];
0511 int err;
0512
0513 err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_VXLAN_MAX, attr,
0514 vxlan_opt_policy, extack);
0515 if (err)
0516 return err;
0517
0518 if (!tb[LWTUNNEL_IP_OPT_VXLAN_GBP])
0519 return -EINVAL;
0520
0521 if (info) {
0522 struct vxlan_metadata *md =
0523 ip_tunnel_info_opts(info) + opts_len;
0524
0525 attr = tb[LWTUNNEL_IP_OPT_VXLAN_GBP];
0526 md->gbp = nla_get_u32(attr);
0527 md->gbp &= VXLAN_GBP_MASK;
0528 info->key.tun_flags |= TUNNEL_VXLAN_OPT;
0529 }
0530
0531 return sizeof(struct vxlan_metadata);
0532 }
0533
0534 static int ip_tun_parse_opts_erspan(struct nlattr *attr,
0535 struct ip_tunnel_info *info, int opts_len,
0536 struct netlink_ext_ack *extack)
0537 {
0538 struct nlattr *tb[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1];
0539 int err;
0540 u8 ver;
0541
0542 err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_ERSPAN_MAX, attr,
0543 erspan_opt_policy, extack);
0544 if (err)
0545 return err;
0546
0547 if (!tb[LWTUNNEL_IP_OPT_ERSPAN_VER])
0548 return -EINVAL;
0549
0550 ver = nla_get_u8(tb[LWTUNNEL_IP_OPT_ERSPAN_VER]);
0551 if (ver == 1) {
0552 if (!tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX])
0553 return -EINVAL;
0554 } else if (ver == 2) {
0555 if (!tb[LWTUNNEL_IP_OPT_ERSPAN_DIR] ||
0556 !tb[LWTUNNEL_IP_OPT_ERSPAN_HWID])
0557 return -EINVAL;
0558 } else {
0559 return -EINVAL;
0560 }
0561
0562 if (info) {
0563 struct erspan_metadata *md =
0564 ip_tunnel_info_opts(info) + opts_len;
0565
0566 md->version = ver;
0567 if (ver == 1) {
0568 attr = tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX];
0569 md->u.index = nla_get_be32(attr);
0570 } else {
0571 attr = tb[LWTUNNEL_IP_OPT_ERSPAN_DIR];
0572 md->u.md2.dir = nla_get_u8(attr);
0573 attr = tb[LWTUNNEL_IP_OPT_ERSPAN_HWID];
0574 set_hwid(&md->u.md2, nla_get_u8(attr));
0575 }
0576
0577 info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
0578 }
0579
0580 return sizeof(struct erspan_metadata);
0581 }
0582
0583 static int ip_tun_parse_opts(struct nlattr *attr, struct ip_tunnel_info *info,
0584 struct netlink_ext_ack *extack)
0585 {
0586 int err, rem, opt_len, opts_len = 0;
0587 struct nlattr *nla;
0588 __be16 type = 0;
0589
0590 if (!attr)
0591 return 0;
0592
0593 err = nla_validate(nla_data(attr), nla_len(attr), LWTUNNEL_IP_OPTS_MAX,
0594 ip_opts_policy, extack);
0595 if (err)
0596 return err;
0597
0598 nla_for_each_attr(nla, nla_data(attr), nla_len(attr), rem) {
0599 switch (nla_type(nla)) {
0600 case LWTUNNEL_IP_OPTS_GENEVE:
0601 if (type && type != TUNNEL_GENEVE_OPT)
0602 return -EINVAL;
0603 opt_len = ip_tun_parse_opts_geneve(nla, info, opts_len,
0604 extack);
0605 if (opt_len < 0)
0606 return opt_len;
0607 opts_len += opt_len;
0608 if (opts_len > IP_TUNNEL_OPTS_MAX)
0609 return -EINVAL;
0610 type = TUNNEL_GENEVE_OPT;
0611 break;
0612 case LWTUNNEL_IP_OPTS_VXLAN:
0613 if (type)
0614 return -EINVAL;
0615 opt_len = ip_tun_parse_opts_vxlan(nla, info, opts_len,
0616 extack);
0617 if (opt_len < 0)
0618 return opt_len;
0619 opts_len += opt_len;
0620 type = TUNNEL_VXLAN_OPT;
0621 break;
0622 case LWTUNNEL_IP_OPTS_ERSPAN:
0623 if (type)
0624 return -EINVAL;
0625 opt_len = ip_tun_parse_opts_erspan(nla, info, opts_len,
0626 extack);
0627 if (opt_len < 0)
0628 return opt_len;
0629 opts_len += opt_len;
0630 type = TUNNEL_ERSPAN_OPT;
0631 break;
0632 default:
0633 return -EINVAL;
0634 }
0635 }
0636
0637 return opts_len;
0638 }
0639
0640 static int ip_tun_get_optlen(struct nlattr *attr,
0641 struct netlink_ext_ack *extack)
0642 {
0643 return ip_tun_parse_opts(attr, NULL, extack);
0644 }
0645
0646 static int ip_tun_set_opts(struct nlattr *attr, struct ip_tunnel_info *info,
0647 struct netlink_ext_ack *extack)
0648 {
0649 return ip_tun_parse_opts(attr, info, extack);
0650 }
0651
0652 static int ip_tun_build_state(struct net *net, struct nlattr *attr,
0653 unsigned int family, const void *cfg,
0654 struct lwtunnel_state **ts,
0655 struct netlink_ext_ack *extack)
0656 {
0657 struct nlattr *tb[LWTUNNEL_IP_MAX + 1];
0658 struct lwtunnel_state *new_state;
0659 struct ip_tunnel_info *tun_info;
0660 int err, opt_len;
0661
0662 err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_MAX, attr,
0663 ip_tun_policy, extack);
0664 if (err < 0)
0665 return err;
0666
0667 opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP_OPTS], extack);
0668 if (opt_len < 0)
0669 return opt_len;
0670
0671 new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
0672 if (!new_state)
0673 return -ENOMEM;
0674
0675 new_state->type = LWTUNNEL_ENCAP_IP;
0676
0677 tun_info = lwt_tun_info(new_state);
0678
0679 err = ip_tun_set_opts(tb[LWTUNNEL_IP_OPTS], tun_info, extack);
0680 if (err < 0) {
0681 lwtstate_free(new_state);
0682 return err;
0683 }
0684
0685 #ifdef CONFIG_DST_CACHE
0686 err = dst_cache_init(&tun_info->dst_cache, GFP_KERNEL);
0687 if (err) {
0688 lwtstate_free(new_state);
0689 return err;
0690 }
0691 #endif
0692
0693 if (tb[LWTUNNEL_IP_ID])
0694 tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP_ID]);
0695
0696 if (tb[LWTUNNEL_IP_DST])
0697 tun_info->key.u.ipv4.dst = nla_get_in_addr(tb[LWTUNNEL_IP_DST]);
0698
0699 if (tb[LWTUNNEL_IP_SRC])
0700 tun_info->key.u.ipv4.src = nla_get_in_addr(tb[LWTUNNEL_IP_SRC]);
0701
0702 if (tb[LWTUNNEL_IP_TTL])
0703 tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]);
0704
0705 if (tb[LWTUNNEL_IP_TOS])
0706 tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]);
0707
0708 if (tb[LWTUNNEL_IP_FLAGS])
0709 tun_info->key.tun_flags |=
0710 (nla_get_be16(tb[LWTUNNEL_IP_FLAGS]) &
0711 ~TUNNEL_OPTIONS_PRESENT);
0712
0713 tun_info->mode = IP_TUNNEL_INFO_TX;
0714 tun_info->options_len = opt_len;
0715
0716 *ts = new_state;
0717
0718 return 0;
0719 }
0720
0721 static void ip_tun_destroy_state(struct lwtunnel_state *lwtstate)
0722 {
0723 #ifdef CONFIG_DST_CACHE
0724 struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
0725
0726 dst_cache_destroy(&tun_info->dst_cache);
0727 #endif
0728 }
0729
0730 static int ip_tun_fill_encap_opts_geneve(struct sk_buff *skb,
0731 struct ip_tunnel_info *tun_info)
0732 {
0733 struct geneve_opt *opt;
0734 struct nlattr *nest;
0735 int offset = 0;
0736
0737 nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_GENEVE);
0738 if (!nest)
0739 return -ENOMEM;
0740
0741 while (tun_info->options_len > offset) {
0742 opt = ip_tunnel_info_opts(tun_info) + offset;
0743 if (nla_put_be16(skb, LWTUNNEL_IP_OPT_GENEVE_CLASS,
0744 opt->opt_class) ||
0745 nla_put_u8(skb, LWTUNNEL_IP_OPT_GENEVE_TYPE, opt->type) ||
0746 nla_put(skb, LWTUNNEL_IP_OPT_GENEVE_DATA, opt->length * 4,
0747 opt->opt_data)) {
0748 nla_nest_cancel(skb, nest);
0749 return -ENOMEM;
0750 }
0751 offset += sizeof(*opt) + opt->length * 4;
0752 }
0753
0754 nla_nest_end(skb, nest);
0755 return 0;
0756 }
0757
0758 static int ip_tun_fill_encap_opts_vxlan(struct sk_buff *skb,
0759 struct ip_tunnel_info *tun_info)
0760 {
0761 struct vxlan_metadata *md;
0762 struct nlattr *nest;
0763
0764 nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_VXLAN);
0765 if (!nest)
0766 return -ENOMEM;
0767
0768 md = ip_tunnel_info_opts(tun_info);
0769 if (nla_put_u32(skb, LWTUNNEL_IP_OPT_VXLAN_GBP, md->gbp)) {
0770 nla_nest_cancel(skb, nest);
0771 return -ENOMEM;
0772 }
0773
0774 nla_nest_end(skb, nest);
0775 return 0;
0776 }
0777
0778 static int ip_tun_fill_encap_opts_erspan(struct sk_buff *skb,
0779 struct ip_tunnel_info *tun_info)
0780 {
0781 struct erspan_metadata *md;
0782 struct nlattr *nest;
0783
0784 nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_ERSPAN);
0785 if (!nest)
0786 return -ENOMEM;
0787
0788 md = ip_tunnel_info_opts(tun_info);
0789 if (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_VER, md->version))
0790 goto err;
0791
0792 if (md->version == 1 &&
0793 nla_put_be32(skb, LWTUNNEL_IP_OPT_ERSPAN_INDEX, md->u.index))
0794 goto err;
0795
0796 if (md->version == 2 &&
0797 (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_DIR, md->u.md2.dir) ||
0798 nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_HWID,
0799 get_hwid(&md->u.md2))))
0800 goto err;
0801
0802 nla_nest_end(skb, nest);
0803 return 0;
0804 err:
0805 nla_nest_cancel(skb, nest);
0806 return -ENOMEM;
0807 }
0808
0809 static int ip_tun_fill_encap_opts(struct sk_buff *skb, int type,
0810 struct ip_tunnel_info *tun_info)
0811 {
0812 struct nlattr *nest;
0813 int err = 0;
0814
0815 if (!(tun_info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))
0816 return 0;
0817
0818 nest = nla_nest_start_noflag(skb, type);
0819 if (!nest)
0820 return -ENOMEM;
0821
0822 if (tun_info->key.tun_flags & TUNNEL_GENEVE_OPT)
0823 err = ip_tun_fill_encap_opts_geneve(skb, tun_info);
0824 else if (tun_info->key.tun_flags & TUNNEL_VXLAN_OPT)
0825 err = ip_tun_fill_encap_opts_vxlan(skb, tun_info);
0826 else if (tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT)
0827 err = ip_tun_fill_encap_opts_erspan(skb, tun_info);
0828
0829 if (err) {
0830 nla_nest_cancel(skb, nest);
0831 return err;
0832 }
0833
0834 nla_nest_end(skb, nest);
0835 return 0;
0836 }
0837
0838 static int ip_tun_fill_encap_info(struct sk_buff *skb,
0839 struct lwtunnel_state *lwtstate)
0840 {
0841 struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
0842
0843 if (nla_put_be64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id,
0844 LWTUNNEL_IP_PAD) ||
0845 nla_put_in_addr(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) ||
0846 nla_put_in_addr(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) ||
0847 nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) ||
0848 nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) ||
0849 nla_put_be16(skb, LWTUNNEL_IP_FLAGS, tun_info->key.tun_flags) ||
0850 ip_tun_fill_encap_opts(skb, LWTUNNEL_IP_OPTS, tun_info))
0851 return -ENOMEM;
0852
0853 return 0;
0854 }
0855
0856 static int ip_tun_opts_nlsize(struct ip_tunnel_info *info)
0857 {
0858 int opt_len;
0859
0860 if (!(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))
0861 return 0;
0862
0863 opt_len = nla_total_size(0);
0864 if (info->key.tun_flags & TUNNEL_GENEVE_OPT) {
0865 struct geneve_opt *opt;
0866 int offset = 0;
0867
0868 opt_len += nla_total_size(0);
0869 while (info->options_len > offset) {
0870 opt = ip_tunnel_info_opts(info) + offset;
0871 opt_len += nla_total_size(2)
0872 + nla_total_size(1)
0873 + nla_total_size(opt->length * 4);
0874
0875 offset += sizeof(*opt) + opt->length * 4;
0876 }
0877 } else if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
0878 opt_len += nla_total_size(0)
0879 + nla_total_size(4);
0880 } else if (info->key.tun_flags & TUNNEL_ERSPAN_OPT) {
0881 struct erspan_metadata *md = ip_tunnel_info_opts(info);
0882
0883 opt_len += nla_total_size(0)
0884 + nla_total_size(1)
0885 + (md->version == 1 ? nla_total_size(4)
0886
0887 : nla_total_size(1) +
0888 nla_total_size(1));
0889
0890 }
0891
0892 return opt_len;
0893 }
0894
0895 static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
0896 {
0897 return nla_total_size_64bit(8)
0898 + nla_total_size(4)
0899 + nla_total_size(4)
0900 + nla_total_size(1)
0901 + nla_total_size(1)
0902 + nla_total_size(2)
0903 + ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
0904
0905 }
0906
0907 static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
0908 {
0909 struct ip_tunnel_info *info_a = lwt_tun_info(a);
0910 struct ip_tunnel_info *info_b = lwt_tun_info(b);
0911
0912 return memcmp(info_a, info_b, sizeof(info_a->key)) ||
0913 info_a->mode != info_b->mode ||
0914 info_a->options_len != info_b->options_len ||
0915 memcmp(ip_tunnel_info_opts(info_a),
0916 ip_tunnel_info_opts(info_b), info_a->options_len);
0917 }
0918
0919 static const struct lwtunnel_encap_ops ip_tun_lwt_ops = {
0920 .build_state = ip_tun_build_state,
0921 .destroy_state = ip_tun_destroy_state,
0922 .fill_encap = ip_tun_fill_encap_info,
0923 .get_encap_size = ip_tun_encap_nlsize,
0924 .cmp_encap = ip_tun_cmp_encap,
0925 .owner = THIS_MODULE,
0926 };
0927
0928 static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = {
0929 [LWTUNNEL_IP6_UNSPEC] = { .strict_start_type = LWTUNNEL_IP6_OPTS },
0930 [LWTUNNEL_IP6_ID] = { .type = NLA_U64 },
0931 [LWTUNNEL_IP6_DST] = { .len = sizeof(struct in6_addr) },
0932 [LWTUNNEL_IP6_SRC] = { .len = sizeof(struct in6_addr) },
0933 [LWTUNNEL_IP6_HOPLIMIT] = { .type = NLA_U8 },
0934 [LWTUNNEL_IP6_TC] = { .type = NLA_U8 },
0935 [LWTUNNEL_IP6_FLAGS] = { .type = NLA_U16 },
0936 [LWTUNNEL_IP6_OPTS] = { .type = NLA_NESTED },
0937 };
0938
0939 static int ip6_tun_build_state(struct net *net, struct nlattr *attr,
0940 unsigned int family, const void *cfg,
0941 struct lwtunnel_state **ts,
0942 struct netlink_ext_ack *extack)
0943 {
0944 struct nlattr *tb[LWTUNNEL_IP6_MAX + 1];
0945 struct lwtunnel_state *new_state;
0946 struct ip_tunnel_info *tun_info;
0947 int err, opt_len;
0948
0949 err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP6_MAX, attr,
0950 ip6_tun_policy, extack);
0951 if (err < 0)
0952 return err;
0953
0954 opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP6_OPTS], extack);
0955 if (opt_len < 0)
0956 return opt_len;
0957
0958 new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
0959 if (!new_state)
0960 return -ENOMEM;
0961
0962 new_state->type = LWTUNNEL_ENCAP_IP6;
0963
0964 tun_info = lwt_tun_info(new_state);
0965
0966 err = ip_tun_set_opts(tb[LWTUNNEL_IP6_OPTS], tun_info, extack);
0967 if (err < 0) {
0968 lwtstate_free(new_state);
0969 return err;
0970 }
0971
0972 if (tb[LWTUNNEL_IP6_ID])
0973 tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP6_ID]);
0974
0975 if (tb[LWTUNNEL_IP6_DST])
0976 tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]);
0977
0978 if (tb[LWTUNNEL_IP6_SRC])
0979 tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]);
0980
0981 if (tb[LWTUNNEL_IP6_HOPLIMIT])
0982 tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]);
0983
0984 if (tb[LWTUNNEL_IP6_TC])
0985 tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]);
0986
0987 if (tb[LWTUNNEL_IP6_FLAGS])
0988 tun_info->key.tun_flags |=
0989 (nla_get_be16(tb[LWTUNNEL_IP6_FLAGS]) &
0990 ~TUNNEL_OPTIONS_PRESENT);
0991
0992 tun_info->mode = IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_IPV6;
0993 tun_info->options_len = opt_len;
0994
0995 *ts = new_state;
0996
0997 return 0;
0998 }
0999
1000 static int ip6_tun_fill_encap_info(struct sk_buff *skb,
1001 struct lwtunnel_state *lwtstate)
1002 {
1003 struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
1004
1005 if (nla_put_be64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id,
1006 LWTUNNEL_IP6_PAD) ||
1007 nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) ||
1008 nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) ||
1009 nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.tos) ||
1010 nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.ttl) ||
1011 nla_put_be16(skb, LWTUNNEL_IP6_FLAGS, tun_info->key.tun_flags) ||
1012 ip_tun_fill_encap_opts(skb, LWTUNNEL_IP6_OPTS, tun_info))
1013 return -ENOMEM;
1014
1015 return 0;
1016 }
1017
1018 static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
1019 {
1020 return nla_total_size_64bit(8)
1021 + nla_total_size(16)
1022 + nla_total_size(16)
1023 + nla_total_size(1)
1024 + nla_total_size(1)
1025 + nla_total_size(2)
1026 + ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
1027
1028 }
1029
1030 static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = {
1031 .build_state = ip6_tun_build_state,
1032 .fill_encap = ip6_tun_fill_encap_info,
1033 .get_encap_size = ip6_tun_encap_nlsize,
1034 .cmp_encap = ip_tun_cmp_encap,
1035 .owner = THIS_MODULE,
1036 };
1037
1038 void __init ip_tunnel_core_init(void)
1039 {
1040
1041
1042
1043
1044 BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255);
1045
1046 lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
1047 lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6);
1048 }
1049
1050 DEFINE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt);
1051 EXPORT_SYMBOL(ip_tunnel_metadata_cnt);
1052
1053 void ip_tunnel_need_metadata(void)
1054 {
1055 static_branch_inc(&ip_tunnel_metadata_cnt);
1056 }
1057 EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata);
1058
1059 void ip_tunnel_unneed_metadata(void)
1060 {
1061 static_branch_dec(&ip_tunnel_metadata_cnt);
1062 }
1063 EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata);
1064
1065
1066 __be16 ip_tunnel_parse_protocol(const struct sk_buff *skb)
1067 {
1068 if (skb_network_header(skb) >= skb->head &&
1069 (skb_network_header(skb) + sizeof(struct iphdr)) <= skb_tail_pointer(skb) &&
1070 ip_hdr(skb)->version == 4)
1071 return htons(ETH_P_IP);
1072 if (skb_network_header(skb) >= skb->head &&
1073 (skb_network_header(skb) + sizeof(struct ipv6hdr)) <= skb_tail_pointer(skb) &&
1074 ipv6_hdr(skb)->version == 6)
1075 return htons(ETH_P_IPV6);
1076 return 0;
1077 }
1078 EXPORT_SYMBOL(ip_tunnel_parse_protocol);
1079
1080 const struct header_ops ip_tunnel_header_ops = { .parse_protocol = ip_tunnel_parse_protocol };
1081 EXPORT_SYMBOL(ip_tunnel_header_ops);