xref: /OK3568_Linux_fs/kernel/Documentation/networking/ieee802154.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun===============================
2*4882a593SmuzhiyunIEEE 802.15.4 Developer's Guide
3*4882a593Smuzhiyun===============================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunIntroduction
6*4882a593Smuzhiyun============
7*4882a593SmuzhiyunThe IEEE 802.15.4 working group focuses on standardization of the bottom
8*4882a593Smuzhiyuntwo layers: Medium Access Control (MAC) and Physical access (PHY). And there
9*4882a593Smuzhiyunare mainly two options available for upper layers:
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun- ZigBee - proprietary protocol from the ZigBee Alliance
12*4882a593Smuzhiyun- 6LoWPAN - IPv6 networking over low rate personal area networks
13*4882a593Smuzhiyun
14*4882a593SmuzhiyunThe goal of the Linux-wpan is to provide a complete implementation
15*4882a593Smuzhiyunof the IEEE 802.15.4 and 6LoWPAN protocols. IEEE 802.15.4 is a stack
16*4882a593Smuzhiyunof protocols for organizing Low-Rate Wireless Personal Area Networks.
17*4882a593Smuzhiyun
18*4882a593SmuzhiyunThe stack is composed of three main parts:
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun- IEEE 802.15.4 layer;  We have chosen to use plain Berkeley socket API,
21*4882a593Smuzhiyun  the generic Linux networking stack to transfer IEEE 802.15.4 data
22*4882a593Smuzhiyun  messages and a special protocol over netlink for configuration/management
23*4882a593Smuzhiyun- MAC - provides access to shared channel and reliable data delivery
24*4882a593Smuzhiyun- PHY - represents device drivers
25*4882a593Smuzhiyun
26*4882a593SmuzhiyunSocket API
27*4882a593Smuzhiyun==========
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun::
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun    int sd = socket(PF_IEEE802154, SOCK_DGRAM, 0);
32*4882a593Smuzhiyun
33*4882a593SmuzhiyunThe address family, socket addresses etc. are defined in the
34*4882a593Smuzhiyuninclude/net/af_ieee802154.h header or in the special header
35*4882a593Smuzhiyunin the userspace package (see either https://linux-wpan.org/wpan-tools.html
36*4882a593Smuzhiyunor the git tree at https://github.com/linux-wpan/wpan-tools).
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun6LoWPAN Linux implementation
39*4882a593Smuzhiyun============================
40*4882a593Smuzhiyun
41*4882a593SmuzhiyunThe IEEE 802.15.4 standard specifies an MTU of 127 bytes, yielding about 80
42*4882a593Smuzhiyunoctets of actual MAC payload once security is turned on, on a wireless link
43*4882a593Smuzhiyunwith a link throughput of 250 kbps or less.  The 6LoWPAN adaptation format
44*4882a593Smuzhiyun[RFC4944] was specified to carry IPv6 datagrams over such constrained links,
45*4882a593Smuzhiyuntaking into account limited bandwidth, memory, or energy resources that are
46*4882a593Smuzhiyunexpected in applications such as wireless Sensor Networks.  [RFC4944] defines
47*4882a593Smuzhiyuna Mesh Addressing header to support sub-IP forwarding, a Fragmentation header
48*4882a593Smuzhiyunto support the IPv6 minimum MTU requirement [RFC2460], and stateless header
49*4882a593Smuzhiyuncompression for IPv6 datagrams (LOWPAN_HC1 and LOWPAN_HC2) to reduce the
50*4882a593Smuzhiyunrelatively large IPv6 and UDP headers down to (in the best case) several bytes.
51*4882a593Smuzhiyun
52*4882a593SmuzhiyunIn September 2011 the standard update was published - [RFC6282].
53*4882a593SmuzhiyunIt deprecates HC1 and HC2 compression and defines IPHC encoding format which is
54*4882a593Smuzhiyunused in this Linux implementation.
55*4882a593Smuzhiyun
56*4882a593SmuzhiyunAll the code related to 6lowpan you may find in files: net/6lowpan/*
57*4882a593Smuzhiyunand net/ieee802154/6lowpan/*
58*4882a593Smuzhiyun
59*4882a593SmuzhiyunTo setup a 6LoWPAN interface you need:
60*4882a593Smuzhiyun1. Add IEEE802.15.4 interface and set channel and PAN ID;
61*4882a593Smuzhiyun2. Add 6lowpan interface by command like:
62*4882a593Smuzhiyun# ip link add link wpan0 name lowpan0 type lowpan
63*4882a593Smuzhiyun3. Bring up 'lowpan0' interface
64*4882a593Smuzhiyun
65*4882a593SmuzhiyunDrivers
66*4882a593Smuzhiyun=======
67*4882a593Smuzhiyun
68*4882a593SmuzhiyunLike with WiFi, there are several types of devices implementing IEEE 802.15.4.
69*4882a593Smuzhiyun1) 'HardMAC'. The MAC layer is implemented in the device itself, the device
70*4882a593Smuzhiyunexports a management (e.g. MLME) and data API.
71*4882a593Smuzhiyun2) 'SoftMAC' or just radio. These types of devices are just radio transceivers
72*4882a593Smuzhiyunpossibly with some kinds of acceleration like automatic CRC computation and
73*4882a593Smuzhiyuncomparation, automagic ACK handling, address matching, etc.
74*4882a593Smuzhiyun
75*4882a593SmuzhiyunThose types of devices require different approach to be hooked into Linux kernel.
76*4882a593Smuzhiyun
77*4882a593SmuzhiyunHardMAC
78*4882a593Smuzhiyun-------
79*4882a593Smuzhiyun
80*4882a593SmuzhiyunSee the header include/net/ieee802154_netdev.h. You have to implement Linux
81*4882a593Smuzhiyunnet_device, with .type = ARPHRD_IEEE802154. Data is exchanged with socket family
82*4882a593Smuzhiyuncode via plain sk_buffs. On skb reception skb->cb must contain additional
83*4882a593Smuzhiyuninfo as described in the struct ieee802154_mac_cb. During packet transmission
84*4882a593Smuzhiyunthe skb->cb is used to provide additional data to device's header_ops->create
85*4882a593Smuzhiyunfunction. Be aware that this data can be overridden later (when socket code
86*4882a593Smuzhiyunsubmits skb to qdisc), so if you need something from that cb later, you should
87*4882a593Smuzhiyunstore info in the skb->data on your own.
88*4882a593Smuzhiyun
89*4882a593SmuzhiyunTo hook the MLME interface you have to populate the ml_priv field of your
90*4882a593Smuzhiyunnet_device with a pointer to struct ieee802154_mlme_ops instance. The fields
91*4882a593Smuzhiyunassoc_req, assoc_resp, disassoc_req, start_req, and scan_req are optional.
92*4882a593SmuzhiyunAll other fields are required.
93*4882a593Smuzhiyun
94*4882a593SmuzhiyunSoftMAC
95*4882a593Smuzhiyun-------
96*4882a593Smuzhiyun
97*4882a593SmuzhiyunThe MAC is the middle layer in the IEEE 802.15.4 Linux stack. This moment it
98*4882a593Smuzhiyunprovides interface for drivers registration and management of slave interfaces.
99*4882a593Smuzhiyun
100*4882a593SmuzhiyunNOTE: Currently the only monitor device type is supported - it's IEEE 802.15.4
101*4882a593Smuzhiyunstack interface for network sniffers (e.g. WireShark).
102*4882a593Smuzhiyun
103*4882a593SmuzhiyunThis layer is going to be extended soon.
104*4882a593Smuzhiyun
105*4882a593SmuzhiyunSee header include/net/mac802154.h and several drivers in
106*4882a593Smuzhiyundrivers/net/ieee802154/.
107*4882a593Smuzhiyun
108*4882a593SmuzhiyunFake drivers
109*4882a593Smuzhiyun------------
110*4882a593Smuzhiyun
111*4882a593SmuzhiyunIn addition there is a driver available which simulates a real device with
112*4882a593SmuzhiyunSoftMAC (fakelb - IEEE 802.15.4 loopback driver) interface. This option
113*4882a593Smuzhiyunprovides a possibility to test and debug the stack without usage of real hardware.
114*4882a593Smuzhiyun
115*4882a593SmuzhiyunDevice drivers API
116*4882a593Smuzhiyun==================
117*4882a593Smuzhiyun
118*4882a593SmuzhiyunThe include/net/mac802154.h defines following functions:
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun.. c:function:: struct ieee802154_dev *ieee802154_alloc_device (size_t priv_size, struct ieee802154_ops *ops)
121*4882a593Smuzhiyun
122*4882a593SmuzhiyunAllocation of IEEE 802.15.4 compatible device.
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun.. c:function:: void ieee802154_free_device(struct ieee802154_dev *dev)
125*4882a593Smuzhiyun
126*4882a593SmuzhiyunFreeing allocated device.
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun.. c:function:: int ieee802154_register_device(struct ieee802154_dev *dev)
129*4882a593Smuzhiyun
130*4882a593SmuzhiyunRegister PHY in the system.
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun.. c:function:: void ieee802154_unregister_device(struct ieee802154_dev *dev)
133*4882a593Smuzhiyun
134*4882a593SmuzhiyunFreeing registered PHY.
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun.. c:function:: void ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb, u8 lqi)
137*4882a593Smuzhiyun
138*4882a593SmuzhiyunTelling 802.15.4 module there is a new received frame in the skb with
139*4882a593Smuzhiyunthe RF Link Quality Indicator (LQI) from the hardware device.
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun.. c:function:: void ieee802154_xmit_complete(struct ieee802154_hw *hw, struct sk_buff *skb, bool ifs_handling)
142*4882a593Smuzhiyun
143*4882a593SmuzhiyunTelling 802.15.4 module the frame in the skb is or going to be
144*4882a593Smuzhiyuntransmitted through the hardware device
145*4882a593Smuzhiyun
146*4882a593SmuzhiyunThe device driver must implement the following callbacks in the IEEE 802.15.4
147*4882a593Smuzhiyunoperations structure at least::
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun   struct ieee802154_ops {
150*4882a593Smuzhiyun        ...
151*4882a593Smuzhiyun        int     (*start)(struct ieee802154_hw *hw);
152*4882a593Smuzhiyun        void    (*stop)(struct ieee802154_hw *hw);
153*4882a593Smuzhiyun        ...
154*4882a593Smuzhiyun        int     (*xmit_async)(struct ieee802154_hw *hw, struct sk_buff *skb);
155*4882a593Smuzhiyun        int     (*ed)(struct ieee802154_hw *hw, u8 *level);
156*4882a593Smuzhiyun        int     (*set_channel)(struct ieee802154_hw *hw, u8 page, u8 channel);
157*4882a593Smuzhiyun        ...
158*4882a593Smuzhiyun   };
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun.. c:function:: int start(struct ieee802154_hw *hw)
161*4882a593Smuzhiyun
162*4882a593SmuzhiyunHandler that 802.15.4 module calls for the hardware device initialization.
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun.. c:function:: void stop(struct ieee802154_hw *hw)
165*4882a593Smuzhiyun
166*4882a593SmuzhiyunHandler that 802.15.4 module calls for the hardware device cleanup.
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun.. c:function:: int xmit_async(struct ieee802154_hw *hw, struct sk_buff *skb)
169*4882a593Smuzhiyun
170*4882a593SmuzhiyunHandler that 802.15.4 module calls for each frame in the skb going to be
171*4882a593Smuzhiyuntransmitted through the hardware device.
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun.. c:function:: int ed(struct ieee802154_hw *hw, u8 *level)
174*4882a593Smuzhiyun
175*4882a593SmuzhiyunHandler that 802.15.4 module calls for Energy Detection from the hardware
176*4882a593Smuzhiyundevice.
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun.. c:function:: int set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
179*4882a593Smuzhiyun
180*4882a593SmuzhiyunSet radio for listening on specific channel of the hardware device.
181*4882a593Smuzhiyun
182*4882a593SmuzhiyunMoreover IEEE 802.15.4 device operations structure should be filled.
183