ip_tunnels.h 19 KB

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