xref: /openbmc/linux/net/ieee802154/socket.c (revision f1575595)
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 <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>
34 
35 #include <net/af_ieee802154.h>
36 #include <net/ieee802154_netdev.h>
37 
38 /* Utility function for families */
39 static struct net_device*
40 ieee802154_get_dev(struct net *net, const struct ieee802154_addr *addr)
41 {
42 	struct net_device *dev = NULL;
43 	struct net_device *tmp;
44 	__le16 pan_id, short_addr;
45 	u8 hwaddr[IEEE802154_ADDR_LEN];
46 
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;
61 
62 		rtnl_lock();
63 
64 		for_each_netdev(net, tmp) {
65 			if (tmp->type != ARPHRD_IEEE802154)
66 				continue;
67 
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;
75 			}
76 		}
77 
78 		rtnl_unlock();
79 		break;
80 	default:
81 		pr_warn("Unsupported ieee802154 address type: %d\n",
82 			addr->mode);
83 		break;
84 	}
85 
86 	return dev;
87 }
88 
89 static int ieee802154_sock_release(struct socket *sock)
90 {
91 	struct sock *sk = sock->sk;
92 
93 	if (sk) {
94 		sock->sk = NULL;
95 		sk->sk_prot->close(sk, 0);
96 	}
97 	return 0;
98 }
99 
100 static int ieee802154_sock_sendmsg(struct socket *sock, struct msghdr *msg,
101 				   size_t len)
102 {
103 	struct sock *sk = sock->sk;
104 
105 	return sk->sk_prot->sendmsg(sk, msg, len);
106 }
107 
108 static int ieee802154_sock_bind(struct socket *sock, struct sockaddr *uaddr,
109 				int addr_len)
110 {
111 	struct sock *sk = sock->sk;
112 
113 	if (sk->sk_prot->bind)
114 		return sk->sk_prot->bind(sk, uaddr, addr_len);
115 
116 	return sock_no_bind(sock, uaddr, addr_len);
117 }
118 
119 static int ieee802154_sock_connect(struct socket *sock, struct sockaddr *uaddr,
120 				   int addr_len, int flags)
121 {
122 	struct sock *sk = sock->sk;
123 
124 	if (addr_len < sizeof(uaddr->sa_family))
125 		return -EINVAL;
126 
127 	if (uaddr->sa_family == AF_UNSPEC)
128 		return sk->sk_prot->disconnect(sk, flags);
129 
130 	return sk->sk_prot->connect(sk, uaddr, addr_len);
131 }
132 
133 static int ieee802154_dev_ioctl(struct sock *sk, struct ifreq __user *arg,
134 				unsigned int cmd)
135 {
136 	struct ifreq ifr;
137 	int ret = -ENOIOCTLCMD;
138 	struct net_device *dev;
139 
140 	if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
141 		return -EFAULT;
142 
143 	ifr.ifr_name[IFNAMSIZ-1] = 0;
144 
145 	dev_load(sock_net(sk), ifr.ifr_name);
146 	dev = dev_get_by_name(sock_net(sk), ifr.ifr_name);
147 
148 	if (!dev)
149 		return -ENODEV;
150 
151 	if (dev->type == ARPHRD_IEEE802154 && dev->netdev_ops->ndo_do_ioctl)
152 		ret = dev->netdev_ops->ndo_do_ioctl(dev, &ifr, cmd);
153 
154 	if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq)))
155 		ret = -EFAULT;
156 	dev_put(dev);
157 
158 	return ret;
159 }
160 
161 static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd,
162 				 unsigned long arg)
163 {
164 	struct sock *sk = sock->sk;
165 
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);
179 	}
180 }
181 
182 /* RAW Sockets (802.15.4 created in userspace) */
183 static HLIST_HEAD(raw_head);
184 static DEFINE_RWLOCK(raw_lock);
185 
186 static int raw_hash(struct sock *sk)
187 {
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);
192 
193 	return 0;
194 }
195 
196 static void raw_unhash(struct sock *sk)
197 {
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);
202 }
203 
204 static void raw_close(struct sock *sk, long timeout)
205 {
206 	sk_common_release(sk);
207 }
208 
209 static int raw_bind(struct sock *sk, struct sockaddr *_uaddr, int len)
210 {
211 	struct ieee802154_addr addr;
212 	struct sockaddr_ieee802154 *uaddr = (struct sockaddr_ieee802154 *)_uaddr;
213 	int err = 0;
214 	struct net_device *dev = NULL;
215 
216 	if (len < sizeof(*uaddr))
217 		return -EINVAL;
218 
219 	uaddr = (struct sockaddr_ieee802154 *)_uaddr;
220 	if (uaddr->family != AF_IEEE802154)
221 		return -EINVAL;
222 
223 	lock_sock(sk);
224 
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;
230 	}
231 
232 	sk->sk_bound_dev_if = dev->ifindex;
233 	sk_dst_reset(sk);
234 
235 	dev_put(dev);
236 out:
237 	release_sock(sk);
238 
239 	return err;
240 }
241 
242 static int raw_connect(struct sock *sk, struct sockaddr *uaddr,
243 		       int addr_len)
244 {
245 	return -ENOTSUPP;
246 }
247 
248 static int raw_disconnect(struct sock *sk, int flags)
249 {
250 	return 0;
251 }
252 
253 static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
254 {
255 	struct net_device *dev;
256 	unsigned int mtu;
257 	struct sk_buff *skb;
258 	int hlen, tlen;
259 	int err;
260 
261 	if (msg->msg_flags & MSG_OOB) {
262 		pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
263 		return -EOPNOTSUPP;
264 	}
265 
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);
272 
273 	if (!dev) {
274 		pr_debug("no dev\n");
275 		err = -ENXIO;
276 		goto out;
277 	}
278 
279 	mtu = IEEE802154_MTU;
280 	pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
281 
282 	if (size > mtu) {
283 		pr_debug("size = %zu, mtu = %u\n", size, mtu);
284 		err = -EMSGSIZE;
285 		goto out_dev;
286 	}
287 
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;
294 
295 	skb_reserve(skb, hlen);
296 
297 	skb_reset_mac_header(skb);
298 	skb_reset_network_header(skb);
299 
300 	err = memcpy_from_msg(skb_put(skb, size), msg, size);
301 	if (err < 0)
302 		goto out_skb;
303 
304 	skb->dev = dev;
305 	skb->protocol = htons(ETH_P_IEEE802154);
306 
307 	err = dev_queue_xmit(skb);
308 	if (err > 0)
309 		err = net_xmit_errno(err);
310 
311 	dev_put(dev);
312 
313 	return err ?: size;
314 
315 out_skb:
316 	kfree_skb(skb);
317 out_dev:
318 	dev_put(dev);
319 out:
320 	return err;
321 }
322 
323 static int raw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
324 		       int noblock, int flags, int *addr_len)
325 {
326 	size_t copied = 0;
327 	int err = -EOPNOTSUPP;
328 	struct sk_buff *skb;
329 
330 	skb = skb_recv_datagram(sk, flags, noblock, &err);
331 	if (!skb)
332 		goto out;
333 
334 	copied = skb->len;
335 	if (len < copied) {
336 		msg->msg_flags |= MSG_TRUNC;
337 		copied = len;
338 	}
339 
340 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
341 	if (err)
342 		goto done;
343 
344 	sock_recv_ts_and_drops(msg, sk, skb);
345 
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;
354 }
355 
356 static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb)
357 {
358 	skb = skb_share_check(skb, GFP_ATOMIC);
359 	if (!skb)
360 		return NET_RX_DROP;
361 
362 	if (sock_queue_rcv_skb(sk, skb) < 0) {
363 		kfree_skb(skb);
364 		return NET_RX_DROP;
365 	}
366 
367 	return NET_RX_SUCCESS;
368 }
369 
370 static void ieee802154_raw_deliver(struct net_device *dev, struct sk_buff *skb)
371 {
372 	struct sock *sk;
373 
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;
380 
381 			clone = skb_clone(skb, GFP_ATOMIC);
382 			if (clone)
383 				raw_rcv_skb(sk, clone);
384 		}
385 		bh_unlock_sock(sk);
386 	}
387 	read_unlock(&raw_lock);
388 }
389 
390 static int raw_getsockopt(struct sock *sk, int level, int optname,
391 			  char __user *optval, int __user *optlen)
392 {
393 	return -EOPNOTSUPP;
394 }
395 
396 static int raw_setsockopt(struct sock *sk, int level, int optname,
397 			  char __user *optval, unsigned int optlen)
398 {
399 	return -EOPNOTSUPP;
400 }
401 
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,
416 };
417 
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
441 };
442 
443 /* DGRAM Sockets (802.15.4 dataframes) */
444 static HLIST_HEAD(dgram_head);
445 static DEFINE_RWLOCK(dgram_lock);
446 
447 struct dgram_sock {
448 	struct sock sk;
449 
450 	struct ieee802154_addr src_addr;
451 	struct ieee802154_addr dst_addr;
452 
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;
461 };
462 
463 static inline struct dgram_sock *dgram_sk(const struct sock *sk)
464 {
465 	return container_of(sk, struct dgram_sock, sk);
466 }
467 
468 static int dgram_hash(struct sock *sk)
469 {
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);
474 
475 	return 0;
476 }
477 
478 static void dgram_unhash(struct sock *sk)
479 {
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);
484 }
485 
486 static int dgram_init(struct sock *sk)
487 {
488 	struct dgram_sock *ro = dgram_sk(sk);
489 
490 	ro->want_ack = 1;
491 	ro->want_lqi = 0;
492 	return 0;
493 }
494 
495 static void dgram_close(struct sock *sk, long timeout)
496 {
497 	sk_common_release(sk);
498 }
499 
500 static int dgram_bind(struct sock *sk, struct sockaddr *uaddr, int len)
501 {
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;
507 
508 	lock_sock(sk);
509 
510 	ro->bound = 0;
511 
512 	if (len < sizeof(*addr))
513 		goto out;
514 
515 	if (addr->family != AF_IEEE802154)
516 		goto out;
517 
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;
523 	}
524 
525 	if (dev->type != ARPHRD_IEEE802154) {
526 		err = -ENODEV;
527 		goto out_put;
528 	}
529 
530 	ro->src_addr = haddr;
531 
532 	ro->bound = 1;
533 	err = 0;
534 out_put:
535 	dev_put(dev);
536 out:
537 	release_sock(sk);
538 
539 	return err;
540 }
541 
542 static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg)
543 {
544 	switch (cmd) {
545 	case SIOCOUTQ:
546 	{
547 		int amount = sk_wmem_alloc_get(sk);
548 
549 		return put_user(amount, (int __user *)arg);
550 	}
551 
552 	case SIOCINQ:
553 	{
554 		struct sk_buff *skb;
555 		unsigned long amount;
556 
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.
564 			 */
565 			amount = skb->len - ieee802154_hdr_length(skb);
566 		}
567 		spin_unlock_bh(&sk->sk_receive_queue.lock);
568 		return put_user(amount, (int __user *)arg);
569 	}
570 	}
571 
572 	return -ENOIOCTLCMD;
573 }
574 
575 /* FIXME: autobind */
576 static int dgram_connect(struct sock *sk, struct sockaddr *uaddr,
577 			 int len)
578 {
579 	struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
580 	struct dgram_sock *ro = dgram_sk(sk);
581 	int err = 0;
582 
583 	if (len < sizeof(*addr))
584 		return -EINVAL;
585 
586 	if (addr->family != AF_IEEE802154)
587 		return -EINVAL;
588 
589 	lock_sock(sk);
590 
591 	if (!ro->bound) {
592 		err = -ENETUNREACH;
593 		goto out;
594 	}
595 
596 	ieee802154_addr_from_sa(&ro->dst_addr, &addr->addr);
597 	ro->connected = 1;
598 
599 out:
600 	release_sock(sk);
601 	return err;
602 }
603 
604 static int dgram_disconnect(struct sock *sk, int flags)
605 {
606 	struct dgram_sock *ro = dgram_sk(sk);
607 
608 	lock_sock(sk);
609 	ro->connected = 0;
610 	release_sock(sk);
611 
612 	return 0;
613 }
614 
615 static int dgram_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
616 {
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;
625 
626 	if (msg->msg_flags & MSG_OOB) {
627 		pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
628 		return -EOPNOTSUPP;
629 	}
630 
631 	if (!ro->connected && !msg->msg_name)
632 		return -EDESTADDRREQ;
633 	else if (ro->connected && msg->msg_name)
634 		return -EISCONN;
635 
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);
640 
641 	if (!dev) {
642 		pr_debug("no dev\n");
643 		err = -ENXIO;
644 		goto out;
645 	}
646 	mtu = IEEE802154_MTU;
647 	pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
648 
649 	if (size > mtu) {
650 		pr_debug("size = %zu, mtu = %u\n", size, mtu);
651 		err = -EMSGSIZE;
652 		goto out_dev;
653 	}
654 
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;
662 
663 	skb_reserve(skb, hlen);
664 
665 	skb_reset_network_header(skb);
666 
667 	cb = mac_cb_init(skb);
668 	cb->type = IEEE802154_FC_TYPE_DATA;
669 	cb->ackreq = ro->want_ack;
670 
671 	if (msg->msg_name) {
672 		DECLARE_SOCKADDR(struct sockaddr_ieee802154*,
673 				 daddr, msg->msg_name);
674 
675 		ieee802154_addr_from_sa(&dst_addr, &daddr->addr);
676 	} else {
677 		dst_addr = ro->dst_addr;
678 	}
679 
680 	cb->secen = ro->secen;
681 	cb->secen_override = ro->secen_override;
682 	cb->seclevel = ro->seclevel;
683 	cb->seclevel_override = ro->seclevel_override;
684 
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;
689 
690 	err = memcpy_from_msg(skb_put(skb, size), msg, size);
691 	if (err < 0)
692 		goto out_skb;
693 
694 	skb->dev = dev;
695 	skb->protocol = htons(ETH_P_IEEE802154);
696 
697 	err = dev_queue_xmit(skb);
698 	if (err > 0)
699 		err = net_xmit_errno(err);
700 
701 	dev_put(dev);
702 
703 	return err ?: size;
704 
705 out_skb:
706 	kfree_skb(skb);
707 out_dev:
708 	dev_put(dev);
709 out:
710 	return err;
711 }
712 
713 static int dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
714 			 int noblock, int flags, int *addr_len)
715 {
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);
721 
722 	skb = skb_recv_datagram(sk, flags, noblock, &err);
723 	if (!skb)
724 		goto out;
725 
726 	copied = skb->len;
727 	if (len < copied) {
728 		msg->msg_flags |= MSG_TRUNC;
729 		copied = len;
730 	}
731 
732 	/* FIXME: skip headers if necessary ?! */
733 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
734 	if (err)
735 		goto done;
736 
737 	sock_recv_ts_and_drops(msg, sk, skb);
738 
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).
743 		 */
744 		memset(saddr, 0, sizeof(*saddr));
745 
746 		saddr->family = AF_IEEE802154;
747 		ieee802154_addr_to_sa(&saddr->addr, &mac_cb(skb)->source);
748 		*addr_len = sizeof(*saddr);
749 	}
750 
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;
756 	}
757 
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;
766 }
767 
768 static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb)
769 {
770 	skb = skb_share_check(skb, GFP_ATOMIC);
771 	if (!skb)
772 		return NET_RX_DROP;
773 
774 	if (sock_queue_rcv_skb(sk, skb) < 0) {
775 		kfree_skb(skb);
776 		return NET_RX_DROP;
777 	}
778 
779 	return NET_RX_SUCCESS;
780 }
781 
782 static inline bool
783 ieee802154_match_sock(__le64 hw_addr, __le16 pan_id, __le16 short_addr,
784 		      struct dgram_sock *ro)
785 {
786 	if (!ro->bound)
787 		return true;
788 
789 	if (ro->src_addr.mode == IEEE802154_ADDR_LONG &&
790 	    hw_addr == ro->src_addr.extended_addr)
791 		return true;
792 
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;
797 
798 	return false;
799 }
800 
801 static int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
802 {
803 	struct sock *sk, *prev = NULL;
804 	int ret = NET_RX_SUCCESS;
805 	__le16 pan_id, short_addr;
806 	__le64 hw_addr;
807 
808 	/* Data frame processing */
809 	BUG_ON(dev->type != ARPHRD_IEEE802154);
810 
811 	pan_id = dev->ieee802154_ptr->pan_id;
812 	short_addr = dev->ieee802154_ptr->short_addr;
813 	hw_addr = dev->ieee802154_ptr->extended_addr;
814 
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;
821 
822 				clone = skb_clone(skb, GFP_ATOMIC);
823 				if (clone)
824 					dgram_rcv_skb(prev, clone);
825 			}
826 
827 			prev = sk;
828 		}
829 	}
830 
831 	if (prev) {
832 		dgram_rcv_skb(prev, skb);
833 	} else {
834 		kfree_skb(skb);
835 		ret = NET_RX_DROP;
836 	}
837 	read_unlock(&dgram_lock);
838 
839 	return ret;
840 }
841 
842 static int dgram_getsockopt(struct sock *sk, int level, int optname,
843 			    char __user *optval, int __user *optlen)
844 {
845 	struct dgram_sock *ro = dgram_sk(sk);
846 
847 	int val, len;
848 
849 	if (level != SOL_IEEE802154)
850 		return -EOPNOTSUPP;
851 
852 	if (get_user(len, optlen))
853 		return -EFAULT;
854 
855 	len = min_t(unsigned int, len, sizeof(int));
856 
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;
880 	}
881 
882 	if (put_user(len, optlen))
883 		return -EFAULT;
884 	if (copy_to_user(optval, &val, len))
885 		return -EFAULT;
886 	return 0;
887 }
888 
889 static int dgram_setsockopt(struct sock *sk, int level, int optname,
890 			    char __user *optval, unsigned int optlen)
891 {
892 	struct dgram_sock *ro = dgram_sk(sk);
893 	struct net *net = sock_net(sk);
894 	int val;
895 	int err = 0;
896 
897 	if (optlen < sizeof(int))
898 		return -EINVAL;
899 
900 	if (get_user(val, (int __user *)optval))
901 		return -EFAULT;
902 
903 	lock_sock(sk);
904 
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;
917 		}
918 
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;
934 		}
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;
941 		}
942 
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;
951 		}
952 		break;
953 	default:
954 		err = -ENOPROTOOPT;
955 		break;
956 	}
957 
958 	release_sock(sk);
959 	return err;
960 }
961 
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,
978 };
979 
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
1003 };
1004 
1005 /* Create a socket. Initialise the socket, blank the addresses
1006  * set the state.
1007  */
1008 static int ieee802154_create(struct net *net, struct socket *sock,
1009 			     int protocol, int kern)
1010 {
1011 	struct sock *sk;
1012 	int rc;
1013 	struct proto *proto;
1014 	const struct proto_ops *ops;
1015 
1016 	if (!net_eq(net, &init_net))
1017 		return -EAFNOSUPPORT;
1018 
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;
1031 	}
1032 
1033 	rc = -ENOMEM;
1034 	sk = sk_alloc(net, PF_IEEE802154, GFP_KERNEL, proto, kern);
1035 	if (!sk)
1036 		goto out;
1037 	rc = 0;
1038 
1039 	sock->ops = ops;
1040 
1041 	sock_init_data(sock, sk);
1042 	/* FIXME: sk->sk_destruct */
1043 	sk->sk_family = PF_IEEE802154;
1044 
1045 	/* Checksums on by default */
1046 	sock_set_flag(sk, SOCK_ZAPPED);
1047 
1048 	if (sk->sk_prot->hash) {
1049 		rc = sk->sk_prot->hash(sk);
1050 		if (rc) {
1051 			sk_common_release(sk);
1052 			goto out;
1053 		}
1054 	}
1055 
1056 	if (sk->sk_prot->init) {
1057 		rc = sk->sk_prot->init(sk);
1058 		if (rc)
1059 			sk_common_release(sk);
1060 	}
1061 out:
1062 	return rc;
1063 }
1064 
1065 static const struct net_proto_family ieee802154_family_ops = {
1066 	.family		= PF_IEEE802154,
1067 	.create		= ieee802154_create,
1068 	.owner		= THIS_MODULE,
1069 };
1070 
1071 static int ieee802154_rcv(struct sk_buff *skb, struct net_device *dev,
1072 			  struct packet_type *pt, struct net_device *orig_dev)
1073 {
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
1081 
1082 	if (!net_eq(dev_net(dev), &init_net))
1083 		goto drop;
1084 
1085 	ieee802154_raw_deliver(dev, skb);
1086 
1087 	if (dev->type != ARPHRD_IEEE802154)
1088 		goto drop;
1089 
1090 	if (skb->pkt_type != PACKET_OTHERHOST)
1091 		return ieee802154_dgram_deliver(dev, skb);
1092 
1093 drop:
1094 	kfree_skb(skb);
1095 	return NET_RX_DROP;
1096 }
1097 
1098 static struct packet_type ieee802154_packet_type = {
1099 	.type = htons(ETH_P_IEEE802154),
1100 	.func = ieee802154_rcv,
1101 };
1102 
1103 static int __init af_ieee802154_init(void)
1104 {
1105 	int rc = -EINVAL;
1106 
1107 	rc = proto_register(&ieee802154_raw_prot, 1);
1108 	if (rc)
1109 		goto out;
1110 
1111 	rc = proto_register(&ieee802154_dgram_prot, 1);
1112 	if (rc)
1113 		goto err_dgram;
1114 
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);
1120 
1121 	rc = 0;
1122 	goto out;
1123 
1124 err_sock:
1125 	proto_unregister(&ieee802154_dgram_prot);
1126 err_dgram:
1127 	proto_unregister(&ieee802154_raw_prot);
1128 out:
1129 	return rc;
1130 }
1131 
1132 static void __exit af_ieee802154_remove(void)
1133 {
1134 	dev_remove_pack(&ieee802154_packet_type);
1135 	sock_unregister(PF_IEEE802154);
1136 	proto_unregister(&ieee802154_dgram_prot);
1137 	proto_unregister(&ieee802154_raw_prot);
1138 }
1139 
1140 module_init(af_ieee802154_init);
1141 module_exit(af_ieee802154_remove);
1142 
1143 MODULE_LICENSE("GPL");
1144 MODULE_ALIAS_NETPROTO(PF_IEEE802154);
1145