1 // SPDX-License-Identifier: GPL-2.0-only
3 * IEEE802154.4 socket interface
5 * Copyright 2007, 2008 Siemens AG
8 * Sergey Lapin <slapin@ossfans.org>
9 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
12 #include <linux/net.h>
13 #include <linux/capability.h>
14 #include <linux/module.h>
15 #include <linux/if_arp.h>
17 #include <linux/termios.h> /* For TIOCOUTQ/INQ */
18 #include <linux/list.h>
19 #include <linux/slab.h>
20 #include <linux/socket.h>
21 #include <net/datalink.h>
22 #include <net/psnap.h>
24 #include <net/tcp_states.h>
25 #include <net/route.h>
27 #include <net/af_ieee802154.h>
28 #include <net/ieee802154_netdev.h>
30 /* Utility function for families */
31 static struct net_device
*
32 ieee802154_get_dev(struct net
*net
, const struct ieee802154_addr
*addr
)
34 struct net_device
*dev
= NULL
;
35 struct net_device
*tmp
;
36 __le16 pan_id
, short_addr
;
37 u8 hwaddr
[IEEE802154_ADDR_LEN
];
40 case IEEE802154_ADDR_LONG
:
41 ieee802154_devaddr_to_raw(hwaddr
, addr
->extended_addr
);
43 dev
= dev_getbyhwaddr_rcu(net
, ARPHRD_IEEE802154
, hwaddr
);
48 case IEEE802154_ADDR_SHORT
:
49 if (addr
->pan_id
== cpu_to_le16(IEEE802154_PANID_BROADCAST
) ||
50 addr
->short_addr
== cpu_to_le16(IEEE802154_ADDR_UNDEF
) ||
51 addr
->short_addr
== cpu_to_le16(IEEE802154_ADDR_BROADCAST
))
56 for_each_netdev(net
, tmp
) {
57 if (tmp
->type
!= ARPHRD_IEEE802154
)
60 pan_id
= tmp
->ieee802154_ptr
->pan_id
;
61 short_addr
= tmp
->ieee802154_ptr
->short_addr
;
62 if (pan_id
== addr
->pan_id
&&
63 short_addr
== addr
->short_addr
) {
73 pr_warn("Unsupported ieee802154 address type: %d\n",
81 static int ieee802154_sock_release(struct socket
*sock
)
83 struct sock
*sk
= sock
->sk
;
87 sk
->sk_prot
->close(sk
, 0);
92 static int ieee802154_sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
95 struct sock
*sk
= sock
->sk
;
97 return sk
->sk_prot
->sendmsg(sk
, msg
, len
);
100 static int ieee802154_sock_bind(struct socket
*sock
, struct sockaddr
*uaddr
,
103 struct sock
*sk
= sock
->sk
;
105 if (sk
->sk_prot
->bind
)
106 return sk
->sk_prot
->bind(sk
, uaddr
, addr_len
);
108 return sock_no_bind(sock
, uaddr
, addr_len
);
111 static int ieee802154_sock_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
112 int addr_len
, int flags
)
114 struct sock
*sk
= sock
->sk
;
116 if (addr_len
< sizeof(uaddr
->sa_family
))
119 if (uaddr
->sa_family
== AF_UNSPEC
)
120 return sk
->sk_prot
->disconnect(sk
, flags
);
122 return sk
->sk_prot
->connect(sk
, uaddr
, addr_len
);
125 static int ieee802154_dev_ioctl(struct sock
*sk
, struct ifreq __user
*arg
,
129 int ret
= -ENOIOCTLCMD
;
130 struct net_device
*dev
;
132 if (copy_from_user(&ifr
, arg
, sizeof(struct ifreq
)))
135 ifr
.ifr_name
[IFNAMSIZ
-1] = 0;
137 dev_load(sock_net(sk
), ifr
.ifr_name
);
138 dev
= dev_get_by_name(sock_net(sk
), ifr
.ifr_name
);
143 if (dev
->type
== ARPHRD_IEEE802154
&& dev
->netdev_ops
->ndo_do_ioctl
)
144 ret
= dev
->netdev_ops
->ndo_do_ioctl(dev
, &ifr
, cmd
);
146 if (!ret
&& copy_to_user(arg
, &ifr
, sizeof(struct ifreq
)))
153 static int ieee802154_sock_ioctl(struct socket
*sock
, unsigned int cmd
,
156 struct sock
*sk
= sock
->sk
;
161 return ieee802154_dev_ioctl(sk
, (struct ifreq __user
*)arg
,
164 if (!sk
->sk_prot
->ioctl
)
166 return sk
->sk_prot
->ioctl(sk
, cmd
, arg
);
170 /* RAW Sockets (802.15.4 created in userspace) */
171 static HLIST_HEAD(raw_head
);
172 static DEFINE_RWLOCK(raw_lock
);
174 static int raw_hash(struct sock
*sk
)
176 write_lock_bh(&raw_lock
);
177 sk_add_node(sk
, &raw_head
);
178 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, 1);
179 write_unlock_bh(&raw_lock
);
184 static void raw_unhash(struct sock
*sk
)
186 write_lock_bh(&raw_lock
);
187 if (sk_del_node_init(sk
))
188 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, -1);
189 write_unlock_bh(&raw_lock
);
192 static void raw_close(struct sock
*sk
, long timeout
)
194 sk_common_release(sk
);
197 static int raw_bind(struct sock
*sk
, struct sockaddr
*_uaddr
, int len
)
199 struct ieee802154_addr addr
;
200 struct sockaddr_ieee802154
*uaddr
= (struct sockaddr_ieee802154
*)_uaddr
;
202 struct net_device
*dev
= NULL
;
204 if (len
< sizeof(*uaddr
))
207 uaddr
= (struct sockaddr_ieee802154
*)_uaddr
;
208 if (uaddr
->family
!= AF_IEEE802154
)
213 ieee802154_addr_from_sa(&addr
, &uaddr
->addr
);
214 dev
= ieee802154_get_dev(sock_net(sk
), &addr
);
220 sk
->sk_bound_dev_if
= dev
->ifindex
;
230 static int raw_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
236 static int raw_disconnect(struct sock
*sk
, int flags
)
241 static int raw_sendmsg(struct sock
*sk
, struct msghdr
*msg
, size_t size
)
243 struct net_device
*dev
;
249 if (msg
->msg_flags
& MSG_OOB
) {
250 pr_debug("msg->msg_flags = 0x%x\n", msg
->msg_flags
);
255 if (!sk
->sk_bound_dev_if
)
256 dev
= dev_getfirstbyhwtype(sock_net(sk
), ARPHRD_IEEE802154
);
258 dev
= dev_get_by_index(sock_net(sk
), sk
->sk_bound_dev_if
);
262 pr_debug("no dev\n");
267 mtu
= IEEE802154_MTU
;
268 pr_debug("name = %s, mtu = %u\n", dev
->name
, mtu
);
271 pr_debug("size = %zu, mtu = %u\n", size
, mtu
);
276 hlen
= LL_RESERVED_SPACE(dev
);
277 tlen
= dev
->needed_tailroom
;
278 skb
= sock_alloc_send_skb(sk
, hlen
+ tlen
+ size
,
279 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
283 skb_reserve(skb
, hlen
);
285 skb_reset_mac_header(skb
);
286 skb_reset_network_header(skb
);
288 err
= memcpy_from_msg(skb_put(skb
, size
), msg
, size
);
293 skb
->protocol
= htons(ETH_P_IEEE802154
);
295 err
= dev_queue_xmit(skb
);
297 err
= net_xmit_errno(err
);
311 static int raw_recvmsg(struct sock
*sk
, struct msghdr
*msg
, size_t len
,
312 int noblock
, int flags
, int *addr_len
)
315 int err
= -EOPNOTSUPP
;
318 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
324 msg
->msg_flags
|= MSG_TRUNC
;
328 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
332 sock_recv_ts_and_drops(msg
, sk
, skb
);
334 if (flags
& MSG_TRUNC
)
337 skb_free_datagram(sk
, skb
);
344 static int raw_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
346 skb
= skb_share_check(skb
, GFP_ATOMIC
);
350 if (sock_queue_rcv_skb(sk
, skb
) < 0) {
355 return NET_RX_SUCCESS
;
358 static void ieee802154_raw_deliver(struct net_device
*dev
, struct sk_buff
*skb
)
362 read_lock(&raw_lock
);
363 sk_for_each(sk
, &raw_head
) {
365 if (!sk
->sk_bound_dev_if
||
366 sk
->sk_bound_dev_if
== dev
->ifindex
) {
367 struct sk_buff
*clone
;
369 clone
= skb_clone(skb
, GFP_ATOMIC
);
371 raw_rcv_skb(sk
, clone
);
375 read_unlock(&raw_lock
);
378 static int raw_getsockopt(struct sock
*sk
, int level
, int optname
,
379 char __user
*optval
, int __user
*optlen
)
384 static int raw_setsockopt(struct sock
*sk
, int level
, int optname
,
385 char __user
*optval
, unsigned int optlen
)
390 static struct proto ieee802154_raw_prot
= {
391 .name
= "IEEE-802.15.4-RAW",
392 .owner
= THIS_MODULE
,
393 .obj_size
= sizeof(struct sock
),
396 .sendmsg
= raw_sendmsg
,
397 .recvmsg
= raw_recvmsg
,
399 .unhash
= raw_unhash
,
400 .connect
= raw_connect
,
401 .disconnect
= raw_disconnect
,
402 .getsockopt
= raw_getsockopt
,
403 .setsockopt
= raw_setsockopt
,
406 static const struct proto_ops ieee802154_raw_ops
= {
407 .family
= PF_IEEE802154
,
408 .owner
= THIS_MODULE
,
409 .release
= ieee802154_sock_release
,
410 .bind
= ieee802154_sock_bind
,
411 .connect
= ieee802154_sock_connect
,
412 .socketpair
= sock_no_socketpair
,
413 .accept
= sock_no_accept
,
414 .getname
= sock_no_getname
,
415 .poll
= datagram_poll
,
416 .ioctl
= ieee802154_sock_ioctl
,
417 .gettstamp
= sock_gettstamp
,
418 .listen
= sock_no_listen
,
419 .shutdown
= sock_no_shutdown
,
420 .setsockopt
= sock_common_setsockopt
,
421 .getsockopt
= sock_common_getsockopt
,
422 .sendmsg
= ieee802154_sock_sendmsg
,
423 .recvmsg
= sock_common_recvmsg
,
424 .mmap
= sock_no_mmap
,
425 .sendpage
= sock_no_sendpage
,
427 .compat_setsockopt
= compat_sock_common_setsockopt
,
428 .compat_getsockopt
= compat_sock_common_getsockopt
,
432 /* DGRAM Sockets (802.15.4 dataframes) */
433 static HLIST_HEAD(dgram_head
);
434 static DEFINE_RWLOCK(dgram_lock
);
439 struct ieee802154_addr src_addr
;
440 struct ieee802154_addr dst_addr
;
442 unsigned int bound
:1;
443 unsigned int connected
:1;
444 unsigned int want_ack
:1;
445 unsigned int want_lqi
:1;
446 unsigned int secen
:1;
447 unsigned int secen_override
:1;
448 unsigned int seclevel
:3;
449 unsigned int seclevel_override
:1;
452 static inline struct dgram_sock
*dgram_sk(const struct sock
*sk
)
454 return container_of(sk
, struct dgram_sock
, sk
);
457 static int dgram_hash(struct sock
*sk
)
459 write_lock_bh(&dgram_lock
);
460 sk_add_node(sk
, &dgram_head
);
461 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, 1);
462 write_unlock_bh(&dgram_lock
);
467 static void dgram_unhash(struct sock
*sk
)
469 write_lock_bh(&dgram_lock
);
470 if (sk_del_node_init(sk
))
471 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, -1);
472 write_unlock_bh(&dgram_lock
);
475 static int dgram_init(struct sock
*sk
)
477 struct dgram_sock
*ro
= dgram_sk(sk
);
484 static void dgram_close(struct sock
*sk
, long timeout
)
486 sk_common_release(sk
);
489 static int dgram_bind(struct sock
*sk
, struct sockaddr
*uaddr
, int len
)
491 struct sockaddr_ieee802154
*addr
= (struct sockaddr_ieee802154
*)uaddr
;
492 struct ieee802154_addr haddr
;
493 struct dgram_sock
*ro
= dgram_sk(sk
);
495 struct net_device
*dev
;
501 if (len
< sizeof(*addr
))
504 if (addr
->family
!= AF_IEEE802154
)
507 ieee802154_addr_from_sa(&haddr
, &addr
->addr
);
508 dev
= ieee802154_get_dev(sock_net(sk
), &haddr
);
514 if (dev
->type
!= ARPHRD_IEEE802154
) {
519 ro
->src_addr
= haddr
;
531 static int dgram_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
536 int amount
= sk_wmem_alloc_get(sk
);
538 return put_user(amount
, (int __user
*)arg
);
544 unsigned long amount
;
547 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
548 skb
= skb_peek(&sk
->sk_receive_queue
);
550 /* We will only return the amount
551 * of this packet since that is all
554 amount
= skb
->len
- ieee802154_hdr_length(skb
);
556 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
557 return put_user(amount
, (int __user
*)arg
);
564 /* FIXME: autobind */
565 static int dgram_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
568 struct sockaddr_ieee802154
*addr
= (struct sockaddr_ieee802154
*)uaddr
;
569 struct dgram_sock
*ro
= dgram_sk(sk
);
572 if (len
< sizeof(*addr
))
575 if (addr
->family
!= AF_IEEE802154
)
585 ieee802154_addr_from_sa(&ro
->dst_addr
, &addr
->addr
);
593 static int dgram_disconnect(struct sock
*sk
, int flags
)
595 struct dgram_sock
*ro
= dgram_sk(sk
);
604 static int dgram_sendmsg(struct sock
*sk
, struct msghdr
*msg
, size_t size
)
606 struct net_device
*dev
;
609 struct ieee802154_mac_cb
*cb
;
610 struct dgram_sock
*ro
= dgram_sk(sk
);
611 struct ieee802154_addr dst_addr
;
615 if (msg
->msg_flags
& MSG_OOB
) {
616 pr_debug("msg->msg_flags = 0x%x\n", msg
->msg_flags
);
620 if (!ro
->connected
&& !msg
->msg_name
)
621 return -EDESTADDRREQ
;
622 else if (ro
->connected
&& msg
->msg_name
)
626 dev
= dev_getfirstbyhwtype(sock_net(sk
), ARPHRD_IEEE802154
);
628 dev
= ieee802154_get_dev(sock_net(sk
), &ro
->src_addr
);
631 pr_debug("no dev\n");
635 mtu
= IEEE802154_MTU
;
636 pr_debug("name = %s, mtu = %u\n", dev
->name
, mtu
);
639 pr_debug("size = %zu, mtu = %u\n", size
, mtu
);
644 hlen
= LL_RESERVED_SPACE(dev
);
645 tlen
= dev
->needed_tailroom
;
646 skb
= sock_alloc_send_skb(sk
, hlen
+ tlen
+ size
,
647 msg
->msg_flags
& MSG_DONTWAIT
,
652 skb_reserve(skb
, hlen
);
654 skb_reset_network_header(skb
);
656 cb
= mac_cb_init(skb
);
657 cb
->type
= IEEE802154_FC_TYPE_DATA
;
658 cb
->ackreq
= ro
->want_ack
;
661 DECLARE_SOCKADDR(struct sockaddr_ieee802154
*,
662 daddr
, msg
->msg_name
);
664 ieee802154_addr_from_sa(&dst_addr
, &daddr
->addr
);
666 dst_addr
= ro
->dst_addr
;
669 cb
->secen
= ro
->secen
;
670 cb
->secen_override
= ro
->secen_override
;
671 cb
->seclevel
= ro
->seclevel
;
672 cb
->seclevel_override
= ro
->seclevel_override
;
674 err
= wpan_dev_hard_header(skb
, dev
, &dst_addr
,
675 ro
->bound
? &ro
->src_addr
: NULL
, size
);
679 err
= memcpy_from_msg(skb_put(skb
, size
), msg
, size
);
684 skb
->protocol
= htons(ETH_P_IEEE802154
);
686 err
= dev_queue_xmit(skb
);
688 err
= net_xmit_errno(err
);
702 static int dgram_recvmsg(struct sock
*sk
, struct msghdr
*msg
, size_t len
,
703 int noblock
, int flags
, int *addr_len
)
706 int err
= -EOPNOTSUPP
;
708 struct dgram_sock
*ro
= dgram_sk(sk
);
709 DECLARE_SOCKADDR(struct sockaddr_ieee802154
*, saddr
, msg
->msg_name
);
711 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
717 msg
->msg_flags
|= MSG_TRUNC
;
721 /* FIXME: skip headers if necessary ?! */
722 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
726 sock_recv_ts_and_drops(msg
, sk
, skb
);
729 /* Clear the implicit padding in struct sockaddr_ieee802154
730 * (16 bits between 'family' and 'addr') and in struct
731 * ieee802154_addr_sa (16 bits at the end of the structure).
733 memset(saddr
, 0, sizeof(*saddr
));
735 saddr
->family
= AF_IEEE802154
;
736 ieee802154_addr_to_sa(&saddr
->addr
, &mac_cb(skb
)->source
);
737 *addr_len
= sizeof(*saddr
);
741 err
= put_cmsg(msg
, SOL_IEEE802154
, WPAN_WANTLQI
,
742 sizeof(uint8_t), &(mac_cb(skb
)->lqi
));
747 if (flags
& MSG_TRUNC
)
750 skb_free_datagram(sk
, skb
);
757 static int dgram_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
759 skb
= skb_share_check(skb
, GFP_ATOMIC
);
763 if (sock_queue_rcv_skb(sk
, skb
) < 0) {
768 return NET_RX_SUCCESS
;
772 ieee802154_match_sock(__le64 hw_addr
, __le16 pan_id
, __le16 short_addr
,
773 struct dgram_sock
*ro
)
778 if (ro
->src_addr
.mode
== IEEE802154_ADDR_LONG
&&
779 hw_addr
== ro
->src_addr
.extended_addr
)
782 if (ro
->src_addr
.mode
== IEEE802154_ADDR_SHORT
&&
783 pan_id
== ro
->src_addr
.pan_id
&&
784 short_addr
== ro
->src_addr
.short_addr
)
790 static int ieee802154_dgram_deliver(struct net_device
*dev
, struct sk_buff
*skb
)
792 struct sock
*sk
, *prev
= NULL
;
793 int ret
= NET_RX_SUCCESS
;
794 __le16 pan_id
, short_addr
;
797 /* Data frame processing */
798 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
800 pan_id
= dev
->ieee802154_ptr
->pan_id
;
801 short_addr
= dev
->ieee802154_ptr
->short_addr
;
802 hw_addr
= dev
->ieee802154_ptr
->extended_addr
;
804 read_lock(&dgram_lock
);
805 sk_for_each(sk
, &dgram_head
) {
806 if (ieee802154_match_sock(hw_addr
, pan_id
, short_addr
,
809 struct sk_buff
*clone
;
811 clone
= skb_clone(skb
, GFP_ATOMIC
);
813 dgram_rcv_skb(prev
, clone
);
821 dgram_rcv_skb(prev
, skb
);
826 read_unlock(&dgram_lock
);
831 static int dgram_getsockopt(struct sock
*sk
, int level
, int optname
,
832 char __user
*optval
, int __user
*optlen
)
834 struct dgram_sock
*ro
= dgram_sk(sk
);
838 if (level
!= SOL_IEEE802154
)
841 if (get_user(len
, optlen
))
844 len
= min_t(unsigned int, len
, sizeof(int));
854 if (!ro
->secen_override
)
855 val
= WPAN_SECURITY_DEFAULT
;
857 val
= WPAN_SECURITY_ON
;
859 val
= WPAN_SECURITY_OFF
;
861 case WPAN_SECURITY_LEVEL
:
862 if (!ro
->seclevel_override
)
863 val
= WPAN_SECURITY_LEVEL_DEFAULT
;
871 if (put_user(len
, optlen
))
873 if (copy_to_user(optval
, &val
, len
))
878 static int dgram_setsockopt(struct sock
*sk
, int level
, int optname
,
879 char __user
*optval
, unsigned int optlen
)
881 struct dgram_sock
*ro
= dgram_sk(sk
);
882 struct net
*net
= sock_net(sk
);
886 if (optlen
< sizeof(int))
889 if (get_user(val
, (int __user
*)optval
))
896 ro
->want_ack
= !!val
;
899 ro
->want_lqi
= !!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 .gettstamp
= sock_gettstamp
,
981 .listen
= sock_no_listen
,
982 .shutdown
= sock_no_shutdown
,
983 .setsockopt
= sock_common_setsockopt
,
984 .getsockopt
= sock_common_getsockopt
,
985 .sendmsg
= ieee802154_sock_sendmsg
,
986 .recvmsg
= sock_common_recvmsg
,
987 .mmap
= sock_no_mmap
,
988 .sendpage
= sock_no_sendpage
,
990 .compat_setsockopt
= compat_sock_common_setsockopt
,
991 .compat_getsockopt
= compat_sock_common_getsockopt
,
995 /* Create a socket. Initialise the socket, blank the addresses
998 static int ieee802154_create(struct net
*net
, struct socket
*sock
,
999 int protocol
, int kern
)
1003 struct proto
*proto
;
1004 const struct proto_ops
*ops
;
1006 if (!net_eq(net
, &init_net
))
1007 return -EAFNOSUPPORT
;
1009 switch (sock
->type
) {
1012 if (!capable(CAP_NET_RAW
))
1014 proto
= &ieee802154_raw_prot
;
1015 ops
= &ieee802154_raw_ops
;
1018 proto
= &ieee802154_dgram_prot
;
1019 ops
= &ieee802154_dgram_ops
;
1022 rc
= -ESOCKTNOSUPPORT
;
1027 sk
= sk_alloc(net
, PF_IEEE802154
, GFP_KERNEL
, proto
, kern
);
1034 sock_init_data(sock
, sk
);
1035 /* FIXME: sk->sk_destruct */
1036 sk
->sk_family
= PF_IEEE802154
;
1038 /* Checksums on by default */
1039 sock_set_flag(sk
, SOCK_ZAPPED
);
1041 if (sk
->sk_prot
->hash
) {
1042 rc
= sk
->sk_prot
->hash(sk
);
1044 sk_common_release(sk
);
1049 if (sk
->sk_prot
->init
) {
1050 rc
= sk
->sk_prot
->init(sk
);
1052 sk_common_release(sk
);
1058 static const struct net_proto_family ieee802154_family_ops
= {
1059 .family
= PF_IEEE802154
,
1060 .create
= ieee802154_create
,
1061 .owner
= THIS_MODULE
,
1064 static int ieee802154_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
1065 struct packet_type
*pt
, struct net_device
*orig_dev
)
1067 if (!netif_running(dev
))
1069 pr_debug("got frame, type %d, dev %p\n", dev
->type
, dev
);
1071 print_hex_dump_bytes("ieee802154_rcv ",
1072 DUMP_PREFIX_NONE
, skb
->data
, skb
->len
);
1075 if (!net_eq(dev_net(dev
), &init_net
))
1078 ieee802154_raw_deliver(dev
, skb
);
1080 if (dev
->type
!= ARPHRD_IEEE802154
)
1083 if (skb
->pkt_type
!= PACKET_OTHERHOST
)
1084 return ieee802154_dgram_deliver(dev
, skb
);
1091 static struct packet_type ieee802154_packet_type
= {
1092 .type
= htons(ETH_P_IEEE802154
),
1093 .func
= ieee802154_rcv
,
1096 static int __init
af_ieee802154_init(void)
1100 rc
= proto_register(&ieee802154_raw_prot
, 1);
1104 rc
= proto_register(&ieee802154_dgram_prot
, 1);
1108 /* Tell SOCKET that we are alive */
1109 rc
= sock_register(&ieee802154_family_ops
);
1112 dev_add_pack(&ieee802154_packet_type
);
1118 proto_unregister(&ieee802154_dgram_prot
);
1120 proto_unregister(&ieee802154_raw_prot
);
1125 static void __exit
af_ieee802154_remove(void)
1127 dev_remove_pack(&ieee802154_packet_type
);
1128 sock_unregister(PF_IEEE802154
);
1129 proto_unregister(&ieee802154_dgram_prot
);
1130 proto_unregister(&ieee802154_raw_prot
);
1133 module_init(af_ieee802154_init
);
1134 module_exit(af_ieee802154_remove
);
1136 MODULE_LICENSE("GPL");
1137 MODULE_ALIAS_NETPROTO(PF_IEEE802154
);