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