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