2 * ZigBee 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.
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>
29 #include <net/ieee802154/af_ieee802154.h>
30 #include <net/ieee802154/mac_def.h>
31 #include <net/ieee802154/netdevice.h>
33 #include <asm/ioctls.h>
37 static HLIST_HEAD(dgram_head
);
38 static DEFINE_RWLOCK(dgram_lock
);
44 struct ieee802154_addr src_addr
;
45 struct ieee802154_addr dst_addr
;
48 static inline struct dgram_sock
*dgram_sk(const struct sock
*sk
)
50 return container_of(sk
, struct dgram_sock
, sk
);
54 static void dgram_hash(struct sock
*sk
)
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
);
62 static void dgram_unhash(struct sock
*sk
)
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
);
70 static int dgram_init(struct sock
*sk
)
72 struct dgram_sock
*ro
= dgram_sk(sk
);
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
));
80 static void dgram_close(struct sock
*sk
, long timeout
)
82 sk_common_release(sk
);
85 static int dgram_bind(struct sock
*sk
, struct sockaddr
*uaddr
, int len
)
87 struct sockaddr_ieee802154
*addr
= (struct sockaddr_ieee802154
*)uaddr
;
88 struct dgram_sock
*ro
= dgram_sk(sk
);
90 struct net_device
*dev
;
94 if (len
< sizeof(*addr
))
97 if (addr
->family
!= AF_IEEE802154
)
102 dev
= ieee802154_get_dev(sock_net(sk
), &addr
->addr
);
108 if (dev
->type
!= ARPHRD_IEEE802154
) {
113 memcpy(&ro
->src_addr
, &addr
->addr
, sizeof(struct ieee802154_addr
));
124 static int dgram_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
129 int amount
= atomic_read(&sk
->sk_wmem_alloc
);
130 return put_user(amount
, (int __user
*)arg
);
136 unsigned long amount
;
139 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
140 skb
= skb_peek(&sk
->sk_receive_queue
);
143 * We will only return the amount
144 * of this packet since that is all
147 /* FIXME: parse the header for more correct value */
148 amount
= skb
->len
- (3+8+8);
150 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
151 return put_user(amount
, (int __user
*)arg
);
158 /* FIXME: autobind */
159 static int dgram_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
162 struct sockaddr_ieee802154
*addr
= (struct sockaddr_ieee802154
*)uaddr
;
163 struct dgram_sock
*ro
= dgram_sk(sk
);
166 if (len
< sizeof(*addr
))
169 if (addr
->family
!= AF_IEEE802154
)
179 memcpy(&ro
->dst_addr
, &addr
->addr
, sizeof(struct ieee802154_addr
));
186 static int dgram_disconnect(struct sock
*sk
, int flags
)
188 struct dgram_sock
*ro
= dgram_sk(sk
);
192 ro
->dst_addr
.addr_type
= IEEE802154_ADDR_LONG
;
193 memset(&ro
->dst_addr
.hwaddr
, 0xff, sizeof(ro
->dst_addr
.hwaddr
));
200 static int dgram_sendmsg(struct kiocb
*iocb
, struct sock
*sk
,
201 struct msghdr
*msg
, size_t size
)
203 struct net_device
*dev
;
206 struct dgram_sock
*ro
= dgram_sk(sk
);
209 if (msg
->msg_flags
& MSG_OOB
) {
210 pr_debug("msg->msg_flags = 0x%x\n", msg
->msg_flags
);
215 dev
= dev_getfirstbyhwtype(sock_net(sk
), ARPHRD_IEEE802154
);
217 dev
= ieee802154_get_dev(sock_net(sk
), &ro
->src_addr
);
220 pr_debug("no dev\n");
225 pr_debug("name = %s, mtu = %u\n", dev
->name
, mtu
);
227 skb
= sock_alloc_send_skb(sk
, LL_ALLOCATED_SPACE(dev
) + size
,
228 msg
->msg_flags
& MSG_DONTWAIT
,
233 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
235 skb_reset_network_header(skb
);
237 mac_cb(skb
)->flags
= IEEE802154_FC_TYPE_DATA
| MAC_CB_FLAG_ACKREQ
;
238 mac_cb(skb
)->seq
= ieee802154_mlme_ops(dev
)->get_dsn(dev
);
239 err
= dev_hard_header(skb
, dev
, ETH_P_IEEE802154
, &ro
->dst_addr
,
240 ro
->bound
? &ro
->src_addr
: NULL
, size
);
244 skb_reset_mac_header(skb
);
246 err
= memcpy_fromiovec(skb_put(skb
, size
), msg
->msg_iov
, size
);
251 pr_debug("size = %Zu, mtu = %u\n", size
, mtu
);
258 skb
->protocol
= htons(ETH_P_IEEE802154
);
262 err
= dev_queue_xmit(skb
);
264 err
= net_xmit_errno(err
);
276 static int dgram_recvmsg(struct kiocb
*iocb
, struct sock
*sk
,
277 struct msghdr
*msg
, size_t len
, int noblock
, int flags
,
281 int err
= -EOPNOTSUPP
;
284 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
290 msg
->msg_flags
|= MSG_TRUNC
;
294 /* FIXME: skip headers if necessary ?! */
295 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
299 sock_recv_timestamp(msg
, sk
, skb
);
301 if (flags
& MSG_TRUNC
)
304 skb_free_datagram(sk
, skb
);
311 static int dgram_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
313 if (sock_queue_rcv_skb(sk
, skb
) < 0) {
314 atomic_inc(&sk
->sk_drops
);
319 return NET_RX_SUCCESS
;
322 static inline int ieee802154_match_sock(u8
*hw_addr
, u16 pan_id
,
323 u16 short_addr
, struct dgram_sock
*ro
)
328 if (ro
->src_addr
.addr_type
== IEEE802154_ADDR_LONG
&&
329 !memcmp(ro
->src_addr
.hwaddr
, hw_addr
, IEEE802154_ADDR_LEN
))
332 if (ro
->src_addr
.addr_type
== IEEE802154_ADDR_SHORT
&&
333 pan_id
== ro
->src_addr
.pan_id
&&
334 short_addr
== ro
->src_addr
.short_addr
)
340 int ieee802154_dgram_deliver(struct net_device
*dev
, struct sk_buff
*skb
)
342 struct sock
*sk
, *prev
= NULL
;
343 struct hlist_node
*node
;
344 int ret
= NET_RX_SUCCESS
;
345 u16 pan_id
, short_addr
;
347 /* Data frame processing */
348 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
350 pan_id
= ieee802154_mlme_ops(dev
)->get_pan_id(dev
);
351 short_addr
= ieee802154_mlme_ops(dev
)->get_short_addr(dev
);
353 read_lock(&dgram_lock
);
354 sk_for_each(sk
, node
, &dgram_head
) {
355 if (ieee802154_match_sock(dev
->dev_addr
, pan_id
, short_addr
,
358 struct sk_buff
*clone
;
359 clone
= skb_clone(skb
, GFP_ATOMIC
);
361 dgram_rcv_skb(prev
, clone
);
369 dgram_rcv_skb(prev
, skb
);
374 read_unlock(&dgram_lock
);
379 struct proto ieee802154_dgram_prot
= {
380 .name
= "IEEE-802.15.4-MAC",
381 .owner
= THIS_MODULE
,
382 .obj_size
= sizeof(struct dgram_sock
),
384 .close
= dgram_close
,
386 .sendmsg
= dgram_sendmsg
,
387 .recvmsg
= dgram_recvmsg
,
389 .unhash
= dgram_unhash
,
390 .connect
= dgram_connect
,
391 .disconnect
= dgram_disconnect
,
392 .ioctl
= dgram_ioctl
,