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); |
| 161 | } |
| 162 | EXPORT_SYMBOL(pn_sock_unhash); |
| 163 | |
Rémi Denis-Courmont | 582b0b6 | 2009-09-23 03:17:10 +0000 | [diff] [blame] | 164 | static DEFINE_MUTEX(port_mutex); |
| 165 | |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 166 | static int pn_socket_bind(struct socket *sock, struct sockaddr *addr, int len) |
| 167 | { |
| 168 | struct sock *sk = sock->sk; |
| 169 | struct pn_sock *pn = pn_sk(sk); |
| 170 | struct sockaddr_pn *spn = (struct sockaddr_pn *)addr; |
| 171 | int err; |
| 172 | u16 handle; |
| 173 | u8 saddr; |
| 174 | |
| 175 | if (sk->sk_prot->bind) |
| 176 | return sk->sk_prot->bind(sk, addr, len); |
| 177 | |
| 178 | if (len < sizeof(struct sockaddr_pn)) |
| 179 | return -EINVAL; |
| 180 | if (spn->spn_family != AF_PHONET) |
| 181 | return -EAFNOSUPPORT; |
| 182 | |
| 183 | handle = pn_sockaddr_get_object((struct sockaddr_pn *)addr); |
| 184 | saddr = pn_addr(handle); |
Rémi Denis-Courmont | 5240488 | 2008-12-03 15:42:56 -0800 | [diff] [blame] | 185 | if (saddr && phonet_address_lookup(sock_net(sk), saddr)) |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 186 | return -EADDRNOTAVAIL; |
| 187 | |
| 188 | lock_sock(sk); |
| 189 | if (sk->sk_state != TCP_CLOSE || pn_port(pn->sobject)) { |
| 190 | err = -EINVAL; /* attempt to rebind */ |
| 191 | goto out; |
| 192 | } |
Rémi Denis-Courmont | 582b0b6 | 2009-09-23 03:17:10 +0000 | [diff] [blame] | 193 | WARN_ON(sk_hashed(sk)); |
| 194 | mutex_lock(&port_mutex); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 195 | err = sk->sk_prot->get_port(sk, pn_port(handle)); |
| 196 | if (err) |
Rémi Denis-Courmont | 582b0b6 | 2009-09-23 03:17:10 +0000 | [diff] [blame] | 197 | goto out_port; |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 198 | |
| 199 | /* get_port() sets the port, bind() sets the address if applicable */ |
| 200 | pn->sobject = pn_object(saddr, pn_port(pn->sobject)); |
| 201 | pn->resource = spn->spn_resource; |
| 202 | |
| 203 | /* Enable RX on the socket */ |
| 204 | sk->sk_prot->hash(sk); |
Rémi Denis-Courmont | 582b0b6 | 2009-09-23 03:17:10 +0000 | [diff] [blame] | 205 | out_port: |
| 206 | mutex_unlock(&port_mutex); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 207 | out: |
| 208 | release_sock(sk); |
| 209 | return err; |
| 210 | } |
| 211 | |
| 212 | static int pn_socket_autobind(struct socket *sock) |
| 213 | { |
| 214 | struct sockaddr_pn sa; |
| 215 | int err; |
| 216 | |
| 217 | memset(&sa, 0, sizeof(sa)); |
| 218 | sa.spn_family = AF_PHONET; |
| 219 | err = pn_socket_bind(sock, (struct sockaddr *)&sa, |
| 220 | sizeof(struct sockaddr_pn)); |
| 221 | if (err != -EINVAL) |
| 222 | return err; |
| 223 | BUG_ON(!pn_port(pn_sk(sock->sk)->sobject)); |
| 224 | return 0; /* socket was already bound */ |
| 225 | } |
| 226 | |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 227 | static int pn_socket_accept(struct socket *sock, struct socket *newsock, |
| 228 | int flags) |
| 229 | { |
| 230 | struct sock *sk = sock->sk; |
| 231 | struct sock *newsk; |
| 232 | int err; |
| 233 | |
| 234 | newsk = sk->sk_prot->accept(sk, flags, &err); |
| 235 | if (!newsk) |
| 236 | return err; |
| 237 | |
| 238 | lock_sock(newsk); |
| 239 | sock_graft(newsk, newsock); |
| 240 | newsock->state = SS_CONNECTED; |
| 241 | release_sock(newsk); |
| 242 | return 0; |
| 243 | } |
| 244 | |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 245 | static int pn_socket_getname(struct socket *sock, struct sockaddr *addr, |
| 246 | int *sockaddr_len, int peer) |
| 247 | { |
| 248 | struct sock *sk = sock->sk; |
| 249 | struct pn_sock *pn = pn_sk(sk); |
| 250 | |
| 251 | memset(addr, 0, sizeof(struct sockaddr_pn)); |
| 252 | addr->sa_family = AF_PHONET; |
| 253 | if (!peer) /* Race with bind() here is userland's problem. */ |
| 254 | pn_sockaddr_set_object((struct sockaddr_pn *)addr, |
| 255 | pn->sobject); |
| 256 | |
| 257 | *sockaddr_len = sizeof(struct sockaddr_pn); |
| 258 | return 0; |
| 259 | } |
| 260 | |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 261 | static unsigned int pn_socket_poll(struct file *file, struct socket *sock, |
| 262 | poll_table *wait) |
| 263 | { |
| 264 | struct sock *sk = sock->sk; |
| 265 | struct pep_sock *pn = pep_sk(sk); |
| 266 | unsigned int mask = 0; |
| 267 | |
| 268 | poll_wait(file, &sock->wait, wait); |
| 269 | |
| 270 | switch (sk->sk_state) { |
| 271 | case TCP_LISTEN: |
| 272 | return hlist_empty(&pn->ackq) ? 0 : POLLIN; |
| 273 | case TCP_CLOSE: |
| 274 | return POLLERR; |
| 275 | } |
| 276 | |
| 277 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
| 278 | mask |= POLLIN | POLLRDNORM; |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 279 | if (!skb_queue_empty(&pn->ctrlreq_queue)) |
| 280 | mask |= POLLPRI; |
| 281 | if (!mask && sk->sk_state == TCP_CLOSE_WAIT) |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 282 | return POLLHUP; |
| 283 | |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 284 | if (sk->sk_state == TCP_ESTABLISHED && atomic_read(&pn->tx_credits)) |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 285 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
| 286 | |
| 287 | return mask; |
| 288 | } |
| 289 | |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 290 | static int pn_socket_ioctl(struct socket *sock, unsigned int cmd, |
| 291 | unsigned long arg) |
| 292 | { |
| 293 | struct sock *sk = sock->sk; |
| 294 | struct pn_sock *pn = pn_sk(sk); |
| 295 | |
| 296 | if (cmd == SIOCPNGETOBJECT) { |
| 297 | struct net_device *dev; |
| 298 | u16 handle; |
| 299 | u8 saddr; |
| 300 | |
| 301 | if (get_user(handle, (__u16 __user *)arg)) |
| 302 | return -EFAULT; |
| 303 | |
| 304 | lock_sock(sk); |
| 305 | if (sk->sk_bound_dev_if) |
| 306 | dev = dev_get_by_index(sock_net(sk), |
| 307 | sk->sk_bound_dev_if); |
| 308 | else |
| 309 | dev = phonet_device_get(sock_net(sk)); |
| 310 | if (dev && (dev->flags & IFF_UP)) |
| 311 | saddr = phonet_address_get(dev, pn_addr(handle)); |
| 312 | else |
| 313 | saddr = PN_NO_ADDR; |
| 314 | release_sock(sk); |
| 315 | |
| 316 | if (dev) |
| 317 | dev_put(dev); |
| 318 | if (saddr == PN_NO_ADDR) |
| 319 | return -EHOSTUNREACH; |
| 320 | |
| 321 | handle = pn_object(saddr, pn_port(pn->sobject)); |
| 322 | return put_user(handle, (__u16 __user *)arg); |
| 323 | } |
| 324 | |
| 325 | return sk->sk_prot->ioctl(sk, cmd, arg); |
| 326 | } |
| 327 | |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 328 | static int pn_socket_listen(struct socket *sock, int backlog) |
| 329 | { |
| 330 | struct sock *sk = sock->sk; |
| 331 | int err = 0; |
| 332 | |
| 333 | if (sock->state != SS_UNCONNECTED) |
| 334 | return -EINVAL; |
| 335 | if (pn_socket_autobind(sock)) |
| 336 | return -ENOBUFS; |
| 337 | |
| 338 | lock_sock(sk); |
| 339 | if (sk->sk_state != TCP_CLOSE) { |
| 340 | err = -EINVAL; |
| 341 | goto out; |
| 342 | } |
| 343 | |
| 344 | sk->sk_state = TCP_LISTEN; |
| 345 | sk->sk_ack_backlog = 0; |
| 346 | sk->sk_max_ack_backlog = backlog; |
| 347 | out: |
| 348 | release_sock(sk); |
| 349 | return err; |
| 350 | } |
| 351 | |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 352 | static int pn_socket_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 353 | struct msghdr *m, size_t total_len) |
| 354 | { |
| 355 | struct sock *sk = sock->sk; |
| 356 | |
| 357 | if (pn_socket_autobind(sock)) |
| 358 | return -EAGAIN; |
| 359 | |
| 360 | return sk->sk_prot->sendmsg(iocb, sk, m, total_len); |
| 361 | } |
| 362 | |
| 363 | const struct proto_ops phonet_dgram_ops = { |
| 364 | .family = AF_PHONET, |
| 365 | .owner = THIS_MODULE, |
| 366 | .release = pn_socket_release, |
| 367 | .bind = pn_socket_bind, |
| 368 | .connect = sock_no_connect, |
| 369 | .socketpair = sock_no_socketpair, |
| 370 | .accept = sock_no_accept, |
| 371 | .getname = pn_socket_getname, |
| 372 | .poll = datagram_poll, |
| 373 | .ioctl = pn_socket_ioctl, |
| 374 | .listen = sock_no_listen, |
| 375 | .shutdown = sock_no_shutdown, |
| 376 | .setsockopt = sock_no_setsockopt, |
| 377 | .getsockopt = sock_no_getsockopt, |
| 378 | #ifdef CONFIG_COMPAT |
| 379 | .compat_setsockopt = sock_no_setsockopt, |
| 380 | .compat_getsockopt = sock_no_getsockopt, |
| 381 | #endif |
| 382 | .sendmsg = pn_socket_sendmsg, |
| 383 | .recvmsg = sock_common_recvmsg, |
| 384 | .mmap = sock_no_mmap, |
| 385 | .sendpage = sock_no_sendpage, |
| 386 | }; |
| 387 | |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 388 | const struct proto_ops phonet_stream_ops = { |
| 389 | .family = AF_PHONET, |
| 390 | .owner = THIS_MODULE, |
| 391 | .release = pn_socket_release, |
| 392 | .bind = pn_socket_bind, |
| 393 | .connect = sock_no_connect, |
| 394 | .socketpair = sock_no_socketpair, |
| 395 | .accept = pn_socket_accept, |
| 396 | .getname = pn_socket_getname, |
| 397 | .poll = pn_socket_poll, |
| 398 | .ioctl = pn_socket_ioctl, |
| 399 | .listen = pn_socket_listen, |
| 400 | .shutdown = sock_no_shutdown, |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 401 | .setsockopt = sock_common_setsockopt, |
| 402 | .getsockopt = sock_common_getsockopt, |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 403 | #ifdef CONFIG_COMPAT |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 404 | .compat_setsockopt = compat_sock_common_setsockopt, |
| 405 | .compat_getsockopt = compat_sock_common_getsockopt, |
Rémi Denis-Courmont | 9995a32 | 2008-10-05 11:14:48 -0700 | [diff] [blame] | 406 | #endif |
| 407 | .sendmsg = pn_socket_sendmsg, |
| 408 | .recvmsg = sock_common_recvmsg, |
| 409 | .mmap = sock_no_mmap, |
| 410 | .sendpage = sock_no_sendpage, |
| 411 | }; |
| 412 | EXPORT_SYMBOL(phonet_stream_ops); |
| 413 | |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 414 | /* allocate port for a socket */ |
| 415 | int pn_sock_get_port(struct sock *sk, unsigned short sport) |
| 416 | { |
| 417 | static int port_cur; |
Rémi Denis-Courmont | 5240488 | 2008-12-03 15:42:56 -0800 | [diff] [blame] | 418 | struct net *net = sock_net(sk); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 419 | struct pn_sock *pn = pn_sk(sk); |
| 420 | struct sockaddr_pn try_sa; |
| 421 | struct sock *tmpsk; |
| 422 | |
| 423 | memset(&try_sa, 0, sizeof(struct sockaddr_pn)); |
| 424 | try_sa.spn_family = AF_PHONET; |
Rémi Denis-Courmont | 582b0b6 | 2009-09-23 03:17:10 +0000 | [diff] [blame] | 425 | WARN_ON(!mutex_is_locked(&port_mutex)); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 426 | if (!sport) { |
| 427 | /* search free port */ |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 428 | int port, pmin, pmax; |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 429 | |
Remi Denis-Courmont | 87ab4e2 | 2008-09-22 20:08:39 -0700 | [diff] [blame] | 430 | phonet_get_local_port_range(&pmin, &pmax); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 431 | for (port = pmin; port <= pmax; port++) { |
| 432 | port_cur++; |
| 433 | if (port_cur < pmin || port_cur > pmax) |
| 434 | port_cur = pmin; |
| 435 | |
| 436 | pn_sockaddr_set_port(&try_sa, port_cur); |
Rémi Denis-Courmont | 5240488 | 2008-12-03 15:42:56 -0800 | [diff] [blame] | 437 | tmpsk = pn_find_sock_by_sa(net, &try_sa); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 438 | if (tmpsk == NULL) { |
| 439 | sport = port_cur; |
| 440 | goto found; |
| 441 | } else |
| 442 | sock_put(tmpsk); |
| 443 | } |
| 444 | } else { |
| 445 | /* try to find specific port */ |
| 446 | pn_sockaddr_set_port(&try_sa, sport); |
Rémi Denis-Courmont | 5240488 | 2008-12-03 15:42:56 -0800 | [diff] [blame] | 447 | tmpsk = pn_find_sock_by_sa(net, &try_sa); |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 448 | if (tmpsk == NULL) |
| 449 | /* No sock there! We can use that port... */ |
| 450 | goto found; |
| 451 | else |
| 452 | sock_put(tmpsk); |
| 453 | } |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 454 | /* the port must be in use already */ |
| 455 | return -EADDRINUSE; |
| 456 | |
| 457 | found: |
Remi Denis-Courmont | ba113a9 | 2008-09-22 20:05:19 -0700 | [diff] [blame] | 458 | pn->sobject = pn_object(pn_addr(pn->sobject), sport); |
| 459 | return 0; |
| 460 | } |
| 461 | EXPORT_SYMBOL(pn_sock_get_port); |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 462 | |
Randy Dunlap | ae6e2ae | 2009-08-17 10:35:49 +0000 | [diff] [blame] | 463 | #ifdef CONFIG_PROC_FS |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 464 | static struct sock *pn_sock_get_idx(struct seq_file *seq, loff_t pos) |
| 465 | { |
| 466 | struct net *net = seq_file_net(seq); |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 467 | struct hlist_head *hlist = pnsocks.hlist; |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 468 | struct hlist_node *node; |
| 469 | struct sock *sknode; |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 470 | unsigned h; |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 471 | |
Rémi Denis-Courmont | 6b0d07b | 2009-11-09 02:17:01 +0000 | [diff] [blame] | 472 | for (h = 0; h < PN_HASHSIZE; h++) { |
| 473 | sk_for_each(sknode, node, hlist) { |
| 474 | if (!net_eq(net, sock_net(sknode))) |
| 475 | continue; |
| 476 | if (!pos) |
| 477 | return sknode; |
| 478 | pos--; |
| 479 | } |
| 480 | hlist++; |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 481 | } |
| 482 | return NULL; |
| 483 | } |
| 484 | |
| 485 | static struct sock *pn_sock_get_next(struct seq_file *seq, struct sock *sk) |
| 486 | { |
| 487 | struct net *net = seq_file_net(seq); |
| 488 | |
| 489 | do |
| 490 | sk = sk_next(sk); |
| 491 | while (sk && !net_eq(net, sock_net(sk))); |
| 492 | |
| 493 | return sk; |
| 494 | } |
| 495 | |
| 496 | static void *pn_sock_seq_start(struct seq_file *seq, loff_t *pos) |
| 497 | __acquires(pnsocks.lock) |
| 498 | { |
| 499 | spin_lock_bh(&pnsocks.lock); |
| 500 | return *pos ? pn_sock_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 501 | } |
| 502 | |
| 503 | static void *pn_sock_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 504 | { |
| 505 | struct sock *sk; |
| 506 | |
| 507 | if (v == SEQ_START_TOKEN) |
| 508 | sk = pn_sock_get_idx(seq, 0); |
| 509 | else |
| 510 | sk = pn_sock_get_next(seq, v); |
| 511 | (*pos)++; |
| 512 | return sk; |
| 513 | } |
| 514 | |
| 515 | static void pn_sock_seq_stop(struct seq_file *seq, void *v) |
| 516 | __releases(pnsocks.lock) |
| 517 | { |
| 518 | spin_unlock_bh(&pnsocks.lock); |
| 519 | } |
| 520 | |
| 521 | static int pn_sock_seq_show(struct seq_file *seq, void *v) |
| 522 | { |
| 523 | int len; |
| 524 | |
| 525 | if (v == SEQ_START_TOKEN) |
| 526 | seq_printf(seq, "%s%n", "pt loc rem rs st tx_queue rx_queue " |
| 527 | " uid inode ref pointer drops", &len); |
| 528 | else { |
| 529 | struct sock *sk = v; |
| 530 | struct pn_sock *pn = pn_sk(sk); |
| 531 | |
| 532 | seq_printf(seq, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu " |
| 533 | "%d %p %d%n", |
| 534 | sk->sk_protocol, pn->sobject, 0, pn->resource, |
| 535 | sk->sk_state, |
| 536 | sk_wmem_alloc_get(sk), sk_rmem_alloc_get(sk), |
| 537 | sock_i_uid(sk), sock_i_ino(sk), |
| 538 | atomic_read(&sk->sk_refcnt), sk, |
| 539 | atomic_read(&sk->sk_drops), &len); |
| 540 | } |
| 541 | seq_printf(seq, "%*s\n", 127 - len, ""); |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | static const struct seq_operations pn_sock_seq_ops = { |
| 546 | .start = pn_sock_seq_start, |
| 547 | .next = pn_sock_seq_next, |
| 548 | .stop = pn_sock_seq_stop, |
| 549 | .show = pn_sock_seq_show, |
| 550 | }; |
| 551 | |
| 552 | static int pn_sock_open(struct inode *inode, struct file *file) |
| 553 | { |
Rémi Denis-Courmont | cb7d9e7f | 2009-08-11 03:12:07 +0000 | [diff] [blame] | 554 | return seq_open_net(inode, file, &pn_sock_seq_ops, |
| 555 | sizeof(struct seq_net_private)); |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | const struct file_operations pn_sock_seq_fops = { |
| 559 | .owner = THIS_MODULE, |
| 560 | .open = pn_sock_open, |
| 561 | .read = seq_read, |
| 562 | .llseek = seq_lseek, |
Rémi Denis-Courmont | cb7d9e7f | 2009-08-11 03:12:07 +0000 | [diff] [blame] | 563 | .release = seq_release_net, |
Rémi Denis-Courmont | c1dc13e | 2009-07-21 01:57:57 +0000 | [diff] [blame] | 564 | }; |
Randy Dunlap | ae6e2ae | 2009-08-17 10:35:49 +0000 | [diff] [blame] | 565 | #endif |