6 #include <gpxe/iscsi.h>
7 #include <gpxe/netdevice.h>
9 static int test_dhcp_aoe_boot ( struct net_device *netdev,
11 unsigned int drivenum;
13 drivenum = find_global_dhcp_num_option ( DHCP_EB_BIOS_DRIVE );
14 return test_aoeboot ( netdev, aoename, drivenum );
17 static int test_dhcp_iscsi_boot ( struct net_device *netdev,
19 char initiator_iqn_buf[32];
20 char *initiator_iqn = initiator_iqn_buf;
25 struct sockaddr_in sin;
26 struct sockaddr_tcpip st;
28 unsigned int drivenum;
29 struct dhcp_option *option;
31 memset ( &target, 0, sizeof ( target ) );
32 target.sin.sin_family = AF_INET;
33 target.sin.sin_port = htons ( ISCSI_PORT );
34 target_iqn = strchr ( iscsiname, ':' );
37 printf ( "Invalid iSCSI DHCP path\n" );
40 inet_aton ( iscsiname, &target.sin.sin_addr );
42 dhcp_snprintf ( initiator_iqn, sizeof ( initiator_iqn ),
43 find_global_dhcp_option ( DHCP_ISCSI_INITIATOR_IQN ) );
44 if ( ! *initiator_iqn )
45 initiator_iqn = "iqn.1900-01.localdomain.localhost:initiator";
46 dhcp_snprintf ( username, sizeof ( username ),
47 find_global_dhcp_option ( DHCP_EB_USERNAME ) );
48 dhcp_snprintf ( password, sizeof ( password ),
49 find_global_dhcp_option ( DHCP_EB_PASSWORD ) );
51 drivenum = find_global_dhcp_num_option ( DHCP_EB_BIOS_DRIVE );
53 return test_iscsiboot ( initiator_iqn, &target.st, target_iqn,
54 username, password, netdev, drivenum );
57 static int test_dhcp_hello ( char *helloname ) {
60 struct sockaddr_in sin;
61 struct sockaddr_tcpip st;
64 memset ( &target, 0, sizeof ( target ) );
65 target.sin.sin_family = AF_INET;
66 target.sin.sin_port = htons ( 80 );
67 message = strchr ( helloname, ':' );
70 printf ( "Invalid hello path\n" );
73 inet_aton ( helloname, &target.sin.sin_addr );
75 test_hello ( &target.st, message );
79 static int test_dhcp_http ( struct net_device *netdev, char *url ) {
81 struct sockaddr_in sin;
82 struct sockaddr_tcpip st;
85 memset ( &target, 0, sizeof ( target ) );
86 target.sin.sin_family = AF_INET;
87 target.sin.sin_port = htons ( 80 );
89 char *addr = url + 7; // http://
90 char *file = strchr(addr, '/');
91 *file = '\0'; // for printf and inet_aton to work
92 printf("connecting to %s\n", addr);
93 inet_aton ( addr, &target.sin.sin_addr );
95 test_http ( netdev, &target.st, file );
99 static int test_dhcp_ftp ( struct net_device *netdev, char *ftpname ) {
101 struct sockaddr_in sin;
102 struct sockaddr_tcpip st;
106 filename = strchr ( ftpname, ':' );
108 printf ( "Invalid FTP path \"%s\"\n", ftpname );
113 memset ( &target, 0, sizeof ( target ) );
114 target.sin.sin_family = AF_INET;
115 target.sin.sin_port = htons ( 21 );
116 inet_aton ( ftpname, &target.sin.sin_addr );
118 test_ftp ( &target, filename );
122 static int test_dhcp_tftp ( struct net_device *netdev, char *tftpname ) {
124 struct sockaddr_in sin;
125 struct sockaddr_tcpip st;
128 memset ( &target, 0, sizeof ( target ) );
129 target.sin.sin_family = AF_INET;
130 target.sin.sin_port = htons ( 69 );
131 find_global_dhcp_ipv4_option ( DHCP_EB_SIADDR,
132 &target.sin.sin_addr );
134 return test_tftp ( netdev, &target.st, tftpname );
137 static int test_dhcp_boot ( struct net_device *netdev, char *filename ) {
139 if ( strncmp ( filename, "aoe:", 4 ) == 0 ) {
140 return test_dhcp_aoe_boot ( netdev, &filename[4] );
143 if ( strncmp ( filename, "iscsi:", 6 ) == 0 ) {
144 return test_dhcp_iscsi_boot ( netdev, &filename[6] );
147 if ( strncmp ( filename, "ftp:", 4 ) == 0 ) {
148 return test_dhcp_ftp ( netdev, &filename[4] );
152 if ( strncmp ( filename, "hello:", 6 ) == 0 ) {
153 return test_dhcp_hello ( &filename[6] );
155 if ( strncmp ( filename, "http:", 5 ) == 0 ) {
156 return test_dhcp_http ( netdev, filename );
159 return test_dhcp_tftp ( netdev, filename );
161 return -EPROTONOSUPPORT;
164 int test_dhcp ( struct net_device *netdev ) {
165 struct dhcp_session dhcp;
166 struct in_addr address = { htonl ( 0 ) };
167 struct in_addr netmask = { htonl ( 0 ) };
168 struct in_addr gateway = { INADDR_NONE };
172 /* Bring IP interface up with address 0.0.0.0 */
173 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
175 goto out_no_del_ipv4;
177 /* Issue DHCP request */
178 printf ( "DHCP (%s)...", netdev_name ( netdev ) );
179 memset ( &dhcp, 0, sizeof ( dhcp ) );
180 dhcp.netdev = netdev;
181 if ( ( rc = async_wait ( start_dhcp ( &dhcp ) ) ) != 0 ) {
182 printf ( "failed\n" );
187 /* Register options received via DHCP */
188 register_dhcp_options ( dhcp.options );
190 /* Retrieve IP address configuration */
191 find_global_dhcp_ipv4_option ( DHCP_EB_YIADDR, &address );
192 find_global_dhcp_ipv4_option ( DHCP_SUBNET_MASK, &netmask );
193 find_global_dhcp_ipv4_option ( DHCP_ROUTERS, &gateway );
195 printf ( "IP %s", inet_ntoa ( address ) );
196 printf ( " netmask %s", inet_ntoa ( netmask ) );
197 printf ( " gateway %s\n", inet_ntoa ( gateway ) );
199 dhcp_snprintf ( filename, sizeof ( filename ),
200 find_global_dhcp_option ( DHCP_BOOTFILE_NAME ) );
201 if ( ! filename[0] ) {
202 printf ( "No filename specified!\n" );
206 printf ( "Bootfile name %s\n", filename );
208 /* Remove old IP address configuration */
209 del_ipv4_address ( netdev );
211 /* Set up new IP address configuration */
212 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
214 goto out_no_del_ipv4;
217 if ( ( rc = test_dhcp_boot ( netdev, filename ) ) != 0 ) {
218 printf ( "Boot failed\n" );
223 /* Unregister and free DHCP options */
224 unregister_dhcp_options ( dhcp.options );
225 free_dhcp_options ( dhcp.options );
227 /* Take down IP interface */
228 del_ipv4_address ( netdev );