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 | * |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 15 | * Written by: |
| 16 | * Sergey Lapin <slapin@ossfans.org> |
| 17 | * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> |
| 18 | */ |
| 19 | |
Phoebe Buckheister | af9eed5 | 2014-05-16 17:46:41 +0200 | [diff] [blame] | 20 | #include <linux/capability.h> |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 21 | #include <linux/net.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/if_arp.h> |
| 24 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Alexander Aring | 4ca24ac | 2014-10-25 09:41:04 +0200 | [diff] [blame] | 26 | #include <linux/ieee802154.h> |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 27 | #include <net/sock.h> |
Dmitry Eremin-Solenikov | f0166e5 | 2009-07-23 16:56:29 +0400 | [diff] [blame] | 28 | #include <net/af_ieee802154.h> |
Dmitry Eremin-Solenikov | f0166e5 | 2009-07-23 16:56:29 +0400 | [diff] [blame] | 29 | #include <net/ieee802154_netdev.h> |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 30 | |
| 31 | #include <asm/ioctls.h> |
| 32 | |
| 33 | #include "af802154.h" |
| 34 | |
| 35 | static HLIST_HEAD(dgram_head); |
| 36 | static DEFINE_RWLOCK(dgram_lock); |
| 37 | |
| 38 | struct dgram_sock { |
| 39 | struct sock sk; |
| 40 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 41 | struct ieee802154_addr src_addr; |
| 42 | struct ieee802154_addr dst_addr; |
Dmitry Baryshkov | 74eda55 | 2009-08-07 02:58:42 +0000 | [diff] [blame] | 43 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 44 | unsigned int bound:1; |
Phoebe Buckheister | 1cc76e3 | 2014-05-14 17:43:10 +0200 | [diff] [blame] | 45 | unsigned int connected:1; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 46 | unsigned int want_ack:1; |
Phoebe Buckheister | af9eed5 | 2014-05-16 17:46:41 +0200 | [diff] [blame] | 47 | unsigned int secen:1; |
| 48 | unsigned int secen_override:1; |
| 49 | unsigned int seclevel:3; |
| 50 | unsigned int seclevel_override:1; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | static inline struct dgram_sock *dgram_sk(const struct sock *sk) |
| 54 | { |
| 55 | return container_of(sk, struct dgram_sock, sk); |
| 56 | } |
| 57 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 58 | static void dgram_hash(struct sock *sk) |
| 59 | { |
| 60 | write_lock_bh(&dgram_lock); |
| 61 | sk_add_node(sk, &dgram_head); |
| 62 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
| 63 | write_unlock_bh(&dgram_lock); |
| 64 | } |
| 65 | |
| 66 | static void dgram_unhash(struct sock *sk) |
| 67 | { |
| 68 | write_lock_bh(&dgram_lock); |
| 69 | if (sk_del_node_init(sk)) |
| 70 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
| 71 | write_unlock_bh(&dgram_lock); |
| 72 | } |
| 73 | |
| 74 | static int dgram_init(struct sock *sk) |
| 75 | { |
| 76 | struct dgram_sock *ro = dgram_sk(sk); |
| 77 | |
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 | 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; |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 90 | struct ieee802154_addr haddr; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 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 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 105 | ieee802154_addr_from_sa(&haddr, &addr->addr); |
| 106 | dev = ieee802154_get_dev(sock_net(sk), &haddr); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 107 | if (!dev) { |
| 108 | err = -ENODEV; |
| 109 | goto out; |
| 110 | } |
| 111 | |
| 112 | if (dev->type != ARPHRD_IEEE802154) { |
| 113 | err = -ENODEV; |
| 114 | goto out_put; |
| 115 | } |
| 116 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 117 | ro->src_addr = haddr; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 118 | |
| 119 | ro->bound = 1; |
Dmitry Baryshkov | 74eda55 | 2009-08-07 02:58:42 +0000 | [diff] [blame] | 120 | err = 0; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 121 | out_put: |
| 122 | dev_put(dev); |
| 123 | out: |
| 124 | release_sock(sk); |
| 125 | |
| 126 | return err; |
| 127 | } |
| 128 | |
| 129 | static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg) |
| 130 | { |
| 131 | switch (cmd) { |
| 132 | case SIOCOUTQ: |
| 133 | { |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 134 | int amount = sk_wmem_alloc_get(sk); |
| 135 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 136 | return put_user(amount, (int __user *)arg); |
| 137 | } |
| 138 | |
| 139 | case SIOCINQ: |
| 140 | { |
| 141 | struct sk_buff *skb; |
| 142 | unsigned long amount; |
| 143 | |
| 144 | amount = 0; |
| 145 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 146 | skb = skb_peek(&sk->sk_receive_queue); |
| 147 | if (skb != NULL) { |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 148 | /* We will only return the amount |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 149 | * of this packet since that is all |
| 150 | * that will be read. |
| 151 | */ |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 152 | amount = skb->len - ieee802154_hdr_length(skb); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 153 | } |
| 154 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 155 | return put_user(amount, (int __user *)arg); |
| 156 | } |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 157 | } |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 158 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 159 | return -ENOIOCTLCMD; |
| 160 | } |
| 161 | |
| 162 | /* FIXME: autobind */ |
| 163 | static int dgram_connect(struct sock *sk, struct sockaddr *uaddr, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 164 | int len) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 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 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 183 | ieee802154_addr_from_sa(&ro->dst_addr, &addr->addr); |
Phoebe Buckheister | 1cc76e3 | 2014-05-14 17:43:10 +0200 | [diff] [blame] | 184 | ro->connected = 1; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 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); |
Phoebe Buckheister | 1cc76e3 | 2014-05-14 17:43:10 +0200 | [diff] [blame] | 196 | ro->connected = 0; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 197 | release_sock(sk); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int dgram_sendmsg(struct kiocb *iocb, struct sock *sk, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 203 | struct msghdr *msg, size_t size) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 204 | { |
| 205 | struct net_device *dev; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 206 | unsigned int mtu; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 207 | struct sk_buff *skb; |
Phoebe Buckheister | 32edc40 | 2014-05-14 17:43:08 +0200 | [diff] [blame] | 208 | struct ieee802154_mac_cb *cb; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 209 | struct dgram_sock *ro = dgram_sk(sk); |
Phoebe Buckheister | 1cc76e3 | 2014-05-14 17:43:10 +0200 | [diff] [blame] | 210 | struct ieee802154_addr dst_addr; |
Herbert Xu | ae64194 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 211 | int hlen, tlen; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 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 | |
Phoebe Buckheister | 1cc76e3 | 2014-05-14 17:43:10 +0200 | [diff] [blame] | 219 | if (!ro->connected && !msg->msg_name) |
| 220 | return -EDESTADDRREQ; |
| 221 | else if (ro->connected && msg->msg_name) |
| 222 | return -EISCONN; |
| 223 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 224 | if (!ro->bound) |
| 225 | dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154); |
| 226 | else |
| 227 | dev = ieee802154_get_dev(sock_net(sk), &ro->src_addr); |
| 228 | |
| 229 | if (!dev) { |
| 230 | pr_debug("no dev\n"); |
| 231 | err = -ENXIO; |
| 232 | goto out; |
| 233 | } |
| 234 | mtu = dev->mtu; |
| 235 | pr_debug("name = %s, mtu = %u\n", dev->name, mtu); |
| 236 | |
Sasha Levin | 3da947b | 2012-07-02 01:29:55 +0000 | [diff] [blame] | 237 | if (size > mtu) { |
| 238 | pr_debug("size = %Zu, mtu = %u\n", size, mtu); |
Phoebe Buckheister | d1d7358 | 2014-03-14 21:24:04 +0100 | [diff] [blame] | 239 | err = -EMSGSIZE; |
Sasha Levin | 3da947b | 2012-07-02 01:29:55 +0000 | [diff] [blame] | 240 | goto out_dev; |
| 241 | } |
| 242 | |
Herbert Xu | ae64194 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 243 | hlen = LL_RESERVED_SPACE(dev); |
| 244 | tlen = dev->needed_tailroom; |
| 245 | skb = sock_alloc_send_skb(sk, hlen + tlen + size, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 246 | msg->msg_flags & MSG_DONTWAIT, |
| 247 | &err); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 248 | if (!skb) |
| 249 | goto out_dev; |
| 250 | |
Herbert Xu | ae64194 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 251 | skb_reserve(skb, hlen); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 252 | |
| 253 | skb_reset_network_header(skb); |
| 254 | |
Phoebe Buckheister | 32edc40 | 2014-05-14 17:43:08 +0200 | [diff] [blame] | 255 | cb = mac_cb_init(skb); |
| 256 | cb->type = IEEE802154_FC_TYPE_DATA; |
| 257 | cb->ackreq = ro->want_ack; |
Dmitry Baryshkov | 99eb855 | 2009-08-07 02:58:43 +0000 | [diff] [blame] | 258 | |
Phoebe Buckheister | 1cc76e3 | 2014-05-14 17:43:10 +0200 | [diff] [blame] | 259 | if (msg->msg_name) { |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 260 | DECLARE_SOCKADDR(struct sockaddr_ieee802154*, |
| 261 | daddr, msg->msg_name); |
Phoebe Buckheister | 1cc76e3 | 2014-05-14 17:43:10 +0200 | [diff] [blame] | 262 | |
| 263 | ieee802154_addr_from_sa(&dst_addr, &daddr->addr); |
| 264 | } else { |
| 265 | dst_addr = ro->dst_addr; |
| 266 | } |
| 267 | |
Phoebe Buckheister | af9eed5 | 2014-05-16 17:46:41 +0200 | [diff] [blame] | 268 | cb->secen = ro->secen; |
| 269 | cb->secen_override = ro->secen_override; |
| 270 | cb->seclevel = ro->seclevel; |
| 271 | cb->seclevel_override = ro->seclevel_override; |
| 272 | |
Phoebe Buckheister | 1cc76e3 | 2014-05-14 17:43:10 +0200 | [diff] [blame] | 273 | err = dev_hard_header(skb, dev, ETH_P_IEEE802154, &dst_addr, |
| 274 | ro->bound ? &ro->src_addr : NULL, size); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 275 | if (err < 0) |
| 276 | goto out_skb; |
| 277 | |
Al Viro | 6ce8e9c | 2014-04-06 21:25:44 -0400 | [diff] [blame] | 278 | err = memcpy_from_msg(skb_put(skb, size), msg, size); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 279 | if (err < 0) |
| 280 | goto out_skb; |
| 281 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 282 | skb->dev = dev; |
| 283 | skb->sk = sk; |
| 284 | skb->protocol = htons(ETH_P_IEEE802154); |
| 285 | |
| 286 | dev_put(dev); |
| 287 | |
| 288 | err = dev_queue_xmit(skb); |
| 289 | if (err > 0) |
| 290 | err = net_xmit_errno(err); |
| 291 | |
| 292 | return err ?: size; |
| 293 | |
| 294 | out_skb: |
| 295 | kfree_skb(skb); |
| 296 | out_dev: |
| 297 | dev_put(dev); |
| 298 | out: |
| 299 | return err; |
| 300 | } |
| 301 | |
| 302 | static int dgram_recvmsg(struct kiocb *iocb, struct sock *sk, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 303 | struct msghdr *msg, size_t len, int noblock, |
| 304 | int flags, int *addr_len) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 305 | { |
| 306 | size_t copied = 0; |
| 307 | int err = -EOPNOTSUPP; |
| 308 | struct sk_buff *skb; |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 309 | DECLARE_SOCKADDR(struct sockaddr_ieee802154 *, saddr, msg->msg_name); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 310 | |
| 311 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 312 | if (!skb) |
| 313 | goto out; |
| 314 | |
| 315 | copied = skb->len; |
| 316 | if (len < copied) { |
| 317 | msg->msg_flags |= MSG_TRUNC; |
| 318 | copied = len; |
| 319 | } |
| 320 | |
| 321 | /* FIXME: skip headers if necessary ?! */ |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 322 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 323 | if (err) |
| 324 | goto done; |
| 325 | |
Neil Horman | 3b88578 | 2009-10-12 13:26:31 -0700 | [diff] [blame] | 326 | sock_recv_ts_and_drops(msg, sk, skb); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 327 | |
Stephen Röttger | 6364e6e | 2013-03-26 12:41:29 +0000 | [diff] [blame] | 328 | if (saddr) { |
| 329 | saddr->family = AF_IEEE802154; |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 330 | ieee802154_addr_to_sa(&saddr->addr, &mac_cb(skb)->source); |
Stephen Röttger | 6364e6e | 2013-03-26 12:41:29 +0000 | [diff] [blame] | 331 | *addr_len = sizeof(*saddr); |
Hannes Frederic Sowa | bceaa90 | 2013-11-18 04:20:45 +0100 | [diff] [blame] | 332 | } |
Stephen Röttger | 6364e6e | 2013-03-26 12:41:29 +0000 | [diff] [blame] | 333 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 334 | if (flags & MSG_TRUNC) |
| 335 | copied = skb->len; |
| 336 | done: |
| 337 | skb_free_datagram(sk, skb); |
| 338 | out: |
| 339 | if (err) |
| 340 | return err; |
| 341 | return copied; |
| 342 | } |
| 343 | |
| 344 | static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 345 | { |
Phoebe Buckheister | 8cfad49 | 2014-03-17 18:30:19 +0100 | [diff] [blame] | 346 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 347 | if (!skb) |
| 348 | return NET_RX_DROP; |
| 349 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 350 | if (sock_queue_rcv_skb(sk, skb) < 0) { |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 351 | kfree_skb(skb); |
| 352 | return NET_RX_DROP; |
| 353 | } |
| 354 | |
| 355 | return NET_RX_SUCCESS; |
| 356 | } |
| 357 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 358 | static inline bool |
| 359 | ieee802154_match_sock(__le64 hw_addr, __le16 pan_id, __le16 short_addr, |
| 360 | struct dgram_sock *ro) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 361 | { |
| 362 | if (!ro->bound) |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 363 | return true; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 364 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 365 | if (ro->src_addr.mode == IEEE802154_ADDR_LONG && |
| 366 | hw_addr == ro->src_addr.extended_addr) |
| 367 | return true; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 368 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 369 | if (ro->src_addr.mode == IEEE802154_ADDR_SHORT && |
| 370 | pan_id == ro->src_addr.pan_id && |
| 371 | short_addr == ro->src_addr.short_addr) |
| 372 | return true; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 373 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 374 | return false; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb) |
| 378 | { |
| 379 | struct sock *sk, *prev = NULL; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 380 | int ret = NET_RX_SUCCESS; |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 381 | __le16 pan_id, short_addr; |
| 382 | __le64 hw_addr; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 383 | |
| 384 | /* Data frame processing */ |
| 385 | BUG_ON(dev->type != ARPHRD_IEEE802154); |
| 386 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 387 | pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev); |
| 388 | short_addr = ieee802154_mlme_ops(dev)->get_short_addr(dev); |
| 389 | hw_addr = ieee802154_devaddr_from_raw(dev->dev_addr); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 390 | |
| 391 | read_lock(&dgram_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 392 | sk_for_each(sk, &dgram_head) { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 393 | if (ieee802154_match_sock(hw_addr, pan_id, short_addr, |
| 394 | dgram_sk(sk))) { |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 395 | if (prev) { |
| 396 | struct sk_buff *clone; |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 397 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 398 | clone = skb_clone(skb, GFP_ATOMIC); |
| 399 | if (clone) |
| 400 | dgram_rcv_skb(prev, clone); |
| 401 | } |
| 402 | |
| 403 | prev = sk; |
| 404 | } |
| 405 | } |
| 406 | |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 407 | if (prev) { |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 408 | dgram_rcv_skb(prev, skb); |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 409 | } else { |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 410 | kfree_skb(skb); |
| 411 | ret = NET_RX_DROP; |
| 412 | } |
| 413 | read_unlock(&dgram_lock); |
| 414 | |
| 415 | return ret; |
| 416 | } |
| 417 | |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 418 | static int dgram_getsockopt(struct sock *sk, int level, int optname, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 419 | char __user *optval, int __user *optlen) |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 420 | { |
Dmitry Baryshkov | 99eb855 | 2009-08-07 02:58:43 +0000 | [diff] [blame] | 421 | struct dgram_sock *ro = dgram_sk(sk); |
| 422 | |
| 423 | int val, len; |
| 424 | |
| 425 | if (level != SOL_IEEE802154) |
| 426 | return -EOPNOTSUPP; |
| 427 | |
| 428 | if (get_user(len, optlen)) |
| 429 | return -EFAULT; |
| 430 | |
| 431 | len = min_t(unsigned int, len, sizeof(int)); |
| 432 | |
| 433 | switch (optname) { |
| 434 | case WPAN_WANTACK: |
| 435 | val = ro->want_ack; |
| 436 | break; |
Phoebe Buckheister | af9eed5 | 2014-05-16 17:46:41 +0200 | [diff] [blame] | 437 | case WPAN_SECURITY: |
| 438 | if (!ro->secen_override) |
| 439 | val = WPAN_SECURITY_DEFAULT; |
| 440 | else if (ro->secen) |
| 441 | val = WPAN_SECURITY_ON; |
| 442 | else |
| 443 | val = WPAN_SECURITY_OFF; |
| 444 | break; |
| 445 | case WPAN_SECURITY_LEVEL: |
| 446 | if (!ro->seclevel_override) |
| 447 | val = WPAN_SECURITY_LEVEL_DEFAULT; |
| 448 | else |
| 449 | val = ro->seclevel; |
| 450 | break; |
Dmitry Baryshkov | 99eb855 | 2009-08-07 02:58:43 +0000 | [diff] [blame] | 451 | default: |
| 452 | return -ENOPROTOOPT; |
| 453 | } |
| 454 | |
| 455 | if (put_user(len, optlen)) |
| 456 | return -EFAULT; |
| 457 | if (copy_to_user(optval, &val, len)) |
| 458 | return -EFAULT; |
| 459 | return 0; |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static int dgram_setsockopt(struct sock *sk, int level, int optname, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 463 | char __user *optval, unsigned int optlen) |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 464 | { |
Dmitry Baryshkov | 99eb855 | 2009-08-07 02:58:43 +0000 | [diff] [blame] | 465 | struct dgram_sock *ro = dgram_sk(sk); |
Phoebe Buckheister | af9eed5 | 2014-05-16 17:46:41 +0200 | [diff] [blame] | 466 | struct net *net = sock_net(sk); |
Dmitry Baryshkov | 99eb855 | 2009-08-07 02:58:43 +0000 | [diff] [blame] | 467 | int val; |
| 468 | int err = 0; |
| 469 | |
| 470 | if (optlen < sizeof(int)) |
| 471 | return -EINVAL; |
| 472 | |
| 473 | if (get_user(val, (int __user *)optval)) |
| 474 | return -EFAULT; |
| 475 | |
| 476 | lock_sock(sk); |
| 477 | |
| 478 | switch (optname) { |
| 479 | case WPAN_WANTACK: |
| 480 | ro->want_ack = !!val; |
| 481 | break; |
Phoebe Buckheister | af9eed5 | 2014-05-16 17:46:41 +0200 | [diff] [blame] | 482 | case WPAN_SECURITY: |
| 483 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN) && |
| 484 | !ns_capable(net->user_ns, CAP_NET_RAW)) { |
| 485 | err = -EPERM; |
| 486 | break; |
| 487 | } |
| 488 | |
| 489 | switch (val) { |
| 490 | case WPAN_SECURITY_DEFAULT: |
| 491 | ro->secen_override = 0; |
| 492 | break; |
| 493 | case WPAN_SECURITY_ON: |
| 494 | ro->secen_override = 1; |
| 495 | ro->secen = 1; |
| 496 | break; |
| 497 | case WPAN_SECURITY_OFF: |
| 498 | ro->secen_override = 1; |
| 499 | ro->secen = 0; |
| 500 | break; |
| 501 | default: |
| 502 | err = -EINVAL; |
| 503 | break; |
| 504 | } |
| 505 | break; |
| 506 | case WPAN_SECURITY_LEVEL: |
| 507 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN) && |
| 508 | !ns_capable(net->user_ns, CAP_NET_RAW)) { |
| 509 | err = -EPERM; |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | if (val < WPAN_SECURITY_LEVEL_DEFAULT || |
| 514 | val > IEEE802154_SCF_SECLEVEL_ENC_MIC128) { |
| 515 | err = -EINVAL; |
| 516 | } else if (val == WPAN_SECURITY_LEVEL_DEFAULT) { |
| 517 | ro->seclevel_override = 0; |
| 518 | } else { |
| 519 | ro->seclevel_override = 1; |
| 520 | ro->seclevel = val; |
| 521 | } |
| 522 | break; |
Dmitry Baryshkov | 99eb855 | 2009-08-07 02:58:43 +0000 | [diff] [blame] | 523 | default: |
| 524 | err = -ENOPROTOOPT; |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | release_sock(sk); |
| 529 | return err; |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 530 | } |
| 531 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 532 | struct proto ieee802154_dgram_prot = { |
| 533 | .name = "IEEE-802.15.4-MAC", |
| 534 | .owner = THIS_MODULE, |
| 535 | .obj_size = sizeof(struct dgram_sock), |
| 536 | .init = dgram_init, |
| 537 | .close = dgram_close, |
| 538 | .bind = dgram_bind, |
| 539 | .sendmsg = dgram_sendmsg, |
| 540 | .recvmsg = dgram_recvmsg, |
| 541 | .hash = dgram_hash, |
| 542 | .unhash = dgram_unhash, |
| 543 | .connect = dgram_connect, |
| 544 | .disconnect = dgram_disconnect, |
| 545 | .ioctl = dgram_ioctl, |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 546 | .getsockopt = dgram_getsockopt, |
| 547 | .setsockopt = dgram_setsockopt, |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 548 | }; |
| 549 | |