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

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