blob: 351d9a94ec2faa429612d6b7da26b09b63e49a25 [file] [log] [blame]
Sergey Lapin9ec76712009-06-08 12:18:48 +00001/*
2 * IEEE802154.4 socket interface
3 *
4 * Copyright 2007, 2008 Siemens AG
5 *
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.
9 *
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.
14 *
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.
18 *
19 * Written by:
20 * Sergey Lapin <slapin@ossfans.org>
21 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
22 */
23
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Sergey Lapin9ec76712009-06-08 12:18:48 +000032#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>
37
Dmitry Eremin-Solenikovf0166e52009-07-23 16:56:29 +040038#include <net/af_ieee802154.h>
39#include <net/ieee802154_netdev.h>
Sergey Lapin9ec76712009-06-08 12:18:48 +000040
41#include "af802154.h"
42
Sergey Lapin9ec76712009-06-08 12:18:48 +000043/*
44 * Utility function for families
45 */
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010046struct net_device*
47ieee802154_get_dev(struct net *net, const struct ieee802154_addr *addr)
Sergey Lapin9ec76712009-06-08 12:18:48 +000048{
49 struct net_device *dev = NULL;
50 struct net_device *tmp;
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010051 __le16 pan_id, short_addr;
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010052 u8 hwaddr[IEEE802154_ADDR_LEN];
Sergey Lapin9ec76712009-06-08 12:18:48 +000053
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010054 switch (addr->mode) {
Sergey Lapin9ec76712009-06-08 12:18:48 +000055 case IEEE802154_ADDR_LONG:
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010056 ieee802154_devaddr_to_raw(hwaddr, addr->extended_addr);
Eric Dumazet941666c2010-12-05 01:23:53 +000057 rcu_read_lock();
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010058 dev = dev_getbyhwaddr_rcu(net, ARPHRD_IEEE802154, hwaddr);
Sergey Lapin9ec76712009-06-08 12:18:48 +000059 if (dev)
60 dev_hold(dev);
Eric Dumazet941666c2010-12-05 01:23:53 +000061 rcu_read_unlock();
Sergey Lapin9ec76712009-06-08 12:18:48 +000062 break;
63 case IEEE802154_ADDR_SHORT:
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010064 if (addr->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST) ||
65 addr->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
Alexander Aring06324f22014-03-20 14:57:02 +010066 addr->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST))
Sergey Lapin9ec76712009-06-08 12:18:48 +000067 break;
68
69 rtnl_lock();
70
71 for_each_netdev(net, tmp) {
72 if (tmp->type != ARPHRD_IEEE802154)
73 continue;
74
75 pan_id = ieee802154_mlme_ops(tmp)->get_pan_id(tmp);
76 short_addr =
77 ieee802154_mlme_ops(tmp)->get_short_addr(tmp);
78
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010079 if (pan_id == addr->pan_id &&
80 short_addr == addr->short_addr) {
Sergey Lapin9ec76712009-06-08 12:18:48 +000081 dev = tmp;
82 dev_hold(dev);
83 break;
84 }
85 }
86
87 rtnl_unlock();
88 break;
89 default:
90 pr_warning("Unsupported ieee802154 address type: %d\n",
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010091 addr->mode);
Sergey Lapin9ec76712009-06-08 12:18:48 +000092 break;
93 }
94
95 return dev;
96}
97
98static int ieee802154_sock_release(struct socket *sock)
99{
100 struct sock *sk = sock->sk;
101
102 if (sk) {
103 sock->sk = NULL;
104 sk->sk_prot->close(sk, 0);
105 }
106 return 0;
107}
108static int ieee802154_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
109 struct msghdr *msg, size_t len)
110{
111 struct sock *sk = sock->sk;
112
113 return sk->sk_prot->sendmsg(iocb, sk, msg, len);
114}
115
116static int ieee802154_sock_bind(struct socket *sock, struct sockaddr *uaddr,
117 int addr_len)
118{
119 struct sock *sk = sock->sk;
120
121 if (sk->sk_prot->bind)
122 return sk->sk_prot->bind(sk, uaddr, addr_len);
123
124 return sock_no_bind(sock, uaddr, addr_len);
125}
126
127static int ieee802154_sock_connect(struct socket *sock, struct sockaddr *uaddr,
128 int addr_len, int flags)
129{
130 struct sock *sk = sock->sk;
131
Changli Gao6503d962010-03-31 22:58:26 +0000132 if (addr_len < sizeof(uaddr->sa_family))
133 return -EINVAL;
134
Sergey Lapin9ec76712009-06-08 12:18:48 +0000135 if (uaddr->sa_family == AF_UNSPEC)
136 return sk->sk_prot->disconnect(sk, flags);
137
138 return sk->sk_prot->connect(sk, uaddr, addr_len);
139}
140
141static int ieee802154_dev_ioctl(struct sock *sk, struct ifreq __user *arg,
142 unsigned int cmd)
143{
144 struct ifreq ifr;
Dmitry Eremin-Solenikov45a41d12009-08-02 14:12:01 +0400145 int ret = -ENOIOCTLCMD;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000146 struct net_device *dev;
147
148 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
149 return -EFAULT;
150
151 ifr.ifr_name[IFNAMSIZ-1] = 0;
152
153 dev_load(sock_net(sk), ifr.ifr_name);
154 dev = dev_get_by_name(sock_net(sk), ifr.ifr_name);
Dmitry Eremin-Solenikov45a41d12009-08-02 14:12:01 +0400155
Stefan Schmidt93c0c8b2010-04-26 11:20:32 -0700156 if (!dev)
157 return -ENODEV;
158
Dmitry Eremin-Solenikov929122cd2009-08-14 20:00:20 +0400159 if (dev->type == ARPHRD_IEEE802154 && dev->netdev_ops->ndo_do_ioctl)
Sergey Lapin9ec76712009-06-08 12:18:48 +0000160 ret = dev->netdev_ops->ndo_do_ioctl(dev, &ifr, cmd);
161
162 if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq)))
163 ret = -EFAULT;
164 dev_put(dev);
165
166 return ret;
167}
168
169static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd,
170 unsigned long arg)
171{
172 struct sock *sk = sock->sk;
173
174 switch (cmd) {
175 case SIOCGSTAMP:
176 return sock_get_timestamp(sk, (struct timeval __user *)arg);
177 case SIOCGSTAMPNS:
178 return sock_get_timestampns(sk, (struct timespec __user *)arg);
179 case SIOCGIFADDR:
180 case SIOCSIFADDR:
181 return ieee802154_dev_ioctl(sk, (struct ifreq __user *)arg,
182 cmd);
183 default:
184 if (!sk->sk_prot->ioctl)
185 return -ENOIOCTLCMD;
186 return sk->sk_prot->ioctl(sk, cmd, arg);
187 }
188}
189
190static const struct proto_ops ieee802154_raw_ops = {
191 .family = PF_IEEE802154,
192 .owner = THIS_MODULE,
193 .release = ieee802154_sock_release,
194 .bind = ieee802154_sock_bind,
195 .connect = ieee802154_sock_connect,
196 .socketpair = sock_no_socketpair,
197 .accept = sock_no_accept,
198 .getname = sock_no_getname,
199 .poll = datagram_poll,
200 .ioctl = ieee802154_sock_ioctl,
201 .listen = sock_no_listen,
202 .shutdown = sock_no_shutdown,
203 .setsockopt = sock_common_setsockopt,
204 .getsockopt = sock_common_getsockopt,
205 .sendmsg = ieee802154_sock_sendmsg,
206 .recvmsg = sock_common_recvmsg,
207 .mmap = sock_no_mmap,
208 .sendpage = sock_no_sendpage,
209#ifdef CONFIG_COMPAT
210 .compat_setsockopt = compat_sock_common_setsockopt,
211 .compat_getsockopt = compat_sock_common_getsockopt,
212#endif
213};
214
215static const struct proto_ops ieee802154_dgram_ops = {
216 .family = PF_IEEE802154,
217 .owner = THIS_MODULE,
218 .release = ieee802154_sock_release,
219 .bind = ieee802154_sock_bind,
220 .connect = ieee802154_sock_connect,
221 .socketpair = sock_no_socketpair,
222 .accept = sock_no_accept,
223 .getname = sock_no_getname,
224 .poll = datagram_poll,
225 .ioctl = ieee802154_sock_ioctl,
226 .listen = sock_no_listen,
227 .shutdown = sock_no_shutdown,
228 .setsockopt = sock_common_setsockopt,
229 .getsockopt = sock_common_getsockopt,
230 .sendmsg = ieee802154_sock_sendmsg,
231 .recvmsg = sock_common_recvmsg,
232 .mmap = sock_no_mmap,
233 .sendpage = sock_no_sendpage,
234#ifdef CONFIG_COMPAT
235 .compat_setsockopt = compat_sock_common_setsockopt,
236 .compat_getsockopt = compat_sock_common_getsockopt,
237#endif
238};
239
240
241/*
242 * Create a socket. Initialise the socket, blank the addresses
243 * set the state.
244 */
245static int ieee802154_create(struct net *net, struct socket *sock,
Eric Paris3f378b62009-11-05 22:18:14 -0800246 int protocol, int kern)
Sergey Lapin9ec76712009-06-08 12:18:48 +0000247{
248 struct sock *sk;
249 int rc;
250 struct proto *proto;
251 const struct proto_ops *ops;
252
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800253 if (!net_eq(net, &init_net))
Sergey Lapin9ec76712009-06-08 12:18:48 +0000254 return -EAFNOSUPPORT;
255
256 switch (sock->type) {
257 case SOCK_RAW:
258 proto = &ieee802154_raw_prot;
259 ops = &ieee802154_raw_ops;
260 break;
261 case SOCK_DGRAM:
262 proto = &ieee802154_dgram_prot;
263 ops = &ieee802154_dgram_ops;
264 break;
265 default:
266 rc = -ESOCKTNOSUPPORT;
267 goto out;
268 }
269
270 rc = -ENOMEM;
271 sk = sk_alloc(net, PF_IEEE802154, GFP_KERNEL, proto);
272 if (!sk)
273 goto out;
274 rc = 0;
275
276 sock->ops = ops;
277
278 sock_init_data(sock, sk);
279 /* FIXME: sk->sk_destruct */
280 sk->sk_family = PF_IEEE802154;
281
282 /* Checksums on by default */
283 sock_set_flag(sk, SOCK_ZAPPED);
284
285 if (sk->sk_prot->hash)
286 sk->sk_prot->hash(sk);
287
288 if (sk->sk_prot->init) {
289 rc = sk->sk_prot->init(sk);
290 if (rc)
291 sk_common_release(sk);
292 }
293out:
294 return rc;
295}
296
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000297static const struct net_proto_family ieee802154_family_ops = {
Sergey Lapin9ec76712009-06-08 12:18:48 +0000298 .family = PF_IEEE802154,
299 .create = ieee802154_create,
300 .owner = THIS_MODULE,
301};
302
303static int ieee802154_rcv(struct sk_buff *skb, struct net_device *dev,
304 struct packet_type *pt, struct net_device *orig_dev)
305{
Sergey Lapin9ec76712009-06-08 12:18:48 +0000306 if (!netif_running(dev))
Alexander Smirnovfa1da882011-06-29 12:51:37 +0000307 goto drop;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000308 pr_debug("got frame, type %d, dev %p\n", dev->type, dev);
Dmitry Baryshkov25502bd2009-06-18 04:16:46 +0000309#ifdef DEBUG
310 print_hex_dump_bytes("ieee802154_rcv ", DUMP_PREFIX_NONE, skb->data, skb->len);
311#endif
Sergey Lapin9ec76712009-06-08 12:18:48 +0000312
313 if (!net_eq(dev_net(dev), &init_net))
314 goto drop;
315
316 ieee802154_raw_deliver(dev, skb);
317
318 if (dev->type != ARPHRD_IEEE802154)
319 goto drop;
320
321 if (skb->pkt_type != PACKET_OTHERHOST)
322 return ieee802154_dgram_deliver(dev, skb);
323
324drop:
325 kfree_skb(skb);
326 return NET_RX_DROP;
327}
328
329
330static struct packet_type ieee802154_packet_type = {
Joe Perchesec633eb2014-03-12 10:04:18 -0700331 .type = htons(ETH_P_IEEE802154),
Sergey Lapin9ec76712009-06-08 12:18:48 +0000332 .func = ieee802154_rcv,
333};
334
335static int __init af_ieee802154_init(void)
336{
337 int rc = -EINVAL;
338
339 rc = proto_register(&ieee802154_raw_prot, 1);
340 if (rc)
341 goto out;
342
343 rc = proto_register(&ieee802154_dgram_prot, 1);
344 if (rc)
345 goto err_dgram;
346
347 /* Tell SOCKET that we are alive */
348 rc = sock_register(&ieee802154_family_ops);
349 if (rc)
350 goto err_sock;
351 dev_add_pack(&ieee802154_packet_type);
352
353 rc = 0;
354 goto out;
355
356err_sock:
357 proto_unregister(&ieee802154_dgram_prot);
358err_dgram:
359 proto_unregister(&ieee802154_raw_prot);
360out:
361 return rc;
362}
363static void __exit af_ieee802154_remove(void)
364{
365 dev_remove_pack(&ieee802154_packet_type);
366 sock_unregister(PF_IEEE802154);
367 proto_unregister(&ieee802154_dgram_prot);
368 proto_unregister(&ieee802154_raw_prot);
369}
370
371module_init(af_ieee802154_init);
372module_exit(af_ieee802154_remove);
373
374MODULE_LICENSE("GPL");
375MODULE_ALIAS_NETPROTO(PF_IEEE802154);