mac802154_hwsim.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HWSIM IEEE 802.15.4 interface
  4. *
  5. * (C) 2018 Mojatau, Alexander Aring <aring@mojatau.com>
  6. * Copyright 2007-2012 Siemens AG
  7. *
  8. * Based on fakelb, original Written by:
  9. * Sergey Lapin <slapin@ossfans.org>
  10. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  11. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  12. */
  13. #include <linux/module.h>
  14. #include <linux/timer.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/rtnetlink.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/device.h>
  19. #include <linux/spinlock.h>
  20. #include <net/ieee802154_netdev.h>
  21. #include <net/mac802154.h>
  22. #include <net/cfg802154.h>
  23. #include <net/genetlink.h>
  24. #include "mac802154_hwsim.h"
  25. MODULE_DESCRIPTION("Software simulator of IEEE 802.15.4 radio(s) for mac802154");
  26. MODULE_LICENSE("GPL");
  27. static LIST_HEAD(hwsim_phys);
  28. static DEFINE_MUTEX(hwsim_phys_lock);
  29. static struct platform_device *mac802154hwsim_dev;
  30. /* MAC802154_HWSIM netlink family */
  31. static struct genl_family hwsim_genl_family;
  32. static int hwsim_radio_idx;
  33. enum hwsim_multicast_groups {
  34. HWSIM_MCGRP_CONFIG,
  35. };
  36. static const struct genl_multicast_group hwsim_mcgrps[] = {
  37. [HWSIM_MCGRP_CONFIG] = { .name = "config", },
  38. };
  39. struct hwsim_pib {
  40. u8 page;
  41. u8 channel;
  42. struct ieee802154_hw_addr_filt filt;
  43. enum ieee802154_filtering_level filt_level;
  44. struct rcu_head rcu;
  45. };
  46. struct hwsim_edge_info {
  47. u8 lqi;
  48. struct rcu_head rcu;
  49. };
  50. struct hwsim_edge {
  51. struct hwsim_phy *endpoint;
  52. struct hwsim_edge_info __rcu *info;
  53. struct list_head list;
  54. struct rcu_head rcu;
  55. };
  56. struct hwsim_phy {
  57. struct ieee802154_hw *hw;
  58. u32 idx;
  59. struct hwsim_pib __rcu *pib;
  60. bool suspended;
  61. struct list_head edges;
  62. struct list_head list;
  63. };
  64. static int hwsim_add_one(struct genl_info *info, struct device *dev,
  65. bool init);
  66. static void hwsim_del(struct hwsim_phy *phy);
  67. static int hwsim_hw_ed(struct ieee802154_hw *hw, u8 *level)
  68. {
  69. *level = 0xbe;
  70. return 0;
  71. }
  72. static int hwsim_update_pib(struct ieee802154_hw *hw, u8 page, u8 channel,
  73. struct ieee802154_hw_addr_filt *filt,
  74. enum ieee802154_filtering_level filt_level)
  75. {
  76. struct hwsim_phy *phy = hw->priv;
  77. struct hwsim_pib *pib, *pib_old;
  78. pib = kzalloc_obj(*pib, GFP_ATOMIC);
  79. if (!pib)
  80. return -ENOMEM;
  81. pib_old = rtnl_dereference(phy->pib);
  82. pib->page = page;
  83. pib->channel = channel;
  84. pib->filt.short_addr = filt->short_addr;
  85. pib->filt.pan_id = filt->pan_id;
  86. pib->filt.ieee_addr = filt->ieee_addr;
  87. pib->filt.pan_coord = filt->pan_coord;
  88. pib->filt_level = filt_level;
  89. rcu_assign_pointer(phy->pib, pib);
  90. kfree_rcu(pib_old, rcu);
  91. return 0;
  92. }
  93. static int hwsim_hw_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
  94. {
  95. struct hwsim_phy *phy = hw->priv;
  96. struct hwsim_pib *pib;
  97. int ret;
  98. rcu_read_lock();
  99. pib = rcu_dereference(phy->pib);
  100. ret = hwsim_update_pib(hw, page, channel, &pib->filt, pib->filt_level);
  101. rcu_read_unlock();
  102. return ret;
  103. }
  104. static int hwsim_hw_addr_filt(struct ieee802154_hw *hw,
  105. struct ieee802154_hw_addr_filt *filt,
  106. unsigned long changed)
  107. {
  108. struct hwsim_phy *phy = hw->priv;
  109. struct hwsim_pib *pib;
  110. int ret;
  111. rcu_read_lock();
  112. pib = rcu_dereference(phy->pib);
  113. ret = hwsim_update_pib(hw, pib->page, pib->channel, filt, pib->filt_level);
  114. rcu_read_unlock();
  115. return ret;
  116. }
  117. static void hwsim_hw_receive(struct ieee802154_hw *hw, struct sk_buff *skb,
  118. u8 lqi)
  119. {
  120. struct ieee802154_hdr hdr;
  121. struct hwsim_phy *phy = hw->priv;
  122. struct hwsim_pib *pib;
  123. rcu_read_lock();
  124. pib = rcu_dereference(phy->pib);
  125. if (!pskb_may_pull(skb, 3)) {
  126. dev_dbg(hw->parent, "invalid frame\n");
  127. goto drop;
  128. }
  129. memcpy(&hdr, skb->data, 3);
  130. /* Level 4 filtering: Frame fields validity */
  131. if (pib->filt_level == IEEE802154_FILTERING_4_FRAME_FIELDS) {
  132. /* a) Drop reserved frame types */
  133. switch (mac_cb(skb)->type) {
  134. case IEEE802154_FC_TYPE_BEACON:
  135. case IEEE802154_FC_TYPE_DATA:
  136. case IEEE802154_FC_TYPE_ACK:
  137. case IEEE802154_FC_TYPE_MAC_CMD:
  138. break;
  139. default:
  140. dev_dbg(hw->parent, "unrecognized frame type 0x%x\n",
  141. mac_cb(skb)->type);
  142. goto drop;
  143. }
  144. /* b) Drop reserved frame versions */
  145. switch (hdr.fc.version) {
  146. case IEEE802154_2003_STD:
  147. case IEEE802154_2006_STD:
  148. case IEEE802154_STD:
  149. break;
  150. default:
  151. dev_dbg(hw->parent,
  152. "unrecognized frame version 0x%x\n",
  153. hdr.fc.version);
  154. goto drop;
  155. }
  156. /* c) PAN ID constraints */
  157. if ((mac_cb(skb)->dest.mode == IEEE802154_ADDR_LONG ||
  158. mac_cb(skb)->dest.mode == IEEE802154_ADDR_SHORT) &&
  159. mac_cb(skb)->dest.pan_id != pib->filt.pan_id &&
  160. mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
  161. dev_dbg(hw->parent,
  162. "unrecognized PAN ID %04x\n",
  163. le16_to_cpu(mac_cb(skb)->dest.pan_id));
  164. goto drop;
  165. }
  166. /* d1) Short address constraints */
  167. if (mac_cb(skb)->dest.mode == IEEE802154_ADDR_SHORT &&
  168. mac_cb(skb)->dest.short_addr != pib->filt.short_addr &&
  169. mac_cb(skb)->dest.short_addr != cpu_to_le16(IEEE802154_ADDR_BROADCAST)) {
  170. dev_dbg(hw->parent,
  171. "unrecognized short address %04x\n",
  172. le16_to_cpu(mac_cb(skb)->dest.short_addr));
  173. goto drop;
  174. }
  175. /* d2) Extended address constraints */
  176. if (mac_cb(skb)->dest.mode == IEEE802154_ADDR_LONG &&
  177. mac_cb(skb)->dest.extended_addr != pib->filt.ieee_addr) {
  178. dev_dbg(hw->parent,
  179. "unrecognized long address 0x%016llx\n",
  180. mac_cb(skb)->dest.extended_addr);
  181. goto drop;
  182. }
  183. /* d4) Specific PAN coordinator case (no parent) */
  184. if ((mac_cb(skb)->type == IEEE802154_FC_TYPE_DATA ||
  185. mac_cb(skb)->type == IEEE802154_FC_TYPE_MAC_CMD) &&
  186. mac_cb(skb)->dest.mode == IEEE802154_ADDR_NONE) {
  187. dev_dbg(hw->parent,
  188. "relaying is not supported\n");
  189. goto drop;
  190. }
  191. /* e) Beacon frames follow specific PAN ID rules */
  192. if (mac_cb(skb)->type == IEEE802154_FC_TYPE_BEACON &&
  193. pib->filt.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST) &&
  194. mac_cb(skb)->dest.pan_id != pib->filt.pan_id) {
  195. dev_dbg(hw->parent,
  196. "invalid beacon PAN ID %04x\n",
  197. le16_to_cpu(mac_cb(skb)->dest.pan_id));
  198. goto drop;
  199. }
  200. }
  201. rcu_read_unlock();
  202. ieee802154_rx_irqsafe(hw, skb, lqi);
  203. return;
  204. drop:
  205. rcu_read_unlock();
  206. kfree_skb(skb);
  207. }
  208. static int hwsim_hw_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
  209. {
  210. struct hwsim_phy *current_phy = hw->priv;
  211. struct hwsim_pib *current_pib, *endpoint_pib;
  212. struct hwsim_edge_info *einfo;
  213. struct hwsim_edge *e;
  214. WARN_ON(current_phy->suspended);
  215. rcu_read_lock();
  216. current_pib = rcu_dereference(current_phy->pib);
  217. list_for_each_entry_rcu(e, &current_phy->edges, list) {
  218. /* Can be changed later in rx_irqsafe, but this is only a
  219. * performance tweak. Received radio should drop the frame
  220. * in mac802154 stack anyway... so we don't need to be
  221. * 100% of locking here to check on suspended
  222. */
  223. if (e->endpoint->suspended)
  224. continue;
  225. endpoint_pib = rcu_dereference(e->endpoint->pib);
  226. if (current_pib->page == endpoint_pib->page &&
  227. current_pib->channel == endpoint_pib->channel) {
  228. struct sk_buff *newskb = pskb_copy(skb, GFP_ATOMIC);
  229. einfo = rcu_dereference(e->info);
  230. if (newskb)
  231. hwsim_hw_receive(e->endpoint->hw, newskb, einfo->lqi);
  232. }
  233. }
  234. rcu_read_unlock();
  235. ieee802154_xmit_complete(hw, skb, false);
  236. return 0;
  237. }
  238. static int hwsim_hw_start(struct ieee802154_hw *hw)
  239. {
  240. struct hwsim_phy *phy = hw->priv;
  241. phy->suspended = false;
  242. return 0;
  243. }
  244. static void hwsim_hw_stop(struct ieee802154_hw *hw)
  245. {
  246. struct hwsim_phy *phy = hw->priv;
  247. phy->suspended = true;
  248. }
  249. static int
  250. hwsim_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
  251. {
  252. enum ieee802154_filtering_level filt_level;
  253. struct hwsim_phy *phy = hw->priv;
  254. struct hwsim_pib *pib;
  255. int ret;
  256. if (on)
  257. filt_level = IEEE802154_FILTERING_NONE;
  258. else
  259. filt_level = IEEE802154_FILTERING_4_FRAME_FIELDS;
  260. rcu_read_lock();
  261. pib = rcu_dereference(phy->pib);
  262. ret = hwsim_update_pib(hw, pib->page, pib->channel, &pib->filt, filt_level);
  263. rcu_read_unlock();
  264. return ret;
  265. }
  266. static const struct ieee802154_ops hwsim_ops = {
  267. .owner = THIS_MODULE,
  268. .xmit_async = hwsim_hw_xmit,
  269. .ed = hwsim_hw_ed,
  270. .set_channel = hwsim_hw_channel,
  271. .start = hwsim_hw_start,
  272. .stop = hwsim_hw_stop,
  273. .set_promiscuous_mode = hwsim_set_promiscuous_mode,
  274. .set_hw_addr_filt = hwsim_hw_addr_filt,
  275. };
  276. static int hwsim_new_radio_nl(struct sk_buff *msg, struct genl_info *info)
  277. {
  278. return hwsim_add_one(info, &mac802154hwsim_dev->dev, false);
  279. }
  280. static int hwsim_del_radio_nl(struct sk_buff *msg, struct genl_info *info)
  281. {
  282. struct hwsim_phy *phy, *tmp;
  283. s64 idx = -1;
  284. if (!info->attrs[MAC802154_HWSIM_ATTR_RADIO_ID])
  285. return -EINVAL;
  286. idx = nla_get_u32(info->attrs[MAC802154_HWSIM_ATTR_RADIO_ID]);
  287. mutex_lock(&hwsim_phys_lock);
  288. list_for_each_entry_safe(phy, tmp, &hwsim_phys, list) {
  289. if (idx == phy->idx) {
  290. hwsim_del(phy);
  291. mutex_unlock(&hwsim_phys_lock);
  292. return 0;
  293. }
  294. }
  295. mutex_unlock(&hwsim_phys_lock);
  296. return -ENODEV;
  297. }
  298. static int append_radio_msg(struct sk_buff *skb, struct hwsim_phy *phy)
  299. {
  300. struct nlattr *nl_edges, *nl_edge;
  301. struct hwsim_edge_info *einfo;
  302. struct hwsim_edge *e;
  303. int ret;
  304. ret = nla_put_u32(skb, MAC802154_HWSIM_ATTR_RADIO_ID, phy->idx);
  305. if (ret < 0)
  306. return ret;
  307. rcu_read_lock();
  308. if (list_empty(&phy->edges)) {
  309. rcu_read_unlock();
  310. return 0;
  311. }
  312. nl_edges = nla_nest_start_noflag(skb,
  313. MAC802154_HWSIM_ATTR_RADIO_EDGES);
  314. if (!nl_edges) {
  315. rcu_read_unlock();
  316. return -ENOBUFS;
  317. }
  318. list_for_each_entry_rcu(e, &phy->edges, list) {
  319. nl_edge = nla_nest_start_noflag(skb,
  320. MAC802154_HWSIM_ATTR_RADIO_EDGE);
  321. if (!nl_edge) {
  322. rcu_read_unlock();
  323. nla_nest_cancel(skb, nl_edges);
  324. return -ENOBUFS;
  325. }
  326. ret = nla_put_u32(skb, MAC802154_HWSIM_EDGE_ATTR_ENDPOINT_ID,
  327. e->endpoint->idx);
  328. if (ret < 0) {
  329. rcu_read_unlock();
  330. nla_nest_cancel(skb, nl_edge);
  331. nla_nest_cancel(skb, nl_edges);
  332. return ret;
  333. }
  334. einfo = rcu_dereference(e->info);
  335. ret = nla_put_u8(skb, MAC802154_HWSIM_EDGE_ATTR_LQI,
  336. einfo->lqi);
  337. if (ret < 0) {
  338. rcu_read_unlock();
  339. nla_nest_cancel(skb, nl_edge);
  340. nla_nest_cancel(skb, nl_edges);
  341. return ret;
  342. }
  343. nla_nest_end(skb, nl_edge);
  344. }
  345. rcu_read_unlock();
  346. nla_nest_end(skb, nl_edges);
  347. return 0;
  348. }
  349. static int hwsim_get_radio(struct sk_buff *skb, struct hwsim_phy *phy,
  350. u32 portid, u32 seq,
  351. struct netlink_callback *cb, int flags)
  352. {
  353. void *hdr;
  354. int res;
  355. hdr = genlmsg_put(skb, portid, seq, &hwsim_genl_family, flags,
  356. MAC802154_HWSIM_CMD_GET_RADIO);
  357. if (!hdr)
  358. return -EMSGSIZE;
  359. if (cb)
  360. genl_dump_check_consistent(cb, hdr);
  361. res = append_radio_msg(skb, phy);
  362. if (res < 0)
  363. goto out_err;
  364. genlmsg_end(skb, hdr);
  365. return 0;
  366. out_err:
  367. genlmsg_cancel(skb, hdr);
  368. return res;
  369. }
  370. static int hwsim_get_radio_nl(struct sk_buff *msg, struct genl_info *info)
  371. {
  372. struct hwsim_phy *phy;
  373. struct sk_buff *skb;
  374. int idx, res = -ENODEV;
  375. if (!info->attrs[MAC802154_HWSIM_ATTR_RADIO_ID])
  376. return -EINVAL;
  377. idx = nla_get_u32(info->attrs[MAC802154_HWSIM_ATTR_RADIO_ID]);
  378. mutex_lock(&hwsim_phys_lock);
  379. list_for_each_entry(phy, &hwsim_phys, list) {
  380. if (phy->idx != idx)
  381. continue;
  382. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  383. if (!skb) {
  384. res = -ENOMEM;
  385. goto out_err;
  386. }
  387. res = hwsim_get_radio(skb, phy, info->snd_portid,
  388. info->snd_seq, NULL, 0);
  389. if (res < 0) {
  390. nlmsg_free(skb);
  391. goto out_err;
  392. }
  393. res = genlmsg_reply(skb, info);
  394. break;
  395. }
  396. out_err:
  397. mutex_unlock(&hwsim_phys_lock);
  398. return res;
  399. }
  400. static int hwsim_dump_radio_nl(struct sk_buff *skb,
  401. struct netlink_callback *cb)
  402. {
  403. int idx = cb->args[0];
  404. struct hwsim_phy *phy;
  405. int res;
  406. mutex_lock(&hwsim_phys_lock);
  407. if (idx == hwsim_radio_idx)
  408. goto done;
  409. list_for_each_entry(phy, &hwsim_phys, list) {
  410. if (phy->idx < idx)
  411. continue;
  412. res = hwsim_get_radio(skb, phy, NETLINK_CB(cb->skb).portid,
  413. cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
  414. if (res < 0)
  415. break;
  416. idx = phy->idx + 1;
  417. }
  418. cb->args[0] = idx;
  419. done:
  420. mutex_unlock(&hwsim_phys_lock);
  421. return skb->len;
  422. }
  423. /* caller need to held hwsim_phys_lock */
  424. static struct hwsim_phy *hwsim_get_radio_by_id(uint32_t idx)
  425. {
  426. struct hwsim_phy *phy;
  427. list_for_each_entry(phy, &hwsim_phys, list) {
  428. if (phy->idx == idx)
  429. return phy;
  430. }
  431. return NULL;
  432. }
  433. static const struct nla_policy hwsim_edge_policy[MAC802154_HWSIM_EDGE_ATTR_MAX + 1] = {
  434. [MAC802154_HWSIM_EDGE_ATTR_ENDPOINT_ID] = { .type = NLA_U32 },
  435. [MAC802154_HWSIM_EDGE_ATTR_LQI] = { .type = NLA_U8 },
  436. };
  437. static struct hwsim_edge *hwsim_alloc_edge(struct hwsim_phy *endpoint, u8 lqi)
  438. {
  439. struct hwsim_edge_info *einfo;
  440. struct hwsim_edge *e;
  441. e = kzalloc_obj(*e);
  442. if (!e)
  443. return NULL;
  444. einfo = kzalloc_obj(*einfo);
  445. if (!einfo) {
  446. kfree(e);
  447. return NULL;
  448. }
  449. einfo->lqi = 0xff;
  450. rcu_assign_pointer(e->info, einfo);
  451. e->endpoint = endpoint;
  452. return e;
  453. }
  454. static void hwsim_free_edge(struct hwsim_edge *e)
  455. {
  456. struct hwsim_edge_info *einfo;
  457. rcu_read_lock();
  458. einfo = rcu_dereference(e->info);
  459. rcu_read_unlock();
  460. kfree_rcu(einfo, rcu);
  461. kfree_rcu(e, rcu);
  462. }
  463. static int hwsim_new_edge_nl(struct sk_buff *msg, struct genl_info *info)
  464. {
  465. struct nlattr *edge_attrs[MAC802154_HWSIM_EDGE_ATTR_MAX + 1];
  466. struct hwsim_phy *phy_v0, *phy_v1;
  467. struct hwsim_edge *e;
  468. u32 v0, v1;
  469. if (!info->attrs[MAC802154_HWSIM_ATTR_RADIO_ID] ||
  470. !info->attrs[MAC802154_HWSIM_ATTR_RADIO_EDGE])
  471. return -EINVAL;
  472. if (nla_parse_nested_deprecated(edge_attrs, MAC802154_HWSIM_EDGE_ATTR_MAX, info->attrs[MAC802154_HWSIM_ATTR_RADIO_EDGE], hwsim_edge_policy, NULL))
  473. return -EINVAL;
  474. if (!edge_attrs[MAC802154_HWSIM_EDGE_ATTR_ENDPOINT_ID])
  475. return -EINVAL;
  476. v0 = nla_get_u32(info->attrs[MAC802154_HWSIM_ATTR_RADIO_ID]);
  477. v1 = nla_get_u32(edge_attrs[MAC802154_HWSIM_EDGE_ATTR_ENDPOINT_ID]);
  478. if (v0 == v1)
  479. return -EINVAL;
  480. mutex_lock(&hwsim_phys_lock);
  481. phy_v0 = hwsim_get_radio_by_id(v0);
  482. if (!phy_v0) {
  483. mutex_unlock(&hwsim_phys_lock);
  484. return -ENOENT;
  485. }
  486. phy_v1 = hwsim_get_radio_by_id(v1);
  487. if (!phy_v1) {
  488. mutex_unlock(&hwsim_phys_lock);
  489. return -ENOENT;
  490. }
  491. rcu_read_lock();
  492. list_for_each_entry_rcu(e, &phy_v0->edges, list) {
  493. if (e->endpoint->idx == v1) {
  494. mutex_unlock(&hwsim_phys_lock);
  495. rcu_read_unlock();
  496. return -EEXIST;
  497. }
  498. }
  499. rcu_read_unlock();
  500. e = hwsim_alloc_edge(phy_v1, 0xff);
  501. if (!e) {
  502. mutex_unlock(&hwsim_phys_lock);
  503. return -ENOMEM;
  504. }
  505. list_add_rcu(&e->list, &phy_v0->edges);
  506. /* wait until changes are done under hwsim_phys_lock lock
  507. * should prevent of calling this function twice while
  508. * edges list has not the changes yet.
  509. */
  510. synchronize_rcu();
  511. mutex_unlock(&hwsim_phys_lock);
  512. return 0;
  513. }
  514. static int hwsim_del_edge_nl(struct sk_buff *msg, struct genl_info *info)
  515. {
  516. struct nlattr *edge_attrs[MAC802154_HWSIM_EDGE_ATTR_MAX + 1];
  517. struct hwsim_phy *phy_v0;
  518. struct hwsim_edge *e;
  519. u32 v0, v1;
  520. if (!info->attrs[MAC802154_HWSIM_ATTR_RADIO_ID] ||
  521. !info->attrs[MAC802154_HWSIM_ATTR_RADIO_EDGE])
  522. return -EINVAL;
  523. if (nla_parse_nested_deprecated(edge_attrs, MAC802154_HWSIM_EDGE_ATTR_MAX, info->attrs[MAC802154_HWSIM_ATTR_RADIO_EDGE], hwsim_edge_policy, NULL))
  524. return -EINVAL;
  525. if (!edge_attrs[MAC802154_HWSIM_EDGE_ATTR_ENDPOINT_ID])
  526. return -EINVAL;
  527. v0 = nla_get_u32(info->attrs[MAC802154_HWSIM_ATTR_RADIO_ID]);
  528. v1 = nla_get_u32(edge_attrs[MAC802154_HWSIM_EDGE_ATTR_ENDPOINT_ID]);
  529. mutex_lock(&hwsim_phys_lock);
  530. phy_v0 = hwsim_get_radio_by_id(v0);
  531. if (!phy_v0) {
  532. mutex_unlock(&hwsim_phys_lock);
  533. return -ENOENT;
  534. }
  535. rcu_read_lock();
  536. list_for_each_entry_rcu(e, &phy_v0->edges, list) {
  537. if (e->endpoint->idx == v1) {
  538. rcu_read_unlock();
  539. list_del_rcu(&e->list);
  540. hwsim_free_edge(e);
  541. /* same again - wait until list changes are done */
  542. synchronize_rcu();
  543. mutex_unlock(&hwsim_phys_lock);
  544. return 0;
  545. }
  546. }
  547. rcu_read_unlock();
  548. mutex_unlock(&hwsim_phys_lock);
  549. return -ENOENT;
  550. }
  551. static int hwsim_set_edge_lqi(struct sk_buff *msg, struct genl_info *info)
  552. {
  553. struct nlattr *edge_attrs[MAC802154_HWSIM_EDGE_ATTR_MAX + 1];
  554. struct hwsim_edge_info *einfo, *einfo_old;
  555. struct hwsim_phy *phy_v0;
  556. struct hwsim_edge *e;
  557. u32 v0, v1;
  558. u8 lqi;
  559. if (!info->attrs[MAC802154_HWSIM_ATTR_RADIO_ID] ||
  560. !info->attrs[MAC802154_HWSIM_ATTR_RADIO_EDGE])
  561. return -EINVAL;
  562. if (nla_parse_nested_deprecated(edge_attrs, MAC802154_HWSIM_EDGE_ATTR_MAX, info->attrs[MAC802154_HWSIM_ATTR_RADIO_EDGE], hwsim_edge_policy, NULL))
  563. return -EINVAL;
  564. if (!edge_attrs[MAC802154_HWSIM_EDGE_ATTR_ENDPOINT_ID] ||
  565. !edge_attrs[MAC802154_HWSIM_EDGE_ATTR_LQI])
  566. return -EINVAL;
  567. v0 = nla_get_u32(info->attrs[MAC802154_HWSIM_ATTR_RADIO_ID]);
  568. v1 = nla_get_u32(edge_attrs[MAC802154_HWSIM_EDGE_ATTR_ENDPOINT_ID]);
  569. lqi = nla_get_u8(edge_attrs[MAC802154_HWSIM_EDGE_ATTR_LQI]);
  570. mutex_lock(&hwsim_phys_lock);
  571. phy_v0 = hwsim_get_radio_by_id(v0);
  572. if (!phy_v0) {
  573. mutex_unlock(&hwsim_phys_lock);
  574. return -ENOENT;
  575. }
  576. einfo = kzalloc_obj(*einfo);
  577. if (!einfo) {
  578. mutex_unlock(&hwsim_phys_lock);
  579. return -ENOMEM;
  580. }
  581. rcu_read_lock();
  582. list_for_each_entry_rcu(e, &phy_v0->edges, list) {
  583. if (e->endpoint->idx == v1) {
  584. einfo->lqi = lqi;
  585. einfo_old = rcu_replace_pointer(e->info, einfo,
  586. lockdep_is_held(&hwsim_phys_lock));
  587. rcu_read_unlock();
  588. kfree_rcu(einfo_old, rcu);
  589. mutex_unlock(&hwsim_phys_lock);
  590. return 0;
  591. }
  592. }
  593. rcu_read_unlock();
  594. kfree(einfo);
  595. mutex_unlock(&hwsim_phys_lock);
  596. return -ENOENT;
  597. }
  598. /* MAC802154_HWSIM netlink policy */
  599. static const struct nla_policy hwsim_genl_policy[MAC802154_HWSIM_ATTR_MAX + 1] = {
  600. [MAC802154_HWSIM_ATTR_RADIO_ID] = { .type = NLA_U32 },
  601. [MAC802154_HWSIM_ATTR_RADIO_EDGE] = { .type = NLA_NESTED },
  602. [MAC802154_HWSIM_ATTR_RADIO_EDGES] = { .type = NLA_NESTED },
  603. };
  604. /* Generic Netlink operations array */
  605. static const struct genl_small_ops hwsim_nl_ops[] = {
  606. {
  607. .cmd = MAC802154_HWSIM_CMD_NEW_RADIO,
  608. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  609. .doit = hwsim_new_radio_nl,
  610. .flags = GENL_UNS_ADMIN_PERM,
  611. },
  612. {
  613. .cmd = MAC802154_HWSIM_CMD_DEL_RADIO,
  614. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  615. .doit = hwsim_del_radio_nl,
  616. .flags = GENL_UNS_ADMIN_PERM,
  617. },
  618. {
  619. .cmd = MAC802154_HWSIM_CMD_GET_RADIO,
  620. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  621. .doit = hwsim_get_radio_nl,
  622. .dumpit = hwsim_dump_radio_nl,
  623. },
  624. {
  625. .cmd = MAC802154_HWSIM_CMD_NEW_EDGE,
  626. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  627. .doit = hwsim_new_edge_nl,
  628. .flags = GENL_UNS_ADMIN_PERM,
  629. },
  630. {
  631. .cmd = MAC802154_HWSIM_CMD_DEL_EDGE,
  632. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  633. .doit = hwsim_del_edge_nl,
  634. .flags = GENL_UNS_ADMIN_PERM,
  635. },
  636. {
  637. .cmd = MAC802154_HWSIM_CMD_SET_EDGE,
  638. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  639. .doit = hwsim_set_edge_lqi,
  640. .flags = GENL_UNS_ADMIN_PERM,
  641. },
  642. };
  643. static struct genl_family hwsim_genl_family __ro_after_init = {
  644. .name = "MAC802154_HWSIM",
  645. .version = 1,
  646. .maxattr = MAC802154_HWSIM_ATTR_MAX,
  647. .policy = hwsim_genl_policy,
  648. .module = THIS_MODULE,
  649. .small_ops = hwsim_nl_ops,
  650. .n_small_ops = ARRAY_SIZE(hwsim_nl_ops),
  651. .resv_start_op = MAC802154_HWSIM_CMD_NEW_EDGE + 1,
  652. .mcgrps = hwsim_mcgrps,
  653. .n_mcgrps = ARRAY_SIZE(hwsim_mcgrps),
  654. };
  655. static void hwsim_mcast_config_msg(struct sk_buff *mcast_skb,
  656. struct genl_info *info)
  657. {
  658. if (info)
  659. genl_notify(&hwsim_genl_family, mcast_skb, info,
  660. HWSIM_MCGRP_CONFIG, GFP_KERNEL);
  661. else
  662. genlmsg_multicast(&hwsim_genl_family, mcast_skb, 0,
  663. HWSIM_MCGRP_CONFIG, GFP_KERNEL);
  664. }
  665. static void hwsim_mcast_new_radio(struct genl_info *info, struct hwsim_phy *phy)
  666. {
  667. struct sk_buff *mcast_skb;
  668. void *data;
  669. mcast_skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  670. if (!mcast_skb)
  671. return;
  672. data = genlmsg_put(mcast_skb, 0, 0, &hwsim_genl_family, 0,
  673. MAC802154_HWSIM_CMD_NEW_RADIO);
  674. if (!data)
  675. goto out_err;
  676. if (append_radio_msg(mcast_skb, phy) < 0)
  677. goto out_err;
  678. genlmsg_end(mcast_skb, data);
  679. hwsim_mcast_config_msg(mcast_skb, info);
  680. return;
  681. out_err:
  682. genlmsg_cancel(mcast_skb, data);
  683. nlmsg_free(mcast_skb);
  684. }
  685. static void hwsim_edge_unsubscribe_me(struct hwsim_phy *phy)
  686. {
  687. struct hwsim_phy *tmp;
  688. struct hwsim_edge *e;
  689. rcu_read_lock();
  690. /* going to all phy edges and remove phy from it */
  691. list_for_each_entry(tmp, &hwsim_phys, list) {
  692. list_for_each_entry_rcu(e, &tmp->edges, list) {
  693. if (e->endpoint->idx == phy->idx) {
  694. list_del_rcu(&e->list);
  695. hwsim_free_edge(e);
  696. }
  697. }
  698. }
  699. rcu_read_unlock();
  700. synchronize_rcu();
  701. }
  702. static int hwsim_subscribe_all_others(struct hwsim_phy *phy)
  703. {
  704. struct hwsim_phy *sub;
  705. struct hwsim_edge *e;
  706. list_for_each_entry(sub, &hwsim_phys, list) {
  707. e = hwsim_alloc_edge(sub, 0xff);
  708. if (!e)
  709. goto me_fail;
  710. list_add_rcu(&e->list, &phy->edges);
  711. }
  712. list_for_each_entry(sub, &hwsim_phys, list) {
  713. e = hwsim_alloc_edge(phy, 0xff);
  714. if (!e)
  715. goto sub_fail;
  716. list_add_rcu(&e->list, &sub->edges);
  717. }
  718. return 0;
  719. sub_fail:
  720. hwsim_edge_unsubscribe_me(phy);
  721. me_fail:
  722. rcu_read_lock();
  723. list_for_each_entry_rcu(e, &phy->edges, list) {
  724. list_del_rcu(&e->list);
  725. hwsim_free_edge(e);
  726. }
  727. rcu_read_unlock();
  728. return -ENOMEM;
  729. }
  730. static int hwsim_add_one(struct genl_info *info, struct device *dev,
  731. bool init)
  732. {
  733. struct ieee802154_hw *hw;
  734. struct hwsim_phy *phy;
  735. struct hwsim_pib *pib;
  736. int idx;
  737. int err;
  738. idx = hwsim_radio_idx++;
  739. hw = ieee802154_alloc_hw(sizeof(*phy), &hwsim_ops);
  740. if (!hw)
  741. return -ENOMEM;
  742. phy = hw->priv;
  743. phy->hw = hw;
  744. /* 868 MHz BPSK 802.15.4-2003 */
  745. hw->phy->supported.channels[0] |= 1;
  746. /* 915 MHz BPSK 802.15.4-2003 */
  747. hw->phy->supported.channels[0] |= 0x7fe;
  748. /* 2.4 GHz O-QPSK 802.15.4-2003 */
  749. hw->phy->supported.channels[0] |= 0x7FFF800;
  750. /* 868 MHz ASK 802.15.4-2006 */
  751. hw->phy->supported.channels[1] |= 1;
  752. /* 915 MHz ASK 802.15.4-2006 */
  753. hw->phy->supported.channels[1] |= 0x7fe;
  754. /* 868 MHz O-QPSK 802.15.4-2006 */
  755. hw->phy->supported.channels[2] |= 1;
  756. /* 915 MHz O-QPSK 802.15.4-2006 */
  757. hw->phy->supported.channels[2] |= 0x7fe;
  758. /* 2.4 GHz CSS 802.15.4a-2007 */
  759. hw->phy->supported.channels[3] |= 0x3fff;
  760. /* UWB Sub-gigahertz 802.15.4a-2007 */
  761. hw->phy->supported.channels[4] |= 1;
  762. /* UWB Low band 802.15.4a-2007 */
  763. hw->phy->supported.channels[4] |= 0x1e;
  764. /* UWB High band 802.15.4a-2007 */
  765. hw->phy->supported.channels[4] |= 0xffe0;
  766. /* 750 MHz O-QPSK 802.15.4c-2009 */
  767. hw->phy->supported.channels[5] |= 0xf;
  768. /* 750 MHz MPSK 802.15.4c-2009 */
  769. hw->phy->supported.channels[5] |= 0xf0;
  770. /* 950 MHz BPSK 802.15.4d-2009 */
  771. hw->phy->supported.channels[6] |= 0x3ff;
  772. /* 950 MHz GFSK 802.15.4d-2009 */
  773. hw->phy->supported.channels[6] |= 0x3ffc00;
  774. ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
  775. /* hwsim phy channel 13 as default */
  776. hw->phy->current_channel = 13;
  777. pib = kzalloc_obj(*pib);
  778. if (!pib) {
  779. err = -ENOMEM;
  780. goto err_pib;
  781. }
  782. pib->channel = 13;
  783. pib->filt.short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
  784. pib->filt.pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
  785. rcu_assign_pointer(phy->pib, pib);
  786. phy->idx = idx;
  787. INIT_LIST_HEAD(&phy->edges);
  788. hw->flags = IEEE802154_HW_PROMISCUOUS;
  789. hw->parent = dev;
  790. err = ieee802154_register_hw(hw);
  791. if (err)
  792. goto err_reg;
  793. mutex_lock(&hwsim_phys_lock);
  794. if (init) {
  795. err = hwsim_subscribe_all_others(phy);
  796. if (err < 0) {
  797. mutex_unlock(&hwsim_phys_lock);
  798. goto err_subscribe;
  799. }
  800. }
  801. list_add_tail(&phy->list, &hwsim_phys);
  802. mutex_unlock(&hwsim_phys_lock);
  803. hwsim_mcast_new_radio(info, phy);
  804. return idx;
  805. err_subscribe:
  806. ieee802154_unregister_hw(phy->hw);
  807. err_reg:
  808. kfree(pib);
  809. err_pib:
  810. ieee802154_free_hw(phy->hw);
  811. return err;
  812. }
  813. static void hwsim_del(struct hwsim_phy *phy)
  814. {
  815. struct hwsim_pib *pib;
  816. struct hwsim_edge *e;
  817. hwsim_edge_unsubscribe_me(phy);
  818. list_del(&phy->list);
  819. rcu_read_lock();
  820. list_for_each_entry_rcu(e, &phy->edges, list) {
  821. list_del_rcu(&e->list);
  822. hwsim_free_edge(e);
  823. }
  824. pib = rcu_dereference(phy->pib);
  825. rcu_read_unlock();
  826. kfree_rcu(pib, rcu);
  827. ieee802154_unregister_hw(phy->hw);
  828. ieee802154_free_hw(phy->hw);
  829. }
  830. static int hwsim_probe(struct platform_device *pdev)
  831. {
  832. struct hwsim_phy *phy, *tmp;
  833. int err, i;
  834. for (i = 0; i < 2; i++) {
  835. err = hwsim_add_one(NULL, &pdev->dev, true);
  836. if (err < 0)
  837. goto err_slave;
  838. }
  839. dev_info(&pdev->dev, "Added 2 mac802154 hwsim hardware radios\n");
  840. return 0;
  841. err_slave:
  842. mutex_lock(&hwsim_phys_lock);
  843. list_for_each_entry_safe(phy, tmp, &hwsim_phys, list)
  844. hwsim_del(phy);
  845. mutex_unlock(&hwsim_phys_lock);
  846. return err;
  847. }
  848. static void hwsim_remove(struct platform_device *pdev)
  849. {
  850. struct hwsim_phy *phy, *tmp;
  851. mutex_lock(&hwsim_phys_lock);
  852. list_for_each_entry_safe(phy, tmp, &hwsim_phys, list)
  853. hwsim_del(phy);
  854. mutex_unlock(&hwsim_phys_lock);
  855. }
  856. static struct platform_driver mac802154hwsim_driver = {
  857. .probe = hwsim_probe,
  858. .remove = hwsim_remove,
  859. .driver = {
  860. .name = "mac802154_hwsim",
  861. },
  862. };
  863. static __init int hwsim_init_module(void)
  864. {
  865. int rc;
  866. rc = genl_register_family(&hwsim_genl_family);
  867. if (rc)
  868. return rc;
  869. mac802154hwsim_dev = platform_device_register_simple("mac802154_hwsim",
  870. -1, NULL, 0);
  871. if (IS_ERR(mac802154hwsim_dev)) {
  872. rc = PTR_ERR(mac802154hwsim_dev);
  873. goto platform_dev;
  874. }
  875. rc = platform_driver_register(&mac802154hwsim_driver);
  876. if (rc < 0)
  877. goto platform_drv;
  878. return 0;
  879. platform_drv:
  880. platform_device_unregister(mac802154hwsim_dev);
  881. platform_dev:
  882. genl_unregister_family(&hwsim_genl_family);
  883. return rc;
  884. }
  885. static __exit void hwsim_remove_module(void)
  886. {
  887. genl_unregister_family(&hwsim_genl_family);
  888. platform_driver_unregister(&mac802154hwsim_driver);
  889. platform_device_unregister(mac802154hwsim_dev);
  890. }
  891. module_init(hwsim_init_module);
  892. module_exit(hwsim_remove_module);