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 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [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 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 47 | unsigned int bound:1; |
| 48 | unsigned int 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 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 76 | ro->dst_addr.mode = IEEE802154_ADDR_LONG; |
Alexander Aring | 54af36e | 2014-03-20 14:57:03 +0100 | [diff] [blame] | 77 | ro->dst_addr.pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST); |
Dmitry Baryshkov | 99eb855 | 2009-08-07 02:58:43 +0000 | [diff] [blame] | 78 | ro->want_ack = 1; |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 79 | memset(&ro->dst_addr.extended_addr, 0xff, IEEE802154_ADDR_LEN); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 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; |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 91 | struct ieee802154_addr haddr; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 92 | struct dgram_sock *ro = dgram_sk(sk); |
Dmitry Baryshkov | 74eda55 | 2009-08-07 02:58:42 +0000 | [diff] [blame] | 93 | int err = -EINVAL; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 94 | struct net_device *dev; |
| 95 | |
Dmitry Baryshkov | 74eda55 | 2009-08-07 02:58:42 +0000 | [diff] [blame] | 96 | lock_sock(sk); |
| 97 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 98 | ro->bound = 0; |
| 99 | |
| 100 | if (len < sizeof(*addr)) |
Dmitry Baryshkov | 74eda55 | 2009-08-07 02:58:42 +0000 | [diff] [blame] | 101 | goto out; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 102 | |
| 103 | if (addr->family != AF_IEEE802154) |
Dmitry Baryshkov | 74eda55 | 2009-08-07 02:58:42 +0000 | [diff] [blame] | 104 | goto out; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 105 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 106 | ieee802154_addr_from_sa(&haddr, &addr->addr); |
| 107 | dev = ieee802154_get_dev(sock_net(sk), &haddr); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 108 | if (!dev) { |
| 109 | err = -ENODEV; |
| 110 | goto out; |
| 111 | } |
| 112 | |
| 113 | if (dev->type != ARPHRD_IEEE802154) { |
| 114 | err = -ENODEV; |
| 115 | goto out_put; |
| 116 | } |
| 117 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 118 | ro->src_addr = haddr; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 119 | |
| 120 | ro->bound = 1; |
Dmitry Baryshkov | 74eda55 | 2009-08-07 02:58:42 +0000 | [diff] [blame] | 121 | err = 0; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 122 | out_put: |
| 123 | dev_put(dev); |
| 124 | out: |
| 125 | release_sock(sk); |
| 126 | |
| 127 | return err; |
| 128 | } |
| 129 | |
| 130 | static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg) |
| 131 | { |
| 132 | switch (cmd) { |
| 133 | case SIOCOUTQ: |
| 134 | { |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 135 | int amount = sk_wmem_alloc_get(sk); |
| 136 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 137 | return put_user(amount, (int __user *)arg); |
| 138 | } |
| 139 | |
| 140 | case SIOCINQ: |
| 141 | { |
| 142 | struct sk_buff *skb; |
| 143 | unsigned long amount; |
| 144 | |
| 145 | amount = 0; |
| 146 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 147 | skb = skb_peek(&sk->sk_receive_queue); |
| 148 | if (skb != NULL) { |
| 149 | /* |
| 150 | * We will only return the amount |
| 151 | * of this packet since that is all |
| 152 | * that will be read. |
| 153 | */ |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 154 | amount = skb->len - ieee802154_hdr_length(skb); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 155 | } |
| 156 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 157 | return put_user(amount, (int __user *)arg); |
| 158 | } |
| 159 | |
| 160 | } |
| 161 | return -ENOIOCTLCMD; |
| 162 | } |
| 163 | |
| 164 | /* FIXME: autobind */ |
| 165 | static int dgram_connect(struct sock *sk, struct sockaddr *uaddr, |
| 166 | int len) |
| 167 | { |
| 168 | struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr; |
| 169 | struct dgram_sock *ro = dgram_sk(sk); |
| 170 | int err = 0; |
| 171 | |
| 172 | if (len < sizeof(*addr)) |
| 173 | return -EINVAL; |
| 174 | |
| 175 | if (addr->family != AF_IEEE802154) |
| 176 | return -EINVAL; |
| 177 | |
| 178 | lock_sock(sk); |
| 179 | |
| 180 | if (!ro->bound) { |
| 181 | err = -ENETUNREACH; |
| 182 | goto out; |
| 183 | } |
| 184 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 185 | ieee802154_addr_from_sa(&ro->dst_addr, &addr->addr); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 186 | |
| 187 | out: |
| 188 | release_sock(sk); |
| 189 | return err; |
| 190 | } |
| 191 | |
| 192 | static int dgram_disconnect(struct sock *sk, int flags) |
| 193 | { |
| 194 | struct dgram_sock *ro = dgram_sk(sk); |
| 195 | |
| 196 | lock_sock(sk); |
| 197 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 198 | ro->dst_addr.mode = IEEE802154_ADDR_LONG; |
| 199 | memset(&ro->dst_addr.extended_addr, 0xff, IEEE802154_ADDR_LEN); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 200 | |
| 201 | release_sock(sk); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static int dgram_sendmsg(struct kiocb *iocb, struct sock *sk, |
| 207 | struct msghdr *msg, size_t size) |
| 208 | { |
| 209 | struct net_device *dev; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 210 | unsigned int mtu; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 211 | struct sk_buff *skb; |
Phoebe Buckheister | 32edc40 | 2014-05-14 17:43:08 +0200 | [diff] [blame^] | 212 | struct ieee802154_mac_cb *cb; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 213 | struct dgram_sock *ro = dgram_sk(sk); |
Herbert Xu | ae64194 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 214 | int hlen, tlen; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 215 | int err; |
| 216 | |
| 217 | if (msg->msg_flags & MSG_OOB) { |
| 218 | pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags); |
| 219 | return -EOPNOTSUPP; |
| 220 | } |
| 221 | |
| 222 | if (!ro->bound) |
| 223 | dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154); |
| 224 | else |
| 225 | dev = ieee802154_get_dev(sock_net(sk), &ro->src_addr); |
| 226 | |
| 227 | if (!dev) { |
| 228 | pr_debug("no dev\n"); |
| 229 | err = -ENXIO; |
| 230 | goto out; |
| 231 | } |
| 232 | mtu = dev->mtu; |
| 233 | pr_debug("name = %s, mtu = %u\n", dev->name, mtu); |
| 234 | |
Sasha Levin | 3da947b | 2012-07-02 01:29:55 +0000 | [diff] [blame] | 235 | if (size > mtu) { |
| 236 | pr_debug("size = %Zu, mtu = %u\n", size, mtu); |
Phoebe Buckheister | d1d7358 | 2014-03-14 21:24:04 +0100 | [diff] [blame] | 237 | err = -EMSGSIZE; |
Sasha Levin | 3da947b | 2012-07-02 01:29:55 +0000 | [diff] [blame] | 238 | goto out_dev; |
| 239 | } |
| 240 | |
Herbert Xu | ae64194 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 241 | hlen = LL_RESERVED_SPACE(dev); |
| 242 | tlen = dev->needed_tailroom; |
| 243 | skb = sock_alloc_send_skb(sk, hlen + tlen + size, |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 244 | msg->msg_flags & MSG_DONTWAIT, |
| 245 | &err); |
| 246 | if (!skb) |
| 247 | goto out_dev; |
| 248 | |
Herbert Xu | ae64194 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 249 | skb_reserve(skb, hlen); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 250 | |
| 251 | skb_reset_network_header(skb); |
| 252 | |
Phoebe Buckheister | 32edc40 | 2014-05-14 17:43:08 +0200 | [diff] [blame^] | 253 | cb = mac_cb_init(skb); |
| 254 | cb->type = IEEE802154_FC_TYPE_DATA; |
| 255 | cb->ackreq = ro->want_ack; |
Dmitry Baryshkov | 99eb855 | 2009-08-07 02:58:43 +0000 | [diff] [blame] | 256 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 257 | err = dev_hard_header(skb, dev, ETH_P_IEEE802154, &ro->dst_addr, |
| 258 | ro->bound ? &ro->src_addr : NULL, size); |
| 259 | if (err < 0) |
| 260 | goto out_skb; |
| 261 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 262 | err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size); |
| 263 | if (err < 0) |
| 264 | goto out_skb; |
| 265 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 266 | skb->dev = dev; |
| 267 | skb->sk = sk; |
| 268 | skb->protocol = htons(ETH_P_IEEE802154); |
| 269 | |
| 270 | dev_put(dev); |
| 271 | |
| 272 | err = dev_queue_xmit(skb); |
| 273 | if (err > 0) |
| 274 | err = net_xmit_errno(err); |
| 275 | |
| 276 | return err ?: size; |
| 277 | |
| 278 | out_skb: |
| 279 | kfree_skb(skb); |
| 280 | out_dev: |
| 281 | dev_put(dev); |
| 282 | out: |
| 283 | return err; |
| 284 | } |
| 285 | |
| 286 | static int dgram_recvmsg(struct kiocb *iocb, struct sock *sk, |
| 287 | struct msghdr *msg, size_t len, int noblock, int flags, |
| 288 | int *addr_len) |
| 289 | { |
| 290 | size_t copied = 0; |
| 291 | int err = -EOPNOTSUPP; |
| 292 | struct sk_buff *skb; |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 293 | DECLARE_SOCKADDR(struct sockaddr_ieee802154 *, saddr, msg->msg_name); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 294 | |
| 295 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 296 | if (!skb) |
| 297 | goto out; |
| 298 | |
| 299 | copied = skb->len; |
| 300 | if (len < copied) { |
| 301 | msg->msg_flags |= MSG_TRUNC; |
| 302 | copied = len; |
| 303 | } |
| 304 | |
| 305 | /* FIXME: skip headers if necessary ?! */ |
| 306 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 307 | if (err) |
| 308 | goto done; |
| 309 | |
Neil Horman | 3b88578 | 2009-10-12 13:26:31 -0700 | [diff] [blame] | 310 | sock_recv_ts_and_drops(msg, sk, skb); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 311 | |
Stephen Röttger | 6364e6e | 2013-03-26 12:41:29 +0000 | [diff] [blame] | 312 | if (saddr) { |
| 313 | saddr->family = AF_IEEE802154; |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 314 | ieee802154_addr_to_sa(&saddr->addr, &mac_cb(skb)->source); |
Stephen Röttger | 6364e6e | 2013-03-26 12:41:29 +0000 | [diff] [blame] | 315 | *addr_len = sizeof(*saddr); |
Hannes Frederic Sowa | bceaa90 | 2013-11-18 04:20:45 +0100 | [diff] [blame] | 316 | } |
Stephen Röttger | 6364e6e | 2013-03-26 12:41:29 +0000 | [diff] [blame] | 317 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 318 | if (flags & MSG_TRUNC) |
| 319 | copied = skb->len; |
| 320 | done: |
| 321 | skb_free_datagram(sk, skb); |
| 322 | out: |
| 323 | if (err) |
| 324 | return err; |
| 325 | return copied; |
| 326 | } |
| 327 | |
| 328 | static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 329 | { |
Phoebe Buckheister | 8cfad49 | 2014-03-17 18:30:19 +0100 | [diff] [blame] | 330 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 331 | if (!skb) |
| 332 | return NET_RX_DROP; |
| 333 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 334 | if (sock_queue_rcv_skb(sk, skb) < 0) { |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 335 | kfree_skb(skb); |
| 336 | return NET_RX_DROP; |
| 337 | } |
| 338 | |
| 339 | return NET_RX_SUCCESS; |
| 340 | } |
| 341 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 342 | static inline bool |
| 343 | ieee802154_match_sock(__le64 hw_addr, __le16 pan_id, __le16 short_addr, |
| 344 | struct dgram_sock *ro) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 345 | { |
| 346 | if (!ro->bound) |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 347 | return true; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 348 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 349 | if (ro->src_addr.mode == IEEE802154_ADDR_LONG && |
| 350 | hw_addr == ro->src_addr.extended_addr) |
| 351 | return true; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 352 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 353 | if (ro->src_addr.mode == IEEE802154_ADDR_SHORT && |
| 354 | pan_id == ro->src_addr.pan_id && |
| 355 | short_addr == ro->src_addr.short_addr) |
| 356 | return true; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 357 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 358 | return false; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb) |
| 362 | { |
| 363 | struct sock *sk, *prev = NULL; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 364 | int ret = NET_RX_SUCCESS; |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 365 | __le16 pan_id, short_addr; |
| 366 | __le64 hw_addr; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 367 | |
| 368 | /* Data frame processing */ |
| 369 | BUG_ON(dev->type != ARPHRD_IEEE802154); |
| 370 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 371 | pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev); |
| 372 | short_addr = ieee802154_mlme_ops(dev)->get_short_addr(dev); |
| 373 | hw_addr = ieee802154_devaddr_from_raw(dev->dev_addr); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 374 | |
| 375 | read_lock(&dgram_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 376 | sk_for_each(sk, &dgram_head) { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 377 | if (ieee802154_match_sock(hw_addr, pan_id, short_addr, |
| 378 | dgram_sk(sk))) { |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 379 | if (prev) { |
| 380 | struct sk_buff *clone; |
| 381 | clone = skb_clone(skb, GFP_ATOMIC); |
| 382 | if (clone) |
| 383 | dgram_rcv_skb(prev, clone); |
| 384 | } |
| 385 | |
| 386 | prev = sk; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | if (prev) |
| 391 | dgram_rcv_skb(prev, skb); |
| 392 | else { |
| 393 | kfree_skb(skb); |
| 394 | ret = NET_RX_DROP; |
| 395 | } |
| 396 | read_unlock(&dgram_lock); |
| 397 | |
| 398 | return ret; |
| 399 | } |
| 400 | |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 401 | static int dgram_getsockopt(struct sock *sk, int level, int optname, |
| 402 | char __user *optval, int __user *optlen) |
| 403 | { |
Dmitry Baryshkov | 99eb855 | 2009-08-07 02:58:43 +0000 | [diff] [blame] | 404 | struct dgram_sock *ro = dgram_sk(sk); |
| 405 | |
| 406 | int val, len; |
| 407 | |
| 408 | if (level != SOL_IEEE802154) |
| 409 | return -EOPNOTSUPP; |
| 410 | |
| 411 | if (get_user(len, optlen)) |
| 412 | return -EFAULT; |
| 413 | |
| 414 | len = min_t(unsigned int, len, sizeof(int)); |
| 415 | |
| 416 | switch (optname) { |
| 417 | case WPAN_WANTACK: |
| 418 | val = ro->want_ack; |
| 419 | break; |
| 420 | default: |
| 421 | return -ENOPROTOOPT; |
| 422 | } |
| 423 | |
| 424 | if (put_user(len, optlen)) |
| 425 | return -EFAULT; |
| 426 | if (copy_to_user(optval, &val, len)) |
| 427 | return -EFAULT; |
| 428 | return 0; |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | static int dgram_setsockopt(struct sock *sk, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 432 | char __user *optval, unsigned int optlen) |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 433 | { |
Dmitry Baryshkov | 99eb855 | 2009-08-07 02:58:43 +0000 | [diff] [blame] | 434 | struct dgram_sock *ro = dgram_sk(sk); |
| 435 | int val; |
| 436 | int err = 0; |
| 437 | |
| 438 | if (optlen < sizeof(int)) |
| 439 | return -EINVAL; |
| 440 | |
| 441 | if (get_user(val, (int __user *)optval)) |
| 442 | return -EFAULT; |
| 443 | |
| 444 | lock_sock(sk); |
| 445 | |
| 446 | switch (optname) { |
| 447 | case WPAN_WANTACK: |
| 448 | ro->want_ack = !!val; |
| 449 | break; |
| 450 | default: |
| 451 | err = -ENOPROTOOPT; |
| 452 | break; |
| 453 | } |
| 454 | |
| 455 | release_sock(sk); |
| 456 | return err; |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 457 | } |
| 458 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 459 | struct proto ieee802154_dgram_prot = { |
| 460 | .name = "IEEE-802.15.4-MAC", |
| 461 | .owner = THIS_MODULE, |
| 462 | .obj_size = sizeof(struct dgram_sock), |
| 463 | .init = dgram_init, |
| 464 | .close = dgram_close, |
| 465 | .bind = dgram_bind, |
| 466 | .sendmsg = dgram_sendmsg, |
| 467 | .recvmsg = dgram_recvmsg, |
| 468 | .hash = dgram_hash, |
| 469 | .unhash = dgram_unhash, |
| 470 | .connect = dgram_connect, |
| 471 | .disconnect = dgram_disconnect, |
| 472 | .ioctl = dgram_ioctl, |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 473 | .getsockopt = dgram_getsockopt, |
| 474 | .setsockopt = dgram_setsockopt, |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 475 | }; |
| 476 | |