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/af_ieee802154.h>
38 #include <net/ieee802154_netdev.h>
43 * Utility function for families
45 struct net_device *ieee802154_get_dev(struct net *net,
46 struct ieee802154_addr *addr)
48 struct net_device *dev = NULL;
49 struct net_device *tmp;
50 u16 pan_id, short_addr;
52 switch (addr->addr_type) {
53 case IEEE802154_ADDR_LONG:
55 dev = dev_getbyhwaddr(net, ARPHRD_IEEE802154, addr->hwaddr);
60 case IEEE802154_ADDR_SHORT:
61 if (addr->pan_id == 0xffff ||
62 addr->short_addr == IEEE802154_ADDR_UNDEF ||
63 addr->short_addr == 0xffff)
68 for_each_netdev(net, tmp) {
69 if (tmp->type != ARPHRD_IEEE802154)
72 pan_id = ieee802154_mlme_ops(tmp)->get_pan_id(tmp);
74 ieee802154_mlme_ops(tmp)->get_short_addr(tmp);
76 if (pan_id == addr->pan_id &&
77 short_addr == addr->short_addr) {
87 pr_warning("Unsupported ieee802154 address type: %d\n",
95 static int ieee802154_sock_release(struct socket *sock)
97 struct sock *sk = sock->sk;
101 sk->sk_prot->close(sk, 0);
105 static int ieee802154_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
106 struct msghdr *msg, size_t len)
108 struct sock *sk = sock->sk;
110 return sk->sk_prot->sendmsg(iocb, sk, msg, len);
113 static int ieee802154_sock_bind(struct socket *sock, struct sockaddr *uaddr,
116 struct sock *sk = sock->sk;
118 if (sk->sk_prot->bind)
119 return sk->sk_prot->bind(sk, uaddr, addr_len);
121 return sock_no_bind(sock, uaddr, addr_len);
124 static int ieee802154_sock_connect(struct socket *sock, struct sockaddr *uaddr,
125 int addr_len, int flags)
127 struct sock *sk = sock->sk;
129 if (uaddr->sa_family == AF_UNSPEC)
130 return sk->sk_prot->disconnect(sk, flags);
132 return sk->sk_prot->connect(sk, uaddr, addr_len);
135 static int ieee802154_dev_ioctl(struct sock *sk, struct ifreq __user *arg,
139 int ret = -ENOIOCTLCMD;
140 struct net_device *dev;
142 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
145 ifr.ifr_name[IFNAMSIZ-1] = 0;
147 dev_load(sock_net(sk), ifr.ifr_name);
148 dev = dev_get_by_name(sock_net(sk), ifr.ifr_name);
150 if (dev->type == ARPHRD_IEEE802154 && dev->netdev_ops->ndo_do_ioctl)
151 ret = dev->netdev_ops->ndo_do_ioctl(dev, &ifr, cmd);
153 if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq)))
160 static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd,
163 struct sock *sk = sock->sk;
167 return sock_get_timestamp(sk, (struct timeval __user *)arg);
169 return sock_get_timestampns(sk, (struct timespec __user *)arg);
172 return ieee802154_dev_ioctl(sk, (struct ifreq __user *)arg,
175 if (!sk->sk_prot->ioctl)
177 return sk->sk_prot->ioctl(sk, cmd, arg);
181 static const struct proto_ops ieee802154_raw_ops = {
182 .family = PF_IEEE802154,
183 .owner = THIS_MODULE,
184 .release = ieee802154_sock_release,
185 .bind = ieee802154_sock_bind,
186 .connect = ieee802154_sock_connect,
187 .socketpair = sock_no_socketpair,
188 .accept = sock_no_accept,
189 .getname = sock_no_getname,
190 .poll = datagram_poll,
191 .ioctl = ieee802154_sock_ioctl,
192 .listen = sock_no_listen,
193 .shutdown = sock_no_shutdown,
194 .setsockopt = sock_common_setsockopt,
195 .getsockopt = sock_common_getsockopt,
196 .sendmsg = ieee802154_sock_sendmsg,
197 .recvmsg = sock_common_recvmsg,
198 .mmap = sock_no_mmap,
199 .sendpage = sock_no_sendpage,
201 .compat_setsockopt = compat_sock_common_setsockopt,
202 .compat_getsockopt = compat_sock_common_getsockopt,
206 static const struct proto_ops ieee802154_dgram_ops = {
207 .family = PF_IEEE802154,
208 .owner = THIS_MODULE,
209 .release = ieee802154_sock_release,
210 .bind = ieee802154_sock_bind,
211 .connect = ieee802154_sock_connect,
212 .socketpair = sock_no_socketpair,
213 .accept = sock_no_accept,
214 .getname = sock_no_getname,
215 .poll = datagram_poll,
216 .ioctl = ieee802154_sock_ioctl,
217 .listen = sock_no_listen,
218 .shutdown = sock_no_shutdown,
219 .setsockopt = sock_common_setsockopt,
220 .getsockopt = sock_common_getsockopt,
221 .sendmsg = ieee802154_sock_sendmsg,
222 .recvmsg = sock_common_recvmsg,
223 .mmap = sock_no_mmap,
224 .sendpage = sock_no_sendpage,
226 .compat_setsockopt = compat_sock_common_setsockopt,
227 .compat_getsockopt = compat_sock_common_getsockopt,
233 * Create a socket. Initialise the socket, blank the addresses
236 static int ieee802154_create(struct net *net, struct socket *sock,
242 const struct proto_ops *ops;
244 if (net != &init_net)
245 return -EAFNOSUPPORT;
247 switch (sock->type) {
249 proto = &ieee802154_raw_prot;
250 ops = &ieee802154_raw_ops;
253 proto = &ieee802154_dgram_prot;
254 ops = &ieee802154_dgram_ops;
257 rc = -ESOCKTNOSUPPORT;
262 sk = sk_alloc(net, PF_IEEE802154, GFP_KERNEL, proto);
269 sock_init_data(sock, sk);
270 /* FIXME: sk->sk_destruct */
271 sk->sk_family = PF_IEEE802154;
273 /* Checksums on by default */
274 sock_set_flag(sk, SOCK_ZAPPED);
276 if (sk->sk_prot->hash)
277 sk->sk_prot->hash(sk);
279 if (sk->sk_prot->init) {
280 rc = sk->sk_prot->init(sk);
282 sk_common_release(sk);
288 static struct net_proto_family ieee802154_family_ops = {
289 .family = PF_IEEE802154,
290 .create = ieee802154_create,
291 .owner = THIS_MODULE,
294 static int ieee802154_rcv(struct sk_buff *skb, struct net_device *dev,
295 struct packet_type *pt, struct net_device *orig_dev)
297 if (!netif_running(dev))
299 pr_debug("got frame, type %d, dev %p\n", dev->type, dev);
301 print_hex_dump_bytes("ieee802154_rcv ", DUMP_PREFIX_NONE, skb->data, skb->len);
304 if (!net_eq(dev_net(dev), &init_net))
307 ieee802154_raw_deliver(dev, skb);
309 if (dev->type != ARPHRD_IEEE802154)
312 if (skb->pkt_type != PACKET_OTHERHOST)
313 return ieee802154_dgram_deliver(dev, skb);
321 static struct packet_type ieee802154_packet_type = {
322 .type = __constant_htons(ETH_P_IEEE802154),
323 .func = ieee802154_rcv,
326 static int __init af_ieee802154_init(void)
330 rc = proto_register(&ieee802154_raw_prot, 1);
334 rc = proto_register(&ieee802154_dgram_prot, 1);
338 /* Tell SOCKET that we are alive */
339 rc = sock_register(&ieee802154_family_ops);
342 dev_add_pack(&ieee802154_packet_type);
348 proto_unregister(&ieee802154_dgram_prot);
350 proto_unregister(&ieee802154_raw_prot);
354 static void __exit af_ieee802154_remove(void)
356 dev_remove_pack(&ieee802154_packet_type);
357 sock_unregister(PF_IEEE802154);
358 proto_unregister(&ieee802154_dgram_prot);
359 proto_unregister(&ieee802154_raw_prot);
362 module_init(af_ieee802154_init);
363 module_exit(af_ieee802154_remove);
365 MODULE_LICENSE("GPL");
366 MODULE_ALIAS_NETPROTO(PF_IEEE802154);