Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ZigBee 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 | * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> |
| 22 | */ |
| 23 | |
| 24 | #include <linux/net.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/if_arp.h> |
| 27 | #include <linux/list.h> |
| 28 | #include <net/sock.h> |
Dmitry Eremin-Solenikov | f0166e5 | 2009-07-23 16:56:29 +0400 | [diff] [blame] | 29 | #include <net/af_ieee802154.h> |
| 30 | #include <net/ieee802154.h> |
| 31 | #include <net/ieee802154_netdev.h> |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 32 | |
| 33 | #include <asm/ioctls.h> |
| 34 | |
| 35 | #include "af802154.h" |
| 36 | |
| 37 | static HLIST_HEAD(dgram_head); |
| 38 | static DEFINE_RWLOCK(dgram_lock); |
| 39 | |
| 40 | struct dgram_sock { |
| 41 | struct sock sk; |
| 42 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 43 | struct ieee802154_addr src_addr; |
| 44 | struct ieee802154_addr dst_addr; |
Dmitry Baryshkov | 74eda55 | 2009-08-07 02:58:42 +0000 | [diff] [blame] | 45 | |
| 46 | unsigned bound:1; |
Dmitry Baryshkov | 99eb855 | 2009-08-07 02:58:43 +0000 | [diff] [blame] | 47 | unsigned want_ack:1; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | static inline struct dgram_sock *dgram_sk(const struct sock *sk) |
| 51 | { |
| 52 | return container_of(sk, struct dgram_sock, sk); |
| 53 | } |
| 54 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 55 | static void dgram_hash(struct sock *sk) |
| 56 | { |
| 57 | write_lock_bh(&dgram_lock); |
| 58 | sk_add_node(sk, &dgram_head); |
| 59 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
| 60 | write_unlock_bh(&dgram_lock); |
| 61 | } |
| 62 | |
| 63 | static void dgram_unhash(struct sock *sk) |
| 64 | { |
| 65 | write_lock_bh(&dgram_lock); |
| 66 | if (sk_del_node_init(sk)) |
| 67 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
| 68 | write_unlock_bh(&dgram_lock); |
| 69 | } |
| 70 | |
| 71 | static int dgram_init(struct sock *sk) |
| 72 | { |
| 73 | struct dgram_sock *ro = dgram_sk(sk); |
| 74 | |
| 75 | ro->dst_addr.addr_type = IEEE802154_ADDR_LONG; |
| 76 | ro->dst_addr.pan_id = 0xffff; |
Dmitry Baryshkov | 99eb855 | 2009-08-07 02:58:43 +0000 | [diff] [blame] | 77 | ro->want_ack = 1; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 78 | memset(&ro->dst_addr.hwaddr, 0xff, sizeof(ro->dst_addr.hwaddr)); |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static void dgram_close(struct sock *sk, long timeout) |
| 83 | { |
| 84 | sk_common_release(sk); |
| 85 | } |
| 86 | |
| 87 | static int dgram_bind(struct sock *sk, struct sockaddr *uaddr, int len) |
| 88 | { |
| 89 | struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr; |
| 90 | struct dgram_sock *ro = dgram_sk(sk); |
Dmitry Baryshkov | 74eda55 | 2009-08-07 02:58:42 +0000 | [diff] [blame] | 91 | int err = -EINVAL; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 92 | struct net_device *dev; |
| 93 | |
Dmitry Baryshkov | 74eda55 | 2009-08-07 02:58:42 +0000 | [diff] [blame] | 94 | lock_sock(sk); |
| 95 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 96 | ro->bound = 0; |
| 97 | |
| 98 | if (len < sizeof(*addr)) |
Dmitry Baryshkov | 74eda55 | 2009-08-07 02:58:42 +0000 | [diff] [blame] | 99 | goto out; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 100 | |
| 101 | if (addr->family != AF_IEEE802154) |
Dmitry Baryshkov | 74eda55 | 2009-08-07 02:58:42 +0000 | [diff] [blame] | 102 | goto out; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 103 | |
| 104 | dev = ieee802154_get_dev(sock_net(sk), &addr->addr); |
| 105 | if (!dev) { |
| 106 | err = -ENODEV; |
| 107 | goto out; |
| 108 | } |
| 109 | |
| 110 | if (dev->type != ARPHRD_IEEE802154) { |
| 111 | err = -ENODEV; |
| 112 | goto out_put; |
| 113 | } |
| 114 | |
| 115 | memcpy(&ro->src_addr, &addr->addr, sizeof(struct ieee802154_addr)); |
| 116 | |
| 117 | ro->bound = 1; |
Dmitry Baryshkov | 74eda55 | 2009-08-07 02:58:42 +0000 | [diff] [blame] | 118 | err = 0; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 119 | out_put: |
| 120 | dev_put(dev); |
| 121 | out: |
| 122 | release_sock(sk); |
| 123 | |
| 124 | return err; |
| 125 | } |
| 126 | |
| 127 | static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg) |
| 128 | { |
| 129 | switch (cmd) { |
| 130 | case SIOCOUTQ: |
| 131 | { |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 132 | int amount = sk_wmem_alloc_get(sk); |
| 133 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 134 | return put_user(amount, (int __user *)arg); |
| 135 | } |
| 136 | |
| 137 | case SIOCINQ: |
| 138 | { |
| 139 | struct sk_buff *skb; |
| 140 | unsigned long amount; |
| 141 | |
| 142 | amount = 0; |
| 143 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 144 | skb = skb_peek(&sk->sk_receive_queue); |
| 145 | if (skb != NULL) { |
| 146 | /* |
| 147 | * We will only return the amount |
| 148 | * of this packet since that is all |
| 149 | * that will be read. |
| 150 | */ |
| 151 | /* FIXME: parse the header for more correct value */ |
| 152 | amount = skb->len - (3+8+8); |
| 153 | } |
| 154 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 155 | return put_user(amount, (int __user *)arg); |
| 156 | } |
| 157 | |
| 158 | } |
| 159 | return -ENOIOCTLCMD; |
| 160 | } |
| 161 | |
| 162 | /* FIXME: autobind */ |
| 163 | static int dgram_connect(struct sock *sk, struct sockaddr *uaddr, |
| 164 | int len) |
| 165 | { |
| 166 | struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr; |
| 167 | struct dgram_sock *ro = dgram_sk(sk); |
| 168 | int err = 0; |
| 169 | |
| 170 | if (len < sizeof(*addr)) |
| 171 | return -EINVAL; |
| 172 | |
| 173 | if (addr->family != AF_IEEE802154) |
| 174 | return -EINVAL; |
| 175 | |
| 176 | lock_sock(sk); |
| 177 | |
| 178 | if (!ro->bound) { |
| 179 | err = -ENETUNREACH; |
| 180 | goto out; |
| 181 | } |
| 182 | |
| 183 | memcpy(&ro->dst_addr, &addr->addr, sizeof(struct ieee802154_addr)); |
| 184 | |
| 185 | out: |
| 186 | release_sock(sk); |
| 187 | return err; |
| 188 | } |
| 189 | |
| 190 | static int dgram_disconnect(struct sock *sk, int flags) |
| 191 | { |
| 192 | struct dgram_sock *ro = dgram_sk(sk); |
| 193 | |
| 194 | lock_sock(sk); |
| 195 | |
| 196 | ro->dst_addr.addr_type = IEEE802154_ADDR_LONG; |
| 197 | memset(&ro->dst_addr.hwaddr, 0xff, sizeof(ro->dst_addr.hwaddr)); |
| 198 | |
| 199 | release_sock(sk); |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int dgram_sendmsg(struct kiocb *iocb, struct sock *sk, |
| 205 | struct msghdr *msg, size_t size) |
| 206 | { |
| 207 | struct net_device *dev; |
| 208 | unsigned mtu; |
| 209 | struct sk_buff *skb; |
| 210 | struct dgram_sock *ro = dgram_sk(sk); |
| 211 | int err; |
| 212 | |
| 213 | if (msg->msg_flags & MSG_OOB) { |
| 214 | pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags); |
| 215 | return -EOPNOTSUPP; |
| 216 | } |
| 217 | |
| 218 | if (!ro->bound) |
| 219 | dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154); |
| 220 | else |
| 221 | dev = ieee802154_get_dev(sock_net(sk), &ro->src_addr); |
| 222 | |
| 223 | if (!dev) { |
| 224 | pr_debug("no dev\n"); |
| 225 | err = -ENXIO; |
| 226 | goto out; |
| 227 | } |
| 228 | mtu = dev->mtu; |
| 229 | pr_debug("name = %s, mtu = %u\n", dev->name, mtu); |
| 230 | |
| 231 | skb = sock_alloc_send_skb(sk, LL_ALLOCATED_SPACE(dev) + size, |
| 232 | msg->msg_flags & MSG_DONTWAIT, |
| 233 | &err); |
| 234 | if (!skb) |
| 235 | goto out_dev; |
| 236 | |
| 237 | skb_reserve(skb, LL_RESERVED_SPACE(dev)); |
| 238 | |
| 239 | skb_reset_network_header(skb); |
| 240 | |
Dmitry Baryshkov | 99eb855 | 2009-08-07 02:58:43 +0000 | [diff] [blame] | 241 | mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA; |
| 242 | if (ro->want_ack) |
| 243 | mac_cb(skb)->flags |= MAC_CB_FLAG_ACKREQ; |
| 244 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 245 | mac_cb(skb)->seq = ieee802154_mlme_ops(dev)->get_dsn(dev); |
| 246 | err = dev_hard_header(skb, dev, ETH_P_IEEE802154, &ro->dst_addr, |
| 247 | ro->bound ? &ro->src_addr : NULL, size); |
| 248 | if (err < 0) |
| 249 | goto out_skb; |
| 250 | |
| 251 | skb_reset_mac_header(skb); |
| 252 | |
| 253 | err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size); |
| 254 | if (err < 0) |
| 255 | goto out_skb; |
| 256 | |
| 257 | if (size > mtu) { |
David S. Miller | e5241c4 | 2009-06-11 02:10:19 -0700 | [diff] [blame] | 258 | pr_debug("size = %Zu, mtu = %u\n", size, mtu); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 259 | err = -EINVAL; |
| 260 | goto out_skb; |
| 261 | } |
| 262 | |
| 263 | skb->dev = dev; |
| 264 | skb->sk = sk; |
| 265 | skb->protocol = htons(ETH_P_IEEE802154); |
| 266 | |
| 267 | dev_put(dev); |
| 268 | |
| 269 | err = dev_queue_xmit(skb); |
| 270 | if (err > 0) |
| 271 | err = net_xmit_errno(err); |
| 272 | |
| 273 | return err ?: size; |
| 274 | |
| 275 | out_skb: |
| 276 | kfree_skb(skb); |
| 277 | out_dev: |
| 278 | dev_put(dev); |
| 279 | out: |
| 280 | return err; |
| 281 | } |
| 282 | |
| 283 | static int dgram_recvmsg(struct kiocb *iocb, struct sock *sk, |
| 284 | struct msghdr *msg, size_t len, int noblock, int flags, |
| 285 | int *addr_len) |
| 286 | { |
| 287 | size_t copied = 0; |
| 288 | int err = -EOPNOTSUPP; |
| 289 | struct sk_buff *skb; |
| 290 | |
| 291 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 292 | if (!skb) |
| 293 | goto out; |
| 294 | |
| 295 | copied = skb->len; |
| 296 | if (len < copied) { |
| 297 | msg->msg_flags |= MSG_TRUNC; |
| 298 | copied = len; |
| 299 | } |
| 300 | |
| 301 | /* FIXME: skip headers if necessary ?! */ |
| 302 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 303 | if (err) |
| 304 | goto done; |
| 305 | |
Neil Horman | 3b88578 | 2009-10-12 13:26:31 -0700 | [diff] [blame] | 306 | sock_recv_ts_and_drops(msg, sk, skb); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 307 | |
| 308 | if (flags & MSG_TRUNC) |
| 309 | copied = skb->len; |
| 310 | done: |
| 311 | skb_free_datagram(sk, skb); |
| 312 | out: |
| 313 | if (err) |
| 314 | return err; |
| 315 | return copied; |
| 316 | } |
| 317 | |
| 318 | static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 319 | { |
| 320 | if (sock_queue_rcv_skb(sk, skb) < 0) { |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 321 | kfree_skb(skb); |
| 322 | return NET_RX_DROP; |
| 323 | } |
| 324 | |
| 325 | return NET_RX_SUCCESS; |
| 326 | } |
| 327 | |
| 328 | static inline int ieee802154_match_sock(u8 *hw_addr, u16 pan_id, |
| 329 | u16 short_addr, struct dgram_sock *ro) |
| 330 | { |
| 331 | if (!ro->bound) |
| 332 | return 1; |
| 333 | |
| 334 | if (ro->src_addr.addr_type == IEEE802154_ADDR_LONG && |
| 335 | !memcmp(ro->src_addr.hwaddr, hw_addr, IEEE802154_ADDR_LEN)) |
| 336 | return 1; |
| 337 | |
| 338 | if (ro->src_addr.addr_type == IEEE802154_ADDR_SHORT && |
| 339 | pan_id == ro->src_addr.pan_id && |
| 340 | short_addr == ro->src_addr.short_addr) |
| 341 | return 1; |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb) |
| 347 | { |
| 348 | struct sock *sk, *prev = NULL; |
| 349 | struct hlist_node *node; |
| 350 | int ret = NET_RX_SUCCESS; |
| 351 | u16 pan_id, short_addr; |
| 352 | |
| 353 | /* Data frame processing */ |
| 354 | BUG_ON(dev->type != ARPHRD_IEEE802154); |
| 355 | |
| 356 | pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev); |
| 357 | short_addr = ieee802154_mlme_ops(dev)->get_short_addr(dev); |
| 358 | |
| 359 | read_lock(&dgram_lock); |
| 360 | sk_for_each(sk, node, &dgram_head) { |
| 361 | if (ieee802154_match_sock(dev->dev_addr, pan_id, short_addr, |
| 362 | dgram_sk(sk))) { |
| 363 | if (prev) { |
| 364 | struct sk_buff *clone; |
| 365 | clone = skb_clone(skb, GFP_ATOMIC); |
| 366 | if (clone) |
| 367 | dgram_rcv_skb(prev, clone); |
| 368 | } |
| 369 | |
| 370 | prev = sk; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | if (prev) |
| 375 | dgram_rcv_skb(prev, skb); |
| 376 | else { |
| 377 | kfree_skb(skb); |
| 378 | ret = NET_RX_DROP; |
| 379 | } |
| 380 | read_unlock(&dgram_lock); |
| 381 | |
| 382 | return ret; |
| 383 | } |
| 384 | |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 385 | static int dgram_getsockopt(struct sock *sk, int level, int optname, |
| 386 | char __user *optval, int __user *optlen) |
| 387 | { |
Dmitry Baryshkov | 99eb855 | 2009-08-07 02:58:43 +0000 | [diff] [blame] | 388 | struct dgram_sock *ro = dgram_sk(sk); |
| 389 | |
| 390 | int val, len; |
| 391 | |
| 392 | if (level != SOL_IEEE802154) |
| 393 | return -EOPNOTSUPP; |
| 394 | |
| 395 | if (get_user(len, optlen)) |
| 396 | return -EFAULT; |
| 397 | |
| 398 | len = min_t(unsigned int, len, sizeof(int)); |
| 399 | |
| 400 | switch (optname) { |
| 401 | case WPAN_WANTACK: |
| 402 | val = ro->want_ack; |
| 403 | break; |
| 404 | default: |
| 405 | return -ENOPROTOOPT; |
| 406 | } |
| 407 | |
| 408 | if (put_user(len, optlen)) |
| 409 | return -EFAULT; |
| 410 | if (copy_to_user(optval, &val, len)) |
| 411 | return -EFAULT; |
| 412 | return 0; |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static int dgram_setsockopt(struct sock *sk, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 416 | char __user *optval, unsigned int optlen) |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 417 | { |
Dmitry Baryshkov | 99eb855 | 2009-08-07 02:58:43 +0000 | [diff] [blame] | 418 | struct dgram_sock *ro = dgram_sk(sk); |
| 419 | int val; |
| 420 | int err = 0; |
| 421 | |
| 422 | if (optlen < sizeof(int)) |
| 423 | return -EINVAL; |
| 424 | |
| 425 | if (get_user(val, (int __user *)optval)) |
| 426 | return -EFAULT; |
| 427 | |
| 428 | lock_sock(sk); |
| 429 | |
| 430 | switch (optname) { |
| 431 | case WPAN_WANTACK: |
| 432 | ro->want_ack = !!val; |
| 433 | break; |
| 434 | default: |
| 435 | err = -ENOPROTOOPT; |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | release_sock(sk); |
| 440 | return err; |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 441 | } |
| 442 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 443 | struct proto ieee802154_dgram_prot = { |
| 444 | .name = "IEEE-802.15.4-MAC", |
| 445 | .owner = THIS_MODULE, |
| 446 | .obj_size = sizeof(struct dgram_sock), |
| 447 | .init = dgram_init, |
| 448 | .close = dgram_close, |
| 449 | .bind = dgram_bind, |
| 450 | .sendmsg = dgram_sendmsg, |
| 451 | .recvmsg = dgram_recvmsg, |
| 452 | .hash = dgram_hash, |
| 453 | .unhash = dgram_unhash, |
| 454 | .connect = dgram_connect, |
| 455 | .disconnect = dgram_disconnect, |
| 456 | .ioctl = dgram_ioctl, |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 457 | .getsockopt = dgram_getsockopt, |
| 458 | .setsockopt = dgram_setsockopt, |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 459 | }; |
| 460 | |