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