Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Raw IEEE 802.15.4 sockets |
| 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 | |
| 20 | #include <linux/net.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/if_arp.h> |
| 23 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 25 | #include <net/sock.h> |
Dmitry Eremin-Solenikov | f0166e5 | 2009-07-23 16:56:29 +0400 | [diff] [blame] | 26 | #include <net/af_ieee802154.h> |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 27 | #include <net/ieee802154_netdev.h> |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 28 | |
| 29 | #include "af802154.h" |
| 30 | |
| 31 | static HLIST_HEAD(raw_head); |
| 32 | static DEFINE_RWLOCK(raw_lock); |
| 33 | |
| 34 | static void raw_hash(struct sock *sk) |
| 35 | { |
| 36 | write_lock_bh(&raw_lock); |
| 37 | sk_add_node(sk, &raw_head); |
| 38 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
| 39 | write_unlock_bh(&raw_lock); |
| 40 | } |
| 41 | |
| 42 | static void raw_unhash(struct sock *sk) |
| 43 | { |
| 44 | write_lock_bh(&raw_lock); |
| 45 | if (sk_del_node_init(sk)) |
| 46 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
| 47 | write_unlock_bh(&raw_lock); |
| 48 | } |
| 49 | |
| 50 | static void raw_close(struct sock *sk, long timeout) |
| 51 | { |
| 52 | sk_common_release(sk); |
| 53 | } |
| 54 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 55 | static int raw_bind(struct sock *sk, struct sockaddr *_uaddr, int len) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 56 | { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 57 | struct ieee802154_addr addr; |
| 58 | struct sockaddr_ieee802154 *uaddr = (struct sockaddr_ieee802154 *)_uaddr; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 59 | int err = 0; |
| 60 | struct net_device *dev = NULL; |
| 61 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 62 | if (len < sizeof(*uaddr)) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 63 | return -EINVAL; |
| 64 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 65 | uaddr = (struct sockaddr_ieee802154 *)_uaddr; |
| 66 | if (uaddr->family != AF_IEEE802154) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 67 | return -EINVAL; |
| 68 | |
| 69 | lock_sock(sk); |
| 70 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 71 | ieee802154_addr_from_sa(&addr, &uaddr->addr); |
| 72 | dev = ieee802154_get_dev(sock_net(sk), &addr); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 73 | if (!dev) { |
| 74 | err = -ENODEV; |
| 75 | goto out; |
| 76 | } |
| 77 | |
Dmitry Eremin-Solenikov | 929122cd | 2009-08-14 20:00:20 +0400 | [diff] [blame] | 78 | if (dev->type != ARPHRD_IEEE802154) { |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 79 | err = -ENODEV; |
| 80 | goto out_put; |
| 81 | } |
| 82 | |
| 83 | sk->sk_bound_dev_if = dev->ifindex; |
| 84 | sk_dst_reset(sk); |
| 85 | |
| 86 | out_put: |
| 87 | dev_put(dev); |
| 88 | out: |
| 89 | release_sock(sk); |
| 90 | |
| 91 | return err; |
| 92 | } |
| 93 | |
| 94 | static int raw_connect(struct sock *sk, struct sockaddr *uaddr, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 95 | int addr_len) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 96 | { |
| 97 | return -ENOTSUPP; |
| 98 | } |
| 99 | |
| 100 | static int raw_disconnect(struct sock *sk, int flags) |
| 101 | { |
| 102 | return 0; |
| 103 | } |
| 104 | |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 105 | static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, |
| 106 | struct msghdr *msg, size_t size) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 107 | { |
| 108 | struct net_device *dev; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 109 | unsigned int mtu; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 110 | struct sk_buff *skb; |
Herbert Xu | ae64194 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 111 | int hlen, tlen; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 112 | int err; |
| 113 | |
| 114 | if (msg->msg_flags & MSG_OOB) { |
| 115 | pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags); |
| 116 | return -EOPNOTSUPP; |
| 117 | } |
| 118 | |
| 119 | lock_sock(sk); |
| 120 | if (!sk->sk_bound_dev_if) |
| 121 | dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154); |
| 122 | else |
| 123 | dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if); |
| 124 | release_sock(sk); |
| 125 | |
| 126 | if (!dev) { |
| 127 | pr_debug("no dev\n"); |
| 128 | err = -ENXIO; |
| 129 | goto out; |
| 130 | } |
| 131 | |
| 132 | mtu = dev->mtu; |
| 133 | pr_debug("name = %s, mtu = %u\n", dev->name, mtu); |
| 134 | |
| 135 | if (size > mtu) { |
David S. Miller | e5241c4 | 2009-06-11 02:10:19 -0700 | [diff] [blame] | 136 | pr_debug("size = %Zu, mtu = %u\n", size, mtu); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 137 | err = -EINVAL; |
| 138 | goto out_dev; |
| 139 | } |
| 140 | |
Herbert Xu | ae64194 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 141 | hlen = LL_RESERVED_SPACE(dev); |
| 142 | tlen = dev->needed_tailroom; |
| 143 | skb = sock_alloc_send_skb(sk, hlen + tlen + size, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 144 | msg->msg_flags & MSG_DONTWAIT, &err); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 145 | if (!skb) |
| 146 | goto out_dev; |
| 147 | |
Herbert Xu | ae64194 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 148 | skb_reserve(skb, hlen); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 149 | |
| 150 | skb_reset_mac_header(skb); |
| 151 | skb_reset_network_header(skb); |
| 152 | |
Al Viro | 6ce8e9c | 2014-04-06 21:25:44 -0400 | [diff] [blame] | 153 | err = memcpy_from_msg(skb_put(skb, size), msg, size); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 154 | if (err < 0) |
| 155 | goto out_skb; |
| 156 | |
| 157 | skb->dev = dev; |
| 158 | skb->sk = sk; |
| 159 | skb->protocol = htons(ETH_P_IEEE802154); |
| 160 | |
| 161 | dev_put(dev); |
| 162 | |
| 163 | err = dev_queue_xmit(skb); |
| 164 | if (err > 0) |
| 165 | err = net_xmit_errno(err); |
| 166 | |
| 167 | return err ?: size; |
| 168 | |
| 169 | out_skb: |
| 170 | kfree_skb(skb); |
| 171 | out_dev: |
| 172 | dev_put(dev); |
| 173 | out: |
| 174 | return err; |
| 175 | } |
| 176 | |
| 177 | static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, |
| 178 | size_t len, int noblock, int flags, int *addr_len) |
| 179 | { |
| 180 | size_t copied = 0; |
| 181 | int err = -EOPNOTSUPP; |
| 182 | struct sk_buff *skb; |
| 183 | |
| 184 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 185 | if (!skb) |
| 186 | goto out; |
| 187 | |
| 188 | copied = skb->len; |
| 189 | if (len < copied) { |
| 190 | msg->msg_flags |= MSG_TRUNC; |
| 191 | copied = len; |
| 192 | } |
| 193 | |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 194 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 195 | if (err) |
| 196 | goto done; |
| 197 | |
Neil Horman | 3b88578 | 2009-10-12 13:26:31 -0700 | [diff] [blame] | 198 | sock_recv_ts_and_drops(msg, sk, skb); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 199 | |
| 200 | if (flags & MSG_TRUNC) |
| 201 | copied = skb->len; |
| 202 | done: |
| 203 | skb_free_datagram(sk, skb); |
| 204 | out: |
| 205 | if (err) |
| 206 | return err; |
| 207 | return copied; |
| 208 | } |
| 209 | |
| 210 | static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 211 | { |
Phoebe Buckheister | 8cfad49 | 2014-03-17 18:30:19 +0100 | [diff] [blame] | 212 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 213 | if (!skb) |
| 214 | return NET_RX_DROP; |
| 215 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 216 | if (sock_queue_rcv_skb(sk, skb) < 0) { |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 217 | kfree_skb(skb); |
| 218 | return NET_RX_DROP; |
| 219 | } |
| 220 | |
| 221 | return NET_RX_SUCCESS; |
| 222 | } |
| 223 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 224 | void ieee802154_raw_deliver(struct net_device *dev, struct sk_buff *skb) |
| 225 | { |
| 226 | struct sock *sk; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 227 | |
| 228 | read_lock(&raw_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 229 | sk_for_each(sk, &raw_head) { |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 230 | bh_lock_sock(sk); |
| 231 | if (!sk->sk_bound_dev_if || |
| 232 | sk->sk_bound_dev_if == dev->ifindex) { |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 233 | struct sk_buff *clone; |
| 234 | |
| 235 | clone = skb_clone(skb, GFP_ATOMIC); |
| 236 | if (clone) |
| 237 | raw_rcv_skb(sk, clone); |
| 238 | } |
| 239 | bh_unlock_sock(sk); |
| 240 | } |
| 241 | read_unlock(&raw_lock); |
| 242 | } |
| 243 | |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 244 | static int raw_getsockopt(struct sock *sk, int level, int optname, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 245 | char __user *optval, int __user *optlen) |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 246 | { |
| 247 | return -EOPNOTSUPP; |
| 248 | } |
| 249 | |
| 250 | static int raw_setsockopt(struct sock *sk, int level, int optname, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 251 | char __user *optval, unsigned int optlen) |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 252 | { |
| 253 | return -EOPNOTSUPP; |
| 254 | } |
| 255 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 256 | struct proto ieee802154_raw_prot = { |
| 257 | .name = "IEEE-802.15.4-RAW", |
| 258 | .owner = THIS_MODULE, |
| 259 | .obj_size = sizeof(struct sock), |
| 260 | .close = raw_close, |
| 261 | .bind = raw_bind, |
| 262 | .sendmsg = raw_sendmsg, |
| 263 | .recvmsg = raw_recvmsg, |
| 264 | .hash = raw_hash, |
| 265 | .unhash = raw_unhash, |
| 266 | .connect = raw_connect, |
| 267 | .disconnect = raw_disconnect, |
Dmitry Eremin-Solenikov | a9dfac3 | 2009-08-03 17:53:00 +0400 | [diff] [blame] | 268 | .getsockopt = raw_getsockopt, |
| 269 | .setsockopt = raw_setsockopt, |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 270 | }; |