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/kernel.h>
27 #include <linux/net.h>
28 #include <linux/poll.h>
30 #include <net/tcp_states.h>
32 #include <linux/phonet.h>
33 #include <net/phonet/phonet.h>
34 #include <net/phonet/pep.h>
35 #include <net/phonet/pn_dev.h>
37 static int pn_socket_release(struct socket
*sock
)
39 struct sock
*sk
= sock
->sk
;
43 sk
->sk_prot
->close(sk
, 0);
49 struct hlist_head hlist
;
52 .hlist
= HLIST_HEAD_INIT
,
53 .lock
= __SPIN_LOCK_UNLOCKED(pnsocks
.lock
),
57 * Find address based on socket address, match only certain fields.
58 * Also grab sock if it was found. Remember to sock_put it later.
60 struct sock
*pn_find_sock_by_sa(struct net
*net
, const struct sockaddr_pn
*spn
)
62 struct hlist_node
*node
;
64 struct sock
*rval
= NULL
;
65 u16 obj
= pn_sockaddr_get_object(spn
);
66 u8 res
= spn
->spn_resource
;
68 spin_lock_bh(&pnsocks
.lock
);
70 sk_for_each(sknode
, node
, &pnsocks
.hlist
) {
71 struct pn_sock
*pn
= pn_sk(sknode
);
72 BUG_ON(!pn
->sobject
); /* unbound socket */
74 if (!net_eq(sock_net(sknode
), net
))
77 /* Look up socket by port */
78 if (pn_port(pn
->sobject
) != pn_port(obj
))
81 /* If port is zero, look up by resource */
82 if (pn
->resource
!= res
)
85 if (pn_addr(pn
->sobject
)
86 && pn_addr(pn
->sobject
) != pn_addr(obj
))
94 spin_unlock_bh(&pnsocks
.lock
);
100 void pn_sock_hash(struct sock
*sk
)
102 spin_lock_bh(&pnsocks
.lock
);
103 sk_add_node(sk
, &pnsocks
.hlist
);
104 spin_unlock_bh(&pnsocks
.lock
);
106 EXPORT_SYMBOL(pn_sock_hash
);
108 void pn_sock_unhash(struct sock
*sk
)
110 spin_lock_bh(&pnsocks
.lock
);
111 sk_del_node_init(sk
);
112 spin_unlock_bh(&pnsocks
.lock
);
114 EXPORT_SYMBOL(pn_sock_unhash
);
116 static DEFINE_MUTEX(port_mutex
);
118 static int pn_socket_bind(struct socket
*sock
, struct sockaddr
*addr
, int len
)
120 struct sock
*sk
= sock
->sk
;
121 struct pn_sock
*pn
= pn_sk(sk
);
122 struct sockaddr_pn
*spn
= (struct sockaddr_pn
*)addr
;
127 if (sk
->sk_prot
->bind
)
128 return sk
->sk_prot
->bind(sk
, addr
, len
);
130 if (len
< sizeof(struct sockaddr_pn
))
132 if (spn
->spn_family
!= AF_PHONET
)
133 return -EAFNOSUPPORT
;
135 handle
= pn_sockaddr_get_object((struct sockaddr_pn
*)addr
);
136 saddr
= pn_addr(handle
);
137 if (saddr
&& phonet_address_lookup(sock_net(sk
), saddr
))
138 return -EADDRNOTAVAIL
;
141 if (sk
->sk_state
!= TCP_CLOSE
|| pn_port(pn
->sobject
)) {
142 err
= -EINVAL
; /* attempt to rebind */
145 WARN_ON(sk_hashed(sk
));
146 mutex_lock(&port_mutex
);
147 err
= sk
->sk_prot
->get_port(sk
, pn_port(handle
));
151 /* get_port() sets the port, bind() sets the address if applicable */
152 pn
->sobject
= pn_object(saddr
, pn_port(pn
->sobject
));
153 pn
->resource
= spn
->spn_resource
;
155 /* Enable RX on the socket */
156 sk
->sk_prot
->hash(sk
);
158 mutex_unlock(&port_mutex
);
164 static int pn_socket_autobind(struct socket
*sock
)
166 struct sockaddr_pn sa
;
169 memset(&sa
, 0, sizeof(sa
));
170 sa
.spn_family
= AF_PHONET
;
171 err
= pn_socket_bind(sock
, (struct sockaddr
*)&sa
,
172 sizeof(struct sockaddr_pn
));
175 BUG_ON(!pn_port(pn_sk(sock
->sk
)->sobject
));
176 return 0; /* socket was already bound */
179 static int pn_socket_accept(struct socket
*sock
, struct socket
*newsock
,
182 struct sock
*sk
= sock
->sk
;
186 newsk
= sk
->sk_prot
->accept(sk
, flags
, &err
);
191 sock_graft(newsk
, newsock
);
192 newsock
->state
= SS_CONNECTED
;
197 static int pn_socket_getname(struct socket
*sock
, struct sockaddr
*addr
,
198 int *sockaddr_len
, int peer
)
200 struct sock
*sk
= sock
->sk
;
201 struct pn_sock
*pn
= pn_sk(sk
);
203 memset(addr
, 0, sizeof(struct sockaddr_pn
));
204 addr
->sa_family
= AF_PHONET
;
205 if (!peer
) /* Race with bind() here is userland's problem. */
206 pn_sockaddr_set_object((struct sockaddr_pn
*)addr
,
209 *sockaddr_len
= sizeof(struct sockaddr_pn
);
213 static unsigned int pn_socket_poll(struct file
*file
, struct socket
*sock
,
216 struct sock
*sk
= sock
->sk
;
217 struct pep_sock
*pn
= pep_sk(sk
);
218 unsigned int mask
= 0;
220 poll_wait(file
, &sock
->wait
, wait
);
222 switch (sk
->sk_state
) {
224 return hlist_empty(&pn
->ackq
) ? 0 : POLLIN
;
229 if (!skb_queue_empty(&sk
->sk_receive_queue
))
230 mask
|= POLLIN
| POLLRDNORM
;
231 if (!skb_queue_empty(&pn
->ctrlreq_queue
))
233 if (!mask
&& sk
->sk_state
== TCP_CLOSE_WAIT
)
236 if (sk
->sk_state
== TCP_ESTABLISHED
&& atomic_read(&pn
->tx_credits
))
237 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
242 static int pn_socket_ioctl(struct socket
*sock
, unsigned int cmd
,
245 struct sock
*sk
= sock
->sk
;
246 struct pn_sock
*pn
= pn_sk(sk
);
248 if (cmd
== SIOCPNGETOBJECT
) {
249 struct net_device
*dev
;
253 if (get_user(handle
, (__u16 __user
*)arg
))
257 if (sk
->sk_bound_dev_if
)
258 dev
= dev_get_by_index(sock_net(sk
),
259 sk
->sk_bound_dev_if
);
261 dev
= phonet_device_get(sock_net(sk
));
262 if (dev
&& (dev
->flags
& IFF_UP
))
263 saddr
= phonet_address_get(dev
, pn_addr(handle
));
270 if (saddr
== PN_NO_ADDR
)
271 return -EHOSTUNREACH
;
273 handle
= pn_object(saddr
, pn_port(pn
->sobject
));
274 return put_user(handle
, (__u16 __user
*)arg
);
277 return sk
->sk_prot
->ioctl(sk
, cmd
, arg
);
280 static int pn_socket_listen(struct socket
*sock
, int backlog
)
282 struct sock
*sk
= sock
->sk
;
285 if (sock
->state
!= SS_UNCONNECTED
)
287 if (pn_socket_autobind(sock
))
291 if (sk
->sk_state
!= TCP_CLOSE
) {
296 sk
->sk_state
= TCP_LISTEN
;
297 sk
->sk_ack_backlog
= 0;
298 sk
->sk_max_ack_backlog
= backlog
;
304 static int pn_socket_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
305 struct msghdr
*m
, size_t total_len
)
307 struct sock
*sk
= sock
->sk
;
309 if (pn_socket_autobind(sock
))
312 return sk
->sk_prot
->sendmsg(iocb
, sk
, m
, total_len
);
315 const struct proto_ops phonet_dgram_ops
= {
317 .owner
= THIS_MODULE
,
318 .release
= pn_socket_release
,
319 .bind
= pn_socket_bind
,
320 .connect
= sock_no_connect
,
321 .socketpair
= sock_no_socketpair
,
322 .accept
= sock_no_accept
,
323 .getname
= pn_socket_getname
,
324 .poll
= datagram_poll
,
325 .ioctl
= pn_socket_ioctl
,
326 .listen
= sock_no_listen
,
327 .shutdown
= sock_no_shutdown
,
328 .setsockopt
= sock_no_setsockopt
,
329 .getsockopt
= sock_no_getsockopt
,
331 .compat_setsockopt
= sock_no_setsockopt
,
332 .compat_getsockopt
= sock_no_getsockopt
,
334 .sendmsg
= pn_socket_sendmsg
,
335 .recvmsg
= sock_common_recvmsg
,
336 .mmap
= sock_no_mmap
,
337 .sendpage
= sock_no_sendpage
,
340 const struct proto_ops phonet_stream_ops
= {
342 .owner
= THIS_MODULE
,
343 .release
= pn_socket_release
,
344 .bind
= pn_socket_bind
,
345 .connect
= sock_no_connect
,
346 .socketpair
= sock_no_socketpair
,
347 .accept
= pn_socket_accept
,
348 .getname
= pn_socket_getname
,
349 .poll
= pn_socket_poll
,
350 .ioctl
= pn_socket_ioctl
,
351 .listen
= pn_socket_listen
,
352 .shutdown
= sock_no_shutdown
,
353 .setsockopt
= sock_common_setsockopt
,
354 .getsockopt
= sock_common_getsockopt
,
356 .compat_setsockopt
= compat_sock_common_setsockopt
,
357 .compat_getsockopt
= compat_sock_common_getsockopt
,
359 .sendmsg
= pn_socket_sendmsg
,
360 .recvmsg
= sock_common_recvmsg
,
361 .mmap
= sock_no_mmap
,
362 .sendpage
= sock_no_sendpage
,
364 EXPORT_SYMBOL(phonet_stream_ops
);
366 /* allocate port for a socket */
367 int pn_sock_get_port(struct sock
*sk
, unsigned short sport
)
370 struct net
*net
= sock_net(sk
);
371 struct pn_sock
*pn
= pn_sk(sk
);
372 struct sockaddr_pn try_sa
;
375 memset(&try_sa
, 0, sizeof(struct sockaddr_pn
));
376 try_sa
.spn_family
= AF_PHONET
;
377 WARN_ON(!mutex_is_locked(&port_mutex
));
379 /* search free port */
380 int port
, pmin
, pmax
;
382 phonet_get_local_port_range(&pmin
, &pmax
);
383 for (port
= pmin
; port
<= pmax
; port
++) {
385 if (port_cur
< pmin
|| port_cur
> pmax
)
388 pn_sockaddr_set_port(&try_sa
, port_cur
);
389 tmpsk
= pn_find_sock_by_sa(net
, &try_sa
);
397 /* try to find specific port */
398 pn_sockaddr_set_port(&try_sa
, sport
);
399 tmpsk
= pn_find_sock_by_sa(net
, &try_sa
);
401 /* No sock there! We can use that port... */
406 /* the port must be in use already */
410 pn
->sobject
= pn_object(pn_addr(pn
->sobject
), sport
);
413 EXPORT_SYMBOL(pn_sock_get_port
);
415 #ifdef CONFIG_PROC_FS
416 static struct sock
*pn_sock_get_idx(struct seq_file
*seq
, loff_t pos
)
418 struct net
*net
= seq_file_net(seq
);
419 struct hlist_node
*node
;
422 sk_for_each(sknode
, node
, &pnsocks
.hlist
) {
423 if (!net_eq(net
, sock_net(sknode
)))
432 static struct sock
*pn_sock_get_next(struct seq_file
*seq
, struct sock
*sk
)
434 struct net
*net
= seq_file_net(seq
);
438 while (sk
&& !net_eq(net
, sock_net(sk
)));
443 static void *pn_sock_seq_start(struct seq_file
*seq
, loff_t
*pos
)
444 __acquires(pnsocks
.lock
)
446 spin_lock_bh(&pnsocks
.lock
);
447 return *pos
? pn_sock_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
450 static void *pn_sock_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
454 if (v
== SEQ_START_TOKEN
)
455 sk
= pn_sock_get_idx(seq
, 0);
457 sk
= pn_sock_get_next(seq
, v
);
462 static void pn_sock_seq_stop(struct seq_file
*seq
, void *v
)
463 __releases(pnsocks
.lock
)
465 spin_unlock_bh(&pnsocks
.lock
);
468 static int pn_sock_seq_show(struct seq_file
*seq
, void *v
)
472 if (v
== SEQ_START_TOKEN
)
473 seq_printf(seq
, "%s%n", "pt loc rem rs st tx_queue rx_queue "
474 " uid inode ref pointer drops", &len
);
477 struct pn_sock
*pn
= pn_sk(sk
);
479 seq_printf(seq
, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu "
481 sk
->sk_protocol
, pn
->sobject
, 0, pn
->resource
,
483 sk_wmem_alloc_get(sk
), sk_rmem_alloc_get(sk
),
484 sock_i_uid(sk
), sock_i_ino(sk
),
485 atomic_read(&sk
->sk_refcnt
), sk
,
486 atomic_read(&sk
->sk_drops
), &len
);
488 seq_printf(seq
, "%*s\n", 127 - len
, "");
492 static const struct seq_operations pn_sock_seq_ops
= {
493 .start
= pn_sock_seq_start
,
494 .next
= pn_sock_seq_next
,
495 .stop
= pn_sock_seq_stop
,
496 .show
= pn_sock_seq_show
,
499 static int pn_sock_open(struct inode
*inode
, struct file
*file
)
501 return seq_open_net(inode
, file
, &pn_sock_seq_ops
,
502 sizeof(struct seq_net_private
));
505 const struct file_operations pn_sock_seq_fops
= {
506 .owner
= THIS_MODULE
,
507 .open
= pn_sock_open
,
510 .release
= seq_release_net
,