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