mac802154.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * IEEE802.15.4-2003 specification
  4. *
  5. * Copyright (C) 2007-2012 Siemens AG
  6. */
  7. #ifndef NET_MAC802154_H
  8. #define NET_MAC802154_H
  9. #include <linux/unaligned.h>
  10. #include <net/af_ieee802154.h>
  11. #include <linux/ieee802154.h>
  12. #include <linux/skbuff.h>
  13. #include <net/cfg802154.h>
  14. /**
  15. * enum ieee802154_hw_addr_filt_flags - hardware address filtering flags
  16. *
  17. * The following flags are used to indicate changed address settings from
  18. * the stack to the hardware.
  19. *
  20. * @IEEE802154_AFILT_SADDR_CHANGED: Indicates that the short address will be
  21. * change.
  22. *
  23. * @IEEE802154_AFILT_IEEEADDR_CHANGED: Indicates that the extended address
  24. * will be change.
  25. *
  26. * @IEEE802154_AFILT_PANID_CHANGED: Indicates that the pan id will be change.
  27. *
  28. * @IEEE802154_AFILT_PANC_CHANGED: Indicates that the address filter will
  29. * do frame address filtering as a pan coordinator.
  30. */
  31. enum ieee802154_hw_addr_filt_flags {
  32. IEEE802154_AFILT_SADDR_CHANGED = BIT(0),
  33. IEEE802154_AFILT_IEEEADDR_CHANGED = BIT(1),
  34. IEEE802154_AFILT_PANID_CHANGED = BIT(2),
  35. IEEE802154_AFILT_PANC_CHANGED = BIT(3),
  36. };
  37. /**
  38. * struct ieee802154_hw_addr_filt - hardware address filtering settings
  39. *
  40. * @pan_id: pan_id which should be set to the hardware address filter.
  41. *
  42. * @short_addr: short_addr which should be set to the hardware address filter.
  43. *
  44. * @ieee_addr: extended address which should be set to the hardware address
  45. * filter.
  46. *
  47. * @pan_coord: boolean if hardware filtering should be operate as coordinator.
  48. */
  49. struct ieee802154_hw_addr_filt {
  50. __le16 pan_id;
  51. __le16 short_addr;
  52. __le64 ieee_addr;
  53. bool pan_coord;
  54. };
  55. /**
  56. * struct ieee802154_hw - ieee802154 hardware
  57. *
  58. * @extra_tx_headroom: headroom to reserve in each transmit skb for use by the
  59. * driver (e.g. for transmit headers.)
  60. *
  61. * @flags: hardware flags, see &enum ieee802154_hw_flags
  62. *
  63. * @parent: parent device of the hardware.
  64. *
  65. * @priv: pointer to private area that was allocated for driver use along with
  66. * this structure.
  67. *
  68. * @phy: This points to the &struct wpan_phy allocated for this 802.15.4 PHY.
  69. */
  70. struct ieee802154_hw {
  71. /* filled by the driver */
  72. int extra_tx_headroom;
  73. u32 flags;
  74. struct device *parent;
  75. void *priv;
  76. /* filled by mac802154 core */
  77. struct wpan_phy *phy;
  78. };
  79. /**
  80. * enum ieee802154_hw_flags - hardware flags
  81. *
  82. * These flags are used to indicate hardware capabilities to
  83. * the stack. Generally, flags here should have their meaning
  84. * done in a way that the simplest hardware doesn't need setting
  85. * any particular flags. There are some exceptions to this rule,
  86. * however, so you are advised to review these flags carefully.
  87. *
  88. * @IEEE802154_HW_TX_OMIT_CKSUM: Indicates that xmitter will add FCS on it's
  89. * own.
  90. *
  91. * @IEEE802154_HW_LBT: Indicates that transceiver will support listen before
  92. * transmit.
  93. *
  94. * @IEEE802154_HW_CSMA_PARAMS: Indicates that transceiver will support csma
  95. * parameters (max_be, min_be, backoff exponents).
  96. *
  97. * @IEEE802154_HW_FRAME_RETRIES: Indicates that transceiver will support ARET
  98. * frame retries setting.
  99. *
  100. * @IEEE802154_HW_AFILT: Indicates that transceiver will support hardware
  101. * address filter setting.
  102. *
  103. * @IEEE802154_HW_PROMISCUOUS: Indicates that transceiver will support
  104. * promiscuous mode setting.
  105. *
  106. * @IEEE802154_HW_RX_OMIT_CKSUM: Indicates that receiver omits FCS.
  107. */
  108. enum ieee802154_hw_flags {
  109. IEEE802154_HW_TX_OMIT_CKSUM = BIT(0),
  110. IEEE802154_HW_LBT = BIT(1),
  111. IEEE802154_HW_CSMA_PARAMS = BIT(2),
  112. IEEE802154_HW_FRAME_RETRIES = BIT(3),
  113. IEEE802154_HW_AFILT = BIT(4),
  114. IEEE802154_HW_PROMISCUOUS = BIT(5),
  115. IEEE802154_HW_RX_OMIT_CKSUM = BIT(6),
  116. };
  117. /* Indicates that receiver omits FCS and xmitter will add FCS on it's own. */
  118. #define IEEE802154_HW_OMIT_CKSUM (IEEE802154_HW_TX_OMIT_CKSUM | \
  119. IEEE802154_HW_RX_OMIT_CKSUM)
  120. /* struct ieee802154_ops - callbacks from mac802154 to the driver
  121. *
  122. * This structure contains various callbacks that the driver may
  123. * handle or, in some cases, must handle, for example to transmit
  124. * a frame.
  125. *
  126. * start: Handler that 802.15.4 module calls for device initialization.
  127. * This function is called before the first interface is attached.
  128. *
  129. * stop: Handler that 802.15.4 module calls for device cleanup.
  130. * This function is called after the last interface is removed.
  131. *
  132. * xmit_sync:
  133. * Handler that 802.15.4 module calls for each transmitted frame.
  134. * skb contains the buffer starting from the IEEE 802.15.4 header.
  135. * The low-level driver should send the frame based on available
  136. * configuration. This is called by a workqueue and useful for
  137. * synchronous 802.15.4 drivers.
  138. * This function should return zero or negative errno.
  139. *
  140. * WARNING:
  141. * This will be deprecated soon. We don't accept synced xmit callbacks
  142. * drivers anymore.
  143. *
  144. * xmit_async:
  145. * Handler that 802.15.4 module calls for each transmitted frame.
  146. * skb contains the buffer starting from the IEEE 802.15.4 header.
  147. * The low-level driver should send the frame based on available
  148. * configuration.
  149. * This function should return zero or negative errno.
  150. *
  151. * ed: Handler that 802.15.4 module calls for Energy Detection.
  152. * This function should place the value for detected energy
  153. * (usually device-dependant) in the level pointer and return
  154. * either zero or negative errno. Called with pib_lock held.
  155. *
  156. * set_channel:
  157. * Set radio for listening on specific channel.
  158. * Set the device for listening on specified channel.
  159. * Returns either zero, or negative errno. Called with pib_lock held.
  160. *
  161. * set_hw_addr_filt:
  162. * Set radio for listening on specific address.
  163. * Set the device for listening on specified address.
  164. * Returns either zero, or negative errno.
  165. *
  166. * set_txpower:
  167. * Set radio transmit power in mBm. Called with pib_lock held.
  168. * Returns either zero, or negative errno.
  169. *
  170. * set_lbt
  171. * Enables or disables listen before talk on the device. Called with
  172. * pib_lock held.
  173. * Returns either zero, or negative errno.
  174. *
  175. * set_cca_mode
  176. * Sets the CCA mode used by the device. Called with pib_lock held.
  177. * Returns either zero, or negative errno.
  178. *
  179. * set_cca_ed_level
  180. * Sets the CCA energy detection threshold in mBm. Called with pib_lock
  181. * held.
  182. * Returns either zero, or negative errno.
  183. *
  184. * set_csma_params
  185. * Sets the CSMA parameter set for the PHY. Called with pib_lock held.
  186. * Returns either zero, or negative errno.
  187. *
  188. * set_frame_retries
  189. * Sets the retransmission attempt limit. Called with pib_lock held.
  190. * Returns either zero, or negative errno.
  191. *
  192. * set_promiscuous_mode
  193. * Enables or disable promiscuous mode.
  194. */
  195. struct ieee802154_ops {
  196. struct module *owner;
  197. int (*start)(struct ieee802154_hw *hw);
  198. void (*stop)(struct ieee802154_hw *hw);
  199. int (*xmit_sync)(struct ieee802154_hw *hw,
  200. struct sk_buff *skb);
  201. int (*xmit_async)(struct ieee802154_hw *hw,
  202. struct sk_buff *skb);
  203. int (*ed)(struct ieee802154_hw *hw, u8 *level);
  204. int (*set_channel)(struct ieee802154_hw *hw, u8 page,
  205. u8 channel);
  206. int (*set_hw_addr_filt)(struct ieee802154_hw *hw,
  207. struct ieee802154_hw_addr_filt *filt,
  208. unsigned long changed);
  209. int (*set_txpower)(struct ieee802154_hw *hw, s32 mbm);
  210. int (*set_lbt)(struct ieee802154_hw *hw, bool on);
  211. int (*set_cca_mode)(struct ieee802154_hw *hw,
  212. const struct wpan_phy_cca *cca);
  213. int (*set_cca_ed_level)(struct ieee802154_hw *hw, s32 mbm);
  214. int (*set_csma_params)(struct ieee802154_hw *hw,
  215. u8 min_be, u8 max_be, u8 retries);
  216. int (*set_frame_retries)(struct ieee802154_hw *hw,
  217. s8 retries);
  218. int (*set_promiscuous_mode)(struct ieee802154_hw *hw,
  219. const bool on);
  220. };
  221. /**
  222. * ieee802154_get_fc_from_skb - get the frame control field from an skb
  223. * @skb: skb where the frame control field will be get from
  224. */
  225. static inline __le16 ieee802154_get_fc_from_skb(const struct sk_buff *skb)
  226. {
  227. __le16 fc;
  228. /* check if we can fc at skb_mac_header of sk buffer */
  229. if (WARN_ON(!skb_mac_header_was_set(skb) ||
  230. (skb_tail_pointer(skb) -
  231. skb_mac_header(skb)) < IEEE802154_FC_LEN))
  232. return cpu_to_le16(0);
  233. memcpy(&fc, skb_mac_header(skb), IEEE802154_FC_LEN);
  234. return fc;
  235. }
  236. /**
  237. * ieee802154_skb_dst_pan - get the pointer to destination pan field
  238. * @fc: mac header frame control field
  239. * @skb: skb where the destination pan pointer will be get from
  240. */
  241. static inline unsigned char *ieee802154_skb_dst_pan(__le16 fc,
  242. const struct sk_buff *skb)
  243. {
  244. unsigned char *dst_pan;
  245. switch (ieee802154_daddr_mode(fc)) {
  246. case cpu_to_le16(IEEE802154_FCTL_ADDR_NONE):
  247. dst_pan = NULL;
  248. break;
  249. case cpu_to_le16(IEEE802154_FCTL_DADDR_SHORT):
  250. case cpu_to_le16(IEEE802154_FCTL_DADDR_EXTENDED):
  251. dst_pan = skb_mac_header(skb) +
  252. IEEE802154_FC_LEN +
  253. IEEE802154_SEQ_LEN;
  254. break;
  255. default:
  256. WARN_ONCE(1, "invalid addr mode detected");
  257. dst_pan = NULL;
  258. break;
  259. }
  260. return dst_pan;
  261. }
  262. /**
  263. * ieee802154_skb_src_pan - get the pointer to source pan field
  264. * @fc: mac header frame control field
  265. * @skb: skb where the source pan pointer will be get from
  266. */
  267. static inline unsigned char *ieee802154_skb_src_pan(__le16 fc,
  268. const struct sk_buff *skb)
  269. {
  270. unsigned char *src_pan;
  271. switch (ieee802154_saddr_mode(fc)) {
  272. case cpu_to_le16(IEEE802154_FCTL_ADDR_NONE):
  273. src_pan = NULL;
  274. break;
  275. case cpu_to_le16(IEEE802154_FCTL_SADDR_SHORT):
  276. case cpu_to_le16(IEEE802154_FCTL_SADDR_EXTENDED):
  277. /* if intra-pan and source addr mode is non none,
  278. * then source pan id is equal destination pan id.
  279. */
  280. if (ieee802154_is_intra_pan(fc)) {
  281. src_pan = ieee802154_skb_dst_pan(fc, skb);
  282. break;
  283. }
  284. switch (ieee802154_daddr_mode(fc)) {
  285. case cpu_to_le16(IEEE802154_FCTL_ADDR_NONE):
  286. src_pan = skb_mac_header(skb) +
  287. IEEE802154_FC_LEN +
  288. IEEE802154_SEQ_LEN;
  289. break;
  290. case cpu_to_le16(IEEE802154_FCTL_DADDR_SHORT):
  291. src_pan = skb_mac_header(skb) +
  292. IEEE802154_FC_LEN +
  293. IEEE802154_SEQ_LEN +
  294. IEEE802154_PAN_ID_LEN +
  295. IEEE802154_SHORT_ADDR_LEN;
  296. break;
  297. case cpu_to_le16(IEEE802154_FCTL_DADDR_EXTENDED):
  298. src_pan = skb_mac_header(skb) +
  299. IEEE802154_FC_LEN +
  300. IEEE802154_SEQ_LEN +
  301. IEEE802154_PAN_ID_LEN +
  302. IEEE802154_EXTENDED_ADDR_LEN;
  303. break;
  304. default:
  305. WARN_ONCE(1, "invalid addr mode detected");
  306. src_pan = NULL;
  307. break;
  308. }
  309. break;
  310. default:
  311. WARN_ONCE(1, "invalid addr mode detected");
  312. src_pan = NULL;
  313. break;
  314. }
  315. return src_pan;
  316. }
  317. /**
  318. * ieee802154_skb_is_intra_pan_addressing - checks whenever the mac addressing
  319. * is an intra pan communication
  320. * @fc: mac header frame control field
  321. * @skb: skb where the source and destination pan should be get from
  322. */
  323. static inline bool ieee802154_skb_is_intra_pan_addressing(__le16 fc,
  324. const struct sk_buff *skb)
  325. {
  326. unsigned char *dst_pan = ieee802154_skb_dst_pan(fc, skb),
  327. *src_pan = ieee802154_skb_src_pan(fc, skb);
  328. /* if one is NULL is no intra pan addressing */
  329. if (!dst_pan || !src_pan)
  330. return false;
  331. return !memcmp(dst_pan, src_pan, IEEE802154_PAN_ID_LEN);
  332. }
  333. /**
  334. * ieee802154_be64_to_le64 - copies and convert be64 to le64
  335. * @le64_dst: le64 destination pointer
  336. * @be64_src: be64 source pointer
  337. */
  338. static inline void ieee802154_be64_to_le64(void *le64_dst, const void *be64_src)
  339. {
  340. put_unaligned_le64(get_unaligned_be64(be64_src), le64_dst);
  341. }
  342. /**
  343. * ieee802154_le64_to_be64 - copies and convert le64 to be64
  344. * @be64_dst: be64 destination pointer
  345. * @le64_src: le64 source pointer
  346. */
  347. static inline void ieee802154_le64_to_be64(void *be64_dst, const void *le64_src)
  348. {
  349. put_unaligned_be64(get_unaligned_le64(le64_src), be64_dst);
  350. }
  351. /**
  352. * ieee802154_le16_to_be16 - copies and convert le16 to be16
  353. * @be16_dst: be16 destination pointer
  354. * @le16_src: le16 source pointer
  355. */
  356. static inline void ieee802154_le16_to_be16(void *be16_dst, const void *le16_src)
  357. {
  358. put_unaligned_be16(get_unaligned_le16(le16_src), be16_dst);
  359. }
  360. /**
  361. * ieee802154_be16_to_le16 - copies and convert be16 to le16
  362. * @le16_dst: le16 destination pointer
  363. * @be16_src: be16 source pointer
  364. */
  365. static inline void ieee802154_be16_to_le16(void *le16_dst, const void *be16_src)
  366. {
  367. put_unaligned_le16(get_unaligned_be16(be16_src), le16_dst);
  368. }
  369. /**
  370. * ieee802154_alloc_hw - Allocate a new hardware device
  371. *
  372. * This must be called once for each hardware device. The returned pointer
  373. * must be used to refer to this device when calling other functions.
  374. * mac802154 allocates a private data area for the driver pointed to by
  375. * @priv in &struct ieee802154_hw, the size of this area is given as
  376. * @priv_data_len.
  377. *
  378. * @priv_data_len: length of private data
  379. * @ops: callbacks for this device
  380. *
  381. * Return: A pointer to the new hardware device, or %NULL on error.
  382. */
  383. struct ieee802154_hw *
  384. ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops);
  385. /**
  386. * ieee802154_free_hw - free hardware descriptor
  387. *
  388. * This function frees everything that was allocated, including the
  389. * private data for the driver. You must call ieee802154_unregister_hw()
  390. * before calling this function.
  391. *
  392. * @hw: the hardware to free
  393. */
  394. void ieee802154_free_hw(struct ieee802154_hw *hw);
  395. /**
  396. * ieee802154_register_hw - Register hardware device
  397. *
  398. * You must call this function before any other functions in
  399. * mac802154. Note that before a hardware can be registered, you
  400. * need to fill the contained wpan_phy's information.
  401. *
  402. * @hw: the device to register as returned by ieee802154_alloc_hw()
  403. *
  404. * Return: 0 on success. An error code otherwise.
  405. */
  406. int ieee802154_register_hw(struct ieee802154_hw *hw);
  407. /**
  408. * ieee802154_unregister_hw - Unregister a hardware device
  409. *
  410. * This function instructs mac802154 to free allocated resources
  411. * and unregister netdevices from the networking subsystem.
  412. *
  413. * @hw: the hardware to unregister
  414. */
  415. void ieee802154_unregister_hw(struct ieee802154_hw *hw);
  416. /**
  417. * ieee802154_rx_irqsafe - receive frame
  418. *
  419. * Like ieee802154_rx() but can be called in IRQ context
  420. * (internally defers to a tasklet.)
  421. *
  422. * @hw: the hardware this frame came in on
  423. * @skb: the buffer to receive, owned by mac802154 after this call
  424. * @lqi: link quality indicator
  425. */
  426. void ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb,
  427. u8 lqi);
  428. /**
  429. * ieee802154_xmit_complete - frame transmission complete
  430. *
  431. * @hw: pointer as obtained from ieee802154_alloc_hw().
  432. * @skb: buffer for transmission
  433. * @ifs_handling: indicate interframe space handling
  434. */
  435. void ieee802154_xmit_complete(struct ieee802154_hw *hw, struct sk_buff *skb,
  436. bool ifs_handling);
  437. /**
  438. * ieee802154_xmit_error - offloaded frame transmission failed
  439. *
  440. * @hw: pointer as obtained from ieee802154_alloc_hw().
  441. * @skb: buffer for transmission
  442. * @reason: error code
  443. */
  444. void ieee802154_xmit_error(struct ieee802154_hw *hw, struct sk_buff *skb,
  445. int reason);
  446. /**
  447. * ieee802154_xmit_hw_error - frame could not be offloaded to the transmitter
  448. * because of a hardware error (bus error, timeout, etc)
  449. *
  450. * @hw: pointer as obtained from ieee802154_alloc_hw().
  451. * @skb: buffer for transmission
  452. */
  453. void ieee802154_xmit_hw_error(struct ieee802154_hw *hw, struct sk_buff *skb);
  454. #endif /* NET_MAC802154_H */