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
= tmp
->ieee802154_ptr
->pan_id
;
68 short_addr
= tmp
->ieee802154_ptr
->short_addr
;
69 if (pan_id
== addr
->pan_id
&&
70 short_addr
== addr
->short_addr
) {
80 pr_warn("Unsupported ieee802154 address type: %d\n",
88 static int ieee802154_sock_release(struct socket
*sock
)
90 struct sock
*sk
= sock
->sk
;
94 sk
->sk_prot
->close(sk
, 0);
99 static int ieee802154_sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
102 struct sock
*sk
= sock
->sk
;
104 return sk
->sk_prot
->sendmsg(sk
, msg
, len
);
107 static int ieee802154_sock_bind(struct socket
*sock
, struct sockaddr
*uaddr
,
110 struct sock
*sk
= sock
->sk
;
112 if (sk
->sk_prot
->bind
)
113 return sk
->sk_prot
->bind(sk
, uaddr
, addr_len
);
115 return sock_no_bind(sock
, uaddr
, addr_len
);
118 static int ieee802154_sock_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
119 int addr_len
, int flags
)
121 struct sock
*sk
= sock
->sk
;
123 if (addr_len
< sizeof(uaddr
->sa_family
))
126 if (uaddr
->sa_family
== AF_UNSPEC
)
127 return sk
->sk_prot
->disconnect(sk
, flags
);
129 return sk
->sk_prot
->connect(sk
, uaddr
, addr_len
);
132 static int ieee802154_dev_ioctl(struct sock
*sk
, struct ifreq __user
*arg
,
136 int ret
= -ENOIOCTLCMD
;
137 struct net_device
*dev
;
139 if (copy_from_user(&ifr
, arg
, sizeof(struct ifreq
)))
142 ifr
.ifr_name
[IFNAMSIZ
-1] = 0;
144 dev_load(sock_net(sk
), ifr
.ifr_name
);
145 dev
= dev_get_by_name(sock_net(sk
), ifr
.ifr_name
);
150 if (dev
->type
== ARPHRD_IEEE802154
&& dev
->netdev_ops
->ndo_do_ioctl
)
151 ret
= dev
->netdev_ops
->ndo_do_ioctl(dev
, &ifr
, cmd
);
153 if (!ret
&& copy_to_user(arg
, &ifr
, sizeof(struct ifreq
)))
160 static int ieee802154_sock_ioctl(struct socket
*sock
, unsigned int cmd
,
163 struct sock
*sk
= sock
->sk
;
167 return sock_get_timestamp(sk
, (struct timeval __user
*)arg
);
169 return sock_get_timestampns(sk
, (struct timespec __user
*)arg
);
172 return ieee802154_dev_ioctl(sk
, (struct ifreq __user
*)arg
,
175 if (!sk
->sk_prot
->ioctl
)
177 return sk
->sk_prot
->ioctl(sk
, cmd
, arg
);
181 /* RAW Sockets (802.15.4 created in userspace) */
182 static HLIST_HEAD(raw_head
);
183 static DEFINE_RWLOCK(raw_lock
);
185 static int raw_hash(struct sock
*sk
)
187 write_lock_bh(&raw_lock
);
188 sk_add_node(sk
, &raw_head
);
189 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, 1);
190 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 sk
->sk_bound_dev_if
= dev
->ifindex
;
241 static int raw_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
247 static int raw_disconnect(struct sock
*sk
, int flags
)
252 static int raw_sendmsg(struct sock
*sk
, struct msghdr
*msg
, size_t size
)
254 struct net_device
*dev
;
260 if (msg
->msg_flags
& MSG_OOB
) {
261 pr_debug("msg->msg_flags = 0x%x\n", msg
->msg_flags
);
266 if (!sk
->sk_bound_dev_if
)
267 dev
= dev_getfirstbyhwtype(sock_net(sk
), ARPHRD_IEEE802154
);
269 dev
= dev_get_by_index(sock_net(sk
), sk
->sk_bound_dev_if
);
273 pr_debug("no dev\n");
278 mtu
= IEEE802154_MTU
;
279 pr_debug("name = %s, mtu = %u\n", dev
->name
, mtu
);
282 pr_debug("size = %zu, mtu = %u\n", size
, mtu
);
287 hlen
= LL_RESERVED_SPACE(dev
);
288 tlen
= dev
->needed_tailroom
;
289 skb
= sock_alloc_send_skb(sk
, hlen
+ tlen
+ size
,
290 msg
->msg_flags
& MSG_DONTWAIT
, &err
);
294 skb_reserve(skb
, hlen
);
296 skb_reset_mac_header(skb
);
297 skb_reset_network_header(skb
);
299 err
= memcpy_from_msg(skb_put(skb
, size
), msg
, size
);
304 skb
->protocol
= htons(ETH_P_IEEE802154
);
306 err
= dev_queue_xmit(skb
);
308 err
= net_xmit_errno(err
);
322 static int raw_recvmsg(struct sock
*sk
, struct msghdr
*msg
, size_t len
,
323 int noblock
, int flags
, int *addr_len
)
326 int err
= -EOPNOTSUPP
;
329 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
335 msg
->msg_flags
|= MSG_TRUNC
;
339 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
343 sock_recv_ts_and_drops(msg
, sk
, skb
);
345 if (flags
& MSG_TRUNC
)
348 skb_free_datagram(sk
, skb
);
355 static int raw_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
357 skb
= skb_share_check(skb
, GFP_ATOMIC
);
361 if (sock_queue_rcv_skb(sk
, skb
) < 0) {
366 return NET_RX_SUCCESS
;
369 static void ieee802154_raw_deliver(struct net_device
*dev
, struct sk_buff
*skb
)
373 read_lock(&raw_lock
);
374 sk_for_each(sk
, &raw_head
) {
376 if (!sk
->sk_bound_dev_if
||
377 sk
->sk_bound_dev_if
== dev
->ifindex
) {
378 struct sk_buff
*clone
;
380 clone
= skb_clone(skb
, GFP_ATOMIC
);
382 raw_rcv_skb(sk
, clone
);
386 read_unlock(&raw_lock
);
389 static int raw_getsockopt(struct sock
*sk
, int level
, int optname
,
390 char __user
*optval
, int __user
*optlen
)
395 static int raw_setsockopt(struct sock
*sk
, int level
, int optname
,
396 char __user
*optval
, unsigned int optlen
)
401 static struct proto ieee802154_raw_prot
= {
402 .name
= "IEEE-802.15.4-RAW",
403 .owner
= THIS_MODULE
,
404 .obj_size
= sizeof(struct sock
),
407 .sendmsg
= raw_sendmsg
,
408 .recvmsg
= raw_recvmsg
,
410 .unhash
= raw_unhash
,
411 .connect
= raw_connect
,
412 .disconnect
= raw_disconnect
,
413 .getsockopt
= raw_getsockopt
,
414 .setsockopt
= raw_setsockopt
,
417 static const struct proto_ops ieee802154_raw_ops
= {
418 .family
= PF_IEEE802154
,
419 .owner
= THIS_MODULE
,
420 .release
= ieee802154_sock_release
,
421 .bind
= ieee802154_sock_bind
,
422 .connect
= ieee802154_sock_connect
,
423 .socketpair
= sock_no_socketpair
,
424 .accept
= sock_no_accept
,
425 .getname
= sock_no_getname
,
426 .poll
= datagram_poll
,
427 .ioctl
= ieee802154_sock_ioctl
,
428 .listen
= sock_no_listen
,
429 .shutdown
= sock_no_shutdown
,
430 .setsockopt
= sock_common_setsockopt
,
431 .getsockopt
= sock_common_getsockopt
,
432 .sendmsg
= ieee802154_sock_sendmsg
,
433 .recvmsg
= sock_common_recvmsg
,
434 .mmap
= sock_no_mmap
,
435 .sendpage
= sock_no_sendpage
,
437 .compat_setsockopt
= compat_sock_common_setsockopt
,
438 .compat_getsockopt
= compat_sock_common_getsockopt
,
442 /* DGRAM Sockets (802.15.4 dataframes) */
443 static HLIST_HEAD(dgram_head
);
444 static DEFINE_RWLOCK(dgram_lock
);
449 struct ieee802154_addr src_addr
;
450 struct ieee802154_addr dst_addr
;
452 unsigned int bound
:1;
453 unsigned int connected
:1;
454 unsigned int want_ack
:1;
455 unsigned int secen
:1;
456 unsigned int secen_override
:1;
457 unsigned int seclevel
:3;
458 unsigned int seclevel_override
:1;
461 static inline struct dgram_sock
*dgram_sk(const struct sock
*sk
)
463 return container_of(sk
, struct dgram_sock
, sk
);
466 static int dgram_hash(struct sock
*sk
)
468 write_lock_bh(&dgram_lock
);
469 sk_add_node(sk
, &dgram_head
);
470 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, 1);
471 write_unlock_bh(&dgram_lock
);
476 static void dgram_unhash(struct sock
*sk
)
478 write_lock_bh(&dgram_lock
);
479 if (sk_del_node_init(sk
))
480 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, -1);
481 write_unlock_bh(&dgram_lock
);
484 static int dgram_init(struct sock
*sk
)
486 struct dgram_sock
*ro
= dgram_sk(sk
);
492 static void dgram_close(struct sock
*sk
, long timeout
)
494 sk_common_release(sk
);
497 static int dgram_bind(struct sock
*sk
, struct sockaddr
*uaddr
, int len
)
499 struct sockaddr_ieee802154
*addr
= (struct sockaddr_ieee802154
*)uaddr
;
500 struct ieee802154_addr haddr
;
501 struct dgram_sock
*ro
= dgram_sk(sk
);
503 struct net_device
*dev
;
509 if (len
< sizeof(*addr
))
512 if (addr
->family
!= AF_IEEE802154
)
515 ieee802154_addr_from_sa(&haddr
, &addr
->addr
);
516 dev
= ieee802154_get_dev(sock_net(sk
), &haddr
);
522 if (dev
->type
!= ARPHRD_IEEE802154
) {
527 ro
->src_addr
= haddr
;
539 static int dgram_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
544 int amount
= sk_wmem_alloc_get(sk
);
546 return put_user(amount
, (int __user
*)arg
);
552 unsigned long amount
;
555 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
556 skb
= skb_peek(&sk
->sk_receive_queue
);
558 /* We will only return the amount
559 * of this packet since that is all
562 amount
= skb
->len
- ieee802154_hdr_length(skb
);
564 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
565 return put_user(amount
, (int __user
*)arg
);
572 /* FIXME: autobind */
573 static int dgram_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
576 struct sockaddr_ieee802154
*addr
= (struct sockaddr_ieee802154
*)uaddr
;
577 struct dgram_sock
*ro
= dgram_sk(sk
);
580 if (len
< sizeof(*addr
))
583 if (addr
->family
!= AF_IEEE802154
)
593 ieee802154_addr_from_sa(&ro
->dst_addr
, &addr
->addr
);
601 static int dgram_disconnect(struct sock
*sk
, int flags
)
603 struct dgram_sock
*ro
= dgram_sk(sk
);
612 static int dgram_sendmsg(struct sock
*sk
, struct msghdr
*msg
, size_t size
)
614 struct net_device
*dev
;
617 struct ieee802154_mac_cb
*cb
;
618 struct dgram_sock
*ro
= dgram_sk(sk
);
619 struct ieee802154_addr dst_addr
;
623 if (msg
->msg_flags
& MSG_OOB
) {
624 pr_debug("msg->msg_flags = 0x%x\n", msg
->msg_flags
);
628 if (!ro
->connected
&& !msg
->msg_name
)
629 return -EDESTADDRREQ
;
630 else if (ro
->connected
&& msg
->msg_name
)
634 dev
= dev_getfirstbyhwtype(sock_net(sk
), ARPHRD_IEEE802154
);
636 dev
= ieee802154_get_dev(sock_net(sk
), &ro
->src_addr
);
639 pr_debug("no dev\n");
643 mtu
= IEEE802154_MTU
;
644 pr_debug("name = %s, mtu = %u\n", dev
->name
, mtu
);
647 pr_debug("size = %zu, mtu = %u\n", size
, mtu
);
652 hlen
= LL_RESERVED_SPACE(dev
);
653 tlen
= dev
->needed_tailroom
;
654 skb
= sock_alloc_send_skb(sk
, hlen
+ tlen
+ size
,
655 msg
->msg_flags
& MSG_DONTWAIT
,
660 skb_reserve(skb
, hlen
);
662 skb_reset_network_header(skb
);
664 cb
= mac_cb_init(skb
);
665 cb
->type
= IEEE802154_FC_TYPE_DATA
;
666 cb
->ackreq
= ro
->want_ack
;
669 DECLARE_SOCKADDR(struct sockaddr_ieee802154
*,
670 daddr
, msg
->msg_name
);
672 ieee802154_addr_from_sa(&dst_addr
, &daddr
->addr
);
674 dst_addr
= ro
->dst_addr
;
677 cb
->secen
= ro
->secen
;
678 cb
->secen_override
= ro
->secen_override
;
679 cb
->seclevel
= ro
->seclevel
;
680 cb
->seclevel_override
= ro
->seclevel_override
;
682 err
= wpan_dev_hard_header(skb
, dev
, &dst_addr
,
683 ro
->bound
? &ro
->src_addr
: NULL
, size
);
687 err
= memcpy_from_msg(skb_put(skb
, size
), msg
, size
);
692 skb
->protocol
= htons(ETH_P_IEEE802154
);
694 err
= dev_queue_xmit(skb
);
696 err
= net_xmit_errno(err
);
710 static int dgram_recvmsg(struct sock
*sk
, struct msghdr
*msg
, size_t len
,
711 int noblock
, int flags
, int *addr_len
)
714 int err
= -EOPNOTSUPP
;
716 DECLARE_SOCKADDR(struct sockaddr_ieee802154
*, saddr
, msg
->msg_name
);
718 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
724 msg
->msg_flags
|= MSG_TRUNC
;
728 /* FIXME: skip headers if necessary ?! */
729 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
733 sock_recv_ts_and_drops(msg
, sk
, skb
);
736 /* Clear the implicit padding in struct sockaddr_ieee802154
737 * (16 bits between 'family' and 'addr') and in struct
738 * ieee802154_addr_sa (16 bits at the end of the structure).
740 memset(saddr
, 0, sizeof(*saddr
));
742 saddr
->family
= AF_IEEE802154
;
743 ieee802154_addr_to_sa(&saddr
->addr
, &mac_cb(skb
)->source
);
744 *addr_len
= sizeof(*saddr
);
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));
851 if (!ro
->secen_override
)
852 val
= WPAN_SECURITY_DEFAULT
;
854 val
= WPAN_SECURITY_ON
;
856 val
= WPAN_SECURITY_OFF
;
858 case WPAN_SECURITY_LEVEL
:
859 if (!ro
->seclevel_override
)
860 val
= WPAN_SECURITY_LEVEL_DEFAULT
;
868 if (put_user(len
, optlen
))
870 if (copy_to_user(optval
, &val
, len
))
875 static int dgram_setsockopt(struct sock
*sk
, int level
, int optname
,
876 char __user
*optval
, unsigned int optlen
)
878 struct dgram_sock
*ro
= dgram_sk(sk
);
879 struct net
*net
= sock_net(sk
);
883 if (optlen
< sizeof(int))
886 if (get_user(val
, (int __user
*)optval
))
893 ro
->want_ack
= !!val
;
896 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
) &&
897 !ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
903 case WPAN_SECURITY_DEFAULT
:
904 ro
->secen_override
= 0;
906 case WPAN_SECURITY_ON
:
907 ro
->secen_override
= 1;
910 case WPAN_SECURITY_OFF
:
911 ro
->secen_override
= 1;
919 case WPAN_SECURITY_LEVEL
:
920 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
) &&
921 !ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
926 if (val
< WPAN_SECURITY_LEVEL_DEFAULT
||
927 val
> IEEE802154_SCF_SECLEVEL_ENC_MIC128
) {
929 } else if (val
== WPAN_SECURITY_LEVEL_DEFAULT
) {
930 ro
->seclevel_override
= 0;
932 ro
->seclevel_override
= 1;
945 static struct proto ieee802154_dgram_prot
= {
946 .name
= "IEEE-802.15.4-MAC",
947 .owner
= THIS_MODULE
,
948 .obj_size
= sizeof(struct dgram_sock
),
950 .close
= dgram_close
,
952 .sendmsg
= dgram_sendmsg
,
953 .recvmsg
= dgram_recvmsg
,
955 .unhash
= dgram_unhash
,
956 .connect
= dgram_connect
,
957 .disconnect
= dgram_disconnect
,
958 .ioctl
= dgram_ioctl
,
959 .getsockopt
= dgram_getsockopt
,
960 .setsockopt
= dgram_setsockopt
,
963 static const struct proto_ops ieee802154_dgram_ops
= {
964 .family
= PF_IEEE802154
,
965 .owner
= THIS_MODULE
,
966 .release
= ieee802154_sock_release
,
967 .bind
= ieee802154_sock_bind
,
968 .connect
= ieee802154_sock_connect
,
969 .socketpair
= sock_no_socketpair
,
970 .accept
= sock_no_accept
,
971 .getname
= sock_no_getname
,
972 .poll
= datagram_poll
,
973 .ioctl
= ieee802154_sock_ioctl
,
974 .listen
= sock_no_listen
,
975 .shutdown
= sock_no_shutdown
,
976 .setsockopt
= sock_common_setsockopt
,
977 .getsockopt
= sock_common_getsockopt
,
978 .sendmsg
= ieee802154_sock_sendmsg
,
979 .recvmsg
= sock_common_recvmsg
,
980 .mmap
= sock_no_mmap
,
981 .sendpage
= sock_no_sendpage
,
983 .compat_setsockopt
= compat_sock_common_setsockopt
,
984 .compat_getsockopt
= compat_sock_common_getsockopt
,
988 /* Create a socket. Initialise the socket, blank the addresses
991 static int ieee802154_create(struct net
*net
, struct socket
*sock
,
992 int protocol
, int kern
)
997 const struct proto_ops
*ops
;
999 if (!net_eq(net
, &init_net
))
1000 return -EAFNOSUPPORT
;
1002 switch (sock
->type
) {
1004 proto
= &ieee802154_raw_prot
;
1005 ops
= &ieee802154_raw_ops
;
1008 proto
= &ieee802154_dgram_prot
;
1009 ops
= &ieee802154_dgram_ops
;
1012 rc
= -ESOCKTNOSUPPORT
;
1017 sk
= sk_alloc(net
, PF_IEEE802154
, GFP_KERNEL
, proto
, kern
);
1024 sock_init_data(sock
, sk
);
1025 /* FIXME: sk->sk_destruct */
1026 sk
->sk_family
= PF_IEEE802154
;
1028 /* Checksums on by default */
1029 sock_set_flag(sk
, SOCK_ZAPPED
);
1031 if (sk
->sk_prot
->hash
) {
1032 rc
= sk
->sk_prot
->hash(sk
);
1034 sk_common_release(sk
);
1039 if (sk
->sk_prot
->init
) {
1040 rc
= sk
->sk_prot
->init(sk
);
1042 sk_common_release(sk
);
1048 static const struct net_proto_family ieee802154_family_ops
= {
1049 .family
= PF_IEEE802154
,
1050 .create
= ieee802154_create
,
1051 .owner
= THIS_MODULE
,
1054 static int ieee802154_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
1055 struct packet_type
*pt
, struct net_device
*orig_dev
)
1057 if (!netif_running(dev
))
1059 pr_debug("got frame, type %d, dev %p\n", dev
->type
, dev
);
1061 print_hex_dump_bytes("ieee802154_rcv ",
1062 DUMP_PREFIX_NONE
, skb
->data
, skb
->len
);
1065 if (!net_eq(dev_net(dev
), &init_net
))
1068 ieee802154_raw_deliver(dev
, skb
);
1070 if (dev
->type
!= ARPHRD_IEEE802154
)
1073 if (skb
->pkt_type
!= PACKET_OTHERHOST
)
1074 return ieee802154_dgram_deliver(dev
, skb
);
1081 static struct packet_type ieee802154_packet_type
= {
1082 .type
= htons(ETH_P_IEEE802154
),
1083 .func
= ieee802154_rcv
,
1086 static int __init
af_ieee802154_init(void)
1090 rc
= proto_register(&ieee802154_raw_prot
, 1);
1094 rc
= proto_register(&ieee802154_dgram_prot
, 1);
1098 /* Tell SOCKET that we are alive */
1099 rc
= sock_register(&ieee802154_family_ops
);
1102 dev_add_pack(&ieee802154_packet_type
);
1108 proto_unregister(&ieee802154_dgram_prot
);
1110 proto_unregister(&ieee802154_raw_prot
);
1115 static void __exit
af_ieee802154_remove(void)
1117 dev_remove_pack(&ieee802154_packet_type
);
1118 sock_unregister(PF_IEEE802154
);
1119 proto_unregister(&ieee802154_dgram_prot
);
1120 proto_unregister(&ieee802154_raw_prot
);
1123 module_init(af_ieee802154_init
);
1124 module_exit(af_ieee802154_remove
);
1126 MODULE_LICENSE("GPL");
1127 MODULE_ALIAS_NETPROTO(PF_IEEE802154
);