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.1KB

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