1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __NET_IP_TUNNELS_H 3 #define __NET_IP_TUNNELS_H 1 4 5 #include <linux/if_tunnel.h> 6 #include <linux/netdevice.h> 7 #include <linux/skbuff.h> 8 #include <linux/socket.h> 9 #include <linux/types.h> 10 #include <linux/u64_stats_sync.h> 11 #include <linux/bitops.h> 12 13 #include <net/dsfield.h> 14 #include <net/flow.h> 15 #include <net/gro_cells.h> 16 #include <net/inet_dscp.h> 17 #include <net/inet_ecn.h> 18 #include <net/netns/generic.h> 19 #include <net/rtnetlink.h> 20 #include <net/lwtunnel.h> 21 #include <net/dst_cache.h> 22 #include <net/netdev_lock.h> 23 24 #if IS_ENABLED(CONFIG_IPV6) 25 #include <net/ipv6.h> 26 #include <net/ip6_fib.h> 27 #include <net/ip6_route.h> 28 #endif 29 30 /* Recursion limit for tunnel xmit to detect routing loops. 31 * Unlike XMIT_RECURSION_LIMIT (8) used in the no-qdisc path, tunnel 32 * recursion involves route lookups and full IP output, consuming much 33 * more stack per level, so a lower limit is needed. 34 */ 35 #define IP_TUNNEL_RECURSION_LIMIT 5 36 37 /* Keep error state on tunnel for 30 sec */ 38 #define IPTUNNEL_ERR_TIMEO (30*HZ) 39 40 /* Used to memset ip_tunnel padding. */ 41 #define IP_TUNNEL_KEY_SIZE offsetofend(struct ip_tunnel_key, tp_dst) 42 43 /* Used to memset ipv4 address padding. */ 44 #define IP_TUNNEL_KEY_IPV4_PAD offsetofend(struct ip_tunnel_key, u.ipv4.dst) 45 #define IP_TUNNEL_KEY_IPV4_PAD_LEN \ 46 (sizeof_field(struct ip_tunnel_key, u) - \ 47 sizeof_field(struct ip_tunnel_key, u.ipv4)) 48 49 #define __ipt_flag_op(op, ...) \ 50 op(__VA_ARGS__, __IP_TUNNEL_FLAG_NUM) 51 52 #define IP_TUNNEL_DECLARE_FLAGS(...) \ 53 __ipt_flag_op(DECLARE_BITMAP, __VA_ARGS__) 54 55 #define ip_tunnel_flags_zero(...) __ipt_flag_op(bitmap_zero, __VA_ARGS__) 56 #define ip_tunnel_flags_copy(...) __ipt_flag_op(bitmap_copy, __VA_ARGS__) 57 #define ip_tunnel_flags_and(...) __ipt_flag_op(bitmap_and, __VA_ARGS__) 58 #define ip_tunnel_flags_or(...) __ipt_flag_op(bitmap_or, __VA_ARGS__) 59 60 #define ip_tunnel_flags_empty(...) \ 61 __ipt_flag_op(bitmap_empty, __VA_ARGS__) 62 #define ip_tunnel_flags_intersect(...) \ 63 __ipt_flag_op(bitmap_intersects, __VA_ARGS__) 64 #define ip_tunnel_flags_subset(...) \ 65 __ipt_flag_op(bitmap_subset, __VA_ARGS__) 66 67 struct ip_tunnel_key { 68 __be64 tun_id; 69 union { 70 struct { 71 __be32 src; 72 __be32 dst; 73 } ipv4; 74 struct { 75 struct in6_addr src; 76 struct in6_addr dst; 77 } ipv6; 78 } u; 79 IP_TUNNEL_DECLARE_FLAGS(tun_flags); 80 __be32 label; /* Flow Label for IPv6 */ 81 u32 nhid; 82 u8 tos; /* TOS for IPv4, TC for IPv6 */ 83 u8 ttl; /* TTL for IPv4, HL for IPv6 */ 84 __be16 tp_src; 85 __be16 tp_dst; 86 __u8 flow_flags; 87 }; 88 89 struct ip_tunnel_encap { 90 u16 type; 91 u16 flags; 92 __be16 sport; 93 __be16 dport; 94 }; 95 96 /* Flags for ip_tunnel_info mode. */ 97 #define IP_TUNNEL_INFO_TX 0x01 /* represents tx tunnel parameters */ 98 #define IP_TUNNEL_INFO_IPV6 0x02 /* key contains IPv6 addresses */ 99 #define IP_TUNNEL_INFO_BRIDGE 0x04 /* represents a bridged tunnel id */ 100 101 /* Maximum tunnel options length. */ 102 #define IP_TUNNEL_OPTS_MAX \ 103 GENMASK((sizeof_field(struct ip_tunnel_info, \ 104 options_len) * BITS_PER_BYTE) - 1, 0) 105 106 #define ip_tunnel_info_opts(info) \ 107 _Generic(info, \ 108 const struct ip_tunnel_info * : ((const void *)(info)->options),\ 109 struct ip_tunnel_info * : ((void *)(info)->options)\ 110 ) 111 112 struct ip_tunnel_info { 113 struct ip_tunnel_key key; 114 struct ip_tunnel_encap encap; 115 #ifdef CONFIG_DST_CACHE 116 struct dst_cache dst_cache; 117 #endif 118 u8 options_len; 119 u8 mode; 120 u8 options[] __aligned_largest __counted_by(options_len); 121 }; 122 123 /* 6rd prefix/relay information */ 124 #ifdef CONFIG_IPV6_SIT_6RD 125 struct ip_tunnel_6rd_parm { 126 struct in6_addr prefix; 127 __be32 relay_prefix; 128 u16 prefixlen; 129 u16 relay_prefixlen; 130 struct rcu_head rcu; 131 }; 132 #endif 133 134 struct ip_tunnel_prl_entry { 135 struct ip_tunnel_prl_entry __rcu *next; 136 __be32 addr; 137 u16 flags; 138 struct rcu_head rcu_head; 139 }; 140 141 struct metadata_dst; 142 143 /* Kernel-side variant of ip_tunnel_parm */ 144 struct ip_tunnel_parm_kern { 145 char name[IFNAMSIZ]; 146 IP_TUNNEL_DECLARE_FLAGS(i_flags); 147 IP_TUNNEL_DECLARE_FLAGS(o_flags); 148 __be32 i_key; 149 __be32 o_key; 150 int link; 151 struct iphdr iph; 152 struct rcu_head rcu; 153 }; 154 155 struct ip_tunnel { 156 struct ip_tunnel __rcu *next; 157 struct hlist_node hash_node; 158 159 struct net_device *dev; 160 netdevice_tracker dev_tracker; 161 162 struct net *net; /* netns for packet i/o */ 163 164 unsigned long err_time; /* Time when the last ICMP error 165 * arrived */ 166 int err_count; /* Number of arrived ICMP errors */ 167 168 /* These four fields used only by GRE */ 169 u32 i_seqno; /* The last seen seqno */ 170 atomic_t o_seqno; /* The last output seqno */ 171 int tun_hlen; /* Precalculated header length */ 172 173 /* These four fields used only by ERSPAN */ 174 u32 index; /* ERSPAN type II index */ 175 u8 erspan_ver; /* ERSPAN version */ 176 u8 dir; /* ERSPAN direction */ 177 u16 hwid; /* ERSPAN hardware ID */ 178 179 struct dst_cache dst_cache; 180 181 struct ip_tunnel_parm_kern parms; 182 183 int mlink; 184 int encap_hlen; /* Encap header length (FOU,GUE) */ 185 int hlen; /* tun_hlen + encap_hlen */ 186 struct ip_tunnel_encap encap; 187 188 /* for SIT */ 189 #ifdef CONFIG_IPV6_SIT_6RD 190 struct ip_tunnel_6rd_parm __rcu *ip6rd; 191 #endif 192 struct ip_tunnel_prl_entry __rcu *prl; /* potential router list */ 193 unsigned int prl_count; /* # of entries in PRL */ 194 struct ip_tunnel_parm_kern __rcu *sit_parms; 195 unsigned int ip_tnl_net_id; 196 struct gro_cells gro_cells; 197 __u32 fwmark; 198 bool collect_md; 199 bool ignore_df; 200 }; 201 202 struct tnl_ptk_info { 203 IP_TUNNEL_DECLARE_FLAGS(flags); 204 __be16 proto; 205 __be32 key; 206 __be32 seq; 207 int hdr_len; 208 }; 209 210 #define PACKET_RCVD 0 211 #define PACKET_REJECT 1 212 #define PACKET_NEXT 2 213 214 #define IP_TNL_HASH_BITS 7 215 #define IP_TNL_HASH_SIZE (1 << IP_TNL_HASH_BITS) 216 217 struct ip_tunnel_net { 218 struct net_device *fb_tunnel_dev; 219 struct rtnl_link_ops *rtnl_link_ops; 220 struct hlist_head tunnels[IP_TNL_HASH_SIZE]; 221 struct mutex tunnels_lock; 222 struct ip_tunnel __rcu *collect_md_tun; 223 int type; 224 }; 225 226 static inline void ip_tunnel_set_options_present(unsigned long *flags) 227 { 228 IP_TUNNEL_DECLARE_FLAGS(present) = { }; 229 230 __set_bit(IP_TUNNEL_GENEVE_OPT_BIT, present); 231 __set_bit(IP_TUNNEL_VXLAN_OPT_BIT, present); 232 __set_bit(IP_TUNNEL_ERSPAN_OPT_BIT, present); 233 __set_bit(IP_TUNNEL_GTP_OPT_BIT, present); 234 __set_bit(IP_TUNNEL_PFCP_OPT_BIT, present); 235 236 ip_tunnel_flags_or(flags, flags, present); 237 } 238 239 static inline void ip_tunnel_clear_options_present(unsigned long *flags) 240 { 241 IP_TUNNEL_DECLARE_FLAGS(present) = { }; 242 243 __set_bit(IP_TUNNEL_GENEVE_OPT_BIT, present); 244 __set_bit(IP_TUNNEL_VXLAN_OPT_BIT, present); 245 __set_bit(IP_TUNNEL_ERSPAN_OPT_BIT, present); 246 __set_bit(IP_TUNNEL_GTP_OPT_BIT, present); 247 __set_bit(IP_TUNNEL_PFCP_OPT_BIT, present); 248 249 __ipt_flag_op(bitmap_andnot, flags, flags, present); 250 } 251 252 static inline bool ip_tunnel_is_options_present(const unsigned long *flags) 253 { 254 IP_TUNNEL_DECLARE_FLAGS(present) = { }; 255 256 __set_bit(IP_TUNNEL_GENEVE_OPT_BIT, present); 257 __set_bit(IP_TUNNEL_VXLAN_OPT_BIT, present); 258 __set_bit(IP_TUNNEL_ERSPAN_OPT_BIT, present); 259 __set_bit(IP_TUNNEL_GTP_OPT_BIT, present); 260 __set_bit(IP_TUNNEL_PFCP_OPT_BIT, present); 261 262 return ip_tunnel_flags_intersect(flags, present); 263 } 264 265 static inline bool ip_tunnel_flags_is_be16_compat(const unsigned long *flags) 266 { 267 IP_TUNNEL_DECLARE_FLAGS(supp) = { }; 268 269 bitmap_set(supp, 0, BITS_PER_TYPE(__be16)); 270 __set_bit(IP_TUNNEL_VTI_BIT, supp); 271 272 return ip_tunnel_flags_subset(flags, supp); 273 } 274 275 static inline void ip_tunnel_flags_from_be16(unsigned long *dst, __be16 flags) 276 { 277 ip_tunnel_flags_zero(dst); 278 279 bitmap_write(dst, be16_to_cpu(flags), 0, BITS_PER_TYPE(__be16)); 280 __assign_bit(IP_TUNNEL_VTI_BIT, dst, flags & VTI_ISVTI); 281 } 282 283 static inline __be16 ip_tunnel_flags_to_be16(const unsigned long *flags) 284 { 285 __be16 ret; 286 287 ret = cpu_to_be16(bitmap_read(flags, 0, BITS_PER_TYPE(__be16))); 288 if (test_bit(IP_TUNNEL_VTI_BIT, flags)) 289 ret |= VTI_ISVTI; 290 291 return ret; 292 } 293 294 static inline void ip_tunnel_key_init(struct ip_tunnel_key *key, 295 __be32 saddr, __be32 daddr, 296 u8 tos, u8 ttl, __be32 label, 297 __be16 tp_src, __be16 tp_dst, 298 __be64 tun_id, 299 const unsigned long *tun_flags) 300 { 301 key->tun_id = tun_id; 302 key->u.ipv4.src = saddr; 303 key->u.ipv4.dst = daddr; 304 memset((unsigned char *)key + IP_TUNNEL_KEY_IPV4_PAD, 305 0, IP_TUNNEL_KEY_IPV4_PAD_LEN); 306 key->tos = tos; 307 key->ttl = ttl; 308 key->label = label; 309 ip_tunnel_flags_copy(key->tun_flags, tun_flags); 310 311 /* For the tunnel types on the top of IPsec, the tp_src and tp_dst of 312 * the upper tunnel are used. 313 * E.g: GRE over IPSEC, the tp_src and tp_port are zero. 314 */ 315 key->tp_src = tp_src; 316 key->tp_dst = tp_dst; 317 318 /* Clear struct padding. */ 319 if (sizeof(*key) != IP_TUNNEL_KEY_SIZE) 320 memset((unsigned char *)key + IP_TUNNEL_KEY_SIZE, 321 0, sizeof(*key) - IP_TUNNEL_KEY_SIZE); 322 } 323 324 static inline bool 325 ip_tunnel_dst_cache_usable(const struct sk_buff *skb, 326 const struct ip_tunnel_info *info) 327 { 328 if (skb->mark) 329 return false; 330 331 return !info || !test_bit(IP_TUNNEL_NOCACHE_BIT, info->key.tun_flags); 332 } 333 334 static inline unsigned short ip_tunnel_info_af(const struct ip_tunnel_info 335 *tun_info) 336 { 337 return tun_info->mode & IP_TUNNEL_INFO_IPV6 ? AF_INET6 : AF_INET; 338 } 339 340 static inline __be64 key32_to_tunnel_id(__be32 key) 341 { 342 #ifdef __BIG_ENDIAN 343 return (__force __be64)key; 344 #else 345 return (__force __be64)((__force u64)key << 32); 346 #endif 347 } 348 349 /* Returns the least-significant 32 bits of a __be64. */ 350 static inline __be32 tunnel_id_to_key32(__be64 tun_id) 351 { 352 #ifdef __BIG_ENDIAN 353 return (__force __be32)tun_id; 354 #else 355 return (__force __be32)((__force u64)tun_id >> 32); 356 #endif 357 } 358 359 #ifdef CONFIG_INET 360 361 static inline void ip_tunnel_init_flow(struct flowi4 *fl4, 362 int proto, 363 __be32 daddr, __be32 saddr, 364 __be32 key, __u8 tos, 365 struct net *net, int oif, 366 __u32 mark, __u32 tun_inner_hash, 367 __u8 flow_flags) 368 { 369 memset(fl4, 0, sizeof(*fl4)); 370 371 if (oif) { 372 fl4->flowi4_l3mdev = l3mdev_master_upper_ifindex_by_index(net, oif); 373 /* Legacy VRF/l3mdev use case */ 374 fl4->flowi4_oif = fl4->flowi4_l3mdev ? 0 : oif; 375 } 376 377 fl4->daddr = daddr; 378 fl4->saddr = saddr; 379 fl4->flowi4_dscp = inet_dsfield_to_dscp(tos); 380 fl4->flowi4_proto = proto; 381 fl4->fl4_gre_key = key; 382 fl4->flowi4_mark = mark; 383 fl4->flowi4_multipath_hash = tun_inner_hash; 384 fl4->flowi4_flags = flow_flags; 385 } 386 387 int __ip_tunnel_init(struct net_device *dev); 388 #define ip_tunnel_init(DEV) \ 389 ({ \ 390 struct net_device *__dev = (DEV); \ 391 int __res = __ip_tunnel_init(__dev); \ 392 \ 393 if (!__res) \ 394 netdev_lockdep_set_classes(__dev);\ 395 __res; \ 396 }) 397 398 void ip_tunnel_uninit(struct net_device *dev); 399 void ip_tunnel_dellink(struct net_device *dev, struct list_head *head); 400 struct net *ip_tunnel_get_link_net(const struct net_device *dev); 401 int ip_tunnel_get_iflink(const struct net_device *dev); 402 int ip_tunnel_init_net(struct net *net, unsigned int ip_tnl_net_id, 403 struct rtnl_link_ops *ops, char *devname); 404 void ip_tunnel_delete_net(struct net *net, unsigned int id, 405 struct list_head *dev_to_kill); 406 407 void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev, 408 const struct iphdr *tnl_params, const u8 protocol); 409 void ip_md_tunnel_xmit(struct sk_buff *skb, struct net_device *dev, 410 const u8 proto, int tunnel_hlen); 411 int ip_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm_kern *p, 412 int cmd); 413 bool ip_tunnel_parm_from_user(struct ip_tunnel_parm_kern *kp, 414 const void __user *data); 415 bool ip_tunnel_parm_to_user(void __user *data, struct ip_tunnel_parm_kern *kp); 416 int ip_tunnel_siocdevprivate(struct net_device *dev, struct ifreq *ifr, 417 void __user *data, int cmd); 418 int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu); 419 420 struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn, 421 int link, const unsigned long *flags, 422 __be32 remote, __be32 local, 423 __be32 key); 424 425 void ip_tunnel_md_udp_encap(struct sk_buff *skb, struct ip_tunnel_info *info); 426 int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb, 427 const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst, 428 bool log_ecn_error); 429 int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[], 430 struct ip_tunnel_parm_kern *p, __u32 fwmark); 431 int ip_tunnel_newlink(struct net *net, struct net_device *dev, 432 struct nlattr *tb[], struct ip_tunnel_parm_kern *p, 433 __u32 fwmark); 434 void ip_tunnel_setup(struct net_device *dev, unsigned int net_id); 435 436 bool ip_tunnel_netlink_encap_parms(struct nlattr *data[], 437 struct ip_tunnel_encap *encap); 438 439 void ip_tunnel_netlink_parms(struct nlattr *data[], 440 struct ip_tunnel_parm_kern *parms); 441 442 extern const struct header_ops ip_tunnel_header_ops; 443 __be16 ip_tunnel_parse_protocol(const struct sk_buff *skb); 444 445 struct ip_tunnel_encap_ops { 446 size_t (*encap_hlen)(struct ip_tunnel_encap *e); 447 int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e, 448 u8 *protocol, struct flowi4 *fl4); 449 int (*err_handler)(struct sk_buff *skb, u32 info); 450 }; 451 452 #define MAX_IPTUN_ENCAP_OPS 8 453 454 extern const struct ip_tunnel_encap_ops __rcu * 455 iptun_encaps[MAX_IPTUN_ENCAP_OPS]; 456 457 int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *op, 458 unsigned int num); 459 int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *op, 460 unsigned int num); 461 462 int ip_tunnel_encap_setup(struct ip_tunnel *t, 463 struct ip_tunnel_encap *ipencap); 464 465 static inline enum skb_drop_reason 466 pskb_inet_may_pull_reason(struct sk_buff *skb) 467 { 468 int nhlen; 469 470 switch (skb->protocol) { 471 #if IS_ENABLED(CONFIG_IPV6) 472 case htons(ETH_P_IPV6): 473 nhlen = sizeof(struct ipv6hdr); 474 break; 475 #endif 476 case htons(ETH_P_IP): 477 nhlen = sizeof(struct iphdr); 478 break; 479 default: 480 nhlen = 0; 481 } 482 483 return pskb_network_may_pull_reason(skb, nhlen); 484 } 485 486 static inline bool pskb_inet_may_pull(struct sk_buff *skb) 487 { 488 return pskb_inet_may_pull_reason(skb) == SKB_NOT_DROPPED_YET; 489 } 490 491 /* Variant of pskb_inet_may_pull(). 492 */ 493 static inline enum skb_drop_reason 494 skb_vlan_inet_prepare(struct sk_buff *skb, bool inner_proto_inherit) 495 { 496 int nhlen = 0, maclen = inner_proto_inherit ? 0 : ETH_HLEN; 497 __be16 type = skb->protocol; 498 enum skb_drop_reason reason; 499 500 /* Essentially this is skb_protocol(skb, true) 501 * And we get MAC len. 502 */ 503 if (eth_type_vlan(type)) 504 type = __vlan_get_protocol(skb, type, &maclen); 505 506 switch (type) { 507 #if IS_ENABLED(CONFIG_IPV6) 508 case htons(ETH_P_IPV6): 509 nhlen = sizeof(struct ipv6hdr); 510 break; 511 #endif 512 case htons(ETH_P_IP): 513 nhlen = sizeof(struct iphdr); 514 break; 515 } 516 /* For ETH_P_IPV6/ETH_P_IP we make sure to pull 517 * a base network header in skb->head. 518 */ 519 reason = pskb_may_pull_reason(skb, maclen + nhlen); 520 if (reason) 521 return reason; 522 523 skb_set_network_header(skb, maclen); 524 525 return SKB_NOT_DROPPED_YET; 526 } 527 528 static inline int ip_encap_hlen(struct ip_tunnel_encap *e) 529 { 530 const struct ip_tunnel_encap_ops *ops; 531 int hlen = -EINVAL; 532 533 if (e->type == TUNNEL_ENCAP_NONE) 534 return 0; 535 536 if (e->type >= MAX_IPTUN_ENCAP_OPS) 537 return -EINVAL; 538 539 rcu_read_lock(); 540 ops = rcu_dereference(iptun_encaps[e->type]); 541 if (likely(ops && ops->encap_hlen)) 542 hlen = ops->encap_hlen(e); 543 rcu_read_unlock(); 544 545 return hlen; 546 } 547 548 static inline int ip_tunnel_encap(struct sk_buff *skb, 549 struct ip_tunnel_encap *e, 550 u8 *protocol, struct flowi4 *fl4) 551 { 552 const struct ip_tunnel_encap_ops *ops; 553 int ret = -EINVAL; 554 555 if (e->type == TUNNEL_ENCAP_NONE) 556 return 0; 557 558 if (e->type >= MAX_IPTUN_ENCAP_OPS) 559 return -EINVAL; 560 561 rcu_read_lock(); 562 ops = rcu_dereference(iptun_encaps[e->type]); 563 if (likely(ops && ops->build_header)) 564 ret = ops->build_header(skb, e, protocol, fl4); 565 rcu_read_unlock(); 566 567 return ret; 568 } 569 570 /* Extract dsfield from inner protocol */ 571 static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph, 572 const struct sk_buff *skb) 573 { 574 __be16 payload_protocol = skb_protocol(skb, true); 575 576 if (payload_protocol == htons(ETH_P_IP)) 577 return iph->tos; 578 else if (payload_protocol == htons(ETH_P_IPV6)) 579 return ipv6_get_dsfield((const struct ipv6hdr *)iph); 580 else 581 return 0; 582 } 583 584 static inline __be32 ip_tunnel_get_flowlabel(const struct iphdr *iph, 585 const struct sk_buff *skb) 586 { 587 __be16 payload_protocol = skb_protocol(skb, true); 588 589 if (payload_protocol == htons(ETH_P_IPV6)) 590 return ip6_flowlabel((const struct ipv6hdr *)iph); 591 else 592 return 0; 593 } 594 595 static inline u8 ip_tunnel_get_ttl(const struct iphdr *iph, 596 const struct sk_buff *skb) 597 { 598 __be16 payload_protocol = skb_protocol(skb, true); 599 600 if (payload_protocol == htons(ETH_P_IP)) 601 return iph->ttl; 602 else if (payload_protocol == htons(ETH_P_IPV6)) 603 return ((const struct ipv6hdr *)iph)->hop_limit; 604 else 605 return 0; 606 } 607 608 /* Propagate ECN bits out */ 609 static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph, 610 const struct sk_buff *skb) 611 { 612 u8 inner = ip_tunnel_get_dsfield(iph, skb); 613 614 return INET_ECN_encapsulate(tos, inner); 615 } 616 617 int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len, 618 __be16 inner_proto, bool raw_proto, bool xnet); 619 620 static inline int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, 621 __be16 inner_proto, bool xnet) 622 { 623 return __iptunnel_pull_header(skb, hdr_len, inner_proto, false, xnet); 624 } 625 626 void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb, 627 __be32 src, __be32 dst, u8 proto, 628 u8 tos, u8 ttl, __be16 df, bool xnet, u16 ipcb_flags); 629 struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md, 630 gfp_t flags); 631 int skb_tunnel_check_pmtu(struct sk_buff *skb, struct dst_entry *encap_dst, 632 int headroom, bool reply); 633 634 static inline unsigned int ip_tunnel_limit_headroom(unsigned int headroom) 635 { 636 /* we must cap headroom to some upperlimit, else pskb_expand_head 637 * will overflow header offsets in skb_headers_offset_update(). 638 */ 639 const unsigned int max_allowed = 512; 640 641 if (headroom > max_allowed) 642 headroom = max_allowed; 643 644 return headroom; 645 } 646 647 static inline void ip_tunnel_adj_headroom(struct net_device *dev, 648 unsigned int headroom) 649 { 650 headroom = ip_tunnel_limit_headroom(headroom); 651 652 if (headroom > READ_ONCE(dev->needed_headroom)) 653 WRITE_ONCE(dev->needed_headroom, headroom); 654 } 655 656 int iptunnel_handle_offloads(struct sk_buff *skb, int gso_type_mask); 657 658 static inline int iptunnel_pull_offloads(struct sk_buff *skb) 659 { 660 if (skb_is_gso(skb)) { 661 int err; 662 663 err = skb_unclone(skb, GFP_ATOMIC); 664 if (unlikely(err)) 665 return err; 666 skb_shinfo(skb)->gso_type &= ~(NETIF_F_GSO_ENCAP_ALL >> 667 NETIF_F_GSO_SHIFT); 668 } 669 670 skb->encapsulation = 0; 671 return 0; 672 } 673 674 static inline void iptunnel_xmit_stats(struct net_device *dev, int pkt_len) 675 { 676 if (pkt_len > 0) { 677 if (dev->pcpu_stat_type == NETDEV_PCPU_STAT_DSTATS) { 678 struct pcpu_dstats *dstats = get_cpu_ptr(dev->dstats); 679 680 u64_stats_update_begin(&dstats->syncp); 681 u64_stats_add(&dstats->tx_bytes, pkt_len); 682 u64_stats_inc(&dstats->tx_packets); 683 u64_stats_update_end(&dstats->syncp); 684 put_cpu_ptr(dstats); 685 return; 686 } 687 if (dev->pcpu_stat_type == NETDEV_PCPU_STAT_TSTATS) { 688 struct pcpu_sw_netstats *tstats = get_cpu_ptr(dev->tstats); 689 690 u64_stats_update_begin(&tstats->syncp); 691 u64_stats_add(&tstats->tx_bytes, pkt_len); 692 u64_stats_inc(&tstats->tx_packets); 693 u64_stats_update_end(&tstats->syncp); 694 put_cpu_ptr(tstats); 695 return; 696 } 697 pr_err_once("iptunnel_xmit_stats pcpu_stat_type=%d\n", 698 dev->pcpu_stat_type); 699 WARN_ON_ONCE(1); 700 return; 701 } 702 703 if (pkt_len < 0) { 704 DEV_STATS_INC(dev, tx_errors); 705 DEV_STATS_INC(dev, tx_aborted_errors); 706 } else { 707 DEV_STATS_INC(dev, tx_dropped); 708 } 709 } 710 711 static inline void ip_tunnel_info_opts_get(void *to, 712 const struct ip_tunnel_info *info) 713 { 714 memcpy(to, ip_tunnel_info_opts(info), info->options_len); 715 } 716 717 static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info, 718 const void *from, int len, 719 const unsigned long *flags) 720 { 721 info->options_len = len; 722 if (len > 0) { 723 memcpy(ip_tunnel_info_opts(info), from, len); 724 ip_tunnel_flags_or(info->key.tun_flags, info->key.tun_flags, 725 flags); 726 } 727 } 728 729 static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate) 730 { 731 return (struct ip_tunnel_info *)lwtstate->data; 732 } 733 734 DECLARE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt); 735 736 /* Returns > 0 if metadata should be collected */ 737 static inline int ip_tunnel_collect_metadata(void) 738 { 739 return static_branch_unlikely(&ip_tunnel_metadata_cnt); 740 } 741 742 void __init ip_tunnel_core_init(void); 743 744 void ip_tunnel_need_metadata(void); 745 void ip_tunnel_unneed_metadata(void); 746 747 #else /* CONFIG_INET */ 748 749 static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate) 750 { 751 return NULL; 752 } 753 754 static inline void ip_tunnel_need_metadata(void) 755 { 756 } 757 758 static inline void ip_tunnel_unneed_metadata(void) 759 { 760 } 761 762 static inline void ip_tunnel_info_opts_get(void *to, 763 const struct ip_tunnel_info *info) 764 { 765 } 766 767 static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info, 768 const void *from, int len, 769 const unsigned long *flags) 770 { 771 info->options_len = 0; 772 } 773 774 #endif /* CONFIG_INET */ 775 776 #endif /* __NET_IP_TUNNELS_H */ 777