seg6_iptunnel.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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/lwtunnel.h>
  19. #include <net/netevent.h>
  20. #include <net/netns/generic.h>
  21. #include <net/ip6_fib.h>
  22. #include <net/route.h>
  23. #include <net/seg6.h>
  24. #include <linux/seg6.h>
  25. #include <linux/seg6_iptunnel.h>
  26. #include <net/addrconf.h>
  27. #include <net/ip6_route.h>
  28. #ifdef CONFIG_DST_CACHE
  29. #include <net/dst_cache.h>
  30. #endif
  31. #ifdef CONFIG_IPV6_SEG6_HMAC
  32. #include <net/seg6_hmac.h>
  33. #endif
  34. struct seg6_lwt {
  35. #ifdef CONFIG_DST_CACHE
  36. struct dst_cache cache;
  37. #endif
  38. struct seg6_iptunnel_encap tuninfo[0];
  39. };
  40. static inline struct seg6_lwt *seg6_lwt_lwtunnel(struct lwtunnel_state *lwt)
  41. {
  42. return (struct seg6_lwt *)lwt->data;
  43. }
  44. static inline struct seg6_iptunnel_encap *
  45. seg6_encap_lwtunnel(struct lwtunnel_state *lwt)
  46. {
  47. return seg6_lwt_lwtunnel(lwt)->tuninfo;
  48. }
  49. static const struct nla_policy seg6_iptunnel_policy[SEG6_IPTUNNEL_MAX + 1] = {
  50. [SEG6_IPTUNNEL_SRH] = { .type = NLA_BINARY },
  51. };
  52. int nla_put_srh(struct sk_buff *skb, int attrtype,
  53. struct seg6_iptunnel_encap *tuninfo)
  54. {
  55. struct seg6_iptunnel_encap *data;
  56. struct nlattr *nla;
  57. int len;
  58. len = SEG6_IPTUN_ENCAP_SIZE(tuninfo);
  59. nla = nla_reserve(skb, attrtype, len);
  60. if (!nla)
  61. return -EMSGSIZE;
  62. data = nla_data(nla);
  63. memcpy(data, tuninfo, len);
  64. return 0;
  65. }
  66. static void set_tun_src(struct net *net, struct net_device *dev,
  67. struct in6_addr *daddr, struct in6_addr *saddr)
  68. {
  69. struct seg6_pernet_data *sdata = seg6_pernet(net);
  70. struct in6_addr *tun_src;
  71. rcu_read_lock();
  72. tun_src = rcu_dereference(sdata->tun_src);
  73. if (!ipv6_addr_any(tun_src)) {
  74. memcpy(saddr, tun_src, sizeof(struct in6_addr));
  75. } else {
  76. ipv6_dev_get_saddr(net, dev, daddr, IPV6_PREFER_SRC_PUBLIC,
  77. saddr);
  78. }
  79. rcu_read_unlock();
  80. }
  81. /* encapsulate an IPv6 packet within an outer IPv6 header with a given SRH */
  82. static int seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_sr_hdr *osrh)
  83. {
  84. struct net *net = dev_net(skb_dst(skb)->dev);
  85. struct ipv6hdr *hdr, *inner_hdr;
  86. struct ipv6_sr_hdr *isrh;
  87. int hdrlen, tot_len, err;
  88. hdrlen = (osrh->hdrlen + 1) << 3;
  89. tot_len = hdrlen + sizeof(*hdr);
  90. err = pskb_expand_head(skb, tot_len, 0, GFP_ATOMIC);
  91. if (unlikely(err))
  92. return err;
  93. inner_hdr = ipv6_hdr(skb);
  94. skb_push(skb, tot_len);
  95. skb_reset_network_header(skb);
  96. skb_mac_header_rebuild(skb);
  97. hdr = ipv6_hdr(skb);
  98. /* inherit tc, flowlabel and hlim
  99. * hlim will be decremented in ip6_forward() afterwards and
  100. * decapsulation will overwrite inner hlim with outer hlim
  101. */
  102. ip6_flow_hdr(hdr, ip6_tclass(ip6_flowinfo(inner_hdr)),
  103. ip6_flowlabel(inner_hdr));
  104. hdr->hop_limit = inner_hdr->hop_limit;
  105. hdr->nexthdr = NEXTHDR_ROUTING;
  106. isrh = (void *)hdr + sizeof(*hdr);
  107. memcpy(isrh, osrh, hdrlen);
  108. isrh->nexthdr = NEXTHDR_IPV6;
  109. hdr->daddr = isrh->segments[isrh->first_segment];
  110. set_tun_src(net, skb->dev, &hdr->daddr, &hdr->saddr);
  111. #ifdef CONFIG_IPV6_SEG6_HMAC
  112. if (sr_has_hmac(isrh)) {
  113. err = seg6_push_hmac(net, &hdr->saddr, isrh);
  114. if (unlikely(err))
  115. return err;
  116. }
  117. #endif
  118. skb_postpush_rcsum(skb, hdr, tot_len);
  119. return 0;
  120. }
  121. /* insert an SRH within an IPv6 packet, just after the IPv6 header */
  122. #ifdef CONFIG_IPV6_SEG6_INLINE
  123. static int seg6_do_srh_inline(struct sk_buff *skb, struct ipv6_sr_hdr *osrh)
  124. {
  125. struct ipv6hdr *hdr, *oldhdr;
  126. struct ipv6_sr_hdr *isrh;
  127. int hdrlen, err;
  128. hdrlen = (osrh->hdrlen + 1) << 3;
  129. err = pskb_expand_head(skb, hdrlen, 0, GFP_ATOMIC);
  130. if (unlikely(err))
  131. return err;
  132. oldhdr = ipv6_hdr(skb);
  133. skb_pull(skb, sizeof(struct ipv6hdr));
  134. skb_postpull_rcsum(skb, skb_network_header(skb),
  135. sizeof(struct ipv6hdr));
  136. skb_push(skb, sizeof(struct ipv6hdr) + hdrlen);
  137. skb_reset_network_header(skb);
  138. skb_mac_header_rebuild(skb);
  139. hdr = ipv6_hdr(skb);
  140. memmove(hdr, oldhdr, sizeof(*hdr));
  141. isrh = (void *)hdr + sizeof(*hdr);
  142. memcpy(isrh, osrh, hdrlen);
  143. isrh->nexthdr = hdr->nexthdr;
  144. hdr->nexthdr = NEXTHDR_ROUTING;
  145. isrh->segments[0] = hdr->daddr;
  146. hdr->daddr = isrh->segments[isrh->first_segment];
  147. #ifdef CONFIG_IPV6_SEG6_HMAC
  148. if (sr_has_hmac(isrh)) {
  149. struct net *net = dev_net(skb_dst(skb)->dev);
  150. err = seg6_push_hmac(net, &hdr->saddr, isrh);
  151. if (unlikely(err))
  152. return err;
  153. }
  154. #endif
  155. skb_postpush_rcsum(skb, hdr, sizeof(struct ipv6hdr) + hdrlen);
  156. return 0;
  157. }
  158. #endif
  159. static int seg6_do_srh(struct sk_buff *skb)
  160. {
  161. struct dst_entry *dst = skb_dst(skb);
  162. struct seg6_iptunnel_encap *tinfo;
  163. int err = 0;
  164. tinfo = seg6_encap_lwtunnel(dst->lwtstate);
  165. if (likely(!skb->encapsulation)) {
  166. skb_reset_inner_headers(skb);
  167. skb->encapsulation = 1;
  168. }
  169. switch (tinfo->mode) {
  170. #ifdef CONFIG_IPV6_SEG6_INLINE
  171. case SEG6_IPTUN_MODE_INLINE:
  172. err = seg6_do_srh_inline(skb, tinfo->srh);
  173. skb_reset_inner_headers(skb);
  174. break;
  175. #endif
  176. case SEG6_IPTUN_MODE_ENCAP:
  177. err = seg6_do_srh_encap(skb, tinfo->srh);
  178. break;
  179. }
  180. if (err)
  181. return err;
  182. ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  183. skb_set_transport_header(skb, sizeof(struct ipv6hdr));
  184. skb_set_inner_protocol(skb, skb->protocol);
  185. return 0;
  186. }
  187. int seg6_input(struct sk_buff *skb)
  188. {
  189. int err;
  190. err = seg6_do_srh(skb);
  191. if (unlikely(err)) {
  192. kfree_skb(skb);
  193. return err;
  194. }
  195. skb_dst_drop(skb);
  196. ip6_route_input(skb);
  197. return dst_input(skb);
  198. }
  199. int seg6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  200. {
  201. struct dst_entry *orig_dst = skb_dst(skb);
  202. struct dst_entry *dst = NULL;
  203. struct seg6_lwt *slwt;
  204. int err = -EINVAL;
  205. err = seg6_do_srh(skb);
  206. if (unlikely(err))
  207. goto drop;
  208. slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate);
  209. #ifdef CONFIG_DST_CACHE
  210. preempt_disable();
  211. dst = dst_cache_get(&slwt->cache);
  212. preempt_enable();
  213. #endif
  214. if (unlikely(!dst)) {
  215. struct ipv6hdr *hdr = ipv6_hdr(skb);
  216. struct flowi6 fl6;
  217. fl6.daddr = hdr->daddr;
  218. fl6.saddr = hdr->saddr;
  219. fl6.flowlabel = ip6_flowinfo(hdr);
  220. fl6.flowi6_mark = skb->mark;
  221. fl6.flowi6_proto = hdr->nexthdr;
  222. dst = ip6_route_output(net, NULL, &fl6);
  223. if (dst->error) {
  224. err = dst->error;
  225. dst_release(dst);
  226. goto drop;
  227. }
  228. #ifdef CONFIG_DST_CACHE
  229. preempt_disable();
  230. dst_cache_set_ip6(&slwt->cache, dst, &fl6.saddr);
  231. preempt_enable();
  232. #endif
  233. }
  234. skb_dst_drop(skb);
  235. skb_dst_set(skb, dst);
  236. return dst_output(net, sk, skb);
  237. drop:
  238. kfree_skb(skb);
  239. return err;
  240. }
  241. static int seg6_build_state(struct net_device *dev, struct nlattr *nla,
  242. unsigned int family, const void *cfg,
  243. struct lwtunnel_state **ts)
  244. {
  245. struct nlattr *tb[SEG6_IPTUNNEL_MAX + 1];
  246. struct seg6_iptunnel_encap *tuninfo;
  247. struct lwtunnel_state *newts;
  248. int tuninfo_len, min_size;
  249. struct seg6_lwt *slwt;
  250. int err;
  251. err = nla_parse_nested(tb, SEG6_IPTUNNEL_MAX, nla,
  252. seg6_iptunnel_policy);
  253. if (err < 0)
  254. return err;
  255. if (!tb[SEG6_IPTUNNEL_SRH])
  256. return -EINVAL;
  257. tuninfo = nla_data(tb[SEG6_IPTUNNEL_SRH]);
  258. tuninfo_len = nla_len(tb[SEG6_IPTUNNEL_SRH]);
  259. /* tuninfo must contain at least the iptunnel encap structure,
  260. * the SRH and one segment
  261. */
  262. min_size = sizeof(*tuninfo) + sizeof(struct ipv6_sr_hdr) +
  263. sizeof(struct in6_addr);
  264. if (tuninfo_len < min_size)
  265. return -EINVAL;
  266. switch (tuninfo->mode) {
  267. #ifdef CONFIG_IPV6_SEG6_INLINE
  268. case SEG6_IPTUN_MODE_INLINE:
  269. break;
  270. #endif
  271. case SEG6_IPTUN_MODE_ENCAP:
  272. break;
  273. default:
  274. return -EINVAL;
  275. }
  276. /* verify that SRH is consistent */
  277. if (!seg6_validate_srh(tuninfo->srh, tuninfo_len - sizeof(*tuninfo)))
  278. return -EINVAL;
  279. newts = lwtunnel_state_alloc(tuninfo_len + sizeof(*slwt));
  280. if (!newts)
  281. return -ENOMEM;
  282. slwt = seg6_lwt_lwtunnel(newts);
  283. #ifdef CONFIG_DST_CACHE
  284. err = dst_cache_init(&slwt->cache, GFP_KERNEL);
  285. if (err) {
  286. kfree(newts);
  287. return err;
  288. }
  289. #endif
  290. memcpy(&slwt->tuninfo, tuninfo, tuninfo_len);
  291. newts->type = LWTUNNEL_ENCAP_SEG6;
  292. newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT |
  293. LWTUNNEL_STATE_INPUT_REDIRECT;
  294. newts->headroom = seg6_lwt_headroom(tuninfo);
  295. *ts = newts;
  296. return 0;
  297. }
  298. #ifdef CONFIG_DST_CACHE
  299. static void seg6_destroy_state(struct lwtunnel_state *lwt)
  300. {
  301. dst_cache_destroy(&seg6_lwt_lwtunnel(lwt)->cache);
  302. }
  303. #endif
  304. static int seg6_fill_encap_info(struct sk_buff *skb,
  305. struct lwtunnel_state *lwtstate)
  306. {
  307. struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate);
  308. if (nla_put_srh(skb, SEG6_IPTUNNEL_SRH, tuninfo))
  309. return -EMSGSIZE;
  310. return 0;
  311. }
  312. static int seg6_encap_nlsize(struct lwtunnel_state *lwtstate)
  313. {
  314. struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate);
  315. return nla_total_size(SEG6_IPTUN_ENCAP_SIZE(tuninfo));
  316. }
  317. static int seg6_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
  318. {
  319. struct seg6_iptunnel_encap *a_hdr = seg6_encap_lwtunnel(a);
  320. struct seg6_iptunnel_encap *b_hdr = seg6_encap_lwtunnel(b);
  321. int len = SEG6_IPTUN_ENCAP_SIZE(a_hdr);
  322. if (len != SEG6_IPTUN_ENCAP_SIZE(b_hdr))
  323. return 1;
  324. return memcmp(a_hdr, b_hdr, len);
  325. }
  326. static const struct lwtunnel_encap_ops seg6_iptun_ops = {
  327. .build_state = seg6_build_state,
  328. #ifdef CONFIG_DST_CACHE
  329. .destroy_state = seg6_destroy_state,
  330. #endif
  331. .output = seg6_output,
  332. .input = seg6_input,
  333. .fill_encap = seg6_fill_encap_info,
  334. .get_encap_size = seg6_encap_nlsize,
  335. .cmp_encap = seg6_encap_cmp,
  336. };
  337. int __init seg6_iptunnel_init(void)
  338. {
  339. return lwtunnel_encap_add_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6);
  340. }
  341. void seg6_iptunnel_exit(void)
  342. {
  343. lwtunnel_encap_del_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6);
  344. }