pcap_kern.c 2.5 KB

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