1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SR-IPv6 implementation
6 * David Lebrun <david.lebrun@uclouvain.be>
9 #include <linux/types.h>
10 #include <linux/skbuff.h>
11 #include <linux/net.h>
12 #include <linux/module.h>
14 #include <net/ip_tunnels.h>
15 #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>
21 #include <linux/seg6.h>
22 #include <linux/seg6_iptunnel.h>
23 #include <net/addrconf.h>
24 #include <net/ip6_route.h>
25 #include <net/dst_cache.h>
26 #ifdef CONFIG_IPV6_SEG6_HMAC
27 #include <net/seg6_hmac.h>
29 #include <linux/netfilter.h>
31 static size_t seg6_lwt_headroom(struct seg6_iptunnel_encap *tuninfo)
35 switch (tuninfo->mode) {
36 case SEG6_IPTUN_MODE_INLINE:
38 case SEG6_IPTUN_MODE_ENCAP:
39 head = sizeof(struct ipv6hdr);
41 case SEG6_IPTUN_MODE_L2ENCAP:
45 return ((tuninfo->srh->hdrlen + 1) << 3) + head;
49 struct dst_cache cache;
50 struct seg6_iptunnel_encap tuninfo[];
53 static inline struct seg6_lwt *seg6_lwt_lwtunnel(struct lwtunnel_state *lwt)
55 return (struct seg6_lwt *)lwt->data;
58 static inline struct seg6_iptunnel_encap *
59 seg6_encap_lwtunnel(struct lwtunnel_state *lwt)
61 return seg6_lwt_lwtunnel(lwt)->tuninfo;
64 static const struct nla_policy seg6_iptunnel_policy[SEG6_IPTUNNEL_MAX + 1] = {
65 [SEG6_IPTUNNEL_SRH] = { .type = NLA_BINARY },
68 static int nla_put_srh(struct sk_buff *skb, int attrtype,
69 struct seg6_iptunnel_encap *tuninfo)
71 struct seg6_iptunnel_encap *data;
75 len = SEG6_IPTUN_ENCAP_SIZE(tuninfo);
77 nla = nla_reserve(skb, attrtype, len);
82 memcpy(data, tuninfo, len);
87 static void set_tun_src(struct net *net, struct net_device *dev,
88 struct in6_addr *daddr, struct in6_addr *saddr)
90 struct seg6_pernet_data *sdata = seg6_pernet(net);
91 struct in6_addr *tun_src;
95 tun_src = rcu_dereference(sdata->tun_src);
97 if (!ipv6_addr_any(tun_src)) {
98 memcpy(saddr, tun_src, sizeof(struct in6_addr));
100 ipv6_dev_get_saddr(net, dev, daddr, IPV6_PREFER_SRC_PUBLIC,
107 /* Compute flowlabel for outer IPv6 header */
108 static __be32 seg6_make_flowlabel(struct net *net, struct sk_buff *skb,
109 struct ipv6hdr *inner_hdr)
111 int do_flowlabel = net->ipv6.sysctl.seg6_flowlabel;
112 __be32 flowlabel = 0;
115 if (do_flowlabel > 0) {
116 hash = skb_get_hash(skb);
117 hash = rol32(hash, 16);
118 flowlabel = (__force __be32)hash & IPV6_FLOWLABEL_MASK;
119 } else if (!do_flowlabel && skb->protocol == htons(ETH_P_IPV6)) {
120 flowlabel = ip6_flowlabel(inner_hdr);
125 /* encapsulate an IPv6 packet within an outer IPv6 header with a given SRH */
126 int seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_sr_hdr *osrh, int proto)
128 struct dst_entry *dst = skb_dst(skb);
129 struct net *net = dev_net(dst->dev);
130 struct ipv6hdr *hdr, *inner_hdr;
131 struct ipv6_sr_hdr *isrh;
132 int hdrlen, tot_len, err;
135 hdrlen = (osrh->hdrlen + 1) << 3;
136 tot_len = hdrlen + sizeof(*hdr);
138 err = skb_cow_head(skb, tot_len + skb->mac_len);
142 inner_hdr = ipv6_hdr(skb);
143 flowlabel = seg6_make_flowlabel(net, skb, inner_hdr);
145 skb_push(skb, tot_len);
146 skb_reset_network_header(skb);
147 skb_mac_header_rebuild(skb);
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
155 if (skb->protocol == htons(ETH_P_IPV6)) {
156 ip6_flow_hdr(hdr, ip6_tclass(ip6_flowinfo(inner_hdr)),
158 hdr->hop_limit = inner_hdr->hop_limit;
160 ip6_flow_hdr(hdr, 0, flowlabel);
161 hdr->hop_limit = ip6_dst_hoplimit(skb_dst(skb));
163 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
165 /* the control block has been erased, so we have to set the
167 * We read the receiving interface index directly from the
168 * skb->skb_iif as it is done in the IPv4 receiving path (i.e.:
171 IP6CB(skb)->iif = skb->skb_iif;
174 hdr->nexthdr = NEXTHDR_ROUTING;
176 isrh = (void *)hdr + sizeof(*hdr);
177 memcpy(isrh, osrh, hdrlen);
179 isrh->nexthdr = proto;
181 hdr->daddr = isrh->segments[isrh->first_segment];
182 set_tun_src(net, dst->dev, &hdr->daddr, &hdr->saddr);
184 #ifdef CONFIG_IPV6_SEG6_HMAC
185 if (sr_has_hmac(isrh)) {
186 err = seg6_push_hmac(net, &hdr->saddr, isrh);
192 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
194 skb_postpush_rcsum(skb, hdr, tot_len);
198 EXPORT_SYMBOL_GPL(seg6_do_srh_encap);
200 /* insert an SRH within an IPv6 packet, just after the IPv6 header */
201 int seg6_do_srh_inline(struct sk_buff *skb, struct ipv6_sr_hdr *osrh)
203 struct ipv6hdr *hdr, *oldhdr;
204 struct ipv6_sr_hdr *isrh;
207 hdrlen = (osrh->hdrlen + 1) << 3;
209 err = skb_cow_head(skb, hdrlen + skb->mac_len);
213 oldhdr = ipv6_hdr(skb);
215 skb_pull(skb, sizeof(struct ipv6hdr));
216 skb_postpull_rcsum(skb, skb_network_header(skb),
217 sizeof(struct ipv6hdr));
219 skb_push(skb, sizeof(struct ipv6hdr) + hdrlen);
220 skb_reset_network_header(skb);
221 skb_mac_header_rebuild(skb);
225 memmove(hdr, oldhdr, sizeof(*hdr));
227 isrh = (void *)hdr + sizeof(*hdr);
228 memcpy(isrh, osrh, hdrlen);
230 isrh->nexthdr = hdr->nexthdr;
231 hdr->nexthdr = NEXTHDR_ROUTING;
233 isrh->segments[0] = hdr->daddr;
234 hdr->daddr = isrh->segments[isrh->first_segment];
236 #ifdef CONFIG_IPV6_SEG6_HMAC
237 if (sr_has_hmac(isrh)) {
238 struct net *net = dev_net(skb_dst(skb)->dev);
240 err = seg6_push_hmac(net, &hdr->saddr, isrh);
246 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
248 skb_postpush_rcsum(skb, hdr, sizeof(struct ipv6hdr) + hdrlen);
252 EXPORT_SYMBOL_GPL(seg6_do_srh_inline);
254 static int seg6_do_srh(struct sk_buff *skb)
256 struct dst_entry *dst = skb_dst(skb);
257 struct seg6_iptunnel_encap *tinfo;
260 tinfo = seg6_encap_lwtunnel(dst->lwtstate);
262 switch (tinfo->mode) {
263 case SEG6_IPTUN_MODE_INLINE:
264 if (skb->protocol != htons(ETH_P_IPV6))
267 err = seg6_do_srh_inline(skb, tinfo->srh);
271 case SEG6_IPTUN_MODE_ENCAP:
272 err = iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6);
276 if (skb->protocol == htons(ETH_P_IPV6))
277 proto = IPPROTO_IPV6;
278 else if (skb->protocol == htons(ETH_P_IP))
279 proto = IPPROTO_IPIP;
283 err = seg6_do_srh_encap(skb, tinfo->srh, proto);
287 skb_set_inner_transport_header(skb, skb_transport_offset(skb));
288 skb_set_inner_protocol(skb, skb->protocol);
289 skb->protocol = htons(ETH_P_IPV6);
291 case SEG6_IPTUN_MODE_L2ENCAP:
292 if (!skb_mac_header_was_set(skb))
295 if (pskb_expand_head(skb, skb->mac_len, 0, GFP_ATOMIC) < 0)
298 skb_mac_header_rebuild(skb);
299 skb_push(skb, skb->mac_len);
301 err = seg6_do_srh_encap(skb, tinfo->srh, IPPROTO_ETHERNET);
305 skb->protocol = htons(ETH_P_IPV6);
309 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
315 static int seg6_input_finish(struct net *net, struct sock *sk,
318 return dst_input(skb);
321 static int seg6_input_core(struct net *net, struct sock *sk,
324 struct dst_entry *orig_dst = skb_dst(skb);
325 struct dst_entry *dst = NULL;
326 struct seg6_lwt *slwt;
329 err = seg6_do_srh(skb);
335 slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate);
338 dst = dst_cache_get(&slwt->cache);
344 ip6_route_input(skb);
348 dst_cache_set_ip6(&slwt->cache, dst,
349 &ipv6_hdr(skb)->saddr);
353 skb_dst_set(skb, dst);
356 err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
360 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
361 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
362 dev_net(skb->dev), NULL, skb, NULL,
363 skb_dst(skb)->dev, seg6_input_finish);
365 return seg6_input_finish(dev_net(skb->dev), NULL, skb);
368 static int seg6_input_nf(struct sk_buff *skb)
370 struct net_device *dev = skb_dst(skb)->dev;
371 struct net *net = dev_net(skb->dev);
373 switch (skb->protocol) {
374 case htons(ETH_P_IP):
375 return NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, net, NULL,
376 skb, NULL, dev, seg6_input_core);
377 case htons(ETH_P_IPV6):
378 return NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, net, NULL,
379 skb, NULL, dev, seg6_input_core);
385 static int seg6_input(struct sk_buff *skb)
387 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
388 return seg6_input_nf(skb);
390 return seg6_input_core(dev_net(skb->dev), NULL, skb);
393 static int seg6_output_core(struct net *net, struct sock *sk,
396 struct dst_entry *orig_dst = skb_dst(skb);
397 struct dst_entry *dst = NULL;
398 struct seg6_lwt *slwt;
401 err = seg6_do_srh(skb);
405 slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate);
408 dst = dst_cache_get(&slwt->cache);
411 if (unlikely(!dst)) {
412 struct ipv6hdr *hdr = ipv6_hdr(skb);
415 memset(&fl6, 0, sizeof(fl6));
416 fl6.daddr = hdr->daddr;
417 fl6.saddr = hdr->saddr;
418 fl6.flowlabel = ip6_flowinfo(hdr);
419 fl6.flowi6_mark = skb->mark;
420 fl6.flowi6_proto = hdr->nexthdr;
422 dst = ip6_route_output(net, NULL, &fl6);
430 dst_cache_set_ip6(&slwt->cache, dst, &fl6.saddr);
435 skb_dst_set(skb, dst);
437 err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
441 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
442 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb,
443 NULL, skb_dst(skb)->dev, dst_output);
445 return dst_output(net, sk, skb);
451 static int seg6_output_nf(struct net *net, struct sock *sk, struct sk_buff *skb)
453 struct net_device *dev = skb_dst(skb)->dev;
455 switch (skb->protocol) {
456 case htons(ETH_P_IP):
457 return NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, net, sk, skb,
458 NULL, dev, seg6_output_core);
459 case htons(ETH_P_IPV6):
460 return NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, net, sk, skb,
461 NULL, dev, seg6_output_core);
467 static int seg6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
469 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
470 return seg6_output_nf(net, sk, skb);
472 return seg6_output_core(net, sk, skb);
475 static int seg6_build_state(struct net *net, struct nlattr *nla,
476 unsigned int family, const void *cfg,
477 struct lwtunnel_state **ts,
478 struct netlink_ext_ack *extack)
480 struct nlattr *tb[SEG6_IPTUNNEL_MAX + 1];
481 struct seg6_iptunnel_encap *tuninfo;
482 struct lwtunnel_state *newts;
483 int tuninfo_len, min_size;
484 struct seg6_lwt *slwt;
487 if (family != AF_INET && family != AF_INET6)
490 err = nla_parse_nested_deprecated(tb, SEG6_IPTUNNEL_MAX, nla,
491 seg6_iptunnel_policy, extack);
496 if (!tb[SEG6_IPTUNNEL_SRH])
499 tuninfo = nla_data(tb[SEG6_IPTUNNEL_SRH]);
500 tuninfo_len = nla_len(tb[SEG6_IPTUNNEL_SRH]);
502 /* tuninfo must contain at least the iptunnel encap structure,
503 * the SRH and one segment
505 min_size = sizeof(*tuninfo) + sizeof(struct ipv6_sr_hdr) +
506 sizeof(struct in6_addr);
507 if (tuninfo_len < min_size)
510 switch (tuninfo->mode) {
511 case SEG6_IPTUN_MODE_INLINE:
512 if (family != AF_INET6)
516 case SEG6_IPTUN_MODE_ENCAP:
518 case SEG6_IPTUN_MODE_L2ENCAP:
524 /* verify that SRH is consistent */
525 if (!seg6_validate_srh(tuninfo->srh, tuninfo_len - sizeof(*tuninfo), false))
528 newts = lwtunnel_state_alloc(tuninfo_len + sizeof(*slwt));
532 slwt = seg6_lwt_lwtunnel(newts);
534 err = dst_cache_init(&slwt->cache, GFP_ATOMIC);
540 memcpy(&slwt->tuninfo, tuninfo, tuninfo_len);
542 newts->type = LWTUNNEL_ENCAP_SEG6;
543 newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT;
545 if (tuninfo->mode != SEG6_IPTUN_MODE_L2ENCAP)
546 newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;
548 newts->headroom = seg6_lwt_headroom(tuninfo);
555 static void seg6_destroy_state(struct lwtunnel_state *lwt)
557 dst_cache_destroy(&seg6_lwt_lwtunnel(lwt)->cache);
560 static int seg6_fill_encap_info(struct sk_buff *skb,
561 struct lwtunnel_state *lwtstate)
563 struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate);
565 if (nla_put_srh(skb, SEG6_IPTUNNEL_SRH, tuninfo))
571 static int seg6_encap_nlsize(struct lwtunnel_state *lwtstate)
573 struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate);
575 return nla_total_size(SEG6_IPTUN_ENCAP_SIZE(tuninfo));
578 static int seg6_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
580 struct seg6_iptunnel_encap *a_hdr = seg6_encap_lwtunnel(a);
581 struct seg6_iptunnel_encap *b_hdr = seg6_encap_lwtunnel(b);
582 int len = SEG6_IPTUN_ENCAP_SIZE(a_hdr);
584 if (len != SEG6_IPTUN_ENCAP_SIZE(b_hdr))
587 return memcmp(a_hdr, b_hdr, len);
590 static const struct lwtunnel_encap_ops seg6_iptun_ops = {
591 .build_state = seg6_build_state,
592 .destroy_state = seg6_destroy_state,
593 .output = seg6_output,
595 .fill_encap = seg6_fill_encap_info,
596 .get_encap_size = seg6_encap_nlsize,
597 .cmp_encap = seg6_encap_cmp,
598 .owner = THIS_MODULE,
601 int __init seg6_iptunnel_init(void)
603 return lwtunnel_encap_add_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6);
606 void seg6_iptunnel_exit(void)
608 lwtunnel_encap_del_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6);