[tcpip] Allow supported address families to be detected at runtime
[ipxe.git] / src / hci / commands / ifmgmt_cmd.c
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 * 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.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <getopt.h>
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>
34
35 /** @file
36 *
37 * Network interface management commands
38 *
39 */
40
41 /**
42 * Execute if<xxx> command
43 *
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
50 */
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;
56 int i;
57 int rc;
58
59 /* Parse options */
60 if ( ( rc = parse_options ( argc, argv, cmd, opts ) ) != 0 )
61 return rc;
62
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 )
67 continue;
68 if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
69 && ifcmd->stop_on_first_success ) {
70 return 0;
71 }
72 }
73 } else {
74 /* Try all interfaces */
75 rc = -ENODEV;
76 for_each_netdev ( netdev ) {
77 if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
78 && ifcmd->stop_on_first_success ) {
79 return 0;
80 }
81 }
82 }
83
84 return rc;
85 }
86
87 /** "ifopen" options */
88 struct ifopen_options {};
89
90 /** "ifopen" option list */
91 static struct option_descriptor ifopen_opts[] = {};
92
93 /**
94 * "ifopen" payload
95 *
96 * @v netdev Network device
97 * @v opts Command options
98 * @ret rc Return status code
99 */
100 static int ifopen_payload ( struct net_device *netdev,
101 struct ifopen_options *opts __unused ) {
102 return ifopen ( netdev );
103 }
104
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>...]",
109 ifopen_payload, 0 );
110
111 /**
112 * The "ifopen" command
113 *
114 * @v argc Argument count
115 * @v argv Argument list
116 * @ret rc Return status code
117 */
118 static int ifopen_exec ( int argc, char **argv ) {
119 return ifcommon_exec ( argc, argv, &ifopen_cmd );
120 }
121
122 /** "ifclose" options */
123 struct ifclose_options {};
124
125 /** "ifclose" option list */
126 static struct option_descriptor ifclose_opts[] = {};
127
128 /**
129 * "ifclose" payload
130 *
131 * @v netdev Network device
132 * @v opts Command options
133 * @ret rc Return status code
134 */
135 static int ifclose_payload ( struct net_device *netdev,
136 struct ifclose_options *opts __unused ) {
137 ifclose ( netdev );
138 return 0;
139 }
140
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 );
146
147 /**
148 * The "ifclose" command
149 *
150 * @v argc Argument count
151 * @v argv Argument list
152 * @ret rc Return status code
153 */
154 static int ifclose_exec ( int argc, char **argv ) {
155 return ifcommon_exec ( argc, argv, &ifclose_cmd );
156 }
157
158 /** "ifstat" options */
159 struct ifstat_options {};
160
161 /** "ifstat" option list */
162 static struct option_descriptor ifstat_opts[] = {};
163
164 /**
165 * "ifstat" payload
166 *
167 * @v netdev Network device
168 * @v opts Command options
169 * @ret rc Return status code
170 */
171 static int ifstat_payload ( struct net_device *netdev,
172 struct ifstat_options *opts __unused ) {
173 ifstat ( netdev );
174 return 0;
175 }
176
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>...]",
181 ifstat_payload, 0 );
182
183 /**
184 * The "ifstat" command
185 *
186 * @v argc Argument count
187 * @v argv Argument list
188 * @ret rc Return status code
189 */
190 static int ifstat_exec ( int argc, char **argv ) {
191 return ifcommon_exec ( argc, argv, &ifstat_cmd );
192 }
193
194 /** "ifconf" options */
195 struct ifconf_options {
196 /** Configurator */
197 struct net_device_configurator *configurator;
198 };
199
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 ),
205 };
206
207 /**
208 * "ifconf" payload
209 *
210 * @v netdev Network device
211 * @v opts Command options
212 * @ret rc Return status code
213 */
214 static int ifconf_payload ( struct net_device *netdev,
215 struct ifconf_options *opts ) {
216 int rc;
217
218 /* Attempt configuration */
219 if ( ( rc = ifconf ( netdev, opts->configurator ) ) != 0 ) {
220
221 /* Close device on failure, to avoid memory exhaustion */
222 netdev_close ( netdev );
223
224 return rc;
225 }
226
227 return 0;
228 }
229
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>...]",
234 ifconf_payload, 1 );
235
236 /**
237 * The "ifconf" command
238 *
239 * @v argc Argument count
240 * @v argv Argument list
241 * @ret rc Return status code
242 */
243 int ifconf_exec ( int argc, char **argv ) {
244 return ifcommon_exec ( argc, argv, &ifconf_cmd );
245 }
246
247 /** Interface management commands */
248 struct command ifmgmt_commands[] __command = {
249 {
250 .name = "ifopen",
251 .exec = ifopen_exec,
252 },
253 {
254 .name = "ifclose",
255 .exec = ifclose_exec,
256 },
257 {
258 .name = "ifstat",
259 .exec = ifstat_exec,
260 },
261 {
262 .name = "ifconf",
263 .exec = ifconf_exec,
264 },
265 };