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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 _SYS_SOCKET_H
  33. #define __GLIBC__ 2
  34. #include <linux/socket.h>
  35. #undef __GLIBC__
  36. #include <linux/if.h>
  37. #include <linux/if_ether.h>
  38. #include <linux/if_tun.h>
  39. #define RX_BUF_SIZE 1536
  40. /** @file
  41. *
  42. * The TAP driver.
  43. *
  44. * The TAP is a Virtual Ethernet network device.
  45. */
  46. struct tap_nic {
  47. /** Tap interface name */
  48. char * interface;
  49. /** File descriptor of the opened tap device */
  50. int fd;
  51. };
  52. /** Open the TAP device */
  53. static int tap_open(struct net_device * netdev)
  54. {
  55. struct tap_nic * nic = netdev->priv;
  56. struct ifreq ifr;
  57. int ret;
  58. nic->fd = linux_open("/dev/net/tun", O_RDWR);
  59. if (nic->fd < 0) {
  60. DBGC(nic, "tap %p open('/dev/net/tun') = %d (%s)\n", nic, nic->fd, linux_strerror(linux_errno));
  61. return nic->fd;
  62. }
  63. memset(&ifr, 0, sizeof(ifr));
  64. /* IFF_NO_PI for no extra packet information */
  65. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  66. strncpy(ifr.ifr_name, nic->interface, IFNAMSIZ);
  67. DBGC(nic, "tap %p interface = '%s'\n", nic, nic->interface);
  68. ret = linux_ioctl(nic->fd, TUNSETIFF, &ifr);
  69. if (ret != 0) {
  70. DBGC(nic, "tap %p ioctl(%d, ...) = %d (%s)\n", nic, nic->fd, ret, linux_strerror(linux_errno));
  71. linux_close(nic->fd);
  72. return ret;
  73. }
  74. /* Set nonblocking mode to make tap_poll easier */
  75. ret = linux_fcntl(nic->fd, F_SETFL, O_NONBLOCK);
  76. if (ret != 0) {
  77. DBGC(nic, "tap %p fcntl(%d, ...) = %d (%s)\n", nic, nic->fd, ret, linux_strerror(linux_errno));
  78. linux_close(nic->fd);
  79. return ret;
  80. }
  81. return 0;
  82. }
  83. /** Close the TAP device */
  84. static void tap_close(struct net_device *netdev)
  85. {
  86. struct tap_nic * nic = netdev->priv;
  87. linux_close(nic->fd);
  88. }
  89. /**
  90. * Transmit an ethernet packet.
  91. *
  92. * The packet can be written to the TAP device and marked as complete immediately.
  93. */
  94. static int tap_transmit(struct net_device *netdev, struct io_buffer *iobuf)
  95. {
  96. struct tap_nic * nic = netdev->priv;
  97. int rc;
  98. /* Pad and align packet */
  99. iob_pad(iobuf, ETH_ZLEN);
  100. rc = linux_write(nic->fd, iobuf->data, iobuf->tail - iobuf->data);
  101. DBGC2(nic, "tap %p wrote %d bytes\n", nic, rc);
  102. netdev_tx_complete(netdev, iobuf);
  103. return 0;
  104. }
  105. /** Poll for new packets */
  106. static void tap_poll(struct net_device *netdev)
  107. {
  108. struct tap_nic * nic = netdev->priv;
  109. struct pollfd pfd;
  110. struct io_buffer * iobuf;
  111. int r;
  112. pfd.fd = nic->fd;
  113. pfd.events = POLLIN;
  114. if (linux_poll(&pfd, 1, 0) == -1) {
  115. DBGC(nic, "tap %p poll failed (%s)\n", nic, linux_strerror(linux_errno));
  116. return;
  117. }
  118. if ((pfd.revents & POLLIN) == 0)
  119. return;
  120. /* At this point we know there is at least one new packet to be read */
  121. iobuf = alloc_iob(RX_BUF_SIZE);
  122. if (! iobuf)
  123. goto allocfail;
  124. while ((r = linux_read(nic->fd, iobuf->data, RX_BUF_SIZE)) > 0) {
  125. DBGC2(nic, "tap %p read %d bytes\n", nic, r);
  126. iob_put(iobuf, r);
  127. netdev_rx(netdev, iobuf);
  128. iobuf = alloc_iob(RX_BUF_SIZE);
  129. if (! iobuf)
  130. goto allocfail;
  131. }
  132. free_iob(iobuf);
  133. return;
  134. allocfail:
  135. DBGC(nic, "tap %p alloc_iob failed\n", nic);
  136. }
  137. /**
  138. * Set irq.
  139. *
  140. * Not used on linux, provide a dummy implementation.
  141. */
  142. static void tap_irq(struct net_device *netdev, int enable)
  143. {
  144. struct tap_nic *nic = netdev->priv;
  145. DBGC(nic, "tap %p irq enable = %d\n", nic, enable);
  146. }
  147. /** Tap operations */
  148. static struct net_device_operations tap_operations = {
  149. .open = tap_open,
  150. .close = tap_close,
  151. .transmit = tap_transmit,
  152. .poll = tap_poll,
  153. .irq = tap_irq,
  154. };
  155. /** Handle a device request for the tap driver */
  156. static int tap_probe(struct linux_device *device, struct linux_device_request *request)
  157. {
  158. struct linux_setting *if_setting;
  159. struct net_device *netdev;
  160. struct tap_nic *nic;
  161. int rc;
  162. netdev = alloc_etherdev(sizeof(*nic));
  163. if (! netdev)
  164. return -ENOMEM;
  165. netdev_init(netdev, &tap_operations);
  166. nic = netdev->priv;
  167. linux_set_drvdata(device, netdev);
  168. netdev->dev = &device->dev;
  169. memset(nic, 0, sizeof(*nic));
  170. /* Look for the mandatory if setting */
  171. if_setting = linux_find_setting("if", &request->settings);
  172. /* No if setting */
  173. if (! if_setting) {
  174. printf("tap missing a mandatory if setting\n");
  175. rc = -EINVAL;
  176. goto err_settings;
  177. }
  178. nic->interface = if_setting->value;
  179. snprintf ( device->dev.name, sizeof ( device->dev.name ), "%s",
  180. nic->interface );
  181. device->dev.desc.bus_type = BUS_TYPE_TAP;
  182. if_setting->applied = 1;
  183. /* Apply rest of the settings */
  184. linux_apply_settings(&request->settings, &netdev->settings.settings);
  185. /* Register network device */
  186. if ((rc = register_netdev(netdev)) != 0)
  187. goto err_register;
  188. netdev_link_up(netdev);
  189. return 0;
  190. err_settings:
  191. unregister_netdev(netdev);
  192. err_register:
  193. netdev_nullify(netdev);
  194. netdev_put(netdev);
  195. return rc;
  196. }
  197. /** Remove the device */
  198. static void tap_remove(struct linux_device *device)
  199. {
  200. struct net_device *netdev = linux_get_drvdata(device);
  201. unregister_netdev(netdev);
  202. netdev_nullify(netdev);
  203. netdev_put(netdev);
  204. }
  205. /** Tap linux_driver */
  206. struct linux_driver tap_driver __linux_driver = {
  207. .name = "tap",
  208. .probe = tap_probe,
  209. .remove = tap_remove,
  210. .can_probe = 1,
  211. };