xref: /openbmc/linux/net/ieee802154/socket.c (revision 260ea95c)
1 /*
2  * IEEE802154.4 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  * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
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>	/* For TIOCOUTQ/INQ */
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 /* Utility function for families */
38 static struct net_device*
39 ieee802154_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 
88 static 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 
99 static int ieee802154_sock_sendmsg(struct socket *sock, struct msghdr *msg,
100 				   size_t len)
101 {
102 	struct sock *sk = sock->sk;
103 
104 	return sk->sk_prot->sendmsg(sk, msg, len);
105 }
106 
107 static 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 
118 static 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 
132 static 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 
160 static 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 /* RAW Sockets (802.15.4 created in userspace) */
182 static HLIST_HEAD(raw_head);
183 static DEFINE_RWLOCK(raw_lock);
184 
185 static int 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 	return 0;
193 }
194 
195 static 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 
203 static void raw_close(struct sock *sk, long timeout)
204 {
205 	sk_common_release(sk);
206 }
207 
208 static 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 	sk->sk_bound_dev_if = dev->ifindex;
232 	sk_dst_reset(sk);
233 
234 	dev_put(dev);
235 out:
236 	release_sock(sk);
237 
238 	return err;
239 }
240 
241 static int raw_connect(struct sock *sk, struct sockaddr *uaddr,
242 		       int addr_len)
243 {
244 	return -ENOTSUPP;
245 }
246 
247 static int raw_disconnect(struct sock *sk, int flags)
248 {
249 	return 0;
250 }
251 
252 static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
253 {
254 	struct net_device *dev;
255 	unsigned int mtu;
256 	struct sk_buff *skb;
257 	int hlen, tlen;
258 	int err;
259 
260 	if (msg->msg_flags & MSG_OOB) {
261 		pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
262 		return -EOPNOTSUPP;
263 	}
264 
265 	lock_sock(sk);
266 	if (!sk->sk_bound_dev_if)
267 		dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
268 	else
269 		dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if);
270 	release_sock(sk);
271 
272 	if (!dev) {
273 		pr_debug("no dev\n");
274 		err = -ENXIO;
275 		goto out;
276 	}
277 
278 	mtu = IEEE802154_MTU;
279 	pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
280 
281 	if (size > mtu) {
282 		pr_debug("size = %zu, mtu = %u\n", size, mtu);
283 		err = -EMSGSIZE;
284 		goto out_dev;
285 	}
286 
287 	hlen = LL_RESERVED_SPACE(dev);
288 	tlen = dev->needed_tailroom;
289 	skb = sock_alloc_send_skb(sk, hlen + tlen + size,
290 				  msg->msg_flags & MSG_DONTWAIT, &err);
291 	if (!skb)
292 		goto out_dev;
293 
294 	skb_reserve(skb, hlen);
295 
296 	skb_reset_mac_header(skb);
297 	skb_reset_network_header(skb);
298 
299 	err = memcpy_from_msg(skb_put(skb, size), msg, size);
300 	if (err < 0)
301 		goto out_skb;
302 
303 	skb->dev = dev;
304 	skb->protocol = htons(ETH_P_IEEE802154);
305 
306 	err = dev_queue_xmit(skb);
307 	if (err > 0)
308 		err = net_xmit_errno(err);
309 
310 	dev_put(dev);
311 
312 	return err ?: size;
313 
314 out_skb:
315 	kfree_skb(skb);
316 out_dev:
317 	dev_put(dev);
318 out:
319 	return err;
320 }
321 
322 static int raw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
323 		       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;
347 done:
348 	skb_free_datagram(sk, skb);
349 out:
350 	if (err)
351 		return err;
352 	return copied;
353 }
354 
355 static 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 
369 static 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 
389 static int raw_getsockopt(struct sock *sk, int level, int optname,
390 			  char __user *optval, int __user *optlen)
391 {
392 	return -EOPNOTSUPP;
393 }
394 
395 static int raw_setsockopt(struct sock *sk, int level, int optname,
396 			  char __user *optval, unsigned int optlen)
397 {
398 	return -EOPNOTSUPP;
399 }
400 
401 static 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 
417 static 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 /* DGRAM Sockets (802.15.4 dataframes) */
443 static HLIST_HEAD(dgram_head);
444 static DEFINE_RWLOCK(dgram_lock);
445 
446 struct 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 
461 static inline struct dgram_sock *dgram_sk(const struct sock *sk)
462 {
463 	return container_of(sk, struct dgram_sock, sk);
464 }
465 
466 static int 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 	return 0;
474 }
475 
476 static void dgram_unhash(struct sock *sk)
477 {
478 	write_lock_bh(&dgram_lock);
479 	if (sk_del_node_init(sk))
480 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
481 	write_unlock_bh(&dgram_lock);
482 }
483 
484 static int dgram_init(struct sock *sk)
485 {
486 	struct dgram_sock *ro = dgram_sk(sk);
487 
488 	ro->want_ack = 1;
489 	return 0;
490 }
491 
492 static void dgram_close(struct sock *sk, long timeout)
493 {
494 	sk_common_release(sk);
495 }
496 
497 static int dgram_bind(struct sock *sk, struct sockaddr *uaddr, int len)
498 {
499 	struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
500 	struct ieee802154_addr haddr;
501 	struct dgram_sock *ro = dgram_sk(sk);
502 	int err = -EINVAL;
503 	struct net_device *dev;
504 
505 	lock_sock(sk);
506 
507 	ro->bound = 0;
508 
509 	if (len < sizeof(*addr))
510 		goto out;
511 
512 	if (addr->family != AF_IEEE802154)
513 		goto out;
514 
515 	ieee802154_addr_from_sa(&haddr, &addr->addr);
516 	dev = ieee802154_get_dev(sock_net(sk), &haddr);
517 	if (!dev) {
518 		err = -ENODEV;
519 		goto out;
520 	}
521 
522 	if (dev->type != ARPHRD_IEEE802154) {
523 		err = -ENODEV;
524 		goto out_put;
525 	}
526 
527 	ro->src_addr = haddr;
528 
529 	ro->bound = 1;
530 	err = 0;
531 out_put:
532 	dev_put(dev);
533 out:
534 	release_sock(sk);
535 
536 	return err;
537 }
538 
539 static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg)
540 {
541 	switch (cmd) {
542 	case SIOCOUTQ:
543 	{
544 		int amount = sk_wmem_alloc_get(sk);
545 
546 		return put_user(amount, (int __user *)arg);
547 	}
548 
549 	case SIOCINQ:
550 	{
551 		struct sk_buff *skb;
552 		unsigned long amount;
553 
554 		amount = 0;
555 		spin_lock_bh(&sk->sk_receive_queue.lock);
556 		skb = skb_peek(&sk->sk_receive_queue);
557 		if (skb) {
558 			/* We will only return the amount
559 			 * of this packet since that is all
560 			 * that will be read.
561 			 */
562 			amount = skb->len - ieee802154_hdr_length(skb);
563 		}
564 		spin_unlock_bh(&sk->sk_receive_queue.lock);
565 		return put_user(amount, (int __user *)arg);
566 	}
567 	}
568 
569 	return -ENOIOCTLCMD;
570 }
571 
572 /* FIXME: autobind */
573 static int dgram_connect(struct sock *sk, struct sockaddr *uaddr,
574 			 int len)
575 {
576 	struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
577 	struct dgram_sock *ro = dgram_sk(sk);
578 	int err = 0;
579 
580 	if (len < sizeof(*addr))
581 		return -EINVAL;
582 
583 	if (addr->family != AF_IEEE802154)
584 		return -EINVAL;
585 
586 	lock_sock(sk);
587 
588 	if (!ro->bound) {
589 		err = -ENETUNREACH;
590 		goto out;
591 	}
592 
593 	ieee802154_addr_from_sa(&ro->dst_addr, &addr->addr);
594 	ro->connected = 1;
595 
596 out:
597 	release_sock(sk);
598 	return err;
599 }
600 
601 static int dgram_disconnect(struct sock *sk, int flags)
602 {
603 	struct dgram_sock *ro = dgram_sk(sk);
604 
605 	lock_sock(sk);
606 	ro->connected = 0;
607 	release_sock(sk);
608 
609 	return 0;
610 }
611 
612 static int dgram_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
613 {
614 	struct net_device *dev;
615 	unsigned int mtu;
616 	struct sk_buff *skb;
617 	struct ieee802154_mac_cb *cb;
618 	struct dgram_sock *ro = dgram_sk(sk);
619 	struct ieee802154_addr dst_addr;
620 	int hlen, tlen;
621 	int err;
622 
623 	if (msg->msg_flags & MSG_OOB) {
624 		pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
625 		return -EOPNOTSUPP;
626 	}
627 
628 	if (!ro->connected && !msg->msg_name)
629 		return -EDESTADDRREQ;
630 	else if (ro->connected && msg->msg_name)
631 		return -EISCONN;
632 
633 	if (!ro->bound)
634 		dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
635 	else
636 		dev = ieee802154_get_dev(sock_net(sk), &ro->src_addr);
637 
638 	if (!dev) {
639 		pr_debug("no dev\n");
640 		err = -ENXIO;
641 		goto out;
642 	}
643 	mtu = IEEE802154_MTU;
644 	pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
645 
646 	if (size > mtu) {
647 		pr_debug("size = %zu, mtu = %u\n", size, mtu);
648 		err = -EMSGSIZE;
649 		goto out_dev;
650 	}
651 
652 	hlen = LL_RESERVED_SPACE(dev);
653 	tlen = dev->needed_tailroom;
654 	skb = sock_alloc_send_skb(sk, hlen + tlen + size,
655 				  msg->msg_flags & MSG_DONTWAIT,
656 				  &err);
657 	if (!skb)
658 		goto out_dev;
659 
660 	skb_reserve(skb, hlen);
661 
662 	skb_reset_network_header(skb);
663 
664 	cb = mac_cb_init(skb);
665 	cb->type = IEEE802154_FC_TYPE_DATA;
666 	cb->ackreq = ro->want_ack;
667 
668 	if (msg->msg_name) {
669 		DECLARE_SOCKADDR(struct sockaddr_ieee802154*,
670 				 daddr, msg->msg_name);
671 
672 		ieee802154_addr_from_sa(&dst_addr, &daddr->addr);
673 	} else {
674 		dst_addr = ro->dst_addr;
675 	}
676 
677 	cb->secen = ro->secen;
678 	cb->secen_override = ro->secen_override;
679 	cb->seclevel = ro->seclevel;
680 	cb->seclevel_override = ro->seclevel_override;
681 
682 	err = wpan_dev_hard_header(skb, dev, &dst_addr,
683 				   ro->bound ? &ro->src_addr : NULL, size);
684 	if (err < 0)
685 		goto out_skb;
686 
687 	err = memcpy_from_msg(skb_put(skb, size), msg, size);
688 	if (err < 0)
689 		goto out_skb;
690 
691 	skb->dev = dev;
692 	skb->protocol = htons(ETH_P_IEEE802154);
693 
694 	err = dev_queue_xmit(skb);
695 	if (err > 0)
696 		err = net_xmit_errno(err);
697 
698 	dev_put(dev);
699 
700 	return err ?: size;
701 
702 out_skb:
703 	kfree_skb(skb);
704 out_dev:
705 	dev_put(dev);
706 out:
707 	return err;
708 }
709 
710 static int dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
711 			 int noblock, int flags, int *addr_len)
712 {
713 	size_t copied = 0;
714 	int err = -EOPNOTSUPP;
715 	struct sk_buff *skb;
716 	DECLARE_SOCKADDR(struct sockaddr_ieee802154 *, saddr, msg->msg_name);
717 
718 	skb = skb_recv_datagram(sk, flags, noblock, &err);
719 	if (!skb)
720 		goto out;
721 
722 	copied = skb->len;
723 	if (len < copied) {
724 		msg->msg_flags |= MSG_TRUNC;
725 		copied = len;
726 	}
727 
728 	/* FIXME: skip headers if necessary ?! */
729 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
730 	if (err)
731 		goto done;
732 
733 	sock_recv_ts_and_drops(msg, sk, skb);
734 
735 	if (saddr) {
736 		/* Clear the implicit padding in struct sockaddr_ieee802154
737 		 * (16 bits between 'family' and 'addr') and in struct
738 		 * ieee802154_addr_sa (16 bits at the end of the structure).
739 		 */
740 		memset(saddr, 0, sizeof(*saddr));
741 
742 		saddr->family = AF_IEEE802154;
743 		ieee802154_addr_to_sa(&saddr->addr, &mac_cb(skb)->source);
744 		*addr_len = sizeof(*saddr);
745 	}
746 
747 	if (flags & MSG_TRUNC)
748 		copied = skb->len;
749 done:
750 	skb_free_datagram(sk, skb);
751 out:
752 	if (err)
753 		return err;
754 	return copied;
755 }
756 
757 static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb)
758 {
759 	skb = skb_share_check(skb, GFP_ATOMIC);
760 	if (!skb)
761 		return NET_RX_DROP;
762 
763 	if (sock_queue_rcv_skb(sk, skb) < 0) {
764 		kfree_skb(skb);
765 		return NET_RX_DROP;
766 	}
767 
768 	return NET_RX_SUCCESS;
769 }
770 
771 static inline bool
772 ieee802154_match_sock(__le64 hw_addr, __le16 pan_id, __le16 short_addr,
773 		      struct dgram_sock *ro)
774 {
775 	if (!ro->bound)
776 		return true;
777 
778 	if (ro->src_addr.mode == IEEE802154_ADDR_LONG &&
779 	    hw_addr == ro->src_addr.extended_addr)
780 		return true;
781 
782 	if (ro->src_addr.mode == IEEE802154_ADDR_SHORT &&
783 	    pan_id == ro->src_addr.pan_id &&
784 	    short_addr == ro->src_addr.short_addr)
785 		return true;
786 
787 	return false;
788 }
789 
790 static int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
791 {
792 	struct sock *sk, *prev = NULL;
793 	int ret = NET_RX_SUCCESS;
794 	__le16 pan_id, short_addr;
795 	__le64 hw_addr;
796 
797 	/* Data frame processing */
798 	BUG_ON(dev->type != ARPHRD_IEEE802154);
799 
800 	pan_id = dev->ieee802154_ptr->pan_id;
801 	short_addr = dev->ieee802154_ptr->short_addr;
802 	hw_addr = dev->ieee802154_ptr->extended_addr;
803 
804 	read_lock(&dgram_lock);
805 	sk_for_each(sk, &dgram_head) {
806 		if (ieee802154_match_sock(hw_addr, pan_id, short_addr,
807 					  dgram_sk(sk))) {
808 			if (prev) {
809 				struct sk_buff *clone;
810 
811 				clone = skb_clone(skb, GFP_ATOMIC);
812 				if (clone)
813 					dgram_rcv_skb(prev, clone);
814 			}
815 
816 			prev = sk;
817 		}
818 	}
819 
820 	if (prev) {
821 		dgram_rcv_skb(prev, skb);
822 	} else {
823 		kfree_skb(skb);
824 		ret = NET_RX_DROP;
825 	}
826 	read_unlock(&dgram_lock);
827 
828 	return ret;
829 }
830 
831 static int dgram_getsockopt(struct sock *sk, int level, int optname,
832 			    char __user *optval, int __user *optlen)
833 {
834 	struct dgram_sock *ro = dgram_sk(sk);
835 
836 	int val, len;
837 
838 	if (level != SOL_IEEE802154)
839 		return -EOPNOTSUPP;
840 
841 	if (get_user(len, optlen))
842 		return -EFAULT;
843 
844 	len = min_t(unsigned int, len, sizeof(int));
845 
846 	switch (optname) {
847 	case WPAN_WANTACK:
848 		val = ro->want_ack;
849 		break;
850 	case WPAN_SECURITY:
851 		if (!ro->secen_override)
852 			val = WPAN_SECURITY_DEFAULT;
853 		else if (ro->secen)
854 			val = WPAN_SECURITY_ON;
855 		else
856 			val = WPAN_SECURITY_OFF;
857 		break;
858 	case WPAN_SECURITY_LEVEL:
859 		if (!ro->seclevel_override)
860 			val = WPAN_SECURITY_LEVEL_DEFAULT;
861 		else
862 			val = ro->seclevel;
863 		break;
864 	default:
865 		return -ENOPROTOOPT;
866 	}
867 
868 	if (put_user(len, optlen))
869 		return -EFAULT;
870 	if (copy_to_user(optval, &val, len))
871 		return -EFAULT;
872 	return 0;
873 }
874 
875 static int dgram_setsockopt(struct sock *sk, int level, int optname,
876 			    char __user *optval, unsigned int optlen)
877 {
878 	struct dgram_sock *ro = dgram_sk(sk);
879 	struct net *net = sock_net(sk);
880 	int val;
881 	int err = 0;
882 
883 	if (optlen < sizeof(int))
884 		return -EINVAL;
885 
886 	if (get_user(val, (int __user *)optval))
887 		return -EFAULT;
888 
889 	lock_sock(sk);
890 
891 	switch (optname) {
892 	case WPAN_WANTACK:
893 		ro->want_ack = !!val;
894 		break;
895 	case WPAN_SECURITY:
896 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
897 		    !ns_capable(net->user_ns, CAP_NET_RAW)) {
898 			err = -EPERM;
899 			break;
900 		}
901 
902 		switch (val) {
903 		case WPAN_SECURITY_DEFAULT:
904 			ro->secen_override = 0;
905 			break;
906 		case WPAN_SECURITY_ON:
907 			ro->secen_override = 1;
908 			ro->secen = 1;
909 			break;
910 		case WPAN_SECURITY_OFF:
911 			ro->secen_override = 1;
912 			ro->secen = 0;
913 			break;
914 		default:
915 			err = -EINVAL;
916 			break;
917 		}
918 		break;
919 	case WPAN_SECURITY_LEVEL:
920 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
921 		    !ns_capable(net->user_ns, CAP_NET_RAW)) {
922 			err = -EPERM;
923 			break;
924 		}
925 
926 		if (val < WPAN_SECURITY_LEVEL_DEFAULT ||
927 		    val > IEEE802154_SCF_SECLEVEL_ENC_MIC128) {
928 			err = -EINVAL;
929 		} else if (val == WPAN_SECURITY_LEVEL_DEFAULT) {
930 			ro->seclevel_override = 0;
931 		} else {
932 			ro->seclevel_override = 1;
933 			ro->seclevel = val;
934 		}
935 		break;
936 	default:
937 		err = -ENOPROTOOPT;
938 		break;
939 	}
940 
941 	release_sock(sk);
942 	return err;
943 }
944 
945 static struct proto ieee802154_dgram_prot = {
946 	.name		= "IEEE-802.15.4-MAC",
947 	.owner		= THIS_MODULE,
948 	.obj_size	= sizeof(struct dgram_sock),
949 	.init		= dgram_init,
950 	.close		= dgram_close,
951 	.bind		= dgram_bind,
952 	.sendmsg	= dgram_sendmsg,
953 	.recvmsg	= dgram_recvmsg,
954 	.hash		= dgram_hash,
955 	.unhash		= dgram_unhash,
956 	.connect	= dgram_connect,
957 	.disconnect	= dgram_disconnect,
958 	.ioctl		= dgram_ioctl,
959 	.getsockopt	= dgram_getsockopt,
960 	.setsockopt	= dgram_setsockopt,
961 };
962 
963 static const struct proto_ops ieee802154_dgram_ops = {
964 	.family		   = PF_IEEE802154,
965 	.owner		   = THIS_MODULE,
966 	.release	   = ieee802154_sock_release,
967 	.bind		   = ieee802154_sock_bind,
968 	.connect	   = ieee802154_sock_connect,
969 	.socketpair	   = sock_no_socketpair,
970 	.accept		   = sock_no_accept,
971 	.getname	   = sock_no_getname,
972 	.poll		   = datagram_poll,
973 	.ioctl		   = ieee802154_sock_ioctl,
974 	.listen		   = sock_no_listen,
975 	.shutdown	   = sock_no_shutdown,
976 	.setsockopt	   = sock_common_setsockopt,
977 	.getsockopt	   = sock_common_getsockopt,
978 	.sendmsg	   = ieee802154_sock_sendmsg,
979 	.recvmsg	   = sock_common_recvmsg,
980 	.mmap		   = sock_no_mmap,
981 	.sendpage	   = sock_no_sendpage,
982 #ifdef CONFIG_COMPAT
983 	.compat_setsockopt = compat_sock_common_setsockopt,
984 	.compat_getsockopt = compat_sock_common_getsockopt,
985 #endif
986 };
987 
988 /* Create a socket. Initialise the socket, blank the addresses
989  * set the state.
990  */
991 static int ieee802154_create(struct net *net, struct socket *sock,
992 			     int protocol, int kern)
993 {
994 	struct sock *sk;
995 	int rc;
996 	struct proto *proto;
997 	const struct proto_ops *ops;
998 
999 	if (!net_eq(net, &init_net))
1000 		return -EAFNOSUPPORT;
1001 
1002 	switch (sock->type) {
1003 	case SOCK_RAW:
1004 		proto = &ieee802154_raw_prot;
1005 		ops = &ieee802154_raw_ops;
1006 		break;
1007 	case SOCK_DGRAM:
1008 		proto = &ieee802154_dgram_prot;
1009 		ops = &ieee802154_dgram_ops;
1010 		break;
1011 	default:
1012 		rc = -ESOCKTNOSUPPORT;
1013 		goto out;
1014 	}
1015 
1016 	rc = -ENOMEM;
1017 	sk = sk_alloc(net, PF_IEEE802154, GFP_KERNEL, proto, kern);
1018 	if (!sk)
1019 		goto out;
1020 	rc = 0;
1021 
1022 	sock->ops = ops;
1023 
1024 	sock_init_data(sock, sk);
1025 	/* FIXME: sk->sk_destruct */
1026 	sk->sk_family = PF_IEEE802154;
1027 
1028 	/* Checksums on by default */
1029 	sock_set_flag(sk, SOCK_ZAPPED);
1030 
1031 	if (sk->sk_prot->hash) {
1032 		rc = sk->sk_prot->hash(sk);
1033 		if (rc) {
1034 			sk_common_release(sk);
1035 			goto out;
1036 		}
1037 	}
1038 
1039 	if (sk->sk_prot->init) {
1040 		rc = sk->sk_prot->init(sk);
1041 		if (rc)
1042 			sk_common_release(sk);
1043 	}
1044 out:
1045 	return rc;
1046 }
1047 
1048 static const struct net_proto_family ieee802154_family_ops = {
1049 	.family		= PF_IEEE802154,
1050 	.create		= ieee802154_create,
1051 	.owner		= THIS_MODULE,
1052 };
1053 
1054 static int ieee802154_rcv(struct sk_buff *skb, struct net_device *dev,
1055 			  struct packet_type *pt, struct net_device *orig_dev)
1056 {
1057 	if (!netif_running(dev))
1058 		goto drop;
1059 	pr_debug("got frame, type %d, dev %p\n", dev->type, dev);
1060 #ifdef DEBUG
1061 	print_hex_dump_bytes("ieee802154_rcv ",
1062 			     DUMP_PREFIX_NONE, skb->data, skb->len);
1063 #endif
1064 
1065 	if (!net_eq(dev_net(dev), &init_net))
1066 		goto drop;
1067 
1068 	ieee802154_raw_deliver(dev, skb);
1069 
1070 	if (dev->type != ARPHRD_IEEE802154)
1071 		goto drop;
1072 
1073 	if (skb->pkt_type != PACKET_OTHERHOST)
1074 		return ieee802154_dgram_deliver(dev, skb);
1075 
1076 drop:
1077 	kfree_skb(skb);
1078 	return NET_RX_DROP;
1079 }
1080 
1081 static struct packet_type ieee802154_packet_type = {
1082 	.type = htons(ETH_P_IEEE802154),
1083 	.func = ieee802154_rcv,
1084 };
1085 
1086 static int __init af_ieee802154_init(void)
1087 {
1088 	int rc = -EINVAL;
1089 
1090 	rc = proto_register(&ieee802154_raw_prot, 1);
1091 	if (rc)
1092 		goto out;
1093 
1094 	rc = proto_register(&ieee802154_dgram_prot, 1);
1095 	if (rc)
1096 		goto err_dgram;
1097 
1098 	/* Tell SOCKET that we are alive */
1099 	rc = sock_register(&ieee802154_family_ops);
1100 	if (rc)
1101 		goto err_sock;
1102 	dev_add_pack(&ieee802154_packet_type);
1103 
1104 	rc = 0;
1105 	goto out;
1106 
1107 err_sock:
1108 	proto_unregister(&ieee802154_dgram_prot);
1109 err_dgram:
1110 	proto_unregister(&ieee802154_raw_prot);
1111 out:
1112 	return rc;
1113 }
1114 
1115 static void __exit af_ieee802154_remove(void)
1116 {
1117 	dev_remove_pack(&ieee802154_packet_type);
1118 	sock_unregister(PF_IEEE802154);
1119 	proto_unregister(&ieee802154_dgram_prot);
1120 	proto_unregister(&ieee802154_raw_prot);
1121 }
1122 
1123 module_init(af_ieee802154_init);
1124 module_exit(af_ieee802154_remove);
1125 
1126 MODULE_LICENSE("GPL");
1127 MODULE_ALIAS_NETPROTO(PF_IEEE802154);
1128