Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <byteswap.h>
  6. #include <gpxe/ip.h>
  7. #include <gpxe/dhcp.h>
  8. #include <gpxe/iscsi.h>
  9. #include <gpxe/netdevice.h>
  10. #if 0
  11. static int test_dhcp_aoe_boot ( struct net_device *netdev,
  12. char *aoename ) {
  13. unsigned int drivenum;
  14. drivenum = find_global_dhcp_num_option ( DHCP_EB_BIOS_DRIVE );
  15. return test_aoeboot ( netdev, aoename, drivenum );
  16. }
  17. enum {
  18. RP_LITERAL = 0,
  19. RP_SERVERNAME,
  20. RP_PROTOCOL,
  21. RP_PORT,
  22. RP_LUN,
  23. RP_TARGETNAME,
  24. NUM_RP_COMPONENTS
  25. };
  26. static int iscsi_split_root_path ( char *root_path,
  27. char * components[NUM_RP_COMPONENTS] ) {
  28. int component = 0;
  29. while ( 1 ) {
  30. components[component++] = root_path;
  31. if ( component == NUM_RP_COMPONENTS ) {
  32. return 0;
  33. }
  34. for ( ; *root_path != ':' ; root_path++ ) {
  35. if ( ! *root_path )
  36. return -EINVAL;
  37. }
  38. *(root_path++) = '\0';
  39. }
  40. }
  41. static int test_dhcp_iscsi_boot ( struct net_device *netdev ) {
  42. char root_path[256];
  43. char *rp_components[NUM_RP_COMPONENTS];
  44. char initiator_iqn_buf[64];
  45. char *initiator_iqn = initiator_iqn_buf;
  46. char username[32];
  47. char password[32];
  48. union {
  49. struct sockaddr_in sin;
  50. struct sockaddr_tcpip st;
  51. } target;
  52. unsigned int drivenum;
  53. unsigned int lun;
  54. struct dhcp_option *option;
  55. int rc;
  56. memset ( &target, 0, sizeof ( target ) );
  57. target.sin.sin_family = AF_INET;
  58. dhcp_snprintf ( root_path, sizeof ( root_path ),
  59. find_global_dhcp_option ( DHCP_ROOT_PATH ) );
  60. printf ( "Root path \"%s\"\n", root_path );
  61. if ( ( rc = iscsi_split_root_path ( root_path, rp_components ) ) != 0 )
  62. goto bad_root_path;
  63. if ( strcmp ( rp_components[RP_LITERAL], "iscsi" ) != 0 )
  64. goto bad_root_path;
  65. if ( inet_aton ( rp_components[RP_SERVERNAME],
  66. &target.sin.sin_addr ) == 0 )
  67. goto bad_root_path;
  68. target.sin.sin_port = strtoul ( rp_components[RP_PORT], NULL, 0 );
  69. if ( ! target.st.st_port )
  70. target.st.st_port = htons ( ISCSI_PORT );
  71. lun = strtoul ( rp_components[RP_LUN], NULL, 0 );
  72. dhcp_snprintf ( initiator_iqn_buf, sizeof ( initiator_iqn_buf ),
  73. find_global_dhcp_option ( DHCP_ISCSI_INITIATOR_IQN ) );
  74. if ( ! initiator_iqn_buf[0] )
  75. initiator_iqn = "iqn.1900-01.localdomain.localhost:initiator";
  76. dhcp_snprintf ( username, sizeof ( username ),
  77. find_global_dhcp_option ( DHCP_EB_USERNAME ) );
  78. dhcp_snprintf ( password, sizeof ( password ),
  79. find_global_dhcp_option ( DHCP_EB_PASSWORD ) );
  80. drivenum = find_global_dhcp_num_option ( DHCP_EB_BIOS_DRIVE );
  81. return test_iscsiboot ( initiator_iqn, &target.st,
  82. rp_components[RP_TARGETNAME], lun,
  83. username, password, netdev, drivenum );
  84. bad_root_path:
  85. printf ( "Invalid iSCSI root path\n" );
  86. return -EINVAL;
  87. }
  88. static int test_dhcp_hello ( char *helloname ) {
  89. char *message;
  90. union {
  91. struct sockaddr_in sin;
  92. struct sockaddr_tcpip st;
  93. } target;
  94. memset ( &target, 0, sizeof ( target ) );
  95. target.sin.sin_family = AF_INET;
  96. target.sin.sin_port = htons ( 80 );
  97. message = strchr ( helloname, ':' );
  98. *message++ = '\0';
  99. if ( ! message ) {
  100. printf ( "Invalid hello path\n" );
  101. return -EINVAL;
  102. }
  103. inet_aton ( helloname, &target.sin.sin_addr );
  104. test_hello ( &target.st, message );
  105. return 0;
  106. }
  107. static int test_dhcp_http ( struct net_device *netdev, char *url ) {
  108. union {
  109. struct sockaddr_in sin;
  110. struct sockaddr_tcpip st;
  111. } target;
  112. memset ( &target, 0, sizeof ( target ) );
  113. target.sin.sin_family = AF_INET;
  114. target.sin.sin_port = htons ( 80 );
  115. char *addr = url + 7; // http://
  116. char *file = strchr(addr, '/');
  117. *file = '\0'; // for printf and inet_aton to work
  118. printf("connecting to %s\n", addr);
  119. inet_aton ( addr, &target.sin.sin_addr );
  120. *file = '/';
  121. test_http ( netdev, &target.st, file );
  122. return 0;
  123. }
  124. static int test_dhcp_ftp ( struct net_device *netdev, char *ftpname ) {
  125. union {
  126. struct sockaddr_in sin;
  127. struct sockaddr_tcpip st;
  128. } target;
  129. char *filename;
  130. filename = strchr ( ftpname, ':' );
  131. if ( ! filename ) {
  132. printf ( "Invalid FTP path \"%s\"\n", ftpname );
  133. return -EINVAL;
  134. }
  135. *filename++ = '\0';
  136. memset ( &target, 0, sizeof ( target ) );
  137. target.sin.sin_family = AF_INET;
  138. target.sin.sin_port = htons ( 21 );
  139. inet_aton ( ftpname, &target.sin.sin_addr );
  140. test_ftp ( &target, filename );
  141. return 0;
  142. }
  143. static int test_dhcp_tftp ( struct net_device *netdev, char *tftpname ) {
  144. union {
  145. struct sockaddr_in sin;
  146. struct sockaddr_tcpip st;
  147. } target;
  148. memset ( &target, 0, sizeof ( target ) );
  149. target.sin.sin_family = AF_INET;
  150. target.sin.sin_port = htons ( 69 );
  151. find_global_dhcp_ipv4_option ( DHCP_EB_SIADDR,
  152. &target.sin.sin_addr );
  153. return test_tftp ( netdev, &target.st, tftpname );
  154. }
  155. static int test_dhcp_boot ( struct net_device *netdev, char *filename ) {
  156. /*
  157. if ( strncmp ( filename, "aoe:", 4 ) == 0 ) {
  158. return test_dhcp_aoe_boot ( netdev, &filename[4] );
  159. }
  160. */
  161. // if ( strncmp ( filename, "iscsi:", 6 ) == 0 ) {
  162. if ( ! filename[0] ) {
  163. return test_dhcp_iscsi_boot ( netdev );
  164. }
  165. /*
  166. if ( strncmp ( filename, "ftp:", 4 ) == 0 ) {
  167. return test_dhcp_ftp ( netdev, &filename[4] );
  168. }
  169. */
  170. /*
  171. if ( strncmp ( filename, "hello:", 6 ) == 0 ) {
  172. return test_dhcp_hello ( &filename[6] );
  173. }
  174. if ( strncmp ( filename, "http:", 5 ) == 0 ) {
  175. return test_dhcp_http ( netdev, filename );
  176. }
  177. */
  178. return test_dhcp_tftp ( netdev, filename );
  179. return -EPROTONOSUPPORT;
  180. }
  181. int test_dhcp ( struct net_device *netdev ) {
  182. struct dhcp_session dhcp;
  183. struct in_addr address = { htonl ( 0 ) };
  184. struct in_addr netmask = { htonl ( 0 ) };
  185. struct in_addr gateway = { INADDR_NONE };
  186. char filename[256];
  187. int rc;
  188. /* Issue DHCP request */
  189. printf ( "DHCP (%s)...", netdev->name );
  190. memset ( &dhcp, 0, sizeof ( dhcp ) );
  191. dhcp.netdev = netdev;
  192. if ( ( rc = async_wait ( start_dhcp ( &dhcp ) ) ) != 0 ) {
  193. printf ( "failed\n" );
  194. goto out_no_options;
  195. }
  196. printf ( "done\n" );
  197. /* Register options received via DHCP */
  198. register_dhcp_options ( dhcp.options );
  199. /* Retrieve IP address configuration */
  200. find_global_dhcp_ipv4_option ( DHCP_EB_YIADDR, &address );
  201. find_global_dhcp_ipv4_option ( DHCP_SUBNET_MASK, &netmask );
  202. find_global_dhcp_ipv4_option ( DHCP_ROUTERS, &gateway );
  203. dhcp_snprintf ( filename, sizeof ( filename ),
  204. find_global_dhcp_option ( DHCP_BOOTFILE_NAME ) );
  205. if ( filename[0] )
  206. printf ( "Bootfile name \"%s\"\n", filename );
  207. /* Set up new IP address configuration */
  208. if ( ( rc = add_ipv4_address ( netdev, address, netmask,
  209. gateway ) ) != 0 )
  210. goto out_no_del_ipv4;
  211. route();
  212. /* Test boot */
  213. if ( ( rc = test_dhcp_boot ( netdev, filename ) ) != 0 ) {
  214. printf ( "Boot failed: %s\n", strerror ( rc ) );
  215. goto out;
  216. }
  217. out:
  218. /* Unregister and free DHCP options */
  219. unregister_dhcp_options ( dhcp.options );
  220. dhcpopt_put ( dhcp.options );
  221. out_no_options:
  222. out_no_del_ipv4:
  223. return rc;
  224. }
  225. #endif