1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * IPv6 IOAM Lightweight Tunnel implementation 4 * 5 * Author: 6 * Justin Iurman <justin.iurman@uliege.be> 7 */ 8 9 #include <linux/kernel.h> 10 #include <linux/skbuff.h> 11 #include <linux/net.h> 12 #include <linux/in6.h> 13 #include <linux/ioam6.h> 14 #include <linux/ioam6_iptunnel.h> 15 #include <net/dst.h> 16 #include <net/sock.h> 17 #include <net/lwtunnel.h> 18 #include <net/ioam6.h> 19 #include <net/netlink.h> 20 #include <net/ipv6.h> 21 #include <net/dst_cache.h> 22 #include <net/ip6_route.h> 23 #include <net/addrconf.h> 24 25 struct ioam6_lwt_encap { 26 struct ipv6_hopopt_hdr eh; 27 u8 pad[2]; /* 2-octet padding for 4n-alignment */ 28 struct ioam6_hdr ioamh; 29 struct ioam6_trace_hdr traceh; 30 } __packed; 31 32 struct ioam6_lwt_freq { 33 u32 k; 34 u32 n; 35 }; 36 37 struct ioam6_lwt { 38 struct rt6_info null_rt; 39 struct dst_cache cache; 40 struct ioam6_lwt_freq freq; 41 atomic_t pkt_cnt; 42 u8 mode; 43 bool has_tunsrc; 44 struct in6_addr tunsrc; 45 struct in6_addr tundst; 46 struct ioam6_lwt_encap tuninfo; 47 }; 48 49 static const struct netlink_range_validation freq_range = { 50 .min = IOAM6_IPTUNNEL_FREQ_MIN, 51 .max = IOAM6_IPTUNNEL_FREQ_MAX, 52 }; 53 54 static struct ioam6_lwt *ioam6_lwt_state(struct lwtunnel_state *lwt) 55 { 56 return (struct ioam6_lwt *)lwt->data; 57 } 58 59 static struct ioam6_lwt_encap *ioam6_lwt_info(struct lwtunnel_state *lwt) 60 { 61 return &ioam6_lwt_state(lwt)->tuninfo; 62 } 63 64 static struct ioam6_trace_hdr *ioam6_lwt_trace(struct lwtunnel_state *lwt) 65 { 66 return &(ioam6_lwt_state(lwt)->tuninfo.traceh); 67 } 68 69 static const struct nla_policy ioam6_iptunnel_policy[IOAM6_IPTUNNEL_MAX + 1] = { 70 [IOAM6_IPTUNNEL_FREQ_K] = NLA_POLICY_FULL_RANGE(NLA_U32, &freq_range), 71 [IOAM6_IPTUNNEL_FREQ_N] = NLA_POLICY_FULL_RANGE(NLA_U32, &freq_range), 72 [IOAM6_IPTUNNEL_MODE] = NLA_POLICY_RANGE(NLA_U8, 73 IOAM6_IPTUNNEL_MODE_MIN, 74 IOAM6_IPTUNNEL_MODE_MAX), 75 [IOAM6_IPTUNNEL_SRC] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 76 [IOAM6_IPTUNNEL_DST] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)), 77 [IOAM6_IPTUNNEL_TRACE] = NLA_POLICY_EXACT_LEN( 78 sizeof(struct ioam6_trace_hdr)), 79 }; 80 81 static bool ioam6_validate_trace_hdr(struct ioam6_trace_hdr *trace) 82 { 83 u32 fields; 84 85 if (!trace->type_be32 || !trace->remlen || 86 trace->remlen > IOAM6_TRACE_DATA_SIZE_MAX / 4 || 87 trace->type.bit12 | trace->type.bit13 | trace->type.bit14 | 88 trace->type.bit15 | trace->type.bit16 | trace->type.bit17 | 89 trace->type.bit18 | trace->type.bit19 | trace->type.bit20 | 90 trace->type.bit21 | trace->type.bit23) 91 return false; 92 93 fields = be32_to_cpu(trace->type_be32); 94 trace->nodelen = ioam6_trace_compute_nodelen(fields); 95 96 return true; 97 } 98 99 static int ioam6_build_state(struct net *net, struct nlattr *nla, 100 unsigned int family, const void *cfg, 101 struct lwtunnel_state **ts, 102 struct netlink_ext_ack *extack) 103 { 104 struct nlattr *tb[IOAM6_IPTUNNEL_MAX + 1]; 105 struct ioam6_lwt_encap *tuninfo; 106 struct ioam6_trace_hdr *trace; 107 struct lwtunnel_state *lwt; 108 struct ioam6_lwt *ilwt; 109 int len_aligned, err; 110 u32 freq_k, freq_n; 111 u8 mode; 112 113 if (family != AF_INET6) 114 return -EINVAL; 115 116 err = nla_parse_nested(tb, IOAM6_IPTUNNEL_MAX, nla, 117 ioam6_iptunnel_policy, extack); 118 if (err < 0) 119 return err; 120 121 if ((!tb[IOAM6_IPTUNNEL_FREQ_K] && tb[IOAM6_IPTUNNEL_FREQ_N]) || 122 (tb[IOAM6_IPTUNNEL_FREQ_K] && !tb[IOAM6_IPTUNNEL_FREQ_N])) { 123 NL_SET_ERR_MSG(extack, "freq: missing parameter"); 124 return -EINVAL; 125 } else if (!tb[IOAM6_IPTUNNEL_FREQ_K] && !tb[IOAM6_IPTUNNEL_FREQ_N]) { 126 freq_k = IOAM6_IPTUNNEL_FREQ_MIN; 127 freq_n = IOAM6_IPTUNNEL_FREQ_MIN; 128 } else { 129 freq_k = nla_get_u32(tb[IOAM6_IPTUNNEL_FREQ_K]); 130 freq_n = nla_get_u32(tb[IOAM6_IPTUNNEL_FREQ_N]); 131 132 if (freq_k > freq_n) { 133 NL_SET_ERR_MSG(extack, "freq: k > n is forbidden"); 134 return -EINVAL; 135 } 136 } 137 138 mode = nla_get_u8_default(tb[IOAM6_IPTUNNEL_MODE], 139 IOAM6_IPTUNNEL_MODE_INLINE); 140 141 if (tb[IOAM6_IPTUNNEL_SRC] && mode == IOAM6_IPTUNNEL_MODE_INLINE) { 142 NL_SET_ERR_MSG(extack, "no tunnel src expected with this mode"); 143 return -EINVAL; 144 } 145 146 if (!tb[IOAM6_IPTUNNEL_DST] && mode != IOAM6_IPTUNNEL_MODE_INLINE) { 147 NL_SET_ERR_MSG(extack, "this mode needs a tunnel destination"); 148 return -EINVAL; 149 } 150 151 if (!tb[IOAM6_IPTUNNEL_TRACE]) { 152 NL_SET_ERR_MSG(extack, "missing trace"); 153 return -EINVAL; 154 } 155 156 trace = nla_data(tb[IOAM6_IPTUNNEL_TRACE]); 157 if (!ioam6_validate_trace_hdr(trace)) { 158 NL_SET_ERR_MSG_ATTR(extack, tb[IOAM6_IPTUNNEL_TRACE], 159 "invalid trace validation"); 160 return -EINVAL; 161 } 162 163 len_aligned = ALIGN(trace->remlen * 4, 8); 164 lwt = lwtunnel_state_alloc(sizeof(*ilwt) + len_aligned); 165 if (!lwt) 166 return -ENOMEM; 167 168 ilwt = ioam6_lwt_state(lwt); 169 err = dst_cache_init(&ilwt->cache, GFP_ATOMIC); 170 if (err) 171 goto free_lwt; 172 173 /* This "fake" dst_entry will be stored in a dst_cache, which will call 174 * dst_hold() and dst_release() on it. We must ensure that dst_destroy() 175 * will never be called. For that, its initial refcount is 1 and +1 when 176 * it is stored in the cache. Then, +1/-1 each time we read the cache 177 * and release it. Long story short, we're fine. 178 */ 179 dst_init(&ilwt->null_rt.dst, NULL, NULL, DST_OBSOLETE_NONE, DST_NOCOUNT); 180 181 atomic_set(&ilwt->pkt_cnt, 0); 182 ilwt->freq.k = freq_k; 183 ilwt->freq.n = freq_n; 184 185 ilwt->mode = mode; 186 187 if (!tb[IOAM6_IPTUNNEL_SRC]) { 188 ilwt->has_tunsrc = false; 189 } else { 190 ilwt->has_tunsrc = true; 191 ilwt->tunsrc = nla_get_in6_addr(tb[IOAM6_IPTUNNEL_SRC]); 192 193 if (ipv6_addr_any(&ilwt->tunsrc)) { 194 NL_SET_ERR_MSG_ATTR(extack, tb[IOAM6_IPTUNNEL_SRC], 195 "invalid tunnel source address"); 196 err = -EINVAL; 197 goto free_cache; 198 } 199 } 200 201 if (tb[IOAM6_IPTUNNEL_DST]) { 202 ilwt->tundst = nla_get_in6_addr(tb[IOAM6_IPTUNNEL_DST]); 203 204 if (ipv6_addr_any(&ilwt->tundst)) { 205 NL_SET_ERR_MSG_ATTR(extack, tb[IOAM6_IPTUNNEL_DST], 206 "invalid tunnel dest address"); 207 err = -EINVAL; 208 goto free_cache; 209 } 210 } 211 212 tuninfo = ioam6_lwt_info(lwt); 213 tuninfo->eh.hdrlen = ((sizeof(*tuninfo) + len_aligned) >> 3) - 1; 214 tuninfo->pad[0] = IPV6_TLV_PADN; 215 tuninfo->ioamh.type = IOAM6_TYPE_PREALLOC; 216 tuninfo->ioamh.opt_type = IPV6_TLV_IOAM; 217 tuninfo->ioamh.opt_len = sizeof(tuninfo->ioamh) - 2 + sizeof(*trace) 218 + trace->remlen * 4; 219 220 memcpy(&tuninfo->traceh, trace, sizeof(*trace)); 221 222 if (len_aligned - trace->remlen * 4) { 223 tuninfo->traceh.data[trace->remlen * 4] = IPV6_TLV_PADN; 224 tuninfo->traceh.data[trace->remlen * 4 + 1] = 2; 225 } 226 227 lwt->type = LWTUNNEL_ENCAP_IOAM6; 228 lwt->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT; 229 230 *ts = lwt; 231 232 return 0; 233 free_cache: 234 dst_cache_destroy(&ilwt->cache); 235 free_lwt: 236 kfree(lwt); 237 return err; 238 } 239 240 static int ioam6_do_fill(struct net *net, struct sk_buff *skb) 241 { 242 struct ioam6_trace_hdr *trace; 243 struct ioam6_namespace *ns; 244 245 trace = (struct ioam6_trace_hdr *)(skb_transport_header(skb) 246 + sizeof(struct ipv6_hopopt_hdr) + 2 247 + sizeof(struct ioam6_hdr)); 248 249 ns = ioam6_namespace(net, trace->namespace_id); 250 if (ns) 251 ioam6_fill_trace_data(skb, ns, trace, false); 252 253 return 0; 254 } 255 256 static int ioam6_do_inline(struct net *net, struct sk_buff *skb, 257 const struct ioam6_lwt_encap *tuninfo, 258 struct dst_entry *cache_dst) 259 { 260 struct ipv6hdr *oldhdr, *hdr; 261 struct ipv6_opt_hdr *opt_hdr; 262 int hdrlen, err; 263 264 hdrlen = (tuninfo->eh.hdrlen + 1) << 3; 265 266 err = skb_cow_head(skb, hdrlen + dst_dev_overhead(cache_dst, skb)); 267 if (unlikely(err)) 268 return err; 269 270 oldhdr = ipv6_hdr(skb); 271 skb_pull(skb, sizeof(*oldhdr)); 272 skb_postpull_rcsum(skb, skb_network_header(skb), sizeof(*oldhdr)); 273 274 skb_push(skb, sizeof(*oldhdr) + hdrlen); 275 skb_reset_network_header(skb); 276 skb_mac_header_rebuild(skb); 277 278 hdr = ipv6_hdr(skb); 279 memmove(hdr, oldhdr, sizeof(*oldhdr)); 280 281 skb_set_transport_header(skb, sizeof(*hdr)); 282 skb_postpush_rcsum(skb, hdr, sizeof(*hdr) + hdrlen); 283 284 opt_hdr = (struct ipv6_opt_hdr *)skb_transport_header(skb); 285 memcpy(opt_hdr, tuninfo, hdrlen); 286 opt_hdr->nexthdr = hdr->nexthdr; 287 288 hdr->nexthdr = NEXTHDR_HOP; 289 hdr->payload_len = cpu_to_be16(skb->len - sizeof(*hdr)); 290 291 return ioam6_do_fill(net, skb); 292 } 293 294 static int ioam6_do_encap(struct net *net, struct sk_buff *skb, 295 const struct ioam6_lwt_encap *tuninfo, 296 bool has_tunsrc, 297 struct in6_addr *tunsrc, 298 struct in6_addr *tundst, 299 struct dst_entry *cache_dst) 300 { 301 struct dst_entry *dst = skb_dst(skb); 302 struct ipv6hdr *hdr, *inner_hdr; 303 struct ipv6_opt_hdr *opt_hdr; 304 int hdrlen, len, err; 305 306 hdrlen = (tuninfo->eh.hdrlen + 1) << 3; 307 len = sizeof(*hdr) + hdrlen; 308 309 err = skb_cow_head(skb, len + dst_dev_overhead(cache_dst, skb)); 310 if (unlikely(err)) 311 return err; 312 313 inner_hdr = ipv6_hdr(skb); 314 315 skb_push(skb, len); 316 skb_reset_network_header(skb); 317 skb_mac_header_rebuild(skb); 318 skb_set_transport_header(skb, sizeof(*hdr)); 319 320 opt_hdr = (struct ipv6_opt_hdr *)skb_transport_header(skb); 321 memcpy(opt_hdr, tuninfo, hdrlen); 322 opt_hdr->nexthdr = NEXTHDR_IPV6; 323 324 hdr = ipv6_hdr(skb); 325 memcpy(hdr, inner_hdr, sizeof(*hdr)); 326 327 hdr->nexthdr = NEXTHDR_HOP; 328 hdr->payload_len = cpu_to_be16(skb->len - sizeof(*hdr)); 329 hdr->daddr = *tundst; 330 331 if (has_tunsrc) 332 memcpy(&hdr->saddr, tunsrc, sizeof(*tunsrc)); 333 else 334 ipv6_dev_get_saddr(net, dst_dev(dst), &hdr->daddr, 335 IPV6_PREFER_SRC_PUBLIC, &hdr->saddr); 336 337 skb_postpush_rcsum(skb, hdr, len); 338 339 return ioam6_do_fill(net, skb); 340 } 341 342 static int ioam6_output(struct net *net, struct sock *sk, struct sk_buff *skb) 343 { 344 struct dst_entry *orig_dst = skb_dst(skb); 345 struct dst_entry *dst = NULL; 346 struct ioam6_lwt *ilwt; 347 int err = -EINVAL; 348 u32 pkt_cnt; 349 350 if (skb->protocol != htons(ETH_P_IPV6)) 351 goto drop; 352 353 ilwt = ioam6_lwt_state(orig_dst->lwtstate); 354 355 /* Check for insertion frequency (i.e., "k over n" insertions) */ 356 pkt_cnt = atomic_fetch_inc(&ilwt->pkt_cnt); 357 if (pkt_cnt % ilwt->freq.n >= ilwt->freq.k) 358 goto out; 359 360 local_bh_disable(); 361 dst = dst_cache_get(&ilwt->cache); 362 local_bh_enable(); 363 364 /* This is how we notify that the destination does not change after 365 * transformation and that we need to use orig_dst instead of the cache 366 */ 367 if (dst == &ilwt->null_rt.dst) { 368 dst_release(dst); 369 370 dst = orig_dst; 371 /* keep refcount balance: dst_release() is called at the end */ 372 dst_hold(dst); 373 } 374 375 switch (ilwt->mode) { 376 case IOAM6_IPTUNNEL_MODE_INLINE: 377 do_inline: 378 /* Direct insertion - if there is no Hop-by-Hop yet */ 379 if (ipv6_hdr(skb)->nexthdr == NEXTHDR_HOP) 380 goto out; 381 382 err = ioam6_do_inline(net, skb, &ilwt->tuninfo, dst); 383 if (unlikely(err)) 384 goto drop; 385 386 break; 387 case IOAM6_IPTUNNEL_MODE_ENCAP: 388 do_encap: 389 /* Encapsulation (ip6ip6) */ 390 err = ioam6_do_encap(net, skb, &ilwt->tuninfo, 391 ilwt->has_tunsrc, &ilwt->tunsrc, 392 &ilwt->tundst, dst); 393 if (unlikely(err)) 394 goto drop; 395 396 break; 397 case IOAM6_IPTUNNEL_MODE_AUTO: 398 /* Automatic (RFC8200 compliant): 399 * - local packets -> INLINE mode 400 * - in-transit packets -> ENCAP mode 401 */ 402 if (!skb->dev) 403 goto do_inline; 404 405 goto do_encap; 406 default: 407 goto drop; 408 } 409 410 if (unlikely(!dst)) { 411 struct ipv6hdr *hdr = ipv6_hdr(skb); 412 struct flowi6 fl6; 413 414 memset(&fl6, 0, sizeof(fl6)); 415 fl6.daddr = hdr->daddr; 416 fl6.saddr = hdr->saddr; 417 fl6.flowlabel = ip6_flowinfo(hdr); 418 fl6.flowi6_mark = skb->mark; 419 fl6.flowi6_proto = hdr->nexthdr; 420 421 dst = ip6_route_output(net, NULL, &fl6); 422 if (dst->error) { 423 err = dst->error; 424 goto drop; 425 } 426 427 /* If the destination is the same after transformation (which is 428 * a valid use case for IOAM), then we don't want to add it to 429 * the cache in order to avoid a reference loop. Instead, we add 430 * our fake dst_entry to the cache as a way to detect this case. 431 * Otherwise, we add the resolved destination to the cache. 432 */ 433 local_bh_disable(); 434 if (orig_dst->lwtstate == dst->lwtstate) 435 dst_cache_set_ip6(&ilwt->cache, 436 &ilwt->null_rt.dst, &fl6.saddr); 437 else 438 dst_cache_set_ip6(&ilwt->cache, dst, &fl6.saddr); 439 local_bh_enable(); 440 441 err = skb_cow_head(skb, LL_RESERVED_SPACE(dst_dev(dst))); 442 if (unlikely(err)) 443 goto drop; 444 } 445 446 /* avoid lwtunnel_output() reentry loop when destination is the same 447 * after transformation (e.g., with the inline mode) 448 */ 449 if (orig_dst->lwtstate != dst->lwtstate) { 450 skb_dst_drop(skb); 451 skb_dst_set(skb, dst); 452 return dst_output(net, sk, skb); 453 } 454 out: 455 dst_release(dst); 456 return orig_dst->lwtstate->orig_output(net, sk, skb); 457 drop: 458 dst_release(dst); 459 kfree_skb(skb); 460 return err; 461 } 462 463 static void ioam6_destroy_state(struct lwtunnel_state *lwt) 464 { 465 /* Since the refcount of per-cpu dst_entry caches will never be 0 (see 466 * why above) when our "fake" dst_entry is used, it is not necessary to 467 * remove them before calling dst_cache_destroy() 468 */ 469 dst_cache_destroy(&ioam6_lwt_state(lwt)->cache); 470 } 471 472 static int ioam6_fill_encap_info(struct sk_buff *skb, 473 struct lwtunnel_state *lwtstate) 474 { 475 struct ioam6_lwt *ilwt = ioam6_lwt_state(lwtstate); 476 int err; 477 478 err = nla_put_u32(skb, IOAM6_IPTUNNEL_FREQ_K, ilwt->freq.k); 479 if (err) 480 goto ret; 481 482 err = nla_put_u32(skb, IOAM6_IPTUNNEL_FREQ_N, ilwt->freq.n); 483 if (err) 484 goto ret; 485 486 err = nla_put_u8(skb, IOAM6_IPTUNNEL_MODE, ilwt->mode); 487 if (err) 488 goto ret; 489 490 if (ilwt->mode != IOAM6_IPTUNNEL_MODE_INLINE) { 491 if (ilwt->has_tunsrc) { 492 err = nla_put_in6_addr(skb, IOAM6_IPTUNNEL_SRC, 493 &ilwt->tunsrc); 494 if (err) 495 goto ret; 496 } 497 498 err = nla_put_in6_addr(skb, IOAM6_IPTUNNEL_DST, &ilwt->tundst); 499 if (err) 500 goto ret; 501 } 502 503 err = nla_put(skb, IOAM6_IPTUNNEL_TRACE, sizeof(ilwt->tuninfo.traceh), 504 &ilwt->tuninfo.traceh); 505 ret: 506 return err; 507 } 508 509 static int ioam6_encap_nlsize(struct lwtunnel_state *lwtstate) 510 { 511 struct ioam6_lwt *ilwt = ioam6_lwt_state(lwtstate); 512 int nlsize; 513 514 nlsize = nla_total_size(sizeof(ilwt->freq.k)) + 515 nla_total_size(sizeof(ilwt->freq.n)) + 516 nla_total_size(sizeof(ilwt->mode)) + 517 nla_total_size(sizeof(ilwt->tuninfo.traceh)); 518 519 if (ilwt->mode != IOAM6_IPTUNNEL_MODE_INLINE) { 520 if (ilwt->has_tunsrc) 521 nlsize += nla_total_size(sizeof(ilwt->tunsrc)); 522 523 nlsize += nla_total_size(sizeof(ilwt->tundst)); 524 } 525 526 return nlsize; 527 } 528 529 static int ioam6_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b) 530 { 531 struct ioam6_trace_hdr *trace_a = ioam6_lwt_trace(a); 532 struct ioam6_trace_hdr *trace_b = ioam6_lwt_trace(b); 533 struct ioam6_lwt *ilwt_a = ioam6_lwt_state(a); 534 struct ioam6_lwt *ilwt_b = ioam6_lwt_state(b); 535 536 return (ilwt_a->freq.k != ilwt_b->freq.k || 537 ilwt_a->freq.n != ilwt_b->freq.n || 538 ilwt_a->mode != ilwt_b->mode || 539 ilwt_a->has_tunsrc != ilwt_b->has_tunsrc || 540 (ilwt_a->mode != IOAM6_IPTUNNEL_MODE_INLINE && 541 !ipv6_addr_equal(&ilwt_a->tundst, &ilwt_b->tundst)) || 542 (ilwt_a->mode != IOAM6_IPTUNNEL_MODE_INLINE && 543 ilwt_a->has_tunsrc && 544 !ipv6_addr_equal(&ilwt_a->tunsrc, &ilwt_b->tunsrc)) || 545 trace_a->namespace_id != trace_b->namespace_id); 546 } 547 548 static const struct lwtunnel_encap_ops ioam6_iptun_ops = { 549 .build_state = ioam6_build_state, 550 .destroy_state = ioam6_destroy_state, 551 .output = ioam6_output, 552 .fill_encap = ioam6_fill_encap_info, 553 .get_encap_size = ioam6_encap_nlsize, 554 .cmp_encap = ioam6_encap_cmp, 555 .owner = THIS_MODULE, 556 }; 557 558 int __init ioam6_iptunnel_init(void) 559 { 560 return lwtunnel_encap_add_ops(&ioam6_iptun_ops, LWTUNNEL_ENCAP_IOAM6); 561 } 562 563 void ioam6_iptunnel_exit(void) 564 { 565 lwtunnel_encap_del_ops(&ioam6_iptun_ops, LWTUNNEL_ENCAP_IOAM6); 566 } 567