6 * Copyright (C) 2008 Nokia Corporation.
8 * Authors: 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>
30 #include <linux/sched/signal.h>
33 #include <net/tcp_states.h>
35 #include <linux/phonet.h>
36 #include <linux/export.h>
37 #include <net/phonet/phonet.h>
38 #include <net/phonet/pep.h>
39 #include <net/phonet/pn_dev.h>
41 static int pn_socket_release(struct socket
*sock
)
43 struct sock
*sk
= sock
->sk
;
47 sk
->sk_prot
->close(sk
, 0);
52 #define PN_HASHSIZE 16
53 #define PN_HASHMASK (PN_HASHSIZE-1)
57 struct hlist_head hlist
[PN_HASHSIZE
];
61 void __init
pn_sock_init(void)
65 for (i
= 0; i
< PN_HASHSIZE
; i
++)
66 INIT_HLIST_HEAD(pnsocks
.hlist
+ i
);
67 mutex_init(&pnsocks
.lock
);
70 static struct hlist_head
*pn_hash_list(u16 obj
)
72 return pnsocks
.hlist
+ (obj
& PN_HASHMASK
);
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.
79 struct sock
*pn_find_sock_by_sa(struct net
*net
, const struct sockaddr_pn
*spn
)
82 struct sock
*rval
= NULL
;
83 u16 obj
= pn_sockaddr_get_object(spn
);
84 u8 res
= spn
->spn_resource
;
85 struct hlist_head
*hlist
= pn_hash_list(obj
);
88 sk_for_each_rcu(sknode
, hlist
) {
89 struct pn_sock
*pn
= pn_sk(sknode
);
90 BUG_ON(!pn
->sobject
); /* unbound socket */
92 if (!net_eq(sock_net(sknode
), net
))
95 /* Look up socket by port */
96 if (pn_port(pn
->sobject
) != pn_port(obj
))
99 /* If port is zero, look up by resource */
100 if (pn
->resource
!= res
)
103 if (pn_addr(pn
->sobject
) &&
104 pn_addr(pn
->sobject
) != pn_addr(obj
))
116 /* Deliver a broadcast packet (only in bottom-half) */
117 void pn_deliver_sock_broadcast(struct net
*net
, struct sk_buff
*skb
)
119 struct hlist_head
*hlist
= pnsocks
.hlist
;
123 for (h
= 0; h
< PN_HASHSIZE
; h
++) {
126 sk_for_each(sknode
, 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 int 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
);
155 EXPORT_SYMBOL(pn_sock_hash
);
157 void pn_sock_unhash(struct sock
*sk
)
159 mutex_lock(&pnsocks
.lock
);
160 sk_del_node_init_rcu(sk
);
161 mutex_unlock(&pnsocks
.lock
);
162 pn_sock_unbind_all_res(sk
);
165 EXPORT_SYMBOL(pn_sock_unhash
);
167 static DEFINE_MUTEX(port_mutex
);
169 static int pn_socket_bind(struct socket
*sock
, struct sockaddr
*addr
, int len
)
171 struct sock
*sk
= sock
->sk
;
172 struct pn_sock
*pn
= pn_sk(sk
);
173 struct sockaddr_pn
*spn
= (struct sockaddr_pn
*)addr
;
178 if (sk
->sk_prot
->bind
)
179 return sk
->sk_prot
->bind(sk
, addr
, len
);
181 if (len
< sizeof(struct sockaddr_pn
))
183 if (spn
->spn_family
!= AF_PHONET
)
184 return -EAFNOSUPPORT
;
186 handle
= pn_sockaddr_get_object((struct sockaddr_pn
*)addr
);
187 saddr
= pn_addr(handle
);
188 if (saddr
&& phonet_address_lookup(sock_net(sk
), saddr
))
189 return -EADDRNOTAVAIL
;
192 if (sk
->sk_state
!= TCP_CLOSE
|| pn_port(pn
->sobject
)) {
193 err
= -EINVAL
; /* attempt to rebind */
196 WARN_ON(sk_hashed(sk
));
197 mutex_lock(&port_mutex
);
198 err
= sk
->sk_prot
->get_port(sk
, pn_port(handle
));
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
;
206 /* Enable RX on the socket */
207 err
= sk
->sk_prot
->hash(sk
);
209 mutex_unlock(&port_mutex
);
215 static int pn_socket_autobind(struct socket
*sock
)
217 struct sockaddr_pn sa
;
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
));
226 BUG_ON(!pn_port(pn_sk(sock
->sk
)->sobject
));
227 return 0; /* socket was already bound */
230 static int pn_socket_connect(struct socket
*sock
, struct sockaddr
*addr
,
233 struct sock
*sk
= sock
->sk
;
234 struct pn_sock
*pn
= pn_sk(sk
);
235 struct sockaddr_pn
*spn
= (struct sockaddr_pn
*)addr
;
236 struct task_struct
*tsk
= current
;
237 long timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
240 if (pn_socket_autobind(sock
))
242 if (len
< sizeof(struct sockaddr_pn
))
244 if (spn
->spn_family
!= AF_PHONET
)
245 return -EAFNOSUPPORT
;
249 switch (sock
->state
) {
251 if (sk
->sk_state
!= TCP_CLOSE
) {
264 pn
->dobject
= pn_sockaddr_get_object(spn
);
265 pn
->resource
= pn_sockaddr_get_resource(spn
);
266 sock
->state
= SS_CONNECTING
;
268 err
= sk
->sk_prot
->connect(sk
, addr
, len
);
270 sock
->state
= SS_UNCONNECTED
;
275 while (sk
->sk_state
== TCP_SYN_SENT
) {
282 if (signal_pending(tsk
)) {
283 err
= sock_intr_errno(timeo
);
287 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
,
290 timeo
= schedule_timeout(timeo
);
292 finish_wait(sk_sleep(sk
), &wait
);
295 if ((1 << sk
->sk_state
) & (TCPF_SYN_RECV
|TCPF_ESTABLISHED
))
297 else if (sk
->sk_state
== TCP_CLOSE_WAIT
)
301 sock
->state
= err
? SS_UNCONNECTED
: SS_CONNECTED
;
307 static int pn_socket_accept(struct socket
*sock
, struct socket
*newsock
,
308 int flags
, bool kern
)
310 struct sock
*sk
= sock
->sk
;
314 if (unlikely(sk
->sk_state
!= TCP_LISTEN
))
317 newsk
= sk
->sk_prot
->accept(sk
, flags
, &err
, kern
);
322 sock_graft(newsk
, newsock
);
323 newsock
->state
= SS_CONNECTED
;
328 static int pn_socket_getname(struct socket
*sock
, struct sockaddr
*addr
,
331 struct sock
*sk
= sock
->sk
;
332 struct pn_sock
*pn
= pn_sk(sk
);
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
,
340 return sizeof(struct sockaddr_pn
);
343 static __poll_t
pn_socket_poll(struct file
*file
, struct socket
*sock
,
346 struct sock
*sk
= sock
->sk
;
347 struct pep_sock
*pn
= pep_sk(sk
);
350 poll_wait(file
, sk_sleep(sk
), wait
);
352 if (sk
->sk_state
== TCP_CLOSE
)
354 if (!skb_queue_empty_lockless(&sk
->sk_receive_queue
))
355 mask
|= EPOLLIN
| EPOLLRDNORM
;
356 if (!skb_queue_empty_lockless(&pn
->ctrlreq_queue
))
358 if (!mask
&& sk
->sk_state
== TCP_CLOSE_WAIT
)
361 if (sk
->sk_state
== TCP_ESTABLISHED
&&
362 refcount_read(&sk
->sk_wmem_alloc
) < sk
->sk_sndbuf
&&
363 atomic_read(&pn
->tx_credits
))
364 mask
|= EPOLLOUT
| EPOLLWRNORM
| EPOLLWRBAND
;
369 static int pn_socket_ioctl(struct socket
*sock
, unsigned int cmd
,
372 struct sock
*sk
= sock
->sk
;
373 struct pn_sock
*pn
= pn_sk(sk
);
375 if (cmd
== SIOCPNGETOBJECT
) {
376 struct net_device
*dev
;
380 if (get_user(handle
, (__u16 __user
*)arg
))
384 if (sk
->sk_bound_dev_if
)
385 dev
= dev_get_by_index(sock_net(sk
),
386 sk
->sk_bound_dev_if
);
388 dev
= phonet_device_get(sock_net(sk
));
389 if (dev
&& (dev
->flags
& IFF_UP
))
390 saddr
= phonet_address_get(dev
, pn_addr(handle
));
397 if (saddr
== PN_NO_ADDR
)
398 return -EHOSTUNREACH
;
400 handle
= pn_object(saddr
, pn_port(pn
->sobject
));
401 return put_user(handle
, (__u16 __user
*)arg
);
404 return sk
->sk_prot
->ioctl(sk
, cmd
, arg
);
407 static int pn_socket_listen(struct socket
*sock
, int backlog
)
409 struct sock
*sk
= sock
->sk
;
412 if (pn_socket_autobind(sock
))
416 if (sock
->state
!= SS_UNCONNECTED
) {
421 if (sk
->sk_state
!= TCP_LISTEN
) {
422 sk
->sk_state
= TCP_LISTEN
;
423 sk
->sk_ack_backlog
= 0;
425 sk
->sk_max_ack_backlog
= backlog
;
431 static int pn_socket_sendmsg(struct socket
*sock
, struct msghdr
*m
,
434 struct sock
*sk
= sock
->sk
;
436 if (pn_socket_autobind(sock
))
439 return sk
->sk_prot
->sendmsg(sk
, m
, total_len
);
442 const struct proto_ops phonet_dgram_ops
= {
444 .owner
= THIS_MODULE
,
445 .release
= pn_socket_release
,
446 .bind
= pn_socket_bind
,
447 .connect
= sock_no_connect
,
448 .socketpair
= sock_no_socketpair
,
449 .accept
= sock_no_accept
,
450 .getname
= pn_socket_getname
,
451 .poll
= datagram_poll
,
452 .ioctl
= pn_socket_ioctl
,
453 .listen
= sock_no_listen
,
454 .shutdown
= sock_no_shutdown
,
455 .setsockopt
= sock_no_setsockopt
,
456 .getsockopt
= sock_no_getsockopt
,
458 .compat_setsockopt
= sock_no_setsockopt
,
459 .compat_getsockopt
= sock_no_getsockopt
,
461 .sendmsg
= pn_socket_sendmsg
,
462 .recvmsg
= sock_common_recvmsg
,
463 .mmap
= sock_no_mmap
,
464 .sendpage
= sock_no_sendpage
,
467 const struct proto_ops phonet_stream_ops
= {
469 .owner
= THIS_MODULE
,
470 .release
= pn_socket_release
,
471 .bind
= pn_socket_bind
,
472 .connect
= pn_socket_connect
,
473 .socketpair
= sock_no_socketpair
,
474 .accept
= pn_socket_accept
,
475 .getname
= pn_socket_getname
,
476 .poll
= pn_socket_poll
,
477 .ioctl
= pn_socket_ioctl
,
478 .listen
= pn_socket_listen
,
479 .shutdown
= sock_no_shutdown
,
480 .setsockopt
= sock_common_setsockopt
,
481 .getsockopt
= sock_common_getsockopt
,
483 .compat_setsockopt
= compat_sock_common_setsockopt
,
484 .compat_getsockopt
= compat_sock_common_getsockopt
,
486 .sendmsg
= pn_socket_sendmsg
,
487 .recvmsg
= sock_common_recvmsg
,
488 .mmap
= sock_no_mmap
,
489 .sendpage
= sock_no_sendpage
,
491 EXPORT_SYMBOL(phonet_stream_ops
);
493 /* allocate port for a socket */
494 int pn_sock_get_port(struct sock
*sk
, unsigned short sport
)
497 struct net
*net
= sock_net(sk
);
498 struct pn_sock
*pn
= pn_sk(sk
);
499 struct sockaddr_pn try_sa
;
502 memset(&try_sa
, 0, sizeof(struct sockaddr_pn
));
503 try_sa
.spn_family
= AF_PHONET
;
504 WARN_ON(!mutex_is_locked(&port_mutex
));
506 /* search free port */
507 int port
, pmin
, pmax
;
509 phonet_get_local_port_range(&pmin
, &pmax
);
510 for (port
= pmin
; port
<= pmax
; port
++) {
512 if (port_cur
< pmin
|| port_cur
> pmax
)
515 pn_sockaddr_set_port(&try_sa
, port_cur
);
516 tmpsk
= pn_find_sock_by_sa(net
, &try_sa
);
524 /* try to find specific port */
525 pn_sockaddr_set_port(&try_sa
, sport
);
526 tmpsk
= pn_find_sock_by_sa(net
, &try_sa
);
528 /* No sock there! We can use that port... */
533 /* the port must be in use already */
537 pn
->sobject
= pn_object(pn_addr(pn
->sobject
), sport
);
540 EXPORT_SYMBOL(pn_sock_get_port
);
542 #ifdef CONFIG_PROC_FS
543 static struct sock
*pn_sock_get_idx(struct seq_file
*seq
, loff_t pos
)
545 struct net
*net
= seq_file_net(seq
);
546 struct hlist_head
*hlist
= pnsocks
.hlist
;
550 for (h
= 0; h
< PN_HASHSIZE
; h
++) {
551 sk_for_each_rcu(sknode
, 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
)
601 seq_setwidth(seq
, 127);
602 if (v
== SEQ_START_TOKEN
)
603 seq_puts(seq
, "pt loc rem rs st tx_queue rx_queue "
604 " uid inode ref pointer drops");
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 from_kuid_munged(seq_user_ns(seq
), sock_i_uid(sk
)),
616 refcount_read(&sk
->sk_refcnt
), sk
,
617 atomic_read(&sk
->sk_drops
));
623 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
,
632 struct sock
*sk
[256];
636 * Find and hold socket based on resource.
638 struct sock
*pn_find_sock_by_res(struct net
*net
, u8 res
)
642 if (!net_eq(net
, &init_net
))
646 sk
= rcu_dereference(pnres
.sk
[res
]);
653 static DEFINE_MUTEX(resource_mutex
);
655 int pn_sock_bind_res(struct sock
*sk
, u8 res
)
657 int ret
= -EADDRINUSE
;
659 if (!net_eq(sock_net(sk
), &init_net
))
661 if (!capable(CAP_SYS_ADMIN
))
663 if (pn_socket_autobind(sk
->sk_socket
))
666 mutex_lock(&resource_mutex
);
667 if (pnres
.sk
[res
] == NULL
) {
669 rcu_assign_pointer(pnres
.sk
[res
], sk
);
672 mutex_unlock(&resource_mutex
);
676 int pn_sock_unbind_res(struct sock
*sk
, u8 res
)
680 if (!capable(CAP_SYS_ADMIN
))
683 mutex_lock(&resource_mutex
);
684 if (pnres
.sk
[res
] == sk
) {
685 RCU_INIT_POINTER(pnres
.sk
[res
], NULL
);
688 mutex_unlock(&resource_mutex
);
697 void pn_sock_unbind_all_res(struct sock
*sk
)
699 unsigned int res
, match
= 0;
701 mutex_lock(&resource_mutex
);
702 for (res
= 0; res
< 256; res
++) {
703 if (pnres
.sk
[res
] == sk
) {
704 RCU_INIT_POINTER(pnres
.sk
[res
], NULL
);
708 mutex_unlock(&resource_mutex
);
714 /* Caller is responsible for RCU sync before final sock_put() */
717 #ifdef CONFIG_PROC_FS
718 static struct sock
**pn_res_get_idx(struct seq_file
*seq
, loff_t pos
)
720 struct net
*net
= seq_file_net(seq
);
723 if (!net_eq(net
, &init_net
))
726 for (i
= 0; i
< 256; i
++) {
727 if (pnres
.sk
[i
] == NULL
)
736 static struct sock
**pn_res_get_next(struct seq_file
*seq
, struct sock
**sk
)
738 struct net
*net
= seq_file_net(seq
);
741 BUG_ON(!net_eq(net
, &init_net
));
743 for (i
= (sk
- pnres
.sk
) + 1; i
< 256; i
++)
749 static void *pn_res_seq_start(struct seq_file
*seq
, loff_t
*pos
)
750 __acquires(resource_mutex
)
752 mutex_lock(&resource_mutex
);
753 return *pos
? pn_res_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
756 static void *pn_res_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
760 if (v
== SEQ_START_TOKEN
)
761 sk
= pn_res_get_idx(seq
, 0);
763 sk
= pn_res_get_next(seq
, v
);
768 static void pn_res_seq_stop(struct seq_file
*seq
, void *v
)
769 __releases(resource_mutex
)
771 mutex_unlock(&resource_mutex
);
774 static int pn_res_seq_show(struct seq_file
*seq
, void *v
)
776 seq_setwidth(seq
, 63);
777 if (v
== SEQ_START_TOKEN
)
778 seq_puts(seq
, "rs uid inode");
780 struct sock
**psk
= v
;
781 struct sock
*sk
= *psk
;
783 seq_printf(seq
, "%02X %5u %lu",
784 (int) (psk
- pnres
.sk
),
785 from_kuid_munged(seq_user_ns(seq
), sock_i_uid(sk
)),
792 const struct seq_operations pn_res_seq_ops
= {
793 .start
= pn_res_seq_start
,
794 .next
= pn_res_seq_next
,
795 .stop
= pn_res_seq_stop
,
796 .show
= pn_res_seq_show
,