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.

dhcp_cmd.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdio.h>
  21. #include <stdint.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <errno.h>
  25. #include <stddef.h>
  26. #include <string.h>
  27. #include <assert.h>
  28. #include <getopt.h>
  29. #include <ipxe/netdevice.h>
  30. #include <ipxe/in.h>
  31. #include <ipxe/command.h>
  32. #include <ipxe/parseopt.h>
  33. #include <usr/dhcpmgmt.h>
  34. #include <hci/ifmgmt_cmd.h>
  35. /** @file
  36. *
  37. * DHCP management commands
  38. *
  39. */
  40. /** "dhcp" command descriptor */
  41. static struct command_descriptor dhcp_cmd =
  42. COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
  43. "[<interface>...]" );
  44. /**
  45. * Execute "dhcp" command for a network device
  46. *
  47. * @v netdev Network device
  48. * @ret rc Return status code
  49. */
  50. static int dhcp_payload ( struct net_device *netdev ) {
  51. int rc;
  52. if ( ( rc = dhcp ( netdev ) ) != 0 ) {
  53. printf ( "Could not configure %s: %s\n",
  54. netdev->name, strerror ( rc ) );
  55. /* Close device on failure, to avoid memory exhaustion */
  56. netdev_close ( netdev );
  57. return rc;
  58. }
  59. return 0;
  60. }
  61. /**
  62. * The "dhcp" command
  63. *
  64. * @v argc Argument count
  65. * @v argv Argument list
  66. * @ret rc Return status code
  67. */
  68. static int dhcp_exec ( int argc, char **argv ) {
  69. return ifcommon_exec ( argc, argv, &dhcp_cmd, dhcp_payload, 1 );
  70. }
  71. /** "pxebs" options */
  72. struct pxebs_options {};
  73. /** "pxebs" option list */
  74. static struct option_descriptor pxebs_opts[] = {};
  75. /** "pxebs" command descriptor */
  76. static struct command_descriptor pxebs_cmd =
  77. COMMAND_DESC ( struct pxebs_options, pxebs_opts, 2, 2,
  78. "<interface> <server type>" );
  79. /**
  80. * The "pxebs" command
  81. *
  82. * @v argc Argument count
  83. * @v argv Argument list
  84. * @ret rc Return status code
  85. */
  86. static int pxebs_exec ( int argc, char **argv ) {
  87. struct pxebs_options opts;
  88. struct net_device *netdev;
  89. unsigned int pxe_type;
  90. int rc;
  91. /* Parse options */
  92. if ( ( rc = parse_options ( argc, argv, &pxebs_cmd, &opts ) ) != 0 )
  93. return rc;
  94. /* Parse net device name */
  95. if ( ( rc = parse_netdev ( argv[optind], &netdev ) ) != 0 )
  96. return rc;
  97. /* Parse boot server type */
  98. if ( ( rc = parse_integer ( argv[ optind + 1 ], &pxe_type ) ) != 0 )
  99. return rc;
  100. /* Perform Boot Server Discovery */
  101. if ( ( rc = pxebs ( netdev, pxe_type ) ) != 0 ) {
  102. printf ( "Could not discover boot server on %s: %s\n",
  103. netdev->name, strerror ( rc ) );
  104. return rc;
  105. }
  106. return 0;
  107. }
  108. /** DHCP management commands */
  109. struct command dhcp_commands[] __command = {
  110. {
  111. .name = "dhcp",
  112. .exec = dhcp_exec,
  113. },
  114. {
  115. .name = "pxebs",
  116. .exec = pxebs_exec,
  117. },
  118. };