blob: d93d4531aa9bc5997df13ab6bf606d7f69e512b2 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Sergey Lapin9ec76712009-06-08 12:18:48 +00002/*
3 * IEEE802154.4 socket interface
4 *
5 * Copyright 2007, 2008 Siemens AG
6 *
Sergey Lapin9ec76712009-06-08 12:18:48 +00007 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Romuald CARI811e2992018-06-07 16:08:02 +020020#include <linux/socket.h>
Sergey Lapin9ec76712009-06-08 12:18:48 +000021#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
Dmitry Eremin-Solenikovf0166e52009-07-23 16:56:29 +040027#include <net/af_ieee802154.h>
28#include <net/ieee802154_netdev.h>
Sergey Lapin9ec76712009-06-08 12:18:48 +000029
Varka Bhadram4710d802014-07-02 09:01:09 +053030/* Utility function for families */
Alexander Aring78f821b2014-12-31 19:39:10 +010031static struct net_device*
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010032ieee802154_get_dev(struct net *net, const struct ieee802154_addr *addr)
Sergey Lapin9ec76712009-06-08 12:18:48 +000033{
34 struct net_device *dev = NULL;
35 struct net_device *tmp;
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010036 __le16 pan_id, short_addr;
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010037 u8 hwaddr[IEEE802154_ADDR_LEN];
Sergey Lapin9ec76712009-06-08 12:18:48 +000038
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010039 switch (addr->mode) {
Sergey Lapin9ec76712009-06-08 12:18:48 +000040 case IEEE802154_ADDR_LONG:
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010041 ieee802154_devaddr_to_raw(hwaddr, addr->extended_addr);
Eric Dumazet941666c2010-12-05 01:23:53 +000042 rcu_read_lock();
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010043 dev = dev_getbyhwaddr_rcu(net, ARPHRD_IEEE802154, hwaddr);
Sergey Lapin9ec76712009-06-08 12:18:48 +000044 if (dev)
45 dev_hold(dev);
Eric Dumazet941666c2010-12-05 01:23:53 +000046 rcu_read_unlock();
Sergey Lapin9ec76712009-06-08 12:18:48 +000047 break;
48 case IEEE802154_ADDR_SHORT:
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010049 if (addr->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST) ||
50 addr->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
Alexander Aring06324f22014-03-20 14:57:02 +010051 addr->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST))
Sergey Lapin9ec76712009-06-08 12:18:48 +000052 break;
53
54 rtnl_lock();
55
56 for_each_netdev(net, tmp) {
57 if (tmp->type != ARPHRD_IEEE802154)
58 continue;
59
Alexander Aringc947f7e2015-05-22 17:43:54 +020060 pan_id = tmp->ieee802154_ptr->pan_id;
61 short_addr = tmp->ieee802154_ptr->short_addr;
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010062 if (pan_id == addr->pan_id &&
63 short_addr == addr->short_addr) {
Sergey Lapin9ec76712009-06-08 12:18:48 +000064 dev = tmp;
65 dev_hold(dev);
66 break;
67 }
68 }
69
70 rtnl_unlock();
71 break;
72 default:
Varka Bhadram4710d802014-07-02 09:01:09 +053073 pr_warn("Unsupported ieee802154 address type: %d\n",
74 addr->mode);
Sergey Lapin9ec76712009-06-08 12:18:48 +000075 break;
76 }
77
78 return dev;
79}
80
81static 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}
Stefan Schmidt6c10dc02014-12-04 11:51:59 +010091
Ying Xue1b784142015-03-02 15:37:48 +080092static int ieee802154_sock_sendmsg(struct socket *sock, struct msghdr *msg,
93 size_t len)
Sergey Lapin9ec76712009-06-08 12:18:48 +000094{
95 struct sock *sk = sock->sk;
96
Ying Xue1b784142015-03-02 15:37:48 +080097 return sk->sk_prot->sendmsg(sk, msg, len);
Sergey Lapin9ec76712009-06-08 12:18:48 +000098}
99
100static int ieee802154_sock_bind(struct socket *sock, struct sockaddr *uaddr,
Varka Bhadram4710d802014-07-02 09:01:09 +0530101 int addr_len)
Sergey Lapin9ec76712009-06-08 12:18:48 +0000102{
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
111static int ieee802154_sock_connect(struct socket *sock, struct sockaddr *uaddr,
Varka Bhadram4710d802014-07-02 09:01:09 +0530112 int addr_len, int flags)
Sergey Lapin9ec76712009-06-08 12:18:48 +0000113{
114 struct sock *sk = sock->sk;
115
Changli Gao6503d962010-03-31 22:58:26 +0000116 if (addr_len < sizeof(uaddr->sa_family))
117 return -EINVAL;
118
Sergey Lapin9ec76712009-06-08 12:18:48 +0000119 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
125static int ieee802154_dev_ioctl(struct sock *sk, struct ifreq __user *arg,
Varka Bhadram4710d802014-07-02 09:01:09 +0530126 unsigned int cmd)
Sergey Lapin9ec76712009-06-08 12:18:48 +0000127{
128 struct ifreq ifr;
Dmitry Eremin-Solenikov45a41d12009-08-02 14:12:01 +0400129 int ret = -ENOIOCTLCMD;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000130 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);
Dmitry Eremin-Solenikov45a41d12009-08-02 14:12:01 +0400139
Stefan Schmidt93c0c8b2010-04-26 11:20:32 -0700140 if (!dev)
141 return -ENODEV;
142
Dmitry Eremin-Solenikov929122c2009-08-14 20:00:20 +0400143 if (dev->type == ARPHRD_IEEE802154 && dev->netdev_ops->ndo_do_ioctl)
Sergey Lapin9ec76712009-06-08 12:18:48 +0000144 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
153static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd,
Varka Bhadram4710d802014-07-02 09:01:09 +0530154 unsigned long arg)
Sergey Lapin9ec76712009-06-08 12:18:48 +0000155{
156 struct sock *sk = sock->sk;
157
158 switch (cmd) {
Sergey Lapin9ec76712009-06-08 12:18:48 +0000159 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
Alexander Aring78f821b2014-12-31 19:39:10 +0100170/* RAW Sockets (802.15.4 created in userspace) */
171static HLIST_HEAD(raw_head);
172static DEFINE_RWLOCK(raw_lock);
173
Craig Gallek086c6532016-02-10 11:50:35 -0500174static int raw_hash(struct sock *sk)
Alexander Aring78f821b2014-12-31 19:39:10 +0100175{
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);
Craig Gallek086c6532016-02-10 11:50:35 -0500180
181 return 0;
Alexander Aring78f821b2014-12-31 19:39:10 +0100182}
183
184static 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
192static void raw_close(struct sock *sk, long timeout)
193{
194 sk_common_release(sk);
195}
196
197static 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
Alexander Aring78f821b2014-12-31 19:39:10 +0100220 sk->sk_bound_dev_if = dev->ifindex;
221 sk_dst_reset(sk);
222
Alexander Aring78f821b2014-12-31 19:39:10 +0100223 dev_put(dev);
224out:
225 release_sock(sk);
226
227 return err;
228}
229
230static int raw_connect(struct sock *sk, struct sockaddr *uaddr,
231 int addr_len)
232{
233 return -ENOTSUPP;
234}
235
236static int raw_disconnect(struct sock *sk, int flags)
237{
238 return 0;
239}
240
Ying Xue1b784142015-03-02 15:37:48 +0800241static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
Alexander Aring78f821b2014-12-31 19:39:10 +0100242{
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
Alexander Aringb40988c2015-09-28 12:36:26 +0200267 mtu = IEEE802154_MTU;
Alexander Aring78f821b2014-12-31 19:39:10 +0100268 pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
269
270 if (size > mtu) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800271 pr_debug("size = %zu, mtu = %u\n", size, mtu);
Lennert Buytenhekc0327052015-05-25 15:38:24 +0300272 err = -EMSGSIZE;
Alexander Aring78f821b2014-12-31 19:39:10 +0100273 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;
Alexander Aring78f821b2014-12-31 19:39:10 +0100293 skb->protocol = htons(ETH_P_IEEE802154);
294
Alexander Aring78f821b2014-12-31 19:39:10 +0100295 err = dev_queue_xmit(skb);
296 if (err > 0)
297 err = net_xmit_errno(err);
298
Lin Zhanga611c582017-05-23 13:29:39 +0800299 dev_put(dev);
300
Alexander Aring78f821b2014-12-31 19:39:10 +0100301 return err ?: size;
302
303out_skb:
304 kfree_skb(skb);
305out_dev:
306 dev_put(dev);
307out:
308 return err;
309}
310
Ying Xue1b784142015-03-02 15:37:48 +0800311static int raw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
312 int noblock, int flags, int *addr_len)
Alexander Aring78f821b2014-12-31 19:39:10 +0100313{
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;
336done:
337 skb_free_datagram(sk, skb);
338out:
339 if (err)
340 return err;
341 return copied;
342}
343
344static 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
358static 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
378static int raw_getsockopt(struct sock *sk, int level, int optname,
379 char __user *optval, int __user *optlen)
380{
381 return -EOPNOTSUPP;
382}
383
384static int raw_setsockopt(struct sock *sk, int level, int optname,
385 char __user *optval, unsigned int optlen)
386{
387 return -EOPNOTSUPP;
388}
389
390static 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
Sergey Lapin9ec76712009-06-08 12:18:48 +0000406static 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,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700415 .poll = datagram_poll,
Sergey Lapin9ec76712009-06-08 12:18:48 +0000416 .ioctl = ieee802154_sock_ioctl,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +0200417 .gettstamp = sock_gettstamp,
Sergey Lapin9ec76712009-06-08 12:18:48 +0000418 .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#ifdef CONFIG_COMPAT
427 .compat_setsockopt = compat_sock_common_setsockopt,
428 .compat_getsockopt = compat_sock_common_getsockopt,
429#endif
430};
431
Alexander Aring78f821b2014-12-31 19:39:10 +0100432/* DGRAM Sockets (802.15.4 dataframes) */
433static HLIST_HEAD(dgram_head);
434static DEFINE_RWLOCK(dgram_lock);
435
436struct 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;
Romuald CARI811e2992018-06-07 16:08:02 +0200445 unsigned int want_lqi:1;
Alexander Aring78f821b2014-12-31 19:39:10 +0100446 unsigned int secen:1;
447 unsigned int secen_override:1;
448 unsigned int seclevel:3;
449 unsigned int seclevel_override:1;
450};
451
452static inline struct dgram_sock *dgram_sk(const struct sock *sk)
453{
454 return container_of(sk, struct dgram_sock, sk);
455}
456
Craig Gallek086c6532016-02-10 11:50:35 -0500457static int dgram_hash(struct sock *sk)
Alexander Aring78f821b2014-12-31 19:39:10 +0100458{
459 write_lock_bh(&dgram_lock);
460 sk_add_node(sk, &dgram_head);
461 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
462 write_unlock_bh(&dgram_lock);
Craig Gallek086c6532016-02-10 11:50:35 -0500463
464 return 0;
Alexander Aring78f821b2014-12-31 19:39:10 +0100465}
466
467static 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
475static int dgram_init(struct sock *sk)
476{
477 struct dgram_sock *ro = dgram_sk(sk);
478
479 ro->want_ack = 1;
Romuald CARI811e2992018-06-07 16:08:02 +0200480 ro->want_lqi = 0;
Alexander Aring78f821b2014-12-31 19:39:10 +0100481 return 0;
482}
483
484static void dgram_close(struct sock *sk, long timeout)
485{
486 sk_common_release(sk);
487}
488
489static 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 if (len < sizeof(*addr))
502 goto out;
503
504 if (addr->family != AF_IEEE802154)
505 goto out;
506
507 ieee802154_addr_from_sa(&haddr, &addr->addr);
508 dev = ieee802154_get_dev(sock_net(sk), &haddr);
509 if (!dev) {
510 err = -ENODEV;
511 goto out;
512 }
513
514 if (dev->type != ARPHRD_IEEE802154) {
515 err = -ENODEV;
516 goto out_put;
517 }
518
519 ro->src_addr = haddr;
520
521 ro->bound = 1;
522 err = 0;
523out_put:
524 dev_put(dev);
525out:
526 release_sock(sk);
527
528 return err;
529}
530
531static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg)
532{
533 switch (cmd) {
534 case SIOCOUTQ:
535 {
536 int amount = sk_wmem_alloc_get(sk);
537
538 return put_user(amount, (int __user *)arg);
539 }
540
541 case SIOCINQ:
542 {
543 struct sk_buff *skb;
544 unsigned long amount;
545
546 amount = 0;
547 spin_lock_bh(&sk->sk_receive_queue.lock);
548 skb = skb_peek(&sk->sk_receive_queue);
Alexander Aringdf2f65d2014-12-31 19:39:11 +0100549 if (skb) {
Alexander Aring78f821b2014-12-31 19:39:10 +0100550 /* We will only return the amount
551 * of this packet since that is all
552 * that will be read.
553 */
554 amount = skb->len - ieee802154_hdr_length(skb);
555 }
556 spin_unlock_bh(&sk->sk_receive_queue.lock);
557 return put_user(amount, (int __user *)arg);
558 }
559 }
560
561 return -ENOIOCTLCMD;
562}
563
564/* FIXME: autobind */
565static int dgram_connect(struct sock *sk, struct sockaddr *uaddr,
566 int len)
567{
568 struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
569 struct dgram_sock *ro = dgram_sk(sk);
570 int err = 0;
571
572 if (len < sizeof(*addr))
573 return -EINVAL;
574
575 if (addr->family != AF_IEEE802154)
576 return -EINVAL;
577
578 lock_sock(sk);
579
580 if (!ro->bound) {
581 err = -ENETUNREACH;
582 goto out;
583 }
584
585 ieee802154_addr_from_sa(&ro->dst_addr, &addr->addr);
586 ro->connected = 1;
587
588out:
589 release_sock(sk);
590 return err;
591}
592
593static int dgram_disconnect(struct sock *sk, int flags)
594{
595 struct dgram_sock *ro = dgram_sk(sk);
596
597 lock_sock(sk);
598 ro->connected = 0;
599 release_sock(sk);
600
601 return 0;
602}
603
Ying Xue1b784142015-03-02 15:37:48 +0800604static int dgram_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
Alexander Aring78f821b2014-12-31 19:39:10 +0100605{
606 struct net_device *dev;
607 unsigned int mtu;
608 struct sk_buff *skb;
609 struct ieee802154_mac_cb *cb;
610 struct dgram_sock *ro = dgram_sk(sk);
611 struct ieee802154_addr dst_addr;
612 int hlen, tlen;
613 int err;
614
615 if (msg->msg_flags & MSG_OOB) {
616 pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
617 return -EOPNOTSUPP;
618 }
619
620 if (!ro->connected && !msg->msg_name)
621 return -EDESTADDRREQ;
622 else if (ro->connected && msg->msg_name)
623 return -EISCONN;
624
625 if (!ro->bound)
626 dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
627 else
628 dev = ieee802154_get_dev(sock_net(sk), &ro->src_addr);
629
630 if (!dev) {
631 pr_debug("no dev\n");
632 err = -ENXIO;
633 goto out;
634 }
Alexander Aringb40988c2015-09-28 12:36:26 +0200635 mtu = IEEE802154_MTU;
Alexander Aring78f821b2014-12-31 19:39:10 +0100636 pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
637
638 if (size > mtu) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800639 pr_debug("size = %zu, mtu = %u\n", size, mtu);
Alexander Aring78f821b2014-12-31 19:39:10 +0100640 err = -EMSGSIZE;
641 goto out_dev;
642 }
643
644 hlen = LL_RESERVED_SPACE(dev);
645 tlen = dev->needed_tailroom;
646 skb = sock_alloc_send_skb(sk, hlen + tlen + size,
647 msg->msg_flags & MSG_DONTWAIT,
648 &err);
649 if (!skb)
650 goto out_dev;
651
652 skb_reserve(skb, hlen);
653
654 skb_reset_network_header(skb);
655
656 cb = mac_cb_init(skb);
657 cb->type = IEEE802154_FC_TYPE_DATA;
658 cb->ackreq = ro->want_ack;
659
660 if (msg->msg_name) {
661 DECLARE_SOCKADDR(struct sockaddr_ieee802154*,
662 daddr, msg->msg_name);
663
664 ieee802154_addr_from_sa(&dst_addr, &daddr->addr);
665 } else {
666 dst_addr = ro->dst_addr;
667 }
668
669 cb->secen = ro->secen;
670 cb->secen_override = ro->secen_override;
671 cb->seclevel = ro->seclevel;
672 cb->seclevel_override = ro->seclevel_override;
673
Alexander Aring838b83d2015-09-18 11:30:42 +0200674 err = wpan_dev_hard_header(skb, dev, &dst_addr,
675 ro->bound ? &ro->src_addr : NULL, size);
Alexander Aring78f821b2014-12-31 19:39:10 +0100676 if (err < 0)
677 goto out_skb;
678
679 err = memcpy_from_msg(skb_put(skb, size), msg, size);
680 if (err < 0)
681 goto out_skb;
682
683 skb->dev = dev;
Alexander Aring78f821b2014-12-31 19:39:10 +0100684 skb->protocol = htons(ETH_P_IEEE802154);
685
Alexander Aring78f821b2014-12-31 19:39:10 +0100686 err = dev_queue_xmit(skb);
687 if (err > 0)
688 err = net_xmit_errno(err);
689
Lin Zhanga611c582017-05-23 13:29:39 +0800690 dev_put(dev);
691
Alexander Aring78f821b2014-12-31 19:39:10 +0100692 return err ?: size;
693
694out_skb:
695 kfree_skb(skb);
696out_dev:
697 dev_put(dev);
698out:
699 return err;
700}
701
Ying Xue1b784142015-03-02 15:37:48 +0800702static int dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
703 int noblock, int flags, int *addr_len)
Alexander Aring78f821b2014-12-31 19:39:10 +0100704{
705 size_t copied = 0;
706 int err = -EOPNOTSUPP;
707 struct sk_buff *skb;
Romuald CARI811e2992018-06-07 16:08:02 +0200708 struct dgram_sock *ro = dgram_sk(sk);
Alexander Aring78f821b2014-12-31 19:39:10 +0100709 DECLARE_SOCKADDR(struct sockaddr_ieee802154 *, saddr, msg->msg_name);
710
711 skb = skb_recv_datagram(sk, flags, noblock, &err);
712 if (!skb)
713 goto out;
714
715 copied = skb->len;
716 if (len < copied) {
717 msg->msg_flags |= MSG_TRUNC;
718 copied = len;
719 }
720
721 /* FIXME: skip headers if necessary ?! */
722 err = skb_copy_datagram_msg(skb, 0, msg, copied);
723 if (err)
724 goto done;
725
726 sock_recv_ts_and_drops(msg, sk, skb);
727
728 if (saddr) {
Lennert Buytenhek8a70cef2015-06-03 10:50:19 +0300729 /* Clear the implicit padding in struct sockaddr_ieee802154
730 * (16 bits between 'family' and 'addr') and in struct
731 * ieee802154_addr_sa (16 bits at the end of the structure).
732 */
733 memset(saddr, 0, sizeof(*saddr));
734
Alexander Aring78f821b2014-12-31 19:39:10 +0100735 saddr->family = AF_IEEE802154;
736 ieee802154_addr_to_sa(&saddr->addr, &mac_cb(skb)->source);
737 *addr_len = sizeof(*saddr);
738 }
739
Romuald CARI811e2992018-06-07 16:08:02 +0200740 if (ro->want_lqi) {
741 err = put_cmsg(msg, SOL_IEEE802154, WPAN_WANTLQI,
742 sizeof(uint8_t), &(mac_cb(skb)->lqi));
743 if (err)
744 goto done;
745 }
746
Alexander Aring78f821b2014-12-31 19:39:10 +0100747 if (flags & MSG_TRUNC)
748 copied = skb->len;
749done:
750 skb_free_datagram(sk, skb);
751out:
752 if (err)
753 return err;
754 return copied;
755}
756
757static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb)
758{
759 skb = skb_share_check(skb, GFP_ATOMIC);
760 if (!skb)
761 return NET_RX_DROP;
762
763 if (sock_queue_rcv_skb(sk, skb) < 0) {
764 kfree_skb(skb);
765 return NET_RX_DROP;
766 }
767
768 return NET_RX_SUCCESS;
769}
770
771static inline bool
772ieee802154_match_sock(__le64 hw_addr, __le16 pan_id, __le16 short_addr,
773 struct dgram_sock *ro)
774{
775 if (!ro->bound)
776 return true;
777
778 if (ro->src_addr.mode == IEEE802154_ADDR_LONG &&
779 hw_addr == ro->src_addr.extended_addr)
780 return true;
781
782 if (ro->src_addr.mode == IEEE802154_ADDR_SHORT &&
783 pan_id == ro->src_addr.pan_id &&
784 short_addr == ro->src_addr.short_addr)
785 return true;
786
787 return false;
788}
789
790static int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
791{
792 struct sock *sk, *prev = NULL;
793 int ret = NET_RX_SUCCESS;
794 __le16 pan_id, short_addr;
795 __le64 hw_addr;
796
797 /* Data frame processing */
798 BUG_ON(dev->type != ARPHRD_IEEE802154);
799
Alexander Aringc947f7e2015-05-22 17:43:54 +0200800 pan_id = dev->ieee802154_ptr->pan_id;
801 short_addr = dev->ieee802154_ptr->short_addr;
802 hw_addr = dev->ieee802154_ptr->extended_addr;
Alexander Aring78f821b2014-12-31 19:39:10 +0100803
804 read_lock(&dgram_lock);
805 sk_for_each(sk, &dgram_head) {
806 if (ieee802154_match_sock(hw_addr, pan_id, short_addr,
807 dgram_sk(sk))) {
808 if (prev) {
809 struct sk_buff *clone;
810
811 clone = skb_clone(skb, GFP_ATOMIC);
812 if (clone)
813 dgram_rcv_skb(prev, clone);
814 }
815
816 prev = sk;
817 }
818 }
819
820 if (prev) {
821 dgram_rcv_skb(prev, skb);
822 } else {
823 kfree_skb(skb);
824 ret = NET_RX_DROP;
825 }
826 read_unlock(&dgram_lock);
827
828 return ret;
829}
830
831static int dgram_getsockopt(struct sock *sk, int level, int optname,
832 char __user *optval, int __user *optlen)
833{
834 struct dgram_sock *ro = dgram_sk(sk);
835
836 int val, len;
837
838 if (level != SOL_IEEE802154)
839 return -EOPNOTSUPP;
840
841 if (get_user(len, optlen))
842 return -EFAULT;
843
844 len = min_t(unsigned int, len, sizeof(int));
845
846 switch (optname) {
847 case WPAN_WANTACK:
848 val = ro->want_ack;
849 break;
Romuald CARI811e2992018-06-07 16:08:02 +0200850 case WPAN_WANTLQI:
851 val = ro->want_lqi;
852 break;
Alexander Aring78f821b2014-12-31 19:39:10 +0100853 case WPAN_SECURITY:
854 if (!ro->secen_override)
855 val = WPAN_SECURITY_DEFAULT;
856 else if (ro->secen)
857 val = WPAN_SECURITY_ON;
858 else
859 val = WPAN_SECURITY_OFF;
860 break;
861 case WPAN_SECURITY_LEVEL:
862 if (!ro->seclevel_override)
863 val = WPAN_SECURITY_LEVEL_DEFAULT;
864 else
865 val = ro->seclevel;
866 break;
867 default:
868 return -ENOPROTOOPT;
869 }
870
871 if (put_user(len, optlen))
872 return -EFAULT;
873 if (copy_to_user(optval, &val, len))
874 return -EFAULT;
875 return 0;
876}
877
878static int dgram_setsockopt(struct sock *sk, int level, int optname,
879 char __user *optval, unsigned int optlen)
880{
881 struct dgram_sock *ro = dgram_sk(sk);
882 struct net *net = sock_net(sk);
883 int val;
884 int err = 0;
885
886 if (optlen < sizeof(int))
887 return -EINVAL;
888
889 if (get_user(val, (int __user *)optval))
890 return -EFAULT;
891
892 lock_sock(sk);
893
894 switch (optname) {
895 case WPAN_WANTACK:
896 ro->want_ack = !!val;
897 break;
Romuald CARI811e2992018-06-07 16:08:02 +0200898 case WPAN_WANTLQI:
899 ro->want_lqi = !!val;
900 break;
Alexander Aring78f821b2014-12-31 19:39:10 +0100901 case WPAN_SECURITY:
902 if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
903 !ns_capable(net->user_ns, CAP_NET_RAW)) {
904 err = -EPERM;
905 break;
906 }
907
908 switch (val) {
909 case WPAN_SECURITY_DEFAULT:
910 ro->secen_override = 0;
911 break;
912 case WPAN_SECURITY_ON:
913 ro->secen_override = 1;
914 ro->secen = 1;
915 break;
916 case WPAN_SECURITY_OFF:
917 ro->secen_override = 1;
918 ro->secen = 0;
919 break;
920 default:
921 err = -EINVAL;
922 break;
923 }
924 break;
925 case WPAN_SECURITY_LEVEL:
926 if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
927 !ns_capable(net->user_ns, CAP_NET_RAW)) {
928 err = -EPERM;
929 break;
930 }
931
932 if (val < WPAN_SECURITY_LEVEL_DEFAULT ||
933 val > IEEE802154_SCF_SECLEVEL_ENC_MIC128) {
934 err = -EINVAL;
935 } else if (val == WPAN_SECURITY_LEVEL_DEFAULT) {
936 ro->seclevel_override = 0;
937 } else {
938 ro->seclevel_override = 1;
939 ro->seclevel = val;
940 }
941 break;
942 default:
943 err = -ENOPROTOOPT;
944 break;
945 }
946
947 release_sock(sk);
948 return err;
949}
950
951static struct proto ieee802154_dgram_prot = {
952 .name = "IEEE-802.15.4-MAC",
953 .owner = THIS_MODULE,
954 .obj_size = sizeof(struct dgram_sock),
955 .init = dgram_init,
956 .close = dgram_close,
957 .bind = dgram_bind,
958 .sendmsg = dgram_sendmsg,
959 .recvmsg = dgram_recvmsg,
960 .hash = dgram_hash,
961 .unhash = dgram_unhash,
962 .connect = dgram_connect,
963 .disconnect = dgram_disconnect,
964 .ioctl = dgram_ioctl,
965 .getsockopt = dgram_getsockopt,
966 .setsockopt = dgram_setsockopt,
967};
968
Sergey Lapin9ec76712009-06-08 12:18:48 +0000969static const struct proto_ops ieee802154_dgram_ops = {
970 .family = PF_IEEE802154,
971 .owner = THIS_MODULE,
972 .release = ieee802154_sock_release,
973 .bind = ieee802154_sock_bind,
974 .connect = ieee802154_sock_connect,
975 .socketpair = sock_no_socketpair,
976 .accept = sock_no_accept,
977 .getname = sock_no_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700978 .poll = datagram_poll,
Sergey Lapin9ec76712009-06-08 12:18:48 +0000979 .ioctl = ieee802154_sock_ioctl,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +0200980 .gettstamp = sock_gettstamp,
Sergey Lapin9ec76712009-06-08 12:18:48 +0000981 .listen = sock_no_listen,
982 .shutdown = sock_no_shutdown,
983 .setsockopt = sock_common_setsockopt,
984 .getsockopt = sock_common_getsockopt,
985 .sendmsg = ieee802154_sock_sendmsg,
986 .recvmsg = sock_common_recvmsg,
987 .mmap = sock_no_mmap,
988 .sendpage = sock_no_sendpage,
989#ifdef CONFIG_COMPAT
990 .compat_setsockopt = compat_sock_common_setsockopt,
991 .compat_getsockopt = compat_sock_common_getsockopt,
992#endif
993};
994
Varka Bhadram4710d802014-07-02 09:01:09 +0530995/* Create a socket. Initialise the socket, blank the addresses
Sergey Lapin9ec76712009-06-08 12:18:48 +0000996 * set the state.
997 */
998static int ieee802154_create(struct net *net, struct socket *sock,
Eric Paris3f378b62009-11-05 22:18:14 -0800999 int protocol, int kern)
Sergey Lapin9ec76712009-06-08 12:18:48 +00001000{
1001 struct sock *sk;
1002 int rc;
1003 struct proto *proto;
1004 const struct proto_ops *ops;
1005
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001006 if (!net_eq(net, &init_net))
Sergey Lapin9ec76712009-06-08 12:18:48 +00001007 return -EAFNOSUPPORT;
1008
1009 switch (sock->type) {
1010 case SOCK_RAW:
Ori Nimrone69dbd42019-09-20 09:35:48 +02001011 rc = -EPERM;
1012 if (!capable(CAP_NET_RAW))
1013 goto out;
Sergey Lapin9ec76712009-06-08 12:18:48 +00001014 proto = &ieee802154_raw_prot;
1015 ops = &ieee802154_raw_ops;
1016 break;
1017 case SOCK_DGRAM:
1018 proto = &ieee802154_dgram_prot;
1019 ops = &ieee802154_dgram_ops;
1020 break;
1021 default:
1022 rc = -ESOCKTNOSUPPORT;
1023 goto out;
1024 }
1025
1026 rc = -ENOMEM;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05001027 sk = sk_alloc(net, PF_IEEE802154, GFP_KERNEL, proto, kern);
Sergey Lapin9ec76712009-06-08 12:18:48 +00001028 if (!sk)
1029 goto out;
1030 rc = 0;
1031
1032 sock->ops = ops;
1033
1034 sock_init_data(sock, sk);
1035 /* FIXME: sk->sk_destruct */
1036 sk->sk_family = PF_IEEE802154;
1037
1038 /* Checksums on by default */
1039 sock_set_flag(sk, SOCK_ZAPPED);
1040
Craig Gallek086c6532016-02-10 11:50:35 -05001041 if (sk->sk_prot->hash) {
1042 rc = sk->sk_prot->hash(sk);
1043 if (rc) {
1044 sk_common_release(sk);
1045 goto out;
1046 }
1047 }
Sergey Lapin9ec76712009-06-08 12:18:48 +00001048
1049 if (sk->sk_prot->init) {
1050 rc = sk->sk_prot->init(sk);
1051 if (rc)
1052 sk_common_release(sk);
1053 }
1054out:
1055 return rc;
1056}
1057
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001058static const struct net_proto_family ieee802154_family_ops = {
Sergey Lapin9ec76712009-06-08 12:18:48 +00001059 .family = PF_IEEE802154,
1060 .create = ieee802154_create,
1061 .owner = THIS_MODULE,
1062};
1063
1064static int ieee802154_rcv(struct sk_buff *skb, struct net_device *dev,
Varka Bhadram4710d802014-07-02 09:01:09 +05301065 struct packet_type *pt, struct net_device *orig_dev)
Sergey Lapin9ec76712009-06-08 12:18:48 +00001066{
Sergey Lapin9ec76712009-06-08 12:18:48 +00001067 if (!netif_running(dev))
Alexander Smirnovfa1da882011-06-29 12:51:37 +00001068 goto drop;
Sergey Lapin9ec76712009-06-08 12:18:48 +00001069 pr_debug("got frame, type %d, dev %p\n", dev->type, dev);
Dmitry Baryshkov25502bd2009-06-18 04:16:46 +00001070#ifdef DEBUG
Varka Bhadram4710d802014-07-02 09:01:09 +05301071 print_hex_dump_bytes("ieee802154_rcv ",
1072 DUMP_PREFIX_NONE, skb->data, skb->len);
Dmitry Baryshkov25502bd2009-06-18 04:16:46 +00001073#endif
Sergey Lapin9ec76712009-06-08 12:18:48 +00001074
1075 if (!net_eq(dev_net(dev), &init_net))
1076 goto drop;
1077
1078 ieee802154_raw_deliver(dev, skb);
1079
1080 if (dev->type != ARPHRD_IEEE802154)
1081 goto drop;
1082
1083 if (skb->pkt_type != PACKET_OTHERHOST)
1084 return ieee802154_dgram_deliver(dev, skb);
1085
1086drop:
1087 kfree_skb(skb);
1088 return NET_RX_DROP;
1089}
1090
Sergey Lapin9ec76712009-06-08 12:18:48 +00001091static struct packet_type ieee802154_packet_type = {
Joe Perchesec633eb2014-03-12 10:04:18 -07001092 .type = htons(ETH_P_IEEE802154),
Sergey Lapin9ec76712009-06-08 12:18:48 +00001093 .func = ieee802154_rcv,
1094};
1095
1096static int __init af_ieee802154_init(void)
1097{
Colin Ian King074014a2019-08-13 15:28:18 +01001098 int rc;
Sergey Lapin9ec76712009-06-08 12:18:48 +00001099
1100 rc = proto_register(&ieee802154_raw_prot, 1);
1101 if (rc)
1102 goto out;
1103
1104 rc = proto_register(&ieee802154_dgram_prot, 1);
1105 if (rc)
1106 goto err_dgram;
1107
1108 /* Tell SOCKET that we are alive */
1109 rc = sock_register(&ieee802154_family_ops);
1110 if (rc)
1111 goto err_sock;
1112 dev_add_pack(&ieee802154_packet_type);
1113
1114 rc = 0;
1115 goto out;
1116
1117err_sock:
1118 proto_unregister(&ieee802154_dgram_prot);
1119err_dgram:
1120 proto_unregister(&ieee802154_raw_prot);
1121out:
1122 return rc;
1123}
Stefan Schmidt6c10dc02014-12-04 11:51:59 +01001124
Sergey Lapin9ec76712009-06-08 12:18:48 +00001125static void __exit af_ieee802154_remove(void)
1126{
1127 dev_remove_pack(&ieee802154_packet_type);
1128 sock_unregister(PF_IEEE802154);
1129 proto_unregister(&ieee802154_dgram_prot);
1130 proto_unregister(&ieee802154_raw_prot);
1131}
1132
1133module_init(af_ieee802154_init);
1134module_exit(af_ieee802154_remove);
1135
1136MODULE_LICENSE("GPL");
1137MODULE_ALIAS_NETPROTO(PF_IEEE802154);