summaryrefslogtreecommitdiffstats
path: root/src/tests/dhcptest.c
blob: 827d02cb294658ac490e5936d7ce273ec1164d7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <string.h>
#include <stdlib.h>
#include <vsprintf.h>
#include <byteswap.h>
#include <gpxe/ip.h>
#include <gpxe/dhcp.h>
#include <gpxe/iscsi.h>
#include <gpxe/netdevice.h>

static int test_dhcp_aoe_boot ( struct net_device *netdev,
				char *aoename ) {
	unsigned int drivenum;
	
	drivenum = find_global_dhcp_num_option ( DHCP_EB_BIOS_DRIVE );
	return test_aoeboot ( netdev, aoename, drivenum );
}

enum {
	RP_LITERAL = 0,
	RP_SERVERNAME,
	RP_PROTOCOL,
	RP_PORT,
	RP_LUN,
	RP_TARGETNAME,
	NUM_RP_COMPONENTS
};

static int iscsi_split_root_path ( char *root_path,
				   char * components[NUM_RP_COMPONENTS] ) {
	int component = 0;
	
	while ( 1 ) {
		components[component++] = root_path;
		if ( component == NUM_RP_COMPONENTS ) {
			return 0;
		}
		for ( ; *root_path != ':' ; root_path++ ) {
			if ( ! *root_path )
				return -EINVAL;
		}
		*(root_path++) = '\0';
	}
}

static int test_dhcp_iscsi_boot ( struct net_device *netdev ) {
	char root_path[256];
	char *rp_components[NUM_RP_COMPONENTS];
	char initiator_iqn_buf[32];
	char *initiator_iqn = initiator_iqn_buf;
	char username[32];
	char password[32];
	union {
		struct sockaddr_in sin;
		struct sockaddr_tcpip st;
	} target;
	unsigned int drivenum;
	unsigned int lun;
	struct dhcp_option *option;
	int rc;

	memset ( &target, 0, sizeof ( target ) );
	target.sin.sin_family = AF_INET;

	dhcp_snprintf ( root_path, sizeof ( root_path ),
			find_global_dhcp_option ( DHCP_ROOT_PATH ) );

	printf ( "Root path \"%s\"\n", root_path );

	if ( ( rc = iscsi_split_root_path ( root_path, rp_components ) ) != 0 )
		goto bad_root_path;

	if ( strcmp ( rp_components[RP_LITERAL], "iscsi" ) != 0 )
		goto bad_root_path;

	if ( inet_aton ( rp_components[RP_SERVERNAME],
			 &target.sin.sin_addr ) == 0 )
		goto bad_root_path;

	target.sin.sin_port = strtoul ( rp_components[RP_PORT], NULL, 0 );
	if ( ! target.st.st_port )
		target.st.st_port = htons ( ISCSI_PORT );

	lun = strtoul ( rp_components[RP_LUN], NULL, 0 );
	
	dhcp_snprintf ( initiator_iqn, sizeof ( initiator_iqn ),
			find_global_dhcp_option ( DHCP_ISCSI_INITIATOR_IQN ) );
	if ( ! *initiator_iqn )
		initiator_iqn = "iqn.1900-01.localdomain.localhost:initiator";
	dhcp_snprintf ( username, sizeof ( username ),
			find_global_dhcp_option ( DHCP_EB_USERNAME ) );
	dhcp_snprintf ( password, sizeof ( password ),
			find_global_dhcp_option ( DHCP_EB_PASSWORD ) );

	drivenum = find_global_dhcp_num_option ( DHCP_EB_BIOS_DRIVE );

	return test_iscsiboot ( initiator_iqn, &target.st,
				rp_components[RP_TARGETNAME], lun,
				username, password, netdev, drivenum );

 bad_root_path:
	printf ( "Invalid iSCSI root path\n" );
	return -EINVAL;
}

static int test_dhcp_hello ( char *helloname ) {
	char *message;
	union {
		struct sockaddr_in sin;
		struct sockaddr_tcpip st;
	} target;

	memset ( &target, 0, sizeof ( target ) );
	target.sin.sin_family = AF_INET;
	target.sin.sin_port = htons ( 80 );
	message = strchr ( helloname, ':' );
	*message++ = '\0';
	if ( ! message ) {
		printf ( "Invalid hello path\n" );
		return -EINVAL;
	}
	inet_aton ( helloname, &target.sin.sin_addr );	

	test_hello ( &target.st, message );
	return 0;
}

static int test_dhcp_http ( struct net_device *netdev, char *url ) {
	union {
		struct sockaddr_in sin;
		struct sockaddr_tcpip st;
	} target;

	memset ( &target, 0, sizeof ( target ) );
	target.sin.sin_family = AF_INET;
	target.sin.sin_port = htons ( 80 );

	char *addr = url + 7; // http://
        char *file = strchr(addr, '/');
	*file = '\0'; // for printf and inet_aton to work
	printf("connecting to %s\n", addr);
	inet_aton ( addr, &target.sin.sin_addr );
	*file = '/';
	test_http ( netdev, &target.st, file );
	return 0;
}

static int test_dhcp_ftp ( struct net_device *netdev, char *ftpname ) {
	union {
		struct sockaddr_in sin;
		struct sockaddr_tcpip st;
	} target;
	char *filename;

	filename = strchr ( ftpname, ':' );
	if ( ! filename ) {
		printf ( "Invalid FTP path \"%s\"\n", ftpname );
		return -EINVAL;
	}
	*filename++ = '\0';

	memset ( &target, 0, sizeof ( target ) );
	target.sin.sin_family = AF_INET;
	target.sin.sin_port = htons ( 21 );
	inet_aton ( ftpname, &target.sin.sin_addr );

	test_ftp ( &target, filename );
	return 0;	
}

static int test_dhcp_tftp ( struct net_device *netdev, char *tftpname ) {
	union {
		struct sockaddr_in sin;
		struct sockaddr_tcpip st;
	} target;

	memset ( &target, 0, sizeof ( target ) );
	target.sin.sin_family = AF_INET;
	target.sin.sin_port = htons ( 69 );
	find_global_dhcp_ipv4_option ( DHCP_EB_SIADDR,
				       &target.sin.sin_addr );

	return test_tftp ( netdev, &target.st, tftpname );
}

static int test_dhcp_boot ( struct net_device *netdev, char *filename ) {
	/*
	if ( strncmp ( filename, "aoe:", 4 ) == 0 ) {
		return test_dhcp_aoe_boot ( netdev, &filename[4] );
	} 
	*/
	//	if ( strncmp ( filename, "iscsi:", 6 ) == 0 ) {
	if ( ! filename[0] ) {
		return test_dhcp_iscsi_boot ( netdev );
	}
	/*
	if ( strncmp ( filename, "ftp:", 4 ) == 0 ) {
		return test_dhcp_ftp ( netdev, &filename[4] );
	}
	*/
	/*
	if ( strncmp ( filename, "hello:", 6 ) == 0 ) {
		return test_dhcp_hello ( &filename[6] );
	}
	if ( strncmp ( filename, "http:", 5 ) == 0 ) {
		return test_dhcp_http ( netdev, filename );
	}
	*/
	return test_dhcp_tftp ( netdev, filename );

	return -EPROTONOSUPPORT;
}

int test_dhcp ( struct net_device *netdev ) {
	struct dhcp_session dhcp;
	struct in_addr address = { htonl ( 0 ) };
	struct in_addr netmask = { htonl ( 0 ) };
	struct in_addr gateway = { INADDR_NONE };
	char filename[256];
	int rc;

	/* Bring IP interface up with address 0.0.0.0 */
	if ( ( rc = add_ipv4_address ( netdev, address, netmask,
				       gateway ) ) != 0 )
		goto out_no_del_ipv4;

	/* Issue DHCP request */
	printf ( "DHCP (%s)...", netdev_name ( netdev ) );
	memset ( &dhcp, 0, sizeof ( dhcp ) );
	dhcp.netdev = netdev;
	if ( ( rc = async_wait ( start_dhcp ( &dhcp ) ) ) != 0 ) {
		printf ( "failed\n" );
		goto out_no_options;
	}
	printf ( "done\n" );

	/* Register options received via DHCP */
	register_dhcp_options ( dhcp.options );

	/* Retrieve IP address configuration */
	find_global_dhcp_ipv4_option ( DHCP_EB_YIADDR, &address );
	find_global_dhcp_ipv4_option ( DHCP_SUBNET_MASK, &netmask );
	find_global_dhcp_ipv4_option ( DHCP_ROUTERS, &gateway );

	printf ( "IP %s", inet_ntoa ( address ) );
	printf ( " netmask %s", inet_ntoa ( netmask ) );
	printf ( " gateway %s\n", inet_ntoa ( gateway ) );

	dhcp_snprintf ( filename, sizeof ( filename ),
			find_global_dhcp_option ( DHCP_BOOTFILE_NAME ) );
	
	if ( filename[0] )
		printf ( "Bootfile name \"%s\"\n", filename );

	/* Remove old IP address configuration */
	del_ipv4_address ( netdev );

	/* Set up new IP address configuration */
	if ( ( rc = add_ipv4_address ( netdev, address, netmask,
				       gateway ) ) != 0 )
		goto out_no_del_ipv4;

	/* Test boot */
	if ( ( rc = test_dhcp_boot ( netdev, filename ) ) != 0 ) {
		printf ( "Boot failed\n" );
		goto out;
	}
	
 out:
	/* Unregister and free DHCP options */
	unregister_dhcp_options ( dhcp.options );
	free_dhcp_options ( dhcp.options );
 out_no_options:
	/* Take down IP interface */
	del_ipv4_address ( netdev );
 out_no_del_ipv4:
	return rc;
}