1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/net.h>
21#include <linux/capability.h>
22#include <linux/module.h>
23#include <linux/if_arp.h>
24#include <linux/if.h>
25#include <linux/termios.h>
26#include <linux/list.h>
27#include <linux/slab.h>
28#include <net/datalink.h>
29#include <net/psnap.h>
30#include <net/sock.h>
31#include <net/tcp_states.h>
32#include <net/route.h>
33
34#include <net/af_ieee802154.h>
35#include <net/ieee802154_netdev.h>
36
37
38static struct net_device*
39ieee802154_get_dev(struct net *net, const struct ieee802154_addr *addr)
40{
41 struct net_device *dev = NULL;
42 struct net_device *tmp;
43 __le16 pan_id, short_addr;
44 u8 hwaddr[IEEE802154_ADDR_LEN];
45
46 switch (addr->mode) {
47 case IEEE802154_ADDR_LONG:
48 ieee802154_devaddr_to_raw(hwaddr, addr->extended_addr);
49 rcu_read_lock();
50 dev = dev_getbyhwaddr_rcu(net, ARPHRD_IEEE802154, hwaddr);
51 if (dev)
52 dev_hold(dev);
53 rcu_read_unlock();
54 break;
55 case IEEE802154_ADDR_SHORT:
56 if (addr->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST) ||
57 addr->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
58 addr->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST))
59 break;
60
61 rtnl_lock();
62
63 for_each_netdev(net, tmp) {
64 if (tmp->type != ARPHRD_IEEE802154)
65 continue;
66
67 pan_id = tmp->ieee802154_ptr->pan_id;
68 short_addr = tmp->ieee802154_ptr->short_addr;
69 if (pan_id == addr->pan_id &&
70 short_addr == addr->short_addr) {
71 dev = tmp;
72 dev_hold(dev);
73 break;
74 }
75 }
76
77 rtnl_unlock();
78 break;
79 default:
80 pr_warn("Unsupported ieee802154 address type: %d\n",
81 addr->mode);
82 break;
83 }
84
85 return dev;
86}
87
88static int ieee802154_sock_release(struct socket *sock)
89{
90 struct sock *sk = sock->sk;
91
92 if (sk) {
93 sock->sk = NULL;
94 sk->sk_prot->close(sk, 0);
95 }
96 return 0;
97}
98
99static int ieee802154_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
100 struct msghdr *msg, size_t len)
101{
102 struct sock *sk = sock->sk;
103
104 return sk->sk_prot->sendmsg(iocb, sk, msg, len);
105}
106
107static int ieee802154_sock_bind(struct socket *sock, struct sockaddr *uaddr,
108 int addr_len)
109{
110 struct sock *sk = sock->sk;
111
112 if (sk->sk_prot->bind)
113 return sk->sk_prot->bind(sk, uaddr, addr_len);
114
115 return sock_no_bind(sock, uaddr, addr_len);
116}
117
118static int ieee802154_sock_connect(struct socket *sock, struct sockaddr *uaddr,
119 int addr_len, int flags)
120{
121 struct sock *sk = sock->sk;
122
123 if (addr_len < sizeof(uaddr->sa_family))
124 return -EINVAL;
125
126 if (uaddr->sa_family == AF_UNSPEC)
127 return sk->sk_prot->disconnect(sk, flags);
128
129 return sk->sk_prot->connect(sk, uaddr, addr_len);
130}
131
132static int ieee802154_dev_ioctl(struct sock *sk, struct ifreq __user *arg,
133 unsigned int cmd)
134{
135 struct ifreq ifr;
136 int ret = -ENOIOCTLCMD;
137 struct net_device *dev;
138
139 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
140 return -EFAULT;
141
142 ifr.ifr_name[IFNAMSIZ-1] = 0;
143
144 dev_load(sock_net(sk), ifr.ifr_name);
145 dev = dev_get_by_name(sock_net(sk), ifr.ifr_name);
146
147 if (!dev)
148 return -ENODEV;
149
150 if (dev->type == ARPHRD_IEEE802154 && dev->netdev_ops->ndo_do_ioctl)
151 ret = dev->netdev_ops->ndo_do_ioctl(dev, &ifr, cmd);
152
153 if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq)))
154 ret = -EFAULT;
155 dev_put(dev);
156
157 return ret;
158}
159
160static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd,
161 unsigned long arg)
162{
163 struct sock *sk = sock->sk;
164
165 switch (cmd) {
166 case SIOCGSTAMP:
167 return sock_get_timestamp(sk, (struct timeval __user *)arg);
168 case SIOCGSTAMPNS:
169 return sock_get_timestampns(sk, (struct timespec __user *)arg);
170 case SIOCGIFADDR:
171 case SIOCSIFADDR:
172 return ieee802154_dev_ioctl(sk, (struct ifreq __user *)arg,
173 cmd);
174 default:
175 if (!sk->sk_prot->ioctl)
176 return -ENOIOCTLCMD;
177 return sk->sk_prot->ioctl(sk, cmd, arg);
178 }
179}
180
181
182static HLIST_HEAD(raw_head);
183static DEFINE_RWLOCK(raw_lock);
184
185static void raw_hash(struct sock *sk)
186{
187 write_lock_bh(&raw_lock);
188 sk_add_node(sk, &raw_head);
189 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
190 write_unlock_bh(&raw_lock);
191}
192
193static void raw_unhash(struct sock *sk)
194{
195 write_lock_bh(&raw_lock);
196 if (sk_del_node_init(sk))
197 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
198 write_unlock_bh(&raw_lock);
199}
200
201static void raw_close(struct sock *sk, long timeout)
202{
203 sk_common_release(sk);
204}
205
206static int raw_bind(struct sock *sk, struct sockaddr *_uaddr, int len)
207{
208 struct ieee802154_addr addr;
209 struct sockaddr_ieee802154 *uaddr = (struct sockaddr_ieee802154 *)_uaddr;
210 int err = 0;
211 struct net_device *dev = NULL;
212
213 if (len < sizeof(*uaddr))
214 return -EINVAL;
215
216 uaddr = (struct sockaddr_ieee802154 *)_uaddr;
217 if (uaddr->family != AF_IEEE802154)
218 return -EINVAL;
219
220 lock_sock(sk);
221
222 ieee802154_addr_from_sa(&addr, &uaddr->addr);
223 dev = ieee802154_get_dev(sock_net(sk), &addr);
224 if (!dev) {
225 err = -ENODEV;
226 goto out;
227 }
228
229 sk->sk_bound_dev_if = dev->ifindex;
230 sk_dst_reset(sk);
231
232 dev_put(dev);
233out:
234 release_sock(sk);
235
236 return err;
237}
238
239static int raw_connect(struct sock *sk, struct sockaddr *uaddr,
240 int addr_len)
241{
242 return -ENOTSUPP;
243}
244
245static int raw_disconnect(struct sock *sk, int flags)
246{
247 return 0;
248}
249
250static int raw_sendmsg(struct kiocb *iocb, struct sock *sk,
251 struct msghdr *msg, size_t size)
252{
253 struct net_device *dev;
254 unsigned int mtu;
255 struct sk_buff *skb;
256 int hlen, tlen;
257 int err;
258
259 if (msg->msg_flags & MSG_OOB) {
260 pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
261 return -EOPNOTSUPP;
262 }
263
264 lock_sock(sk);
265 if (!sk->sk_bound_dev_if)
266 dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
267 else
268 dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if);
269 release_sock(sk);
270
271 if (!dev) {
272 pr_debug("no dev\n");
273 err = -ENXIO;
274 goto out;
275 }
276
277 mtu = IEEE802154_MTU;
278 pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
279
280 if (size > mtu) {
281 pr_debug("size = %Zu, mtu = %u\n", size, mtu);
282 err = -EMSGSIZE;
283 goto out_dev;
284 }
285
286 hlen = LL_RESERVED_SPACE(dev);
287 tlen = dev->needed_tailroom;
288 skb = sock_alloc_send_skb(sk, hlen + tlen + size,
289 msg->msg_flags & MSG_DONTWAIT, &err);
290 if (!skb)
291 goto out_dev;
292
293 skb_reserve(skb, hlen);
294
295 skb_reset_mac_header(skb);
296 skb_reset_network_header(skb);
297
298 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
299 if (err < 0)
300 goto out_skb;
301
302 skb->dev = dev;
303 skb->sk = sk;
304 skb->protocol = htons(ETH_P_IEEE802154);
305
306 dev_put(dev);
307
308 err = dev_queue_xmit(skb);
309 if (err > 0)
310 err = net_xmit_errno(err);
311
312 return err ?: size;
313
314out_skb:
315 kfree_skb(skb);
316out_dev:
317 dev_put(dev);
318out:
319 return err;
320}
321
322static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
323 size_t len, int noblock, int flags, int *addr_len)
324{
325 size_t copied = 0;
326 int err = -EOPNOTSUPP;
327 struct sk_buff *skb;
328
329 skb = skb_recv_datagram(sk, flags, noblock, &err);
330 if (!skb)
331 goto out;
332
333 copied = skb->len;
334 if (len < copied) {
335 msg->msg_flags |= MSG_TRUNC;
336 copied = len;
337 }
338
339 err = skb_copy_datagram_msg(skb, 0, msg, copied);
340 if (err)
341 goto done;
342
343 sock_recv_ts_and_drops(msg, sk, skb);
344
345 if (flags & MSG_TRUNC)
346 copied = skb->len;
347done:
348 skb_free_datagram(sk, skb);
349out:
350 if (err)
351 return err;
352 return copied;
353}
354
355static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb)
356{
357 skb = skb_share_check(skb, GFP_ATOMIC);
358 if (!skb)
359 return NET_RX_DROP;
360
361 if (sock_queue_rcv_skb(sk, skb) < 0) {
362 kfree_skb(skb);
363 return NET_RX_DROP;
364 }
365
366 return NET_RX_SUCCESS;
367}
368
369static void ieee802154_raw_deliver(struct net_device *dev, struct sk_buff *skb)
370{
371 struct sock *sk;
372
373 read_lock(&raw_lock);
374 sk_for_each(sk, &raw_head) {
375 bh_lock_sock(sk);
376 if (!sk->sk_bound_dev_if ||
377 sk->sk_bound_dev_if == dev->ifindex) {
378 struct sk_buff *clone;
379
380 clone = skb_clone(skb, GFP_ATOMIC);
381 if (clone)
382 raw_rcv_skb(sk, clone);
383 }
384 bh_unlock_sock(sk);
385 }
386 read_unlock(&raw_lock);
387}
388
389static int raw_getsockopt(struct sock *sk, int level, int optname,
390 char __user *optval, int __user *optlen)
391{
392 return -EOPNOTSUPP;
393}
394
395static int raw_setsockopt(struct sock *sk, int level, int optname,
396 char __user *optval, unsigned int optlen)
397{
398 return -EOPNOTSUPP;
399}
400
401static struct proto ieee802154_raw_prot = {
402 .name = "IEEE-802.15.4-RAW",
403 .owner = THIS_MODULE,
404 .obj_size = sizeof(struct sock),
405 .close = raw_close,
406 .bind = raw_bind,
407 .sendmsg = raw_sendmsg,
408 .recvmsg = raw_recvmsg,
409 .hash = raw_hash,
410 .unhash = raw_unhash,
411 .connect = raw_connect,
412 .disconnect = raw_disconnect,
413 .getsockopt = raw_getsockopt,
414 .setsockopt = raw_setsockopt,
415};
416
417static const struct proto_ops ieee802154_raw_ops = {
418 .family = PF_IEEE802154,
419 .owner = THIS_MODULE,
420 .release = ieee802154_sock_release,
421 .bind = ieee802154_sock_bind,
422 .connect = ieee802154_sock_connect,
423 .socketpair = sock_no_socketpair,
424 .accept = sock_no_accept,
425 .getname = sock_no_getname,
426 .poll = datagram_poll,
427 .ioctl = ieee802154_sock_ioctl,
428 .listen = sock_no_listen,
429 .shutdown = sock_no_shutdown,
430 .setsockopt = sock_common_setsockopt,
431 .getsockopt = sock_common_getsockopt,
432 .sendmsg = ieee802154_sock_sendmsg,
433 .recvmsg = sock_common_recvmsg,
434 .mmap = sock_no_mmap,
435 .sendpage = sock_no_sendpage,
436#ifdef CONFIG_COMPAT
437 .compat_setsockopt = compat_sock_common_setsockopt,
438 .compat_getsockopt = compat_sock_common_getsockopt,
439#endif
440};
441
442
443static HLIST_HEAD(dgram_head);
444static DEFINE_RWLOCK(dgram_lock);
445
446struct dgram_sock {
447 struct sock sk;
448
449 struct ieee802154_addr src_addr;
450 struct ieee802154_addr dst_addr;
451
452 unsigned int bound:1;
453 unsigned int connected:1;
454 unsigned int want_ack:1;
455 unsigned int secen:1;
456 unsigned int secen_override:1;
457 unsigned int seclevel:3;
458 unsigned int seclevel_override:1;
459};
460
461static inline struct dgram_sock *dgram_sk(const struct sock *sk)
462{
463 return container_of(sk, struct dgram_sock, sk);
464}
465
466static void dgram_hash(struct sock *sk)
467{
468 write_lock_bh(&dgram_lock);
469 sk_add_node(sk, &dgram_head);
470 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
471 write_unlock_bh(&dgram_lock);
472}
473
474static void dgram_unhash(struct sock *sk)
475{
476 write_lock_bh(&dgram_lock);
477 if (sk_del_node_init(sk))
478 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
479 write_unlock_bh(&dgram_lock);
480}
481
482static int dgram_init(struct sock *sk)
483{
484 struct dgram_sock *ro = dgram_sk(sk);
485
486 ro->want_ack = 1;
487 return 0;
488}
489
490static void dgram_close(struct sock *sk, long timeout)
491{
492 sk_common_release(sk);
493}
494
495static int dgram_bind(struct sock *sk, struct sockaddr *uaddr, int len)
496{
497 struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
498 struct ieee802154_addr haddr;
499 struct dgram_sock *ro = dgram_sk(sk);
500 int err = -EINVAL;
501 struct net_device *dev;
502
503 lock_sock(sk);
504
505 ro->bound = 0;
506
507 if (len < sizeof(*addr))
508 goto out;
509
510 if (addr->family != AF_IEEE802154)
511 goto out;
512
513 ieee802154_addr_from_sa(&haddr, &addr->addr);
514 dev = ieee802154_get_dev(sock_net(sk), &haddr);
515 if (!dev) {
516 err = -ENODEV;
517 goto out;
518 }
519
520 if (dev->type != ARPHRD_IEEE802154) {
521 err = -ENODEV;
522 goto out_put;
523 }
524
525 ro->src_addr = haddr;
526
527 ro->bound = 1;
528 err = 0;
529out_put:
530 dev_put(dev);
531out:
532 release_sock(sk);
533
534 return err;
535}
536
537static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg)
538{
539 switch (cmd) {
540 case SIOCOUTQ:
541 {
542 int amount = sk_wmem_alloc_get(sk);
543
544 return put_user(amount, (int __user *)arg);
545 }
546
547 case SIOCINQ:
548 {
549 struct sk_buff *skb;
550 unsigned long amount;
551
552 amount = 0;
553 spin_lock_bh(&sk->sk_receive_queue.lock);
554 skb = skb_peek(&sk->sk_receive_queue);
555 if (skb) {
556
557
558
559
560 amount = skb->len - ieee802154_hdr_length(skb);
561 }
562 spin_unlock_bh(&sk->sk_receive_queue.lock);
563 return put_user(amount, (int __user *)arg);
564 }
565 }
566
567 return -ENOIOCTLCMD;
568}
569
570
571static int dgram_connect(struct sock *sk, struct sockaddr *uaddr,
572 int len)
573{
574 struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
575 struct dgram_sock *ro = dgram_sk(sk);
576 int err = 0;
577
578 if (len < sizeof(*addr))
579 return -EINVAL;
580
581 if (addr->family != AF_IEEE802154)
582 return -EINVAL;
583
584 lock_sock(sk);
585
586 if (!ro->bound) {
587 err = -ENETUNREACH;
588 goto out;
589 }
590
591 ieee802154_addr_from_sa(&ro->dst_addr, &addr->addr);
592 ro->connected = 1;
593
594out:
595 release_sock(sk);
596 return err;
597}
598
599static int dgram_disconnect(struct sock *sk, int flags)
600{
601 struct dgram_sock *ro = dgram_sk(sk);
602
603 lock_sock(sk);
604 ro->connected = 0;
605 release_sock(sk);
606
607 return 0;
608}
609
610static int dgram_sendmsg(struct kiocb *iocb, struct sock *sk,
611 struct msghdr *msg, size_t size)
612{
613 struct net_device *dev;
614 unsigned int mtu;
615 struct sk_buff *skb;
616 struct ieee802154_mac_cb *cb;
617 struct dgram_sock *ro = dgram_sk(sk);
618 struct ieee802154_addr dst_addr;
619 int hlen, tlen;
620 int err;
621
622 if (msg->msg_flags & MSG_OOB) {
623 pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
624 return -EOPNOTSUPP;
625 }
626
627 if (!ro->connected && !msg->msg_name)
628 return -EDESTADDRREQ;
629 else if (ro->connected && msg->msg_name)
630 return -EISCONN;
631
632 if (!ro->bound)
633 dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
634 else
635 dev = ieee802154_get_dev(sock_net(sk), &ro->src_addr);
636
637 if (!dev) {
638 pr_debug("no dev\n");
639 err = -ENXIO;
640 goto out;
641 }
642 mtu = IEEE802154_MTU;
643 pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
644
645 if (size > mtu) {
646 pr_debug("size = %Zu, mtu = %u\n", size, mtu);
647 err = -EMSGSIZE;
648 goto out_dev;
649 }
650
651 hlen = LL_RESERVED_SPACE(dev);
652 tlen = dev->needed_tailroom;
653 skb = sock_alloc_send_skb(sk, hlen + tlen + size,
654 msg->msg_flags & MSG_DONTWAIT,
655 &err);
656 if (!skb)
657 goto out_dev;
658
659 skb_reserve(skb, hlen);
660
661 skb_reset_network_header(skb);
662
663 cb = mac_cb_init(skb);
664 cb->type = IEEE802154_FC_TYPE_DATA;
665 cb->ackreq = ro->want_ack;
666
667 if (msg->msg_name) {
668 DECLARE_SOCKADDR(struct sockaddr_ieee802154*,
669 daddr, msg->msg_name);
670
671 ieee802154_addr_from_sa(&dst_addr, &daddr->addr);
672 } else {
673 dst_addr = ro->dst_addr;
674 }
675
676 cb->secen = ro->secen;
677 cb->secen_override = ro->secen_override;
678 cb->seclevel = ro->seclevel;
679 cb->seclevel_override = ro->seclevel_override;
680
681 err = wpan_dev_hard_header(skb, dev, &dst_addr,
682 ro->bound ? &ro->src_addr : NULL, size);
683 if (err < 0)
684 goto out_skb;
685
686 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
687 if (err < 0)
688 goto out_skb;
689
690 skb->dev = dev;
691 skb->sk = sk;
692 skb->protocol = htons(ETH_P_IEEE802154);
693
694 dev_put(dev);
695
696 err = dev_queue_xmit(skb);
697 if (err > 0)
698 err = net_xmit_errno(err);
699
700 return err ?: size;
701
702out_skb:
703 kfree_skb(skb);
704out_dev:
705 dev_put(dev);
706out:
707 return err;
708}
709
710static int dgram_recvmsg(struct kiocb *iocb, struct sock *sk,
711 struct msghdr *msg, size_t len, int noblock,
712 int flags, int *addr_len)
713{
714 size_t copied = 0;
715 int err = -EOPNOTSUPP;
716 struct sk_buff *skb;
717 DECLARE_SOCKADDR(struct sockaddr_ieee802154 *, saddr, msg->msg_name);
718
719 skb = skb_recv_datagram(sk, flags, noblock, &err);
720 if (!skb)
721 goto out;
722
723 copied = skb->len;
724 if (len < copied) {
725 msg->msg_flags |= MSG_TRUNC;
726 copied = len;
727 }
728
729
730 err = skb_copy_datagram_msg(skb, 0, msg, copied);
731 if (err)
732 goto done;
733
734 sock_recv_ts_and_drops(msg, sk, skb);
735
736 if (saddr) {
737
738
739
740
741 memset(saddr, 0, sizeof(*saddr));
742
743 saddr->family = AF_IEEE802154;
744 ieee802154_addr_to_sa(&saddr->addr, &mac_cb(skb)->source);
745 *addr_len = sizeof(*saddr);
746 }
747
748 if (flags & MSG_TRUNC)
749 copied = skb->len;
750done:
751 skb_free_datagram(sk, skb);
752out:
753 if (err)
754 return err;
755 return copied;
756}
757
758static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb)
759{
760 skb = skb_share_check(skb, GFP_ATOMIC);
761 if (!skb)
762 return NET_RX_DROP;
763
764 if (sock_queue_rcv_skb(sk, skb) < 0) {
765 kfree_skb(skb);
766 return NET_RX_DROP;
767 }
768
769 return NET_RX_SUCCESS;
770}
771
772static inline bool
773ieee802154_match_sock(__le64 hw_addr, __le16 pan_id, __le16 short_addr,
774 struct dgram_sock *ro)
775{
776 if (!ro->bound)
777 return true;
778
779 if (ro->src_addr.mode == IEEE802154_ADDR_LONG &&
780 hw_addr == ro->src_addr.extended_addr)
781 return true;
782
783 if (ro->src_addr.mode == IEEE802154_ADDR_SHORT &&
784 pan_id == ro->src_addr.pan_id &&
785 short_addr == ro->src_addr.short_addr)
786 return true;
787
788 return false;
789}
790
791static int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
792{
793 struct sock *sk, *prev = NULL;
794 int ret = NET_RX_SUCCESS;
795 __le16 pan_id, short_addr;
796 __le64 hw_addr;
797
798
799 BUG_ON(dev->type != ARPHRD_IEEE802154);
800
801 pan_id = dev->ieee802154_ptr->pan_id;
802 short_addr = dev->ieee802154_ptr->short_addr;
803 hw_addr = dev->ieee802154_ptr->extended_addr;
804
805 read_lock(&dgram_lock);
806 sk_for_each(sk, &dgram_head) {
807 if (ieee802154_match_sock(hw_addr, pan_id, short_addr,
808 dgram_sk(sk))) {
809 if (prev) {
810 struct sk_buff *clone;
811
812 clone = skb_clone(skb, GFP_ATOMIC);
813 if (clone)
814 dgram_rcv_skb(prev, clone);
815 }
816
817 prev = sk;
818 }
819 }
820
821 if (prev) {
822 dgram_rcv_skb(prev, skb);
823 } else {
824 kfree_skb(skb);
825 ret = NET_RX_DROP;
826 }
827 read_unlock(&dgram_lock);
828
829 return ret;
830}
831
832static int dgram_getsockopt(struct sock *sk, int level, int optname,
833 char __user *optval, int __user *optlen)
834{
835 struct dgram_sock *ro = dgram_sk(sk);
836
837 int val, len;
838
839 if (level != SOL_IEEE802154)
840 return -EOPNOTSUPP;
841
842 if (get_user(len, optlen))
843 return -EFAULT;
844
845 len = min_t(unsigned int, len, sizeof(int));
846
847 switch (optname) {
848 case WPAN_WANTACK:
849 val = ro->want_ack;
850 break;
851 case WPAN_SECURITY:
852 if (!ro->secen_override)
853 val = WPAN_SECURITY_DEFAULT;
854 else if (ro->secen)
855 val = WPAN_SECURITY_ON;
856 else
857 val = WPAN_SECURITY_OFF;
858 break;
859 case WPAN_SECURITY_LEVEL:
860 if (!ro->seclevel_override)
861 val = WPAN_SECURITY_LEVEL_DEFAULT;
862 else
863 val = ro->seclevel;
864 break;
865 default:
866 return -ENOPROTOOPT;
867 }
868
869 if (put_user(len, optlen))
870 return -EFAULT;
871 if (copy_to_user(optval, &val, len))
872 return -EFAULT;
873 return 0;
874}
875
876static int dgram_setsockopt(struct sock *sk, int level, int optname,
877 char __user *optval, unsigned int optlen)
878{
879 struct dgram_sock *ro = dgram_sk(sk);
880 struct net *net = sock_net(sk);
881 int val;
882 int err = 0;
883
884 if (optlen < sizeof(int))
885 return -EINVAL;
886
887 if (get_user(val, (int __user *)optval))
888 return -EFAULT;
889
890 lock_sock(sk);
891
892 switch (optname) {
893 case WPAN_WANTACK:
894 ro->want_ack = !!val;
895 break;
896 case WPAN_SECURITY:
897 if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
898 !ns_capable(net->user_ns, CAP_NET_RAW)) {
899 err = -EPERM;
900 break;
901 }
902
903 switch (val) {
904 case WPAN_SECURITY_DEFAULT:
905 ro->secen_override = 0;
906 break;
907 case WPAN_SECURITY_ON:
908 ro->secen_override = 1;
909 ro->secen = 1;
910 break;
911 case WPAN_SECURITY_OFF:
912 ro->secen_override = 1;
913 ro->secen = 0;
914 break;
915 default:
916 err = -EINVAL;
917 break;
918 }
919 break;
920 case WPAN_SECURITY_LEVEL:
921 if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
922 !ns_capable(net->user_ns, CAP_NET_RAW)) {
923 err = -EPERM;
924 break;
925 }
926
927 if (val < WPAN_SECURITY_LEVEL_DEFAULT ||
928 val > IEEE802154_SCF_SECLEVEL_ENC_MIC128) {
929 err = -EINVAL;
930 } else if (val == WPAN_SECURITY_LEVEL_DEFAULT) {
931 ro->seclevel_override = 0;
932 } else {
933 ro->seclevel_override = 1;
934 ro->seclevel = val;
935 }
936 break;
937 default:
938 err = -ENOPROTOOPT;
939 break;
940 }
941
942 release_sock(sk);
943 return err;
944}
945
946static struct proto ieee802154_dgram_prot = {
947 .name = "IEEE-802.15.4-MAC",
948 .owner = THIS_MODULE,
949 .obj_size = sizeof(struct dgram_sock),
950 .init = dgram_init,
951 .close = dgram_close,
952 .bind = dgram_bind,
953 .sendmsg = dgram_sendmsg,
954 .recvmsg = dgram_recvmsg,
955 .hash = dgram_hash,
956 .unhash = dgram_unhash,
957 .connect = dgram_connect,
958 .disconnect = dgram_disconnect,
959 .ioctl = dgram_ioctl,
960 .getsockopt = dgram_getsockopt,
961 .setsockopt = dgram_setsockopt,
962};
963
964static const struct proto_ops ieee802154_dgram_ops = {
965 .family = PF_IEEE802154,
966 .owner = THIS_MODULE,
967 .release = ieee802154_sock_release,
968 .bind = ieee802154_sock_bind,
969 .connect = ieee802154_sock_connect,
970 .socketpair = sock_no_socketpair,
971 .accept = sock_no_accept,
972 .getname = sock_no_getname,
973 .poll = datagram_poll,
974 .ioctl = ieee802154_sock_ioctl,
975 .listen = sock_no_listen,
976 .shutdown = sock_no_shutdown,
977 .setsockopt = sock_common_setsockopt,
978 .getsockopt = sock_common_getsockopt,
979 .sendmsg = ieee802154_sock_sendmsg,
980 .recvmsg = sock_common_recvmsg,
981 .mmap = sock_no_mmap,
982 .sendpage = sock_no_sendpage,
983#ifdef CONFIG_COMPAT
984 .compat_setsockopt = compat_sock_common_setsockopt,
985 .compat_getsockopt = compat_sock_common_getsockopt,
986#endif
987};
988
989
990
991
992static int ieee802154_create(struct net *net, struct socket *sock,
993 int protocol, int kern)
994{
995 struct sock *sk;
996 int rc;
997 struct proto *proto;
998 const struct proto_ops *ops;
999
1000 if (!net_eq(net, &init_net))
1001 return -EAFNOSUPPORT;
1002
1003 switch (sock->type) {
1004 case SOCK_RAW:
1005 proto = &ieee802154_raw_prot;
1006 ops = &ieee802154_raw_ops;
1007 break;
1008 case SOCK_DGRAM:
1009 proto = &ieee802154_dgram_prot;
1010 ops = &ieee802154_dgram_ops;
1011 break;
1012 default:
1013 rc = -ESOCKTNOSUPPORT;
1014 goto out;
1015 }
1016
1017 rc = -ENOMEM;
1018 sk = sk_alloc(net, PF_IEEE802154, GFP_KERNEL, proto);
1019 if (!sk)
1020 goto out;
1021 rc = 0;
1022
1023 sock->ops = ops;
1024
1025 sock_init_data(sock, sk);
1026
1027 sk->sk_family = PF_IEEE802154;
1028
1029
1030 sock_set_flag(sk, SOCK_ZAPPED);
1031
1032 if (sk->sk_prot->hash)
1033 sk->sk_prot->hash(sk);
1034
1035 if (sk->sk_prot->init) {
1036 rc = sk->sk_prot->init(sk);
1037 if (rc)
1038 sk_common_release(sk);
1039 }
1040out:
1041 return rc;
1042}
1043
1044static const struct net_proto_family ieee802154_family_ops = {
1045 .family = PF_IEEE802154,
1046 .create = ieee802154_create,
1047 .owner = THIS_MODULE,
1048};
1049
1050static int ieee802154_rcv(struct sk_buff *skb, struct net_device *dev,
1051 struct packet_type *pt, struct net_device *orig_dev)
1052{
1053 if (!netif_running(dev))
1054 goto drop;
1055 pr_debug("got frame, type %d, dev %p\n", dev->type, dev);
1056#ifdef DEBUG
1057 print_hex_dump_bytes("ieee802154_rcv ",
1058 DUMP_PREFIX_NONE, skb->data, skb->len);
1059#endif
1060
1061 if (!net_eq(dev_net(dev), &init_net))
1062 goto drop;
1063
1064 ieee802154_raw_deliver(dev, skb);
1065
1066 if (dev->type != ARPHRD_IEEE802154)
1067 goto drop;
1068
1069 if (skb->pkt_type != PACKET_OTHERHOST)
1070 return ieee802154_dgram_deliver(dev, skb);
1071
1072drop:
1073 kfree_skb(skb);
1074 return NET_RX_DROP;
1075}
1076
1077static struct packet_type ieee802154_packet_type = {
1078 .type = htons(ETH_P_IEEE802154),
1079 .func = ieee802154_rcv,
1080};
1081
1082static int __init af_ieee802154_init(void)
1083{
1084 int rc = -EINVAL;
1085
1086 rc = proto_register(&ieee802154_raw_prot, 1);
1087 if (rc)
1088 goto out;
1089
1090 rc = proto_register(&ieee802154_dgram_prot, 1);
1091 if (rc)
1092 goto err_dgram;
1093
1094
1095 rc = sock_register(&ieee802154_family_ops);
1096 if (rc)
1097 goto err_sock;
1098 dev_add_pack(&ieee802154_packet_type);
1099
1100 rc = 0;
1101 goto out;
1102
1103err_sock:
1104 proto_unregister(&ieee802154_dgram_prot);
1105err_dgram:
1106 proto_unregister(&ieee802154_raw_prot);
1107out:
1108 return rc;
1109}
1110
1111static void __exit af_ieee802154_remove(void)
1112{
1113 dev_remove_pack(&ieee802154_packet_type);
1114 sock_unregister(PF_IEEE802154);
1115 proto_unregister(&ieee802154_dgram_prot);
1116 proto_unregister(&ieee802154_raw_prot);
1117}
1118
1119module_init(af_ieee802154_init);
1120module_exit(af_ieee802154_remove);
1121
1122MODULE_LICENSE("GPL");
1123MODULE_ALIAS_NETPROTO(PF_IEEE802154);
1124