aboutsummaryrefslogtreecommitdiffstats
path: root/net/ieee802154
diff options
context:
space:
mode:
authorAlexander Aring <alex.aring@gmail.com>2014-12-31 13:39:10 -0500
committerMarcel Holtmann <marcel@holtmann.org>2015-01-02 19:49:24 -0500
commit78f821b648267d3ee1a63093cd0c41cc99b97727 (patch)
treeea8b300e8f080c392210f2050c35ef1ba93e3076 /net/ieee802154
parent79300e3a438f93c7431b4bf40fa18580ccb34be1 (diff)
ieee802154: socket: put handling into one file
This patch puts all related socket handling into one file. This is just a cleanup to do all socket handling stuff inside of one implementation file. Signed-off-by: Alexander Aring <alex.aring@gmail.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
Diffstat (limited to 'net/ieee802154')
-rw-r--r--net/ieee802154/Makefile2
-rw-r--r--net/ieee802154/af802154.h33
-rw-r--r--net/ieee802154/af_ieee802154.c762
-rw-r--r--net/ieee802154/dgram.c549
-rw-r--r--net/ieee802154/raw.c270
5 files changed, 760 insertions, 856 deletions
diff --git a/net/ieee802154/Makefile b/net/ieee802154/Makefile
index 19b2bec1fd09..e6ddfcd73510 100644
--- a/net/ieee802154/Makefile
+++ b/net/ieee802154/Makefile
@@ -5,6 +5,6 @@ obj-$(CONFIG_IEEE802154_6LOWPAN) += ieee802154_6lowpan.o
5ieee802154_6lowpan-y := 6lowpan_rtnl.o reassembly.o 5ieee802154_6lowpan-y := 6lowpan_rtnl.o reassembly.o
6ieee802154-y := netlink.o nl-mac.o nl-phy.o nl_policy.o core.o \ 6ieee802154-y := netlink.o nl-mac.o nl-phy.o nl_policy.o core.o \
7 header_ops.o sysfs.o nl802154.o 7 header_ops.o sysfs.o nl802154.o
8ieee802154_socket-y := af_ieee802154.o raw.o dgram.o 8ieee802154_socket-y := af_ieee802154.o
9 9
10ccflags-y += -D__CHECK_ENDIAN__ 10ccflags-y += -D__CHECK_ENDIAN__
diff --git a/net/ieee802154/af802154.h b/net/ieee802154/af802154.h
deleted file mode 100644
index 343b63e6f953..000000000000
--- a/net/ieee802154/af802154.h
+++ /dev/null
@@ -1,33 +0,0 @@
1/*
2 * Internal interfaces for ieee 802.15.4 address family.
3 *
4 * Copyright 2007, 2008, 2009 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 * Written by:
16 * Sergey Lapin <slapin@ossfans.org>
17 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
18 */
19
20#ifndef AF802154_H
21#define AF802154_H
22
23struct sk_buff;
24struct net_device;
25struct ieee802154_addr;
26extern struct proto ieee802154_raw_prot;
27extern struct proto ieee802154_dgram_prot;
28void ieee802154_raw_deliver(struct net_device *dev, struct sk_buff *skb);
29int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb);
30struct net_device *ieee802154_get_dev(struct net *net,
31 const struct ieee802154_addr *addr);
32
33#endif
diff --git a/net/ieee802154/af_ieee802154.c b/net/ieee802154/af_ieee802154.c
index d0a1282cdf43..64beb0b3c560 100644
--- a/net/ieee802154/af_ieee802154.c
+++ b/net/ieee802154/af_ieee802154.c
@@ -34,10 +34,8 @@
34#include <net/af_ieee802154.h> 34#include <net/af_ieee802154.h>
35#include <net/ieee802154_netdev.h> 35#include <net/ieee802154_netdev.h>
36 36
37#include "af802154.h"
38
39/* Utility function for families */ 37/* Utility function for families */
40struct net_device* 38static struct net_device*
41ieee802154_get_dev(struct net *net, const struct ieee802154_addr *addr) 39ieee802154_get_dev(struct net *net, const struct ieee802154_addr *addr)
42{ 40{
43 struct net_device *dev = NULL; 41 struct net_device *dev = NULL;
@@ -182,6 +180,248 @@ static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd,
182 } 180 }
183} 181}
184 182
183/* RAW Sockets (802.15.4 created in userspace) */
184static HLIST_HEAD(raw_head);
185static DEFINE_RWLOCK(raw_lock);
186
187static void raw_hash(struct sock *sk)
188{
189 write_lock_bh(&raw_lock);
190 sk_add_node(sk, &raw_head);
191 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
192 write_unlock_bh(&raw_lock);
193}
194
195static void raw_unhash(struct sock *sk)
196{
197 write_lock_bh(&raw_lock);
198 if (sk_del_node_init(sk))
199 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
200 write_unlock_bh(&raw_lock);
201}
202
203static void raw_close(struct sock *sk, long timeout)
204{
205 sk_common_release(sk);
206}
207
208static int raw_bind(struct sock *sk, struct sockaddr *_uaddr, int len)
209{
210 struct ieee802154_addr addr;
211 struct sockaddr_ieee802154 *uaddr = (struct sockaddr_ieee802154 *)_uaddr;
212 int err = 0;
213 struct net_device *dev = NULL;
214
215 if (len < sizeof(*uaddr))
216 return -EINVAL;
217
218 uaddr = (struct sockaddr_ieee802154 *)_uaddr;
219 if (uaddr->family != AF_IEEE802154)
220 return -EINVAL;
221
222 lock_sock(sk);
223
224 ieee802154_addr_from_sa(&addr, &uaddr->addr);
225 dev = ieee802154_get_dev(sock_net(sk), &addr);
226 if (!dev) {
227 err = -ENODEV;
228 goto out;
229 }
230
231 if (dev->type != ARPHRD_IEEE802154) {
232 err = -ENODEV;
233 goto out_put;
234 }
235
236 sk->sk_bound_dev_if = dev->ifindex;
237 sk_dst_reset(sk);
238
239out_put:
240 dev_put(dev);
241out:
242 release_sock(sk);
243
244 return err;
245}
246
247static int raw_connect(struct sock *sk, struct sockaddr *uaddr,
248 int addr_len)
249{
250 return -ENOTSUPP;
251}
252
253static int raw_disconnect(struct sock *sk, int flags)
254{
255 return 0;
256}
257
258static int raw_sendmsg(struct kiocb *iocb, struct sock *sk,
259 struct msghdr *msg, size_t size)
260{
261 struct net_device *dev;
262 unsigned int mtu;
263 struct sk_buff *skb;
264 int hlen, tlen;
265 int err;
266
267 if (msg->msg_flags & MSG_OOB) {
268 pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
269 return -EOPNOTSUPP;
270 }
271
272 lock_sock(sk);
273 if (!sk->sk_bound_dev_if)
274 dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
275 else
276 dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if);
277 release_sock(sk);
278
279 if (!dev) {
280 pr_debug("no dev\n");
281 err = -ENXIO;
282 goto out;
283 }
284
285 mtu = dev->mtu;
286 pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
287
288 if (size > mtu) {
289 pr_debug("size = %Zu, mtu = %u\n", size, mtu);
290 err = -EINVAL;
291 goto out_dev;
292 }
293
294 hlen = LL_RESERVED_SPACE(dev);
295 tlen = dev->needed_tailroom;
296 skb = sock_alloc_send_skb(sk, hlen + tlen + size,
297 msg->msg_flags & MSG_DONTWAIT, &err);
298 if (!skb)
299 goto out_dev;
300
301 skb_reserve(skb, hlen);
302
303 skb_reset_mac_header(skb);
304 skb_reset_network_header(skb);
305
306 err = memcpy_from_msg(skb_put(skb, size), msg, size);
307 if (err < 0)
308 goto out_skb;
309
310 skb->dev = dev;
311 skb->sk = sk;
312 skb->protocol = htons(ETH_P_IEEE802154);
313
314 dev_put(dev);
315
316 err = dev_queue_xmit(skb);
317 if (err > 0)
318 err = net_xmit_errno(err);
319
320 return err ?: size;
321
322out_skb:
323 kfree_skb(skb);
324out_dev:
325 dev_put(dev);
326out:
327 return err;
328}
329
330static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
331 size_t len, int noblock, int flags, int *addr_len)
332{
333 size_t copied = 0;
334 int err = -EOPNOTSUPP;
335 struct sk_buff *skb;
336
337 skb = skb_recv_datagram(sk, flags, noblock, &err);
338 if (!skb)
339 goto out;
340
341 copied = skb->len;
342 if (len < copied) {
343 msg->msg_flags |= MSG_TRUNC;
344 copied = len;
345 }
346
347 err = skb_copy_datagram_msg(skb, 0, msg, copied);
348 if (err)
349 goto done;
350
351 sock_recv_ts_and_drops(msg, sk, skb);
352
353 if (flags & MSG_TRUNC)
354 copied = skb->len;
355done:
356 skb_free_datagram(sk, skb);
357out:
358 if (err)
359 return err;
360 return copied;
361}
362
363static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb)
364{
365 skb = skb_share_check(skb, GFP_ATOMIC);
366 if (!skb)
367 return NET_RX_DROP;
368
369 if (sock_queue_rcv_skb(sk, skb) < 0) {
370 kfree_skb(skb);
371 return NET_RX_DROP;
372 }
373
374 return NET_RX_SUCCESS;
375}
376
377static void ieee802154_raw_deliver(struct net_device *dev, struct sk_buff *skb)
378{
379 struct sock *sk;
380
381 read_lock(&raw_lock);
382 sk_for_each(sk, &raw_head) {
383 bh_lock_sock(sk);
384 if (!sk->sk_bound_dev_if ||
385 sk->sk_bound_dev_if == dev->ifindex) {
386 struct sk_buff *clone;
387
388 clone = skb_clone(skb, GFP_ATOMIC);
389 if (clone)
390 raw_rcv_skb(sk, clone);
391 }
392 bh_unlock_sock(sk);
393 }
394 read_unlock(&raw_lock);
395}
396
397static int raw_getsockopt(struct sock *sk, int level, int optname,
398 char __user *optval, int __user *optlen)
399{
400 return -EOPNOTSUPP;
401}
402
403static int raw_setsockopt(struct sock *sk, int level, int optname,
404 char __user *optval, unsigned int optlen)
405{
406 return -EOPNOTSUPP;
407}
408
409static struct proto ieee802154_raw_prot = {
410 .name = "IEEE-802.15.4-RAW",
411 .owner = THIS_MODULE,
412 .obj_size = sizeof(struct sock),
413 .close = raw_close,
414 .bind = raw_bind,
415 .sendmsg = raw_sendmsg,
416 .recvmsg = raw_recvmsg,
417 .hash = raw_hash,
418 .unhash = raw_unhash,
419 .connect = raw_connect,
420 .disconnect = raw_disconnect,
421 .getsockopt = raw_getsockopt,
422 .setsockopt = raw_setsockopt,
423};
424
185static const struct proto_ops ieee802154_raw_ops = { 425static const struct proto_ops ieee802154_raw_ops = {
186 .family = PF_IEEE802154, 426 .family = PF_IEEE802154,
187 .owner = THIS_MODULE, 427 .owner = THIS_MODULE,
@@ -207,6 +447,522 @@ static const struct proto_ops ieee802154_raw_ops = {
207#endif 447#endif
208}; 448};
209 449
450/* DGRAM Sockets (802.15.4 dataframes) */
451static HLIST_HEAD(dgram_head);
452static DEFINE_RWLOCK(dgram_lock);
453
454struct dgram_sock {
455 struct sock sk;
456
457 struct ieee802154_addr src_addr;
458 struct ieee802154_addr dst_addr;
459
460 unsigned int bound:1;
461 unsigned int connected:1;
462 unsigned int want_ack:1;
463 unsigned int secen:1;
464 unsigned int secen_override:1;
465 unsigned int seclevel:3;
466 unsigned int seclevel_override:1;
467};
468
469static inline struct dgram_sock *dgram_sk(const struct sock *sk)
470{
471 return container_of(sk, struct dgram_sock, sk);
472}
473
474static void dgram_hash(struct sock *sk)
475{
476 write_lock_bh(&dgram_lock);
477 sk_add_node(sk, &dgram_head);
478 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
479 write_unlock_bh(&dgram_lock);
480}
481
482static void dgram_unhash(struct sock *sk)
483{
484 write_lock_bh(&dgram_lock);
485 if (sk_del_node_init(sk))
486 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
487 write_unlock_bh(&dgram_lock);
488}
489
490static int dgram_init(struct sock *sk)
491{
492 struct dgram_sock *ro = dgram_sk(sk);
493
494 ro->want_ack = 1;
495 return 0;
496}
497
498static void dgram_close(struct sock *sk, long timeout)
499{
500 sk_common_release(sk);
501}
502
503static int dgram_bind(struct sock *sk, struct sockaddr *uaddr, int len)
504{
505 struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
506 struct ieee802154_addr haddr;
507 struct dgram_sock *ro = dgram_sk(sk);
508 int err = -EINVAL;
509 struct net_device *dev;
510
511 lock_sock(sk);
512
513 ro->bound = 0;
514
515 if (len < sizeof(*addr))
516 goto out;
517
518 if (addr->family != AF_IEEE802154)
519 goto out;
520
521 ieee802154_addr_from_sa(&haddr, &addr->addr);
522 dev = ieee802154_get_dev(sock_net(sk), &haddr);
523 if (!dev) {
524 err = -ENODEV;
525 goto out;
526 }
527
528 if (dev->type != ARPHRD_IEEE802154) {
529 err = -ENODEV;
530 goto out_put;
531 }
532
533 ro->src_addr = haddr;
534
535 ro->bound = 1;
536 err = 0;
537out_put:
538 dev_put(dev);
539out:
540 release_sock(sk);
541
542 return err;
543}
544
545static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg)
546{
547 switch (cmd) {
548 case SIOCOUTQ:
549 {
550 int amount = sk_wmem_alloc_get(sk);
551
552 return put_user(amount, (int __user *)arg);
553 }
554
555 case SIOCINQ:
556 {
557 struct sk_buff *skb;
558 unsigned long amount;
559
560 amount = 0;
561 spin_lock_bh(&sk->sk_receive_queue.lock);
562 skb = skb_peek(&sk->sk_receive_queue);
563 if (skb != NULL) {
564 /* We will only return the amount
565 * of this packet since that is all
566 * that will be read.
567 */
568 amount = skb->len - ieee802154_hdr_length(skb);
569 }
570 spin_unlock_bh(&sk->sk_receive_queue.lock);
571 return put_user(amount, (int __user *)arg);
572 }
573 }
574
575 return -ENOIOCTLCMD;
576}
577
578/* FIXME: autobind */
579static int dgram_connect(struct sock *sk, struct sockaddr *uaddr,
580 int len)
581{
582 struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
583 struct dgram_sock *ro = dgram_sk(sk);
584 int err = 0;
585
586 if (len < sizeof(*addr))
587 return -EINVAL;
588
589 if (addr->family != AF_IEEE802154)
590 return -EINVAL;
591
592 lock_sock(sk);
593
594 if (!ro->bound) {
595 err = -ENETUNREACH;
596 goto out;
597 }
598
599 ieee802154_addr_from_sa(&ro->dst_addr, &addr->addr);
600 ro->connected = 1;
601
602out:
603 release_sock(sk);
604 return err;
605}
606
607static int dgram_disconnect(struct sock *sk, int flags)
608{
609 struct dgram_sock *ro = dgram_sk(sk);
610
611 lock_sock(sk);
612 ro->connected = 0;
613 release_sock(sk);
614
615 return 0;
616}
617
618static int dgram_sendmsg(struct kiocb *iocb, struct sock *sk,
619 struct msghdr *msg, size_t size)
620{
621 struct net_device *dev;
622 unsigned int mtu;
623 struct sk_buff *skb;
624 struct ieee802154_mac_cb *cb;
625 struct dgram_sock *ro = dgram_sk(sk);
626 struct ieee802154_addr dst_addr;
627 int hlen, tlen;
628 int err;
629
630 if (msg->msg_flags & MSG_OOB) {
631 pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
632 return -EOPNOTSUPP;
633 }
634
635 if (!ro->connected && !msg->msg_name)
636 return -EDESTADDRREQ;
637 else if (ro->connected && msg->msg_name)
638 return -EISCONN;
639
640 if (!ro->bound)
641 dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
642 else
643 dev = ieee802154_get_dev(sock_net(sk), &ro->src_addr);
644
645 if (!dev) {
646 pr_debug("no dev\n");
647 err = -ENXIO;
648 goto out;
649 }
650 mtu = dev->mtu;
651 pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
652
653 if (size > mtu) {
654 pr_debug("size = %Zu, mtu = %u\n", size, mtu);
655 err = -EMSGSIZE;
656 goto out_dev;
657 }
658
659 hlen = LL_RESERVED_SPACE(dev);
660 tlen = dev->needed_tailroom;
661 skb = sock_alloc_send_skb(sk, hlen + tlen + size,
662 msg->msg_flags & MSG_DONTWAIT,
663 &err);
664 if (!skb)
665 goto out_dev;
666
667 skb_reserve(skb, hlen);
668
669 skb_reset_network_header(skb);
670
671 cb = mac_cb_init(skb);
672 cb->type = IEEE802154_FC_TYPE_DATA;
673 cb->ackreq = ro->want_ack;
674
675 if (msg->msg_name) {
676 DECLARE_SOCKADDR(struct sockaddr_ieee802154*,
677 daddr, msg->msg_name);
678
679 ieee802154_addr_from_sa(&dst_addr, &daddr->addr);
680 } else {
681 dst_addr = ro->dst_addr;
682 }
683
684 cb->secen = ro->secen;
685 cb->secen_override = ro->secen_override;
686 cb->seclevel = ro->seclevel;
687 cb->seclevel_override = ro->seclevel_override;
688
689 err = dev_hard_header(skb, dev, ETH_P_IEEE802154, &dst_addr,
690 ro->bound ? &ro->src_addr : NULL, size);
691 if (err < 0)
692 goto out_skb;
693
694 err = memcpy_from_msg(skb_put(skb, size), msg, size);
695 if (err < 0)
696 goto out_skb;
697
698 skb->dev = dev;
699 skb->sk = sk;
700 skb->protocol = htons(ETH_P_IEEE802154);
701
702 dev_put(dev);
703
704 err = dev_queue_xmit(skb);
705 if (err > 0)
706 err = net_xmit_errno(err);
707
708 return err ?: size;
709
710out_skb:
711 kfree_skb(skb);
712out_dev:
713 dev_put(dev);
714out:
715 return err;
716}
717
718static int dgram_recvmsg(struct kiocb *iocb, struct sock *sk,
719 struct msghdr *msg, size_t len, int noblock,
720 int flags, int *addr_len)
721{
722 size_t copied = 0;
723 int err = -EOPNOTSUPP;
724 struct sk_buff *skb;
725 DECLARE_SOCKADDR(struct sockaddr_ieee802154 *, saddr, msg->msg_name);
726
727 skb = skb_recv_datagram(sk, flags, noblock, &err);
728 if (!skb)
729 goto out;
730
731 copied = skb->len;
732 if (len < copied) {
733 msg->msg_flags |= MSG_TRUNC;
734 copied = len;
735 }
736
737 /* FIXME: skip headers if necessary ?! */
738 err = skb_copy_datagram_msg(skb, 0, msg, copied);
739 if (err)
740 goto done;
741
742 sock_recv_ts_and_drops(msg, sk, skb);
743
744 if (saddr) {
745 saddr->family = AF_IEEE802154;
746 ieee802154_addr_to_sa(&saddr->addr, &mac_cb(skb)->source);
747 *addr_len = sizeof(*saddr);
748 }
749
750 if (flags & MSG_TRUNC)
751 copied = skb->len;
752done:
753 skb_free_datagram(sk, skb);
754out:
755 if (err)
756 return err;
757 return copied;
758}
759
760static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb)
761{
762 skb = skb_share_check(skb, GFP_ATOMIC);
763 if (!skb)
764 return NET_RX_DROP;
765
766 if (sock_queue_rcv_skb(sk, skb) < 0) {
767 kfree_skb(skb);
768 return NET_RX_DROP;
769 }
770
771 return NET_RX_SUCCESS;
772}
773
774static inline bool
775ieee802154_match_sock(__le64 hw_addr, __le16 pan_id, __le16 short_addr,
776 struct dgram_sock *ro)
777{
778 if (!ro->bound)
779 return true;
780
781 if (ro->src_addr.mode == IEEE802154_ADDR_LONG &&
782 hw_addr == ro->src_addr.extended_addr)
783 return true;
784
785 if (ro->src_addr.mode == IEEE802154_ADDR_SHORT &&
786 pan_id == ro->src_addr.pan_id &&
787 short_addr == ro->src_addr.short_addr)
788 return true;
789
790 return false;
791}
792
793static int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
794{
795 struct sock *sk, *prev = NULL;
796 int ret = NET_RX_SUCCESS;
797 __le16 pan_id, short_addr;
798 __le64 hw_addr;
799
800 /* Data frame processing */
801 BUG_ON(dev->type != ARPHRD_IEEE802154);
802
803 pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
804 short_addr = ieee802154_mlme_ops(dev)->get_short_addr(dev);
805 hw_addr = ieee802154_devaddr_from_raw(dev->dev_addr);
806
807 read_lock(&dgram_lock);
808 sk_for_each(sk, &dgram_head) {
809 if (ieee802154_match_sock(hw_addr, pan_id, short_addr,
810 dgram_sk(sk))) {
811 if (prev) {
812 struct sk_buff *clone;
813
814 clone = skb_clone(skb, GFP_ATOMIC);
815 if (clone)
816 dgram_rcv_skb(prev, clone);
817 }
818
819 prev = sk;
820 }
821 }
822
823 if (prev) {
824 dgram_rcv_skb(prev, skb);
825 } else {
826 kfree_skb(skb);
827 ret = NET_RX_DROP;
828 }
829 read_unlock(&dgram_lock);
830
831 return ret;
832}
833
834static int dgram_getsockopt(struct sock *sk, int level, int optname,
835 char __user *optval, int __user *optlen)
836{
837 struct dgram_sock *ro = dgram_sk(sk);
838
839 int val, len;
840
841 if (level != SOL_IEEE802154)
842 return -EOPNOTSUPP;
843
844 if (get_user(len, optlen))
845 return -EFAULT;
846
847 len = min_t(unsigned int, len, sizeof(int));
848
849 switch (optname) {
850 case WPAN_WANTACK:
851 val = ro->want_ack;
852 break;
853 case WPAN_SECURITY:
854 if (!ro->secen_override)
855 val = WPAN_SECURITY_DEFAULT;
856 else if (ro->secen)
857 val = WPAN_SECURITY_ON;
858 else
859 val = WPAN_SECURITY_OFF;
860 break;
861 case WPAN_SECURITY_LEVEL:
862 if (!ro->seclevel_override)
863 val = WPAN_SECURITY_LEVEL_DEFAULT;
864 else
865 val = ro->seclevel;
866 break;
867 default:
868 return -ENOPROTOOPT;
869 }
870
871 if (put_user(len, optlen))
872 return -EFAULT;
873 if (copy_to_user(optval, &val, len))
874 return -EFAULT;
875 return 0;
876}
877
878static int dgram_setsockopt(struct sock *sk, int level, int optname,
879 char __user *optval, unsigned int optlen)
880{
881 struct dgram_sock *ro = dgram_sk(sk);
882 struct net *net = sock_net(sk);
883 int val;
884 int err = 0;
885
886 if (optlen < sizeof(int))
887 return -EINVAL;
888
889 if (get_user(val, (int __user *)optval))
890 return -EFAULT;
891
892 lock_sock(sk);
893
894 switch (optname) {
895 case WPAN_WANTACK:
896 ro->want_ack = !!val;
897 break;
898 case WPAN_SECURITY:
899 if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
900 !ns_capable(net->user_ns, CAP_NET_RAW)) {
901 err = -EPERM;
902 break;
903 }
904
905 switch (val) {
906 case WPAN_SECURITY_DEFAULT:
907 ro->secen_override = 0;
908 break;
909 case WPAN_SECURITY_ON:
910 ro->secen_override = 1;
911 ro->secen = 1;
912 break;
913 case WPAN_SECURITY_OFF:
914 ro->secen_override = 1;
915 ro->secen = 0;
916 break;
917 default:
918 err = -EINVAL;
919 break;
920 }
921 break;
922 case WPAN_SECURITY_LEVEL:
923 if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
924 !ns_capable(net->user_ns, CAP_NET_RAW)) {
925 err = -EPERM;
926 break;
927 }
928
929 if (val < WPAN_SECURITY_LEVEL_DEFAULT ||
930 val > IEEE802154_SCF_SECLEVEL_ENC_MIC128) {
931 err = -EINVAL;
932 } else if (val == WPAN_SECURITY_LEVEL_DEFAULT) {
933 ro->seclevel_override = 0;
934 } else {
935 ro->seclevel_override = 1;
936 ro->seclevel = val;
937 }
938 break;
939 default:
940 err = -ENOPROTOOPT;
941 break;
942 }
943
944 release_sock(sk);
945 return err;
946}
947
948static struct proto ieee802154_dgram_prot = {
949 .name = "IEEE-802.15.4-MAC",
950 .owner = THIS_MODULE,
951 .obj_size = sizeof(struct dgram_sock),
952 .init = dgram_init,
953 .close = dgram_close,
954 .bind = dgram_bind,
955 .sendmsg = dgram_sendmsg,
956 .recvmsg = dgram_recvmsg,
957 .hash = dgram_hash,
958 .unhash = dgram_unhash,
959 .connect = dgram_connect,
960 .disconnect = dgram_disconnect,
961 .ioctl = dgram_ioctl,
962 .getsockopt = dgram_getsockopt,
963 .setsockopt = dgram_setsockopt,
964};
965
210static const struct proto_ops ieee802154_dgram_ops = { 966static const struct proto_ops ieee802154_dgram_ops = {
211 .family = PF_IEEE802154, 967 .family = PF_IEEE802154,
212 .owner = THIS_MODULE, 968 .owner = THIS_MODULE,
diff --git a/net/ieee802154/dgram.c b/net/ieee802154/dgram.c
deleted file mode 100644
index d1930b70c4aa..000000000000
--- a/net/ieee802154/dgram.c
+++ /dev/null
@@ -1,549 +0,0 @@
1/*
2 * IEEE 802.15.4 dgram 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 * Written by:
16 * Sergey Lapin <slapin@ossfans.org>
17 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
18 */
19
20#include <linux/capability.h>
21#include <linux/net.h>
22#include <linux/module.h>
23#include <linux/if_arp.h>
24#include <linux/list.h>
25#include <linux/slab.h>
26#include <linux/ieee802154.h>
27#include <net/sock.h>
28#include <net/af_ieee802154.h>
29#include <net/ieee802154_netdev.h>
30
31#include <asm/ioctls.h>
32
33#include "af802154.h"
34
35static HLIST_HEAD(dgram_head);
36static DEFINE_RWLOCK(dgram_lock);
37
38struct dgram_sock {
39 struct sock sk;
40
41 struct ieee802154_addr src_addr;
42 struct ieee802154_addr dst_addr;
43
44 unsigned int bound:1;
45 unsigned int connected:1;
46 unsigned int want_ack:1;
47 unsigned int secen:1;
48 unsigned int secen_override:1;
49 unsigned int seclevel:3;
50 unsigned int seclevel_override:1;
51};
52
53static inline struct dgram_sock *dgram_sk(const struct sock *sk)
54{
55 return container_of(sk, struct dgram_sock, sk);
56}
57
58static void dgram_hash(struct sock *sk)
59{
60 write_lock_bh(&dgram_lock);
61 sk_add_node(sk, &dgram_head);
62 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
63 write_unlock_bh(&dgram_lock);
64}
65
66static void dgram_unhash(struct sock *sk)
67{
68 write_lock_bh(&dgram_lock);
69 if (sk_del_node_init(sk))
70 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
71 write_unlock_bh(&dgram_lock);
72}
73
74static int dgram_init(struct sock *sk)
75{
76 struct dgram_sock *ro = dgram_sk(sk);
77
78 ro->want_ack = 1;
79 return 0;
80}
81
82static void dgram_close(struct sock *sk, long timeout)
83{
84 sk_common_release(sk);
85}
86
87static int dgram_bind(struct sock *sk, struct sockaddr *uaddr, int len)
88{
89 struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
90 struct ieee802154_addr haddr;
91 struct dgram_sock *ro = dgram_sk(sk);
92 int err = -EINVAL;
93 struct net_device *dev;
94
95 lock_sock(sk);
96
97 ro->bound = 0;
98
99 if (len < sizeof(*addr))
100 goto out;
101
102 if (addr->family != AF_IEEE802154)
103 goto out;
104
105 ieee802154_addr_from_sa(&haddr, &addr->addr);
106 dev = ieee802154_get_dev(sock_net(sk), &haddr);
107 if (!dev) {
108 err = -ENODEV;
109 goto out;
110 }
111
112 if (dev->type != ARPHRD_IEEE802154) {
113 err = -ENODEV;
114 goto out_put;
115 }
116
117 ro->src_addr = haddr;
118
119 ro->bound = 1;
120 err = 0;
121out_put:
122 dev_put(dev);
123out:
124 release_sock(sk);
125
126 return err;
127}
128
129static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg)
130{
131 switch (cmd) {
132 case SIOCOUTQ:
133 {
134 int amount = sk_wmem_alloc_get(sk);
135
136 return put_user(amount, (int __user *)arg);
137 }
138
139 case SIOCINQ:
140 {
141 struct sk_buff *skb;
142 unsigned long amount;
143
144 amount = 0;
145 spin_lock_bh(&sk->sk_receive_queue.lock);
146 skb = skb_peek(&sk->sk_receive_queue);
147 if (skb != NULL) {
148 /* We will only return the amount
149 * of this packet since that is all
150 * that will be read.
151 */
152 amount = skb->len - ieee802154_hdr_length(skb);
153 }
154 spin_unlock_bh(&sk->sk_receive_queue.lock);
155 return put_user(amount, (int __user *)arg);
156 }
157 }
158
159 return -ENOIOCTLCMD;
160}
161
162/* FIXME: autobind */
163static int dgram_connect(struct sock *sk, struct sockaddr *uaddr,
164 int len)
165{
166 struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
167 struct dgram_sock *ro = dgram_sk(sk);
168 int err = 0;
169
170 if (len < sizeof(*addr))
171 return -EINVAL;
172
173 if (addr->family != AF_IEEE802154)
174 return -EINVAL;
175
176 lock_sock(sk);
177
178 if (!ro->bound) {
179 err = -ENETUNREACH;
180 goto out;
181 }
182
183 ieee802154_addr_from_sa(&ro->dst_addr, &addr->addr);
184 ro->connected = 1;
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 ro->connected = 0;
197 release_sock(sk);
198
199 return 0;
200}
201
202static int dgram_sendmsg(struct kiocb *iocb, struct sock *sk,
203 struct msghdr *msg, size_t size)
204{
205 struct net_device *dev;
206 unsigned int mtu;
207 struct sk_buff *skb;
208 struct ieee802154_mac_cb *cb;
209 struct dgram_sock *ro = dgram_sk(sk);
210 struct ieee802154_addr dst_addr;
211 int hlen, tlen;
212 int err;
213
214 if (msg->msg_flags & MSG_OOB) {
215 pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
216 return -EOPNOTSUPP;
217 }
218
219 if (!ro->connected && !msg->msg_name)
220 return -EDESTADDRREQ;
221 else if (ro->connected && msg->msg_name)
222 return -EISCONN;
223
224 if (!ro->bound)
225 dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
226 else
227 dev = ieee802154_get_dev(sock_net(sk), &ro->src_addr);
228
229 if (!dev) {
230 pr_debug("no dev\n");
231 err = -ENXIO;
232 goto out;
233 }
234 mtu = dev->mtu;
235 pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
236
237 if (size > mtu) {
238 pr_debug("size = %Zu, mtu = %u\n", size, mtu);
239 err = -EMSGSIZE;
240 goto out_dev;
241 }
242
243 hlen = LL_RESERVED_SPACE(dev);
244 tlen = dev->needed_tailroom;
245 skb = sock_alloc_send_skb(sk, hlen + tlen + size,
246 msg->msg_flags & MSG_DONTWAIT,
247 &err);
248 if (!skb)
249 goto out_dev;
250
251 skb_reserve(skb, hlen);
252
253 skb_reset_network_header(skb);
254
255 cb = mac_cb_init(skb);
256 cb->type = IEEE802154_FC_TYPE_DATA;
257 cb->ackreq = ro->want_ack;
258
259 if (msg->msg_name) {
260 DECLARE_SOCKADDR(struct sockaddr_ieee802154*,
261 daddr, msg->msg_name);
262
263 ieee802154_addr_from_sa(&dst_addr, &daddr->addr);
264 } else {
265 dst_addr = ro->dst_addr;
266 }
267
268 cb->secen = ro->secen;
269 cb->secen_override = ro->secen_override;
270 cb->seclevel = ro->seclevel;
271 cb->seclevel_override = ro->seclevel_override;
272
273 err = dev_hard_header(skb, dev, ETH_P_IEEE802154, &dst_addr,
274 ro->bound ? &ro->src_addr : NULL, size);
275 if (err < 0)
276 goto out_skb;
277
278 err = memcpy_from_msg(skb_put(skb, size), msg, size);
279 if (err < 0)
280 goto out_skb;
281
282 skb->dev = dev;
283 skb->sk = sk;
284 skb->protocol = htons(ETH_P_IEEE802154);
285
286 dev_put(dev);
287
288 err = dev_queue_xmit(skb);
289 if (err > 0)
290 err = net_xmit_errno(err);
291
292 return err ?: size;
293
294out_skb:
295 kfree_skb(skb);
296out_dev:
297 dev_put(dev);
298out:
299 return err;
300}
301
302static int dgram_recvmsg(struct kiocb *iocb, struct sock *sk,
303 struct msghdr *msg, size_t len, int noblock,
304 int flags, int *addr_len)
305{
306 size_t copied = 0;
307 int err = -EOPNOTSUPP;
308 struct sk_buff *skb;
309 DECLARE_SOCKADDR(struct sockaddr_ieee802154 *, saddr, msg->msg_name);
310
311 skb = skb_recv_datagram(sk, flags, noblock, &err);
312 if (!skb)
313 goto out;
314
315 copied = skb->len;
316 if (len < copied) {
317 msg->msg_flags |= MSG_TRUNC;
318 copied = len;
319 }
320
321 /* FIXME: skip headers if necessary ?! */
322 err = skb_copy_datagram_msg(skb, 0, msg, copied);
323 if (err)
324 goto done;
325
326 sock_recv_ts_and_drops(msg, sk, skb);
327
328 if (saddr) {
329 saddr->family = AF_IEEE802154;
330 ieee802154_addr_to_sa(&saddr->addr, &mac_cb(skb)->source);
331 *addr_len = sizeof(*saddr);
332 }
333
334 if (flags & MSG_TRUNC)
335 copied = skb->len;
336done:
337 skb_free_datagram(sk, skb);
338out:
339 if (err)
340 return err;
341 return copied;
342}
343
344static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb)
345{
346 skb = skb_share_check(skb, GFP_ATOMIC);
347 if (!skb)
348 return NET_RX_DROP;
349
350 if (sock_queue_rcv_skb(sk, skb) < 0) {
351 kfree_skb(skb);
352 return NET_RX_DROP;
353 }
354
355 return NET_RX_SUCCESS;
356}
357
358static inline bool
359ieee802154_match_sock(__le64 hw_addr, __le16 pan_id, __le16 short_addr,
360 struct dgram_sock *ro)
361{
362 if (!ro->bound)
363 return true;
364
365 if (ro->src_addr.mode == IEEE802154_ADDR_LONG &&
366 hw_addr == ro->src_addr.extended_addr)
367 return true;
368
369 if (ro->src_addr.mode == IEEE802154_ADDR_SHORT &&
370 pan_id == ro->src_addr.pan_id &&
371 short_addr == ro->src_addr.short_addr)
372 return true;
373
374 return false;
375}
376
377int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
378{
379 struct sock *sk, *prev = NULL;
380 int ret = NET_RX_SUCCESS;
381 __le16 pan_id, short_addr;
382 __le64 hw_addr;
383
384 /* Data frame processing */
385 BUG_ON(dev->type != ARPHRD_IEEE802154);
386
387 pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
388 short_addr = ieee802154_mlme_ops(dev)->get_short_addr(dev);
389 hw_addr = ieee802154_devaddr_from_raw(dev->dev_addr);
390
391 read_lock(&dgram_lock);
392 sk_for_each(sk, &dgram_head) {
393 if (ieee802154_match_sock(hw_addr, pan_id, short_addr,
394 dgram_sk(sk))) {
395 if (prev) {
396 struct sk_buff *clone;
397
398 clone = skb_clone(skb, GFP_ATOMIC);
399 if (clone)
400 dgram_rcv_skb(prev, clone);
401 }
402
403 prev = sk;
404 }
405 }
406
407 if (prev) {
408 dgram_rcv_skb(prev, skb);
409 } else {
410 kfree_skb(skb);
411 ret = NET_RX_DROP;
412 }
413 read_unlock(&dgram_lock);
414
415 return ret;
416}
417
418static int dgram_getsockopt(struct sock *sk, int level, int optname,
419 char __user *optval, int __user *optlen)
420{
421 struct dgram_sock *ro = dgram_sk(sk);
422
423 int val, len;
424
425 if (level != SOL_IEEE802154)
426 return -EOPNOTSUPP;
427
428 if (get_user(len, optlen))
429 return -EFAULT;
430
431 len = min_t(unsigned int, len, sizeof(int));
432
433 switch (optname) {
434 case WPAN_WANTACK:
435 val = ro->want_ack;
436 break;
437 case WPAN_SECURITY:
438 if (!ro->secen_override)
439 val = WPAN_SECURITY_DEFAULT;
440 else if (ro->secen)
441 val = WPAN_SECURITY_ON;
442 else
443 val = WPAN_SECURITY_OFF;
444 break;
445 case WPAN_SECURITY_LEVEL:
446 if (!ro->seclevel_override)
447 val = WPAN_SECURITY_LEVEL_DEFAULT;
448 else
449 val = ro->seclevel;
450 break;
451 default:
452 return -ENOPROTOOPT;
453 }
454
455 if (put_user(len, optlen))
456 return -EFAULT;
457 if (copy_to_user(optval, &val, len))
458 return -EFAULT;
459 return 0;
460}
461
462static int dgram_setsockopt(struct sock *sk, int level, int optname,
463 char __user *optval, unsigned int optlen)
464{
465 struct dgram_sock *ro = dgram_sk(sk);
466 struct net *net = sock_net(sk);
467 int val;
468 int err = 0;
469
470 if (optlen < sizeof(int))
471 return -EINVAL;
472
473 if (get_user(val, (int __user *)optval))
474 return -EFAULT;
475
476 lock_sock(sk);
477
478 switch (optname) {
479 case WPAN_WANTACK:
480 ro->want_ack = !!val;
481 break;
482 case WPAN_SECURITY:
483 if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
484 !ns_capable(net->user_ns, CAP_NET_RAW)) {
485 err = -EPERM;
486 break;
487 }
488
489 switch (val) {
490 case WPAN_SECURITY_DEFAULT:
491 ro->secen_override = 0;
492 break;
493 case WPAN_SECURITY_ON:
494 ro->secen_override = 1;
495 ro->secen = 1;
496 break;
497 case WPAN_SECURITY_OFF:
498 ro->secen_override = 1;
499 ro->secen = 0;
500 break;
501 default:
502 err = -EINVAL;
503 break;
504 }
505 break;
506 case WPAN_SECURITY_LEVEL:
507 if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
508 !ns_capable(net->user_ns, CAP_NET_RAW)) {
509 err = -EPERM;
510 break;
511 }
512
513 if (val < WPAN_SECURITY_LEVEL_DEFAULT ||
514 val > IEEE802154_SCF_SECLEVEL_ENC_MIC128) {
515 err = -EINVAL;
516 } else if (val == WPAN_SECURITY_LEVEL_DEFAULT) {
517 ro->seclevel_override = 0;
518 } else {
519 ro->seclevel_override = 1;
520 ro->seclevel = val;
521 }
522 break;
523 default:
524 err = -ENOPROTOOPT;
525 break;
526 }
527
528 release_sock(sk);
529 return err;
530}
531
532struct proto ieee802154_dgram_prot = {
533 .name = "IEEE-802.15.4-MAC",
534 .owner = THIS_MODULE,
535 .obj_size = sizeof(struct dgram_sock),
536 .init = dgram_init,
537 .close = dgram_close,
538 .bind = dgram_bind,
539 .sendmsg = dgram_sendmsg,
540 .recvmsg = dgram_recvmsg,
541 .hash = dgram_hash,
542 .unhash = dgram_unhash,
543 .connect = dgram_connect,
544 .disconnect = dgram_disconnect,
545 .ioctl = dgram_ioctl,
546 .getsockopt = dgram_getsockopt,
547 .setsockopt = dgram_setsockopt,
548};
549
diff --git a/net/ieee802154/raw.c b/net/ieee802154/raw.c
deleted file mode 100644
index 1674b115c891..000000000000
--- a/net/ieee802154/raw.c
+++ /dev/null
@@ -1,270 +0,0 @@
1/*
2 * Raw IEEE 802.15.4 sockets
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 * Written by:
16 * Sergey Lapin <slapin@ossfans.org>
17 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
18 */
19
20#include <linux/net.h>
21#include <linux/module.h>
22#include <linux/if_arp.h>
23#include <linux/list.h>
24#include <linux/slab.h>
25#include <net/sock.h>
26#include <net/af_ieee802154.h>
27#include <net/ieee802154_netdev.h>
28
29#include "af802154.h"
30
31static HLIST_HEAD(raw_head);
32static DEFINE_RWLOCK(raw_lock);
33
34static void raw_hash(struct sock *sk)
35{
36 write_lock_bh(&raw_lock);
37 sk_add_node(sk, &raw_head);
38 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
39 write_unlock_bh(&raw_lock);
40}
41
42static void raw_unhash(struct sock *sk)
43{
44 write_lock_bh(&raw_lock);
45 if (sk_del_node_init(sk))
46 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
47 write_unlock_bh(&raw_lock);
48}
49
50static void raw_close(struct sock *sk, long timeout)
51{
52 sk_common_release(sk);
53}
54
55static int raw_bind(struct sock *sk, struct sockaddr *_uaddr, int len)
56{
57 struct ieee802154_addr addr;
58 struct sockaddr_ieee802154 *uaddr = (struct sockaddr_ieee802154 *)_uaddr;
59 int err = 0;
60 struct net_device *dev = NULL;
61
62 if (len < sizeof(*uaddr))
63 return -EINVAL;
64
65 uaddr = (struct sockaddr_ieee802154 *)_uaddr;
66 if (uaddr->family != AF_IEEE802154)
67 return -EINVAL;
68
69 lock_sock(sk);
70
71 ieee802154_addr_from_sa(&addr, &uaddr->addr);
72 dev = ieee802154_get_dev(sock_net(sk), &addr);
73 if (!dev) {
74 err = -ENODEV;
75 goto out;
76 }
77
78 if (dev->type != ARPHRD_IEEE802154) {
79 err = -ENODEV;
80 goto out_put;
81 }
82
83 sk->sk_bound_dev_if = dev->ifindex;
84 sk_dst_reset(sk);
85
86out_put:
87 dev_put(dev);
88out:
89 release_sock(sk);
90
91 return err;
92}
93
94static int raw_connect(struct sock *sk, struct sockaddr *uaddr,
95 int addr_len)
96{
97 return -ENOTSUPP;
98}
99
100static int raw_disconnect(struct sock *sk, int flags)
101{
102 return 0;
103}
104
105static int raw_sendmsg(struct kiocb *iocb, struct sock *sk,
106 struct msghdr *msg, size_t size)
107{
108 struct net_device *dev;
109 unsigned int mtu;
110 struct sk_buff *skb;
111 int hlen, tlen;
112 int err;
113
114 if (msg->msg_flags & MSG_OOB) {
115 pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
116 return -EOPNOTSUPP;
117 }
118
119 lock_sock(sk);
120 if (!sk->sk_bound_dev_if)
121 dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
122 else
123 dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if);
124 release_sock(sk);
125
126 if (!dev) {
127 pr_debug("no dev\n");
128 err = -ENXIO;
129 goto out;
130 }
131
132 mtu = dev->mtu;
133 pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
134
135 if (size > mtu) {
136 pr_debug("size = %Zu, mtu = %u\n", size, mtu);
137 err = -EINVAL;
138 goto out_dev;
139 }
140
141 hlen = LL_RESERVED_SPACE(dev);
142 tlen = dev->needed_tailroom;
143 skb = sock_alloc_send_skb(sk, hlen + tlen + size,
144 msg->msg_flags & MSG_DONTWAIT, &err);
145 if (!skb)
146 goto out_dev;
147
148 skb_reserve(skb, hlen);
149
150 skb_reset_mac_header(skb);
151 skb_reset_network_header(skb);
152
153 err = memcpy_from_msg(skb_put(skb, size), msg, size);
154 if (err < 0)
155 goto out_skb;
156
157 skb->dev = dev;
158 skb->sk = sk;
159 skb->protocol = htons(ETH_P_IEEE802154);
160
161 dev_put(dev);
162
163 err = dev_queue_xmit(skb);
164 if (err > 0)
165 err = net_xmit_errno(err);
166
167 return err ?: size;
168
169out_skb:
170 kfree_skb(skb);
171out_dev:
172 dev_put(dev);
173out:
174 return err;
175}
176
177static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
178 size_t len, int noblock, int flags, int *addr_len)
179{
180 size_t copied = 0;
181 int err = -EOPNOTSUPP;
182 struct sk_buff *skb;
183
184 skb = skb_recv_datagram(sk, flags, noblock, &err);
185 if (!skb)
186 goto out;
187
188 copied = skb->len;
189 if (len < copied) {
190 msg->msg_flags |= MSG_TRUNC;
191 copied = len;
192 }
193
194 err = skb_copy_datagram_msg(skb, 0, msg, copied);
195 if (err)
196 goto done;
197
198 sock_recv_ts_and_drops(msg, sk, skb);
199
200 if (flags & MSG_TRUNC)
201 copied = skb->len;
202done:
203 skb_free_datagram(sk, skb);
204out:
205 if (err)
206 return err;
207 return copied;
208}
209
210static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb)
211{
212 skb = skb_share_check(skb, GFP_ATOMIC);
213 if (!skb)
214 return NET_RX_DROP;
215
216 if (sock_queue_rcv_skb(sk, skb) < 0) {
217 kfree_skb(skb);
218 return NET_RX_DROP;
219 }
220
221 return NET_RX_SUCCESS;
222}
223
224void ieee802154_raw_deliver(struct net_device *dev, struct sk_buff *skb)
225{
226 struct sock *sk;
227
228 read_lock(&raw_lock);
229 sk_for_each(sk, &raw_head) {
230 bh_lock_sock(sk);
231 if (!sk->sk_bound_dev_if ||
232 sk->sk_bound_dev_if == dev->ifindex) {
233 struct sk_buff *clone;
234
235 clone = skb_clone(skb, GFP_ATOMIC);
236 if (clone)
237 raw_rcv_skb(sk, clone);
238 }
239 bh_unlock_sock(sk);
240 }
241 read_unlock(&raw_lock);
242}
243
244static int raw_getsockopt(struct sock *sk, int level, int optname,
245 char __user *optval, int __user *optlen)
246{
247 return -EOPNOTSUPP;
248}
249
250static int raw_setsockopt(struct sock *sk, int level, int optname,
251 char __user *optval, unsigned int optlen)
252{
253 return -EOPNOTSUPP;
254}
255
256struct proto ieee802154_raw_prot = {
257 .name = "IEEE-802.15.4-RAW",
258 .owner = THIS_MODULE,
259 .obj_size = sizeof(struct sock),
260 .close = raw_close,
261 .bind = raw_bind,
262 .sendmsg = raw_sendmsg,
263 .recvmsg = raw_recvmsg,
264 .hash = raw_hash,
265 .unhash = raw_unhash,
266 .connect = raw_connect,
267 .disconnect = raw_disconnect,
268 .getsockopt = raw_getsockopt,
269 .setsockopt = raw_setsockopt,
270};