blob: c295c4e20f012f31c1b443c5f859969caf412cec [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-Courmont31fdc5552012-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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010030#include <linux/sched/signal.h>
31
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070032#include <net/sock.h>
33#include <net/tcp_states.h>
34
35#include <linux/phonet.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040036#include <linux/export.h>
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070037#include <net/phonet/phonet.h>
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -070038#include <net/phonet/pep.h>
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070039#include <net/phonet/pn_dev.h>
40
41static 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-Courmont6b0d07b2009-11-09 02:17:01 +000052#define PN_HASHSIZE 16
53#define PN_HASHMASK (PN_HASHSIZE-1)
54
55
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070056static struct {
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +000057 struct hlist_head hlist[PN_HASHSIZE];
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +000058 struct mutex lock;
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +000059} pnsocks;
60
61void __init pn_sock_init(void)
62{
Eric Dumazet95c96172012-04-15 05:58:06 +000063 unsigned int i;
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +000064
65 for (i = 0; i < PN_HASHSIZE; i++)
66 INIT_HLIST_HEAD(pnsocks.hlist + i);
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +000067 mutex_init(&pnsocks.lock);
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +000068}
69
70static struct hlist_head *pn_hash_list(u16 obj)
71{
72 return pnsocks.hlist + (obj & PN_HASHMASK);
73}
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070074
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-Courmont52404882008-12-03 15:42:56 -080079struct sock *pn_find_sock_by_sa(struct net *net, const struct sockaddr_pn *spn)
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070080{
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070081 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-Courmont6b0d07b2009-11-09 02:17:01 +000085 struct hlist_head *hlist = pn_hash_list(obj);
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070086
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +000087 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -080088 sk_for_each_rcu(sknode, hlist) {
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070089 struct pn_sock *pn = pn_sk(sknode);
90 BUG_ON(!pn->sobject); /* unbound socket */
91
Rémi Denis-Courmont52404882008-12-03 15:42:56 -080092 if (!net_eq(sock_net(sknode), net))
93 continue;
Remi Denis-Courmontba113a92008-09-22 20:05:19 -070094 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 Perchesf64f9e72009-11-29 16:55:45 -0800103 if (pn_addr(pn->sobject) &&
104 pn_addr(pn->sobject) != pn_addr(obj))
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700105 continue;
106
107 rval = sknode;
108 sock_hold(sknode);
109 break;
110 }
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000111 rcu_read_unlock();
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700112
113 return rval;
Rémi Denis-Courmontf14001f2009-10-14 00:48:27 +0000114}
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700115
Rémi Denis-Courmontf14001f2009-10-14 00:48:27 +0000116/* Deliver a broadcast packet (only in bottom-half) */
117void pn_deliver_sock_broadcast(struct net *net, struct sk_buff *skb)
118{
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000119 struct hlist_head *hlist = pnsocks.hlist;
Eric Dumazet95c96172012-04-15 05:58:06 +0000120 unsigned int h;
Rémi Denis-Courmontf14001f2009-10-14 00:48:27 +0000121
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000122 rcu_read_lock();
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000123 for (h = 0; h < PN_HASHSIZE; h++) {
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000124 struct sock *sknode;
Rémi Denis-Courmontf14001f2009-10-14 00:48:27 +0000125
Sasha Levinb67bfe02013-02-27 17:06:00 -0800126 sk_for_each(sknode, hlist) {
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000127 struct sk_buff *clone;
Rémi Denis-Courmontf14001f2009-10-14 00:48:27 +0000128
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000129 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-Courmont21912d12009-10-15 12:29:14 -0700139 }
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000140 hlist++;
Rémi Denis-Courmontf14001f2009-10-14 00:48:27 +0000141 }
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000142 rcu_read_unlock();
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700143}
144
Craig Gallek086c6532016-02-10 11:50:35 -0500145int pn_sock_hash(struct sock *sk)
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700146{
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000147 struct hlist_head *hlist = pn_hash_list(pn_sk(sk)->sobject);
148
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000149 mutex_lock(&pnsocks.lock);
150 sk_add_node_rcu(sk, hlist);
151 mutex_unlock(&pnsocks.lock);
Craig Gallek086c6532016-02-10 11:50:35 -0500152
153 return 0;
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700154}
155EXPORT_SYMBOL(pn_sock_hash);
156
157void pn_sock_unhash(struct sock *sk)
158{
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000159 mutex_lock(&pnsocks.lock);
160 sk_del_node_init_rcu(sk);
161 mutex_unlock(&pnsocks.lock);
Rémi Denis-Courmont7417fa82010-09-15 12:30:12 +0000162 pn_sock_unbind_all_res(sk);
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000163 synchronize_rcu();
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700164}
165EXPORT_SYMBOL(pn_sock_unhash);
166
Rémi Denis-Courmont582b0b62009-09-23 03:17:10 +0000167static DEFINE_MUTEX(port_mutex);
168
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700169static 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-Courmont52404882008-12-03 15:42:56 -0800188 if (saddr && phonet_address_lookup(sock_net(sk), saddr))
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700189 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-Courmont582b0b62009-09-23 03:17:10 +0000196 WARN_ON(sk_hashed(sk));
197 mutex_lock(&port_mutex);
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700198 err = sk->sk_prot->get_port(sk, pn_port(handle));
199 if (err)
Rémi Denis-Courmont582b0b62009-09-23 03:17:10 +0000200 goto out_port;
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700201
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 Gallek086c6532016-02-10 11:50:35 -0500207 err = sk->sk_prot->hash(sk);
Rémi Denis-Courmont582b0b62009-09-23 03:17:10 +0000208out_port:
209 mutex_unlock(&port_mutex);
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700210out:
211 release_sock(sk);
212 return err;
213}
214
215static 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 Sanghvib3d62552010-10-12 20:14:43 +0000230static 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-Courmont297edb62011-03-08 22:44:12 +0000234 struct pn_sock *pn = pn_sk(sk);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000235 struct sockaddr_pn *spn = (struct sockaddr_pn *)addr;
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000236 struct task_struct *tsk = current;
237 long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000238 int err;
239
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000240 if (pn_socket_autobind(sock))
241 return -ENOBUFS;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000242 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-Courmont297edb62011-03-08 22:44:12 +0000251 if (sk->sk_state != TCP_CLOSE) {
252 err = -EISCONN;
253 goto out;
254 }
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000255 break;
256 case SS_CONNECTING:
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000257 err = -EALREADY;
258 goto out;
259 default:
260 err = -EISCONN;
261 goto out;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000262 }
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000263
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000264 pn->dobject = pn_sockaddr_get_object(spn);
265 pn->resource = pn_sockaddr_get_resource(spn);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000266 sock->state = SS_CONNECTING;
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000267
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000268 err = sk->sk_prot->connect(sk, addr, len);
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000269 if (err) {
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000270 sock->state = SS_UNCONNECTED;
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000271 pn->dobject = 0;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000272 goto out;
273 }
274
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000275 while (sk->sk_state == TCP_SYN_SENT) {
276 DEFINE_WAIT(wait);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000277
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000278 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 Sanghvib3d62552010-10-12 20:14:43 +0000286
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000287 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 Sanghvib3d62552010-10-12 20:14:43 +0000293 }
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000294
Rémi Denis-Courmont297edb62011-03-08 22:44:12 +0000295 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 Sanghvib3d62552010-10-12 20:14:43 +0000302out:
303 release_sock(sk);
304 return err;
305}
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000306
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700307static int pn_socket_accept(struct socket *sock, struct socket *newsock,
David Howellscdfbabf2017-03-09 08:09:05 +0000308 int flags, bool kern)
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700309{
310 struct sock *sk = sock->sk;
311 struct sock *newsk;
312 int err;
313
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +0000314 if (unlikely(sk->sk_state != TCP_LISTEN))
315 return -EINVAL;
316
David Howellscdfbabf2017-03-09 08:09:05 +0000317 newsk = sk->sk_prot->accept(sk, flags, &err, kern);
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700318 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-Courmontba113a92008-09-22 20:05:19 -0700328static int pn_socket_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100329 int peer)
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700330{
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 Vlasenko9b2c45d2018-02-12 20:00:20 +0100340 return sizeof(struct sockaddr_pn);
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700341}
342
Christoph Hellwige7a98d472017-12-31 16:39:42 +0100343static __poll_t pn_socket_poll_mask(struct socket *sock, __poll_t events)
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700344{
345 struct sock *sk = sock->sk;
346 struct pep_sock *pn = pep_sk(sk);
Al Viroade994f2017-07-03 00:01:49 -0400347 __poll_t mask = 0;
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700348
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +0000349 if (sk->sk_state == TCP_CLOSE)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800350 return EPOLLERR;
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700351 if (!skb_queue_empty(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800352 mask |= EPOLLIN | EPOLLRDNORM;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700353 if (!skb_queue_empty(&pn->ctrlreq_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800354 mask |= EPOLLPRI;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700355 if (!mask && sk->sk_state == TCP_CLOSE_WAIT)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800356 return EPOLLHUP;
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700357
Rémi Denis-Courmont01b38602010-08-30 12:57:05 +0000358 if (sk->sk_state == TCP_ESTABLISHED &&
Reshetova, Elena14afee42017-06-30 13:08:00 +0300359 refcount_read(&sk->sk_wmem_alloc) < sk->sk_sndbuf &&
Rémi Denis-Courmont01b38602010-08-30 12:57:05 +0000360 atomic_read(&pn->tx_credits))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800361 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700362
363 return mask;
364}
365
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700366static int pn_socket_ioctl(struct socket *sock, unsigned int cmd,
367 unsigned long arg)
368{
369 struct sock *sk = sock->sk;
370 struct pn_sock *pn = pn_sk(sk);
371
372 if (cmd == SIOCPNGETOBJECT) {
373 struct net_device *dev;
374 u16 handle;
375 u8 saddr;
376
377 if (get_user(handle, (__u16 __user *)arg))
378 return -EFAULT;
379
380 lock_sock(sk);
381 if (sk->sk_bound_dev_if)
382 dev = dev_get_by_index(sock_net(sk),
383 sk->sk_bound_dev_if);
384 else
385 dev = phonet_device_get(sock_net(sk));
386 if (dev && (dev->flags & IFF_UP))
387 saddr = phonet_address_get(dev, pn_addr(handle));
388 else
389 saddr = PN_NO_ADDR;
390 release_sock(sk);
391
392 if (dev)
393 dev_put(dev);
394 if (saddr == PN_NO_ADDR)
395 return -EHOSTUNREACH;
396
397 handle = pn_object(saddr, pn_port(pn->sobject));
398 return put_user(handle, (__u16 __user *)arg);
399 }
400
401 return sk->sk_prot->ioctl(sk, cmd, arg);
402}
403
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700404static int pn_socket_listen(struct socket *sock, int backlog)
405{
406 struct sock *sk = sock->sk;
407 int err = 0;
408
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700409 if (pn_socket_autobind(sock))
410 return -ENOBUFS;
411
412 lock_sock(sk);
Rémi Denis-Courmont96241542011-02-24 23:14:56 +0000413 if (sock->state != SS_UNCONNECTED) {
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700414 err = -EINVAL;
415 goto out;
416 }
417
Rémi Denis-Courmont96241542011-02-24 23:14:56 +0000418 if (sk->sk_state != TCP_LISTEN) {
419 sk->sk_state = TCP_LISTEN;
420 sk->sk_ack_backlog = 0;
421 }
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700422 sk->sk_max_ack_backlog = backlog;
423out:
424 release_sock(sk);
425 return err;
426}
427
Ying Xue1b784142015-03-02 15:37:48 +0800428static int pn_socket_sendmsg(struct socket *sock, struct msghdr *m,
429 size_t total_len)
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700430{
431 struct sock *sk = sock->sk;
432
433 if (pn_socket_autobind(sock))
434 return -EAGAIN;
435
Ying Xue1b784142015-03-02 15:37:48 +0800436 return sk->sk_prot->sendmsg(sk, m, total_len);
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700437}
438
439const struct proto_ops phonet_dgram_ops = {
440 .family = AF_PHONET,
441 .owner = THIS_MODULE,
442 .release = pn_socket_release,
443 .bind = pn_socket_bind,
444 .connect = sock_no_connect,
445 .socketpair = sock_no_socketpair,
446 .accept = sock_no_accept,
447 .getname = pn_socket_getname,
Christoph Hellwigdb5051e2018-04-09 15:27:37 +0200448 .poll_mask = datagram_poll_mask,
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700449 .ioctl = pn_socket_ioctl,
450 .listen = sock_no_listen,
451 .shutdown = sock_no_shutdown,
452 .setsockopt = sock_no_setsockopt,
453 .getsockopt = sock_no_getsockopt,
454#ifdef CONFIG_COMPAT
455 .compat_setsockopt = sock_no_setsockopt,
456 .compat_getsockopt = sock_no_getsockopt,
457#endif
458 .sendmsg = pn_socket_sendmsg,
459 .recvmsg = sock_common_recvmsg,
460 .mmap = sock_no_mmap,
461 .sendpage = sock_no_sendpage,
462};
463
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700464const struct proto_ops phonet_stream_ops = {
465 .family = AF_PHONET,
466 .owner = THIS_MODULE,
467 .release = pn_socket_release,
468 .bind = pn_socket_bind,
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000469 .connect = pn_socket_connect,
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700470 .socketpair = sock_no_socketpair,
471 .accept = pn_socket_accept,
472 .getname = pn_socket_getname,
Christoph Hellwige7a98d472017-12-31 16:39:42 +0100473 .poll_mask = pn_socket_poll_mask,
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700474 .ioctl = pn_socket_ioctl,
475 .listen = pn_socket_listen,
476 .shutdown = sock_no_shutdown,
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700477 .setsockopt = sock_common_setsockopt,
478 .getsockopt = sock_common_getsockopt,
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700479#ifdef CONFIG_COMPAT
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700480 .compat_setsockopt = compat_sock_common_setsockopt,
481 .compat_getsockopt = compat_sock_common_getsockopt,
Rémi Denis-Courmont9995a322008-10-05 11:14:48 -0700482#endif
483 .sendmsg = pn_socket_sendmsg,
484 .recvmsg = sock_common_recvmsg,
485 .mmap = sock_no_mmap,
486 .sendpage = sock_no_sendpage,
487};
488EXPORT_SYMBOL(phonet_stream_ops);
489
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700490/* allocate port for a socket */
491int pn_sock_get_port(struct sock *sk, unsigned short sport)
492{
493 static int port_cur;
Rémi Denis-Courmont52404882008-12-03 15:42:56 -0800494 struct net *net = sock_net(sk);
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700495 struct pn_sock *pn = pn_sk(sk);
496 struct sockaddr_pn try_sa;
497 struct sock *tmpsk;
498
499 memset(&try_sa, 0, sizeof(struct sockaddr_pn));
500 try_sa.spn_family = AF_PHONET;
Rémi Denis-Courmont582b0b62009-09-23 03:17:10 +0000501 WARN_ON(!mutex_is_locked(&port_mutex));
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700502 if (!sport) {
503 /* search free port */
Remi Denis-Courmont87ab4e22008-09-22 20:08:39 -0700504 int port, pmin, pmax;
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700505
Remi Denis-Courmont87ab4e22008-09-22 20:08:39 -0700506 phonet_get_local_port_range(&pmin, &pmax);
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700507 for (port = pmin; port <= pmax; port++) {
508 port_cur++;
509 if (port_cur < pmin || port_cur > pmax)
510 port_cur = pmin;
511
512 pn_sockaddr_set_port(&try_sa, port_cur);
Rémi Denis-Courmont52404882008-12-03 15:42:56 -0800513 tmpsk = pn_find_sock_by_sa(net, &try_sa);
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700514 if (tmpsk == NULL) {
515 sport = port_cur;
516 goto found;
517 } else
518 sock_put(tmpsk);
519 }
520 } else {
521 /* try to find specific port */
522 pn_sockaddr_set_port(&try_sa, sport);
Rémi Denis-Courmont52404882008-12-03 15:42:56 -0800523 tmpsk = pn_find_sock_by_sa(net, &try_sa);
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700524 if (tmpsk == NULL)
525 /* No sock there! We can use that port... */
526 goto found;
527 else
528 sock_put(tmpsk);
529 }
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700530 /* the port must be in use already */
531 return -EADDRINUSE;
532
533found:
Remi Denis-Courmontba113a92008-09-22 20:05:19 -0700534 pn->sobject = pn_object(pn_addr(pn->sobject), sport);
535 return 0;
536}
537EXPORT_SYMBOL(pn_sock_get_port);
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000538
Randy Dunlapae6e2ae2009-08-17 10:35:49 +0000539#ifdef CONFIG_PROC_FS
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000540static struct sock *pn_sock_get_idx(struct seq_file *seq, loff_t pos)
541{
542 struct net *net = seq_file_net(seq);
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000543 struct hlist_head *hlist = pnsocks.hlist;
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000544 struct sock *sknode;
Eric Dumazet95c96172012-04-15 05:58:06 +0000545 unsigned int h;
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000546
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000547 for (h = 0; h < PN_HASHSIZE; h++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800548 sk_for_each_rcu(sknode, hlist) {
Rémi Denis-Courmont6b0d07b2009-11-09 02:17:01 +0000549 if (!net_eq(net, sock_net(sknode)))
550 continue;
551 if (!pos)
552 return sknode;
553 pos--;
554 }
555 hlist++;
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000556 }
557 return NULL;
558}
559
560static struct sock *pn_sock_get_next(struct seq_file *seq, struct sock *sk)
561{
562 struct net *net = seq_file_net(seq);
563
564 do
565 sk = sk_next(sk);
566 while (sk && !net_eq(net, sock_net(sk)));
567
568 return sk;
569}
570
571static void *pn_sock_seq_start(struct seq_file *seq, loff_t *pos)
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000572 __acquires(rcu)
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000573{
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000574 rcu_read_lock();
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000575 return *pos ? pn_sock_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
576}
577
578static void *pn_sock_seq_next(struct seq_file *seq, void *v, loff_t *pos)
579{
580 struct sock *sk;
581
582 if (v == SEQ_START_TOKEN)
583 sk = pn_sock_get_idx(seq, 0);
584 else
585 sk = pn_sock_get_next(seq, v);
586 (*pos)++;
587 return sk;
588}
589
590static void pn_sock_seq_stop(struct seq_file *seq, void *v)
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000591 __releases(rcu)
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000592{
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000593 rcu_read_unlock();
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000594}
595
596static int pn_sock_seq_show(struct seq_file *seq, void *v)
597{
Tetsuo Handa652586d2013-11-14 14:31:57 -0800598 seq_setwidth(seq, 127);
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000599 if (v == SEQ_START_TOKEN)
Tetsuo Handa652586d2013-11-14 14:31:57 -0800600 seq_puts(seq, "pt loc rem rs st tx_queue rx_queue "
601 " uid inode ref pointer drops");
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000602 else {
603 struct sock *sk = v;
604 struct pn_sock *pn = pn_sk(sk);
605
606 seq_printf(seq, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu "
Tetsuo Handa652586d2013-11-14 14:31:57 -0800607 "%d %pK %d",
Rémi Denis-Courmonta8059512011-02-24 23:14:57 +0000608 sk->sk_protocol, pn->sobject, pn->dobject,
609 pn->resource, sk->sk_state,
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000610 sk_wmem_alloc_get(sk), sk_rmem_alloc_get(sk),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -0600611 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
612 sock_i_ino(sk),
Reshetova, Elena41c6d652017-06-30 13:08:01 +0300613 refcount_read(&sk->sk_refcnt), sk,
Tetsuo Handa652586d2013-11-14 14:31:57 -0800614 atomic_read(&sk->sk_drops));
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000615 }
Tetsuo Handa652586d2013-11-14 14:31:57 -0800616 seq_pad(seq, '\n');
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000617 return 0;
618}
619
Christoph Hellwigc3506372018-04-10 19:42:55 +0200620const struct seq_operations pn_sock_seq_ops = {
Rémi Denis-Courmontc1dc13e2009-07-21 01:57:57 +0000621 .start = pn_sock_seq_start,
622 .next = pn_sock_seq_next,
623 .stop = pn_sock_seq_stop,
624 .show = pn_sock_seq_show,
625};
Randy Dunlapae6e2ae2009-08-17 10:35:49 +0000626#endif
Rémi Denis-Courmont4e3d16c2010-09-15 12:30:11 +0000627
628static struct {
629 struct sock *sk[256];
630} pnres;
631
632/*
633 * Find and hold socket based on resource.
634 */
635struct sock *pn_find_sock_by_res(struct net *net, u8 res)
636{
637 struct sock *sk;
638
639 if (!net_eq(net, &init_net))
640 return NULL;
641
642 rcu_read_lock();
643 sk = rcu_dereference(pnres.sk[res]);
644 if (sk)
645 sock_hold(sk);
646 rcu_read_unlock();
647 return sk;
648}
649
650static DEFINE_MUTEX(resource_mutex);
651
652int pn_sock_bind_res(struct sock *sk, u8 res)
653{
654 int ret = -EADDRINUSE;
655
656 if (!net_eq(sock_net(sk), &init_net))
657 return -ENOIOCTLCMD;
658 if (!capable(CAP_SYS_ADMIN))
659 return -EPERM;
660 if (pn_socket_autobind(sk->sk_socket))
661 return -EAGAIN;
662
663 mutex_lock(&resource_mutex);
664 if (pnres.sk[res] == NULL) {
665 sock_hold(sk);
Eric Dumazetcf778b02012-01-12 04:41:32 +0000666 rcu_assign_pointer(pnres.sk[res], sk);
Rémi Denis-Courmont4e3d16c2010-09-15 12:30:11 +0000667 ret = 0;
668 }
669 mutex_unlock(&resource_mutex);
670 return ret;
671}
672
673int pn_sock_unbind_res(struct sock *sk, u8 res)
674{
675 int ret = -ENOENT;
676
677 if (!capable(CAP_SYS_ADMIN))
678 return -EPERM;
679
680 mutex_lock(&resource_mutex);
681 if (pnres.sk[res] == sk) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000682 RCU_INIT_POINTER(pnres.sk[res], NULL);
Rémi Denis-Courmont4e3d16c2010-09-15 12:30:11 +0000683 ret = 0;
684 }
685 mutex_unlock(&resource_mutex);
686
687 if (ret == 0) {
688 synchronize_rcu();
689 sock_put(sk);
690 }
691 return ret;
692}
693
694void pn_sock_unbind_all_res(struct sock *sk)
695{
Eric Dumazet95c96172012-04-15 05:58:06 +0000696 unsigned int res, match = 0;
Rémi Denis-Courmont4e3d16c2010-09-15 12:30:11 +0000697
698 mutex_lock(&resource_mutex);
699 for (res = 0; res < 256; res++) {
700 if (pnres.sk[res] == sk) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000701 RCU_INIT_POINTER(pnres.sk[res], NULL);
Rémi Denis-Courmont4e3d16c2010-09-15 12:30:11 +0000702 match++;
703 }
704 }
705 mutex_unlock(&resource_mutex);
706
Rémi Denis-Courmont4e3d16c2010-09-15 12:30:11 +0000707 while (match > 0) {
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000708 __sock_put(sk);
Rémi Denis-Courmont4e3d16c2010-09-15 12:30:11 +0000709 match--;
710 }
Rémi Denis-Courmont44f4d5a2011-04-12 23:27:36 +0000711 /* Caller is responsible for RCU sync before final sock_put() */
Rémi Denis-Courmont4e3d16c2010-09-15 12:30:11 +0000712}
Rémi Denis-Courmont507215f2010-09-15 12:30:14 +0000713
714#ifdef CONFIG_PROC_FS
715static struct sock **pn_res_get_idx(struct seq_file *seq, loff_t pos)
716{
717 struct net *net = seq_file_net(seq);
Eric Dumazet95c96172012-04-15 05:58:06 +0000718 unsigned int i;
Rémi Denis-Courmont507215f2010-09-15 12:30:14 +0000719
720 if (!net_eq(net, &init_net))
721 return NULL;
722
723 for (i = 0; i < 256; i++) {
724 if (pnres.sk[i] == NULL)
725 continue;
726 if (!pos)
727 return pnres.sk + i;
728 pos--;
729 }
730 return NULL;
731}
732
733static struct sock **pn_res_get_next(struct seq_file *seq, struct sock **sk)
734{
735 struct net *net = seq_file_net(seq);
Eric Dumazet95c96172012-04-15 05:58:06 +0000736 unsigned int i;
Rémi Denis-Courmont507215f2010-09-15 12:30:14 +0000737
738 BUG_ON(!net_eq(net, &init_net));
739
740 for (i = (sk - pnres.sk) + 1; i < 256; i++)
741 if (pnres.sk[i])
742 return pnres.sk + i;
743 return NULL;
744}
745
746static void *pn_res_seq_start(struct seq_file *seq, loff_t *pos)
747 __acquires(resource_mutex)
748{
749 mutex_lock(&resource_mutex);
750 return *pos ? pn_res_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
751}
752
753static void *pn_res_seq_next(struct seq_file *seq, void *v, loff_t *pos)
754{
755 struct sock **sk;
756
757 if (v == SEQ_START_TOKEN)
758 sk = pn_res_get_idx(seq, 0);
759 else
760 sk = pn_res_get_next(seq, v);
761 (*pos)++;
762 return sk;
763}
764
765static void pn_res_seq_stop(struct seq_file *seq, void *v)
766 __releases(resource_mutex)
767{
768 mutex_unlock(&resource_mutex);
769}
770
771static int pn_res_seq_show(struct seq_file *seq, void *v)
772{
Tetsuo Handa652586d2013-11-14 14:31:57 -0800773 seq_setwidth(seq, 63);
Rémi Denis-Courmont507215f2010-09-15 12:30:14 +0000774 if (v == SEQ_START_TOKEN)
Tetsuo Handa652586d2013-11-14 14:31:57 -0800775 seq_puts(seq, "rs uid inode");
Rémi Denis-Courmont507215f2010-09-15 12:30:14 +0000776 else {
777 struct sock **psk = v;
778 struct sock *sk = *psk;
779
Tetsuo Handa652586d2013-11-14 14:31:57 -0800780 seq_printf(seq, "%02X %5u %lu",
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -0600781 (int) (psk - pnres.sk),
782 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
Tetsuo Handa652586d2013-11-14 14:31:57 -0800783 sock_i_ino(sk));
Rémi Denis-Courmont507215f2010-09-15 12:30:14 +0000784 }
Tetsuo Handa652586d2013-11-14 14:31:57 -0800785 seq_pad(seq, '\n');
Rémi Denis-Courmont507215f2010-09-15 12:30:14 +0000786 return 0;
787}
788
Christoph Hellwigc3506372018-04-10 19:42:55 +0200789const struct seq_operations pn_res_seq_ops = {
Rémi Denis-Courmont507215f2010-09-15 12:30:14 +0000790 .start = pn_res_seq_start,
791 .next = pn_res_seq_next,
792 .stop = pn_res_seq_stop,
793 .show = pn_res_seq_show,
794};
Rémi Denis-Courmont507215f2010-09-15 12:30:14 +0000795#endif