monitor.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2007, 2008, 2009 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/skbuff.h>
  25. #include <linux/if_arp.h>
  26. #include <linux/crc-ccitt.h>
  27. #include <net/ieee802154.h>
  28. #include <net/mac802154.h>
  29. #include <net/netlink.h>
  30. #include <net/wpan-phy.h>
  31. #include <linux/nl802154.h>
  32. #include "mac802154.h"
  33. static netdev_tx_t mac802154_monitor_xmit(struct sk_buff *skb,
  34. struct net_device *dev)
  35. {
  36. struct mac802154_sub_if_data *priv;
  37. u8 chan, page;
  38. priv = netdev_priv(dev);
  39. /* FIXME: locking */
  40. chan = priv->hw->phy->current_channel;
  41. page = priv->hw->phy->current_page;
  42. if (chan == MAC802154_CHAN_NONE) /* not initialized */
  43. return NETDEV_TX_OK;
  44. if (WARN_ON(page >= WPAN_NUM_PAGES) ||
  45. WARN_ON(chan >= WPAN_NUM_CHANNELS))
  46. return NETDEV_TX_OK;
  47. skb->skb_iif = dev->ifindex;
  48. dev->stats.tx_packets++;
  49. dev->stats.tx_bytes += skb->len;
  50. return mac802154_tx(priv->hw, skb, page, chan);
  51. }
  52. void mac802154_monitors_rx(struct mac802154_priv *priv, struct sk_buff *skb)
  53. {
  54. struct sk_buff *skb2;
  55. struct mac802154_sub_if_data *sdata;
  56. u16 crc = crc_ccitt(0, skb->data, skb->len);
  57. u8 *data;
  58. rcu_read_lock();
  59. list_for_each_entry_rcu(sdata, &priv->slaves, list) {
  60. if (sdata->type != IEEE802154_DEV_MONITOR)
  61. continue;
  62. skb2 = skb_clone(skb, GFP_ATOMIC);
  63. skb2->dev = sdata->dev;
  64. skb2->pkt_type = PACKET_HOST;
  65. data = skb_put(skb2, 2);
  66. data[0] = crc & 0xff;
  67. data[1] = crc >> 8;
  68. netif_rx_ni(skb2);
  69. }
  70. rcu_read_unlock();
  71. }
  72. static const struct net_device_ops mac802154_monitor_ops = {
  73. .ndo_open = mac802154_slave_open,
  74. .ndo_stop = mac802154_slave_close,
  75. .ndo_start_xmit = mac802154_monitor_xmit,
  76. };
  77. void mac802154_monitor_setup(struct net_device *dev)
  78. {
  79. struct mac802154_sub_if_data *priv;
  80. dev->addr_len = 0;
  81. dev->hard_header_len = 0;
  82. dev->needed_tailroom = 2; /* room for FCS */
  83. dev->mtu = IEEE802154_MTU;
  84. dev->tx_queue_len = 10;
  85. dev->type = ARPHRD_IEEE802154_MONITOR;
  86. dev->flags = IFF_NOARP | IFF_BROADCAST;
  87. dev->watchdog_timeo = 0;
  88. dev->destructor = free_netdev;
  89. dev->netdev_ops = &mac802154_monitor_ops;
  90. dev->ml_priv = &mac802154_mlme_reduced;
  91. priv = netdev_priv(dev);
  92. priv->type = IEEE802154_DEV_MONITOR;
  93. priv->chan = MAC802154_CHAN_NONE; /* not initialized */
  94. priv->page = 0;
  95. }