blob: 79d0aecf79dff820bed61856bef53dff04d7948d [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. */
Pablo Neira Ayuso2ade3a02011-07-07 19:46:50 +020085
Harald Welte4c053012010-05-31 16:01:59 +020086 _LAST_OSMOVTY_NODE
Harald Welte3fb0b6f2010-05-19 19:02:52 +020087};
88
Harald Welte7acb30c2011-08-17 17:13:48 +020089/*! \brief Node which has some commands and prompt string and
90 * configuration function pointer . */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020091struct cmd_node {
Harald Welte7acb30c2011-08-17 17:13:48 +020092 /*! \brief Node index */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020093 enum node_type node;
94
Harald Welte7acb30c2011-08-17 17:13:48 +020095 /*! \brief Prompt character at vty interface. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020096 const char *prompt;
97
Harald Welte7acb30c2011-08-17 17:13:48 +020098 /*! \brief Is this node's configuration goes to vtysh ? */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020099 int vtysh;
100
Harald Welte7acb30c2011-08-17 17:13:48 +0200101 /*! \brief Node's configuration write function */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200102 int (*func) (struct vty *);
103
Harald Welte7acb30c2011-08-17 17:13:48 +0200104 /*! \brief Vector of this node's command list. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200105 vector cmd_vector;
106};
107
108enum {
109 CMD_ATTR_DEPRECATED = 1,
110 CMD_ATTR_HIDDEN,
111};
112
Harald Welte7acb30c2011-08-17 17:13:48 +0200113/*! \brief Structure of a command element */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200114struct cmd_element {
Harald Welte7acb30c2011-08-17 17:13:48 +0200115 const char *string; /*!< \brief Command specification by string. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200116 int (*func) (struct cmd_element *, struct vty *, int, const char *[]);
Harald Welte7acb30c2011-08-17 17:13:48 +0200117 const char *doc; /*!< \brief Documentation of this command. */
118 int daemon; /*!< \brief Daemon to which this command belong. */
119 vector strvec; /*!< \brief Pointing out each description vector. */
120 unsigned int cmdsize; /*!< \brief Command index count. */
121 char *config; /*!< \brief Configuration string */
122 vector subconfig; /*!< \brief Sub configuration string */
123 u_char attr; /*!< \brief Command attributes */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200124};
125
Harald Welte7acb30c2011-08-17 17:13:48 +0200126/*! \brief Command description structure. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200127struct desc {
Harald Welte7acb30c2011-08-17 17:13:48 +0200128 const char *cmd; /*!< \brief Command string. */
129 const char *str; /*!< \brief Command's description. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200130};
131
Harald Welte7acb30c2011-08-17 17:13:48 +0200132/*! \brief Return value of the commands. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200133#define CMD_SUCCESS 0
134#define CMD_WARNING 1
135#define CMD_ERR_NO_MATCH 2
136#define CMD_ERR_AMBIGUOUS 3
137#define CMD_ERR_INCOMPLETE 4
138#define CMD_ERR_EXEED_ARGC_MAX 5
139#define CMD_ERR_NOTHING_TODO 6
140#define CMD_COMPLETE_FULL_MATCH 7
141#define CMD_COMPLETE_MATCH 8
142#define CMD_COMPLETE_LIST_MATCH 9
143#define CMD_SUCCESS_DAEMON 10
144
145/* Argc max counts. */
Holger Hans Peter Freytherc0dbe0b2011-07-24 19:58:06 +0200146#define CMD_ARGC_MAX 256
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200147
148/* Turn off these macros when uisng cpp with extract.pl */
149#ifndef VTYSH_EXTRACT_PL
150
151/* helper defines for end-user DEFUN* macros */
152#define DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
153 static struct cmd_element cmdname = \
154 { \
155 .string = cmdstr, \
156 .func = funcname, \
157 .doc = helpstr, \
158 .attr = attrs, \
159 .daemon = dnum, \
160 };
161
162/* global (non static) cmd_element */
163#define gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
164 struct cmd_element cmdname = \
165 { \
166 .string = cmdstr, \
167 .func = funcname, \
168 .doc = helpstr, \
169 .attr = attrs, \
170 .daemon = dnum, \
171 };
172
173#define DEFUN_CMD_FUNC_DECL(funcname) \
174 static int funcname (struct cmd_element *, struct vty *, int, const char *[]); \
175
176#define DEFUN_CMD_FUNC_TEXT(funcname) \
177 static int funcname \
178 (struct cmd_element *self, struct vty *vty, int argc, const char *argv[])
179
Harald Welte7acb30c2011-08-17 17:13:48 +0200180/*! \brief Macro for defining a VTY node and function
181 * \param[in] funcname Name of the function implementing the node
182 * \param[in] cmdname Name of the command node
183 * \param[in] cmdstr String with syntax of node
184 * \param[in] helpstr String with help message of node
185 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200186#define DEFUN(funcname, cmdname, cmdstr, helpstr) \
187 DEFUN_CMD_FUNC_DECL(funcname) \
188 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
189 DEFUN_CMD_FUNC_TEXT(funcname)
190
Harald Welte7acb30c2011-08-17 17:13:48 +0200191/*! \brief Macro for defining a non-static (global) VTY node and function
192 * \param[in] funcname Name of the function implementing the node
193 * \param[in] cmdname Name of the command node
194 * \param[in] cmdstr String with syntax of node
195 * \param[in] helpstr String with help message of node
196 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200197#define gDEFUN(funcname, cmdname, cmdstr, helpstr) \
198 DEFUN_CMD_FUNC_DECL(funcname) \
199 gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
200 DEFUN_CMD_FUNC_TEXT(funcname)
201
202#define DEFUN_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
203 DEFUN_CMD_FUNC_DECL(funcname) \
204 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0) \
205 DEFUN_CMD_FUNC_TEXT(funcname)
206
207#define DEFUN_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
208 DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
209
210#define DEFUN_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
211 DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED) \
212
213/* DEFUN_NOSH for commands that vtysh should ignore */
214#define DEFUN_NOSH(funcname, cmdname, cmdstr, helpstr) \
215 DEFUN(funcname, cmdname, cmdstr, helpstr)
216
217/* DEFSH for vtysh. */
218#define DEFSH(daemon, cmdname, cmdstr, helpstr) \
219 DEFUN_CMD_ELEMENT(NULL, cmdname, cmdstr, helpstr, 0, daemon) \
220
221/* DEFUN + DEFSH */
222#define DEFUNSH(daemon, funcname, cmdname, cmdstr, helpstr) \
223 DEFUN_CMD_FUNC_DECL(funcname) \
224 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon) \
225 DEFUN_CMD_FUNC_TEXT(funcname)
226
227/* DEFUN + DEFSH with attributes */
228#define DEFUNSH_ATTR(daemon, funcname, cmdname, cmdstr, helpstr, attr) \
229 DEFUN_CMD_FUNC_DECL(funcname) \
230 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, daemon) \
231 DEFUN_CMD_FUNC_TEXT(funcname)
232
233#define DEFUNSH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
234 DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
235
236#define DEFUNSH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
237 DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED)
238
239/* ALIAS macro which define existing command's alias. */
240#define ALIAS(funcname, cmdname, cmdstr, helpstr) \
241 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
242
243/* global (non static) cmd_element */
244#define gALIAS(funcname, cmdname, cmdstr, helpstr) \
245 gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
246
247#define ALIAS_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
248 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0)
249
250#define ALIAS_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
251 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, 0)
252
253#define ALIAS_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
254 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, 0)
255
256#define ALIAS_SH(daemon, funcname, cmdname, cmdstr, helpstr) \
257 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon)
258
259#define ALIAS_SH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
260 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, daemon)
261
262#define ALIAS_SH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
263 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, daemon)
264
265#endif /* VTYSH_EXTRACT_PL */
266
267/* Some macroes */
268#define CMD_OPTION(S) ((S[0]) == '[')
269#define CMD_VARIABLE(S) (((S[0]) >= 'A' && (S[0]) <= 'Z') || ((S[0]) == '<'))
270#define CMD_VARARG(S) ((S[0]) == '.')
271#define CMD_RANGE(S) ((S[0] == '<'))
272
273#define CMD_IPV4(S) ((strcmp ((S), "A.B.C.D") == 0))
274#define CMD_IPV4_PREFIX(S) ((strcmp ((S), "A.B.C.D/M") == 0))
275#define CMD_IPV6(S) ((strcmp ((S), "X:X::X:X") == 0))
276#define CMD_IPV6_PREFIX(S) ((strcmp ((S), "X:X::X:X/M") == 0))
277
278/* Common descriptions. */
279#define SHOW_STR "Show running system information\n"
280#define IP_STR "IP information\n"
281#define IPV6_STR "IPv6 information\n"
282#define NO_STR "Negate a command or set its defaults\n"
283#define CLEAR_STR "Reset functions\n"
284#define RIP_STR "RIP information\n"
285#define BGP_STR "BGP information\n"
286#define OSPF_STR "OSPF information\n"
287#define NEIGHBOR_STR "Specify neighbor router\n"
288#define DEBUG_STR "Debugging functions (see also 'undebug')\n"
289#define UNDEBUG_STR "Disable debugging functions (see also 'debug')\n"
290#define ROUTER_STR "Enable a routing process\n"
291#define AS_STR "AS number\n"
292#define MBGP_STR "MBGP information\n"
293#define MATCH_STR "Match values from routing table\n"
294#define SET_STR "Set values in destination routing protocol\n"
295#define OUT_STR "Filter outgoing routing updates\n"
296#define IN_STR "Filter incoming routing updates\n"
297#define V4NOTATION_STR "specify by IPv4 address notation(e.g. 0.0.0.0)\n"
298#define OSPF6_NUMBER_STR "Specify by number\n"
299#define INTERFACE_STR "Interface infomation\n"
300#define IFNAME_STR "Interface name(e.g. ep0)\n"
301#define IP6_STR "IPv6 Information\n"
302#define OSPF6_STR "Open Shortest Path First (OSPF) for IPv6\n"
303#define OSPF6_ROUTER_STR "Enable a routing process\n"
304#define OSPF6_INSTANCE_STR "<1-65535> Instance ID\n"
305#define SECONDS_STR "<1-65535> Seconds\n"
306#define ROUTE_STR "Routing Table\n"
307#define PREFIX_LIST_STR "Build a prefix list\n"
308#define OSPF6_DUMP_TYPE_LIST \
309"(neighbor|interface|area|lsa|zebra|config|dbex|spf|route|lsdb|redistribute|hook|asbr|prefix|abr)"
310#define ISIS_STR "IS-IS information\n"
311#define AREA_TAG_STR "[area tag]\n"
312
313#define CONF_BACKUP_EXT ".sav"
314
315/* IPv4 only machine should not accept IPv6 address for peer's IP
316 address. So we replace VTY command string like below. */
317#ifdef HAVE_IPV6
318#define NEIGHBOR_CMD "neighbor (A.B.C.D|X:X::X:X) "
319#define NO_NEIGHBOR_CMD "no neighbor (A.B.C.D|X:X::X:X) "
320#define NEIGHBOR_ADDR_STR "Neighbor address\nIPv6 address\n"
321#define NEIGHBOR_CMD2 "neighbor (A.B.C.D|X:X::X:X|WORD) "
322#define NO_NEIGHBOR_CMD2 "no neighbor (A.B.C.D|X:X::X:X|WORD) "
323#define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor IPv6 address\nNeighbor tag\n"
324#else
325#define NEIGHBOR_CMD "neighbor A.B.C.D "
326#define NO_NEIGHBOR_CMD "no neighbor A.B.C.D "
327#define NEIGHBOR_ADDR_STR "Neighbor address\n"
328#define NEIGHBOR_CMD2 "neighbor (A.B.C.D|WORD) "
329#define NO_NEIGHBOR_CMD2 "no neighbor (A.B.C.D|WORD) "
330#define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor tag\n"
331#endif /* HAVE_IPV6 */
332
333/* Prototypes. */
334void install_node(struct cmd_node *, int (*)(struct vty *));
335void install_default(enum node_type);
336void install_element(enum node_type, struct cmd_element *);
337void install_element_ve(struct cmd_element *cmd);
Harald Welte95b2b472011-07-16 11:58:09 +0200338void sort_node(void);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200339
340/* Concatenates argv[shift] through argv[argc-1] into a single NUL-terminated
341 string with a space between each element (allocated using
342 XMALLOC(MTYPE_TMP)). Returns NULL if shift >= argc. */
343char *argv_concat(const char **argv, int argc, int shift);
344
345vector cmd_make_strvec(const char *);
346void cmd_free_strvec(vector);
347vector cmd_describe_command();
348char **cmd_complete_command();
349const char *cmd_prompt(enum node_type);
350int config_from_file(struct vty *, FILE *);
351enum node_type node_parent(enum node_type);
352int cmd_execute_command(vector, struct vty *, struct cmd_element **, int);
353int cmd_execute_command_strict(vector, struct vty *, struct cmd_element **);
354void config_replace_string(struct cmd_element *, char *, ...);
355void cmd_init(int);
356
357/* Export typical functions. */
358extern struct cmd_element config_exit_cmd;
359extern struct cmd_element config_help_cmd;
360extern struct cmd_element config_list_cmd;
361char *host_config_file();
362void host_config_set(const char *);
363
364/* This is called from main when a daemon is invoked with -v or --version. */
365void print_version(int print_copyright);
366
367extern void *tall_vty_cmd_ctx;
368
Harald Welte7acb30c2011-08-17 17:13:48 +0200369/*! }@ */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200370#endif /* _ZEBRA_COMMAND_H */