Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 2 | * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL. |
| 4 | */ |
| 5 | |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 6 | #include <linux/init.h> |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 7 | #include <linux/netdevice.h> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 8 | #include <net_kern.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include "pcap_user.h" |
| 10 | |
| 11 | struct pcap_init { |
| 12 | char *host_if; |
| 13 | int promisc; |
| 14 | int optimize; |
| 15 | char *filter; |
| 16 | }; |
| 17 | |
| 18 | void pcap_init(struct net_device *dev, void *data) |
| 19 | { |
| 20 | struct uml_net_private *pri; |
| 21 | struct pcap_data *ppri; |
| 22 | struct pcap_init *init = data; |
| 23 | |
Wang Chen | 17c324f | 2008-12-04 15:07:33 -0800 | [diff] [blame] | 24 | pri = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | ppri = (struct pcap_data *) pri->user; |
| 26 | ppri->host_if = init->host_if; |
| 27 | ppri->promisc = init->promisc; |
| 28 | ppri->optimize = init->optimize; |
| 29 | ppri->filter = init->filter; |
Jeff Dike | 7d98230 | 2007-05-08 00:35:04 -0700 | [diff] [blame] | 30 | |
| 31 | printk("pcap backend, host interface %s\n", ppri->host_if); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Jeff Dike | b53f35a | 2007-10-16 01:27:31 -0700 | [diff] [blame] | 34 | static int pcap_read(int fd, struct sk_buff *skb, struct uml_net_private *lp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
Jeff Dike | b53f35a | 2007-10-16 01:27:31 -0700 | [diff] [blame] | 36 | return pcap_user_read(fd, skb_mac_header(skb), |
| 37 | skb->dev->mtu + ETH_HEADER_OTHER, |
Jeff Dike | 7d98230 | 2007-05-08 00:35:04 -0700 | [diff] [blame] | 38 | (struct pcap_data *) &lp->user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Jeff Dike | b53f35a | 2007-10-16 01:27:31 -0700 | [diff] [blame] | 41 | static int pcap_write(int fd, struct sk_buff *skb, struct uml_net_private *lp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
Jeff Dike | 7d98230 | 2007-05-08 00:35:04 -0700 | [diff] [blame] | 43 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 46 | static const struct net_kern_info pcap_kern_info = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | .init = pcap_init, |
| 48 | .protocol = eth_protocol, |
| 49 | .read = pcap_read, |
| 50 | .write = pcap_write, |
| 51 | }; |
| 52 | |
| 53 | int pcap_setup(char *str, char **mac_out, void *data) |
| 54 | { |
| 55 | struct pcap_init *init = data; |
| 56 | char *remain, *host_if = NULL, *options[2] = { NULL, NULL }; |
| 57 | int i; |
| 58 | |
| 59 | *init = ((struct pcap_init) |
| 60 | { .host_if = "eth0", |
| 61 | .promisc = 1, |
| 62 | .optimize = 0, |
| 63 | .filter = NULL }); |
| 64 | |
Jeff Dike | 7d98230 | 2007-05-08 00:35:04 -0700 | [diff] [blame] | 65 | remain = split_if_spec(str, &host_if, &init->filter, |
Jeff Dike | 0ba34e1 | 2007-05-08 00:35:06 -0700 | [diff] [blame] | 66 | &options[0], &options[1], mac_out, NULL); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 67 | if (remain != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | printk(KERN_ERR "pcap_setup - Extra garbage on " |
| 69 | "specification : '%s'\n", remain); |
Jeff Dike | 7d98230 | 2007-05-08 00:35:04 -0700 | [diff] [blame] | 70 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 73 | if (host_if != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | init->host_if = host_if; |
| 75 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 76 | for (i = 0; i < ARRAY_SIZE(options); i++) { |
| 77 | if (options[i] == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | continue; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 79 | if (!strcmp(options[i], "promisc")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | init->promisc = 1; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 81 | else if (!strcmp(options[i], "nopromisc")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | init->promisc = 0; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 83 | else if (!strcmp(options[i], "optimize")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | init->optimize = 1; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 85 | else if (!strcmp(options[i], "nooptimize")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | init->optimize = 0; |
Jeff Dike | 7d98230 | 2007-05-08 00:35:04 -0700 | [diff] [blame] | 87 | else { |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 88 | printk(KERN_ERR "pcap_setup : bad option - '%s'\n", |
| 89 | options[i]); |
Jeff Dike | 7d98230 | 2007-05-08 00:35:04 -0700 | [diff] [blame] | 90 | return 0; |
| 91 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Jeff Dike | 7d98230 | 2007-05-08 00:35:04 -0700 | [diff] [blame] | 94 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static struct transport pcap_transport = { |
| 98 | .list = LIST_HEAD_INIT(pcap_transport.list), |
| 99 | .name = "pcap", |
| 100 | .setup = pcap_setup, |
| 101 | .user = &pcap_user_info, |
| 102 | .kern = &pcap_kern_info, |
| 103 | .private_size = sizeof(struct pcap_data), |
| 104 | .setup_size = sizeof(struct pcap_init), |
| 105 | }; |
| 106 | |
| 107 | static int register_pcap(void) |
| 108 | { |
| 109 | register_transport(&pcap_transport); |
Jeff Dike | f4c57a7 | 2006-03-31 02:30:10 -0800 | [diff] [blame] | 110 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Jeff Dike | 8210fd2 | 2006-12-06 20:34:55 -0800 | [diff] [blame] | 113 | late_initcall(register_pcap); |