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.
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>
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>
35 #include <net/tcp_states.h>
36 #include <net/route.h>
38 #include <net/af_ieee802154.h>
39 #include <net/ieee802154_netdev.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:
56 dev = dev_getbyhwaddr_rcu(net, ARPHRD_IEEE802154, addr->hwaddr);
61 case IEEE802154_ADDR_SHORT:
62 if (addr->pan_id == 0xffff ||
63 addr->short_addr == IEEE802154_ADDR_UNDEF ||
64 addr->short_addr == 0xffff)
69 for_each_netdev(net, tmp) {
70 if (tmp->type != ARPHRD_IEEE802154)
73 pan_id = ieee802154_mlme_ops(tmp)->get_pan_id(tmp);
75 ieee802154_mlme_ops(tmp)->get_short_addr(tmp);
77 if (pan_id == addr->pan_id &&
78 short_addr == addr->short_addr) {
88 pr_warning("Unsupported ieee802154 address type: %d\n",
96 static int ieee802154_sock_release(struct socket *sock)
98 struct sock *sk = sock->sk;
102 sk->sk_prot->close(sk, 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,
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))
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,
143 int ret = -ENOIOCTLCMD;
144 struct net_device *dev;
146 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
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);
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)))
167 static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd,
170 struct sock *sk = sock->sk;
174 return sock_get_timestamp(sk, (struct timeval __user *)arg);
176 return sock_get_timestampns(sk, (struct timespec __user *)arg);
179 return ieee802154_dev_ioctl(sk, (struct ifreq __user *)arg,
182 if (!sk->sk_prot->ioctl)
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,
208 .compat_setsockopt = compat_sock_common_setsockopt,
209 .compat_getsockopt = compat_sock_common_getsockopt,
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,
233 .compat_setsockopt = compat_sock_common_setsockopt,
234 .compat_getsockopt = compat_sock_common_getsockopt,
240 * Create a socket. Initialise the socket, blank the addresses
243 static int ieee802154_create(struct net *net, struct socket *sock,
244 int protocol, int kern)
249 const struct proto_ops *ops;
251 if (!net_eq(net, &init_net))
252 return -EAFNOSUPPORT;
254 switch (sock->type) {
256 proto = &ieee802154_raw_prot;
257 ops = &ieee802154_raw_ops;
260 proto = &ieee802154_dgram_prot;
261 ops = &ieee802154_dgram_ops;
264 rc = -ESOCKTNOSUPPORT;
269 sk = sk_alloc(net, PF_IEEE802154, GFP_KERNEL, proto);
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);
289 sk_common_release(sk);
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))
306 pr_debug("got frame, type %d, dev %p\n", dev->type, dev);
308 print_hex_dump_bytes("ieee802154_rcv ", DUMP_PREFIX_NONE, skb->data, skb->len);
311 if (!net_eq(dev_net(dev), &init_net))
314 ieee802154_raw_deliver(dev, skb);
316 if (dev->type != ARPHRD_IEEE802154)
319 if (skb->pkt_type != PACKET_OTHERHOST)
320 return ieee802154_dgram_deliver(dev, skb);
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)
337 rc = proto_register(&ieee802154_raw_prot, 1);
341 rc = proto_register(&ieee802154_dgram_prot, 1);
345 /* Tell SOCKET that we are alive */
346 rc = sock_register(&ieee802154_family_ops);
349 dev_add_pack(&ieee802154_packet_type);
355 proto_unregister(&ieee802154_dgram_prot);
357 proto_unregister(&ieee802154_raw_prot);
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);