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 <net/phonet/phonet.h>
35 #include <net/phonet/pep.h>
36 #include <net/phonet/pn_dev.h>
38 static int pn_socket_release(struct socket
*sock
)
40 struct sock
*sk
= sock
->sk
;
44 sk
->sk_prot
->close(sk
, 0);
49 #define PN_HASHSIZE 16
50 #define PN_HASHMASK (PN_HASHSIZE-1)
54 struct hlist_head hlist
[PN_HASHSIZE
];
58 void __init
pn_sock_init(void)
62 for (i
= 0; i
< PN_HASHSIZE
; i
++)
63 INIT_HLIST_HEAD(pnsocks
.hlist
+ i
);
64 mutex_init(&pnsocks
.lock
);
67 static struct hlist_head
*pn_hash_list(u16 obj
)
69 return pnsocks
.hlist
+ (obj
& PN_HASHMASK
);
73 * Find address based on socket address, match only certain fields.
74 * Also grab sock if it was found. Remember to sock_put it later.
76 struct sock
*pn_find_sock_by_sa(struct net
*net
, const struct sockaddr_pn
*spn
)
78 struct hlist_node
*node
;
80 struct sock
*rval
= NULL
;
81 u16 obj
= pn_sockaddr_get_object(spn
);
82 u8 res
= spn
->spn_resource
;
83 struct hlist_head
*hlist
= pn_hash_list(obj
);
86 sk_for_each_rcu(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
))
93 /* Look up socket by port */
94 if (pn_port(pn
->sobject
) != pn_port(obj
))
97 /* If port is zero, look up by resource */
98 if (pn
->resource
!= res
)
101 if (pn_addr(pn
->sobject
) &&
102 pn_addr(pn
->sobject
) != pn_addr(obj
))
114 /* Deliver a broadcast packet (only in bottom-half) */
115 void pn_deliver_sock_broadcast(struct net
*net
, struct sk_buff
*skb
)
117 struct hlist_head
*hlist
= pnsocks
.hlist
;
121 for (h
= 0; h
< PN_HASHSIZE
; h
++) {
122 struct hlist_node
*node
;
125 sk_for_each(sknode
, node
, hlist
) {
126 struct sk_buff
*clone
;
128 if (!net_eq(sock_net(sknode
), net
))
130 if (!sock_flag(sknode
, SOCK_BROADCAST
))
133 clone
= skb_clone(skb
, GFP_ATOMIC
);
136 sk_receive_skb(sknode
, clone
, 0);
144 void pn_sock_hash(struct sock
*sk
)
146 struct hlist_head
*hlist
= pn_hash_list(pn_sk(sk
)->sobject
);
148 mutex_lock(&pnsocks
.lock
);
149 sk_add_node_rcu(sk
, hlist
);
150 mutex_unlock(&pnsocks
.lock
);
152 EXPORT_SYMBOL(pn_sock_hash
);
154 void pn_sock_unhash(struct sock
*sk
)
156 mutex_lock(&pnsocks
.lock
);
157 sk_del_node_init_rcu(sk
);
158 mutex_unlock(&pnsocks
.lock
);
159 pn_sock_unbind_all_res(sk
);
162 EXPORT_SYMBOL(pn_sock_unhash
);
164 static DEFINE_MUTEX(port_mutex
);
166 static int pn_socket_bind(struct socket
*sock
, struct sockaddr
*addr
, int len
)
168 struct sock
*sk
= sock
->sk
;
169 struct pn_sock
*pn
= pn_sk(sk
);
170 struct sockaddr_pn
*spn
= (struct sockaddr_pn
*)addr
;
175 if (sk
->sk_prot
->bind
)
176 return sk
->sk_prot
->bind(sk
, addr
, len
);
178 if (len
< sizeof(struct sockaddr_pn
))
180 if (spn
->spn_family
!= AF_PHONET
)
181 return -EAFNOSUPPORT
;
183 handle
= pn_sockaddr_get_object((struct sockaddr_pn
*)addr
);
184 saddr
= pn_addr(handle
);
185 if (saddr
&& phonet_address_lookup(sock_net(sk
), saddr
))
186 return -EADDRNOTAVAIL
;
189 if (sk
->sk_state
!= TCP_CLOSE
|| pn_port(pn
->sobject
)) {
190 err
= -EINVAL
; /* attempt to rebind */
193 WARN_ON(sk_hashed(sk
));
194 mutex_lock(&port_mutex
);
195 err
= sk
->sk_prot
->get_port(sk
, pn_port(handle
));
199 /* get_port() sets the port, bind() sets the address if applicable */
200 pn
->sobject
= pn_object(saddr
, pn_port(pn
->sobject
));
201 pn
->resource
= spn
->spn_resource
;
203 /* Enable RX on the socket */
204 sk
->sk_prot
->hash(sk
);
206 mutex_unlock(&port_mutex
);
212 static int pn_socket_autobind(struct socket
*sock
)
214 struct sockaddr_pn sa
;
217 memset(&sa
, 0, sizeof(sa
));
218 sa
.spn_family
= AF_PHONET
;
219 err
= pn_socket_bind(sock
, (struct sockaddr
*)&sa
,
220 sizeof(struct sockaddr_pn
));
223 BUG_ON(!pn_port(pn_sk(sock
->sk
)->sobject
));
224 return 0; /* socket was already bound */
227 static int pn_socket_connect(struct socket
*sock
, struct sockaddr
*addr
,
230 struct sock
*sk
= sock
->sk
;
231 struct pn_sock
*pn
= pn_sk(sk
);
232 struct sockaddr_pn
*spn
= (struct sockaddr_pn
*)addr
;
233 struct task_struct
*tsk
= current
;
234 long timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
237 if (pn_socket_autobind(sock
))
239 if (len
< sizeof(struct sockaddr_pn
))
241 if (spn
->spn_family
!= AF_PHONET
)
242 return -EAFNOSUPPORT
;
246 switch (sock
->state
) {
248 if (sk
->sk_state
!= TCP_CLOSE
) {
261 pn
->dobject
= pn_sockaddr_get_object(spn
);
262 pn
->resource
= pn_sockaddr_get_resource(spn
);
263 sock
->state
= SS_CONNECTING
;
265 err
= sk
->sk_prot
->connect(sk
, addr
, len
);
267 sock
->state
= SS_UNCONNECTED
;
272 while (sk
->sk_state
== TCP_SYN_SENT
) {
279 if (signal_pending(tsk
)) {
280 err
= sock_intr_errno(timeo
);
284 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
,
287 timeo
= schedule_timeout(timeo
);
289 finish_wait(sk_sleep(sk
), &wait
);
292 if ((1 << sk
->sk_state
) & (TCPF_SYN_RECV
|TCPF_ESTABLISHED
))
294 else if (sk
->sk_state
== TCP_CLOSE_WAIT
)
298 sock
->state
= err
? SS_UNCONNECTED
: SS_CONNECTED
;
304 static int pn_socket_accept(struct socket
*sock
, struct socket
*newsock
,
307 struct sock
*sk
= sock
->sk
;
311 if (unlikely(sk
->sk_state
!= TCP_LISTEN
))
314 newsk
= sk
->sk_prot
->accept(sk
, flags
, &err
);
319 sock_graft(newsk
, newsock
);
320 newsock
->state
= SS_CONNECTED
;
325 static int pn_socket_getname(struct socket
*sock
, struct sockaddr
*addr
,
326 int *sockaddr_len
, int peer
)
328 struct sock
*sk
= sock
->sk
;
329 struct pn_sock
*pn
= pn_sk(sk
);
331 memset(addr
, 0, sizeof(struct sockaddr_pn
));
332 addr
->sa_family
= AF_PHONET
;
333 if (!peer
) /* Race with bind() here is userland's problem. */
334 pn_sockaddr_set_object((struct sockaddr_pn
*)addr
,
337 *sockaddr_len
= sizeof(struct sockaddr_pn
);
341 static unsigned int pn_socket_poll(struct file
*file
, struct socket
*sock
,
344 struct sock
*sk
= sock
->sk
;
345 struct pep_sock
*pn
= pep_sk(sk
);
346 unsigned int mask
= 0;
348 poll_wait(file
, sk_sleep(sk
), wait
);
350 if (sk
->sk_state
== TCP_CLOSE
)
352 if (!skb_queue_empty(&sk
->sk_receive_queue
))
353 mask
|= POLLIN
| POLLRDNORM
;
354 if (!skb_queue_empty(&pn
->ctrlreq_queue
))
356 if (!mask
&& sk
->sk_state
== TCP_CLOSE_WAIT
)
359 if (sk
->sk_state
== TCP_ESTABLISHED
&&
360 atomic_read(&sk
->sk_wmem_alloc
) < sk
->sk_sndbuf
&&
361 atomic_read(&pn
->tx_credits
))
362 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
367 static int pn_socket_ioctl(struct socket
*sock
, unsigned int cmd
,
370 struct sock
*sk
= sock
->sk
;
371 struct pn_sock
*pn
= pn_sk(sk
);
373 if (cmd
== SIOCPNGETOBJECT
) {
374 struct net_device
*dev
;
378 if (get_user(handle
, (__u16 __user
*)arg
))
382 if (sk
->sk_bound_dev_if
)
383 dev
= dev_get_by_index(sock_net(sk
),
384 sk
->sk_bound_dev_if
);
386 dev
= phonet_device_get(sock_net(sk
));
387 if (dev
&& (dev
->flags
& IFF_UP
))
388 saddr
= phonet_address_get(dev
, pn_addr(handle
));
395 if (saddr
== PN_NO_ADDR
)
396 return -EHOSTUNREACH
;
398 handle
= pn_object(saddr
, pn_port(pn
->sobject
));
399 return put_user(handle
, (__u16 __user
*)arg
);
402 return sk
->sk_prot
->ioctl(sk
, cmd
, arg
);
405 static int pn_socket_listen(struct socket
*sock
, int backlog
)
407 struct sock
*sk
= sock
->sk
;
410 if (pn_socket_autobind(sock
))
414 if (sock
->state
!= SS_UNCONNECTED
) {
419 if (sk
->sk_state
!= TCP_LISTEN
) {
420 sk
->sk_state
= TCP_LISTEN
;
421 sk
->sk_ack_backlog
= 0;
423 sk
->sk_max_ack_backlog
= backlog
;
429 static int pn_socket_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
430 struct msghdr
*m
, size_t total_len
)
432 struct sock
*sk
= sock
->sk
;
434 if (pn_socket_autobind(sock
))
437 return sk
->sk_prot
->sendmsg(iocb
, sk
, m
, total_len
);
440 const struct proto_ops phonet_dgram_ops
= {
442 .owner
= THIS_MODULE
,
443 .release
= pn_socket_release
,
444 .bind
= pn_socket_bind
,
445 .connect
= sock_no_connect
,
446 .socketpair
= sock_no_socketpair
,
447 .accept
= sock_no_accept
,
448 .getname
= pn_socket_getname
,
449 .poll
= datagram_poll
,
450 .ioctl
= pn_socket_ioctl
,
451 .listen
= sock_no_listen
,
452 .shutdown
= sock_no_shutdown
,
453 .setsockopt
= sock_no_setsockopt
,
454 .getsockopt
= sock_no_getsockopt
,
456 .compat_setsockopt
= sock_no_setsockopt
,
457 .compat_getsockopt
= sock_no_getsockopt
,
459 .sendmsg
= pn_socket_sendmsg
,
460 .recvmsg
= sock_common_recvmsg
,
461 .mmap
= sock_no_mmap
,
462 .sendpage
= sock_no_sendpage
,
465 const struct proto_ops phonet_stream_ops
= {
467 .owner
= THIS_MODULE
,
468 .release
= pn_socket_release
,
469 .bind
= pn_socket_bind
,
470 .connect
= pn_socket_connect
,
471 .socketpair
= sock_no_socketpair
,
472 .accept
= pn_socket_accept
,
473 .getname
= pn_socket_getname
,
474 .poll
= pn_socket_poll
,
475 .ioctl
= pn_socket_ioctl
,
476 .listen
= pn_socket_listen
,
477 .shutdown
= sock_no_shutdown
,
478 .setsockopt
= sock_common_setsockopt
,
479 .getsockopt
= sock_common_getsockopt
,
481 .compat_setsockopt
= compat_sock_common_setsockopt
,
482 .compat_getsockopt
= compat_sock_common_getsockopt
,
484 .sendmsg
= pn_socket_sendmsg
,
485 .recvmsg
= sock_common_recvmsg
,
486 .mmap
= sock_no_mmap
,
487 .sendpage
= sock_no_sendpage
,
489 EXPORT_SYMBOL(phonet_stream_ops
);
491 /* allocate port for a socket */
492 int pn_sock_get_port(struct sock
*sk
, unsigned short sport
)
495 struct net
*net
= sock_net(sk
);
496 struct pn_sock
*pn
= pn_sk(sk
);
497 struct sockaddr_pn try_sa
;
500 memset(&try_sa
, 0, sizeof(struct sockaddr_pn
));
501 try_sa
.spn_family
= AF_PHONET
;
502 WARN_ON(!mutex_is_locked(&port_mutex
));
504 /* search free port */
505 int port
, pmin
, pmax
;
507 phonet_get_local_port_range(&pmin
, &pmax
);
508 for (port
= pmin
; port
<= pmax
; port
++) {
510 if (port_cur
< pmin
|| port_cur
> pmax
)
513 pn_sockaddr_set_port(&try_sa
, port_cur
);
514 tmpsk
= pn_find_sock_by_sa(net
, &try_sa
);
522 /* try to find specific port */
523 pn_sockaddr_set_port(&try_sa
, sport
);
524 tmpsk
= pn_find_sock_by_sa(net
, &try_sa
);
526 /* No sock there! We can use that port... */
531 /* the port must be in use already */
535 pn
->sobject
= pn_object(pn_addr(pn
->sobject
), sport
);
538 EXPORT_SYMBOL(pn_sock_get_port
);
540 #ifdef CONFIG_PROC_FS
541 static struct sock
*pn_sock_get_idx(struct seq_file
*seq
, loff_t pos
)
543 struct net
*net
= seq_file_net(seq
);
544 struct hlist_head
*hlist
= pnsocks
.hlist
;
545 struct hlist_node
*node
;
549 for (h
= 0; h
< PN_HASHSIZE
; h
++) {
550 sk_for_each_rcu(sknode
, node
, hlist
) {
551 if (!net_eq(net
, sock_net(sknode
)))
562 static struct sock
*pn_sock_get_next(struct seq_file
*seq
, struct sock
*sk
)
564 struct net
*net
= seq_file_net(seq
);
568 while (sk
&& !net_eq(net
, sock_net(sk
)));
573 static void *pn_sock_seq_start(struct seq_file
*seq
, loff_t
*pos
)
577 return *pos
? pn_sock_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
580 static void *pn_sock_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
584 if (v
== SEQ_START_TOKEN
)
585 sk
= pn_sock_get_idx(seq
, 0);
587 sk
= pn_sock_get_next(seq
, v
);
592 static void pn_sock_seq_stop(struct seq_file
*seq
, void *v
)
598 static int pn_sock_seq_show(struct seq_file
*seq
, void *v
)
602 if (v
== SEQ_START_TOKEN
)
603 seq_printf(seq
, "%s%n", "pt loc rem rs st tx_queue rx_queue "
604 " uid inode ref pointer drops", &len
);
607 struct pn_sock
*pn
= pn_sk(sk
);
609 seq_printf(seq
, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu "
611 sk
->sk_protocol
, pn
->sobject
, pn
->dobject
,
612 pn
->resource
, sk
->sk_state
,
613 sk_wmem_alloc_get(sk
), sk_rmem_alloc_get(sk
),
614 sock_i_uid(sk
), sock_i_ino(sk
),
615 atomic_read(&sk
->sk_refcnt
), sk
,
616 atomic_read(&sk
->sk_drops
), &len
);
618 seq_printf(seq
, "%*s\n", 127 - len
, "");
622 static 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
,
629 static int pn_sock_open(struct inode
*inode
, struct file
*file
)
631 return seq_open_net(inode
, file
, &pn_sock_seq_ops
,
632 sizeof(struct seq_net_private
));
635 const struct file_operations pn_sock_seq_fops
= {
636 .owner
= THIS_MODULE
,
637 .open
= pn_sock_open
,
640 .release
= seq_release_net
,
645 struct sock
*sk
[256];
649 * Find and hold socket based on resource.
651 struct sock
*pn_find_sock_by_res(struct net
*net
, u8 res
)
655 if (!net_eq(net
, &init_net
))
659 sk
= rcu_dereference(pnres
.sk
[res
]);
666 static DEFINE_MUTEX(resource_mutex
);
668 int pn_sock_bind_res(struct sock
*sk
, u8 res
)
670 int ret
= -EADDRINUSE
;
672 if (!net_eq(sock_net(sk
), &init_net
))
674 if (!capable(CAP_SYS_ADMIN
))
676 if (pn_socket_autobind(sk
->sk_socket
))
679 mutex_lock(&resource_mutex
);
680 if (pnres
.sk
[res
] == NULL
) {
682 rcu_assign_pointer(pnres
.sk
[res
], sk
);
685 mutex_unlock(&resource_mutex
);
689 int pn_sock_unbind_res(struct sock
*sk
, u8 res
)
693 if (!capable(CAP_SYS_ADMIN
))
696 mutex_lock(&resource_mutex
);
697 if (pnres
.sk
[res
] == sk
) {
698 rcu_assign_pointer(pnres
.sk
[res
], NULL
);
701 mutex_unlock(&resource_mutex
);
710 void pn_sock_unbind_all_res(struct sock
*sk
)
712 unsigned res
, match
= 0;
714 mutex_lock(&resource_mutex
);
715 for (res
= 0; res
< 256; res
++) {
716 if (pnres
.sk
[res
] == sk
) {
717 rcu_assign_pointer(pnres
.sk
[res
], NULL
);
721 mutex_unlock(&resource_mutex
);
727 /* Caller is responsible for RCU sync before final sock_put() */
730 #ifdef CONFIG_PROC_FS
731 static struct sock
**pn_res_get_idx(struct seq_file
*seq
, loff_t pos
)
733 struct net
*net
= seq_file_net(seq
);
736 if (!net_eq(net
, &init_net
))
739 for (i
= 0; i
< 256; i
++) {
740 if (pnres
.sk
[i
] == NULL
)
749 static struct sock
**pn_res_get_next(struct seq_file
*seq
, struct sock
**sk
)
751 struct net
*net
= seq_file_net(seq
);
754 BUG_ON(!net_eq(net
, &init_net
));
756 for (i
= (sk
- pnres
.sk
) + 1; i
< 256; i
++)
762 static void *pn_res_seq_start(struct seq_file
*seq
, loff_t
*pos
)
763 __acquires(resource_mutex
)
765 mutex_lock(&resource_mutex
);
766 return *pos
? pn_res_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
769 static void *pn_res_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
773 if (v
== SEQ_START_TOKEN
)
774 sk
= pn_res_get_idx(seq
, 0);
776 sk
= pn_res_get_next(seq
, v
);
781 static void pn_res_seq_stop(struct seq_file
*seq
, void *v
)
782 __releases(resource_mutex
)
784 mutex_unlock(&resource_mutex
);
787 static int pn_res_seq_show(struct seq_file
*seq
, void *v
)
791 if (v
== SEQ_START_TOKEN
)
792 seq_printf(seq
, "%s%n", "rs uid inode", &len
);
794 struct sock
**psk
= v
;
795 struct sock
*sk
= *psk
;
797 seq_printf(seq
, "%02X %5d %lu%n",
798 (int) (psk
- pnres
.sk
), sock_i_uid(sk
),
799 sock_i_ino(sk
), &len
);
801 seq_printf(seq
, "%*s\n", 63 - len
, "");
805 static 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
,
812 static int pn_res_open(struct inode
*inode
, struct file
*file
)
814 return seq_open_net(inode
, file
, &pn_res_seq_ops
,
815 sizeof(struct seq_net_private
));
818 const struct file_operations pn_res_seq_fops
= {
819 .owner
= THIS_MODULE
,
823 .release
= seq_release_net
,