2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
29 #include <ipxe/netdevice.h>
30 #include <ipxe/command.h>
31 #include <ipxe/parseopt.h>
32 #include <usr/ifmgmt.h>
33 #include <hci/ifmgmt_cmd.h>
37 * Network interface management commands
42 * Execute if<xxx> command
44 * @v argc Argument count
45 * @v argv Argument list
46 * @v cmd Command descriptor
47 * @v payload Command to execute
48 * @v verb Verb describing the action of the command
49 * @ret rc Return status code
51 int ifcommon_exec ( int argc, char **argv,
52 struct ifcommon_command_descriptor *ifcmd ) {
53 struct command_descriptor *cmd = &ifcmd->cmd;
54 uint8_t opts[cmd->len];
55 struct net_device *netdev;
60 if ( ( rc = parse_options ( argc, argv, cmd, opts ) ) != 0 )
63 if ( optind != argc ) {
64 /* Treat arguments as a list of interfaces to try */
65 for ( i = optind ; i < argc ; i++ ) {
66 if ( ( rc = parse_netdev ( argv[i], &netdev ) ) != 0 )
68 if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
69 && ifcmd->stop_on_first_success ) {
74 /* Try all interfaces */
76 for_each_netdev ( netdev ) {
77 if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
78 && ifcmd->stop_on_first_success ) {
87 /** "ifopen" options */
88 struct ifopen_options {};
90 /** "ifopen" option list */
91 static struct option_descriptor ifopen_opts[] = {};
96 * @v netdev Network device
97 * @v opts Command options
98 * @ret rc Return status code
100 static int ifopen_payload ( struct net_device *netdev,
101 struct ifopen_options *opts __unused ) {
102 return ifopen ( netdev );
105 /** "ifopen" command descriptor */
106 static struct ifcommon_command_descriptor ifopen_cmd =
107 IFCOMMON_COMMAND_DESC ( struct ifopen_options, ifopen_opts,
108 0, MAX_ARGUMENTS, "[<interface>...]",
112 * The "ifopen" command
114 * @v argc Argument count
115 * @v argv Argument list
116 * @ret rc Return status code
118 static int ifopen_exec ( int argc, char **argv ) {
119 return ifcommon_exec ( argc, argv, &ifopen_cmd );
122 /** "ifclose" options */
123 struct ifclose_options {};
125 /** "ifclose" option list */
126 static struct option_descriptor ifclose_opts[] = {};
131 * @v netdev Network device
132 * @v opts Command options
133 * @ret rc Return status code
135 static int ifclose_payload ( struct net_device *netdev,
136 struct ifclose_options *opts __unused ) {
141 /** "ifclose" command descriptor */
142 static struct ifcommon_command_descriptor ifclose_cmd =
143 IFCOMMON_COMMAND_DESC ( struct ifclose_options, ifclose_opts,
144 0, MAX_ARGUMENTS, "[<interface>...]",
145 ifclose_payload, 0 );
148 * The "ifclose" command
150 * @v argc Argument count
151 * @v argv Argument list
152 * @ret rc Return status code
154 static int ifclose_exec ( int argc, char **argv ) {
155 return ifcommon_exec ( argc, argv, &ifclose_cmd );
158 /** "ifstat" options */
159 struct ifstat_options {};
161 /** "ifstat" option list */
162 static struct option_descriptor ifstat_opts[] = {};
167 * @v netdev Network device
168 * @v opts Command options
169 * @ret rc Return status code
171 static int ifstat_payload ( struct net_device *netdev,
172 struct ifstat_options *opts __unused ) {
177 /** "ifstat" command descriptor */
178 static struct ifcommon_command_descriptor ifstat_cmd =
179 IFCOMMON_COMMAND_DESC ( struct ifstat_options, ifstat_opts,
180 0, MAX_ARGUMENTS, "[<interface>...]",
184 * The "ifstat" command
186 * @v argc Argument count
187 * @v argv Argument list
188 * @ret rc Return status code
190 static int ifstat_exec ( int argc, char **argv ) {
191 return ifcommon_exec ( argc, argv, &ifstat_cmd );
194 /** "ifconf" options */
195 struct ifconf_options {
197 struct net_device_configurator *configurator;
200 /** "ifconf" option list */
201 static struct option_descriptor ifconf_opts[] = {
202 OPTION_DESC ( "configurator", 'c', required_argument,
203 struct ifconf_options, configurator,
204 parse_netdev_configurator ),
210 * @v netdev Network device
211 * @v opts Command options
212 * @ret rc Return status code
214 static int ifconf_payload ( struct net_device *netdev,
215 struct ifconf_options *opts ) {
218 /* Attempt configuration */
219 if ( ( rc = ifconf ( netdev, opts->configurator ) ) != 0 ) {
221 /* Close device on failure, to avoid memory exhaustion */
222 netdev_close ( netdev );
230 /** "ifconf" command descriptor */
231 static struct ifcommon_command_descriptor ifconf_cmd =
232 IFCOMMON_COMMAND_DESC ( struct ifconf_options, ifconf_opts,
233 0, MAX_ARGUMENTS, "[<interface>...]",
237 * The "ifconf" command
239 * @v argc Argument count
240 * @v argv Argument list
241 * @ret rc Return status code
243 int ifconf_exec ( int argc, char **argv ) {
244 return ifcommon_exec ( argc, argv, &ifconf_cmd );
247 /** Interface management commands */
248 struct command ifmgmt_commands[] __command = {
255 .exec = ifclose_exec,