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>
28 #include <linux/slab.h>
30 #include <net/af_ieee802154.h>
31 #include <net/ieee802154_netdev.h>
35 static HLIST_HEAD(raw_head
);
36 static DEFINE_RWLOCK(raw_lock
);
38 static void raw_hash(struct sock
*sk
)
40 write_lock_bh(&raw_lock
);
41 sk_add_node(sk
, &raw_head
);
42 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, 1);
43 write_unlock_bh(&raw_lock
);
46 static void raw_unhash(struct sock
*sk
)
48 write_lock_bh(&raw_lock
);
49 if (sk_del_node_init(sk
))
50 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, -1);
51 write_unlock_bh(&raw_lock
);
54 static void raw_close(struct sock
*sk
, long timeout
)
56 sk_common_release(sk
);
59 static int raw_bind(struct sock
*sk
, struct sockaddr
*_uaddr
, int len
)
61 struct ieee802154_addr addr
;
62 struct sockaddr_ieee802154
*uaddr
= (struct sockaddr_ieee802154
*)_uaddr
;
64 struct net_device
*dev
= NULL
;
66 if (len
< sizeof(*uaddr
))
69 uaddr
= (struct sockaddr_ieee802154
*)_uaddr
;
70 if (uaddr
->family
!= AF_IEEE802154
)
75 ieee802154_addr_from_sa(&addr
, &uaddr
->addr
);
76 dev
= ieee802154_get_dev(sock_net(sk
), &addr
);
82 if (dev
->type
!= ARPHRD_IEEE802154
) {
87 sk
->sk_bound_dev_if
= dev
->ifindex
;
98 static int raw_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
104 static int raw_disconnect(struct sock
*sk
, int flags
)
109 static int raw_sendmsg(struct kiocb
*iocb
, struct sock
*sk
, struct msghdr
*msg
,
112 struct net_device
*dev
;
118 if (msg
->msg_flags
& MSG_OOB
) {
119 pr_debug("msg->msg_flags = 0x%x\n", msg
->msg_flags
);
124 if (!sk
->sk_bound_dev_if
)
125 dev
= dev_getfirstbyhwtype(sock_net(sk
), ARPHRD_IEEE802154
);
127 dev
= dev_get_by_index(sock_net(sk
), sk
->sk_bound_dev_if
);
131 pr_debug("no dev\n");
137 pr_debug("name = %s, mtu = %u\n", dev
->name
, mtu
);
140 pr_debug("size = %Zu, mtu = %u\n", size
, mtu
);
145 hlen
= LL_RESERVED_SPACE(dev
);
146 tlen
= dev
->needed_tailroom
;
147 skb
= sock_alloc_send_skb(sk
, hlen
+ tlen
+ size
,
148 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
152 skb_reserve(skb
, hlen
);
154 skb_reset_mac_header(skb
);
155 skb_reset_network_header(skb
);
157 err
= memcpy_fromiovec(skb_put(skb
, size
), msg
->msg_iov
, size
);
163 skb
->protocol
= htons(ETH_P_IEEE802154
);
167 err
= dev_queue_xmit(skb
);
169 err
= net_xmit_errno(err
);
181 static int raw_recvmsg(struct kiocb
*iocb
, struct sock
*sk
, struct msghdr
*msg
,
182 size_t len
, int noblock
, int flags
, int *addr_len
)
185 int err
= -EOPNOTSUPP
;
188 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
194 msg
->msg_flags
|= MSG_TRUNC
;
198 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
202 sock_recv_ts_and_drops(msg
, sk
, skb
);
204 if (flags
& MSG_TRUNC
)
207 skb_free_datagram(sk
, skb
);
214 static int raw_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
216 skb
= skb_share_check(skb
, GFP_ATOMIC
);
220 if (sock_queue_rcv_skb(sk
, skb
) < 0) {
225 return NET_RX_SUCCESS
;
229 void ieee802154_raw_deliver(struct net_device
*dev
, struct sk_buff
*skb
)
233 read_lock(&raw_lock
);
234 sk_for_each(sk
, &raw_head
) {
236 if (!sk
->sk_bound_dev_if
||
237 sk
->sk_bound_dev_if
== dev
->ifindex
) {
239 struct sk_buff
*clone
;
241 clone
= skb_clone(skb
, GFP_ATOMIC
);
243 raw_rcv_skb(sk
, clone
);
247 read_unlock(&raw_lock
);
250 static int raw_getsockopt(struct sock
*sk
, int level
, int optname
,
251 char __user
*optval
, int __user
*optlen
)
256 static int raw_setsockopt(struct sock
*sk
, int level
, int optname
,
257 char __user
*optval
, unsigned int optlen
)
262 struct proto ieee802154_raw_prot
= {
263 .name
= "IEEE-802.15.4-RAW",
264 .owner
= THIS_MODULE
,
265 .obj_size
= sizeof(struct sock
),
268 .sendmsg
= raw_sendmsg
,
269 .recvmsg
= raw_recvmsg
,
271 .unhash
= raw_unhash
,
272 .connect
= raw_connect
,
273 .disconnect
= raw_disconnect
,
274 .getsockopt
= raw_getsockopt
,
275 .setsockopt
= raw_setsockopt
,