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