You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tap.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (C) 2010 Piotr Jaroszyński <p.jaroszynski@gmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <linux_api.h>
  22. #include <ipxe/list.h>
  23. #include <ipxe/linux.h>
  24. #include <ipxe/malloc.h>
  25. #include <ipxe/device.h>
  26. #include <ipxe/netdevice.h>
  27. #include <ipxe/iobuf.h>
  28. #include <ipxe/ethernet.h>
  29. #include <ipxe/settings.h>
  30. #include <ipxe/socket.h>
  31. /* This hack prevents pre-2.6.32 headers from redefining struct sockaddr */
  32. #define __GLIBC__ 2
  33. #include <linux/socket.h>
  34. #undef __GLIBC__
  35. #include <linux/if.h>
  36. #include <linux/if_ether.h>
  37. #include <linux/if_tun.h>
  38. #define RX_BUF_SIZE 1536
  39. /** @file
  40. *
  41. * The TAP driver.
  42. *
  43. * The TAP is a Virtual Ethernet network device.
  44. */
  45. struct tap_nic {
  46. /** Tap interface name */
  47. char * interface;
  48. /** File descriptor of the opened tap device */
  49. int fd;
  50. };
  51. /** Open the TAP device */
  52. static int tap_open(struct net_device * netdev)
  53. {
  54. struct tap_nic * nic = netdev->priv;
  55. struct ifreq ifr;
  56. int ret;
  57. nic->fd = linux_open("/dev/net/tun", O_RDWR);
  58. if (nic->fd < 0) {
  59. DBGC(nic, "tap %p open('/dev/net/tun') = %d (%s)\n", nic, nic->fd, linux_strerror(linux_errno));
  60. return nic->fd;
  61. }
  62. memset(&ifr, 0, sizeof(ifr));
  63. /* IFF_NO_PI for no extra packet information */
  64. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  65. strncpy(ifr.ifr_name, nic->interface, IFNAMSIZ);
  66. DBGC(nic, "tap %p interface = '%s'\n", nic, nic->interface);
  67. ret = linux_ioctl(nic->fd, TUNSETIFF, &ifr);
  68. if (ret != 0) {
  69. DBGC(nic, "tap %p ioctl(%d, ...) = %d (%s)\n", nic, nic->fd, ret, linux_strerror(linux_errno));
  70. linux_close(nic->fd);
  71. return ret;
  72. }
  73. /* Set nonblocking mode to make tap_poll easier */
  74. ret = linux_fcntl(nic->fd, F_SETFL, O_NONBLOCK);
  75. if (ret != 0) {
  76. DBGC(nic, "tap %p fcntl(%d, ...) = %d (%s)\n", nic, nic->fd, ret, linux_strerror(linux_errno));
  77. linux_close(nic->fd);
  78. return ret;
  79. }
  80. return 0;
  81. }
  82. /** Close the TAP device */
  83. static void tap_close(struct net_device *netdev)
  84. {
  85. struct tap_nic * nic = netdev->priv;
  86. linux_close(nic->fd);
  87. }
  88. /**
  89. * Transmit an ethernet packet.
  90. *
  91. * The packet can be written to the TAP device and marked as complete immediately.
  92. */
  93. static int tap_transmit(struct net_device *netdev, struct io_buffer *iobuf)
  94. {
  95. struct tap_nic * nic = netdev->priv;
  96. int rc;
  97. /* Pad and align packet */
  98. iob_pad(iobuf, ETH_ZLEN);
  99. rc = linux_write(nic->fd, iobuf->data, iobuf->tail - iobuf->data);
  100. DBGC2(nic, "tap %p wrote %d bytes\n", nic, rc);
  101. netdev_tx_complete(netdev, iobuf);
  102. return 0;
  103. }
  104. /** Poll for new packets */
  105. static void tap_poll(struct net_device *netdev)
  106. {
  107. struct tap_nic * nic = netdev->priv;
  108. struct pollfd pfd;
  109. struct io_buffer * iobuf;
  110. int r;
  111. pfd.fd = nic->fd;
  112. pfd.events = POLLIN;
  113. if (linux_poll(&pfd, 1, 0) == -1) {
  114. DBGC(nic, "tap %p poll failed (%s)\n", nic, linux_strerror(linux_errno));
  115. return;
  116. }
  117. if ((pfd.revents & POLLIN) == 0)
  118. return;
  119. /* At this point we know there is at least one new packet to be read */
  120. iobuf = alloc_iob(RX_BUF_SIZE);
  121. if (! iobuf)
  122. goto allocfail;
  123. while ((r = linux_read(nic->fd, iobuf->data, RX_BUF_SIZE)) > 0) {
  124. DBGC2(nic, "tap %p read %d bytes\n", nic, r);
  125. iob_put(iobuf, r);
  126. netdev_rx(netdev, iobuf);
  127. iobuf = alloc_iob(RX_BUF_SIZE);
  128. if (! iobuf)
  129. goto allocfail;
  130. }
  131. free_iob(iobuf);
  132. return;
  133. allocfail:
  134. DBGC(nic, "tap %p alloc_iob failed\n", nic);
  135. }
  136. /**
  137. * Set irq.
  138. *
  139. * Not used on linux, provide a dummy implementation.
  140. */
  141. static void tap_irq(struct net_device *netdev, int enable)
  142. {
  143. struct tap_nic *nic = netdev->priv;
  144. DBGC(nic, "tap %p irq enable = %d\n", nic, enable);
  145. }
  146. /** Tap operations */
  147. static struct net_device_operations tap_operations = {
  148. .open = tap_open,
  149. .close = tap_close,
  150. .transmit = tap_transmit,
  151. .poll = tap_poll,
  152. .irq = tap_irq,
  153. };
  154. /** Handle a device request for the tap driver */
  155. static int tap_probe(struct linux_device *device, struct linux_device_request *request)
  156. {
  157. struct linux_setting *if_setting;
  158. struct net_device *netdev;
  159. struct tap_nic *nic;
  160. int rc;
  161. netdev = alloc_etherdev(sizeof(*nic));
  162. if (! netdev)
  163. return -ENOMEM;
  164. netdev_init(netdev, &tap_operations);
  165. nic = netdev->priv;
  166. linux_set_drvdata(device, netdev);
  167. netdev->dev = &device->dev;
  168. memset(nic, 0, sizeof(*nic));
  169. /* Look for the mandatory if setting */
  170. if_setting = linux_find_setting("if", &request->settings);
  171. /* No if setting */
  172. if (! if_setting) {
  173. printf("tap missing a mandatory if setting\n");
  174. rc = -EINVAL;
  175. goto err_settings;
  176. }
  177. nic->interface = if_setting->value;
  178. snprintf ( device->dev.name, sizeof ( device->dev.name ), "%s",
  179. nic->interface );
  180. device->dev.desc.bus_type = BUS_TYPE_TAP;
  181. if_setting->applied = 1;
  182. /* Apply rest of the settings */
  183. linux_apply_settings(&request->settings, &netdev->settings.settings);
  184. /* Register network device */
  185. if ((rc = register_netdev(netdev)) != 0)
  186. goto err_register;
  187. netdev_link_up(netdev);
  188. return 0;
  189. err_settings:
  190. unregister_netdev(netdev);
  191. err_register:
  192. netdev_nullify(netdev);
  193. netdev_put(netdev);
  194. return rc;
  195. }
  196. /** Remove the device */
  197. static void tap_remove(struct linux_device *device)
  198. {
  199. struct net_device *netdev = linux_get_drvdata(device);
  200. unregister_netdev(netdev);
  201. netdev_nullify(netdev);
  202. netdev_put(netdev);
  203. }
  204. /** Tap linux_driver */
  205. struct linux_driver tap_driver __linux_driver = {
  206. .name = "tap",
  207. .probe = tap_probe,
  208. .remove = tap_remove,
  209. .can_probe = 1,
  210. };