blob: 581a59504bd5f49d9a54584f3441c4e323b7bcd5 [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
Sergey Lapin9ec76712009-06-08 12:18:48 +000044 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
76 ro->dst_addr.addr_type = IEEE802154_ADDR_LONG;
77 ro->dst_addr.pan_id = 0xffff;
Dmitry Baryshkov99eb8552009-08-07 02:58:43 +000078 ro->want_ack = 1;
Sergey Lapin9ec76712009-06-08 12:18:48 +000079 memset(&ro->dst_addr.hwaddr, 0xff, sizeof(ro->dst_addr.hwaddr));
80 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;
91 struct dgram_sock *ro = dgram_sk(sk);
Dmitry Baryshkov74eda552009-08-07 02:58:42 +000092 int err = -EINVAL;
Sergey Lapin9ec76712009-06-08 12:18:48 +000093 struct net_device *dev;
94
Dmitry Baryshkov74eda552009-08-07 02:58:42 +000095 lock_sock(sk);
96
Sergey Lapin9ec76712009-06-08 12:18:48 +000097 ro->bound = 0;
98
99 if (len < sizeof(*addr))
Dmitry Baryshkov74eda552009-08-07 02:58:42 +0000100 goto out;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000101
102 if (addr->family != AF_IEEE802154)
Dmitry Baryshkov74eda552009-08-07 02:58:42 +0000103 goto out;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000104
105 dev = ieee802154_get_dev(sock_net(sk), &addr->addr);
106 if (!dev) {
107 err = -ENODEV;
108 goto out;
109 }
110
111 if (dev->type != ARPHRD_IEEE802154) {
112 err = -ENODEV;
113 goto out_put;
114 }
115
116 memcpy(&ro->src_addr, &addr->addr, sizeof(struct ieee802154_addr));
117
118 ro->bound = 1;
Dmitry Baryshkov74eda552009-08-07 02:58:42 +0000119 err = 0;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000120out_put:
121 dev_put(dev);
122out:
123 release_sock(sk);
124
125 return err;
126}
127
128static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg)
129{
130 switch (cmd) {
131 case SIOCOUTQ:
132 {
Eric Dumazet31e6d362009-06-17 19:05:41 -0700133 int amount = sk_wmem_alloc_get(sk);
134
Sergey Lapin9ec76712009-06-08 12:18:48 +0000135 return put_user(amount, (int __user *)arg);
136 }
137
138 case SIOCINQ:
139 {
140 struct sk_buff *skb;
141 unsigned long amount;
142
143 amount = 0;
144 spin_lock_bh(&sk->sk_receive_queue.lock);
145 skb = skb_peek(&sk->sk_receive_queue);
146 if (skb != NULL) {
147 /*
148 * We will only return the amount
149 * of this packet since that is all
150 * that will be read.
151 */
152 /* FIXME: parse the header for more correct value */
153 amount = skb->len - (3+8+8);
154 }
155 spin_unlock_bh(&sk->sk_receive_queue.lock);
156 return put_user(amount, (int __user *)arg);
157 }
158
159 }
160 return -ENOIOCTLCMD;
161}
162
163/* FIXME: autobind */
164static int dgram_connect(struct sock *sk, struct sockaddr *uaddr,
165 int len)
166{
167 struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
168 struct dgram_sock *ro = dgram_sk(sk);
169 int err = 0;
170
171 if (len < sizeof(*addr))
172 return -EINVAL;
173
174 if (addr->family != AF_IEEE802154)
175 return -EINVAL;
176
177 lock_sock(sk);
178
179 if (!ro->bound) {
180 err = -ENETUNREACH;
181 goto out;
182 }
183
184 memcpy(&ro->dst_addr, &addr->addr, sizeof(struct ieee802154_addr));
185
186out:
187 release_sock(sk);
188 return err;
189}
190
191static int dgram_disconnect(struct sock *sk, int flags)
192{
193 struct dgram_sock *ro = dgram_sk(sk);
194
195 lock_sock(sk);
196
197 ro->dst_addr.addr_type = IEEE802154_ADDR_LONG;
198 memset(&ro->dst_addr.hwaddr, 0xff, sizeof(ro->dst_addr.hwaddr));
199
200 release_sock(sk);
201
202 return 0;
203}
204
205static int dgram_sendmsg(struct kiocb *iocb, struct sock *sk,
206 struct msghdr *msg, size_t size)
207{
208 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +0000209 unsigned int mtu;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000210 struct sk_buff *skb;
211 struct dgram_sock *ro = dgram_sk(sk);
Herbert Xuae641942011-11-18 02:20:04 +0000212 int hlen, tlen;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000213 int err;
214
215 if (msg->msg_flags & MSG_OOB) {
216 pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
217 return -EOPNOTSUPP;
218 }
219
220 if (!ro->bound)
221 dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
222 else
223 dev = ieee802154_get_dev(sock_net(sk), &ro->src_addr);
224
225 if (!dev) {
226 pr_debug("no dev\n");
227 err = -ENXIO;
228 goto out;
229 }
230 mtu = dev->mtu;
231 pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
232
Sasha Levin3da947b2012-07-02 01:29:55 +0000233 if (size > mtu) {
234 pr_debug("size = %Zu, mtu = %u\n", size, mtu);
235 err = -EINVAL;
236 goto out_dev;
237 }
238
Herbert Xuae641942011-11-18 02:20:04 +0000239 hlen = LL_RESERVED_SPACE(dev);
240 tlen = dev->needed_tailroom;
241 skb = sock_alloc_send_skb(sk, hlen + tlen + size,
Sergey Lapin9ec76712009-06-08 12:18:48 +0000242 msg->msg_flags & MSG_DONTWAIT,
243 &err);
244 if (!skb)
245 goto out_dev;
246
Herbert Xuae641942011-11-18 02:20:04 +0000247 skb_reserve(skb, hlen);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000248
249 skb_reset_network_header(skb);
250
Dmitry Baryshkov99eb8552009-08-07 02:58:43 +0000251 mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA;
252 if (ro->want_ack)
253 mac_cb(skb)->flags |= MAC_CB_FLAG_ACKREQ;
254
Sergey Lapin9ec76712009-06-08 12:18:48 +0000255 mac_cb(skb)->seq = ieee802154_mlme_ops(dev)->get_dsn(dev);
256 err = dev_hard_header(skb, dev, ETH_P_IEEE802154, &ro->dst_addr,
257 ro->bound ? &ro->src_addr : NULL, size);
258 if (err < 0)
259 goto out_skb;
260
261 skb_reset_mac_header(skb);
262
263 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
264 if (err < 0)
265 goto out_skb;
266
Sergey Lapin9ec76712009-06-08 12:18:48 +0000267 skb->dev = dev;
268 skb->sk = sk;
269 skb->protocol = htons(ETH_P_IEEE802154);
270
271 dev_put(dev);
272
273 err = dev_queue_xmit(skb);
274 if (err > 0)
275 err = net_xmit_errno(err);
276
277 return err ?: size;
278
279out_skb:
280 kfree_skb(skb);
281out_dev:
282 dev_put(dev);
283out:
284 return err;
285}
286
287static int dgram_recvmsg(struct kiocb *iocb, struct sock *sk,
288 struct msghdr *msg, size_t len, int noblock, int flags,
289 int *addr_len)
290{
291 size_t copied = 0;
292 int err = -EOPNOTSUPP;
293 struct sk_buff *skb;
Stephen Röttger6364e6e2013-03-26 12:41:29 +0000294 struct sockaddr_ieee802154 *saddr;
295
296 saddr = (struct sockaddr_ieee802154 *)msg->msg_name;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000297
298 skb = skb_recv_datagram(sk, flags, noblock, &err);
299 if (!skb)
300 goto out;
301
302 copied = skb->len;
303 if (len < copied) {
304 msg->msg_flags |= MSG_TRUNC;
305 copied = len;
306 }
307
308 /* FIXME: skip headers if necessary ?! */
309 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
310 if (err)
311 goto done;
312
Neil Horman3b885782009-10-12 13:26:31 -0700313 sock_recv_ts_and_drops(msg, sk, skb);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000314
Stephen Röttger6364e6e2013-03-26 12:41:29 +0000315 if (saddr) {
316 saddr->family = AF_IEEE802154;
317 saddr->addr = mac_cb(skb)->sa;
318 }
319 if (addr_len)
320 *addr_len = sizeof(*saddr);
321
Sergey Lapin9ec76712009-06-08 12:18:48 +0000322 if (flags & MSG_TRUNC)
323 copied = skb->len;
324done:
325 skb_free_datagram(sk, skb);
326out:
327 if (err)
328 return err;
329 return copied;
330}
331
332static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb)
333{
334 if (sock_queue_rcv_skb(sk, skb) < 0) {
Sergey Lapin9ec76712009-06-08 12:18:48 +0000335 kfree_skb(skb);
336 return NET_RX_DROP;
337 }
338
339 return NET_RX_SUCCESS;
340}
341
342static inline int ieee802154_match_sock(u8 *hw_addr, u16 pan_id,
343 u16 short_addr, struct dgram_sock *ro)
344{
345 if (!ro->bound)
346 return 1;
347
348 if (ro->src_addr.addr_type == IEEE802154_ADDR_LONG &&
349 !memcmp(ro->src_addr.hwaddr, hw_addr, IEEE802154_ADDR_LEN))
350 return 1;
351
352 if (ro->src_addr.addr_type == IEEE802154_ADDR_SHORT &&
353 pan_id == ro->src_addr.pan_id &&
354 short_addr == ro->src_addr.short_addr)
355 return 1;
356
357 return 0;
358}
359
360int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
361{
362 struct sock *sk, *prev = NULL;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000363 int ret = NET_RX_SUCCESS;
364 u16 pan_id, short_addr;
365
366 /* Data frame processing */
367 BUG_ON(dev->type != ARPHRD_IEEE802154);
368
369 pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
370 short_addr = ieee802154_mlme_ops(dev)->get_short_addr(dev);
371
372 read_lock(&dgram_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800373 sk_for_each(sk, &dgram_head) {
Sergey Lapin9ec76712009-06-08 12:18:48 +0000374 if (ieee802154_match_sock(dev->dev_addr, pan_id, short_addr,
375 dgram_sk(sk))) {
376 if (prev) {
377 struct sk_buff *clone;
378 clone = skb_clone(skb, GFP_ATOMIC);
379 if (clone)
380 dgram_rcv_skb(prev, clone);
381 }
382
383 prev = sk;
384 }
385 }
386
387 if (prev)
388 dgram_rcv_skb(prev, skb);
389 else {
390 kfree_skb(skb);
391 ret = NET_RX_DROP;
392 }
393 read_unlock(&dgram_lock);
394
395 return ret;
396}
397
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400398static int dgram_getsockopt(struct sock *sk, int level, int optname,
399 char __user *optval, int __user *optlen)
400{
Dmitry Baryshkov99eb8552009-08-07 02:58:43 +0000401 struct dgram_sock *ro = dgram_sk(sk);
402
403 int val, len;
404
405 if (level != SOL_IEEE802154)
406 return -EOPNOTSUPP;
407
408 if (get_user(len, optlen))
409 return -EFAULT;
410
411 len = min_t(unsigned int, len, sizeof(int));
412
413 switch (optname) {
414 case WPAN_WANTACK:
415 val = ro->want_ack;
416 break;
417 default:
418 return -ENOPROTOOPT;
419 }
420
421 if (put_user(len, optlen))
422 return -EFAULT;
423 if (copy_to_user(optval, &val, len))
424 return -EFAULT;
425 return 0;
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400426}
427
428static int dgram_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700429 char __user *optval, unsigned int optlen)
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400430{
Dmitry Baryshkov99eb8552009-08-07 02:58:43 +0000431 struct dgram_sock *ro = dgram_sk(sk);
432 int val;
433 int err = 0;
434
435 if (optlen < sizeof(int))
436 return -EINVAL;
437
438 if (get_user(val, (int __user *)optval))
439 return -EFAULT;
440
441 lock_sock(sk);
442
443 switch (optname) {
444 case WPAN_WANTACK:
445 ro->want_ack = !!val;
446 break;
447 default:
448 err = -ENOPROTOOPT;
449 break;
450 }
451
452 release_sock(sk);
453 return err;
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400454}
455
Sergey Lapin9ec76712009-06-08 12:18:48 +0000456struct proto ieee802154_dgram_prot = {
457 .name = "IEEE-802.15.4-MAC",
458 .owner = THIS_MODULE,
459 .obj_size = sizeof(struct dgram_sock),
460 .init = dgram_init,
461 .close = dgram_close,
462 .bind = dgram_bind,
463 .sendmsg = dgram_sendmsg,
464 .recvmsg = dgram_recvmsg,
465 .hash = dgram_hash,
466 .unhash = dgram_unhash,
467 .connect = dgram_connect,
468 .disconnect = dgram_disconnect,
469 .ioctl = dgram_ioctl,
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400470 .getsockopt = dgram_getsockopt,
471 .setsockopt = dgram_setsockopt,
Sergey Lapin9ec76712009-06-08 12:18:48 +0000472};
473