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
26 #include <linux/gfp.h>
27 #include <linux/kernel.h>
28 #include <linux/net.h>
29 #include <linux/poll.h>
31 #include <net/tcp_states.h>
33 #include <linux/phonet.h>
34 #include <linux/export.h>
35 #include <net/phonet/phonet.h>
36 #include <net/phonet/pep.h>
37 #include <net/phonet/pn_dev.h>
39 static int pn_socket_release(struct socket
*sock
)
41 struct sock
*sk
= sock
->sk
;
45 sk
->sk_prot
->close(sk
, 0);
50 #define PN_HASHSIZE 16
51 #define PN_HASHMASK (PN_HASHSIZE-1)
55 struct hlist_head hlist
[PN_HASHSIZE
];
59 void __init
pn_sock_init(void)
63 for (i
= 0; i
< PN_HASHSIZE
; i
++)
64 INIT_HLIST_HEAD(pnsocks
.hlist
+ i
);
65 mutex_init(&pnsocks
.lock
);
68 static struct hlist_head
*pn_hash_list(u16 obj
)
70 return pnsocks
.hlist
+ (obj
& PN_HASHMASK
);
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.
77 struct sock
*pn_find_sock_by_sa(struct net
*net
, const struct sockaddr_pn
*spn
)
79 struct hlist_node
*node
;
81 struct sock
*rval
= NULL
;
82 u16 obj
= pn_sockaddr_get_object(spn
);
83 u8 res
= spn
->spn_resource
;
84 struct hlist_head
*hlist
= pn_hash_list(obj
);
87 sk_for_each_rcu(sknode
, node
, hlist
) {
88 struct pn_sock
*pn
= pn_sk(sknode
);
89 BUG_ON(!pn
->sobject
); /* unbound socket */
91 if (!net_eq(sock_net(sknode
), net
))
94 /* Look up socket by port */
95 if (pn_port(pn
->sobject
) != pn_port(obj
))
98 /* If port is zero, look up by resource */
99 if (pn
->resource
!= res
)
102 if (pn_addr(pn
->sobject
) &&
103 pn_addr(pn
->sobject
) != pn_addr(obj
))
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
;
122 for (h
= 0; h
< PN_HASHSIZE
; h
++) {
123 struct hlist_node
*node
;
126 sk_for_each(sknode
, node
, hlist
) {
127 struct sk_buff
*clone
;
129 if (!net_eq(sock_net(sknode
), net
))
131 if (!sock_flag(sknode
, SOCK_BROADCAST
))
134 clone
= skb_clone(skb
, GFP_ATOMIC
);
137 sk_receive_skb(sknode
, clone
, 0);
145 void pn_sock_hash(struct sock
*sk
)
147 struct hlist_head
*hlist
= pn_hash_list(pn_sk(sk
)->sobject
);
149 mutex_lock(&pnsocks
.lock
);
150 sk_add_node_rcu(sk
, hlist
);
151 mutex_unlock(&pnsocks
.lock
);
153 EXPORT_SYMBOL(pn_sock_hash
);
155 void pn_sock_unhash(struct sock
*sk
)
157 mutex_lock(&pnsocks
.lock
);
158 sk_del_node_init_rcu(sk
);
159 mutex_unlock(&pnsocks
.lock
);
160 pn_sock_unbind_all_res(sk
);
163 EXPORT_SYMBOL(pn_sock_unhash
);
165 static DEFINE_MUTEX(port_mutex
);
167 static int pn_socket_bind(struct socket
*sock
, struct sockaddr
*addr
, int len
)
169 struct sock
*sk
= sock
->sk
;
170 struct pn_sock
*pn
= pn_sk(sk
);
171 struct sockaddr_pn
*spn
= (struct sockaddr_pn
*)addr
;
176 if (sk
->sk_prot
->bind
)
177 return sk
->sk_prot
->bind(sk
, addr
, len
);
179 if (len
< sizeof(struct sockaddr_pn
))
181 if (spn
->spn_family
!= AF_PHONET
)
182 return -EAFNOSUPPORT
;
184 handle
= pn_sockaddr_get_object((struct sockaddr_pn
*)addr
);
185 saddr
= pn_addr(handle
);
186 if (saddr
&& phonet_address_lookup(sock_net(sk
), saddr
))
187 return -EADDRNOTAVAIL
;
190 if (sk
->sk_state
!= TCP_CLOSE
|| pn_port(pn
->sobject
)) {
191 err
= -EINVAL
; /* attempt to rebind */
194 WARN_ON(sk_hashed(sk
));
195 mutex_lock(&port_mutex
);
196 err
= sk
->sk_prot
->get_port(sk
, pn_port(handle
));
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
;
204 /* Enable RX on the socket */
205 sk
->sk_prot
->hash(sk
);
207 mutex_unlock(&port_mutex
);
213 static int pn_socket_autobind(struct socket
*sock
)
215 struct sockaddr_pn sa
;
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
));
224 BUG_ON(!pn_port(pn_sk(sock
->sk
)->sobject
));
225 return 0; /* socket was already bound */
228 static int pn_socket_connect(struct socket
*sock
, struct sockaddr
*addr
,
231 struct sock
*sk
= sock
->sk
;
232 struct pn_sock
*pn
= pn_sk(sk
);
233 struct sockaddr_pn
*spn
= (struct sockaddr_pn
*)addr
;
234 struct task_struct
*tsk
= current
;
235 long timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
238 if (pn_socket_autobind(sock
))
240 if (len
< sizeof(struct sockaddr_pn
))
242 if (spn
->spn_family
!= AF_PHONET
)
243 return -EAFNOSUPPORT
;
247 switch (sock
->state
) {
249 if (sk
->sk_state
!= TCP_CLOSE
) {
262 pn
->dobject
= pn_sockaddr_get_object(spn
);
263 pn
->resource
= pn_sockaddr_get_resource(spn
);
264 sock
->state
= SS_CONNECTING
;
266 err
= sk
->sk_prot
->connect(sk
, addr
, len
);
268 sock
->state
= SS_UNCONNECTED
;
273 while (sk
->sk_state
== TCP_SYN_SENT
) {
280 if (signal_pending(tsk
)) {
281 err
= sock_intr_errno(timeo
);
285 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
,
288 timeo
= schedule_timeout(timeo
);
290 finish_wait(sk_sleep(sk
), &wait
);
293 if ((1 << sk
->sk_state
) & (TCPF_SYN_RECV
|TCPF_ESTABLISHED
))
295 else if (sk
->sk_state
== TCP_CLOSE_WAIT
)
299 sock
->state
= err
? SS_UNCONNECTED
: SS_CONNECTED
;
305 static int pn_socket_accept(struct socket
*sock
, struct socket
*newsock
,
308 struct sock
*sk
= sock
->sk
;
312 if (unlikely(sk
->sk_state
!= TCP_LISTEN
))
315 newsk
= sk
->sk_prot
->accept(sk
, flags
, &err
);
320 sock_graft(newsk
, newsock
);
321 newsock
->state
= SS_CONNECTED
;
326 static int pn_socket_getname(struct socket
*sock
, struct sockaddr
*addr
,
327 int *sockaddr_len
, int peer
)
329 struct sock
*sk
= sock
->sk
;
330 struct pn_sock
*pn
= pn_sk(sk
);
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
,
338 *sockaddr_len
= sizeof(struct sockaddr_pn
);
342 static unsigned int pn_socket_poll(struct file
*file
, struct socket
*sock
,
345 struct sock
*sk
= sock
->sk
;
346 struct pep_sock
*pn
= pep_sk(sk
);
347 unsigned int mask
= 0;
349 poll_wait(file
, sk_sleep(sk
), wait
);
351 if (sk
->sk_state
== TCP_CLOSE
)
353 if (!skb_queue_empty(&sk
->sk_receive_queue
))
354 mask
|= POLLIN
| POLLRDNORM
;
355 if (!skb_queue_empty(&pn
->ctrlreq_queue
))
357 if (!mask
&& sk
->sk_state
== TCP_CLOSE_WAIT
)
360 if (sk
->sk_state
== TCP_ESTABLISHED
&&
361 atomic_read(&sk
->sk_wmem_alloc
) < sk
->sk_sndbuf
&&
362 atomic_read(&pn
->tx_credits
))
363 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
368 static int pn_socket_ioctl(struct socket
*sock
, unsigned int cmd
,
371 struct sock
*sk
= sock
->sk
;
372 struct pn_sock
*pn
= pn_sk(sk
);
374 if (cmd
== SIOCPNGETOBJECT
) {
375 struct net_device
*dev
;
379 if (get_user(handle
, (__u16 __user
*)arg
))
383 if (sk
->sk_bound_dev_if
)
384 dev
= dev_get_by_index(sock_net(sk
),
385 sk
->sk_bound_dev_if
);
387 dev
= phonet_device_get(sock_net(sk
));
388 if (dev
&& (dev
->flags
& IFF_UP
))
389 saddr
= phonet_address_get(dev
, pn_addr(handle
));
396 if (saddr
== PN_NO_ADDR
)
397 return -EHOSTUNREACH
;
399 handle
= pn_object(saddr
, pn_port(pn
->sobject
));
400 return put_user(handle
, (__u16 __user
*)arg
);
403 return sk
->sk_prot
->ioctl(sk
, cmd
, arg
);
406 static int pn_socket_listen(struct socket
*sock
, int backlog
)
408 struct sock
*sk
= sock
->sk
;
411 if (pn_socket_autobind(sock
))
415 if (sock
->state
!= SS_UNCONNECTED
) {
420 if (sk
->sk_state
!= TCP_LISTEN
) {
421 sk
->sk_state
= TCP_LISTEN
;
422 sk
->sk_ack_backlog
= 0;
424 sk
->sk_max_ack_backlog
= backlog
;
430 static int pn_socket_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
431 struct msghdr
*m
, size_t total_len
)
433 struct sock
*sk
= sock
->sk
;
435 if (pn_socket_autobind(sock
))
438 return sk
->sk_prot
->sendmsg(iocb
, sk
, m
, total_len
);
441 const struct proto_ops phonet_dgram_ops
= {
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
,
457 .compat_setsockopt
= sock_no_setsockopt
,
458 .compat_getsockopt
= sock_no_getsockopt
,
460 .sendmsg
= pn_socket_sendmsg
,
461 .recvmsg
= sock_common_recvmsg
,
462 .mmap
= sock_no_mmap
,
463 .sendpage
= sock_no_sendpage
,
466 const struct proto_ops phonet_stream_ops
= {
468 .owner
= THIS_MODULE
,
469 .release
= pn_socket_release
,
470 .bind
= pn_socket_bind
,
471 .connect
= pn_socket_connect
,
472 .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
,
479 .setsockopt
= sock_common_setsockopt
,
480 .getsockopt
= sock_common_getsockopt
,
482 .compat_setsockopt
= compat_sock_common_setsockopt
,
483 .compat_getsockopt
= compat_sock_common_getsockopt
,
485 .sendmsg
= pn_socket_sendmsg
,
486 .recvmsg
= sock_common_recvmsg
,
487 .mmap
= sock_no_mmap
,
488 .sendpage
= sock_no_sendpage
,
490 EXPORT_SYMBOL(phonet_stream_ops
);
492 /* allocate port for a socket */
493 int pn_sock_get_port(struct sock
*sk
, unsigned short sport
)
496 struct net
*net
= sock_net(sk
);
497 struct pn_sock
*pn
= pn_sk(sk
);
498 struct sockaddr_pn try_sa
;
501 memset(&try_sa
, 0, sizeof(struct sockaddr_pn
));
502 try_sa
.spn_family
= AF_PHONET
;
503 WARN_ON(!mutex_is_locked(&port_mutex
));
505 /* search free port */
506 int port
, pmin
, pmax
;
508 phonet_get_local_port_range(&pmin
, &pmax
);
509 for (port
= pmin
; port
<= pmax
; port
++) {
511 if (port_cur
< pmin
|| port_cur
> pmax
)
514 pn_sockaddr_set_port(&try_sa
, port_cur
);
515 tmpsk
= pn_find_sock_by_sa(net
, &try_sa
);
523 /* try to find specific port */
524 pn_sockaddr_set_port(&try_sa
, sport
);
525 tmpsk
= pn_find_sock_by_sa(net
, &try_sa
);
527 /* No sock there! We can use that port... */
532 /* the port must be in use already */
536 pn
->sobject
= pn_object(pn_addr(pn
->sobject
), sport
);
539 EXPORT_SYMBOL(pn_sock_get_port
);
541 #ifdef CONFIG_PROC_FS
542 static struct sock
*pn_sock_get_idx(struct seq_file
*seq
, loff_t pos
)
544 struct net
*net
= seq_file_net(seq
);
545 struct hlist_head
*hlist
= pnsocks
.hlist
;
546 struct hlist_node
*node
;
550 for (h
= 0; h
< PN_HASHSIZE
; h
++) {
551 sk_for_each_rcu(sknode
, node
, hlist
) {
552 if (!net_eq(net
, sock_net(sknode
)))
563 static struct sock
*pn_sock_get_next(struct seq_file
*seq
, struct sock
*sk
)
565 struct net
*net
= seq_file_net(seq
);
569 while (sk
&& !net_eq(net
, sock_net(sk
)));
574 static void *pn_sock_seq_start(struct seq_file
*seq
, loff_t
*pos
)
578 return *pos
? pn_sock_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
581 static void *pn_sock_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
585 if (v
== SEQ_START_TOKEN
)
586 sk
= pn_sock_get_idx(seq
, 0);
588 sk
= pn_sock_get_next(seq
, v
);
593 static void pn_sock_seq_stop(struct seq_file
*seq
, void *v
)
599 static int pn_sock_seq_show(struct seq_file
*seq
, void *v
)
603 if (v
== SEQ_START_TOKEN
)
604 seq_printf(seq
, "%s%n", "pt loc rem rs st tx_queue rx_queue "
605 " uid inode ref pointer drops", &len
);
608 struct pn_sock
*pn
= pn_sk(sk
);
610 seq_printf(seq
, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu "
612 sk
->sk_protocol
, pn
->sobject
, pn
->dobject
,
613 pn
->resource
, sk
->sk_state
,
614 sk_wmem_alloc_get(sk
), sk_rmem_alloc_get(sk
),
615 sock_i_uid(sk
), sock_i_ino(sk
),
616 atomic_read(&sk
->sk_refcnt
), sk
,
617 atomic_read(&sk
->sk_drops
), &len
);
619 seq_printf(seq
, "%*s\n", 127 - len
, "");
623 static const struct seq_operations pn_sock_seq_ops
= {
624 .start
= pn_sock_seq_start
,
625 .next
= pn_sock_seq_next
,
626 .stop
= pn_sock_seq_stop
,
627 .show
= pn_sock_seq_show
,
630 static int pn_sock_open(struct inode
*inode
, struct file
*file
)
632 return seq_open_net(inode
, file
, &pn_sock_seq_ops
,
633 sizeof(struct seq_net_private
));
636 const struct file_operations pn_sock_seq_fops
= {
637 .owner
= THIS_MODULE
,
638 .open
= pn_sock_open
,
641 .release
= seq_release_net
,
646 struct sock
*sk
[256];
650 * Find and hold socket based on resource.
652 struct sock
*pn_find_sock_by_res(struct net
*net
, u8 res
)
656 if (!net_eq(net
, &init_net
))
660 sk
= rcu_dereference(pnres
.sk
[res
]);
667 static DEFINE_MUTEX(resource_mutex
);
669 int pn_sock_bind_res(struct sock
*sk
, u8 res
)
671 int ret
= -EADDRINUSE
;
673 if (!net_eq(sock_net(sk
), &init_net
))
675 if (!capable(CAP_SYS_ADMIN
))
677 if (pn_socket_autobind(sk
->sk_socket
))
680 mutex_lock(&resource_mutex
);
681 if (pnres
.sk
[res
] == NULL
) {
683 RCU_INIT_POINTER(pnres
.sk
[res
], sk
);
686 mutex_unlock(&resource_mutex
);
690 int pn_sock_unbind_res(struct sock
*sk
, u8 res
)
694 if (!capable(CAP_SYS_ADMIN
))
697 mutex_lock(&resource_mutex
);
698 if (pnres
.sk
[res
] == sk
) {
699 RCU_INIT_POINTER(pnres
.sk
[res
], NULL
);
702 mutex_unlock(&resource_mutex
);
711 void pn_sock_unbind_all_res(struct sock
*sk
)
713 unsigned res
, match
= 0;
715 mutex_lock(&resource_mutex
);
716 for (res
= 0; res
< 256; res
++) {
717 if (pnres
.sk
[res
] == sk
) {
718 RCU_INIT_POINTER(pnres
.sk
[res
], NULL
);
722 mutex_unlock(&resource_mutex
);
728 /* Caller is responsible for RCU sync before final sock_put() */
731 #ifdef CONFIG_PROC_FS
732 static struct sock
**pn_res_get_idx(struct seq_file
*seq
, loff_t pos
)
734 struct net
*net
= seq_file_net(seq
);
737 if (!net_eq(net
, &init_net
))
740 for (i
= 0; i
< 256; i
++) {
741 if (pnres
.sk
[i
] == NULL
)
750 static struct sock
**pn_res_get_next(struct seq_file
*seq
, struct sock
**sk
)
752 struct net
*net
= seq_file_net(seq
);
755 BUG_ON(!net_eq(net
, &init_net
));
757 for (i
= (sk
- pnres
.sk
) + 1; i
< 256; i
++)
763 static void *pn_res_seq_start(struct seq_file
*seq
, loff_t
*pos
)
764 __acquires(resource_mutex
)
766 mutex_lock(&resource_mutex
);
767 return *pos
? pn_res_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
770 static void *pn_res_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
774 if (v
== SEQ_START_TOKEN
)
775 sk
= pn_res_get_idx(seq
, 0);
777 sk
= pn_res_get_next(seq
, v
);
782 static void pn_res_seq_stop(struct seq_file
*seq
, void *v
)
783 __releases(resource_mutex
)
785 mutex_unlock(&resource_mutex
);
788 static int pn_res_seq_show(struct seq_file
*seq
, void *v
)
792 if (v
== SEQ_START_TOKEN
)
793 seq_printf(seq
, "%s%n", "rs uid inode", &len
);
795 struct sock
**psk
= v
;
796 struct sock
*sk
= *psk
;
798 seq_printf(seq
, "%02X %5d %lu%n",
799 (int) (psk
- pnres
.sk
), sock_i_uid(sk
),
800 sock_i_ino(sk
), &len
);
802 seq_printf(seq
, "%*s\n", 63 - len
, "");
806 static const struct seq_operations pn_res_seq_ops
= {
807 .start
= pn_res_seq_start
,
808 .next
= pn_res_seq_next
,
809 .stop
= pn_res_seq_stop
,
810 .show
= pn_res_seq_show
,
813 static int pn_res_open(struct inode
*inode
, struct file
*file
)
815 return seq_open_net(inode
, file
, &pn_res_seq_ops
,
816 sizeof(struct seq_net_private
));
819 const struct file_operations pn_res_seq_fops
= {
820 .owner
= THIS_MODULE
,
824 .release
= seq_release_net
,