xref: /openbmc/linux/Documentation/networking/ieee802154.rst (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
18e4a0740SStefan Schmidt===============================
28e4a0740SStefan SchmidtIEEE 802.15.4 Developer's Guide
38e4a0740SStefan Schmidt===============================
48e4a0740SStefan Schmidt
58e4a0740SStefan SchmidtIntroduction
68e4a0740SStefan Schmidt============
78e4a0740SStefan SchmidtThe IEEE 802.15.4 working group focuses on standardization of the bottom
88e4a0740SStefan Schmidttwo layers: Medium Access Control (MAC) and Physical access (PHY). And there
98e4a0740SStefan Schmidtare mainly two options available for upper layers:
108e4a0740SStefan Schmidt
118e4a0740SStefan Schmidt- ZigBee - proprietary protocol from the ZigBee Alliance
128e4a0740SStefan Schmidt- 6LoWPAN - IPv6 networking over low rate personal area networks
138e4a0740SStefan Schmidt
148e4a0740SStefan SchmidtThe goal of the Linux-wpan is to provide a complete implementation
158e4a0740SStefan Schmidtof the IEEE 802.15.4 and 6LoWPAN protocols. IEEE 802.15.4 is a stack
168e4a0740SStefan Schmidtof protocols for organizing Low-Rate Wireless Personal Area Networks.
178e4a0740SStefan Schmidt
188e4a0740SStefan SchmidtThe stack is composed of three main parts:
198e4a0740SStefan Schmidt
208e4a0740SStefan Schmidt- IEEE 802.15.4 layer;  We have chosen to use plain Berkeley socket API,
218e4a0740SStefan Schmidt  the generic Linux networking stack to transfer IEEE 802.15.4 data
228e4a0740SStefan Schmidt  messages and a special protocol over netlink for configuration/management
238e4a0740SStefan Schmidt- MAC - provides access to shared channel and reliable data delivery
248e4a0740SStefan Schmidt- PHY - represents device drivers
258e4a0740SStefan Schmidt
268e4a0740SStefan SchmidtSocket API
278e4a0740SStefan Schmidt==========
288e4a0740SStefan Schmidt
29640e3f80SMauro Carvalho Chehab::
30640e3f80SMauro Carvalho Chehab
31640e3f80SMauro Carvalho Chehab    int sd = socket(PF_IEEE802154, SOCK_DGRAM, 0);
328e4a0740SStefan Schmidt
338e4a0740SStefan SchmidtThe address family, socket addresses etc. are defined in the
348e4a0740SStefan Schmidtinclude/net/af_ieee802154.h header or in the special header
35e2c0b971SStefan Schmidtin the userspace package (see either https://linux-wpan.org/wpan-tools.html
36e2c0b971SStefan Schmidtor the git tree at https://github.com/linux-wpan/wpan-tools).
378e4a0740SStefan Schmidt
388e4a0740SStefan Schmidt6LoWPAN Linux implementation
398e4a0740SStefan Schmidt============================
408e4a0740SStefan Schmidt
418e4a0740SStefan SchmidtThe IEEE 802.15.4 standard specifies an MTU of 127 bytes, yielding about 80
428e4a0740SStefan Schmidtoctets of actual MAC payload once security is turned on, on a wireless link
438e4a0740SStefan Schmidtwith a link throughput of 250 kbps or less.  The 6LoWPAN adaptation format
448e4a0740SStefan Schmidt[RFC4944] was specified to carry IPv6 datagrams over such constrained links,
458e4a0740SStefan Schmidttaking into account limited bandwidth, memory, or energy resources that are
468e4a0740SStefan Schmidtexpected in applications such as wireless Sensor Networks.  [RFC4944] defines
478e4a0740SStefan Schmidta Mesh Addressing header to support sub-IP forwarding, a Fragmentation header
488e4a0740SStefan Schmidtto support the IPv6 minimum MTU requirement [RFC2460], and stateless header
498e4a0740SStefan Schmidtcompression for IPv6 datagrams (LOWPAN_HC1 and LOWPAN_HC2) to reduce the
508e4a0740SStefan Schmidtrelatively large IPv6 and UDP headers down to (in the best case) several bytes.
518e4a0740SStefan Schmidt
528e4a0740SStefan SchmidtIn September 2011 the standard update was published - [RFC6282].
538e4a0740SStefan SchmidtIt deprecates HC1 and HC2 compression and defines IPHC encoding format which is
548e4a0740SStefan Schmidtused in this Linux implementation.
558e4a0740SStefan Schmidt
568e4a0740SStefan SchmidtAll the code related to 6lowpan you may find in files: net/6lowpan/*
578e4a0740SStefan Schmidtand net/ieee802154/6lowpan/*
588e4a0740SStefan Schmidt
598e4a0740SStefan SchmidtTo setup a 6LoWPAN interface you need:
608e4a0740SStefan Schmidt1. Add IEEE802.15.4 interface and set channel and PAN ID;
618e4a0740SStefan Schmidt2. Add 6lowpan interface by command like:
628e4a0740SStefan Schmidt# ip link add link wpan0 name lowpan0 type lowpan
638e4a0740SStefan Schmidt3. Bring up 'lowpan0' interface
648e4a0740SStefan Schmidt
658e4a0740SStefan SchmidtDrivers
668e4a0740SStefan Schmidt=======
678e4a0740SStefan Schmidt
688e4a0740SStefan SchmidtLike with WiFi, there are several types of devices implementing IEEE 802.15.4.
698e4a0740SStefan Schmidt1) 'HardMAC'. The MAC layer is implemented in the device itself, the device
708e4a0740SStefan Schmidtexports a management (e.g. MLME) and data API.
718e4a0740SStefan Schmidt2) 'SoftMAC' or just radio. These types of devices are just radio transceivers
728e4a0740SStefan Schmidtpossibly with some kinds of acceleration like automatic CRC computation and
73*a266ef69SRandy Dunlapcomparison, automagic ACK handling, address matching, etc.
748e4a0740SStefan Schmidt
758e4a0740SStefan SchmidtThose types of devices require different approach to be hooked into Linux kernel.
768e4a0740SStefan Schmidt
778e4a0740SStefan SchmidtHardMAC
788e4a0740SStefan Schmidt-------
798e4a0740SStefan Schmidt
808e4a0740SStefan SchmidtSee the header include/net/ieee802154_netdev.h. You have to implement Linux
818e4a0740SStefan Schmidtnet_device, with .type = ARPHRD_IEEE802154. Data is exchanged with socket family
828e4a0740SStefan Schmidtcode via plain sk_buffs. On skb reception skb->cb must contain additional
838e4a0740SStefan Schmidtinfo as described in the struct ieee802154_mac_cb. During packet transmission
848e4a0740SStefan Schmidtthe skb->cb is used to provide additional data to device's header_ops->create
858e4a0740SStefan Schmidtfunction. Be aware that this data can be overridden later (when socket code
868e4a0740SStefan Schmidtsubmits skb to qdisc), so if you need something from that cb later, you should
878e4a0740SStefan Schmidtstore info in the skb->data on your own.
888e4a0740SStefan Schmidt
898e4a0740SStefan SchmidtTo hook the MLME interface you have to populate the ml_priv field of your
908e4a0740SStefan Schmidtnet_device with a pointer to struct ieee802154_mlme_ops instance. The fields
918e4a0740SStefan Schmidtassoc_req, assoc_resp, disassoc_req, start_req, and scan_req are optional.
928e4a0740SStefan SchmidtAll other fields are required.
938e4a0740SStefan Schmidt
948e4a0740SStefan SchmidtSoftMAC
958e4a0740SStefan Schmidt-------
968e4a0740SStefan Schmidt
978e4a0740SStefan SchmidtThe MAC is the middle layer in the IEEE 802.15.4 Linux stack. This moment it
988e4a0740SStefan Schmidtprovides interface for drivers registration and management of slave interfaces.
998e4a0740SStefan Schmidt
1008e4a0740SStefan SchmidtNOTE: Currently the only monitor device type is supported - it's IEEE 802.15.4
1018e4a0740SStefan Schmidtstack interface for network sniffers (e.g. WireShark).
1028e4a0740SStefan Schmidt
1038e4a0740SStefan SchmidtThis layer is going to be extended soon.
1048e4a0740SStefan Schmidt
1058e4a0740SStefan SchmidtSee header include/net/mac802154.h and several drivers in
1068e4a0740SStefan Schmidtdrivers/net/ieee802154/.
1078e4a0740SStefan Schmidt
1088e4a0740SStefan SchmidtFake drivers
1098e4a0740SStefan Schmidt------------
1108e4a0740SStefan Schmidt
1118e4a0740SStefan SchmidtIn addition there is a driver available which simulates a real device with
1128e4a0740SStefan SchmidtSoftMAC (fakelb - IEEE 802.15.4 loopback driver) interface. This option
1138e4a0740SStefan Schmidtprovides a possibility to test and debug the stack without usage of real hardware.
1148e4a0740SStefan Schmidt
1158e4a0740SStefan SchmidtDevice drivers API
1168e4a0740SStefan Schmidt==================
1178e4a0740SStefan Schmidt
1188e4a0740SStefan SchmidtThe include/net/mac802154.h defines following functions:
1198e4a0740SStefan Schmidt
1208e4a0740SStefan Schmidt.. c:function:: struct ieee802154_dev *ieee802154_alloc_device (size_t priv_size, struct ieee802154_ops *ops)
1218e4a0740SStefan Schmidt
1228e4a0740SStefan SchmidtAllocation of IEEE 802.15.4 compatible device.
1238e4a0740SStefan Schmidt
1248e4a0740SStefan Schmidt.. c:function:: void ieee802154_free_device(struct ieee802154_dev *dev)
1258e4a0740SStefan Schmidt
1268e4a0740SStefan SchmidtFreeing allocated device.
1278e4a0740SStefan Schmidt
1288e4a0740SStefan Schmidt.. c:function:: int ieee802154_register_device(struct ieee802154_dev *dev)
1298e4a0740SStefan Schmidt
1308e4a0740SStefan SchmidtRegister PHY in the system.
1318e4a0740SStefan Schmidt
1328e4a0740SStefan Schmidt.. c:function:: void ieee802154_unregister_device(struct ieee802154_dev *dev)
1338e4a0740SStefan Schmidt
1348e4a0740SStefan SchmidtFreeing registered PHY.
1358e4a0740SStefan Schmidt
136640e3f80SMauro Carvalho Chehab.. c:function:: void ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb, u8 lqi)
1378e4a0740SStefan Schmidt
1388e4a0740SStefan SchmidtTelling 802.15.4 module there is a new received frame in the skb with
1398e4a0740SStefan Schmidtthe RF Link Quality Indicator (LQI) from the hardware device.
1408e4a0740SStefan Schmidt
141640e3f80SMauro Carvalho Chehab.. c:function:: void ieee802154_xmit_complete(struct ieee802154_hw *hw, struct sk_buff *skb, bool ifs_handling)
1428e4a0740SStefan Schmidt
1438e4a0740SStefan SchmidtTelling 802.15.4 module the frame in the skb is or going to be
1448e4a0740SStefan Schmidttransmitted through the hardware device
1458e4a0740SStefan Schmidt
1468e4a0740SStefan SchmidtThe device driver must implement the following callbacks in the IEEE 802.15.4
1478e4a0740SStefan Schmidtoperations structure at least::
1488e4a0740SStefan Schmidt
1498e4a0740SStefan Schmidt   struct ieee802154_ops {
1508e4a0740SStefan Schmidt        ...
1518e4a0740SStefan Schmidt        int     (*start)(struct ieee802154_hw *hw);
1528e4a0740SStefan Schmidt        void    (*stop)(struct ieee802154_hw *hw);
1538e4a0740SStefan Schmidt        ...
1548e4a0740SStefan Schmidt        int     (*xmit_async)(struct ieee802154_hw *hw, struct sk_buff *skb);
1558e4a0740SStefan Schmidt        int     (*ed)(struct ieee802154_hw *hw, u8 *level);
1568e4a0740SStefan Schmidt        int     (*set_channel)(struct ieee802154_hw *hw, u8 page, u8 channel);
1578e4a0740SStefan Schmidt        ...
1588e4a0740SStefan Schmidt   };
1598e4a0740SStefan Schmidt
160640e3f80SMauro Carvalho Chehab.. c:function:: int start(struct ieee802154_hw *hw)
1618e4a0740SStefan Schmidt
1628e4a0740SStefan SchmidtHandler that 802.15.4 module calls for the hardware device initialization.
1638e4a0740SStefan Schmidt
164640e3f80SMauro Carvalho Chehab.. c:function:: void stop(struct ieee802154_hw *hw)
1658e4a0740SStefan Schmidt
1668e4a0740SStefan SchmidtHandler that 802.15.4 module calls for the hardware device cleanup.
1678e4a0740SStefan Schmidt
168640e3f80SMauro Carvalho Chehab.. c:function:: int xmit_async(struct ieee802154_hw *hw, struct sk_buff *skb)
1698e4a0740SStefan Schmidt
1708e4a0740SStefan SchmidtHandler that 802.15.4 module calls for each frame in the skb going to be
1718e4a0740SStefan Schmidttransmitted through the hardware device.
1728e4a0740SStefan Schmidt
173640e3f80SMauro Carvalho Chehab.. c:function:: int ed(struct ieee802154_hw *hw, u8 *level)
1748e4a0740SStefan Schmidt
1758e4a0740SStefan SchmidtHandler that 802.15.4 module calls for Energy Detection from the hardware
1768e4a0740SStefan Schmidtdevice.
1778e4a0740SStefan Schmidt
178640e3f80SMauro Carvalho Chehab.. c:function:: int set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
1798e4a0740SStefan Schmidt
1808e4a0740SStefan SchmidtSet radio for listening on specific channel of the hardware device.
1818e4a0740SStefan Schmidt
1828e4a0740SStefan SchmidtMoreover IEEE 802.15.4 device operations structure should be filled.
183