blob: ffd5f2297584879288eb55d2cf20b3b382b2378d [file] [log] [blame]
Remi Denis-Courmontba113a92008-09-22 20:05:19 -07001/*
2 * File: socket.c
3 *
4 * Phonet sockets
5 *
6 * Copyright (C) 2008 Nokia Corporation.
7 *
Rémi Denis-Courmont31fdc552012-06-13 22:29:03 +00008 * Authors: Sakari Ailus <sakari.ailus@nokia.com>
9 * Rémi Denis-Courmont
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070010 *
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 Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/gfp.h>
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070027#include <linux/kernel.h>
28#include <linux/net.h>
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -070029#include <linux/poll.h>
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070030#include <net/sock.h>
31#include <net/tcp_states.h>
32
33#include <linux/phonet.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040034#include <linux/export.h>
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070035#include <net/phonet/phonet.h>
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -070036#include <net/phonet/pep.h>
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070037#include <net/phonet/pn_dev.h>
38
39static int pn_socket_release(struct socket *sock)
40{
41 struct sock *sk = sock->sk;
42
43 if (sk) {
44 sock->sk = NULL;
45 sk->sk_prot->close(sk, 0);
46 }
47 return 0;
48}
49
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +000050#define PN_HASHSIZE 16
51#define PN_HASHMASK (PN_HASHSIZE-1)
52
53
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070054static struct {
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +000055 struct hlist_head hlist[PN_HASHSIZE];
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +000056 struct mutex lock;
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +000057} pnsocks;
58
59void __init pn_sock_init(void)
60{
Eric Dumazet95c96172012-04-15 05:58:06 +000061 unsigned int i;
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +000062
63 for (i = 0; i < PN_HASHSIZE; i++)
64 INIT_HLIST_HEAD(pnsocks.hlist + i);
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +000065 mutex_init(&pnsocks.lock);
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +000066}
67
68static struct hlist_head *pn_hash_list(u16 obj)
69{
70 return pnsocks.hlist + (obj & PN_HASHMASK);
71}
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070072
73/*
74 * Find address based on socket address, match only certain fields.
75 * Also grab sock if it was found. Remember to sock_put it later.
76 */
Rémi Denis-Courmont52404882008-12-03 15:42:56 -080077struct sock *pn_find_sock_by_sa(struct net *net, const struct sockaddr_pn *spn)
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070078{
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070079 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-Courmont6b0d07b2009-11-09 02:17:01 +000083 struct hlist_head *hlist = pn_hash_list(obj);
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070084
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +000085 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -080086 sk_for_each_rcu(sknode, hlist) {
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070087 struct pn_sock *pn = pn_sk(sknode);
88 BUG_ON(!pn->sobject); /* unbound socket */
89
Rémi Denis-Courmont52404882008-12-03 15:42:56 -080090 if (!net_eq(sock_net(sknode), net))
91 continue;
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070092 if (pn_port(obj)) {
93 /* Look up socket by port */
94 if (pn_port(pn->sobject) != pn_port(obj))
95 continue;
96 } else {
97 /* If port is zero, look up by resource */
98 if (pn->resource != res)
99 continue;
100 }
Joe Perchesf64f9e72009-11-29 16:55:45 -0800101 if (pn_addr(pn->sobject) &&
102 pn_addr(pn->sobject) != pn_addr(obj))
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700103 continue;
104
105 rval = sknode;
106 sock_hold(sknode);
107 break;
108 }
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000109 rcu_read_unlock();
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700110
111 return rval;
Rémi Denis-Courmontf14001f2009-10-14 00:48:27 +0000112}
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700113
Rémi Denis-Courmontf14001f2009-10-14 00:48:27 +0000114/* Deliver a broadcast packet (only in bottom-half) */
115void pn_deliver_sock_broadcast(struct net *net, struct sk_buff *skb)
116{
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000117 struct hlist_head *hlist = pnsocks.hlist;
Eric Dumazet95c96172012-04-15 05:58:06 +0000118 unsigned int h;
Rémi Denis-Courmontf14001f2009-10-14 00:48:27 +0000119
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000120 rcu_read_lock();
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000121 for (h = 0; h < PN_HASHSIZE; h++) {
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000122 struct sock *sknode;
Rémi Denis-Courmontf14001f2009-10-14 00:48:27 +0000123
Sasha Levinb67bfe02013-02-27 17:06:00 -0800124 sk_for_each(sknode, hlist) {
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000125 struct sk_buff *clone;
Rémi Denis-Courmontf14001f2009-10-14 00:48:27 +0000126
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000127 if (!net_eq(sock_net(sknode), net))
128 continue;
129 if (!sock_flag(sknode, SOCK_BROADCAST))
130 continue;
131
132 clone = skb_clone(skb, GFP_ATOMIC);
133 if (clone) {
134 sock_hold(sknode);
135 sk_receive_skb(sknode, clone, 0);
136 }
Rémi Denis-Courmont21912d12009-10-15 12:29:14 -0700137 }
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000138 hlist++;
Rémi Denis-Courmontf14001f2009-10-14 00:48:27 +0000139 }
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000140 rcu_read_unlock();
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700141}
142
Craig Gallek086c6532016-02-10 11:50:35 -0500143int pn_sock_hash(struct sock *sk)
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700144{
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000145 struct hlist_head *hlist = pn_hash_list(pn_sk(sk)->sobject);
146
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000147 mutex_lock(&pnsocks.lock);
148 sk_add_node_rcu(sk, hlist);
149 mutex_unlock(&pnsocks.lock);
Craig Gallek086c6532016-02-10 11:50:35 -0500150
151 return 0;
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700152}
153EXPORT_SYMBOL(pn_sock_hash);
154
155void pn_sock_unhash(struct sock *sk)
156{
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000157 mutex_lock(&pnsocks.lock);
158 sk_del_node_init_rcu(sk);
159 mutex_unlock(&pnsocks.lock);
Rémi Denis-Courmont7417fa82010-09-15 12:30:12 +0000160 pn_sock_unbind_all_res(sk);
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000161 synchronize_rcu();
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700162}
163EXPORT_SYMBOL(pn_sock_unhash);
164
Rémi Denis-Courmont582b0b62009-09-23 03:17:10 +0000165static DEFINE_MUTEX(port_mutex);
166
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700167static 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-Courmont52404882008-12-03 15:42:56 -0800186 if (saddr && phonet_address_lookup(sock_net(sk), saddr))
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700187 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-Courmont582b0b62009-09-23 03:17:10 +0000194 WARN_ON(sk_hashed(sk));
195 mutex_lock(&port_mutex);
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700196 err = sk->sk_prot->get_port(sk, pn_port(handle));
197 if (err)
Rémi Denis-Courmont582b0b62009-09-23 03:17:10 +0000198 goto out_port;
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700199
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 */
Craig Gallek086c6532016-02-10 11:50:35 -0500205 err = sk->sk_prot->hash(sk);
Rémi Denis-Courmont582b0b62009-09-23 03:17:10 +0000206out_port:
207 mutex_unlock(&port_mutex);
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700208out:
209 release_sock(sk);
210 return err;
211}
212
213static 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
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000228static int pn_socket_connect(struct socket *sock, struct sockaddr *addr,
229 int len, int flags)
230{
231 struct sock *sk = sock->sk;
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000232 struct pn_sock *pn = pn_sk(sk);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000233 struct sockaddr_pn *spn = (struct sockaddr_pn *)addr;
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000234 struct task_struct *tsk = current;
235 long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000236 int err;
237
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000238 if (pn_socket_autobind(sock))
239 return -ENOBUFS;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000240 if (len < sizeof(struct sockaddr_pn))
241 return -EINVAL;
242 if (spn->spn_family != AF_PHONET)
243 return -EAFNOSUPPORT;
244
245 lock_sock(sk);
246
247 switch (sock->state) {
248 case SS_UNCONNECTED:
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000249 if (sk->sk_state != TCP_CLOSE) {
250 err = -EISCONN;
251 goto out;
252 }
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000253 break;
254 case SS_CONNECTING:
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000255 err = -EALREADY;
256 goto out;
257 default:
258 err = -EISCONN;
259 goto out;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000260 }
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000261
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000262 pn->dobject = pn_sockaddr_get_object(spn);
263 pn->resource = pn_sockaddr_get_resource(spn);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000264 sock->state = SS_CONNECTING;
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000265
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000266 err = sk->sk_prot->connect(sk, addr, len);
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000267 if (err) {
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000268 sock->state = SS_UNCONNECTED;
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000269 pn->dobject = 0;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000270 goto out;
271 }
272
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000273 while (sk->sk_state == TCP_SYN_SENT) {
274 DEFINE_WAIT(wait);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000275
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000276 if (!timeo) {
277 err = -EINPROGRESS;
278 goto out;
279 }
280 if (signal_pending(tsk)) {
281 err = sock_intr_errno(timeo);
282 goto out;
283 }
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000284
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000285 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
286 TASK_INTERRUPTIBLE);
287 release_sock(sk);
288 timeo = schedule_timeout(timeo);
289 lock_sock(sk);
290 finish_wait(sk_sleep(sk), &wait);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000291 }
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000292
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000293 if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED))
294 err = 0;
295 else if (sk->sk_state == TCP_CLOSE_WAIT)
296 err = -ECONNRESET;
297 else
298 err = -ECONNREFUSED;
299 sock->state = err ? SS_UNCONNECTED : SS_CONNECTED;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000300out:
301 release_sock(sk);
302 return err;
303}
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000304
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700305static int pn_socket_accept(struct socket *sock, struct socket *newsock,
306 int flags)
307{
308 struct sock *sk = sock->sk;
309 struct sock *newsk;
310 int err;
311
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +0000312 if (unlikely(sk->sk_state != TCP_LISTEN))
313 return -EINVAL;
314
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700315 newsk = sk->sk_prot->accept(sk, flags, &err);
316 if (!newsk)
317 return err;
318
319 lock_sock(newsk);
320 sock_graft(newsk, newsock);
321 newsock->state = SS_CONNECTED;
322 release_sock(newsk);
323 return 0;
324}
325
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700326static int pn_socket_getname(struct socket *sock, struct sockaddr *addr,
327 int *sockaddr_len, int peer)
328{
329 struct sock *sk = sock->sk;
330 struct pn_sock *pn = pn_sk(sk);
331
332 memset(addr, 0, sizeof(struct sockaddr_pn));
333 addr->sa_family = AF_PHONET;
334 if (!peer) /* Race with bind() here is userland's problem. */
335 pn_sockaddr_set_object((struct sockaddr_pn *)addr,
336 pn->sobject);
337
338 *sockaddr_len = sizeof(struct sockaddr_pn);
339 return 0;
340}
341
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700342static unsigned int pn_socket_poll(struct file *file, struct socket *sock,
343 poll_table *wait)
344{
345 struct sock *sk = sock->sk;
346 struct pep_sock *pn = pep_sk(sk);
347 unsigned int mask = 0;
348
Eric Dumazet43815482010-04-29 11:01:49 +0000349 poll_wait(file, sk_sleep(sk), wait);
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700350
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +0000351 if (sk->sk_state == TCP_CLOSE)
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700352 return POLLERR;
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700353 if (!skb_queue_empty(&sk->sk_receive_queue))
354 mask |= POLLIN | POLLRDNORM;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700355 if (!skb_queue_empty(&pn->ctrlreq_queue))
356 mask |= POLLPRI;
357 if (!mask && sk->sk_state == TCP_CLOSE_WAIT)
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700358 return POLLHUP;
359
Rémi Denis-Courmont01b38602010-08-30 12:57:05 +0000360 if (sk->sk_state == TCP_ESTABLISHED &&
361 atomic_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf &&
362 atomic_read(&pn->tx_credits))
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700363 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
364
365 return mask;
366}
367
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700368static int pn_socket_ioctl(struct socket *sock, unsigned int cmd,
369 unsigned long arg)
370{
371 struct sock *sk = sock->sk;
372 struct pn_sock *pn = pn_sk(sk);
373
374 if (cmd == SIOCPNGETOBJECT) {
375 struct net_device *dev;
376 u16 handle;
377 u8 saddr;
378
379 if (get_user(handle, (__u16 __user *)arg))
380 return -EFAULT;
381
382 lock_sock(sk);
383 if (sk->sk_bound_dev_if)
384 dev = dev_get_by_index(sock_net(sk),
385 sk->sk_bound_dev_if);
386 else
387 dev = phonet_device_get(sock_net(sk));
388 if (dev && (dev->flags & IFF_UP))
389 saddr = phonet_address_get(dev, pn_addr(handle));
390 else
391 saddr = PN_NO_ADDR;
392 release_sock(sk);
393
394 if (dev)
395 dev_put(dev);
396 if (saddr == PN_NO_ADDR)
397 return -EHOSTUNREACH;
398
399 handle = pn_object(saddr, pn_port(pn->sobject));
400 return put_user(handle, (__u16 __user *)arg);
401 }
402
403 return sk->sk_prot->ioctl(sk, cmd, arg);
404}
405
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700406static int pn_socket_listen(struct socket *sock, int backlog)
407{
408 struct sock *sk = sock->sk;
409 int err = 0;
410
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700411 if (pn_socket_autobind(sock))
412 return -ENOBUFS;
413
414 lock_sock(sk);
Rémi Denis-Courmont96241542011-02-24 23:14:56 +0000415 if (sock->state != SS_UNCONNECTED) {
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700416 err = -EINVAL;
417 goto out;
418 }
419
Rémi Denis-Courmont96241542011-02-24 23:14:56 +0000420 if (sk->sk_state != TCP_LISTEN) {
421 sk->sk_state = TCP_LISTEN;
422 sk->sk_ack_backlog = 0;
423 }
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700424 sk->sk_max_ack_backlog = backlog;
425out:
426 release_sock(sk);
427 return err;
428}
429
Ying Xue1b784142015-03-02 15:37:48 +0800430static int pn_socket_sendmsg(struct socket *sock, struct msghdr *m,
431 size_t total_len)
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700432{
433 struct sock *sk = sock->sk;
434
435 if (pn_socket_autobind(sock))
436 return -EAGAIN;
437
Ying Xue1b784142015-03-02 15:37:48 +0800438 return sk->sk_prot->sendmsg(sk, m, total_len);
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700439}
440
441const struct proto_ops phonet_dgram_ops = {
442 .family = AF_PHONET,
443 .owner = THIS_MODULE,
444 .release = pn_socket_release,
445 .bind = pn_socket_bind,
446 .connect = sock_no_connect,
447 .socketpair = sock_no_socketpair,
448 .accept = sock_no_accept,
449 .getname = pn_socket_getname,
450 .poll = datagram_poll,
451 .ioctl = pn_socket_ioctl,
452 .listen = sock_no_listen,
453 .shutdown = sock_no_shutdown,
454 .setsockopt = sock_no_setsockopt,
455 .getsockopt = sock_no_getsockopt,
456#ifdef CONFIG_COMPAT
457 .compat_setsockopt = sock_no_setsockopt,
458 .compat_getsockopt = sock_no_getsockopt,
459#endif
460 .sendmsg = pn_socket_sendmsg,
461 .recvmsg = sock_common_recvmsg,
462 .mmap = sock_no_mmap,
463 .sendpage = sock_no_sendpage,
464};
465
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700466const struct proto_ops phonet_stream_ops = {
467 .family = AF_PHONET,
468 .owner = THIS_MODULE,
469 .release = pn_socket_release,
470 .bind = pn_socket_bind,
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000471 .connect = pn_socket_connect,
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700472 .socketpair = sock_no_socketpair,
473 .accept = pn_socket_accept,
474 .getname = pn_socket_getname,
475 .poll = pn_socket_poll,
476 .ioctl = pn_socket_ioctl,
477 .listen = pn_socket_listen,
478 .shutdown = sock_no_shutdown,
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700479 .setsockopt = sock_common_setsockopt,
480 .getsockopt = sock_common_getsockopt,
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700481#ifdef CONFIG_COMPAT
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700482 .compat_setsockopt = compat_sock_common_setsockopt,
483 .compat_getsockopt = compat_sock_common_getsockopt,
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700484#endif
485 .sendmsg = pn_socket_sendmsg,
486 .recvmsg = sock_common_recvmsg,
487 .mmap = sock_no_mmap,
488 .sendpage = sock_no_sendpage,
489};
490EXPORT_SYMBOL(phonet_stream_ops);
491
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700492/* allocate port for a socket */
493int pn_sock_get_port(struct sock *sk, unsigned short sport)
494{
495 static int port_cur;
Rémi Denis-Courmont52404882008-12-03 15:42:56 -0800496 struct net *net = sock_net(sk);
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700497 struct pn_sock *pn = pn_sk(sk);
498 struct sockaddr_pn try_sa;
499 struct sock *tmpsk;
500
501 memset(&try_sa, 0, sizeof(struct sockaddr_pn));
502 try_sa.spn_family = AF_PHONET;
Rémi Denis-Courmont582b0b62009-09-23 03:17:10 +0000503 WARN_ON(!mutex_is_locked(&port_mutex));
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700504 if (!sport) {
505 /* search free port */
Remi Denis-Courmont87ab4e22008-09-22 20:08:39 -0700506 int port, pmin, pmax;
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700507
Remi Denis-Courmont87ab4e22008-09-22 20:08:39 -0700508 phonet_get_local_port_range(&pmin, &pmax);
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700509 for (port = pmin; port <= pmax; port++) {
510 port_cur++;
511 if (port_cur < pmin || port_cur > pmax)
512 port_cur = pmin;
513
514 pn_sockaddr_set_port(&try_sa, port_cur);
Rémi Denis-Courmont52404882008-12-03 15:42:56 -0800515 tmpsk = pn_find_sock_by_sa(net, &try_sa);
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700516 if (tmpsk == NULL) {
517 sport = port_cur;
518 goto found;
519 } else
520 sock_put(tmpsk);
521 }
522 } else {
523 /* try to find specific port */
524 pn_sockaddr_set_port(&try_sa, sport);
Rémi Denis-Courmont52404882008-12-03 15:42:56 -0800525 tmpsk = pn_find_sock_by_sa(net, &try_sa);
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700526 if (tmpsk == NULL)
527 /* No sock there! We can use that port... */
528 goto found;
529 else
530 sock_put(tmpsk);
531 }
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700532 /* the port must be in use already */
533 return -EADDRINUSE;
534
535found:
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700536 pn->sobject = pn_object(pn_addr(pn->sobject), sport);
537 return 0;
538}
539EXPORT_SYMBOL(pn_sock_get_port);
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000540
Randy Dunlapae6e2ae2009-08-17 10:35:49 +0000541#ifdef CONFIG_PROC_FS
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000542static struct sock *pn_sock_get_idx(struct seq_file *seq, loff_t pos)
543{
544 struct net *net = seq_file_net(seq);
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000545 struct hlist_head *hlist = pnsocks.hlist;
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000546 struct sock *sknode;
Eric Dumazet95c96172012-04-15 05:58:06 +0000547 unsigned int h;
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000548
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000549 for (h = 0; h < PN_HASHSIZE; h++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800550 sk_for_each_rcu(sknode, hlist) {
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000551 if (!net_eq(net, sock_net(sknode)))
552 continue;
553 if (!pos)
554 return sknode;
555 pos--;
556 }
557 hlist++;
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000558 }
559 return NULL;
560}
561
562static struct sock *pn_sock_get_next(struct seq_file *seq, struct sock *sk)
563{
564 struct net *net = seq_file_net(seq);
565
566 do
567 sk = sk_next(sk);
568 while (sk && !net_eq(net, sock_net(sk)));
569
570 return sk;
571}
572
573static void *pn_sock_seq_start(struct seq_file *seq, loff_t *pos)
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000574 __acquires(rcu)
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000575{
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000576 rcu_read_lock();
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000577 return *pos ? pn_sock_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
578}
579
580static void *pn_sock_seq_next(struct seq_file *seq, void *v, loff_t *pos)
581{
582 struct sock *sk;
583
584 if (v == SEQ_START_TOKEN)
585 sk = pn_sock_get_idx(seq, 0);
586 else
587 sk = pn_sock_get_next(seq, v);
588 (*pos)++;
589 return sk;
590}
591
592static void pn_sock_seq_stop(struct seq_file *seq, void *v)
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000593 __releases(rcu)
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000594{
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000595 rcu_read_unlock();
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000596}
597
598static int pn_sock_seq_show(struct seq_file *seq, void *v)
599{
Tetsuo Handa652586d2013-11-14 14:31:57 -0800600 seq_setwidth(seq, 127);
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000601 if (v == SEQ_START_TOKEN)
Tetsuo Handa652586d2013-11-14 14:31:57 -0800602 seq_puts(seq, "pt loc rem rs st tx_queue rx_queue "
603 " uid inode ref pointer drops");
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000604 else {
605 struct sock *sk = v;
606 struct pn_sock *pn = pn_sk(sk);
607
608 seq_printf(seq, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu "
Tetsuo Handa652586d2013-11-14 14:31:57 -0800609 "%d %pK %d",
Rémi Denis-Courmonta8059512011-02-24 23:14:57 +0000610 sk->sk_protocol, pn->sobject, pn->dobject,
611 pn->resource, sk->sk_state,
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000612 sk_wmem_alloc_get(sk), sk_rmem_alloc_get(sk),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -0600613 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
614 sock_i_ino(sk),
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000615 atomic_read(&sk->sk_refcnt), sk,
Tetsuo Handa652586d2013-11-14 14:31:57 -0800616 atomic_read(&sk->sk_drops));
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000617 }
Tetsuo Handa652586d2013-11-14 14:31:57 -0800618 seq_pad(seq, '\n');
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000619 return 0;
620}
621
622static const struct seq_operations pn_sock_seq_ops = {
623 .start = pn_sock_seq_start,
624 .next = pn_sock_seq_next,
625 .stop = pn_sock_seq_stop,
626 .show = pn_sock_seq_show,
627};
628
629static int pn_sock_open(struct inode *inode, struct file *file)
630{
Rémi Denis-Courmontcb7d9e7f2009-08-11 03:12:07 +0000631 return seq_open_net(inode, file, &pn_sock_seq_ops,
632 sizeof(struct seq_net_private));
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000633}
634
635const struct file_operations pn_sock_seq_fops = {
636 .owner = THIS_MODULE,
637 .open = pn_sock_open,
638 .read = seq_read,
639 .llseek = seq_lseek,
Rémi Denis-Courmontcb7d9e7f2009-08-11 03:12:07 +0000640 .release = seq_release_net,
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000641};
Randy Dunlapae6e2ae2009-08-17 10:35:49 +0000642#endif
Rémi Denis-Courmont4e3d16c2010-09-15 12:30:11 +0000643
644static struct {
645 struct sock *sk[256];
646} pnres;
647
648/*
649 * Find and hold socket based on resource.
650 */
651struct 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
666static DEFINE_MUTEX(resource_mutex);
667
668int 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 Dumazetcf778b02012-01-12 04:41:32 +0000682 rcu_assign_pointer(pnres.sk[res], sk);
Rémi Denis-Courmont4e3d16c2010-09-15 12:30:11 +0000683 ret = 0;
684 }
685 mutex_unlock(&resource_mutex);
686 return ret;
687}
688
689int 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 Hemmingera9b3cd72011-08-01 16:19:00 +0000698 RCU_INIT_POINTER(pnres.sk[res], NULL);
Rémi Denis-Courmont4e3d16c2010-09-15 12:30:11 +0000699 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
710void pn_sock_unbind_all_res(struct sock *sk)
711{
Eric Dumazet95c96172012-04-15 05:58:06 +0000712 unsigned int res, match = 0;
Rémi Denis-Courmont4e3d16c2010-09-15 12:30:11 +0000713
714 mutex_lock(&resource_mutex);
715 for (res = 0; res < 256; res++) {
716 if (pnres.sk[res] == sk) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000717 RCU_INIT_POINTER(pnres.sk[res], NULL);
Rémi Denis-Courmont4e3d16c2010-09-15 12:30:11 +0000718 match++;
719 }
720 }
721 mutex_unlock(&resource_mutex);
722
Rémi Denis-Courmont4e3d16c2010-09-15 12:30:11 +0000723 while (match > 0) {
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000724 __sock_put(sk);
Rémi Denis-Courmont4e3d16c2010-09-15 12:30:11 +0000725 match--;
726 }
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000727 /* Caller is responsible for RCU sync before final sock_put() */
Rémi Denis-Courmont4e3d16c2010-09-15 12:30:11 +0000728}
Rémi Denis-Courmont507215f2010-09-15 12:30:14 +0000729
730#ifdef CONFIG_PROC_FS
731static struct sock **pn_res_get_idx(struct seq_file *seq, loff_t pos)
732{
733 struct net *net = seq_file_net(seq);
Eric Dumazet95c96172012-04-15 05:58:06 +0000734 unsigned int i;
Rémi Denis-Courmont507215f2010-09-15 12:30:14 +0000735
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
749static struct sock **pn_res_get_next(struct seq_file *seq, struct sock **sk)
750{
751 struct net *net = seq_file_net(seq);
Eric Dumazet95c96172012-04-15 05:58:06 +0000752 unsigned int i;
Rémi Denis-Courmont507215f2010-09-15 12:30:14 +0000753
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
762static 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
769static 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
781static void pn_res_seq_stop(struct seq_file *seq, void *v)
782 __releases(resource_mutex)
783{
784 mutex_unlock(&resource_mutex);
785}
786
787static int pn_res_seq_show(struct seq_file *seq, void *v)
788{
Tetsuo Handa652586d2013-11-14 14:31:57 -0800789 seq_setwidth(seq, 63);
Rémi Denis-Courmont507215f2010-09-15 12:30:14 +0000790 if (v == SEQ_START_TOKEN)
Tetsuo Handa652586d2013-11-14 14:31:57 -0800791 seq_puts(seq, "rs uid inode");
Rémi Denis-Courmont507215f2010-09-15 12:30:14 +0000792 else {
793 struct sock **psk = v;
794 struct sock *sk = *psk;
795
Tetsuo Handa652586d2013-11-14 14:31:57 -0800796 seq_printf(seq, "%02X %5u %lu",
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -0600797 (int) (psk - pnres.sk),
798 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
Tetsuo Handa652586d2013-11-14 14:31:57 -0800799 sock_i_ino(sk));
Rémi Denis-Courmont507215f2010-09-15 12:30:14 +0000800 }
Tetsuo Handa652586d2013-11-14 14:31:57 -0800801 seq_pad(seq, '\n');
Rémi Denis-Courmont507215f2010-09-15 12:30:14 +0000802 return 0;
803}
804
805static 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
812static 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
818const struct file_operations pn_res_seq_fops = {
819 .owner = THIS_MODULE,
820 .open = pn_res_open,
821 .read = seq_read,
822 .llseek = seq_lseek,
823 .release = seq_release_net,
824};
825#endif