ip_tunnel_core.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013 Nicira, Inc.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/types.h>
  7. #include <linux/kernel.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/in.h>
  11. #include <linux/if_arp.h>
  12. #include <linux/init.h>
  13. #include <linux/in6.h>
  14. #include <linux/inetdevice.h>
  15. #include <linux/netfilter_ipv4.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/if_ether.h>
  18. #include <linux/if_vlan.h>
  19. #include <linux/static_key.h>
  20. #include <net/ip.h>
  21. #include <net/icmp.h>
  22. #include <net/protocol.h>
  23. #include <net/ip_tunnels.h>
  24. #include <net/ip6_tunnel.h>
  25. #include <net/ip6_checksum.h>
  26. #include <net/arp.h>
  27. #include <net/checksum.h>
  28. #include <net/dsfield.h>
  29. #include <net/inet_ecn.h>
  30. #include <net/xfrm.h>
  31. #include <net/net_namespace.h>
  32. #include <net/netns/generic.h>
  33. #include <net/rtnetlink.h>
  34. #include <net/dst_metadata.h>
  35. #include <net/geneve.h>
  36. #include <net/vxlan.h>
  37. #include <net/erspan.h>
  38. const struct ip_tunnel_encap_ops __rcu *
  39. iptun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
  40. EXPORT_SYMBOL(iptun_encaps);
  41. const struct ip6_tnl_encap_ops __rcu *
  42. ip6tun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
  43. EXPORT_SYMBOL(ip6tun_encaps);
  44. void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
  45. __be32 src, __be32 dst, __u8 proto,
  46. __u8 tos, __u8 ttl, __be16 df, bool xnet)
  47. {
  48. int pkt_len = skb->len - skb_inner_network_offset(skb);
  49. struct net *net = dev_net(rt->dst.dev);
  50. struct net_device *dev = skb->dev;
  51. struct iphdr *iph;
  52. int err;
  53. skb_scrub_packet(skb, xnet);
  54. skb_clear_hash_if_not_l4(skb);
  55. skb_dst_set(skb, &rt->dst);
  56. memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
  57. /* Push down and install the IP header. */
  58. skb_push(skb, sizeof(struct iphdr));
  59. skb_reset_network_header(skb);
  60. iph = ip_hdr(skb);
  61. iph->version = 4;
  62. iph->ihl = sizeof(struct iphdr) >> 2;
  63. iph->frag_off = ip_mtu_locked(&rt->dst) ? 0 : df;
  64. iph->protocol = proto;
  65. iph->tos = tos;
  66. iph->daddr = dst;
  67. iph->saddr = src;
  68. iph->ttl = ttl;
  69. __ip_select_ident(net, iph, skb_shinfo(skb)->gso_segs ?: 1);
  70. err = ip_local_out(net, sk, skb);
  71. if (dev) {
  72. if (unlikely(net_xmit_eval(err)))
  73. pkt_len = 0;
  74. iptunnel_xmit_stats(dev, pkt_len);
  75. }
  76. }
  77. EXPORT_SYMBOL_GPL(iptunnel_xmit);
  78. int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
  79. __be16 inner_proto, bool raw_proto, bool xnet)
  80. {
  81. if (unlikely(!pskb_may_pull(skb, hdr_len)))
  82. return -ENOMEM;
  83. skb_pull_rcsum(skb, hdr_len);
  84. if (!raw_proto && inner_proto == htons(ETH_P_TEB)) {
  85. struct ethhdr *eh;
  86. if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
  87. return -ENOMEM;
  88. eh = (struct ethhdr *)skb->data;
  89. if (likely(eth_proto_is_802_3(eh->h_proto)))
  90. skb->protocol = eh->h_proto;
  91. else
  92. skb->protocol = htons(ETH_P_802_2);
  93. } else {
  94. skb->protocol = inner_proto;
  95. }
  96. skb_clear_hash_if_not_l4(skb);
  97. __vlan_hwaccel_clear_tag(skb);
  98. skb_set_queue_mapping(skb, 0);
  99. skb_scrub_packet(skb, xnet);
  100. return iptunnel_pull_offloads(skb);
  101. }
  102. EXPORT_SYMBOL_GPL(__iptunnel_pull_header);
  103. struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
  104. gfp_t flags)
  105. {
  106. struct metadata_dst *res;
  107. struct ip_tunnel_info *dst, *src;
  108. if (!md || md->type != METADATA_IP_TUNNEL ||
  109. md->u.tun_info.mode & IP_TUNNEL_INFO_TX)
  110. return NULL;
  111. src = &md->u.tun_info;
  112. res = metadata_dst_alloc(src->options_len, METADATA_IP_TUNNEL, flags);
  113. if (!res)
  114. return NULL;
  115. dst = &res->u.tun_info;
  116. dst->key.tun_id = src->key.tun_id;
  117. if (src->mode & IP_TUNNEL_INFO_IPV6)
  118. memcpy(&dst->key.u.ipv6.dst, &src->key.u.ipv6.src,
  119. sizeof(struct in6_addr));
  120. else
  121. dst->key.u.ipv4.dst = src->key.u.ipv4.src;
  122. dst->key.tun_flags = src->key.tun_flags;
  123. dst->mode = src->mode | IP_TUNNEL_INFO_TX;
  124. ip_tunnel_info_opts_set(dst, ip_tunnel_info_opts(src),
  125. src->options_len, 0);
  126. return res;
  127. }
  128. EXPORT_SYMBOL_GPL(iptunnel_metadata_reply);
  129. int iptunnel_handle_offloads(struct sk_buff *skb,
  130. int gso_type_mask)
  131. {
  132. int err;
  133. if (likely(!skb->encapsulation)) {
  134. skb_reset_inner_headers(skb);
  135. skb->encapsulation = 1;
  136. }
  137. if (skb_is_gso(skb)) {
  138. err = skb_header_unclone(skb, GFP_ATOMIC);
  139. if (unlikely(err))
  140. return err;
  141. skb_shinfo(skb)->gso_type |= gso_type_mask;
  142. return 0;
  143. }
  144. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  145. skb->ip_summed = CHECKSUM_NONE;
  146. /* We clear encapsulation here to prevent badly-written
  147. * drivers potentially deciding to offload an inner checksum
  148. * if we set CHECKSUM_PARTIAL on the outer header.
  149. * This should go away when the drivers are all fixed.
  150. */
  151. skb->encapsulation = 0;
  152. }
  153. return 0;
  154. }
  155. EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
  156. /**
  157. * iptunnel_pmtud_build_icmp() - Build ICMP error message for PMTUD
  158. * @skb: Original packet with L2 header
  159. * @mtu: MTU value for ICMP error
  160. *
  161. * Return: length on success, negative error code if message couldn't be built.
  162. */
  163. static int iptunnel_pmtud_build_icmp(struct sk_buff *skb, int mtu)
  164. {
  165. const struct iphdr *iph = ip_hdr(skb);
  166. struct icmphdr *icmph;
  167. struct iphdr *niph;
  168. struct ethhdr eh;
  169. int len, err;
  170. if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct iphdr)))
  171. return -EINVAL;
  172. skb_copy_bits(skb, skb_mac_offset(skb), &eh, ETH_HLEN);
  173. pskb_pull(skb, ETH_HLEN);
  174. skb_reset_network_header(skb);
  175. err = pskb_trim(skb, 576 - sizeof(*niph) - sizeof(*icmph));
  176. if (err)
  177. return err;
  178. len = skb->len + sizeof(*icmph);
  179. err = skb_cow(skb, sizeof(*niph) + sizeof(*icmph) + ETH_HLEN);
  180. if (err)
  181. return err;
  182. icmph = skb_push(skb, sizeof(*icmph));
  183. *icmph = (struct icmphdr) {
  184. .type = ICMP_DEST_UNREACH,
  185. .code = ICMP_FRAG_NEEDED,
  186. .checksum = 0,
  187. .un.frag.__unused = 0,
  188. .un.frag.mtu = ntohs(mtu),
  189. };
  190. icmph->checksum = ip_compute_csum(icmph, len);
  191. skb_reset_transport_header(skb);
  192. niph = skb_push(skb, sizeof(*niph));
  193. *niph = (struct iphdr) {
  194. .ihl = sizeof(*niph) / 4u,
  195. .version = 4,
  196. .tos = 0,
  197. .tot_len = htons(len + sizeof(*niph)),
  198. .id = 0,
  199. .frag_off = htons(IP_DF),
  200. .ttl = iph->ttl,
  201. .protocol = IPPROTO_ICMP,
  202. .saddr = iph->daddr,
  203. .daddr = iph->saddr,
  204. };
  205. ip_send_check(niph);
  206. skb_reset_network_header(skb);
  207. skb->ip_summed = CHECKSUM_NONE;
  208. eth_header(skb, skb->dev, htons(eh.h_proto), eh.h_source, eh.h_dest, 0);
  209. skb_reset_mac_header(skb);
  210. return skb->len;
  211. }
  212. /**
  213. * iptunnel_pmtud_check_icmp() - Trigger ICMP reply if needed and allowed
  214. * @skb: Buffer being sent by encapsulation, L2 headers expected
  215. * @mtu: Network MTU for path
  216. *
  217. * Return: 0 for no ICMP reply, length if built, negative value on error.
  218. */
  219. static int iptunnel_pmtud_check_icmp(struct sk_buff *skb, int mtu)
  220. {
  221. const struct icmphdr *icmph = icmp_hdr(skb);
  222. const struct iphdr *iph = ip_hdr(skb);
  223. if (mtu < 576 || iph->frag_off != htons(IP_DF))
  224. return 0;
  225. if (ipv4_is_lbcast(iph->daddr) || ipv4_is_multicast(iph->daddr) ||
  226. ipv4_is_zeronet(iph->saddr) || ipv4_is_loopback(iph->saddr) ||
  227. ipv4_is_lbcast(iph->saddr) || ipv4_is_multicast(iph->saddr))
  228. return 0;
  229. if (iph->protocol == IPPROTO_ICMP && icmp_is_err(icmph->type))
  230. return 0;
  231. return iptunnel_pmtud_build_icmp(skb, mtu);
  232. }
  233. #if IS_ENABLED(CONFIG_IPV6)
  234. /**
  235. * iptunnel_pmtud_build_icmpv6() - Build ICMPv6 error message for PMTUD
  236. * @skb: Original packet with L2 header
  237. * @mtu: MTU value for ICMPv6 error
  238. *
  239. * Return: length on success, negative error code if message couldn't be built.
  240. */
  241. static int iptunnel_pmtud_build_icmpv6(struct sk_buff *skb, int mtu)
  242. {
  243. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  244. struct icmp6hdr *icmp6h;
  245. struct ipv6hdr *nip6h;
  246. struct ethhdr eh;
  247. int len, err;
  248. __wsum csum;
  249. if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct ipv6hdr)))
  250. return -EINVAL;
  251. skb_copy_bits(skb, skb_mac_offset(skb), &eh, ETH_HLEN);
  252. pskb_pull(skb, ETH_HLEN);
  253. skb_reset_network_header(skb);
  254. err = pskb_trim(skb, IPV6_MIN_MTU - sizeof(*nip6h) - sizeof(*icmp6h));
  255. if (err)
  256. return err;
  257. len = skb->len + sizeof(*icmp6h);
  258. err = skb_cow(skb, sizeof(*nip6h) + sizeof(*icmp6h) + ETH_HLEN);
  259. if (err)
  260. return err;
  261. icmp6h = skb_push(skb, sizeof(*icmp6h));
  262. *icmp6h = (struct icmp6hdr) {
  263. .icmp6_type = ICMPV6_PKT_TOOBIG,
  264. .icmp6_code = 0,
  265. .icmp6_cksum = 0,
  266. .icmp6_mtu = htonl(mtu),
  267. };
  268. skb_reset_transport_header(skb);
  269. nip6h = skb_push(skb, sizeof(*nip6h));
  270. *nip6h = (struct ipv6hdr) {
  271. .priority = 0,
  272. .version = 6,
  273. .flow_lbl = { 0 },
  274. .payload_len = htons(len),
  275. .nexthdr = IPPROTO_ICMPV6,
  276. .hop_limit = ip6h->hop_limit,
  277. .saddr = ip6h->daddr,
  278. .daddr = ip6h->saddr,
  279. };
  280. skb_reset_network_header(skb);
  281. csum = csum_partial(icmp6h, len, 0);
  282. icmp6h->icmp6_cksum = csum_ipv6_magic(&nip6h->saddr, &nip6h->daddr, len,
  283. IPPROTO_ICMPV6, csum);
  284. skb->ip_summed = CHECKSUM_NONE;
  285. eth_header(skb, skb->dev, htons(eh.h_proto), eh.h_source, eh.h_dest, 0);
  286. skb_reset_mac_header(skb);
  287. return skb->len;
  288. }
  289. /**
  290. * iptunnel_pmtud_check_icmpv6() - Trigger ICMPv6 reply if needed and allowed
  291. * @skb: Buffer being sent by encapsulation, L2 headers expected
  292. * @mtu: Network MTU for path
  293. *
  294. * Return: 0 for no ICMPv6 reply, length if built, negative value on error.
  295. */
  296. static int iptunnel_pmtud_check_icmpv6(struct sk_buff *skb, int mtu)
  297. {
  298. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  299. int stype = ipv6_addr_type(&ip6h->saddr);
  300. u8 proto = ip6h->nexthdr;
  301. __be16 frag_off;
  302. int offset;
  303. if (mtu < IPV6_MIN_MTU)
  304. return 0;
  305. if (stype == IPV6_ADDR_ANY || stype == IPV6_ADDR_MULTICAST ||
  306. stype == IPV6_ADDR_LOOPBACK)
  307. return 0;
  308. offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &proto,
  309. &frag_off);
  310. if (offset < 0 || (frag_off & htons(~0x7)))
  311. return 0;
  312. if (proto == IPPROTO_ICMPV6) {
  313. struct icmp6hdr *icmp6h;
  314. if (!pskb_may_pull(skb, skb_network_header(skb) +
  315. offset + 1 - skb->data))
  316. return 0;
  317. icmp6h = (struct icmp6hdr *)(skb_network_header(skb) + offset);
  318. if (icmpv6_is_err(icmp6h->icmp6_type) ||
  319. icmp6h->icmp6_type == NDISC_REDIRECT)
  320. return 0;
  321. }
  322. return iptunnel_pmtud_build_icmpv6(skb, mtu);
  323. }
  324. #endif /* IS_ENABLED(CONFIG_IPV6) */
  325. /**
  326. * skb_tunnel_check_pmtu() - Check, update PMTU and trigger ICMP reply as needed
  327. * @skb: Buffer being sent by encapsulation, L2 headers expected
  328. * @encap_dst: Destination for tunnel encapsulation (outer IP)
  329. * @headroom: Encapsulation header size, bytes
  330. * @reply: Build matching ICMP or ICMPv6 message as a result
  331. *
  332. * L2 tunnel implementations that can carry IP and can be directly bridged
  333. * (currently UDP tunnels) can't always rely on IP forwarding paths to handle
  334. * PMTU discovery. In the bridged case, ICMP or ICMPv6 messages need to be built
  335. * based on payload and sent back by the encapsulation itself.
  336. *
  337. * For routable interfaces, we just need to update the PMTU for the destination.
  338. *
  339. * Return: 0 if ICMP error not needed, length if built, negative value on error
  340. */
  341. int skb_tunnel_check_pmtu(struct sk_buff *skb, struct dst_entry *encap_dst,
  342. int headroom, bool reply)
  343. {
  344. u32 mtu = dst_mtu(encap_dst) - headroom;
  345. if ((skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu)) ||
  346. (!skb_is_gso(skb) && (skb->len - skb_mac_header_len(skb)) <= mtu))
  347. return 0;
  348. skb_dst_update_pmtu_no_confirm(skb, mtu);
  349. if (!reply || skb->pkt_type == PACKET_HOST)
  350. return 0;
  351. if (skb->protocol == htons(ETH_P_IP))
  352. return iptunnel_pmtud_check_icmp(skb, mtu);
  353. #if IS_ENABLED(CONFIG_IPV6)
  354. if (skb->protocol == htons(ETH_P_IPV6))
  355. return iptunnel_pmtud_check_icmpv6(skb, mtu);
  356. #endif
  357. return 0;
  358. }
  359. EXPORT_SYMBOL(skb_tunnel_check_pmtu);
  360. /* Often modified stats are per cpu, other are shared (netdev->stats) */
  361. void ip_tunnel_get_stats64(struct net_device *dev,
  362. struct rtnl_link_stats64 *tot)
  363. {
  364. netdev_stats_to_stats64(tot, &dev->stats);
  365. dev_fetch_sw_netstats(tot, dev->tstats);
  366. }
  367. EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64);
  368. static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = {
  369. [LWTUNNEL_IP_UNSPEC] = { .strict_start_type = LWTUNNEL_IP_OPTS },
  370. [LWTUNNEL_IP_ID] = { .type = NLA_U64 },
  371. [LWTUNNEL_IP_DST] = { .type = NLA_U32 },
  372. [LWTUNNEL_IP_SRC] = { .type = NLA_U32 },
  373. [LWTUNNEL_IP_TTL] = { .type = NLA_U8 },
  374. [LWTUNNEL_IP_TOS] = { .type = NLA_U8 },
  375. [LWTUNNEL_IP_FLAGS] = { .type = NLA_U16 },
  376. [LWTUNNEL_IP_OPTS] = { .type = NLA_NESTED },
  377. };
  378. static const struct nla_policy ip_opts_policy[LWTUNNEL_IP_OPTS_MAX + 1] = {
  379. [LWTUNNEL_IP_OPTS_GENEVE] = { .type = NLA_NESTED },
  380. [LWTUNNEL_IP_OPTS_VXLAN] = { .type = NLA_NESTED },
  381. [LWTUNNEL_IP_OPTS_ERSPAN] = { .type = NLA_NESTED },
  382. };
  383. static const struct nla_policy
  384. geneve_opt_policy[LWTUNNEL_IP_OPT_GENEVE_MAX + 1] = {
  385. [LWTUNNEL_IP_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
  386. [LWTUNNEL_IP_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
  387. [LWTUNNEL_IP_OPT_GENEVE_DATA] = { .type = NLA_BINARY, .len = 128 },
  388. };
  389. static const struct nla_policy
  390. vxlan_opt_policy[LWTUNNEL_IP_OPT_VXLAN_MAX + 1] = {
  391. [LWTUNNEL_IP_OPT_VXLAN_GBP] = { .type = NLA_U32 },
  392. };
  393. static const struct nla_policy
  394. erspan_opt_policy[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1] = {
  395. [LWTUNNEL_IP_OPT_ERSPAN_VER] = { .type = NLA_U8 },
  396. [LWTUNNEL_IP_OPT_ERSPAN_INDEX] = { .type = NLA_U32 },
  397. [LWTUNNEL_IP_OPT_ERSPAN_DIR] = { .type = NLA_U8 },
  398. [LWTUNNEL_IP_OPT_ERSPAN_HWID] = { .type = NLA_U8 },
  399. };
  400. static int ip_tun_parse_opts_geneve(struct nlattr *attr,
  401. struct ip_tunnel_info *info, int opts_len,
  402. struct netlink_ext_ack *extack)
  403. {
  404. struct nlattr *tb[LWTUNNEL_IP_OPT_GENEVE_MAX + 1];
  405. int data_len, err;
  406. err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_GENEVE_MAX, attr,
  407. geneve_opt_policy, extack);
  408. if (err)
  409. return err;
  410. if (!tb[LWTUNNEL_IP_OPT_GENEVE_CLASS] ||
  411. !tb[LWTUNNEL_IP_OPT_GENEVE_TYPE] ||
  412. !tb[LWTUNNEL_IP_OPT_GENEVE_DATA])
  413. return -EINVAL;
  414. attr = tb[LWTUNNEL_IP_OPT_GENEVE_DATA];
  415. data_len = nla_len(attr);
  416. if (data_len % 4)
  417. return -EINVAL;
  418. if (info) {
  419. struct geneve_opt *opt = ip_tunnel_info_opts(info) + opts_len;
  420. memcpy(opt->opt_data, nla_data(attr), data_len);
  421. opt->length = data_len / 4;
  422. attr = tb[LWTUNNEL_IP_OPT_GENEVE_CLASS];
  423. opt->opt_class = nla_get_be16(attr);
  424. attr = tb[LWTUNNEL_IP_OPT_GENEVE_TYPE];
  425. opt->type = nla_get_u8(attr);
  426. info->key.tun_flags |= TUNNEL_GENEVE_OPT;
  427. }
  428. return sizeof(struct geneve_opt) + data_len;
  429. }
  430. static int ip_tun_parse_opts_vxlan(struct nlattr *attr,
  431. struct ip_tunnel_info *info, int opts_len,
  432. struct netlink_ext_ack *extack)
  433. {
  434. struct nlattr *tb[LWTUNNEL_IP_OPT_VXLAN_MAX + 1];
  435. int err;
  436. err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_VXLAN_MAX, attr,
  437. vxlan_opt_policy, extack);
  438. if (err)
  439. return err;
  440. if (!tb[LWTUNNEL_IP_OPT_VXLAN_GBP])
  441. return -EINVAL;
  442. if (info) {
  443. struct vxlan_metadata *md =
  444. ip_tunnel_info_opts(info) + opts_len;
  445. attr = tb[LWTUNNEL_IP_OPT_VXLAN_GBP];
  446. md->gbp = nla_get_u32(attr);
  447. md->gbp &= VXLAN_GBP_MASK;
  448. info->key.tun_flags |= TUNNEL_VXLAN_OPT;
  449. }
  450. return sizeof(struct vxlan_metadata);
  451. }
  452. static int ip_tun_parse_opts_erspan(struct nlattr *attr,
  453. struct ip_tunnel_info *info, int opts_len,
  454. struct netlink_ext_ack *extack)
  455. {
  456. struct nlattr *tb[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1];
  457. int err;
  458. u8 ver;
  459. err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_ERSPAN_MAX, attr,
  460. erspan_opt_policy, extack);
  461. if (err)
  462. return err;
  463. if (!tb[LWTUNNEL_IP_OPT_ERSPAN_VER])
  464. return -EINVAL;
  465. ver = nla_get_u8(tb[LWTUNNEL_IP_OPT_ERSPAN_VER]);
  466. if (ver == 1) {
  467. if (!tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX])
  468. return -EINVAL;
  469. } else if (ver == 2) {
  470. if (!tb[LWTUNNEL_IP_OPT_ERSPAN_DIR] ||
  471. !tb[LWTUNNEL_IP_OPT_ERSPAN_HWID])
  472. return -EINVAL;
  473. } else {
  474. return -EINVAL;
  475. }
  476. if (info) {
  477. struct erspan_metadata *md =
  478. ip_tunnel_info_opts(info) + opts_len;
  479. md->version = ver;
  480. if (ver == 1) {
  481. attr = tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX];
  482. md->u.index = nla_get_be32(attr);
  483. } else {
  484. attr = tb[LWTUNNEL_IP_OPT_ERSPAN_DIR];
  485. md->u.md2.dir = nla_get_u8(attr);
  486. attr = tb[LWTUNNEL_IP_OPT_ERSPAN_HWID];
  487. set_hwid(&md->u.md2, nla_get_u8(attr));
  488. }
  489. info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
  490. }
  491. return sizeof(struct erspan_metadata);
  492. }
  493. static int ip_tun_parse_opts(struct nlattr *attr, struct ip_tunnel_info *info,
  494. struct netlink_ext_ack *extack)
  495. {
  496. int err, rem, opt_len, opts_len = 0, type = 0;
  497. struct nlattr *nla;
  498. if (!attr)
  499. return 0;
  500. err = nla_validate(nla_data(attr), nla_len(attr), LWTUNNEL_IP_OPTS_MAX,
  501. ip_opts_policy, extack);
  502. if (err)
  503. return err;
  504. nla_for_each_attr(nla, nla_data(attr), nla_len(attr), rem) {
  505. switch (nla_type(nla)) {
  506. case LWTUNNEL_IP_OPTS_GENEVE:
  507. if (type && type != TUNNEL_GENEVE_OPT)
  508. return -EINVAL;
  509. opt_len = ip_tun_parse_opts_geneve(nla, info, opts_len,
  510. extack);
  511. if (opt_len < 0)
  512. return opt_len;
  513. opts_len += opt_len;
  514. if (opts_len > IP_TUNNEL_OPTS_MAX)
  515. return -EINVAL;
  516. type = TUNNEL_GENEVE_OPT;
  517. break;
  518. case LWTUNNEL_IP_OPTS_VXLAN:
  519. if (type)
  520. return -EINVAL;
  521. opt_len = ip_tun_parse_opts_vxlan(nla, info, opts_len,
  522. extack);
  523. if (opt_len < 0)
  524. return opt_len;
  525. opts_len += opt_len;
  526. type = TUNNEL_VXLAN_OPT;
  527. break;
  528. case LWTUNNEL_IP_OPTS_ERSPAN:
  529. if (type)
  530. return -EINVAL;
  531. opt_len = ip_tun_parse_opts_erspan(nla, info, opts_len,
  532. extack);
  533. if (opt_len < 0)
  534. return opt_len;
  535. opts_len += opt_len;
  536. type = TUNNEL_ERSPAN_OPT;
  537. break;
  538. default:
  539. return -EINVAL;
  540. }
  541. }
  542. return opts_len;
  543. }
  544. static int ip_tun_get_optlen(struct nlattr *attr,
  545. struct netlink_ext_ack *extack)
  546. {
  547. return ip_tun_parse_opts(attr, NULL, extack);
  548. }
  549. static int ip_tun_set_opts(struct nlattr *attr, struct ip_tunnel_info *info,
  550. struct netlink_ext_ack *extack)
  551. {
  552. return ip_tun_parse_opts(attr, info, extack);
  553. }
  554. static int ip_tun_build_state(struct net *net, struct nlattr *attr,
  555. unsigned int family, const void *cfg,
  556. struct lwtunnel_state **ts,
  557. struct netlink_ext_ack *extack)
  558. {
  559. struct nlattr *tb[LWTUNNEL_IP_MAX + 1];
  560. struct lwtunnel_state *new_state;
  561. struct ip_tunnel_info *tun_info;
  562. int err, opt_len;
  563. err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_MAX, attr,
  564. ip_tun_policy, extack);
  565. if (err < 0)
  566. return err;
  567. opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP_OPTS], extack);
  568. if (opt_len < 0)
  569. return opt_len;
  570. new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
  571. if (!new_state)
  572. return -ENOMEM;
  573. new_state->type = LWTUNNEL_ENCAP_IP;
  574. tun_info = lwt_tun_info(new_state);
  575. err = ip_tun_set_opts(tb[LWTUNNEL_IP_OPTS], tun_info, extack);
  576. if (err < 0) {
  577. lwtstate_free(new_state);
  578. return err;
  579. }
  580. #ifdef CONFIG_DST_CACHE
  581. err = dst_cache_init(&tun_info->dst_cache, GFP_KERNEL);
  582. if (err) {
  583. lwtstate_free(new_state);
  584. return err;
  585. }
  586. #endif
  587. if (tb[LWTUNNEL_IP_ID])
  588. tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP_ID]);
  589. if (tb[LWTUNNEL_IP_DST])
  590. tun_info->key.u.ipv4.dst = nla_get_in_addr(tb[LWTUNNEL_IP_DST]);
  591. if (tb[LWTUNNEL_IP_SRC])
  592. tun_info->key.u.ipv4.src = nla_get_in_addr(tb[LWTUNNEL_IP_SRC]);
  593. if (tb[LWTUNNEL_IP_TTL])
  594. tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]);
  595. if (tb[LWTUNNEL_IP_TOS])
  596. tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]);
  597. if (tb[LWTUNNEL_IP_FLAGS])
  598. tun_info->key.tun_flags |=
  599. (nla_get_be16(tb[LWTUNNEL_IP_FLAGS]) &
  600. ~TUNNEL_OPTIONS_PRESENT);
  601. tun_info->mode = IP_TUNNEL_INFO_TX;
  602. tun_info->options_len = opt_len;
  603. *ts = new_state;
  604. return 0;
  605. }
  606. static void ip_tun_destroy_state(struct lwtunnel_state *lwtstate)
  607. {
  608. #ifdef CONFIG_DST_CACHE
  609. struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
  610. dst_cache_destroy(&tun_info->dst_cache);
  611. #endif
  612. }
  613. static int ip_tun_fill_encap_opts_geneve(struct sk_buff *skb,
  614. struct ip_tunnel_info *tun_info)
  615. {
  616. struct geneve_opt *opt;
  617. struct nlattr *nest;
  618. int offset = 0;
  619. nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_GENEVE);
  620. if (!nest)
  621. return -ENOMEM;
  622. while (tun_info->options_len > offset) {
  623. opt = ip_tunnel_info_opts(tun_info) + offset;
  624. if (nla_put_be16(skb, LWTUNNEL_IP_OPT_GENEVE_CLASS,
  625. opt->opt_class) ||
  626. nla_put_u8(skb, LWTUNNEL_IP_OPT_GENEVE_TYPE, opt->type) ||
  627. nla_put(skb, LWTUNNEL_IP_OPT_GENEVE_DATA, opt->length * 4,
  628. opt->opt_data)) {
  629. nla_nest_cancel(skb, nest);
  630. return -ENOMEM;
  631. }
  632. offset += sizeof(*opt) + opt->length * 4;
  633. }
  634. nla_nest_end(skb, nest);
  635. return 0;
  636. }
  637. static int ip_tun_fill_encap_opts_vxlan(struct sk_buff *skb,
  638. struct ip_tunnel_info *tun_info)
  639. {
  640. struct vxlan_metadata *md;
  641. struct nlattr *nest;
  642. nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_VXLAN);
  643. if (!nest)
  644. return -ENOMEM;
  645. md = ip_tunnel_info_opts(tun_info);
  646. if (nla_put_u32(skb, LWTUNNEL_IP_OPT_VXLAN_GBP, md->gbp)) {
  647. nla_nest_cancel(skb, nest);
  648. return -ENOMEM;
  649. }
  650. nla_nest_end(skb, nest);
  651. return 0;
  652. }
  653. static int ip_tun_fill_encap_opts_erspan(struct sk_buff *skb,
  654. struct ip_tunnel_info *tun_info)
  655. {
  656. struct erspan_metadata *md;
  657. struct nlattr *nest;
  658. nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_ERSPAN);
  659. if (!nest)
  660. return -ENOMEM;
  661. md = ip_tunnel_info_opts(tun_info);
  662. if (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_VER, md->version))
  663. goto err;
  664. if (md->version == 1 &&
  665. nla_put_be32(skb, LWTUNNEL_IP_OPT_ERSPAN_INDEX, md->u.index))
  666. goto err;
  667. if (md->version == 2 &&
  668. (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_DIR, md->u.md2.dir) ||
  669. nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_HWID,
  670. get_hwid(&md->u.md2))))
  671. goto err;
  672. nla_nest_end(skb, nest);
  673. return 0;
  674. err:
  675. nla_nest_cancel(skb, nest);
  676. return -ENOMEM;
  677. }
  678. static int ip_tun_fill_encap_opts(struct sk_buff *skb, int type,
  679. struct ip_tunnel_info *tun_info)
  680. {
  681. struct nlattr *nest;
  682. int err = 0;
  683. if (!(tun_info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))
  684. return 0;
  685. nest = nla_nest_start_noflag(skb, type);
  686. if (!nest)
  687. return -ENOMEM;
  688. if (tun_info->key.tun_flags & TUNNEL_GENEVE_OPT)
  689. err = ip_tun_fill_encap_opts_geneve(skb, tun_info);
  690. else if (tun_info->key.tun_flags & TUNNEL_VXLAN_OPT)
  691. err = ip_tun_fill_encap_opts_vxlan(skb, tun_info);
  692. else if (tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT)
  693. err = ip_tun_fill_encap_opts_erspan(skb, tun_info);
  694. if (err) {
  695. nla_nest_cancel(skb, nest);
  696. return err;
  697. }
  698. nla_nest_end(skb, nest);
  699. return 0;
  700. }
  701. static int ip_tun_fill_encap_info(struct sk_buff *skb,
  702. struct lwtunnel_state *lwtstate)
  703. {
  704. struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
  705. if (nla_put_be64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id,
  706. LWTUNNEL_IP_PAD) ||
  707. nla_put_in_addr(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) ||
  708. nla_put_in_addr(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) ||
  709. nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) ||
  710. nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) ||
  711. nla_put_be16(skb, LWTUNNEL_IP_FLAGS, tun_info->key.tun_flags) ||
  712. ip_tun_fill_encap_opts(skb, LWTUNNEL_IP_OPTS, tun_info))
  713. return -ENOMEM;
  714. return 0;
  715. }
  716. static int ip_tun_opts_nlsize(struct ip_tunnel_info *info)
  717. {
  718. int opt_len;
  719. if (!(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))
  720. return 0;
  721. opt_len = nla_total_size(0); /* LWTUNNEL_IP_OPTS */
  722. if (info->key.tun_flags & TUNNEL_GENEVE_OPT) {
  723. struct geneve_opt *opt;
  724. int offset = 0;
  725. opt_len += nla_total_size(0); /* LWTUNNEL_IP_OPTS_GENEVE */
  726. while (info->options_len > offset) {
  727. opt = ip_tunnel_info_opts(info) + offset;
  728. opt_len += nla_total_size(2) /* OPT_GENEVE_CLASS */
  729. + nla_total_size(1) /* OPT_GENEVE_TYPE */
  730. + nla_total_size(opt->length * 4);
  731. /* OPT_GENEVE_DATA */
  732. offset += sizeof(*opt) + opt->length * 4;
  733. }
  734. } else if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
  735. opt_len += nla_total_size(0) /* LWTUNNEL_IP_OPTS_VXLAN */
  736. + nla_total_size(4); /* OPT_VXLAN_GBP */
  737. } else if (info->key.tun_flags & TUNNEL_ERSPAN_OPT) {
  738. struct erspan_metadata *md = ip_tunnel_info_opts(info);
  739. opt_len += nla_total_size(0) /* LWTUNNEL_IP_OPTS_ERSPAN */
  740. + nla_total_size(1) /* OPT_ERSPAN_VER */
  741. + (md->version == 1 ? nla_total_size(4)
  742. /* OPT_ERSPAN_INDEX (v1) */
  743. : nla_total_size(1) +
  744. nla_total_size(1));
  745. /* OPT_ERSPAN_DIR + HWID (v2) */
  746. }
  747. return opt_len;
  748. }
  749. static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
  750. {
  751. return nla_total_size_64bit(8) /* LWTUNNEL_IP_ID */
  752. + nla_total_size(4) /* LWTUNNEL_IP_DST */
  753. + nla_total_size(4) /* LWTUNNEL_IP_SRC */
  754. + nla_total_size(1) /* LWTUNNEL_IP_TOS */
  755. + nla_total_size(1) /* LWTUNNEL_IP_TTL */
  756. + nla_total_size(2) /* LWTUNNEL_IP_FLAGS */
  757. + ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
  758. /* LWTUNNEL_IP_OPTS */
  759. }
  760. static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
  761. {
  762. struct ip_tunnel_info *info_a = lwt_tun_info(a);
  763. struct ip_tunnel_info *info_b = lwt_tun_info(b);
  764. return memcmp(info_a, info_b, sizeof(info_a->key)) ||
  765. info_a->mode != info_b->mode ||
  766. info_a->options_len != info_b->options_len ||
  767. memcmp(ip_tunnel_info_opts(info_a),
  768. ip_tunnel_info_opts(info_b), info_a->options_len);
  769. }
  770. static const struct lwtunnel_encap_ops ip_tun_lwt_ops = {
  771. .build_state = ip_tun_build_state,
  772. .destroy_state = ip_tun_destroy_state,
  773. .fill_encap = ip_tun_fill_encap_info,
  774. .get_encap_size = ip_tun_encap_nlsize,
  775. .cmp_encap = ip_tun_cmp_encap,
  776. .owner = THIS_MODULE,
  777. };
  778. static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = {
  779. [LWTUNNEL_IP6_UNSPEC] = { .strict_start_type = LWTUNNEL_IP6_OPTS },
  780. [LWTUNNEL_IP6_ID] = { .type = NLA_U64 },
  781. [LWTUNNEL_IP6_DST] = { .len = sizeof(struct in6_addr) },
  782. [LWTUNNEL_IP6_SRC] = { .len = sizeof(struct in6_addr) },
  783. [LWTUNNEL_IP6_HOPLIMIT] = { .type = NLA_U8 },
  784. [LWTUNNEL_IP6_TC] = { .type = NLA_U8 },
  785. [LWTUNNEL_IP6_FLAGS] = { .type = NLA_U16 },
  786. [LWTUNNEL_IP6_OPTS] = { .type = NLA_NESTED },
  787. };
  788. static int ip6_tun_build_state(struct net *net, struct nlattr *attr,
  789. unsigned int family, const void *cfg,
  790. struct lwtunnel_state **ts,
  791. struct netlink_ext_ack *extack)
  792. {
  793. struct nlattr *tb[LWTUNNEL_IP6_MAX + 1];
  794. struct lwtunnel_state *new_state;
  795. struct ip_tunnel_info *tun_info;
  796. int err, opt_len;
  797. err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP6_MAX, attr,
  798. ip6_tun_policy, extack);
  799. if (err < 0)
  800. return err;
  801. opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP6_OPTS], extack);
  802. if (opt_len < 0)
  803. return opt_len;
  804. new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
  805. if (!new_state)
  806. return -ENOMEM;
  807. new_state->type = LWTUNNEL_ENCAP_IP6;
  808. tun_info = lwt_tun_info(new_state);
  809. err = ip_tun_set_opts(tb[LWTUNNEL_IP6_OPTS], tun_info, extack);
  810. if (err < 0) {
  811. lwtstate_free(new_state);
  812. return err;
  813. }
  814. if (tb[LWTUNNEL_IP6_ID])
  815. tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP6_ID]);
  816. if (tb[LWTUNNEL_IP6_DST])
  817. tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]);
  818. if (tb[LWTUNNEL_IP6_SRC])
  819. tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]);
  820. if (tb[LWTUNNEL_IP6_HOPLIMIT])
  821. tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]);
  822. if (tb[LWTUNNEL_IP6_TC])
  823. tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]);
  824. if (tb[LWTUNNEL_IP6_FLAGS])
  825. tun_info->key.tun_flags |=
  826. (nla_get_be16(tb[LWTUNNEL_IP6_FLAGS]) &
  827. ~TUNNEL_OPTIONS_PRESENT);
  828. tun_info->mode = IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_IPV6;
  829. tun_info->options_len = opt_len;
  830. *ts = new_state;
  831. return 0;
  832. }
  833. static int ip6_tun_fill_encap_info(struct sk_buff *skb,
  834. struct lwtunnel_state *lwtstate)
  835. {
  836. struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
  837. if (nla_put_be64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id,
  838. LWTUNNEL_IP6_PAD) ||
  839. nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) ||
  840. nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) ||
  841. nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.tos) ||
  842. nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.ttl) ||
  843. nla_put_be16(skb, LWTUNNEL_IP6_FLAGS, tun_info->key.tun_flags) ||
  844. ip_tun_fill_encap_opts(skb, LWTUNNEL_IP6_OPTS, tun_info))
  845. return -ENOMEM;
  846. return 0;
  847. }
  848. static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
  849. {
  850. return nla_total_size_64bit(8) /* LWTUNNEL_IP6_ID */
  851. + nla_total_size(16) /* LWTUNNEL_IP6_DST */
  852. + nla_total_size(16) /* LWTUNNEL_IP6_SRC */
  853. + nla_total_size(1) /* LWTUNNEL_IP6_HOPLIMIT */
  854. + nla_total_size(1) /* LWTUNNEL_IP6_TC */
  855. + nla_total_size(2) /* LWTUNNEL_IP6_FLAGS */
  856. + ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
  857. /* LWTUNNEL_IP6_OPTS */
  858. }
  859. static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = {
  860. .build_state = ip6_tun_build_state,
  861. .fill_encap = ip6_tun_fill_encap_info,
  862. .get_encap_size = ip6_tun_encap_nlsize,
  863. .cmp_encap = ip_tun_cmp_encap,
  864. .owner = THIS_MODULE,
  865. };
  866. void __init ip_tunnel_core_init(void)
  867. {
  868. /* If you land here, make sure whether increasing ip_tunnel_info's
  869. * options_len is a reasonable choice with its usage in front ends
  870. * (f.e., it's part of flow keys, etc).
  871. */
  872. BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255);
  873. lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
  874. lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6);
  875. }
  876. DEFINE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt);
  877. EXPORT_SYMBOL(ip_tunnel_metadata_cnt);
  878. void ip_tunnel_need_metadata(void)
  879. {
  880. static_branch_inc(&ip_tunnel_metadata_cnt);
  881. }
  882. EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata);
  883. void ip_tunnel_unneed_metadata(void)
  884. {
  885. static_branch_dec(&ip_tunnel_metadata_cnt);
  886. }
  887. EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata);
  888. /* Returns either the correct skb->protocol value, or 0 if invalid. */
  889. __be16 ip_tunnel_parse_protocol(const struct sk_buff *skb)
  890. {
  891. if (skb_network_header(skb) >= skb->head &&
  892. (skb_network_header(skb) + sizeof(struct iphdr)) <= skb_tail_pointer(skb) &&
  893. ip_hdr(skb)->version == 4)
  894. return htons(ETH_P_IP);
  895. if (skb_network_header(skb) >= skb->head &&
  896. (skb_network_header(skb) + sizeof(struct ipv6hdr)) <= skb_tail_pointer(skb) &&
  897. ipv6_hdr(skb)->version == 6)
  898. return htons(ETH_P_IPV6);
  899. return 0;
  900. }
  901. EXPORT_SYMBOL(ip_tunnel_parse_protocol);
  902. const struct header_ops ip_tunnel_header_ops = { .parse_protocol = ip_tunnel_parse_protocol };
  903. EXPORT_SYMBOL(ip_tunnel_header_ops);