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 spin_lock_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
);
85 spin_lock_bh(&pnsocks
.lock
);
87 sk_for_each(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
))
111 spin_unlock_bh(&pnsocks
.lock
);
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
;
122 spin_lock(&pnsocks
.lock
);
123 for (h
= 0; h
< PN_HASHSIZE
; h
++) {
124 struct hlist_node
*node
;
127 sk_for_each(sknode
, node
, hlist
) {
128 struct sk_buff
*clone
;
130 if (!net_eq(sock_net(sknode
), net
))
132 if (!sock_flag(sknode
, SOCK_BROADCAST
))
135 clone
= skb_clone(skb
, GFP_ATOMIC
);
138 sk_receive_skb(sknode
, clone
, 0);
143 spin_unlock(&pnsocks
.lock
);
146 void pn_sock_hash(struct sock
*sk
)
148 struct hlist_head
*hlist
= pn_hash_list(pn_sk(sk
)->sobject
);
150 spin_lock_bh(&pnsocks
.lock
);
151 sk_add_node(sk
, hlist
);
152 spin_unlock_bh(&pnsocks
.lock
);
154 EXPORT_SYMBOL(pn_sock_hash
);
156 void pn_sock_unhash(struct sock
*sk
)
158 spin_lock_bh(&pnsocks
.lock
);
159 sk_del_node_init(sk
);
160 spin_unlock_bh(&pnsocks
.lock
);
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_accept(struct socket
*sock
, struct socket
*newsock
,
230 struct sock
*sk
= sock
->sk
;
234 newsk
= sk
->sk_prot
->accept(sk
, flags
, &err
);
239 sock_graft(newsk
, newsock
);
240 newsock
->state
= SS_CONNECTED
;
245 static int pn_socket_getname(struct socket
*sock
, struct sockaddr
*addr
,
246 int *sockaddr_len
, int peer
)
248 struct sock
*sk
= sock
->sk
;
249 struct pn_sock
*pn
= pn_sk(sk
);
251 memset(addr
, 0, sizeof(struct sockaddr_pn
));
252 addr
->sa_family
= AF_PHONET
;
253 if (!peer
) /* Race with bind() here is userland's problem. */
254 pn_sockaddr_set_object((struct sockaddr_pn
*)addr
,
257 *sockaddr_len
= sizeof(struct sockaddr_pn
);
261 static unsigned int pn_socket_poll(struct file
*file
, struct socket
*sock
,
264 struct sock
*sk
= sock
->sk
;
265 struct pep_sock
*pn
= pep_sk(sk
);
266 unsigned int mask
= 0;
268 poll_wait(file
, sk_sleep(sk
), wait
);
270 switch (sk
->sk_state
) {
272 return hlist_empty(&pn
->ackq
) ? 0 : POLLIN
;
277 if (!skb_queue_empty(&sk
->sk_receive_queue
))
278 mask
|= POLLIN
| POLLRDNORM
;
279 if (!skb_queue_empty(&pn
->ctrlreq_queue
))
281 if (!mask
&& sk
->sk_state
== TCP_CLOSE_WAIT
)
284 if (sk
->sk_state
== TCP_ESTABLISHED
&&
285 atomic_read(&sk
->sk_wmem_alloc
) < sk
->sk_sndbuf
&&
286 atomic_read(&pn
->tx_credits
))
287 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
292 static int pn_socket_ioctl(struct socket
*sock
, unsigned int cmd
,
295 struct sock
*sk
= sock
->sk
;
296 struct pn_sock
*pn
= pn_sk(sk
);
298 if (cmd
== SIOCPNGETOBJECT
) {
299 struct net_device
*dev
;
303 if (get_user(handle
, (__u16 __user
*)arg
))
307 if (sk
->sk_bound_dev_if
)
308 dev
= dev_get_by_index(sock_net(sk
),
309 sk
->sk_bound_dev_if
);
311 dev
= phonet_device_get(sock_net(sk
));
312 if (dev
&& (dev
->flags
& IFF_UP
))
313 saddr
= phonet_address_get(dev
, pn_addr(handle
));
320 if (saddr
== PN_NO_ADDR
)
321 return -EHOSTUNREACH
;
323 handle
= pn_object(saddr
, pn_port(pn
->sobject
));
324 return put_user(handle
, (__u16 __user
*)arg
);
327 return sk
->sk_prot
->ioctl(sk
, cmd
, arg
);
330 static int pn_socket_listen(struct socket
*sock
, int backlog
)
332 struct sock
*sk
= sock
->sk
;
335 if (sock
->state
!= SS_UNCONNECTED
)
337 if (pn_socket_autobind(sock
))
341 if (sk
->sk_state
!= TCP_CLOSE
) {
346 sk
->sk_state
= TCP_LISTEN
;
347 sk
->sk_ack_backlog
= 0;
348 sk
->sk_max_ack_backlog
= backlog
;
354 static int pn_socket_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
355 struct msghdr
*m
, size_t total_len
)
357 struct sock
*sk
= sock
->sk
;
359 if (pn_socket_autobind(sock
))
362 return sk
->sk_prot
->sendmsg(iocb
, sk
, m
, total_len
);
365 const struct proto_ops phonet_dgram_ops
= {
367 .owner
= THIS_MODULE
,
368 .release
= pn_socket_release
,
369 .bind
= pn_socket_bind
,
370 .connect
= sock_no_connect
,
371 .socketpair
= sock_no_socketpair
,
372 .accept
= sock_no_accept
,
373 .getname
= pn_socket_getname
,
374 .poll
= datagram_poll
,
375 .ioctl
= pn_socket_ioctl
,
376 .listen
= sock_no_listen
,
377 .shutdown
= sock_no_shutdown
,
378 .setsockopt
= sock_no_setsockopt
,
379 .getsockopt
= sock_no_getsockopt
,
381 .compat_setsockopt
= sock_no_setsockopt
,
382 .compat_getsockopt
= sock_no_getsockopt
,
384 .sendmsg
= pn_socket_sendmsg
,
385 .recvmsg
= sock_common_recvmsg
,
386 .mmap
= sock_no_mmap
,
387 .sendpage
= sock_no_sendpage
,
390 const struct proto_ops phonet_stream_ops
= {
392 .owner
= THIS_MODULE
,
393 .release
= pn_socket_release
,
394 .bind
= pn_socket_bind
,
395 .connect
= sock_no_connect
,
396 .socketpair
= sock_no_socketpair
,
397 .accept
= pn_socket_accept
,
398 .getname
= pn_socket_getname
,
399 .poll
= pn_socket_poll
,
400 .ioctl
= pn_socket_ioctl
,
401 .listen
= pn_socket_listen
,
402 .shutdown
= sock_no_shutdown
,
403 .setsockopt
= sock_common_setsockopt
,
404 .getsockopt
= sock_common_getsockopt
,
406 .compat_setsockopt
= compat_sock_common_setsockopt
,
407 .compat_getsockopt
= compat_sock_common_getsockopt
,
409 .sendmsg
= pn_socket_sendmsg
,
410 .recvmsg
= sock_common_recvmsg
,
411 .mmap
= sock_no_mmap
,
412 .sendpage
= sock_no_sendpage
,
414 EXPORT_SYMBOL(phonet_stream_ops
);
416 /* allocate port for a socket */
417 int pn_sock_get_port(struct sock
*sk
, unsigned short sport
)
420 struct net
*net
= sock_net(sk
);
421 struct pn_sock
*pn
= pn_sk(sk
);
422 struct sockaddr_pn try_sa
;
425 memset(&try_sa
, 0, sizeof(struct sockaddr_pn
));
426 try_sa
.spn_family
= AF_PHONET
;
427 WARN_ON(!mutex_is_locked(&port_mutex
));
429 /* search free port */
430 int port
, pmin
, pmax
;
432 phonet_get_local_port_range(&pmin
, &pmax
);
433 for (port
= pmin
; port
<= pmax
; port
++) {
435 if (port_cur
< pmin
|| port_cur
> pmax
)
438 pn_sockaddr_set_port(&try_sa
, port_cur
);
439 tmpsk
= pn_find_sock_by_sa(net
, &try_sa
);
447 /* try to find specific port */
448 pn_sockaddr_set_port(&try_sa
, sport
);
449 tmpsk
= pn_find_sock_by_sa(net
, &try_sa
);
451 /* No sock there! We can use that port... */
456 /* the port must be in use already */
460 pn
->sobject
= pn_object(pn_addr(pn
->sobject
), sport
);
463 EXPORT_SYMBOL(pn_sock_get_port
);
465 #ifdef CONFIG_PROC_FS
466 static struct sock
*pn_sock_get_idx(struct seq_file
*seq
, loff_t pos
)
468 struct net
*net
= seq_file_net(seq
);
469 struct hlist_head
*hlist
= pnsocks
.hlist
;
470 struct hlist_node
*node
;
474 for (h
= 0; h
< PN_HASHSIZE
; h
++) {
475 sk_for_each(sknode
, node
, hlist
) {
476 if (!net_eq(net
, sock_net(sknode
)))
487 static struct sock
*pn_sock_get_next(struct seq_file
*seq
, struct sock
*sk
)
489 struct net
*net
= seq_file_net(seq
);
493 while (sk
&& !net_eq(net
, sock_net(sk
)));
498 static void *pn_sock_seq_start(struct seq_file
*seq
, loff_t
*pos
)
499 __acquires(pnsocks
.lock
)
501 spin_lock_bh(&pnsocks
.lock
);
502 return *pos
? pn_sock_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
505 static void *pn_sock_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
509 if (v
== SEQ_START_TOKEN
)
510 sk
= pn_sock_get_idx(seq
, 0);
512 sk
= pn_sock_get_next(seq
, v
);
517 static void pn_sock_seq_stop(struct seq_file
*seq
, void *v
)
518 __releases(pnsocks
.lock
)
520 spin_unlock_bh(&pnsocks
.lock
);
523 static int pn_sock_seq_show(struct seq_file
*seq
, void *v
)
527 if (v
== SEQ_START_TOKEN
)
528 seq_printf(seq
, "%s%n", "pt loc rem rs st tx_queue rx_queue "
529 " uid inode ref pointer drops", &len
);
532 struct pn_sock
*pn
= pn_sk(sk
);
534 seq_printf(seq
, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu "
536 sk
->sk_protocol
, pn
->sobject
, 0, pn
->resource
,
538 sk_wmem_alloc_get(sk
), sk_rmem_alloc_get(sk
),
539 sock_i_uid(sk
), sock_i_ino(sk
),
540 atomic_read(&sk
->sk_refcnt
), sk
,
541 atomic_read(&sk
->sk_drops
), &len
);
543 seq_printf(seq
, "%*s\n", 127 - len
, "");
547 static const struct seq_operations pn_sock_seq_ops
= {
548 .start
= pn_sock_seq_start
,
549 .next
= pn_sock_seq_next
,
550 .stop
= pn_sock_seq_stop
,
551 .show
= pn_sock_seq_show
,
554 static int pn_sock_open(struct inode
*inode
, struct file
*file
)
556 return seq_open_net(inode
, file
, &pn_sock_seq_ops
,
557 sizeof(struct seq_net_private
));
560 const struct file_operations pn_sock_seq_fops
= {
561 .owner
= THIS_MODULE
,
562 .open
= pn_sock_open
,
565 .release
= seq_release_net
,