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