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>
29 #include <net/tcp_states.h>
31 #include <linux/phonet.h>
32 #include <net/phonet/phonet.h>
33 #include <net/phonet/pn_dev.h>
35 static int pn_socket_release(struct socket
*sock
)
37 struct sock
*sk
= sock
->sk
;
41 sk
->sk_prot
->close(sk
, 0);
47 struct hlist_head hlist
;
50 .hlist
= HLIST_HEAD_INIT
,
51 .lock
= __SPIN_LOCK_UNLOCKED(pnsocks
.lock
),
55 * Find address based on socket address, match only certain fields.
56 * Also grab sock if it was found. Remember to sock_put it later.
58 struct sock
*pn_find_sock_by_sa(const struct sockaddr_pn
*spn
)
60 struct hlist_node
*node
;
62 struct sock
*rval
= NULL
;
63 u16 obj
= pn_sockaddr_get_object(spn
);
64 u8 res
= spn
->spn_resource
;
66 spin_lock_bh(&pnsocks
.lock
);
68 sk_for_each(sknode
, node
, &pnsocks
.hlist
) {
69 struct pn_sock
*pn
= pn_sk(sknode
);
70 BUG_ON(!pn
->sobject
); /* unbound socket */
73 /* Look up socket by port */
74 if (pn_port(pn
->sobject
) != pn_port(obj
))
77 /* If port is zero, look up by resource */
78 if (pn
->resource
!= res
)
81 if (pn_addr(pn
->sobject
)
82 && pn_addr(pn
->sobject
) != pn_addr(obj
))
90 spin_unlock_bh(&pnsocks
.lock
);
96 void pn_sock_hash(struct sock
*sk
)
98 spin_lock_bh(&pnsocks
.lock
);
99 sk_add_node(sk
, &pnsocks
.hlist
);
100 spin_unlock_bh(&pnsocks
.lock
);
102 EXPORT_SYMBOL(pn_sock_hash
);
104 void pn_sock_unhash(struct sock
*sk
)
106 spin_lock_bh(&pnsocks
.lock
);
107 sk_del_node_init(sk
);
108 spin_unlock_bh(&pnsocks
.lock
);
110 EXPORT_SYMBOL(pn_sock_unhash
);
112 static int pn_socket_bind(struct socket
*sock
, struct sockaddr
*addr
, int len
)
114 struct sock
*sk
= sock
->sk
;
115 struct pn_sock
*pn
= pn_sk(sk
);
116 struct sockaddr_pn
*spn
= (struct sockaddr_pn
*)addr
;
121 if (sk
->sk_prot
->bind
)
122 return sk
->sk_prot
->bind(sk
, addr
, len
);
124 if (len
< sizeof(struct sockaddr_pn
))
126 if (spn
->spn_family
!= AF_PHONET
)
127 return -EAFNOSUPPORT
;
129 handle
= pn_sockaddr_get_object((struct sockaddr_pn
*)addr
);
130 saddr
= pn_addr(handle
);
131 if (saddr
&& phonet_address_lookup(saddr
))
132 return -EADDRNOTAVAIL
;
135 if (sk
->sk_state
!= TCP_CLOSE
|| pn_port(pn
->sobject
)) {
136 err
= -EINVAL
; /* attempt to rebind */
139 err
= sk
->sk_prot
->get_port(sk
, pn_port(handle
));
143 /* get_port() sets the port, bind() sets the address if applicable */
144 pn
->sobject
= pn_object(saddr
, pn_port(pn
->sobject
));
145 pn
->resource
= spn
->spn_resource
;
147 /* Enable RX on the socket */
148 sk
->sk_prot
->hash(sk
);
154 static int pn_socket_autobind(struct socket
*sock
)
156 struct sockaddr_pn sa
;
159 memset(&sa
, 0, sizeof(sa
));
160 sa
.spn_family
= AF_PHONET
;
161 err
= pn_socket_bind(sock
, (struct sockaddr
*)&sa
,
162 sizeof(struct sockaddr_pn
));
165 BUG_ON(!pn_port(pn_sk(sock
->sk
)->sobject
));
166 return 0; /* socket was already bound */
169 static int pn_socket_getname(struct socket
*sock
, struct sockaddr
*addr
,
170 int *sockaddr_len
, int peer
)
172 struct sock
*sk
= sock
->sk
;
173 struct pn_sock
*pn
= pn_sk(sk
);
175 memset(addr
, 0, sizeof(struct sockaddr_pn
));
176 addr
->sa_family
= AF_PHONET
;
177 if (!peer
) /* Race with bind() here is userland's problem. */
178 pn_sockaddr_set_object((struct sockaddr_pn
*)addr
,
181 *sockaddr_len
= sizeof(struct sockaddr_pn
);
185 static int pn_socket_ioctl(struct socket
*sock
, unsigned int cmd
,
188 struct sock
*sk
= sock
->sk
;
189 struct pn_sock
*pn
= pn_sk(sk
);
191 if (cmd
== SIOCPNGETOBJECT
) {
192 struct net_device
*dev
;
196 if (get_user(handle
, (__u16 __user
*)arg
))
200 if (sk
->sk_bound_dev_if
)
201 dev
= dev_get_by_index(sock_net(sk
),
202 sk
->sk_bound_dev_if
);
204 dev
= phonet_device_get(sock_net(sk
));
205 if (dev
&& (dev
->flags
& IFF_UP
))
206 saddr
= phonet_address_get(dev
, pn_addr(handle
));
213 if (saddr
== PN_NO_ADDR
)
214 return -EHOSTUNREACH
;
216 handle
= pn_object(saddr
, pn_port(pn
->sobject
));
217 return put_user(handle
, (__u16 __user
*)arg
);
220 return sk
->sk_prot
->ioctl(sk
, cmd
, arg
);
223 static int pn_socket_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
224 struct msghdr
*m
, size_t total_len
)
226 struct sock
*sk
= sock
->sk
;
228 if (pn_socket_autobind(sock
))
231 return sk
->sk_prot
->sendmsg(iocb
, sk
, m
, total_len
);
234 const struct proto_ops phonet_dgram_ops
= {
236 .owner
= THIS_MODULE
,
237 .release
= pn_socket_release
,
238 .bind
= pn_socket_bind
,
239 .connect
= sock_no_connect
,
240 .socketpair
= sock_no_socketpair
,
241 .accept
= sock_no_accept
,
242 .getname
= pn_socket_getname
,
243 .poll
= datagram_poll
,
244 .ioctl
= pn_socket_ioctl
,
245 .listen
= sock_no_listen
,
246 .shutdown
= sock_no_shutdown
,
247 .setsockopt
= sock_no_setsockopt
,
248 .getsockopt
= sock_no_getsockopt
,
250 .compat_setsockopt
= sock_no_setsockopt
,
251 .compat_getsockopt
= sock_no_getsockopt
,
253 .sendmsg
= pn_socket_sendmsg
,
254 .recvmsg
= sock_common_recvmsg
,
255 .mmap
= sock_no_mmap
,
256 .sendpage
= sock_no_sendpage
,
259 static DEFINE_MUTEX(port_mutex
);
261 /* allocate port for a socket */
262 int pn_sock_get_port(struct sock
*sk
, unsigned short sport
)
265 struct pn_sock
*pn
= pn_sk(sk
);
266 struct sockaddr_pn try_sa
;
269 memset(&try_sa
, 0, sizeof(struct sockaddr_pn
));
270 try_sa
.spn_family
= AF_PHONET
;
272 mutex_lock(&port_mutex
);
275 /* search free port */
276 int port
, pmin
, pmax
;
278 phonet_get_local_port_range(&pmin
, &pmax
);
279 for (port
= pmin
; port
<= pmax
; port
++) {
281 if (port_cur
< pmin
|| port_cur
> pmax
)
284 pn_sockaddr_set_port(&try_sa
, port_cur
);
285 tmpsk
= pn_find_sock_by_sa(&try_sa
);
293 /* try to find specific port */
294 pn_sockaddr_set_port(&try_sa
, sport
);
295 tmpsk
= pn_find_sock_by_sa(&try_sa
);
297 /* No sock there! We can use that port... */
302 mutex_unlock(&port_mutex
);
304 /* the port must be in use already */
308 mutex_unlock(&port_mutex
);
309 pn
->sobject
= pn_object(pn_addr(pn
->sobject
), sport
);
312 EXPORT_SYMBOL(pn_sock_get_port
);