OMAP3 SRF: OMAP PM srf implementation
[linux-ginger.git] / net / phonet / socket.c
blobaa5b5a972bff7a3e40700b8969e3b145bca428eb
1 /*
2 * File: socket.c
4 * Phonet sockets
6 * Copyright (C) 2008 Nokia Corporation.
8 * Contact: Remi Denis-Courmont <remi.denis-courmont@nokia.com>
9 * Original author: Sakari Ailus <sakari.ailus@nokia.com>
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.
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.
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
26 #include <linux/kernel.h>
27 #include <linux/net.h>
28 #include <linux/poll.h>
29 #include <net/sock.h>
30 #include <net/tcp_states.h>
32 #include <linux/phonet.h>
33 #include <net/phonet/phonet.h>
34 #include <net/phonet/pep.h>
35 #include <net/phonet/pn_dev.h>
37 static int pn_socket_release(struct socket *sock)
39 struct sock *sk = sock->sk;
41 if (sk) {
42 sock->sk = NULL;
43 sk->sk_prot->close(sk, 0);
45 return 0;
48 static struct {
49 struct hlist_head hlist;
50 spinlock_t lock;
51 } pnsocks = {
52 .hlist = HLIST_HEAD_INIT,
53 .lock = __SPIN_LOCK_UNLOCKED(pnsocks.lock),
57 * Find address based on socket address, match only certain fields.
58 * Also grab sock if it was found. Remember to sock_put it later.
60 struct sock *pn_find_sock_by_sa(struct net *net, const struct sockaddr_pn *spn)
62 struct hlist_node *node;
63 struct sock *sknode;
64 struct sock *rval = NULL;
65 u16 obj = pn_sockaddr_get_object(spn);
66 u8 res = spn->spn_resource;
68 spin_lock_bh(&pnsocks.lock);
70 sk_for_each(sknode, node, &pnsocks.hlist) {
71 struct pn_sock *pn = pn_sk(sknode);
72 BUG_ON(!pn->sobject); /* unbound socket */
74 if (!net_eq(sock_net(sknode), net))
75 continue;
76 if (pn_port(obj)) {
77 /* Look up socket by port */
78 if (pn_port(pn->sobject) != pn_port(obj))
79 continue;
80 } else {
81 /* If port is zero, look up by resource */
82 if (pn->resource != res)
83 continue;
85 if (pn_addr(pn->sobject)
86 && pn_addr(pn->sobject) != pn_addr(obj))
87 continue;
89 rval = sknode;
90 sock_hold(sknode);
91 break;
94 spin_unlock_bh(&pnsocks.lock);
96 return rval;
100 void pn_sock_hash(struct sock *sk)
102 spin_lock_bh(&pnsocks.lock);
103 sk_add_node(sk, &pnsocks.hlist);
104 spin_unlock_bh(&pnsocks.lock);
106 EXPORT_SYMBOL(pn_sock_hash);
108 void pn_sock_unhash(struct sock *sk)
110 spin_lock_bh(&pnsocks.lock);
111 sk_del_node_init(sk);
112 spin_unlock_bh(&pnsocks.lock);
114 EXPORT_SYMBOL(pn_sock_unhash);
116 static DEFINE_MUTEX(port_mutex);
118 static int pn_socket_bind(struct socket *sock, struct sockaddr *addr, int len)
120 struct sock *sk = sock->sk;
121 struct pn_sock *pn = pn_sk(sk);
122 struct sockaddr_pn *spn = (struct sockaddr_pn *)addr;
123 int err;
124 u16 handle;
125 u8 saddr;
127 if (sk->sk_prot->bind)
128 return sk->sk_prot->bind(sk, addr, len);
130 if (len < sizeof(struct sockaddr_pn))
131 return -EINVAL;
132 if (spn->spn_family != AF_PHONET)
133 return -EAFNOSUPPORT;
135 handle = pn_sockaddr_get_object((struct sockaddr_pn *)addr);
136 saddr = pn_addr(handle);
137 if (saddr && phonet_address_lookup(sock_net(sk), saddr))
138 return -EADDRNOTAVAIL;
140 lock_sock(sk);
141 if (sk->sk_state != TCP_CLOSE || pn_port(pn->sobject)) {
142 err = -EINVAL; /* attempt to rebind */
143 goto out;
145 WARN_ON(sk_hashed(sk));
146 mutex_lock(&port_mutex);
147 err = sk->sk_prot->get_port(sk, pn_port(handle));
148 if (err)
149 goto out_port;
151 /* get_port() sets the port, bind() sets the address if applicable */
152 pn->sobject = pn_object(saddr, pn_port(pn->sobject));
153 pn->resource = spn->spn_resource;
155 /* Enable RX on the socket */
156 sk->sk_prot->hash(sk);
157 out_port:
158 mutex_unlock(&port_mutex);
159 out:
160 release_sock(sk);
161 return err;
164 static int pn_socket_autobind(struct socket *sock)
166 struct sockaddr_pn sa;
167 int err;
169 memset(&sa, 0, sizeof(sa));
170 sa.spn_family = AF_PHONET;
171 err = pn_socket_bind(sock, (struct sockaddr *)&sa,
172 sizeof(struct sockaddr_pn));
173 if (err != -EINVAL)
174 return err;
175 BUG_ON(!pn_port(pn_sk(sock->sk)->sobject));
176 return 0; /* socket was already bound */
179 static int pn_socket_accept(struct socket *sock, struct socket *newsock,
180 int flags)
182 struct sock *sk = sock->sk;
183 struct sock *newsk;
184 int err;
186 newsk = sk->sk_prot->accept(sk, flags, &err);
187 if (!newsk)
188 return err;
190 lock_sock(newsk);
191 sock_graft(newsk, newsock);
192 newsock->state = SS_CONNECTED;
193 release_sock(newsk);
194 return 0;
197 static int pn_socket_getname(struct socket *sock, struct sockaddr *addr,
198 int *sockaddr_len, int peer)
200 struct sock *sk = sock->sk;
201 struct pn_sock *pn = pn_sk(sk);
203 memset(addr, 0, sizeof(struct sockaddr_pn));
204 addr->sa_family = AF_PHONET;
205 if (!peer) /* Race with bind() here is userland's problem. */
206 pn_sockaddr_set_object((struct sockaddr_pn *)addr,
207 pn->sobject);
209 *sockaddr_len = sizeof(struct sockaddr_pn);
210 return 0;
213 static unsigned int pn_socket_poll(struct file *file, struct socket *sock,
214 poll_table *wait)
216 struct sock *sk = sock->sk;
217 struct pep_sock *pn = pep_sk(sk);
218 unsigned int mask = 0;
220 poll_wait(file, &sock->wait, wait);
222 switch (sk->sk_state) {
223 case TCP_LISTEN:
224 return hlist_empty(&pn->ackq) ? 0 : POLLIN;
225 case TCP_CLOSE:
226 return POLLERR;
229 if (!skb_queue_empty(&sk->sk_receive_queue))
230 mask |= POLLIN | POLLRDNORM;
231 if (!skb_queue_empty(&pn->ctrlreq_queue))
232 mask |= POLLPRI;
233 if (!mask && sk->sk_state == TCP_CLOSE_WAIT)
234 return POLLHUP;
236 if (sk->sk_state == TCP_ESTABLISHED && atomic_read(&pn->tx_credits))
237 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
239 return mask;
242 static int pn_socket_ioctl(struct socket *sock, unsigned int cmd,
243 unsigned long arg)
245 struct sock *sk = sock->sk;
246 struct pn_sock *pn = pn_sk(sk);
248 if (cmd == SIOCPNGETOBJECT) {
249 struct net_device *dev;
250 u16 handle;
251 u8 saddr;
253 if (get_user(handle, (__u16 __user *)arg))
254 return -EFAULT;
256 lock_sock(sk);
257 if (sk->sk_bound_dev_if)
258 dev = dev_get_by_index(sock_net(sk),
259 sk->sk_bound_dev_if);
260 else
261 dev = phonet_device_get(sock_net(sk));
262 if (dev && (dev->flags & IFF_UP))
263 saddr = phonet_address_get(dev, pn_addr(handle));
264 else
265 saddr = PN_NO_ADDR;
266 release_sock(sk);
268 if (dev)
269 dev_put(dev);
270 if (saddr == PN_NO_ADDR)
271 return -EHOSTUNREACH;
273 handle = pn_object(saddr, pn_port(pn->sobject));
274 return put_user(handle, (__u16 __user *)arg);
277 return sk->sk_prot->ioctl(sk, cmd, arg);
280 static int pn_socket_listen(struct socket *sock, int backlog)
282 struct sock *sk = sock->sk;
283 int err = 0;
285 if (sock->state != SS_UNCONNECTED)
286 return -EINVAL;
287 if (pn_socket_autobind(sock))
288 return -ENOBUFS;
290 lock_sock(sk);
291 if (sk->sk_state != TCP_CLOSE) {
292 err = -EINVAL;
293 goto out;
296 sk->sk_state = TCP_LISTEN;
297 sk->sk_ack_backlog = 0;
298 sk->sk_max_ack_backlog = backlog;
299 out:
300 release_sock(sk);
301 return err;
304 static int pn_socket_sendmsg(struct kiocb *iocb, struct socket *sock,
305 struct msghdr *m, size_t total_len)
307 struct sock *sk = sock->sk;
309 if (pn_socket_autobind(sock))
310 return -EAGAIN;
312 return sk->sk_prot->sendmsg(iocb, sk, m, total_len);
315 const struct proto_ops phonet_dgram_ops = {
316 .family = AF_PHONET,
317 .owner = THIS_MODULE,
318 .release = pn_socket_release,
319 .bind = pn_socket_bind,
320 .connect = sock_no_connect,
321 .socketpair = sock_no_socketpair,
322 .accept = sock_no_accept,
323 .getname = pn_socket_getname,
324 .poll = datagram_poll,
325 .ioctl = pn_socket_ioctl,
326 .listen = sock_no_listen,
327 .shutdown = sock_no_shutdown,
328 .setsockopt = sock_no_setsockopt,
329 .getsockopt = sock_no_getsockopt,
330 #ifdef CONFIG_COMPAT
331 .compat_setsockopt = sock_no_setsockopt,
332 .compat_getsockopt = sock_no_getsockopt,
333 #endif
334 .sendmsg = pn_socket_sendmsg,
335 .recvmsg = sock_common_recvmsg,
336 .mmap = sock_no_mmap,
337 .sendpage = sock_no_sendpage,
340 const struct proto_ops phonet_stream_ops = {
341 .family = AF_PHONET,
342 .owner = THIS_MODULE,
343 .release = pn_socket_release,
344 .bind = pn_socket_bind,
345 .connect = sock_no_connect,
346 .socketpair = sock_no_socketpair,
347 .accept = pn_socket_accept,
348 .getname = pn_socket_getname,
349 .poll = pn_socket_poll,
350 .ioctl = pn_socket_ioctl,
351 .listen = pn_socket_listen,
352 .shutdown = sock_no_shutdown,
353 .setsockopt = sock_common_setsockopt,
354 .getsockopt = sock_common_getsockopt,
355 #ifdef CONFIG_COMPAT
356 .compat_setsockopt = compat_sock_common_setsockopt,
357 .compat_getsockopt = compat_sock_common_getsockopt,
358 #endif
359 .sendmsg = pn_socket_sendmsg,
360 .recvmsg = sock_common_recvmsg,
361 .mmap = sock_no_mmap,
362 .sendpage = sock_no_sendpage,
364 EXPORT_SYMBOL(phonet_stream_ops);
366 /* allocate port for a socket */
367 int pn_sock_get_port(struct sock *sk, unsigned short sport)
369 static int port_cur;
370 struct net *net = sock_net(sk);
371 struct pn_sock *pn = pn_sk(sk);
372 struct sockaddr_pn try_sa;
373 struct sock *tmpsk;
375 memset(&try_sa, 0, sizeof(struct sockaddr_pn));
376 try_sa.spn_family = AF_PHONET;
377 WARN_ON(!mutex_is_locked(&port_mutex));
378 if (!sport) {
379 /* search free port */
380 int port, pmin, pmax;
382 phonet_get_local_port_range(&pmin, &pmax);
383 for (port = pmin; port <= pmax; port++) {
384 port_cur++;
385 if (port_cur < pmin || port_cur > pmax)
386 port_cur = pmin;
388 pn_sockaddr_set_port(&try_sa, port_cur);
389 tmpsk = pn_find_sock_by_sa(net, &try_sa);
390 if (tmpsk == NULL) {
391 sport = port_cur;
392 goto found;
393 } else
394 sock_put(tmpsk);
396 } else {
397 /* try to find specific port */
398 pn_sockaddr_set_port(&try_sa, sport);
399 tmpsk = pn_find_sock_by_sa(net, &try_sa);
400 if (tmpsk == NULL)
401 /* No sock there! We can use that port... */
402 goto found;
403 else
404 sock_put(tmpsk);
406 /* the port must be in use already */
407 return -EADDRINUSE;
409 found:
410 pn->sobject = pn_object(pn_addr(pn->sobject), sport);
411 return 0;
413 EXPORT_SYMBOL(pn_sock_get_port);
415 #ifdef CONFIG_PROC_FS
416 static struct sock *pn_sock_get_idx(struct seq_file *seq, loff_t pos)
418 struct net *net = seq_file_net(seq);
419 struct hlist_node *node;
420 struct sock *sknode;
422 sk_for_each(sknode, node, &pnsocks.hlist) {
423 if (!net_eq(net, sock_net(sknode)))
424 continue;
425 if (!pos)
426 return sknode;
427 pos--;
429 return NULL;
432 static struct sock *pn_sock_get_next(struct seq_file *seq, struct sock *sk)
434 struct net *net = seq_file_net(seq);
437 sk = sk_next(sk);
438 while (sk && !net_eq(net, sock_net(sk)));
440 return sk;
443 static void *pn_sock_seq_start(struct seq_file *seq, loff_t *pos)
444 __acquires(pnsocks.lock)
446 spin_lock_bh(&pnsocks.lock);
447 return *pos ? pn_sock_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
450 static void *pn_sock_seq_next(struct seq_file *seq, void *v, loff_t *pos)
452 struct sock *sk;
454 if (v == SEQ_START_TOKEN)
455 sk = pn_sock_get_idx(seq, 0);
456 else
457 sk = pn_sock_get_next(seq, v);
458 (*pos)++;
459 return sk;
462 static void pn_sock_seq_stop(struct seq_file *seq, void *v)
463 __releases(pnsocks.lock)
465 spin_unlock_bh(&pnsocks.lock);
468 static int pn_sock_seq_show(struct seq_file *seq, void *v)
470 int len;
472 if (v == SEQ_START_TOKEN)
473 seq_printf(seq, "%s%n", "pt loc rem rs st tx_queue rx_queue "
474 " uid inode ref pointer drops", &len);
475 else {
476 struct sock *sk = v;
477 struct pn_sock *pn = pn_sk(sk);
479 seq_printf(seq, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu "
480 "%d %p %d%n",
481 sk->sk_protocol, pn->sobject, 0, pn->resource,
482 sk->sk_state,
483 sk_wmem_alloc_get(sk), sk_rmem_alloc_get(sk),
484 sock_i_uid(sk), sock_i_ino(sk),
485 atomic_read(&sk->sk_refcnt), sk,
486 atomic_read(&sk->sk_drops), &len);
488 seq_printf(seq, "%*s\n", 127 - len, "");
489 return 0;
492 static const struct seq_operations pn_sock_seq_ops = {
493 .start = pn_sock_seq_start,
494 .next = pn_sock_seq_next,
495 .stop = pn_sock_seq_stop,
496 .show = pn_sock_seq_show,
499 static int pn_sock_open(struct inode *inode, struct file *file)
501 return seq_open_net(inode, file, &pn_sock_seq_ops,
502 sizeof(struct seq_net_private));
505 const struct file_operations pn_sock_seq_fops = {
506 .owner = THIS_MODULE,
507 .open = pn_sock_open,
508 .read = seq_read,
509 .llseek = seq_lseek,
510 .release = seq_release_net,
512 #endif