dhcp_cmd.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdio.h>
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <stddef.h>
  25. #include <string.h>
  26. #include <assert.h>
  27. #include <getopt.h>
  28. #include <ipxe/netdevice.h>
  29. #include <ipxe/in.h>
  30. #include <ipxe/command.h>
  31. #include <ipxe/parseopt.h>
  32. #include <usr/dhcpmgmt.h>
  33. #include <hci/ifmgmt_cmd.h>
  34. /** @file
  35. *
  36. * DHCP management commands
  37. *
  38. */
  39. /** "dhcp" command descriptor */
  40. static struct command_descriptor dhcp_cmd =
  41. COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
  42. "[<interface>] [<interface>...]",
  43. "Configure network interface(s) using DHCP" );
  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. "Perform PXE Boot Server discovery" );
  80. /**
  81. * The "pxebs" command
  82. *
  83. * @v argc Argument count
  84. * @v argv Argument list
  85. * @ret rc Return status code
  86. */
  87. static int pxebs_exec ( int argc, char **argv ) {
  88. struct pxebs_options opts;
  89. struct net_device *netdev;
  90. unsigned int pxe_type;
  91. int rc;
  92. /* Parse options */
  93. if ( ( rc = parse_options ( argc, argv, &pxebs_cmd, &opts ) ) != 0 )
  94. return rc;
  95. /* Parse net device name */
  96. if ( ( rc = parse_netdev ( argv[optind], &netdev ) ) != 0 )
  97. return rc;
  98. /* Parse boot server type */
  99. if ( ( rc = parse_integer ( argv[ optind + 1 ], &pxe_type ) ) != 0 )
  100. return rc;
  101. /* Perform Boot Server Discovery */
  102. if ( ( rc = pxebs ( netdev, pxe_type ) ) != 0 ) {
  103. printf ( "Could not discover boot server on %s: %s\n",
  104. netdev->name, strerror ( rc ) );
  105. return rc;
  106. }
  107. return 0;
  108. }
  109. /** DHCP management commands */
  110. struct command dhcp_commands[] __command = {
  111. {
  112. .name = "dhcp",
  113. .exec = dhcp_exec,
  114. },
  115. {
  116. .name = "pxebs",
  117. .exec = pxebs_exec,
  118. },
  119. };