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, char *iscsiname ) {
18 char *initiator_iqn = "iqn.1900-01.localdomain.localhost:initiator";
23 struct sockaddr_in sin;
24 struct sockaddr_tcpip st;
27 memset ( &target, 0, sizeof ( target ) );
28 target.sin.sin_family = AF_INET;
29 target.sin.sin_port = htons ( ISCSI_PORT );
30 target_iqn = strchr ( iscsiname, ':' );
33 printf ( "Invalid iSCSI DHCP path\n" );
36 inet_aton ( iscsiname, &target.sin.sin_addr );
38 dhcp_snprintf ( username, sizeof ( username ),
39 find_global_dhcp_option ( DHCP_EB_USERNAME ) );
40 dhcp_snprintf ( password, sizeof ( password ),
41 find_global_dhcp_option ( DHCP_EB_PASSWORD ) );
43 return test_iscsiboot ( initiator_iqn, &target.st, target_iqn,
44 username, password, netdev );
47 static int test_dhcp_hello ( char *helloname ) {
50 struct sockaddr_in sin;
51 struct sockaddr_tcpip st;
54 memset ( &target, 0, sizeof ( target ) );
55 target.sin.sin_family = AF_INET;
56 target.sin.sin_port = htons ( 80 );
57 message = strchr ( helloname, ':' );
60 printf ( "Invalid hello path\n" );
63 inet_aton ( helloname, &target.sin.sin_addr );
65 test_hello ( &target.st, message );
69 static int test_dhcp_http ( struct net_device *netdev, char *url ) {
71 struct sockaddr_in sin;
72 struct sockaddr_tcpip st;
75 memset ( &target, 0, sizeof ( target ) );
76 target.sin.sin_family = AF_INET;
77 target.sin.sin_port = htons ( 80 );
79 char *addr = url + 7; // http://
80 char *file = strchr(addr, '/');
81 *file = '\0'; // for printf and inet_aton to work
82 printf("connecting to %s\n", addr);
83 inet_aton ( addr, &target.sin.sin_addr );
85 test_http ( netdev, &target.st, file );
89 static int test_dhcp_tftp ( struct net_device *netdev, char *tftpname ) {
91 struct sockaddr_in sin;
92 struct sockaddr_tcpip st;
95 memset ( &target, 0, sizeof ( target ) );
96 target.sin.sin_family = AF_INET;
97 target.sin.sin_port = htons ( 69 );
98 find_global_dhcp_ipv4_option ( DHCP_EB_SIADDR,
99 &target.sin.sin_addr );
101 return test_tftp ( netdev, &target.st, tftpname );
104 static int test_dhcp_boot ( struct net_device *netdev, char *filename ) {
105 if ( strncmp ( filename, "aoe:", 4 ) == 0 ) {
106 return test_dhcp_aoe_boot ( netdev, &filename[4] );
107 } else if ( strncmp ( filename, "iscsi:", 6 ) == 0 ) {
108 return test_dhcp_iscsi_boot ( netdev, &filename[6] );
109 } else if ( strncmp ( filename, "hello:", 6 ) == 0 ) {
110 return test_dhcp_hello ( &filename[6] );
111 } else if ( strncmp ( filename, "http:", 5 ) == 0 ) {
112 return test_dhcp_http ( netdev, filename );
114 return test_dhcp_tftp ( netdev, filename );
118 int test_dhcp ( struct net_device *netdev ) {
119 struct dhcp_session dhcp;
120 struct in_addr address = { htonl ( 0 ) };
121 struct in_addr netmask = { htonl ( 0 ) };
122 struct in_addr gateway = { INADDR_NONE };
126 /* Bring IP interface up with address 0.0.0.0 */
127 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
129 goto out_no_del_ipv4;
131 /* Issue DHCP request */
132 printf ( "DHCP (%s)...", netdev_name ( netdev ) );
133 memset ( &dhcp, 0, sizeof ( dhcp ) );
134 dhcp.netdev = netdev;
135 if ( ( rc = async_wait ( start_dhcp ( &dhcp ) ) ) != 0 ) {
136 printf ( "failed\n" );
141 /* Register options received via DHCP */
142 register_dhcp_options ( dhcp.options );
144 /* Retrieve IP address configuration */
145 find_global_dhcp_ipv4_option ( DHCP_EB_YIADDR, &address );
146 find_global_dhcp_ipv4_option ( DHCP_SUBNET_MASK, &netmask );
147 find_global_dhcp_ipv4_option ( DHCP_ROUTERS, &gateway );
149 printf ( "IP %s", inet_ntoa ( address ) );
150 printf ( " netmask %s", inet_ntoa ( netmask ) );
151 printf ( " gateway %s\n", inet_ntoa ( gateway ) );
153 dhcp_snprintf ( filename, sizeof ( filename ),
154 find_global_dhcp_option ( DHCP_BOOTFILE_NAME ) );
155 if ( ! filename[0] ) {
156 printf ( "No filename specified!\n" );
160 printf ( "Bootfile name %s\n", filename );
162 /* Remove old IP address configuration */
163 del_ipv4_address ( netdev );
165 /* Set up new IP address configuration */
166 if ( ( rc = add_ipv4_address ( netdev, address, netmask,
168 goto out_no_del_ipv4;
171 if ( ( rc = test_dhcp_boot ( netdev, filename ) ) != 0 ) {
172 printf ( "Boot failed\n" );
177 /* Unregister and free DHCP options */
178 unregister_dhcp_options ( dhcp.options );
179 free_dhcp_options ( dhcp.options );
181 /* Take down IP interface */
182 del_ipv4_address ( netdev );