Make BOOTP work with slirp
cover Make BOOTP work with slirp
The following patch series allows for BOOTP to be used with slirp. The original patch was written by Erwan Velu.
With the full series, you can do:
qemu -hda /path/to/disk.img -boot n -tftp ~/tftpdir -bootp /pxelinux.0
And the guest will boot from ~/tftpdir/pxelinux.0.
Regards,
Anthony Liguori
PATCH 1/3 Add OACK support to slirp TFTP server
Specifically, this patch adds tsize handling which is needed for PXELinux.
Regards,
Anthony Liguori
slirp/tftp.c +83 -0
@@ -120,6 +120,45 @@ static int tftp_read_data(struct tftp_sereturn bytes_read; } +static int tftp_send_oack (struct tftp_session *spt, +const char *key,uint32_t value, +struct tftp_t *recv_tp) +{ +struct sockaddr_in saddr, daddr; +struct mbuf *m; +struct tftp_t *tp; +int n =0 ; + + m =m_get (); + +if (!m) +return -1 ; + +memset (m->m_data,0 , m->m_size); + + m->m_data += if_maxlinkhdr; + tp = (void *)m->m_data; + m->m_data +=sizeof (struct udpiphdr ); + + tp->tp_op =htons (TFTP_OACK ); + n +=sprintf (tp->x.tp_buf + n,"%s" , key) +1 ; + n +=sprintf (tp->x.tp_buf + n,"%u" , value) +1 ; + + saddr.sin_addr = recv_tp->ip.ip_dst; + saddr.sin_port = recv_tp->udp.uh_dport; + + daddr.sin_addr = spt->client_ip; + daddr.sin_port = spt->client_port; + + m->m_len =sizeof (struct tftp_t ) -514 + n - +sizeof (struct ip ) -sizeof (struct udphdr ); +udp_output2 (NULL , m, &saddr, &daddr,IPTOS_LOWDELAY ); + +return 0 ; +} + + +static int tftp_send_error (struct tftp_session *spt,u_int16_t errorcode,const char *msg,struct tftp_t *recv_tp)
@@ -273,6 +312,8 @@ static void tftp_handle_rrq(struct tftp_return ; } + k +=6 ;/* skipping octet*/ +/* do sanity checks on the filename */ if ((spt->filename[0] != '/')
@@ -297,6 +338,48 @@ static void tftp_handle_rrq(struct tftp_return ; } +if (src[n -1 ] !=0 ) { +tftp_send_error (spt,2 ,"Access violation" , tp); +return ; + } + +while (k < n) { +const char *key, *value; + + key = src + k; + k +=strlen (key) +1 ; + +if (k >= n) { +tftp_send_error (spt,2 ,"Access violation" , tp); +return ; + } + + value = src + k; + k +=strlen (value) +1 ; + +if (strcmp (key,"tsize" ) ==0 ) { +int tsize =atoi (value); +struct stat stat_p; + +if (tsize ==0 && tftp_prefix) { +char buffer[1024 ]; +int len; + + len =snprintf (buffer,sizeof (buffer),"%s/%s" , + tftp_prefix, spt->filename); + +if (stat (buffer, &stat_p) ==0 ) + tsize = stat_p.st_size; +else { +tftp_send_error (spt,1 ,"File not found" , tp); +return ; + } + } + +tftp_send_oack (spt,"tsize" , tsize, tp); + } + } +tftp_send_data (spt,1 , tp); }
slirp/tftp.h +1 -0
@@ -9,6 +9,7 @@#define TFTP_DATA 3#define TFTP_ACK 4#define TFTP_ERROR 5 +#define TFTP_OACK 6#define TFTP_FILENAME_MAX 512
PATCH 2/3 Add -bootp option for slip
In reply to Anthony LiguoriThe following patch adds a -bootp option. This option takes a filename that will be advertised as the BOOTP filename.
Regards,
Anthony Liguori
slirp/bootp.c +6 -0
@@ -38,6 +38,8 @@ typedef struct {BOOTPClient bootp_clients[NB_ADDR ]; +const char *bootp_filename; +static const uint8_t rfc1533_cookie[] = {RFC1533_COOKIE };#ifdef DEBUG
@@ -168,6 +170,10 @@ static void bootp_reply(struct bootp_t * goto new_addr; } } + +if (bootp_filename) +snprintf (rbp->bp_file,sizeof (rbp->bp_file),"%s" , bootp_filename); +dprintf ("offered addr=%08x\n" ,ntohl (daddr.sin_addr.s_addr)); saddr.sin_addr.s_addr =htonl (ntohl (special_addr.s_addr) |CTL_ALIAS );
vl.c +6 -0
@@ -6488,6 +6488,7 @@ void help(void) "\n"#ifdef CONFIG_SLIRP "-tftp prefix allow tftp access to files starting with prefix [-net user]\n" +"-bootp file advertise file in BOOTP replies\n" #ifndef _WIN32"-smb dir allow SMB access to files in 'dir' [-net user]\n" #endif
@@ -6576,6 +6577,7 @@ enum { QEMU_OPTION_net, QEMU_OPTION_tftp, + QEMU_OPTION_bootp, QEMU_OPTION_smb, QEMU_OPTION_redir,
@@ -6652,6 +6654,7 @@ const QEMUOption qemu_options[] = { { "net", HAS_ARG, QEMU_OPTION_net}, #ifdef CONFIG_SLIRP { "tftp", HAS_ARG, QEMU_OPTION_tftp }, + { "bootp", HAS_ARG, QEMU_OPTION_bootp }, #ifndef _WIN32 { "smb", HAS_ARG, QEMU_OPTION_smb }, #endif
@@ -7185,6 +7188,9 @@ int main(int argc, char **argv)case QEMU_OPTION_tftp: tftp_prefix = optarg;break ; +case QEMU_OPTION_bootp: + bootp_filename = optarg; +break ;#ifndef _WIN32case QEMU_OPTION_smb:net_slirp_smb (optarg);
vl.h +1 -0
@@ -163,6 +163,7 @@ extern int no_quit;extern int no_quit;extern int semihosting_enabled;extern int autostart; +extern const char *bootp_filename;#define MAX_OPTION_ROMS 16extern const char *option_rom[MAX_OPTION_ROMS ];
qemu-doc.texi +10 -0
@@ -445,6 +445,16 @@ guest must be configured in binary mode guest must be configured in binary mode (use the command @code{bin} of the Unix TFTP client). The host IP address on the guest is as usual 10.0.2.2. + +@item -bootp file +When using the user mode network stack, broadcast @var{file} as the BOOTP +filename. In conjunction with @option{-tftp}, this can be used to network boot +a guest from a local directory. + +Example (using pxelinux): +@example +qemu -hda linux.img -boot n -tftp /path/to/tftp/files -bootp /pxelinux.0 +@end example @item -smb dir When using the user mode network stack, activate a built-in SMB
PATCH 3/3 Change -tftp option to take a root directory
In reply to Anthony LiguoriThis patch changes the -tftp option to take a root directory instead of a prefix. I know it's common for a TFTP server to take a prefix but it's easier typing wise to expose files as being part of a root. For example, without this patch, to PXE boot:
qemu -hda disk.img -tftp /home/anthony/tftpboot -bootp /home/anthony/tftpboot/pxelinux.0
With the patch:
qemu -hda disk.img -tftp /home/anthony/tftpboot -bootp /pxelinux.0
Also helps deal with the fact that TFTP filenames are limited to 512 characters.
Regards,
Anthony Liguori
qemu-doc.texi +5 -6
@@ -438,13 +438,12 @@ override the default configuration (@opt override the default configuration (@option{-net nic -net user}) which is activated if no @option{-net} options are provided. -@item -tftp prefix +@item -tftp dir When using the user mode network stack, activate a built-in TFTP -server. All filenames beginning with @var{prefix} can be downloaded -from the host to the guest using a TFTP client. The TFTP client on the -guest must be configured in binary mode (use the command @code{bin} of -the Unix TFTP client). The host IP address on the guest is as usual -10.0.2.2. +server. The files in @var{dir} will be exposed as the root of a TFTP server. +The TFTP client on the guest must be configured in binary mode (use the command +@code{bin} of the Unix TFTP client). The host IP address on the guest is as +usual 10.0.2.2. @item -bootp file When using the user mode network stack, broadcast @var{file} as the BOOTP
slirp/tftp.c +10 -4
@@ -102,8 +102,15 @@ static int tftp_read_data(struct tftp_se {int fd;int bytes_read =0 ; - - fd =open (spt->filename,O_RDONLY |O_BINARY ); +char buffer[1024 ]; +int n; + + n =snprintf (buffer,sizeof (buffer),"%s/%s" , + tftp_prefix, spt->filename); +if (n >=sizeof (buffer)) +return -1 ; + + fd =open (buffer,O_RDONLY |O_BINARY );if (fd <0 ) {return -1 ;
@@ -328,8 +335,7 @@ static void tftp_handle_rrq(struct tftp_/* only allow exported prefixes */ -if (!tftp_prefix - || (strncmp (spt->filename, tftp_prefix,strlen (tftp_prefix)) !=0 )) { +if (!tftp_prefix) {tftp_send_error (spt,2 ,"Access violation" , tp);return ; }
vl.c +1 -1
@@ -6487,7 +6487,7 @@ void help(void)" is provided, the default is '-net nic -net user'\n" "\n" #ifdef CONFIG_SLIRP -"-tftp prefix allow tftp access to files starting with prefix [-net user]\n" +"-tftp dir allow tftp access to files in dir [-net user]\n" "-bootp file advertise file in BOOTP replies\n" #ifndef _WIN32"-smb dir allow SMB access to files in 'dir' [-net user]\n"
Quoted 11 lines · Anthony Liguori
The following patch series allows for BOOTP to be used with slirp. The original patch was written by Erwan Velu.
With the full series, you can do:
qemu -hda /path/to/disk.img -boot n -tftp ~/tftpdir -bootp /pxelinux.0
And the guest will boot from ~/tftpdir/pxelinux.0.
Regards,
Anthony Liguori
One more time anthony, thanks for you work.
I really love to see this feature entering qemu.
Regards,
Erwan Velu