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.

af_packet.c 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (C) 2016 David Decotigny <ddecotig@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. #include <linux/if.h>
  35. #include <linux/if_ether.h>
  36. #include <linux/if_packet.h>
  37. #undef __GLIBC__
  38. #include <byteswap.h>
  39. /* linux-specifc syscall params */
  40. #define LINUX_AF_PACKET 17
  41. #define LINUX_SOCK_RAW 3
  42. #define LINUX_SIOCGIFINDEX 0x8933
  43. #define LINUX_SIOCGIFHWADDR 0x8927
  44. #define RX_BUF_SIZE 1536
  45. /** @file
  46. *
  47. * The AF_PACKET driver.
  48. *
  49. * Bind to an existing linux network interface.
  50. */
  51. struct af_packet_nic {
  52. /** Linux network interface name */
  53. char * ifname;
  54. /** Packet socket descriptor */
  55. int fd;
  56. /** ifindex */
  57. int ifindex;
  58. };
  59. /** Open the linux interface */
  60. static int af_packet_nic_open ( struct net_device * netdev )
  61. {
  62. struct af_packet_nic * nic = netdev->priv;
  63. struct sockaddr_ll socket_address;
  64. struct ifreq if_data;
  65. int ret;
  66. nic->fd = linux_socket(LINUX_AF_PACKET, LINUX_SOCK_RAW,
  67. htons(ETH_P_ALL));
  68. if (nic->fd < 0) {
  69. DBGC(nic, "af_packet %p socket(AF_PACKET) = %d (%s)\n",
  70. nic, nic->fd, linux_strerror(linux_errno));
  71. return nic->fd;
  72. }
  73. /* resolve ifindex of ifname */
  74. memset(&if_data, 0, sizeof(if_data));
  75. strncpy(if_data.ifr_name, nic->ifname, sizeof(if_data.ifr_name));
  76. ret = linux_ioctl(nic->fd, LINUX_SIOCGIFINDEX, &if_data);
  77. if (ret < 0) {
  78. DBGC(nic, "af_packet %p ioctl(SIOCGIFINDEX) = %d (%s)\n",
  79. nic, ret, linux_strerror(linux_errno));
  80. linux_close(nic->fd);
  81. return ret;
  82. }
  83. nic->ifindex = if_data.ifr_ifindex;
  84. /* bind to interface */
  85. memset(&socket_address, 0, sizeof(socket_address));
  86. socket_address.sll_family = LINUX_AF_PACKET;
  87. socket_address.sll_ifindex = nic->ifindex;
  88. socket_address.sll_protocol = htons(ETH_P_ALL);
  89. ret = linux_bind(nic->fd, (void *) &socket_address,
  90. sizeof(socket_address));
  91. if (ret == -1) {
  92. DBGC(nic, "af_packet %p bind() = %d (%s)\n",
  93. nic, ret, linux_strerror(linux_errno));
  94. linux_close(nic->fd);
  95. return ret;
  96. }
  97. /* Set nonblocking mode to make af_packet_nic_poll() easier */
  98. ret = linux_fcntl(nic->fd, F_SETFL, O_NONBLOCK);
  99. if (ret != 0) {
  100. DBGC(nic, "af_packet %p fcntl(%d, ...) = %d (%s)\n",
  101. nic, nic->fd, ret, linux_strerror(linux_errno));
  102. linux_close(nic->fd);
  103. return ret;
  104. }
  105. return 0;
  106. }
  107. /** Close the packet socket */
  108. static void af_packet_nic_close ( struct net_device *netdev )
  109. {
  110. struct af_packet_nic * nic = netdev->priv;
  111. linux_close(nic->fd);
  112. }
  113. /**
  114. * Transmit an ethernet packet.
  115. *
  116. * The packet can be written to the socket and marked as complete immediately.
  117. */
  118. static int af_packet_nic_transmit ( struct net_device *netdev,
  119. struct io_buffer *iobuf )
  120. {
  121. struct af_packet_nic * nic = netdev->priv;
  122. struct sockaddr_ll socket_address;
  123. const struct ethhdr * eh;
  124. int rc;
  125. memset(&socket_address, 0, sizeof(socket_address));
  126. socket_address.sll_family = LINUX_AF_PACKET;
  127. socket_address.sll_ifindex = nic->ifindex;
  128. socket_address.sll_halen = ETH_ALEN;
  129. eh = iobuf->data;
  130. memcpy(socket_address.sll_addr, eh->h_dest, ETH_ALEN);
  131. rc = linux_sendto(nic->fd, iobuf->data, iobuf->tail - iobuf->data,
  132. 0, (struct sockaddr *)&socket_address,
  133. sizeof(socket_address));
  134. DBGC2(nic, "af_packet %p wrote %d bytes\n", nic, rc);
  135. netdev_tx_complete(netdev, iobuf);
  136. return 0;
  137. }
  138. /** Poll for new packets */
  139. static void af_packet_nic_poll ( struct net_device *netdev )
  140. {
  141. struct af_packet_nic * nic = netdev->priv;
  142. struct pollfd pfd;
  143. struct io_buffer * iobuf;
  144. int r;
  145. pfd.fd = nic->fd;
  146. pfd.events = POLLIN;
  147. if (linux_poll(&pfd, 1, 0) == -1) {
  148. DBGC(nic, "af_packet %p poll failed (%s)\n",
  149. nic, linux_strerror(linux_errno));
  150. return;
  151. }
  152. if ((pfd.revents & POLLIN) == 0)
  153. return;
  154. /* At this point we know there is at least one new packet to be read */
  155. iobuf = alloc_iob(RX_BUF_SIZE);
  156. if (! iobuf)
  157. goto allocfail;
  158. while ((r = linux_read(nic->fd, iobuf->data, RX_BUF_SIZE)) > 0) {
  159. DBGC2(nic, "af_packet %p read %d bytes\n", nic, r);
  160. iob_put(iobuf, r);
  161. netdev_rx(netdev, iobuf);
  162. iobuf = alloc_iob(RX_BUF_SIZE);
  163. if (! iobuf)
  164. goto allocfail;
  165. }
  166. free_iob(iobuf);
  167. return;
  168. allocfail:
  169. DBGC(nic, "af_packet %p alloc_iob failed\n", nic);
  170. }
  171. /**
  172. * Set irq.
  173. *
  174. * Not used on linux, provide a dummy implementation.
  175. */
  176. static void af_packet_nic_irq ( struct net_device *netdev, int enable )
  177. {
  178. struct af_packet_nic *nic = netdev->priv;
  179. DBGC(nic, "af_packet %p irq enable = %d\n", nic, enable);
  180. }
  181. static int af_packet_update_properties ( struct net_device *netdev )
  182. {
  183. struct af_packet_nic *nic = netdev->priv;
  184. struct ifreq if_data;
  185. int ret;
  186. /* retrieve default MAC address */
  187. int fd = linux_socket(LINUX_AF_PACKET, LINUX_SOCK_RAW, 0);
  188. if (fd < 0) {
  189. DBGC(nic, "af_packet %p cannot create raw socket (%s)\n",
  190. nic, linux_strerror(linux_errno));
  191. return fd;
  192. }
  193. /* retrieve host's MAC address */
  194. memset(&if_data, 0, sizeof(if_data));
  195. strncpy(if_data.ifr_name, nic->ifname, sizeof(if_data.ifr_name));
  196. ret = linux_ioctl(fd, LINUX_SIOCGIFHWADDR, &if_data);
  197. if (ret < 0) {
  198. DBGC(nic, "af_packet %p cannot get mac addr (%s)\n",
  199. nic, linux_strerror(linux_errno));
  200. linux_close(fd);
  201. return ret;
  202. }
  203. linux_close(fd);
  204. /* struct sockaddr = { u16 family, u8 pad[14] (equiv. sa_data) }; */
  205. memcpy(netdev->ll_addr, if_data.ifr_hwaddr.pad, ETH_ALEN);
  206. return 0;
  207. }
  208. /** AF_PACKET operations */
  209. static struct net_device_operations af_packet_nic_operations = {
  210. .open = af_packet_nic_open,
  211. .close = af_packet_nic_close,
  212. .transmit = af_packet_nic_transmit,
  213. .poll = af_packet_nic_poll,
  214. .irq = af_packet_nic_irq,
  215. };
  216. /** Handle a device request for the af_packet driver */
  217. static int af_packet_nic_probe ( struct linux_device *device,
  218. struct linux_device_request *request )
  219. {
  220. struct linux_setting *if_setting;
  221. struct net_device *netdev;
  222. struct af_packet_nic *nic;
  223. int rc;
  224. netdev = alloc_etherdev(sizeof(*nic));
  225. if (! netdev)
  226. return -ENOMEM;
  227. netdev_init(netdev, &af_packet_nic_operations);
  228. nic = netdev->priv;
  229. linux_set_drvdata(device, netdev);
  230. netdev->dev = &device->dev;
  231. memset(nic, 0, sizeof(*nic));
  232. /* Look for the mandatory if setting */
  233. if_setting = linux_find_setting("if", &request->settings);
  234. /* No if setting */
  235. if (! if_setting) {
  236. printf("af_packet missing a mandatory if setting\n");
  237. rc = -EINVAL;
  238. goto err_settings;
  239. }
  240. nic->ifname = if_setting->value;
  241. snprintf ( device->dev.name, sizeof ( device->dev.name ), "%s",
  242. nic->ifname );
  243. device->dev.desc.bus_type = BUS_TYPE_TAP;
  244. af_packet_update_properties(netdev);
  245. if_setting->applied = 1;
  246. /* Apply rest of the settings */
  247. linux_apply_settings(&request->settings, &netdev->settings.settings);
  248. /* Register network device */
  249. if ((rc = register_netdev(netdev)) != 0)
  250. goto err_register;
  251. netdev_link_up(netdev);
  252. return 0;
  253. err_settings:
  254. unregister_netdev(netdev);
  255. err_register:
  256. netdev_nullify(netdev);
  257. netdev_put(netdev);
  258. return rc;
  259. }
  260. /** Remove the device */
  261. static void af_packet_nic_remove ( struct linux_device *device )
  262. {
  263. struct net_device *netdev = linux_get_drvdata(device);
  264. unregister_netdev(netdev);
  265. netdev_nullify(netdev);
  266. netdev_put(netdev);
  267. }
  268. /** AF_PACKET linux_driver */
  269. struct linux_driver af_packet_nic_driver __linux_driver = {
  270. .name = "af_packet",
  271. .probe = af_packet_nic_probe,
  272. .remove = af_packet_nic_remove,
  273. .can_probe = 1,
  274. };