Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mac802154: monitor device support

Support for monitor device intended to capture all the network activity.
This interface could be used by networks sniffers and is already
supported by WireShark. That's a good test point to check that basic
MAC support works.

Signed-off-by: Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

alex.bluesman.smirnov@gmail.com and committed by
David S. Miller
0606069d 62610ad2

+150 -2
+1
include/linux/if_arp.h
··· 87 87 #define ARPHRD_IEEE80211_PRISM 802 /* IEEE 802.11 + Prism2 header */ 88 88 #define ARPHRD_IEEE80211_RADIOTAP 803 /* IEEE 802.11 + radiotap header */ 89 89 #define ARPHRD_IEEE802154 804 90 + #define ARPHRD_IEEE802154_MONITOR 805 /* IEEE 802.15.4 network monitor */ 90 91 91 92 #define ARPHRD_PHONET 820 /* PhoNet media type */ 92 93 #define ARPHRD_PHONET_PIPE 821 /* PhoNet pipe header */
+14
include/linux/nl802154.h
··· 129 129 130 130 enum { 131 131 __IEEE802154_DEV_INVALID = -1, 132 + 133 + /* TODO: 134 + * Nowadays three device types supported by this stack at linux-zigbee 135 + * project: WPAN = 0, MONITOR = 1 and SMAC = 2. 136 + * 137 + * Since this stack implementation exists many years, it's definitely 138 + * bad idea to change the assigned values due to they are already used 139 + * by third-party userspace software like: iz-tools, wireshark... 140 + * 141 + * Currently only monitor device is added and initialized by '1' for 142 + * compatibility. 143 + */ 144 + IEEE802154_DEV_MONITOR = 1, 145 + 132 146 __IEEE802154_DEV_MAX, 133 147 }; 134 148
+8
include/net/wpan-phy.h
··· 25 25 #include <linux/mutex.h> 26 26 #include <linux/bug.h> 27 27 28 + /* According to the IEEE 802.15.4 stadard the upper most significant bits of 29 + * the 32-bit channel bitmaps shall be used as an integer value to specify 32 30 + * possible channel pages. The lower 27 bits of the channel bit map shall be 31 + * used as a bit mask to specify channel numbers within a channel page. 32 + */ 33 + #define WPAN_NUM_CHANNELS 27 34 + #define WPAN_NUM_PAGES 32 35 + 28 36 struct wpan_phy { 29 37 struct mutex pib_lock; 30 38
+1 -1
net/mac802154/Makefile
··· 1 1 obj-$(CONFIG_MAC802154) += mac802154.o 2 - mac802154-objs := ieee802154_dev.o rx.o tx.o mac_cmd.o mib.o 2 + mac802154-objs := ieee802154_dev.o rx.o tx.o mac_cmd.o mib.o monitor.o
+4 -1
net/mac802154/ieee802154_dev.c
··· 135 135 struct net_device *dev; 136 136 int err = -ENOMEM; 137 137 138 - /* No devices is currently added */ 139 138 switch (type) { 139 + case IEEE802154_DEV_MONITOR: 140 + dev = alloc_netdev(sizeof(struct mac802154_sub_if_data), 141 + name, mac802154_monitor_setup); 142 + break; 140 143 default: 141 144 dev = NULL; 142 145 err = -EINVAL;
+5
net/mac802154/mac802154.h
··· 90 90 91 91 #define MAC802154_MAX_XMIT_ATTEMPTS 3 92 92 93 + #define MAC802154_CHAN_NONE (~(u8)0) /* No channel is assigned */ 94 + 93 95 extern struct ieee802154_reduced_mlme_ops mac802154_mlme_reduced; 94 96 95 97 int mac802154_slave_open(struct net_device *dev); 96 98 int mac802154_slave_close(struct net_device *dev); 99 + 100 + void mac802154_monitors_rx(struct mac802154_priv *priv, struct sk_buff *skb); 101 + void mac802154_monitor_setup(struct net_device *dev); 97 102 98 103 netdev_tx_t mac802154_tx(struct mac802154_priv *priv, struct sk_buff *skb, 99 104 u8 page, u8 chan);
+116
net/mac802154/monitor.c
··· 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 + 24 + #include <linux/netdevice.h> 25 + #include <linux/skbuff.h> 26 + #include <linux/if_arp.h> 27 + #include <linux/crc-ccitt.h> 28 + 29 + #include <net/ieee802154.h> 30 + #include <net/mac802154.h> 31 + #include <net/netlink.h> 32 + #include <net/wpan-phy.h> 33 + #include <linux/nl802154.h> 34 + 35 + #include "mac802154.h" 36 + 37 + static netdev_tx_t mac802154_monitor_xmit(struct sk_buff *skb, 38 + struct net_device *dev) 39 + { 40 + struct mac802154_sub_if_data *priv; 41 + u8 chan, page; 42 + 43 + priv = netdev_priv(dev); 44 + 45 + /* FIXME: locking */ 46 + chan = priv->hw->phy->current_channel; 47 + page = priv->hw->phy->current_page; 48 + 49 + if (chan == MAC802154_CHAN_NONE) /* not initialized */ 50 + return NETDEV_TX_OK; 51 + 52 + if (WARN_ON(page >= WPAN_NUM_PAGES) || 53 + WARN_ON(chan >= WPAN_NUM_CHANNELS)) 54 + return NETDEV_TX_OK; 55 + 56 + skb->skb_iif = dev->ifindex; 57 + dev->stats.tx_packets++; 58 + dev->stats.tx_bytes += skb->len; 59 + 60 + return mac802154_tx(priv->hw, skb, page, chan); 61 + } 62 + 63 + 64 + void mac802154_monitors_rx(struct mac802154_priv *priv, struct sk_buff *skb) 65 + { 66 + struct sk_buff *skb2; 67 + struct mac802154_sub_if_data *sdata; 68 + u16 crc = crc_ccitt(0, skb->data, skb->len); 69 + u8 *data; 70 + 71 + rcu_read_lock(); 72 + list_for_each_entry_rcu(sdata, &priv->slaves, list) { 73 + if (sdata->type != IEEE802154_DEV_MONITOR) 74 + continue; 75 + 76 + skb2 = skb_clone(skb, GFP_ATOMIC); 77 + skb2->dev = sdata->dev; 78 + skb2->pkt_type = PACKET_HOST; 79 + data = skb_put(skb2, 2); 80 + data[0] = crc & 0xff; 81 + data[1] = crc >> 8; 82 + 83 + netif_rx_ni(skb2); 84 + } 85 + rcu_read_unlock(); 86 + } 87 + 88 + static const struct net_device_ops mac802154_monitor_ops = { 89 + .ndo_open = mac802154_slave_open, 90 + .ndo_stop = mac802154_slave_close, 91 + .ndo_start_xmit = mac802154_monitor_xmit, 92 + }; 93 + 94 + void mac802154_monitor_setup(struct net_device *dev) 95 + { 96 + struct mac802154_sub_if_data *priv; 97 + 98 + dev->addr_len = 0; 99 + dev->hard_header_len = 0; 100 + dev->needed_tailroom = 2; /* room for FCS */ 101 + dev->mtu = IEEE802154_MTU; 102 + dev->tx_queue_len = 10; 103 + dev->type = ARPHRD_IEEE802154_MONITOR; 104 + dev->flags = IFF_NOARP | IFF_BROADCAST; 105 + dev->watchdog_timeo = 0; 106 + 107 + dev->destructor = free_netdev; 108 + dev->netdev_ops = &mac802154_monitor_ops; 109 + dev->ml_priv = &mac802154_mlme_reduced; 110 + 111 + priv = netdev_priv(dev); 112 + priv->type = IEEE802154_DEV_MONITOR; 113 + 114 + priv->chan = MAC802154_CHAN_NONE; /* not initialized */ 115 + priv->page = 0; 116 + }
+1
net/mac802154/rx.c
··· 76 76 skb_trim(skb, skb->len - 2); /* CRC */ 77 77 } 78 78 79 + mac802154_monitors_rx(priv, skb); 79 80 out: 80 81 dev_kfree_skb(skb); 81 82 return;