Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 2 | /* |
| 3 | * SR-IPv6 implementation |
| 4 | * |
| 5 | * Author: |
| 6 | * David Lebrun <david.lebrun@uclouvain.be> |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 7 | */ |
| 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 Lebrun | 5807b22 | 2018-03-29 17:59:36 +0100 | [diff] [blame] | 14 | #include <net/ip_tunnels.h> |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 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> |
| 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 Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 25 | #include <net/dst_cache.h> |
David Lebrun | 9baee83 | 2016-11-08 14:59:19 +0100 | [diff] [blame] | 26 | #ifdef CONFIG_IPV6_SEG6_HMAC |
| 27 | #include <net/seg6_hmac.h> |
| 28 | #endif |
Ryoga Saito | 7a3f5b0 | 2021-08-17 08:39:37 +0000 | [diff] [blame] | 29 | #include <linux/netfilter.h> |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 30 | |
Ioana-Ruxandra Stăncioi | 88fab21 | 2020-08-03 07:33:33 +0000 | [diff] [blame] | 31 | static 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 Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 48 | struct seg6_lwt { |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 49 | struct dst_cache cache; |
Gustavo A. R. Silva | b0c9a2d | 2020-02-28 07:36:41 -0600 | [diff] [blame] | 50 | struct seg6_iptunnel_encap tuninfo[]; |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | static inline struct seg6_lwt *seg6_lwt_lwtunnel(struct lwtunnel_state *lwt) |
| 54 | { |
| 55 | return (struct seg6_lwt *)lwt->data; |
| 56 | } |
| 57 | |
| 58 | static inline struct seg6_iptunnel_encap * |
| 59 | seg6_encap_lwtunnel(struct lwtunnel_state *lwt) |
| 60 | { |
| 61 | return seg6_lwt_lwtunnel(lwt)->tuninfo; |
| 62 | } |
| 63 | |
| 64 | static const struct nla_policy seg6_iptunnel_policy[SEG6_IPTUNNEL_MAX + 1] = { |
| 65 | [SEG6_IPTUNNEL_SRH] = { .type = NLA_BINARY }, |
| 66 | }; |
| 67 | |
Wei Yongjun | bb4005b | 2017-02-06 16:15:05 +0000 | [diff] [blame] | 68 | static int nla_put_srh(struct sk_buff *skb, int attrtype, |
| 69 | struct seg6_iptunnel_encap *tuninfo) |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 70 | { |
| 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 | |
| 87 | static 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 Abdelsalam | b5facfd | 2018-04-24 20:23:16 +0200 | [diff] [blame] | 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) |
| 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 King | 3ee593a | 2018-07-17 16:52:54 +0100 | [diff] [blame] | 117 | hash = rol32(hash, 16); |
Ahmed Abdelsalam | b5facfd | 2018-04-24 20:23:16 +0200 | [diff] [blame] | 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); |
| 121 | } |
| 122 | return flowlabel; |
| 123 | } |
| 124 | |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 125 | /* encapsulate an IPv6 packet within an outer IPv6 header with a given SRH */ |
David Lebrun | 32d99d0 | 2017-08-25 09:56:44 +0200 | [diff] [blame] | 126 | int seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_sr_hdr *osrh, int proto) |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 127 | { |
David Lebrun | 8936ef7 | 2018-03-20 14:44:56 +0000 | [diff] [blame] | 128 | struct dst_entry *dst = skb_dst(skb); |
| 129 | struct net *net = dev_net(dst->dev); |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 130 | struct ipv6hdr *hdr, *inner_hdr; |
| 131 | struct ipv6_sr_hdr *isrh; |
| 132 | int hdrlen, tot_len, err; |
Ahmed Abdelsalam | b5facfd | 2018-04-24 20:23:16 +0200 | [diff] [blame] | 133 | __be32 flowlabel; |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 134 | |
| 135 | hdrlen = (osrh->hdrlen + 1) << 3; |
| 136 | tot_len = hdrlen + sizeof(*hdr); |
| 137 | |
Mathieu Xhonneux | bbb40a0 | 2018-05-25 13:29:41 +0100 | [diff] [blame] | 138 | err = skb_cow_head(skb, tot_len + skb->mac_len); |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 139 | if (unlikely(err)) |
| 140 | return err; |
| 141 | |
| 142 | inner_hdr = ipv6_hdr(skb); |
Ahmed Abdelsalam | 6df9346 | 2018-04-28 12:18:35 +0200 | [diff] [blame] | 143 | flowlabel = seg6_make_flowlabel(net, skb, inner_hdr); |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 144 | |
| 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 Lebrun | 32d99d0 | 2017-08-25 09:56:44 +0200 | [diff] [blame] | 154 | |
| 155 | if (skb->protocol == htons(ETH_P_IPV6)) { |
| 156 | ip6_flow_hdr(hdr, ip6_tclass(ip6_flowinfo(inner_hdr)), |
Ahmed Abdelsalam | b5facfd | 2018-04-24 20:23:16 +0200 | [diff] [blame] | 157 | flowlabel); |
David Lebrun | 32d99d0 | 2017-08-25 09:56:44 +0200 | [diff] [blame] | 158 | hdr->hop_limit = inner_hdr->hop_limit; |
| 159 | } else { |
Ahmed Abdelsalam | b5facfd | 2018-04-24 20:23:16 +0200 | [diff] [blame] | 160 | ip6_flow_hdr(hdr, 0, flowlabel); |
David Lebrun | 32d99d0 | 2017-08-25 09:56:44 +0200 | [diff] [blame] | 161 | hdr->hop_limit = ip6_dst_hoplimit(skb_dst(skb)); |
Yohei Kanemaru | ef48974 | 2019-01-29 15:52:34 +0900 | [diff] [blame] | 162 | |
| 163 | memset(IP6CB(skb), 0, sizeof(*IP6CB(skb))); |
Andrea Mayer | ae68d933 | 2021-12-08 20:54:09 +0100 | [diff] [blame] | 164 | |
| 165 | /* the control block has been erased, so we have to set the |
| 166 | * iif once again. |
| 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.: |
| 169 | * ip_rcv_core(...)). |
| 170 | */ |
| 171 | IP6CB(skb)->iif = skb->skb_iif; |
David Lebrun | 32d99d0 | 2017-08-25 09:56:44 +0200 | [diff] [blame] | 172 | } |
| 173 | |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 174 | hdr->nexthdr = NEXTHDR_ROUTING; |
| 175 | |
| 176 | isrh = (void *)hdr + sizeof(*hdr); |
| 177 | memcpy(isrh, osrh, hdrlen); |
| 178 | |
David Lebrun | 32d99d0 | 2017-08-25 09:56:44 +0200 | [diff] [blame] | 179 | isrh->nexthdr = proto; |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 180 | |
| 181 | hdr->daddr = isrh->segments[isrh->first_segment]; |
Ahmed Abdelsalam | a957fa1 | 2018-04-20 15:58:05 +0200 | [diff] [blame] | 182 | set_tun_src(net, dst->dev, &hdr->daddr, &hdr->saddr); |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 183 | |
David Lebrun | 9baee83 | 2016-11-08 14:59:19 +0100 | [diff] [blame] | 184 | #ifdef CONFIG_IPV6_SEG6_HMAC |
| 185 | if (sr_has_hmac(isrh)) { |
| 186 | err = seg6_push_hmac(net, &hdr->saddr, isrh); |
| 187 | if (unlikely(err)) |
| 188 | return err; |
| 189 | } |
| 190 | #endif |
| 191 | |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 192 | skb_postpush_rcsum(skb, hdr, tot_len); |
| 193 | |
| 194 | return 0; |
| 195 | } |
David Lebrun | b04c80d | 2017-08-05 12:38:25 +0200 | [diff] [blame] | 196 | EXPORT_SYMBOL_GPL(seg6_do_srh_encap); |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 197 | |
| 198 | /* insert an SRH within an IPv6 packet, just after the IPv6 header */ |
David Lebrun | b04c80d | 2017-08-05 12:38:25 +0200 | [diff] [blame] | 199 | int seg6_do_srh_inline(struct sk_buff *skb, struct ipv6_sr_hdr *osrh) |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 200 | { |
| 201 | struct ipv6hdr *hdr, *oldhdr; |
| 202 | struct ipv6_sr_hdr *isrh; |
| 203 | int hdrlen, err; |
| 204 | |
| 205 | hdrlen = (osrh->hdrlen + 1) << 3; |
| 206 | |
Mathieu Xhonneux | bbb40a0 | 2018-05-25 13:29:41 +0100 | [diff] [blame] | 207 | err = skb_cow_head(skb, hdrlen + skb->mac_len); |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 208 | if (unlikely(err)) |
| 209 | return err; |
| 210 | |
| 211 | oldhdr = ipv6_hdr(skb); |
| 212 | |
| 213 | skb_pull(skb, sizeof(struct ipv6hdr)); |
| 214 | skb_postpull_rcsum(skb, skb_network_header(skb), |
| 215 | sizeof(struct ipv6hdr)); |
| 216 | |
| 217 | skb_push(skb, sizeof(struct ipv6hdr) + hdrlen); |
| 218 | skb_reset_network_header(skb); |
| 219 | skb_mac_header_rebuild(skb); |
| 220 | |
| 221 | hdr = ipv6_hdr(skb); |
| 222 | |
| 223 | memmove(hdr, oldhdr, sizeof(*hdr)); |
| 224 | |
| 225 | isrh = (void *)hdr + sizeof(*hdr); |
| 226 | memcpy(isrh, osrh, hdrlen); |
| 227 | |
| 228 | isrh->nexthdr = hdr->nexthdr; |
| 229 | hdr->nexthdr = NEXTHDR_ROUTING; |
| 230 | |
| 231 | isrh->segments[0] = hdr->daddr; |
| 232 | hdr->daddr = isrh->segments[isrh->first_segment]; |
| 233 | |
David Lebrun | 9baee83 | 2016-11-08 14:59:19 +0100 | [diff] [blame] | 234 | #ifdef CONFIG_IPV6_SEG6_HMAC |
| 235 | if (sr_has_hmac(isrh)) { |
| 236 | struct net *net = dev_net(skb_dst(skb)->dev); |
| 237 | |
| 238 | err = seg6_push_hmac(net, &hdr->saddr, isrh); |
| 239 | if (unlikely(err)) |
| 240 | return err; |
| 241 | } |
| 242 | #endif |
| 243 | |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 244 | skb_postpush_rcsum(skb, hdr, sizeof(struct ipv6hdr) + hdrlen); |
| 245 | |
| 246 | return 0; |
| 247 | } |
David Lebrun | b04c80d | 2017-08-05 12:38:25 +0200 | [diff] [blame] | 248 | EXPORT_SYMBOL_GPL(seg6_do_srh_inline); |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 249 | |
| 250 | static int seg6_do_srh(struct sk_buff *skb) |
| 251 | { |
| 252 | struct dst_entry *dst = skb_dst(skb); |
| 253 | struct seg6_iptunnel_encap *tinfo; |
David Lebrun | 32d99d0 | 2017-08-25 09:56:44 +0200 | [diff] [blame] | 254 | int proto, err = 0; |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 255 | |
| 256 | tinfo = seg6_encap_lwtunnel(dst->lwtstate); |
| 257 | |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 258 | switch (tinfo->mode) { |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 259 | case SEG6_IPTUN_MODE_INLINE: |
David Lebrun | 32d99d0 | 2017-08-25 09:56:44 +0200 | [diff] [blame] | 260 | if (skb->protocol != htons(ETH_P_IPV6)) |
| 261 | return -EINVAL; |
| 262 | |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 263 | err = seg6_do_srh_inline(skb, tinfo->srh); |
David Lebrun | 32d99d0 | 2017-08-25 09:56:44 +0200 | [diff] [blame] | 264 | if (err) |
| 265 | return err; |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 266 | break; |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 267 | case SEG6_IPTUN_MODE_ENCAP: |
David Lebrun | 5807b22 | 2018-03-29 17:59:36 +0100 | [diff] [blame] | 268 | err = iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6); |
| 269 | if (err) |
| 270 | return err; |
| 271 | |
David Lebrun | 32d99d0 | 2017-08-25 09:56:44 +0200 | [diff] [blame] | 272 | if (skb->protocol == htons(ETH_P_IPV6)) |
| 273 | proto = IPPROTO_IPV6; |
| 274 | else if (skb->protocol == htons(ETH_P_IP)) |
| 275 | proto = IPPROTO_IPIP; |
| 276 | else |
| 277 | return -EINVAL; |
| 278 | |
| 279 | err = seg6_do_srh_encap(skb, tinfo->srh, proto); |
| 280 | if (err) |
| 281 | return err; |
| 282 | |
David Lebrun | 5807b22 | 2018-03-29 17:59:36 +0100 | [diff] [blame] | 283 | skb_set_inner_transport_header(skb, skb_transport_offset(skb)); |
| 284 | skb_set_inner_protocol(skb, skb->protocol); |
David Lebrun | 32d99d0 | 2017-08-25 09:56:44 +0200 | [diff] [blame] | 285 | skb->protocol = htons(ETH_P_IPV6); |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 286 | break; |
David Lebrun | 38ee7f2 | 2017-08-25 09:56:45 +0200 | [diff] [blame] | 287 | case SEG6_IPTUN_MODE_L2ENCAP: |
| 288 | if (!skb_mac_header_was_set(skb)) |
| 289 | return -EINVAL; |
| 290 | |
| 291 | if (pskb_expand_head(skb, skb->mac_len, 0, GFP_ATOMIC) < 0) |
| 292 | return -ENOMEM; |
| 293 | |
| 294 | skb_mac_header_rebuild(skb); |
| 295 | skb_push(skb, skb->mac_len); |
| 296 | |
Paolo Lungaroni | 2677625 | 2020-03-11 17:54:06 +0100 | [diff] [blame] | 297 | err = seg6_do_srh_encap(skb, tinfo->srh, IPPROTO_ETHERNET); |
David Lebrun | 38ee7f2 | 2017-08-25 09:56:45 +0200 | [diff] [blame] | 298 | if (err) |
| 299 | return err; |
| 300 | |
| 301 | skb->protocol = htons(ETH_P_IPV6); |
| 302 | break; |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 303 | } |
| 304 | |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 305 | ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); |
| 306 | skb_set_transport_header(skb, sizeof(struct ipv6hdr)); |
Ryoga Saito | 7a3f5b0 | 2021-08-17 08:39:37 +0000 | [diff] [blame] | 307 | nf_reset_ct(skb); |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 308 | |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 309 | return 0; |
| 310 | } |
| 311 | |
Ryoga Saito | 7a3f5b0 | 2021-08-17 08:39:37 +0000 | [diff] [blame] | 312 | static int seg6_input_finish(struct net *net, struct sock *sk, |
| 313 | struct sk_buff *skb) |
| 314 | { |
| 315 | return dst_input(skb); |
| 316 | } |
| 317 | |
| 318 | static int seg6_input_core(struct net *net, struct sock *sk, |
| 319 | struct sk_buff *skb) |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 320 | { |
David Lebrun | af4a220 | 2017-03-24 10:46:27 +0100 | [diff] [blame] | 321 | struct dst_entry *orig_dst = skb_dst(skb); |
| 322 | struct dst_entry *dst = NULL; |
| 323 | struct seg6_lwt *slwt; |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 324 | int err; |
| 325 | |
| 326 | err = seg6_do_srh(skb); |
| 327 | if (unlikely(err)) { |
| 328 | kfree_skb(skb); |
| 329 | return err; |
| 330 | } |
| 331 | |
David Lebrun | af4a220 | 2017-03-24 10:46:27 +0100 | [diff] [blame] | 332 | slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate); |
| 333 | |
David Lebrun | af4a220 | 2017-03-24 10:46:27 +0100 | [diff] [blame] | 334 | preempt_disable(); |
| 335 | dst = dst_cache_get(&slwt->cache); |
| 336 | preempt_enable(); |
David Lebrun | af4a220 | 2017-03-24 10:46:27 +0100 | [diff] [blame] | 337 | |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 338 | skb_dst_drop(skb); |
David Lebrun | af4a220 | 2017-03-24 10:46:27 +0100 | [diff] [blame] | 339 | |
| 340 | if (!dst) { |
| 341 | ip6_route_input(skb); |
David Lebrun | af4a220 | 2017-03-24 10:46:27 +0100 | [diff] [blame] | 342 | dst = skb_dst(skb); |
| 343 | if (!dst->error) { |
| 344 | preempt_disable(); |
| 345 | dst_cache_set_ip6(&slwt->cache, dst, |
| 346 | &ipv6_hdr(skb)->saddr); |
| 347 | preempt_enable(); |
| 348 | } |
David Lebrun | af4a220 | 2017-03-24 10:46:27 +0100 | [diff] [blame] | 349 | } else { |
| 350 | skb_dst_set(skb, dst); |
| 351 | } |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 352 | |
David Lebrun | af3b515 | 2017-04-16 12:27:14 +0200 | [diff] [blame] | 353 | err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); |
| 354 | if (unlikely(err)) |
| 355 | return err; |
| 356 | |
Ryoga Saito | 7a3f5b0 | 2021-08-17 08:39:37 +0000 | [diff] [blame] | 357 | if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled)) |
| 358 | return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, |
| 359 | dev_net(skb->dev), NULL, skb, NULL, |
| 360 | skb_dst(skb)->dev, seg6_input_finish); |
| 361 | |
| 362 | return seg6_input_finish(dev_net(skb->dev), NULL, skb); |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 363 | } |
| 364 | |
Ryoga Saito | 7a3f5b0 | 2021-08-17 08:39:37 +0000 | [diff] [blame] | 365 | static int seg6_input_nf(struct sk_buff *skb) |
| 366 | { |
| 367 | struct net_device *dev = skb_dst(skb)->dev; |
| 368 | struct net *net = dev_net(skb->dev); |
| 369 | |
| 370 | switch (skb->protocol) { |
| 371 | case htons(ETH_P_IP): |
| 372 | return NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, net, NULL, |
| 373 | skb, NULL, dev, seg6_input_core); |
| 374 | case htons(ETH_P_IPV6): |
| 375 | return NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, net, NULL, |
| 376 | skb, NULL, dev, seg6_input_core); |
| 377 | } |
| 378 | |
| 379 | return -EINVAL; |
| 380 | } |
| 381 | |
| 382 | static int seg6_input(struct sk_buff *skb) |
| 383 | { |
| 384 | if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled)) |
| 385 | return seg6_input_nf(skb); |
| 386 | |
| 387 | return seg6_input_core(dev_net(skb->dev), NULL, skb); |
| 388 | } |
| 389 | |
| 390 | static int seg6_output_core(struct net *net, struct sock *sk, |
| 391 | struct sk_buff *skb) |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 392 | { |
| 393 | struct dst_entry *orig_dst = skb_dst(skb); |
| 394 | struct dst_entry *dst = NULL; |
| 395 | struct seg6_lwt *slwt; |
Colin Ian King | bf0df73 | 2021-09-02 15:35:05 +0100 | [diff] [blame] | 396 | int err; |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 397 | |
| 398 | err = seg6_do_srh(skb); |
| 399 | if (unlikely(err)) |
| 400 | goto drop; |
| 401 | |
| 402 | slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate); |
| 403 | |
David Lebrun | fa79581 | 2017-01-12 21:30:01 +0100 | [diff] [blame] | 404 | preempt_disable(); |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 405 | dst = dst_cache_get(&slwt->cache); |
David Lebrun | fa79581 | 2017-01-12 21:30:01 +0100 | [diff] [blame] | 406 | preempt_enable(); |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 407 | |
| 408 | if (unlikely(!dst)) { |
| 409 | struct ipv6hdr *hdr = ipv6_hdr(skb); |
| 410 | struct flowi6 fl6; |
| 411 | |
Shmulik Ladkani | 1b4e5ad | 2018-12-07 09:50:17 +0200 | [diff] [blame] | 412 | memset(&fl6, 0, sizeof(fl6)); |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 413 | fl6.daddr = hdr->daddr; |
| 414 | fl6.saddr = hdr->saddr; |
| 415 | fl6.flowlabel = ip6_flowinfo(hdr); |
| 416 | fl6.flowi6_mark = skb->mark; |
| 417 | fl6.flowi6_proto = hdr->nexthdr; |
| 418 | |
| 419 | dst = ip6_route_output(net, NULL, &fl6); |
| 420 | if (dst->error) { |
| 421 | err = dst->error; |
| 422 | dst_release(dst); |
| 423 | goto drop; |
| 424 | } |
| 425 | |
David Lebrun | fa79581 | 2017-01-12 21:30:01 +0100 | [diff] [blame] | 426 | preempt_disable(); |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 427 | dst_cache_set_ip6(&slwt->cache, dst, &fl6.saddr); |
David Lebrun | fa79581 | 2017-01-12 21:30:01 +0100 | [diff] [blame] | 428 | preempt_enable(); |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | skb_dst_drop(skb); |
| 432 | skb_dst_set(skb, dst); |
| 433 | |
David Lebrun | af3b515 | 2017-04-16 12:27:14 +0200 | [diff] [blame] | 434 | err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); |
| 435 | if (unlikely(err)) |
| 436 | goto drop; |
| 437 | |
Ryoga Saito | 7a3f5b0 | 2021-08-17 08:39:37 +0000 | [diff] [blame] | 438 | if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled)) |
| 439 | return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb, |
| 440 | NULL, skb_dst(skb)->dev, dst_output); |
| 441 | |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 442 | return dst_output(net, sk, skb); |
| 443 | drop: |
| 444 | kfree_skb(skb); |
| 445 | return err; |
| 446 | } |
| 447 | |
Ryoga Saito | 7a3f5b0 | 2021-08-17 08:39:37 +0000 | [diff] [blame] | 448 | static int seg6_output_nf(struct net *net, struct sock *sk, struct sk_buff *skb) |
| 449 | { |
| 450 | struct net_device *dev = skb_dst(skb)->dev; |
| 451 | |
| 452 | switch (skb->protocol) { |
| 453 | case htons(ETH_P_IP): |
| 454 | return NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, net, sk, skb, |
| 455 | NULL, dev, seg6_output_core); |
| 456 | case htons(ETH_P_IPV6): |
| 457 | return NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, net, sk, skb, |
| 458 | NULL, dev, seg6_output_core); |
| 459 | } |
| 460 | |
| 461 | return -EINVAL; |
| 462 | } |
| 463 | |
| 464 | static int seg6_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
| 465 | { |
| 466 | if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled)) |
| 467 | return seg6_output_nf(net, sk, skb); |
| 468 | |
| 469 | return seg6_output_core(net, sk, skb); |
| 470 | } |
| 471 | |
Alexander Aring | faee676 | 2020-03-27 18:00:21 -0400 | [diff] [blame] | 472 | static int seg6_build_state(struct net *net, struct nlattr *nla, |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 473 | unsigned int family, const void *cfg, |
David Ahern | 9ae2872 | 2017-05-27 16:19:28 -0600 | [diff] [blame] | 474 | struct lwtunnel_state **ts, |
| 475 | struct netlink_ext_ack *extack) |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 476 | { |
| 477 | struct nlattr *tb[SEG6_IPTUNNEL_MAX + 1]; |
| 478 | struct seg6_iptunnel_encap *tuninfo; |
| 479 | struct lwtunnel_state *newts; |
| 480 | int tuninfo_len, min_size; |
| 481 | struct seg6_lwt *slwt; |
| 482 | int err; |
| 483 | |
David Lebrun | 32d99d0 | 2017-08-25 09:56:44 +0200 | [diff] [blame] | 484 | if (family != AF_INET && family != AF_INET6) |
| 485 | return -EINVAL; |
| 486 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 487 | err = nla_parse_nested_deprecated(tb, SEG6_IPTUNNEL_MAX, nla, |
| 488 | seg6_iptunnel_policy, extack); |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 489 | |
| 490 | if (err < 0) |
| 491 | return err; |
| 492 | |
| 493 | if (!tb[SEG6_IPTUNNEL_SRH]) |
| 494 | return -EINVAL; |
| 495 | |
| 496 | tuninfo = nla_data(tb[SEG6_IPTUNNEL_SRH]); |
| 497 | tuninfo_len = nla_len(tb[SEG6_IPTUNNEL_SRH]); |
| 498 | |
| 499 | /* tuninfo must contain at least the iptunnel encap structure, |
| 500 | * the SRH and one segment |
| 501 | */ |
| 502 | min_size = sizeof(*tuninfo) + sizeof(struct ipv6_sr_hdr) + |
| 503 | sizeof(struct in6_addr); |
| 504 | if (tuninfo_len < min_size) |
| 505 | return -EINVAL; |
| 506 | |
| 507 | switch (tuninfo->mode) { |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 508 | case SEG6_IPTUN_MODE_INLINE: |
David Lebrun | 32d99d0 | 2017-08-25 09:56:44 +0200 | [diff] [blame] | 509 | if (family != AF_INET6) |
| 510 | return -EINVAL; |
| 511 | |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 512 | break; |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 513 | case SEG6_IPTUN_MODE_ENCAP: |
| 514 | break; |
David Lebrun | 38ee7f2 | 2017-08-25 09:56:45 +0200 | [diff] [blame] | 515 | case SEG6_IPTUN_MODE_L2ENCAP: |
| 516 | break; |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 517 | default: |
| 518 | return -EINVAL; |
| 519 | } |
| 520 | |
| 521 | /* verify that SRH is consistent */ |
Ahmed Abdelsalam | bb986a5 | 2020-06-03 06:54:42 +0000 | [diff] [blame] | 522 | if (!seg6_validate_srh(tuninfo->srh, tuninfo_len - sizeof(*tuninfo), false)) |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 523 | return -EINVAL; |
| 524 | |
| 525 | newts = lwtunnel_state_alloc(tuninfo_len + sizeof(*slwt)); |
| 526 | if (!newts) |
| 527 | return -ENOMEM; |
| 528 | |
| 529 | slwt = seg6_lwt_lwtunnel(newts); |
| 530 | |
David Lebrun | 191f86c | 2018-03-20 14:44:55 +0000 | [diff] [blame] | 531 | err = dst_cache_init(&slwt->cache, GFP_ATOMIC); |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 532 | if (err) { |
| 533 | kfree(newts); |
| 534 | return err; |
| 535 | } |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 536 | |
| 537 | memcpy(&slwt->tuninfo, tuninfo, tuninfo_len); |
| 538 | |
| 539 | newts->type = LWTUNNEL_ENCAP_SEG6; |
David Lebrun | 38ee7f2 | 2017-08-25 09:56:45 +0200 | [diff] [blame] | 540 | newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT; |
| 541 | |
| 542 | if (tuninfo->mode != SEG6_IPTUN_MODE_L2ENCAP) |
| 543 | newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT; |
| 544 | |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 545 | newts->headroom = seg6_lwt_headroom(tuninfo); |
| 546 | |
| 547 | *ts = newts; |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 552 | static void seg6_destroy_state(struct lwtunnel_state *lwt) |
| 553 | { |
| 554 | dst_cache_destroy(&seg6_lwt_lwtunnel(lwt)->cache); |
| 555 | } |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 556 | |
| 557 | static int seg6_fill_encap_info(struct sk_buff *skb, |
| 558 | struct lwtunnel_state *lwtstate) |
| 559 | { |
| 560 | struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate); |
| 561 | |
| 562 | if (nla_put_srh(skb, SEG6_IPTUNNEL_SRH, tuninfo)) |
| 563 | return -EMSGSIZE; |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static int seg6_encap_nlsize(struct lwtunnel_state *lwtstate) |
| 569 | { |
| 570 | struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate); |
| 571 | |
| 572 | return nla_total_size(SEG6_IPTUN_ENCAP_SIZE(tuninfo)); |
| 573 | } |
| 574 | |
| 575 | static int seg6_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b) |
| 576 | { |
| 577 | struct seg6_iptunnel_encap *a_hdr = seg6_encap_lwtunnel(a); |
| 578 | struct seg6_iptunnel_encap *b_hdr = seg6_encap_lwtunnel(b); |
| 579 | int len = SEG6_IPTUN_ENCAP_SIZE(a_hdr); |
| 580 | |
| 581 | if (len != SEG6_IPTUN_ENCAP_SIZE(b_hdr)) |
| 582 | return 1; |
| 583 | |
| 584 | return memcmp(a_hdr, b_hdr, len); |
| 585 | } |
| 586 | |
| 587 | static const struct lwtunnel_encap_ops seg6_iptun_ops = { |
| 588 | .build_state = seg6_build_state, |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 589 | .destroy_state = seg6_destroy_state, |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 590 | .output = seg6_output, |
| 591 | .input = seg6_input, |
| 592 | .fill_encap = seg6_fill_encap_info, |
| 593 | .get_encap_size = seg6_encap_nlsize, |
| 594 | .cmp_encap = seg6_encap_cmp, |
Robert Shearman | 88ff733 | 2017-01-24 16:26:47 +0000 | [diff] [blame] | 595 | .owner = THIS_MODULE, |
David Lebrun | 6c8702c | 2016-11-08 14:57:41 +0100 | [diff] [blame] | 596 | }; |
| 597 | |
| 598 | int __init seg6_iptunnel_init(void) |
| 599 | { |
| 600 | return lwtunnel_encap_add_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6); |
| 601 | } |
| 602 | |
| 603 | void seg6_iptunnel_exit(void) |
| 604 | { |
| 605 | lwtunnel_encap_del_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6); |
| 606 | } |