seg6_iptunnel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * SR-IPv6 implementation
  3. *
  4. * Author:
  5. * David Lebrun <david.lebrun@uclouvain.be>
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include <linux/types.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/net.h>
  16. #include <linux/module.h>
  17. #include <net/ip.h>
  18. #include <net/ip_tunnels.h>
  19. #include <net/lwtunnel.h>
  20. #include <net/netevent.h>
  21. #include <net/netns/generic.h>
  22. #include <net/ip6_fib.h>
  23. #include <net/route.h>
  24. #include <net/seg6.h>
  25. #include <linux/seg6.h>
  26. #include <linux/seg6_iptunnel.h>
  27. #include <net/addrconf.h>
  28. #include <net/ip6_route.h>
  29. #include <net/dst_cache.h>
  30. #ifdef CONFIG_IPV6_SEG6_HMAC
  31. #include <net/seg6_hmac.h>
  32. #endif
  33. struct seg6_lwt {
  34. struct dst_cache cache;
  35. struct seg6_iptunnel_encap tuninfo[0];
  36. };
  37. static inline struct seg6_lwt *seg6_lwt_lwtunnel(struct lwtunnel_state *lwt)
  38. {
  39. return (struct seg6_lwt *)lwt->data;
  40. }
  41. static inline struct seg6_iptunnel_encap *
  42. seg6_encap_lwtunnel(struct lwtunnel_state *lwt)
  43. {
  44. return seg6_lwt_lwtunnel(lwt)->tuninfo;
  45. }
  46. static const struct nla_policy seg6_iptunnel_policy[SEG6_IPTUNNEL_MAX + 1] = {
  47. [SEG6_IPTUNNEL_SRH] = { .type = NLA_BINARY },
  48. };
  49. static int nla_put_srh(struct sk_buff *skb, int attrtype,
  50. struct seg6_iptunnel_encap *tuninfo)
  51. {
  52. struct seg6_iptunnel_encap *data;
  53. struct nlattr *nla;
  54. int len;
  55. len = SEG6_IPTUN_ENCAP_SIZE(tuninfo);
  56. nla = nla_reserve(skb, attrtype, len);
  57. if (!nla)
  58. return -EMSGSIZE;
  59. data = nla_data(nla);
  60. memcpy(data, tuninfo, len);
  61. return 0;
  62. }
  63. static void set_tun_src(struct net *net, struct net_device *dev,
  64. struct in6_addr *daddr, struct in6_addr *saddr)
  65. {
  66. struct seg6_pernet_data *sdata = seg6_pernet(net);
  67. struct in6_addr *tun_src;
  68. rcu_read_lock();
  69. tun_src = rcu_dereference(sdata->tun_src);
  70. if (!ipv6_addr_any(tun_src)) {
  71. memcpy(saddr, tun_src, sizeof(struct in6_addr));
  72. } else {
  73. ipv6_dev_get_saddr(net, dev, daddr, IPV6_PREFER_SRC_PUBLIC,
  74. saddr);
  75. }
  76. rcu_read_unlock();
  77. }
  78. /* Compute flowlabel for outer IPv6 header */
  79. static __be32 seg6_make_flowlabel(struct net *net, struct sk_buff *skb,
  80. struct ipv6hdr *inner_hdr)
  81. {
  82. int do_flowlabel = net->ipv6.sysctl.seg6_flowlabel;
  83. __be32 flowlabel = 0;
  84. u32 hash;
  85. if (do_flowlabel > 0) {
  86. hash = skb_get_hash(skb);
  87. hash = rol32(hash, 16);
  88. flowlabel = (__force __be32)hash & IPV6_FLOWLABEL_MASK;
  89. } else if (!do_flowlabel && skb->protocol == htons(ETH_P_IPV6)) {
  90. flowlabel = ip6_flowlabel(inner_hdr);
  91. }
  92. return flowlabel;
  93. }
  94. /* encapsulate an IPv6 packet within an outer IPv6 header with a given SRH */
  95. int seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_sr_hdr *osrh, int proto)
  96. {
  97. struct dst_entry *dst = skb_dst(skb);
  98. struct net *net = dev_net(dst->dev);
  99. struct ipv6hdr *hdr, *inner_hdr;
  100. struct ipv6_sr_hdr *isrh;
  101. int hdrlen, tot_len, err;
  102. __be32 flowlabel;
  103. hdrlen = (osrh->hdrlen + 1) << 3;
  104. tot_len = hdrlen + sizeof(*hdr);
  105. err = skb_cow_head(skb, tot_len + skb->mac_len);
  106. if (unlikely(err))
  107. return err;
  108. inner_hdr = ipv6_hdr(skb);
  109. flowlabel = seg6_make_flowlabel(net, skb, inner_hdr);
  110. skb_push(skb, tot_len);
  111. skb_reset_network_header(skb);
  112. skb_mac_header_rebuild(skb);
  113. hdr = ipv6_hdr(skb);
  114. /* inherit tc, flowlabel and hlim
  115. * hlim will be decremented in ip6_forward() afterwards and
  116. * decapsulation will overwrite inner hlim with outer hlim
  117. */
  118. if (skb->protocol == htons(ETH_P_IPV6)) {
  119. ip6_flow_hdr(hdr, ip6_tclass(ip6_flowinfo(inner_hdr)),
  120. flowlabel);
  121. hdr->hop_limit = inner_hdr->hop_limit;
  122. } else {
  123. ip6_flow_hdr(hdr, 0, flowlabel);
  124. hdr->hop_limit = ip6_dst_hoplimit(skb_dst(skb));
  125. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  126. }
  127. hdr->nexthdr = NEXTHDR_ROUTING;
  128. isrh = (void *)hdr + sizeof(*hdr);
  129. memcpy(isrh, osrh, hdrlen);
  130. isrh->nexthdr = proto;
  131. hdr->daddr = isrh->segments[isrh->first_segment];
  132. set_tun_src(net, dst->dev, &hdr->daddr, &hdr->saddr);
  133. #ifdef CONFIG_IPV6_SEG6_HMAC
  134. if (sr_has_hmac(isrh)) {
  135. err = seg6_push_hmac(net, &hdr->saddr, isrh);
  136. if (unlikely(err))
  137. return err;
  138. }
  139. #endif
  140. skb_postpush_rcsum(skb, hdr, tot_len);
  141. return 0;
  142. }
  143. EXPORT_SYMBOL_GPL(seg6_do_srh_encap);
  144. /* insert an SRH within an IPv6 packet, just after the IPv6 header */
  145. int seg6_do_srh_inline(struct sk_buff *skb, struct ipv6_sr_hdr *osrh)
  146. {
  147. struct ipv6hdr *hdr, *oldhdr;
  148. struct ipv6_sr_hdr *isrh;
  149. int hdrlen, err;
  150. hdrlen = (osrh->hdrlen + 1) << 3;
  151. err = skb_cow_head(skb, hdrlen + skb->mac_len);
  152. if (unlikely(err))
  153. return err;
  154. oldhdr = ipv6_hdr(skb);
  155. skb_pull(skb, sizeof(struct ipv6hdr));
  156. skb_postpull_rcsum(skb, skb_network_header(skb),
  157. sizeof(struct ipv6hdr));
  158. skb_push(skb, sizeof(struct ipv6hdr) + hdrlen);
  159. skb_reset_network_header(skb);
  160. skb_mac_header_rebuild(skb);
  161. hdr = ipv6_hdr(skb);
  162. memmove(hdr, oldhdr, sizeof(*hdr));
  163. isrh = (void *)hdr + sizeof(*hdr);
  164. memcpy(isrh, osrh, hdrlen);
  165. isrh->nexthdr = hdr->nexthdr;
  166. hdr->nexthdr = NEXTHDR_ROUTING;
  167. isrh->segments[0] = hdr->daddr;
  168. hdr->daddr = isrh->segments[isrh->first_segment];
  169. #ifdef CONFIG_IPV6_SEG6_HMAC
  170. if (sr_has_hmac(isrh)) {
  171. struct net *net = dev_net(skb_dst(skb)->dev);
  172. err = seg6_push_hmac(net, &hdr->saddr, isrh);
  173. if (unlikely(err))
  174. return err;
  175. }
  176. #endif
  177. skb_postpush_rcsum(skb, hdr, sizeof(struct ipv6hdr) + hdrlen);
  178. return 0;
  179. }
  180. EXPORT_SYMBOL_GPL(seg6_do_srh_inline);
  181. static int seg6_do_srh(struct sk_buff *skb)
  182. {
  183. struct dst_entry *dst = skb_dst(skb);
  184. struct seg6_iptunnel_encap *tinfo;
  185. int proto, err = 0;
  186. tinfo = seg6_encap_lwtunnel(dst->lwtstate);
  187. switch (tinfo->mode) {
  188. case SEG6_IPTUN_MODE_INLINE:
  189. if (skb->protocol != htons(ETH_P_IPV6))
  190. return -EINVAL;
  191. err = seg6_do_srh_inline(skb, tinfo->srh);
  192. if (err)
  193. return err;
  194. break;
  195. case SEG6_IPTUN_MODE_ENCAP:
  196. err = iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6);
  197. if (err)
  198. return err;
  199. if (skb->protocol == htons(ETH_P_IPV6))
  200. proto = IPPROTO_IPV6;
  201. else if (skb->protocol == htons(ETH_P_IP))
  202. proto = IPPROTO_IPIP;
  203. else
  204. return -EINVAL;
  205. err = seg6_do_srh_encap(skb, tinfo->srh, proto);
  206. if (err)
  207. return err;
  208. skb_set_inner_transport_header(skb, skb_transport_offset(skb));
  209. skb_set_inner_protocol(skb, skb->protocol);
  210. skb->protocol = htons(ETH_P_IPV6);
  211. break;
  212. case SEG6_IPTUN_MODE_L2ENCAP:
  213. if (!skb_mac_header_was_set(skb))
  214. return -EINVAL;
  215. if (pskb_expand_head(skb, skb->mac_len, 0, GFP_ATOMIC) < 0)
  216. return -ENOMEM;
  217. skb_mac_header_rebuild(skb);
  218. skb_push(skb, skb->mac_len);
  219. err = seg6_do_srh_encap(skb, tinfo->srh, NEXTHDR_NONE);
  220. if (err)
  221. return err;
  222. skb->protocol = htons(ETH_P_IPV6);
  223. break;
  224. }
  225. ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  226. skb_set_transport_header(skb, sizeof(struct ipv6hdr));
  227. return 0;
  228. }
  229. static int seg6_input(struct sk_buff *skb)
  230. {
  231. struct dst_entry *orig_dst = skb_dst(skb);
  232. struct dst_entry *dst = NULL;
  233. struct seg6_lwt *slwt;
  234. int err;
  235. err = seg6_do_srh(skb);
  236. if (unlikely(err)) {
  237. kfree_skb(skb);
  238. return err;
  239. }
  240. slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate);
  241. preempt_disable();
  242. dst = dst_cache_get(&slwt->cache);
  243. preempt_enable();
  244. skb_dst_drop(skb);
  245. if (!dst) {
  246. ip6_route_input(skb);
  247. dst = skb_dst(skb);
  248. if (!dst->error) {
  249. preempt_disable();
  250. dst_cache_set_ip6(&slwt->cache, dst,
  251. &ipv6_hdr(skb)->saddr);
  252. preempt_enable();
  253. }
  254. } else {
  255. skb_dst_set(skb, dst);
  256. }
  257. err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
  258. if (unlikely(err))
  259. return err;
  260. return dst_input(skb);
  261. }
  262. static int seg6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  263. {
  264. struct dst_entry *orig_dst = skb_dst(skb);
  265. struct dst_entry *dst = NULL;
  266. struct seg6_lwt *slwt;
  267. int err = -EINVAL;
  268. err = seg6_do_srh(skb);
  269. if (unlikely(err))
  270. goto drop;
  271. slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate);
  272. preempt_disable();
  273. dst = dst_cache_get(&slwt->cache);
  274. preempt_enable();
  275. if (unlikely(!dst)) {
  276. struct ipv6hdr *hdr = ipv6_hdr(skb);
  277. struct flowi6 fl6;
  278. memset(&fl6, 0, sizeof(fl6));
  279. fl6.daddr = hdr->daddr;
  280. fl6.saddr = hdr->saddr;
  281. fl6.flowlabel = ip6_flowinfo(hdr);
  282. fl6.flowi6_mark = skb->mark;
  283. fl6.flowi6_proto = hdr->nexthdr;
  284. dst = ip6_route_output(net, NULL, &fl6);
  285. if (dst->error) {
  286. err = dst->error;
  287. dst_release(dst);
  288. goto drop;
  289. }
  290. preempt_disable();
  291. dst_cache_set_ip6(&slwt->cache, dst, &fl6.saddr);
  292. preempt_enable();
  293. }
  294. skb_dst_drop(skb);
  295. skb_dst_set(skb, dst);
  296. err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
  297. if (unlikely(err))
  298. goto drop;
  299. return dst_output(net, sk, skb);
  300. drop:
  301. kfree_skb(skb);
  302. return err;
  303. }
  304. static int seg6_build_state(struct nlattr *nla,
  305. unsigned int family, const void *cfg,
  306. struct lwtunnel_state **ts,
  307. struct netlink_ext_ack *extack)
  308. {
  309. struct nlattr *tb[SEG6_IPTUNNEL_MAX + 1];
  310. struct seg6_iptunnel_encap *tuninfo;
  311. struct lwtunnel_state *newts;
  312. int tuninfo_len, min_size;
  313. struct seg6_lwt *slwt;
  314. int err;
  315. if (family != AF_INET && family != AF_INET6)
  316. return -EINVAL;
  317. err = nla_parse_nested(tb, SEG6_IPTUNNEL_MAX, nla,
  318. seg6_iptunnel_policy, extack);
  319. if (err < 0)
  320. return err;
  321. if (!tb[SEG6_IPTUNNEL_SRH])
  322. return -EINVAL;
  323. tuninfo = nla_data(tb[SEG6_IPTUNNEL_SRH]);
  324. tuninfo_len = nla_len(tb[SEG6_IPTUNNEL_SRH]);
  325. /* tuninfo must contain at least the iptunnel encap structure,
  326. * the SRH and one segment
  327. */
  328. min_size = sizeof(*tuninfo) + sizeof(struct ipv6_sr_hdr) +
  329. sizeof(struct in6_addr);
  330. if (tuninfo_len < min_size)
  331. return -EINVAL;
  332. switch (tuninfo->mode) {
  333. case SEG6_IPTUN_MODE_INLINE:
  334. if (family != AF_INET6)
  335. return -EINVAL;
  336. break;
  337. case SEG6_IPTUN_MODE_ENCAP:
  338. break;
  339. case SEG6_IPTUN_MODE_L2ENCAP:
  340. break;
  341. default:
  342. return -EINVAL;
  343. }
  344. /* verify that SRH is consistent */
  345. if (!seg6_validate_srh(tuninfo->srh, tuninfo_len - sizeof(*tuninfo)))
  346. return -EINVAL;
  347. newts = lwtunnel_state_alloc(tuninfo_len + sizeof(*slwt));
  348. if (!newts)
  349. return -ENOMEM;
  350. slwt = seg6_lwt_lwtunnel(newts);
  351. err = dst_cache_init(&slwt->cache, GFP_ATOMIC);
  352. if (err) {
  353. kfree(newts);
  354. return err;
  355. }
  356. memcpy(&slwt->tuninfo, tuninfo, tuninfo_len);
  357. newts->type = LWTUNNEL_ENCAP_SEG6;
  358. newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT;
  359. if (tuninfo->mode != SEG6_IPTUN_MODE_L2ENCAP)
  360. newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;
  361. newts->headroom = seg6_lwt_headroom(tuninfo);
  362. *ts = newts;
  363. return 0;
  364. }
  365. static void seg6_destroy_state(struct lwtunnel_state *lwt)
  366. {
  367. dst_cache_destroy(&seg6_lwt_lwtunnel(lwt)->cache);
  368. }
  369. static int seg6_fill_encap_info(struct sk_buff *skb,
  370. struct lwtunnel_state *lwtstate)
  371. {
  372. struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate);
  373. if (nla_put_srh(skb, SEG6_IPTUNNEL_SRH, tuninfo))
  374. return -EMSGSIZE;
  375. return 0;
  376. }
  377. static int seg6_encap_nlsize(struct lwtunnel_state *lwtstate)
  378. {
  379. struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate);
  380. return nla_total_size(SEG6_IPTUN_ENCAP_SIZE(tuninfo));
  381. }
  382. static int seg6_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
  383. {
  384. struct seg6_iptunnel_encap *a_hdr = seg6_encap_lwtunnel(a);
  385. struct seg6_iptunnel_encap *b_hdr = seg6_encap_lwtunnel(b);
  386. int len = SEG6_IPTUN_ENCAP_SIZE(a_hdr);
  387. if (len != SEG6_IPTUN_ENCAP_SIZE(b_hdr))
  388. return 1;
  389. return memcmp(a_hdr, b_hdr, len);
  390. }
  391. static const struct lwtunnel_encap_ops seg6_iptun_ops = {
  392. .build_state = seg6_build_state,
  393. .destroy_state = seg6_destroy_state,
  394. .output = seg6_output,
  395. .input = seg6_input,
  396. .fill_encap = seg6_fill_encap_info,
  397. .get_encap_size = seg6_encap_nlsize,
  398. .cmp_encap = seg6_encap_cmp,
  399. .owner = THIS_MODULE,
  400. };
  401. int __init seg6_iptunnel_init(void)
  402. {
  403. return lwtunnel_encap_add_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6);
  404. }
  405. void seg6_iptunnel_exit(void)
  406. {
  407. lwtunnel_encap_del_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6);
  408. }