blob: 9d1f64806f02127808986a84b36fae92e33df4a2 [file] [log] [blame]
Sergey Lapin9ec76712009-06-08 12:18:48 +00001/*
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 *
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 Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Sergey Lapin9ec76712009-06-08 12:18:48 +000029#include <net/sock.h>
Dmitry Eremin-Solenikovf0166e52009-07-23 16:56:29 +040030#include <net/af_ieee802154.h>
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010031#include <net/ieee802154_netdev.h>
Sergey Lapin9ec76712009-06-08 12:18:48 +000032
33#include "af802154.h"
34
35static HLIST_HEAD(raw_head);
36static DEFINE_RWLOCK(raw_lock);
37
38static void raw_hash(struct sock *sk)
39{
40 write_lock_bh(&raw_lock);
41 sk_add_node(sk, &raw_head);
42 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
43 write_unlock_bh(&raw_lock);
44}
45
46static void raw_unhash(struct sock *sk)
47{
48 write_lock_bh(&raw_lock);
49 if (sk_del_node_init(sk))
50 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
51 write_unlock_bh(&raw_lock);
52}
53
54static void raw_close(struct sock *sk, long timeout)
55{
56 sk_common_release(sk);
57}
58
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010059static int raw_bind(struct sock *sk, struct sockaddr *_uaddr, int len)
Sergey Lapin9ec76712009-06-08 12:18:48 +000060{
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010061 struct ieee802154_addr addr;
62 struct sockaddr_ieee802154 *uaddr = (struct sockaddr_ieee802154 *)_uaddr;
Sergey Lapin9ec76712009-06-08 12:18:48 +000063 int err = 0;
64 struct net_device *dev = NULL;
65
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010066 if (len < sizeof(*uaddr))
Sergey Lapin9ec76712009-06-08 12:18:48 +000067 return -EINVAL;
68
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010069 uaddr = (struct sockaddr_ieee802154 *)_uaddr;
70 if (uaddr->family != AF_IEEE802154)
Sergey Lapin9ec76712009-06-08 12:18:48 +000071 return -EINVAL;
72
73 lock_sock(sk);
74
Phoebe Buckheistere6278d92014-03-14 21:24:01 +010075 ieee802154_addr_from_sa(&addr, &uaddr->addr);
76 dev = ieee802154_get_dev(sock_net(sk), &addr);
Sergey Lapin9ec76712009-06-08 12:18:48 +000077 if (!dev) {
78 err = -ENODEV;
79 goto out;
80 }
81
Dmitry Eremin-Solenikov929122c2009-08-14 20:00:20 +040082 if (dev->type != ARPHRD_IEEE802154) {
Sergey Lapin9ec76712009-06-08 12:18:48 +000083 err = -ENODEV;
84 goto out_put;
85 }
86
87 sk->sk_bound_dev_if = dev->ifindex;
88 sk_dst_reset(sk);
89
90out_put:
91 dev_put(dev);
92out:
93 release_sock(sk);
94
95 return err;
96}
97
98static int raw_connect(struct sock *sk, struct sockaddr *uaddr,
Varka Bhadram4710d802014-07-02 09:01:09 +053099 int addr_len)
Sergey Lapin9ec76712009-06-08 12:18:48 +0000100{
101 return -ENOTSUPP;
102}
103
104static int raw_disconnect(struct sock *sk, int flags)
105{
106 return 0;
107}
108
Varka Bhadram4710d802014-07-02 09:01:09 +0530109static int raw_sendmsg(struct kiocb *iocb, struct sock *sk,
110 struct msghdr *msg, size_t size)
Sergey Lapin9ec76712009-06-08 12:18:48 +0000111{
112 struct net_device *dev;
Eric Dumazet95c96172012-04-15 05:58:06 +0000113 unsigned int mtu;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000114 struct sk_buff *skb;
Herbert Xuae641942011-11-18 02:20:04 +0000115 int hlen, tlen;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000116 int err;
117
118 if (msg->msg_flags & MSG_OOB) {
119 pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
120 return -EOPNOTSUPP;
121 }
122
123 lock_sock(sk);
124 if (!sk->sk_bound_dev_if)
125 dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
126 else
127 dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if);
128 release_sock(sk);
129
130 if (!dev) {
131 pr_debug("no dev\n");
132 err = -ENXIO;
133 goto out;
134 }
135
136 mtu = dev->mtu;
137 pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
138
139 if (size > mtu) {
David S. Millere5241c42009-06-11 02:10:19 -0700140 pr_debug("size = %Zu, mtu = %u\n", size, mtu);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000141 err = -EINVAL;
142 goto out_dev;
143 }
144
Herbert Xuae641942011-11-18 02:20:04 +0000145 hlen = LL_RESERVED_SPACE(dev);
146 tlen = dev->needed_tailroom;
147 skb = sock_alloc_send_skb(sk, hlen + tlen + size,
Varka Bhadram4710d802014-07-02 09:01:09 +0530148 msg->msg_flags & MSG_DONTWAIT, &err);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000149 if (!skb)
150 goto out_dev;
151
Herbert Xuae641942011-11-18 02:20:04 +0000152 skb_reserve(skb, hlen);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000153
154 skb_reset_mac_header(skb);
155 skb_reset_network_header(skb);
156
157 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
158 if (err < 0)
159 goto out_skb;
160
161 skb->dev = dev;
162 skb->sk = sk;
163 skb->protocol = htons(ETH_P_IEEE802154);
164
165 dev_put(dev);
166
167 err = dev_queue_xmit(skb);
168 if (err > 0)
169 err = net_xmit_errno(err);
170
171 return err ?: size;
172
173out_skb:
174 kfree_skb(skb);
175out_dev:
176 dev_put(dev);
177out:
178 return err;
179}
180
181static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
182 size_t len, int noblock, int flags, int *addr_len)
183{
184 size_t copied = 0;
185 int err = -EOPNOTSUPP;
186 struct sk_buff *skb;
187
188 skb = skb_recv_datagram(sk, flags, noblock, &err);
189 if (!skb)
190 goto out;
191
192 copied = skb->len;
193 if (len < copied) {
194 msg->msg_flags |= MSG_TRUNC;
195 copied = len;
196 }
197
198 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
199 if (err)
200 goto done;
201
Neil Horman3b885782009-10-12 13:26:31 -0700202 sock_recv_ts_and_drops(msg, sk, skb);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000203
204 if (flags & MSG_TRUNC)
205 copied = skb->len;
206done:
207 skb_free_datagram(sk, skb);
208out:
209 if (err)
210 return err;
211 return copied;
212}
213
214static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb)
215{
Phoebe Buckheister8cfad492014-03-17 18:30:19 +0100216 skb = skb_share_check(skb, GFP_ATOMIC);
217 if (!skb)
218 return NET_RX_DROP;
219
Sergey Lapin9ec76712009-06-08 12:18:48 +0000220 if (sock_queue_rcv_skb(sk, skb) < 0) {
Sergey Lapin9ec76712009-06-08 12:18:48 +0000221 kfree_skb(skb);
222 return NET_RX_DROP;
223 }
224
225 return NET_RX_SUCCESS;
226}
227
228
229void ieee802154_raw_deliver(struct net_device *dev, struct sk_buff *skb)
230{
231 struct sock *sk;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000232
233 read_lock(&raw_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800234 sk_for_each(sk, &raw_head) {
Sergey Lapin9ec76712009-06-08 12:18:48 +0000235 bh_lock_sock(sk);
236 if (!sk->sk_bound_dev_if ||
237 sk->sk_bound_dev_if == dev->ifindex) {
Sergey Lapin9ec76712009-06-08 12:18:48 +0000238 struct sk_buff *clone;
239
240 clone = skb_clone(skb, GFP_ATOMIC);
241 if (clone)
242 raw_rcv_skb(sk, clone);
243 }
244 bh_unlock_sock(sk);
245 }
246 read_unlock(&raw_lock);
247}
248
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400249static int raw_getsockopt(struct sock *sk, int level, int optname,
Varka Bhadram4710d802014-07-02 09:01:09 +0530250 char __user *optval, int __user *optlen)
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400251{
252 return -EOPNOTSUPP;
253}
254
255static int raw_setsockopt(struct sock *sk, int level, int optname,
Varka Bhadram4710d802014-07-02 09:01:09 +0530256 char __user *optval, unsigned int optlen)
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400257{
258 return -EOPNOTSUPP;
259}
260
Sergey Lapin9ec76712009-06-08 12:18:48 +0000261struct proto ieee802154_raw_prot = {
262 .name = "IEEE-802.15.4-RAW",
263 .owner = THIS_MODULE,
264 .obj_size = sizeof(struct sock),
265 .close = raw_close,
266 .bind = raw_bind,
267 .sendmsg = raw_sendmsg,
268 .recvmsg = raw_recvmsg,
269 .hash = raw_hash,
270 .unhash = raw_unhash,
271 .connect = raw_connect,
272 .disconnect = raw_disconnect,
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400273 .getsockopt = raw_getsockopt,
274 .setsockopt = raw_setsockopt,
Sergey Lapin9ec76712009-06-08 12:18:48 +0000275};