spi-topcliff-pch: Fix issue for transmitting over 4KByte
[zen-stable.git] / net / ieee802154 / dgram.c
blob1b09eaabaac1239475e1abab843af353bcccdd4f
1 /*
2 * IEEE 802.15.4 dgram socket interface
4 * Copyright 2007, 2008 Siemens AG
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.
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.
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.
19 * Written by:
20 * Sergey Lapin <slapin@ossfans.org>
21 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
24 #include <linux/net.h>
25 #include <linux/module.h>
26 #include <linux/if_arp.h>
27 #include <linux/list.h>
28 #include <linux/slab.h>
29 #include <net/sock.h>
30 #include <net/af_ieee802154.h>
31 #include <net/ieee802154.h>
32 #include <net/ieee802154_netdev.h>
34 #include <asm/ioctls.h>
36 #include "af802154.h"
38 static HLIST_HEAD(dgram_head);
39 static DEFINE_RWLOCK(dgram_lock);
41 struct dgram_sock {
42 struct sock sk;
44 struct ieee802154_addr src_addr;
45 struct ieee802154_addr dst_addr;
47 unsigned bound:1;
48 unsigned want_ack:1;
51 static inline struct dgram_sock *dgram_sk(const struct sock *sk)
53 return container_of(sk, struct dgram_sock, sk);
56 static void dgram_hash(struct sock *sk)
58 write_lock_bh(&dgram_lock);
59 sk_add_node(sk, &dgram_head);
60 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
61 write_unlock_bh(&dgram_lock);
64 static void dgram_unhash(struct sock *sk)
66 write_lock_bh(&dgram_lock);
67 if (sk_del_node_init(sk))
68 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
69 write_unlock_bh(&dgram_lock);
72 static int dgram_init(struct sock *sk)
74 struct dgram_sock *ro = dgram_sk(sk);
76 ro->dst_addr.addr_type = IEEE802154_ADDR_LONG;
77 ro->dst_addr.pan_id = 0xffff;
78 ro->want_ack = 1;
79 memset(&ro->dst_addr.hwaddr, 0xff, sizeof(ro->dst_addr.hwaddr));
80 return 0;
83 static void dgram_close(struct sock *sk, long timeout)
85 sk_common_release(sk);
88 static int dgram_bind(struct sock *sk, struct sockaddr *uaddr, int len)
90 struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
91 struct dgram_sock *ro = dgram_sk(sk);
92 int err = -EINVAL;
93 struct net_device *dev;
95 lock_sock(sk);
97 ro->bound = 0;
99 if (len < sizeof(*addr))
100 goto out;
102 if (addr->family != AF_IEEE802154)
103 goto out;
105 dev = ieee802154_get_dev(sock_net(sk), &addr->addr);
106 if (!dev) {
107 err = -ENODEV;
108 goto out;
111 if (dev->type != ARPHRD_IEEE802154) {
112 err = -ENODEV;
113 goto out_put;
116 memcpy(&ro->src_addr, &addr->addr, sizeof(struct ieee802154_addr));
118 ro->bound = 1;
119 err = 0;
120 out_put:
121 dev_put(dev);
122 out:
123 release_sock(sk);
125 return err;
128 static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg)
130 switch (cmd) {
131 case SIOCOUTQ:
133 int amount = sk_wmem_alloc_get(sk);
135 return put_user(amount, (int __user *)arg);
138 case SIOCINQ:
140 struct sk_buff *skb;
141 unsigned long amount;
143 amount = 0;
144 spin_lock_bh(&sk->sk_receive_queue.lock);
145 skb = skb_peek(&sk->sk_receive_queue);
146 if (skb != NULL) {
148 * We will only return the amount
149 * of this packet since that is all
150 * that will be read.
152 /* FIXME: parse the header for more correct value */
153 amount = skb->len - (3+8+8);
155 spin_unlock_bh(&sk->sk_receive_queue.lock);
156 return put_user(amount, (int __user *)arg);
160 return -ENOIOCTLCMD;
163 /* FIXME: autobind */
164 static int dgram_connect(struct sock *sk, struct sockaddr *uaddr,
165 int len)
167 struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
168 struct dgram_sock *ro = dgram_sk(sk);
169 int err = 0;
171 if (len < sizeof(*addr))
172 return -EINVAL;
174 if (addr->family != AF_IEEE802154)
175 return -EINVAL;
177 lock_sock(sk);
179 if (!ro->bound) {
180 err = -ENETUNREACH;
181 goto out;
184 memcpy(&ro->dst_addr, &addr->addr, sizeof(struct ieee802154_addr));
186 out:
187 release_sock(sk);
188 return err;
191 static int dgram_disconnect(struct sock *sk, int flags)
193 struct dgram_sock *ro = dgram_sk(sk);
195 lock_sock(sk);
197 ro->dst_addr.addr_type = IEEE802154_ADDR_LONG;
198 memset(&ro->dst_addr.hwaddr, 0xff, sizeof(ro->dst_addr.hwaddr));
200 release_sock(sk);
202 return 0;
205 static int dgram_sendmsg(struct kiocb *iocb, struct sock *sk,
206 struct msghdr *msg, size_t size)
208 struct net_device *dev;
209 unsigned mtu;
210 struct sk_buff *skb;
211 struct dgram_sock *ro = dgram_sk(sk);
212 int hlen, tlen;
213 int err;
215 if (msg->msg_flags & MSG_OOB) {
216 pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
217 return -EOPNOTSUPP;
220 if (!ro->bound)
221 dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
222 else
223 dev = ieee802154_get_dev(sock_net(sk), &ro->src_addr);
225 if (!dev) {
226 pr_debug("no dev\n");
227 err = -ENXIO;
228 goto out;
230 mtu = dev->mtu;
231 pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
233 hlen = LL_RESERVED_SPACE(dev);
234 tlen = dev->needed_tailroom;
235 skb = sock_alloc_send_skb(sk, hlen + tlen + size,
236 msg->msg_flags & MSG_DONTWAIT,
237 &err);
238 if (!skb)
239 goto out_dev;
241 skb_reserve(skb, hlen);
243 skb_reset_network_header(skb);
245 mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA;
246 if (ro->want_ack)
247 mac_cb(skb)->flags |= MAC_CB_FLAG_ACKREQ;
249 mac_cb(skb)->seq = ieee802154_mlme_ops(dev)->get_dsn(dev);
250 err = dev_hard_header(skb, dev, ETH_P_IEEE802154, &ro->dst_addr,
251 ro->bound ? &ro->src_addr : NULL, size);
252 if (err < 0)
253 goto out_skb;
255 skb_reset_mac_header(skb);
257 err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
258 if (err < 0)
259 goto out_skb;
261 if (size > mtu) {
262 pr_debug("size = %Zu, mtu = %u\n", size, mtu);
263 err = -EINVAL;
264 goto out_skb;
267 skb->dev = dev;
268 skb->sk = sk;
269 skb->protocol = htons(ETH_P_IEEE802154);
271 dev_put(dev);
273 err = dev_queue_xmit(skb);
274 if (err > 0)
275 err = net_xmit_errno(err);
277 return err ?: size;
279 out_skb:
280 kfree_skb(skb);
281 out_dev:
282 dev_put(dev);
283 out:
284 return err;
287 static int dgram_recvmsg(struct kiocb *iocb, struct sock *sk,
288 struct msghdr *msg, size_t len, int noblock, int flags,
289 int *addr_len)
291 size_t copied = 0;
292 int err = -EOPNOTSUPP;
293 struct sk_buff *skb;
295 skb = skb_recv_datagram(sk, flags, noblock, &err);
296 if (!skb)
297 goto out;
299 copied = skb->len;
300 if (len < copied) {
301 msg->msg_flags |= MSG_TRUNC;
302 copied = len;
305 /* FIXME: skip headers if necessary ?! */
306 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
307 if (err)
308 goto done;
310 sock_recv_ts_and_drops(msg, sk, skb);
312 if (flags & MSG_TRUNC)
313 copied = skb->len;
314 done:
315 skb_free_datagram(sk, skb);
316 out:
317 if (err)
318 return err;
319 return copied;
322 static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb)
324 if (sock_queue_rcv_skb(sk, skb) < 0) {
325 kfree_skb(skb);
326 return NET_RX_DROP;
329 return NET_RX_SUCCESS;
332 static inline int ieee802154_match_sock(u8 *hw_addr, u16 pan_id,
333 u16 short_addr, struct dgram_sock *ro)
335 if (!ro->bound)
336 return 1;
338 if (ro->src_addr.addr_type == IEEE802154_ADDR_LONG &&
339 !memcmp(ro->src_addr.hwaddr, hw_addr, IEEE802154_ADDR_LEN))
340 return 1;
342 if (ro->src_addr.addr_type == IEEE802154_ADDR_SHORT &&
343 pan_id == ro->src_addr.pan_id &&
344 short_addr == ro->src_addr.short_addr)
345 return 1;
347 return 0;
350 int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
352 struct sock *sk, *prev = NULL;
353 struct hlist_node *node;
354 int ret = NET_RX_SUCCESS;
355 u16 pan_id, short_addr;
357 /* Data frame processing */
358 BUG_ON(dev->type != ARPHRD_IEEE802154);
360 pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
361 short_addr = ieee802154_mlme_ops(dev)->get_short_addr(dev);
363 read_lock(&dgram_lock);
364 sk_for_each(sk, node, &dgram_head) {
365 if (ieee802154_match_sock(dev->dev_addr, pan_id, short_addr,
366 dgram_sk(sk))) {
367 if (prev) {
368 struct sk_buff *clone;
369 clone = skb_clone(skb, GFP_ATOMIC);
370 if (clone)
371 dgram_rcv_skb(prev, clone);
374 prev = sk;
378 if (prev)
379 dgram_rcv_skb(prev, skb);
380 else {
381 kfree_skb(skb);
382 ret = NET_RX_DROP;
384 read_unlock(&dgram_lock);
386 return ret;
389 static int dgram_getsockopt(struct sock *sk, int level, int optname,
390 char __user *optval, int __user *optlen)
392 struct dgram_sock *ro = dgram_sk(sk);
394 int val, len;
396 if (level != SOL_IEEE802154)
397 return -EOPNOTSUPP;
399 if (get_user(len, optlen))
400 return -EFAULT;
402 len = min_t(unsigned int, len, sizeof(int));
404 switch (optname) {
405 case WPAN_WANTACK:
406 val = ro->want_ack;
407 break;
408 default:
409 return -ENOPROTOOPT;
412 if (put_user(len, optlen))
413 return -EFAULT;
414 if (copy_to_user(optval, &val, len))
415 return -EFAULT;
416 return 0;
419 static int dgram_setsockopt(struct sock *sk, int level, int optname,
420 char __user *optval, unsigned int optlen)
422 struct dgram_sock *ro = dgram_sk(sk);
423 int val;
424 int err = 0;
426 if (optlen < sizeof(int))
427 return -EINVAL;
429 if (get_user(val, (int __user *)optval))
430 return -EFAULT;
432 lock_sock(sk);
434 switch (optname) {
435 case WPAN_WANTACK:
436 ro->want_ack = !!val;
437 break;
438 default:
439 err = -ENOPROTOOPT;
440 break;
443 release_sock(sk);
444 return err;
447 struct proto ieee802154_dgram_prot = {
448 .name = "IEEE-802.15.4-MAC",
449 .owner = THIS_MODULE,
450 .obj_size = sizeof(struct dgram_sock),
451 .init = dgram_init,
452 .close = dgram_close,
453 .bind = dgram_bind,
454 .sendmsg = dgram_sendmsg,
455 .recvmsg = dgram_recvmsg,
456 .hash = dgram_hash,
457 .unhash = dgram_unhash,
458 .connect = dgram_connect,
459 .disconnect = dgram_disconnect,
460 .ioctl = dgram_ioctl,
461 .getsockopt = dgram_getsockopt,
462 .setsockopt = dgram_setsockopt,