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