1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Management Component Transport Protocol (MCTP) - routing 4 * implementation. 5 * 6 * This is currently based on a simple routing table, with no dst cache. The 7 * number of routes should stay fairly small, so the lookup cost is small. 8 * 9 * Copyright (c) 2021 Code Construct 10 * Copyright (c) 2021 Google 11 */ 12 13 #include <linux/idr.h> 14 #include <linux/kconfig.h> 15 #include <linux/mctp.h> 16 #include <linux/netdevice.h> 17 #include <linux/rtnetlink.h> 18 #include <linux/skbuff.h> 19 20 #include <kunit/static_stub.h> 21 22 #include <uapi/linux/if_arp.h> 23 24 #include <net/mctp.h> 25 #include <net/mctpdevice.h> 26 #include <net/netlink.h> 27 #include <net/sock.h> 28 29 #include <trace/events/mctp.h> 30 31 static const unsigned int mctp_message_maxlen = 64 * 1024; 32 static const unsigned long mctp_key_lifetime = 6 * CONFIG_HZ; 33 34 static void mctp_flow_prepare_output(struct sk_buff *skb, struct mctp_dev *dev); 35 36 /* route output callbacks */ 37 static int mctp_dst_discard(struct mctp_dst *dst, struct sk_buff *skb) 38 { 39 kfree_skb(skb); 40 return 0; 41 } 42 43 static struct mctp_sock *mctp_lookup_bind_details(struct net *net, 44 struct sk_buff *skb, 45 u8 type, u8 dest, 46 u8 src, bool allow_net_any) 47 { 48 struct mctp_skb_cb *cb = mctp_cb(skb); 49 struct sock *sk; 50 u8 hash; 51 52 WARN_ON_ONCE(!rcu_read_lock_held()); 53 54 hash = mctp_bind_hash(type, dest, src); 55 56 sk_for_each_rcu(sk, &net->mctp.binds[hash]) { 57 struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk); 58 59 if (!allow_net_any && msk->bind_net == MCTP_NET_ANY) 60 continue; 61 62 if (msk->bind_net != MCTP_NET_ANY && msk->bind_net != cb->net) 63 continue; 64 65 if (msk->bind_type != type) 66 continue; 67 68 if (msk->bind_peer_set && 69 !mctp_address_matches(msk->bind_peer_addr, src)) 70 continue; 71 72 if (!mctp_address_matches(msk->bind_local_addr, dest)) 73 continue; 74 75 return msk; 76 } 77 78 return NULL; 79 } 80 81 static struct mctp_sock *mctp_lookup_bind(struct net *net, struct sk_buff *skb) 82 { 83 struct mctp_sock *msk; 84 struct mctp_hdr *mh; 85 u8 type; 86 87 /* TODO: look up in skb->cb? */ 88 mh = mctp_hdr(skb); 89 90 if (!skb_headlen(skb)) 91 return NULL; 92 93 type = (*(u8 *)skb->data) & 0x7f; 94 95 /* Look for binds in order of widening scope. A given destination or 96 * source address also implies matching on a particular network. 97 * 98 * - Matching destination and source 99 * - Matching destination 100 * - Matching source 101 * - Matching network, any address 102 * - Any network or address 103 */ 104 105 msk = mctp_lookup_bind_details(net, skb, type, mh->dest, mh->src, 106 false); 107 if (msk) 108 return msk; 109 msk = mctp_lookup_bind_details(net, skb, type, MCTP_ADDR_ANY, mh->src, 110 false); 111 if (msk) 112 return msk; 113 msk = mctp_lookup_bind_details(net, skb, type, mh->dest, MCTP_ADDR_ANY, 114 false); 115 if (msk) 116 return msk; 117 msk = mctp_lookup_bind_details(net, skb, type, MCTP_ADDR_ANY, 118 MCTP_ADDR_ANY, false); 119 if (msk) 120 return msk; 121 msk = mctp_lookup_bind_details(net, skb, type, MCTP_ADDR_ANY, 122 MCTP_ADDR_ANY, true); 123 if (msk) 124 return msk; 125 126 return NULL; 127 } 128 129 /* A note on the key allocations. 130 * 131 * struct net->mctp.keys contains our set of currently-allocated keys for 132 * MCTP tag management. The lookup tuple for these is the peer EID, 133 * local EID and MCTP tag. 134 * 135 * In some cases, the peer EID may be MCTP_EID_ANY: for example, when a 136 * broadcast message is sent, we may receive responses from any peer EID. 137 * Because the broadcast dest address is equivalent to ANY, we create 138 * a key with (local = local-eid, peer = ANY). This allows a match on the 139 * incoming broadcast responses from any peer. 140 * 141 * We perform lookups when packets are received, and when tags are allocated 142 * in two scenarios: 143 * 144 * - when a packet is sent, with a locally-owned tag: we need to find an 145 * unused tag value for the (local, peer) EID pair. 146 * 147 * - when a tag is manually allocated: we need to find an unused tag value 148 * for the peer EID, but don't have a specific local EID at that stage. 149 * 150 * in the latter case, on successful allocation, we end up with a tag with 151 * (local = ANY, peer = peer-eid). 152 * 153 * So, the key set allows both a local EID of ANY, as well as a peer EID of 154 * ANY in the lookup tuple. Both may be ANY if we prealloc for a broadcast. 155 * The matching (in mctp_key_match()) during lookup allows the match value to 156 * be ANY in either the dest or source addresses. 157 * 158 * When allocating (+ inserting) a tag, we need to check for conflicts amongst 159 * the existing tag set. This requires macthing either exactly on the local 160 * and peer addresses, or either being ANY. 161 */ 162 163 static bool mctp_key_match(struct mctp_sk_key *key, unsigned int net, 164 mctp_eid_t local, mctp_eid_t peer, u8 tag) 165 { 166 if (key->net != net) 167 return false; 168 169 if (!mctp_address_matches(key->local_addr, local)) 170 return false; 171 172 if (!mctp_address_matches(key->peer_addr, peer)) 173 return false; 174 175 if (key->tag != tag) 176 return false; 177 178 return true; 179 } 180 181 /* returns a key (with key->lock held, and refcounted), or NULL if no such 182 * key exists. 183 */ 184 static struct mctp_sk_key *mctp_lookup_key(struct net *net, struct sk_buff *skb, 185 unsigned int netid, mctp_eid_t peer, 186 unsigned long *irqflags) 187 __acquires(&key->lock) 188 { 189 struct mctp_sk_key *key, *ret; 190 unsigned long flags; 191 struct mctp_hdr *mh; 192 u8 tag; 193 194 mh = mctp_hdr(skb); 195 tag = mh->flags_seq_tag & (MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO); 196 197 ret = NULL; 198 spin_lock_irqsave(&net->mctp.keys_lock, flags); 199 200 hlist_for_each_entry(key, &net->mctp.keys, hlist) { 201 if (!mctp_key_match(key, netid, mh->dest, peer, tag)) 202 continue; 203 204 spin_lock(&key->lock); 205 if (key->valid) { 206 refcount_inc(&key->refs); 207 ret = key; 208 break; 209 } 210 spin_unlock(&key->lock); 211 } 212 213 if (ret) { 214 spin_unlock(&net->mctp.keys_lock); 215 *irqflags = flags; 216 } else { 217 spin_unlock_irqrestore(&net->mctp.keys_lock, flags); 218 } 219 220 return ret; 221 } 222 223 static struct mctp_sk_key *mctp_key_alloc(struct mctp_sock *msk, 224 unsigned int net, 225 mctp_eid_t local, mctp_eid_t peer, 226 u8 tag, gfp_t gfp) 227 { 228 struct mctp_sk_key *key; 229 230 key = kzalloc_obj(*key, gfp); 231 if (!key) 232 return NULL; 233 234 key->net = net; 235 key->peer_addr = peer; 236 key->local_addr = local; 237 key->tag = tag; 238 key->sk = &msk->sk; 239 key->valid = true; 240 spin_lock_init(&key->lock); 241 refcount_set(&key->refs, 1); 242 sock_hold(key->sk); 243 244 return key; 245 } 246 247 void mctp_key_unref(struct mctp_sk_key *key) 248 { 249 unsigned long flags; 250 251 if (!refcount_dec_and_test(&key->refs)) 252 return; 253 254 /* even though no refs exist here, the lock allows us to stay 255 * consistent with the locking requirement of mctp_dev_release_key 256 */ 257 spin_lock_irqsave(&key->lock, flags); 258 mctp_dev_release_key(key->dev, key); 259 spin_unlock_irqrestore(&key->lock, flags); 260 261 sock_put(key->sk); 262 kfree(key); 263 } 264 265 static int mctp_key_add(struct mctp_sk_key *key, struct mctp_sock *msk) 266 { 267 struct net *net = sock_net(&msk->sk); 268 struct mctp_sk_key *tmp; 269 unsigned long flags; 270 int rc = 0; 271 272 spin_lock_irqsave(&net->mctp.keys_lock, flags); 273 274 if (sock_flag(&msk->sk, SOCK_DEAD)) { 275 rc = -EINVAL; 276 goto out_unlock; 277 } 278 279 hlist_for_each_entry(tmp, &net->mctp.keys, hlist) { 280 if (mctp_key_match(tmp, key->net, key->local_addr, 281 key->peer_addr, key->tag)) { 282 spin_lock(&tmp->lock); 283 if (tmp->valid) 284 rc = -EEXIST; 285 spin_unlock(&tmp->lock); 286 if (rc) 287 break; 288 } 289 } 290 291 if (!rc) { 292 refcount_inc(&key->refs); 293 key->expiry = jiffies + mctp_key_lifetime; 294 timer_reduce(&msk->key_expiry, key->expiry); 295 296 hlist_add_head(&key->hlist, &net->mctp.keys); 297 hlist_add_head(&key->sklist, &msk->keys); 298 } 299 300 out_unlock: 301 spin_unlock_irqrestore(&net->mctp.keys_lock, flags); 302 303 return rc; 304 } 305 306 /* Helper for mctp_route_input(). 307 * We're done with the key; unlock and unref the key. 308 * For the usual case of automatic expiry we remove the key from lists. 309 * In the case that manual allocation is set on a key we release the lock 310 * and local ref, reset reassembly, but don't remove from lists. 311 */ 312 static void __mctp_key_done_in(struct mctp_sk_key *key, struct net *net, 313 unsigned long flags, unsigned long reason) 314 __releases(&key->lock) 315 { 316 struct sk_buff *skb; 317 318 trace_mctp_key_release(key, reason); 319 skb = key->reasm_head; 320 key->reasm_head = NULL; 321 322 if (!key->manual_alloc) { 323 key->reasm_dead = true; 324 key->valid = false; 325 mctp_dev_release_key(key->dev, key); 326 } 327 spin_unlock_irqrestore(&key->lock, flags); 328 329 if (!key->manual_alloc) { 330 spin_lock_irqsave(&net->mctp.keys_lock, flags); 331 if (!hlist_unhashed(&key->hlist)) { 332 hlist_del_init(&key->hlist); 333 hlist_del_init(&key->sklist); 334 mctp_key_unref(key); 335 } 336 spin_unlock_irqrestore(&net->mctp.keys_lock, flags); 337 } 338 339 /* and one for the local reference */ 340 mctp_key_unref(key); 341 342 kfree_skb(skb); 343 } 344 345 #ifdef CONFIG_MCTP_FLOWS 346 static void mctp_skb_set_flow(struct sk_buff *skb, struct mctp_sk_key *key) 347 { 348 struct mctp_flow *flow; 349 350 flow = skb_ext_add(skb, SKB_EXT_MCTP); 351 if (!flow) 352 return; 353 354 refcount_inc(&key->refs); 355 flow->key = key; 356 } 357 358 static void mctp_flow_prepare_output(struct sk_buff *skb, struct mctp_dev *dev) 359 { 360 struct mctp_sk_key *key; 361 struct mctp_flow *flow; 362 unsigned long flags; 363 364 flow = skb_ext_find(skb, SKB_EXT_MCTP); 365 if (!flow) 366 return; 367 368 key = flow->key; 369 370 spin_lock_irqsave(&key->lock, flags); 371 372 if (!key->dev) 373 mctp_dev_set_key(dev, key); 374 else 375 WARN_ON(key->dev != dev); 376 377 spin_unlock_irqrestore(&key->lock, flags); 378 } 379 #else 380 static void mctp_skb_set_flow(struct sk_buff *skb, struct mctp_sk_key *key) {} 381 static void mctp_flow_prepare_output(struct sk_buff *skb, struct mctp_dev *dev) {} 382 #endif 383 384 /* takes ownership of skb, both in success and failure cases */ 385 static int mctp_frag_queue(struct mctp_sk_key *key, struct sk_buff *skb) 386 { 387 struct mctp_hdr *hdr = mctp_hdr(skb); 388 u8 exp_seq, this_seq; 389 390 this_seq = (hdr->flags_seq_tag >> MCTP_HDR_SEQ_SHIFT) 391 & MCTP_HDR_SEQ_MASK; 392 393 if (!key->reasm_head) { 394 /* Since we're manipulating the shared frag_list, ensure it 395 * isn't shared with any other SKBs. In the cloned case, 396 * this will free the skb; callers can no longer access it 397 * safely. 398 */ 399 key->reasm_head = skb_unshare(skb, GFP_ATOMIC); 400 if (!key->reasm_head) 401 return -ENOMEM; 402 403 key->reasm_tailp = &(skb_shinfo(key->reasm_head)->frag_list); 404 key->last_seq = this_seq; 405 return 0; 406 } 407 408 exp_seq = (key->last_seq + 1) & MCTP_HDR_SEQ_MASK; 409 410 if (this_seq != exp_seq) 411 goto err_free; 412 413 if (key->reasm_head->len + skb->len > mctp_message_maxlen) 414 goto err_free; 415 416 skb->next = NULL; 417 skb->sk = NULL; 418 *key->reasm_tailp = skb; 419 key->reasm_tailp = &skb->next; 420 421 key->last_seq = this_seq; 422 423 key->reasm_head->data_len += skb->len; 424 key->reasm_head->len += skb->len; 425 key->reasm_head->truesize += skb->truesize; 426 427 return 0; 428 429 err_free: 430 kfree_skb(skb); 431 return -EINVAL; 432 } 433 434 static int mctp_dst_input(struct mctp_dst *dst, struct sk_buff *skb) 435 { 436 struct mctp_sk_key *key, *any_key = NULL; 437 struct net *net = dev_net(skb->dev); 438 struct mctp_sock *msk; 439 struct mctp_hdr *mh; 440 unsigned int netid; 441 unsigned long f; 442 u8 tag, flags; 443 int rc; 444 445 msk = NULL; 446 rc = -EINVAL; 447 448 /* We may be receiving a locally-routed packet; drop source sk 449 * accounting. 450 * 451 * From here, we will either queue the skb - either to a frag_queue, or 452 * to a receiving socket. When that succeeds, we clear the skb pointer; 453 * a non-NULL skb on exit will be otherwise unowned, and hence 454 * kfree_skb()-ed. 455 */ 456 skb_orphan(skb); 457 458 if (skb->pkt_type == PACKET_OUTGOING) 459 skb->pkt_type = PACKET_LOOPBACK; 460 461 /* ensure we have enough data for a header and a type */ 462 if (skb->len < sizeof(struct mctp_hdr) + 1) 463 goto out; 464 465 /* grab header, advance data ptr */ 466 mh = mctp_hdr(skb); 467 netid = mctp_cb(skb)->net; 468 skb_pull(skb, sizeof(struct mctp_hdr)); 469 470 if (mh->ver != 1) 471 goto out; 472 473 flags = mh->flags_seq_tag & (MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM); 474 tag = mh->flags_seq_tag & (MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO); 475 476 rcu_read_lock(); 477 478 /* lookup socket / reasm context, exactly matching (src,dest,tag). 479 * we hold a ref on the key, and key->lock held. 480 */ 481 key = mctp_lookup_key(net, skb, netid, mh->src, &f); 482 483 if (flags & MCTP_HDR_FLAG_SOM) { 484 if (key) { 485 msk = container_of(key->sk, struct mctp_sock, sk); 486 } else { 487 /* first response to a broadcast? do a more general 488 * key lookup to find the socket, but don't use this 489 * key for reassembly - we'll create a more specific 490 * one for future packets if required (ie, !EOM). 491 * 492 * this lookup requires key->peer to be MCTP_ADDR_ANY, 493 * it doesn't match just any key->peer. 494 */ 495 any_key = mctp_lookup_key(net, skb, netid, 496 MCTP_ADDR_ANY, &f); 497 if (any_key) { 498 msk = container_of(any_key->sk, 499 struct mctp_sock, sk); 500 spin_unlock_irqrestore(&any_key->lock, f); 501 } 502 } 503 504 if (!key && !msk && (tag & MCTP_HDR_FLAG_TO)) 505 msk = mctp_lookup_bind(net, skb); 506 507 if (!msk) { 508 rc = -ENOENT; 509 goto out_unlock; 510 } 511 512 /* single-packet message? deliver to socket, clean up any 513 * pending key. 514 */ 515 if (flags & MCTP_HDR_FLAG_EOM) { 516 rc = sock_queue_rcv_skb(&msk->sk, skb); 517 if (!rc) 518 skb = NULL; 519 if (key) { 520 /* we've hit a pending reassembly; not much we 521 * can do but drop it 522 */ 523 __mctp_key_done_in(key, net, f, 524 MCTP_TRACE_KEY_REPLIED); 525 key = NULL; 526 } 527 goto out_unlock; 528 } 529 530 /* broadcast response or a bind() - create a key for further 531 * packets for this message 532 */ 533 if (!key) { 534 key = mctp_key_alloc(msk, netid, mh->dest, mh->src, 535 tag, GFP_ATOMIC); 536 if (!key) { 537 rc = -ENOMEM; 538 goto out_unlock; 539 } 540 541 /* we can queue without the key lock here, as the 542 * key isn't observable yet 543 */ 544 mctp_frag_queue(key, skb); 545 skb = NULL; 546 547 /* if the key_add fails, we've raced with another 548 * SOM packet with the same src, dest and tag. There's 549 * no way to distinguish future packets, so all we 550 * can do is drop. 551 */ 552 rc = mctp_key_add(key, msk); 553 if (!rc) 554 trace_mctp_key_acquire(key); 555 556 /* we don't need to release key->lock on exit, so 557 * clean up here and suppress the unlock via 558 * setting to NULL 559 */ 560 mctp_key_unref(key); 561 key = NULL; 562 563 } else { 564 if (key->reasm_head || key->reasm_dead) { 565 /* duplicate start? drop everything */ 566 __mctp_key_done_in(key, net, f, 567 MCTP_TRACE_KEY_INVALIDATED); 568 rc = -EEXIST; 569 key = NULL; 570 } else { 571 rc = mctp_frag_queue(key, skb); 572 skb = NULL; 573 } 574 } 575 576 } else if (key) { 577 /* this packet continues a previous message; reassemble 578 * using the message-specific key 579 */ 580 581 /* we need to be continuing an existing reassembly... */ 582 if (!key->reasm_head) { 583 rc = -EINVAL; 584 } else { 585 rc = mctp_frag_queue(key, skb); 586 skb = NULL; 587 } 588 589 if (rc) 590 goto out_unlock; 591 592 /* end of message? deliver to socket, and we're done with 593 * the reassembly/response key 594 */ 595 if (flags & MCTP_HDR_FLAG_EOM) { 596 rc = sock_queue_rcv_skb(key->sk, key->reasm_head); 597 if (!rc) 598 key->reasm_head = NULL; 599 __mctp_key_done_in(key, net, f, MCTP_TRACE_KEY_REPLIED); 600 key = NULL; 601 } 602 603 } else { 604 /* not a start, no matching key */ 605 rc = -ENOENT; 606 } 607 608 out_unlock: 609 rcu_read_unlock(); 610 if (key) { 611 spin_unlock_irqrestore(&key->lock, f); 612 mctp_key_unref(key); 613 } 614 if (any_key) 615 mctp_key_unref(any_key); 616 out: 617 kfree_skb(skb); 618 return rc; 619 } 620 621 static int mctp_dst_output(struct mctp_dst *dst, struct sk_buff *skb) 622 { 623 char daddr_buf[MAX_ADDR_LEN]; 624 char *daddr = NULL; 625 int rc; 626 627 skb->protocol = htons(ETH_P_MCTP); 628 skb->pkt_type = PACKET_OUTGOING; 629 skb->dev = dst->dev->dev; 630 631 if (skb->len > dst->mtu) { 632 kfree_skb(skb); 633 return -EMSGSIZE; 634 } 635 636 /* direct route; use the hwaddr we stashed in sendmsg */ 637 if (dst->halen) { 638 if (dst->halen != skb->dev->addr_len) { 639 /* sanity check, sendmsg should have already caught this */ 640 kfree_skb(skb); 641 return -EMSGSIZE; 642 } 643 daddr = dst->haddr; 644 } else { 645 /* If lookup fails let the device handle daddr==NULL */ 646 if (mctp_neigh_lookup(dst->dev, dst->nexthop, daddr_buf) == 0) 647 daddr = daddr_buf; 648 } 649 650 rc = dev_hard_header(skb, skb->dev, ntohs(skb->protocol), 651 daddr, skb->dev->dev_addr, skb->len); 652 if (rc < 0) { 653 kfree_skb(skb); 654 return -EHOSTUNREACH; 655 } 656 657 mctp_flow_prepare_output(skb, dst->dev); 658 659 rc = dev_queue_xmit(skb); 660 if (rc) 661 rc = net_xmit_errno(rc); 662 663 return rc; 664 } 665 666 /* route alloc/release */ 667 static void mctp_route_release(struct mctp_route *rt) 668 { 669 if (refcount_dec_and_test(&rt->refs)) { 670 if (rt->dst_type == MCTP_ROUTE_DIRECT) 671 mctp_dev_put(rt->dev); 672 kfree_rcu(rt, rcu); 673 } 674 } 675 676 /* returns a route with the refcount at 1 */ 677 static struct mctp_route *mctp_route_alloc(void) 678 { 679 struct mctp_route *rt; 680 681 rt = kzalloc_obj(*rt); 682 if (!rt) 683 return NULL; 684 685 INIT_LIST_HEAD(&rt->list); 686 refcount_set(&rt->refs, 1); 687 rt->output = mctp_dst_discard; 688 689 return rt; 690 } 691 692 unsigned int mctp_default_net(struct net *net) 693 { 694 return READ_ONCE(net->mctp.default_net); 695 } 696 697 int mctp_default_net_set(struct net *net, unsigned int index) 698 { 699 if (index == 0) 700 return -EINVAL; 701 WRITE_ONCE(net->mctp.default_net, index); 702 return 0; 703 } 704 705 /* tag management */ 706 static void mctp_reserve_tag(struct net *net, struct mctp_sk_key *key, 707 struct mctp_sock *msk) 708 { 709 struct netns_mctp *mns = &net->mctp; 710 711 lockdep_assert_held(&mns->keys_lock); 712 713 key->expiry = jiffies + mctp_key_lifetime; 714 timer_reduce(&msk->key_expiry, key->expiry); 715 716 /* we hold the net->key_lock here, allowing updates to both 717 * then net and sk 718 */ 719 hlist_add_head_rcu(&key->hlist, &mns->keys); 720 hlist_add_head_rcu(&key->sklist, &msk->keys); 721 refcount_inc(&key->refs); 722 } 723 724 /* Allocate a locally-owned tag value for (local, peer), and reserve 725 * it for the socket msk 726 */ 727 struct mctp_sk_key *mctp_alloc_local_tag(struct mctp_sock *msk, 728 unsigned int netid, 729 mctp_eid_t local, mctp_eid_t peer, 730 bool manual, u8 *tagp) 731 { 732 struct net *net = sock_net(&msk->sk); 733 struct netns_mctp *mns = &net->mctp; 734 struct mctp_sk_key *key, *tmp; 735 unsigned long flags; 736 u8 tagbits; 737 738 /* for NULL destination EIDs, we may get a response from any peer */ 739 if (peer == MCTP_ADDR_NULL) 740 peer = MCTP_ADDR_ANY; 741 742 /* be optimistic, alloc now */ 743 key = mctp_key_alloc(msk, netid, local, peer, 0, GFP_KERNEL); 744 if (!key) 745 return ERR_PTR(-ENOMEM); 746 747 /* 8 possible tag values */ 748 tagbits = 0xff; 749 750 spin_lock_irqsave(&mns->keys_lock, flags); 751 752 /* Walk through the existing keys, looking for potential conflicting 753 * tags. If we find a conflict, clear that bit from tagbits 754 */ 755 hlist_for_each_entry(tmp, &mns->keys, hlist) { 756 /* We can check the lookup fields (*_addr, tag) without the 757 * lock held, they don't change over the lifetime of the key. 758 */ 759 760 /* tags are net-specific */ 761 if (tmp->net != netid) 762 continue; 763 764 /* if we don't own the tag, it can't conflict */ 765 if (tmp->tag & MCTP_HDR_FLAG_TO) 766 continue; 767 768 /* Since we're avoiding conflicting entries, match peer and 769 * local addresses, including with a wildcard on ANY. See 770 * 'A note on key allocations' for background. 771 */ 772 if (peer != MCTP_ADDR_ANY && 773 !mctp_address_matches(tmp->peer_addr, peer)) 774 continue; 775 776 if (local != MCTP_ADDR_ANY && 777 !mctp_address_matches(tmp->local_addr, local)) 778 continue; 779 780 spin_lock(&tmp->lock); 781 /* key must still be valid. If we find a match, clear the 782 * potential tag value 783 */ 784 if (tmp->valid) 785 tagbits &= ~(1 << tmp->tag); 786 spin_unlock(&tmp->lock); 787 788 if (!tagbits) 789 break; 790 } 791 792 if (tagbits) { 793 key->tag = __ffs(tagbits); 794 mctp_reserve_tag(net, key, msk); 795 trace_mctp_key_acquire(key); 796 797 key->manual_alloc = manual; 798 *tagp = key->tag; 799 } 800 801 spin_unlock_irqrestore(&mns->keys_lock, flags); 802 803 if (!tagbits) { 804 mctp_key_unref(key); 805 return ERR_PTR(-EBUSY); 806 } 807 808 return key; 809 } 810 811 static struct mctp_sk_key *mctp_lookup_prealloc_tag(struct mctp_sock *msk, 812 unsigned int netid, 813 mctp_eid_t daddr, 814 u8 req_tag, u8 *tagp) 815 { 816 struct net *net = sock_net(&msk->sk); 817 struct netns_mctp *mns = &net->mctp; 818 struct mctp_sk_key *key, *tmp; 819 unsigned long flags; 820 821 req_tag &= ~(MCTP_TAG_PREALLOC | MCTP_TAG_OWNER); 822 key = NULL; 823 824 spin_lock_irqsave(&mns->keys_lock, flags); 825 826 hlist_for_each_entry(tmp, &mns->keys, hlist) { 827 if (tmp->net != netid) 828 continue; 829 830 if (tmp->tag != req_tag) 831 continue; 832 833 if (!mctp_address_matches(tmp->peer_addr, daddr)) 834 continue; 835 836 if (!tmp->manual_alloc) 837 continue; 838 839 spin_lock(&tmp->lock); 840 if (tmp->valid) { 841 key = tmp; 842 refcount_inc(&key->refs); 843 spin_unlock(&tmp->lock); 844 break; 845 } 846 spin_unlock(&tmp->lock); 847 } 848 spin_unlock_irqrestore(&mns->keys_lock, flags); 849 850 if (!key) 851 return ERR_PTR(-ENOENT); 852 853 if (tagp) 854 *tagp = key->tag; 855 856 return key; 857 } 858 859 /* routing lookups */ 860 static unsigned int mctp_route_netid(struct mctp_route *rt) 861 { 862 return rt->dst_type == MCTP_ROUTE_DIRECT ? 863 READ_ONCE(rt->dev->net) : rt->gateway.net; 864 } 865 866 static bool mctp_rt_match_eid(struct mctp_route *rt, 867 unsigned int net, mctp_eid_t eid) 868 { 869 return mctp_route_netid(rt) == net && 870 rt->min <= eid && rt->max >= eid; 871 } 872 873 /* compares match, used for duplicate prevention */ 874 static bool mctp_rt_compare_exact(struct mctp_route *rt1, 875 struct mctp_route *rt2) 876 { 877 ASSERT_RTNL(); 878 return mctp_route_netid(rt1) == mctp_route_netid(rt2) && 879 rt1->min == rt2->min && 880 rt1->max == rt2->max; 881 } 882 883 static mctp_eid_t mctp_dev_saddr(struct mctp_dev *dev) 884 { 885 mctp_eid_t addr = MCTP_ADDR_NULL; 886 unsigned long flags; 887 888 spin_lock_irqsave(&dev->addrs_lock, flags); 889 if (dev->num_addrs) { 890 /* use the outbound interface's first address as our source */ 891 addr = dev->addrs[0]; 892 } 893 spin_unlock_irqrestore(&dev->addrs_lock, flags); 894 895 return addr; 896 } 897 898 /* must only be called on a direct route, as the final output hop */ 899 static void mctp_dst_from_route(struct mctp_dst *dst, mctp_eid_t eid, 900 mctp_eid_t saddr, unsigned int mtu, 901 struct mctp_route *route) 902 { 903 mctp_dev_hold(route->dev); 904 dst->nexthop = eid; 905 dst->dev = route->dev; 906 dst->mtu = READ_ONCE(dst->dev->dev->mtu); 907 if (mtu) 908 dst->mtu = min(dst->mtu, mtu); 909 dst->halen = 0; 910 dst->output = route->output; 911 dst->saddr = saddr; 912 } 913 914 int mctp_dst_from_extaddr(struct mctp_dst *dst, struct net *net, int ifindex, 915 unsigned char halen, const unsigned char *haddr) 916 { 917 struct net_device *netdev; 918 struct mctp_dev *dev; 919 int rc = -ENOENT; 920 921 if (halen > sizeof(dst->haddr)) 922 return -EINVAL; 923 924 rcu_read_lock(); 925 926 netdev = dev_get_by_index_rcu(net, ifindex); 927 if (!netdev) 928 goto out_unlock; 929 930 if (netdev->addr_len != halen) { 931 rc = -EINVAL; 932 goto out_unlock; 933 } 934 935 dev = __mctp_dev_get(netdev); 936 if (!dev) 937 goto out_unlock; 938 939 dst->dev = dev; 940 dst->mtu = READ_ONCE(netdev->mtu); 941 dst->halen = halen; 942 dst->output = mctp_dst_output; 943 dst->nexthop = 0; 944 dst->saddr = mctp_dev_saddr(dev); 945 memcpy(dst->haddr, haddr, halen); 946 947 rc = 0; 948 949 out_unlock: 950 rcu_read_unlock(); 951 return rc; 952 } 953 954 void mctp_dst_release(struct mctp_dst *dst) 955 { 956 mctp_dev_put(dst->dev); 957 } 958 959 static struct mctp_route *mctp_route_lookup_single(struct net *net, 960 unsigned int dnet, 961 mctp_eid_t daddr) 962 { 963 struct mctp_route *rt; 964 965 list_for_each_entry_rcu(rt, &net->mctp.routes, list) { 966 if (mctp_rt_match_eid(rt, dnet, daddr)) 967 return rt; 968 } 969 970 return NULL; 971 } 972 973 /* populates *dst on successful lookup, if set */ 974 int mctp_route_lookup(struct net *net, unsigned int dnet, 975 mctp_eid_t daddr, struct mctp_dst *dst) 976 { 977 const unsigned int max_depth = 32; 978 unsigned int depth, mtu = 0; 979 int rc = -EHOSTUNREACH; 980 981 rcu_read_lock(); 982 983 for (depth = 0; depth < max_depth; depth++) { 984 struct mctp_route *rt; 985 986 rt = mctp_route_lookup_single(net, dnet, daddr); 987 if (!rt) 988 break; 989 990 /* clamp mtu to the smallest in the path, allowing 0 991 * to specify no restrictions 992 */ 993 if (mtu && rt->mtu) 994 mtu = min(mtu, rt->mtu); 995 else 996 mtu = mtu ?: rt->mtu; 997 998 if (rt->dst_type == MCTP_ROUTE_DIRECT) { 999 mctp_eid_t saddr = mctp_dev_saddr(rt->dev); 1000 1001 /* cannot do gateway-ed routes without a src */ 1002 if (saddr == MCTP_ADDR_NULL && depth != 0) 1003 break; 1004 1005 if (dst) 1006 mctp_dst_from_route(dst, daddr, saddr, mtu, rt); 1007 rc = 0; 1008 break; 1009 1010 } else if (rt->dst_type == MCTP_ROUTE_GATEWAY) { 1011 daddr = rt->gateway.eid; 1012 } 1013 } 1014 1015 rcu_read_unlock(); 1016 1017 return rc; 1018 } 1019 1020 static int mctp_dst_input_null(struct net *net, struct net_device *dev, 1021 struct mctp_dst *dst) 1022 { 1023 rcu_read_lock(); 1024 dst->dev = __mctp_dev_get(dev); 1025 rcu_read_unlock(); 1026 1027 if (!dst->dev) 1028 return -EHOSTUNREACH; 1029 1030 dst->mtu = READ_ONCE(dev->mtu); 1031 dst->halen = 0; 1032 dst->output = mctp_dst_input; 1033 dst->nexthop = 0; 1034 1035 return 0; 1036 } 1037 1038 static int mctp_do_fragment_route(struct mctp_dst *dst, struct sk_buff *skb, 1039 unsigned int mtu, u8 tag) 1040 { 1041 const unsigned int hlen = sizeof(struct mctp_hdr); 1042 struct mctp_hdr *hdr, *hdr2; 1043 unsigned int pos, size, headroom; 1044 struct sk_buff *skb2; 1045 int rc; 1046 u8 seq; 1047 1048 hdr = mctp_hdr(skb); 1049 seq = 0; 1050 rc = 0; 1051 1052 if (mtu < hlen + 1) { 1053 kfree_skb(skb); 1054 return -EMSGSIZE; 1055 } 1056 1057 /* within MTU? avoid the copy, send original skb */ 1058 if (skb->len <= mtu) { 1059 hdr->flags_seq_tag = MCTP_HDR_FLAG_SOM | 1060 MCTP_HDR_FLAG_EOM | tag; 1061 return dst->output(dst, skb); 1062 } 1063 1064 /* keep same headroom as the original skb */ 1065 headroom = skb_headroom(skb); 1066 1067 /* we've got the header */ 1068 skb_pull(skb, hlen); 1069 1070 for (pos = 0; pos < skb->len;) { 1071 /* size of message payload */ 1072 size = min(mtu - hlen, skb->len - pos); 1073 1074 skb2 = alloc_skb(headroom + hlen + size, GFP_KERNEL); 1075 if (!skb2) { 1076 rc = -ENOMEM; 1077 break; 1078 } 1079 1080 /* generic skb copy */ 1081 skb2->protocol = skb->protocol; 1082 skb2->priority = skb->priority; 1083 skb2->dev = skb->dev; 1084 memcpy(skb2->cb, skb->cb, sizeof(skb2->cb)); 1085 1086 if (skb->sk) 1087 skb_set_owner_w(skb2, skb->sk); 1088 1089 /* establish packet */ 1090 skb_reserve(skb2, headroom); 1091 skb_reset_network_header(skb2); 1092 skb_put(skb2, hlen + size); 1093 skb2->transport_header = skb2->network_header + hlen; 1094 1095 /* copy header fields, calculate SOM/EOM flags & seq */ 1096 hdr2 = mctp_hdr(skb2); 1097 hdr2->ver = hdr->ver; 1098 hdr2->dest = hdr->dest; 1099 hdr2->src = hdr->src; 1100 hdr2->flags_seq_tag = tag & 1101 (MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO); 1102 1103 if (pos == 0) 1104 hdr2->flags_seq_tag |= MCTP_HDR_FLAG_SOM; 1105 1106 if (pos + size == skb->len) 1107 hdr2->flags_seq_tag |= MCTP_HDR_FLAG_EOM; 1108 1109 hdr2->flags_seq_tag |= seq << MCTP_HDR_SEQ_SHIFT; 1110 1111 /* copy message payload */ 1112 skb_copy_bits(skb, pos, skb_transport_header(skb2), size); 1113 1114 /* we need to copy the extensions, for MCTP flow data */ 1115 skb_ext_copy(skb2, skb); 1116 1117 /* do route */ 1118 rc = dst->output(dst, skb2); 1119 if (rc) 1120 break; 1121 1122 seq = (seq + 1) & MCTP_HDR_SEQ_MASK; 1123 pos += size; 1124 } 1125 1126 consume_skb(skb); 1127 return rc; 1128 } 1129 1130 int mctp_local_output(struct sock *sk, struct mctp_dst *dst, 1131 struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag) 1132 { 1133 struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk); 1134 struct mctp_sk_key *key; 1135 struct mctp_hdr *hdr; 1136 unsigned int netid; 1137 u8 tag; 1138 1139 KUNIT_STATIC_STUB_REDIRECT(mctp_local_output, sk, dst, skb, daddr, 1140 req_tag); 1141 1142 netid = READ_ONCE(dst->dev->net); 1143 1144 if (req_tag & MCTP_TAG_OWNER) { 1145 if (req_tag & MCTP_TAG_PREALLOC) 1146 key = mctp_lookup_prealloc_tag(msk, netid, daddr, 1147 req_tag, &tag); 1148 else 1149 key = mctp_alloc_local_tag(msk, netid, dst->saddr, 1150 daddr, false, &tag); 1151 1152 if (IS_ERR(key)) { 1153 kfree_skb(skb); 1154 return PTR_ERR(key); 1155 } 1156 mctp_skb_set_flow(skb, key); 1157 /* done with the key in this scope */ 1158 mctp_key_unref(key); 1159 tag |= MCTP_HDR_FLAG_TO; 1160 } else { 1161 key = NULL; 1162 tag = req_tag & MCTP_TAG_MASK; 1163 } 1164 1165 skb->pkt_type = PACKET_OUTGOING; 1166 skb->protocol = htons(ETH_P_MCTP); 1167 skb->priority = 0; 1168 skb_reset_transport_header(skb); 1169 skb_push(skb, sizeof(struct mctp_hdr)); 1170 skb_reset_network_header(skb); 1171 skb->dev = dst->dev->dev; 1172 1173 /* set up common header fields */ 1174 hdr = mctp_hdr(skb); 1175 hdr->ver = 1; 1176 hdr->dest = daddr; 1177 hdr->src = dst->saddr; 1178 1179 /* route output functions consume the skb, even on error */ 1180 return mctp_do_fragment_route(dst, skb, dst->mtu, tag); 1181 } 1182 1183 /* route management */ 1184 1185 /* mctp_route_add(): Add the provided route, previously allocated via 1186 * mctp_route_alloc(). On success, takes ownership of @rt, which includes a 1187 * hold on rt->dev for usage in the route table. On failure a caller will want 1188 * to mctp_route_release(). 1189 * 1190 * We expect that the caller has set rt->type, rt->dst_type, rt->min, rt->max, 1191 * rt->mtu and either rt->dev (with a reference held appropriately) or 1192 * rt->gateway. Other fields will be populated. 1193 */ 1194 static int mctp_route_add(struct net *net, struct mctp_route *rt) 1195 { 1196 struct mctp_route *ert; 1197 1198 if (!mctp_address_unicast(rt->min) || !mctp_address_unicast(rt->max)) 1199 return -EINVAL; 1200 1201 if (rt->dst_type == MCTP_ROUTE_DIRECT && !rt->dev) 1202 return -EINVAL; 1203 1204 if (rt->dst_type == MCTP_ROUTE_GATEWAY && !rt->gateway.eid) 1205 return -EINVAL; 1206 1207 switch (rt->type) { 1208 case RTN_LOCAL: 1209 rt->output = mctp_dst_input; 1210 break; 1211 case RTN_UNICAST: 1212 rt->output = mctp_dst_output; 1213 break; 1214 default: 1215 return -EINVAL; 1216 } 1217 1218 ASSERT_RTNL(); 1219 1220 /* Prevent duplicate identical routes. */ 1221 list_for_each_entry(ert, &net->mctp.routes, list) { 1222 if (mctp_rt_compare_exact(rt, ert)) { 1223 return -EEXIST; 1224 } 1225 } 1226 1227 list_add_rcu(&rt->list, &net->mctp.routes); 1228 1229 return 0; 1230 } 1231 1232 static int mctp_route_remove(struct net *net, unsigned int netid, 1233 mctp_eid_t daddr_start, unsigned int daddr_extent, 1234 unsigned char type) 1235 { 1236 struct mctp_route *rt, *tmp; 1237 mctp_eid_t daddr_end; 1238 bool dropped; 1239 1240 if (daddr_extent > 0xff || daddr_start + daddr_extent >= 255) 1241 return -EINVAL; 1242 1243 daddr_end = daddr_start + daddr_extent; 1244 dropped = false; 1245 1246 ASSERT_RTNL(); 1247 1248 list_for_each_entry_safe(rt, tmp, &net->mctp.routes, list) { 1249 if (mctp_route_netid(rt) == netid && 1250 rt->min == daddr_start && rt->max == daddr_end && 1251 rt->type == type) { 1252 list_del_rcu(&rt->list); 1253 /* TODO: immediate RTM_DELROUTE */ 1254 mctp_route_release(rt); 1255 dropped = true; 1256 } 1257 } 1258 1259 return dropped ? 0 : -ENOENT; 1260 } 1261 1262 int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr) 1263 { 1264 struct mctp_route *rt; 1265 int rc; 1266 1267 rt = mctp_route_alloc(); 1268 if (!rt) 1269 return -ENOMEM; 1270 1271 rt->min = addr; 1272 rt->max = addr; 1273 rt->dst_type = MCTP_ROUTE_DIRECT; 1274 rt->dev = mdev; 1275 rt->type = RTN_LOCAL; 1276 1277 mctp_dev_hold(rt->dev); 1278 1279 rc = mctp_route_add(dev_net(mdev->dev), rt); 1280 if (rc) 1281 mctp_route_release(rt); 1282 1283 return rc; 1284 } 1285 1286 int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr) 1287 { 1288 return mctp_route_remove(dev_net(mdev->dev), mdev->net, 1289 addr, 0, RTN_LOCAL); 1290 } 1291 1292 /* removes all entries for a given device */ 1293 void mctp_route_remove_dev(struct mctp_dev *mdev) 1294 { 1295 struct net *net = dev_net(mdev->dev); 1296 struct mctp_route *rt, *tmp; 1297 1298 ASSERT_RTNL(); 1299 list_for_each_entry_safe(rt, tmp, &net->mctp.routes, list) { 1300 if (rt->dst_type == MCTP_ROUTE_DIRECT && rt->dev == mdev) { 1301 list_del_rcu(&rt->list); 1302 /* TODO: immediate RTM_DELROUTE */ 1303 mctp_route_release(rt); 1304 } 1305 } 1306 } 1307 1308 /* Incoming packet-handling */ 1309 1310 static int mctp_pkttype_receive(struct sk_buff *skb, struct net_device *dev, 1311 struct packet_type *pt, 1312 struct net_device *orig_dev) 1313 { 1314 struct net *net = dev_net(dev); 1315 struct mctp_dev *mdev; 1316 struct mctp_skb_cb *cb; 1317 struct mctp_dst dst; 1318 struct mctp_hdr *mh; 1319 int rc; 1320 1321 rcu_read_lock(); 1322 mdev = __mctp_dev_get(dev); 1323 rcu_read_unlock(); 1324 if (!mdev) { 1325 /* basic non-data sanity checks */ 1326 goto err_drop; 1327 } 1328 1329 if (!pskb_may_pull(skb, sizeof(struct mctp_hdr))) 1330 goto err_drop; 1331 1332 skb_reset_transport_header(skb); 1333 skb_reset_network_header(skb); 1334 1335 /* We have enough for a header; decode and route */ 1336 mh = mctp_hdr(skb); 1337 if (mh->ver < MCTP_VER_MIN || mh->ver > MCTP_VER_MAX) 1338 goto err_drop; 1339 1340 /* source must be valid unicast or null; drop reserved ranges and 1341 * broadcast 1342 */ 1343 if (!(mctp_address_unicast(mh->src) || mctp_address_null(mh->src))) 1344 goto err_drop; 1345 1346 /* dest address: as above, but allow broadcast */ 1347 if (!(mctp_address_unicast(mh->dest) || mctp_address_null(mh->dest) || 1348 mctp_address_broadcast(mh->dest))) 1349 goto err_drop; 1350 1351 /* MCTP drivers must populate halen/haddr */ 1352 if (dev->type == ARPHRD_MCTP) { 1353 cb = mctp_cb(skb); 1354 } else { 1355 cb = __mctp_cb(skb); 1356 cb->halen = 0; 1357 } 1358 cb->net = READ_ONCE(mdev->net); 1359 cb->ifindex = dev->ifindex; 1360 1361 rc = mctp_route_lookup(net, cb->net, mh->dest, &dst); 1362 1363 /* NULL EID, but addressed to our physical address */ 1364 if (rc && mh->dest == MCTP_ADDR_NULL && skb->pkt_type == PACKET_HOST) 1365 rc = mctp_dst_input_null(net, dev, &dst); 1366 1367 if (rc) 1368 goto err_drop; 1369 1370 dst.output(&dst, skb); 1371 mctp_dst_release(&dst); 1372 mctp_dev_put(mdev); 1373 1374 return NET_RX_SUCCESS; 1375 1376 err_drop: 1377 kfree_skb(skb); 1378 mctp_dev_put(mdev); 1379 return NET_RX_DROP; 1380 } 1381 1382 static struct packet_type mctp_packet_type = { 1383 .type = cpu_to_be16(ETH_P_MCTP), 1384 .func = mctp_pkttype_receive, 1385 }; 1386 1387 /* netlink interface */ 1388 1389 static const struct nla_policy rta_mctp_policy[RTA_MAX + 1] = { 1390 [RTA_DST] = { .type = NLA_U8 }, 1391 [RTA_METRICS] = { .type = NLA_NESTED }, 1392 [RTA_OIF] = { .type = NLA_U32 }, 1393 [RTA_GATEWAY] = NLA_POLICY_EXACT_LEN(sizeof(struct mctp_fq_addr)), 1394 }; 1395 1396 static const struct nla_policy rta_metrics_policy[RTAX_MAX + 1] = { 1397 [RTAX_MTU] = { .type = NLA_U32 }, 1398 }; 1399 1400 /* base parsing; common to both _lookup and _populate variants. 1401 * 1402 * For gateway routes (which have a RTA_GATEWAY, and no RTA_OIF), we populate 1403 * *gatweayp. for direct routes (RTA_OIF, no RTA_GATEWAY), we populate *mdev. 1404 */ 1405 static int mctp_route_nlparse_common(struct net *net, struct nlmsghdr *nlh, 1406 struct netlink_ext_ack *extack, 1407 struct nlattr **tb, struct rtmsg **rtm, 1408 struct mctp_dev **mdev, 1409 struct mctp_fq_addr *gatewayp, 1410 mctp_eid_t *daddr_start) 1411 { 1412 struct mctp_fq_addr *gateway = NULL; 1413 unsigned int ifindex = 0; 1414 struct net_device *dev; 1415 int rc; 1416 1417 rc = nlmsg_parse(nlh, sizeof(struct rtmsg), tb, RTA_MAX, 1418 rta_mctp_policy, extack); 1419 if (rc < 0) { 1420 NL_SET_ERR_MSG(extack, "incorrect format"); 1421 return rc; 1422 } 1423 1424 if (!tb[RTA_DST]) { 1425 NL_SET_ERR_MSG(extack, "dst EID missing"); 1426 return -EINVAL; 1427 } 1428 *daddr_start = nla_get_u8(tb[RTA_DST]); 1429 1430 if (tb[RTA_OIF]) 1431 ifindex = nla_get_u32(tb[RTA_OIF]); 1432 1433 if (tb[RTA_GATEWAY]) 1434 gateway = nla_data(tb[RTA_GATEWAY]); 1435 1436 if (ifindex && gateway) { 1437 NL_SET_ERR_MSG(extack, 1438 "cannot specify both ifindex and gateway"); 1439 return -EINVAL; 1440 1441 } else if (ifindex) { 1442 dev = __dev_get_by_index(net, ifindex); 1443 if (!dev) { 1444 NL_SET_ERR_MSG(extack, "bad ifindex"); 1445 return -ENODEV; 1446 } 1447 *mdev = mctp_dev_get_rtnl(dev); 1448 if (!*mdev) 1449 return -ENODEV; 1450 gatewayp->eid = 0; 1451 1452 } else if (gateway) { 1453 if (!mctp_address_unicast(gateway->eid)) { 1454 NL_SET_ERR_MSG(extack, "bad gateway"); 1455 return -EINVAL; 1456 } 1457 1458 gatewayp->eid = gateway->eid; 1459 gatewayp->net = gateway->net != MCTP_NET_ANY ? 1460 gateway->net : 1461 READ_ONCE(net->mctp.default_net); 1462 *mdev = NULL; 1463 1464 } else { 1465 NL_SET_ERR_MSG(extack, "no route output provided"); 1466 return -EINVAL; 1467 } 1468 1469 *rtm = nlmsg_data(nlh); 1470 if ((*rtm)->rtm_family != AF_MCTP) { 1471 NL_SET_ERR_MSG(extack, "route family must be AF_MCTP"); 1472 return -EINVAL; 1473 } 1474 1475 if ((*rtm)->rtm_type != RTN_UNICAST) { 1476 NL_SET_ERR_MSG(extack, "rtm_type must be RTN_UNICAST"); 1477 return -EINVAL; 1478 } 1479 1480 return 0; 1481 } 1482 1483 /* Route parsing for lookup operations; we only need the "route target" 1484 * components (ie., network and dest-EID range). 1485 */ 1486 static int mctp_route_nlparse_lookup(struct net *net, struct nlmsghdr *nlh, 1487 struct netlink_ext_ack *extack, 1488 unsigned char *type, unsigned int *netid, 1489 mctp_eid_t *daddr_start, 1490 unsigned int *daddr_extent) 1491 { 1492 struct nlattr *tb[RTA_MAX + 1]; 1493 struct mctp_fq_addr gw; 1494 struct mctp_dev *mdev; 1495 struct rtmsg *rtm; 1496 int rc; 1497 1498 rc = mctp_route_nlparse_common(net, nlh, extack, tb, &rtm, 1499 &mdev, &gw, daddr_start); 1500 if (rc) 1501 return rc; 1502 1503 if (mdev) { 1504 *netid = mdev->net; 1505 } else if (gw.eid) { 1506 *netid = gw.net; 1507 } else { 1508 /* bug: _nlparse_common should not allow this */ 1509 return -1; 1510 } 1511 1512 *type = rtm->rtm_type; 1513 *daddr_extent = rtm->rtm_dst_len; 1514 1515 return 0; 1516 } 1517 1518 /* Full route parse for RTM_NEWROUTE: populate @rt. On success, 1519 * MCTP_ROUTE_DIRECT routes (ie, those with a direct dev) will hold a reference 1520 * to that dev. 1521 */ 1522 static int mctp_route_nlparse_populate(struct net *net, struct nlmsghdr *nlh, 1523 struct netlink_ext_ack *extack, 1524 struct mctp_route *rt) 1525 { 1526 struct nlattr *tbx[RTAX_MAX + 1]; 1527 struct nlattr *tb[RTA_MAX + 1]; 1528 unsigned int daddr_extent; 1529 struct mctp_fq_addr gw; 1530 mctp_eid_t daddr_start; 1531 struct mctp_dev *dev; 1532 struct rtmsg *rtm; 1533 u32 mtu = 0; 1534 int rc; 1535 1536 rc = mctp_route_nlparse_common(net, nlh, extack, tb, &rtm, 1537 &dev, &gw, &daddr_start); 1538 if (rc) 1539 return rc; 1540 1541 daddr_extent = rtm->rtm_dst_len; 1542 1543 if (daddr_extent > 0xff || daddr_extent + daddr_start >= 255) { 1544 NL_SET_ERR_MSG(extack, "invalid eid range"); 1545 return -EINVAL; 1546 } 1547 1548 if (tb[RTA_METRICS]) { 1549 rc = nla_parse_nested(tbx, RTAX_MAX, tb[RTA_METRICS], 1550 rta_metrics_policy, NULL); 1551 if (rc < 0) { 1552 NL_SET_ERR_MSG(extack, "incorrect RTA_METRICS format"); 1553 return rc; 1554 } 1555 if (tbx[RTAX_MTU]) 1556 mtu = nla_get_u32(tbx[RTAX_MTU]); 1557 } 1558 1559 rt->type = rtm->rtm_type; 1560 rt->min = daddr_start; 1561 rt->max = daddr_start + daddr_extent; 1562 rt->mtu = mtu; 1563 if (gw.eid) { 1564 rt->dst_type = MCTP_ROUTE_GATEWAY; 1565 rt->gateway.eid = gw.eid; 1566 rt->gateway.net = gw.net; 1567 } else { 1568 rt->dst_type = MCTP_ROUTE_DIRECT; 1569 rt->dev = dev; 1570 mctp_dev_hold(rt->dev); 1571 } 1572 1573 return 0; 1574 } 1575 1576 static int mctp_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 1577 struct netlink_ext_ack *extack) 1578 { 1579 struct net *net = sock_net(skb->sk); 1580 struct mctp_route *rt; 1581 int rc; 1582 1583 rt = mctp_route_alloc(); 1584 if (!rt) 1585 return -ENOMEM; 1586 1587 rc = mctp_route_nlparse_populate(net, nlh, extack, rt); 1588 if (rc < 0) 1589 goto err_free; 1590 1591 if (rt->dst_type == MCTP_ROUTE_DIRECT && 1592 rt->dev->dev->flags & IFF_LOOPBACK) { 1593 NL_SET_ERR_MSG(extack, "no routes to loopback"); 1594 rc = -EINVAL; 1595 goto err_free; 1596 } 1597 1598 rc = mctp_route_add(net, rt); 1599 if (!rc) 1600 return 0; 1601 1602 err_free: 1603 mctp_route_release(rt); 1604 return rc; 1605 } 1606 1607 static int mctp_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 1608 struct netlink_ext_ack *extack) 1609 { 1610 struct net *net = sock_net(skb->sk); 1611 unsigned int netid, daddr_extent; 1612 unsigned char type = RTN_UNSPEC; 1613 mctp_eid_t daddr_start; 1614 int rc; 1615 1616 rc = mctp_route_nlparse_lookup(net, nlh, extack, &type, &netid, 1617 &daddr_start, &daddr_extent); 1618 if (rc < 0) 1619 return rc; 1620 1621 /* we only have unicast routes */ 1622 if (type != RTN_UNICAST) 1623 return -EINVAL; 1624 1625 rc = mctp_route_remove(net, netid, daddr_start, daddr_extent, type); 1626 return rc; 1627 } 1628 1629 static int mctp_fill_rtinfo(struct sk_buff *skb, struct mctp_route *rt, 1630 u32 portid, u32 seq, int event, unsigned int flags) 1631 { 1632 struct nlmsghdr *nlh; 1633 struct rtmsg *hdr; 1634 void *metrics; 1635 1636 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 1637 if (!nlh) 1638 return -EMSGSIZE; 1639 1640 hdr = nlmsg_data(nlh); 1641 memset(hdr, 0, sizeof(*hdr)); 1642 hdr->rtm_family = AF_MCTP; 1643 1644 /* we use the _len fields as a number of EIDs, rather than 1645 * a number of bits in the address 1646 */ 1647 hdr->rtm_dst_len = rt->max - rt->min; 1648 hdr->rtm_src_len = 0; 1649 hdr->rtm_tos = 0; 1650 hdr->rtm_table = RT_TABLE_DEFAULT; 1651 hdr->rtm_protocol = RTPROT_STATIC; /* everything is user-defined */ 1652 hdr->rtm_type = rt->type; 1653 1654 if (nla_put_u8(skb, RTA_DST, rt->min)) 1655 goto cancel; 1656 1657 metrics = nla_nest_start_noflag(skb, RTA_METRICS); 1658 if (!metrics) 1659 goto cancel; 1660 1661 if (rt->mtu) { 1662 if (nla_put_u32(skb, RTAX_MTU, rt->mtu)) 1663 goto cancel; 1664 } 1665 1666 nla_nest_end(skb, metrics); 1667 1668 if (rt->dst_type == MCTP_ROUTE_DIRECT) { 1669 hdr->rtm_scope = RT_SCOPE_LINK; 1670 if (nla_put_u32(skb, RTA_OIF, rt->dev->dev->ifindex)) 1671 goto cancel; 1672 } else if (rt->dst_type == MCTP_ROUTE_GATEWAY) { 1673 hdr->rtm_scope = RT_SCOPE_UNIVERSE; 1674 if (nla_put(skb, RTA_GATEWAY, 1675 sizeof(rt->gateway), &rt->gateway)) 1676 goto cancel; 1677 } 1678 1679 nlmsg_end(skb, nlh); 1680 1681 return 0; 1682 1683 cancel: 1684 nlmsg_cancel(skb, nlh); 1685 return -EMSGSIZE; 1686 } 1687 1688 static int mctp_dump_rtinfo(struct sk_buff *skb, struct netlink_callback *cb) 1689 { 1690 struct net *net = sock_net(skb->sk); 1691 struct mctp_route *rt; 1692 int s_idx, idx; 1693 1694 /* TODO: allow filtering on route data, possibly under 1695 * cb->strict_check 1696 */ 1697 1698 /* TODO: change to struct overlay */ 1699 s_idx = cb->args[0]; 1700 idx = 0; 1701 1702 rcu_read_lock(); 1703 list_for_each_entry_rcu(rt, &net->mctp.routes, list) { 1704 if (idx++ < s_idx) 1705 continue; 1706 if (mctp_fill_rtinfo(skb, rt, 1707 NETLINK_CB(cb->skb).portid, 1708 cb->nlh->nlmsg_seq, 1709 RTM_NEWROUTE, NLM_F_MULTI) < 0) 1710 break; 1711 } 1712 1713 rcu_read_unlock(); 1714 cb->args[0] = idx; 1715 1716 return skb->len; 1717 } 1718 1719 /* net namespace implementation */ 1720 static int __net_init mctp_routes_net_init(struct net *net) 1721 { 1722 struct netns_mctp *ns = &net->mctp; 1723 1724 INIT_LIST_HEAD(&ns->routes); 1725 hash_init(ns->binds); 1726 mutex_init(&ns->bind_lock); 1727 INIT_HLIST_HEAD(&ns->keys); 1728 spin_lock_init(&ns->keys_lock); 1729 WARN_ON(mctp_default_net_set(net, MCTP_INITIAL_DEFAULT_NET)); 1730 return 0; 1731 } 1732 1733 static void __net_exit mctp_routes_net_exit(struct net *net) 1734 { 1735 struct mctp_route *rt; 1736 1737 rcu_read_lock(); 1738 list_for_each_entry_rcu(rt, &net->mctp.routes, list) 1739 mctp_route_release(rt); 1740 rcu_read_unlock(); 1741 } 1742 1743 static struct pernet_operations mctp_net_ops = { 1744 .init = mctp_routes_net_init, 1745 .exit = mctp_routes_net_exit, 1746 }; 1747 1748 static const struct rtnl_msg_handler mctp_route_rtnl_msg_handlers[] = { 1749 {THIS_MODULE, PF_MCTP, RTM_NEWROUTE, mctp_newroute, NULL, 0}, 1750 {THIS_MODULE, PF_MCTP, RTM_DELROUTE, mctp_delroute, NULL, 0}, 1751 {THIS_MODULE, PF_MCTP, RTM_GETROUTE, NULL, mctp_dump_rtinfo, 0}, 1752 }; 1753 1754 int __init mctp_routes_init(void) 1755 { 1756 int err; 1757 1758 dev_add_pack(&mctp_packet_type); 1759 1760 err = register_pernet_subsys(&mctp_net_ops); 1761 if (err) 1762 goto err_pernet; 1763 1764 err = rtnl_register_many(mctp_route_rtnl_msg_handlers); 1765 if (err) 1766 goto err_rtnl; 1767 1768 return 0; 1769 1770 err_rtnl: 1771 unregister_pernet_subsys(&mctp_net_ops); 1772 err_pernet: 1773 dev_remove_pack(&mctp_packet_type); 1774 return err; 1775 } 1776 1777 void mctp_routes_exit(void) 1778 { 1779 rtnl_unregister_many(mctp_route_rtnl_msg_handlers); 1780 unregister_pernet_subsys(&mctp_net_ops); 1781 dev_remove_pack(&mctp_packet_type); 1782 } 1783 1784 #if IS_ENABLED(CONFIG_MCTP_TEST) 1785 #include "test/route-test.c" 1786 #endif 1787