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.
20 * Sergey Lapin <slapin@ossfans.org>
21 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
24 #include <linux/capability.h>
25 #include <linux/net.h>
26 #include <linux/module.h>
27 #include <linux/if_arp.h>
28 #include <linux/list.h>
29 #include <linux/slab.h>
31 #include <net/af_ieee802154.h>
32 #include <net/ieee802154.h>
33 #include <net/ieee802154_netdev.h>
35 #include <asm/ioctls.h>
39 static HLIST_HEAD(dgram_head
);
40 static DEFINE_RWLOCK(dgram_lock
);
45 struct ieee802154_addr src_addr
;
46 struct ieee802154_addr dst_addr
;
49 unsigned int connected
:1;
50 unsigned int want_ack
:1;
52 unsigned int secen_override
:1;
53 unsigned int seclevel
:3;
54 unsigned int seclevel_override
:1;
57 static inline struct dgram_sock
*dgram_sk(const struct sock
*sk
)
59 return container_of(sk
, struct dgram_sock
, sk
);
62 static void dgram_hash(struct sock
*sk
)
64 write_lock_bh(&dgram_lock
);
65 sk_add_node(sk
, &dgram_head
);
66 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, 1);
67 write_unlock_bh(&dgram_lock
);
70 static void dgram_unhash(struct sock
*sk
)
72 write_lock_bh(&dgram_lock
);
73 if (sk_del_node_init(sk
))
74 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, -1);
75 write_unlock_bh(&dgram_lock
);
78 static int dgram_init(struct sock
*sk
)
80 struct dgram_sock
*ro
= dgram_sk(sk
);
86 static void dgram_close(struct sock
*sk
, long timeout
)
88 sk_common_release(sk
);
91 static int dgram_bind(struct sock
*sk
, struct sockaddr
*uaddr
, int len
)
93 struct sockaddr_ieee802154
*addr
= (struct sockaddr_ieee802154
*)uaddr
;
94 struct ieee802154_addr haddr
;
95 struct dgram_sock
*ro
= dgram_sk(sk
);
97 struct net_device
*dev
;
103 if (len
< sizeof(*addr
))
106 if (addr
->family
!= AF_IEEE802154
)
109 ieee802154_addr_from_sa(&haddr
, &addr
->addr
);
110 dev
= ieee802154_get_dev(sock_net(sk
), &haddr
);
116 if (dev
->type
!= ARPHRD_IEEE802154
) {
121 ro
->src_addr
= haddr
;
133 static int dgram_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
138 int amount
= sk_wmem_alloc_get(sk
);
140 return put_user(amount
, (int __user
*)arg
);
146 unsigned long amount
;
149 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
150 skb
= skb_peek(&sk
->sk_receive_queue
);
153 * We will only return the amount
154 * of this packet since that is all
157 amount
= skb
->len
- ieee802154_hdr_length(skb
);
159 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
160 return put_user(amount
, (int __user
*)arg
);
167 /* FIXME: autobind */
168 static int dgram_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
171 struct sockaddr_ieee802154
*addr
= (struct sockaddr_ieee802154
*)uaddr
;
172 struct dgram_sock
*ro
= dgram_sk(sk
);
175 if (len
< sizeof(*addr
))
178 if (addr
->family
!= AF_IEEE802154
)
188 ieee802154_addr_from_sa(&ro
->dst_addr
, &addr
->addr
);
196 static int dgram_disconnect(struct sock
*sk
, int flags
)
198 struct dgram_sock
*ro
= dgram_sk(sk
);
207 static int dgram_sendmsg(struct kiocb
*iocb
, struct sock
*sk
,
208 struct msghdr
*msg
, size_t size
)
210 struct net_device
*dev
;
213 struct ieee802154_mac_cb
*cb
;
214 struct dgram_sock
*ro
= dgram_sk(sk
);
215 struct ieee802154_addr dst_addr
;
219 if (msg
->msg_flags
& MSG_OOB
) {
220 pr_debug("msg->msg_flags = 0x%x\n", msg
->msg_flags
);
224 if (!ro
->connected
&& !msg
->msg_name
)
225 return -EDESTADDRREQ
;
226 else if (ro
->connected
&& msg
->msg_name
)
230 dev
= dev_getfirstbyhwtype(sock_net(sk
), ARPHRD_IEEE802154
);
232 dev
= ieee802154_get_dev(sock_net(sk
), &ro
->src_addr
);
235 pr_debug("no dev\n");
240 pr_debug("name = %s, mtu = %u\n", dev
->name
, mtu
);
243 pr_debug("size = %Zu, mtu = %u\n", size
, mtu
);
248 hlen
= LL_RESERVED_SPACE(dev
);
249 tlen
= dev
->needed_tailroom
;
250 skb
= sock_alloc_send_skb(sk
, hlen
+ tlen
+ size
,
251 msg
->msg_flags
& MSG_DONTWAIT
,
256 skb_reserve(skb
, hlen
);
258 skb_reset_network_header(skb
);
260 cb
= mac_cb_init(skb
);
261 cb
->type
= IEEE802154_FC_TYPE_DATA
;
262 cb
->ackreq
= ro
->want_ack
;
265 DECLARE_SOCKADDR(struct sockaddr_ieee802154
*, daddr
, msg
->msg_name
);
267 ieee802154_addr_from_sa(&dst_addr
, &daddr
->addr
);
269 dst_addr
= ro
->dst_addr
;
272 cb
->secen
= ro
->secen
;
273 cb
->secen_override
= ro
->secen_override
;
274 cb
->seclevel
= ro
->seclevel
;
275 cb
->seclevel_override
= ro
->seclevel_override
;
277 err
= dev_hard_header(skb
, dev
, ETH_P_IEEE802154
, &dst_addr
,
278 ro
->bound
? &ro
->src_addr
: NULL
, size
);
282 err
= memcpy_fromiovec(skb_put(skb
, size
), msg
->msg_iov
, size
);
288 skb
->protocol
= htons(ETH_P_IEEE802154
);
292 err
= dev_queue_xmit(skb
);
294 err
= net_xmit_errno(err
);
306 static int dgram_recvmsg(struct kiocb
*iocb
, struct sock
*sk
,
307 struct msghdr
*msg
, size_t len
, int noblock
, int flags
,
311 int err
= -EOPNOTSUPP
;
313 DECLARE_SOCKADDR(struct sockaddr_ieee802154
*, saddr
, msg
->msg_name
);
315 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
321 msg
->msg_flags
|= MSG_TRUNC
;
325 /* FIXME: skip headers if necessary ?! */
326 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
330 sock_recv_ts_and_drops(msg
, sk
, skb
);
333 saddr
->family
= AF_IEEE802154
;
334 ieee802154_addr_to_sa(&saddr
->addr
, &mac_cb(skb
)->source
);
335 *addr_len
= sizeof(*saddr
);
338 if (flags
& MSG_TRUNC
)
341 skb_free_datagram(sk
, skb
);
348 static int dgram_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
350 skb
= skb_share_check(skb
, GFP_ATOMIC
);
354 if (sock_queue_rcv_skb(sk
, skb
) < 0) {
359 return NET_RX_SUCCESS
;
363 ieee802154_match_sock(__le64 hw_addr
, __le16 pan_id
, __le16 short_addr
,
364 struct dgram_sock
*ro
)
369 if (ro
->src_addr
.mode
== IEEE802154_ADDR_LONG
&&
370 hw_addr
== ro
->src_addr
.extended_addr
)
373 if (ro
->src_addr
.mode
== IEEE802154_ADDR_SHORT
&&
374 pan_id
== ro
->src_addr
.pan_id
&&
375 short_addr
== ro
->src_addr
.short_addr
)
381 int ieee802154_dgram_deliver(struct net_device
*dev
, struct sk_buff
*skb
)
383 struct sock
*sk
, *prev
= NULL
;
384 int ret
= NET_RX_SUCCESS
;
385 __le16 pan_id
, short_addr
;
388 /* Data frame processing */
389 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
391 pan_id
= ieee802154_mlme_ops(dev
)->get_pan_id(dev
);
392 short_addr
= ieee802154_mlme_ops(dev
)->get_short_addr(dev
);
393 hw_addr
= ieee802154_devaddr_from_raw(dev
->dev_addr
);
395 read_lock(&dgram_lock
);
396 sk_for_each(sk
, &dgram_head
) {
397 if (ieee802154_match_sock(hw_addr
, pan_id
, short_addr
,
400 struct sk_buff
*clone
;
401 clone
= skb_clone(skb
, GFP_ATOMIC
);
403 dgram_rcv_skb(prev
, clone
);
411 dgram_rcv_skb(prev
, skb
);
416 read_unlock(&dgram_lock
);
421 static int dgram_getsockopt(struct sock
*sk
, int level
, int optname
,
422 char __user
*optval
, int __user
*optlen
)
424 struct dgram_sock
*ro
= dgram_sk(sk
);
428 if (level
!= SOL_IEEE802154
)
431 if (get_user(len
, optlen
))
434 len
= min_t(unsigned int, len
, sizeof(int));
441 if (!ro
->secen_override
)
442 val
= WPAN_SECURITY_DEFAULT
;
444 val
= WPAN_SECURITY_ON
;
446 val
= WPAN_SECURITY_OFF
;
448 case WPAN_SECURITY_LEVEL
:
449 if (!ro
->seclevel_override
)
450 val
= WPAN_SECURITY_LEVEL_DEFAULT
;
458 if (put_user(len
, optlen
))
460 if (copy_to_user(optval
, &val
, len
))
465 static int dgram_setsockopt(struct sock
*sk
, int level
, int optname
,
466 char __user
*optval
, unsigned int optlen
)
468 struct dgram_sock
*ro
= dgram_sk(sk
);
469 struct net
*net
= sock_net(sk
);
473 if (optlen
< sizeof(int))
476 if (get_user(val
, (int __user
*)optval
))
483 ro
->want_ack
= !!val
;
486 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
) &&
487 !ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
493 case WPAN_SECURITY_DEFAULT
:
494 ro
->secen_override
= 0;
496 case WPAN_SECURITY_ON
:
497 ro
->secen_override
= 1;
500 case WPAN_SECURITY_OFF
:
501 ro
->secen_override
= 1;
509 case WPAN_SECURITY_LEVEL
:
510 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
) &&
511 !ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
516 if (val
< WPAN_SECURITY_LEVEL_DEFAULT
||
517 val
> IEEE802154_SCF_SECLEVEL_ENC_MIC128
) {
519 } else if (val
== WPAN_SECURITY_LEVEL_DEFAULT
) {
520 ro
->seclevel_override
= 0;
522 ro
->seclevel_override
= 1;
535 struct proto ieee802154_dgram_prot
= {
536 .name
= "IEEE-802.15.4-MAC",
537 .owner
= THIS_MODULE
,
538 .obj_size
= sizeof(struct dgram_sock
),
540 .close
= dgram_close
,
542 .sendmsg
= dgram_sendmsg
,
543 .recvmsg
= dgram_recvmsg
,
545 .unhash
= dgram_unhash
,
546 .connect
= dgram_connect
,
547 .disconnect
= dgram_disconnect
,
548 .ioctl
= dgram_ioctl
,
549 .getsockopt
= dgram_getsockopt
,
550 .setsockopt
= dgram_setsockopt
,