7 #include <gpxe/iscsi.h>
8 #include <gpxe/netdevice.h>
10 static int test_dhcp_aoe_boot ( struct net_device *netdev,
12 unsigned int drivenum;
14 drivenum = find_global_dhcp_num_option ( DHCP_EB_BIOS_DRIVE );
15 return test_aoeboot ( netdev, aoename, drivenum );
28 static int iscsi_split_root_path ( char *root_path,
29 char * components[NUM_RP_COMPONENTS] ) {
33 components[component++] = root_path;
34 if ( component == NUM_RP_COMPONENTS ) {
37 for ( ; *root_path != ':' ; root_path++ ) {
41 *(root_path++) = '\0';
45 static int test_dhcp_iscsi_boot ( struct net_device *netdev ) {
47 char *rp_components[NUM_RP_COMPONENTS];
48 char initiator_iqn_buf[64];
49 char *initiator_iqn = initiator_iqn_buf;
53 struct sockaddr_in sin;
54 struct sockaddr_tcpip st;
56 unsigned int drivenum;
58 struct dhcp_option *option;
61 memset ( &target, 0, sizeof ( target ) );
62 target.sin.sin_family = AF_INET;
64 dhcp_snprintf ( root_path, sizeof ( root_path ),
65 find_global_dhcp_option ( DHCP_ROOT_PATH ) );
67 printf ( "Root path \"%s\"\n", root_path );
69 if ( ( rc = iscsi_split_root_path ( root_path, rp_components ) ) != 0 )
72 if ( strcmp ( rp_components[RP_LITERAL], "iscsi" ) != 0 )
75 if ( inet_aton ( rp_components[RP_SERVERNAME],
76 &target.sin.sin_addr ) == 0 )
79 target.sin.sin_port = strtoul ( rp_components[RP_PORT], NULL, 0 );
80 if ( ! target.st.st_port )
81 target.st.st_port = htons ( ISCSI_PORT );
83 lun = strtoul ( rp_components[RP_LUN], NULL, 0 );
85 dhcp_snprintf ( initiator_iqn_buf, sizeof ( initiator_iqn_buf ),
86 find_global_dhcp_option ( DHCP_ISCSI_INITIATOR_IQN ) );
87 if ( ! initiator_iqn_buf[0] )
88 initiator_iqn = "iqn.1900-01.localdomain.localhost:initiator";
89 dhcp_snprintf ( username, sizeof ( username ),
90 find_global_dhcp_option ( DHCP_EB_USERNAME ) );
91 dhcp_snprintf ( password, sizeof ( password ),
92 find_global_dhcp_option ( DHCP_EB_PASSWORD ) );
94 drivenum = find_global_dhcp_num_option ( DHCP_EB_BIOS_DRIVE );
96 return test_iscsiboot ( initiator_iqn, &target.st,
97 rp_components[RP_TARGETNAME], lun,
98 username, password, netdev, drivenum );
101 printf ( "Invalid iSCSI root path\n" );
105 static int test_dhcp_hello ( char *helloname ) {
108 struct sockaddr_in sin;
109 struct sockaddr_tcpip st;
112 memset ( &target, 0, sizeof ( target ) );
113 target.sin.sin_family = AF_INET;
114 target.sin.sin_port = htons ( 80 );
115 message = strchr ( helloname, ':' );
118 printf ( "Invalid hello path\n" );
121 inet_aton ( helloname, &target.sin.sin_addr );
123 test_hello ( &target.st, message );
127 static int test_dhcp_http ( struct net_device *netdev, char *url ) {
129 struct sockaddr_in sin;
130 struct sockaddr_tcpip st;
133 memset ( &target, 0, sizeof ( target ) );
134 target.sin.sin_family = AF_INET;
135 target.sin.sin_port = htons ( 80 );
137 char *addr = url + 7; // http://
138 char *file = strchr(addr, '/');
139 *file = '\0'; // for printf and inet_aton to work
140 printf("connecting to %s\n", addr);
141 inet_aton ( addr, &target.sin.sin_addr );
143 test_http ( netdev, &target.st, file );
147 static int test_dhcp_ftp ( struct net_device *netdev, char *ftpname ) {
149 struct sockaddr_in sin;
150 struct sockaddr_tcpip st;
154 filename = strchr ( ftpname, ':' );
156 printf ( "Invalid FTP path \"%s\"\n", ftpname );
161 memset ( &target, 0, sizeof ( target ) );
162 target.sin.sin_family = AF_INET;
163 target.sin.sin_port = htons ( 21 );
164 inet_aton ( ftpname, &target.sin.sin_addr );
166 test_ftp ( &target, filename );
170 static int test_dhcp_tftp ( struct net_device *netdev, char *tftpname ) {
172 struct sockaddr_in sin;
173 struct sockaddr_tcpip st;
176 memset ( &target, 0, sizeof ( target ) );
177 target.sin.sin_family = AF_INET;
178 target.sin.sin_port = htons ( 69 );
179 find_global_dhcp_ipv4_option ( DHCP_EB_SIADDR,
180 &target.sin.sin_addr );
182 return test_tftp ( netdev, &target.st, tftpname );
185 static int test_dhcp_boot ( struct net_device *netdev, char *filename ) {
187 if ( strncmp ( filename, "aoe:", 4 ) == 0 ) {
188 return test_dhcp_aoe_boot ( netdev, &filename[4] );
191 // if ( strncmp ( filename, "iscsi:", 6 ) == 0 ) {
192 if ( ! filename[0] ) {
193 return test_dhcp_iscsi_boot ( netdev );
196 if ( strncmp ( filename, "ftp:", 4 ) == 0 ) {
197 return test_dhcp_ftp ( netdev, &filename[4] );
201 if ( strncmp ( filename, "hello:", 6 ) == 0 ) {
202 return test_dhcp_hello ( &filename[6] );
204 if ( strncmp ( filename, "http:", 5 ) == 0 ) {
205 return test_dhcp_http ( netdev, filename );
208 return test_dhcp_tftp ( netdev, filename );
210 return -EPROTONOSUPPORT;
213 int test_dhcp ( struct net_device *netdev ) {
214 struct dhcp_session dhcp;
215 struct in_addr address = { htonl ( 0 ) };
216 struct in_addr netmask = { htonl ( 0 ) };
217 struct in_addr gateway = { INADDR_NONE };
221 /* Bring IP interface up with address 0.0.0.0 */
222 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
224 goto out_no_del_ipv4;
226 /* Issue DHCP request */
227 printf ( "DHCP (%s)...", netdev->name );
228 memset ( &dhcp, 0, sizeof ( dhcp ) );
229 dhcp.netdev = netdev;
230 if ( ( rc = async_wait ( start_dhcp ( &dhcp ) ) ) != 0 ) {
231 printf ( "failed\n" );
236 /* Register options received via DHCP */
237 register_dhcp_options ( dhcp.options );
239 /* Retrieve IP address configuration */
240 find_global_dhcp_ipv4_option ( DHCP_EB_YIADDR, &address );
241 find_global_dhcp_ipv4_option ( DHCP_SUBNET_MASK, &netmask );
242 find_global_dhcp_ipv4_option ( DHCP_ROUTERS, &gateway );
244 printf ( "IP %s", inet_ntoa ( address ) );
245 printf ( " netmask %s", inet_ntoa ( netmask ) );
246 printf ( " gateway %s\n", inet_ntoa ( gateway ) );
248 dhcp_snprintf ( filename, sizeof ( filename ),
249 find_global_dhcp_option ( DHCP_BOOTFILE_NAME ) );
252 printf ( "Bootfile name \"%s\"\n", filename );
254 /* Remove old IP address configuration */
255 del_ipv4_address ( netdev );
257 /* Set up new IP address configuration */
258 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
260 goto out_no_del_ipv4;
263 if ( ( rc = test_dhcp_boot ( netdev, filename ) ) != 0 ) {
264 printf ( "Boot failed\n" );
269 /* Unregister and free DHCP options */
270 unregister_dhcp_options ( dhcp.options );
271 free_dhcp_options ( dhcp.options );
273 /* Take down IP interface */
274 del_ipv4_address ( netdev );