blob: 7a78dcfda68a17e10e5e951db21d8113c7f65301 [file] [log] [blame]
David Lebrun6c8702c2016-11-08 14:57:41 +01001/*
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
14#include <linux/types.h>
15#include <linux/skbuff.h>
16#include <linux/net.h>
17#include <linux/module.h>
18#include <net/ip.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>
David Lebrun6c8702c2016-11-08 14:57:41 +010029#include <net/dst_cache.h>
David Lebrun9baee832016-11-08 14:59:19 +010030#ifdef CONFIG_IPV6_SEG6_HMAC
31#include <net/seg6_hmac.h>
32#endif
David Lebrun6c8702c2016-11-08 14:57:41 +010033
34struct seg6_lwt {
David Lebrun6c8702c2016-11-08 14:57:41 +010035 struct dst_cache cache;
David Lebrun6c8702c2016-11-08 14:57:41 +010036 struct seg6_iptunnel_encap tuninfo[0];
37};
38
39static inline struct seg6_lwt *seg6_lwt_lwtunnel(struct lwtunnel_state *lwt)
40{
41 return (struct seg6_lwt *)lwt->data;
42}
43
44static inline struct seg6_iptunnel_encap *
45seg6_encap_lwtunnel(struct lwtunnel_state *lwt)
46{
47 return seg6_lwt_lwtunnel(lwt)->tuninfo;
48}
49
50static const struct nla_policy seg6_iptunnel_policy[SEG6_IPTUNNEL_MAX + 1] = {
51 [SEG6_IPTUNNEL_SRH] = { .type = NLA_BINARY },
52};
53
Wei Yongjunbb4005b2017-02-06 16:15:05 +000054static int nla_put_srh(struct sk_buff *skb, int attrtype,
55 struct seg6_iptunnel_encap *tuninfo)
David Lebrun6c8702c2016-11-08 14:57:41 +010056{
57 struct seg6_iptunnel_encap *data;
58 struct nlattr *nla;
59 int len;
60
61 len = SEG6_IPTUN_ENCAP_SIZE(tuninfo);
62
63 nla = nla_reserve(skb, attrtype, len);
64 if (!nla)
65 return -EMSGSIZE;
66
67 data = nla_data(nla);
68 memcpy(data, tuninfo, len);
69
70 return 0;
71}
72
73static void set_tun_src(struct net *net, struct net_device *dev,
74 struct in6_addr *daddr, struct in6_addr *saddr)
75{
76 struct seg6_pernet_data *sdata = seg6_pernet(net);
77 struct in6_addr *tun_src;
78
79 rcu_read_lock();
80
81 tun_src = rcu_dereference(sdata->tun_src);
82
83 if (!ipv6_addr_any(tun_src)) {
84 memcpy(saddr, tun_src, sizeof(struct in6_addr));
85 } else {
86 ipv6_dev_get_saddr(net, dev, daddr, IPV6_PREFER_SRC_PUBLIC,
87 saddr);
88 }
89
90 rcu_read_unlock();
91}
92
93/* encapsulate an IPv6 packet within an outer IPv6 header with a given SRH */
David Lebrun32d99d02017-08-25 09:56:44 +020094int seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_sr_hdr *osrh, int proto)
David Lebrun6c8702c2016-11-08 14:57:41 +010095{
David Lebrun8936ef72018-03-20 14:44:56 +000096 struct dst_entry *dst = skb_dst(skb);
97 struct net *net = dev_net(dst->dev);
David Lebrun6c8702c2016-11-08 14:57:41 +010098 struct ipv6hdr *hdr, *inner_hdr;
99 struct ipv6_sr_hdr *isrh;
100 int hdrlen, tot_len, err;
101
102 hdrlen = (osrh->hdrlen + 1) << 3;
103 tot_len = hdrlen + sizeof(*hdr);
104
David Lebrun19d5a262017-03-24 10:46:26 +0100105 err = skb_cow_head(skb, tot_len);
David Lebrun6c8702c2016-11-08 14:57:41 +0100106 if (unlikely(err))
107 return err;
108
109 inner_hdr = ipv6_hdr(skb);
110
111 skb_push(skb, tot_len);
112 skb_reset_network_header(skb);
113 skb_mac_header_rebuild(skb);
114 hdr = ipv6_hdr(skb);
115
116 /* inherit tc, flowlabel and hlim
117 * hlim will be decremented in ip6_forward() afterwards and
118 * decapsulation will overwrite inner hlim with outer hlim
119 */
David Lebrun32d99d02017-08-25 09:56:44 +0200120
121 if (skb->protocol == htons(ETH_P_IPV6)) {
122 ip6_flow_hdr(hdr, ip6_tclass(ip6_flowinfo(inner_hdr)),
123 ip6_flowlabel(inner_hdr));
124 hdr->hop_limit = inner_hdr->hop_limit;
125 } else {
126 ip6_flow_hdr(hdr, 0, 0);
127 hdr->hop_limit = ip6_dst_hoplimit(skb_dst(skb));
128 }
129
David Lebrun6c8702c2016-11-08 14:57:41 +0100130 hdr->nexthdr = NEXTHDR_ROUTING;
131
132 isrh = (void *)hdr + sizeof(*hdr);
133 memcpy(isrh, osrh, hdrlen);
134
David Lebrun32d99d02017-08-25 09:56:44 +0200135 isrh->nexthdr = proto;
David Lebrun6c8702c2016-11-08 14:57:41 +0100136
137 hdr->daddr = isrh->segments[isrh->first_segment];
David Lebrun8936ef72018-03-20 14:44:56 +0000138 set_tun_src(net, ip6_dst_idev(dst)->dev, &hdr->daddr, &hdr->saddr);
David Lebrun6c8702c2016-11-08 14:57:41 +0100139
David Lebrun9baee832016-11-08 14:59:19 +0100140#ifdef CONFIG_IPV6_SEG6_HMAC
141 if (sr_has_hmac(isrh)) {
142 err = seg6_push_hmac(net, &hdr->saddr, isrh);
143 if (unlikely(err))
144 return err;
145 }
146#endif
147
David Lebrun6c8702c2016-11-08 14:57:41 +0100148 skb_postpush_rcsum(skb, hdr, tot_len);
149
150 return 0;
151}
David Lebrunb04c80d2017-08-05 12:38:25 +0200152EXPORT_SYMBOL_GPL(seg6_do_srh_encap);
David Lebrun6c8702c2016-11-08 14:57:41 +0100153
154/* insert an SRH within an IPv6 packet, just after the IPv6 header */
David Lebrunb04c80d2017-08-05 12:38:25 +0200155int seg6_do_srh_inline(struct sk_buff *skb, struct ipv6_sr_hdr *osrh)
David Lebrun6c8702c2016-11-08 14:57:41 +0100156{
157 struct ipv6hdr *hdr, *oldhdr;
158 struct ipv6_sr_hdr *isrh;
159 int hdrlen, err;
160
161 hdrlen = (osrh->hdrlen + 1) << 3;
162
David Lebrun19d5a262017-03-24 10:46:26 +0100163 err = skb_cow_head(skb, hdrlen);
David Lebrun6c8702c2016-11-08 14:57:41 +0100164 if (unlikely(err))
165 return err;
166
167 oldhdr = ipv6_hdr(skb);
168
169 skb_pull(skb, sizeof(struct ipv6hdr));
170 skb_postpull_rcsum(skb, skb_network_header(skb),
171 sizeof(struct ipv6hdr));
172
173 skb_push(skb, sizeof(struct ipv6hdr) + hdrlen);
174 skb_reset_network_header(skb);
175 skb_mac_header_rebuild(skb);
176
177 hdr = ipv6_hdr(skb);
178
179 memmove(hdr, oldhdr, sizeof(*hdr));
180
181 isrh = (void *)hdr + sizeof(*hdr);
182 memcpy(isrh, osrh, hdrlen);
183
184 isrh->nexthdr = hdr->nexthdr;
185 hdr->nexthdr = NEXTHDR_ROUTING;
186
187 isrh->segments[0] = hdr->daddr;
188 hdr->daddr = isrh->segments[isrh->first_segment];
189
David Lebrun9baee832016-11-08 14:59:19 +0100190#ifdef CONFIG_IPV6_SEG6_HMAC
191 if (sr_has_hmac(isrh)) {
192 struct net *net = dev_net(skb_dst(skb)->dev);
193
194 err = seg6_push_hmac(net, &hdr->saddr, isrh);
195 if (unlikely(err))
196 return err;
197 }
198#endif
199
David Lebrun6c8702c2016-11-08 14:57:41 +0100200 skb_postpush_rcsum(skb, hdr, sizeof(struct ipv6hdr) + hdrlen);
201
202 return 0;
203}
David Lebrunb04c80d2017-08-05 12:38:25 +0200204EXPORT_SYMBOL_GPL(seg6_do_srh_inline);
David Lebrun6c8702c2016-11-08 14:57:41 +0100205
206static int seg6_do_srh(struct sk_buff *skb)
207{
208 struct dst_entry *dst = skb_dst(skb);
209 struct seg6_iptunnel_encap *tinfo;
David Lebrun32d99d02017-08-25 09:56:44 +0200210 int proto, err = 0;
David Lebrun6c8702c2016-11-08 14:57:41 +0100211
212 tinfo = seg6_encap_lwtunnel(dst->lwtstate);
213
214 if (likely(!skb->encapsulation)) {
215 skb_reset_inner_headers(skb);
216 skb->encapsulation = 1;
217 }
218
219 switch (tinfo->mode) {
David Lebrun6c8702c2016-11-08 14:57:41 +0100220 case SEG6_IPTUN_MODE_INLINE:
David Lebrun32d99d02017-08-25 09:56:44 +0200221 if (skb->protocol != htons(ETH_P_IPV6))
222 return -EINVAL;
223
David Lebrun6c8702c2016-11-08 14:57:41 +0100224 err = seg6_do_srh_inline(skb, tinfo->srh);
David Lebrun32d99d02017-08-25 09:56:44 +0200225 if (err)
226 return err;
227
David Lebrun6c8702c2016-11-08 14:57:41 +0100228 skb_reset_inner_headers(skb);
229 break;
David Lebrun6c8702c2016-11-08 14:57:41 +0100230 case SEG6_IPTUN_MODE_ENCAP:
David Lebrun32d99d02017-08-25 09:56:44 +0200231 if (skb->protocol == htons(ETH_P_IPV6))
232 proto = IPPROTO_IPV6;
233 else if (skb->protocol == htons(ETH_P_IP))
234 proto = IPPROTO_IPIP;
235 else
236 return -EINVAL;
237
238 err = seg6_do_srh_encap(skb, tinfo->srh, proto);
239 if (err)
240 return err;
241
242 skb->protocol = htons(ETH_P_IPV6);
David Lebrun6c8702c2016-11-08 14:57:41 +0100243 break;
David Lebrun38ee7f22017-08-25 09:56:45 +0200244 case SEG6_IPTUN_MODE_L2ENCAP:
245 if (!skb_mac_header_was_set(skb))
246 return -EINVAL;
247
248 if (pskb_expand_head(skb, skb->mac_len, 0, GFP_ATOMIC) < 0)
249 return -ENOMEM;
250
251 skb_mac_header_rebuild(skb);
252 skb_push(skb, skb->mac_len);
253
254 err = seg6_do_srh_encap(skb, tinfo->srh, NEXTHDR_NONE);
255 if (err)
256 return err;
257
258 skb->protocol = htons(ETH_P_IPV6);
259 break;
David Lebrun6c8702c2016-11-08 14:57:41 +0100260 }
261
David Lebrun6c8702c2016-11-08 14:57:41 +0100262 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
263 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
264
265 skb_set_inner_protocol(skb, skb->protocol);
266
267 return 0;
268}
269
Wei Yongjunbb4005b2017-02-06 16:15:05 +0000270static int seg6_input(struct sk_buff *skb)
David Lebrun6c8702c2016-11-08 14:57:41 +0100271{
David Lebrunaf4a2202017-03-24 10:46:27 +0100272 struct dst_entry *orig_dst = skb_dst(skb);
273 struct dst_entry *dst = NULL;
274 struct seg6_lwt *slwt;
David Lebrun6c8702c2016-11-08 14:57:41 +0100275 int err;
276
277 err = seg6_do_srh(skb);
278 if (unlikely(err)) {
279 kfree_skb(skb);
280 return err;
281 }
282
David Lebrunaf4a2202017-03-24 10:46:27 +0100283 slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate);
284
David Lebrunaf4a2202017-03-24 10:46:27 +0100285 preempt_disable();
286 dst = dst_cache_get(&slwt->cache);
287 preempt_enable();
David Lebrunaf4a2202017-03-24 10:46:27 +0100288
David Lebrun6c8702c2016-11-08 14:57:41 +0100289 skb_dst_drop(skb);
David Lebrunaf4a2202017-03-24 10:46:27 +0100290
291 if (!dst) {
292 ip6_route_input(skb);
David Lebrunaf4a2202017-03-24 10:46:27 +0100293 dst = skb_dst(skb);
294 if (!dst->error) {
295 preempt_disable();
296 dst_cache_set_ip6(&slwt->cache, dst,
297 &ipv6_hdr(skb)->saddr);
298 preempt_enable();
299 }
David Lebrunaf4a2202017-03-24 10:46:27 +0100300 } else {
301 skb_dst_set(skb, dst);
302 }
David Lebrun6c8702c2016-11-08 14:57:41 +0100303
David Lebrunaf3b5152017-04-16 12:27:14 +0200304 err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
305 if (unlikely(err))
306 return err;
307
David Lebrun6c8702c2016-11-08 14:57:41 +0100308 return dst_input(skb);
309}
310
Wei Yongjunbb4005b2017-02-06 16:15:05 +0000311static int seg6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
David Lebrun6c8702c2016-11-08 14:57:41 +0100312{
313 struct dst_entry *orig_dst = skb_dst(skb);
314 struct dst_entry *dst = NULL;
315 struct seg6_lwt *slwt;
316 int err = -EINVAL;
317
318 err = seg6_do_srh(skb);
319 if (unlikely(err))
320 goto drop;
321
322 slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate);
323
David Lebrunfa795812017-01-12 21:30:01 +0100324 preempt_disable();
David Lebrun6c8702c2016-11-08 14:57:41 +0100325 dst = dst_cache_get(&slwt->cache);
David Lebrunfa795812017-01-12 21:30:01 +0100326 preempt_enable();
David Lebrun6c8702c2016-11-08 14:57:41 +0100327
328 if (unlikely(!dst)) {
329 struct ipv6hdr *hdr = ipv6_hdr(skb);
330 struct flowi6 fl6;
331
332 fl6.daddr = hdr->daddr;
333 fl6.saddr = hdr->saddr;
334 fl6.flowlabel = ip6_flowinfo(hdr);
335 fl6.flowi6_mark = skb->mark;
336 fl6.flowi6_proto = hdr->nexthdr;
337
338 dst = ip6_route_output(net, NULL, &fl6);
339 if (dst->error) {
340 err = dst->error;
341 dst_release(dst);
342 goto drop;
343 }
344
David Lebrunfa795812017-01-12 21:30:01 +0100345 preempt_disable();
David Lebrun6c8702c2016-11-08 14:57:41 +0100346 dst_cache_set_ip6(&slwt->cache, dst, &fl6.saddr);
David Lebrunfa795812017-01-12 21:30:01 +0100347 preempt_enable();
David Lebrun6c8702c2016-11-08 14:57:41 +0100348 }
349
350 skb_dst_drop(skb);
351 skb_dst_set(skb, dst);
352
David Lebrunaf3b5152017-04-16 12:27:14 +0200353 err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
354 if (unlikely(err))
355 goto drop;
356
David Lebrun6c8702c2016-11-08 14:57:41 +0100357 return dst_output(net, sk, skb);
358drop:
359 kfree_skb(skb);
360 return err;
361}
362
David Ahern30357d72017-01-30 12:07:37 -0800363static int seg6_build_state(struct nlattr *nla,
David Lebrun6c8702c2016-11-08 14:57:41 +0100364 unsigned int family, const void *cfg,
David Ahern9ae28722017-05-27 16:19:28 -0600365 struct lwtunnel_state **ts,
366 struct netlink_ext_ack *extack)
David Lebrun6c8702c2016-11-08 14:57:41 +0100367{
368 struct nlattr *tb[SEG6_IPTUNNEL_MAX + 1];
369 struct seg6_iptunnel_encap *tuninfo;
370 struct lwtunnel_state *newts;
371 int tuninfo_len, min_size;
372 struct seg6_lwt *slwt;
373 int err;
374
David Lebrun32d99d02017-08-25 09:56:44 +0200375 if (family != AF_INET && family != AF_INET6)
376 return -EINVAL;
377
David Lebrun6c8702c2016-11-08 14:57:41 +0100378 err = nla_parse_nested(tb, SEG6_IPTUNNEL_MAX, nla,
David Ahern9ae28722017-05-27 16:19:28 -0600379 seg6_iptunnel_policy, extack);
David Lebrun6c8702c2016-11-08 14:57:41 +0100380
381 if (err < 0)
382 return err;
383
384 if (!tb[SEG6_IPTUNNEL_SRH])
385 return -EINVAL;
386
387 tuninfo = nla_data(tb[SEG6_IPTUNNEL_SRH]);
388 tuninfo_len = nla_len(tb[SEG6_IPTUNNEL_SRH]);
389
390 /* tuninfo must contain at least the iptunnel encap structure,
391 * the SRH and one segment
392 */
393 min_size = sizeof(*tuninfo) + sizeof(struct ipv6_sr_hdr) +
394 sizeof(struct in6_addr);
395 if (tuninfo_len < min_size)
396 return -EINVAL;
397
398 switch (tuninfo->mode) {
David Lebrun6c8702c2016-11-08 14:57:41 +0100399 case SEG6_IPTUN_MODE_INLINE:
David Lebrun32d99d02017-08-25 09:56:44 +0200400 if (family != AF_INET6)
401 return -EINVAL;
402
David Lebrun6c8702c2016-11-08 14:57:41 +0100403 break;
David Lebrun6c8702c2016-11-08 14:57:41 +0100404 case SEG6_IPTUN_MODE_ENCAP:
405 break;
David Lebrun38ee7f22017-08-25 09:56:45 +0200406 case SEG6_IPTUN_MODE_L2ENCAP:
407 break;
David Lebrun6c8702c2016-11-08 14:57:41 +0100408 default:
409 return -EINVAL;
410 }
411
412 /* verify that SRH is consistent */
413 if (!seg6_validate_srh(tuninfo->srh, tuninfo_len - sizeof(*tuninfo)))
414 return -EINVAL;
415
416 newts = lwtunnel_state_alloc(tuninfo_len + sizeof(*slwt));
417 if (!newts)
418 return -ENOMEM;
419
420 slwt = seg6_lwt_lwtunnel(newts);
421
David Lebrun191f86c2018-03-20 14:44:55 +0000422 err = dst_cache_init(&slwt->cache, GFP_ATOMIC);
David Lebrun6c8702c2016-11-08 14:57:41 +0100423 if (err) {
424 kfree(newts);
425 return err;
426 }
David Lebrun6c8702c2016-11-08 14:57:41 +0100427
428 memcpy(&slwt->tuninfo, tuninfo, tuninfo_len);
429
430 newts->type = LWTUNNEL_ENCAP_SEG6;
David Lebrun38ee7f22017-08-25 09:56:45 +0200431 newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT;
432
433 if (tuninfo->mode != SEG6_IPTUN_MODE_L2ENCAP)
434 newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;
435
David Lebrun6c8702c2016-11-08 14:57:41 +0100436 newts->headroom = seg6_lwt_headroom(tuninfo);
437
438 *ts = newts;
439
440 return 0;
441}
442
David Lebrun6c8702c2016-11-08 14:57:41 +0100443static void seg6_destroy_state(struct lwtunnel_state *lwt)
444{
445 dst_cache_destroy(&seg6_lwt_lwtunnel(lwt)->cache);
446}
David Lebrun6c8702c2016-11-08 14:57:41 +0100447
448static int seg6_fill_encap_info(struct sk_buff *skb,
449 struct lwtunnel_state *lwtstate)
450{
451 struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate);
452
453 if (nla_put_srh(skb, SEG6_IPTUNNEL_SRH, tuninfo))
454 return -EMSGSIZE;
455
456 return 0;
457}
458
459static int seg6_encap_nlsize(struct lwtunnel_state *lwtstate)
460{
461 struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate);
462
463 return nla_total_size(SEG6_IPTUN_ENCAP_SIZE(tuninfo));
464}
465
466static int seg6_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
467{
468 struct seg6_iptunnel_encap *a_hdr = seg6_encap_lwtunnel(a);
469 struct seg6_iptunnel_encap *b_hdr = seg6_encap_lwtunnel(b);
470 int len = SEG6_IPTUN_ENCAP_SIZE(a_hdr);
471
472 if (len != SEG6_IPTUN_ENCAP_SIZE(b_hdr))
473 return 1;
474
475 return memcmp(a_hdr, b_hdr, len);
476}
477
478static const struct lwtunnel_encap_ops seg6_iptun_ops = {
479 .build_state = seg6_build_state,
David Lebrun6c8702c2016-11-08 14:57:41 +0100480 .destroy_state = seg6_destroy_state,
David Lebrun6c8702c2016-11-08 14:57:41 +0100481 .output = seg6_output,
482 .input = seg6_input,
483 .fill_encap = seg6_fill_encap_info,
484 .get_encap_size = seg6_encap_nlsize,
485 .cmp_encap = seg6_encap_cmp,
Robert Shearman88ff7332017-01-24 16:26:47 +0000486 .owner = THIS_MODULE,
David Lebrun6c8702c2016-11-08 14:57:41 +0100487};
488
489int __init seg6_iptunnel_init(void)
490{
491 return lwtunnel_encap_add_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6);
492}
493
494void seg6_iptunnel_exit(void)
495{
496 lwtunnel_encap_del_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6);
497}