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 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Written by: |
| 20 | * Sergey Lapin <slapin@ossfans.org> |
| 21 | * Maxim Gorbachyov <maxim.gorbachev@siemens.com> |
| 22 | */ |
| 23 | |
| 24 | #include <linux/net.h> |
| 25 | #include <linux/capability.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/if_arp.h> |
| 28 | #include <linux/if.h> |
| 29 | #include <linux/termios.h> /* For TIOCOUTQ/INQ */ |
| 30 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 32 | #include <net/datalink.h> |
| 33 | #include <net/psnap.h> |
| 34 | #include <net/sock.h> |
| 35 | #include <net/tcp_states.h> |
| 36 | #include <net/route.h> |
| 37 | |
Dmitry Eremin-Solenikov | f0166e5 | 2009-07-23 16:56:29 +0400 | [diff] [blame] | 38 | #include <net/af_ieee802154.h> |
| 39 | #include <net/ieee802154_netdev.h> |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 40 | |
| 41 | #include "af802154.h" |
| 42 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 43 | /* |
| 44 | * Utility function for families |
| 45 | */ |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 46 | struct net_device* |
| 47 | ieee802154_get_dev(struct net *net, const struct ieee802154_addr *addr) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 48 | { |
| 49 | struct net_device *dev = NULL; |
| 50 | struct net_device *tmp; |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 51 | __le16 pan_id, short_addr; |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 52 | u8 hwaddr[IEEE802154_ADDR_LEN]; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 53 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 54 | switch (addr->mode) { |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 55 | case IEEE802154_ADDR_LONG: |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 56 | ieee802154_devaddr_to_raw(hwaddr, addr->extended_addr); |
Eric Dumazet | 941666c | 2010-12-05 01:23:53 +0000 | [diff] [blame] | 57 | rcu_read_lock(); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 58 | dev = dev_getbyhwaddr_rcu(net, ARPHRD_IEEE802154, hwaddr); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 59 | if (dev) |
| 60 | dev_hold(dev); |
Eric Dumazet | 941666c | 2010-12-05 01:23:53 +0000 | [diff] [blame] | 61 | rcu_read_unlock(); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 62 | break; |
| 63 | case IEEE802154_ADDR_SHORT: |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 64 | if (addr->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST) || |
| 65 | addr->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) || |
Alexander Aring | 06324f2 | 2014-03-20 14:57:02 +0100 | [diff] [blame] | 66 | addr->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST)) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 67 | break; |
| 68 | |
| 69 | rtnl_lock(); |
| 70 | |
| 71 | for_each_netdev(net, tmp) { |
| 72 | if (tmp->type != ARPHRD_IEEE802154) |
| 73 | continue; |
| 74 | |
| 75 | pan_id = ieee802154_mlme_ops(tmp)->get_pan_id(tmp); |
| 76 | short_addr = |
| 77 | ieee802154_mlme_ops(tmp)->get_short_addr(tmp); |
| 78 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 79 | if (pan_id == addr->pan_id && |
| 80 | short_addr == addr->short_addr) { |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 81 | dev = tmp; |
| 82 | dev_hold(dev); |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | rtnl_unlock(); |
| 88 | break; |
| 89 | default: |
| 90 | pr_warning("Unsupported ieee802154 address type: %d\n", |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 91 | addr->mode); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 92 | break; |
| 93 | } |
| 94 | |
| 95 | return dev; |
| 96 | } |
| 97 | |
| 98 | static int ieee802154_sock_release(struct socket *sock) |
| 99 | { |
| 100 | struct sock *sk = sock->sk; |
| 101 | |
| 102 | if (sk) { |
| 103 | sock->sk = NULL; |
| 104 | sk->sk_prot->close(sk, 0); |
| 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | static int ieee802154_sock_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 109 | struct msghdr *msg, size_t len) |
| 110 | { |
| 111 | struct sock *sk = sock->sk; |
| 112 | |
| 113 | return sk->sk_prot->sendmsg(iocb, sk, msg, len); |
| 114 | } |
| 115 | |
| 116 | static int ieee802154_sock_bind(struct socket *sock, struct sockaddr *uaddr, |
| 117 | int addr_len) |
| 118 | { |
| 119 | struct sock *sk = sock->sk; |
| 120 | |
| 121 | if (sk->sk_prot->bind) |
| 122 | return sk->sk_prot->bind(sk, uaddr, addr_len); |
| 123 | |
| 124 | return sock_no_bind(sock, uaddr, addr_len); |
| 125 | } |
| 126 | |
| 127 | static int ieee802154_sock_connect(struct socket *sock, struct sockaddr *uaddr, |
| 128 | int addr_len, int flags) |
| 129 | { |
| 130 | struct sock *sk = sock->sk; |
| 131 | |
Changli Gao | 6503d96 | 2010-03-31 22:58:26 +0000 | [diff] [blame] | 132 | if (addr_len < sizeof(uaddr->sa_family)) |
| 133 | return -EINVAL; |
| 134 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 135 | if (uaddr->sa_family == AF_UNSPEC) |
| 136 | return sk->sk_prot->disconnect(sk, flags); |
| 137 | |
| 138 | return sk->sk_prot->connect(sk, uaddr, addr_len); |
| 139 | } |
| 140 | |
| 141 | static int ieee802154_dev_ioctl(struct sock *sk, struct ifreq __user *arg, |
| 142 | unsigned int cmd) |
| 143 | { |
| 144 | struct ifreq ifr; |
Dmitry Eremin-Solenikov | 45a41d1 | 2009-08-02 14:12:01 +0400 | [diff] [blame] | 145 | int ret = -ENOIOCTLCMD; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 146 | struct net_device *dev; |
| 147 | |
| 148 | if (copy_from_user(&ifr, arg, sizeof(struct ifreq))) |
| 149 | return -EFAULT; |
| 150 | |
| 151 | ifr.ifr_name[IFNAMSIZ-1] = 0; |
| 152 | |
| 153 | dev_load(sock_net(sk), ifr.ifr_name); |
| 154 | dev = dev_get_by_name(sock_net(sk), ifr.ifr_name); |
Dmitry Eremin-Solenikov | 45a41d1 | 2009-08-02 14:12:01 +0400 | [diff] [blame] | 155 | |
Stefan Schmidt | 93c0c8b | 2010-04-26 11:20:32 -0700 | [diff] [blame] | 156 | if (!dev) |
| 157 | return -ENODEV; |
| 158 | |
Dmitry Eremin-Solenikov | 929122cd | 2009-08-14 20:00:20 +0400 | [diff] [blame] | 159 | if (dev->type == ARPHRD_IEEE802154 && dev->netdev_ops->ndo_do_ioctl) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 160 | ret = dev->netdev_ops->ndo_do_ioctl(dev, &ifr, cmd); |
| 161 | |
| 162 | if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq))) |
| 163 | ret = -EFAULT; |
| 164 | dev_put(dev); |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd, |
| 170 | unsigned long arg) |
| 171 | { |
| 172 | struct sock *sk = sock->sk; |
| 173 | |
| 174 | switch (cmd) { |
| 175 | case SIOCGSTAMP: |
| 176 | return sock_get_timestamp(sk, (struct timeval __user *)arg); |
| 177 | case SIOCGSTAMPNS: |
| 178 | return sock_get_timestampns(sk, (struct timespec __user *)arg); |
| 179 | case SIOCGIFADDR: |
| 180 | case SIOCSIFADDR: |
| 181 | return ieee802154_dev_ioctl(sk, (struct ifreq __user *)arg, |
| 182 | cmd); |
| 183 | default: |
| 184 | if (!sk->sk_prot->ioctl) |
| 185 | return -ENOIOCTLCMD; |
| 186 | return sk->sk_prot->ioctl(sk, cmd, arg); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | static const struct proto_ops ieee802154_raw_ops = { |
| 191 | .family = PF_IEEE802154, |
| 192 | .owner = THIS_MODULE, |
| 193 | .release = ieee802154_sock_release, |
| 194 | .bind = ieee802154_sock_bind, |
| 195 | .connect = ieee802154_sock_connect, |
| 196 | .socketpair = sock_no_socketpair, |
| 197 | .accept = sock_no_accept, |
| 198 | .getname = sock_no_getname, |
| 199 | .poll = datagram_poll, |
| 200 | .ioctl = ieee802154_sock_ioctl, |
| 201 | .listen = sock_no_listen, |
| 202 | .shutdown = sock_no_shutdown, |
| 203 | .setsockopt = sock_common_setsockopt, |
| 204 | .getsockopt = sock_common_getsockopt, |
| 205 | .sendmsg = ieee802154_sock_sendmsg, |
| 206 | .recvmsg = sock_common_recvmsg, |
| 207 | .mmap = sock_no_mmap, |
| 208 | .sendpage = sock_no_sendpage, |
| 209 | #ifdef CONFIG_COMPAT |
| 210 | .compat_setsockopt = compat_sock_common_setsockopt, |
| 211 | .compat_getsockopt = compat_sock_common_getsockopt, |
| 212 | #endif |
| 213 | }; |
| 214 | |
| 215 | static const struct proto_ops ieee802154_dgram_ops = { |
| 216 | .family = PF_IEEE802154, |
| 217 | .owner = THIS_MODULE, |
| 218 | .release = ieee802154_sock_release, |
| 219 | .bind = ieee802154_sock_bind, |
| 220 | .connect = ieee802154_sock_connect, |
| 221 | .socketpair = sock_no_socketpair, |
| 222 | .accept = sock_no_accept, |
| 223 | .getname = sock_no_getname, |
| 224 | .poll = datagram_poll, |
| 225 | .ioctl = ieee802154_sock_ioctl, |
| 226 | .listen = sock_no_listen, |
| 227 | .shutdown = sock_no_shutdown, |
| 228 | .setsockopt = sock_common_setsockopt, |
| 229 | .getsockopt = sock_common_getsockopt, |
| 230 | .sendmsg = ieee802154_sock_sendmsg, |
| 231 | .recvmsg = sock_common_recvmsg, |
| 232 | .mmap = sock_no_mmap, |
| 233 | .sendpage = sock_no_sendpage, |
| 234 | #ifdef CONFIG_COMPAT |
| 235 | .compat_setsockopt = compat_sock_common_setsockopt, |
| 236 | .compat_getsockopt = compat_sock_common_getsockopt, |
| 237 | #endif |
| 238 | }; |
| 239 | |
| 240 | |
| 241 | /* |
| 242 | * Create a socket. Initialise the socket, blank the addresses |
| 243 | * set the state. |
| 244 | */ |
| 245 | static int ieee802154_create(struct net *net, struct socket *sock, |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 246 | int protocol, int kern) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 247 | { |
| 248 | struct sock *sk; |
| 249 | int rc; |
| 250 | struct proto *proto; |
| 251 | const struct proto_ops *ops; |
| 252 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 253 | if (!net_eq(net, &init_net)) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 254 | return -EAFNOSUPPORT; |
| 255 | |
| 256 | switch (sock->type) { |
| 257 | case SOCK_RAW: |
| 258 | proto = &ieee802154_raw_prot; |
| 259 | ops = &ieee802154_raw_ops; |
| 260 | break; |
| 261 | case SOCK_DGRAM: |
| 262 | proto = &ieee802154_dgram_prot; |
| 263 | ops = &ieee802154_dgram_ops; |
| 264 | break; |
| 265 | default: |
| 266 | rc = -ESOCKTNOSUPPORT; |
| 267 | goto out; |
| 268 | } |
| 269 | |
| 270 | rc = -ENOMEM; |
| 271 | sk = sk_alloc(net, PF_IEEE802154, GFP_KERNEL, proto); |
| 272 | if (!sk) |
| 273 | goto out; |
| 274 | rc = 0; |
| 275 | |
| 276 | sock->ops = ops; |
| 277 | |
| 278 | sock_init_data(sock, sk); |
| 279 | /* FIXME: sk->sk_destruct */ |
| 280 | sk->sk_family = PF_IEEE802154; |
| 281 | |
| 282 | /* Checksums on by default */ |
| 283 | sock_set_flag(sk, SOCK_ZAPPED); |
| 284 | |
| 285 | if (sk->sk_prot->hash) |
| 286 | sk->sk_prot->hash(sk); |
| 287 | |
| 288 | if (sk->sk_prot->init) { |
| 289 | rc = sk->sk_prot->init(sk); |
| 290 | if (rc) |
| 291 | sk_common_release(sk); |
| 292 | } |
| 293 | out: |
| 294 | return rc; |
| 295 | } |
| 296 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 297 | static const struct net_proto_family ieee802154_family_ops = { |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 298 | .family = PF_IEEE802154, |
| 299 | .create = ieee802154_create, |
| 300 | .owner = THIS_MODULE, |
| 301 | }; |
| 302 | |
| 303 | static int ieee802154_rcv(struct sk_buff *skb, struct net_device *dev, |
| 304 | struct packet_type *pt, struct net_device *orig_dev) |
| 305 | { |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 306 | if (!netif_running(dev)) |
Alexander Smirnov | fa1da88 | 2011-06-29 12:51:37 +0000 | [diff] [blame] | 307 | goto drop; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 308 | pr_debug("got frame, type %d, dev %p\n", dev->type, dev); |
Dmitry Baryshkov | 25502bd | 2009-06-18 04:16:46 +0000 | [diff] [blame] | 309 | #ifdef DEBUG |
| 310 | print_hex_dump_bytes("ieee802154_rcv ", DUMP_PREFIX_NONE, skb->data, skb->len); |
| 311 | #endif |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 312 | |
| 313 | if (!net_eq(dev_net(dev), &init_net)) |
| 314 | goto drop; |
| 315 | |
| 316 | ieee802154_raw_deliver(dev, skb); |
| 317 | |
| 318 | if (dev->type != ARPHRD_IEEE802154) |
| 319 | goto drop; |
| 320 | |
| 321 | if (skb->pkt_type != PACKET_OTHERHOST) |
| 322 | return ieee802154_dgram_deliver(dev, skb); |
| 323 | |
| 324 | drop: |
| 325 | kfree_skb(skb); |
| 326 | return NET_RX_DROP; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | static struct packet_type ieee802154_packet_type = { |
Joe Perches | ec633eb | 2014-03-12 10:04:18 -0700 | [diff] [blame] | 331 | .type = htons(ETH_P_IEEE802154), |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 332 | .func = ieee802154_rcv, |
| 333 | }; |
| 334 | |
| 335 | static int __init af_ieee802154_init(void) |
| 336 | { |
| 337 | int rc = -EINVAL; |
| 338 | |
| 339 | rc = proto_register(&ieee802154_raw_prot, 1); |
| 340 | if (rc) |
| 341 | goto out; |
| 342 | |
| 343 | rc = proto_register(&ieee802154_dgram_prot, 1); |
| 344 | if (rc) |
| 345 | goto err_dgram; |
| 346 | |
| 347 | /* Tell SOCKET that we are alive */ |
| 348 | rc = sock_register(&ieee802154_family_ops); |
| 349 | if (rc) |
| 350 | goto err_sock; |
| 351 | dev_add_pack(&ieee802154_packet_type); |
| 352 | |
| 353 | rc = 0; |
| 354 | goto out; |
| 355 | |
| 356 | err_sock: |
| 357 | proto_unregister(&ieee802154_dgram_prot); |
| 358 | err_dgram: |
| 359 | proto_unregister(&ieee802154_raw_prot); |
| 360 | out: |
| 361 | return rc; |
| 362 | } |
| 363 | static void __exit af_ieee802154_remove(void) |
| 364 | { |
| 365 | dev_remove_pack(&ieee802154_packet_type); |
| 366 | sock_unregister(PF_IEEE802154); |
| 367 | proto_unregister(&ieee802154_dgram_prot); |
| 368 | proto_unregister(&ieee802154_raw_prot); |
| 369 | } |
| 370 | |
| 371 | module_init(af_ieee802154_init); |
| 372 | module_exit(af_ieee802154_remove); |
| 373 | |
| 374 | MODULE_LICENSE("GPL"); |
| 375 | MODULE_ALIAS_NETPROTO(PF_IEEE802154); |