2 * Raw IEEE 802.15.4 sockets
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>
33 static HLIST_HEAD(raw_head
);
34 static DEFINE_RWLOCK(raw_lock
);
36 static void raw_hash(struct sock
*sk
)
38 write_lock_bh(&raw_lock
);
39 sk_add_node(sk
, &raw_head
);
40 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, 1);
41 write_unlock_bh(&raw_lock
);
44 static void raw_unhash(struct sock
*sk
)
46 write_lock_bh(&raw_lock
);
47 if (sk_del_node_init(sk
))
48 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, -1);
49 write_unlock_bh(&raw_lock
);
52 static void raw_close(struct sock
*sk
, long timeout
)
54 sk_common_release(sk
);
57 static int raw_bind(struct sock
*sk
, struct sockaddr
*uaddr
, int len
)
59 struct sockaddr_ieee802154
*addr
= (struct sockaddr_ieee802154
*)uaddr
;
61 struct net_device
*dev
= NULL
;
63 if (len
< sizeof(*addr
))
66 if (addr
->family
!= AF_IEEE802154
)
71 dev
= ieee802154_get_dev(sock_net(sk
), &addr
->addr
);
77 if (dev
->type
!= ARPHRD_IEEE802154_PHY
&&
78 dev
->type
!= ARPHRD_IEEE802154
) {
83 sk
->sk_bound_dev_if
= dev
->ifindex
;
94 static int raw_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
100 static int raw_disconnect(struct sock
*sk
, int flags
)
105 static int raw_sendmsg(struct kiocb
*iocb
, struct sock
*sk
, struct msghdr
*msg
,
108 struct net_device
*dev
;
113 if (msg
->msg_flags
& MSG_OOB
) {
114 pr_debug("msg->msg_flags = 0x%x\n", msg
->msg_flags
);
119 if (!sk
->sk_bound_dev_if
)
120 dev
= dev_getfirstbyhwtype(sock_net(sk
), ARPHRD_IEEE802154
);
122 dev
= dev_get_by_index(sock_net(sk
), sk
->sk_bound_dev_if
);
126 pr_debug("no dev\n");
132 pr_debug("name = %s, mtu = %u\n", dev
->name
, mtu
);
135 pr_debug("size = %Zu, mtu = %u\n", size
, mtu
);
140 skb
= sock_alloc_send_skb(sk
, LL_ALLOCATED_SPACE(dev
) + size
,
141 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
145 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
147 skb_reset_mac_header(skb
);
148 skb_reset_network_header(skb
);
150 err
= memcpy_fromiovec(skb_put(skb
, size
), msg
->msg_iov
, size
);
156 skb
->protocol
= htons(ETH_P_IEEE802154
);
160 err
= dev_queue_xmit(skb
);
162 err
= net_xmit_errno(err
);
174 static int raw_recvmsg(struct kiocb
*iocb
, struct sock
*sk
, struct msghdr
*msg
,
175 size_t len
, int noblock
, int flags
, int *addr_len
)
178 int err
= -EOPNOTSUPP
;
181 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
187 msg
->msg_flags
|= MSG_TRUNC
;
191 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
195 sock_recv_timestamp(msg
, sk
, skb
);
197 if (flags
& MSG_TRUNC
)
200 skb_free_datagram(sk
, skb
);
207 static int raw_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
209 if (sock_queue_rcv_skb(sk
, skb
) < 0) {
210 atomic_inc(&sk
->sk_drops
);
215 return NET_RX_SUCCESS
;
219 void ieee802154_raw_deliver(struct net_device
*dev
, struct sk_buff
*skb
)
222 struct hlist_node
*node
;
224 read_lock(&raw_lock
);
225 sk_for_each(sk
, node
, &raw_head
) {
227 if (!sk
->sk_bound_dev_if
||
228 sk
->sk_bound_dev_if
== dev
->ifindex
) {
230 struct sk_buff
*clone
;
232 clone
= skb_clone(skb
, GFP_ATOMIC
);
234 raw_rcv_skb(sk
, clone
);
238 read_unlock(&raw_lock
);
241 static int raw_getsockopt(struct sock
*sk
, int level
, int optname
,
242 char __user
*optval
, int __user
*optlen
)
247 static int raw_setsockopt(struct sock
*sk
, int level
, int optname
,
248 char __user
*optval
, int __user optlen
)
253 struct proto ieee802154_raw_prot
= {
254 .name
= "IEEE-802.15.4-RAW",
255 .owner
= THIS_MODULE
,
256 .obj_size
= sizeof(struct sock
),
259 .sendmsg
= raw_sendmsg
,
260 .recvmsg
= raw_recvmsg
,
262 .unhash
= raw_unhash
,
263 .connect
= raw_connect
,
264 .disconnect
= raw_disconnect
,
265 .getsockopt
= raw_getsockopt
,
266 .setsockopt
= raw_setsockopt
,