This code does not currently compile and it is disabled via the Kconfig BROKEN 
flag.
---
 include/linux/if_ether.h       |    1 
 include/linux/socket.h         |    4 
 include/net/zigbee/af_zigbee.h |   17 ++
 include/net/zigbee/nwk.h       |   94 +++++++++
 net/Kconfig                    |    1 
 net/Makefile                   |    1 
 net/core/sock.c                |    3 
 net/zigbee/Kconfig             |    7 +
 net/zigbee/Makefile            |    5 
 net/zigbee/af_zigbee.c         |  285 ++++++++++++++++++++++++++++
 net/zigbee/dgram.c             |  401 ++++++++++++++++++++++++++++++++++++++++
 11 files changed, 818 insertions(+), 1 deletions(-)
 create mode 100644 include/net/zigbee/af_zigbee.h
 create mode 100644 include/net/zigbee/nwk.h
 create mode 100644 net/zigbee/Kconfig
 create mode 100644 net/zigbee/Makefile
 create mode 100644 net/zigbee/af_zigbee.c
 create mode 100644 net/zigbee/dgram.c

diff --git a/include/linux/if_ether.h b/include/linux/if_ether.h
index be69043..8eea36c 100644
--- a/include/linux/if_ether.h
+++ b/include/linux/if_ether.h
@@ -111,6 +111,7 @@
 #define ETH_P_PHONET   0x00F5          /* Nokia Phonet frames          */
 #define ETH_P_IEEE802154 0x00F6                /* IEEE802.15.4 frame           
*/
 #define ETH_P_CAIF     0x00F7          /* ST-Ericsson CAIF protocol    */
+#define ETH_P_ZIGBEE   0x00F8          /* ZigBee frame                 */
 
 /*
  *     This is an Ethernet frame header.
diff --git a/include/linux/socket.h b/include/linux/socket.h
index edbb1d0..b33c117 100644
--- a/include/linux/socket.h
+++ b/include/linux/socket.h
@@ -192,7 +192,8 @@ struct ucred {
 #define AF_IEEE802154  36      /* IEEE802154 sockets           */
 #define AF_CAIF                37      /* CAIF sockets                 */
 #define AF_ALG         38      /* Algorithm sockets            */
-#define AF_MAX         39      /* For now.. */
+#define AF_ZIGBEE      39      /* ZIGBEE sockets               */
+#define AF_MAX         40      /* For now.. */
 
 /* Protocol families, same as address families. */
 #define PF_UNSPEC      AF_UNSPEC
@@ -234,6 +235,7 @@ struct ucred {
 #define PF_IEEE802154  AF_IEEE802154
 #define PF_CAIF                AF_CAIF
 #define PF_ALG         AF_ALG
+#define PF_ZIGBEE      AF_ZIGBEE
 #define PF_MAX         AF_MAX
 
 /* Maximum queue length specifiable by listen.  */
diff --git a/include/net/zigbee/af_zigbee.h b/include/net/zigbee/af_zigbee.h
new file mode 100644
index 0000000..a405ddd
--- /dev/null
+++ b/include/net/zigbee/af_zigbee.h
@@ -0,0 +1,17 @@
+#ifndef  _AF_ZIGBEE_H
+#define  _AF_ZIGBEE_H
+#include <linux/if.h>
+
+struct sockaddr_zb {
+       sa_family_t family; /* AF_ZIGBEE */
+       u16 addr;
+};
+
+#ifdef __KERNEL__
+extern struct proto zb_raw_prot;
+extern struct proto zb_dgram_prot;
+void zb_raw_deliver(struct net_device *dev, struct sk_buff *skb);
+int zb_dgram_deliver(struct net_device *dev, struct sk_buff *skb);
+#endif
+
+#endif /* _AF_ZIGBEE_H */
diff --git a/include/net/zigbee/nwk.h b/include/net/zigbee/nwk.h
new file mode 100644
index 0000000..0560c0e
--- /dev/null
+++ b/include/net/zigbee/nwk.h
@@ -0,0 +1,94 @@
+/*
+ * Definitions of ZigBEE NWK
+ *
+ * Copyright 2009 Siemens AG
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Written by:
+ * Maxim Yu. Osipov  <[email protected]>
+ */
+#ifndef _NET_ZIGBEE_NWK_H
+#define _NET_ZIGBEE_NWK_H
+#include <linux/types.h>
+#include <asm/byteorder.h>
+
+/*
+ * The General NWK frame format:
+ * NWK header (8):
+ *  Name Length (in octets) Value
+ *  ---- ------------------ -----
+ *  Frame Control       (2) See below
+ *  Destination Address (2) Same as IEEE 802.15.4 MAC short address
+ *  Source Address      (2) Same as IEEE 802.15.4 MAC short address
+ *  Radius              (1) Range of a radius transmission
+ *  Sequence Number     (1)
+ * NWK Payload:
+ *
+ * Data Frame:
+ *  Name     Length (in octets)
+ *  ----     ------------------
+ *  Payload  (variable)
+ *
+ * Command Frame:
+ *  Name       Length (in octets)
+ *  ----       ------------------
+ *  Command id (1)
+ *  Payload    (variable)
+ *
+ * Frame Control Fields:
+ * bit   Subfield         Value
+ * ---   --------         -----
+ * 0-1   Frame Type       <Data/Command>
+ * 2-5   Protocol Version 0x1
+ * 6-7   Discover Route   <Supress/Enable/Force>
+ * 9     Security         <Enabled/Disabled>
+ * 10-15 Reserved
+ *
+ *
+*/
+struct nwkhdr {
+#if defined(__LITTLE_ENDIAN_BITFIELD)
+       __u16   reserved2:6,
+               security:1,
+               reserved1:1,
+               disc_route:2,
+               version:4,
+               type:2;
+#elif defined (__BIG_ENDIAN_BITFIELD)
+       __u16   type:2,
+               version:4,
+               disc_route:2,
+               reserved1:1,
+               security:1,
+               reserved2:6;
+#else
+#error "Please fix <asm/byteorder.h>"
+#endif
+       __be16  daddr;
+       __be16  saddr;
+       __u8    radius;
+       __u8    seqnum;
+};
+
+#ifdef __KERNEL__
+#include <linux/skbuff.h>
+
+static inline struct nwkhdr *nwk_hdr(const struct sk_buff *skb)
+{
+       return (struct nwkhdr *)skb_network_header(skb);
+}
+#endif
+
+#endif /* _NET_ZIGBEE_NWK_H */
diff --git a/net/Kconfig b/net/Kconfig
index a4709ed..b8e1879 100644
--- a/net/Kconfig
+++ b/net/Kconfig
@@ -212,6 +212,7 @@ source "net/wanrouter/Kconfig"
 source "net/phonet/Kconfig"
 source "net/ieee802154/Kconfig"
 source "net/mac802154/Kconfig"
+source "net/zigbee/Kconfig"
 source "net/sched/Kconfig"
 source "net/dcb/Kconfig"
 source "net/dns_resolver/Kconfig"
diff --git a/net/Makefile b/net/Makefile
index b7a6585..25364db 100644
--- a/net/Makefile
+++ b/net/Makefile
@@ -50,6 +50,7 @@ obj-$(CONFIG_IP_SCTP)         += sctp/
 obj-$(CONFIG_RDS)              += rds/
 obj-$(CONFIG_WIRELESS)         += wireless/
 obj-$(CONFIG_MAC80211)         += mac80211/
+obj-$(CONFIG_ZIGBEE)           += zigbee/
 obj-$(CONFIG_TIPC)             += tipc/
 obj-$(CONFIG_NETLABEL)         += netlabel/
 obj-$(CONFIG_IUCV)             += iucv/
diff --git a/net/core/sock.c b/net/core/sock.c
index 7dfed79..d6ab7d2 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -158,6 +158,7 @@ static const char *const af_family_key_strings[AF_MAX+1] = {
   "sk_lock-AF_TIPC"  , "sk_lock-AF_BLUETOOTH", "sk_lock-IUCV"        ,
   "sk_lock-AF_RXRPC" , "sk_lock-AF_ISDN"     , "sk_lock-AF_PHONET"   ,
   "sk_lock-AF_IEEE802154", "sk_lock-AF_CAIF" , "sk_lock-AF_ALG"      ,
+  "sk_lock-AF_ZIGBEE",
   "sk_lock-AF_MAX"
 };
 static const char *const af_family_slock_key_strings[AF_MAX+1] = {
@@ -174,6 +175,7 @@ static const char *const 
af_family_slock_key_strings[AF_MAX+1] = {
   "slock-AF_TIPC"  , "slock-AF_BLUETOOTH", "slock-AF_IUCV"     ,
   "slock-AF_RXRPC" , "slock-AF_ISDN"     , "slock-AF_PHONET"   ,
   "slock-AF_IEEE802154", "slock-AF_CAIF" , "slock-AF_ALG"      ,
+  "slock-AF_ZIGBEE",
   "slock-AF_MAX"
 };
 static const char *const af_family_clock_key_strings[AF_MAX+1] = {
@@ -190,6 +192,7 @@ static const char *const 
af_family_clock_key_strings[AF_MAX+1] = {
   "clock-AF_TIPC"  , "clock-AF_BLUETOOTH", "clock-AF_IUCV"     ,
   "clock-AF_RXRPC" , "clock-AF_ISDN"     , "clock-AF_PHONET"   ,
   "clock-AF_IEEE802154", "clock-AF_CAIF" , "clock-AF_ALG"      ,
+  "clock-AF_ZIGBEE",
   "clock-AF_MAX"
 };
 
diff --git a/net/zigbee/Kconfig b/net/zigbee/Kconfig
new file mode 100644
index 0000000..7a0334d
--- /dev/null
+++ b/net/zigbee/Kconfig
@@ -0,0 +1,7 @@
+config ZIGBEE 
+       tristate "ZigBee Low-Rate Wireless Personal Area Networks support 
(EXPERIMENTAL)"
+       depends on EXPERIMENTAL & BROKEN
+       ---help---
+         
+       Say Y here to compile ZigBee support into the kernel or say M to
+         compile it as modules.
diff --git a/net/zigbee/Makefile b/net/zigbee/Makefile
new file mode 100644
index 0000000..8c2eee5
--- /dev/null
+++ b/net/zigbee/Makefile
@@ -0,0 +1,5 @@
+obj-$(CONFIG_ZIGBEE) +=        af_zb.o
+
+af_zb-objs             := af_zigbee.o dgram.o
+
+EXTRA_CFLAGS += -Wall -DEXPORT_SYMTAB -DCONFIG_FFD -DCONFIG_ZIGBEE_DEBUG 
-DIEEE80215_DEBUG -DDEBUG
diff --git a/net/zigbee/af_zigbee.c b/net/zigbee/af_zigbee.c
new file mode 100644
index 0000000..b3eb775
--- /dev/null
+++ b/net/zigbee/af_zigbee.c
@@ -0,0 +1,285 @@
+/*
+ * ZigBee socket interface
+ *
+ * Copyright 2008, 2009 Siemens AG
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Written by:
+ * Sergey Lapin <[email protected]>
+ * Maxim Yu. Osipov <[email protected]>
+ */
+#include <linux/net.h>
+#include <linux/capability.h>
+#include <linux/module.h>
+#include <linux/if_arp.h>
+#include <linux/if.h>
+#include <linux/termios.h>     /* For TIOCOUTQ/INQ */
+#include <linux/list.h>
+#include <net/datalink.h>
+#include <net/psnap.h>
+#include <net/sock.h>
+#include <net/tcp_states.h>
+#include <net/route.h>
+
+//#include <net/ieee80215/af_ieee80215.h>
+#include <net/zigbee/af_zigbee.h>
+#include <net/zigbee/nwk.h>
+
+#define DBG_DUMP(data, len) { \
+       int i; \
+       pr_debug("file %s: function: %s: data: len %d:\n", __FILE__, __func__, 
len); \
+       for (i = 0; i < len; i++) {\
+               pr_debug("%02x: %02x\n", i, (data)[i]); \
+       } \
+}
+
+static int zb_sock_release(struct socket *sock)
+{
+       struct sock *sk = sock->sk;
+
+       if (sk) {
+               sock->sk = NULL;
+               sk->sk_prot->close(sk, 0);
+       }
+       return 0;
+}
+static int zb_sock_sendmsg(struct kiocb *iocb, struct socket *sock, struct 
msghdr *msg, size_t len)
+{
+       struct sock *sk = sock->sk;
+
+       return sk->sk_prot->sendmsg(iocb, sk, msg, len);
+}
+
+static int zb_sock_bind(struct socket *sock, struct sockaddr *uaddr, int 
addr_len)
+{
+       struct sock *sk = sock->sk;
+
+       if (sk->sk_prot->bind)
+               return sk->sk_prot->bind(sk, uaddr, addr_len);
+
+       return sock_no_bind(sock, uaddr, addr_len);
+}
+
+static int zb_sock_connect(struct socket *sock, struct sockaddr *uaddr,
+                       int addr_len, int flags)
+{
+       struct sock *sk = sock->sk;
+
+       if (uaddr->sa_family == AF_UNSPEC)
+               return sk->sk_prot->disconnect(sk, flags);
+
+       return sk->sk_prot->connect(sk, uaddr, addr_len);
+}
+
+#if 0
+static int zb_dev_ioctl(struct sock *sk, struct ifreq __user *arg, unsigned 
int cmd)
+{
+       struct ifreq ifr;
+       int ret = -EINVAL;
+       struct net_device *dev;
+
+       if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
+               return -EFAULT;
+
+       ifr.ifr_name[IFNAMSIZ-1] = 0;
+
+       dev_load(sock_net(sk), ifr.ifr_name);
+       dev = dev_get_by_name(sock_net(sk), ifr.ifr_name);
+       if (dev->type == ARPHRD_ZIGBEE || dev->type == ARPHRD_ZIGBEE_PHY)
+               ret = dev->do_ioctl(dev, &ifr, cmd);
+
+       if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq)))
+               ret = -EFAULT;
+       dev_put(dev);
+
+       return ret;
+}
+#endif
+
+static int zb_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long 
arg)
+{
+       struct sock *sk = sock->sk;
+
+       switch (cmd) {
+       case SIOCGSTAMP:
+               return sock_get_timestamp(sk, (struct timeval __user *)arg);
+       case SIOCGSTAMPNS:
+               return sock_get_timestampns(sk, (struct timespec __user *)arg);
+#if 0
+       case SIOCGIFADDR:
+       case SIOCSIFADDR:
+               return zb_dev_ioctl(sk, (struct ifreq __user *)arg, cmd);
+#endif
+       default:
+               if (!sk->sk_prot->ioctl)
+                       return -ENOIOCTLCMD;
+               return sk->sk_prot->ioctl(sk, cmd, arg);
+       }
+}
+
+static const struct proto_ops zb_dgram_ops = {
+       .family            = PF_ZIGBEE,
+       .owner             = THIS_MODULE,
+       .release           = zb_sock_release,
+       .bind              = zb_sock_bind,
+       .connect           = zb_sock_connect,
+       .socketpair        = sock_no_socketpair,
+       .accept            = sock_no_accept,
+       .getname           = sock_no_getname,
+       .poll              = datagram_poll,
+       .ioctl             = zb_sock_ioctl,
+       .listen            = sock_no_listen,
+       .shutdown          = sock_no_shutdown,
+       .setsockopt        = sock_common_setsockopt,
+       .getsockopt        = sock_common_getsockopt,
+       .sendmsg           = zb_sock_sendmsg,
+       .recvmsg           = sock_common_recvmsg,
+       .mmap              = sock_no_mmap,
+       .sendpage          = sock_no_sendpage,
+#ifdef CONFIG_COMPAT
+       .compat_setsockopt = compat_sock_common_setsockopt,
+       .compat_getsockopt = compat_sock_common_getsockopt,
+#endif
+};
+
+
+/*
+ * Create a socket. Initialise the socket, blank the addresses
+ * set the state.
+ */
+static int zb_create(struct net *net, struct socket *sock, int protocol)
+{
+       struct sock *sk;
+       int rc;
+       struct proto *proto;
+       const struct proto_ops *ops;
+
+       // FIXME: init_net
+       if (net != &init_net)
+               return -EAFNOSUPPORT;
+
+       if (sock->type == SOCK_DGRAM) {
+               proto = &zb_dgram_prot;
+               ops = &zb_dgram_ops;
+       }
+       else {
+               rc = -ESOCKTNOSUPPORT;
+               goto out;
+       }
+
+       rc = -ENOMEM;
+       sk = sk_alloc(net, PF_ZIGBEE, GFP_KERNEL, proto);
+       if (!sk)
+               goto out;
+       rc = 0;
+
+       sock->ops = ops;
+
+       sock_init_data(sock, sk);
+       // FIXME: sk->sk_destruct
+       sk->sk_family = PF_ZIGBEE;
+
+#if 0
+       /* Checksums on by default */
+       // FIXME:
+       sock_set_flag(sk, SOCK_ZAPPED);
+
+       // FIXME:
+       if (sk->sk_prot->hash)
+               sk->sk_prot->hash(sk);
+#endif
+
+       if (sk->sk_prot->init) {
+               rc = sk->sk_prot->init(sk);
+               if (rc)
+                       sk_common_release(sk);
+       }
+out:
+       return rc;
+}
+
+static struct net_proto_family zb_family_ops = {
+       .family         = PF_ZIGBEE,
+       .create         = zb_create,
+       .owner          = THIS_MODULE,
+};
+
+/* 
+ * Main ZigBEE NWK receive routine.
+ */
+static int zb_rcv(struct sk_buff *skb, struct net_device *dev, struct 
packet_type *pt, struct net_device *orig_dev)
+{
+       struct nwkhdr *nwkh;
+       u32 len;
+       
+       DBG_DUMP(skb->data, skb->len);
+       pr_debug("got frame, type %d, dev %p\n", dev->type, dev);
+       // FIXME: init_net
+       if (!net_eq(dev_net(dev), &init_net))
+               goto drop;
+
+       zb_raw_deliver(dev, skb);
+
+       if (skb->pkt_type != PACKET_OTHERHOST)
+               return zb_dgram_deliver(dev, skb);
+
+drop:
+       kfree_skb(skb);
+       return NET_RX_DROP;
+}
+
+
+static struct packet_type zb_packet_type = {
+       .type = __constant_htons(ETH_P_ZIGBEE),
+       .func = zb_rcv,
+};
+
+static int __init af_zb_init(void)
+{
+       int rc = -EINVAL;
+
+       rc = proto_register(&zb_dgram_prot, 1);
+       if (rc)
+               goto err;
+
+       /* Tell SOCKET that we are alive */
+       rc = sock_register(&zb_family_ops);
+
+       if (rc)
+               goto err;
+
+       dev_add_pack(&zb_packet_type);
+
+       rc = 0;
+       goto out;
+
+err:
+       proto_unregister(&zb_dgram_prot);
+out:
+       return rc;
+}
+
+static void af_zb_remove(void)
+{
+       dev_remove_pack(&zb_packet_type);
+       sock_unregister(PF_ZIGBEE);
+       proto_unregister(&zb_dgram_prot);
+}
+
+module_init(af_zb_init);
+module_exit(af_zb_remove);
+
+MODULE_LICENSE("GPL");
+MODULE_ALIAS_NETPROTO(PF_ZIGBEE);
diff --git a/net/zigbee/dgram.c b/net/zigbee/dgram.c
new file mode 100644
index 0000000..1808fa1
--- /dev/null
+++ b/net/zigbee/dgram.c
@@ -0,0 +1,401 @@
+/*
+ * ZigBee socket interface
+ *
+ * Copyright 2008, 2009 Siemens AG
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Written by:
+ * Sergey Lapin <[email protected]>
+ * Dmitry Eremin-Solenikov <[email protected]>
+ * Maxim Yu. Osipov <[email protected]>
+ */
+
+#include <linux/net.h>
+#include <linux/module.h>
+#include <linux/if_arp.h>
+#include <linux/list.h>
+#include <net/sock.h>
+//#include <net/ieee80215/netdev.h>
+//#include <net/ieee80215/af_ieee80215.h>
+//#include <net/ieee80215/mac_def.h>
+#include <net/zigbee/nwk.h>
+#include <asm/ioctls.h>
+
+static HLIST_HEAD(dgram_head);
+static DEFINE_RWLOCK(dgram_lock);
+
+struct dgram_sock {
+       struct sock sk;
+
+       int bound;
+       struct ieee80215_addr src_addr;
+       struct ieee80215_addr dst_addr;
+};
+
+static void dgram_hash(struct sock *sk)
+{
+       write_lock_bh(&dgram_lock);
+       sk_add_node(sk, &dgram_head);
+       sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
+       write_unlock_bh(&dgram_lock);
+}
+
+static void dgram_unhash(struct sock *sk)
+{
+       write_lock_bh(&dgram_lock);
+       if (sk_del_node_init(sk))
+               sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
+       write_unlock_bh(&dgram_lock);
+}
+
+static int dgram_init(struct sock *sk)
+{
+       struct dgram_sock *ro = container_of(sk, struct dgram_sock, sk);
+
+       ro->dst_addr.addr_type = IEEE80215_ADDR_SHORT;
+//     ro->dst_addr.pan_id = 0xffff;
+       ro->dst_addr.short_addr = 0xffff;
+       return 0;
+}
+
+static void dgram_close(struct sock *sk, long timeout)
+{
+       sk_common_release(sk);
+}
+
+static int dgram_bind(struct sock *sk, struct sockaddr *uaddr, int len)
+{
+       struct sockaddr_ieee80215 *addr = (struct sockaddr_ieee80215 *)uaddr;
+       struct dgram_sock *ro = container_of(sk, struct dgram_sock, sk);
+       int err = 0;
+
+       if (ro->bound)
+               return -EINVAL;
+
+       if (len < sizeof(*addr))
+               return -EINVAL;
+
+       if ((addr->family != AF_ZIGBEE) ||
+               (addr->addr.addr_type != IEEE80215_ADDR_SHORT))
+               return -EINVAL;
+
+       lock_sock(sk);
+       /*
+       * FIXME: should check here that address is not already in use
+       * Problem that this address is not independent - this is the
+       * address of the lower layer
+       */
+#if 0
+       dev = ieee80215_get_dev(sock_net(sk), &addr->addr);
+       if (!dev) {
+               err = -ENODEV;
+               goto out;
+       }
+
+       if (dev->type != ARPHRD_IEEE80215) {
+               err = -ENODEV;
+               goto out_put;
+       }
+#endif
+       memcpy(&ro->src_addr, &addr->addr, sizeof(struct ieee80215_addr));
+
+       ro->bound = 1;
+#if 0
+out_put:
+       dev_put(dev);
+out:
+#endif
+       release_sock(sk);
+
+       return err;
+}
+
+static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg)
+{
+       switch (cmd) {
+       case SIOCOUTQ:
+       {
+               int amount = atomic_read(&sk->sk_wmem_alloc);
+               return put_user(amount, (int __user *)arg);
+       }
+
+       case SIOCINQ:
+       {
+               struct sk_buff *skb;
+               unsigned long amount = 0;
+
+               spin_lock_bh(&sk->sk_receive_queue.lock);
+               skb = skb_peek(&sk->sk_receive_queue);
+               if (skb != NULL) {
+                       /*
+                        * We will only return the amount
+                        * of this packet since that is all
+                        * that will be read.
+                        */
+                       amount = skb->len - sizeof(struct nwkhdr);
+               }
+               spin_unlock_bh(&sk->sk_receive_queue.lock);
+               return put_user(amount, (int __user *)arg);
+
+       }
+#if 0
+       /* May be implement here the commands */ 
+       case IEEE80215_SIOC_NETWORK_DISCOVERY:
+               return ioctl_network_discovery(sk, (struct ieee80215_user_data 
__user *) arg);
+               break;
+       case IEEE80215_SIOC_NETWORK_FORMATION:
+               return ioctl_network_formation(sk, (struct ieee80215_user_data 
__user *) arg);
+               break;
+       case IEEE80215_SIOC_PERMIT_JOINING:
+               return ioctl_permit_joining(sk, (struct ieee80215_user_data 
__user *) arg);
+               break;
+       case IEEE80215_SIOC_START_ROUTER:
+               return ioctl_start_router(sk, (struct ieee80215_user_data 
__user *) arg);
+               break;
+       case IEEE80215_SIOC_JOIN:
+               return ioctl_mac_join(sk, (struct ieee80215_user_data __user *) 
arg);
+               break;
+       case IEEE80215_SIOC_MAC_CMD:
+               return ioctl_mac_cmd(sk, (struct ieee80215_user_data __user *) 
arg);
+
+               break;
+#endif
+       default:
+       return -ENOIOCTLCMD;
+       }
+}
+
+// FIXME: autobind
+static int dgram_connect(struct sock *sk, struct sockaddr *uaddr,
+                       int len)
+{
+       struct sockaddr_ieee80215 *addr = (struct sockaddr_ieee80215 *)uaddr;
+       struct dgram_sock *ro = container_of(sk, struct dgram_sock, sk);
+
+       int err = 0;
+
+       if (len < sizeof(*addr))
+               return -EINVAL;
+
+       if ((addr->family != AF_ZIGBEE) ||
+               (addr->addr.addr_type != IEEE80215_ADDR_SHORT))
+               return -EINVAL;
+
+       lock_sock(sk);
+
+       if (!ro->bound) {
+               err = -ENETUNREACH;
+               goto out;
+       }
+
+       memcpy(&ro->dst_addr, &addr->addr, sizeof(struct ieee80215_addr));
+
+out:
+       release_sock(sk);
+       return err;
+}
+
+static int dgram_disconnect(struct sock *sk, int flags)
+{
+       struct dgram_sock *ro = container_of(sk, struct dgram_sock, sk);
+
+       lock_sock(sk);
+
+       ro->dst_addr.addr_type = IEEE80215_ADDR_SHORT;
+//     ro->dst_addr.pan_id = 0xffff;
+       ro->dst_addr.short_addr = 0xffff;
+
+       release_sock(sk);
+
+       return 0;
+}
+
+static int dgram_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr 
*msg,
+                      size_t size)
+{
+       struct net_device *dev;
+       unsigned mtu;
+       struct sk_buff *skb;
+       struct dgram_sock *ro = container_of(sk, struct dgram_sock, sk);
+
+       int err;
+       struct ieee80215_priv *hw;
+
+       if (msg->msg_flags & MSG_OOB) {
+               pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
+               return -EOPNOTSUPP;
+       }
+
+       if (!ro->bound)
+               dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE80215);
+       else
+               dev = ieee80215_get_dev(sock_net(sk), &ro->src_addr);
+
+       if (!dev) {
+               pr_debug("no dev\n");
+               return -ENXIO;
+       }
+       hw = ieee80215_slave_get_hw(dev);
+       mtu = dev->mtu;
+       pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
+
+       skb = sock_alloc_send_skb(sk, LL_ALLOCATED_SPACE(dev) + size, 
msg->msg_flags & MSG_DONTWAIT,
+                                 &err);
+       if (!skb) {
+               dev_put(dev);
+               return err;
+       }
+       skb_reserve(skb, LL_RESERVED_SPACE(dev));
+
+       skb_reset_network_header(skb);
+
+       MAC_CB(skb)->flags = IEEE80215_FC_TYPE_DATA | MAC_CB_FLAG_ACKREQ;
+       MAC_CB(skb)->seq = hw->dsn;
+       err = dev_hard_header(skb, dev, ETH_P_IEEE80215, &ro->dst_addr, 
ro->bound ? &ro->src_addr : NULL, size);
+       if (err < 0) {
+               kfree_skb(skb);
+               dev_put(dev);
+               return err;
+       }
+
+       skb_reset_mac_header(skb);
+
+       err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
+       if (err < 0) {
+               kfree_skb(skb);
+               dev_put(dev);
+               return err;
+       }
+
+       if (size > mtu) {
+               pr_debug("size = %u, mtu = %u\n", size, mtu);
+               return -EINVAL;
+       }
+
+       skb->dev = dev;
+       skb->sk  = sk;
+       skb->protocol = htons(ETH_P_IEEE80215);
+
+       err = dev_queue_xmit(skb);
+       hw->dsn++;
+
+       dev_put(dev);
+
+       if (err)
+               return err;
+
+       return size;
+}
+
+static int dgram_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr 
*msg,
+                      size_t len, int noblock, int flags, int *addr_len)
+{
+       size_t copied = 0;
+       int err = -EOPNOTSUPP;
+       struct sk_buff *skb;
+
+       skb = skb_recv_datagram(sk, flags, noblock, &err);
+       if (!skb)
+               goto out;
+
+       copied = skb->len;
+       if (len < copied) {
+               msg->msg_flags |= MSG_TRUNC;
+               copied = len;
+       }
+
+       // FIXME: skip headers if necessary ?!
+       err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
+       if (err)
+               goto done;
+
+       sock_recv_timestamp(msg, sk, skb);
+
+       if (flags & MSG_TRUNC)
+               copied = skb->len;
+done:
+       skb_free_datagram(sk, skb);
+out:
+       if (err)
+               return err;
+       return copied;
+}
+
+static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb)
+{
+       if (sock_queue_rcv_skb(sk, skb) < 0) {
+               atomic_inc(&sk->sk_drops);
+               kfree_skb(skb);
+               return NET_RX_DROP;
+       }
+
+       return NET_RX_SUCCESS;
+}
+
+int ieee80215_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
+{
+       struct sock *sk, *prev = NULL;
+       struct hlist_node*node;
+       int ret = NET_RX_SUCCESS;
+
+       /* Data frame processing */
+
+       read_lock(&dgram_lock);
+       sk_for_each(sk, node, &dgram_head) {
+               struct dgram_sock *ro = container_of(sk, struct dgram_sock, sk);
+               if (!ro->bound ||
+                 (ro->src_addr.addr_type == IEEE80215_ADDR_LONG &&
+                    !memcmp(ro->src_addr.hwaddr, dev->dev_addr, 
IEEE80215_ADDR_LEN)) ||
+                 (ro->src_addr.addr_type == IEEE80215_ADDR_SHORT &&
+                    ieee80215_dev_get_pan_id(dev) == ro->src_addr.pan_id &&
+                    ieee80215_dev_get_short_addr(dev) == 
ro->src_addr.short_addr)) {
+                       if (prev) {
+                               struct sk_buff *clone;
+                               clone = skb_clone(skb, GFP_ATOMIC);
+                               if (clone)
+                                       dgram_rcv_skb(prev, clone);
+                       }
+
+                       prev = sk;
+               }
+       }
+
+       if (prev)
+               dgram_rcv_skb(prev, skb);
+       else {
+               kfree_skb(skb);
+               ret = NET_RX_DROP;
+       }
+       read_unlock(&dgram_lock);
+
+       return ret;
+}
+
+struct proto ieee80215_dgram_prot = {
+       .name           = "ZigBEE",
+       .owner          = THIS_MODULE,
+       .obj_size       = sizeof(struct dgram_sock),
+       .init           = dgram_init,
+       .close          = dgram_close,
+       .bind           = dgram_bind,
+       .sendmsg        = dgram_sendmsg,
+       .recvmsg        = dgram_recvmsg,
+       .hash           = dgram_hash,
+       .unhash         = dgram_unhash,
+       .connect        = dgram_connect,
+       .disconnect     = dgram_disconnect,
+       .ioctl          = dgram_ioctl,
+};
+


------------------------------------------------------------------------------
Colocation vs. Managed Hosting
A question and answer guide to determining the best fit
for your organization - today and in the future.
http://p.sf.net/sfu/internap-sfd2d
_______________________________________________
Linux-zigbee-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-zigbee-devel

Reply via email to