1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Management Component Transport Protocol (MCTP) 4 * 5 * Copyright (c) 2021 Code Construct 6 * Copyright (c) 2021 Google 7 */ 8 9 #include <linux/compat.h> 10 #include <linux/if_arp.h> 11 #include <linux/net.h> 12 #include <linux/mctp.h> 13 #include <linux/module.h> 14 #include <linux/socket.h> 15 16 #include <net/mctp.h> 17 #include <net/mctpdevice.h> 18 #include <net/sock.h> 19 20 #define CREATE_TRACE_POINTS 21 #include <trace/events/mctp.h> 22 23 /* socket implementation */ 24 25 static void mctp_sk_expire_keys(struct timer_list *timer); 26 27 static int mctp_release(struct socket *sock) 28 { 29 struct sock *sk = sock->sk; 30 31 if (sk) { 32 sock->sk = NULL; 33 sk->sk_prot->close(sk, 0); 34 } 35 36 return 0; 37 } 38 39 /* Generic sockaddr checks, padding checks only so far */ 40 static bool mctp_sockaddr_is_ok(const struct sockaddr_mctp *addr) 41 { 42 return !addr->__smctp_pad0 && !addr->__smctp_pad1; 43 } 44 45 static bool mctp_sockaddr_ext_is_ok(const struct sockaddr_mctp_ext *addr) 46 { 47 return !addr->__smctp_pad0[0] && 48 !addr->__smctp_pad0[1] && 49 !addr->__smctp_pad0[2]; 50 } 51 52 static int mctp_bind(struct socket *sock, struct sockaddr *addr, int addrlen) 53 { 54 struct sock *sk = sock->sk; 55 struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk); 56 struct sockaddr_mctp *smctp; 57 int rc; 58 59 if (addrlen < sizeof(*smctp)) 60 return -EINVAL; 61 62 if (addr->sa_family != AF_MCTP) 63 return -EAFNOSUPPORT; 64 65 if (!capable(CAP_NET_BIND_SERVICE)) 66 return -EACCES; 67 68 /* it's a valid sockaddr for MCTP, cast and do protocol checks */ 69 smctp = (struct sockaddr_mctp *)addr; 70 71 if (!mctp_sockaddr_is_ok(smctp)) 72 return -EINVAL; 73 74 lock_sock(sk); 75 76 /* TODO: allow rebind */ 77 if (sk_hashed(sk)) { 78 rc = -EADDRINUSE; 79 goto out_release; 80 } 81 msk->bind_net = smctp->smctp_network; 82 msk->bind_addr = smctp->smctp_addr.s_addr; 83 msk->bind_type = smctp->smctp_type & 0x7f; /* ignore the IC bit */ 84 85 rc = sk->sk_prot->hash(sk); 86 87 out_release: 88 release_sock(sk); 89 90 return rc; 91 } 92 93 static int mctp_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) 94 { 95 DECLARE_SOCKADDR(struct sockaddr_mctp *, addr, msg->msg_name); 96 int rc, addrlen = msg->msg_namelen; 97 struct sock *sk = sock->sk; 98 struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk); 99 struct mctp_skb_cb *cb; 100 struct mctp_route *rt; 101 struct sk_buff *skb = NULL; 102 int hlen; 103 104 if (addr) { 105 const u8 tagbits = MCTP_TAG_MASK | MCTP_TAG_OWNER | 106 MCTP_TAG_PREALLOC; 107 108 if (addrlen < sizeof(struct sockaddr_mctp)) 109 return -EINVAL; 110 if (addr->smctp_family != AF_MCTP) 111 return -EINVAL; 112 if (!mctp_sockaddr_is_ok(addr)) 113 return -EINVAL; 114 if (addr->smctp_tag & ~tagbits) 115 return -EINVAL; 116 /* can't preallocate a non-owned tag */ 117 if (addr->smctp_tag & MCTP_TAG_PREALLOC && 118 !(addr->smctp_tag & MCTP_TAG_OWNER)) 119 return -EINVAL; 120 121 } else { 122 /* TODO: connect()ed sockets */ 123 return -EDESTADDRREQ; 124 } 125 126 if (!capable(CAP_NET_RAW)) 127 return -EACCES; 128 129 if (addr->smctp_network == MCTP_NET_ANY) 130 addr->smctp_network = mctp_default_net(sock_net(sk)); 131 132 /* direct addressing */ 133 if (msk->addr_ext && addrlen >= sizeof(struct sockaddr_mctp_ext)) { 134 DECLARE_SOCKADDR(struct sockaddr_mctp_ext *, 135 extaddr, msg->msg_name); 136 struct net_device *dev; 137 138 rc = -EINVAL; 139 rcu_read_lock(); 140 dev = dev_get_by_index_rcu(sock_net(sk), extaddr->smctp_ifindex); 141 /* check for correct halen */ 142 if (dev && extaddr->smctp_halen == dev->addr_len) { 143 hlen = LL_RESERVED_SPACE(dev) + sizeof(struct mctp_hdr); 144 rc = 0; 145 } 146 rcu_read_unlock(); 147 if (rc) 148 goto err_free; 149 rt = NULL; 150 } else { 151 rt = mctp_route_lookup(sock_net(sk), addr->smctp_network, 152 addr->smctp_addr.s_addr); 153 if (!rt) { 154 rc = -EHOSTUNREACH; 155 goto err_free; 156 } 157 hlen = LL_RESERVED_SPACE(rt->dev->dev) + sizeof(struct mctp_hdr); 158 } 159 160 skb = sock_alloc_send_skb(sk, hlen + 1 + len, 161 msg->msg_flags & MSG_DONTWAIT, &rc); 162 if (!skb) 163 return rc; 164 165 skb_reserve(skb, hlen); 166 167 /* set type as fist byte in payload */ 168 *(u8 *)skb_put(skb, 1) = addr->smctp_type; 169 170 rc = memcpy_from_msg((void *)skb_put(skb, len), msg, len); 171 if (rc < 0) 172 goto err_free; 173 174 /* set up cb */ 175 cb = __mctp_cb(skb); 176 cb->net = addr->smctp_network; 177 178 if (!rt) { 179 /* fill extended address in cb */ 180 DECLARE_SOCKADDR(struct sockaddr_mctp_ext *, 181 extaddr, msg->msg_name); 182 183 if (!mctp_sockaddr_ext_is_ok(extaddr) || 184 extaddr->smctp_halen > sizeof(cb->haddr)) { 185 rc = -EINVAL; 186 goto err_free; 187 } 188 189 cb->ifindex = extaddr->smctp_ifindex; 190 /* smctp_halen is checked above */ 191 cb->halen = extaddr->smctp_halen; 192 memcpy(cb->haddr, extaddr->smctp_haddr, cb->halen); 193 } 194 195 rc = mctp_local_output(sk, rt, skb, addr->smctp_addr.s_addr, 196 addr->smctp_tag); 197 198 return rc ? : len; 199 200 err_free: 201 kfree_skb(skb); 202 return rc; 203 } 204 205 static int mctp_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 206 int flags) 207 { 208 DECLARE_SOCKADDR(struct sockaddr_mctp *, addr, msg->msg_name); 209 struct sock *sk = sock->sk; 210 struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk); 211 struct sk_buff *skb; 212 size_t msglen; 213 u8 type; 214 int rc; 215 216 if (flags & ~(MSG_DONTWAIT | MSG_TRUNC | MSG_PEEK)) 217 return -EOPNOTSUPP; 218 219 skb = skb_recv_datagram(sk, flags, &rc); 220 if (!skb) 221 return rc; 222 223 if (!skb->len) { 224 rc = 0; 225 goto out_free; 226 } 227 228 /* extract message type, remove from data */ 229 type = *((u8 *)skb->data); 230 msglen = skb->len - 1; 231 232 if (len < msglen) 233 msg->msg_flags |= MSG_TRUNC; 234 else 235 len = msglen; 236 237 rc = skb_copy_datagram_msg(skb, 1, msg, len); 238 if (rc < 0) 239 goto out_free; 240 241 sock_recv_cmsgs(msg, sk, skb); 242 243 if (addr) { 244 struct mctp_skb_cb *cb = mctp_cb(skb); 245 /* TODO: expand mctp_skb_cb for header fields? */ 246 struct mctp_hdr *hdr = mctp_hdr(skb); 247 248 addr = msg->msg_name; 249 addr->smctp_family = AF_MCTP; 250 addr->__smctp_pad0 = 0; 251 addr->smctp_network = cb->net; 252 addr->smctp_addr.s_addr = hdr->src; 253 addr->smctp_type = type; 254 addr->smctp_tag = hdr->flags_seq_tag & 255 (MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO); 256 addr->__smctp_pad1 = 0; 257 msg->msg_namelen = sizeof(*addr); 258 259 if (msk->addr_ext) { 260 DECLARE_SOCKADDR(struct sockaddr_mctp_ext *, ae, 261 msg->msg_name); 262 msg->msg_namelen = sizeof(*ae); 263 ae->smctp_ifindex = cb->ifindex; 264 ae->smctp_halen = cb->halen; 265 memset(ae->__smctp_pad0, 0x0, sizeof(ae->__smctp_pad0)); 266 memset(ae->smctp_haddr, 0x0, sizeof(ae->smctp_haddr)); 267 memcpy(ae->smctp_haddr, cb->haddr, cb->halen); 268 } 269 } 270 271 rc = len; 272 273 if (flags & MSG_TRUNC) 274 rc = msglen; 275 276 out_free: 277 skb_free_datagram(sk, skb); 278 return rc; 279 } 280 281 /* We're done with the key; invalidate, stop reassembly, and remove from lists. 282 */ 283 static void __mctp_key_remove(struct mctp_sk_key *key, struct net *net, 284 unsigned long flags, unsigned long reason) 285 __releases(&key->lock) 286 __must_hold(&net->mctp.keys_lock) 287 { 288 struct sk_buff *skb; 289 290 trace_mctp_key_release(key, reason); 291 skb = key->reasm_head; 292 key->reasm_head = NULL; 293 key->reasm_dead = true; 294 key->valid = false; 295 mctp_dev_release_key(key->dev, key); 296 spin_unlock_irqrestore(&key->lock, flags); 297 298 if (!hlist_unhashed(&key->hlist)) { 299 hlist_del_init(&key->hlist); 300 hlist_del_init(&key->sklist); 301 /* unref for the lists */ 302 mctp_key_unref(key); 303 } 304 305 kfree_skb(skb); 306 } 307 308 static int mctp_setsockopt(struct socket *sock, int level, int optname, 309 sockptr_t optval, unsigned int optlen) 310 { 311 struct mctp_sock *msk = container_of(sock->sk, struct mctp_sock, sk); 312 int val; 313 314 if (level != SOL_MCTP) 315 return -EINVAL; 316 317 if (optname == MCTP_OPT_ADDR_EXT) { 318 if (optlen != sizeof(int)) 319 return -EINVAL; 320 if (copy_from_sockptr(&val, optval, sizeof(int))) 321 return -EFAULT; 322 msk->addr_ext = val; 323 return 0; 324 } 325 326 return -ENOPROTOOPT; 327 } 328 329 static int mctp_getsockopt(struct socket *sock, int level, int optname, 330 char __user *optval, int __user *optlen) 331 { 332 struct mctp_sock *msk = container_of(sock->sk, struct mctp_sock, sk); 333 int len, val; 334 335 if (level != SOL_MCTP) 336 return -EINVAL; 337 338 if (get_user(len, optlen)) 339 return -EFAULT; 340 341 if (optname == MCTP_OPT_ADDR_EXT) { 342 if (len != sizeof(int)) 343 return -EINVAL; 344 val = !!msk->addr_ext; 345 if (copy_to_user(optval, &val, len)) 346 return -EFAULT; 347 return 0; 348 } 349 350 return -EINVAL; 351 } 352 353 /* helpers for reading/writing the tag ioc, handling compatibility across the 354 * two versions, and some basic API error checking 355 */ 356 static int mctp_ioctl_tag_copy_from_user(unsigned long arg, 357 struct mctp_ioc_tag_ctl2 *ctl, 358 bool tagv2) 359 { 360 struct mctp_ioc_tag_ctl ctl_compat; 361 unsigned long size; 362 void *ptr; 363 int rc; 364 365 if (tagv2) { 366 size = sizeof(*ctl); 367 ptr = ctl; 368 } else { 369 size = sizeof(ctl_compat); 370 ptr = &ctl_compat; 371 } 372 373 rc = copy_from_user(ptr, (void __user *)arg, size); 374 if (rc) 375 return -EFAULT; 376 377 if (!tagv2) { 378 /* compat, using defaults for new fields */ 379 ctl->net = MCTP_INITIAL_DEFAULT_NET; 380 ctl->peer_addr = ctl_compat.peer_addr; 381 ctl->local_addr = MCTP_ADDR_ANY; 382 ctl->flags = ctl_compat.flags; 383 ctl->tag = ctl_compat.tag; 384 } 385 386 if (ctl->flags) 387 return -EINVAL; 388 389 if (ctl->local_addr != MCTP_ADDR_ANY && 390 ctl->local_addr != MCTP_ADDR_NULL) 391 return -EINVAL; 392 393 return 0; 394 } 395 396 static int mctp_ioctl_tag_copy_to_user(unsigned long arg, 397 struct mctp_ioc_tag_ctl2 *ctl, 398 bool tagv2) 399 { 400 struct mctp_ioc_tag_ctl ctl_compat; 401 unsigned long size; 402 void *ptr; 403 int rc; 404 405 if (tagv2) { 406 ptr = ctl; 407 size = sizeof(*ctl); 408 } else { 409 ctl_compat.peer_addr = ctl->peer_addr; 410 ctl_compat.tag = ctl->tag; 411 ctl_compat.flags = ctl->flags; 412 413 ptr = &ctl_compat; 414 size = sizeof(ctl_compat); 415 } 416 417 rc = copy_to_user((void __user *)arg, ptr, size); 418 if (rc) 419 return -EFAULT; 420 421 return 0; 422 } 423 424 static int mctp_ioctl_alloctag(struct mctp_sock *msk, bool tagv2, 425 unsigned long arg) 426 { 427 struct net *net = sock_net(&msk->sk); 428 struct mctp_sk_key *key = NULL; 429 struct mctp_ioc_tag_ctl2 ctl; 430 unsigned long flags; 431 u8 tag; 432 int rc; 433 434 rc = mctp_ioctl_tag_copy_from_user(arg, &ctl, tagv2); 435 if (rc) 436 return rc; 437 438 if (ctl.tag) 439 return -EINVAL; 440 441 key = mctp_alloc_local_tag(msk, ctl.net, MCTP_ADDR_ANY, 442 ctl.peer_addr, true, &tag); 443 if (IS_ERR(key)) 444 return PTR_ERR(key); 445 446 ctl.tag = tag | MCTP_TAG_OWNER | MCTP_TAG_PREALLOC; 447 rc = mctp_ioctl_tag_copy_to_user(arg, &ctl, tagv2); 448 if (rc) { 449 unsigned long fl2; 450 /* Unwind our key allocation: the keys list lock needs to be 451 * taken before the individual key locks, and we need a valid 452 * flags value (fl2) to pass to __mctp_key_remove, hence the 453 * second spin_lock_irqsave() rather than a plain spin_lock(). 454 */ 455 spin_lock_irqsave(&net->mctp.keys_lock, flags); 456 spin_lock_irqsave(&key->lock, fl2); 457 __mctp_key_remove(key, net, fl2, MCTP_TRACE_KEY_DROPPED); 458 mctp_key_unref(key); 459 spin_unlock_irqrestore(&net->mctp.keys_lock, flags); 460 return rc; 461 } 462 463 mctp_key_unref(key); 464 return 0; 465 } 466 467 static int mctp_ioctl_droptag(struct mctp_sock *msk, bool tagv2, 468 unsigned long arg) 469 { 470 struct net *net = sock_net(&msk->sk); 471 struct mctp_ioc_tag_ctl2 ctl; 472 unsigned long flags, fl2; 473 struct mctp_sk_key *key; 474 struct hlist_node *tmp; 475 int rc; 476 u8 tag; 477 478 rc = mctp_ioctl_tag_copy_from_user(arg, &ctl, tagv2); 479 if (rc) 480 return rc; 481 482 /* Must be a local tag, TO set, preallocated */ 483 if ((ctl.tag & ~MCTP_TAG_MASK) != (MCTP_TAG_OWNER | MCTP_TAG_PREALLOC)) 484 return -EINVAL; 485 486 tag = ctl.tag & MCTP_TAG_MASK; 487 rc = -EINVAL; 488 489 spin_lock_irqsave(&net->mctp.keys_lock, flags); 490 hlist_for_each_entry_safe(key, tmp, &msk->keys, sklist) { 491 /* we do an irqsave here, even though we know the irq state, 492 * so we have the flags to pass to __mctp_key_remove 493 */ 494 spin_lock_irqsave(&key->lock, fl2); 495 if (key->manual_alloc && 496 ctl.net == key->net && 497 ctl.peer_addr == key->peer_addr && 498 tag == key->tag) { 499 __mctp_key_remove(key, net, fl2, 500 MCTP_TRACE_KEY_DROPPED); 501 rc = 0; 502 } else { 503 spin_unlock_irqrestore(&key->lock, fl2); 504 } 505 } 506 spin_unlock_irqrestore(&net->mctp.keys_lock, flags); 507 508 return rc; 509 } 510 511 static int mctp_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 512 { 513 struct mctp_sock *msk = container_of(sock->sk, struct mctp_sock, sk); 514 bool tagv2 = false; 515 516 switch (cmd) { 517 case SIOCMCTPALLOCTAG2: 518 case SIOCMCTPALLOCTAG: 519 tagv2 = cmd == SIOCMCTPALLOCTAG2; 520 return mctp_ioctl_alloctag(msk, tagv2, arg); 521 case SIOCMCTPDROPTAG: 522 case SIOCMCTPDROPTAG2: 523 tagv2 = cmd == SIOCMCTPDROPTAG2; 524 return mctp_ioctl_droptag(msk, tagv2, arg); 525 } 526 527 return -EINVAL; 528 } 529 530 #ifdef CONFIG_COMPAT 531 static int mctp_compat_ioctl(struct socket *sock, unsigned int cmd, 532 unsigned long arg) 533 { 534 void __user *argp = compat_ptr(arg); 535 536 switch (cmd) { 537 /* These have compatible ptr layouts */ 538 case SIOCMCTPALLOCTAG: 539 case SIOCMCTPDROPTAG: 540 return mctp_ioctl(sock, cmd, (unsigned long)argp); 541 } 542 543 return -ENOIOCTLCMD; 544 } 545 #endif 546 547 static const struct proto_ops mctp_dgram_ops = { 548 .family = PF_MCTP, 549 .release = mctp_release, 550 .bind = mctp_bind, 551 .connect = sock_no_connect, 552 .socketpair = sock_no_socketpair, 553 .accept = sock_no_accept, 554 .getname = sock_no_getname, 555 .poll = datagram_poll, 556 .ioctl = mctp_ioctl, 557 .gettstamp = sock_gettstamp, 558 .listen = sock_no_listen, 559 .shutdown = sock_no_shutdown, 560 .setsockopt = mctp_setsockopt, 561 .getsockopt = mctp_getsockopt, 562 .sendmsg = mctp_sendmsg, 563 .recvmsg = mctp_recvmsg, 564 .mmap = sock_no_mmap, 565 #ifdef CONFIG_COMPAT 566 .compat_ioctl = mctp_compat_ioctl, 567 #endif 568 }; 569 570 static void mctp_sk_expire_keys(struct timer_list *timer) 571 { 572 struct mctp_sock *msk = container_of(timer, struct mctp_sock, 573 key_expiry); 574 struct net *net = sock_net(&msk->sk); 575 unsigned long next_expiry, flags, fl2; 576 struct mctp_sk_key *key; 577 struct hlist_node *tmp; 578 bool next_expiry_valid = false; 579 580 spin_lock_irqsave(&net->mctp.keys_lock, flags); 581 582 hlist_for_each_entry_safe(key, tmp, &msk->keys, sklist) { 583 /* don't expire. manual_alloc is immutable, no locking 584 * required. 585 */ 586 if (key->manual_alloc) 587 continue; 588 589 spin_lock_irqsave(&key->lock, fl2); 590 if (!time_after_eq(key->expiry, jiffies)) { 591 __mctp_key_remove(key, net, fl2, 592 MCTP_TRACE_KEY_TIMEOUT); 593 continue; 594 } 595 596 if (next_expiry_valid) { 597 if (time_before(key->expiry, next_expiry)) 598 next_expiry = key->expiry; 599 } else { 600 next_expiry = key->expiry; 601 next_expiry_valid = true; 602 } 603 spin_unlock_irqrestore(&key->lock, fl2); 604 } 605 606 spin_unlock_irqrestore(&net->mctp.keys_lock, flags); 607 608 if (next_expiry_valid) 609 mod_timer(timer, next_expiry); 610 } 611 612 static int mctp_sk_init(struct sock *sk) 613 { 614 struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk); 615 616 INIT_HLIST_HEAD(&msk->keys); 617 timer_setup(&msk->key_expiry, mctp_sk_expire_keys, 0); 618 return 0; 619 } 620 621 static void mctp_sk_close(struct sock *sk, long timeout) 622 { 623 sk_common_release(sk); 624 } 625 626 static int mctp_sk_hash(struct sock *sk) 627 { 628 struct net *net = sock_net(sk); 629 630 mutex_lock(&net->mctp.bind_lock); 631 sk_add_node_rcu(sk, &net->mctp.binds); 632 mutex_unlock(&net->mctp.bind_lock); 633 634 return 0; 635 } 636 637 static void mctp_sk_unhash(struct sock *sk) 638 { 639 struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk); 640 struct net *net = sock_net(sk); 641 unsigned long flags, fl2; 642 struct mctp_sk_key *key; 643 struct hlist_node *tmp; 644 645 /* remove from any type-based binds */ 646 mutex_lock(&net->mctp.bind_lock); 647 sk_del_node_init_rcu(sk); 648 mutex_unlock(&net->mctp.bind_lock); 649 650 /* remove tag allocations */ 651 spin_lock_irqsave(&net->mctp.keys_lock, flags); 652 hlist_for_each_entry_safe(key, tmp, &msk->keys, sklist) { 653 spin_lock_irqsave(&key->lock, fl2); 654 __mctp_key_remove(key, net, fl2, MCTP_TRACE_KEY_CLOSED); 655 } 656 sock_set_flag(sk, SOCK_DEAD); 657 spin_unlock_irqrestore(&net->mctp.keys_lock, flags); 658 659 /* Since there are no more tag allocations (we have removed all of the 660 * keys), stop any pending expiry events. the timer cannot be re-queued 661 * as the sk is no longer observable 662 */ 663 del_timer_sync(&msk->key_expiry); 664 } 665 666 static void mctp_sk_destruct(struct sock *sk) 667 { 668 skb_queue_purge(&sk->sk_receive_queue); 669 } 670 671 static struct proto mctp_proto = { 672 .name = "MCTP", 673 .owner = THIS_MODULE, 674 .obj_size = sizeof(struct mctp_sock), 675 .init = mctp_sk_init, 676 .close = mctp_sk_close, 677 .hash = mctp_sk_hash, 678 .unhash = mctp_sk_unhash, 679 }; 680 681 static int mctp_pf_create(struct net *net, struct socket *sock, 682 int protocol, int kern) 683 { 684 const struct proto_ops *ops; 685 struct proto *proto; 686 struct sock *sk; 687 int rc; 688 689 if (protocol) 690 return -EPROTONOSUPPORT; 691 692 /* only datagram sockets are supported */ 693 if (sock->type != SOCK_DGRAM) 694 return -ESOCKTNOSUPPORT; 695 696 proto = &mctp_proto; 697 ops = &mctp_dgram_ops; 698 699 sock->state = SS_UNCONNECTED; 700 sock->ops = ops; 701 702 sk = sk_alloc(net, PF_MCTP, GFP_KERNEL, proto, kern); 703 if (!sk) 704 return -ENOMEM; 705 706 sock_init_data(sock, sk); 707 sk->sk_destruct = mctp_sk_destruct; 708 709 rc = 0; 710 if (sk->sk_prot->init) 711 rc = sk->sk_prot->init(sk); 712 713 if (rc) 714 goto err_sk_put; 715 716 return 0; 717 718 err_sk_put: 719 sock_orphan(sk); 720 sock_put(sk); 721 return rc; 722 } 723 724 static struct net_proto_family mctp_pf = { 725 .family = PF_MCTP, 726 .create = mctp_pf_create, 727 .owner = THIS_MODULE, 728 }; 729 730 static __init int mctp_init(void) 731 { 732 int rc; 733 734 /* ensure our uapi tag definitions match the header format */ 735 BUILD_BUG_ON(MCTP_TAG_OWNER != MCTP_HDR_FLAG_TO); 736 BUILD_BUG_ON(MCTP_TAG_MASK != MCTP_HDR_TAG_MASK); 737 738 pr_info("mctp: management component transport protocol core\n"); 739 740 rc = sock_register(&mctp_pf); 741 if (rc) 742 return rc; 743 744 rc = proto_register(&mctp_proto, 0); 745 if (rc) 746 goto err_unreg_sock; 747 748 rc = mctp_routes_init(); 749 if (rc) 750 goto err_unreg_proto; 751 752 rc = mctp_neigh_init(); 753 if (rc) 754 goto err_unreg_routes; 755 756 mctp_device_init(); 757 758 return 0; 759 760 err_unreg_routes: 761 mctp_routes_exit(); 762 err_unreg_proto: 763 proto_unregister(&mctp_proto); 764 err_unreg_sock: 765 sock_unregister(PF_MCTP); 766 767 return rc; 768 } 769 770 static __exit void mctp_exit(void) 771 { 772 mctp_device_exit(); 773 mctp_neigh_exit(); 774 mctp_routes_exit(); 775 proto_unregister(&mctp_proto); 776 sock_unregister(PF_MCTP); 777 } 778 779 subsys_initcall(mctp_init); 780 module_exit(mctp_exit); 781 782 MODULE_DESCRIPTION("MCTP core"); 783 MODULE_AUTHOR("Jeremy Kerr <jk@codeconstruct.com.au>"); 784 785 MODULE_ALIAS_NETPROTO(PF_MCTP); 786