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