Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / core / gdbudp.c
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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <byteswap.h>
25 #include <ipxe/iobuf.h>
26 #include <ipxe/in.h>
27 #include <ipxe/if_arp.h>
28 #include <ipxe/if_ether.h>
29 #include <ipxe/ip.h>
30 #include <ipxe/udp.h>
31 #include <ipxe/netdevice.h>
32 #include <ipxe/nap.h>
33 #include <ipxe/gdbstub.h>
34 #include <ipxe/gdbudp.h>
35
36 /** @file
37  *
38  * GDB over UDP transport
39  *
40  */
41
42 enum {
43         DEFAULT_PORT = 43770, /* UDP listen port */
44 };
45
46 struct gdb_transport udp_gdb_transport __gdb_transport;
47
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;
52
53 static void gdbudp_ensure_netdev_open ( struct net_device *netdev ) {
54         /* The device may have been closed between breakpoints */
55         assert ( netdev );
56         netdev_open ( netdev );
57
58         /* Strictly speaking, we may need to close the device when leaving the interrupt handler */
59 }
60
61 static size_t gdbudp_recv ( char *buf, size_t len ) {
62         struct io_buffer *iob;
63         struct ethhdr *ethhdr;
64         struct arphdr *arphdr;
65         struct iphdr *iphdr;
66         struct udp_header *udphdr;
67         size_t payload_len;
68
69         gdbudp_ensure_netdev_open ( netdev );
70
71         for ( ; ; ) {
72                 netdev_poll ( netdev );
73                 while ( ( iob = netdev_rx_dequeue ( netdev ) ) != NULL ) {
74                         /* Ethernet header */
75                         if ( iob_len ( iob ) < sizeof ( *ethhdr ) ) {
76                                 goto bad_packet;
77                         }
78                         ethhdr = iob->data;
79                         iob_pull ( iob, sizeof ( *ethhdr ) );
80
81                         /* Handle ARP requests so the client can find our MAC */
82                         if ( ethhdr->h_protocol == htons ( ETH_P_ARP ) ) {
83                                 arphdr = iob->data;
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 ) {
91                                         goto bad_packet;
92                                 }
93
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 );
99
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 );
104
105                                 netdev_tx ( netdev, iob );
106                                 continue; /* no need to free iob */
107                         }
108
109                         if ( ethhdr->h_protocol != htons ( ETH_P_IP ) ) {
110                                 goto bad_packet;
111                         }
112
113                         /* IP header */
114                         if ( iob_len ( iob ) < sizeof ( *iphdr ) ) {
115                                 goto bad_packet;
116                         }
117                         iphdr = iob->data;
118                         iob_pull ( iob, sizeof ( *iphdr ) );
119                         if ( iphdr->protocol != IP_UDP || iphdr->dest.s_addr != source_addr.sin_addr.s_addr ) {
120                                 goto bad_packet;
121                         }
122
123                         /* UDP header */
124                         if ( iob_len ( iob ) < sizeof ( *udphdr ) ) {
125                                 goto bad_packet;
126                         }
127                         udphdr = iob->data;
128                         if ( udphdr->dest != source_addr.sin_port ) {
129                                 goto bad_packet;
130                         }
131
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;
136
137                         /* Payload */
138                         payload_len = ntohs ( udphdr->len );
139                         if ( payload_len < sizeof ( *udphdr ) || payload_len > iob_len ( iob ) ) {
140                                 goto bad_packet;
141                         }
142                         payload_len -= sizeof ( *udphdr );
143                         iob_pull ( iob, sizeof ( *udphdr ) );
144                         if ( payload_len > len ) {
145                                 goto bad_packet;
146                         }
147                         memcpy ( buf, iob->data, payload_len );
148
149                         free_iob ( iob );
150                         return payload_len;
151
152 bad_packet:
153                         free_iob ( iob );
154                 }
155                 cpu_nap();
156         }
157 }
158
159 static void gdbudp_send ( const char *buf, size_t len ) {
160         struct io_buffer *iob;
161         struct ethhdr *ethhdr;
162         struct iphdr *iphdr;
163         struct udp_header *udphdr;
164
165         /* Check that we are connected */
166         if ( dest_addr.sin_port == 0 ) {
167                 return;
168         }
169
170         gdbudp_ensure_netdev_open ( netdev );
171
172         iob = alloc_iob ( sizeof ( *ethhdr ) + sizeof ( *iphdr ) + sizeof ( *udphdr ) + len );
173         if ( !iob ) {
174                 return;
175         }
176
177         /* Payload */
178         iob_reserve ( iob, sizeof ( *ethhdr ) + sizeof ( *iphdr ) + sizeof ( *udphdr ) );
179         memcpy ( iob_put ( iob, len ), buf, len );
180
181         /* UDP header */
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 */
187
188         /* IP header */
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 ) ); 
194         iphdr->ttl = IP_TTL;
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 ) );
199
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 );
205
206         netdev_tx ( netdev, iob );
207 }
208
209 struct gdb_transport *gdbudp_configure ( const char *name, struct sockaddr_in *addr ) {
210         struct settings *settings;
211
212         /* Release old network device */
213         netdev_put ( netdev );
214
215         netdev = find_netdev ( name );
216         if ( !netdev ) {
217                 return NULL;
218         }
219
220         /* Hold network device */
221         netdev_get ( netdev );
222
223         /* Source UDP port */
224         source_addr.sin_port = ( addr && addr->sin_port ) ? addr->sin_port : htons ( DEFAULT_PORT );
225
226         /* Source IP address */
227         if ( addr && addr->sin_addr.s_addr ) {
228                 source_addr.sin_addr.s_addr = addr->sin_addr.s_addr;
229         } else {
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 );
234                         netdev = NULL;
235                         return NULL;
236                 }
237         }
238
239         return &udp_gdb_transport;
240 }
241
242 static int gdbudp_init ( int argc, char **argv ) {
243         if ( argc != 1 ) {
244                 printf ( "udp: missing <interface> argument\n" );
245                 return 1;
246         }
247
248         if ( !gdbudp_configure ( argv[0], NULL ) ) {
249                 printf ( "%s: device does not exist or has no IP address\n", argv[0] );
250                 return 1;
251         }
252         return 0;
253 }
254
255 struct gdb_transport udp_gdb_transport __gdb_transport = {
256         .name = "udp",
257         .init = gdbudp_init,
258         .send = gdbudp_send,
259         .recv = gdbudp_recv,
260 };