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