blob: 786437bc0c08531785d3f5fa1deb5bff8efe9e62 [file] [log] [blame]
Sergey Lapin9ec76712009-06-08 12:18:48 +00001/*
Dmitry Eremin-Solenikov5fd72602010-10-22 01:25:25 +04002 * IEEE 802.15.4 dgram socket interface
Sergey Lapin9ec76712009-06-08 12:18:48 +00003 *
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 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
22 */
23
24#include <linux/net.h>
25#include <linux/module.h>
26#include <linux/if_arp.h>
27#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Sergey Lapin9ec76712009-06-08 12:18:48 +000029#include <net/sock.h>
Dmitry Eremin-Solenikovf0166e52009-07-23 16:56:29 +040030#include <net/af_ieee802154.h>
31#include <net/ieee802154.h>
32#include <net/ieee802154_netdev.h>
Sergey Lapin9ec76712009-06-08 12:18:48 +000033
34#include <asm/ioctls.h>
35
36#include "af802154.h"
37
38static HLIST_HEAD(dgram_head);
39static DEFINE_RWLOCK(dgram_lock);
40
41struct dgram_sock {
42 struct sock sk;
43
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010044 struct ieee802154_addr src_addr;
45 struct ieee802154_addr dst_addr;
Dmitry Baryshkov74eda552009-08-07 02:58:42 +000046
Eric Dumazet95c96172012-04-15 05:58:06 +000047 unsigned int bound:1;
48 unsigned int want_ack:1;
Sergey Lapin9ec76712009-06-08 12:18:48 +000049};
50
51static inline struct dgram_sock *dgram_sk(const struct sock *sk)
52{
53 return container_of(sk, struct dgram_sock, sk);
54}
55
Sergey Lapin9ec76712009-06-08 12:18:48 +000056static void dgram_hash(struct sock *sk)
57{
58 write_lock_bh(&dgram_lock);
59 sk_add_node(sk, &dgram_head);
60 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
61 write_unlock_bh(&dgram_lock);
62}
63
64static void dgram_unhash(struct sock *sk)
65{
66 write_lock_bh(&dgram_lock);
67 if (sk_del_node_init(sk))
68 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
69 write_unlock_bh(&dgram_lock);
70}
71
72static int dgram_init(struct sock *sk)
73{
74 struct dgram_sock *ro = dgram_sk(sk);
75
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010076 ro->dst_addr.mode = IEEE802154_ADDR_LONG;
Alexander Aring54af36e2014-03-20 14:57:03 +010077 ro->dst_addr.pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
Dmitry Baryshkov99eb8552009-08-07 02:58:43 +000078 ro->want_ack = 1;
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010079 memset(&ro->dst_addr.extended_addr, 0xff, IEEE802154_ADDR_LEN);
Sergey Lapin9ec76712009-06-08 12:18:48 +000080 return 0;
81}
82
83static void dgram_close(struct sock *sk, long timeout)
84{
85 sk_common_release(sk);
86}
87
88static int dgram_bind(struct sock *sk, struct sockaddr *uaddr, int len)
89{
90 struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010091 struct ieee802154_addr haddr;
Sergey Lapin9ec76712009-06-08 12:18:48 +000092 struct dgram_sock *ro = dgram_sk(sk);
Dmitry Baryshkov74eda552009-08-07 02:58:42 +000093 int err = -EINVAL;
Sergey Lapin9ec76712009-06-08 12:18:48 +000094 struct net_device *dev;
95
Dmitry Baryshkov74eda552009-08-07 02:58:42 +000096 lock_sock(sk);
97
Sergey Lapin9ec76712009-06-08 12:18:48 +000098 ro->bound = 0;
99
100 if (len < sizeof(*addr))
Dmitry Baryshkov74eda552009-08-07 02:58:42 +0000101 goto out;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000102
103 if (addr->family != AF_IEEE802154)
Dmitry Baryshkov74eda552009-08-07 02:58:42 +0000104 goto out;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000105
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100106 ieee802154_addr_from_sa(&haddr, &addr->addr);
107 dev = ieee802154_get_dev(sock_net(sk), &haddr);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000108 if (!dev) {
109 err = -ENODEV;
110 goto out;
111 }
112
113 if (dev->type != ARPHRD_IEEE802154) {
114 err = -ENODEV;
115 goto out_put;
116 }
117
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100118 ro->src_addr = haddr;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000119
120 ro->bound = 1;
Dmitry Baryshkov74eda552009-08-07 02:58:42 +0000121 err = 0;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000122out_put:
123 dev_put(dev);
124out:
125 release_sock(sk);
126
127 return err;
128}
129
130static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg)
131{
132 switch (cmd) {
133 case SIOCOUTQ:
134 {
Eric Dumazet31e6d362009-06-17 19:05:41 -0700135 int amount = sk_wmem_alloc_get(sk);
136
Sergey Lapin9ec76712009-06-08 12:18:48 +0000137 return put_user(amount, (int __user *)arg);
138 }
139
140 case SIOCINQ:
141 {
142 struct sk_buff *skb;
143 unsigned long amount;
144
145 amount = 0;
146 spin_lock_bh(&sk->sk_receive_queue.lock);
147 skb = skb_peek(&sk->sk_receive_queue);
148 if (skb != NULL) {
149 /*
150 * We will only return the amount
151 * of this packet since that is all
152 * that will be read.
153 */
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100154 amount = skb->len - ieee802154_hdr_length(skb);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000155 }
156 spin_unlock_bh(&sk->sk_receive_queue.lock);
157 return put_user(amount, (int __user *)arg);
158 }
159
160 }
161 return -ENOIOCTLCMD;
162}
163
164/* FIXME: autobind */
165static int dgram_connect(struct sock *sk, struct sockaddr *uaddr,
166 int len)
167{
168 struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
169 struct dgram_sock *ro = dgram_sk(sk);
170 int err = 0;
171
172 if (len < sizeof(*addr))
173 return -EINVAL;
174
175 if (addr->family != AF_IEEE802154)
176 return -EINVAL;
177
178 lock_sock(sk);
179
180 if (!ro->bound) {
181 err = -ENETUNREACH;
182 goto out;
183 }
184
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100185 ieee802154_addr_from_sa(&ro->dst_addr, &addr->addr);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000186
187out:
188 release_sock(sk);
189 return err;
190}
191
192static int dgram_disconnect(struct sock *sk, int flags)
193{
194 struct dgram_sock *ro = dgram_sk(sk);
195
196 lock_sock(sk);
197
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100198 ro->dst_addr.mode = IEEE802154_ADDR_LONG;
199 memset(&ro->dst_addr.extended_addr, 0xff, IEEE802154_ADDR_LEN);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000200
201 release_sock(sk);
202
203 return 0;
204}
205
206static int dgram_sendmsg(struct kiocb *iocb, struct sock *sk,
207 struct msghdr *msg, size_t size)
208{
209 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +0000210 unsigned int mtu;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000211 struct sk_buff *skb;
212 struct dgram_sock *ro = dgram_sk(sk);
Herbert Xuae641942011-11-18 02:20:04 +0000213 int hlen, tlen;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000214 int err;
215
216 if (msg->msg_flags & MSG_OOB) {
217 pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
218 return -EOPNOTSUPP;
219 }
220
221 if (!ro->bound)
222 dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
223 else
224 dev = ieee802154_get_dev(sock_net(sk), &ro->src_addr);
225
226 if (!dev) {
227 pr_debug("no dev\n");
228 err = -ENXIO;
229 goto out;
230 }
231 mtu = dev->mtu;
232 pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
233
Sasha Levin3da947b2012-07-02 01:29:55 +0000234 if (size > mtu) {
235 pr_debug("size = %Zu, mtu = %u\n", size, mtu);
Phoebe Buckheisterd1d73582014-03-14 21:24:04 +0100236 err = -EMSGSIZE;
Sasha Levin3da947b2012-07-02 01:29:55 +0000237 goto out_dev;
238 }
239
Herbert Xuae641942011-11-18 02:20:04 +0000240 hlen = LL_RESERVED_SPACE(dev);
241 tlen = dev->needed_tailroom;
242 skb = sock_alloc_send_skb(sk, hlen + tlen + size,
Sergey Lapin9ec76712009-06-08 12:18:48 +0000243 msg->msg_flags & MSG_DONTWAIT,
244 &err);
245 if (!skb)
246 goto out_dev;
247
Herbert Xuae641942011-11-18 02:20:04 +0000248 skb_reserve(skb, hlen);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000249
250 skb_reset_network_header(skb);
251
Dmitry Baryshkov99eb8552009-08-07 02:58:43 +0000252 mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA;
253 if (ro->want_ack)
254 mac_cb(skb)->flags |= MAC_CB_FLAG_ACKREQ;
255
Sergey Lapin9ec76712009-06-08 12:18:48 +0000256 mac_cb(skb)->seq = ieee802154_mlme_ops(dev)->get_dsn(dev);
257 err = dev_hard_header(skb, dev, ETH_P_IEEE802154, &ro->dst_addr,
258 ro->bound ? &ro->src_addr : NULL, size);
259 if (err < 0)
260 goto out_skb;
261
262 skb_reset_mac_header(skb);
263
264 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
265 if (err < 0)
266 goto out_skb;
267
Sergey Lapin9ec76712009-06-08 12:18:48 +0000268 skb->dev = dev;
269 skb->sk = sk;
270 skb->protocol = htons(ETH_P_IEEE802154);
271
272 dev_put(dev);
273
274 err = dev_queue_xmit(skb);
275 if (err > 0)
276 err = net_xmit_errno(err);
277
278 return err ?: size;
279
280out_skb:
281 kfree_skb(skb);
282out_dev:
283 dev_put(dev);
284out:
285 return err;
286}
287
288static int dgram_recvmsg(struct kiocb *iocb, struct sock *sk,
289 struct msghdr *msg, size_t len, int noblock, int flags,
290 int *addr_len)
291{
292 size_t copied = 0;
293 int err = -EOPNOTSUPP;
294 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100295 DECLARE_SOCKADDR(struct sockaddr_ieee802154 *, saddr, msg->msg_name);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000296
297 skb = skb_recv_datagram(sk, flags, noblock, &err);
298 if (!skb)
299 goto out;
300
301 copied = skb->len;
302 if (len < copied) {
303 msg->msg_flags |= MSG_TRUNC;
304 copied = len;
305 }
306
307 /* FIXME: skip headers if necessary ?! */
308 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
309 if (err)
310 goto done;
311
Neil Horman3b885782009-10-12 13:26:31 -0700312 sock_recv_ts_and_drops(msg, sk, skb);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000313
Stephen Röttger6364e6e2013-03-26 12:41:29 +0000314 if (saddr) {
315 saddr->family = AF_IEEE802154;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100316 ieee802154_addr_to_sa(&saddr->addr, &mac_cb(skb)->source);
Stephen Röttger6364e6e2013-03-26 12:41:29 +0000317 *addr_len = sizeof(*saddr);
Hannes Frederic Sowabceaa902013-11-18 04:20:45 +0100318 }
Stephen Röttger6364e6e2013-03-26 12:41:29 +0000319
Sergey Lapin9ec76712009-06-08 12:18:48 +0000320 if (flags & MSG_TRUNC)
321 copied = skb->len;
322done:
323 skb_free_datagram(sk, skb);
324out:
325 if (err)
326 return err;
327 return copied;
328}
329
330static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb)
331{
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100332 skb = skb_share_check(skb, GFP_ATOMIC);
333 if (!skb)
334 return NET_RX_DROP;
335
Sergey Lapin9ec76712009-06-08 12:18:48 +0000336 if (sock_queue_rcv_skb(sk, skb) < 0) {
Sergey Lapin9ec76712009-06-08 12:18:48 +0000337 kfree_skb(skb);
338 return NET_RX_DROP;
339 }
340
341 return NET_RX_SUCCESS;
342}
343
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100344static inline bool
345ieee802154_match_sock(__le64 hw_addr, __le16 pan_id, __le16 short_addr,
346 struct dgram_sock *ro)
Sergey Lapin9ec76712009-06-08 12:18:48 +0000347{
348 if (!ro->bound)
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100349 return true;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000350
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100351 if (ro->src_addr.mode == IEEE802154_ADDR_LONG &&
352 hw_addr == ro->src_addr.extended_addr)
353 return true;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000354
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100355 if (ro->src_addr.mode == IEEE802154_ADDR_SHORT &&
356 pan_id == ro->src_addr.pan_id &&
357 short_addr == ro->src_addr.short_addr)
358 return true;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000359
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100360 return false;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000361}
362
363int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
364{
365 struct sock *sk, *prev = NULL;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000366 int ret = NET_RX_SUCCESS;
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100367 __le16 pan_id, short_addr;
368 __le64 hw_addr;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000369
370 /* Data frame processing */
371 BUG_ON(dev->type != ARPHRD_IEEE802154);
372
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100373 pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
374 short_addr = ieee802154_mlme_ops(dev)->get_short_addr(dev);
375 hw_addr = ieee802154_devaddr_from_raw(dev->dev_addr);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000376
377 read_lock(&dgram_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800378 sk_for_each(sk, &dgram_head) {
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100379 if (ieee802154_match_sock(hw_addr, pan_id, short_addr,
380 dgram_sk(sk))) {
Sergey Lapin9ec76712009-06-08 12:18:48 +0000381 if (prev) {
382 struct sk_buff *clone;
383 clone = skb_clone(skb, GFP_ATOMIC);
384 if (clone)
385 dgram_rcv_skb(prev, clone);
386 }
387
388 prev = sk;
389 }
390 }
391
392 if (prev)
393 dgram_rcv_skb(prev, skb);
394 else {
395 kfree_skb(skb);
396 ret = NET_RX_DROP;
397 }
398 read_unlock(&dgram_lock);
399
400 return ret;
401}
402
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400403static int dgram_getsockopt(struct sock *sk, int level, int optname,
404 char __user *optval, int __user *optlen)
405{
Dmitry Baryshkov99eb8552009-08-07 02:58:43 +0000406 struct dgram_sock *ro = dgram_sk(sk);
407
408 int val, len;
409
410 if (level != SOL_IEEE802154)
411 return -EOPNOTSUPP;
412
413 if (get_user(len, optlen))
414 return -EFAULT;
415
416 len = min_t(unsigned int, len, sizeof(int));
417
418 switch (optname) {
419 case WPAN_WANTACK:
420 val = ro->want_ack;
421 break;
422 default:
423 return -ENOPROTOOPT;
424 }
425
426 if (put_user(len, optlen))
427 return -EFAULT;
428 if (copy_to_user(optval, &val, len))
429 return -EFAULT;
430 return 0;
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400431}
432
433static int dgram_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700434 char __user *optval, unsigned int optlen)
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400435{
Dmitry Baryshkov99eb8552009-08-07 02:58:43 +0000436 struct dgram_sock *ro = dgram_sk(sk);
437 int val;
438 int err = 0;
439
440 if (optlen < sizeof(int))
441 return -EINVAL;
442
443 if (get_user(val, (int __user *)optval))
444 return -EFAULT;
445
446 lock_sock(sk);
447
448 switch (optname) {
449 case WPAN_WANTACK:
450 ro->want_ack = !!val;
451 break;
452 default:
453 err = -ENOPROTOOPT;
454 break;
455 }
456
457 release_sock(sk);
458 return err;
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400459}
460
Sergey Lapin9ec76712009-06-08 12:18:48 +0000461struct proto ieee802154_dgram_prot = {
462 .name = "IEEE-802.15.4-MAC",
463 .owner = THIS_MODULE,
464 .obj_size = sizeof(struct dgram_sock),
465 .init = dgram_init,
466 .close = dgram_close,
467 .bind = dgram_bind,
468 .sendmsg = dgram_sendmsg,
469 .recvmsg = dgram_recvmsg,
470 .hash = dgram_hash,
471 .unhash = dgram_unhash,
472 .connect = dgram_connect,
473 .disconnect = dgram_disconnect,
474 .ioctl = dgram_ioctl,
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400475 .getsockopt = dgram_getsockopt,
476 .setsockopt = dgram_setsockopt,
Sergey Lapin9ec76712009-06-08 12:18:48 +0000477};
478