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> |
| 31 | #include <net/datalink.h> |
| 32 | #include <net/psnap.h> |
| 33 | #include <net/sock.h> |
| 34 | #include <net/tcp_states.h> |
| 35 | #include <net/route.h> |
| 36 | |
Dmitry Eremin-Solenikov | f0166e5 | 2009-07-23 16:56:29 +0400 | [diff] [blame] | 37 | #include <net/af_ieee802154.h> |
| 38 | #include <net/ieee802154_netdev.h> |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 39 | |
| 40 | #include "af802154.h" |
| 41 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 42 | /* |
| 43 | * Utility function for families |
| 44 | */ |
| 45 | struct net_device *ieee802154_get_dev(struct net *net, |
| 46 | struct ieee802154_addr *addr) |
| 47 | { |
| 48 | struct net_device *dev = NULL; |
| 49 | struct net_device *tmp; |
| 50 | u16 pan_id, short_addr; |
| 51 | |
| 52 | switch (addr->addr_type) { |
| 53 | case IEEE802154_ADDR_LONG: |
| 54 | rtnl_lock(); |
| 55 | dev = dev_getbyhwaddr(net, ARPHRD_IEEE802154, addr->hwaddr); |
| 56 | if (dev) |
| 57 | dev_hold(dev); |
| 58 | rtnl_unlock(); |
| 59 | break; |
| 60 | case IEEE802154_ADDR_SHORT: |
| 61 | if (addr->pan_id == 0xffff || |
| 62 | addr->short_addr == IEEE802154_ADDR_UNDEF || |
| 63 | addr->short_addr == 0xffff) |
| 64 | break; |
| 65 | |
| 66 | rtnl_lock(); |
| 67 | |
| 68 | for_each_netdev(net, tmp) { |
| 69 | if (tmp->type != ARPHRD_IEEE802154) |
| 70 | continue; |
| 71 | |
| 72 | pan_id = ieee802154_mlme_ops(tmp)->get_pan_id(tmp); |
| 73 | short_addr = |
| 74 | ieee802154_mlme_ops(tmp)->get_short_addr(tmp); |
| 75 | |
| 76 | if (pan_id == addr->pan_id && |
| 77 | short_addr == addr->short_addr) { |
| 78 | dev = tmp; |
| 79 | dev_hold(dev); |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | rtnl_unlock(); |
| 85 | break; |
| 86 | default: |
| 87 | pr_warning("Unsupported ieee802154 address type: %d\n", |
| 88 | addr->addr_type); |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | return dev; |
| 93 | } |
| 94 | |
| 95 | static int ieee802154_sock_release(struct socket *sock) |
| 96 | { |
| 97 | struct sock *sk = sock->sk; |
| 98 | |
| 99 | if (sk) { |
| 100 | sock->sk = NULL; |
| 101 | sk->sk_prot->close(sk, 0); |
| 102 | } |
| 103 | return 0; |
| 104 | } |
| 105 | static int ieee802154_sock_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 106 | struct msghdr *msg, size_t len) |
| 107 | { |
| 108 | struct sock *sk = sock->sk; |
| 109 | |
| 110 | return sk->sk_prot->sendmsg(iocb, sk, msg, len); |
| 111 | } |
| 112 | |
| 113 | static int ieee802154_sock_bind(struct socket *sock, struct sockaddr *uaddr, |
| 114 | int addr_len) |
| 115 | { |
| 116 | struct sock *sk = sock->sk; |
| 117 | |
| 118 | if (sk->sk_prot->bind) |
| 119 | return sk->sk_prot->bind(sk, uaddr, addr_len); |
| 120 | |
| 121 | return sock_no_bind(sock, uaddr, addr_len); |
| 122 | } |
| 123 | |
| 124 | static int ieee802154_sock_connect(struct socket *sock, struct sockaddr *uaddr, |
| 125 | int addr_len, int flags) |
| 126 | { |
| 127 | struct sock *sk = sock->sk; |
| 128 | |
| 129 | if (uaddr->sa_family == AF_UNSPEC) |
| 130 | return sk->sk_prot->disconnect(sk, flags); |
| 131 | |
| 132 | return sk->sk_prot->connect(sk, uaddr, addr_len); |
| 133 | } |
| 134 | |
| 135 | static int ieee802154_dev_ioctl(struct sock *sk, struct ifreq __user *arg, |
| 136 | unsigned int cmd) |
| 137 | { |
| 138 | struct ifreq ifr; |
Dmitry Eremin-Solenikov | 45a41d1 | 2009-08-02 14:12:01 +0400 | [diff] [blame] | 139 | int ret = -ENOIOCTLCMD; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 140 | struct net_device *dev; |
| 141 | |
| 142 | if (copy_from_user(&ifr, arg, sizeof(struct ifreq))) |
| 143 | return -EFAULT; |
| 144 | |
| 145 | ifr.ifr_name[IFNAMSIZ-1] = 0; |
| 146 | |
| 147 | dev_load(sock_net(sk), ifr.ifr_name); |
| 148 | dev = dev_get_by_name(sock_net(sk), ifr.ifr_name); |
Dmitry Eremin-Solenikov | 45a41d1 | 2009-08-02 14:12:01 +0400 | [diff] [blame] | 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, |
| 161 | unsigned long arg) |
| 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 | |
| 181 | static const struct proto_ops ieee802154_raw_ops = { |
| 182 | .family = PF_IEEE802154, |
| 183 | .owner = THIS_MODULE, |
| 184 | .release = ieee802154_sock_release, |
| 185 | .bind = ieee802154_sock_bind, |
| 186 | .connect = ieee802154_sock_connect, |
| 187 | .socketpair = sock_no_socketpair, |
| 188 | .accept = sock_no_accept, |
| 189 | .getname = sock_no_getname, |
| 190 | .poll = datagram_poll, |
| 191 | .ioctl = ieee802154_sock_ioctl, |
| 192 | .listen = sock_no_listen, |
| 193 | .shutdown = sock_no_shutdown, |
| 194 | .setsockopt = sock_common_setsockopt, |
| 195 | .getsockopt = sock_common_getsockopt, |
| 196 | .sendmsg = ieee802154_sock_sendmsg, |
| 197 | .recvmsg = sock_common_recvmsg, |
| 198 | .mmap = sock_no_mmap, |
| 199 | .sendpage = sock_no_sendpage, |
| 200 | #ifdef CONFIG_COMPAT |
| 201 | .compat_setsockopt = compat_sock_common_setsockopt, |
| 202 | .compat_getsockopt = compat_sock_common_getsockopt, |
| 203 | #endif |
| 204 | }; |
| 205 | |
| 206 | static const struct proto_ops ieee802154_dgram_ops = { |
| 207 | .family = PF_IEEE802154, |
| 208 | .owner = THIS_MODULE, |
| 209 | .release = ieee802154_sock_release, |
| 210 | .bind = ieee802154_sock_bind, |
| 211 | .connect = ieee802154_sock_connect, |
| 212 | .socketpair = sock_no_socketpair, |
| 213 | .accept = sock_no_accept, |
| 214 | .getname = sock_no_getname, |
| 215 | .poll = datagram_poll, |
| 216 | .ioctl = ieee802154_sock_ioctl, |
| 217 | .listen = sock_no_listen, |
| 218 | .shutdown = sock_no_shutdown, |
| 219 | .setsockopt = sock_common_setsockopt, |
| 220 | .getsockopt = sock_common_getsockopt, |
| 221 | .sendmsg = ieee802154_sock_sendmsg, |
| 222 | .recvmsg = sock_common_recvmsg, |
| 223 | .mmap = sock_no_mmap, |
| 224 | .sendpage = sock_no_sendpage, |
| 225 | #ifdef CONFIG_COMPAT |
| 226 | .compat_setsockopt = compat_sock_common_setsockopt, |
| 227 | .compat_getsockopt = compat_sock_common_getsockopt, |
| 228 | #endif |
| 229 | }; |
| 230 | |
| 231 | |
| 232 | /* |
| 233 | * Create a socket. Initialise the socket, blank the addresses |
| 234 | * set the state. |
| 235 | */ |
| 236 | static int ieee802154_create(struct net *net, struct socket *sock, |
| 237 | int protocol) |
| 238 | { |
| 239 | struct sock *sk; |
| 240 | int rc; |
| 241 | struct proto *proto; |
| 242 | const struct proto_ops *ops; |
| 243 | |
| 244 | if (net != &init_net) |
| 245 | return -EAFNOSUPPORT; |
| 246 | |
| 247 | switch (sock->type) { |
| 248 | case SOCK_RAW: |
| 249 | proto = &ieee802154_raw_prot; |
| 250 | ops = &ieee802154_raw_ops; |
| 251 | break; |
| 252 | case SOCK_DGRAM: |
| 253 | proto = &ieee802154_dgram_prot; |
| 254 | ops = &ieee802154_dgram_ops; |
| 255 | break; |
| 256 | default: |
| 257 | rc = -ESOCKTNOSUPPORT; |
| 258 | goto out; |
| 259 | } |
| 260 | |
| 261 | rc = -ENOMEM; |
| 262 | sk = sk_alloc(net, PF_IEEE802154, GFP_KERNEL, proto); |
| 263 | if (!sk) |
| 264 | goto out; |
| 265 | rc = 0; |
| 266 | |
| 267 | sock->ops = ops; |
| 268 | |
| 269 | sock_init_data(sock, sk); |
| 270 | /* FIXME: sk->sk_destruct */ |
| 271 | sk->sk_family = PF_IEEE802154; |
| 272 | |
| 273 | /* Checksums on by default */ |
| 274 | sock_set_flag(sk, SOCK_ZAPPED); |
| 275 | |
| 276 | if (sk->sk_prot->hash) |
| 277 | sk->sk_prot->hash(sk); |
| 278 | |
| 279 | if (sk->sk_prot->init) { |
| 280 | rc = sk->sk_prot->init(sk); |
| 281 | if (rc) |
| 282 | sk_common_release(sk); |
| 283 | } |
| 284 | out: |
| 285 | return rc; |
| 286 | } |
| 287 | |
| 288 | static struct net_proto_family ieee802154_family_ops = { |
| 289 | .family = PF_IEEE802154, |
| 290 | .create = ieee802154_create, |
| 291 | .owner = THIS_MODULE, |
| 292 | }; |
| 293 | |
| 294 | static int ieee802154_rcv(struct sk_buff *skb, struct net_device *dev, |
| 295 | struct packet_type *pt, struct net_device *orig_dev) |
| 296 | { |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 297 | if (!netif_running(dev)) |
| 298 | return -ENODEV; |
| 299 | pr_debug("got frame, type %d, dev %p\n", dev->type, dev); |
Dmitry Baryshkov | 25502bd | 2009-06-18 04:16:46 +0000 | [diff] [blame] | 300 | #ifdef DEBUG |
| 301 | print_hex_dump_bytes("ieee802154_rcv ", DUMP_PREFIX_NONE, skb->data, skb->len); |
| 302 | #endif |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 303 | |
| 304 | if (!net_eq(dev_net(dev), &init_net)) |
| 305 | goto drop; |
| 306 | |
| 307 | ieee802154_raw_deliver(dev, skb); |
| 308 | |
| 309 | if (dev->type != ARPHRD_IEEE802154) |
| 310 | goto drop; |
| 311 | |
| 312 | if (skb->pkt_type != PACKET_OTHERHOST) |
| 313 | return ieee802154_dgram_deliver(dev, skb); |
| 314 | |
| 315 | drop: |
| 316 | kfree_skb(skb); |
| 317 | return NET_RX_DROP; |
| 318 | } |
| 319 | |
| 320 | |
| 321 | static struct packet_type ieee802154_packet_type = { |
| 322 | .type = __constant_htons(ETH_P_IEEE802154), |
| 323 | .func = ieee802154_rcv, |
| 324 | }; |
| 325 | |
| 326 | static int __init af_ieee802154_init(void) |
| 327 | { |
| 328 | int rc = -EINVAL; |
| 329 | |
| 330 | rc = proto_register(&ieee802154_raw_prot, 1); |
| 331 | if (rc) |
| 332 | goto out; |
| 333 | |
| 334 | rc = proto_register(&ieee802154_dgram_prot, 1); |
| 335 | if (rc) |
| 336 | goto err_dgram; |
| 337 | |
| 338 | /* Tell SOCKET that we are alive */ |
| 339 | rc = sock_register(&ieee802154_family_ops); |
| 340 | if (rc) |
| 341 | goto err_sock; |
| 342 | dev_add_pack(&ieee802154_packet_type); |
| 343 | |
| 344 | rc = 0; |
| 345 | goto out; |
| 346 | |
| 347 | err_sock: |
| 348 | proto_unregister(&ieee802154_dgram_prot); |
| 349 | err_dgram: |
| 350 | proto_unregister(&ieee802154_raw_prot); |
| 351 | out: |
| 352 | return rc; |
| 353 | } |
| 354 | static void __exit af_ieee802154_remove(void) |
| 355 | { |
| 356 | dev_remove_pack(&ieee802154_packet_type); |
| 357 | sock_unregister(PF_IEEE802154); |
| 358 | proto_unregister(&ieee802154_dgram_prot); |
| 359 | proto_unregister(&ieee802154_raw_prot); |
| 360 | } |
| 361 | |
| 362 | module_init(af_ieee802154_init); |
| 363 | module_exit(af_ieee802154_remove); |
| 364 | |
| 365 | MODULE_LICENSE("GPL"); |
| 366 | MODULE_ALIAS_NETPROTO(PF_IEEE802154); |