2 * Copyright (C) 2008 Stefan Hajnoczi <stefanha@gmail.com>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
25 #include <ipxe/iobuf.h>
27 #include <ipxe/if_arp.h>
28 #include <ipxe/if_ether.h>
31 #include <ipxe/netdevice.h>
33 #include <ipxe/gdbstub.h>
34 #include <ipxe/gdbudp.h>
38 * GDB over UDP transport
43 DEFAULT_PORT = 43770, /* UDP listen port */
46 struct gdb_transport udp_gdb_transport __gdb_transport;
48 static struct net_device *netdev;
49 static uint8_t dest_eth[ETH_ALEN];
50 static struct sockaddr_in dest_addr;
51 static struct sockaddr_in source_addr;
53 static void gdbudp_ensure_netdev_open ( struct net_device *netdev ) {
54 /* The device may have been closed between breakpoints */
56 netdev_open ( netdev );
58 /* Strictly speaking, we may need to close the device when leaving the interrupt handler */
61 static size_t gdbudp_recv ( char *buf, size_t len ) {
62 struct io_buffer *iob;
63 struct ethhdr *ethhdr;
64 struct arphdr *arphdr;
66 struct udp_header *udphdr;
69 gdbudp_ensure_netdev_open ( netdev );
72 netdev_poll ( netdev );
73 while ( ( iob = netdev_rx_dequeue ( netdev ) ) != NULL ) {
75 if ( iob_len ( iob ) < sizeof ( *ethhdr ) ) {
79 iob_pull ( iob, sizeof ( *ethhdr ) );
81 /* Handle ARP requests so the client can find our MAC */
82 if ( ethhdr->h_protocol == htons ( ETH_P_ARP ) ) {
84 if ( iob_len ( iob ) < sizeof ( *arphdr ) + 2 * ( ETH_ALEN + sizeof ( struct in_addr ) ) ||
85 arphdr->ar_hrd != htons ( ARPHRD_ETHER ) ||
86 arphdr->ar_pro != htons ( ETH_P_IP ) ||
87 arphdr->ar_hln != ETH_ALEN ||
88 arphdr->ar_pln != sizeof ( struct in_addr ) ||
89 arphdr->ar_op != htons ( ARPOP_REQUEST ) ||
90 * ( uint32_t * ) arp_target_pa ( arphdr ) != source_addr.sin_addr.s_addr ) {
94 /* Generate an ARP reply */
95 arphdr->ar_op = htons ( ARPOP_REPLY );
96 memswap ( arp_sender_pa ( arphdr ), arp_target_pa ( arphdr ), sizeof ( struct in_addr ) );
97 memcpy ( arp_target_ha ( arphdr ), arp_sender_ha ( arphdr ), ETH_ALEN );
98 memcpy ( arp_sender_ha ( arphdr ), netdev->ll_addr, ETH_ALEN );
100 /* Fix up ethernet header */
101 ethhdr = iob_push ( iob, sizeof ( *ethhdr ) );
102 memcpy ( ethhdr->h_dest, ethhdr->h_source, ETH_ALEN );
103 memcpy ( ethhdr->h_source, netdev->ll_addr, ETH_ALEN );
105 netdev_tx ( netdev, iob );
106 continue; /* no need to free iob */
109 if ( ethhdr->h_protocol != htons ( ETH_P_IP ) ) {
114 if ( iob_len ( iob ) < sizeof ( *iphdr ) ) {
118 iob_pull ( iob, sizeof ( *iphdr ) );
119 if ( iphdr->protocol != IP_UDP || iphdr->dest.s_addr != source_addr.sin_addr.s_addr ) {
124 if ( iob_len ( iob ) < sizeof ( *udphdr ) ) {
128 if ( udphdr->dest != source_addr.sin_port ) {
132 /* Learn the remote connection details */
133 memcpy ( dest_eth, ethhdr->h_source, ETH_ALEN );
134 dest_addr.sin_addr.s_addr = iphdr->src.s_addr;
135 dest_addr.sin_port = udphdr->src;
138 payload_len = ntohs ( udphdr->len );
139 if ( payload_len < sizeof ( *udphdr ) || payload_len > iob_len ( iob ) ) {
142 payload_len -= sizeof ( *udphdr );
143 iob_pull ( iob, sizeof ( *udphdr ) );
144 if ( payload_len > len ) {
147 memcpy ( buf, iob->data, payload_len );
159 static void gdbudp_send ( const char *buf, size_t len ) {
160 struct io_buffer *iob;
161 struct ethhdr *ethhdr;
163 struct udp_header *udphdr;
165 /* Check that we are connected */
166 if ( dest_addr.sin_port == 0 ) {
170 gdbudp_ensure_netdev_open ( netdev );
172 iob = alloc_iob ( sizeof ( *ethhdr ) + sizeof ( *iphdr ) + sizeof ( *udphdr ) + len );
178 iob_reserve ( iob, sizeof ( *ethhdr ) + sizeof ( *iphdr ) + sizeof ( *udphdr ) );
179 memcpy ( iob_put ( iob, len ), buf, len );
182 udphdr = iob_push ( iob, sizeof ( *udphdr ) );
183 udphdr->src = source_addr.sin_port;
184 udphdr->dest = dest_addr.sin_port;
185 udphdr->len = htons ( iob_len ( iob ) );
186 udphdr->chksum = 0; /* optional and we are not using it */
189 iphdr = iob_push ( iob, sizeof ( *iphdr ) );
190 memset ( iphdr, 0, sizeof ( *iphdr ) );
191 iphdr->verhdrlen = ( IP_VER | ( sizeof ( *iphdr ) / 4 ) );
192 iphdr->service = IP_TOS;
193 iphdr->len = htons ( iob_len ( iob ) );
195 iphdr->protocol = IP_UDP;
196 iphdr->dest.s_addr = dest_addr.sin_addr.s_addr;
197 iphdr->src.s_addr = source_addr.sin_addr.s_addr;
198 iphdr->chksum = tcpip_chksum ( iphdr, sizeof ( *iphdr ) );
200 /* Ethernet header */
201 ethhdr = iob_push ( iob, sizeof ( *ethhdr ) );
202 memcpy ( ethhdr->h_dest, dest_eth, ETH_ALEN );
203 memcpy ( ethhdr->h_source, netdev->ll_addr, ETH_ALEN );
204 ethhdr->h_protocol = htons ( ETH_P_IP );
206 netdev_tx ( netdev, iob );
209 struct gdb_transport *gdbudp_configure ( const char *name, struct sockaddr_in *addr ) {
210 struct settings *settings;
212 /* Release old network device */
213 netdev_put ( netdev );
215 netdev = find_netdev ( name );
220 /* Hold network device */
221 netdev_get ( netdev );
223 /* Source UDP port */
224 source_addr.sin_port = ( addr && addr->sin_port ) ? addr->sin_port : htons ( DEFAULT_PORT );
226 /* Source IP address */
227 if ( addr && addr->sin_addr.s_addr ) {
228 source_addr.sin_addr.s_addr = addr->sin_addr.s_addr;
230 settings = netdev_settings ( netdev );
231 fetch_ipv4_setting ( settings, &ip_setting, &source_addr.sin_addr );
232 if ( source_addr.sin_addr.s_addr == 0 ) {
233 netdev_put ( netdev );
239 return &udp_gdb_transport;
242 static int gdbudp_init ( int argc, char **argv ) {
244 printf ( "udp: missing <interface> argument\n" );
248 if ( !gdbudp_configure ( argv[0], NULL ) ) {
249 printf ( "%s: device does not exist or has no IP address\n", argv[0] );
255 struct gdb_transport udp_gdb_transport __gdb_transport = {