wpan.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Copyright 2007-2012 Siemens AG
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Written by:
  18. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  19. * Sergey Lapin <slapin@ossfans.org>
  20. * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
  21. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  22. */
  23. #include <linux/netdevice.h>
  24. #include <linux/module.h>
  25. #include <linux/if_arp.h>
  26. #include <net/rtnetlink.h>
  27. #include <linux/nl802154.h>
  28. #include <net/af_ieee802154.h>
  29. #include <net/mac802154.h>
  30. #include <net/ieee802154_netdev.h>
  31. #include <net/ieee802154.h>
  32. #include <net/wpan-phy.h>
  33. #include "mac802154.h"
  34. static int
  35. mac802154_wpan_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  36. {
  37. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  38. struct sockaddr_ieee802154 *sa =
  39. (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
  40. int err = -ENOIOCTLCMD;
  41. spin_lock_bh(&priv->mib_lock);
  42. switch (cmd) {
  43. case SIOCGIFADDR:
  44. {
  45. u16 pan_id, short_addr;
  46. pan_id = le16_to_cpu(priv->pan_id);
  47. short_addr = le16_to_cpu(priv->short_addr);
  48. if (pan_id == IEEE802154_PANID_BROADCAST ||
  49. short_addr == IEEE802154_ADDR_BROADCAST) {
  50. err = -EADDRNOTAVAIL;
  51. break;
  52. }
  53. sa->family = AF_IEEE802154;
  54. sa->addr.addr_type = IEEE802154_ADDR_SHORT;
  55. sa->addr.pan_id = pan_id;
  56. sa->addr.short_addr = short_addr;
  57. err = 0;
  58. break;
  59. }
  60. case SIOCSIFADDR:
  61. dev_warn(&dev->dev,
  62. "Using DEBUGing ioctl SIOCSIFADDR isn't recommened!\n");
  63. if (sa->family != AF_IEEE802154 ||
  64. sa->addr.addr_type != IEEE802154_ADDR_SHORT ||
  65. sa->addr.pan_id == IEEE802154_PANID_BROADCAST ||
  66. sa->addr.short_addr == IEEE802154_ADDR_BROADCAST ||
  67. sa->addr.short_addr == IEEE802154_ADDR_UNDEF) {
  68. err = -EINVAL;
  69. break;
  70. }
  71. priv->pan_id = cpu_to_le16(sa->addr.pan_id);
  72. priv->short_addr = cpu_to_le16(sa->addr.short_addr);
  73. err = 0;
  74. break;
  75. }
  76. spin_unlock_bh(&priv->mib_lock);
  77. return err;
  78. }
  79. static int mac802154_wpan_mac_addr(struct net_device *dev, void *p)
  80. {
  81. struct sockaddr *addr = p;
  82. if (netif_running(dev))
  83. return -EBUSY;
  84. /* FIXME: validate addr */
  85. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  86. mac802154_dev_set_ieee_addr(dev);
  87. return 0;
  88. }
  89. int mac802154_set_mac_params(struct net_device *dev,
  90. const struct ieee802154_mac_params *params)
  91. {
  92. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  93. mutex_lock(&priv->hw->slaves_mtx);
  94. priv->mac_params = *params;
  95. mutex_unlock(&priv->hw->slaves_mtx);
  96. return 0;
  97. }
  98. void mac802154_get_mac_params(struct net_device *dev,
  99. struct ieee802154_mac_params *params)
  100. {
  101. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  102. mutex_lock(&priv->hw->slaves_mtx);
  103. *params = priv->mac_params;
  104. mutex_unlock(&priv->hw->slaves_mtx);
  105. }
  106. int mac802154_wpan_open(struct net_device *dev)
  107. {
  108. int rc;
  109. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  110. struct wpan_phy *phy = priv->hw->phy;
  111. rc = mac802154_slave_open(dev);
  112. if (rc < 0)
  113. return rc;
  114. mutex_lock(&phy->pib_lock);
  115. if (phy->set_txpower) {
  116. rc = phy->set_txpower(phy, priv->mac_params.transmit_power);
  117. if (rc < 0)
  118. goto out;
  119. }
  120. if (phy->set_lbt) {
  121. rc = phy->set_lbt(phy, priv->mac_params.lbt);
  122. if (rc < 0)
  123. goto out;
  124. }
  125. if (phy->set_cca_mode) {
  126. rc = phy->set_cca_mode(phy, priv->mac_params.cca_mode);
  127. if (rc < 0)
  128. goto out;
  129. }
  130. if (phy->set_cca_ed_level) {
  131. rc = phy->set_cca_ed_level(phy, priv->mac_params.cca_ed_level);
  132. if (rc < 0)
  133. goto out;
  134. }
  135. if (phy->set_csma_params) {
  136. rc = phy->set_csma_params(phy, priv->mac_params.min_be,
  137. priv->mac_params.max_be,
  138. priv->mac_params.csma_retries);
  139. if (rc < 0)
  140. goto out;
  141. }
  142. if (phy->set_frame_retries) {
  143. rc = phy->set_frame_retries(phy,
  144. priv->mac_params.frame_retries);
  145. if (rc < 0)
  146. goto out;
  147. }
  148. mutex_unlock(&phy->pib_lock);
  149. return 0;
  150. out:
  151. mutex_unlock(&phy->pib_lock);
  152. return rc;
  153. }
  154. static int mac802154_header_create(struct sk_buff *skb,
  155. struct net_device *dev,
  156. unsigned short type,
  157. const void *daddr,
  158. const void *saddr,
  159. unsigned len)
  160. {
  161. struct ieee802154_hdr hdr;
  162. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  163. int hlen;
  164. if (!daddr)
  165. return -EINVAL;
  166. memset(&hdr.fc, 0, sizeof(hdr.fc));
  167. hdr.fc.type = mac_cb_type(skb);
  168. hdr.fc.security_enabled = mac_cb_is_secen(skb);
  169. hdr.fc.ack_request = mac_cb_is_ackreq(skb);
  170. if (!saddr) {
  171. spin_lock_bh(&priv->mib_lock);
  172. if (priv->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) ||
  173. priv->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
  174. priv->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
  175. hdr.source.mode = IEEE802154_ADDR_LONG;
  176. hdr.source.extended_addr = priv->extended_addr;
  177. } else {
  178. hdr.source.mode = IEEE802154_ADDR_SHORT;
  179. hdr.source.short_addr = priv->short_addr;
  180. }
  181. hdr.source.pan_id = priv->pan_id;
  182. spin_unlock_bh(&priv->mib_lock);
  183. } else {
  184. hdr.source = *(const struct ieee802154_addr *)saddr;
  185. }
  186. hdr.dest = *(const struct ieee802154_addr *)daddr;
  187. hlen = ieee802154_hdr_push(skb, &hdr);
  188. if (hlen < 0)
  189. return -EINVAL;
  190. skb_reset_mac_header(skb);
  191. skb->mac_len = hlen;
  192. if (hlen + len + 2 > dev->mtu)
  193. return -EMSGSIZE;
  194. return hlen;
  195. }
  196. static int
  197. mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
  198. {
  199. struct ieee802154_hdr hdr;
  200. struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr;
  201. if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
  202. pr_debug("malformed packet\n");
  203. return 0;
  204. }
  205. *addr = hdr.source;
  206. return sizeof(*addr);
  207. }
  208. static netdev_tx_t
  209. mac802154_wpan_xmit(struct sk_buff *skb, struct net_device *dev)
  210. {
  211. struct mac802154_sub_if_data *priv;
  212. u8 chan, page;
  213. priv = netdev_priv(dev);
  214. spin_lock_bh(&priv->mib_lock);
  215. chan = priv->chan;
  216. page = priv->page;
  217. spin_unlock_bh(&priv->mib_lock);
  218. if (chan == MAC802154_CHAN_NONE ||
  219. page >= WPAN_NUM_PAGES ||
  220. chan >= WPAN_NUM_CHANNELS) {
  221. kfree_skb(skb);
  222. return NETDEV_TX_OK;
  223. }
  224. skb->skb_iif = dev->ifindex;
  225. dev->stats.tx_packets++;
  226. dev->stats.tx_bytes += skb->len;
  227. return mac802154_tx(priv->hw, skb, page, chan);
  228. }
  229. static struct header_ops mac802154_header_ops = {
  230. .create = mac802154_header_create,
  231. .parse = mac802154_header_parse,
  232. };
  233. static const struct net_device_ops mac802154_wpan_ops = {
  234. .ndo_open = mac802154_wpan_open,
  235. .ndo_stop = mac802154_slave_close,
  236. .ndo_start_xmit = mac802154_wpan_xmit,
  237. .ndo_do_ioctl = mac802154_wpan_ioctl,
  238. .ndo_set_mac_address = mac802154_wpan_mac_addr,
  239. };
  240. void mac802154_wpan_setup(struct net_device *dev)
  241. {
  242. struct mac802154_sub_if_data *priv;
  243. dev->addr_len = IEEE802154_ADDR_LEN;
  244. memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
  245. dev->hard_header_len = MAC802154_FRAME_HARD_HEADER_LEN;
  246. dev->header_ops = &mac802154_header_ops;
  247. dev->needed_tailroom = 2; /* FCS */
  248. dev->mtu = IEEE802154_MTU;
  249. dev->tx_queue_len = 300;
  250. dev->type = ARPHRD_IEEE802154;
  251. dev->flags = IFF_NOARP | IFF_BROADCAST;
  252. dev->watchdog_timeo = 0;
  253. dev->destructor = free_netdev;
  254. dev->netdev_ops = &mac802154_wpan_ops;
  255. dev->ml_priv = &mac802154_mlme_wpan;
  256. priv = netdev_priv(dev);
  257. priv->type = IEEE802154_DEV_WPAN;
  258. priv->chan = MAC802154_CHAN_NONE;
  259. priv->page = 0;
  260. spin_lock_init(&priv->mib_lock);
  261. get_random_bytes(&priv->bsn, 1);
  262. get_random_bytes(&priv->dsn, 1);
  263. /* defaults per 802.15.4-2011 */
  264. priv->mac_params.min_be = 3;
  265. priv->mac_params.max_be = 5;
  266. priv->mac_params.csma_retries = 4;
  267. priv->mac_params.frame_retries = -1; /* for compatibility, actual default is 3 */
  268. priv->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
  269. priv->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
  270. }
  271. static int mac802154_process_data(struct net_device *dev, struct sk_buff *skb)
  272. {
  273. return netif_rx_ni(skb);
  274. }
  275. static int
  276. mac802154_subif_frame(struct mac802154_sub_if_data *sdata, struct sk_buff *skb)
  277. {
  278. __le16 span, sshort;
  279. pr_debug("getting packet via slave interface %s\n", sdata->dev->name);
  280. spin_lock_bh(&sdata->mib_lock);
  281. span = sdata->pan_id;
  282. sshort = sdata->short_addr;
  283. switch (mac_cb(skb)->dest.mode) {
  284. case IEEE802154_ADDR_NONE:
  285. if (mac_cb(skb)->dest.mode != IEEE802154_ADDR_NONE)
  286. /* FIXME: check if we are PAN coordinator */
  287. skb->pkt_type = PACKET_OTHERHOST;
  288. else
  289. /* ACK comes with both addresses empty */
  290. skb->pkt_type = PACKET_HOST;
  291. break;
  292. case IEEE802154_ADDR_LONG:
  293. if (mac_cb(skb)->dest.pan_id != span &&
  294. mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
  295. skb->pkt_type = PACKET_OTHERHOST;
  296. else if (mac_cb(skb)->dest.extended_addr == sdata->extended_addr)
  297. skb->pkt_type = PACKET_HOST;
  298. else
  299. skb->pkt_type = PACKET_OTHERHOST;
  300. break;
  301. case IEEE802154_ADDR_SHORT:
  302. if (mac_cb(skb)->dest.pan_id != span &&
  303. mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
  304. skb->pkt_type = PACKET_OTHERHOST;
  305. else if (mac_cb(skb)->dest.short_addr == sshort)
  306. skb->pkt_type = PACKET_HOST;
  307. else if (mac_cb(skb)->dest.short_addr ==
  308. cpu_to_le16(IEEE802154_ADDR_BROADCAST))
  309. skb->pkt_type = PACKET_BROADCAST;
  310. else
  311. skb->pkt_type = PACKET_OTHERHOST;
  312. break;
  313. default:
  314. break;
  315. }
  316. spin_unlock_bh(&sdata->mib_lock);
  317. skb->dev = sdata->dev;
  318. sdata->dev->stats.rx_packets++;
  319. sdata->dev->stats.rx_bytes += skb->len;
  320. switch (mac_cb_type(skb)) {
  321. case IEEE802154_FC_TYPE_DATA:
  322. return mac802154_process_data(sdata->dev, skb);
  323. default:
  324. pr_warn("ieee802154: bad frame received (type = %d)\n",
  325. mac_cb_type(skb));
  326. kfree_skb(skb);
  327. return NET_RX_DROP;
  328. }
  329. }
  330. static void mac802154_print_addr(const char *name,
  331. const struct ieee802154_addr *addr)
  332. {
  333. if (addr->mode == IEEE802154_ADDR_NONE)
  334. pr_debug("%s not present\n", name);
  335. pr_debug("%s PAN ID: %04x\n", name, le16_to_cpu(addr->pan_id));
  336. if (addr->mode == IEEE802154_ADDR_SHORT) {
  337. pr_debug("%s is short: %04x\n", name,
  338. le16_to_cpu(addr->short_addr));
  339. } else {
  340. u64 hw = swab64((__force u64) addr->extended_addr);
  341. pr_debug("%s is hardware: %8phC\n", name, &hw);
  342. }
  343. }
  344. static int mac802154_parse_frame_start(struct sk_buff *skb)
  345. {
  346. int hlen;
  347. struct ieee802154_hdr hdr;
  348. hlen = ieee802154_hdr_pull(skb, &hdr);
  349. if (hlen < 0)
  350. return -EINVAL;
  351. skb->mac_len = hlen;
  352. pr_debug("fc: %04x dsn: %02x\n", le16_to_cpup((__le16 *)&hdr.fc),
  353. hdr.seq);
  354. mac_cb(skb)->flags = hdr.fc.type;
  355. if (hdr.fc.ack_request)
  356. mac_cb(skb)->flags |= MAC_CB_FLAG_ACKREQ;
  357. if (hdr.fc.security_enabled)
  358. mac_cb(skb)->flags |= MAC_CB_FLAG_SECEN;
  359. mac802154_print_addr("destination", &hdr.dest);
  360. mac802154_print_addr("source", &hdr.source);
  361. mac_cb(skb)->source = hdr.source;
  362. mac_cb(skb)->dest = hdr.dest;
  363. if (hdr.fc.security_enabled) {
  364. u64 key;
  365. pr_debug("seclevel %i\n", hdr.sec.level);
  366. switch (hdr.sec.key_id_mode) {
  367. case IEEE802154_SCF_KEY_IMPLICIT:
  368. pr_debug("implicit key\n");
  369. break;
  370. case IEEE802154_SCF_KEY_INDEX:
  371. pr_debug("key %02x\n", hdr.sec.key_id);
  372. break;
  373. case IEEE802154_SCF_KEY_SHORT_INDEX:
  374. pr_debug("key %04x:%04x %02x\n",
  375. le32_to_cpu(hdr.sec.short_src) >> 16,
  376. le32_to_cpu(hdr.sec.short_src) & 0xffff,
  377. hdr.sec.key_id);
  378. break;
  379. case IEEE802154_SCF_KEY_HW_INDEX:
  380. key = swab64((__force u64) hdr.sec.extended_src);
  381. pr_debug("key source %8phC %02x\n", &key,
  382. hdr.sec.key_id);
  383. break;
  384. }
  385. return -EINVAL;
  386. }
  387. return 0;
  388. }
  389. void mac802154_wpans_rx(struct mac802154_priv *priv, struct sk_buff *skb)
  390. {
  391. int ret;
  392. struct sk_buff *sskb;
  393. struct mac802154_sub_if_data *sdata;
  394. ret = mac802154_parse_frame_start(skb);
  395. if (ret) {
  396. pr_debug("got invalid frame\n");
  397. return;
  398. }
  399. rcu_read_lock();
  400. list_for_each_entry_rcu(sdata, &priv->slaves, list) {
  401. if (sdata->type != IEEE802154_DEV_WPAN)
  402. continue;
  403. sskb = skb_clone(skb, GFP_ATOMIC);
  404. if (sskb)
  405. mac802154_subif_frame(sdata, sskb);
  406. }
  407. rcu_read_unlock();
  408. }