powerpc/tm: Unset MSR[TS] if not recheckpointing
[linux/fpc-iii.git] / net / ieee802154 / socket.c
blobbc6b912603f164b981e761974e4e64f39b01dc8f
1 /*
2 * IEEE802154.4 socket interface
4 * Copyright 2007, 2008 Siemens AG
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.
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.
15 * Written by:
16 * Sergey Lapin <slapin@ossfans.org>
17 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
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> /* For TIOCOUTQ/INQ */
26 #include <linux/list.h>
27 #include <linux/slab.h>
28 #include <linux/socket.h>
29 #include <net/datalink.h>
30 #include <net/psnap.h>
31 #include <net/sock.h>
32 #include <net/tcp_states.h>
33 #include <net/route.h>
35 #include <net/af_ieee802154.h>
36 #include <net/ieee802154_netdev.h>
38 /* Utility function for families */
39 static struct net_device*
40 ieee802154_get_dev(struct net *net, const struct ieee802154_addr *addr)
42 struct net_device *dev = NULL;
43 struct net_device *tmp;
44 __le16 pan_id, short_addr;
45 u8 hwaddr[IEEE802154_ADDR_LEN];
47 switch (addr->mode) {
48 case IEEE802154_ADDR_LONG:
49 ieee802154_devaddr_to_raw(hwaddr, addr->extended_addr);
50 rcu_read_lock();
51 dev = dev_getbyhwaddr_rcu(net, ARPHRD_IEEE802154, hwaddr);
52 if (dev)
53 dev_hold(dev);
54 rcu_read_unlock();
55 break;
56 case IEEE802154_ADDR_SHORT:
57 if (addr->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST) ||
58 addr->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
59 addr->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST))
60 break;
62 rtnl_lock();
64 for_each_netdev(net, tmp) {
65 if (tmp->type != ARPHRD_IEEE802154)
66 continue;
68 pan_id = tmp->ieee802154_ptr->pan_id;
69 short_addr = tmp->ieee802154_ptr->short_addr;
70 if (pan_id == addr->pan_id &&
71 short_addr == addr->short_addr) {
72 dev = tmp;
73 dev_hold(dev);
74 break;
78 rtnl_unlock();
79 break;
80 default:
81 pr_warn("Unsupported ieee802154 address type: %d\n",
82 addr->mode);
83 break;
86 return dev;
89 static int ieee802154_sock_release(struct socket *sock)
91 struct sock *sk = sock->sk;
93 if (sk) {
94 sock->sk = NULL;
95 sk->sk_prot->close(sk, 0);
97 return 0;
100 static int ieee802154_sock_sendmsg(struct socket *sock, struct msghdr *msg,
101 size_t len)
103 struct sock *sk = sock->sk;
105 return sk->sk_prot->sendmsg(sk, msg, len);
108 static int ieee802154_sock_bind(struct socket *sock, struct sockaddr *uaddr,
109 int addr_len)
111 struct sock *sk = sock->sk;
113 if (sk->sk_prot->bind)
114 return sk->sk_prot->bind(sk, uaddr, addr_len);
116 return sock_no_bind(sock, uaddr, addr_len);
119 static int ieee802154_sock_connect(struct socket *sock, struct sockaddr *uaddr,
120 int addr_len, int flags)
122 struct sock *sk = sock->sk;
124 if (addr_len < sizeof(uaddr->sa_family))
125 return -EINVAL;
127 if (uaddr->sa_family == AF_UNSPEC)
128 return sk->sk_prot->disconnect(sk, flags);
130 return sk->sk_prot->connect(sk, uaddr, addr_len);
133 static int ieee802154_dev_ioctl(struct sock *sk, struct ifreq __user *arg,
134 unsigned int cmd)
136 struct ifreq ifr;
137 int ret = -ENOIOCTLCMD;
138 struct net_device *dev;
140 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
141 return -EFAULT;
143 ifr.ifr_name[IFNAMSIZ-1] = 0;
145 dev_load(sock_net(sk), ifr.ifr_name);
146 dev = dev_get_by_name(sock_net(sk), ifr.ifr_name);
148 if (!dev)
149 return -ENODEV;
151 if (dev->type == ARPHRD_IEEE802154 && dev->netdev_ops->ndo_do_ioctl)
152 ret = dev->netdev_ops->ndo_do_ioctl(dev, &ifr, cmd);
154 if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq)))
155 ret = -EFAULT;
156 dev_put(dev);
158 return ret;
161 static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd,
162 unsigned long arg)
164 struct sock *sk = sock->sk;
166 switch (cmd) {
167 case SIOCGSTAMP:
168 return sock_get_timestamp(sk, (struct timeval __user *)arg);
169 case SIOCGSTAMPNS:
170 return sock_get_timestampns(sk, (struct timespec __user *)arg);
171 case SIOCGIFADDR:
172 case SIOCSIFADDR:
173 return ieee802154_dev_ioctl(sk, (struct ifreq __user *)arg,
174 cmd);
175 default:
176 if (!sk->sk_prot->ioctl)
177 return -ENOIOCTLCMD;
178 return sk->sk_prot->ioctl(sk, cmd, arg);
182 /* RAW Sockets (802.15.4 created in userspace) */
183 static HLIST_HEAD(raw_head);
184 static DEFINE_RWLOCK(raw_lock);
186 static int raw_hash(struct sock *sk)
188 write_lock_bh(&raw_lock);
189 sk_add_node(sk, &raw_head);
190 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
191 write_unlock_bh(&raw_lock);
193 return 0;
196 static void raw_unhash(struct sock *sk)
198 write_lock_bh(&raw_lock);
199 if (sk_del_node_init(sk))
200 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
201 write_unlock_bh(&raw_lock);
204 static void raw_close(struct sock *sk, long timeout)
206 sk_common_release(sk);
209 static int raw_bind(struct sock *sk, struct sockaddr *_uaddr, int len)
211 struct ieee802154_addr addr;
212 struct sockaddr_ieee802154 *uaddr = (struct sockaddr_ieee802154 *)_uaddr;
213 int err = 0;
214 struct net_device *dev = NULL;
216 if (len < sizeof(*uaddr))
217 return -EINVAL;
219 uaddr = (struct sockaddr_ieee802154 *)_uaddr;
220 if (uaddr->family != AF_IEEE802154)
221 return -EINVAL;
223 lock_sock(sk);
225 ieee802154_addr_from_sa(&addr, &uaddr->addr);
226 dev = ieee802154_get_dev(sock_net(sk), &addr);
227 if (!dev) {
228 err = -ENODEV;
229 goto out;
232 sk->sk_bound_dev_if = dev->ifindex;
233 sk_dst_reset(sk);
235 dev_put(dev);
236 out:
237 release_sock(sk);
239 return err;
242 static int raw_connect(struct sock *sk, struct sockaddr *uaddr,
243 int addr_len)
245 return -ENOTSUPP;
248 static int raw_disconnect(struct sock *sk, int flags)
250 return 0;
253 static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
255 struct net_device *dev;
256 unsigned int mtu;
257 struct sk_buff *skb;
258 int hlen, tlen;
259 int err;
261 if (msg->msg_flags & MSG_OOB) {
262 pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
263 return -EOPNOTSUPP;
266 lock_sock(sk);
267 if (!sk->sk_bound_dev_if)
268 dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
269 else
270 dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if);
271 release_sock(sk);
273 if (!dev) {
274 pr_debug("no dev\n");
275 err = -ENXIO;
276 goto out;
279 mtu = IEEE802154_MTU;
280 pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
282 if (size > mtu) {
283 pr_debug("size = %zu, mtu = %u\n", size, mtu);
284 err = -EMSGSIZE;
285 goto out_dev;
288 hlen = LL_RESERVED_SPACE(dev);
289 tlen = dev->needed_tailroom;
290 skb = sock_alloc_send_skb(sk, hlen + tlen + size,
291 msg->msg_flags & MSG_DONTWAIT, &err);
292 if (!skb)
293 goto out_dev;
295 skb_reserve(skb, hlen);
297 skb_reset_mac_header(skb);
298 skb_reset_network_header(skb);
300 err = memcpy_from_msg(skb_put(skb, size), msg, size);
301 if (err < 0)
302 goto out_skb;
304 skb->dev = dev;
305 skb->protocol = htons(ETH_P_IEEE802154);
307 err = dev_queue_xmit(skb);
308 if (err > 0)
309 err = net_xmit_errno(err);
311 dev_put(dev);
313 return err ?: size;
315 out_skb:
316 kfree_skb(skb);
317 out_dev:
318 dev_put(dev);
319 out:
320 return err;
323 static int raw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
324 int noblock, int flags, int *addr_len)
326 size_t copied = 0;
327 int err = -EOPNOTSUPP;
328 struct sk_buff *skb;
330 skb = skb_recv_datagram(sk, flags, noblock, &err);
331 if (!skb)
332 goto out;
334 copied = skb->len;
335 if (len < copied) {
336 msg->msg_flags |= MSG_TRUNC;
337 copied = len;
340 err = skb_copy_datagram_msg(skb, 0, msg, copied);
341 if (err)
342 goto done;
344 sock_recv_ts_and_drops(msg, sk, skb);
346 if (flags & MSG_TRUNC)
347 copied = skb->len;
348 done:
349 skb_free_datagram(sk, skb);
350 out:
351 if (err)
352 return err;
353 return copied;
356 static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb)
358 skb = skb_share_check(skb, GFP_ATOMIC);
359 if (!skb)
360 return NET_RX_DROP;
362 if (sock_queue_rcv_skb(sk, skb) < 0) {
363 kfree_skb(skb);
364 return NET_RX_DROP;
367 return NET_RX_SUCCESS;
370 static void ieee802154_raw_deliver(struct net_device *dev, struct sk_buff *skb)
372 struct sock *sk;
374 read_lock(&raw_lock);
375 sk_for_each(sk, &raw_head) {
376 bh_lock_sock(sk);
377 if (!sk->sk_bound_dev_if ||
378 sk->sk_bound_dev_if == dev->ifindex) {
379 struct sk_buff *clone;
381 clone = skb_clone(skb, GFP_ATOMIC);
382 if (clone)
383 raw_rcv_skb(sk, clone);
385 bh_unlock_sock(sk);
387 read_unlock(&raw_lock);
390 static int raw_getsockopt(struct sock *sk, int level, int optname,
391 char __user *optval, int __user *optlen)
393 return -EOPNOTSUPP;
396 static int raw_setsockopt(struct sock *sk, int level, int optname,
397 char __user *optval, unsigned int optlen)
399 return -EOPNOTSUPP;
402 static struct proto ieee802154_raw_prot = {
403 .name = "IEEE-802.15.4-RAW",
404 .owner = THIS_MODULE,
405 .obj_size = sizeof(struct sock),
406 .close = raw_close,
407 .bind = raw_bind,
408 .sendmsg = raw_sendmsg,
409 .recvmsg = raw_recvmsg,
410 .hash = raw_hash,
411 .unhash = raw_unhash,
412 .connect = raw_connect,
413 .disconnect = raw_disconnect,
414 .getsockopt = raw_getsockopt,
415 .setsockopt = raw_setsockopt,
418 static const struct proto_ops ieee802154_raw_ops = {
419 .family = PF_IEEE802154,
420 .owner = THIS_MODULE,
421 .release = ieee802154_sock_release,
422 .bind = ieee802154_sock_bind,
423 .connect = ieee802154_sock_connect,
424 .socketpair = sock_no_socketpair,
425 .accept = sock_no_accept,
426 .getname = sock_no_getname,
427 .poll = datagram_poll,
428 .ioctl = ieee802154_sock_ioctl,
429 .listen = sock_no_listen,
430 .shutdown = sock_no_shutdown,
431 .setsockopt = sock_common_setsockopt,
432 .getsockopt = sock_common_getsockopt,
433 .sendmsg = ieee802154_sock_sendmsg,
434 .recvmsg = sock_common_recvmsg,
435 .mmap = sock_no_mmap,
436 .sendpage = sock_no_sendpage,
437 #ifdef CONFIG_COMPAT
438 .compat_setsockopt = compat_sock_common_setsockopt,
439 .compat_getsockopt = compat_sock_common_getsockopt,
440 #endif
443 /* DGRAM Sockets (802.15.4 dataframes) */
444 static HLIST_HEAD(dgram_head);
445 static DEFINE_RWLOCK(dgram_lock);
447 struct dgram_sock {
448 struct sock sk;
450 struct ieee802154_addr src_addr;
451 struct ieee802154_addr dst_addr;
453 unsigned int bound:1;
454 unsigned int connected:1;
455 unsigned int want_ack:1;
456 unsigned int want_lqi:1;
457 unsigned int secen:1;
458 unsigned int secen_override:1;
459 unsigned int seclevel:3;
460 unsigned int seclevel_override:1;
463 static inline struct dgram_sock *dgram_sk(const struct sock *sk)
465 return container_of(sk, struct dgram_sock, sk);
468 static int dgram_hash(struct sock *sk)
470 write_lock_bh(&dgram_lock);
471 sk_add_node(sk, &dgram_head);
472 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
473 write_unlock_bh(&dgram_lock);
475 return 0;
478 static void dgram_unhash(struct sock *sk)
480 write_lock_bh(&dgram_lock);
481 if (sk_del_node_init(sk))
482 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
483 write_unlock_bh(&dgram_lock);
486 static int dgram_init(struct sock *sk)
488 struct dgram_sock *ro = dgram_sk(sk);
490 ro->want_ack = 1;
491 ro->want_lqi = 0;
492 return 0;
495 static void dgram_close(struct sock *sk, long timeout)
497 sk_common_release(sk);
500 static int dgram_bind(struct sock *sk, struct sockaddr *uaddr, int len)
502 struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
503 struct ieee802154_addr haddr;
504 struct dgram_sock *ro = dgram_sk(sk);
505 int err = -EINVAL;
506 struct net_device *dev;
508 lock_sock(sk);
510 ro->bound = 0;
512 if (len < sizeof(*addr))
513 goto out;
515 if (addr->family != AF_IEEE802154)
516 goto out;
518 ieee802154_addr_from_sa(&haddr, &addr->addr);
519 dev = ieee802154_get_dev(sock_net(sk), &haddr);
520 if (!dev) {
521 err = -ENODEV;
522 goto out;
525 if (dev->type != ARPHRD_IEEE802154) {
526 err = -ENODEV;
527 goto out_put;
530 ro->src_addr = haddr;
532 ro->bound = 1;
533 err = 0;
534 out_put:
535 dev_put(dev);
536 out:
537 release_sock(sk);
539 return err;
542 static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg)
544 switch (cmd) {
545 case SIOCOUTQ:
547 int amount = sk_wmem_alloc_get(sk);
549 return put_user(amount, (int __user *)arg);
552 case SIOCINQ:
554 struct sk_buff *skb;
555 unsigned long amount;
557 amount = 0;
558 spin_lock_bh(&sk->sk_receive_queue.lock);
559 skb = skb_peek(&sk->sk_receive_queue);
560 if (skb) {
561 /* We will only return the amount
562 * of this packet since that is all
563 * that will be read.
565 amount = skb->len - ieee802154_hdr_length(skb);
567 spin_unlock_bh(&sk->sk_receive_queue.lock);
568 return put_user(amount, (int __user *)arg);
572 return -ENOIOCTLCMD;
575 /* FIXME: autobind */
576 static int dgram_connect(struct sock *sk, struct sockaddr *uaddr,
577 int len)
579 struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
580 struct dgram_sock *ro = dgram_sk(sk);
581 int err = 0;
583 if (len < sizeof(*addr))
584 return -EINVAL;
586 if (addr->family != AF_IEEE802154)
587 return -EINVAL;
589 lock_sock(sk);
591 if (!ro->bound) {
592 err = -ENETUNREACH;
593 goto out;
596 ieee802154_addr_from_sa(&ro->dst_addr, &addr->addr);
597 ro->connected = 1;
599 out:
600 release_sock(sk);
601 return err;
604 static int dgram_disconnect(struct sock *sk, int flags)
606 struct dgram_sock *ro = dgram_sk(sk);
608 lock_sock(sk);
609 ro->connected = 0;
610 release_sock(sk);
612 return 0;
615 static int dgram_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
617 struct net_device *dev;
618 unsigned int mtu;
619 struct sk_buff *skb;
620 struct ieee802154_mac_cb *cb;
621 struct dgram_sock *ro = dgram_sk(sk);
622 struct ieee802154_addr dst_addr;
623 int hlen, tlen;
624 int err;
626 if (msg->msg_flags & MSG_OOB) {
627 pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
628 return -EOPNOTSUPP;
631 if (!ro->connected && !msg->msg_name)
632 return -EDESTADDRREQ;
633 else if (ro->connected && msg->msg_name)
634 return -EISCONN;
636 if (!ro->bound)
637 dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
638 else
639 dev = ieee802154_get_dev(sock_net(sk), &ro->src_addr);
641 if (!dev) {
642 pr_debug("no dev\n");
643 err = -ENXIO;
644 goto out;
646 mtu = IEEE802154_MTU;
647 pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
649 if (size > mtu) {
650 pr_debug("size = %zu, mtu = %u\n", size, mtu);
651 err = -EMSGSIZE;
652 goto out_dev;
655 hlen = LL_RESERVED_SPACE(dev);
656 tlen = dev->needed_tailroom;
657 skb = sock_alloc_send_skb(sk, hlen + tlen + size,
658 msg->msg_flags & MSG_DONTWAIT,
659 &err);
660 if (!skb)
661 goto out_dev;
663 skb_reserve(skb, hlen);
665 skb_reset_network_header(skb);
667 cb = mac_cb_init(skb);
668 cb->type = IEEE802154_FC_TYPE_DATA;
669 cb->ackreq = ro->want_ack;
671 if (msg->msg_name) {
672 DECLARE_SOCKADDR(struct sockaddr_ieee802154*,
673 daddr, msg->msg_name);
675 ieee802154_addr_from_sa(&dst_addr, &daddr->addr);
676 } else {
677 dst_addr = ro->dst_addr;
680 cb->secen = ro->secen;
681 cb->secen_override = ro->secen_override;
682 cb->seclevel = ro->seclevel;
683 cb->seclevel_override = ro->seclevel_override;
685 err = wpan_dev_hard_header(skb, dev, &dst_addr,
686 ro->bound ? &ro->src_addr : NULL, size);
687 if (err < 0)
688 goto out_skb;
690 err = memcpy_from_msg(skb_put(skb, size), msg, size);
691 if (err < 0)
692 goto out_skb;
694 skb->dev = dev;
695 skb->protocol = htons(ETH_P_IEEE802154);
697 err = dev_queue_xmit(skb);
698 if (err > 0)
699 err = net_xmit_errno(err);
701 dev_put(dev);
703 return err ?: size;
705 out_skb:
706 kfree_skb(skb);
707 out_dev:
708 dev_put(dev);
709 out:
710 return err;
713 static int dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
714 int noblock, int flags, int *addr_len)
716 size_t copied = 0;
717 int err = -EOPNOTSUPP;
718 struct sk_buff *skb;
719 struct dgram_sock *ro = dgram_sk(sk);
720 DECLARE_SOCKADDR(struct sockaddr_ieee802154 *, saddr, msg->msg_name);
722 skb = skb_recv_datagram(sk, flags, noblock, &err);
723 if (!skb)
724 goto out;
726 copied = skb->len;
727 if (len < copied) {
728 msg->msg_flags |= MSG_TRUNC;
729 copied = len;
732 /* FIXME: skip headers if necessary ?! */
733 err = skb_copy_datagram_msg(skb, 0, msg, copied);
734 if (err)
735 goto done;
737 sock_recv_ts_and_drops(msg, sk, skb);
739 if (saddr) {
740 /* Clear the implicit padding in struct sockaddr_ieee802154
741 * (16 bits between 'family' and 'addr') and in struct
742 * ieee802154_addr_sa (16 bits at the end of the structure).
744 memset(saddr, 0, sizeof(*saddr));
746 saddr->family = AF_IEEE802154;
747 ieee802154_addr_to_sa(&saddr->addr, &mac_cb(skb)->source);
748 *addr_len = sizeof(*saddr);
751 if (ro->want_lqi) {
752 err = put_cmsg(msg, SOL_IEEE802154, WPAN_WANTLQI,
753 sizeof(uint8_t), &(mac_cb(skb)->lqi));
754 if (err)
755 goto done;
758 if (flags & MSG_TRUNC)
759 copied = skb->len;
760 done:
761 skb_free_datagram(sk, skb);
762 out:
763 if (err)
764 return err;
765 return copied;
768 static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb)
770 skb = skb_share_check(skb, GFP_ATOMIC);
771 if (!skb)
772 return NET_RX_DROP;
774 if (sock_queue_rcv_skb(sk, skb) < 0) {
775 kfree_skb(skb);
776 return NET_RX_DROP;
779 return NET_RX_SUCCESS;
782 static inline bool
783 ieee802154_match_sock(__le64 hw_addr, __le16 pan_id, __le16 short_addr,
784 struct dgram_sock *ro)
786 if (!ro->bound)
787 return true;
789 if (ro->src_addr.mode == IEEE802154_ADDR_LONG &&
790 hw_addr == ro->src_addr.extended_addr)
791 return true;
793 if (ro->src_addr.mode == IEEE802154_ADDR_SHORT &&
794 pan_id == ro->src_addr.pan_id &&
795 short_addr == ro->src_addr.short_addr)
796 return true;
798 return false;
801 static int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
803 struct sock *sk, *prev = NULL;
804 int ret = NET_RX_SUCCESS;
805 __le16 pan_id, short_addr;
806 __le64 hw_addr;
808 /* Data frame processing */
809 BUG_ON(dev->type != ARPHRD_IEEE802154);
811 pan_id = dev->ieee802154_ptr->pan_id;
812 short_addr = dev->ieee802154_ptr->short_addr;
813 hw_addr = dev->ieee802154_ptr->extended_addr;
815 read_lock(&dgram_lock);
816 sk_for_each(sk, &dgram_head) {
817 if (ieee802154_match_sock(hw_addr, pan_id, short_addr,
818 dgram_sk(sk))) {
819 if (prev) {
820 struct sk_buff *clone;
822 clone = skb_clone(skb, GFP_ATOMIC);
823 if (clone)
824 dgram_rcv_skb(prev, clone);
827 prev = sk;
831 if (prev) {
832 dgram_rcv_skb(prev, skb);
833 } else {
834 kfree_skb(skb);
835 ret = NET_RX_DROP;
837 read_unlock(&dgram_lock);
839 return ret;
842 static int dgram_getsockopt(struct sock *sk, int level, int optname,
843 char __user *optval, int __user *optlen)
845 struct dgram_sock *ro = dgram_sk(sk);
847 int val, len;
849 if (level != SOL_IEEE802154)
850 return -EOPNOTSUPP;
852 if (get_user(len, optlen))
853 return -EFAULT;
855 len = min_t(unsigned int, len, sizeof(int));
857 switch (optname) {
858 case WPAN_WANTACK:
859 val = ro->want_ack;
860 break;
861 case WPAN_WANTLQI:
862 val = ro->want_lqi;
863 break;
864 case WPAN_SECURITY:
865 if (!ro->secen_override)
866 val = WPAN_SECURITY_DEFAULT;
867 else if (ro->secen)
868 val = WPAN_SECURITY_ON;
869 else
870 val = WPAN_SECURITY_OFF;
871 break;
872 case WPAN_SECURITY_LEVEL:
873 if (!ro->seclevel_override)
874 val = WPAN_SECURITY_LEVEL_DEFAULT;
875 else
876 val = ro->seclevel;
877 break;
878 default:
879 return -ENOPROTOOPT;
882 if (put_user(len, optlen))
883 return -EFAULT;
884 if (copy_to_user(optval, &val, len))
885 return -EFAULT;
886 return 0;
889 static int dgram_setsockopt(struct sock *sk, int level, int optname,
890 char __user *optval, unsigned int optlen)
892 struct dgram_sock *ro = dgram_sk(sk);
893 struct net *net = sock_net(sk);
894 int val;
895 int err = 0;
897 if (optlen < sizeof(int))
898 return -EINVAL;
900 if (get_user(val, (int __user *)optval))
901 return -EFAULT;
903 lock_sock(sk);
905 switch (optname) {
906 case WPAN_WANTACK:
907 ro->want_ack = !!val;
908 break;
909 case WPAN_WANTLQI:
910 ro->want_lqi = !!val;
911 break;
912 case WPAN_SECURITY:
913 if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
914 !ns_capable(net->user_ns, CAP_NET_RAW)) {
915 err = -EPERM;
916 break;
919 switch (val) {
920 case WPAN_SECURITY_DEFAULT:
921 ro->secen_override = 0;
922 break;
923 case WPAN_SECURITY_ON:
924 ro->secen_override = 1;
925 ro->secen = 1;
926 break;
927 case WPAN_SECURITY_OFF:
928 ro->secen_override = 1;
929 ro->secen = 0;
930 break;
931 default:
932 err = -EINVAL;
933 break;
935 break;
936 case WPAN_SECURITY_LEVEL:
937 if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
938 !ns_capable(net->user_ns, CAP_NET_RAW)) {
939 err = -EPERM;
940 break;
943 if (val < WPAN_SECURITY_LEVEL_DEFAULT ||
944 val > IEEE802154_SCF_SECLEVEL_ENC_MIC128) {
945 err = -EINVAL;
946 } else if (val == WPAN_SECURITY_LEVEL_DEFAULT) {
947 ro->seclevel_override = 0;
948 } else {
949 ro->seclevel_override = 1;
950 ro->seclevel = val;
952 break;
953 default:
954 err = -ENOPROTOOPT;
955 break;
958 release_sock(sk);
959 return err;
962 static struct proto ieee802154_dgram_prot = {
963 .name = "IEEE-802.15.4-MAC",
964 .owner = THIS_MODULE,
965 .obj_size = sizeof(struct dgram_sock),
966 .init = dgram_init,
967 .close = dgram_close,
968 .bind = dgram_bind,
969 .sendmsg = dgram_sendmsg,
970 .recvmsg = dgram_recvmsg,
971 .hash = dgram_hash,
972 .unhash = dgram_unhash,
973 .connect = dgram_connect,
974 .disconnect = dgram_disconnect,
975 .ioctl = dgram_ioctl,
976 .getsockopt = dgram_getsockopt,
977 .setsockopt = dgram_setsockopt,
980 static const struct proto_ops ieee802154_dgram_ops = {
981 .family = PF_IEEE802154,
982 .owner = THIS_MODULE,
983 .release = ieee802154_sock_release,
984 .bind = ieee802154_sock_bind,
985 .connect = ieee802154_sock_connect,
986 .socketpair = sock_no_socketpair,
987 .accept = sock_no_accept,
988 .getname = sock_no_getname,
989 .poll = datagram_poll,
990 .ioctl = ieee802154_sock_ioctl,
991 .listen = sock_no_listen,
992 .shutdown = sock_no_shutdown,
993 .setsockopt = sock_common_setsockopt,
994 .getsockopt = sock_common_getsockopt,
995 .sendmsg = ieee802154_sock_sendmsg,
996 .recvmsg = sock_common_recvmsg,
997 .mmap = sock_no_mmap,
998 .sendpage = sock_no_sendpage,
999 #ifdef CONFIG_COMPAT
1000 .compat_setsockopt = compat_sock_common_setsockopt,
1001 .compat_getsockopt = compat_sock_common_getsockopt,
1002 #endif
1005 /* Create a socket. Initialise the socket, blank the addresses
1006 * set the state.
1008 static int ieee802154_create(struct net *net, struct socket *sock,
1009 int protocol, int kern)
1011 struct sock *sk;
1012 int rc;
1013 struct proto *proto;
1014 const struct proto_ops *ops;
1016 if (!net_eq(net, &init_net))
1017 return -EAFNOSUPPORT;
1019 switch (sock->type) {
1020 case SOCK_RAW:
1021 proto = &ieee802154_raw_prot;
1022 ops = &ieee802154_raw_ops;
1023 break;
1024 case SOCK_DGRAM:
1025 proto = &ieee802154_dgram_prot;
1026 ops = &ieee802154_dgram_ops;
1027 break;
1028 default:
1029 rc = -ESOCKTNOSUPPORT;
1030 goto out;
1033 rc = -ENOMEM;
1034 sk = sk_alloc(net, PF_IEEE802154, GFP_KERNEL, proto, kern);
1035 if (!sk)
1036 goto out;
1037 rc = 0;
1039 sock->ops = ops;
1041 sock_init_data(sock, sk);
1042 /* FIXME: sk->sk_destruct */
1043 sk->sk_family = PF_IEEE802154;
1045 /* Checksums on by default */
1046 sock_set_flag(sk, SOCK_ZAPPED);
1048 if (sk->sk_prot->hash) {
1049 rc = sk->sk_prot->hash(sk);
1050 if (rc) {
1051 sk_common_release(sk);
1052 goto out;
1056 if (sk->sk_prot->init) {
1057 rc = sk->sk_prot->init(sk);
1058 if (rc)
1059 sk_common_release(sk);
1061 out:
1062 return rc;
1065 static const struct net_proto_family ieee802154_family_ops = {
1066 .family = PF_IEEE802154,
1067 .create = ieee802154_create,
1068 .owner = THIS_MODULE,
1071 static int ieee802154_rcv(struct sk_buff *skb, struct net_device *dev,
1072 struct packet_type *pt, struct net_device *orig_dev)
1074 if (!netif_running(dev))
1075 goto drop;
1076 pr_debug("got frame, type %d, dev %p\n", dev->type, dev);
1077 #ifdef DEBUG
1078 print_hex_dump_bytes("ieee802154_rcv ",
1079 DUMP_PREFIX_NONE, skb->data, skb->len);
1080 #endif
1082 if (!net_eq(dev_net(dev), &init_net))
1083 goto drop;
1085 ieee802154_raw_deliver(dev, skb);
1087 if (dev->type != ARPHRD_IEEE802154)
1088 goto drop;
1090 if (skb->pkt_type != PACKET_OTHERHOST)
1091 return ieee802154_dgram_deliver(dev, skb);
1093 drop:
1094 kfree_skb(skb);
1095 return NET_RX_DROP;
1098 static struct packet_type ieee802154_packet_type = {
1099 .type = htons(ETH_P_IEEE802154),
1100 .func = ieee802154_rcv,
1103 static int __init af_ieee802154_init(void)
1105 int rc = -EINVAL;
1107 rc = proto_register(&ieee802154_raw_prot, 1);
1108 if (rc)
1109 goto out;
1111 rc = proto_register(&ieee802154_dgram_prot, 1);
1112 if (rc)
1113 goto err_dgram;
1115 /* Tell SOCKET that we are alive */
1116 rc = sock_register(&ieee802154_family_ops);
1117 if (rc)
1118 goto err_sock;
1119 dev_add_pack(&ieee802154_packet_type);
1121 rc = 0;
1122 goto out;
1124 err_sock:
1125 proto_unregister(&ieee802154_dgram_prot);
1126 err_dgram:
1127 proto_unregister(&ieee802154_raw_prot);
1128 out:
1129 return rc;
1132 static void __exit af_ieee802154_remove(void)
1134 dev_remove_pack(&ieee802154_packet_type);
1135 sock_unregister(PF_IEEE802154);
1136 proto_unregister(&ieee802154_dgram_prot);
1137 proto_unregister(&ieee802154_raw_prot);
1140 module_init(af_ieee802154_init);
1141 module_exit(af_ieee802154_remove);
1143 MODULE_LICENSE("GPL");
1144 MODULE_ALIAS_NETPROTO(PF_IEEE802154);