cfg802154.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2007, 2008, 2009 Siemens AG
  4. *
  5. * Written by:
  6. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  7. */
  8. #ifndef __NET_CFG802154_H
  9. #define __NET_CFG802154_H
  10. #include <linux/ieee802154.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/bug.h>
  14. #include <net/nl802154.h>
  15. struct wpan_phy;
  16. struct wpan_phy_cca;
  17. struct cfg802154_scan_request;
  18. struct cfg802154_beacon_request;
  19. struct ieee802154_addr;
  20. #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  21. struct ieee802154_llsec_device_key;
  22. struct ieee802154_llsec_seclevel;
  23. struct ieee802154_llsec_params;
  24. struct ieee802154_llsec_device;
  25. struct ieee802154_llsec_table;
  26. struct ieee802154_llsec_key_id;
  27. struct ieee802154_llsec_key;
  28. #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
  29. struct cfg802154_ops {
  30. struct net_device * (*add_virtual_intf_deprecated)(struct wpan_phy *wpan_phy,
  31. const char *name,
  32. unsigned char name_assign_type,
  33. int type);
  34. void (*del_virtual_intf_deprecated)(struct wpan_phy *wpan_phy,
  35. struct net_device *dev);
  36. int (*suspend)(struct wpan_phy *wpan_phy);
  37. int (*resume)(struct wpan_phy *wpan_phy);
  38. int (*add_virtual_intf)(struct wpan_phy *wpan_phy,
  39. const char *name,
  40. unsigned char name_assign_type,
  41. enum nl802154_iftype type,
  42. __le64 extended_addr);
  43. int (*del_virtual_intf)(struct wpan_phy *wpan_phy,
  44. struct wpan_dev *wpan_dev);
  45. int (*set_channel)(struct wpan_phy *wpan_phy, u8 page, u8 channel);
  46. int (*set_cca_mode)(struct wpan_phy *wpan_phy,
  47. const struct wpan_phy_cca *cca);
  48. int (*set_cca_ed_level)(struct wpan_phy *wpan_phy, s32 ed_level);
  49. int (*set_tx_power)(struct wpan_phy *wpan_phy, s32 power);
  50. int (*set_pan_id)(struct wpan_phy *wpan_phy,
  51. struct wpan_dev *wpan_dev, __le16 pan_id);
  52. int (*set_short_addr)(struct wpan_phy *wpan_phy,
  53. struct wpan_dev *wpan_dev, __le16 short_addr);
  54. int (*set_backoff_exponent)(struct wpan_phy *wpan_phy,
  55. struct wpan_dev *wpan_dev, u8 min_be,
  56. u8 max_be);
  57. int (*set_max_csma_backoffs)(struct wpan_phy *wpan_phy,
  58. struct wpan_dev *wpan_dev,
  59. u8 max_csma_backoffs);
  60. int (*set_max_frame_retries)(struct wpan_phy *wpan_phy,
  61. struct wpan_dev *wpan_dev,
  62. s8 max_frame_retries);
  63. int (*set_lbt_mode)(struct wpan_phy *wpan_phy,
  64. struct wpan_dev *wpan_dev, bool mode);
  65. int (*set_ackreq_default)(struct wpan_phy *wpan_phy,
  66. struct wpan_dev *wpan_dev, bool ackreq);
  67. int (*trigger_scan)(struct wpan_phy *wpan_phy,
  68. struct cfg802154_scan_request *request);
  69. int (*abort_scan)(struct wpan_phy *wpan_phy,
  70. struct wpan_dev *wpan_dev);
  71. int (*send_beacons)(struct wpan_phy *wpan_phy,
  72. struct cfg802154_beacon_request *request);
  73. int (*stop_beacons)(struct wpan_phy *wpan_phy,
  74. struct wpan_dev *wpan_dev);
  75. int (*associate)(struct wpan_phy *wpan_phy,
  76. struct wpan_dev *wpan_dev,
  77. struct ieee802154_addr *coord);
  78. int (*disassociate)(struct wpan_phy *wpan_phy,
  79. struct wpan_dev *wpan_dev,
  80. struct ieee802154_addr *target);
  81. #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  82. void (*get_llsec_table)(struct wpan_phy *wpan_phy,
  83. struct wpan_dev *wpan_dev,
  84. struct ieee802154_llsec_table **table);
  85. void (*lock_llsec_table)(struct wpan_phy *wpan_phy,
  86. struct wpan_dev *wpan_dev);
  87. void (*unlock_llsec_table)(struct wpan_phy *wpan_phy,
  88. struct wpan_dev *wpan_dev);
  89. /* TODO remove locking/get table callbacks, this is part of the
  90. * nl802154 interface and should be accessible from ieee802154 layer.
  91. */
  92. int (*get_llsec_params)(struct wpan_phy *wpan_phy,
  93. struct wpan_dev *wpan_dev,
  94. struct ieee802154_llsec_params *params);
  95. int (*set_llsec_params)(struct wpan_phy *wpan_phy,
  96. struct wpan_dev *wpan_dev,
  97. const struct ieee802154_llsec_params *params,
  98. int changed);
  99. int (*add_llsec_key)(struct wpan_phy *wpan_phy,
  100. struct wpan_dev *wpan_dev,
  101. const struct ieee802154_llsec_key_id *id,
  102. const struct ieee802154_llsec_key *key);
  103. int (*del_llsec_key)(struct wpan_phy *wpan_phy,
  104. struct wpan_dev *wpan_dev,
  105. const struct ieee802154_llsec_key_id *id);
  106. int (*add_seclevel)(struct wpan_phy *wpan_phy,
  107. struct wpan_dev *wpan_dev,
  108. const struct ieee802154_llsec_seclevel *sl);
  109. int (*del_seclevel)(struct wpan_phy *wpan_phy,
  110. struct wpan_dev *wpan_dev,
  111. const struct ieee802154_llsec_seclevel *sl);
  112. int (*add_device)(struct wpan_phy *wpan_phy,
  113. struct wpan_dev *wpan_dev,
  114. const struct ieee802154_llsec_device *dev);
  115. int (*del_device)(struct wpan_phy *wpan_phy,
  116. struct wpan_dev *wpan_dev, __le64 extended_addr);
  117. int (*add_devkey)(struct wpan_phy *wpan_phy,
  118. struct wpan_dev *wpan_dev,
  119. __le64 extended_addr,
  120. const struct ieee802154_llsec_device_key *key);
  121. int (*del_devkey)(struct wpan_phy *wpan_phy,
  122. struct wpan_dev *wpan_dev,
  123. __le64 extended_addr,
  124. const struct ieee802154_llsec_device_key *key);
  125. #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
  126. };
  127. static inline bool
  128. wpan_phy_supported_bool(bool b, enum nl802154_supported_bool_states st)
  129. {
  130. switch (st) {
  131. case NL802154_SUPPORTED_BOOL_TRUE:
  132. return b;
  133. case NL802154_SUPPORTED_BOOL_FALSE:
  134. return !b;
  135. case NL802154_SUPPORTED_BOOL_BOTH:
  136. return true;
  137. default:
  138. WARN_ON(1);
  139. }
  140. return false;
  141. }
  142. struct wpan_phy_supported {
  143. u32 channels[IEEE802154_MAX_PAGE + 1],
  144. cca_modes, cca_opts, iftypes;
  145. enum nl802154_supported_bool_states lbt;
  146. u8 min_minbe, max_minbe, min_maxbe, max_maxbe,
  147. min_csma_backoffs, max_csma_backoffs;
  148. s8 min_frame_retries, max_frame_retries;
  149. size_t tx_powers_size, cca_ed_levels_size;
  150. const s32 *tx_powers, *cca_ed_levels;
  151. };
  152. struct wpan_phy_cca {
  153. enum nl802154_cca_modes mode;
  154. enum nl802154_cca_opts opt;
  155. };
  156. static inline bool
  157. wpan_phy_cca_cmp(const struct wpan_phy_cca *a, const struct wpan_phy_cca *b)
  158. {
  159. if (a->mode != b->mode)
  160. return false;
  161. if (a->mode == NL802154_CCA_ENERGY_CARRIER)
  162. return a->opt == b->opt;
  163. return true;
  164. }
  165. /**
  166. * enum wpan_phy_flags - WPAN PHY state flags
  167. * @WPAN_PHY_FLAG_TXPOWER: Indicates that transceiver will support
  168. * transmit power setting.
  169. * @WPAN_PHY_FLAG_CCA_ED_LEVEL: Indicates that transceiver will support cca ed
  170. * level setting.
  171. * @WPAN_PHY_FLAG_CCA_MODE: Indicates that transceiver will support cca mode
  172. * setting.
  173. * @WPAN_PHY_FLAG_STATE_QUEUE_STOPPED: Indicates that the transmit queue was
  174. * temporarily stopped.
  175. * @WPAN_PHY_FLAG_DATAGRAMS_ONLY: Indicates that transceiver is only able to
  176. * send/receive datagrams.
  177. */
  178. enum wpan_phy_flags {
  179. WPAN_PHY_FLAG_TXPOWER = BIT(1),
  180. WPAN_PHY_FLAG_CCA_ED_LEVEL = BIT(2),
  181. WPAN_PHY_FLAG_CCA_MODE = BIT(3),
  182. WPAN_PHY_FLAG_STATE_QUEUE_STOPPED = BIT(4),
  183. WPAN_PHY_FLAG_DATAGRAMS_ONLY = BIT(5),
  184. };
  185. struct wpan_phy {
  186. /* If multiple wpan_phys are registered and you're handed e.g.
  187. * a regular netdev with assigned ieee802154_ptr, you won't
  188. * know whether it points to a wpan_phy your driver has registered
  189. * or not. Assign this to something global to your driver to
  190. * help determine whether you own this wpan_phy or not.
  191. */
  192. const void *privid;
  193. unsigned long flags;
  194. /*
  195. * This is a PIB according to 802.15.4-2011.
  196. * We do not provide timing-related variables, as they
  197. * aren't used outside of driver
  198. */
  199. u8 current_channel;
  200. u8 current_page;
  201. struct wpan_phy_supported supported;
  202. /* current transmit_power in mBm */
  203. s32 transmit_power;
  204. struct wpan_phy_cca cca;
  205. __le64 perm_extended_addr;
  206. /* current cca ed threshold in mBm */
  207. s32 cca_ed_level;
  208. /* PHY depended MAC PIB values */
  209. /* 802.15.4 acronym: Tdsym in nsec */
  210. u32 symbol_duration;
  211. /* lifs and sifs periods timing */
  212. u16 lifs_period;
  213. u16 sifs_period;
  214. struct device dev;
  215. /* the network namespace this phy lives in currently */
  216. possible_net_t _net;
  217. /* Transmission monitoring and control */
  218. spinlock_t queue_lock;
  219. atomic_t ongoing_txs;
  220. atomic_t hold_txs;
  221. wait_queue_head_t sync_txq;
  222. /* Current filtering level on reception.
  223. * Only allowed to be changed if phy is not operational.
  224. */
  225. enum ieee802154_filtering_level filtering;
  226. char priv[] __aligned(NETDEV_ALIGN);
  227. };
  228. static inline struct net *wpan_phy_net(struct wpan_phy *wpan_phy)
  229. {
  230. return read_pnet(&wpan_phy->_net);
  231. }
  232. static inline void wpan_phy_net_set(struct wpan_phy *wpan_phy, struct net *net)
  233. {
  234. write_pnet(&wpan_phy->_net, net);
  235. }
  236. static inline bool ieee802154_chan_is_valid(struct wpan_phy *phy,
  237. u8 page, u8 channel)
  238. {
  239. if (page > IEEE802154_MAX_PAGE ||
  240. channel > IEEE802154_MAX_CHANNEL ||
  241. !(phy->supported.channels[page] & BIT(channel)))
  242. return false;
  243. return true;
  244. }
  245. /**
  246. * struct ieee802154_addr - IEEE802.15.4 device address
  247. * @mode: Address mode from frame header. Can be one of:
  248. * - @IEEE802154_ADDR_NONE
  249. * - @IEEE802154_ADDR_SHORT
  250. * - @IEEE802154_ADDR_LONG
  251. * @pan_id: The PAN ID this address belongs to
  252. * @short_addr: address if @mode is @IEEE802154_ADDR_SHORT
  253. * @extended_addr: address if @mode is @IEEE802154_ADDR_LONG
  254. */
  255. struct ieee802154_addr {
  256. u8 mode;
  257. __le16 pan_id;
  258. union {
  259. __le16 short_addr;
  260. __le64 extended_addr;
  261. };
  262. };
  263. /**
  264. * struct ieee802154_coord_desc - Coordinator descriptor
  265. * @addr: PAN ID and coordinator address
  266. * @page: page this coordinator is using
  267. * @channel: channel this coordinator is using
  268. * @superframe_spec: SuperFrame specification as received
  269. * @link_quality: link quality indicator at which the beacon was received
  270. * @gts_permit: the coordinator accepts GTS requests
  271. */
  272. struct ieee802154_coord_desc {
  273. struct ieee802154_addr addr;
  274. u8 page;
  275. u8 channel;
  276. u16 superframe_spec;
  277. u8 link_quality;
  278. bool gts_permit;
  279. };
  280. /**
  281. * struct ieee802154_pan_device - PAN device information
  282. * @pan_id: the PAN ID of this device
  283. * @mode: the preferred mode to reach the device
  284. * @short_addr: the short address of this device
  285. * @extended_addr: the extended address of this device
  286. * @node: the list node
  287. */
  288. struct ieee802154_pan_device {
  289. __le16 pan_id;
  290. u8 mode;
  291. __le16 short_addr;
  292. __le64 extended_addr;
  293. struct list_head node;
  294. };
  295. /**
  296. * struct cfg802154_scan_request - Scan request
  297. *
  298. * @type: type of scan to be performed
  299. * @page: page on which to perform the scan
  300. * @channels: channels in te %page to be scanned
  301. * @duration: time spent on each channel, calculated with:
  302. * aBaseSuperframeDuration * (2 ^ duration + 1)
  303. * @wpan_dev: the wpan device on which to perform the scan
  304. * @wpan_phy: the wpan phy on which to perform the scan
  305. */
  306. struct cfg802154_scan_request {
  307. enum nl802154_scan_types type;
  308. u8 page;
  309. u32 channels;
  310. u8 duration;
  311. struct wpan_dev *wpan_dev;
  312. struct wpan_phy *wpan_phy;
  313. };
  314. /**
  315. * struct cfg802154_beacon_request - Beacon request descriptor
  316. *
  317. * @interval: interval n between sendings, in multiple order of the super frame
  318. * duration: aBaseSuperframeDuration * (2^n) unless the interval
  319. * order is greater or equal to 15, in this case beacons won't be
  320. * passively sent out at a fixed rate but instead inform the device
  321. * that it should answer beacon requests as part of active scan
  322. * procedures
  323. * @wpan_dev: the concerned wpan device
  324. * @wpan_phy: the wpan phy this was for
  325. */
  326. struct cfg802154_beacon_request {
  327. u8 interval;
  328. struct wpan_dev *wpan_dev;
  329. struct wpan_phy *wpan_phy;
  330. };
  331. /**
  332. * struct cfg802154_mac_pkt - MAC packet descriptor (beacon/command)
  333. * @node: MAC packets to process list member
  334. * @skb: the received sk_buff
  335. * @sdata: the interface on which @skb was received
  336. * @page: page configuration when @skb was received
  337. * @channel: channel configuration when @skb was received
  338. */
  339. struct cfg802154_mac_pkt {
  340. struct list_head node;
  341. struct sk_buff *skb;
  342. struct ieee802154_sub_if_data *sdata;
  343. u8 page;
  344. u8 channel;
  345. };
  346. struct ieee802154_llsec_key_id {
  347. u8 mode;
  348. u8 id;
  349. union {
  350. struct ieee802154_addr device_addr;
  351. __le32 short_source;
  352. __le64 extended_source;
  353. };
  354. };
  355. #define IEEE802154_LLSEC_KEY_SIZE 16
  356. struct ieee802154_llsec_key {
  357. u8 frame_types;
  358. u32 cmd_frame_ids;
  359. /* TODO replace with NL802154_KEY_SIZE */
  360. u8 key[IEEE802154_LLSEC_KEY_SIZE];
  361. };
  362. struct ieee802154_llsec_key_entry {
  363. struct list_head list;
  364. struct rcu_head rcu;
  365. struct ieee802154_llsec_key_id id;
  366. struct ieee802154_llsec_key *key;
  367. };
  368. struct ieee802154_llsec_params {
  369. bool enabled;
  370. __be32 frame_counter;
  371. u8 out_level;
  372. struct ieee802154_llsec_key_id out_key;
  373. __le64 default_key_source;
  374. __le16 pan_id;
  375. __le64 hwaddr;
  376. __le64 coord_hwaddr;
  377. __le16 coord_shortaddr;
  378. };
  379. struct ieee802154_llsec_table {
  380. struct list_head keys;
  381. struct list_head devices;
  382. struct list_head security_levels;
  383. };
  384. struct ieee802154_llsec_seclevel {
  385. struct list_head list;
  386. u8 frame_type;
  387. u8 cmd_frame_id;
  388. bool device_override;
  389. u32 sec_levels;
  390. };
  391. struct ieee802154_llsec_device {
  392. struct list_head list;
  393. __le16 pan_id;
  394. __le16 short_addr;
  395. __le64 hwaddr;
  396. u32 frame_counter;
  397. bool seclevel_exempt;
  398. u8 key_mode;
  399. struct list_head keys;
  400. };
  401. struct ieee802154_llsec_device_key {
  402. struct list_head list;
  403. struct ieee802154_llsec_key_id key_id;
  404. u32 frame_counter;
  405. };
  406. struct wpan_dev_header_ops {
  407. /* TODO create callback currently assumes ieee802154_mac_cb inside
  408. * skb->cb. This should be changed to give these information as
  409. * parameter.
  410. */
  411. int (*create)(struct sk_buff *skb, struct net_device *dev,
  412. const struct ieee802154_addr *daddr,
  413. const struct ieee802154_addr *saddr,
  414. unsigned int len);
  415. };
  416. struct wpan_dev {
  417. struct wpan_phy *wpan_phy;
  418. int iftype;
  419. /* the remainder of this struct should be private to cfg802154 */
  420. struct list_head list;
  421. struct net_device *netdev;
  422. const struct wpan_dev_header_ops *header_ops;
  423. /* lowpan interface, set when the wpan_dev belongs to one lowpan_dev */
  424. struct net_device *lowpan_dev;
  425. u32 identifier;
  426. /* MAC PIB */
  427. __le16 pan_id;
  428. __le16 short_addr;
  429. __le64 extended_addr;
  430. /* MAC BSN field */
  431. atomic_t bsn;
  432. /* MAC DSN field */
  433. atomic_t dsn;
  434. u8 min_be;
  435. u8 max_be;
  436. u8 csma_retries;
  437. s8 frame_retries;
  438. bool lbt;
  439. /* fallback for acknowledgment bit setting */
  440. bool ackreq;
  441. /* Associations */
  442. struct mutex association_lock;
  443. struct ieee802154_pan_device *parent;
  444. struct list_head children;
  445. unsigned int max_associations;
  446. unsigned int nchildren;
  447. };
  448. #define to_phy(_dev) container_of(_dev, struct wpan_phy, dev)
  449. #if IS_ENABLED(CONFIG_IEEE802154) || IS_ENABLED(CONFIG_6LOWPAN)
  450. static inline int
  451. wpan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
  452. const struct ieee802154_addr *daddr,
  453. const struct ieee802154_addr *saddr,
  454. unsigned int len)
  455. {
  456. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  457. return wpan_dev->header_ops->create(skb, dev, daddr, saddr, len);
  458. }
  459. #endif
  460. struct wpan_phy *
  461. wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size);
  462. static inline void wpan_phy_set_dev(struct wpan_phy *phy, struct device *dev)
  463. {
  464. phy->dev.parent = dev;
  465. }
  466. int wpan_phy_register(struct wpan_phy *phy);
  467. void wpan_phy_unregister(struct wpan_phy *phy);
  468. void wpan_phy_free(struct wpan_phy *phy);
  469. /* Same semantics as for class_for_each_device */
  470. int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data), void *data);
  471. static inline void *wpan_phy_priv(struct wpan_phy *phy)
  472. {
  473. BUG_ON(!phy);
  474. return &phy->priv;
  475. }
  476. struct wpan_phy *wpan_phy_find(const char *str);
  477. static inline void wpan_phy_put(struct wpan_phy *phy)
  478. {
  479. put_device(&phy->dev);
  480. }
  481. static inline const char *wpan_phy_name(struct wpan_phy *phy)
  482. {
  483. return dev_name(&phy->dev);
  484. }
  485. void ieee802154_configure_durations(struct wpan_phy *phy,
  486. unsigned int page, unsigned int channel);
  487. /**
  488. * cfg802154_device_is_associated - Checks whether we are associated to any device
  489. * @wpan_dev: the wpan device
  490. * @return: true if we are associated
  491. */
  492. bool cfg802154_device_is_associated(struct wpan_dev *wpan_dev);
  493. /**
  494. * cfg802154_device_is_parent - Checks if a device is our coordinator
  495. * @wpan_dev: the wpan device
  496. * @target: the expected parent
  497. * @return: true if @target is our coordinator
  498. */
  499. bool cfg802154_device_is_parent(struct wpan_dev *wpan_dev,
  500. struct ieee802154_addr *target);
  501. /**
  502. * cfg802154_device_is_child - Checks whether a device is associated to us
  503. * @wpan_dev: the wpan device
  504. * @target: the expected child
  505. * @return: the PAN device
  506. */
  507. struct ieee802154_pan_device *
  508. cfg802154_device_is_child(struct wpan_dev *wpan_dev,
  509. struct ieee802154_addr *target);
  510. /**
  511. * cfg802154_set_max_associations - Limit the number of future associations
  512. * @wpan_dev: the wpan device
  513. * @max: the maximum number of devices we accept to associate
  514. * @return: the old maximum value
  515. */
  516. unsigned int cfg802154_set_max_associations(struct wpan_dev *wpan_dev,
  517. unsigned int max);
  518. /**
  519. * cfg802154_get_free_short_addr - Get a free address among the known devices
  520. * @wpan_dev: the wpan device
  521. * @return: a random short address expectedly unused on our PAN
  522. */
  523. __le16 cfg802154_get_free_short_addr(struct wpan_dev *wpan_dev);
  524. #endif /* __NET_CFG802154_H */