Jeremy Kerr | bc49d81 | 2021-07-29 10:20:39 +0800 | [diff] [blame] | 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 | |
Jeremy Kerr | 583be98 | 2021-07-29 10:20:44 +0800 | [diff] [blame] | 9 | #include <linux/if_arp.h> |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 10 | #include <linux/net.h> |
| 11 | #include <linux/mctp.h> |
Jeremy Kerr | bc49d81 | 2021-07-29 10:20:39 +0800 | [diff] [blame] | 12 | #include <linux/module.h> |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 13 | #include <linux/socket.h> |
| 14 | |
Jeremy Kerr | 583be98 | 2021-07-29 10:20:44 +0800 | [diff] [blame] | 15 | #include <net/mctp.h> |
| 16 | #include <net/mctpdevice.h> |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 17 | #include <net/sock.h> |
| 18 | |
Jeremy Kerr | 4f9e1ba | 2021-09-29 15:26:10 +0800 | [diff] [blame] | 19 | #define CREATE_TRACE_POINTS |
| 20 | #include <trace/events/mctp.h> |
| 21 | |
Jeremy Kerr | 583be98 | 2021-07-29 10:20:44 +0800 | [diff] [blame] | 22 | /* socket implementation */ |
| 23 | |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 24 | static int mctp_release(struct socket *sock) |
| 25 | { |
| 26 | struct sock *sk = sock->sk; |
| 27 | |
| 28 | if (sk) { |
| 29 | sock->sk = NULL; |
| 30 | sk->sk_prot->close(sk, 0); |
| 31 | } |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
Eugene Syromiatnikov | 1e4b50f | 2021-11-03 20:09:42 +0100 | [diff] [blame] | 36 | /* Generic sockaddr checks, padding checks only so far */ |
| 37 | static bool mctp_sockaddr_is_ok(const struct sockaddr_mctp *addr) |
| 38 | { |
| 39 | return !addr->__smctp_pad0 && !addr->__smctp_pad1; |
| 40 | } |
| 41 | |
Eugene Syromiatnikov | e9ea574 | 2021-11-03 20:09:46 +0100 | [diff] [blame] | 42 | static bool mctp_sockaddr_ext_is_ok(const struct sockaddr_mctp_ext *addr) |
| 43 | { |
| 44 | return !addr->__smctp_pad0[0] && |
| 45 | !addr->__smctp_pad0[1] && |
| 46 | !addr->__smctp_pad0[2]; |
| 47 | } |
| 48 | |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 49 | static int mctp_bind(struct socket *sock, struct sockaddr *addr, int addrlen) |
| 50 | { |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 51 | struct sock *sk = sock->sk; |
| 52 | struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk); |
| 53 | struct sockaddr_mctp *smctp; |
| 54 | int rc; |
| 55 | |
| 56 | if (addrlen < sizeof(*smctp)) |
| 57 | return -EINVAL; |
| 58 | |
| 59 | if (addr->sa_family != AF_MCTP) |
| 60 | return -EAFNOSUPPORT; |
| 61 | |
| 62 | if (!capable(CAP_NET_BIND_SERVICE)) |
| 63 | return -EACCES; |
| 64 | |
| 65 | /* it's a valid sockaddr for MCTP, cast and do protocol checks */ |
| 66 | smctp = (struct sockaddr_mctp *)addr; |
| 67 | |
Eugene Syromiatnikov | 1e4b50f | 2021-11-03 20:09:42 +0100 | [diff] [blame] | 68 | if (!mctp_sockaddr_is_ok(smctp)) |
| 69 | return -EINVAL; |
| 70 | |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 71 | lock_sock(sk); |
| 72 | |
| 73 | /* TODO: allow rebind */ |
| 74 | if (sk_hashed(sk)) { |
| 75 | rc = -EADDRINUSE; |
| 76 | goto out_release; |
| 77 | } |
| 78 | msk->bind_net = smctp->smctp_network; |
| 79 | msk->bind_addr = smctp->smctp_addr.s_addr; |
| 80 | msk->bind_type = smctp->smctp_type & 0x7f; /* ignore the IC bit */ |
| 81 | |
| 82 | rc = sk->sk_prot->hash(sk); |
| 83 | |
| 84 | out_release: |
| 85 | release_sock(sk); |
| 86 | |
| 87 | return rc; |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static int mctp_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) |
| 91 | { |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 92 | DECLARE_SOCKADDR(struct sockaddr_mctp *, addr, msg->msg_name); |
| 93 | const int hlen = MCTP_HEADER_MAXLEN + sizeof(struct mctp_hdr); |
| 94 | int rc, addrlen = msg->msg_namelen; |
| 95 | struct sock *sk = sock->sk; |
Jeremy Kerr | 99ce45d | 2021-10-26 09:57:28 +0800 | [diff] [blame] | 96 | struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk); |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 97 | struct mctp_skb_cb *cb; |
| 98 | struct mctp_route *rt; |
| 99 | struct sk_buff *skb; |
| 100 | |
| 101 | if (addr) { |
| 102 | if (addrlen < sizeof(struct sockaddr_mctp)) |
| 103 | return -EINVAL; |
| 104 | if (addr->smctp_family != AF_MCTP) |
| 105 | return -EINVAL; |
Eugene Syromiatnikov | 1e4b50f | 2021-11-03 20:09:42 +0100 | [diff] [blame] | 106 | if (!mctp_sockaddr_is_ok(addr)) |
| 107 | return -EINVAL; |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 108 | if (addr->smctp_tag & ~(MCTP_TAG_MASK | MCTP_TAG_OWNER)) |
| 109 | return -EINVAL; |
| 110 | |
| 111 | } else { |
| 112 | /* TODO: connect()ed sockets */ |
| 113 | return -EDESTADDRREQ; |
| 114 | } |
| 115 | |
| 116 | if (!capable(CAP_NET_RAW)) |
| 117 | return -EACCES; |
| 118 | |
Matt Johnston | 03f2bbc | 2021-07-29 10:20:52 +0800 | [diff] [blame] | 119 | if (addr->smctp_network == MCTP_NET_ANY) |
| 120 | addr->smctp_network = mctp_default_net(sock_net(sk)); |
| 121 | |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 122 | skb = sock_alloc_send_skb(sk, hlen + 1 + len, |
| 123 | msg->msg_flags & MSG_DONTWAIT, &rc); |
| 124 | if (!skb) |
| 125 | return rc; |
| 126 | |
| 127 | skb_reserve(skb, hlen); |
| 128 | |
| 129 | /* set type as fist byte in payload */ |
| 130 | *(u8 *)skb_put(skb, 1) = addr->smctp_type; |
| 131 | |
| 132 | rc = memcpy_from_msg((void *)skb_put(skb, len), msg, len); |
Jeremy Kerr | 99ce45d | 2021-10-26 09:57:28 +0800 | [diff] [blame] | 133 | if (rc < 0) |
| 134 | goto err_free; |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 135 | |
| 136 | /* set up cb */ |
| 137 | cb = __mctp_cb(skb); |
| 138 | cb->net = addr->smctp_network; |
| 139 | |
Jeremy Kerr | 99ce45d | 2021-10-26 09:57:28 +0800 | [diff] [blame] | 140 | /* direct addressing */ |
| 141 | if (msk->addr_ext && addrlen >= sizeof(struct sockaddr_mctp_ext)) { |
| 142 | DECLARE_SOCKADDR(struct sockaddr_mctp_ext *, |
| 143 | extaddr, msg->msg_name); |
| 144 | |
Eugene Syromiatnikov | e9ea574 | 2021-11-03 20:09:46 +0100 | [diff] [blame] | 145 | if (!mctp_sockaddr_ext_is_ok(extaddr) || |
| 146 | extaddr->smctp_halen > sizeof(cb->haddr)) { |
Jeremy Kerr | 99ce45d | 2021-10-26 09:57:28 +0800 | [diff] [blame] | 147 | rc = -EINVAL; |
| 148 | goto err_free; |
| 149 | } |
| 150 | |
| 151 | cb->ifindex = extaddr->smctp_ifindex; |
| 152 | cb->halen = extaddr->smctp_halen; |
| 153 | memcpy(cb->haddr, extaddr->smctp_haddr, cb->halen); |
| 154 | |
| 155 | rt = NULL; |
| 156 | } else { |
| 157 | rt = mctp_route_lookup(sock_net(sk), addr->smctp_network, |
| 158 | addr->smctp_addr.s_addr); |
| 159 | if (!rt) { |
| 160 | rc = -EHOSTUNREACH; |
| 161 | goto err_free; |
| 162 | } |
| 163 | } |
| 164 | |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 165 | rc = mctp_local_output(sk, rt, skb, addr->smctp_addr.s_addr, |
| 166 | addr->smctp_tag); |
| 167 | |
| 168 | return rc ? : len; |
Jeremy Kerr | 99ce45d | 2021-10-26 09:57:28 +0800 | [diff] [blame] | 169 | |
| 170 | err_free: |
| 171 | kfree_skb(skb); |
| 172 | return rc; |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static int mctp_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, |
| 176 | int flags) |
| 177 | { |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 178 | DECLARE_SOCKADDR(struct sockaddr_mctp *, addr, msg->msg_name); |
| 179 | struct sock *sk = sock->sk; |
Jeremy Kerr | 99ce45d | 2021-10-26 09:57:28 +0800 | [diff] [blame] | 180 | struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk); |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 181 | struct sk_buff *skb; |
| 182 | size_t msglen; |
| 183 | u8 type; |
| 184 | int rc; |
| 185 | |
| 186 | if (flags & ~(MSG_DONTWAIT | MSG_TRUNC | MSG_PEEK)) |
| 187 | return -EOPNOTSUPP; |
| 188 | |
| 189 | skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &rc); |
| 190 | if (!skb) |
| 191 | return rc; |
| 192 | |
| 193 | if (!skb->len) { |
| 194 | rc = 0; |
| 195 | goto out_free; |
| 196 | } |
| 197 | |
| 198 | /* extract message type, remove from data */ |
| 199 | type = *((u8 *)skb->data); |
| 200 | msglen = skb->len - 1; |
| 201 | |
| 202 | if (len < msglen) |
| 203 | msg->msg_flags |= MSG_TRUNC; |
| 204 | else |
| 205 | len = msglen; |
| 206 | |
| 207 | rc = skb_copy_datagram_msg(skb, 1, msg, len); |
| 208 | if (rc < 0) |
| 209 | goto out_free; |
| 210 | |
| 211 | sock_recv_ts_and_drops(msg, sk, skb); |
| 212 | |
| 213 | if (addr) { |
| 214 | struct mctp_skb_cb *cb = mctp_cb(skb); |
| 215 | /* TODO: expand mctp_skb_cb for header fields? */ |
| 216 | struct mctp_hdr *hdr = mctp_hdr(skb); |
| 217 | |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 218 | addr = msg->msg_name; |
| 219 | addr->smctp_family = AF_MCTP; |
Eugene Syromiatnikov | 1e4b50f | 2021-11-03 20:09:42 +0100 | [diff] [blame] | 220 | addr->__smctp_pad0 = 0; |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 221 | addr->smctp_network = cb->net; |
| 222 | addr->smctp_addr.s_addr = hdr->src; |
| 223 | addr->smctp_type = type; |
| 224 | addr->smctp_tag = hdr->flags_seq_tag & |
| 225 | (MCTP_HDR_TAG_MASK | MCTP_HDR_FLAG_TO); |
Eugene Syromiatnikov | 1e4b50f | 2021-11-03 20:09:42 +0100 | [diff] [blame] | 226 | addr->__smctp_pad1 = 0; |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 227 | msg->msg_namelen = sizeof(*addr); |
Jeremy Kerr | 99ce45d | 2021-10-26 09:57:28 +0800 | [diff] [blame] | 228 | |
| 229 | if (msk->addr_ext) { |
| 230 | DECLARE_SOCKADDR(struct sockaddr_mctp_ext *, ae, |
| 231 | msg->msg_name); |
| 232 | msg->msg_namelen = sizeof(*ae); |
| 233 | ae->smctp_ifindex = cb->ifindex; |
| 234 | ae->smctp_halen = cb->halen; |
Eugene Syromiatnikov | e9ea574 | 2021-11-03 20:09:46 +0100 | [diff] [blame] | 235 | memset(ae->__smctp_pad0, 0x0, sizeof(ae->__smctp_pad0)); |
Jeremy Kerr | 99ce45d | 2021-10-26 09:57:28 +0800 | [diff] [blame] | 236 | memset(ae->smctp_haddr, 0x0, sizeof(ae->smctp_haddr)); |
| 237 | memcpy(ae->smctp_haddr, cb->haddr, cb->halen); |
| 238 | } |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | rc = len; |
| 242 | |
| 243 | if (flags & MSG_TRUNC) |
| 244 | rc = msglen; |
| 245 | |
| 246 | out_free: |
| 247 | skb_free_datagram(sk, skb); |
| 248 | return rc; |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static int mctp_setsockopt(struct socket *sock, int level, int optname, |
| 252 | sockptr_t optval, unsigned int optlen) |
| 253 | { |
Jeremy Kerr | 99ce45d | 2021-10-26 09:57:28 +0800 | [diff] [blame] | 254 | struct mctp_sock *msk = container_of(sock->sk, struct mctp_sock, sk); |
| 255 | int val; |
| 256 | |
| 257 | if (level != SOL_MCTP) |
| 258 | return -EINVAL; |
| 259 | |
| 260 | if (optname == MCTP_OPT_ADDR_EXT) { |
| 261 | if (optlen != sizeof(int)) |
| 262 | return -EINVAL; |
| 263 | if (copy_from_sockptr(&val, optval, sizeof(int))) |
| 264 | return -EFAULT; |
| 265 | msk->addr_ext = val; |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | return -ENOPROTOOPT; |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static int mctp_getsockopt(struct socket *sock, int level, int optname, |
| 273 | char __user *optval, int __user *optlen) |
| 274 | { |
Jeremy Kerr | 99ce45d | 2021-10-26 09:57:28 +0800 | [diff] [blame] | 275 | struct mctp_sock *msk = container_of(sock->sk, struct mctp_sock, sk); |
| 276 | int len, val; |
| 277 | |
| 278 | if (level != SOL_MCTP) |
| 279 | return -EINVAL; |
| 280 | |
| 281 | if (get_user(len, optlen)) |
| 282 | return -EFAULT; |
| 283 | |
| 284 | if (optname == MCTP_OPT_ADDR_EXT) { |
| 285 | if (len != sizeof(int)) |
| 286 | return -EINVAL; |
| 287 | val = !!msk->addr_ext; |
| 288 | if (copy_to_user(optval, &val, len)) |
| 289 | return -EFAULT; |
| 290 | return 0; |
| 291 | } |
| 292 | |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 293 | return -EINVAL; |
| 294 | } |
| 295 | |
| 296 | static const struct proto_ops mctp_dgram_ops = { |
| 297 | .family = PF_MCTP, |
| 298 | .release = mctp_release, |
| 299 | .bind = mctp_bind, |
| 300 | .connect = sock_no_connect, |
| 301 | .socketpair = sock_no_socketpair, |
| 302 | .accept = sock_no_accept, |
| 303 | .getname = sock_no_getname, |
| 304 | .poll = datagram_poll, |
| 305 | .ioctl = sock_no_ioctl, |
| 306 | .gettstamp = sock_gettstamp, |
| 307 | .listen = sock_no_listen, |
| 308 | .shutdown = sock_no_shutdown, |
| 309 | .setsockopt = mctp_setsockopt, |
| 310 | .getsockopt = mctp_getsockopt, |
| 311 | .sendmsg = mctp_sendmsg, |
| 312 | .recvmsg = mctp_recvmsg, |
| 313 | .mmap = sock_no_mmap, |
| 314 | .sendpage = sock_no_sendpage, |
| 315 | }; |
| 316 | |
Jeremy Kerr | 7b14e15 | 2021-09-29 15:26:09 +0800 | [diff] [blame] | 317 | static void mctp_sk_expire_keys(struct timer_list *timer) |
| 318 | { |
| 319 | struct mctp_sock *msk = container_of(timer, struct mctp_sock, |
| 320 | key_expiry); |
| 321 | struct net *net = sock_net(&msk->sk); |
| 322 | unsigned long next_expiry, flags; |
| 323 | struct mctp_sk_key *key; |
| 324 | struct hlist_node *tmp; |
| 325 | bool next_expiry_valid = false; |
| 326 | |
| 327 | spin_lock_irqsave(&net->mctp.keys_lock, flags); |
| 328 | |
| 329 | hlist_for_each_entry_safe(key, tmp, &msk->keys, sklist) { |
| 330 | spin_lock(&key->lock); |
| 331 | |
| 332 | if (!time_after_eq(key->expiry, jiffies)) { |
Jeremy Kerr | 4f9e1ba | 2021-09-29 15:26:10 +0800 | [diff] [blame] | 333 | trace_mctp_key_release(key, MCTP_TRACE_KEY_TIMEOUT); |
Jeremy Kerr | 7b14e15 | 2021-09-29 15:26:09 +0800 | [diff] [blame] | 334 | key->valid = false; |
| 335 | hlist_del_rcu(&key->hlist); |
| 336 | hlist_del_rcu(&key->sklist); |
| 337 | spin_unlock(&key->lock); |
| 338 | mctp_key_unref(key); |
| 339 | continue; |
| 340 | } |
| 341 | |
| 342 | if (next_expiry_valid) { |
| 343 | if (time_before(key->expiry, next_expiry)) |
| 344 | next_expiry = key->expiry; |
| 345 | } else { |
| 346 | next_expiry = key->expiry; |
| 347 | next_expiry_valid = true; |
| 348 | } |
| 349 | spin_unlock(&key->lock); |
| 350 | } |
| 351 | |
| 352 | spin_unlock_irqrestore(&net->mctp.keys_lock, flags); |
| 353 | |
| 354 | if (next_expiry_valid) |
| 355 | mod_timer(timer, next_expiry); |
| 356 | } |
| 357 | |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 358 | static int mctp_sk_init(struct sock *sk) |
| 359 | { |
| 360 | struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk); |
| 361 | |
| 362 | INIT_HLIST_HEAD(&msk->keys); |
Jeremy Kerr | 7b14e15 | 2021-09-29 15:26:09 +0800 | [diff] [blame] | 363 | timer_setup(&msk->key_expiry, mctp_sk_expire_keys, 0); |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 367 | static void mctp_sk_close(struct sock *sk, long timeout) |
| 368 | { |
Jeremy Kerr | 7b14e15 | 2021-09-29 15:26:09 +0800 | [diff] [blame] | 369 | struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk); |
| 370 | |
| 371 | del_timer_sync(&msk->key_expiry); |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 372 | sk_common_release(sk); |
| 373 | } |
| 374 | |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 375 | static int mctp_sk_hash(struct sock *sk) |
| 376 | { |
| 377 | struct net *net = sock_net(sk); |
| 378 | |
| 379 | mutex_lock(&net->mctp.bind_lock); |
| 380 | sk_add_node_rcu(sk, &net->mctp.binds); |
| 381 | mutex_unlock(&net->mctp.bind_lock); |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | static void mctp_sk_unhash(struct sock *sk) |
| 387 | { |
| 388 | struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk); |
| 389 | struct net *net = sock_net(sk); |
| 390 | struct mctp_sk_key *key; |
| 391 | struct hlist_node *tmp; |
| 392 | unsigned long flags; |
| 393 | |
| 394 | /* remove from any type-based binds */ |
| 395 | mutex_lock(&net->mctp.bind_lock); |
| 396 | sk_del_node_init_rcu(sk); |
| 397 | mutex_unlock(&net->mctp.bind_lock); |
| 398 | |
| 399 | /* remove tag allocations */ |
| 400 | spin_lock_irqsave(&net->mctp.keys_lock, flags); |
| 401 | hlist_for_each_entry_safe(key, tmp, &msk->keys, sklist) { |
Jeremy Kerr | 73c6184 | 2021-09-29 15:26:07 +0800 | [diff] [blame] | 402 | hlist_del(&key->sklist); |
| 403 | hlist_del(&key->hlist); |
Jeremy Kerr | 4a992bb | 2021-07-29 10:20:50 +0800 | [diff] [blame] | 404 | |
Jeremy Kerr | 4f9e1ba | 2021-09-29 15:26:10 +0800 | [diff] [blame] | 405 | trace_mctp_key_release(key, MCTP_TRACE_KEY_CLOSED); |
| 406 | |
Jeremy Kerr | 73c6184 | 2021-09-29 15:26:07 +0800 | [diff] [blame] | 407 | spin_lock(&key->lock); |
Yang Yingliang | 5cfe53c | 2021-11-30 11:12:43 +0800 | [diff] [blame] | 408 | kfree_skb(key->reasm_head); |
Jeremy Kerr | 4a992bb | 2021-07-29 10:20:50 +0800 | [diff] [blame] | 409 | key->reasm_head = NULL; |
| 410 | key->reasm_dead = true; |
Jeremy Kerr | 73c6184 | 2021-09-29 15:26:07 +0800 | [diff] [blame] | 411 | key->valid = false; |
| 412 | spin_unlock(&key->lock); |
Jeremy Kerr | 4a992bb | 2021-07-29 10:20:50 +0800 | [diff] [blame] | 413 | |
Jeremy Kerr | 73c6184 | 2021-09-29 15:26:07 +0800 | [diff] [blame] | 414 | /* key is no longer on the lookup lists, unref */ |
| 415 | mctp_key_unref(key); |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 416 | } |
| 417 | spin_unlock_irqrestore(&net->mctp.keys_lock, flags); |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 418 | } |
| 419 | |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 420 | static struct proto mctp_proto = { |
| 421 | .name = "MCTP", |
| 422 | .owner = THIS_MODULE, |
| 423 | .obj_size = sizeof(struct mctp_sock), |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 424 | .init = mctp_sk_init, |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 425 | .close = mctp_sk_close, |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 426 | .hash = mctp_sk_hash, |
| 427 | .unhash = mctp_sk_unhash, |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 428 | }; |
| 429 | |
| 430 | static int mctp_pf_create(struct net *net, struct socket *sock, |
| 431 | int protocol, int kern) |
| 432 | { |
| 433 | const struct proto_ops *ops; |
| 434 | struct proto *proto; |
| 435 | struct sock *sk; |
| 436 | int rc; |
| 437 | |
| 438 | if (protocol) |
| 439 | return -EPROTONOSUPPORT; |
| 440 | |
| 441 | /* only datagram sockets are supported */ |
| 442 | if (sock->type != SOCK_DGRAM) |
| 443 | return -ESOCKTNOSUPPORT; |
| 444 | |
| 445 | proto = &mctp_proto; |
| 446 | ops = &mctp_dgram_ops; |
| 447 | |
| 448 | sock->state = SS_UNCONNECTED; |
| 449 | sock->ops = ops; |
| 450 | |
| 451 | sk = sk_alloc(net, PF_MCTP, GFP_KERNEL, proto, kern); |
| 452 | if (!sk) |
| 453 | return -ENOMEM; |
| 454 | |
| 455 | sock_init_data(sock, sk); |
| 456 | |
| 457 | rc = 0; |
| 458 | if (sk->sk_prot->init) |
| 459 | rc = sk->sk_prot->init(sk); |
| 460 | |
| 461 | if (rc) |
| 462 | goto err_sk_put; |
| 463 | |
| 464 | return 0; |
| 465 | |
| 466 | err_sk_put: |
| 467 | sock_orphan(sk); |
| 468 | sock_put(sk); |
| 469 | return rc; |
| 470 | } |
| 471 | |
| 472 | static struct net_proto_family mctp_pf = { |
| 473 | .family = PF_MCTP, |
| 474 | .create = mctp_pf_create, |
| 475 | .owner = THIS_MODULE, |
| 476 | }; |
| 477 | |
| 478 | static __init int mctp_init(void) |
| 479 | { |
| 480 | int rc; |
| 481 | |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 482 | /* ensure our uapi tag definitions match the header format */ |
| 483 | BUILD_BUG_ON(MCTP_TAG_OWNER != MCTP_HDR_FLAG_TO); |
| 484 | BUILD_BUG_ON(MCTP_TAG_MASK != MCTP_HDR_TAG_MASK); |
| 485 | |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 486 | pr_info("mctp: management component transport protocol core\n"); |
| 487 | |
| 488 | rc = sock_register(&mctp_pf); |
| 489 | if (rc) |
| 490 | return rc; |
| 491 | |
| 492 | rc = proto_register(&mctp_proto, 0); |
| 493 | if (rc) |
| 494 | goto err_unreg_sock; |
| 495 | |
Jeremy Kerr | 889b7da | 2021-07-29 10:20:45 +0800 | [diff] [blame] | 496 | rc = mctp_routes_init(); |
| 497 | if (rc) |
| 498 | goto err_unreg_proto; |
| 499 | |
Matt Johnston | 4d8b931 | 2021-07-29 10:20:47 +0800 | [diff] [blame] | 500 | rc = mctp_neigh_init(); |
| 501 | if (rc) |
| 502 | goto err_unreg_proto; |
| 503 | |
Jeremy Kerr | 583be98 | 2021-07-29 10:20:44 +0800 | [diff] [blame] | 504 | mctp_device_init(); |
| 505 | |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 506 | return 0; |
| 507 | |
Jeremy Kerr | 889b7da | 2021-07-29 10:20:45 +0800 | [diff] [blame] | 508 | err_unreg_proto: |
| 509 | proto_unregister(&mctp_proto); |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 510 | err_unreg_sock: |
| 511 | sock_unregister(PF_MCTP); |
| 512 | |
| 513 | return rc; |
| 514 | } |
| 515 | |
| 516 | static __exit void mctp_exit(void) |
| 517 | { |
Jeremy Kerr | 583be98 | 2021-07-29 10:20:44 +0800 | [diff] [blame] | 518 | mctp_device_exit(); |
Matt Johnston | 4d8b931 | 2021-07-29 10:20:47 +0800 | [diff] [blame] | 519 | mctp_neigh_exit(); |
Jeremy Kerr | 889b7da | 2021-07-29 10:20:45 +0800 | [diff] [blame] | 520 | mctp_routes_exit(); |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 521 | proto_unregister(&mctp_proto); |
| 522 | sock_unregister(PF_MCTP); |
| 523 | } |
| 524 | |
Jeremy Kerr | 97f09ab | 2021-09-29 15:26:11 +0800 | [diff] [blame] | 525 | subsys_initcall(mctp_init); |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 526 | module_exit(mctp_exit); |
Jeremy Kerr | bc49d81 | 2021-07-29 10:20:39 +0800 | [diff] [blame] | 527 | |
| 528 | MODULE_DESCRIPTION("MCTP core"); |
| 529 | MODULE_LICENSE("GPL v2"); |
| 530 | MODULE_AUTHOR("Jeremy Kerr <jk@codeconstruct.com.au>"); |
Jeremy Kerr | 8f601a1 | 2021-07-29 10:20:40 +0800 | [diff] [blame] | 531 | |
| 532 | MODULE_ALIAS_NETPROTO(PF_MCTP); |