Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

gdbudp.c 7.0KB

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