2 * IEEE802154.4 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.
16 * Sergey Lapin <slapin@ossfans.org>
17 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
20 #include <linux/net.h>
21 #include <linux/capability.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
25 #include <linux/termios.h> /* For TIOCOUTQ/INQ */
26 #include <linux/list.h>
27 #include <linux/slab.h>
28 #include <net/datalink.h>
29 #include <net/psnap.h>
31 #include <net/tcp_states.h>
32 #include <net/route.h>
34 #include <net/af_ieee802154.h>
35 #include <net/ieee802154_netdev.h>
37 /* Utility function for families */
38 static struct net_device
*
39 ieee802154_get_dev(struct net
*net
, const struct ieee802154_addr
*addr
)
41 struct net_device
*dev
= NULL
;
42 struct net_device
*tmp
;
43 __le16 pan_id
, short_addr
;
44 u8 hwaddr
[IEEE802154_ADDR_LEN
];
47 case IEEE802154_ADDR_LONG
:
48 ieee802154_devaddr_to_raw(hwaddr
, addr
->extended_addr
);
50 dev
= dev_getbyhwaddr_rcu(net
, ARPHRD_IEEE802154
, hwaddr
);
55 case IEEE802154_ADDR_SHORT
:
56 if (addr
->pan_id
== cpu_to_le16(IEEE802154_PANID_BROADCAST
) ||
57 addr
->short_addr
== cpu_to_le16(IEEE802154_ADDR_UNDEF
) ||
58 addr
->short_addr
== cpu_to_le16(IEEE802154_ADDR_BROADCAST
))
63 for_each_netdev(net
, tmp
) {
64 if (tmp
->type
!= ARPHRD_IEEE802154
)
67 pan_id
= ieee802154_mlme_ops(tmp
)->get_pan_id(tmp
);
69 ieee802154_mlme_ops(tmp
)->get_short_addr(tmp
);
71 if (pan_id
== addr
->pan_id
&&
72 short_addr
== addr
->short_addr
) {
82 pr_warn("Unsupported ieee802154 address type: %d\n",
90 static int ieee802154_sock_release(struct socket
*sock
)
92 struct sock
*sk
= sock
->sk
;
96 sk
->sk_prot
->close(sk
, 0);
101 static int ieee802154_sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
104 struct sock
*sk
= sock
->sk
;
106 return sk
->sk_prot
->sendmsg(sk
, msg
, len
);
109 static int ieee802154_sock_bind(struct socket
*sock
, struct sockaddr
*uaddr
,
112 struct sock
*sk
= sock
->sk
;
114 if (sk
->sk_prot
->bind
)
115 return sk
->sk_prot
->bind(sk
, uaddr
, addr_len
);
117 return sock_no_bind(sock
, uaddr
, addr_len
);
120 static int ieee802154_sock_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
121 int addr_len
, int flags
)
123 struct sock
*sk
= sock
->sk
;
125 if (addr_len
< sizeof(uaddr
->sa_family
))
128 if (uaddr
->sa_family
== AF_UNSPEC
)
129 return sk
->sk_prot
->disconnect(sk
, flags
);
131 return sk
->sk_prot
->connect(sk
, uaddr
, addr_len
);
134 static int ieee802154_dev_ioctl(struct sock
*sk
, struct ifreq __user
*arg
,
138 int ret
= -ENOIOCTLCMD
;
139 struct net_device
*dev
;
141 if (copy_from_user(&ifr
, arg
, sizeof(struct ifreq
)))
144 ifr
.ifr_name
[IFNAMSIZ
-1] = 0;
146 dev_load(sock_net(sk
), ifr
.ifr_name
);
147 dev
= dev_get_by_name(sock_net(sk
), ifr
.ifr_name
);
152 if (dev
->type
== ARPHRD_IEEE802154
&& dev
->netdev_ops
->ndo_do_ioctl
)
153 ret
= dev
->netdev_ops
->ndo_do_ioctl(dev
, &ifr
, cmd
);
155 if (!ret
&& copy_to_user(arg
, &ifr
, sizeof(struct ifreq
)))
162 static int ieee802154_sock_ioctl(struct socket
*sock
, unsigned int cmd
,
165 struct sock
*sk
= sock
->sk
;
169 return sock_get_timestamp(sk
, (struct timeval __user
*)arg
);
171 return sock_get_timestampns(sk
, (struct timespec __user
*)arg
);
174 return ieee802154_dev_ioctl(sk
, (struct ifreq __user
*)arg
,
177 if (!sk
->sk_prot
->ioctl
)
179 return sk
->sk_prot
->ioctl(sk
, cmd
, arg
);
183 /* RAW Sockets (802.15.4 created in userspace) */
184 static HLIST_HEAD(raw_head
);
185 static DEFINE_RWLOCK(raw_lock
);
187 static void raw_hash(struct sock
*sk
)
189 write_lock_bh(&raw_lock
);
190 sk_add_node(sk
, &raw_head
);
191 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, 1);
192 write_unlock_bh(&raw_lock
);
195 static void raw_unhash(struct sock
*sk
)
197 write_lock_bh(&raw_lock
);
198 if (sk_del_node_init(sk
))
199 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, -1);
200 write_unlock_bh(&raw_lock
);
203 static void raw_close(struct sock
*sk
, long timeout
)
205 sk_common_release(sk
);
208 static int raw_bind(struct sock
*sk
, struct sockaddr
*_uaddr
, int len
)
210 struct ieee802154_addr addr
;
211 struct sockaddr_ieee802154
*uaddr
= (struct sockaddr_ieee802154
*)_uaddr
;
213 struct net_device
*dev
= NULL
;
215 if (len
< sizeof(*uaddr
))
218 uaddr
= (struct sockaddr_ieee802154
*)_uaddr
;
219 if (uaddr
->family
!= AF_IEEE802154
)
224 ieee802154_addr_from_sa(&addr
, &uaddr
->addr
);
225 dev
= ieee802154_get_dev(sock_net(sk
), &addr
);
231 if (dev
->type
!= ARPHRD_IEEE802154
) {
236 sk
->sk_bound_dev_if
= dev
->ifindex
;
247 static int raw_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
253 static int raw_disconnect(struct sock
*sk
, int flags
)
258 static int raw_sendmsg(struct sock
*sk
, struct msghdr
*msg
, size_t size
)
260 struct net_device
*dev
;
266 if (msg
->msg_flags
& MSG_OOB
) {
267 pr_debug("msg->msg_flags = 0x%x\n", msg
->msg_flags
);
272 if (!sk
->sk_bound_dev_if
)
273 dev
= dev_getfirstbyhwtype(sock_net(sk
), ARPHRD_IEEE802154
);
275 dev
= dev_get_by_index(sock_net(sk
), sk
->sk_bound_dev_if
);
279 pr_debug("no dev\n");
285 pr_debug("name = %s, mtu = %u\n", dev
->name
, mtu
);
288 pr_debug("size = %Zu, mtu = %u\n", size
, mtu
);
293 hlen
= LL_RESERVED_SPACE(dev
);
294 tlen
= dev
->needed_tailroom
;
295 skb
= sock_alloc_send_skb(sk
, hlen
+ tlen
+ size
,
296 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
300 skb_reserve(skb
, hlen
);
302 skb_reset_mac_header(skb
);
303 skb_reset_network_header(skb
);
305 err
= memcpy_from_msg(skb_put(skb
, size
), msg
, size
);
311 skb
->protocol
= htons(ETH_P_IEEE802154
);
315 err
= dev_queue_xmit(skb
);
317 err
= net_xmit_errno(err
);
329 static int raw_recvmsg(struct sock
*sk
, struct msghdr
*msg
, size_t len
,
330 int noblock
, int flags
, int *addr_len
)
333 int err
= -EOPNOTSUPP
;
336 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
342 msg
->msg_flags
|= MSG_TRUNC
;
346 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
350 sock_recv_ts_and_drops(msg
, sk
, skb
);
352 if (flags
& MSG_TRUNC
)
355 skb_free_datagram(sk
, skb
);
362 static int raw_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
364 skb
= skb_share_check(skb
, GFP_ATOMIC
);
368 if (sock_queue_rcv_skb(sk
, skb
) < 0) {
373 return NET_RX_SUCCESS
;
376 static void ieee802154_raw_deliver(struct net_device
*dev
, struct sk_buff
*skb
)
380 read_lock(&raw_lock
);
381 sk_for_each(sk
, &raw_head
) {
383 if (!sk
->sk_bound_dev_if
||
384 sk
->sk_bound_dev_if
== dev
->ifindex
) {
385 struct sk_buff
*clone
;
387 clone
= skb_clone(skb
, GFP_ATOMIC
);
389 raw_rcv_skb(sk
, clone
);
393 read_unlock(&raw_lock
);
396 static int raw_getsockopt(struct sock
*sk
, int level
, int optname
,
397 char __user
*optval
, int __user
*optlen
)
402 static int raw_setsockopt(struct sock
*sk
, int level
, int optname
,
403 char __user
*optval
, unsigned int optlen
)
408 static struct proto ieee802154_raw_prot
= {
409 .name
= "IEEE-802.15.4-RAW",
410 .owner
= THIS_MODULE
,
411 .obj_size
= sizeof(struct sock
),
414 .sendmsg
= raw_sendmsg
,
415 .recvmsg
= raw_recvmsg
,
417 .unhash
= raw_unhash
,
418 .connect
= raw_connect
,
419 .disconnect
= raw_disconnect
,
420 .getsockopt
= raw_getsockopt
,
421 .setsockopt
= raw_setsockopt
,
424 static const struct proto_ops ieee802154_raw_ops
= {
425 .family
= PF_IEEE802154
,
426 .owner
= THIS_MODULE
,
427 .release
= ieee802154_sock_release
,
428 .bind
= ieee802154_sock_bind
,
429 .connect
= ieee802154_sock_connect
,
430 .socketpair
= sock_no_socketpair
,
431 .accept
= sock_no_accept
,
432 .getname
= sock_no_getname
,
433 .poll
= datagram_poll
,
434 .ioctl
= ieee802154_sock_ioctl
,
435 .listen
= sock_no_listen
,
436 .shutdown
= sock_no_shutdown
,
437 .setsockopt
= sock_common_setsockopt
,
438 .getsockopt
= sock_common_getsockopt
,
439 .sendmsg
= ieee802154_sock_sendmsg
,
440 .recvmsg
= sock_common_recvmsg
,
441 .mmap
= sock_no_mmap
,
442 .sendpage
= sock_no_sendpage
,
444 .compat_setsockopt
= compat_sock_common_setsockopt
,
445 .compat_getsockopt
= compat_sock_common_getsockopt
,
449 /* DGRAM Sockets (802.15.4 dataframes) */
450 static HLIST_HEAD(dgram_head
);
451 static DEFINE_RWLOCK(dgram_lock
);
456 struct ieee802154_addr src_addr
;
457 struct ieee802154_addr dst_addr
;
459 unsigned int bound
:1;
460 unsigned int connected
:1;
461 unsigned int want_ack
:1;
462 unsigned int secen
:1;
463 unsigned int secen_override
:1;
464 unsigned int seclevel
:3;
465 unsigned int seclevel_override
:1;
468 static inline struct dgram_sock
*dgram_sk(const struct sock
*sk
)
470 return container_of(sk
, struct dgram_sock
, sk
);
473 static void dgram_hash(struct sock
*sk
)
475 write_lock_bh(&dgram_lock
);
476 sk_add_node(sk
, &dgram_head
);
477 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, 1);
478 write_unlock_bh(&dgram_lock
);
481 static void dgram_unhash(struct sock
*sk
)
483 write_lock_bh(&dgram_lock
);
484 if (sk_del_node_init(sk
))
485 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, -1);
486 write_unlock_bh(&dgram_lock
);
489 static int dgram_init(struct sock
*sk
)
491 struct dgram_sock
*ro
= dgram_sk(sk
);
497 static void dgram_close(struct sock
*sk
, long timeout
)
499 sk_common_release(sk
);
502 static int dgram_bind(struct sock
*sk
, struct sockaddr
*uaddr
, int len
)
504 struct sockaddr_ieee802154
*addr
= (struct sockaddr_ieee802154
*)uaddr
;
505 struct ieee802154_addr haddr
;
506 struct dgram_sock
*ro
= dgram_sk(sk
);
508 struct net_device
*dev
;
514 if (len
< sizeof(*addr
))
517 if (addr
->family
!= AF_IEEE802154
)
520 ieee802154_addr_from_sa(&haddr
, &addr
->addr
);
521 dev
= ieee802154_get_dev(sock_net(sk
), &haddr
);
527 if (dev
->type
!= ARPHRD_IEEE802154
) {
532 ro
->src_addr
= haddr
;
544 static int dgram_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
549 int amount
= sk_wmem_alloc_get(sk
);
551 return put_user(amount
, (int __user
*)arg
);
557 unsigned long amount
;
560 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
561 skb
= skb_peek(&sk
->sk_receive_queue
);
563 /* We will only return the amount
564 * of this packet since that is all
567 amount
= skb
->len
- ieee802154_hdr_length(skb
);
569 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
570 return put_user(amount
, (int __user
*)arg
);
577 /* FIXME: autobind */
578 static int dgram_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
581 struct sockaddr_ieee802154
*addr
= (struct sockaddr_ieee802154
*)uaddr
;
582 struct dgram_sock
*ro
= dgram_sk(sk
);
585 if (len
< sizeof(*addr
))
588 if (addr
->family
!= AF_IEEE802154
)
598 ieee802154_addr_from_sa(&ro
->dst_addr
, &addr
->addr
);
606 static int dgram_disconnect(struct sock
*sk
, int flags
)
608 struct dgram_sock
*ro
= dgram_sk(sk
);
617 static int dgram_sendmsg(struct sock
*sk
, struct msghdr
*msg
, size_t size
)
619 struct net_device
*dev
;
622 struct ieee802154_mac_cb
*cb
;
623 struct dgram_sock
*ro
= dgram_sk(sk
);
624 struct ieee802154_addr dst_addr
;
628 if (msg
->msg_flags
& MSG_OOB
) {
629 pr_debug("msg->msg_flags = 0x%x\n", msg
->msg_flags
);
633 if (!ro
->connected
&& !msg
->msg_name
)
634 return -EDESTADDRREQ
;
635 else if (ro
->connected
&& msg
->msg_name
)
639 dev
= dev_getfirstbyhwtype(sock_net(sk
), ARPHRD_IEEE802154
);
641 dev
= ieee802154_get_dev(sock_net(sk
), &ro
->src_addr
);
644 pr_debug("no dev\n");
649 pr_debug("name = %s, mtu = %u\n", dev
->name
, mtu
);
652 pr_debug("size = %Zu, mtu = %u\n", size
, mtu
);
657 hlen
= LL_RESERVED_SPACE(dev
);
658 tlen
= dev
->needed_tailroom
;
659 skb
= sock_alloc_send_skb(sk
, hlen
+ tlen
+ size
,
660 msg
->msg_flags
& MSG_DONTWAIT
,
665 skb_reserve(skb
, hlen
);
667 skb_reset_network_header(skb
);
669 cb
= mac_cb_init(skb
);
670 cb
->type
= IEEE802154_FC_TYPE_DATA
;
671 cb
->ackreq
= ro
->want_ack
;
674 DECLARE_SOCKADDR(struct sockaddr_ieee802154
*,
675 daddr
, msg
->msg_name
);
677 ieee802154_addr_from_sa(&dst_addr
, &daddr
->addr
);
679 dst_addr
= ro
->dst_addr
;
682 cb
->secen
= ro
->secen
;
683 cb
->secen_override
= ro
->secen_override
;
684 cb
->seclevel
= ro
->seclevel
;
685 cb
->seclevel_override
= ro
->seclevel_override
;
687 err
= dev_hard_header(skb
, dev
, ETH_P_IEEE802154
, &dst_addr
,
688 ro
->bound
? &ro
->src_addr
: NULL
, size
);
692 err
= memcpy_from_msg(skb_put(skb
, size
), msg
, size
);
698 skb
->protocol
= htons(ETH_P_IEEE802154
);
702 err
= dev_queue_xmit(skb
);
704 err
= net_xmit_errno(err
);
716 static int dgram_recvmsg(struct sock
*sk
, struct msghdr
*msg
, size_t len
,
717 int noblock
, int flags
, int *addr_len
)
720 int err
= -EOPNOTSUPP
;
722 DECLARE_SOCKADDR(struct sockaddr_ieee802154
*, saddr
, msg
->msg_name
);
724 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
730 msg
->msg_flags
|= MSG_TRUNC
;
734 /* FIXME: skip headers if necessary ?! */
735 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
739 sock_recv_ts_and_drops(msg
, sk
, skb
);
742 /* Clear the implicit padding in struct sockaddr_ieee802154
743 * (16 bits between 'family' and 'addr') and in struct
744 * ieee802154_addr_sa (16 bits at the end of the structure).
746 memset(saddr
, 0, sizeof(*saddr
));
748 saddr
->family
= AF_IEEE802154
;
749 ieee802154_addr_to_sa(&saddr
->addr
, &mac_cb(skb
)->source
);
750 *addr_len
= sizeof(*saddr
);
753 if (flags
& MSG_TRUNC
)
756 skb_free_datagram(sk
, skb
);
763 static int dgram_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
765 skb
= skb_share_check(skb
, GFP_ATOMIC
);
769 if (sock_queue_rcv_skb(sk
, skb
) < 0) {
774 return NET_RX_SUCCESS
;
778 ieee802154_match_sock(__le64 hw_addr
, __le16 pan_id
, __le16 short_addr
,
779 struct dgram_sock
*ro
)
784 if (ro
->src_addr
.mode
== IEEE802154_ADDR_LONG
&&
785 hw_addr
== ro
->src_addr
.extended_addr
)
788 if (ro
->src_addr
.mode
== IEEE802154_ADDR_SHORT
&&
789 pan_id
== ro
->src_addr
.pan_id
&&
790 short_addr
== ro
->src_addr
.short_addr
)
796 static int ieee802154_dgram_deliver(struct net_device
*dev
, struct sk_buff
*skb
)
798 struct sock
*sk
, *prev
= NULL
;
799 int ret
= NET_RX_SUCCESS
;
800 __le16 pan_id
, short_addr
;
803 /* Data frame processing */
804 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
806 pan_id
= ieee802154_mlme_ops(dev
)->get_pan_id(dev
);
807 short_addr
= ieee802154_mlme_ops(dev
)->get_short_addr(dev
);
808 hw_addr
= ieee802154_devaddr_from_raw(dev
->dev_addr
);
810 read_lock(&dgram_lock
);
811 sk_for_each(sk
, &dgram_head
) {
812 if (ieee802154_match_sock(hw_addr
, pan_id
, short_addr
,
815 struct sk_buff
*clone
;
817 clone
= skb_clone(skb
, GFP_ATOMIC
);
819 dgram_rcv_skb(prev
, clone
);
827 dgram_rcv_skb(prev
, skb
);
832 read_unlock(&dgram_lock
);
837 static int dgram_getsockopt(struct sock
*sk
, int level
, int optname
,
838 char __user
*optval
, int __user
*optlen
)
840 struct dgram_sock
*ro
= dgram_sk(sk
);
844 if (level
!= SOL_IEEE802154
)
847 if (get_user(len
, optlen
))
850 len
= min_t(unsigned int, len
, sizeof(int));
857 if (!ro
->secen_override
)
858 val
= WPAN_SECURITY_DEFAULT
;
860 val
= WPAN_SECURITY_ON
;
862 val
= WPAN_SECURITY_OFF
;
864 case WPAN_SECURITY_LEVEL
:
865 if (!ro
->seclevel_override
)
866 val
= WPAN_SECURITY_LEVEL_DEFAULT
;
874 if (put_user(len
, optlen
))
876 if (copy_to_user(optval
, &val
, len
))
881 static int dgram_setsockopt(struct sock
*sk
, int level
, int optname
,
882 char __user
*optval
, unsigned int optlen
)
884 struct dgram_sock
*ro
= dgram_sk(sk
);
885 struct net
*net
= sock_net(sk
);
889 if (optlen
< sizeof(int))
892 if (get_user(val
, (int __user
*)optval
))
899 ro
->want_ack
= !!val
;
902 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
) &&
903 !ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
909 case WPAN_SECURITY_DEFAULT
:
910 ro
->secen_override
= 0;
912 case WPAN_SECURITY_ON
:
913 ro
->secen_override
= 1;
916 case WPAN_SECURITY_OFF
:
917 ro
->secen_override
= 1;
925 case WPAN_SECURITY_LEVEL
:
926 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
) &&
927 !ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
932 if (val
< WPAN_SECURITY_LEVEL_DEFAULT
||
933 val
> IEEE802154_SCF_SECLEVEL_ENC_MIC128
) {
935 } else if (val
== WPAN_SECURITY_LEVEL_DEFAULT
) {
936 ro
->seclevel_override
= 0;
938 ro
->seclevel_override
= 1;
951 static struct proto ieee802154_dgram_prot
= {
952 .name
= "IEEE-802.15.4-MAC",
953 .owner
= THIS_MODULE
,
954 .obj_size
= sizeof(struct dgram_sock
),
956 .close
= dgram_close
,
958 .sendmsg
= dgram_sendmsg
,
959 .recvmsg
= dgram_recvmsg
,
961 .unhash
= dgram_unhash
,
962 .connect
= dgram_connect
,
963 .disconnect
= dgram_disconnect
,
964 .ioctl
= dgram_ioctl
,
965 .getsockopt
= dgram_getsockopt
,
966 .setsockopt
= dgram_setsockopt
,
969 static const struct proto_ops ieee802154_dgram_ops
= {
970 .family
= PF_IEEE802154
,
971 .owner
= THIS_MODULE
,
972 .release
= ieee802154_sock_release
,
973 .bind
= ieee802154_sock_bind
,
974 .connect
= ieee802154_sock_connect
,
975 .socketpair
= sock_no_socketpair
,
976 .accept
= sock_no_accept
,
977 .getname
= sock_no_getname
,
978 .poll
= datagram_poll
,
979 .ioctl
= ieee802154_sock_ioctl
,
980 .listen
= sock_no_listen
,
981 .shutdown
= sock_no_shutdown
,
982 .setsockopt
= sock_common_setsockopt
,
983 .getsockopt
= sock_common_getsockopt
,
984 .sendmsg
= ieee802154_sock_sendmsg
,
985 .recvmsg
= sock_common_recvmsg
,
986 .mmap
= sock_no_mmap
,
987 .sendpage
= sock_no_sendpage
,
989 .compat_setsockopt
= compat_sock_common_setsockopt
,
990 .compat_getsockopt
= compat_sock_common_getsockopt
,
994 /* Create a socket. Initialise the socket, blank the addresses
997 static int ieee802154_create(struct net
*net
, struct socket
*sock
,
998 int protocol
, int kern
)
1002 struct proto
*proto
;
1003 const struct proto_ops
*ops
;
1005 if (!net_eq(net
, &init_net
))
1006 return -EAFNOSUPPORT
;
1008 switch (sock
->type
) {
1010 proto
= &ieee802154_raw_prot
;
1011 ops
= &ieee802154_raw_ops
;
1014 proto
= &ieee802154_dgram_prot
;
1015 ops
= &ieee802154_dgram_ops
;
1018 rc
= -ESOCKTNOSUPPORT
;
1023 sk
= sk_alloc(net
, PF_IEEE802154
, GFP_KERNEL
, proto
);
1030 sock_init_data(sock
, sk
);
1031 /* FIXME: sk->sk_destruct */
1032 sk
->sk_family
= PF_IEEE802154
;
1034 /* Checksums on by default */
1035 sock_set_flag(sk
, SOCK_ZAPPED
);
1037 if (sk
->sk_prot
->hash
)
1038 sk
->sk_prot
->hash(sk
);
1040 if (sk
->sk_prot
->init
) {
1041 rc
= sk
->sk_prot
->init(sk
);
1043 sk_common_release(sk
);
1049 static const struct net_proto_family ieee802154_family_ops
= {
1050 .family
= PF_IEEE802154
,
1051 .create
= ieee802154_create
,
1052 .owner
= THIS_MODULE
,
1055 static int ieee802154_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
1056 struct packet_type
*pt
, struct net_device
*orig_dev
)
1058 if (!netif_running(dev
))
1060 pr_debug("got frame, type %d, dev %p\n", dev
->type
, dev
);
1062 print_hex_dump_bytes("ieee802154_rcv ",
1063 DUMP_PREFIX_NONE
, skb
->data
, skb
->len
);
1066 if (!net_eq(dev_net(dev
), &init_net
))
1069 ieee802154_raw_deliver(dev
, skb
);
1071 if (dev
->type
!= ARPHRD_IEEE802154
)
1074 if (skb
->pkt_type
!= PACKET_OTHERHOST
)
1075 return ieee802154_dgram_deliver(dev
, skb
);
1082 static struct packet_type ieee802154_packet_type
= {
1083 .type
= htons(ETH_P_IEEE802154
),
1084 .func
= ieee802154_rcv
,
1087 static int __init
af_ieee802154_init(void)
1091 rc
= proto_register(&ieee802154_raw_prot
, 1);
1095 rc
= proto_register(&ieee802154_dgram_prot
, 1);
1099 /* Tell SOCKET that we are alive */
1100 rc
= sock_register(&ieee802154_family_ops
);
1103 dev_add_pack(&ieee802154_packet_type
);
1109 proto_unregister(&ieee802154_dgram_prot
);
1111 proto_unregister(&ieee802154_raw_prot
);
1116 static void __exit
af_ieee802154_remove(void)
1118 dev_remove_pack(&ieee802154_packet_type
);
1119 sock_unregister(PF_IEEE802154
);
1120 proto_unregister(&ieee802154_dgram_prot
);
1121 proto_unregister(&ieee802154_raw_prot
);
1124 module_init(af_ieee802154_init
);
1125 module_exit(af_ieee802154_remove
);
1127 MODULE_LICENSE("GPL");
1128 MODULE_ALIAS_NETPROTO(PF_IEEE802154
);