seg6_iptunnel.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SR-IPv6 implementation
  4. *
  5. * Author:
  6. * David Lebrun <[email protected]>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/net.h>
  11. #include <linux/module.h>
  12. #include <net/ip.h>
  13. #include <net/ip_tunnels.h>
  14. #include <net/lwtunnel.h>
  15. #include <net/netevent.h>
  16. #include <net/netns/generic.h>
  17. #include <net/ip6_fib.h>
  18. #include <net/route.h>
  19. #include <net/seg6.h>
  20. #include <linux/seg6.h>
  21. #include <linux/seg6_iptunnel.h>
  22. #include <net/addrconf.h>
  23. #include <net/ip6_route.h>
  24. #include <net/dst_cache.h>
  25. #ifdef CONFIG_IPV6_SEG6_HMAC
  26. #include <net/seg6_hmac.h>
  27. #endif
  28. #include <linux/netfilter.h>
  29. static size_t seg6_lwt_headroom(struct seg6_iptunnel_encap *tuninfo)
  30. {
  31. int head = 0;
  32. switch (tuninfo->mode) {
  33. case SEG6_IPTUN_MODE_INLINE:
  34. break;
  35. case SEG6_IPTUN_MODE_ENCAP:
  36. case SEG6_IPTUN_MODE_ENCAP_RED:
  37. head = sizeof(struct ipv6hdr);
  38. break;
  39. case SEG6_IPTUN_MODE_L2ENCAP:
  40. case SEG6_IPTUN_MODE_L2ENCAP_RED:
  41. return 0;
  42. }
  43. return ((tuninfo->srh->hdrlen + 1) << 3) + head;
  44. }
  45. struct seg6_lwt {
  46. struct dst_cache cache;
  47. struct seg6_iptunnel_encap tuninfo[];
  48. };
  49. static inline struct seg6_lwt *seg6_lwt_lwtunnel(struct lwtunnel_state *lwt)
  50. {
  51. return (struct seg6_lwt *)lwt->data;
  52. }
  53. static inline struct seg6_iptunnel_encap *
  54. seg6_encap_lwtunnel(struct lwtunnel_state *lwt)
  55. {
  56. return seg6_lwt_lwtunnel(lwt)->tuninfo;
  57. }
  58. static const struct nla_policy seg6_iptunnel_policy[SEG6_IPTUNNEL_MAX + 1] = {
  59. [SEG6_IPTUNNEL_SRH] = { .type = NLA_BINARY },
  60. };
  61. static int nla_put_srh(struct sk_buff *skb, int attrtype,
  62. struct seg6_iptunnel_encap *tuninfo)
  63. {
  64. struct seg6_iptunnel_encap *data;
  65. struct nlattr *nla;
  66. int len;
  67. len = SEG6_IPTUN_ENCAP_SIZE(tuninfo);
  68. nla = nla_reserve(skb, attrtype, len);
  69. if (!nla)
  70. return -EMSGSIZE;
  71. data = nla_data(nla);
  72. memcpy(data, tuninfo, len);
  73. return 0;
  74. }
  75. static void set_tun_src(struct net *net, struct net_device *dev,
  76. struct in6_addr *daddr, struct in6_addr *saddr)
  77. {
  78. struct seg6_pernet_data *sdata = seg6_pernet(net);
  79. struct in6_addr *tun_src;
  80. rcu_read_lock();
  81. tun_src = rcu_dereference(sdata->tun_src);
  82. if (!ipv6_addr_any(tun_src)) {
  83. memcpy(saddr, tun_src, sizeof(struct in6_addr));
  84. } else {
  85. ipv6_dev_get_saddr(net, dev, daddr, IPV6_PREFER_SRC_PUBLIC,
  86. saddr);
  87. }
  88. rcu_read_unlock();
  89. }
  90. /* Compute flowlabel for outer IPv6 header */
  91. static __be32 seg6_make_flowlabel(struct net *net, struct sk_buff *skb,
  92. struct ipv6hdr *inner_hdr)
  93. {
  94. int do_flowlabel = net->ipv6.sysctl.seg6_flowlabel;
  95. __be32 flowlabel = 0;
  96. u32 hash;
  97. if (do_flowlabel > 0) {
  98. hash = skb_get_hash(skb);
  99. hash = rol32(hash, 16);
  100. flowlabel = (__force __be32)hash & IPV6_FLOWLABEL_MASK;
  101. } else if (!do_flowlabel && skb->protocol == htons(ETH_P_IPV6)) {
  102. flowlabel = ip6_flowlabel(inner_hdr);
  103. }
  104. return flowlabel;
  105. }
  106. /* encapsulate an IPv6 packet within an outer IPv6 header with a given SRH */
  107. int seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_sr_hdr *osrh, int proto)
  108. {
  109. struct dst_entry *dst = skb_dst(skb);
  110. struct net *net = dev_net(dst->dev);
  111. struct ipv6hdr *hdr, *inner_hdr;
  112. struct ipv6_sr_hdr *isrh;
  113. int hdrlen, tot_len, err;
  114. __be32 flowlabel;
  115. hdrlen = (osrh->hdrlen + 1) << 3;
  116. tot_len = hdrlen + sizeof(*hdr);
  117. err = skb_cow_head(skb, tot_len + skb->mac_len);
  118. if (unlikely(err))
  119. return err;
  120. inner_hdr = ipv6_hdr(skb);
  121. flowlabel = seg6_make_flowlabel(net, skb, inner_hdr);
  122. skb_push(skb, tot_len);
  123. skb_reset_network_header(skb);
  124. skb_mac_header_rebuild(skb);
  125. hdr = ipv6_hdr(skb);
  126. /* inherit tc, flowlabel and hlim
  127. * hlim will be decremented in ip6_forward() afterwards and
  128. * decapsulation will overwrite inner hlim with outer hlim
  129. */
  130. if (skb->protocol == htons(ETH_P_IPV6)) {
  131. ip6_flow_hdr(hdr, ip6_tclass(ip6_flowinfo(inner_hdr)),
  132. flowlabel);
  133. hdr->hop_limit = inner_hdr->hop_limit;
  134. } else {
  135. ip6_flow_hdr(hdr, 0, flowlabel);
  136. hdr->hop_limit = ip6_dst_hoplimit(skb_dst(skb));
  137. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  138. /* the control block has been erased, so we have to set the
  139. * iif once again.
  140. * We read the receiving interface index directly from the
  141. * skb->skb_iif as it is done in the IPv4 receiving path (i.e.:
  142. * ip_rcv_core(...)).
  143. */
  144. IP6CB(skb)->iif = skb->skb_iif;
  145. }
  146. hdr->nexthdr = NEXTHDR_ROUTING;
  147. isrh = (void *)hdr + sizeof(*hdr);
  148. memcpy(isrh, osrh, hdrlen);
  149. isrh->nexthdr = proto;
  150. hdr->daddr = isrh->segments[isrh->first_segment];
  151. set_tun_src(net, dst->dev, &hdr->daddr, &hdr->saddr);
  152. #ifdef CONFIG_IPV6_SEG6_HMAC
  153. if (sr_has_hmac(isrh)) {
  154. err = seg6_push_hmac(net, &hdr->saddr, isrh);
  155. if (unlikely(err))
  156. return err;
  157. }
  158. #endif
  159. hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  160. skb_postpush_rcsum(skb, hdr, tot_len);
  161. return 0;
  162. }
  163. EXPORT_SYMBOL_GPL(seg6_do_srh_encap);
  164. /* encapsulate an IPv6 packet within an outer IPv6 header with reduced SRH */
  165. static int seg6_do_srh_encap_red(struct sk_buff *skb,
  166. struct ipv6_sr_hdr *osrh, int proto)
  167. {
  168. __u8 first_seg = osrh->first_segment;
  169. struct dst_entry *dst = skb_dst(skb);
  170. struct net *net = dev_net(dst->dev);
  171. struct ipv6hdr *hdr, *inner_hdr;
  172. int hdrlen = ipv6_optlen(osrh);
  173. int red_tlv_offset, tlv_offset;
  174. struct ipv6_sr_hdr *isrh;
  175. bool skip_srh = false;
  176. __be32 flowlabel;
  177. int tot_len, err;
  178. int red_hdrlen;
  179. int tlvs_len;
  180. if (first_seg > 0) {
  181. red_hdrlen = hdrlen - sizeof(struct in6_addr);
  182. } else {
  183. /* NOTE: if tag/flags and/or other TLVs are introduced in the
  184. * seg6_iptunnel infrastructure, they should be considered when
  185. * deciding to skip the SRH.
  186. */
  187. skip_srh = !sr_has_hmac(osrh);
  188. red_hdrlen = skip_srh ? 0 : hdrlen;
  189. }
  190. tot_len = red_hdrlen + sizeof(struct ipv6hdr);
  191. err = skb_cow_head(skb, tot_len + skb->mac_len);
  192. if (unlikely(err))
  193. return err;
  194. inner_hdr = ipv6_hdr(skb);
  195. flowlabel = seg6_make_flowlabel(net, skb, inner_hdr);
  196. skb_push(skb, tot_len);
  197. skb_reset_network_header(skb);
  198. skb_mac_header_rebuild(skb);
  199. hdr = ipv6_hdr(skb);
  200. /* based on seg6_do_srh_encap() */
  201. if (skb->protocol == htons(ETH_P_IPV6)) {
  202. ip6_flow_hdr(hdr, ip6_tclass(ip6_flowinfo(inner_hdr)),
  203. flowlabel);
  204. hdr->hop_limit = inner_hdr->hop_limit;
  205. } else {
  206. ip6_flow_hdr(hdr, 0, flowlabel);
  207. hdr->hop_limit = ip6_dst_hoplimit(skb_dst(skb));
  208. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  209. IP6CB(skb)->iif = skb->skb_iif;
  210. }
  211. /* no matter if we have to skip the SRH or not, the first segment
  212. * always comes in the pushed IPv6 header.
  213. */
  214. hdr->daddr = osrh->segments[first_seg];
  215. if (skip_srh) {
  216. hdr->nexthdr = proto;
  217. set_tun_src(net, dst->dev, &hdr->daddr, &hdr->saddr);
  218. goto out;
  219. }
  220. /* we cannot skip the SRH, slow path */
  221. hdr->nexthdr = NEXTHDR_ROUTING;
  222. isrh = (void *)hdr + sizeof(struct ipv6hdr);
  223. if (unlikely(!first_seg)) {
  224. /* this is a very rare case; we have only one SID but
  225. * we cannot skip the SRH since we are carrying some
  226. * other info.
  227. */
  228. memcpy(isrh, osrh, hdrlen);
  229. goto srcaddr;
  230. }
  231. tlv_offset = sizeof(*osrh) + (first_seg + 1) * sizeof(struct in6_addr);
  232. red_tlv_offset = tlv_offset - sizeof(struct in6_addr);
  233. memcpy(isrh, osrh, red_tlv_offset);
  234. tlvs_len = hdrlen - tlv_offset;
  235. if (unlikely(tlvs_len > 0)) {
  236. const void *s = (const void *)osrh + tlv_offset;
  237. void *d = (void *)isrh + red_tlv_offset;
  238. memcpy(d, s, tlvs_len);
  239. }
  240. --isrh->first_segment;
  241. isrh->hdrlen -= 2;
  242. srcaddr:
  243. isrh->nexthdr = proto;
  244. set_tun_src(net, dst->dev, &hdr->daddr, &hdr->saddr);
  245. #ifdef CONFIG_IPV6_SEG6_HMAC
  246. if (unlikely(!skip_srh && sr_has_hmac(isrh))) {
  247. err = seg6_push_hmac(net, &hdr->saddr, isrh);
  248. if (unlikely(err))
  249. return err;
  250. }
  251. #endif
  252. out:
  253. hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  254. skb_postpush_rcsum(skb, hdr, tot_len);
  255. return 0;
  256. }
  257. /* insert an SRH within an IPv6 packet, just after the IPv6 header */
  258. int seg6_do_srh_inline(struct sk_buff *skb, struct ipv6_sr_hdr *osrh)
  259. {
  260. struct ipv6hdr *hdr, *oldhdr;
  261. struct ipv6_sr_hdr *isrh;
  262. int hdrlen, err;
  263. hdrlen = (osrh->hdrlen + 1) << 3;
  264. err = skb_cow_head(skb, hdrlen + skb->mac_len);
  265. if (unlikely(err))
  266. return err;
  267. oldhdr = ipv6_hdr(skb);
  268. skb_pull(skb, sizeof(struct ipv6hdr));
  269. skb_postpull_rcsum(skb, skb_network_header(skb),
  270. sizeof(struct ipv6hdr));
  271. skb_push(skb, sizeof(struct ipv6hdr) + hdrlen);
  272. skb_reset_network_header(skb);
  273. skb_mac_header_rebuild(skb);
  274. hdr = ipv6_hdr(skb);
  275. memmove(hdr, oldhdr, sizeof(*hdr));
  276. isrh = (void *)hdr + sizeof(*hdr);
  277. memcpy(isrh, osrh, hdrlen);
  278. isrh->nexthdr = hdr->nexthdr;
  279. hdr->nexthdr = NEXTHDR_ROUTING;
  280. isrh->segments[0] = hdr->daddr;
  281. hdr->daddr = isrh->segments[isrh->first_segment];
  282. #ifdef CONFIG_IPV6_SEG6_HMAC
  283. if (sr_has_hmac(isrh)) {
  284. struct net *net = dev_net(skb_dst(skb)->dev);
  285. err = seg6_push_hmac(net, &hdr->saddr, isrh);
  286. if (unlikely(err))
  287. return err;
  288. }
  289. #endif
  290. hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  291. skb_postpush_rcsum(skb, hdr, sizeof(struct ipv6hdr) + hdrlen);
  292. return 0;
  293. }
  294. EXPORT_SYMBOL_GPL(seg6_do_srh_inline);
  295. static int seg6_do_srh(struct sk_buff *skb)
  296. {
  297. struct dst_entry *dst = skb_dst(skb);
  298. struct seg6_iptunnel_encap *tinfo;
  299. int proto, err = 0;
  300. tinfo = seg6_encap_lwtunnel(dst->lwtstate);
  301. switch (tinfo->mode) {
  302. case SEG6_IPTUN_MODE_INLINE:
  303. if (skb->protocol != htons(ETH_P_IPV6))
  304. return -EINVAL;
  305. err = seg6_do_srh_inline(skb, tinfo->srh);
  306. if (err)
  307. return err;
  308. break;
  309. case SEG6_IPTUN_MODE_ENCAP:
  310. case SEG6_IPTUN_MODE_ENCAP_RED:
  311. err = iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6);
  312. if (err)
  313. return err;
  314. if (skb->protocol == htons(ETH_P_IPV6))
  315. proto = IPPROTO_IPV6;
  316. else if (skb->protocol == htons(ETH_P_IP))
  317. proto = IPPROTO_IPIP;
  318. else
  319. return -EINVAL;
  320. if (tinfo->mode == SEG6_IPTUN_MODE_ENCAP)
  321. err = seg6_do_srh_encap(skb, tinfo->srh, proto);
  322. else
  323. err = seg6_do_srh_encap_red(skb, tinfo->srh, proto);
  324. if (err)
  325. return err;
  326. skb_set_inner_transport_header(skb, skb_transport_offset(skb));
  327. skb_set_inner_protocol(skb, skb->protocol);
  328. skb->protocol = htons(ETH_P_IPV6);
  329. break;
  330. case SEG6_IPTUN_MODE_L2ENCAP:
  331. case SEG6_IPTUN_MODE_L2ENCAP_RED:
  332. if (!skb_mac_header_was_set(skb))
  333. return -EINVAL;
  334. if (pskb_expand_head(skb, skb->mac_len, 0, GFP_ATOMIC) < 0)
  335. return -ENOMEM;
  336. skb_mac_header_rebuild(skb);
  337. skb_push(skb, skb->mac_len);
  338. if (tinfo->mode == SEG6_IPTUN_MODE_L2ENCAP)
  339. err = seg6_do_srh_encap(skb, tinfo->srh,
  340. IPPROTO_ETHERNET);
  341. else
  342. err = seg6_do_srh_encap_red(skb, tinfo->srh,
  343. IPPROTO_ETHERNET);
  344. if (err)
  345. return err;
  346. skb->protocol = htons(ETH_P_IPV6);
  347. break;
  348. }
  349. skb_set_transport_header(skb, sizeof(struct ipv6hdr));
  350. nf_reset_ct(skb);
  351. return 0;
  352. }
  353. static int seg6_input_finish(struct net *net, struct sock *sk,
  354. struct sk_buff *skb)
  355. {
  356. return dst_input(skb);
  357. }
  358. static int seg6_input_core(struct net *net, struct sock *sk,
  359. struct sk_buff *skb)
  360. {
  361. struct dst_entry *orig_dst = skb_dst(skb);
  362. struct dst_entry *dst = NULL;
  363. struct seg6_lwt *slwt;
  364. int err;
  365. err = seg6_do_srh(skb);
  366. if (unlikely(err)) {
  367. kfree_skb(skb);
  368. return err;
  369. }
  370. slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate);
  371. preempt_disable();
  372. dst = dst_cache_get(&slwt->cache);
  373. preempt_enable();
  374. skb_dst_drop(skb);
  375. if (!dst) {
  376. ip6_route_input(skb);
  377. dst = skb_dst(skb);
  378. if (!dst->error) {
  379. preempt_disable();
  380. dst_cache_set_ip6(&slwt->cache, dst,
  381. &ipv6_hdr(skb)->saddr);
  382. preempt_enable();
  383. }
  384. } else {
  385. skb_dst_set(skb, dst);
  386. }
  387. err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
  388. if (unlikely(err))
  389. return err;
  390. if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
  391. return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
  392. dev_net(skb->dev), NULL, skb, NULL,
  393. skb_dst(skb)->dev, seg6_input_finish);
  394. return seg6_input_finish(dev_net(skb->dev), NULL, skb);
  395. }
  396. static int seg6_input_nf(struct sk_buff *skb)
  397. {
  398. struct net_device *dev = skb_dst(skb)->dev;
  399. struct net *net = dev_net(skb->dev);
  400. switch (skb->protocol) {
  401. case htons(ETH_P_IP):
  402. return NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, net, NULL,
  403. skb, NULL, dev, seg6_input_core);
  404. case htons(ETH_P_IPV6):
  405. return NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, net, NULL,
  406. skb, NULL, dev, seg6_input_core);
  407. }
  408. return -EINVAL;
  409. }
  410. static int seg6_input(struct sk_buff *skb)
  411. {
  412. if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
  413. return seg6_input_nf(skb);
  414. return seg6_input_core(dev_net(skb->dev), NULL, skb);
  415. }
  416. static int seg6_output_core(struct net *net, struct sock *sk,
  417. struct sk_buff *skb)
  418. {
  419. struct dst_entry *orig_dst = skb_dst(skb);
  420. struct dst_entry *dst = NULL;
  421. struct seg6_lwt *slwt;
  422. int err;
  423. err = seg6_do_srh(skb);
  424. if (unlikely(err))
  425. goto drop;
  426. slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate);
  427. preempt_disable();
  428. dst = dst_cache_get(&slwt->cache);
  429. preempt_enable();
  430. if (unlikely(!dst)) {
  431. struct ipv6hdr *hdr = ipv6_hdr(skb);
  432. struct flowi6 fl6;
  433. memset(&fl6, 0, sizeof(fl6));
  434. fl6.daddr = hdr->daddr;
  435. fl6.saddr = hdr->saddr;
  436. fl6.flowlabel = ip6_flowinfo(hdr);
  437. fl6.flowi6_mark = skb->mark;
  438. fl6.flowi6_proto = hdr->nexthdr;
  439. dst = ip6_route_output(net, NULL, &fl6);
  440. if (dst->error) {
  441. err = dst->error;
  442. dst_release(dst);
  443. goto drop;
  444. }
  445. preempt_disable();
  446. dst_cache_set_ip6(&slwt->cache, dst, &fl6.saddr);
  447. preempt_enable();
  448. }
  449. skb_dst_drop(skb);
  450. skb_dst_set(skb, dst);
  451. err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
  452. if (unlikely(err))
  453. goto drop;
  454. if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
  455. return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb,
  456. NULL, skb_dst(skb)->dev, dst_output);
  457. return dst_output(net, sk, skb);
  458. drop:
  459. kfree_skb(skb);
  460. return err;
  461. }
  462. static int seg6_output_nf(struct net *net, struct sock *sk, struct sk_buff *skb)
  463. {
  464. struct net_device *dev = skb_dst(skb)->dev;
  465. switch (skb->protocol) {
  466. case htons(ETH_P_IP):
  467. return NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, net, sk, skb,
  468. NULL, dev, seg6_output_core);
  469. case htons(ETH_P_IPV6):
  470. return NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, net, sk, skb,
  471. NULL, dev, seg6_output_core);
  472. }
  473. return -EINVAL;
  474. }
  475. static int seg6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  476. {
  477. if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
  478. return seg6_output_nf(net, sk, skb);
  479. return seg6_output_core(net, sk, skb);
  480. }
  481. static int seg6_build_state(struct net *net, struct nlattr *nla,
  482. unsigned int family, const void *cfg,
  483. struct lwtunnel_state **ts,
  484. struct netlink_ext_ack *extack)
  485. {
  486. struct nlattr *tb[SEG6_IPTUNNEL_MAX + 1];
  487. struct seg6_iptunnel_encap *tuninfo;
  488. struct lwtunnel_state *newts;
  489. int tuninfo_len, min_size;
  490. struct seg6_lwt *slwt;
  491. int err;
  492. if (family != AF_INET && family != AF_INET6)
  493. return -EINVAL;
  494. err = nla_parse_nested_deprecated(tb, SEG6_IPTUNNEL_MAX, nla,
  495. seg6_iptunnel_policy, extack);
  496. if (err < 0)
  497. return err;
  498. if (!tb[SEG6_IPTUNNEL_SRH])
  499. return -EINVAL;
  500. tuninfo = nla_data(tb[SEG6_IPTUNNEL_SRH]);
  501. tuninfo_len = nla_len(tb[SEG6_IPTUNNEL_SRH]);
  502. /* tuninfo must contain at least the iptunnel encap structure,
  503. * the SRH and one segment
  504. */
  505. min_size = sizeof(*tuninfo) + sizeof(struct ipv6_sr_hdr) +
  506. sizeof(struct in6_addr);
  507. if (tuninfo_len < min_size)
  508. return -EINVAL;
  509. switch (tuninfo->mode) {
  510. case SEG6_IPTUN_MODE_INLINE:
  511. if (family != AF_INET6)
  512. return -EINVAL;
  513. break;
  514. case SEG6_IPTUN_MODE_ENCAP:
  515. break;
  516. case SEG6_IPTUN_MODE_L2ENCAP:
  517. break;
  518. case SEG6_IPTUN_MODE_ENCAP_RED:
  519. break;
  520. case SEG6_IPTUN_MODE_L2ENCAP_RED:
  521. break;
  522. default:
  523. return -EINVAL;
  524. }
  525. /* verify that SRH is consistent */
  526. if (!seg6_validate_srh(tuninfo->srh, tuninfo_len - sizeof(*tuninfo), false))
  527. return -EINVAL;
  528. newts = lwtunnel_state_alloc(tuninfo_len + sizeof(*slwt));
  529. if (!newts)
  530. return -ENOMEM;
  531. slwt = seg6_lwt_lwtunnel(newts);
  532. err = dst_cache_init(&slwt->cache, GFP_ATOMIC);
  533. if (err) {
  534. kfree(newts);
  535. return err;
  536. }
  537. memcpy(&slwt->tuninfo, tuninfo, tuninfo_len);
  538. newts->type = LWTUNNEL_ENCAP_SEG6;
  539. newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT;
  540. if (tuninfo->mode != SEG6_IPTUN_MODE_L2ENCAP)
  541. newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;
  542. newts->headroom = seg6_lwt_headroom(tuninfo);
  543. *ts = newts;
  544. return 0;
  545. }
  546. static void seg6_destroy_state(struct lwtunnel_state *lwt)
  547. {
  548. dst_cache_destroy(&seg6_lwt_lwtunnel(lwt)->cache);
  549. }
  550. static int seg6_fill_encap_info(struct sk_buff *skb,
  551. struct lwtunnel_state *lwtstate)
  552. {
  553. struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate);
  554. if (nla_put_srh(skb, SEG6_IPTUNNEL_SRH, tuninfo))
  555. return -EMSGSIZE;
  556. return 0;
  557. }
  558. static int seg6_encap_nlsize(struct lwtunnel_state *lwtstate)
  559. {
  560. struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate);
  561. return nla_total_size(SEG6_IPTUN_ENCAP_SIZE(tuninfo));
  562. }
  563. static int seg6_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
  564. {
  565. struct seg6_iptunnel_encap *a_hdr = seg6_encap_lwtunnel(a);
  566. struct seg6_iptunnel_encap *b_hdr = seg6_encap_lwtunnel(b);
  567. int len = SEG6_IPTUN_ENCAP_SIZE(a_hdr);
  568. if (len != SEG6_IPTUN_ENCAP_SIZE(b_hdr))
  569. return 1;
  570. return memcmp(a_hdr, b_hdr, len);
  571. }
  572. static const struct lwtunnel_encap_ops seg6_iptun_ops = {
  573. .build_state = seg6_build_state,
  574. .destroy_state = seg6_destroy_state,
  575. .output = seg6_output,
  576. .input = seg6_input,
  577. .fill_encap = seg6_fill_encap_info,
  578. .get_encap_size = seg6_encap_nlsize,
  579. .cmp_encap = seg6_encap_cmp,
  580. .owner = THIS_MODULE,
  581. };
  582. int __init seg6_iptunnel_init(void)
  583. {
  584. return lwtunnel_encap_add_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6);
  585. }
  586. void seg6_iptunnel_exit(void)
  587. {
  588. lwtunnel_encap_del_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6);
  589. }