| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* SR-IPv6 implementation |
| 4 |
* |
| 5 |
* Author: |
| 6 |
* David Lebrun <[email protected]> |
| 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> |
| 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> |
| 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> |
| 25 |
#include <net/dst_cache.h> |
| 26 |
#ifdef CONFIG_IPV6_SEG6_HMAC |
| 27 |
#include <net/seg6_hmac.h> |
| 28 |
#endif |
| 29 |
#include <linux/netfilter.h> |
| 30 |
|
| 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 |
case SEG6_IPTUN_MODE_ENCAP_RED: |
| 40 |
head = sizeof(struct ipv6hdr); |
| 41 |
break; |
| 42 |
case SEG6_IPTUN_MODE_L2ENCAP: |
| 43 |
case SEG6_IPTUN_MODE_L2ENCAP_RED: |
| 44 |
return 0; |
| 45 |
} |
| 46 |
|
| 47 |
return ((tuninfo->srh->hdrlen + 1) << 3) + head; |
| 48 |
} |
| 49 |
|
| 50 |
struct seg6_lwt { |
| 51 |
struct dst_cache cache_input; |
| 52 |
struct dst_cache cache_output; |
| 53 |
struct seg6_iptunnel_encap tuninfo[]; |
| 54 |
}; |
| 55 |
|
| 56 |
static inline struct seg6_lwt *seg6_lwt_lwtunnel(struct lwtunnel_state *lwt) |
| 57 |
{ |
| 58 |
return (struct seg6_lwt *)lwt->data; |
| 59 |
} |
| 60 |
|
| 61 |
static inline struct seg6_iptunnel_encap * |
| 62 |
seg6_encap_lwtunnel(struct lwtunnel_state *lwt) |
| 63 |
{ |
| 64 |
return seg6_lwt_lwtunnel(lwt)->tuninfo; |
| 65 |
} |
| 66 |
|
| 67 |
static const struct nla_policy seg6_iptunnel_policy[SEG6_IPTUNNEL_MAX + 1] = { |
| 68 |
[SEG6_IPTUNNEL_SRH] = { .type = NLA_BINARY }, |
| 69 |
}; |
| 70 |
|
| 71 |
static int nla_put_srh(struct sk_buff *skb, int attrtype, |
| 72 |
struct seg6_iptunnel_encap *tuninfo) |
| 73 |
{ |
| 74 |
struct seg6_iptunnel_encap *data; |
| 75 |
struct nlattr *nla; |
| 76 |
int len; |
| 77 |
|
| 78 |
len = SEG6_IPTUN_ENCAP_SIZE(tuninfo); |
| 79 |
|
| 80 |
nla = nla_reserve(skb, attrtype, len); |
| 81 |
if (!nla) |
| 82 |
return -EMSGSIZE; |
| 83 |
|
| 84 |
data = nla_data(nla); |
| 85 |
memcpy(data, tuninfo, len); |
| 86 |
|
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
static void set_tun_src(struct net *net, struct net_device *dev, |
| 91 |
struct in6_addr *daddr, struct in6_addr *saddr) |
| 92 |
{ |
| 93 |
struct seg6_pernet_data *sdata = seg6_pernet(net); |
| 94 |
struct in6_addr *tun_src; |
| 95 |
|
| 96 |
rcu_read_lock(); |
| 97 |
|
| 98 |
tun_src = rcu_dereference(sdata->tun_src); |
| 99 |
|
| 100 |
if (!ipv6_addr_any(tun_src)) { |
| 101 |
memcpy(saddr, tun_src, sizeof(struct in6_addr)); |
| 102 |
} else { |
| 103 |
ipv6_dev_get_saddr(net, dev, daddr, IPV6_PREFER_SRC_PUBLIC, |
| 104 |
saddr); |
| 105 |
} |
| 106 |
|
| 107 |
rcu_read_unlock(); |
| 108 |
} |
| 109 |
|
| 110 |
/* Compute flowlabel for outer IPv6 header */ |
| 111 |
static __be32 seg6_make_flowlabel(struct net *net, struct sk_buff *skb, |
| 112 |
struct ipv6hdr *inner_hdr) |
| 113 |
{ |
| 114 |
int do_flowlabel = net->ipv6.sysctl.seg6_flowlabel; |
| 115 |
__be32 flowlabel = 0; |
| 116 |
u32 hash; |
| 117 |
|
| 118 |
if (do_flowlabel > 0) { |
| 119 |
hash = skb_get_hash(skb); |
| 120 |
hash = rol32(hash, 16); |
| 121 |
flowlabel = (__force __be32)hash & IPV6_FLOWLABEL_MASK; |
| 122 |
} else if (!do_flowlabel && skb->protocol == htons(ETH_P_IPV6)) { |
| 123 |
flowlabel = ip6_flowlabel(inner_hdr); |
| 124 |
} |
| 125 |
return flowlabel; |
| 126 |
} |
| 127 |
|
| 128 |
static int __seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_sr_hdr *osrh, |
| 129 |
int proto, struct dst_entry *cache_dst) |
| 130 |
{ |
| 131 |
struct dst_entry *dst = skb_dst(skb); |
| 132 |
struct net_device *dev = dst_dev(dst); |
| 133 |
struct net *net = dev_net(dev); |
| 134 |
struct ipv6hdr *hdr, *inner_hdr; |
| 135 |
struct ipv6_sr_hdr *isrh; |
| 136 |
int hdrlen, tot_len, err; |
| 137 |
__be32 flowlabel; |
| 138 |
|
| 139 |
hdrlen = (osrh->hdrlen + 1) << 3; |
| 140 |
tot_len = hdrlen + sizeof(*hdr); |
| 141 |
|
| 142 |
err = skb_cow_head(skb, tot_len + dst_dev_overhead(cache_dst, skb)); |
| 143 |
if (unlikely(err)) |
| 144 |
return err; |
| 145 |
|
| 146 |
inner_hdr = ipv6_hdr(skb); |
| 147 |
flowlabel = seg6_make_flowlabel(net, skb, inner_hdr); |
| 148 |
|
| 149 |
skb_push(skb, tot_len); |
| 150 |
skb_reset_network_header(skb); |
| 151 |
skb_mac_header_rebuild(skb); |
| 152 |
hdr = ipv6_hdr(skb); |
| 153 |
|
| 154 |
/* inherit tc, flowlabel and hlim |
| 155 |
* hlim will be decremented in ip6_forward() afterwards and |
| 156 |
* decapsulation will overwrite inner hlim with outer hlim |
| 157 |
*/ |
| 158 |
|
| 159 |
if (skb->protocol == htons(ETH_P_IPV6)) { |
| 160 |
ip6_flow_hdr(hdr, ip6_tclass(ip6_flowinfo(inner_hdr)), |
| 161 |
flowlabel); |
| 162 |
hdr->hop_limit = inner_hdr->hop_limit; |
| 163 |
} else { |
| 164 |
ip6_flow_hdr(hdr, 0, flowlabel); |
| 165 |
hdr->hop_limit = ip6_dst_hoplimit(skb_dst(skb)); |
| 166 |
|
| 167 |
memset(IP6CB(skb), 0, sizeof(*IP6CB(skb))); |
| 168 |
|
| 169 |
/* the control block has been erased, so we have to set the |
| 170 |
* iif once again. |
| 171 |
* We read the receiving interface index directly from the |
| 172 |
* skb->skb_iif as it is done in the IPv4 receiving path (i.e.: |
| 173 |
* ip_rcv_core(...)). |
| 174 |
*/ |
| 175 |
IP6CB(skb)->iif = skb->skb_iif; |
| 176 |
} |
| 177 |
|
| 178 |
hdr->nexthdr = NEXTHDR_ROUTING; |
| 179 |
|
| 180 |
isrh = (void *)hdr + sizeof(*hdr); |
| 181 |
memcpy(isrh, osrh, hdrlen); |
| 182 |
|
| 183 |
isrh->nexthdr = proto; |
| 184 |
|
| 185 |
hdr->daddr = isrh->segments[isrh->first_segment]; |
| 186 |
set_tun_src(net, dev, &hdr->daddr, &hdr->saddr); |
| 187 |
|
| 188 |
#ifdef CONFIG_IPV6_SEG6_HMAC |
| 189 |
if (sr_has_hmac(isrh)) { |
| 190 |
err = seg6_push_hmac(net, &hdr->saddr, isrh); |
| 191 |
if (unlikely(err)) |
| 192 |
return err; |
| 193 |
} |
| 194 |
#endif |
| 195 |
|
| 196 |
hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); |
| 197 |
|
| 198 |
skb_postpush_rcsum(skb, hdr, tot_len); |
| 199 |
|
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
/* encapsulate an IPv6 packet within an outer IPv6 header with a given SRH */ |
| 204 |
int seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_sr_hdr *osrh, int proto) |
| 205 |
{ |
| 206 |
return __seg6_do_srh_encap(skb, osrh, proto, NULL); |
| 207 |
} |
| 208 |
EXPORT_SYMBOL_GPL(seg6_do_srh_encap); |
| 209 |
|
| 210 |
/* encapsulate an IPv6 packet within an outer IPv6 header with reduced SRH */ |
| 211 |
static int seg6_do_srh_encap_red(struct sk_buff *skb, |
| 212 |
struct ipv6_sr_hdr *osrh, int proto, |
| 213 |
struct dst_entry *cache_dst) |
| 214 |
{ |
| 215 |
__u8 first_seg = osrh->first_segment; |
| 216 |
struct dst_entry *dst = skb_dst(skb); |
| 217 |
struct net_device *dev = dst_dev(dst); |
| 218 |
struct net *net = dev_net(dev); |
| 219 |
struct ipv6hdr *hdr, *inner_hdr; |
| 220 |
int hdrlen = ipv6_optlen(osrh); |
| 221 |
int red_tlv_offset, tlv_offset; |
| 222 |
struct ipv6_sr_hdr *isrh; |
| 223 |
bool skip_srh = false; |
| 224 |
__be32 flowlabel; |
| 225 |
int tot_len, err; |
| 226 |
int red_hdrlen; |
| 227 |
int tlvs_len; |
| 228 |
|
| 229 |
if (first_seg > 0) { |
| 230 |
red_hdrlen = hdrlen - sizeof(struct in6_addr); |
| 231 |
} else { |
| 232 |
/* NOTE: if tag/flags and/or other TLVs are introduced in the |
| 233 |
* seg6_iptunnel infrastructure, they should be considered when |
| 234 |
* deciding to skip the SRH. |
| 235 |
*/ |
| 236 |
skip_srh = !sr_has_hmac(osrh); |
| 237 |
|
| 238 |
red_hdrlen = skip_srh ? 0 : hdrlen; |
| 239 |
} |
| 240 |
|
| 241 |
tot_len = red_hdrlen + sizeof(struct ipv6hdr); |
| 242 |
|
| 243 |
err = skb_cow_head(skb, tot_len + dst_dev_overhead(cache_dst, skb)); |
| 244 |
if (unlikely(err)) |
| 245 |
return err; |
| 246 |
|
| 247 |
inner_hdr = ipv6_hdr(skb); |
| 248 |
flowlabel = seg6_make_flowlabel(net, skb, inner_hdr); |
| 249 |
|
| 250 |
skb_push(skb, tot_len); |
| 251 |
skb_reset_network_header(skb); |
| 252 |
skb_mac_header_rebuild(skb); |
| 253 |
hdr = ipv6_hdr(skb); |
| 254 |
|
| 255 |
/* based on seg6_do_srh_encap() */ |
| 256 |
if (skb->protocol == htons(ETH_P_IPV6)) { |
| 257 |
ip6_flow_hdr(hdr, ip6_tclass(ip6_flowinfo(inner_hdr)), |
| 258 |
flowlabel); |
| 259 |
hdr->hop_limit = inner_hdr->hop_limit; |
| 260 |
} else { |
| 261 |
ip6_flow_hdr(hdr, 0, flowlabel); |
| 262 |
hdr->hop_limit = ip6_dst_hoplimit(skb_dst(skb)); |
| 263 |
|
| 264 |
memset(IP6CB(skb), 0, sizeof(*IP6CB(skb))); |
| 265 |
IP6CB(skb)->iif = skb->skb_iif; |
| 266 |
} |
| 267 |
|
| 268 |
/* no matter if we have to skip the SRH or not, the first segment |
| 269 |
* always comes in the pushed IPv6 header. |
| 270 |
*/ |
| 271 |
hdr->daddr = osrh->segments[first_seg]; |
| 272 |
|
| 273 |
if (skip_srh) { |
| 274 |
hdr->nexthdr = proto; |
| 275 |
|
| 276 |
set_tun_src(net, dev, &hdr->daddr, &hdr->saddr); |
| 277 |
goto out; |
| 278 |
} |
| 279 |
|
| 280 |
/* we cannot skip the SRH, slow path */ |
| 281 |
|
| 282 |
hdr->nexthdr = NEXTHDR_ROUTING; |
| 283 |
isrh = (void *)hdr + sizeof(struct ipv6hdr); |
| 284 |
|
| 285 |
if (unlikely(!first_seg)) { |
| 286 |
/* this is a very rare case; we have only one SID but |
| 287 |
* we cannot skip the SRH since we are carrying some |
| 288 |
* other info. |
| 289 |
*/ |
| 290 |
memcpy(isrh, osrh, hdrlen); |
| 291 |
goto srcaddr; |
| 292 |
} |
| 293 |
|
| 294 |
tlv_offset = sizeof(*osrh) + (first_seg + 1) * sizeof(struct in6_addr); |
| 295 |
red_tlv_offset = tlv_offset - sizeof(struct in6_addr); |
| 296 |
|
| 297 |
memcpy(isrh, osrh, red_tlv_offset); |
| 298 |
|
| 299 |
tlvs_len = hdrlen - tlv_offset; |
| 300 |
if (unlikely(tlvs_len > 0)) { |
| 301 |
const void *s = (const void *)osrh + tlv_offset; |
| 302 |
void *d = (void *)isrh + red_tlv_offset; |
| 303 |
|
| 304 |
memcpy(d, s, tlvs_len); |
| 305 |
} |
| 306 |
|
| 307 |
--isrh->first_segment; |
| 308 |
isrh->hdrlen -= 2; |
| 309 |
|
| 310 |
srcaddr: |
| 311 |
isrh->nexthdr = proto; |
| 312 |
set_tun_src(net, dev, &hdr->daddr, &hdr->saddr); |
| 313 |
|
| 314 |
#ifdef CONFIG_IPV6_SEG6_HMAC |
| 315 |
if (unlikely(!skip_srh && sr_has_hmac(isrh))) { |
| 316 |
err = seg6_push_hmac(net, &hdr->saddr, isrh); |
| 317 |
if (unlikely(err)) |
| 318 |
return err; |
| 319 |
} |
| 320 |
#endif |
| 321 |
|
| 322 |
out: |
| 323 |
hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); |
| 324 |
|
| 325 |
skb_postpush_rcsum(skb, hdr, tot_len); |
| 326 |
|
| 327 |
return 0; |
| 328 |
} |
| 329 |
|
| 330 |
static int __seg6_do_srh_inline(struct sk_buff *skb, struct ipv6_sr_hdr *osrh, |
| 331 |
struct dst_entry *cache_dst) |
| 332 |
{ |
| 333 |
struct ipv6hdr *hdr, *oldhdr; |
| 334 |
struct ipv6_sr_hdr *isrh; |
| 335 |
int hdrlen, err; |
| 336 |
|
| 337 |
hdrlen = (osrh->hdrlen + 1) << 3; |
| 338 |
|
| 339 |
err = skb_cow_head(skb, hdrlen + dst_dev_overhead(cache_dst, skb)); |
| 340 |
if (unlikely(err)) |
| 341 |
return err; |
| 342 |
|
| 343 |
oldhdr = ipv6_hdr(skb); |
| 344 |
|
| 345 |
skb_pull(skb, sizeof(struct ipv6hdr)); |
| 346 |
skb_postpull_rcsum(skb, skb_network_header(skb), |
| 347 |
sizeof(struct ipv6hdr)); |
| 348 |
|
| 349 |
skb_push(skb, sizeof(struct ipv6hdr) + hdrlen); |
| 350 |
skb_reset_network_header(skb); |
| 351 |
skb_mac_header_rebuild(skb); |
| 352 |
|
| 353 |
hdr = ipv6_hdr(skb); |
| 354 |
|
| 355 |
memmove(hdr, oldhdr, sizeof(*hdr)); |
| 356 |
|
| 357 |
isrh = (void *)hdr + sizeof(*hdr); |
| 358 |
memcpy(isrh, osrh, hdrlen); |
| 359 |
|
| 360 |
isrh->nexthdr = hdr->nexthdr; |
| 361 |
hdr->nexthdr = NEXTHDR_ROUTING; |
| 362 |
|
| 363 |
isrh->segments[0] = hdr->daddr; |
| 364 |
hdr->daddr = isrh->segments[isrh->first_segment]; |
| 365 |
|
| 366 |
#ifdef CONFIG_IPV6_SEG6_HMAC |
| 367 |
if (sr_has_hmac(isrh)) { |
| 368 |
struct net *net = skb_dst_dev_net(skb); |
| 369 |
|
| 370 |
err = seg6_push_hmac(net, &hdr->saddr, isrh); |
| 371 |
if (unlikely(err)) |
| 372 |
return err; |
| 373 |
} |
| 374 |
#endif |
| 375 |
|
| 376 |
hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); |
| 377 |
|
| 378 |
skb_postpush_rcsum(skb, hdr, sizeof(struct ipv6hdr) + hdrlen); |
| 379 |
|
| 380 |
return 0; |
| 381 |
} |
| 382 |
|
| 383 |
static int seg6_do_srh(struct sk_buff *skb, struct dst_entry *cache_dst) |
| 384 |
{ |
| 385 |
struct dst_entry *dst = skb_dst(skb); |
| 386 |
struct seg6_iptunnel_encap *tinfo; |
| 387 |
int proto, err = 0; |
| 388 |
|
| 389 |
tinfo = seg6_encap_lwtunnel(dst->lwtstate); |
| 390 |
|
| 391 |
switch (tinfo->mode) { |
| 392 |
case SEG6_IPTUN_MODE_INLINE: |
| 393 |
if (skb->protocol != htons(ETH_P_IPV6)) |
| 394 |
return -EINVAL; |
| 395 |
|
| 396 |
err = __seg6_do_srh_inline(skb, tinfo->srh, cache_dst); |
| 397 |
if (err) |
| 398 |
return err; |
| 399 |
break; |
| 400 |
case SEG6_IPTUN_MODE_ENCAP: |
| 401 |
case SEG6_IPTUN_MODE_ENCAP_RED: |
| 402 |
err = iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6); |
| 403 |
if (err) |
| 404 |
return err; |
| 405 |
|
| 406 |
if (skb->protocol == htons(ETH_P_IPV6)) |
| 407 |
proto = IPPROTO_IPV6; |
| 408 |
else if (skb->protocol == htons(ETH_P_IP)) |
| 409 |
proto = IPPROTO_IPIP; |
| 410 |
else |
| 411 |
return -EINVAL; |
| 412 |
|
| 413 |
if (tinfo->mode == SEG6_IPTUN_MODE_ENCAP) |
| 414 |
err = __seg6_do_srh_encap(skb, tinfo->srh, |
| 415 |
proto, cache_dst); |
| 416 |
else |
| 417 |
err = seg6_do_srh_encap_red(skb, tinfo->srh, |
| 418 |
proto, cache_dst); |
| 419 |
|
| 420 |
if (err) |
| 421 |
return err; |
| 422 |
|
| 423 |
skb_set_inner_transport_header(skb, skb_transport_offset(skb)); |
| 424 |
skb_set_inner_protocol(skb, skb->protocol); |
| 425 |
skb->protocol = htons(ETH_P_IPV6); |
| 426 |
break; |
| 427 |
case SEG6_IPTUN_MODE_L2ENCAP: |
| 428 |
case SEG6_IPTUN_MODE_L2ENCAP_RED: |
| 429 |
if (!skb_mac_header_was_set(skb)) |
| 430 |
return -EINVAL; |
| 431 |
|
| 432 |
if (pskb_expand_head(skb, skb->mac_len, 0, GFP_ATOMIC) < 0) |
| 433 |
return -ENOMEM; |
| 434 |
|
| 435 |
skb_mac_header_rebuild(skb); |
| 436 |
skb_push(skb, skb->mac_len); |
| 437 |
|
| 438 |
if (tinfo->mode == SEG6_IPTUN_MODE_L2ENCAP) |
| 439 |
err = __seg6_do_srh_encap(skb, tinfo->srh, |
| 440 |
IPPROTO_ETHERNET, |
| 441 |
cache_dst); |
| 442 |
else |
| 443 |
err = seg6_do_srh_encap_red(skb, tinfo->srh, |
| 444 |
IPPROTO_ETHERNET, |
| 445 |
cache_dst); |
| 446 |
|
| 447 |
if (err) |
| 448 |
return err; |
| 449 |
|
| 450 |
skb->protocol = htons(ETH_P_IPV6); |
| 451 |
break; |
| 452 |
} |
| 453 |
|
| 454 |
skb_set_transport_header(skb, sizeof(struct ipv6hdr)); |
| 455 |
nf_reset_ct(skb); |
| 456 |
|
| 457 |
return 0; |
| 458 |
} |
| 459 |
|
| 460 |
/* insert an SRH within an IPv6 packet, just after the IPv6 header */ |
| 461 |
int seg6_do_srh_inline(struct sk_buff *skb, struct ipv6_sr_hdr *osrh) |
| 462 |
{ |
| 463 |
return __seg6_do_srh_inline(skb, osrh, NULL); |
| 464 |
} |
| 465 |
EXPORT_SYMBOL_GPL(seg6_do_srh_inline); |
| 466 |
|
| 467 |
static int seg6_input_finish(struct net *net, struct sock *sk, |
| 468 |
struct sk_buff *skb) |
| 469 |
{ |
| 470 |
return dst_input(skb); |
| 471 |
} |
| 472 |
|
| 473 |
static int seg6_input_core(struct net *net, struct sock *sk, |
| 474 |
struct sk_buff *skb) |
| 475 |
{ |
| 476 |
struct dst_entry *orig_dst = skb_dst(skb); |
| 477 |
struct dst_entry *dst = NULL; |
| 478 |
struct lwtunnel_state *lwtst; |
| 479 |
struct seg6_lwt *slwt; |
| 480 |
int err; |
| 481 |
|
| 482 |
/* We cannot dereference "orig_dst" once ip6_route_input() or |
| 483 |
* skb_dst_drop() is called. However, in order to detect a dst loop, we |
| 484 |
* need the address of its lwtstate. So, save the address of lwtstate |
| 485 |
* now and use it later as a comparison. |
| 486 |
*/ |
| 487 |
lwtst = orig_dst->lwtstate; |
| 488 |
|
| 489 |
slwt = seg6_lwt_lwtunnel(lwtst); |
| 490 |
|
| 491 |
local_bh_disable(); |
| 492 |
dst = dst_cache_get(&slwt->cache_input); |
| 493 |
local_bh_enable(); |
| 494 |
|
| 495 |
err = seg6_do_srh(skb, dst); |
| 496 |
if (unlikely(err)) { |
| 497 |
dst_release(dst); |
| 498 |
goto drop; |
| 499 |
} |
| 500 |
|
| 501 |
if (!dst) { |
| 502 |
ip6_route_input(skb); |
| 503 |
|
| 504 |
/* ip6_route_input() sets a NOREF dst; force a refcount on it |
| 505 |
* before caching or further use. |
| 506 |
*/ |
| 507 |
skb_dst_force(skb); |
| 508 |
dst = skb_dst(skb); |
| 509 |
if (unlikely(!dst)) { |
| 510 |
err = -ENETUNREACH; |
| 511 |
goto drop; |
| 512 |
} |
| 513 |
|
| 514 |
/* cache only if we don't create a dst reference loop */ |
| 515 |
if (!dst->error && lwtst != dst->lwtstate) { |
| 516 |
local_bh_disable(); |
| 517 |
dst_cache_set_ip6(&slwt->cache_input, dst, |
| 518 |
&ipv6_hdr(skb)->saddr); |
| 519 |
local_bh_enable(); |
| 520 |
} |
| 521 |
|
| 522 |
err = skb_cow_head(skb, LL_RESERVED_SPACE(dst_dev(dst))); |
| 523 |
if (unlikely(err)) |
| 524 |
goto drop; |
| 525 |
} else { |
| 526 |
skb_dst_drop(skb); |
| 527 |
skb_dst_set(skb, dst); |
| 528 |
} |
| 529 |
|
| 530 |
if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled)) |
| 531 |
return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, |
| 532 |
dev_net(skb->dev), NULL, skb, NULL, |
| 533 |
skb_dst_dev(skb), seg6_input_finish); |
| 534 |
|
| 535 |
return seg6_input_finish(dev_net(skb->dev), NULL, skb); |
| 536 |
drop: |
| 537 |
kfree_skb(skb); |
| 538 |
return err; |
| 539 |
} |
| 540 |
|
| 541 |
static int seg6_input_nf(struct sk_buff *skb) |
| 542 |
{ |
| 543 |
struct net_device *dev = skb_dst_dev(skb); |
| 544 |
struct net *net = dev_net(skb->dev); |
| 545 |
|
| 546 |
switch (skb->protocol) { |
| 547 |
case htons(ETH_P_IP): |
| 548 |
return NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, net, NULL, |
| 549 |
skb, NULL, dev, seg6_input_core); |
| 550 |
case htons(ETH_P_IPV6): |
| 551 |
return NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, net, NULL, |
| 552 |
skb, NULL, dev, seg6_input_core); |
| 553 |
} |
| 554 |
|
| 555 |
return -EINVAL; |
| 556 |
} |
| 557 |
|
| 558 |
static int seg6_input(struct sk_buff *skb) |
| 559 |
{ |
| 560 |
if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled)) |
| 561 |
return seg6_input_nf(skb); |
| 562 |
|
| 563 |
return seg6_input_core(dev_net(skb->dev), NULL, skb); |
| 564 |
} |
| 565 |
|
| 566 |
static int seg6_output_core(struct net *net, struct sock *sk, |
| 567 |
struct sk_buff *skb) |
| 568 |
{ |
| 569 |
struct dst_entry *orig_dst = skb_dst(skb); |
| 570 |
struct dst_entry *dst = NULL; |
| 571 |
struct seg6_lwt *slwt; |
| 572 |
int err; |
| 573 |
|
| 574 |
slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate); |
| 575 |
|
| 576 |
local_bh_disable(); |
| 577 |
dst = dst_cache_get(&slwt->cache_output); |
| 578 |
local_bh_enable(); |
| 579 |
|
| 580 |
err = seg6_do_srh(skb, dst); |
| 581 |
if (unlikely(err)) |
| 582 |
goto drop; |
| 583 |
|
| 584 |
if (unlikely(!dst)) { |
| 585 |
struct ipv6hdr *hdr = ipv6_hdr(skb); |
| 586 |
struct flowi6 fl6; |
| 587 |
|
| 588 |
memset(&fl6, 0, sizeof(fl6)); |
| 589 |
fl6.daddr = hdr->daddr; |
| 590 |
fl6.saddr = hdr->saddr; |
| 591 |
fl6.flowlabel = ip6_flowinfo(hdr); |
| 592 |
fl6.flowi6_mark = skb->mark; |
| 593 |
fl6.flowi6_proto = hdr->nexthdr; |
| 594 |
|
| 595 |
dst = ip6_route_output(net, NULL, &fl6); |
| 596 |
if (dst->error) { |
| 597 |
err = dst->error; |
| 598 |
goto drop; |
| 599 |
} |
| 600 |
|
| 601 |
/* cache only if we don't create a dst reference loop */ |
| 602 |
if (orig_dst->lwtstate != dst->lwtstate) { |
| 603 |
local_bh_disable(); |
| 604 |
dst_cache_set_ip6(&slwt->cache_output, dst, &fl6.saddr); |
| 605 |
local_bh_enable(); |
| 606 |
} |
| 607 |
|
| 608 |
err = skb_cow_head(skb, LL_RESERVED_SPACE(dst_dev(dst))); |
| 609 |
if (unlikely(err)) |
| 610 |
goto drop; |
| 611 |
} |
| 612 |
|
| 613 |
skb_dst_drop(skb); |
| 614 |
skb_dst_set(skb, dst); |
| 615 |
|
| 616 |
if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled)) |
| 617 |
return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb, |
| 618 |
NULL, dst_dev(dst), dst_output); |
| 619 |
|
| 620 |
return dst_output(net, sk, skb); |
| 621 |
drop: |
| 622 |
dst_release(dst); |
| 623 |
kfree_skb(skb); |
| 624 |
return err; |
| 625 |
} |
| 626 |
|
| 627 |
static int seg6_output_nf(struct net *net, struct sock *sk, struct sk_buff *skb) |
| 628 |
{ |
| 629 |
struct net_device *dev = skb_dst_dev(skb); |
| 630 |
|
| 631 |
switch (skb->protocol) { |
| 632 |
case htons(ETH_P_IP): |
| 633 |
return NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, net, sk, skb, |
| 634 |
NULL, dev, seg6_output_core); |
| 635 |
case htons(ETH_P_IPV6): |
| 636 |
return NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, net, sk, skb, |
| 637 |
NULL, dev, seg6_output_core); |
| 638 |
} |
| 639 |
|
| 640 |
return -EINVAL; |
| 641 |
} |
| 642 |
|
| 643 |
static int seg6_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
| 644 |
{ |
| 645 |
if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled)) |
| 646 |
return seg6_output_nf(net, sk, skb); |
| 647 |
|
| 648 |
return seg6_output_core(net, sk, skb); |
| 649 |
} |
| 650 |
|
| 651 |
static int seg6_build_state(struct net *net, struct nlattr *nla, |
| 652 |
unsigned int family, const void *cfg, |
| 653 |
struct lwtunnel_state **ts, |
| 654 |
struct netlink_ext_ack *extack) |
| 655 |
{ |
| 656 |
struct nlattr *tb[SEG6_IPTUNNEL_MAX + 1]; |
| 657 |
struct seg6_iptunnel_encap *tuninfo; |
| 658 |
struct lwtunnel_state *newts; |
| 659 |
int tuninfo_len, min_size; |
| 660 |
struct seg6_lwt *slwt; |
| 661 |
int err; |
| 662 |
|
| 663 |
if (family != AF_INET && family != AF_INET6) |
| 664 |
return -EINVAL; |
| 665 |
|
| 666 |
err = nla_parse_nested_deprecated(tb, SEG6_IPTUNNEL_MAX, nla, |
| 667 |
seg6_iptunnel_policy, extack); |
| 668 |
|
| 669 |
if (err < 0) |
| 670 |
return err; |
| 671 |
|
| 672 |
if (!tb[SEG6_IPTUNNEL_SRH]) |
| 673 |
return -EINVAL; |
| 674 |
|
| 675 |
tuninfo = nla_data(tb[SEG6_IPTUNNEL_SRH]); |
| 676 |
tuninfo_len = nla_len(tb[SEG6_IPTUNNEL_SRH]); |
| 677 |
|
| 678 |
/* tuninfo must contain at least the iptunnel encap structure, |
| 679 |
* the SRH and one segment |
| 680 |
*/ |
| 681 |
min_size = sizeof(*tuninfo) + sizeof(struct ipv6_sr_hdr) + |
| 682 |
sizeof(struct in6_addr); |
| 683 |
if (tuninfo_len < min_size) |
| 684 |
return -EINVAL; |
| 685 |
|
| 686 |
switch (tuninfo->mode) { |
| 687 |
case SEG6_IPTUN_MODE_INLINE: |
| 688 |
if (family != AF_INET6) |
| 689 |
return -EINVAL; |
| 690 |
|
| 691 |
break; |
| 692 |
case SEG6_IPTUN_MODE_ENCAP: |
| 693 |
break; |
| 694 |
case SEG6_IPTUN_MODE_L2ENCAP: |
| 695 |
break; |
| 696 |
case SEG6_IPTUN_MODE_ENCAP_RED: |
| 697 |
break; |
| 698 |
case SEG6_IPTUN_MODE_L2ENCAP_RED: |
| 699 |
break; |
| 700 |
default: |
| 701 |
return -EINVAL; |
| 702 |
} |
| 703 |
|
| 704 |
/* verify that SRH is consistent */ |
| 705 |
if (!seg6_validate_srh(tuninfo->srh, tuninfo_len - sizeof(*tuninfo), false)) |
| 706 |
return -EINVAL; |
| 707 |
|
| 708 |
newts = lwtunnel_state_alloc(tuninfo_len + sizeof(*slwt)); |
| 709 |
if (!newts) |
| 710 |
return -ENOMEM; |
| 711 |
|
| 712 |
slwt = seg6_lwt_lwtunnel(newts); |
| 713 |
|
| 714 |
err = dst_cache_init(&slwt->cache_input, GFP_ATOMIC); |
| 715 |
if (err) |
| 716 |
goto err_free_newts; |
| 717 |
|
| 718 |
err = dst_cache_init(&slwt->cache_output, GFP_ATOMIC); |
| 719 |
if (err) |
| 720 |
goto err_destroy_input; |
| 721 |
|
| 722 |
memcpy(&slwt->tuninfo, tuninfo, tuninfo_len); |
| 723 |
|
| 724 |
newts->type = LWTUNNEL_ENCAP_SEG6; |
| 725 |
newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT; |
| 726 |
|
| 727 |
if (tuninfo->mode != SEG6_IPTUN_MODE_L2ENCAP && |
| 728 |
tuninfo->mode != SEG6_IPTUN_MODE_L2ENCAP_RED) |
| 729 |
newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT; |
| 730 |
|
| 731 |
newts->headroom = seg6_lwt_headroom(tuninfo); |
| 732 |
|
| 733 |
*ts = newts; |
| 734 |
|
| 735 |
return 0; |
| 736 |
|
| 737 |
err_destroy_input: |
| 738 |
dst_cache_destroy(&slwt->cache_input); |
| 739 |
err_free_newts: |
| 740 |
kfree(newts); |
| 741 |
return err; |
| 742 |
} |
| 743 |
|
| 744 |
static void seg6_destroy_state(struct lwtunnel_state *lwt) |
| 745 |
{ |
| 746 |
struct seg6_lwt *slwt = seg6_lwt_lwtunnel(lwt); |
| 747 |
|
| 748 |
dst_cache_destroy(&slwt->cache_input); |
| 749 |
dst_cache_destroy(&slwt->cache_output); |
| 750 |
} |
| 751 |
|
| 752 |
static int seg6_fill_encap_info(struct sk_buff *skb, |
| 753 |
struct lwtunnel_state *lwtstate) |
| 754 |
{ |
| 755 |
struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate); |
| 756 |
|
| 757 |
if (nla_put_srh(skb, SEG6_IPTUNNEL_SRH, tuninfo)) |
| 758 |
return -EMSGSIZE; |
| 759 |
|
| 760 |
return 0; |
| 761 |
} |
| 762 |
|
| 763 |
static int seg6_encap_nlsize(struct lwtunnel_state *lwtstate) |
| 764 |
{ |
| 765 |
struct seg6_iptunnel_encap *tuninfo = seg6_encap_lwtunnel(lwtstate); |
| 766 |
|
| 767 |
return nla_total_size(SEG6_IPTUN_ENCAP_SIZE(tuninfo)); |
| 768 |
} |
| 769 |
|
| 770 |
static int seg6_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b) |
| 771 |
{ |
| 772 |
struct seg6_iptunnel_encap *a_hdr = seg6_encap_lwtunnel(a); |
| 773 |
struct seg6_iptunnel_encap *b_hdr = seg6_encap_lwtunnel(b); |
| 774 |
int len = SEG6_IPTUN_ENCAP_SIZE(a_hdr); |
| 775 |
|
| 776 |
if (len != SEG6_IPTUN_ENCAP_SIZE(b_hdr)) |
| 777 |
return 1; |
| 778 |
|
| 779 |
return memcmp(a_hdr, b_hdr, len); |
| 780 |
} |
| 781 |
|
| 782 |
static const struct lwtunnel_encap_ops seg6_iptun_ops = { |
| 783 |
.build_state = seg6_build_state, |
| 784 |
.destroy_state = seg6_destroy_state, |
| 785 |
.output = seg6_output, |
| 786 |
.input = seg6_input, |
| 787 |
.fill_encap = seg6_fill_encap_info, |
| 788 |
.get_encap_size = seg6_encap_nlsize, |
| 789 |
.cmp_encap = seg6_encap_cmp, |
| 790 |
.owner = THIS_MODULE, |
| 791 |
}; |
| 792 |
|
| 793 |
int __init seg6_iptunnel_init(void) |
| 794 |
{ |
| 795 |
return lwtunnel_encap_add_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6); |
| 796 |
} |
| 797 |
|
| 798 |
void seg6_iptunnel_exit(void) |
| 799 |
{ |
| 800 |
lwtunnel_encap_del_ops(&seg6_iptun_ops, LWTUNNEL_ENCAP_SEG6); |
| 801 |
} |