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] );
146 if ( strncmp ( filename, "ftp:", 4 ) == 0 ) {
147 return test_dhcp_ftp ( netdev, &filename[4] );
150 if ( strncmp ( filename, "hello:", 6 ) == 0 ) {
151 return test_dhcp_hello ( &filename[6] );
153 if ( strncmp ( filename, "http:", 5 ) == 0 ) {
154 return test_dhcp_http ( netdev, filename );
156 return test_dhcp_tftp ( netdev, filename );
158 return -EPROTONOSUPPORT;
161 int test_dhcp ( struct net_device *netdev ) {
162 struct dhcp_session dhcp;
163 struct in_addr address = { htonl ( 0 ) };
164 struct in_addr netmask = { htonl ( 0 ) };
165 struct in_addr gateway = { INADDR_NONE };
169 /* Bring IP interface up with address 0.0.0.0 */
170 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
172 goto out_no_del_ipv4;
174 /* Issue DHCP request */
175 printf ( "DHCP (%s)...", netdev_name ( netdev ) );
176 memset ( &dhcp, 0, sizeof ( dhcp ) );
177 dhcp.netdev = netdev;
178 if ( ( rc = async_wait ( start_dhcp ( &dhcp ) ) ) != 0 ) {
179 printf ( "failed\n" );
184 /* Register options received via DHCP */
185 register_dhcp_options ( dhcp.options );
187 /* Retrieve IP address configuration */
188 find_global_dhcp_ipv4_option ( DHCP_EB_YIADDR, &address );
189 find_global_dhcp_ipv4_option ( DHCP_SUBNET_MASK, &netmask );
190 find_global_dhcp_ipv4_option ( DHCP_ROUTERS, &gateway );
192 printf ( "IP %s", inet_ntoa ( address ) );
193 printf ( " netmask %s", inet_ntoa ( netmask ) );
194 printf ( " gateway %s\n", inet_ntoa ( gateway ) );
196 dhcp_snprintf ( filename, sizeof ( filename ),
197 find_global_dhcp_option ( DHCP_BOOTFILE_NAME ) );
198 if ( ! filename[0] ) {
199 printf ( "No filename specified!\n" );
203 printf ( "Bootfile name %s\n", filename );
205 /* Remove old IP address configuration */
206 del_ipv4_address ( netdev );
208 /* Set up new IP address configuration */
209 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
211 goto out_no_del_ipv4;
214 if ( ( rc = test_dhcp_boot ( netdev, filename ) ) != 0 ) {
215 printf ( "Boot failed\n" );
220 /* Unregister and free DHCP options */
221 unregister_dhcp_options ( dhcp.options );
222 free_dhcp_options ( dhcp.options );
224 /* Take down IP interface */
225 del_ipv4_address ( netdev );