blob: d95e2e1bdf54b4e9dc1c00fe85cca914631d580d [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;
Phoebe Buckheister32edc402014-05-14 17:43:08 +0200212 struct ieee802154_mac_cb *cb;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000213 struct dgram_sock *ro = dgram_sk(sk);
Herbert Xuae641942011-11-18 02:20:04 +0000214 int hlen, tlen;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000215 int err;
216
217 if (msg->msg_flags & MSG_OOB) {
218 pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
219 return -EOPNOTSUPP;
220 }
221
222 if (!ro->bound)
223 dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
224 else
225 dev = ieee802154_get_dev(sock_net(sk), &ro->src_addr);
226
227 if (!dev) {
228 pr_debug("no dev\n");
229 err = -ENXIO;
230 goto out;
231 }
232 mtu = dev->mtu;
233 pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
234
Sasha Levin3da947b2012-07-02 01:29:55 +0000235 if (size > mtu) {
236 pr_debug("size = %Zu, mtu = %u\n", size, mtu);
Phoebe Buckheisterd1d73582014-03-14 21:24:04 +0100237 err = -EMSGSIZE;
Sasha Levin3da947b2012-07-02 01:29:55 +0000238 goto out_dev;
239 }
240
Herbert Xuae641942011-11-18 02:20:04 +0000241 hlen = LL_RESERVED_SPACE(dev);
242 tlen = dev->needed_tailroom;
243 skb = sock_alloc_send_skb(sk, hlen + tlen + size,
Sergey Lapin9ec76712009-06-08 12:18:48 +0000244 msg->msg_flags & MSG_DONTWAIT,
245 &err);
246 if (!skb)
247 goto out_dev;
248
Herbert Xuae641942011-11-18 02:20:04 +0000249 skb_reserve(skb, hlen);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000250
251 skb_reset_network_header(skb);
252
Phoebe Buckheister32edc402014-05-14 17:43:08 +0200253 cb = mac_cb_init(skb);
254 cb->type = IEEE802154_FC_TYPE_DATA;
255 cb->ackreq = ro->want_ack;
Dmitry Baryshkov99eb8552009-08-07 02:58:43 +0000256
Sergey Lapin9ec76712009-06-08 12:18:48 +0000257 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
Sergey Lapin9ec76712009-06-08 12:18:48 +0000262 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
263 if (err < 0)
264 goto out_skb;
265
Sergey Lapin9ec76712009-06-08 12:18:48 +0000266 skb->dev = dev;
267 skb->sk = sk;
268 skb->protocol = htons(ETH_P_IEEE802154);
269
270 dev_put(dev);
271
272 err = dev_queue_xmit(skb);
273 if (err > 0)
274 err = net_xmit_errno(err);
275
276 return err ?: size;
277
278out_skb:
279 kfree_skb(skb);
280out_dev:
281 dev_put(dev);
282out:
283 return err;
284}
285
286static int dgram_recvmsg(struct kiocb *iocb, struct sock *sk,
287 struct msghdr *msg, size_t len, int noblock, int flags,
288 int *addr_len)
289{
290 size_t copied = 0;
291 int err = -EOPNOTSUPP;
292 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100293 DECLARE_SOCKADDR(struct sockaddr_ieee802154 *, saddr, msg->msg_name);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000294
295 skb = skb_recv_datagram(sk, flags, noblock, &err);
296 if (!skb)
297 goto out;
298
299 copied = skb->len;
300 if (len < copied) {
301 msg->msg_flags |= MSG_TRUNC;
302 copied = len;
303 }
304
305 /* FIXME: skip headers if necessary ?! */
306 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
307 if (err)
308 goto done;
309
Neil Horman3b885782009-10-12 13:26:31 -0700310 sock_recv_ts_and_drops(msg, sk, skb);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000311
Stephen Röttger6364e6e2013-03-26 12:41:29 +0000312 if (saddr) {
313 saddr->family = AF_IEEE802154;
Phoebe Buckheisterae531b92014-03-14 21:24:02 +0100314 ieee802154_addr_to_sa(&saddr->addr, &mac_cb(skb)->source);
Stephen Röttger6364e6e2013-03-26 12:41:29 +0000315 *addr_len = sizeof(*saddr);
Hannes Frederic Sowabceaa902013-11-18 04:20:45 +0100316 }
Stephen Röttger6364e6e2013-03-26 12:41:29 +0000317
Sergey Lapin9ec76712009-06-08 12:18:48 +0000318 if (flags & MSG_TRUNC)
319 copied = skb->len;
320done:
321 skb_free_datagram(sk, skb);
322out:
323 if (err)
324 return err;
325 return copied;
326}
327
328static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb)
329{
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100330 skb = skb_share_check(skb, GFP_ATOMIC);
331 if (!skb)
332 return NET_RX_DROP;
333
Sergey Lapin9ec76712009-06-08 12:18:48 +0000334 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
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100342static inline bool
343ieee802154_match_sock(__le64 hw_addr, __le16 pan_id, __le16 short_addr,
344 struct dgram_sock *ro)
Sergey Lapin9ec76712009-06-08 12:18:48 +0000345{
346 if (!ro->bound)
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100347 return true;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000348
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100349 if (ro->src_addr.mode == IEEE802154_ADDR_LONG &&
350 hw_addr == ro->src_addr.extended_addr)
351 return true;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000352
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100353 if (ro->src_addr.mode == IEEE802154_ADDR_SHORT &&
354 pan_id == ro->src_addr.pan_id &&
355 short_addr == ro->src_addr.short_addr)
356 return true;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000357
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100358 return false;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000359}
360
361int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
362{
363 struct sock *sk, *prev = NULL;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000364 int ret = NET_RX_SUCCESS;
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100365 __le16 pan_id, short_addr;
366 __le64 hw_addr;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000367
368 /* Data frame processing */
369 BUG_ON(dev->type != ARPHRD_IEEE802154);
370
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100371 pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
372 short_addr = ieee802154_mlme_ops(dev)->get_short_addr(dev);
373 hw_addr = ieee802154_devaddr_from_raw(dev->dev_addr);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000374
375 read_lock(&dgram_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800376 sk_for_each(sk, &dgram_head) {
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100377 if (ieee802154_match_sock(hw_addr, pan_id, short_addr,
378 dgram_sk(sk))) {
Sergey Lapin9ec76712009-06-08 12:18:48 +0000379 if (prev) {
380 struct sk_buff *clone;
381 clone = skb_clone(skb, GFP_ATOMIC);
382 if (clone)
383 dgram_rcv_skb(prev, clone);
384 }
385
386 prev = sk;
387 }
388 }
389
390 if (prev)
391 dgram_rcv_skb(prev, skb);
392 else {
393 kfree_skb(skb);
394 ret = NET_RX_DROP;
395 }
396 read_unlock(&dgram_lock);
397
398 return ret;
399}
400
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400401static int dgram_getsockopt(struct sock *sk, int level, int optname,
402 char __user *optval, int __user *optlen)
403{
Dmitry Baryshkov99eb8552009-08-07 02:58:43 +0000404 struct dgram_sock *ro = dgram_sk(sk);
405
406 int val, len;
407
408 if (level != SOL_IEEE802154)
409 return -EOPNOTSUPP;
410
411 if (get_user(len, optlen))
412 return -EFAULT;
413
414 len = min_t(unsigned int, len, sizeof(int));
415
416 switch (optname) {
417 case WPAN_WANTACK:
418 val = ro->want_ack;
419 break;
420 default:
421 return -ENOPROTOOPT;
422 }
423
424 if (put_user(len, optlen))
425 return -EFAULT;
426 if (copy_to_user(optval, &val, len))
427 return -EFAULT;
428 return 0;
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400429}
430
431static int dgram_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700432 char __user *optval, unsigned int optlen)
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400433{
Dmitry Baryshkov99eb8552009-08-07 02:58:43 +0000434 struct dgram_sock *ro = dgram_sk(sk);
435 int val;
436 int err = 0;
437
438 if (optlen < sizeof(int))
439 return -EINVAL;
440
441 if (get_user(val, (int __user *)optval))
442 return -EFAULT;
443
444 lock_sock(sk);
445
446 switch (optname) {
447 case WPAN_WANTACK:
448 ro->want_ack = !!val;
449 break;
450 default:
451 err = -ENOPROTOOPT;
452 break;
453 }
454
455 release_sock(sk);
456 return err;
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400457}
458
Sergey Lapin9ec76712009-06-08 12:18:48 +0000459struct proto ieee802154_dgram_prot = {
460 .name = "IEEE-802.15.4-MAC",
461 .owner = THIS_MODULE,
462 .obj_size = sizeof(struct dgram_sock),
463 .init = dgram_init,
464 .close = dgram_close,
465 .bind = dgram_bind,
466 .sendmsg = dgram_sendmsg,
467 .recvmsg = dgram_recvmsg,
468 .hash = dgram_hash,
469 .unhash = dgram_unhash,
470 .connect = dgram_connect,
471 .disconnect = dgram_disconnect,
472 .ioctl = dgram_ioctl,
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400473 .getsockopt = dgram_getsockopt,
474 .setsockopt = dgram_setsockopt,
Sergey Lapin9ec76712009-06-08 12:18:48 +0000475};
476