1 // SPDX-License-Identifier: GPL-2.0-only
7 * Copyright (C) 2008 Nokia Corporation.
9 * Authors: Sakari Ailus <sakari.ailus@nokia.com>
10 * RĂ©mi Denis-Courmont
13 #include <linux/gfp.h>
14 #include <linux/kernel.h>
15 #include <linux/net.h>
16 #include <linux/poll.h>
17 #include <linux/sched/signal.h>
20 #include <net/tcp_states.h>
22 #include <linux/phonet.h>
23 #include <linux/export.h>
24 #include <net/phonet/phonet.h>
25 #include <net/phonet/pep.h>
26 #include <net/phonet/pn_dev.h>
28 static int pn_socket_release(struct socket
*sock
)
30 struct sock
*sk
= sock
->sk
;
34 sk
->sk_prot
->close(sk
, 0);
39 #define PN_HASHSIZE 16
40 #define PN_HASHMASK (PN_HASHSIZE-1)
44 struct hlist_head hlist
[PN_HASHSIZE
];
48 void __init
pn_sock_init(void)
52 for (i
= 0; i
< PN_HASHSIZE
; i
++)
53 INIT_HLIST_HEAD(pnsocks
.hlist
+ i
);
54 mutex_init(&pnsocks
.lock
);
57 static struct hlist_head
*pn_hash_list(u16 obj
)
59 return pnsocks
.hlist
+ (obj
& PN_HASHMASK
);
63 * Find address based on socket address, match only certain fields.
64 * Also grab sock if it was found. Remember to sock_put it later.
66 struct sock
*pn_find_sock_by_sa(struct net
*net
, const struct sockaddr_pn
*spn
)
69 struct sock
*rval
= NULL
;
70 u16 obj
= pn_sockaddr_get_object(spn
);
71 u8 res
= spn
->spn_resource
;
72 struct hlist_head
*hlist
= pn_hash_list(obj
);
75 sk_for_each_rcu(sknode
, hlist
) {
76 struct pn_sock
*pn
= pn_sk(sknode
);
77 BUG_ON(!pn
->sobject
); /* unbound socket */
79 if (!net_eq(sock_net(sknode
), net
))
82 /* Look up socket by port */
83 if (pn_port(pn
->sobject
) != pn_port(obj
))
86 /* If port is zero, look up by resource */
87 if (pn
->resource
!= res
)
90 if (pn_addr(pn
->sobject
) &&
91 pn_addr(pn
->sobject
) != pn_addr(obj
))
103 /* Deliver a broadcast packet (only in bottom-half) */
104 void pn_deliver_sock_broadcast(struct net
*net
, struct sk_buff
*skb
)
106 struct hlist_head
*hlist
= pnsocks
.hlist
;
110 for (h
= 0; h
< PN_HASHSIZE
; h
++) {
113 sk_for_each(sknode
, hlist
) {
114 struct sk_buff
*clone
;
116 if (!net_eq(sock_net(sknode
), net
))
118 if (!sock_flag(sknode
, SOCK_BROADCAST
))
121 clone
= skb_clone(skb
, GFP_ATOMIC
);
124 sk_receive_skb(sknode
, clone
, 0);
132 int pn_sock_hash(struct sock
*sk
)
134 struct hlist_head
*hlist
= pn_hash_list(pn_sk(sk
)->sobject
);
136 mutex_lock(&pnsocks
.lock
);
137 sk_add_node_rcu(sk
, hlist
);
138 mutex_unlock(&pnsocks
.lock
);
142 EXPORT_SYMBOL(pn_sock_hash
);
144 void pn_sock_unhash(struct sock
*sk
)
146 mutex_lock(&pnsocks
.lock
);
147 sk_del_node_init_rcu(sk
);
148 mutex_unlock(&pnsocks
.lock
);
149 pn_sock_unbind_all_res(sk
);
152 EXPORT_SYMBOL(pn_sock_unhash
);
154 static DEFINE_MUTEX(port_mutex
);
156 static int pn_socket_bind(struct socket
*sock
, struct sockaddr
*addr
, int len
)
158 struct sock
*sk
= sock
->sk
;
159 struct pn_sock
*pn
= pn_sk(sk
);
160 struct sockaddr_pn
*spn
= (struct sockaddr_pn
*)addr
;
165 if (sk
->sk_prot
->bind
)
166 return sk
->sk_prot
->bind(sk
, addr
, len
);
168 if (len
< sizeof(struct sockaddr_pn
))
170 if (spn
->spn_family
!= AF_PHONET
)
171 return -EAFNOSUPPORT
;
173 handle
= pn_sockaddr_get_object((struct sockaddr_pn
*)addr
);
174 saddr
= pn_addr(handle
);
175 if (saddr
&& phonet_address_lookup(sock_net(sk
), saddr
))
176 return -EADDRNOTAVAIL
;
179 if (sk
->sk_state
!= TCP_CLOSE
|| pn_port(pn
->sobject
)) {
180 err
= -EINVAL
; /* attempt to rebind */
183 WARN_ON(sk_hashed(sk
));
184 mutex_lock(&port_mutex
);
185 err
= sk
->sk_prot
->get_port(sk
, pn_port(handle
));
189 /* get_port() sets the port, bind() sets the address if applicable */
190 pn
->sobject
= pn_object(saddr
, pn_port(pn
->sobject
));
191 pn
->resource
= spn
->spn_resource
;
193 /* Enable RX on the socket */
194 err
= sk
->sk_prot
->hash(sk
);
196 mutex_unlock(&port_mutex
);
202 static int pn_socket_autobind(struct socket
*sock
)
204 struct sockaddr_pn sa
;
207 memset(&sa
, 0, sizeof(sa
));
208 sa
.spn_family
= AF_PHONET
;
209 err
= pn_socket_bind(sock
, (struct sockaddr
*)&sa
,
210 sizeof(struct sockaddr_pn
));
213 BUG_ON(!pn_port(pn_sk(sock
->sk
)->sobject
));
214 return 0; /* socket was already bound */
217 static int pn_socket_connect(struct socket
*sock
, struct sockaddr
*addr
,
220 struct sock
*sk
= sock
->sk
;
221 struct pn_sock
*pn
= pn_sk(sk
);
222 struct sockaddr_pn
*spn
= (struct sockaddr_pn
*)addr
;
223 struct task_struct
*tsk
= current
;
224 long timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
227 if (pn_socket_autobind(sock
))
229 if (len
< sizeof(struct sockaddr_pn
))
231 if (spn
->spn_family
!= AF_PHONET
)
232 return -EAFNOSUPPORT
;
236 switch (sock
->state
) {
238 if (sk
->sk_state
!= TCP_CLOSE
) {
251 pn
->dobject
= pn_sockaddr_get_object(spn
);
252 pn
->resource
= pn_sockaddr_get_resource(spn
);
253 sock
->state
= SS_CONNECTING
;
255 err
= sk
->sk_prot
->connect(sk
, addr
, len
);
257 sock
->state
= SS_UNCONNECTED
;
262 while (sk
->sk_state
== TCP_SYN_SENT
) {
269 if (signal_pending(tsk
)) {
270 err
= sock_intr_errno(timeo
);
274 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
,
277 timeo
= schedule_timeout(timeo
);
279 finish_wait(sk_sleep(sk
), &wait
);
282 if ((1 << sk
->sk_state
) & (TCPF_SYN_RECV
|TCPF_ESTABLISHED
))
284 else if (sk
->sk_state
== TCP_CLOSE_WAIT
)
288 sock
->state
= err
? SS_UNCONNECTED
: SS_CONNECTED
;
294 static int pn_socket_accept(struct socket
*sock
, struct socket
*newsock
,
295 struct proto_accept_arg
*arg
)
297 struct sock
*sk
= sock
->sk
;
300 if (unlikely(sk
->sk_state
!= TCP_LISTEN
))
303 newsk
= sk
->sk_prot
->accept(sk
, arg
);
308 sock_graft(newsk
, newsock
);
309 newsock
->state
= SS_CONNECTED
;
314 static int pn_socket_getname(struct socket
*sock
, struct sockaddr
*addr
,
317 struct sock
*sk
= sock
->sk
;
318 struct pn_sock
*pn
= pn_sk(sk
);
320 memset(addr
, 0, sizeof(struct sockaddr_pn
));
321 addr
->sa_family
= AF_PHONET
;
322 if (!peer
) /* Race with bind() here is userland's problem. */
323 pn_sockaddr_set_object((struct sockaddr_pn
*)addr
,
326 return sizeof(struct sockaddr_pn
);
329 static __poll_t
pn_socket_poll(struct file
*file
, struct socket
*sock
,
332 struct sock
*sk
= sock
->sk
;
333 struct pep_sock
*pn
= pep_sk(sk
);
336 poll_wait(file
, sk_sleep(sk
), wait
);
338 if (sk
->sk_state
== TCP_CLOSE
)
340 if (!skb_queue_empty_lockless(&sk
->sk_receive_queue
))
341 mask
|= EPOLLIN
| EPOLLRDNORM
;
342 if (!skb_queue_empty_lockless(&pn
->ctrlreq_queue
))
344 if (!mask
&& sk
->sk_state
== TCP_CLOSE_WAIT
)
347 if (sk
->sk_state
== TCP_ESTABLISHED
&&
348 refcount_read(&sk
->sk_wmem_alloc
) < sk
->sk_sndbuf
&&
349 atomic_read(&pn
->tx_credits
))
350 mask
|= EPOLLOUT
| EPOLLWRNORM
| EPOLLWRBAND
;
355 static int pn_socket_ioctl(struct socket
*sock
, unsigned int cmd
,
358 struct sock
*sk
= sock
->sk
;
359 struct pn_sock
*pn
= pn_sk(sk
);
361 if (cmd
== SIOCPNGETOBJECT
) {
362 struct net_device
*dev
;
366 if (get_user(handle
, (__u16 __user
*)arg
))
370 if (sk
->sk_bound_dev_if
)
371 dev
= dev_get_by_index(sock_net(sk
),
372 sk
->sk_bound_dev_if
);
374 dev
= phonet_device_get(sock_net(sk
));
375 if (dev
&& (dev
->flags
& IFF_UP
))
376 saddr
= phonet_address_get(dev
, pn_addr(handle
));
382 if (saddr
== PN_NO_ADDR
)
383 return -EHOSTUNREACH
;
385 handle
= pn_object(saddr
, pn_port(pn
->sobject
));
386 return put_user(handle
, (__u16 __user
*)arg
);
389 return sk_ioctl(sk
, cmd
, (void __user
*)arg
);
392 static int pn_socket_listen(struct socket
*sock
, int backlog
)
394 struct sock
*sk
= sock
->sk
;
397 if (pn_socket_autobind(sock
))
401 if (sock
->state
!= SS_UNCONNECTED
) {
406 if (sk
->sk_state
!= TCP_LISTEN
) {
407 sk
->sk_state
= TCP_LISTEN
;
408 sk
->sk_ack_backlog
= 0;
410 sk
->sk_max_ack_backlog
= backlog
;
416 static int pn_socket_sendmsg(struct socket
*sock
, struct msghdr
*m
,
419 struct sock
*sk
= sock
->sk
;
421 if (pn_socket_autobind(sock
))
424 return sk
->sk_prot
->sendmsg(sk
, m
, total_len
);
427 const struct proto_ops phonet_dgram_ops
= {
429 .owner
= THIS_MODULE
,
430 .release
= pn_socket_release
,
431 .bind
= pn_socket_bind
,
432 .connect
= sock_no_connect
,
433 .socketpair
= sock_no_socketpair
,
434 .accept
= sock_no_accept
,
435 .getname
= pn_socket_getname
,
436 .poll
= datagram_poll
,
437 .ioctl
= pn_socket_ioctl
,
438 .listen
= sock_no_listen
,
439 .shutdown
= sock_no_shutdown
,
440 .sendmsg
= pn_socket_sendmsg
,
441 .recvmsg
= sock_common_recvmsg
,
442 .mmap
= sock_no_mmap
,
445 const struct proto_ops phonet_stream_ops
= {
447 .owner
= THIS_MODULE
,
448 .release
= pn_socket_release
,
449 .bind
= pn_socket_bind
,
450 .connect
= pn_socket_connect
,
451 .socketpair
= sock_no_socketpair
,
452 .accept
= pn_socket_accept
,
453 .getname
= pn_socket_getname
,
454 .poll
= pn_socket_poll
,
455 .ioctl
= pn_socket_ioctl
,
456 .listen
= pn_socket_listen
,
457 .shutdown
= sock_no_shutdown
,
458 .setsockopt
= sock_common_setsockopt
,
459 .getsockopt
= sock_common_getsockopt
,
460 .sendmsg
= pn_socket_sendmsg
,
461 .recvmsg
= sock_common_recvmsg
,
462 .mmap
= sock_no_mmap
,
464 EXPORT_SYMBOL(phonet_stream_ops
);
466 /* allocate port for a socket */
467 int pn_sock_get_port(struct sock
*sk
, unsigned short sport
)
470 struct net
*net
= sock_net(sk
);
471 struct pn_sock
*pn
= pn_sk(sk
);
472 struct sockaddr_pn try_sa
;
475 memset(&try_sa
, 0, sizeof(struct sockaddr_pn
));
476 try_sa
.spn_family
= AF_PHONET
;
477 WARN_ON(!mutex_is_locked(&port_mutex
));
479 /* search free port */
480 int port
, pmin
, pmax
;
482 phonet_get_local_port_range(&pmin
, &pmax
);
483 for (port
= pmin
; port
<= pmax
; port
++) {
485 if (port_cur
< pmin
|| port_cur
> pmax
)
488 pn_sockaddr_set_port(&try_sa
, port_cur
);
489 tmpsk
= pn_find_sock_by_sa(net
, &try_sa
);
497 /* try to find specific port */
498 pn_sockaddr_set_port(&try_sa
, sport
);
499 tmpsk
= pn_find_sock_by_sa(net
, &try_sa
);
501 /* No sock there! We can use that port... */
506 /* the port must be in use already */
510 pn
->sobject
= pn_object(pn_addr(pn
->sobject
), sport
);
513 EXPORT_SYMBOL(pn_sock_get_port
);
515 #ifdef CONFIG_PROC_FS
516 static struct sock
*pn_sock_get_idx(struct seq_file
*seq
, loff_t pos
)
518 struct net
*net
= seq_file_net(seq
);
519 struct hlist_head
*hlist
= pnsocks
.hlist
;
523 for (h
= 0; h
< PN_HASHSIZE
; h
++) {
524 sk_for_each_rcu(sknode
, hlist
) {
525 if (!net_eq(net
, sock_net(sknode
)))
536 static struct sock
*pn_sock_get_next(struct seq_file
*seq
, struct sock
*sk
)
538 struct net
*net
= seq_file_net(seq
);
542 while (sk
&& !net_eq(net
, sock_net(sk
)));
547 static void *pn_sock_seq_start(struct seq_file
*seq
, loff_t
*pos
)
551 return *pos
? pn_sock_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
554 static void *pn_sock_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
558 if (v
== SEQ_START_TOKEN
)
559 sk
= pn_sock_get_idx(seq
, 0);
561 sk
= pn_sock_get_next(seq
, v
);
566 static void pn_sock_seq_stop(struct seq_file
*seq
, void *v
)
572 static int pn_sock_seq_show(struct seq_file
*seq
, void *v
)
574 seq_setwidth(seq
, 127);
575 if (v
== SEQ_START_TOKEN
)
576 seq_puts(seq
, "pt loc rem rs st tx_queue rx_queue "
577 " uid inode ref pointer drops");
580 struct pn_sock
*pn
= pn_sk(sk
);
582 seq_printf(seq
, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu "
584 sk
->sk_protocol
, pn
->sobject
, pn
->dobject
,
585 pn
->resource
, sk
->sk_state
,
586 sk_wmem_alloc_get(sk
), sk_rmem_alloc_get(sk
),
587 from_kuid_munged(seq_user_ns(seq
), sock_i_uid(sk
)),
589 refcount_read(&sk
->sk_refcnt
), sk
,
590 atomic_read(&sk
->sk_drops
));
596 const struct seq_operations pn_sock_seq_ops
= {
597 .start
= pn_sock_seq_start
,
598 .next
= pn_sock_seq_next
,
599 .stop
= pn_sock_seq_stop
,
600 .show
= pn_sock_seq_show
,
605 struct sock
*sk
[256];
609 * Find and hold socket based on resource.
611 struct sock
*pn_find_sock_by_res(struct net
*net
, u8 res
)
615 if (!net_eq(net
, &init_net
))
619 sk
= rcu_dereference(pnres
.sk
[res
]);
626 static DEFINE_MUTEX(resource_mutex
);
628 int pn_sock_bind_res(struct sock
*sk
, u8 res
)
630 int ret
= -EADDRINUSE
;
632 if (!net_eq(sock_net(sk
), &init_net
))
634 if (!capable(CAP_SYS_ADMIN
))
636 if (pn_socket_autobind(sk
->sk_socket
))
639 mutex_lock(&resource_mutex
);
640 if (pnres
.sk
[res
] == NULL
) {
642 rcu_assign_pointer(pnres
.sk
[res
], sk
);
645 mutex_unlock(&resource_mutex
);
649 int pn_sock_unbind_res(struct sock
*sk
, u8 res
)
653 if (!capable(CAP_SYS_ADMIN
))
656 mutex_lock(&resource_mutex
);
657 if (pnres
.sk
[res
] == sk
) {
658 RCU_INIT_POINTER(pnres
.sk
[res
], NULL
);
661 mutex_unlock(&resource_mutex
);
670 void pn_sock_unbind_all_res(struct sock
*sk
)
672 unsigned int res
, match
= 0;
674 mutex_lock(&resource_mutex
);
675 for (res
= 0; res
< 256; res
++) {
676 if (pnres
.sk
[res
] == sk
) {
677 RCU_INIT_POINTER(pnres
.sk
[res
], NULL
);
681 mutex_unlock(&resource_mutex
);
687 /* Caller is responsible for RCU sync before final sock_put() */
690 #ifdef CONFIG_PROC_FS
691 static struct sock
**pn_res_get_idx(struct seq_file
*seq
, loff_t pos
)
693 struct net
*net
= seq_file_net(seq
);
696 if (!net_eq(net
, &init_net
))
699 for (i
= 0; i
< 256; i
++) {
700 if (pnres
.sk
[i
] == NULL
)
709 static struct sock
**pn_res_get_next(struct seq_file
*seq
, struct sock
**sk
)
711 struct net
*net
= seq_file_net(seq
);
714 BUG_ON(!net_eq(net
, &init_net
));
716 for (i
= (sk
- pnres
.sk
) + 1; i
< 256; i
++)
722 static void *pn_res_seq_start(struct seq_file
*seq
, loff_t
*pos
)
723 __acquires(resource_mutex
)
725 mutex_lock(&resource_mutex
);
726 return *pos
? pn_res_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
729 static void *pn_res_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
733 if (v
== SEQ_START_TOKEN
)
734 sk
= pn_res_get_idx(seq
, 0);
736 sk
= pn_res_get_next(seq
, v
);
741 static void pn_res_seq_stop(struct seq_file
*seq
, void *v
)
742 __releases(resource_mutex
)
744 mutex_unlock(&resource_mutex
);
747 static int pn_res_seq_show(struct seq_file
*seq
, void *v
)
749 seq_setwidth(seq
, 63);
750 if (v
== SEQ_START_TOKEN
)
751 seq_puts(seq
, "rs uid inode");
753 struct sock
**psk
= v
;
754 struct sock
*sk
= *psk
;
756 seq_printf(seq
, "%02X %5u %lu",
757 (int) (psk
- pnres
.sk
),
758 from_kuid_munged(seq_user_ns(seq
), sock_i_uid(sk
)),
765 const struct seq_operations pn_res_seq_ops
= {
766 .start
= pn_res_seq_start
,
767 .next
= pn_res_seq_next
,
768 .stop
= pn_res_seq_stop
,
769 .show
= pn_res_seq_show
,