Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit ef8388a93f89bf71688a961c115481617e3280ec 0 parents
@ice799 ice799 authored
10 .gitignore
@@ -0,0 +1,10 @@
+*.ko
+*.o
+.tmp_version
+*.cmd
+*.mod.c
+.tmp_versions
+ioctl_test
+output_packets
+Module.symvers
+modules.order
201 LICENSE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+Copyright [yyyy] [name of copyright owner]
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
21 Makefile
@@ -0,0 +1,21 @@
+ifeq ($(KERNELRELEASE),)
+
+KERNELDIR ?= /lib/modules/$(shell uname -r)/build
+PWD := $(shell pwd)
+
+.PHONY: build clean
+
+build:
+ $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
+ gcc ioctl_test.c -o ioctl_test
+ gcc output_packets.c -o output_packets -lpcap
+
+clean:
+ rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c
+ rm -rf ioctl_test output_packets
+else
+
+$(info Building with KERNELRELEASE = ${KERNELRELEASE})
+obj-m := khial.o
+khial-objs := khial_main.o khial_ethtool.o
+endif
39 README.md
@@ -0,0 +1,39 @@
+### khial
+
+khial is a kernel module that pretends to be a network device and exposes a few ways for userland programs to interact with the driver, pass packets through it, gather statistics information, or reset internal counters.
+
+### why khial?
+
+i wanted a more realistic way to test applications which:
+
+ - gather/examine network interface statistics from /proc/net/dev
+ - sniff packets from network devices (specifically the Boundary flow meter, but any application, really)
+
+it turns out that a lot (but certainly not all) of testing in this area is focused on reading packet traces out of files, which is great for certain types of testing.
+
+i wanted a way to test for strange bugs in different network configurations to detect weird bugs before shipping to customers (see also: http://blog.boundary.com/2012/04/04/a-deep-dive-to-find-a-nasty-bug/). khial helps to do this, beacuse you can ask khial to create several test devices and assign them various IP addresses, MAC addresses, and so on.
+
+### how to build, load, and unload
+
+you will need to follow the directions for building kernel modules on your linux distribution. usually, you'll just need to install some sort of kernel header file package (on Ubuntu, it might be called something like linux-headers-$(uname -r)).
+
+once you've gotten the build enviroment set up (in the source directory):
+
+ - make
+ - sudo ./loaddriver.sh
+
+to unload simply run:
+
+ - sudo rmmod khial
+
+### how to use khial
+
+once it has been loaded, khial will expose a network device. you should be able to see it by runnig ifconfig -a.
+
+khial exposes a character device that gets mknod'd in loaddriver.sh and appears in /dev/char/, named testpacket0, testpacket1, etc.
+
+see output_packet.c for an example of how to write packets to khial's character device.
+
+### bugs
+
+i don't know much about writing network device drivers for linux, so there are probably better/more correct ways to write this driver. pull requests are happily accepted.
63 ioctl_test.c
@@ -0,0 +1,63 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <string.h>
+#include <net/if.h>
+
+#include "khial.h"
+
+int main(int argc, char *argv[])
+{
+ int fd = 0;
+ struct ifreq ifr;
+ int err = 0;
+
+ unsigned long go = 696969;
+
+ /* open a control socket */
+ fd = socket(AF_INET, SOCK_DGRAM, 0);
+ if (fd == -1) {
+ perror("socket");
+ exit(1);
+ }
+
+ memset(&ifr, 0, sizeof(ifr));
+ strncpy(ifr.ifr_name, "test0", IFNAMSIZ);
+
+ /* clear all stats */
+ ioctl(fd, KHIAL_PKT_CLEAR_ALL);
+
+ /* increment RX packet count by 1 */
+ err = ioctl(fd, KHIAL_PKT_RX_INCR, &ifr);
+ if (err == -1) {
+ perror("ioctl");
+ exit(1);
+ }
+
+ /* increment TX packet count by 1 */
+ ioctl(fd, KHIAL_PKT_TX_INCR, &ifr);
+
+ /* increment TX packet count by 1 */
+ ioctl(fd, KHIAL_PKT_TX_INCR, &ifr);
+
+ /* increment the TX byte count by the value of 'go' */
+ ifr.ifr_data = (caddr_t) &go;
+ err = ioctl(fd, KHIAL_BYTE_TX_INCR, &ifr);
+ if (err == -1) {
+ perror("ioctl");
+ exit(1);
+ }
+
+ /* incremenet the RX byte count byte the value of 'go' */
+ go = 31337;
+ ifr.ifr_data = (caddr_t) &go;
+ err = ioctl(fd, KHIAL_BYTE_RX_INCR, &ifr);
+ if (err == -1) {
+ perror("ioctl");
+ exit(1);
+ }
+
+ return 0;
+}
37 khial.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2012, Boundary
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __KHIAL_H__
+#define __KHIAL_H__
+
+/* these are the file_operations ioctls */
+#define KHIAL_SET_DIRECTION (0x1)
+#define KHIAL_GET_DIRECTION (0x2)
+
+/* these are the directions */
+typedef enum {
+ KHIAL_DIRECTION_RX = 0,
+ KHIAL_DIRECTION_TX,
+} khial_direction_t;
+
+/* these are the netdev ioctls */
+#define KHIAL_PKT_CLEAR_ALL (0x89F0)
+#define KHIAL_PKT_RX_INCR (0x89F1)
+#define KHIAL_PKT_TX_INCR (0x89F2)
+#define KHIAL_BYTE_TX_INCR (0x89F3)
+#define KHIAL_BYTE_RX_INCR (0x89F4)
+
+#endif /* __KHIAL_H__ */
46 khial_ethtool.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2012, Boundary
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <linux/string.h>
+#include <linux/netdevice.h>
+#include <linux/ethtool.h>
+
+struct khial_stats {
+ char stat_string[ETH_GSTRING_LEN];
+ int sizeof_stat;
+ int stat_offset;
+};
+
+static const char *khial_driver_name = "khial faux nic";
+static const char *khial_driver_version = "JOEXTREEM v1";
+static const char *fw_version = "12.19.84";
+
+static void khial_get_drvinfo(struct net_device *netdev,
+ struct ethtool_drvinfo *drvinfo)
+{
+ strncpy(drvinfo->driver, khial_driver_name, 32);
+ strncpy(drvinfo->version, khial_driver_version, 32);
+ strncpy(drvinfo->fw_version, fw_version, 32);
+}
+
+static const struct ethtool_ops khial_ethtool_ops = {
+ .get_drvinfo = khial_get_drvinfo,
+};
+
+void khial_set_ethtool_ops(struct net_device *netdev)
+{
+ SET_ETHTOOL_OPS(netdev, &khial_ethtool_ops);
+}
411 khial_main.c
@@ -0,0 +1,411 @@
+/*
+ * Copyright 2012, Boundary
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "khial.h"
+
+#include <linux/list.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/types.h>
+#include <linux/string.h>
+#include <linux/socket.h>
+#include <linux/errno.h>
+#include <linux/fcntl.h>
+#include <linux/in.h>
+#include <linux/init.h>
+#include <linux/cdev.h>
+#include <linux/netdevice.h>
+#include <linux/etherdevice.h>
+#include <linux/skbuff.h>
+#include <linux/ethtool.h>
+#include <net/sock.h>
+#include <net/sch_generic.h>
+
+extern void khial_set_ethtool_ops(struct net_device *);
+
+struct pcap_pkthdr {
+ struct timeval ts;
+ uint32_t caplen;
+ uint32_t len;
+};
+
+struct khial {
+ struct net_device *dev;
+ struct cdev cdev;
+ rwlock_t lock;
+ struct net_device_stats net_stats;
+ struct list_head khial_list;
+ khial_direction_t direction;
+ int packet_minor;
+};
+
+static int packet_major = 0;
+static int packet_minor = 0;
+
+/* Number of khial devices to be set up by this module. */
+static int num_khial_devs = 1;
+module_param(num_khial_devs, int, 0);
+MODULE_PARM_DESC(num_khial_devs, "Number of khial pseudo network devices");
+
+LIST_HEAD(khial_dev_list);
+
+static int khial_open(struct inode *inode, struct file *filp)
+{
+ struct khial *dev;
+
+ dev = container_of(inode->i_cdev, struct khial, cdev);
+
+ filp->private_data = dev;
+
+ /* reset all stats */
+ write_lock(&dev->lock);
+ dev->net_stats.rx_packets = 0;
+ dev->net_stats.rx_bytes = 0;
+ dev->net_stats.tx_packets = 0;
+ dev->net_stats.tx_bytes = 0;
+ write_unlock(&dev->lock);
+ return 0;
+}
+
+static ssize_t khial_write(struct file *filp, const char __user *buf, size_t count, loff_t *fpos)
+{
+ ssize_t ret = 0;
+ struct pcap_pkthdr hdr;
+ struct khial *dev;
+ struct sk_buff *skb;
+
+ dev = filp->private_data;
+
+ if (copy_from_user(&hdr, buf, sizeof(struct pcap_pkthdr))) {
+ ret = -EFAULT;
+ goto out;
+ }
+
+ skb = netdev_alloc_skb(dev->dev, hdr.len + NET_IP_ALIGN);
+ if (unlikely(!skb)) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ buf += sizeof(struct pcap_pkthdr);
+
+ if (copy_from_user(skb->data, buf, hdr.caplen)) {
+ ret = -EFAULT;
+ goto out;
+ }
+
+ skb_put(skb, hdr.len);
+
+ if (dev->direction == KHIAL_DIRECTION_RX) {
+ skb->protocol = eth_type_trans(skb, dev->dev);
+ write_lock(&dev->lock);
+ dev->net_stats.rx_packets++;
+ dev->net_stats.rx_bytes += hdr.len;
+ write_unlock(&dev->lock);
+ netif_receive_skb(skb);
+ } else if (dev->direction == KHIAL_DIRECTION_TX) {
+ skb_reset_network_header(skb);
+ skb->priority = 1;
+ dev_queue_xmit(skb);
+ }
+
+ *fpos += count;
+ ret = count;
+
+out:
+ return ret;
+}
+
+static int khial_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
+{
+ struct khial *dev = filp->private_data;
+ int retval = 0;
+ char __user *argp = (char __user *)arg;
+
+ switch (cmd) {
+ case KHIAL_SET_DIRECTION:
+ if (copy_from_user(&dev->direction, argp, sizeof(khial_direction_t)))
+ retval = -EFAULT;
+
+ break;
+
+ case KHIAL_GET_DIRECTION:
+ if (copy_to_user(argp, &dev->direction, sizeof(khial_direction_t)))
+ retval = -EFAULT;
+
+ break;
+
+ default:
+ retval = -1;
+ break;
+ }
+
+ return retval;
+}
+
+const struct file_operations khial_fops = {
+ .open = khial_open,
+ .write = khial_write,
+ .ioctl = khial_ioctl
+};
+
+static int khial_set_address(struct net_device *dev, void *p)
+{
+ struct sockaddr *sa = p;
+
+ if (!is_valid_ether_addr(sa->sa_data))
+ return -EADDRNOTAVAIL;
+
+ memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
+ return 0;
+}
+
+/* fake multicast ability */
+static void set_multicast_list(struct net_device *dev)
+{
+ return;
+}
+
+static netdev_tx_t khial_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+ struct khial *adapter = netdev_priv(dev);
+
+ dev_kfree_skb_any(skb);
+
+ write_lock(&adapter->lock);
+ adapter->net_stats.tx_bytes += skb->len;
+ adapter->net_stats.tx_packets ++;
+ write_unlock(&adapter->lock);
+
+ return NETDEV_TX_OK;
+}
+
+static struct net_device_stats *khial_get_stats(struct net_device *dev)
+{
+ struct khial *adapter = netdev_priv(dev);
+ return &adapter->net_stats;
+}
+
+static int khial_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
+{
+ struct khial *adapter = netdev_priv(dev);
+ unsigned long len = 0;
+ int retval = 0;
+
+ write_lock(&adapter->lock);
+
+ switch (cmd) {
+ case KHIAL_PKT_CLEAR_ALL:
+ adapter->net_stats.rx_packets = 0;
+ adapter->net_stats.tx_packets = 0;
+ adapter->net_stats.rx_bytes = 0;
+ adapter->net_stats.tx_bytes = 0;
+ break;
+ case KHIAL_PKT_RX_INCR:
+ adapter->net_stats.rx_packets++;
+ break;
+ case KHIAL_PKT_TX_INCR:
+ adapter->net_stats.tx_packets++;
+ break;
+ case KHIAL_BYTE_TX_INCR:
+ if (copy_from_user(&len, ifr->ifr_data, sizeof(len))) {
+ retval = -EFAULT;
+ goto out;
+ }
+ adapter->net_stats.tx_bytes += len;
+ break;
+ case KHIAL_BYTE_RX_INCR:
+ if (copy_from_user(&len, ifr->ifr_data, sizeof(len))) {
+ retval = -EFAULT;
+ goto out;
+ }
+ adapter->net_stats.rx_bytes += len;
+ break;
+ default:
+ retval = -EOPNOTSUPP;
+ }
+
+out:
+ write_unlock(&adapter->lock);
+
+ return 0;
+}
+
+static const struct net_device_ops khial_netdev_ops = {
+ .ndo_start_xmit = khial_xmit,
+ .ndo_validate_addr = eth_validate_addr,
+ .ndo_set_multicast_list = set_multicast_list,
+ .ndo_set_mac_address = khial_set_address,
+ .ndo_do_ioctl = khial_do_ioctl,
+ .ndo_get_stats = khial_get_stats,
+};
+
+static void khial_setup(struct net_device *dev)
+{
+ struct khial *adapter;
+
+ /* Initialize the device structure. */
+ dev->netdev_ops = &khial_netdev_ops;
+
+ /* Fill in device structure with ethernet-generic values. */
+ ether_setup(dev);
+
+ dev->tx_queue_len = 0;
+ dev->mtu = ETH_DATA_LEN;
+ dev->hard_header_len = ETH_HLEN;
+ dev->addr_len = ETH_ALEN;
+
+ /* make sure the device is broadcast, multicast, and UP */
+ dev->flags |= (IFF_MULTICAST | IFF_BROADCAST);
+ dev->features = NETIF_F_LLTX | NETIF_F_NO_CSUM;
+
+ random_ether_addr(dev->perm_addr);
+ random_ether_addr(dev->dev_addr);
+
+ adapter = netdev_priv(dev);
+ memset(adapter, 0, sizeof(struct khial));
+ adapter->dev = dev;
+
+ rwlock_init(&adapter->lock);
+
+ list_add_tail(&adapter->khial_list, &khial_dev_list);
+
+ return;
+}
+
+static int khial_init_one(struct khial **adapter)
+{
+ struct net_device *khial_dev;
+ int err;
+
+ khial_dev = alloc_netdev(sizeof(struct khial), "test%d", khial_setup);
+
+ if (!khial_dev) {
+ printk(KERN_WARNING "Error, can't alloc netdev!");
+ err = -ENOMEM;
+ goto out;
+ }
+
+ err = dev_alloc_name(khial_dev, khial_dev->name);
+ if (err < 0) {
+ err = -ENOMEM;
+ goto out_free;
+ }
+
+ err = register_netdev(khial_dev);
+ if (err < 0) {
+ err = -ENOMEM;
+ goto out_free;
+ }
+
+ khial_set_ethtool_ops(khial_dev);
+ netif_carrier_on(khial_dev);
+ *adapter = netdev_priv(khial_dev);
+ return 0;
+
+out_free:
+ free_netdev(khial_dev);
+
+out:
+ return err;
+}
+
+static int setup_cdev(struct khial *adapter, int idx)
+{
+ dev_t devno = MKDEV(packet_major, packet_minor + idx);
+ cdev_init(&(adapter->cdev), &khial_fops);
+
+ adapter->cdev.owner = THIS_MODULE;
+ adapter->cdev.ops = &khial_fops;
+
+ return cdev_add(&adapter->cdev, devno, 1);
+}
+
+static void khial_dev_free(struct net_device *dev)
+{
+ unregister_netdev(dev);
+ free_netdev(dev);
+}
+
+static void khial_free_all(void)
+{
+ struct khial *adapter, *next;
+
+ list_for_each_entry_safe(adapter, next, &khial_dev_list, khial_list) {
+ struct net_device *dev = adapter->dev;
+ cdev_del(&adapter->cdev);
+ khial_dev_free(dev);
+ list_del(&adapter->khial_list);
+ }
+
+ return;
+}
+
+static int __init khial_init_module(void)
+{
+ int i, err = 0;
+ dev_t dev = 0;
+ struct khial *adapter;
+
+ err = alloc_chrdev_region(&dev, 0, num_khial_devs, "testpackets");
+ if (err < 0) {
+ printk(KERN_WARNING "unable to allocate char devices for khial: %d\n", err);
+ goto out;
+ }
+
+ packet_major = MAJOR(dev);
+
+ for (i = 0; i < num_khial_devs && !err; i++) {
+ err = khial_init_one(&adapter);
+ if (err < 0) {
+ printk("Unable to initialize device!");
+ goto out_unregister;
+ }
+
+
+ err = setup_cdev(adapter, i);
+ if (err < 0) {
+ printk(KERN_WARNING "Error %d adding char dev!", err);
+ goto out_deinit;
+ }
+
+ rtnl_lock();
+ dev_open(adapter->dev);
+ rtnl_unlock();
+ }
+
+out:
+ return err;
+
+out_deinit:
+ khial_free_all();
+
+out_unregister:
+ unregister_chrdev_region(dev, num_khial_devs);
+ return -1;
+}
+
+static void __exit khial_cleanup_module(void)
+{
+ dev_t devno = MKDEV(packet_major, 0);
+ khial_free_all();
+ unregister_chrdev_region(devno, num_khial_devs);
+ return;
+}
+
+module_init(khial_init_module);
+module_exit(khial_cleanup_module);
+MODULE_LICENSE("GPL");
44 loaddriver.sh
@@ -0,0 +1,44 @@
+#!/bin/bash
+set -x
+# Copyright 2012, Boundary
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+module="khial"
+device="testpackets"
+mode="664"
+
+num_devs=$1
+if [ -z $num_devs ]; then
+ num_devs=1
+fi
+
+/sbin/insmod ./$module.ko num_khial_devs=$num_devs || exit 1
+
+# remove stale nodes
+rm -f /dev/char/${device}*
+
+major=$(awk "\$2==\"$device\" {print \$1}" /proc/devices)
+
+for (( i = 0; i < $num_devs; i++ ))
+do
+ mknod /dev/char/${device}${i} c $major $i
+done
+
+# give appropriate group/permissions, and change the group.
+# Not all distributions have staff, some have "wheel" instead.
+group="root"
+grep -q '^staff:' /etc/group || group="wheel"
+
+chgrp $group /dev/char/${device}0
+chmod $mode /dev/char/${device}0
93 output_packets.c
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2012, Boundary
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pcap/pcap.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "khial.h"
+
+void usage(void)
+{
+ fprintf(stderr, "./output_packets pcap_file_name\n");
+ return;
+}
+
+int main(int argc, char *argv[])
+{
+ size_t count = 0;
+ char errbuf[PCAP_ERRBUF_SIZE];
+ struct pcap_pkthdr header;
+ const u_char *packet;
+ pcap_t *pf;
+ int fd = 0;
+ FILE *file;
+ int direction = 0;
+
+ if (argc != 2) {
+ usage();
+ exit(1);
+ }
+
+ /* open a socket to the character device testpackets0.
+ * this device corresponds to the test0 interface.
+ */
+ fd = open("/dev/char/testpackets0", O_WRONLY);
+ if (fd < 0) {
+ perror("open failed");
+ exit(1);
+ }
+ file = fdopen(fd, "w+");
+
+ /* set the direction as TX */
+ direction = KHIAL_DIRECTION_TX;
+ ioctl(fd, KHIAL_SET_DIRECTION, &direction);
+
+ pf = pcap_open_offline(argv[1], errbuf);
+ if (!pf) {
+ fprintf(stderr, "Error opening pcap file %s: %s\n", argv[1], errbuf);
+ exit(1);
+ }
+
+ while (packet = pcap_next(pf, &header)) {
+ size_t bufsz = sizeof(header) + header.caplen;
+ char *buffer = malloc(bufsz);
+ if (!buffer) {
+ fprintf(stderr, "not enough memory!\n");
+ exit(1);
+ }
+
+ /* copy the header and the packet data into a buffer */
+ memcpy(buffer, &header, sizeof(header));
+ memcpy(buffer+sizeof(header), packet, header.caplen);
+
+ /* write the blob to the character device */
+ write(fd, buffer, bufsz);
+ fflush(file);
+ count++;
+ }
+
+ fprintf(stderr, "size of header: %zd\n", sizeof(header));
+ fprintf(stderr, "packet count: %zd\n", count);
+ close(fd);
+ return 0;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.