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