blob: 25018a9a53aa7100b74f2eeeed917dec9286ac2e [file] [log] [blame]
Sergey Lapin9ec76712009-06-08 12:18:48 +00001/*
2 * ZigBee socket interface
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>
28#include <net/sock.h>
Dmitry Eremin-Solenikovf0166e52009-07-23 16:56:29 +040029#include <net/af_ieee802154.h>
30#include <net/ieee802154.h>
31#include <net/ieee802154_netdev.h>
Sergey Lapin9ec76712009-06-08 12:18:48 +000032
33#include <asm/ioctls.h>
34
35#include "af802154.h"
36
37static HLIST_HEAD(dgram_head);
38static DEFINE_RWLOCK(dgram_lock);
39
40struct dgram_sock {
41 struct sock sk;
42
Sergey Lapin9ec76712009-06-08 12:18:48 +000043 struct ieee802154_addr src_addr;
44 struct ieee802154_addr dst_addr;
Dmitry Baryshkov74eda552009-08-07 02:58:42 +000045
46 unsigned bound:1;
Sergey Lapin9ec76712009-06-08 12:18:48 +000047};
48
49static inline struct dgram_sock *dgram_sk(const struct sock *sk)
50{
51 return container_of(sk, struct dgram_sock, sk);
52}
53
54
55static void dgram_hash(struct sock *sk)
56{
57 write_lock_bh(&dgram_lock);
58 sk_add_node(sk, &dgram_head);
59 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
60 write_unlock_bh(&dgram_lock);
61}
62
63static void dgram_unhash(struct sock *sk)
64{
65 write_lock_bh(&dgram_lock);
66 if (sk_del_node_init(sk))
67 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
68 write_unlock_bh(&dgram_lock);
69}
70
71static int dgram_init(struct sock *sk)
72{
73 struct dgram_sock *ro = dgram_sk(sk);
74
75 ro->dst_addr.addr_type = IEEE802154_ADDR_LONG;
76 ro->dst_addr.pan_id = 0xffff;
77 memset(&ro->dst_addr.hwaddr, 0xff, sizeof(ro->dst_addr.hwaddr));
78 return 0;
79}
80
81static void dgram_close(struct sock *sk, long timeout)
82{
83 sk_common_release(sk);
84}
85
86static int dgram_bind(struct sock *sk, struct sockaddr *uaddr, int len)
87{
88 struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
89 struct dgram_sock *ro = dgram_sk(sk);
Dmitry Baryshkov74eda552009-08-07 02:58:42 +000090 int err = -EINVAL;
Sergey Lapin9ec76712009-06-08 12:18:48 +000091 struct net_device *dev;
92
Dmitry Baryshkov74eda552009-08-07 02:58:42 +000093 lock_sock(sk);
94
Sergey Lapin9ec76712009-06-08 12:18:48 +000095 ro->bound = 0;
96
97 if (len < sizeof(*addr))
Dmitry Baryshkov74eda552009-08-07 02:58:42 +000098 goto out;
Sergey Lapin9ec76712009-06-08 12:18:48 +000099
100 if (addr->family != AF_IEEE802154)
Dmitry Baryshkov74eda552009-08-07 02:58:42 +0000101 goto out;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000102
103 dev = ieee802154_get_dev(sock_net(sk), &addr->addr);
104 if (!dev) {
105 err = -ENODEV;
106 goto out;
107 }
108
109 if (dev->type != ARPHRD_IEEE802154) {
110 err = -ENODEV;
111 goto out_put;
112 }
113
114 memcpy(&ro->src_addr, &addr->addr, sizeof(struct ieee802154_addr));
115
116 ro->bound = 1;
Dmitry Baryshkov74eda552009-08-07 02:58:42 +0000117 err = 0;
Sergey Lapin9ec76712009-06-08 12:18:48 +0000118out_put:
119 dev_put(dev);
120out:
121 release_sock(sk);
122
123 return err;
124}
125
126static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg)
127{
128 switch (cmd) {
129 case SIOCOUTQ:
130 {
Eric Dumazet31e6d362009-06-17 19:05:41 -0700131 int amount = sk_wmem_alloc_get(sk);
132
Sergey Lapin9ec76712009-06-08 12:18:48 +0000133 return put_user(amount, (int __user *)arg);
134 }
135
136 case SIOCINQ:
137 {
138 struct sk_buff *skb;
139 unsigned long amount;
140
141 amount = 0;
142 spin_lock_bh(&sk->sk_receive_queue.lock);
143 skb = skb_peek(&sk->sk_receive_queue);
144 if (skb != NULL) {
145 /*
146 * We will only return the amount
147 * of this packet since that is all
148 * that will be read.
149 */
150 /* FIXME: parse the header for more correct value */
151 amount = skb->len - (3+8+8);
152 }
153 spin_unlock_bh(&sk->sk_receive_queue.lock);
154 return put_user(amount, (int __user *)arg);
155 }
156
157 }
158 return -ENOIOCTLCMD;
159}
160
161/* FIXME: autobind */
162static int dgram_connect(struct sock *sk, struct sockaddr *uaddr,
163 int len)
164{
165 struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
166 struct dgram_sock *ro = dgram_sk(sk);
167 int err = 0;
168
169 if (len < sizeof(*addr))
170 return -EINVAL;
171
172 if (addr->family != AF_IEEE802154)
173 return -EINVAL;
174
175 lock_sock(sk);
176
177 if (!ro->bound) {
178 err = -ENETUNREACH;
179 goto out;
180 }
181
182 memcpy(&ro->dst_addr, &addr->addr, sizeof(struct ieee802154_addr));
183
184out:
185 release_sock(sk);
186 return err;
187}
188
189static int dgram_disconnect(struct sock *sk, int flags)
190{
191 struct dgram_sock *ro = dgram_sk(sk);
192
193 lock_sock(sk);
194
195 ro->dst_addr.addr_type = IEEE802154_ADDR_LONG;
196 memset(&ro->dst_addr.hwaddr, 0xff, sizeof(ro->dst_addr.hwaddr));
197
198 release_sock(sk);
199
200 return 0;
201}
202
203static int dgram_sendmsg(struct kiocb *iocb, struct sock *sk,
204 struct msghdr *msg, size_t size)
205{
206 struct net_device *dev;
207 unsigned mtu;
208 struct sk_buff *skb;
209 struct dgram_sock *ro = dgram_sk(sk);
210 int err;
211
212 if (msg->msg_flags & MSG_OOB) {
213 pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
214 return -EOPNOTSUPP;
215 }
216
217 if (!ro->bound)
218 dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
219 else
220 dev = ieee802154_get_dev(sock_net(sk), &ro->src_addr);
221
222 if (!dev) {
223 pr_debug("no dev\n");
224 err = -ENXIO;
225 goto out;
226 }
227 mtu = dev->mtu;
228 pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
229
230 skb = sock_alloc_send_skb(sk, LL_ALLOCATED_SPACE(dev) + size,
231 msg->msg_flags & MSG_DONTWAIT,
232 &err);
233 if (!skb)
234 goto out_dev;
235
236 skb_reserve(skb, LL_RESERVED_SPACE(dev));
237
238 skb_reset_network_header(skb);
239
240 mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA | MAC_CB_FLAG_ACKREQ;
241 mac_cb(skb)->seq = ieee802154_mlme_ops(dev)->get_dsn(dev);
242 err = dev_hard_header(skb, dev, ETH_P_IEEE802154, &ro->dst_addr,
243 ro->bound ? &ro->src_addr : NULL, size);
244 if (err < 0)
245 goto out_skb;
246
247 skb_reset_mac_header(skb);
248
249 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
250 if (err < 0)
251 goto out_skb;
252
253 if (size > mtu) {
David S. Millere5241c42009-06-11 02:10:19 -0700254 pr_debug("size = %Zu, mtu = %u\n", size, mtu);
Sergey Lapin9ec76712009-06-08 12:18:48 +0000255 err = -EINVAL;
256 goto out_skb;
257 }
258
259 skb->dev = dev;
260 skb->sk = sk;
261 skb->protocol = htons(ETH_P_IEEE802154);
262
263 dev_put(dev);
264
265 err = dev_queue_xmit(skb);
266 if (err > 0)
267 err = net_xmit_errno(err);
268
269 return err ?: size;
270
271out_skb:
272 kfree_skb(skb);
273out_dev:
274 dev_put(dev);
275out:
276 return err;
277}
278
279static int dgram_recvmsg(struct kiocb *iocb, struct sock *sk,
280 struct msghdr *msg, size_t len, int noblock, int flags,
281 int *addr_len)
282{
283 size_t copied = 0;
284 int err = -EOPNOTSUPP;
285 struct sk_buff *skb;
286
287 skb = skb_recv_datagram(sk, flags, noblock, &err);
288 if (!skb)
289 goto out;
290
291 copied = skb->len;
292 if (len < copied) {
293 msg->msg_flags |= MSG_TRUNC;
294 copied = len;
295 }
296
297 /* FIXME: skip headers if necessary ?! */
298 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
299 if (err)
300 goto done;
301
302 sock_recv_timestamp(msg, sk, skb);
303
304 if (flags & MSG_TRUNC)
305 copied = skb->len;
306done:
307 skb_free_datagram(sk, skb);
308out:
309 if (err)
310 return err;
311 return copied;
312}
313
314static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb)
315{
316 if (sock_queue_rcv_skb(sk, skb) < 0) {
317 atomic_inc(&sk->sk_drops);
318 kfree_skb(skb);
319 return NET_RX_DROP;
320 }
321
322 return NET_RX_SUCCESS;
323}
324
325static inline int ieee802154_match_sock(u8 *hw_addr, u16 pan_id,
326 u16 short_addr, struct dgram_sock *ro)
327{
328 if (!ro->bound)
329 return 1;
330
331 if (ro->src_addr.addr_type == IEEE802154_ADDR_LONG &&
332 !memcmp(ro->src_addr.hwaddr, hw_addr, IEEE802154_ADDR_LEN))
333 return 1;
334
335 if (ro->src_addr.addr_type == IEEE802154_ADDR_SHORT &&
336 pan_id == ro->src_addr.pan_id &&
337 short_addr == ro->src_addr.short_addr)
338 return 1;
339
340 return 0;
341}
342
343int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
344{
345 struct sock *sk, *prev = NULL;
346 struct hlist_node *node;
347 int ret = NET_RX_SUCCESS;
348 u16 pan_id, short_addr;
349
350 /* Data frame processing */
351 BUG_ON(dev->type != ARPHRD_IEEE802154);
352
353 pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
354 short_addr = ieee802154_mlme_ops(dev)->get_short_addr(dev);
355
356 read_lock(&dgram_lock);
357 sk_for_each(sk, node, &dgram_head) {
358 if (ieee802154_match_sock(dev->dev_addr, pan_id, short_addr,
359 dgram_sk(sk))) {
360 if (prev) {
361 struct sk_buff *clone;
362 clone = skb_clone(skb, GFP_ATOMIC);
363 if (clone)
364 dgram_rcv_skb(prev, clone);
365 }
366
367 prev = sk;
368 }
369 }
370
371 if (prev)
372 dgram_rcv_skb(prev, skb);
373 else {
374 kfree_skb(skb);
375 ret = NET_RX_DROP;
376 }
377 read_unlock(&dgram_lock);
378
379 return ret;
380}
381
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400382static int dgram_getsockopt(struct sock *sk, int level, int optname,
383 char __user *optval, int __user *optlen)
384{
385 return -EOPNOTSUPP;
386}
387
388static int dgram_setsockopt(struct sock *sk, int level, int optname,
389 char __user *optval, int __user optlen)
390{
391 return -EOPNOTSUPP;
392}
393
Sergey Lapin9ec76712009-06-08 12:18:48 +0000394struct proto ieee802154_dgram_prot = {
395 .name = "IEEE-802.15.4-MAC",
396 .owner = THIS_MODULE,
397 .obj_size = sizeof(struct dgram_sock),
398 .init = dgram_init,
399 .close = dgram_close,
400 .bind = dgram_bind,
401 .sendmsg = dgram_sendmsg,
402 .recvmsg = dgram_recvmsg,
403 .hash = dgram_hash,
404 .unhash = dgram_unhash,
405 .connect = dgram_connect,
406 .disconnect = dgram_disconnect,
407 .ioctl = dgram_ioctl,
Dmitry Eremin-Solenikova9dfac32009-08-03 17:53:00 +0400408 .getsockopt = dgram_getsockopt,
409 .setsockopt = dgram_setsockopt,
Sergey Lapin9ec76712009-06-08 12:18:48 +0000410};
411