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