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.

gdbudp.c 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (C) 2008 Stefan Hajnoczi <stefanha@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 Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <byteswap.h>
  27. #include <ipxe/iobuf.h>
  28. #include <ipxe/in.h>
  29. #include <ipxe/if_arp.h>
  30. #include <ipxe/if_ether.h>
  31. #include <ipxe/ip.h>
  32. #include <ipxe/udp.h>
  33. #include <ipxe/netdevice.h>
  34. #include <ipxe/nap.h>
  35. #include <ipxe/gdbstub.h>
  36. #include <ipxe/gdbudp.h>
  37. /** @file
  38. *
  39. * GDB over UDP transport
  40. *
  41. */
  42. enum {
  43. DEFAULT_PORT = 43770, /* UDP listen port */
  44. };
  45. struct gdb_transport udp_gdb_transport __gdb_transport;
  46. static struct net_device *netdev;
  47. static uint8_t dest_eth[ETH_ALEN];
  48. static struct sockaddr_in dest_addr;
  49. static struct sockaddr_in source_addr;
  50. static void gdbudp_ensure_netdev_open ( struct net_device *netdev ) {
  51. /* The device may have been closed between breakpoints */
  52. assert ( netdev );
  53. netdev_open ( netdev );
  54. /* Strictly speaking, we may need to close the device when leaving the interrupt handler */
  55. }
  56. static size_t gdbudp_recv ( char *buf, size_t len ) {
  57. struct io_buffer *iob;
  58. struct ethhdr *ethhdr;
  59. struct arphdr *arphdr;
  60. struct iphdr *iphdr;
  61. struct udp_header *udphdr;
  62. size_t payload_len;
  63. gdbudp_ensure_netdev_open ( netdev );
  64. for ( ; ; ) {
  65. netdev_poll ( netdev );
  66. while ( ( iob = netdev_rx_dequeue ( netdev ) ) != NULL ) {
  67. /* Ethernet header */
  68. if ( iob_len ( iob ) < sizeof ( *ethhdr ) ) {
  69. goto bad_packet;
  70. }
  71. ethhdr = iob->data;
  72. iob_pull ( iob, sizeof ( *ethhdr ) );
  73. /* Handle ARP requests so the client can find our MAC */
  74. if ( ethhdr->h_protocol == htons ( ETH_P_ARP ) ) {
  75. arphdr = iob->data;
  76. if ( iob_len ( iob ) < sizeof ( *arphdr ) + 2 * ( ETH_ALEN + sizeof ( struct in_addr ) ) ||
  77. arphdr->ar_hrd != htons ( ARPHRD_ETHER ) ||
  78. arphdr->ar_pro != htons ( ETH_P_IP ) ||
  79. arphdr->ar_hln != ETH_ALEN ||
  80. arphdr->ar_pln != sizeof ( struct in_addr ) ||
  81. arphdr->ar_op != htons ( ARPOP_REQUEST ) ||
  82. * ( uint32_t * ) arp_target_pa ( arphdr ) != source_addr.sin_addr.s_addr ) {
  83. goto bad_packet;
  84. }
  85. /* Generate an ARP reply */
  86. arphdr->ar_op = htons ( ARPOP_REPLY );
  87. memswap ( arp_sender_pa ( arphdr ), arp_target_pa ( arphdr ), sizeof ( struct in_addr ) );
  88. memcpy ( arp_target_ha ( arphdr ), arp_sender_ha ( arphdr ), ETH_ALEN );
  89. memcpy ( arp_sender_ha ( arphdr ), netdev->ll_addr, ETH_ALEN );
  90. /* Fix up ethernet header */
  91. ethhdr = iob_push ( iob, sizeof ( *ethhdr ) );
  92. memcpy ( ethhdr->h_dest, ethhdr->h_source, ETH_ALEN );
  93. memcpy ( ethhdr->h_source, netdev->ll_addr, ETH_ALEN );
  94. netdev_tx ( netdev, iob );
  95. continue; /* no need to free iob */
  96. }
  97. if ( ethhdr->h_protocol != htons ( ETH_P_IP ) ) {
  98. goto bad_packet;
  99. }
  100. /* IP header */
  101. if ( iob_len ( iob ) < sizeof ( *iphdr ) ) {
  102. goto bad_packet;
  103. }
  104. iphdr = iob->data;
  105. iob_pull ( iob, sizeof ( *iphdr ) );
  106. if ( iphdr->protocol != IP_UDP || iphdr->dest.s_addr != source_addr.sin_addr.s_addr ) {
  107. goto bad_packet;
  108. }
  109. /* UDP header */
  110. if ( iob_len ( iob ) < sizeof ( *udphdr ) ) {
  111. goto bad_packet;
  112. }
  113. udphdr = iob->data;
  114. if ( udphdr->dest != source_addr.sin_port ) {
  115. goto bad_packet;
  116. }
  117. /* Learn the remote connection details */
  118. memcpy ( dest_eth, ethhdr->h_source, ETH_ALEN );
  119. dest_addr.sin_addr.s_addr = iphdr->src.s_addr;
  120. dest_addr.sin_port = udphdr->src;
  121. /* Payload */
  122. payload_len = ntohs ( udphdr->len );
  123. if ( payload_len < sizeof ( *udphdr ) || payload_len > iob_len ( iob ) ) {
  124. goto bad_packet;
  125. }
  126. payload_len -= sizeof ( *udphdr );
  127. iob_pull ( iob, sizeof ( *udphdr ) );
  128. if ( payload_len > len ) {
  129. goto bad_packet;
  130. }
  131. memcpy ( buf, iob->data, payload_len );
  132. free_iob ( iob );
  133. return payload_len;
  134. bad_packet:
  135. free_iob ( iob );
  136. }
  137. cpu_nap();
  138. }
  139. }
  140. static void gdbudp_send ( const char *buf, size_t len ) {
  141. struct io_buffer *iob;
  142. struct ethhdr *ethhdr;
  143. struct iphdr *iphdr;
  144. struct udp_header *udphdr;
  145. /* Check that we are connected */
  146. if ( dest_addr.sin_port == 0 ) {
  147. return;
  148. }
  149. gdbudp_ensure_netdev_open ( netdev );
  150. iob = alloc_iob ( sizeof ( *ethhdr ) + sizeof ( *iphdr ) + sizeof ( *udphdr ) + len );
  151. if ( !iob ) {
  152. return;
  153. }
  154. /* Payload */
  155. iob_reserve ( iob, sizeof ( *ethhdr ) + sizeof ( *iphdr ) + sizeof ( *udphdr ) );
  156. memcpy ( iob_put ( iob, len ), buf, len );
  157. /* UDP header */
  158. udphdr = iob_push ( iob, sizeof ( *udphdr ) );
  159. udphdr->src = source_addr.sin_port;
  160. udphdr->dest = dest_addr.sin_port;
  161. udphdr->len = htons ( iob_len ( iob ) );
  162. udphdr->chksum = 0; /* optional and we are not using it */
  163. /* IP header */
  164. iphdr = iob_push ( iob, sizeof ( *iphdr ) );
  165. memset ( iphdr, 0, sizeof ( *iphdr ) );
  166. iphdr->verhdrlen = ( IP_VER | ( sizeof ( *iphdr ) / 4 ) );
  167. iphdr->service = IP_TOS;
  168. iphdr->len = htons ( iob_len ( iob ) );
  169. iphdr->ttl = IP_TTL;
  170. iphdr->protocol = IP_UDP;
  171. iphdr->dest.s_addr = dest_addr.sin_addr.s_addr;
  172. iphdr->src.s_addr = source_addr.sin_addr.s_addr;
  173. iphdr->chksum = tcpip_chksum ( iphdr, sizeof ( *iphdr ) );
  174. /* Ethernet header */
  175. ethhdr = iob_push ( iob, sizeof ( *ethhdr ) );
  176. memcpy ( ethhdr->h_dest, dest_eth, ETH_ALEN );
  177. memcpy ( ethhdr->h_source, netdev->ll_addr, ETH_ALEN );
  178. ethhdr->h_protocol = htons ( ETH_P_IP );
  179. netdev_tx ( netdev, iob );
  180. }
  181. struct gdb_transport *gdbudp_configure ( const char *name, struct sockaddr_in *addr ) {
  182. struct settings *settings;
  183. /* Release old network device */
  184. netdev_put ( netdev );
  185. netdev = find_netdev ( name );
  186. if ( !netdev ) {
  187. return NULL;
  188. }
  189. /* Hold network device */
  190. netdev_get ( netdev );
  191. /* Source UDP port */
  192. source_addr.sin_port = ( addr && addr->sin_port ) ? addr->sin_port : htons ( DEFAULT_PORT );
  193. /* Source IP address */
  194. if ( addr && addr->sin_addr.s_addr ) {
  195. source_addr.sin_addr.s_addr = addr->sin_addr.s_addr;
  196. } else {
  197. settings = netdev_settings ( netdev );
  198. fetch_ipv4_setting ( settings, &ip_setting, &source_addr.sin_addr );
  199. if ( source_addr.sin_addr.s_addr == 0 ) {
  200. netdev_put ( netdev );
  201. netdev = NULL;
  202. return NULL;
  203. }
  204. }
  205. return &udp_gdb_transport;
  206. }
  207. static int gdbudp_init ( int argc, char **argv ) {
  208. if ( argc != 1 ) {
  209. printf ( "udp: missing <interface> argument\n" );
  210. return 1;
  211. }
  212. if ( !gdbudp_configure ( argv[0], NULL ) ) {
  213. printf ( "%s: device does not exist or has no IP address\n", argv[0] );
  214. return 1;
  215. }
  216. return 0;
  217. }
  218. struct gdb_transport udp_gdb_transport __gdb_transport = {
  219. .name = "udp",
  220. .init = gdbudp_init,
  221. .send = gdbudp_send,
  222. .recv = gdbudp_recv,
  223. };