Thomas Gleixner | 2b27bdc | 2019-05-29 16:57:50 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 2 | /* |
| 3 | * File: socket.c |
| 4 | * |
| 5 | * Phonet sockets |
| 6 | * |
| 7 | * Copyright (C) 2008 Nokia Corporation. |
| 8 | * |
Rémi Denis-Courmont | 31fdc55 | 2012-06-13 22:29:03 +0000 | [diff] [blame] | 9 | * Authors: Sakari Ailus <sakari.ailus@nokia.com> |
| 10 | * Rémi Denis-Courmont |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/gfp.h> |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 14 | #include <linux/kernel.h> |
| 15 | #include <linux/net.h> |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 16 | #include <linux/poll.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 17 | #include <linux/sched/signal.h> |
| 18 | |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 19 | #include <net/sock.h> |
| 20 | #include <net/tcp_states.h> |
| 21 | |
| 22 | #include <linux/phonet.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 23 | #include <linux/export.h> |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 24 | #include <net/phonet/phonet.h> |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 25 | #include <net/phonet/pep.h> |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 26 | #include <net/phonet/pn_dev.h> |
| 27 | |
| 28 | static int pn_socket_release(struct socket *sock) |
| 29 | { |
| 30 | struct sock *sk = sock->sk; |
| 31 | |
| 32 | if (sk) { |
| 33 | sock->sk = NULL; |
| 34 | sk->sk_prot->close(sk, 0); |
| 35 | } |
| 36 | return 0; |
| 37 | } |
| 38 | |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 39 | #define PN_HASHSIZE 16 |
| 40 | #define PN_HASHMASK (PN_HASHSIZE-1) |
| 41 | |
| 42 | |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 43 | static struct { |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 44 | struct hlist_head hlist[PN_HASHSIZE]; |
Rémi Denis-Courmont | 44f4d5a | 2011-04-12 23:27:36 +0000 | [diff] [blame] | 45 | struct mutex lock; |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 46 | } pnsocks; |
| 47 | |
| 48 | void __init pn_sock_init(void) |
| 49 | { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 50 | unsigned int i; |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 51 | |
| 52 | for (i = 0; i < PN_HASHSIZE; i++) |
| 53 | INIT_HLIST_HEAD(pnsocks.hlist + i); |
Rémi Denis-Courmont | 44f4d5a | 2011-04-12 23:27:36 +0000 | [diff] [blame] | 54 | mutex_init(&pnsocks.lock); |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static struct hlist_head *pn_hash_list(u16 obj) |
| 58 | { |
| 59 | return pnsocks.hlist + (obj & PN_HASHMASK); |
| 60 | } |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * Find address based on socket address, match only certain fields. |
| 64 | * Also grab sock if it was found. Remember to sock_put it later. |
| 65 | */ |
Rémi Denis-Courmont | 5240488 | 2008-12-03 15:42:56 -0800 | [diff] [blame] | 66 | struct sock *pn_find_sock_by_sa(struct net *net, const struct sockaddr_pn *spn) |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 67 | { |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 68 | struct sock *sknode; |
| 69 | struct sock *rval = NULL; |
| 70 | u16 obj = pn_sockaddr_get_object(spn); |
| 71 | u8 res = spn->spn_resource; |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 72 | struct hlist_head *hlist = pn_hash_list(obj); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 73 | |
Rémi Denis-Courmont | 44f4d5a | 2011-04-12 23:27:36 +0000 | [diff] [blame] | 74 | rcu_read_lock(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 75 | sk_for_each_rcu(sknode, hlist) { |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 76 | struct pn_sock *pn = pn_sk(sknode); |
| 77 | BUG_ON(!pn->sobject); /* unbound socket */ |
| 78 | |
Rémi Denis-Courmont | 5240488 | 2008-12-03 15:42:56 -0800 | [diff] [blame] | 79 | if (!net_eq(sock_net(sknode), net)) |
| 80 | continue; |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 81 | if (pn_port(obj)) { |
| 82 | /* Look up socket by port */ |
| 83 | if (pn_port(pn->sobject) != pn_port(obj)) |
| 84 | continue; |
| 85 | } else { |
| 86 | /* If port is zero, look up by resource */ |
| 87 | if (pn->resource != res) |
| 88 | continue; |
| 89 | } |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 90 | if (pn_addr(pn->sobject) && |
| 91 | pn_addr(pn->sobject) != pn_addr(obj)) |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 92 | continue; |
| 93 | |
| 94 | rval = sknode; |
| 95 | sock_hold(sknode); |
| 96 | break; |
| 97 | } |
Rémi Denis-Courmont | 44f4d5a | 2011-04-12 23:27:36 +0000 | [diff] [blame] | 98 | rcu_read_unlock(); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 99 | |
| 100 | return rval; |
Rémi Denis-Courmont | f14001f | 2009-10-14 00:48:27 +0000 | [diff] [blame] | 101 | } |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 102 | |
Rémi Denis-Courmont | f14001f | 2009-10-14 00:48:27 +0000 | [diff] [blame] | 103 | /* Deliver a broadcast packet (only in bottom-half) */ |
| 104 | void pn_deliver_sock_broadcast(struct net *net, struct sk_buff *skb) |
| 105 | { |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 106 | struct hlist_head *hlist = pnsocks.hlist; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 107 | unsigned int h; |
Rémi Denis-Courmont | f14001f | 2009-10-14 00:48:27 +0000 | [diff] [blame] | 108 | |
Rémi Denis-Courmont | 44f4d5a | 2011-04-12 23:27:36 +0000 | [diff] [blame] | 109 | rcu_read_lock(); |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 110 | for (h = 0; h < PN_HASHSIZE; h++) { |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 111 | struct sock *sknode; |
Rémi Denis-Courmont | f14001f | 2009-10-14 00:48:27 +0000 | [diff] [blame] | 112 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 113 | sk_for_each(sknode, hlist) { |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 114 | struct sk_buff *clone; |
Rémi Denis-Courmont | f14001f | 2009-10-14 00:48:27 +0000 | [diff] [blame] | 115 | |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 116 | if (!net_eq(sock_net(sknode), net)) |
| 117 | continue; |
| 118 | if (!sock_flag(sknode, SOCK_BROADCAST)) |
| 119 | continue; |
| 120 | |
| 121 | clone = skb_clone(skb, GFP_ATOMIC); |
| 122 | if (clone) { |
| 123 | sock_hold(sknode); |
| 124 | sk_receive_skb(sknode, clone, 0); |
| 125 | } |
Rémi Denis-Courmont | 21912d1 | 2009-10-15 12:29:14 -0700 | [diff] [blame] | 126 | } |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 127 | hlist++; |
Rémi Denis-Courmont | f14001f | 2009-10-14 00:48:27 +0000 | [diff] [blame] | 128 | } |
Rémi Denis-Courmont | 44f4d5a | 2011-04-12 23:27:36 +0000 | [diff] [blame] | 129 | rcu_read_unlock(); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Craig Gallek | 086c653 | 2016-02-10 11:50:35 -0500 | [diff] [blame] | 132 | int pn_sock_hash(struct sock *sk) |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 133 | { |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 134 | struct hlist_head *hlist = pn_hash_list(pn_sk(sk)->sobject); |
| 135 | |
Rémi Denis-Courmont | 44f4d5a | 2011-04-12 23:27:36 +0000 | [diff] [blame] | 136 | mutex_lock(&pnsocks.lock); |
| 137 | sk_add_node_rcu(sk, hlist); |
| 138 | mutex_unlock(&pnsocks.lock); |
Craig Gallek | 086c653 | 2016-02-10 11:50:35 -0500 | [diff] [blame] | 139 | |
| 140 | return 0; |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 141 | } |
| 142 | EXPORT_SYMBOL(pn_sock_hash); |
| 143 | |
| 144 | void pn_sock_unhash(struct sock *sk) |
| 145 | { |
Rémi Denis-Courmont | 44f4d5a | 2011-04-12 23:27:36 +0000 | [diff] [blame] | 146 | mutex_lock(&pnsocks.lock); |
| 147 | sk_del_node_init_rcu(sk); |
| 148 | mutex_unlock(&pnsocks.lock); |
Rémi Denis-Courmont | 7417fa8 | 2010-09-15 12:30:12 +0000 | [diff] [blame] | 149 | pn_sock_unbind_all_res(sk); |
Rémi Denis-Courmont | 44f4d5a | 2011-04-12 23:27:36 +0000 | [diff] [blame] | 150 | synchronize_rcu(); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 151 | } |
| 152 | EXPORT_SYMBOL(pn_sock_unhash); |
| 153 | |
Rémi Denis-Courmont | 582b0b6 | 2009-09-23 03:17:10 +0000 | [diff] [blame] | 154 | static DEFINE_MUTEX(port_mutex); |
| 155 | |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 156 | static int pn_socket_bind(struct socket *sock, struct sockaddr *addr, int len) |
| 157 | { |
| 158 | struct sock *sk = sock->sk; |
| 159 | struct pn_sock *pn = pn_sk(sk); |
| 160 | struct sockaddr_pn *spn = (struct sockaddr_pn *)addr; |
| 161 | int err; |
| 162 | u16 handle; |
| 163 | u8 saddr; |
| 164 | |
| 165 | if (sk->sk_prot->bind) |
| 166 | return sk->sk_prot->bind(sk, addr, len); |
| 167 | |
| 168 | if (len < sizeof(struct sockaddr_pn)) |
| 169 | return -EINVAL; |
| 170 | if (spn->spn_family != AF_PHONET) |
| 171 | return -EAFNOSUPPORT; |
| 172 | |
| 173 | handle = pn_sockaddr_get_object((struct sockaddr_pn *)addr); |
| 174 | saddr = pn_addr(handle); |
Rémi Denis-Courmont | 5240488 | 2008-12-03 15:42:56 -0800 | [diff] [blame] | 175 | if (saddr && phonet_address_lookup(sock_net(sk), saddr)) |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 176 | return -EADDRNOTAVAIL; |
| 177 | |
| 178 | lock_sock(sk); |
| 179 | if (sk->sk_state != TCP_CLOSE || pn_port(pn->sobject)) { |
| 180 | err = -EINVAL; /* attempt to rebind */ |
| 181 | goto out; |
| 182 | } |
Rémi Denis-Courmont | 582b0b6 | 2009-09-23 03:17:10 +0000 | [diff] [blame] | 183 | WARN_ON(sk_hashed(sk)); |
| 184 | mutex_lock(&port_mutex); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 185 | err = sk->sk_prot->get_port(sk, pn_port(handle)); |
| 186 | if (err) |
Rémi Denis-Courmont | 582b0b6 | 2009-09-23 03:17:10 +0000 | [diff] [blame] | 187 | goto out_port; |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 188 | |
| 189 | /* get_port() sets the port, bind() sets the address if applicable */ |
| 190 | pn->sobject = pn_object(saddr, pn_port(pn->sobject)); |
| 191 | pn->resource = spn->spn_resource; |
| 192 | |
| 193 | /* Enable RX on the socket */ |
Craig Gallek | 086c653 | 2016-02-10 11:50:35 -0500 | [diff] [blame] | 194 | err = sk->sk_prot->hash(sk); |
Rémi Denis-Courmont | 582b0b6 | 2009-09-23 03:17:10 +0000 | [diff] [blame] | 195 | out_port: |
| 196 | mutex_unlock(&port_mutex); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 197 | out: |
| 198 | release_sock(sk); |
| 199 | return err; |
| 200 | } |
| 201 | |
| 202 | static int pn_socket_autobind(struct socket *sock) |
| 203 | { |
| 204 | struct sockaddr_pn sa; |
| 205 | int err; |
| 206 | |
| 207 | memset(&sa, 0, sizeof(sa)); |
| 208 | sa.spn_family = AF_PHONET; |
| 209 | err = pn_socket_bind(sock, (struct sockaddr *)&sa, |
| 210 | sizeof(struct sockaddr_pn)); |
| 211 | if (err != -EINVAL) |
| 212 | return err; |
| 213 | BUG_ON(!pn_port(pn_sk(sock->sk)->sobject)); |
| 214 | return 0; /* socket was already bound */ |
| 215 | } |
| 216 | |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 217 | static int pn_socket_connect(struct socket *sock, struct sockaddr *addr, |
| 218 | int len, int flags) |
| 219 | { |
| 220 | struct sock *sk = sock->sk; |
Rémi Denis-Courmont | 297edb6 | 2011-03-08 22:44:12 +0000 | [diff] [blame] | 221 | struct pn_sock *pn = pn_sk(sk); |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 222 | struct sockaddr_pn *spn = (struct sockaddr_pn *)addr; |
Rémi Denis-Courmont | 297edb6 | 2011-03-08 22:44:12 +0000 | [diff] [blame] | 223 | struct task_struct *tsk = current; |
| 224 | long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 225 | int err; |
| 226 | |
Rémi Denis-Courmont | 297edb6 | 2011-03-08 22:44:12 +0000 | [diff] [blame] | 227 | if (pn_socket_autobind(sock)) |
| 228 | return -ENOBUFS; |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 229 | if (len < sizeof(struct sockaddr_pn)) |
| 230 | return -EINVAL; |
| 231 | if (spn->spn_family != AF_PHONET) |
| 232 | return -EAFNOSUPPORT; |
| 233 | |
| 234 | lock_sock(sk); |
| 235 | |
| 236 | switch (sock->state) { |
| 237 | case SS_UNCONNECTED: |
Rémi Denis-Courmont | 297edb6 | 2011-03-08 22:44:12 +0000 | [diff] [blame] | 238 | if (sk->sk_state != TCP_CLOSE) { |
| 239 | err = -EISCONN; |
| 240 | goto out; |
| 241 | } |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 242 | break; |
| 243 | case SS_CONNECTING: |
Rémi Denis-Courmont | 297edb6 | 2011-03-08 22:44:12 +0000 | [diff] [blame] | 244 | err = -EALREADY; |
| 245 | goto out; |
| 246 | default: |
| 247 | err = -EISCONN; |
| 248 | goto out; |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 249 | } |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 250 | |
Rémi Denis-Courmont | 297edb6 | 2011-03-08 22:44:12 +0000 | [diff] [blame] | 251 | pn->dobject = pn_sockaddr_get_object(spn); |
| 252 | pn->resource = pn_sockaddr_get_resource(spn); |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 253 | sock->state = SS_CONNECTING; |
Rémi Denis-Courmont | 297edb6 | 2011-03-08 22:44:12 +0000 | [diff] [blame] | 254 | |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 255 | err = sk->sk_prot->connect(sk, addr, len); |
Rémi Denis-Courmont | 297edb6 | 2011-03-08 22:44:12 +0000 | [diff] [blame] | 256 | if (err) { |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 257 | sock->state = SS_UNCONNECTED; |
Rémi Denis-Courmont | 297edb6 | 2011-03-08 22:44:12 +0000 | [diff] [blame] | 258 | pn->dobject = 0; |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 259 | goto out; |
| 260 | } |
| 261 | |
Rémi Denis-Courmont | 297edb6 | 2011-03-08 22:44:12 +0000 | [diff] [blame] | 262 | while (sk->sk_state == TCP_SYN_SENT) { |
| 263 | DEFINE_WAIT(wait); |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 264 | |
Rémi Denis-Courmont | 297edb6 | 2011-03-08 22:44:12 +0000 | [diff] [blame] | 265 | if (!timeo) { |
| 266 | err = -EINPROGRESS; |
| 267 | goto out; |
| 268 | } |
| 269 | if (signal_pending(tsk)) { |
| 270 | err = sock_intr_errno(timeo); |
| 271 | goto out; |
| 272 | } |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 273 | |
Rémi Denis-Courmont | 297edb6 | 2011-03-08 22:44:12 +0000 | [diff] [blame] | 274 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, |
| 275 | TASK_INTERRUPTIBLE); |
| 276 | release_sock(sk); |
| 277 | timeo = schedule_timeout(timeo); |
| 278 | lock_sock(sk); |
| 279 | finish_wait(sk_sleep(sk), &wait); |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 280 | } |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 281 | |
Rémi Denis-Courmont | 297edb6 | 2011-03-08 22:44:12 +0000 | [diff] [blame] | 282 | if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED)) |
| 283 | err = 0; |
| 284 | else if (sk->sk_state == TCP_CLOSE_WAIT) |
| 285 | err = -ECONNRESET; |
| 286 | else |
| 287 | err = -ECONNREFUSED; |
| 288 | sock->state = err ? SS_UNCONNECTED : SS_CONNECTED; |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 289 | out: |
| 290 | release_sock(sk); |
| 291 | return err; |
| 292 | } |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 293 | |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 294 | static int pn_socket_accept(struct socket *sock, struct socket *newsock, |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 295 | int flags, bool kern) |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 296 | { |
| 297 | struct sock *sk = sock->sk; |
| 298 | struct sock *newsk; |
| 299 | int err; |
| 300 | |
Rémi Denis-Courmont | f7ae8d5 | 2011-03-08 22:44:10 +0000 | [diff] [blame] | 301 | if (unlikely(sk->sk_state != TCP_LISTEN)) |
| 302 | return -EINVAL; |
| 303 | |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 304 | newsk = sk->sk_prot->accept(sk, flags, &err, kern); |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 305 | if (!newsk) |
| 306 | return err; |
| 307 | |
| 308 | lock_sock(newsk); |
| 309 | sock_graft(newsk, newsock); |
| 310 | newsock->state = SS_CONNECTED; |
| 311 | release_sock(newsk); |
| 312 | return 0; |
| 313 | } |
| 314 | |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 315 | static int pn_socket_getname(struct socket *sock, struct sockaddr *addr, |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 316 | int peer) |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 317 | { |
| 318 | struct sock *sk = sock->sk; |
| 319 | struct pn_sock *pn = pn_sk(sk); |
| 320 | |
| 321 | memset(addr, 0, sizeof(struct sockaddr_pn)); |
| 322 | addr->sa_family = AF_PHONET; |
| 323 | if (!peer) /* Race with bind() here is userland's problem. */ |
| 324 | pn_sockaddr_set_object((struct sockaddr_pn *)addr, |
| 325 | pn->sobject); |
| 326 | |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 327 | return sizeof(struct sockaddr_pn); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 330 | static __poll_t pn_socket_poll(struct file *file, struct socket *sock, |
| 331 | poll_table *wait) |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 332 | { |
| 333 | struct sock *sk = sock->sk; |
| 334 | struct pep_sock *pn = pep_sk(sk); |
Al Viro | ade994f | 2017-07-03 00:01:49 -0400 | [diff] [blame] | 335 | __poll_t mask = 0; |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 336 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 337 | poll_wait(file, sk_sleep(sk), wait); |
| 338 | |
Rémi Denis-Courmont | f7ae8d5 | 2011-03-08 22:44:10 +0000 | [diff] [blame] | 339 | if (sk->sk_state == TCP_CLOSE) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 340 | return EPOLLERR; |
Eric Dumazet | 3ef7cf5 | 2019-10-23 22:44:50 -0700 | [diff] [blame] | 341 | if (!skb_queue_empty_lockless(&sk->sk_receive_queue)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 342 | mask |= EPOLLIN | EPOLLRDNORM; |
Eric Dumazet | 3ef7cf5 | 2019-10-23 22:44:50 -0700 | [diff] [blame] | 343 | if (!skb_queue_empty_lockless(&pn->ctrlreq_queue)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 344 | mask |= EPOLLPRI; |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 345 | if (!mask && sk->sk_state == TCP_CLOSE_WAIT) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 346 | return EPOLLHUP; |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 347 | |
Rémi Denis-Courmont | 01b3860 | 2010-08-30 12:57:05 +0000 | [diff] [blame] | 348 | if (sk->sk_state == TCP_ESTABLISHED && |
Reshetova, Elena | 14afee4 | 2017-06-30 13:08:00 +0300 | [diff] [blame] | 349 | refcount_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf && |
Rémi Denis-Courmont | 01b3860 | 2010-08-30 12:57:05 +0000 | [diff] [blame] | 350 | atomic_read(&pn->tx_credits)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 351 | mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND; |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 352 | |
| 353 | return mask; |
| 354 | } |
| 355 | |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 356 | static int pn_socket_ioctl(struct socket *sock, unsigned int cmd, |
| 357 | unsigned long arg) |
| 358 | { |
| 359 | struct sock *sk = sock->sk; |
| 360 | struct pn_sock *pn = pn_sk(sk); |
| 361 | |
| 362 | if (cmd == SIOCPNGETOBJECT) { |
| 363 | struct net_device *dev; |
| 364 | u16 handle; |
| 365 | u8 saddr; |
| 366 | |
| 367 | if (get_user(handle, (__u16 __user *)arg)) |
| 368 | return -EFAULT; |
| 369 | |
| 370 | lock_sock(sk); |
| 371 | if (sk->sk_bound_dev_if) |
| 372 | dev = dev_get_by_index(sock_net(sk), |
| 373 | sk->sk_bound_dev_if); |
| 374 | else |
| 375 | dev = phonet_device_get(sock_net(sk)); |
| 376 | if (dev && (dev->flags & IFF_UP)) |
| 377 | saddr = phonet_address_get(dev, pn_addr(handle)); |
| 378 | else |
| 379 | saddr = PN_NO_ADDR; |
| 380 | release_sock(sk); |
| 381 | |
Yajun Deng | 1160dfa | 2021-08-05 19:55:27 +0800 | [diff] [blame] | 382 | dev_put(dev); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 383 | if (saddr == PN_NO_ADDR) |
| 384 | return -EHOSTUNREACH; |
| 385 | |
| 386 | handle = pn_object(saddr, pn_port(pn->sobject)); |
| 387 | return put_user(handle, (__u16 __user *)arg); |
| 388 | } |
| 389 | |
| 390 | return sk->sk_prot->ioctl(sk, cmd, arg); |
| 391 | } |
| 392 | |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 393 | static int pn_socket_listen(struct socket *sock, int backlog) |
| 394 | { |
| 395 | struct sock *sk = sock->sk; |
| 396 | int err = 0; |
| 397 | |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 398 | if (pn_socket_autobind(sock)) |
| 399 | return -ENOBUFS; |
| 400 | |
| 401 | lock_sock(sk); |
Rémi Denis-Courmont | 9624154 | 2011-02-24 23:14:56 +0000 | [diff] [blame] | 402 | if (sock->state != SS_UNCONNECTED) { |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 403 | err = -EINVAL; |
| 404 | goto out; |
| 405 | } |
| 406 | |
Rémi Denis-Courmont | 9624154 | 2011-02-24 23:14:56 +0000 | [diff] [blame] | 407 | if (sk->sk_state != TCP_LISTEN) { |
| 408 | sk->sk_state = TCP_LISTEN; |
| 409 | sk->sk_ack_backlog = 0; |
| 410 | } |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 411 | sk->sk_max_ack_backlog = backlog; |
| 412 | out: |
| 413 | release_sock(sk); |
| 414 | return err; |
| 415 | } |
| 416 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 417 | static int pn_socket_sendmsg(struct socket *sock, struct msghdr *m, |
| 418 | size_t total_len) |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 419 | { |
| 420 | struct sock *sk = sock->sk; |
| 421 | |
| 422 | if (pn_socket_autobind(sock)) |
| 423 | return -EAGAIN; |
| 424 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 425 | return sk->sk_prot->sendmsg(sk, m, total_len); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | const struct proto_ops phonet_dgram_ops = { |
| 429 | .family = AF_PHONET, |
| 430 | .owner = THIS_MODULE, |
| 431 | .release = pn_socket_release, |
| 432 | .bind = pn_socket_bind, |
| 433 | .connect = sock_no_connect, |
| 434 | .socketpair = sock_no_socketpair, |
| 435 | .accept = sock_no_accept, |
| 436 | .getname = pn_socket_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 437 | .poll = datagram_poll, |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 438 | .ioctl = pn_socket_ioctl, |
| 439 | .listen = sock_no_listen, |
| 440 | .shutdown = sock_no_shutdown, |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 441 | .sendmsg = pn_socket_sendmsg, |
| 442 | .recvmsg = sock_common_recvmsg, |
| 443 | .mmap = sock_no_mmap, |
| 444 | .sendpage = sock_no_sendpage, |
| 445 | }; |
| 446 | |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 447 | const struct proto_ops phonet_stream_ops = { |
| 448 | .family = AF_PHONET, |
| 449 | .owner = THIS_MODULE, |
| 450 | .release = pn_socket_release, |
| 451 | .bind = pn_socket_bind, |
Kumar Sanghvi | b3d6255 | 2010-10-12 20:14:43 +0000 | [diff] [blame] | 452 | .connect = pn_socket_connect, |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 453 | .socketpair = sock_no_socketpair, |
| 454 | .accept = pn_socket_accept, |
| 455 | .getname = pn_socket_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 456 | .poll = pn_socket_poll, |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 457 | .ioctl = pn_socket_ioctl, |
| 458 | .listen = pn_socket_listen, |
| 459 | .shutdown = sock_no_shutdown, |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 460 | .setsockopt = sock_common_setsockopt, |
| 461 | .getsockopt = sock_common_getsockopt, |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 462 | .sendmsg = pn_socket_sendmsg, |
| 463 | .recvmsg = sock_common_recvmsg, |
| 464 | .mmap = sock_no_mmap, |
| 465 | .sendpage = sock_no_sendpage, |
| 466 | }; |
| 467 | EXPORT_SYMBOL(phonet_stream_ops); |
| 468 | |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 469 | /* allocate port for a socket */ |
| 470 | int pn_sock_get_port(struct sock *sk, unsigned short sport) |
| 471 | { |
| 472 | static int port_cur; |
Rémi Denis-Courmont | 5240488 | 2008-12-03 15:42:56 -0800 | [diff] [blame] | 473 | struct net *net = sock_net(sk); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 474 | struct pn_sock *pn = pn_sk(sk); |
| 475 | struct sockaddr_pn try_sa; |
| 476 | struct sock *tmpsk; |
| 477 | |
| 478 | memset(&try_sa, 0, sizeof(struct sockaddr_pn)); |
| 479 | try_sa.spn_family = AF_PHONET; |
Rémi Denis-Courmont | 582b0b6 | 2009-09-23 03:17:10 +0000 | [diff] [blame] | 480 | WARN_ON(!mutex_is_locked(&port_mutex)); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 481 | if (!sport) { |
| 482 | /* search free port */ |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 483 | int port, pmin, pmax; |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 484 | |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 485 | phonet_get_local_port_range(&pmin, &pmax); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 486 | for (port = pmin; port <= pmax; port++) { |
| 487 | port_cur++; |
| 488 | if (port_cur < pmin || port_cur > pmax) |
| 489 | port_cur = pmin; |
| 490 | |
| 491 | pn_sockaddr_set_port(&try_sa, port_cur); |
Rémi Denis-Courmont | 5240488 | 2008-12-03 15:42:56 -0800 | [diff] [blame] | 492 | tmpsk = pn_find_sock_by_sa(net, &try_sa); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 493 | if (tmpsk == NULL) { |
| 494 | sport = port_cur; |
| 495 | goto found; |
| 496 | } else |
| 497 | sock_put(tmpsk); |
| 498 | } |
| 499 | } else { |
| 500 | /* try to find specific port */ |
| 501 | pn_sockaddr_set_port(&try_sa, sport); |
Rémi Denis-Courmont | 5240488 | 2008-12-03 15:42:56 -0800 | [diff] [blame] | 502 | tmpsk = pn_find_sock_by_sa(net, &try_sa); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 503 | if (tmpsk == NULL) |
| 504 | /* No sock there! We can use that port... */ |
| 505 | goto found; |
| 506 | else |
| 507 | sock_put(tmpsk); |
| 508 | } |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 509 | /* the port must be in use already */ |
| 510 | return -EADDRINUSE; |
| 511 | |
| 512 | found: |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 513 | pn->sobject = pn_object(pn_addr(pn->sobject), sport); |
| 514 | return 0; |
| 515 | } |
| 516 | EXPORT_SYMBOL(pn_sock_get_port); |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 517 | |
Randy Dunlap | ae6e2ae | 2009-08-17 10:35:49 +0000 | [diff] [blame] | 518 | #ifdef CONFIG_PROC_FS |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 519 | static struct sock *pn_sock_get_idx(struct seq_file *seq, loff_t pos) |
| 520 | { |
| 521 | struct net *net = seq_file_net(seq); |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 522 | struct hlist_head *hlist = pnsocks.hlist; |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 523 | struct sock *sknode; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 524 | unsigned int h; |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 525 | |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 526 | for (h = 0; h < PN_HASHSIZE; h++) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 527 | sk_for_each_rcu(sknode, hlist) { |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 528 | if (!net_eq(net, sock_net(sknode))) |
| 529 | continue; |
| 530 | if (!pos) |
| 531 | return sknode; |
| 532 | pos--; |
| 533 | } |
| 534 | hlist++; |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 535 | } |
| 536 | return NULL; |
| 537 | } |
| 538 | |
| 539 | static struct sock *pn_sock_get_next(struct seq_file *seq, struct sock *sk) |
| 540 | { |
| 541 | struct net *net = seq_file_net(seq); |
| 542 | |
| 543 | do |
| 544 | sk = sk_next(sk); |
| 545 | while (sk && !net_eq(net, sock_net(sk))); |
| 546 | |
| 547 | return sk; |
| 548 | } |
| 549 | |
| 550 | static void *pn_sock_seq_start(struct seq_file *seq, loff_t *pos) |
Rémi Denis-Courmont | 44f4d5a | 2011-04-12 23:27:36 +0000 | [diff] [blame] | 551 | __acquires(rcu) |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 552 | { |
Rémi Denis-Courmont | 44f4d5a | 2011-04-12 23:27:36 +0000 | [diff] [blame] | 553 | rcu_read_lock(); |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 554 | return *pos ? pn_sock_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 555 | } |
| 556 | |
| 557 | static void *pn_sock_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 558 | { |
| 559 | struct sock *sk; |
| 560 | |
| 561 | if (v == SEQ_START_TOKEN) |
| 562 | sk = pn_sock_get_idx(seq, 0); |
| 563 | else |
| 564 | sk = pn_sock_get_next(seq, v); |
| 565 | (*pos)++; |
| 566 | return sk; |
| 567 | } |
| 568 | |
| 569 | static void pn_sock_seq_stop(struct seq_file *seq, void *v) |
Rémi Denis-Courmont | 44f4d5a | 2011-04-12 23:27:36 +0000 | [diff] [blame] | 570 | __releases(rcu) |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 571 | { |
Rémi Denis-Courmont | 44f4d5a | 2011-04-12 23:27:36 +0000 | [diff] [blame] | 572 | rcu_read_unlock(); |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | static int pn_sock_seq_show(struct seq_file *seq, void *v) |
| 576 | { |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 577 | seq_setwidth(seq, 127); |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 578 | if (v == SEQ_START_TOKEN) |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 579 | seq_puts(seq, "pt loc rem rs st tx_queue rx_queue " |
| 580 | " uid inode ref pointer drops"); |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 581 | else { |
| 582 | struct sock *sk = v; |
| 583 | struct pn_sock *pn = pn_sk(sk); |
| 584 | |
| 585 | seq_printf(seq, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu " |
Patrick Talbert | ea9a037 | 2019-05-17 17:11:28 +0200 | [diff] [blame] | 586 | "%d %pK %u", |
Rémi Denis-Courmont | a805951 | 2011-02-24 23:14:57 +0000 | [diff] [blame] | 587 | sk->sk_protocol, pn->sobject, pn->dobject, |
| 588 | pn->resource, sk->sk_state, |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 589 | sk_wmem_alloc_get(sk), sk_rmem_alloc_get(sk), |
Eric W. Biederman | a7cb5a4 | 2012-05-24 01:10:10 -0600 | [diff] [blame] | 590 | from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)), |
| 591 | sock_i_ino(sk), |
Reshetova, Elena | 41c6d65 | 2017-06-30 13:08:01 +0300 | [diff] [blame] | 592 | refcount_read(&sk->sk_refcnt), sk, |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 593 | atomic_read(&sk->sk_drops)); |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 594 | } |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 595 | seq_pad(seq, '\n'); |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 596 | return 0; |
| 597 | } |
| 598 | |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 599 | const struct seq_operations pn_sock_seq_ops = { |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 600 | .start = pn_sock_seq_start, |
| 601 | .next = pn_sock_seq_next, |
| 602 | .stop = pn_sock_seq_stop, |
| 603 | .show = pn_sock_seq_show, |
| 604 | }; |
Randy Dunlap | ae6e2ae | 2009-08-17 10:35:49 +0000 | [diff] [blame] | 605 | #endif |
Rémi Denis-Courmont | 4e3d16c | 2010-09-15 12:30:11 +0000 | [diff] [blame] | 606 | |
| 607 | static struct { |
| 608 | struct sock *sk[256]; |
| 609 | } pnres; |
| 610 | |
| 611 | /* |
| 612 | * Find and hold socket based on resource. |
| 613 | */ |
| 614 | struct sock *pn_find_sock_by_res(struct net *net, u8 res) |
| 615 | { |
| 616 | struct sock *sk; |
| 617 | |
| 618 | if (!net_eq(net, &init_net)) |
| 619 | return NULL; |
| 620 | |
| 621 | rcu_read_lock(); |
| 622 | sk = rcu_dereference(pnres.sk[res]); |
| 623 | if (sk) |
| 624 | sock_hold(sk); |
| 625 | rcu_read_unlock(); |
| 626 | return sk; |
| 627 | } |
| 628 | |
| 629 | static DEFINE_MUTEX(resource_mutex); |
| 630 | |
| 631 | int pn_sock_bind_res(struct sock *sk, u8 res) |
| 632 | { |
| 633 | int ret = -EADDRINUSE; |
| 634 | |
| 635 | if (!net_eq(sock_net(sk), &init_net)) |
| 636 | return -ENOIOCTLCMD; |
| 637 | if (!capable(CAP_SYS_ADMIN)) |
| 638 | return -EPERM; |
| 639 | if (pn_socket_autobind(sk->sk_socket)) |
| 640 | return -EAGAIN; |
| 641 | |
| 642 | mutex_lock(&resource_mutex); |
| 643 | if (pnres.sk[res] == NULL) { |
| 644 | sock_hold(sk); |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 645 | rcu_assign_pointer(pnres.sk[res], sk); |
Rémi Denis-Courmont | 4e3d16c | 2010-09-15 12:30:11 +0000 | [diff] [blame] | 646 | ret = 0; |
| 647 | } |
| 648 | mutex_unlock(&resource_mutex); |
| 649 | return ret; |
| 650 | } |
| 651 | |
| 652 | int pn_sock_unbind_res(struct sock *sk, u8 res) |
| 653 | { |
| 654 | int ret = -ENOENT; |
| 655 | |
| 656 | if (!capable(CAP_SYS_ADMIN)) |
| 657 | return -EPERM; |
| 658 | |
| 659 | mutex_lock(&resource_mutex); |
| 660 | if (pnres.sk[res] == sk) { |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 661 | RCU_INIT_POINTER(pnres.sk[res], NULL); |
Rémi Denis-Courmont | 4e3d16c | 2010-09-15 12:30:11 +0000 | [diff] [blame] | 662 | ret = 0; |
| 663 | } |
| 664 | mutex_unlock(&resource_mutex); |
| 665 | |
| 666 | if (ret == 0) { |
| 667 | synchronize_rcu(); |
| 668 | sock_put(sk); |
| 669 | } |
| 670 | return ret; |
| 671 | } |
| 672 | |
| 673 | void pn_sock_unbind_all_res(struct sock *sk) |
| 674 | { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 675 | unsigned int res, match = 0; |
Rémi Denis-Courmont | 4e3d16c | 2010-09-15 12:30:11 +0000 | [diff] [blame] | 676 | |
| 677 | mutex_lock(&resource_mutex); |
| 678 | for (res = 0; res < 256; res++) { |
| 679 | if (pnres.sk[res] == sk) { |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 680 | RCU_INIT_POINTER(pnres.sk[res], NULL); |
Rémi Denis-Courmont | 4e3d16c | 2010-09-15 12:30:11 +0000 | [diff] [blame] | 681 | match++; |
| 682 | } |
| 683 | } |
| 684 | mutex_unlock(&resource_mutex); |
| 685 | |
Rémi Denis-Courmont | 4e3d16c | 2010-09-15 12:30:11 +0000 | [diff] [blame] | 686 | while (match > 0) { |
Rémi Denis-Courmont | 44f4d5a | 2011-04-12 23:27:36 +0000 | [diff] [blame] | 687 | __sock_put(sk); |
Rémi Denis-Courmont | 4e3d16c | 2010-09-15 12:30:11 +0000 | [diff] [blame] | 688 | match--; |
| 689 | } |
Rémi Denis-Courmont | 44f4d5a | 2011-04-12 23:27:36 +0000 | [diff] [blame] | 690 | /* Caller is responsible for RCU sync before final sock_put() */ |
Rémi Denis-Courmont | 4e3d16c | 2010-09-15 12:30:11 +0000 | [diff] [blame] | 691 | } |
Rémi Denis-Courmont | 507215f | 2010-09-15 12:30:14 +0000 | [diff] [blame] | 692 | |
| 693 | #ifdef CONFIG_PROC_FS |
| 694 | static struct sock **pn_res_get_idx(struct seq_file *seq, loff_t pos) |
| 695 | { |
| 696 | struct net *net = seq_file_net(seq); |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 697 | unsigned int i; |
Rémi Denis-Courmont | 507215f | 2010-09-15 12:30:14 +0000 | [diff] [blame] | 698 | |
| 699 | if (!net_eq(net, &init_net)) |
| 700 | return NULL; |
| 701 | |
| 702 | for (i = 0; i < 256; i++) { |
| 703 | if (pnres.sk[i] == NULL) |
| 704 | continue; |
| 705 | if (!pos) |
| 706 | return pnres.sk + i; |
| 707 | pos--; |
| 708 | } |
| 709 | return NULL; |
| 710 | } |
| 711 | |
| 712 | static struct sock **pn_res_get_next(struct seq_file *seq, struct sock **sk) |
| 713 | { |
| 714 | struct net *net = seq_file_net(seq); |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 715 | unsigned int i; |
Rémi Denis-Courmont | 507215f | 2010-09-15 12:30:14 +0000 | [diff] [blame] | 716 | |
| 717 | BUG_ON(!net_eq(net, &init_net)); |
| 718 | |
| 719 | for (i = (sk - pnres.sk) + 1; i < 256; i++) |
| 720 | if (pnres.sk[i]) |
| 721 | return pnres.sk + i; |
| 722 | return NULL; |
| 723 | } |
| 724 | |
| 725 | static void *pn_res_seq_start(struct seq_file *seq, loff_t *pos) |
| 726 | __acquires(resource_mutex) |
| 727 | { |
| 728 | mutex_lock(&resource_mutex); |
| 729 | return *pos ? pn_res_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 730 | } |
| 731 | |
| 732 | static void *pn_res_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 733 | { |
| 734 | struct sock **sk; |
| 735 | |
| 736 | if (v == SEQ_START_TOKEN) |
| 737 | sk = pn_res_get_idx(seq, 0); |
| 738 | else |
| 739 | sk = pn_res_get_next(seq, v); |
| 740 | (*pos)++; |
| 741 | return sk; |
| 742 | } |
| 743 | |
| 744 | static void pn_res_seq_stop(struct seq_file *seq, void *v) |
| 745 | __releases(resource_mutex) |
| 746 | { |
| 747 | mutex_unlock(&resource_mutex); |
| 748 | } |
| 749 | |
| 750 | static int pn_res_seq_show(struct seq_file *seq, void *v) |
| 751 | { |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 752 | seq_setwidth(seq, 63); |
Rémi Denis-Courmont | 507215f | 2010-09-15 12:30:14 +0000 | [diff] [blame] | 753 | if (v == SEQ_START_TOKEN) |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 754 | seq_puts(seq, "rs uid inode"); |
Rémi Denis-Courmont | 507215f | 2010-09-15 12:30:14 +0000 | [diff] [blame] | 755 | else { |
| 756 | struct sock **psk = v; |
| 757 | struct sock *sk = *psk; |
| 758 | |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 759 | seq_printf(seq, "%02X %5u %lu", |
Eric W. Biederman | a7cb5a4 | 2012-05-24 01:10:10 -0600 | [diff] [blame] | 760 | (int) (psk - pnres.sk), |
| 761 | from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)), |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 762 | sock_i_ino(sk)); |
Rémi Denis-Courmont | 507215f | 2010-09-15 12:30:14 +0000 | [diff] [blame] | 763 | } |
Tetsuo Handa | 652586d | 2013-11-14 14:31:57 -0800 | [diff] [blame] | 764 | seq_pad(seq, '\n'); |
Rémi Denis-Courmont | 507215f | 2010-09-15 12:30:14 +0000 | [diff] [blame] | 765 | return 0; |
| 766 | } |
| 767 | |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 768 | const struct seq_operations pn_res_seq_ops = { |
Rémi Denis-Courmont | 507215f | 2010-09-15 12:30:14 +0000 | [diff] [blame] | 769 | .start = pn_res_seq_start, |
| 770 | .next = pn_res_seq_next, |
| 771 | .stop = pn_res_seq_stop, |
| 772 | .show = pn_res_seq_show, |
| 773 | }; |
Rémi Denis-Courmont | 507215f | 2010-09-15 12:30:14 +0000 | [diff] [blame] | 774 | #endif |