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