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

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