You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dhcptest.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include <string.h>
  2. #include <vsprintf.h>
  3. #include <byteswap.h>
  4. #include <gpxe/ip.h>
  5. #include <gpxe/dhcp.h>
  6. #include <gpxe/iscsi.h>
  7. #include <gpxe/netdevice.h>
  8. static int test_dhcp_aoe_boot ( struct net_device *netdev,
  9. char *aoename ) {
  10. unsigned int drivenum;
  11. drivenum = find_global_dhcp_num_option ( DHCP_EB_BIOS_DRIVE );
  12. return test_aoeboot ( netdev, aoename, drivenum );
  13. }
  14. static int test_dhcp_iscsi_boot ( struct net_device *netdev, char *iscsiname ) {
  15. char *initiator_iqn = "iqn.1900-01.localdomain.localhost:initiator";
  16. char *target_iqn;
  17. union {
  18. struct sockaddr_in sin;
  19. struct sockaddr_tcpip st;
  20. } target;
  21. memset ( &target, 0, sizeof ( target ) );
  22. target.sin.sin_family = AF_INET;
  23. target.sin.sin_port = htons ( ISCSI_PORT );
  24. target_iqn = strchr ( iscsiname, ':' );
  25. *target_iqn++ = '\0';
  26. if ( ! target_iqn ) {
  27. printf ( "Invalid iSCSI DHCP path\n" );
  28. return -EINVAL;
  29. }
  30. inet_aton ( iscsiname, &target.sin.sin_addr );
  31. return test_iscsiboot ( initiator_iqn, &target.st, target_iqn, netdev );
  32. }
  33. static int test_dhcp_hello ( char *helloname ) {
  34. char *message;
  35. union {
  36. struct sockaddr_in sin;
  37. struct sockaddr_tcpip st;
  38. } target;
  39. memset ( &target, 0, sizeof ( target ) );
  40. target.sin.sin_family = AF_INET;
  41. target.sin.sin_port = htons ( 80 );
  42. message = strchr ( helloname, ':' );
  43. *message++ = '\0';
  44. if ( ! message ) {
  45. printf ( "Invalid hello path\n" );
  46. return -EINVAL;
  47. }
  48. inet_aton ( helloname, &target.sin.sin_addr );
  49. test_hello ( &target.st, message );
  50. return 0;
  51. }
  52. static int test_dhcp_http ( struct net_device *netdev, char *url ) {
  53. union {
  54. struct sockaddr_in sin;
  55. struct sockaddr_tcpip st;
  56. } target;
  57. memset ( &target, 0, sizeof ( target ) );
  58. target.sin.sin_family = AF_INET;
  59. target.sin.sin_port = htons ( 80 );
  60. char *addr = url + 7; // http://
  61. char *file = strchr(addr, '/');
  62. *file = '\0'; // for printf and inet_aton to work
  63. printf("connecting to %s\n", addr);
  64. inet_aton ( addr, &target.sin.sin_addr );
  65. *file = '/';
  66. test_http ( netdev, &target.st, file );
  67. return 0;
  68. }
  69. static int test_dhcp_tftp ( struct net_device *netdev, char *tftpname ) {
  70. union {
  71. struct sockaddr_in sin;
  72. struct sockaddr_tcpip st;
  73. } target;
  74. memset ( &target, 0, sizeof ( target ) );
  75. target.sin.sin_family = AF_INET;
  76. target.sin.sin_port = htons ( 69 );
  77. find_global_dhcp_ipv4_option ( DHCP_EB_SIADDR,
  78. &target.sin.sin_addr );
  79. return test_tftp ( netdev, &target.st, tftpname );
  80. }
  81. static int test_dhcp_boot ( struct net_device *netdev, char *filename ) {
  82. if ( strncmp ( filename, "aoe:", 4 ) == 0 ) {
  83. return test_dhcp_aoe_boot ( netdev, &filename[4] );
  84. } else if ( strncmp ( filename, "iscsi:", 6 ) == 0 ) {
  85. return test_dhcp_iscsi_boot ( netdev, &filename[6] );
  86. } else if ( strncmp ( filename, "hello:", 6 ) == 0 ) {
  87. return test_dhcp_hello ( &filename[6] );
  88. } else if ( strncmp ( filename, "http:", 5 ) == 0 ) {
  89. return test_dhcp_http ( netdev, filename );
  90. } else {
  91. return test_dhcp_tftp ( netdev, filename );
  92. }
  93. }
  94. int test_dhcp ( struct net_device *netdev ) {
  95. struct dhcp_session dhcp;
  96. struct in_addr address = { htonl ( 0 ) };
  97. struct in_addr netmask = { htonl ( 0 ) };
  98. struct in_addr gateway = { INADDR_NONE };
  99. char filename[256];
  100. int rc;
  101. /* Bring IP interface up with address 0.0.0.0 */
  102. if ( ( rc = add_ipv4_address ( netdev, address, netmask,
  103. gateway ) ) != 0 )
  104. goto out_no_del_ipv4;
  105. /* Issue DHCP request */
  106. printf ( "DHCP (%s)...", netdev_name ( netdev ) );
  107. memset ( &dhcp, 0, sizeof ( dhcp ) );
  108. dhcp.netdev = netdev;
  109. if ( ( rc = async_wait ( start_dhcp ( &dhcp ) ) ) != 0 ) {
  110. printf ( "failed\n" );
  111. goto out_no_options;
  112. }
  113. printf ( "done\n" );
  114. /* Register options received via DHCP */
  115. register_dhcp_options ( dhcp.options );
  116. /* Retrieve IP address configuration */
  117. find_global_dhcp_ipv4_option ( DHCP_EB_YIADDR, &address );
  118. find_global_dhcp_ipv4_option ( DHCP_SUBNET_MASK, &netmask );
  119. find_global_dhcp_ipv4_option ( DHCP_ROUTERS, &gateway );
  120. printf ( "IP %s", inet_ntoa ( address ) );
  121. printf ( " netmask %s", inet_ntoa ( netmask ) );
  122. printf ( " gateway %s\n", inet_ntoa ( gateway ) );
  123. dhcp_snprintf ( filename, sizeof ( filename ),
  124. find_global_dhcp_option ( DHCP_BOOTFILE_NAME ) );
  125. if ( ! filename[0] ) {
  126. printf ( "No filename specified!\n" );
  127. goto out;
  128. }
  129. printf ( "Bootfile name %s\n", filename );
  130. /* Remove old IP address configuration */
  131. del_ipv4_address ( netdev );
  132. /* Set up new IP address configuration */
  133. if ( ( rc = add_ipv4_address ( netdev, address, netmask,
  134. gateway ) ) != 0 )
  135. goto out_no_del_ipv4;
  136. /* Test boot */
  137. if ( ( rc = test_dhcp_boot ( netdev, filename ) ) != 0 ) {
  138. printf ( "Boot failed\n" );
  139. goto out;
  140. }
  141. out:
  142. /* Unregister and free DHCP options */
  143. unregister_dhcp_options ( dhcp.options );
  144. free_dhcp_options ( dhcp.options );
  145. out_no_options:
  146. /* Take down IP interface */
  147. del_ipv4_address ( netdev );
  148. out_no_del_ipv4:
  149. return rc;
  150. }