Staging: fix wlan-ng printk format warning
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / phonet / socket.c
blob69c8b826a0cef427e3c1e00c1216af5a014f4c32
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 #define PN_HASHSIZE 16
49 #define PN_HASHMASK (PN_HASHSIZE-1)
52 static struct {
53 struct hlist_head hlist[PN_HASHSIZE];
54 spinlock_t lock;
55 } pnsocks;
57 void __init pn_sock_init(void)
59 unsigned i;
61 for (i = 0; i < PN_HASHSIZE; i++)
62 INIT_HLIST_HEAD(pnsocks.hlist + i);
63 spin_lock_init(&pnsocks.lock);
66 static struct hlist_head *pn_hash_list(u16 obj)
68 return pnsocks.hlist + (obj & PN_HASHMASK);
72 * Find address based on socket address, match only certain fields.
73 * Also grab sock if it was found. Remember to sock_put it later.
75 struct sock *pn_find_sock_by_sa(struct net *net, const struct sockaddr_pn *spn)
77 struct hlist_node *node;
78 struct sock *sknode;
79 struct sock *rval = NULL;
80 u16 obj = pn_sockaddr_get_object(spn);
81 u8 res = spn->spn_resource;
82 struct hlist_head *hlist = pn_hash_list(obj);
84 spin_lock_bh(&pnsocks.lock);
86 sk_for_each(sknode, node, hlist) {
87 struct pn_sock *pn = pn_sk(sknode);
88 BUG_ON(!pn->sobject); /* unbound socket */
90 if (!net_eq(sock_net(sknode), net))
91 continue;
92 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;
101 if (pn_addr(pn->sobject) &&
102 pn_addr(pn->sobject) != pn_addr(obj))
103 continue;
105 rval = sknode;
106 sock_hold(sknode);
107 break;
110 spin_unlock_bh(&pnsocks.lock);
112 return rval;
115 /* Deliver a broadcast packet (only in bottom-half) */
116 void pn_deliver_sock_broadcast(struct net *net, struct sk_buff *skb)
118 struct hlist_head *hlist = pnsocks.hlist;
119 unsigned h;
121 spin_lock(&pnsocks.lock);
122 for (h = 0; h < PN_HASHSIZE; h++) {
123 struct hlist_node *node;
124 struct sock *sknode;
126 sk_for_each(sknode, node, hlist) {
127 struct sk_buff *clone;
129 if (!net_eq(sock_net(sknode), net))
130 continue;
131 if (!sock_flag(sknode, SOCK_BROADCAST))
132 continue;
134 clone = skb_clone(skb, GFP_ATOMIC);
135 if (clone) {
136 sock_hold(sknode);
137 sk_receive_skb(sknode, clone, 0);
140 hlist++;
142 spin_unlock(&pnsocks.lock);
145 void pn_sock_hash(struct sock *sk)
147 struct hlist_head *hlist = pn_hash_list(pn_sk(sk)->sobject);
149 spin_lock_bh(&pnsocks.lock);
150 sk_add_node(sk, hlist);
151 spin_unlock_bh(&pnsocks.lock);
153 EXPORT_SYMBOL(pn_sock_hash);
155 void pn_sock_unhash(struct sock *sk)
157 spin_lock_bh(&pnsocks.lock);
158 sk_del_node_init(sk);
159 spin_unlock_bh(&pnsocks.lock);
161 EXPORT_SYMBOL(pn_sock_unhash);
163 static DEFINE_MUTEX(port_mutex);
165 static int pn_socket_bind(struct socket *sock, struct sockaddr *addr, int len)
167 struct sock *sk = sock->sk;
168 struct pn_sock *pn = pn_sk(sk);
169 struct sockaddr_pn *spn = (struct sockaddr_pn *)addr;
170 int err;
171 u16 handle;
172 u8 saddr;
174 if (sk->sk_prot->bind)
175 return sk->sk_prot->bind(sk, addr, len);
177 if (len < sizeof(struct sockaddr_pn))
178 return -EINVAL;
179 if (spn->spn_family != AF_PHONET)
180 return -EAFNOSUPPORT;
182 handle = pn_sockaddr_get_object((struct sockaddr_pn *)addr);
183 saddr = pn_addr(handle);
184 if (saddr && phonet_address_lookup(sock_net(sk), saddr))
185 return -EADDRNOTAVAIL;
187 lock_sock(sk);
188 if (sk->sk_state != TCP_CLOSE || pn_port(pn->sobject)) {
189 err = -EINVAL; /* attempt to rebind */
190 goto out;
192 WARN_ON(sk_hashed(sk));
193 mutex_lock(&port_mutex);
194 err = sk->sk_prot->get_port(sk, pn_port(handle));
195 if (err)
196 goto out_port;
198 /* get_port() sets the port, bind() sets the address if applicable */
199 pn->sobject = pn_object(saddr, pn_port(pn->sobject));
200 pn->resource = spn->spn_resource;
202 /* Enable RX on the socket */
203 sk->sk_prot->hash(sk);
204 out_port:
205 mutex_unlock(&port_mutex);
206 out:
207 release_sock(sk);
208 return err;
211 static int pn_socket_autobind(struct socket *sock)
213 struct sockaddr_pn sa;
214 int err;
216 memset(&sa, 0, sizeof(sa));
217 sa.spn_family = AF_PHONET;
218 err = pn_socket_bind(sock, (struct sockaddr *)&sa,
219 sizeof(struct sockaddr_pn));
220 if (err != -EINVAL)
221 return err;
222 BUG_ON(!pn_port(pn_sk(sock->sk)->sobject));
223 return 0; /* socket was already bound */
226 static int pn_socket_accept(struct socket *sock, struct socket *newsock,
227 int flags)
229 struct sock *sk = sock->sk;
230 struct sock *newsk;
231 int err;
233 newsk = sk->sk_prot->accept(sk, flags, &err);
234 if (!newsk)
235 return err;
237 lock_sock(newsk);
238 sock_graft(newsk, newsock);
239 newsock->state = SS_CONNECTED;
240 release_sock(newsk);
241 return 0;
244 static int pn_socket_getname(struct socket *sock, struct sockaddr *addr,
245 int *sockaddr_len, int peer)
247 struct sock *sk = sock->sk;
248 struct pn_sock *pn = pn_sk(sk);
250 memset(addr, 0, sizeof(struct sockaddr_pn));
251 addr->sa_family = AF_PHONET;
252 if (!peer) /* Race with bind() here is userland's problem. */
253 pn_sockaddr_set_object((struct sockaddr_pn *)addr,
254 pn->sobject);
256 *sockaddr_len = sizeof(struct sockaddr_pn);
257 return 0;
260 static unsigned int pn_socket_poll(struct file *file, struct socket *sock,
261 poll_table *wait)
263 struct sock *sk = sock->sk;
264 struct pep_sock *pn = pep_sk(sk);
265 unsigned int mask = 0;
267 poll_wait(file, &sock->wait, wait);
269 switch (sk->sk_state) {
270 case TCP_LISTEN:
271 return hlist_empty(&pn->ackq) ? 0 : POLLIN;
272 case TCP_CLOSE:
273 return POLLERR;
276 if (!skb_queue_empty(&sk->sk_receive_queue))
277 mask |= POLLIN | POLLRDNORM;
278 if (!skb_queue_empty(&pn->ctrlreq_queue))
279 mask |= POLLPRI;
280 if (!mask && sk->sk_state == TCP_CLOSE_WAIT)
281 return POLLHUP;
283 if (sk->sk_state == TCP_ESTABLISHED && atomic_read(&pn->tx_credits))
284 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
286 return mask;
289 static int pn_socket_ioctl(struct socket *sock, unsigned int cmd,
290 unsigned long arg)
292 struct sock *sk = sock->sk;
293 struct pn_sock *pn = pn_sk(sk);
295 if (cmd == SIOCPNGETOBJECT) {
296 struct net_device *dev;
297 u16 handle;
298 u8 saddr;
300 if (get_user(handle, (__u16 __user *)arg))
301 return -EFAULT;
303 lock_sock(sk);
304 if (sk->sk_bound_dev_if)
305 dev = dev_get_by_index(sock_net(sk),
306 sk->sk_bound_dev_if);
307 else
308 dev = phonet_device_get(sock_net(sk));
309 if (dev && (dev->flags & IFF_UP))
310 saddr = phonet_address_get(dev, pn_addr(handle));
311 else
312 saddr = PN_NO_ADDR;
313 release_sock(sk);
315 if (dev)
316 dev_put(dev);
317 if (saddr == PN_NO_ADDR)
318 return -EHOSTUNREACH;
320 handle = pn_object(saddr, pn_port(pn->sobject));
321 return put_user(handle, (__u16 __user *)arg);
324 return sk->sk_prot->ioctl(sk, cmd, arg);
327 static int pn_socket_listen(struct socket *sock, int backlog)
329 struct sock *sk = sock->sk;
330 int err = 0;
332 if (sock->state != SS_UNCONNECTED)
333 return -EINVAL;
334 if (pn_socket_autobind(sock))
335 return -ENOBUFS;
337 lock_sock(sk);
338 if (sk->sk_state != TCP_CLOSE) {
339 err = -EINVAL;
340 goto out;
343 sk->sk_state = TCP_LISTEN;
344 sk->sk_ack_backlog = 0;
345 sk->sk_max_ack_backlog = backlog;
346 out:
347 release_sock(sk);
348 return err;
351 static int pn_socket_sendmsg(struct kiocb *iocb, struct socket *sock,
352 struct msghdr *m, size_t total_len)
354 struct sock *sk = sock->sk;
356 if (pn_socket_autobind(sock))
357 return -EAGAIN;
359 return sk->sk_prot->sendmsg(iocb, sk, m, total_len);
362 const struct proto_ops phonet_dgram_ops = {
363 .family = AF_PHONET,
364 .owner = THIS_MODULE,
365 .release = pn_socket_release,
366 .bind = pn_socket_bind,
367 .connect = sock_no_connect,
368 .socketpair = sock_no_socketpair,
369 .accept = sock_no_accept,
370 .getname = pn_socket_getname,
371 .poll = datagram_poll,
372 .ioctl = pn_socket_ioctl,
373 .listen = sock_no_listen,
374 .shutdown = sock_no_shutdown,
375 .setsockopt = sock_no_setsockopt,
376 .getsockopt = sock_no_getsockopt,
377 #ifdef CONFIG_COMPAT
378 .compat_setsockopt = sock_no_setsockopt,
379 .compat_getsockopt = sock_no_getsockopt,
380 #endif
381 .sendmsg = pn_socket_sendmsg,
382 .recvmsg = sock_common_recvmsg,
383 .mmap = sock_no_mmap,
384 .sendpage = sock_no_sendpage,
387 const struct proto_ops phonet_stream_ops = {
388 .family = AF_PHONET,
389 .owner = THIS_MODULE,
390 .release = pn_socket_release,
391 .bind = pn_socket_bind,
392 .connect = sock_no_connect,
393 .socketpair = sock_no_socketpair,
394 .accept = pn_socket_accept,
395 .getname = pn_socket_getname,
396 .poll = pn_socket_poll,
397 .ioctl = pn_socket_ioctl,
398 .listen = pn_socket_listen,
399 .shutdown = sock_no_shutdown,
400 .setsockopt = sock_common_setsockopt,
401 .getsockopt = sock_common_getsockopt,
402 #ifdef CONFIG_COMPAT
403 .compat_setsockopt = compat_sock_common_setsockopt,
404 .compat_getsockopt = compat_sock_common_getsockopt,
405 #endif
406 .sendmsg = pn_socket_sendmsg,
407 .recvmsg = sock_common_recvmsg,
408 .mmap = sock_no_mmap,
409 .sendpage = sock_no_sendpage,
411 EXPORT_SYMBOL(phonet_stream_ops);
413 /* allocate port for a socket */
414 int pn_sock_get_port(struct sock *sk, unsigned short sport)
416 static int port_cur;
417 struct net *net = sock_net(sk);
418 struct pn_sock *pn = pn_sk(sk);
419 struct sockaddr_pn try_sa;
420 struct sock *tmpsk;
422 memset(&try_sa, 0, sizeof(struct sockaddr_pn));
423 try_sa.spn_family = AF_PHONET;
424 WARN_ON(!mutex_is_locked(&port_mutex));
425 if (!sport) {
426 /* search free port */
427 int port, pmin, pmax;
429 phonet_get_local_port_range(&pmin, &pmax);
430 for (port = pmin; port <= pmax; port++) {
431 port_cur++;
432 if (port_cur < pmin || port_cur > pmax)
433 port_cur = pmin;
435 pn_sockaddr_set_port(&try_sa, port_cur);
436 tmpsk = pn_find_sock_by_sa(net, &try_sa);
437 if (tmpsk == NULL) {
438 sport = port_cur;
439 goto found;
440 } else
441 sock_put(tmpsk);
443 } else {
444 /* try to find specific port */
445 pn_sockaddr_set_port(&try_sa, sport);
446 tmpsk = pn_find_sock_by_sa(net, &try_sa);
447 if (tmpsk == NULL)
448 /* No sock there! We can use that port... */
449 goto found;
450 else
451 sock_put(tmpsk);
453 /* the port must be in use already */
454 return -EADDRINUSE;
456 found:
457 pn->sobject = pn_object(pn_addr(pn->sobject), sport);
458 return 0;
460 EXPORT_SYMBOL(pn_sock_get_port);
462 #ifdef CONFIG_PROC_FS
463 static struct sock *pn_sock_get_idx(struct seq_file *seq, loff_t pos)
465 struct net *net = seq_file_net(seq);
466 struct hlist_head *hlist = pnsocks.hlist;
467 struct hlist_node *node;
468 struct sock *sknode;
469 unsigned h;
471 for (h = 0; h < PN_HASHSIZE; h++) {
472 sk_for_each(sknode, node, hlist) {
473 if (!net_eq(net, sock_net(sknode)))
474 continue;
475 if (!pos)
476 return sknode;
477 pos--;
479 hlist++;
481 return NULL;
484 static struct sock *pn_sock_get_next(struct seq_file *seq, struct sock *sk)
486 struct net *net = seq_file_net(seq);
489 sk = sk_next(sk);
490 while (sk && !net_eq(net, sock_net(sk)));
492 return sk;
495 static void *pn_sock_seq_start(struct seq_file *seq, loff_t *pos)
496 __acquires(pnsocks.lock)
498 spin_lock_bh(&pnsocks.lock);
499 return *pos ? pn_sock_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
502 static void *pn_sock_seq_next(struct seq_file *seq, void *v, loff_t *pos)
504 struct sock *sk;
506 if (v == SEQ_START_TOKEN)
507 sk = pn_sock_get_idx(seq, 0);
508 else
509 sk = pn_sock_get_next(seq, v);
510 (*pos)++;
511 return sk;
514 static void pn_sock_seq_stop(struct seq_file *seq, void *v)
515 __releases(pnsocks.lock)
517 spin_unlock_bh(&pnsocks.lock);
520 static int pn_sock_seq_show(struct seq_file *seq, void *v)
522 int len;
524 if (v == SEQ_START_TOKEN)
525 seq_printf(seq, "%s%n", "pt loc rem rs st tx_queue rx_queue "
526 " uid inode ref pointer drops", &len);
527 else {
528 struct sock *sk = v;
529 struct pn_sock *pn = pn_sk(sk);
531 seq_printf(seq, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu "
532 "%d %p %d%n",
533 sk->sk_protocol, pn->sobject, 0, pn->resource,
534 sk->sk_state,
535 sk_wmem_alloc_get(sk), sk_rmem_alloc_get(sk),
536 sock_i_uid(sk), sock_i_ino(sk),
537 atomic_read(&sk->sk_refcnt), sk,
538 atomic_read(&sk->sk_drops), &len);
540 seq_printf(seq, "%*s\n", 127 - len, "");
541 return 0;
544 static const struct seq_operations pn_sock_seq_ops = {
545 .start = pn_sock_seq_start,
546 .next = pn_sock_seq_next,
547 .stop = pn_sock_seq_stop,
548 .show = pn_sock_seq_show,
551 static int pn_sock_open(struct inode *inode, struct file *file)
553 return seq_open_net(inode, file, &pn_sock_seq_ops,
554 sizeof(struct seq_net_private));
557 const struct file_operations pn_sock_seq_fops = {
558 .owner = THIS_MODULE,
559 .open = pn_sock_open,
560 .read = seq_read,
561 .llseek = seq_lseek,
562 .release = seq_release_net,
564 #endif