Merge branch 'for-3.9' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq
[linux-2.6/cjktty.git] / net / ieee802154 / af_ieee802154.c
blob40e606f3788f11f5dcdeb59420ff18a4ec24eab1
1 /*
2 * IEEE802154.4 socket interface
4 * Copyright 2007, 2008 Siemens AG
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 * Written by:
20 * Sergey Lapin <slapin@ossfans.org>
21 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
24 #include <linux/net.h>
25 #include <linux/capability.h>
26 #include <linux/module.h>
27 #include <linux/if_arp.h>
28 #include <linux/if.h>
29 #include <linux/termios.h> /* For TIOCOUTQ/INQ */
30 #include <linux/list.h>
31 #include <linux/slab.h>
32 #include <net/datalink.h>
33 #include <net/psnap.h>
34 #include <net/sock.h>
35 #include <net/tcp_states.h>
36 #include <net/route.h>
38 #include <net/af_ieee802154.h>
39 #include <net/ieee802154_netdev.h>
41 #include "af802154.h"
44 * Utility function for families
46 struct net_device *ieee802154_get_dev(struct net *net,
47 struct ieee802154_addr *addr)
49 struct net_device *dev = NULL;
50 struct net_device *tmp;
51 u16 pan_id, short_addr;
53 switch (addr->addr_type) {
54 case IEEE802154_ADDR_LONG:
55 rcu_read_lock();
56 dev = dev_getbyhwaddr_rcu(net, ARPHRD_IEEE802154, addr->hwaddr);
57 if (dev)
58 dev_hold(dev);
59 rcu_read_unlock();
60 break;
61 case IEEE802154_ADDR_SHORT:
62 if (addr->pan_id == 0xffff ||
63 addr->short_addr == IEEE802154_ADDR_UNDEF ||
64 addr->short_addr == 0xffff)
65 break;
67 rtnl_lock();
69 for_each_netdev(net, tmp) {
70 if (tmp->type != ARPHRD_IEEE802154)
71 continue;
73 pan_id = ieee802154_mlme_ops(tmp)->get_pan_id(tmp);
74 short_addr =
75 ieee802154_mlme_ops(tmp)->get_short_addr(tmp);
77 if (pan_id == addr->pan_id &&
78 short_addr == addr->short_addr) {
79 dev = tmp;
80 dev_hold(dev);
81 break;
85 rtnl_unlock();
86 break;
87 default:
88 pr_warning("Unsupported ieee802154 address type: %d\n",
89 addr->addr_type);
90 break;
93 return dev;
96 static int ieee802154_sock_release(struct socket *sock)
98 struct sock *sk = sock->sk;
100 if (sk) {
101 sock->sk = NULL;
102 sk->sk_prot->close(sk, 0);
104 return 0;
106 static int ieee802154_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
107 struct msghdr *msg, size_t len)
109 struct sock *sk = sock->sk;
111 return sk->sk_prot->sendmsg(iocb, sk, msg, len);
114 static int ieee802154_sock_bind(struct socket *sock, struct sockaddr *uaddr,
115 int addr_len)
117 struct sock *sk = sock->sk;
119 if (sk->sk_prot->bind)
120 return sk->sk_prot->bind(sk, uaddr, addr_len);
122 return sock_no_bind(sock, uaddr, addr_len);
125 static int ieee802154_sock_connect(struct socket *sock, struct sockaddr *uaddr,
126 int addr_len, int flags)
128 struct sock *sk = sock->sk;
130 if (addr_len < sizeof(uaddr->sa_family))
131 return -EINVAL;
133 if (uaddr->sa_family == AF_UNSPEC)
134 return sk->sk_prot->disconnect(sk, flags);
136 return sk->sk_prot->connect(sk, uaddr, addr_len);
139 static int ieee802154_dev_ioctl(struct sock *sk, struct ifreq __user *arg,
140 unsigned int cmd)
142 struct ifreq ifr;
143 int ret = -ENOIOCTLCMD;
144 struct net_device *dev;
146 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
147 return -EFAULT;
149 ifr.ifr_name[IFNAMSIZ-1] = 0;
151 dev_load(sock_net(sk), ifr.ifr_name);
152 dev = dev_get_by_name(sock_net(sk), ifr.ifr_name);
154 if (!dev)
155 return -ENODEV;
157 if (dev->type == ARPHRD_IEEE802154 && dev->netdev_ops->ndo_do_ioctl)
158 ret = dev->netdev_ops->ndo_do_ioctl(dev, &ifr, cmd);
160 if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq)))
161 ret = -EFAULT;
162 dev_put(dev);
164 return ret;
167 static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd,
168 unsigned long arg)
170 struct sock *sk = sock->sk;
172 switch (cmd) {
173 case SIOCGSTAMP:
174 return sock_get_timestamp(sk, (struct timeval __user *)arg);
175 case SIOCGSTAMPNS:
176 return sock_get_timestampns(sk, (struct timespec __user *)arg);
177 case SIOCGIFADDR:
178 case SIOCSIFADDR:
179 return ieee802154_dev_ioctl(sk, (struct ifreq __user *)arg,
180 cmd);
181 default:
182 if (!sk->sk_prot->ioctl)
183 return -ENOIOCTLCMD;
184 return sk->sk_prot->ioctl(sk, cmd, arg);
188 static const struct proto_ops ieee802154_raw_ops = {
189 .family = PF_IEEE802154,
190 .owner = THIS_MODULE,
191 .release = ieee802154_sock_release,
192 .bind = ieee802154_sock_bind,
193 .connect = ieee802154_sock_connect,
194 .socketpair = sock_no_socketpair,
195 .accept = sock_no_accept,
196 .getname = sock_no_getname,
197 .poll = datagram_poll,
198 .ioctl = ieee802154_sock_ioctl,
199 .listen = sock_no_listen,
200 .shutdown = sock_no_shutdown,
201 .setsockopt = sock_common_setsockopt,
202 .getsockopt = sock_common_getsockopt,
203 .sendmsg = ieee802154_sock_sendmsg,
204 .recvmsg = sock_common_recvmsg,
205 .mmap = sock_no_mmap,
206 .sendpage = sock_no_sendpage,
207 #ifdef CONFIG_COMPAT
208 .compat_setsockopt = compat_sock_common_setsockopt,
209 .compat_getsockopt = compat_sock_common_getsockopt,
210 #endif
213 static const struct proto_ops ieee802154_dgram_ops = {
214 .family = PF_IEEE802154,
215 .owner = THIS_MODULE,
216 .release = ieee802154_sock_release,
217 .bind = ieee802154_sock_bind,
218 .connect = ieee802154_sock_connect,
219 .socketpair = sock_no_socketpair,
220 .accept = sock_no_accept,
221 .getname = sock_no_getname,
222 .poll = datagram_poll,
223 .ioctl = ieee802154_sock_ioctl,
224 .listen = sock_no_listen,
225 .shutdown = sock_no_shutdown,
226 .setsockopt = sock_common_setsockopt,
227 .getsockopt = sock_common_getsockopt,
228 .sendmsg = ieee802154_sock_sendmsg,
229 .recvmsg = sock_common_recvmsg,
230 .mmap = sock_no_mmap,
231 .sendpage = sock_no_sendpage,
232 #ifdef CONFIG_COMPAT
233 .compat_setsockopt = compat_sock_common_setsockopt,
234 .compat_getsockopt = compat_sock_common_getsockopt,
235 #endif
240 * Create a socket. Initialise the socket, blank the addresses
241 * set the state.
243 static int ieee802154_create(struct net *net, struct socket *sock,
244 int protocol, int kern)
246 struct sock *sk;
247 int rc;
248 struct proto *proto;
249 const struct proto_ops *ops;
251 if (!net_eq(net, &init_net))
252 return -EAFNOSUPPORT;
254 switch (sock->type) {
255 case SOCK_RAW:
256 proto = &ieee802154_raw_prot;
257 ops = &ieee802154_raw_ops;
258 break;
259 case SOCK_DGRAM:
260 proto = &ieee802154_dgram_prot;
261 ops = &ieee802154_dgram_ops;
262 break;
263 default:
264 rc = -ESOCKTNOSUPPORT;
265 goto out;
268 rc = -ENOMEM;
269 sk = sk_alloc(net, PF_IEEE802154, GFP_KERNEL, proto);
270 if (!sk)
271 goto out;
272 rc = 0;
274 sock->ops = ops;
276 sock_init_data(sock, sk);
277 /* FIXME: sk->sk_destruct */
278 sk->sk_family = PF_IEEE802154;
280 /* Checksums on by default */
281 sock_set_flag(sk, SOCK_ZAPPED);
283 if (sk->sk_prot->hash)
284 sk->sk_prot->hash(sk);
286 if (sk->sk_prot->init) {
287 rc = sk->sk_prot->init(sk);
288 if (rc)
289 sk_common_release(sk);
291 out:
292 return rc;
295 static const struct net_proto_family ieee802154_family_ops = {
296 .family = PF_IEEE802154,
297 .create = ieee802154_create,
298 .owner = THIS_MODULE,
301 static int ieee802154_rcv(struct sk_buff *skb, struct net_device *dev,
302 struct packet_type *pt, struct net_device *orig_dev)
304 if (!netif_running(dev))
305 goto drop;
306 pr_debug("got frame, type %d, dev %p\n", dev->type, dev);
307 #ifdef DEBUG
308 print_hex_dump_bytes("ieee802154_rcv ", DUMP_PREFIX_NONE, skb->data, skb->len);
309 #endif
311 if (!net_eq(dev_net(dev), &init_net))
312 goto drop;
314 ieee802154_raw_deliver(dev, skb);
316 if (dev->type != ARPHRD_IEEE802154)
317 goto drop;
319 if (skb->pkt_type != PACKET_OTHERHOST)
320 return ieee802154_dgram_deliver(dev, skb);
322 drop:
323 kfree_skb(skb);
324 return NET_RX_DROP;
328 static struct packet_type ieee802154_packet_type = {
329 .type = __constant_htons(ETH_P_IEEE802154),
330 .func = ieee802154_rcv,
333 static int __init af_ieee802154_init(void)
335 int rc = -EINVAL;
337 rc = proto_register(&ieee802154_raw_prot, 1);
338 if (rc)
339 goto out;
341 rc = proto_register(&ieee802154_dgram_prot, 1);
342 if (rc)
343 goto err_dgram;
345 /* Tell SOCKET that we are alive */
346 rc = sock_register(&ieee802154_family_ops);
347 if (rc)
348 goto err_sock;
349 dev_add_pack(&ieee802154_packet_type);
351 rc = 0;
352 goto out;
354 err_sock:
355 proto_unregister(&ieee802154_dgram_prot);
356 err_dgram:
357 proto_unregister(&ieee802154_raw_prot);
358 out:
359 return rc;
361 static void __exit af_ieee802154_remove(void)
363 dev_remove_pack(&ieee802154_packet_type);
364 sock_unregister(PF_IEEE802154);
365 proto_unregister(&ieee802154_dgram_prot);
366 proto_unregister(&ieee802154_raw_prot);
369 module_init(af_ieee802154_init);
370 module_exit(af_ieee802154_remove);
372 MODULE_LICENSE("GPL");
373 MODULE_ALIAS_NETPROTO(PF_IEEE802154);