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