| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * IPv6 tunneling device |
|---|
| 3 | 4 | * Linux INET6 implementation |
|---|
| .. | .. |
|---|
| 10 | 11 | * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c |
|---|
| 11 | 12 | * |
|---|
| 12 | 13 | * RFC 2473 |
|---|
| 13 | | - * |
|---|
| 14 | | - * This program is free software; you can redistribute it and/or |
|---|
| 15 | | - * modify it under the terms of the GNU General Public License |
|---|
| 16 | | - * as published by the Free Software Foundation; either version |
|---|
| 17 | | - * 2 of the License, or (at your option) any later version. |
|---|
| 18 | | - * |
|---|
| 19 | 14 | */ |
|---|
| 20 | 15 | |
|---|
| 21 | 16 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
|---|
| .. | .. |
|---|
| 94 | 89 | struct ip6_tnl __rcu *collect_md_tun; |
|---|
| 95 | 90 | }; |
|---|
| 96 | 91 | |
|---|
| 92 | +static inline int ip6_tnl_mpls_supported(void) |
|---|
| 93 | +{ |
|---|
| 94 | + return IS_ENABLED(CONFIG_MPLS); |
|---|
| 95 | +} |
|---|
| 96 | + |
|---|
| 97 | 97 | static struct net_device_stats *ip6_get_stats(struct net_device *dev) |
|---|
| 98 | 98 | { |
|---|
| 99 | 99 | struct pcpu_sw_netstats tmp, sum = { 0 }; |
|---|
| .. | .. |
|---|
| 124 | 124 | return &dev->stats; |
|---|
| 125 | 125 | } |
|---|
| 126 | 126 | |
|---|
| 127 | +#define for_each_ip6_tunnel_rcu(start) \ |
|---|
| 128 | + for (t = rcu_dereference(start); t; t = rcu_dereference(t->next)) |
|---|
| 129 | + |
|---|
| 127 | 130 | /** |
|---|
| 128 | 131 | * ip6_tnl_lookup - fetch tunnel matching the end-point addresses |
|---|
| 132 | + * @net: network namespace |
|---|
| 133 | + * @link: ifindex of underlying interface |
|---|
| 129 | 134 | * @remote: the address of the tunnel exit-point |
|---|
| 130 | 135 | * @local: the address of the tunnel entry-point |
|---|
| 131 | 136 | * |
|---|
| .. | .. |
|---|
| 135 | 140 | * else %NULL |
|---|
| 136 | 141 | **/ |
|---|
| 137 | 142 | |
|---|
| 138 | | -#define for_each_ip6_tunnel_rcu(start) \ |
|---|
| 139 | | - for (t = rcu_dereference(start); t; t = rcu_dereference(t->next)) |
|---|
| 140 | | - |
|---|
| 141 | 143 | static struct ip6_tnl * |
|---|
| 142 | | -ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local) |
|---|
| 144 | +ip6_tnl_lookup(struct net *net, int link, |
|---|
| 145 | + const struct in6_addr *remote, const struct in6_addr *local) |
|---|
| 143 | 146 | { |
|---|
| 144 | 147 | unsigned int hash = HASH(remote, local); |
|---|
| 145 | | - struct ip6_tnl *t; |
|---|
| 148 | + struct ip6_tnl *t, *cand = NULL; |
|---|
| 146 | 149 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
|---|
| 147 | 150 | struct in6_addr any; |
|---|
| 148 | 151 | |
|---|
| 149 | 152 | for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) { |
|---|
| 150 | | - if (ipv6_addr_equal(local, &t->parms.laddr) && |
|---|
| 151 | | - ipv6_addr_equal(remote, &t->parms.raddr) && |
|---|
| 152 | | - (t->dev->flags & IFF_UP)) |
|---|
| 153 | + if (!ipv6_addr_equal(local, &t->parms.laddr) || |
|---|
| 154 | + !ipv6_addr_equal(remote, &t->parms.raddr) || |
|---|
| 155 | + !(t->dev->flags & IFF_UP)) |
|---|
| 156 | + continue; |
|---|
| 157 | + |
|---|
| 158 | + if (link == t->parms.link) |
|---|
| 153 | 159 | return t; |
|---|
| 160 | + else |
|---|
| 161 | + cand = t; |
|---|
| 154 | 162 | } |
|---|
| 155 | 163 | |
|---|
| 156 | 164 | memset(&any, 0, sizeof(any)); |
|---|
| 157 | 165 | hash = HASH(&any, local); |
|---|
| 158 | 166 | for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) { |
|---|
| 159 | | - if (ipv6_addr_equal(local, &t->parms.laddr) && |
|---|
| 160 | | - ipv6_addr_any(&t->parms.raddr) && |
|---|
| 161 | | - (t->dev->flags & IFF_UP)) |
|---|
| 167 | + if (!ipv6_addr_equal(local, &t->parms.laddr) || |
|---|
| 168 | + !ipv6_addr_any(&t->parms.raddr) || |
|---|
| 169 | + !(t->dev->flags & IFF_UP)) |
|---|
| 170 | + continue; |
|---|
| 171 | + |
|---|
| 172 | + if (link == t->parms.link) |
|---|
| 162 | 173 | return t; |
|---|
| 174 | + else if (!cand) |
|---|
| 175 | + cand = t; |
|---|
| 163 | 176 | } |
|---|
| 164 | 177 | |
|---|
| 165 | 178 | hash = HASH(remote, &any); |
|---|
| 166 | 179 | for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) { |
|---|
| 167 | | - if (ipv6_addr_equal(remote, &t->parms.raddr) && |
|---|
| 168 | | - ipv6_addr_any(&t->parms.laddr) && |
|---|
| 169 | | - (t->dev->flags & IFF_UP)) |
|---|
| 180 | + if (!ipv6_addr_equal(remote, &t->parms.raddr) || |
|---|
| 181 | + !ipv6_addr_any(&t->parms.laddr) || |
|---|
| 182 | + !(t->dev->flags & IFF_UP)) |
|---|
| 183 | + continue; |
|---|
| 184 | + |
|---|
| 185 | + if (link == t->parms.link) |
|---|
| 170 | 186 | return t; |
|---|
| 187 | + else if (!cand) |
|---|
| 188 | + cand = t; |
|---|
| 171 | 189 | } |
|---|
| 190 | + |
|---|
| 191 | + if (cand) |
|---|
| 192 | + return cand; |
|---|
| 172 | 193 | |
|---|
| 173 | 194 | t = rcu_dereference(ip6n->collect_md_tun); |
|---|
| 174 | 195 | if (t && t->dev->flags & IFF_UP) |
|---|
| .. | .. |
|---|
| 281 | 302 | |
|---|
| 282 | 303 | /** |
|---|
| 283 | 304 | * ip6_tnl_create - create a new tunnel |
|---|
| 305 | + * @net: network namespace |
|---|
| 284 | 306 | * @p: tunnel parameters |
|---|
| 285 | | - * @pt: pointer to new tunnel |
|---|
| 286 | 307 | * |
|---|
| 287 | 308 | * Description: |
|---|
| 288 | 309 | * Create tunnel matching given parameters. |
|---|
| .. | .. |
|---|
| 330 | 351 | |
|---|
| 331 | 352 | /** |
|---|
| 332 | 353 | * ip6_tnl_locate - find or create tunnel matching given parameters |
|---|
| 354 | + * @net: network namespace |
|---|
| 333 | 355 | * @p: tunnel parameters |
|---|
| 334 | 356 | * @create: != 0 if allowed to create new tunnel if no match found |
|---|
| 335 | 357 | * |
|---|
| .. | .. |
|---|
| 355 | 377 | (t = rtnl_dereference(*tp)) != NULL; |
|---|
| 356 | 378 | tp = &t->next) { |
|---|
| 357 | 379 | if (ipv6_addr_equal(local, &t->parms.laddr) && |
|---|
| 358 | | - ipv6_addr_equal(remote, &t->parms.raddr)) { |
|---|
| 380 | + ipv6_addr_equal(remote, &t->parms.raddr) && |
|---|
| 381 | + p->link == t->parms.link) { |
|---|
| 359 | 382 | if (create) |
|---|
| 360 | 383 | return ERR_PTR(-EEXIST); |
|---|
| 361 | 384 | |
|---|
| .. | .. |
|---|
| 420 | 443 | break; |
|---|
| 421 | 444 | optlen = 8; |
|---|
| 422 | 445 | } else if (nexthdr == NEXTHDR_AUTH) { |
|---|
| 423 | | - optlen = (hdr->hdrlen + 2) << 2; |
|---|
| 446 | + optlen = ipv6_authlen(hdr); |
|---|
| 424 | 447 | } else { |
|---|
| 425 | 448 | optlen = ipv6_optlen(hdr); |
|---|
| 426 | 449 | } |
|---|
| .. | .. |
|---|
| 489 | 512 | processing of the error. */ |
|---|
| 490 | 513 | |
|---|
| 491 | 514 | rcu_read_lock(); |
|---|
| 492 | | - t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr); |
|---|
| 515 | + t = ip6_tnl_lookup(dev_net(skb->dev), skb->dev->ifindex, &ipv6h->daddr, &ipv6h->saddr); |
|---|
| 493 | 516 | if (!t) |
|---|
| 494 | 517 | goto out; |
|---|
| 495 | 518 | |
|---|
| .. | .. |
|---|
| 500 | 523 | err = 0; |
|---|
| 501 | 524 | |
|---|
| 502 | 525 | switch (*type) { |
|---|
| 503 | | - struct ipv6_tlv_tnl_enc_lim *tel; |
|---|
| 504 | | - __u32 mtu, teli; |
|---|
| 505 | 526 | case ICMPV6_DEST_UNREACH: |
|---|
| 506 | 527 | net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n", |
|---|
| 507 | 528 | t->parms.name); |
|---|
| .. | .. |
|---|
| 514 | 535 | rel_msg = 1; |
|---|
| 515 | 536 | } |
|---|
| 516 | 537 | break; |
|---|
| 517 | | - case ICMPV6_PARAMPROB: |
|---|
| 538 | + case ICMPV6_PARAMPROB: { |
|---|
| 539 | + struct ipv6_tlv_tnl_enc_lim *tel; |
|---|
| 540 | + __u32 teli; |
|---|
| 541 | + |
|---|
| 518 | 542 | teli = 0; |
|---|
| 519 | 543 | if ((*code) == ICMPV6_HDR_FIELD) |
|---|
| 520 | 544 | teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data); |
|---|
| .. | .. |
|---|
| 531 | 555 | t->parms.name); |
|---|
| 532 | 556 | } |
|---|
| 533 | 557 | break; |
|---|
| 534 | | - case ICMPV6_PKT_TOOBIG: |
|---|
| 558 | + } |
|---|
| 559 | + case ICMPV6_PKT_TOOBIG: { |
|---|
| 560 | + __u32 mtu; |
|---|
| 561 | + |
|---|
| 535 | 562 | ip6_update_pmtu(skb, net, htonl(*info), 0, 0, |
|---|
| 536 | 563 | sock_net_uid(net, NULL)); |
|---|
| 537 | 564 | mtu = *info - offset; |
|---|
| .. | .. |
|---|
| 545 | 572 | rel_msg = 1; |
|---|
| 546 | 573 | } |
|---|
| 547 | 574 | break; |
|---|
| 575 | + } |
|---|
| 548 | 576 | case NDISC_REDIRECT: |
|---|
| 549 | 577 | ip6_redirect(skb, net, skb->dev->ifindex, 0, |
|---|
| 550 | 578 | sock_net_uid(net, NULL)); |
|---|
| .. | .. |
|---|
| 696 | 724 | return 0; |
|---|
| 697 | 725 | } |
|---|
| 698 | 726 | |
|---|
| 727 | +static int |
|---|
| 728 | +mplsip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
|---|
| 729 | + u8 type, u8 code, int offset, __be32 info) |
|---|
| 730 | +{ |
|---|
| 731 | + __u32 rel_info = ntohl(info); |
|---|
| 732 | + int err, rel_msg = 0; |
|---|
| 733 | + u8 rel_type = type; |
|---|
| 734 | + u8 rel_code = code; |
|---|
| 735 | + |
|---|
| 736 | + err = ip6_tnl_err(skb, IPPROTO_MPLS, opt, &rel_type, &rel_code, |
|---|
| 737 | + &rel_msg, &rel_info, offset); |
|---|
| 738 | + return err; |
|---|
| 739 | +} |
|---|
| 740 | + |
|---|
| 699 | 741 | static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t, |
|---|
| 700 | 742 | const struct ipv6hdr *ipv6h, |
|---|
| 701 | 743 | struct sk_buff *skb) |
|---|
| .. | .. |
|---|
| 716 | 758 | ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb)); |
|---|
| 717 | 759 | |
|---|
| 718 | 760 | return IP6_ECN_decapsulate(ipv6h, skb); |
|---|
| 761 | +} |
|---|
| 762 | + |
|---|
| 763 | +static inline int mplsip6_dscp_ecn_decapsulate(const struct ip6_tnl *t, |
|---|
| 764 | + const struct ipv6hdr *ipv6h, |
|---|
| 765 | + struct sk_buff *skb) |
|---|
| 766 | +{ |
|---|
| 767 | + /* ECN is not supported in AF_MPLS */ |
|---|
| 768 | + return 0; |
|---|
| 719 | 769 | } |
|---|
| 720 | 770 | |
|---|
| 721 | 771 | __u32 ip6_tnl_get_cap(struct ip6_tnl *t, |
|---|
| .. | .. |
|---|
| 887 | 937 | .proto = htons(ETH_P_IP), |
|---|
| 888 | 938 | }; |
|---|
| 889 | 939 | |
|---|
| 940 | +static const struct tnl_ptk_info tpi_mpls = { |
|---|
| 941 | + /* no tunnel info required for mplsip6. */ |
|---|
| 942 | + .proto = htons(ETH_P_MPLS_UC), |
|---|
| 943 | +}; |
|---|
| 944 | + |
|---|
| 890 | 945 | static int ipxip6_rcv(struct sk_buff *skb, u8 ipproto, |
|---|
| 891 | 946 | const struct tnl_ptk_info *tpi, |
|---|
| 892 | 947 | int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t, |
|---|
| .. | .. |
|---|
| 899 | 954 | int ret = -1; |
|---|
| 900 | 955 | |
|---|
| 901 | 956 | rcu_read_lock(); |
|---|
| 902 | | - t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr); |
|---|
| 957 | + t = ip6_tnl_lookup(dev_net(skb->dev), skb->dev->ifindex, &ipv6h->saddr, &ipv6h->daddr); |
|---|
| 903 | 958 | |
|---|
| 904 | 959 | if (t) { |
|---|
| 905 | 960 | u8 tproto = READ_ONCE(t->parms.proto); |
|---|
| .. | .. |
|---|
| 942 | 997 | { |
|---|
| 943 | 998 | return ipxip6_rcv(skb, IPPROTO_IPV6, &tpi_v6, |
|---|
| 944 | 999 | ip6ip6_dscp_ecn_decapsulate); |
|---|
| 1000 | +} |
|---|
| 1001 | + |
|---|
| 1002 | +static int mplsip6_rcv(struct sk_buff *skb) |
|---|
| 1003 | +{ |
|---|
| 1004 | + return ipxip6_rcv(skb, IPPROTO_MPLS, &tpi_mpls, |
|---|
| 1005 | + mplsip6_dscp_ecn_decapsulate); |
|---|
| 945 | 1006 | } |
|---|
| 946 | 1007 | |
|---|
| 947 | 1008 | struct ipv6_tel_txoption { |
|---|
| .. | .. |
|---|
| 1206 | 1267 | */ |
|---|
| 1207 | 1268 | max_headroom = LL_RESERVED_SPACE(dst->dev) + sizeof(struct ipv6hdr) |
|---|
| 1208 | 1269 | + dst->header_len + t->hlen; |
|---|
| 1209 | | - if (max_headroom > dev->needed_headroom) |
|---|
| 1210 | | - dev->needed_headroom = max_headroom; |
|---|
| 1270 | + if (max_headroom > READ_ONCE(dev->needed_headroom)) |
|---|
| 1271 | + WRITE_ONCE(dev->needed_headroom, max_headroom); |
|---|
| 1211 | 1272 | |
|---|
| 1212 | 1273 | err = ip6_tnl_encap(skb, t, &proto, fl6); |
|---|
| 1213 | 1274 | if (err) |
|---|
| .. | .. |
|---|
| 1239 | 1300 | EXPORT_SYMBOL(ip6_tnl_xmit); |
|---|
| 1240 | 1301 | |
|---|
| 1241 | 1302 | static inline int |
|---|
| 1242 | | -ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) |
|---|
| 1303 | +ipxip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, |
|---|
| 1304 | + u8 protocol) |
|---|
| 1243 | 1305 | { |
|---|
| 1244 | 1306 | struct ip6_tnl *t = netdev_priv(dev); |
|---|
| 1307 | + struct ipv6hdr *ipv6h; |
|---|
| 1245 | 1308 | const struct iphdr *iph; |
|---|
| 1246 | 1309 | int encap_limit = -1; |
|---|
| 1310 | + __u16 offset; |
|---|
| 1247 | 1311 | struct flowi6 fl6; |
|---|
| 1248 | | - __u8 dsfield; |
|---|
| 1312 | + __u8 dsfield, orig_dsfield; |
|---|
| 1249 | 1313 | __u32 mtu; |
|---|
| 1250 | 1314 | u8 tproto; |
|---|
| 1251 | 1315 | int err; |
|---|
| 1252 | 1316 | |
|---|
| 1253 | | - iph = ip_hdr(skb); |
|---|
| 1254 | | - memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
|---|
| 1255 | | - |
|---|
| 1256 | 1317 | tproto = READ_ONCE(t->parms.proto); |
|---|
| 1257 | | - if (tproto != IPPROTO_IPIP && tproto != 0) |
|---|
| 1318 | + if (tproto != protocol && tproto != 0) |
|---|
| 1258 | 1319 | return -1; |
|---|
| 1259 | 1320 | |
|---|
| 1260 | 1321 | if (t->parms.collect_md) { |
|---|
| .. | .. |
|---|
| 1267 | 1328 | return -1; |
|---|
| 1268 | 1329 | key = &tun_info->key; |
|---|
| 1269 | 1330 | memset(&fl6, 0, sizeof(fl6)); |
|---|
| 1270 | | - fl6.flowi6_proto = IPPROTO_IPIP; |
|---|
| 1331 | + fl6.flowi6_proto = protocol; |
|---|
| 1271 | 1332 | fl6.saddr = key->u.ipv6.src; |
|---|
| 1272 | 1333 | fl6.daddr = key->u.ipv6.dst; |
|---|
| 1273 | 1334 | fl6.flowlabel = key->label; |
|---|
| 1274 | 1335 | dsfield = key->tos; |
|---|
| 1336 | + switch (protocol) { |
|---|
| 1337 | + case IPPROTO_IPIP: |
|---|
| 1338 | + iph = ip_hdr(skb); |
|---|
| 1339 | + orig_dsfield = ipv4_get_dsfield(iph); |
|---|
| 1340 | + break; |
|---|
| 1341 | + case IPPROTO_IPV6: |
|---|
| 1342 | + ipv6h = ipv6_hdr(skb); |
|---|
| 1343 | + orig_dsfield = ipv6_get_dsfield(ipv6h); |
|---|
| 1344 | + break; |
|---|
| 1345 | + default: |
|---|
| 1346 | + orig_dsfield = dsfield; |
|---|
| 1347 | + break; |
|---|
| 1348 | + } |
|---|
| 1275 | 1349 | } else { |
|---|
| 1276 | 1350 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
|---|
| 1277 | 1351 | encap_limit = t->parms.encap_limit; |
|---|
| 1352 | + if (protocol == IPPROTO_IPV6) { |
|---|
| 1353 | + offset = ip6_tnl_parse_tlv_enc_lim(skb, |
|---|
| 1354 | + skb_network_header(skb)); |
|---|
| 1355 | + /* ip6_tnl_parse_tlv_enc_lim() might have |
|---|
| 1356 | + * reallocated skb->head |
|---|
| 1357 | + */ |
|---|
| 1358 | + if (offset > 0) { |
|---|
| 1359 | + struct ipv6_tlv_tnl_enc_lim *tel; |
|---|
| 1278 | 1360 | |
|---|
| 1279 | | - memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); |
|---|
| 1280 | | - fl6.flowi6_proto = IPPROTO_IPIP; |
|---|
| 1281 | | - |
|---|
| 1282 | | - if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
|---|
| 1283 | | - dsfield = ipv4_get_dsfield(iph); |
|---|
| 1284 | | - else |
|---|
| 1285 | | - dsfield = ip6_tclass(t->parms.flowinfo); |
|---|
| 1286 | | - if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
|---|
| 1287 | | - fl6.flowi6_mark = skb->mark; |
|---|
| 1288 | | - else |
|---|
| 1289 | | - fl6.flowi6_mark = t->parms.fwmark; |
|---|
| 1290 | | - } |
|---|
| 1291 | | - |
|---|
| 1292 | | - fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL); |
|---|
| 1293 | | - dsfield = INET_ECN_encapsulate(dsfield, ipv4_get_dsfield(iph)); |
|---|
| 1294 | | - |
|---|
| 1295 | | - if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6)) |
|---|
| 1296 | | - return -1; |
|---|
| 1297 | | - |
|---|
| 1298 | | - skb_set_inner_ipproto(skb, IPPROTO_IPIP); |
|---|
| 1299 | | - |
|---|
| 1300 | | - err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, |
|---|
| 1301 | | - IPPROTO_IPIP); |
|---|
| 1302 | | - if (err != 0) { |
|---|
| 1303 | | - /* XXX: send ICMP error even if DF is not set. */ |
|---|
| 1304 | | - if (err == -EMSGSIZE) |
|---|
| 1305 | | - icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, |
|---|
| 1306 | | - htonl(mtu)); |
|---|
| 1307 | | - return -1; |
|---|
| 1308 | | - } |
|---|
| 1309 | | - |
|---|
| 1310 | | - return 0; |
|---|
| 1311 | | -} |
|---|
| 1312 | | - |
|---|
| 1313 | | -static inline int |
|---|
| 1314 | | -ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) |
|---|
| 1315 | | -{ |
|---|
| 1316 | | - struct ip6_tnl *t = netdev_priv(dev); |
|---|
| 1317 | | - struct ipv6hdr *ipv6h; |
|---|
| 1318 | | - int encap_limit = -1; |
|---|
| 1319 | | - __u16 offset; |
|---|
| 1320 | | - struct flowi6 fl6; |
|---|
| 1321 | | - __u8 dsfield; |
|---|
| 1322 | | - __u32 mtu; |
|---|
| 1323 | | - u8 tproto; |
|---|
| 1324 | | - int err; |
|---|
| 1325 | | - |
|---|
| 1326 | | - ipv6h = ipv6_hdr(skb); |
|---|
| 1327 | | - tproto = READ_ONCE(t->parms.proto); |
|---|
| 1328 | | - if ((tproto != IPPROTO_IPV6 && tproto != 0) || |
|---|
| 1329 | | - ip6_tnl_addr_conflict(t, ipv6h)) |
|---|
| 1330 | | - return -1; |
|---|
| 1331 | | - |
|---|
| 1332 | | - if (t->parms.collect_md) { |
|---|
| 1333 | | - struct ip_tunnel_info *tun_info; |
|---|
| 1334 | | - const struct ip_tunnel_key *key; |
|---|
| 1335 | | - |
|---|
| 1336 | | - tun_info = skb_tunnel_info(skb); |
|---|
| 1337 | | - if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) || |
|---|
| 1338 | | - ip_tunnel_info_af(tun_info) != AF_INET6)) |
|---|
| 1339 | | - return -1; |
|---|
| 1340 | | - key = &tun_info->key; |
|---|
| 1341 | | - memset(&fl6, 0, sizeof(fl6)); |
|---|
| 1342 | | - fl6.flowi6_proto = IPPROTO_IPV6; |
|---|
| 1343 | | - fl6.saddr = key->u.ipv6.src; |
|---|
| 1344 | | - fl6.daddr = key->u.ipv6.dst; |
|---|
| 1345 | | - fl6.flowlabel = key->label; |
|---|
| 1346 | | - dsfield = key->tos; |
|---|
| 1347 | | - } else { |
|---|
| 1348 | | - offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb)); |
|---|
| 1349 | | - /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */ |
|---|
| 1350 | | - ipv6h = ipv6_hdr(skb); |
|---|
| 1351 | | - if (offset > 0) { |
|---|
| 1352 | | - struct ipv6_tlv_tnl_enc_lim *tel; |
|---|
| 1353 | | - |
|---|
| 1354 | | - tel = (void *)&skb_network_header(skb)[offset]; |
|---|
| 1355 | | - if (tel->encap_limit == 0) { |
|---|
| 1356 | | - icmpv6_send(skb, ICMPV6_PARAMPROB, |
|---|
| 1357 | | - ICMPV6_HDR_FIELD, offset + 2); |
|---|
| 1358 | | - return -1; |
|---|
| 1361 | + tel = (void *)&skb_network_header(skb)[offset]; |
|---|
| 1362 | + if (tel->encap_limit == 0) { |
|---|
| 1363 | + icmpv6_ndo_send(skb, ICMPV6_PARAMPROB, |
|---|
| 1364 | + ICMPV6_HDR_FIELD, offset + 2); |
|---|
| 1365 | + return -1; |
|---|
| 1366 | + } |
|---|
| 1367 | + encap_limit = tel->encap_limit - 1; |
|---|
| 1359 | 1368 | } |
|---|
| 1360 | | - encap_limit = tel->encap_limit - 1; |
|---|
| 1361 | | - } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) { |
|---|
| 1362 | | - encap_limit = t->parms.encap_limit; |
|---|
| 1363 | 1369 | } |
|---|
| 1364 | 1370 | |
|---|
| 1365 | 1371 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); |
|---|
| 1366 | | - fl6.flowi6_proto = IPPROTO_IPV6; |
|---|
| 1372 | + fl6.flowi6_proto = protocol; |
|---|
| 1367 | 1373 | |
|---|
| 1368 | | - if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
|---|
| 1369 | | - dsfield = ipv6_get_dsfield(ipv6h); |
|---|
| 1370 | | - else |
|---|
| 1371 | | - dsfield = ip6_tclass(t->parms.flowinfo); |
|---|
| 1372 | | - if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) |
|---|
| 1373 | | - fl6.flowlabel |= ip6_flowlabel(ipv6h); |
|---|
| 1374 | 1374 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
|---|
| 1375 | 1375 | fl6.flowi6_mark = skb->mark; |
|---|
| 1376 | 1376 | else |
|---|
| 1377 | 1377 | fl6.flowi6_mark = t->parms.fwmark; |
|---|
| 1378 | + switch (protocol) { |
|---|
| 1379 | + case IPPROTO_IPIP: |
|---|
| 1380 | + iph = ip_hdr(skb); |
|---|
| 1381 | + orig_dsfield = ipv4_get_dsfield(iph); |
|---|
| 1382 | + if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
|---|
| 1383 | + dsfield = orig_dsfield; |
|---|
| 1384 | + else |
|---|
| 1385 | + dsfield = ip6_tclass(t->parms.flowinfo); |
|---|
| 1386 | + break; |
|---|
| 1387 | + case IPPROTO_IPV6: |
|---|
| 1388 | + ipv6h = ipv6_hdr(skb); |
|---|
| 1389 | + orig_dsfield = ipv6_get_dsfield(ipv6h); |
|---|
| 1390 | + if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
|---|
| 1391 | + dsfield = orig_dsfield; |
|---|
| 1392 | + else |
|---|
| 1393 | + dsfield = ip6_tclass(t->parms.flowinfo); |
|---|
| 1394 | + if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) |
|---|
| 1395 | + fl6.flowlabel |= ip6_flowlabel(ipv6h); |
|---|
| 1396 | + break; |
|---|
| 1397 | + default: |
|---|
| 1398 | + orig_dsfield = dsfield = ip6_tclass(t->parms.flowinfo); |
|---|
| 1399 | + break; |
|---|
| 1400 | + } |
|---|
| 1378 | 1401 | } |
|---|
| 1379 | 1402 | |
|---|
| 1380 | 1403 | fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL); |
|---|
| 1381 | | - dsfield = INET_ECN_encapsulate(dsfield, ipv6_get_dsfield(ipv6h)); |
|---|
| 1404 | + dsfield = INET_ECN_encapsulate(dsfield, orig_dsfield); |
|---|
| 1382 | 1405 | |
|---|
| 1383 | 1406 | if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6)) |
|---|
| 1384 | 1407 | return -1; |
|---|
| 1385 | 1408 | |
|---|
| 1386 | | - skb_set_inner_ipproto(skb, IPPROTO_IPV6); |
|---|
| 1409 | + skb_set_inner_ipproto(skb, protocol); |
|---|
| 1387 | 1410 | |
|---|
| 1388 | 1411 | err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, |
|---|
| 1389 | | - IPPROTO_IPV6); |
|---|
| 1412 | + protocol); |
|---|
| 1390 | 1413 | if (err != 0) { |
|---|
| 1414 | + /* XXX: send ICMP error even if DF is not set. */ |
|---|
| 1391 | 1415 | if (err == -EMSGSIZE) |
|---|
| 1392 | | - icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
|---|
| 1416 | + switch (protocol) { |
|---|
| 1417 | + case IPPROTO_IPIP: |
|---|
| 1418 | + icmp_ndo_send(skb, ICMP_DEST_UNREACH, |
|---|
| 1419 | + ICMP_FRAG_NEEDED, htonl(mtu)); |
|---|
| 1420 | + break; |
|---|
| 1421 | + case IPPROTO_IPV6: |
|---|
| 1422 | + icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
|---|
| 1423 | + break; |
|---|
| 1424 | + default: |
|---|
| 1425 | + break; |
|---|
| 1426 | + } |
|---|
| 1393 | 1427 | return -1; |
|---|
| 1394 | 1428 | } |
|---|
| 1395 | 1429 | |
|---|
| .. | .. |
|---|
| 1401 | 1435 | { |
|---|
| 1402 | 1436 | struct ip6_tnl *t = netdev_priv(dev); |
|---|
| 1403 | 1437 | struct net_device_stats *stats = &t->dev->stats; |
|---|
| 1438 | + u8 ipproto; |
|---|
| 1404 | 1439 | int ret; |
|---|
| 1405 | 1440 | |
|---|
| 1406 | 1441 | if (!pskb_inet_may_pull(skb)) |
|---|
| .. | .. |
|---|
| 1408 | 1443 | |
|---|
| 1409 | 1444 | switch (skb->protocol) { |
|---|
| 1410 | 1445 | case htons(ETH_P_IP): |
|---|
| 1411 | | - ret = ip4ip6_tnl_xmit(skb, dev); |
|---|
| 1446 | + ipproto = IPPROTO_IPIP; |
|---|
| 1412 | 1447 | break; |
|---|
| 1413 | 1448 | case htons(ETH_P_IPV6): |
|---|
| 1414 | | - ret = ip6ip6_tnl_xmit(skb, dev); |
|---|
| 1449 | + if (ip6_tnl_addr_conflict(t, ipv6_hdr(skb))) |
|---|
| 1450 | + goto tx_err; |
|---|
| 1451 | + ipproto = IPPROTO_IPV6; |
|---|
| 1452 | + break; |
|---|
| 1453 | + case htons(ETH_P_MPLS_UC): |
|---|
| 1454 | + ipproto = IPPROTO_MPLS; |
|---|
| 1415 | 1455 | break; |
|---|
| 1416 | 1456 | default: |
|---|
| 1417 | 1457 | goto tx_err; |
|---|
| 1418 | 1458 | } |
|---|
| 1419 | 1459 | |
|---|
| 1460 | + ret = ipxip6_tnl_xmit(skb, dev, ipproto); |
|---|
| 1420 | 1461 | if (ret < 0) |
|---|
| 1421 | 1462 | goto tx_err; |
|---|
| 1422 | 1463 | |
|---|
| .. | .. |
|---|
| 1432 | 1473 | static void ip6_tnl_link_config(struct ip6_tnl *t) |
|---|
| 1433 | 1474 | { |
|---|
| 1434 | 1475 | struct net_device *dev = t->dev; |
|---|
| 1476 | + struct net_device *tdev = NULL; |
|---|
| 1435 | 1477 | struct __ip6_tnl_parm *p = &t->parms; |
|---|
| 1436 | 1478 | struct flowi6 *fl6 = &t->fl.u.ip6; |
|---|
| 1437 | 1479 | int t_hlen; |
|---|
| 1480 | + int mtu; |
|---|
| 1438 | 1481 | |
|---|
| 1439 | 1482 | memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr)); |
|---|
| 1440 | 1483 | memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr)); |
|---|
| .. | .. |
|---|
| 1469 | 1512 | struct rt6_info *rt = rt6_lookup(t->net, |
|---|
| 1470 | 1513 | &p->raddr, &p->laddr, |
|---|
| 1471 | 1514 | p->link, NULL, strict); |
|---|
| 1472 | | - |
|---|
| 1473 | | - if (!rt) |
|---|
| 1474 | | - return; |
|---|
| 1475 | | - |
|---|
| 1476 | | - if (rt->dst.dev) { |
|---|
| 1477 | | - dev->hard_header_len = rt->dst.dev->hard_header_len + |
|---|
| 1478 | | - t_hlen; |
|---|
| 1479 | | - |
|---|
| 1480 | | - dev->mtu = rt->dst.dev->mtu - t_hlen; |
|---|
| 1481 | | - if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
|---|
| 1482 | | - dev->mtu -= 8; |
|---|
| 1483 | | - |
|---|
| 1484 | | - if (dev->mtu < IPV6_MIN_MTU) |
|---|
| 1485 | | - dev->mtu = IPV6_MIN_MTU; |
|---|
| 1515 | + if (rt) { |
|---|
| 1516 | + tdev = rt->dst.dev; |
|---|
| 1517 | + ip6_rt_put(rt); |
|---|
| 1486 | 1518 | } |
|---|
| 1487 | | - ip6_rt_put(rt); |
|---|
| 1519 | + |
|---|
| 1520 | + if (!tdev && p->link) |
|---|
| 1521 | + tdev = __dev_get_by_index(t->net, p->link); |
|---|
| 1522 | + |
|---|
| 1523 | + if (tdev) { |
|---|
| 1524 | + dev->hard_header_len = tdev->hard_header_len + t_hlen; |
|---|
| 1525 | + mtu = min_t(unsigned int, tdev->mtu, IP6_MAX_MTU); |
|---|
| 1526 | + |
|---|
| 1527 | + mtu = mtu - t_hlen; |
|---|
| 1528 | + if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
|---|
| 1529 | + mtu -= 8; |
|---|
| 1530 | + |
|---|
| 1531 | + if (mtu < IPV6_MIN_MTU) |
|---|
| 1532 | + mtu = IPV6_MIN_MTU; |
|---|
| 1533 | + WRITE_ONCE(dev->mtu, mtu); |
|---|
| 1534 | + } |
|---|
| 1488 | 1535 | } |
|---|
| 1489 | 1536 | } |
|---|
| 1490 | 1537 | |
|---|
| .. | .. |
|---|
| 1809 | 1856 | static void ip6_tnl_dev_setup(struct net_device *dev) |
|---|
| 1810 | 1857 | { |
|---|
| 1811 | 1858 | dev->netdev_ops = &ip6_tnl_netdev_ops; |
|---|
| 1859 | + dev->header_ops = &ip_tunnel_header_ops; |
|---|
| 1812 | 1860 | dev->needs_free_netdev = true; |
|---|
| 1813 | 1861 | dev->priv_destructor = ip6_dev_free; |
|---|
| 1814 | 1862 | |
|---|
| .. | .. |
|---|
| 2199 | 2247 | .priority = 1, |
|---|
| 2200 | 2248 | }; |
|---|
| 2201 | 2249 | |
|---|
| 2250 | +static struct xfrm6_tunnel mplsip6_handler __read_mostly = { |
|---|
| 2251 | + .handler = mplsip6_rcv, |
|---|
| 2252 | + .err_handler = mplsip6_err, |
|---|
| 2253 | + .priority = 1, |
|---|
| 2254 | +}; |
|---|
| 2255 | + |
|---|
| 2202 | 2256 | static void __net_exit ip6_tnl_destroy_tunnels(struct net *net, struct list_head *list) |
|---|
| 2203 | 2257 | { |
|---|
| 2204 | 2258 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
|---|
| .. | .. |
|---|
| 2323 | 2377 | pr_err("%s: can't register ip6ip6\n", __func__); |
|---|
| 2324 | 2378 | goto out_ip6ip6; |
|---|
| 2325 | 2379 | } |
|---|
| 2380 | + |
|---|
| 2381 | + if (ip6_tnl_mpls_supported()) { |
|---|
| 2382 | + err = xfrm6_tunnel_register(&mplsip6_handler, AF_MPLS); |
|---|
| 2383 | + if (err < 0) { |
|---|
| 2384 | + pr_err("%s: can't register mplsip6\n", __func__); |
|---|
| 2385 | + goto out_mplsip6; |
|---|
| 2386 | + } |
|---|
| 2387 | + } |
|---|
| 2388 | + |
|---|
| 2326 | 2389 | err = rtnl_link_register(&ip6_link_ops); |
|---|
| 2327 | 2390 | if (err < 0) |
|---|
| 2328 | 2391 | goto rtnl_link_failed; |
|---|
| .. | .. |
|---|
| 2330 | 2393 | return 0; |
|---|
| 2331 | 2394 | |
|---|
| 2332 | 2395 | rtnl_link_failed: |
|---|
| 2396 | + if (ip6_tnl_mpls_supported()) |
|---|
| 2397 | + xfrm6_tunnel_deregister(&mplsip6_handler, AF_MPLS); |
|---|
| 2398 | +out_mplsip6: |
|---|
| 2333 | 2399 | xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6); |
|---|
| 2334 | 2400 | out_ip6ip6: |
|---|
| 2335 | 2401 | xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET); |
|---|
| .. | .. |
|---|
| 2352 | 2418 | if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6)) |
|---|
| 2353 | 2419 | pr_info("%s: can't deregister ip6ip6\n", __func__); |
|---|
| 2354 | 2420 | |
|---|
| 2421 | + if (ip6_tnl_mpls_supported() && |
|---|
| 2422 | + xfrm6_tunnel_deregister(&mplsip6_handler, AF_MPLS)) |
|---|
| 2423 | + pr_info("%s: can't deregister mplsip6\n", __func__); |
|---|
| 2355 | 2424 | unregister_pernet_device(&ip6_tnl_net_ops); |
|---|
| 2356 | 2425 | } |
|---|
| 2357 | 2426 | |
|---|