blob: 3adc5d9211ad695bb1cdf66405be3dfe1a539c76 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Lebrun6c8702c2016-11-08 14:57:41 +01002/*
3 * SR-IPv6 implementation
4 *
5 * Author:
6 * David Lebrun <david.lebrun@uclouvain.be>
David Lebrun6c8702c2016-11-08 14:57:41 +01007 */
8
9#include <linux/types.h>
10#include <linux/skbuff.h>
11#include <linux/net.h>
12#include <linux/module.h>
13#include <net/ip.h>
David Lebrun5807b222018-03-29 17:59:36 +010014#include <net/ip_tunnels.h>
David Lebrun6c8702c2016-11-08 14:57:41 +010015#include <net/lwtunnel.h>
16#include <net/netevent.h>
17#include <net/netns/generic.h>
18#include <net/ip6_fib.h>
19#include <net/route.h>
20#include <net/seg6.h>
21#include <linux/seg6.h>
22#include <linux/seg6_iptunnel.h>
23#include <net/addrconf.h>
24#include <net/ip6_route.h>
David Lebrun6c8702c2016-11-08 14:57:41 +010025#include <net/dst_cache.h>
David Lebrun9baee832016-11-08 14:59:19 +010026#ifdef CONFIG_IPV6_SEG6_HMAC
27#include <net/seg6_hmac.h>
28#endif
Ryoga Saito7a3f5b02021-08-17 08:39:37 +000029#include <linux/netfilter.h>
David Lebrun6c8702c2016-11-08 14:57:41 +010030
Ioana-Ruxandra Stăncioi88fab212020-08-03 07:33:33 +000031static size_t seg6_lwt_headroom(struct seg6_iptunnel_encap *tuninfo)
32{
33 int head = 0;
34
35 switch (tuninfo->mode) {
36 case SEG6_IPTUN_MODE_INLINE:
37 break;
38 case SEG6_IPTUN_MODE_ENCAP:
39 head = sizeof(struct ipv6hdr);
40 break;
41 case SEG6_IPTUN_MODE_L2ENCAP:
42 return 0;
43 }
44
45 return ((tuninfo->srh->hdrlen + 1) << 3) + head;
46}
47
David Lebrun6c8702c2016-11-08 14:57:41 +010048struct seg6_lwt {
David Lebrun6c8702c2016-11-08 14:57:41 +010049 struct dst_cache cache;
Gustavo A. R. Silvab0c9a2d2020-02-28 07:36:41 -060050 struct seg6_iptunnel_encap tuninfo[];
David Lebrun6c8702c2016-11-08 14:57:41 +010051};
52
53static inline struct seg6_lwt *seg6_lwt_lwtunnel(struct lwtunnel_state *lwt)
54{
55 return (struct seg6_lwt *)lwt->data;
56}
57
58static inline struct seg6_iptunnel_encap *
59seg6_encap_lwtunnel(struct lwtunnel_state *lwt)
60{
61 return seg6_lwt_lwtunnel(lwt)->tuninfo;
62}
63
64static const struct nla_policy seg6_iptunnel_policy[SEG6_IPTUNNEL_MAX + 1] = {
65 [SEG6_IPTUNNEL_SRH] = { .type = NLA_BINARY },
66};
67
Wei Yongjunbb4005b2017-02-06 16:15:05 +000068static int nla_put_srh(struct sk_buff *skb, int attrtype,
69 struct seg6_iptunnel_encap *tuninfo)
David Lebrun6c8702c2016-11-08 14:57:41 +010070{
71 struct seg6_iptunnel_encap *data;
72 struct nlattr *nla;
73 int len;
74
75 len = SEG6_IPTUN_ENCAP_SIZE(tuninfo);
76
77 nla = nla_reserve(skb, attrtype, len);
78 if (!nla)
79 return -EMSGSIZE;
80
81 data = nla_data(nla);
82 memcpy(data, tuninfo, len);
83
84 return 0;
85}
86
87static void set_tun_src(struct net *net, struct net_device *dev,
88 struct in6_addr *daddr, struct in6_addr *saddr)
89{
90 struct seg6_pernet_data *sdata = seg6_pernet(net);
91 struct in6_addr *tun_src;
92
93 rcu_read_lock();
94
95 tun_src = rcu_dereference(sdata->tun_src);
96
97 if (!ipv6_addr_any(tun_src)) {
98 memcpy(saddr, tun_src, sizeof(struct in6_addr));
99 } else {
100 ipv6_dev_get_saddr(net, dev, daddr, IPV6_PREFER_SRC_PUBLIC,
101 saddr);
102 }
103
104 rcu_read_unlock();
105}
106
Ahmed Abdelsalamb5facfd2018-04-24 20:23:16 +0200107/* Compute flowlabel for outer IPv6 header */
108static __be32 seg6_make_flowlabel(struct net *net, struct sk_buff *skb,
109 struct ipv6hdr *inner_hdr)
110{
111 int do_flowlabel = net->ipv6.sysctl.seg6_flowlabel;
112 __be32 flowlabel = 0;
113 u32 hash;
114
115 if (do_flowlabel > 0) {
116 hash = skb_get_hash(skb);
Colin Ian King3ee593a2018-07-17 16:52:54 +0100117 hash = rol32(hash, 16);
Ahmed Abdelsalamb5facfd2018-04-24 20:23:16 +0200118 flowlabel = (__force __be32)hash & IPV6_FLOWLABEL_MASK;
119 } else if (!do_flowlabel && skb->protocol == htons(ETH_P_IPV6)) {
120 flowlabel = ip6_flowlabel(inner_hdr);
121 }
122 return flowlabel;
123}
124
David Lebrun6c8702c2016-11-08 14:57:41 +0100125/* encapsulate an IPv6 packet within an outer IPv6 header with a given SRH */
David Lebrun32d99d02017-08-25 09:56:44 +0200126int seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_sr_hdr *osrh, int proto)
David Lebrun6c8702c2016-11-08 14:57:41 +0100127{
David Lebrun8936ef72018-03-20 14:44:56 +0000128 struct dst_entry *dst = skb_dst(skb);
129 struct net *net = dev_net(dst->dev);
David Lebrun6c8702c2016-11-08 14:57:41 +0100130 struct ipv6hdr *hdr, *inner_hdr;
131 struct ipv6_sr_hdr *isrh;
132 int hdrlen, tot_len, err;
Ahmed Abdelsalamb5facfd2018-04-24 20:23:16 +0200133 __be32 flowlabel;
David Lebrun6c8702c2016-11-08 14:57:41 +0100134
135 hdrlen = (osrh->hdrlen + 1) << 3;
136 tot_len = hdrlen + sizeof(*hdr);
137
Mathieu Xhonneuxbbb40a02018-05-25 13:29:41 +0100138 err = skb_cow_head(skb, tot_len + skb->mac_len);
David Lebrun6c8702c2016-11-08 14:57:41 +0100139 if (unlikely(err))
140 return err;
141
142 inner_hdr = ipv6_hdr(skb);
Ahmed Abdelsalam6df93462018-04-28 12:18:35 +0200143 flowlabel = seg6_make_flowlabel(net, skb, inner_hdr);
David Lebrun6c8702c2016-11-08 14:57:41 +0100144
145 skb_push(skb, tot_len);
146 skb_reset_network_header(skb);
147 skb_mac_header_rebuild(skb);
148 hdr = ipv6_hdr(skb);
149
150 /* inherit tc, flowlabel and hlim
151 * hlim will be decremented in ip6_forward() afterwards and
152 * decapsulation will overwrite inner hlim with outer hlim
153 */
David Lebrun32d99d02017-08-25 09:56:44 +0200154
155 if (skb->protocol == htons(ETH_P_IPV6)) {
156 ip6_flow_hdr(hdr, ip6_tclass(ip6_flowinfo(inner_hdr)),
Ahmed Abdelsalamb5facfd2018-04-24 20:23:16 +0200157 flowlabel);
David Lebrun32d99d02017-08-25 09:56:44 +0200158 hdr->hop_limit = inner_hdr->hop_limit;
159 } else {
Ahmed Abdelsalamb5facfd2018-04-24 20:23:16 +0200160 ip6_flow_hdr(hdr, 0, flowlabel);
David Lebrun32d99d02017-08-25 09:56:44 +0200161 hdr->hop_limit = ip6_dst_hoplimit(skb_dst(skb));
Yohei Kanemaruef489742019-01-29 15:52:34 +0900162
163 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
David Lebrun32d99d02017-08-25 09:56:44 +0200164 }
165
David Lebrun6c8702c2016-11-08 14:57:41 +0100166 hdr->nexthdr = NEXTHDR_ROUTING;
167
168 isrh = (void *)hdr + sizeof(*hdr);
169 memcpy(isrh, osrh, hdrlen);
170
David Lebrun32d99d02017-08-25 09:56:44 +0200171 isrh->nexthdr = proto;
David Lebrun6c8702c2016-11-08 14:57:41 +0100172
173 hdr->daddr = isrh->segments[isrh->first_segment];
Ahmed Abdelsalama957fa12018-04-20 15:58:05 +0200174 set_tun_src(net, dst->dev, &hdr->daddr, &hdr->saddr);
David Lebrun6c8702c2016-11-08 14:57:41 +0100175
David Lebrun9baee832016-11-08 14:59:19 +0100176#ifdef CONFIG_IPV6_SEG6_HMAC
177 if (sr_has_hmac(isrh)) {
178 err = seg6_push_hmac(net, &hdr->saddr, isrh);
179 if (unlikely(err))
180 return err;
181 }
182#endif
183
David Lebrun6c8702c2016-11-08 14:57:41 +0100184 skb_postpush_rcsum(skb, hdr, tot_len);
185
186 return 0;
187}
David Lebrunb04c80d2017-08-05 12:38:25 +0200188EXPORT_SYMBOL_GPL(seg6_do_srh_encap);
David Lebrun6c8702c2016-11-08 14:57:41 +0100189
190/* insert an SRH within an IPv6 packet, just after the IPv6 header */
David Lebrunb04c80d2017-08-05 12:38:25 +0200191int seg6_do_srh_inline(struct sk_buff *skb, struct ipv6_sr_hdr *osrh)
David Lebrun6c8702c2016-11-08 14:57:41 +0100192{
193 struct ipv6hdr *hdr, *oldhdr;
194 struct ipv6_sr_hdr *isrh;
195 int hdrlen, err;
196
197 hdrlen = (osrh->hdrlen + 1) << 3;
198
Mathieu Xhonneuxbbb40a02018-05-25 13:29:41 +0100199 err = skb_cow_head(skb, hdrlen + skb->mac_len);
David Lebrun6c8702c2016-11-08 14:57:41 +0100200 if (unlikely(err))
201 return err;
202
203 oldhdr = ipv6_hdr(skb);
204
205 skb_pull(skb, sizeof(struct ipv6hdr));
206 skb_postpull_rcsum(skb, skb_network_header(skb),
207 sizeof(struct ipv6hdr));
208
209 skb_push(skb, sizeof(struct ipv6hdr) + hdrlen);
210 skb_reset_network_header(skb);
211 skb_mac_header_rebuild(skb);
212
213 hdr = ipv6_hdr(skb);
214
215 memmove(hdr, oldhdr, sizeof(*hdr));
216
217 isrh = (void *)hdr + sizeof(*hdr);
218 memcpy(isrh, osrh, hdrlen);
219
220 isrh->nexthdr = hdr->nexthdr;
221 hdr->nexthdr = NEXTHDR_ROUTING;
222
223 isrh->segments[0] = hdr->daddr;
224 hdr->daddr = isrh->segments[isrh->first_segment];
225
David Lebrun9baee832016-11-08 14:59:19 +0100226#ifdef CONFIG_IPV6_SEG6_HMAC
227 if (sr_has_hmac(isrh)) {
228 struct net *net = dev_net(skb_dst(skb)->dev);
229
230 err = seg6_push_hmac(net, &hdr->saddr, isrh);
231 if (unlikely(err))
232 return err;
233 }
234#endif
235
David Lebrun6c8702c2016-11-08 14:57:41 +0100236 skb_postpush_rcsum(skb, hdr, sizeof(struct ipv6hdr) + hdrlen);
237
238 return 0;
239}
David Lebrunb04c80d2017-08-05 12:38:25 +0200240EXPORT_SYMBOL_GPL(seg6_do_srh_inline);
David Lebrun6c8702c2016-11-08 14:57:41 +0100241
242static int seg6_do_srh(struct sk_buff *skb)
243{
244 struct dst_entry *dst = skb_dst(skb);
245 struct seg6_iptunnel_encap *tinfo;
David Lebrun32d99d02017-08-25 09:56:44 +0200246 int proto, err = 0;
David Lebrun6c8702c2016-11-08 14:57:41 +0100247
248 tinfo = seg6_encap_lwtunnel(dst->lwtstate);
249
David Lebrun6c8702c2016-11-08 14:57:41 +0100250 switch (tinfo->mode) {
David Lebrun6c8702c2016-11-08 14:57:41 +0100251 case SEG6_IPTUN_MODE_INLINE:
David Lebrun32d99d02017-08-25 09:56:44 +0200252 if (skb->protocol != htons(ETH_P_IPV6))
253 return -EINVAL;
254
David Lebrun6c8702c2016-11-08 14:57:41 +0100255 err = seg6_do_srh_inline(skb, tinfo->srh);
David Lebrun32d99d02017-08-25 09:56:44 +0200256 if (err)
257 return err;
David Lebrun6c8702c2016-11-08 14:57:41 +0100258 break;
David Lebrun6c8702c2016-11-08 14:57:41 +0100259 case SEG6_IPTUN_MODE_ENCAP:
David Lebrun5807b222018-03-29 17:59:36 +0100260 err = iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6);
261 if (err)
262 return err;
263
David Lebrun32d99d02017-08-25 09:56:44 +0200264 if (skb->protocol == htons(ETH_P_IPV6))
265 proto = IPPROTO_IPV6;
266 else if (skb->protocol == htons(ETH_P_IP))
267 proto = IPPROTO_IPIP;
268 else
269 return -EINVAL;
270
271 err = seg6_do_srh_encap(skb, tinfo->srh, proto);
272 if (err)
273 return err;
274
David Lebrun5807b222018-03-29 17:59:36 +0100275 skb_set_inner_transport_header(skb, skb_transport_offset(skb));
276 skb_set_inner_protocol(skb, skb->protocol);
David Lebrun32d99d02017-08-25 09:56:44 +0200277 skb->protocol = htons(ETH_P_IPV6);
David Lebrun6c8702c2016-11-08 14:57:41 +0100278 break;
David Lebrun38ee7f22017-08-25 09:56:45 +0200279 case SEG6_IPTUN_MODE_L2ENCAP:
280 if (!skb_mac_header_was_set(skb))
281 return -EINVAL;
282
283 if (pskb_expand_head(skb, skb->mac_len, 0, GFP_ATOMIC) < 0)
284 return -ENOMEM;
285
286 skb_mac_header_rebuild(skb);
287 skb_push(skb, skb->mac_len);
288
Paolo Lungaroni26776252020-03-11 17:54:06 +0100289 err = seg6_do_srh_encap(skb, tinfo->srh, IPPROTO_ETHERNET);
David Lebrun38ee7f22017-08-25 09:56:45 +0200290 if (err)
291 return err;
292
293 skb->protocol = htons(ETH_P_IPV6);
294 break;
David Lebrun6c8702c2016-11-08 14:57:41 +0100295 }
296
David Lebrun6c8702c2016-11-08 14:57:41 +0100297 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
298 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
Ryoga Saito7a3f5b02021-08-17 08:39:37 +0000299 nf_reset_ct(skb);
David Lebrun6c8702c2016-11-08 14:57:41 +0100300
David Lebrun6c8702c2016-11-08 14:57:41 +0100301 return 0;
302}
303
Ryoga Saito7a3f5b02021-08-17 08:39:37 +0000304static int seg6_input_finish(struct net *net, struct sock *sk,
305 struct sk_buff *skb)
306{
307 return dst_input(skb);
308}
309
310static int seg6_input_core(struct net *net, struct sock *sk,
311 struct sk_buff *skb)
David Lebrun6c8702c2016-11-08 14:57:41 +0100312{
David Lebrunaf4a2202017-03-24 10:46:27 +0100313 struct dst_entry *orig_dst = skb_dst(skb);
314 struct dst_entry *dst = NULL;
315 struct seg6_lwt *slwt;
David Lebrun6c8702c2016-11-08 14:57:41 +0100316 int err;
317
318 err = seg6_do_srh(skb);
319 if (unlikely(err)) {
320 kfree_skb(skb);
321 return err;
322 }
323
David Lebrunaf4a2202017-03-24 10:46:27 +0100324 slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate);
325
David Lebrunaf4a2202017-03-24 10:46:27 +0100326 preempt_disable();
327 dst = dst_cache_get(&slwt->cache);
328 preempt_enable();
David Lebrunaf4a2202017-03-24 10:46:27 +0100329
David Lebrun6c8702c2016-11-08 14:57:41 +0100330 skb_dst_drop(skb);
David Lebrunaf4a2202017-03-24 10:46:27 +0100331
332 if (!dst) {
333 ip6_route_input(skb);
David Lebrunaf4a2202017-03-24 10:46:27 +0100334 dst = skb_dst(skb);
335 if (!dst->error) {
336 preempt_disable();
337 dst_cache_set_ip6(&slwt->cache, dst,
338 &ipv6_hdr(skb)->saddr);
339 preempt_enable();
340 }
David Lebrunaf4a2202017-03-24 10:46:27 +0100341 } else {
342 skb_dst_set(skb, dst);
343 }
David Lebrun6c8702c2016-11-08 14:57:41 +0100344
David Lebrunaf3b5152017-04-16 12:27:14 +0200345 err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
346 if (unlikely(err))
347 return err;
348
Ryoga Saito7a3f5b02021-08-17 08:39:37 +0000349 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
350 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
351 dev_net(skb->dev), NULL, skb, NULL,
352 skb_dst(skb)->dev, seg6_input_finish);
353
354 return seg6_input_finish(dev_net(skb->dev), NULL, skb);
David Lebrun6c8702c2016-11-08 14:57:41 +0100355}
356
Ryoga Saito7a3f5b02021-08-17 08:39:37 +0000357static int seg6_input_nf(struct sk_buff *skb)
358{
359 struct net_device *dev = skb_dst(skb)->dev;
360 struct net *net = dev_net(skb->dev);
361
362 switch (skb->protocol) {
363 case htons(ETH_P_IP):
364 return NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, net, NULL,
365 skb, NULL, dev, seg6_input_core);
366 case htons(ETH_P_IPV6):
367 return NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, net, NULL,
368 skb, NULL, dev, seg6_input_core);
369 }
370
371 return -EINVAL;
372}
373
374static int seg6_input(struct sk_buff *skb)
375{
376 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
377 return seg6_input_nf(skb);
378
379 return seg6_input_core(dev_net(skb->dev), NULL, skb);
380}
381
382static int seg6_output_core(struct net *net, struct sock *sk,
383 struct sk_buff *skb)
David Lebrun6c8702c2016-11-08 14:57:41 +0100384{
385 struct dst_entry *orig_dst = skb_dst(skb);
386 struct dst_entry *dst = NULL;
387 struct seg6_lwt *slwt;
Colin Ian Kingbf0df732021-09-02 15:35:05 +0100388 int err;
David Lebrun6c8702c2016-11-08 14:57:41 +0100389
390 err = seg6_do_srh(skb);
391 if (unlikely(err))
392 goto drop;
393
394 slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate);
395
David Lebrunfa795812017-01-12 21:30:01 +0100396 preempt_disable();
David Lebrun6c8702c2016-11-08 14:57:41 +0100397 dst = dst_cache_get(&slwt->cache);
David Lebrunfa795812017-01-12 21:30:01 +0100398 preempt_enable();
David Lebrun6c8702c2016-11-08 14:57:41 +0100399
400 if (unlikely(!dst)) {
401 struct ipv6hdr *hdr = ipv6_hdr(skb);
402 struct flowi6 fl6;
403
Shmulik Ladkani1b4e5ad2018-12-07 09:50:17 +0200404 memset(&fl6, 0, sizeof(fl6));
David Lebrun6c8702c2016-11-08 14:57:41 +0100405 fl6.daddr = hdr->daddr;
406 fl6.saddr = hdr->saddr;
407 fl6.flowlabel = ip6_flowinfo(hdr);
408 fl6.flowi6_mark = skb->mark;
409 fl6.flowi6_proto = hdr->nexthdr;
410
411 dst = ip6_route_output(net, NULL, &fl6);
412 if (dst->error) {
413 err = dst->error;
414 dst_release(dst);
415 goto drop;
416 }
417
David Lebrunfa795812017-01-12 21:30:01 +0100418 preempt_disable();
David Lebrun6c8702c2016-11-08 14:57:41 +0100419 dst_cache_set_ip6(&slwt->cache, dst, &fl6.saddr);
David Lebrunfa795812017-01-12 21:30:01 +0100420 preempt_enable();
David Lebrun6c8702c2016-11-08 14:57:41 +0100421 }
422
423 skb_dst_drop(skb);
424 skb_dst_set(skb, dst);
425
David Lebrunaf3b5152017-04-16 12:27:14 +0200426 err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
427 if (unlikely(err))
428 goto drop;
429
Ryoga Saito7a3f5b02021-08-17 08:39:37 +0000430 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
431 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb,
432 NULL, skb_dst(skb)->dev, dst_output);
433
David Lebrun6c8702c2016-11-08 14:57:41 +0100434 return dst_output(net, sk, skb);
435drop:
436 kfree_skb(skb);
437 return err;
438}
439
Ryoga Saito7a3f5b02021-08-17 08:39:37 +0000440static int seg6_output_nf(struct net *net, struct sock *sk, struct sk_buff *skb)
441{
442 struct net_device *dev = skb_dst(skb)->dev;
443
444 switch (skb->protocol) {
445 case htons(ETH_P_IP):
446 return NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, net, sk, skb,
447 NULL, dev, seg6_output_core);
448 case htons(ETH_P_IPV6):
449 return NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, net, sk, skb,
450 NULL, dev, seg6_output_core);
451 }
452
453 return -EINVAL;
454}
455
456static int seg6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
457{
458 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
459 return seg6_output_nf(net, sk, skb);
460
461 return seg6_output_core(net, sk, skb);
462}
463
Alexander Aringfaee6762020-03-27 18:00:21 -0400464static int seg6_build_state(struct net *net, struct nlattr *nla,
David Lebrun6c8702c2016-11-08 14:57:41 +0100465 unsigned int family, const void *cfg,
David Ahern9ae28722017-05-27 16:19:28 -0600466 struct lwtunnel_state **ts,
467 struct netlink_ext_ack *extack)
David Lebrun6c8702c2016-11-08 14:57:41 +0100468{
469 struct nlattr *tb[SEG6_IPTUNNEL_MAX + 1];
470 struct seg6_iptunnel_encap *tuninfo;
471 struct lwtunnel_state *newts;
472 int tuninfo_len, min_size;
473 struct seg6_lwt *slwt;
474 int err;
475
David Lebrun32d99d02017-08-25 09:56:44 +0200476 if (family != AF_INET && family != AF_INET6)
477 return -EINVAL;
478
Johannes Berg8cb08172019-04-26 14:07:28 +0200479 err = nla_parse_nested_deprecated(tb, SEG6_IPTUNNEL_MAX, nla,
480 seg6_iptunnel_policy, extack);
David Lebrun6c8702c2016-11-08 14:57:41 +0100481
482 if (err < 0)
483 return err;
484
485 if (!tb[SEG6_IPTUNNEL_SRH])
486 return -EINVAL;
487
488 tuninfo = nla_data(tb[SEG6_IPTUNNEL_SRH]);
489 tuninfo_len = nla_len(tb[SEG6_IPTUNNEL_SRH]);
490
491 /* tuninfo must contain at least the iptunnel encap structure,
492 * the SRH and one segment
493 */
494 min_size = sizeof(*tuninfo) + sizeof(struct ipv6_sr_hdr) +
495 sizeof(struct in6_addr);
496 if (tuninfo_len < min_size)
497 return -EINVAL;
498
499 switch (tuninfo->mode) {
David Lebrun6c8702c2016-11-08 14:57:41 +0100500 case SEG6_IPTUN_MODE_INLINE:
David Lebrun32d99d02017-08-25 09:56:44 +0200501 if (family != AF_INET6)
502 return -EINVAL;
503
David Lebrun6c8702c2016-11-08 14:57:41 +0100504 break;
David Lebrun6c8702c2016-11-08 14:57:41 +0100505 case SEG6_IPTUN_MODE_ENCAP:
506 break;
David Lebrun38ee7f22017-08-25 09:56:45 +0200507 case SEG6_IPTUN_MODE_L2ENCAP:
508 break;
David Lebrun6c8702c2016-11-08 14:57:41 +0100509 default:
510 return -EINVAL;
511 }
512
513 /* verify that SRH is consistent */
Ahmed Abdelsalambb986a52020-06-03 06:54:42 +0000514 if (!seg6_validate_srh(tuninfo->srh, tuninfo_len - sizeof(*tuninfo), false))
David Lebrun6c8702c2016-11-08 14:57:41 +0100515 return -EINVAL;
516
517 newts = lwtunnel_state_alloc(tuninfo_len + sizeof(*slwt));
518 if (!newts)
519 return -ENOMEM;
520
521 slwt = seg6_lwt_lwtunnel(newts);
522
David Lebrun191f86c2018-03-20 14:44:55 +0000523 err = dst_cache_init(&slwt->cache, GFP_ATOMIC);
David Lebrun6c8702c2016-11-08 14:57:41 +0100524 if (err) {
525 kfree(newts);
526 return err;
527 }
David Lebrun6c8702c2016-11-08 14:57:41 +0100528
529 memcpy(&slwt->tuninfo, tuninfo, tuninfo_len);
530
531 newts->type = LWTUNNEL_ENCAP_SEG6;
David Lebrun38ee7f22017-08-25 09:56:45 +0200532 newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT;
533
534 if (tuninfo->mode != SEG6_IPTUN_MODE_L2ENCAP)
535 newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;
536
David Lebrun6c8702c2016-11-08 14:57:41 +0100537 newts->headroom = seg6_lwt_headroom(tuninfo);
538
539 *ts = newts;
540
541 return 0;
542}
543
David Lebrun6c8702c2016-11-08 14:57:41 +0100544static void seg6_destroy_state(struct lwtunnel_state *lwt)
545{
546 dst_cache_destroy(&seg6_lwt_lwtunnel(lwt)->cache);
547}
David Lebrun6c8702c2016-11-08 14:57:41 +0100548
549static int seg6_fill_encap_info(struct sk_buff *skb,
550 struct lwtunnel_state *lwtstate)
551{
552 struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate);
553
554 if (nla_put_srh(skb, SEG6_IPTUNNEL_SRH, tuninfo))
555 return -EMSGSIZE;
556
557 return 0;
558}
559
560static int seg6_encap_nlsize(struct lwtunnel_state *lwtstate)
561{
562 struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate);
563
564 return nla_total_size(SEG6_IPTUN_ENCAP_SIZE(tuninfo));
565}
566
567static int seg6_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
568{
569 struct seg6_iptunnel_encap *a_hdr = seg6_encap_lwtunnel(a);
570 struct seg6_iptunnel_encap *b_hdr = seg6_encap_lwtunnel(b);
571 int len = SEG6_IPTUN_ENCAP_SIZE(a_hdr);
572
573 if (len != SEG6_IPTUN_ENCAP_SIZE(b_hdr))
574 return 1;
575
576 return memcmp(a_hdr, b_hdr, len);
577}
578
579static const struct lwtunnel_encap_ops seg6_iptun_ops = {
580 .build_state = seg6_build_state,
David Lebrun6c8702c2016-11-08 14:57:41 +0100581 .destroy_state = seg6_destroy_state,
David Lebrun6c8702c2016-11-08 14:57:41 +0100582 .output = seg6_output,
583 .input = seg6_input,
584 .fill_encap = seg6_fill_encap_info,
585 .get_encap_size = seg6_encap_nlsize,
586 .cmp_encap = seg6_encap_cmp,
Robert Shearman88ff7332017-01-24 16:26:47 +0000587 .owner = THIS_MODULE,
David Lebrun6c8702c2016-11-08 14:57:41 +0100588};
589
590int __init seg6_iptunnel_init(void)
591{
592 return lwtunnel_encap_add_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6);
593}
594
595void seg6_iptunnel_exit(void)
596{
597 lwtunnel_encap_del_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6);
598}