blob: c8cea7c54e8cd6073655e1bcc3e25a66fa31d859 [file] [log] [blame]
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001/*
2 * Zebra configuration command interface routine
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23#ifndef _ZEBRA_COMMAND_H
24#define _ZEBRA_COMMAND_H
25
26#include <stdio.h>
27#include <sys/types.h>
28#include "vector.h"
29#include "vty.h"
30
Harald Weltee881b1b2011-08-17 18:52:30 +020031/*! \defgroup command VTY Command
Harald Welte7acb30c2011-08-17 17:13:48 +020032 * @{
33 */
34/*! \file command.h */
35
36/*! \brief Host configuration variable */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020037struct host {
Harald Welte7acb30c2011-08-17 17:13:48 +020038 /*! \brief Host name of this router. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020039 char *name;
40
Harald Welte7acb30c2011-08-17 17:13:48 +020041 /*! \brief Password for vty interface. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020042 char *password;
43 char *password_encrypt;
44
Harald Welte7acb30c2011-08-17 17:13:48 +020045 /*! \brief Enable password */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020046 char *enable;
47 char *enable_encrypt;
48
Harald Welte7acb30c2011-08-17 17:13:48 +020049 /*! \brief System wide terminal lines. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020050 int lines;
51
Harald Welte7acb30c2011-08-17 17:13:48 +020052 /*! \brief Log filename. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020053 char *logfile;
54
Harald Welte7acb30c2011-08-17 17:13:48 +020055 /*! \brief config file name of this host */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020056 char *config;
57
Harald Welte7acb30c2011-08-17 17:13:48 +020058 /*! \brief Flags for services */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020059 int advanced;
60 int encrypt;
61
Harald Welte7acb30c2011-08-17 17:13:48 +020062 /*! \brief Banner configuration. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020063 const char *motd;
64 char *motdfile;
65
Harald Welte7acb30c2011-08-17 17:13:48 +020066 /*! \brief VTY application information */
Harald Welte237f6242010-05-25 23:00:45 +020067 const struct vty_app_info *app_info;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020068};
69
Harald Welte7acb30c2011-08-17 17:13:48 +020070/*! \brief There are some command levels which called from command node. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020071enum node_type {
Harald Welte7acb30c2011-08-17 17:13:48 +020072 AUTH_NODE, /*!< \brief Authentication mode of vty interface. */
73 VIEW_NODE, /*!< \brief View node. Default mode of vty interface. */
74 AUTH_ENABLE_NODE, /*!< \brief Authentication mode for change enable. */
75 ENABLE_NODE, /*!< \brief Enable node. */
76 CONFIG_NODE, /*!< \brief Config node. Default mode of config file. */
77 SERVICE_NODE, /*!< \brief Service node. */
78 DEBUG_NODE, /*!< \brief Debug node. */
79 CFG_LOG_NODE, /*!< \brief Configure the logging */
Harald Welte4c053012010-05-31 16:01:59 +020080
Harald Welte7acb30c2011-08-17 17:13:48 +020081 VTY_NODE, /*!< \brief Vty node. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020082
Harald Welte7acb30c2011-08-17 17:13:48 +020083 L_E1INP_NODE, /*!< \brief E1 line in libosmo-abis. */
84 L_IPA_NODE, /*!< \brief IPA proxying commands in libosmo-abis. */
Harald Welted85d0152012-06-16 16:54:19 +080085 L_NS_NODE, /*!< \brief NS node in libosmo-gb. */
86 L_BSSGP_NODE, /*!< \brief BSSGP node in libosmo-gb. */
Pablo Neira Ayuso2ade3a02011-07-07 19:46:50 +020087
Harald Welte4c053012010-05-31 16:01:59 +020088 _LAST_OSMOVTY_NODE
Harald Welte3fb0b6f2010-05-19 19:02:52 +020089};
90
Harald Welte7acb30c2011-08-17 17:13:48 +020091/*! \brief Node which has some commands and prompt string and
92 * configuration function pointer . */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020093struct cmd_node {
Harald Welte7acb30c2011-08-17 17:13:48 +020094 /*! \brief Node index */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020095 enum node_type node;
96
Harald Welte7acb30c2011-08-17 17:13:48 +020097 /*! \brief Prompt character at vty interface. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020098 const char *prompt;
99
Harald Welte7acb30c2011-08-17 17:13:48 +0200100 /*! \brief Is this node's configuration goes to vtysh ? */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200101 int vtysh;
102
Harald Welte7acb30c2011-08-17 17:13:48 +0200103 /*! \brief Node's configuration write function */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200104 int (*func) (struct vty *);
105
Harald Welte7acb30c2011-08-17 17:13:48 +0200106 /*! \brief Vector of this node's command list. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200107 vector cmd_vector;
108};
109
110enum {
111 CMD_ATTR_DEPRECATED = 1,
112 CMD_ATTR_HIDDEN,
113};
114
Harald Welte7acb30c2011-08-17 17:13:48 +0200115/*! \brief Structure of a command element */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200116struct cmd_element {
Harald Welte7acb30c2011-08-17 17:13:48 +0200117 const char *string; /*!< \brief Command specification by string. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200118 int (*func) (struct cmd_element *, struct vty *, int, const char *[]);
Harald Welte7acb30c2011-08-17 17:13:48 +0200119 const char *doc; /*!< \brief Documentation of this command. */
120 int daemon; /*!< \brief Daemon to which this command belong. */
121 vector strvec; /*!< \brief Pointing out each description vector. */
122 unsigned int cmdsize; /*!< \brief Command index count. */
123 char *config; /*!< \brief Configuration string */
124 vector subconfig; /*!< \brief Sub configuration string */
125 u_char attr; /*!< \brief Command attributes */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200126};
127
Harald Welte7acb30c2011-08-17 17:13:48 +0200128/*! \brief Command description structure. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200129struct desc {
Harald Welte7acb30c2011-08-17 17:13:48 +0200130 const char *cmd; /*!< \brief Command string. */
131 const char *str; /*!< \brief Command's description. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200132};
133
Harald Welte7acb30c2011-08-17 17:13:48 +0200134/*! \brief Return value of the commands. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200135#define CMD_SUCCESS 0
136#define CMD_WARNING 1
137#define CMD_ERR_NO_MATCH 2
138#define CMD_ERR_AMBIGUOUS 3
139#define CMD_ERR_INCOMPLETE 4
140#define CMD_ERR_EXEED_ARGC_MAX 5
141#define CMD_ERR_NOTHING_TODO 6
142#define CMD_COMPLETE_FULL_MATCH 7
143#define CMD_COMPLETE_MATCH 8
144#define CMD_COMPLETE_LIST_MATCH 9
145#define CMD_SUCCESS_DAEMON 10
146
147/* Argc max counts. */
Holger Hans Peter Freytherc0dbe0b2011-07-24 19:58:06 +0200148#define CMD_ARGC_MAX 256
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200149
150/* Turn off these macros when uisng cpp with extract.pl */
151#ifndef VTYSH_EXTRACT_PL
152
153/* helper defines for end-user DEFUN* macros */
154#define DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
155 static struct cmd_element cmdname = \
156 { \
157 .string = cmdstr, \
158 .func = funcname, \
159 .doc = helpstr, \
160 .attr = attrs, \
161 .daemon = dnum, \
162 };
163
164/* global (non static) cmd_element */
165#define gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
166 struct cmd_element cmdname = \
167 { \
168 .string = cmdstr, \
169 .func = funcname, \
170 .doc = helpstr, \
171 .attr = attrs, \
172 .daemon = dnum, \
173 };
174
175#define DEFUN_CMD_FUNC_DECL(funcname) \
176 static int funcname (struct cmd_element *, struct vty *, int, const char *[]); \
177
178#define DEFUN_CMD_FUNC_TEXT(funcname) \
179 static int funcname \
180 (struct cmd_element *self, struct vty *vty, int argc, const char *argv[])
181
Harald Welte7acb30c2011-08-17 17:13:48 +0200182/*! \brief Macro for defining a VTY node and function
183 * \param[in] funcname Name of the function implementing the node
184 * \param[in] cmdname Name of the command node
185 * \param[in] cmdstr String with syntax of node
186 * \param[in] helpstr String with help message of node
187 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200188#define DEFUN(funcname, cmdname, cmdstr, helpstr) \
189 DEFUN_CMD_FUNC_DECL(funcname) \
190 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
191 DEFUN_CMD_FUNC_TEXT(funcname)
192
Harald Welte7acb30c2011-08-17 17:13:48 +0200193/*! \brief Macro for defining a non-static (global) VTY node and function
194 * \param[in] funcname Name of the function implementing the node
195 * \param[in] cmdname Name of the command node
196 * \param[in] cmdstr String with syntax of node
197 * \param[in] helpstr String with help message of node
198 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200199#define gDEFUN(funcname, cmdname, cmdstr, helpstr) \
200 DEFUN_CMD_FUNC_DECL(funcname) \
201 gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
202 DEFUN_CMD_FUNC_TEXT(funcname)
203
204#define DEFUN_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
205 DEFUN_CMD_FUNC_DECL(funcname) \
206 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0) \
207 DEFUN_CMD_FUNC_TEXT(funcname)
208
209#define DEFUN_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
210 DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
211
212#define DEFUN_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
213 DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED) \
214
215/* DEFUN_NOSH for commands that vtysh should ignore */
216#define DEFUN_NOSH(funcname, cmdname, cmdstr, helpstr) \
217 DEFUN(funcname, cmdname, cmdstr, helpstr)
218
219/* DEFSH for vtysh. */
220#define DEFSH(daemon, cmdname, cmdstr, helpstr) \
221 DEFUN_CMD_ELEMENT(NULL, cmdname, cmdstr, helpstr, 0, daemon) \
222
223/* DEFUN + DEFSH */
224#define DEFUNSH(daemon, funcname, cmdname, cmdstr, helpstr) \
225 DEFUN_CMD_FUNC_DECL(funcname) \
226 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon) \
227 DEFUN_CMD_FUNC_TEXT(funcname)
228
229/* DEFUN + DEFSH with attributes */
230#define DEFUNSH_ATTR(daemon, funcname, cmdname, cmdstr, helpstr, attr) \
231 DEFUN_CMD_FUNC_DECL(funcname) \
232 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, daemon) \
233 DEFUN_CMD_FUNC_TEXT(funcname)
234
235#define DEFUNSH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
236 DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
237
238#define DEFUNSH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
239 DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED)
240
241/* ALIAS macro which define existing command's alias. */
242#define ALIAS(funcname, cmdname, cmdstr, helpstr) \
243 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
244
245/* global (non static) cmd_element */
246#define gALIAS(funcname, cmdname, cmdstr, helpstr) \
247 gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
248
249#define ALIAS_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
250 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0)
251
252#define ALIAS_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
253 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, 0)
254
255#define ALIAS_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
256 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, 0)
257
258#define ALIAS_SH(daemon, funcname, cmdname, cmdstr, helpstr) \
259 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon)
260
261#define ALIAS_SH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
262 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, daemon)
263
264#define ALIAS_SH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
265 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, daemon)
266
267#endif /* VTYSH_EXTRACT_PL */
268
269/* Some macroes */
270#define CMD_OPTION(S) ((S[0]) == '[')
271#define CMD_VARIABLE(S) (((S[0]) >= 'A' && (S[0]) <= 'Z') || ((S[0]) == '<'))
272#define CMD_VARARG(S) ((S[0]) == '.')
273#define CMD_RANGE(S) ((S[0] == '<'))
274
275#define CMD_IPV4(S) ((strcmp ((S), "A.B.C.D") == 0))
276#define CMD_IPV4_PREFIX(S) ((strcmp ((S), "A.B.C.D/M") == 0))
277#define CMD_IPV6(S) ((strcmp ((S), "X:X::X:X") == 0))
278#define CMD_IPV6_PREFIX(S) ((strcmp ((S), "X:X::X:X/M") == 0))
279
280/* Common descriptions. */
281#define SHOW_STR "Show running system information\n"
282#define IP_STR "IP information\n"
283#define IPV6_STR "IPv6 information\n"
284#define NO_STR "Negate a command or set its defaults\n"
285#define CLEAR_STR "Reset functions\n"
286#define RIP_STR "RIP information\n"
287#define BGP_STR "BGP information\n"
288#define OSPF_STR "OSPF information\n"
289#define NEIGHBOR_STR "Specify neighbor router\n"
290#define DEBUG_STR "Debugging functions (see also 'undebug')\n"
291#define UNDEBUG_STR "Disable debugging functions (see also 'debug')\n"
292#define ROUTER_STR "Enable a routing process\n"
293#define AS_STR "AS number\n"
294#define MBGP_STR "MBGP information\n"
295#define MATCH_STR "Match values from routing table\n"
296#define SET_STR "Set values in destination routing protocol\n"
297#define OUT_STR "Filter outgoing routing updates\n"
298#define IN_STR "Filter incoming routing updates\n"
299#define V4NOTATION_STR "specify by IPv4 address notation(e.g. 0.0.0.0)\n"
300#define OSPF6_NUMBER_STR "Specify by number\n"
301#define INTERFACE_STR "Interface infomation\n"
302#define IFNAME_STR "Interface name(e.g. ep0)\n"
303#define IP6_STR "IPv6 Information\n"
304#define OSPF6_STR "Open Shortest Path First (OSPF) for IPv6\n"
305#define OSPF6_ROUTER_STR "Enable a routing process\n"
306#define OSPF6_INSTANCE_STR "<1-65535> Instance ID\n"
307#define SECONDS_STR "<1-65535> Seconds\n"
308#define ROUTE_STR "Routing Table\n"
309#define PREFIX_LIST_STR "Build a prefix list\n"
310#define OSPF6_DUMP_TYPE_LIST \
311"(neighbor|interface|area|lsa|zebra|config|dbex|spf|route|lsdb|redistribute|hook|asbr|prefix|abr)"
312#define ISIS_STR "IS-IS information\n"
313#define AREA_TAG_STR "[area tag]\n"
314
315#define CONF_BACKUP_EXT ".sav"
316
317/* IPv4 only machine should not accept IPv6 address for peer's IP
318 address. So we replace VTY command string like below. */
319#ifdef HAVE_IPV6
320#define NEIGHBOR_CMD "neighbor (A.B.C.D|X:X::X:X) "
321#define NO_NEIGHBOR_CMD "no neighbor (A.B.C.D|X:X::X:X) "
322#define NEIGHBOR_ADDR_STR "Neighbor address\nIPv6 address\n"
323#define NEIGHBOR_CMD2 "neighbor (A.B.C.D|X:X::X:X|WORD) "
324#define NO_NEIGHBOR_CMD2 "no neighbor (A.B.C.D|X:X::X:X|WORD) "
325#define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor IPv6 address\nNeighbor tag\n"
326#else
327#define NEIGHBOR_CMD "neighbor A.B.C.D "
328#define NO_NEIGHBOR_CMD "no neighbor A.B.C.D "
329#define NEIGHBOR_ADDR_STR "Neighbor address\n"
330#define NEIGHBOR_CMD2 "neighbor (A.B.C.D|WORD) "
331#define NO_NEIGHBOR_CMD2 "no neighbor (A.B.C.D|WORD) "
332#define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor tag\n"
333#endif /* HAVE_IPV6 */
334
335/* Prototypes. */
336void install_node(struct cmd_node *, int (*)(struct vty *));
337void install_default(enum node_type);
338void install_element(enum node_type, struct cmd_element *);
339void install_element_ve(struct cmd_element *cmd);
Harald Welte95b2b472011-07-16 11:58:09 +0200340void sort_node(void);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200341
342/* Concatenates argv[shift] through argv[argc-1] into a single NUL-terminated
343 string with a space between each element (allocated using
344 XMALLOC(MTYPE_TMP)). Returns NULL if shift >= argc. */
345char *argv_concat(const char **argv, int argc, int shift);
346
347vector cmd_make_strvec(const char *);
348void cmd_free_strvec(vector);
349vector cmd_describe_command();
350char **cmd_complete_command();
351const char *cmd_prompt(enum node_type);
352int config_from_file(struct vty *, FILE *);
353enum node_type node_parent(enum node_type);
354int cmd_execute_command(vector, struct vty *, struct cmd_element **, int);
355int cmd_execute_command_strict(vector, struct vty *, struct cmd_element **);
356void config_replace_string(struct cmd_element *, char *, ...);
357void cmd_init(int);
358
359/* Export typical functions. */
360extern struct cmd_element config_exit_cmd;
361extern struct cmd_element config_help_cmd;
362extern struct cmd_element config_list_cmd;
363char *host_config_file();
364void host_config_set(const char *);
365
366/* This is called from main when a daemon is invoked with -v or --version. */
367void print_version(int print_copyright);
368
369extern void *tall_vty_cmd_ctx;
370
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200371/*! @} */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200372#endif /* _ZEBRA_COMMAND_H */