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 4 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 }; 131 #endif 132 133 struct ip_tunnel_prl_entry { 134 struct ip_tunnel_prl_entry __rcu *next; 135 __be32 addr; 136 u16 flags; 137 struct rcu_head rcu_head; 138 }; 139 140 struct metadata_dst; 141 142 /* Kernel-side variant of ip_tunnel_parm */ 143 struct ip_tunnel_parm_kern { 144 char name[IFNAMSIZ]; 145 IP_TUNNEL_DECLARE_FLAGS(i_flags); 146 IP_TUNNEL_DECLARE_FLAGS(o_flags); 147 __be32 i_key; 148 __be32 o_key; 149 int link; 150 struct iphdr iph; 151 }; 152 153 struct ip_tunnel { 154 struct ip_tunnel __rcu *next; 155 struct hlist_node hash_node; 156 157 struct net_device *dev; 158 netdevice_tracker dev_tracker; 159 160 struct net *net; /* netns for packet i/o */ 161 162 unsigned long err_time; /* Time when the last ICMP error 163 * arrived */ 164 int err_count; /* Number of arrived ICMP errors */ 165 166 /* These four fields used only by GRE */ 167 u32 i_seqno; /* The last seen seqno */ 168 atomic_t o_seqno; /* The last output seqno */ 169 int tun_hlen; /* Precalculated header length */ 170 171 /* These four fields used only by ERSPAN */ 172 u32 index; /* ERSPAN type II index */ 173 u8 erspan_ver; /* ERSPAN version */ 174 u8 dir; /* ERSPAN direction */ 175 u16 hwid; /* ERSPAN hardware ID */ 176 177 struct dst_cache dst_cache; 178 179 struct ip_tunnel_parm_kern parms; 180 181 int mlink; 182 int encap_hlen; /* Encap header length (FOU,GUE) */ 183 int hlen; /* tun_hlen + encap_hlen */ 184 struct ip_tunnel_encap encap; 185 186 /* for SIT */ 187 #ifdef CONFIG_IPV6_SIT_6RD 188 struct ip_tunnel_6rd_parm ip6rd; 189 #endif 190 struct ip_tunnel_prl_entry __rcu *prl; /* potential router list */ 191 unsigned int prl_count; /* # of entries in PRL */ 192 unsigned int ip_tnl_net_id; 193 struct gro_cells gro_cells; 194 __u32 fwmark; 195 bool collect_md; 196 bool ignore_df; 197 }; 198 199 struct tnl_ptk_info { 200 IP_TUNNEL_DECLARE_FLAGS(flags); 201 __be16 proto; 202 __be32 key; 203 __be32 seq; 204 int hdr_len; 205 }; 206 207 #define PACKET_RCVD 0 208 #define PACKET_REJECT 1 209 #define PACKET_NEXT 2 210 211 #define IP_TNL_HASH_BITS 7 212 #define IP_TNL_HASH_SIZE (1 << IP_TNL_HASH_BITS) 213 214 struct ip_tunnel_net { 215 struct net_device *fb_tunnel_dev; 216 struct rtnl_link_ops *rtnl_link_ops; 217 struct hlist_head tunnels[IP_TNL_HASH_SIZE]; 218 struct ip_tunnel __rcu *collect_md_tun; 219 int type; 220 }; 221 222 static inline void ip_tunnel_set_options_present(unsigned long *flags) 223 { 224 IP_TUNNEL_DECLARE_FLAGS(present) = { }; 225 226 __set_bit(IP_TUNNEL_GENEVE_OPT_BIT, present); 227 __set_bit(IP_TUNNEL_VXLAN_OPT_BIT, present); 228 __set_bit(IP_TUNNEL_ERSPAN_OPT_BIT, present); 229 __set_bit(IP_TUNNEL_GTP_OPT_BIT, present); 230 __set_bit(IP_TUNNEL_PFCP_OPT_BIT, present); 231 232 ip_tunnel_flags_or(flags, flags, present); 233 } 234 235 static inline void ip_tunnel_clear_options_present(unsigned long *flags) 236 { 237 IP_TUNNEL_DECLARE_FLAGS(present) = { }; 238 239 __set_bit(IP_TUNNEL_GENEVE_OPT_BIT, present); 240 __set_bit(IP_TUNNEL_VXLAN_OPT_BIT, present); 241 __set_bit(IP_TUNNEL_ERSPAN_OPT_BIT, present); 242 __set_bit(IP_TUNNEL_GTP_OPT_BIT, present); 243 __set_bit(IP_TUNNEL_PFCP_OPT_BIT, present); 244 245 __ipt_flag_op(bitmap_andnot, flags, flags, present); 246 } 247 248 static inline bool ip_tunnel_is_options_present(const unsigned long *flags) 249 { 250 IP_TUNNEL_DECLARE_FLAGS(present) = { }; 251 252 __set_bit(IP_TUNNEL_GENEVE_OPT_BIT, present); 253 __set_bit(IP_TUNNEL_VXLAN_OPT_BIT, present); 254 __set_bit(IP_TUNNEL_ERSPAN_OPT_BIT, present); 255 __set_bit(IP_TUNNEL_GTP_OPT_BIT, present); 256 __set_bit(IP_TUNNEL_PFCP_OPT_BIT, present); 257 258 return ip_tunnel_flags_intersect(flags, present); 259 } 260 261 static inline bool ip_tunnel_flags_is_be16_compat(const unsigned long *flags) 262 { 263 IP_TUNNEL_DECLARE_FLAGS(supp) = { }; 264 265 bitmap_set(supp, 0, BITS_PER_TYPE(__be16)); 266 __set_bit(IP_TUNNEL_VTI_BIT, supp); 267 268 return ip_tunnel_flags_subset(flags, supp); 269 } 270 271 static inline void ip_tunnel_flags_from_be16(unsigned long *dst, __be16 flags) 272 { 273 ip_tunnel_flags_zero(dst); 274 275 bitmap_write(dst, be16_to_cpu(flags), 0, BITS_PER_TYPE(__be16)); 276 __assign_bit(IP_TUNNEL_VTI_BIT, dst, flags & VTI_ISVTI); 277 } 278 279 static inline __be16 ip_tunnel_flags_to_be16(const unsigned long *flags) 280 { 281 __be16 ret; 282 283 ret = cpu_to_be16(bitmap_read(flags, 0, BITS_PER_TYPE(__be16))); 284 if (test_bit(IP_TUNNEL_VTI_BIT, flags)) 285 ret |= VTI_ISVTI; 286 287 return ret; 288 } 289 290 static inline void ip_tunnel_key_init(struct ip_tunnel_key *key, 291 __be32 saddr, __be32 daddr, 292 u8 tos, u8 ttl, __be32 label, 293 __be16 tp_src, __be16 tp_dst, 294 __be64 tun_id, 295 const unsigned long *tun_flags) 296 { 297 key->tun_id = tun_id; 298 key->u.ipv4.src = saddr; 299 key->u.ipv4.dst = daddr; 300 memset((unsigned char *)key + IP_TUNNEL_KEY_IPV4_PAD, 301 0, IP_TUNNEL_KEY_IPV4_PAD_LEN); 302 key->tos = tos; 303 key->ttl = ttl; 304 key->label = label; 305 ip_tunnel_flags_copy(key->tun_flags, tun_flags); 306 307 /* For the tunnel types on the top of IPsec, the tp_src and tp_dst of 308 * the upper tunnel are used. 309 * E.g: GRE over IPSEC, the tp_src and tp_port are zero. 310 */ 311 key->tp_src = tp_src; 312 key->tp_dst = tp_dst; 313 314 /* Clear struct padding. */ 315 if (sizeof(*key) != IP_TUNNEL_KEY_SIZE) 316 memset((unsigned char *)key + IP_TUNNEL_KEY_SIZE, 317 0, sizeof(*key) - IP_TUNNEL_KEY_SIZE); 318 } 319 320 static inline bool 321 ip_tunnel_dst_cache_usable(const struct sk_buff *skb, 322 const struct ip_tunnel_info *info) 323 { 324 if (skb->mark) 325 return false; 326 327 return !info || !test_bit(IP_TUNNEL_NOCACHE_BIT, info->key.tun_flags); 328 } 329 330 static inline unsigned short ip_tunnel_info_af(const struct ip_tunnel_info 331 *tun_info) 332 { 333 return tun_info->mode & IP_TUNNEL_INFO_IPV6 ? AF_INET6 : AF_INET; 334 } 335 336 static inline __be64 key32_to_tunnel_id(__be32 key) 337 { 338 #ifdef __BIG_ENDIAN 339 return (__force __be64)key; 340 #else 341 return (__force __be64)((__force u64)key << 32); 342 #endif 343 } 344 345 /* Returns the least-significant 32 bits of a __be64. */ 346 static inline __be32 tunnel_id_to_key32(__be64 tun_id) 347 { 348 #ifdef __BIG_ENDIAN 349 return (__force __be32)tun_id; 350 #else 351 return (__force __be32)((__force u64)tun_id >> 32); 352 #endif 353 } 354 355 #ifdef CONFIG_INET 356 357 static inline void ip_tunnel_init_flow(struct flowi4 *fl4, 358 int proto, 359 __be32 daddr, __be32 saddr, 360 __be32 key, __u8 tos, 361 struct net *net, int oif, 362 __u32 mark, __u32 tun_inner_hash, 363 __u8 flow_flags) 364 { 365 memset(fl4, 0, sizeof(*fl4)); 366 367 if (oif) { 368 fl4->flowi4_l3mdev = l3mdev_master_upper_ifindex_by_index(net, oif); 369 /* Legacy VRF/l3mdev use case */ 370 fl4->flowi4_oif = fl4->flowi4_l3mdev ? 0 : oif; 371 } 372 373 fl4->daddr = daddr; 374 fl4->saddr = saddr; 375 fl4->flowi4_dscp = inet_dsfield_to_dscp(tos); 376 fl4->flowi4_proto = proto; 377 fl4->fl4_gre_key = key; 378 fl4->flowi4_mark = mark; 379 fl4->flowi4_multipath_hash = tun_inner_hash; 380 fl4->flowi4_flags = flow_flags; 381 } 382 383 int __ip_tunnel_init(struct net_device *dev); 384 #define ip_tunnel_init(DEV) \ 385 ({ \ 386 struct net_device *__dev = (DEV); \ 387 int __res = __ip_tunnel_init(__dev); \ 388 \ 389 if (!__res) \ 390 netdev_lockdep_set_classes(__dev);\ 391 __res; \ 392 }) 393 394 void ip_tunnel_uninit(struct net_device *dev); 395 void ip_tunnel_dellink(struct net_device *dev, struct list_head *head); 396 struct net *ip_tunnel_get_link_net(const struct net_device *dev); 397 int ip_tunnel_get_iflink(const struct net_device *dev); 398 int ip_tunnel_init_net(struct net *net, unsigned int ip_tnl_net_id, 399 struct rtnl_link_ops *ops, char *devname); 400 void ip_tunnel_delete_net(struct net *net, unsigned int id, 401 struct rtnl_link_ops *ops, 402 struct list_head *dev_to_kill); 403 404 void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev, 405 const struct iphdr *tnl_params, const u8 protocol); 406 void ip_md_tunnel_xmit(struct sk_buff *skb, struct net_device *dev, 407 const u8 proto, int tunnel_hlen); 408 int ip_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm_kern *p, 409 int cmd); 410 bool ip_tunnel_parm_from_user(struct ip_tunnel_parm_kern *kp, 411 const void __user *data); 412 bool ip_tunnel_parm_to_user(void __user *data, struct ip_tunnel_parm_kern *kp); 413 int ip_tunnel_siocdevprivate(struct net_device *dev, struct ifreq *ifr, 414 void __user *data, int cmd); 415 int __ip_tunnel_change_mtu(struct net_device *dev, int new_mtu, bool strict); 416 int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu); 417 418 struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn, 419 int link, const unsigned long *flags, 420 __be32 remote, __be32 local, 421 __be32 key); 422 423 void ip_tunnel_md_udp_encap(struct sk_buff *skb, struct ip_tunnel_info *info); 424 int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb, 425 const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst, 426 bool log_ecn_error); 427 int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[], 428 struct ip_tunnel_parm_kern *p, __u32 fwmark); 429 int ip_tunnel_newlink(struct net *net, struct net_device *dev, 430 struct nlattr *tb[], struct ip_tunnel_parm_kern *p, 431 __u32 fwmark); 432 void ip_tunnel_setup(struct net_device *dev, unsigned int net_id); 433 434 bool ip_tunnel_netlink_encap_parms(struct nlattr *data[], 435 struct ip_tunnel_encap *encap); 436 437 void ip_tunnel_netlink_parms(struct nlattr *data[], 438 struct ip_tunnel_parm_kern *parms); 439 440 extern const struct header_ops ip_tunnel_header_ops; 441 __be16 ip_tunnel_parse_protocol(const struct sk_buff *skb); 442 443 struct ip_tunnel_encap_ops { 444 size_t (*encap_hlen)(struct ip_tunnel_encap *e); 445 int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e, 446 u8 *protocol, struct flowi4 *fl4); 447 int (*err_handler)(struct sk_buff *skb, u32 info); 448 }; 449 450 #define MAX_IPTUN_ENCAP_OPS 8 451 452 extern const struct ip_tunnel_encap_ops __rcu * 453 iptun_encaps[MAX_IPTUN_ENCAP_OPS]; 454 455 int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *op, 456 unsigned int num); 457 int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *op, 458 unsigned int num); 459 460 int ip_tunnel_encap_setup(struct ip_tunnel *t, 461 struct ip_tunnel_encap *ipencap); 462 463 static inline enum skb_drop_reason 464 pskb_inet_may_pull_reason(struct sk_buff *skb) 465 { 466 int nhlen; 467 468 switch (skb->protocol) { 469 #if IS_ENABLED(CONFIG_IPV6) 470 case htons(ETH_P_IPV6): 471 nhlen = sizeof(struct ipv6hdr); 472 break; 473 #endif 474 case htons(ETH_P_IP): 475 nhlen = sizeof(struct iphdr); 476 break; 477 default: 478 nhlen = 0; 479 } 480 481 return pskb_network_may_pull_reason(skb, nhlen); 482 } 483 484 static inline bool pskb_inet_may_pull(struct sk_buff *skb) 485 { 486 return pskb_inet_may_pull_reason(skb) == SKB_NOT_DROPPED_YET; 487 } 488 489 /* Variant of pskb_inet_may_pull(). 490 */ 491 static inline enum skb_drop_reason 492 skb_vlan_inet_prepare(struct sk_buff *skb, bool inner_proto_inherit) 493 { 494 int nhlen = 0, maclen = inner_proto_inherit ? 0 : ETH_HLEN; 495 __be16 type = skb->protocol; 496 enum skb_drop_reason reason; 497 498 /* Essentially this is skb_protocol(skb, true) 499 * And we get MAC len. 500 */ 501 if (eth_type_vlan(type)) 502 type = __vlan_get_protocol(skb, type, &maclen); 503 504 switch (type) { 505 #if IS_ENABLED(CONFIG_IPV6) 506 case htons(ETH_P_IPV6): 507 nhlen = sizeof(struct ipv6hdr); 508 break; 509 #endif 510 case htons(ETH_P_IP): 511 nhlen = sizeof(struct iphdr); 512 break; 513 } 514 /* For ETH_P_IPV6/ETH_P_IP we make sure to pull 515 * a base network header in skb->head. 516 */ 517 reason = pskb_may_pull_reason(skb, maclen + nhlen); 518 if (reason) 519 return reason; 520 521 skb_set_network_header(skb, maclen); 522 523 return SKB_NOT_DROPPED_YET; 524 } 525 526 static inline int ip_encap_hlen(struct ip_tunnel_encap *e) 527 { 528 const struct ip_tunnel_encap_ops *ops; 529 int hlen = -EINVAL; 530 531 if (e->type == TUNNEL_ENCAP_NONE) 532 return 0; 533 534 if (e->type >= MAX_IPTUN_ENCAP_OPS) 535 return -EINVAL; 536 537 rcu_read_lock(); 538 ops = rcu_dereference(iptun_encaps[e->type]); 539 if (likely(ops && ops->encap_hlen)) 540 hlen = ops->encap_hlen(e); 541 rcu_read_unlock(); 542 543 return hlen; 544 } 545 546 static inline int ip_tunnel_encap(struct sk_buff *skb, 547 struct ip_tunnel_encap *e, 548 u8 *protocol, struct flowi4 *fl4) 549 { 550 const struct ip_tunnel_encap_ops *ops; 551 int ret = -EINVAL; 552 553 if (e->type == TUNNEL_ENCAP_NONE) 554 return 0; 555 556 if (e->type >= MAX_IPTUN_ENCAP_OPS) 557 return -EINVAL; 558 559 rcu_read_lock(); 560 ops = rcu_dereference(iptun_encaps[e->type]); 561 if (likely(ops && ops->build_header)) 562 ret = ops->build_header(skb, e, protocol, fl4); 563 rcu_read_unlock(); 564 565 return ret; 566 } 567 568 /* Extract dsfield from inner protocol */ 569 static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph, 570 const struct sk_buff *skb) 571 { 572 __be16 payload_protocol = skb_protocol(skb, true); 573 574 if (payload_protocol == htons(ETH_P_IP)) 575 return iph->tos; 576 else if (payload_protocol == htons(ETH_P_IPV6)) 577 return ipv6_get_dsfield((const struct ipv6hdr *)iph); 578 else 579 return 0; 580 } 581 582 static inline __be32 ip_tunnel_get_flowlabel(const struct iphdr *iph, 583 const struct sk_buff *skb) 584 { 585 __be16 payload_protocol = skb_protocol(skb, true); 586 587 if (payload_protocol == htons(ETH_P_IPV6)) 588 return ip6_flowlabel((const struct ipv6hdr *)iph); 589 else 590 return 0; 591 } 592 593 static inline u8 ip_tunnel_get_ttl(const struct iphdr *iph, 594 const struct sk_buff *skb) 595 { 596 __be16 payload_protocol = skb_protocol(skb, true); 597 598 if (payload_protocol == htons(ETH_P_IP)) 599 return iph->ttl; 600 else if (payload_protocol == htons(ETH_P_IPV6)) 601 return ((const struct ipv6hdr *)iph)->hop_limit; 602 else 603 return 0; 604 } 605 606 /* Propagate ECN bits out */ 607 static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph, 608 const struct sk_buff *skb) 609 { 610 u8 inner = ip_tunnel_get_dsfield(iph, skb); 611 612 return INET_ECN_encapsulate(tos, inner); 613 } 614 615 int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len, 616 __be16 inner_proto, bool raw_proto, bool xnet); 617 618 static inline int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, 619 __be16 inner_proto, bool xnet) 620 { 621 return __iptunnel_pull_header(skb, hdr_len, inner_proto, false, xnet); 622 } 623 624 void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb, 625 __be32 src, __be32 dst, u8 proto, 626 u8 tos, u8 ttl, __be16 df, bool xnet, u16 ipcb_flags); 627 struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md, 628 gfp_t flags); 629 int skb_tunnel_check_pmtu(struct sk_buff *skb, struct dst_entry *encap_dst, 630 int headroom, bool reply); 631 632 static inline void ip_tunnel_adj_headroom(struct net_device *dev, 633 unsigned int headroom) 634 { 635 /* we must cap headroom to some upperlimit, else pskb_expand_head 636 * will overflow header offsets in skb_headers_offset_update(). 637 */ 638 const unsigned int max_allowed = 512; 639 640 if (headroom > max_allowed) 641 headroom = max_allowed; 642 643 if (headroom > READ_ONCE(dev->needed_headroom)) 644 WRITE_ONCE(dev->needed_headroom, headroom); 645 } 646 647 int iptunnel_handle_offloads(struct sk_buff *skb, int gso_type_mask); 648 649 static inline int iptunnel_pull_offloads(struct sk_buff *skb) 650 { 651 if (skb_is_gso(skb)) { 652 int err; 653 654 err = skb_unclone(skb, GFP_ATOMIC); 655 if (unlikely(err)) 656 return err; 657 skb_shinfo(skb)->gso_type &= ~(NETIF_F_GSO_ENCAP_ALL >> 658 NETIF_F_GSO_SHIFT); 659 } 660 661 skb->encapsulation = 0; 662 return 0; 663 } 664 665 static inline void iptunnel_xmit_stats(struct net_device *dev, int pkt_len) 666 { 667 if (pkt_len > 0) { 668 struct pcpu_sw_netstats *tstats = get_cpu_ptr(dev->tstats); 669 670 u64_stats_update_begin(&tstats->syncp); 671 u64_stats_add(&tstats->tx_bytes, pkt_len); 672 u64_stats_inc(&tstats->tx_packets); 673 u64_stats_update_end(&tstats->syncp); 674 put_cpu_ptr(tstats); 675 return; 676 } 677 678 if (pkt_len < 0) { 679 DEV_STATS_INC(dev, tx_errors); 680 DEV_STATS_INC(dev, tx_aborted_errors); 681 } else { 682 DEV_STATS_INC(dev, tx_dropped); 683 } 684 } 685 686 static inline void ip_tunnel_info_opts_get(void *to, 687 const struct ip_tunnel_info *info) 688 { 689 memcpy(to, ip_tunnel_info_opts(info), info->options_len); 690 } 691 692 static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info, 693 const void *from, int len, 694 const unsigned long *flags) 695 { 696 info->options_len = len; 697 if (len > 0) { 698 memcpy(ip_tunnel_info_opts(info), from, len); 699 ip_tunnel_flags_or(info->key.tun_flags, info->key.tun_flags, 700 flags); 701 } 702 } 703 704 static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate) 705 { 706 return (struct ip_tunnel_info *)lwtstate->data; 707 } 708 709 DECLARE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt); 710 711 /* Returns > 0 if metadata should be collected */ 712 static inline int ip_tunnel_collect_metadata(void) 713 { 714 return static_branch_unlikely(&ip_tunnel_metadata_cnt); 715 } 716 717 void __init ip_tunnel_core_init(void); 718 719 void ip_tunnel_need_metadata(void); 720 void ip_tunnel_unneed_metadata(void); 721 722 #else /* CONFIG_INET */ 723 724 static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate) 725 { 726 return NULL; 727 } 728 729 static inline void ip_tunnel_need_metadata(void) 730 { 731 } 732 733 static inline void ip_tunnel_unneed_metadata(void) 734 { 735 } 736 737 static inline void ip_tunnel_info_opts_get(void *to, 738 const struct ip_tunnel_info *info) 739 { 740 } 741 742 static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info, 743 const void *from, int len, 744 const unsigned long *flags) 745 { 746 info->options_len = 0; 747 } 748 749 #endif /* CONFIG_INET */ 750 751 #endif /* __NET_IP_TUNNELS_H */ 752