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