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 <net/datalink.h>
32 #include <net/psnap.h>
34 #include <net/tcp_states.h>
35 #include <net/route.h>
37 #include <net/ieee802154/af_ieee802154.h>
38 #include <net/ieee802154/netdevice.h>
42 #define DBG_DUMP(data, len) { \
44 pr_debug("function: %s: data: len %d:\n", __func__, len); \
45 for (i = 0; i < len; i++) {\
46 pr_debug("%02x: %02x\n", i, (data)[i]); \
51 * Utility function for families
53 struct net_device *ieee802154_get_dev(struct net *net,
54 struct ieee802154_addr *addr)
56 struct net_device *dev = NULL;
57 struct net_device *tmp;
58 u16 pan_id, short_addr;
60 switch (addr->addr_type) {
61 case IEEE802154_ADDR_LONG:
63 dev = dev_getbyhwaddr(net, ARPHRD_IEEE802154, addr->hwaddr);
68 case IEEE802154_ADDR_SHORT:
69 if (addr->pan_id == 0xffff ||
70 addr->short_addr == IEEE802154_ADDR_UNDEF ||
71 addr->short_addr == 0xffff)
76 for_each_netdev(net, tmp) {
77 if (tmp->type != ARPHRD_IEEE802154)
80 pan_id = ieee802154_mlme_ops(tmp)->get_pan_id(tmp);
82 ieee802154_mlme_ops(tmp)->get_short_addr(tmp);
84 if (pan_id == addr->pan_id &&
85 short_addr == addr->short_addr) {
95 pr_warning("Unsupported ieee802154 address type: %d\n",
103 static int ieee802154_sock_release(struct socket *sock)
105 struct sock *sk = sock->sk;
109 sk->sk_prot->close(sk, 0);
113 static int ieee802154_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
114 struct msghdr *msg, size_t len)
116 struct sock *sk = sock->sk;
118 return sk->sk_prot->sendmsg(iocb, sk, msg, len);
121 static int ieee802154_sock_bind(struct socket *sock, struct sockaddr *uaddr,
124 struct sock *sk = sock->sk;
126 if (sk->sk_prot->bind)
127 return sk->sk_prot->bind(sk, uaddr, addr_len);
129 return sock_no_bind(sock, uaddr, addr_len);
132 static int ieee802154_sock_connect(struct socket *sock, struct sockaddr *uaddr,
133 int addr_len, int flags)
135 struct sock *sk = sock->sk;
137 if (uaddr->sa_family == AF_UNSPEC)
138 return sk->sk_prot->disconnect(sk, flags);
140 return sk->sk_prot->connect(sk, uaddr, addr_len);
143 static int ieee802154_dev_ioctl(struct sock *sk, struct ifreq __user *arg,
148 struct net_device *dev;
150 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
153 ifr.ifr_name[IFNAMSIZ-1] = 0;
155 dev_load(sock_net(sk), ifr.ifr_name);
156 dev = dev_get_by_name(sock_net(sk), ifr.ifr_name);
157 if (dev->type == ARPHRD_IEEE802154 ||
158 dev->type == ARPHRD_IEEE802154_PHY)
159 ret = dev->netdev_ops->ndo_do_ioctl(dev, &ifr, cmd);
161 if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq)))
168 static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd,
171 struct sock *sk = sock->sk;
175 return sock_get_timestamp(sk, (struct timeval __user *)arg);
177 return sock_get_timestampns(sk, (struct timespec __user *)arg);
180 return ieee802154_dev_ioctl(sk, (struct ifreq __user *)arg,
183 if (!sk->sk_prot->ioctl)
185 return sk->sk_prot->ioctl(sk, cmd, arg);
189 static const struct proto_ops ieee802154_raw_ops = {
190 .family = PF_IEEE802154,
191 .owner = THIS_MODULE,
192 .release = ieee802154_sock_release,
193 .bind = ieee802154_sock_bind,
194 .connect = ieee802154_sock_connect,
195 .socketpair = sock_no_socketpair,
196 .accept = sock_no_accept,
197 .getname = sock_no_getname,
198 .poll = datagram_poll,
199 .ioctl = ieee802154_sock_ioctl,
200 .listen = sock_no_listen,
201 .shutdown = sock_no_shutdown,
202 .setsockopt = sock_common_setsockopt,
203 .getsockopt = sock_common_getsockopt,
204 .sendmsg = ieee802154_sock_sendmsg,
205 .recvmsg = sock_common_recvmsg,
206 .mmap = sock_no_mmap,
207 .sendpage = sock_no_sendpage,
209 .compat_setsockopt = compat_sock_common_setsockopt,
210 .compat_getsockopt = compat_sock_common_getsockopt,
214 static const struct proto_ops ieee802154_dgram_ops = {
215 .family = PF_IEEE802154,
216 .owner = THIS_MODULE,
217 .release = ieee802154_sock_release,
218 .bind = ieee802154_sock_bind,
219 .connect = ieee802154_sock_connect,
220 .socketpair = sock_no_socketpair,
221 .accept = sock_no_accept,
222 .getname = sock_no_getname,
223 .poll = datagram_poll,
224 .ioctl = ieee802154_sock_ioctl,
225 .listen = sock_no_listen,
226 .shutdown = sock_no_shutdown,
227 .setsockopt = sock_common_setsockopt,
228 .getsockopt = sock_common_getsockopt,
229 .sendmsg = ieee802154_sock_sendmsg,
230 .recvmsg = sock_common_recvmsg,
231 .mmap = sock_no_mmap,
232 .sendpage = sock_no_sendpage,
234 .compat_setsockopt = compat_sock_common_setsockopt,
235 .compat_getsockopt = compat_sock_common_getsockopt,
241 * Create a socket. Initialise the socket, blank the addresses
244 static int ieee802154_create(struct net *net, struct socket *sock,
250 const struct proto_ops *ops;
252 if (net != &init_net)
253 return -EAFNOSUPPORT;
255 switch (sock->type) {
257 proto = &ieee802154_raw_prot;
258 ops = &ieee802154_raw_ops;
261 proto = &ieee802154_dgram_prot;
262 ops = &ieee802154_dgram_ops;
265 rc = -ESOCKTNOSUPPORT;
270 sk = sk_alloc(net, PF_IEEE802154, GFP_KERNEL, proto);
277 sock_init_data(sock, sk);
278 /* FIXME: sk->sk_destruct */
279 sk->sk_family = PF_IEEE802154;
281 /* Checksums on by default */
282 sock_set_flag(sk, SOCK_ZAPPED);
284 if (sk->sk_prot->hash)
285 sk->sk_prot->hash(sk);
287 if (sk->sk_prot->init) {
288 rc = sk->sk_prot->init(sk);
290 sk_common_release(sk);
296 static struct net_proto_family ieee802154_family_ops = {
297 .family = PF_IEEE802154,
298 .create = ieee802154_create,
299 .owner = THIS_MODULE,
302 static int ieee802154_rcv(struct sk_buff *skb, struct net_device *dev,
303 struct packet_type *pt, struct net_device *orig_dev)
305 DBG_DUMP(skb->data, skb->len);
306 if (!netif_running(dev))
308 pr_debug("got frame, type %d, dev %p\n", dev->type, dev);
310 if (!net_eq(dev_net(dev), &init_net))
313 ieee802154_raw_deliver(dev, skb);
315 if (dev->type != ARPHRD_IEEE802154)
318 if (skb->pkt_type != PACKET_OTHERHOST)
319 return ieee802154_dgram_deliver(dev, skb);
327 static struct packet_type ieee802154_packet_type = {
328 .type = __constant_htons(ETH_P_IEEE802154),
329 .func = ieee802154_rcv,
332 static int __init af_ieee802154_init(void)
336 rc = proto_register(&ieee802154_raw_prot, 1);
340 rc = proto_register(&ieee802154_dgram_prot, 1);
344 /* Tell SOCKET that we are alive */
345 rc = sock_register(&ieee802154_family_ops);
348 dev_add_pack(&ieee802154_packet_type);
354 proto_unregister(&ieee802154_dgram_prot);
356 proto_unregister(&ieee802154_raw_prot);
360 static void __exit af_ieee802154_remove(void)
362 dev_remove_pack(&ieee802154_packet_type);
363 sock_unregister(PF_IEEE802154);
364 proto_unregister(&ieee802154_dgram_prot);
365 proto_unregister(&ieee802154_raw_prot);
368 module_init(af_ieee802154_init);
369 module_exit(af_ieee802154_remove);
371 MODULE_LICENSE("GPL");
372 MODULE_ALIAS_NETPROTO(PF_IEEE802154);