blob: b3b3029161ef5bc5cf493bbde0dce3cfa817c47f [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"
Harald Welte3fb0b6f2010-05-19 19:02:52 +020029
Harald Weltee881b1b2011-08-17 18:52:30 +020030/*! \defgroup command VTY Command
Harald Welte7acb30c2011-08-17 17:13:48 +020031 * @{
32 */
33/*! \file command.h */
34
35/*! \brief Host configuration variable */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020036struct host {
Harald Welte7acb30c2011-08-17 17:13:48 +020037 /*! \brief Host name of this router. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020038 char *name;
39
Harald Welte7acb30c2011-08-17 17:13:48 +020040 /*! \brief Password for vty interface. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020041 char *password;
42 char *password_encrypt;
43
Harald Welte7acb30c2011-08-17 17:13:48 +020044 /*! \brief Enable password */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020045 char *enable;
46 char *enable_encrypt;
47
Harald Welte7acb30c2011-08-17 17:13:48 +020048 /*! \brief System wide terminal lines. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020049 int lines;
50
Harald Welte7acb30c2011-08-17 17:13:48 +020051 /*! \brief Log filename. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020052 char *logfile;
53
Harald Welte7acb30c2011-08-17 17:13:48 +020054 /*! \brief config file name of this host */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020055 char *config;
56
Harald Welte7acb30c2011-08-17 17:13:48 +020057 /*! \brief Flags for services */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020058 int advanced;
59 int encrypt;
60
Harald Welte7acb30c2011-08-17 17:13:48 +020061 /*! \brief Banner configuration. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020062 const char *motd;
63 char *motdfile;
64
Harald Welte7acb30c2011-08-17 17:13:48 +020065 /*! \brief VTY application information */
Harald Welte237f6242010-05-25 23:00:45 +020066 const struct vty_app_info *app_info;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020067};
68
Harald Welte7acb30c2011-08-17 17:13:48 +020069/*! \brief There are some command levels which called from command node. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020070enum node_type {
Harald Welte7acb30c2011-08-17 17:13:48 +020071 AUTH_NODE, /*!< \brief Authentication mode of vty interface. */
72 VIEW_NODE, /*!< \brief View node. Default mode of vty interface. */
73 AUTH_ENABLE_NODE, /*!< \brief Authentication mode for change enable. */
74 ENABLE_NODE, /*!< \brief Enable node. */
75 CONFIG_NODE, /*!< \brief Config node. Default mode of config file. */
76 SERVICE_NODE, /*!< \brief Service node. */
77 DEBUG_NODE, /*!< \brief Debug node. */
78 CFG_LOG_NODE, /*!< \brief Configure the logging */
Harald Welte4c053012010-05-31 16:01:59 +020079
Harald Welte7acb30c2011-08-17 17:13:48 +020080 VTY_NODE, /*!< \brief Vty node. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020081
Harald Welte7acb30c2011-08-17 17:13:48 +020082 L_E1INP_NODE, /*!< \brief E1 line in libosmo-abis. */
83 L_IPA_NODE, /*!< \brief IPA proxying commands in libosmo-abis. */
Harald Welted85d0152012-06-16 16:54:19 +080084 L_NS_NODE, /*!< \brief NS node in libosmo-gb. */
85 L_BSSGP_NODE, /*!< \brief BSSGP node in libosmo-gb. */
Pablo Neira Ayuso2ade3a02011-07-07 19:46:50 +020086
Harald Welte4c053012010-05-31 16:01:59 +020087 _LAST_OSMOVTY_NODE
Harald Welte3fb0b6f2010-05-19 19:02:52 +020088};
89
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +020090#include "vty.h"
91
Harald Welte7acb30c2011-08-17 17:13:48 +020092/*! \brief Node which has some commands and prompt string and
93 * configuration function pointer . */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020094struct cmd_node {
Harald Welte7acb30c2011-08-17 17:13:48 +020095 /*! \brief Node index */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020096 enum node_type node;
97
Harald Welte7acb30c2011-08-17 17:13:48 +020098 /*! \brief Prompt character at vty interface. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020099 const char *prompt;
100
Harald Welte7acb30c2011-08-17 17:13:48 +0200101 /*! \brief Is this node's configuration goes to vtysh ? */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200102 int vtysh;
103
Harald Welte7acb30c2011-08-17 17:13:48 +0200104 /*! \brief Node's configuration write function */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200105 int (*func) (struct vty *);
106
Harald Welte7acb30c2011-08-17 17:13:48 +0200107 /*! \brief Vector of this node's command list. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200108 vector cmd_vector;
109};
110
111enum {
112 CMD_ATTR_DEPRECATED = 1,
113 CMD_ATTR_HIDDEN,
114};
115
Harald Welte7acb30c2011-08-17 17:13:48 +0200116/*! \brief Structure of a command element */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200117struct cmd_element {
Harald Welte7acb30c2011-08-17 17:13:48 +0200118 const char *string; /*!< \brief Command specification by string. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200119 int (*func) (struct cmd_element *, struct vty *, int, const char *[]);
Harald Welte7acb30c2011-08-17 17:13:48 +0200120 const char *doc; /*!< \brief Documentation of this command. */
121 int daemon; /*!< \brief Daemon to which this command belong. */
122 vector strvec; /*!< \brief Pointing out each description vector. */
123 unsigned int cmdsize; /*!< \brief Command index count. */
124 char *config; /*!< \brief Configuration string */
125 vector subconfig; /*!< \brief Sub configuration string */
126 u_char attr; /*!< \brief Command attributes */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200127};
128
Harald Welte7acb30c2011-08-17 17:13:48 +0200129/*! \brief Command description structure. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200130struct desc {
Harald Welte7acb30c2011-08-17 17:13:48 +0200131 const char *cmd; /*!< \brief Command string. */
132 const char *str; /*!< \brief Command's description. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200133};
134
Harald Welte7acb30c2011-08-17 17:13:48 +0200135/*! \brief Return value of the commands. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200136#define CMD_SUCCESS 0
137#define CMD_WARNING 1
138#define CMD_ERR_NO_MATCH 2
139#define CMD_ERR_AMBIGUOUS 3
140#define CMD_ERR_INCOMPLETE 4
141#define CMD_ERR_EXEED_ARGC_MAX 5
142#define CMD_ERR_NOTHING_TODO 6
143#define CMD_COMPLETE_FULL_MATCH 7
144#define CMD_COMPLETE_MATCH 8
145#define CMD_COMPLETE_LIST_MATCH 9
146#define CMD_SUCCESS_DAEMON 10
147
148/* Argc max counts. */
Holger Hans Peter Freytherc0dbe0b2011-07-24 19:58:06 +0200149#define CMD_ARGC_MAX 256
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200150
151/* Turn off these macros when uisng cpp with extract.pl */
152#ifndef VTYSH_EXTRACT_PL
153
154/* helper defines for end-user DEFUN* macros */
155#define DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
156 static struct cmd_element cmdname = \
157 { \
158 .string = cmdstr, \
159 .func = funcname, \
160 .doc = helpstr, \
161 .attr = attrs, \
162 .daemon = dnum, \
163 };
164
165/* global (non static) cmd_element */
166#define gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
167 struct cmd_element cmdname = \
168 { \
169 .string = cmdstr, \
170 .func = funcname, \
171 .doc = helpstr, \
172 .attr = attrs, \
173 .daemon = dnum, \
174 };
175
176#define DEFUN_CMD_FUNC_DECL(funcname) \
177 static int funcname (struct cmd_element *, struct vty *, int, const char *[]); \
178
179#define DEFUN_CMD_FUNC_TEXT(funcname) \
180 static int funcname \
181 (struct cmd_element *self, struct vty *vty, int argc, const char *argv[])
182
Harald Welte7acb30c2011-08-17 17:13:48 +0200183/*! \brief Macro for defining a VTY node and function
184 * \param[in] funcname Name of the function implementing the node
185 * \param[in] cmdname Name of the command node
186 * \param[in] cmdstr String with syntax of node
187 * \param[in] helpstr String with help message of node
188 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200189#define DEFUN(funcname, cmdname, cmdstr, helpstr) \
190 DEFUN_CMD_FUNC_DECL(funcname) \
191 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
192 DEFUN_CMD_FUNC_TEXT(funcname)
193
Harald Welte7acb30c2011-08-17 17:13:48 +0200194/*! \brief Macro for defining a non-static (global) VTY node and function
195 * \param[in] funcname Name of the function implementing the node
196 * \param[in] cmdname Name of the command node
197 * \param[in] cmdstr String with syntax of node
198 * \param[in] helpstr String with help message of node
199 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200200#define gDEFUN(funcname, cmdname, cmdstr, helpstr) \
201 DEFUN_CMD_FUNC_DECL(funcname) \
202 gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
203 DEFUN_CMD_FUNC_TEXT(funcname)
204
205#define DEFUN_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
206 DEFUN_CMD_FUNC_DECL(funcname) \
207 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0) \
208 DEFUN_CMD_FUNC_TEXT(funcname)
209
210#define DEFUN_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
211 DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
212
213#define DEFUN_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
214 DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED) \
215
216/* DEFUN_NOSH for commands that vtysh should ignore */
217#define DEFUN_NOSH(funcname, cmdname, cmdstr, helpstr) \
218 DEFUN(funcname, cmdname, cmdstr, helpstr)
219
220/* DEFSH for vtysh. */
221#define DEFSH(daemon, cmdname, cmdstr, helpstr) \
222 DEFUN_CMD_ELEMENT(NULL, cmdname, cmdstr, helpstr, 0, daemon) \
223
224/* DEFUN + DEFSH */
225#define DEFUNSH(daemon, funcname, cmdname, cmdstr, helpstr) \
226 DEFUN_CMD_FUNC_DECL(funcname) \
227 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon) \
228 DEFUN_CMD_FUNC_TEXT(funcname)
229
230/* DEFUN + DEFSH with attributes */
231#define DEFUNSH_ATTR(daemon, funcname, cmdname, cmdstr, helpstr, attr) \
232 DEFUN_CMD_FUNC_DECL(funcname) \
233 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, daemon) \
234 DEFUN_CMD_FUNC_TEXT(funcname)
235
236#define DEFUNSH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
237 DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
238
239#define DEFUNSH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
240 DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED)
241
242/* ALIAS macro which define existing command's alias. */
243#define ALIAS(funcname, cmdname, cmdstr, helpstr) \
244 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
245
246/* global (non static) cmd_element */
247#define gALIAS(funcname, cmdname, cmdstr, helpstr) \
248 gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
249
250#define ALIAS_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
251 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0)
252
253#define ALIAS_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
254 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, 0)
255
256#define ALIAS_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
257 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, 0)
258
259#define ALIAS_SH(daemon, funcname, cmdname, cmdstr, helpstr) \
260 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon)
261
262#define ALIAS_SH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
263 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, daemon)
264
265#define ALIAS_SH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
266 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, daemon)
267
268#endif /* VTYSH_EXTRACT_PL */
269
270/* Some macroes */
271#define CMD_OPTION(S) ((S[0]) == '[')
272#define CMD_VARIABLE(S) (((S[0]) >= 'A' && (S[0]) <= 'Z') || ((S[0]) == '<'))
273#define CMD_VARARG(S) ((S[0]) == '.')
274#define CMD_RANGE(S) ((S[0] == '<'))
275
276#define CMD_IPV4(S) ((strcmp ((S), "A.B.C.D") == 0))
277#define CMD_IPV4_PREFIX(S) ((strcmp ((S), "A.B.C.D/M") == 0))
278#define CMD_IPV6(S) ((strcmp ((S), "X:X::X:X") == 0))
279#define CMD_IPV6_PREFIX(S) ((strcmp ((S), "X:X::X:X/M") == 0))
280
281/* Common descriptions. */
282#define SHOW_STR "Show running system information\n"
283#define IP_STR "IP information\n"
284#define IPV6_STR "IPv6 information\n"
285#define NO_STR "Negate a command or set its defaults\n"
286#define CLEAR_STR "Reset functions\n"
287#define RIP_STR "RIP information\n"
288#define BGP_STR "BGP information\n"
289#define OSPF_STR "OSPF information\n"
290#define NEIGHBOR_STR "Specify neighbor router\n"
291#define DEBUG_STR "Debugging functions (see also 'undebug')\n"
292#define UNDEBUG_STR "Disable debugging functions (see also 'debug')\n"
293#define ROUTER_STR "Enable a routing process\n"
294#define AS_STR "AS number\n"
295#define MBGP_STR "MBGP information\n"
296#define MATCH_STR "Match values from routing table\n"
297#define SET_STR "Set values in destination routing protocol\n"
298#define OUT_STR "Filter outgoing routing updates\n"
299#define IN_STR "Filter incoming routing updates\n"
300#define V4NOTATION_STR "specify by IPv4 address notation(e.g. 0.0.0.0)\n"
301#define OSPF6_NUMBER_STR "Specify by number\n"
302#define INTERFACE_STR "Interface infomation\n"
303#define IFNAME_STR "Interface name(e.g. ep0)\n"
304#define IP6_STR "IPv6 Information\n"
305#define OSPF6_STR "Open Shortest Path First (OSPF) for IPv6\n"
306#define OSPF6_ROUTER_STR "Enable a routing process\n"
307#define OSPF6_INSTANCE_STR "<1-65535> Instance ID\n"
308#define SECONDS_STR "<1-65535> Seconds\n"
309#define ROUTE_STR "Routing Table\n"
310#define PREFIX_LIST_STR "Build a prefix list\n"
311#define OSPF6_DUMP_TYPE_LIST \
312"(neighbor|interface|area|lsa|zebra|config|dbex|spf|route|lsdb|redistribute|hook|asbr|prefix|abr)"
313#define ISIS_STR "IS-IS information\n"
314#define AREA_TAG_STR "[area tag]\n"
315
316#define CONF_BACKUP_EXT ".sav"
317
318/* IPv4 only machine should not accept IPv6 address for peer's IP
319 address. So we replace VTY command string like below. */
320#ifdef HAVE_IPV6
321#define NEIGHBOR_CMD "neighbor (A.B.C.D|X:X::X:X) "
322#define NO_NEIGHBOR_CMD "no neighbor (A.B.C.D|X:X::X:X) "
323#define NEIGHBOR_ADDR_STR "Neighbor address\nIPv6 address\n"
324#define NEIGHBOR_CMD2 "neighbor (A.B.C.D|X:X::X:X|WORD) "
325#define NO_NEIGHBOR_CMD2 "no neighbor (A.B.C.D|X:X::X:X|WORD) "
326#define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor IPv6 address\nNeighbor tag\n"
327#else
328#define NEIGHBOR_CMD "neighbor A.B.C.D "
329#define NO_NEIGHBOR_CMD "no neighbor A.B.C.D "
330#define NEIGHBOR_ADDR_STR "Neighbor address\n"
331#define NEIGHBOR_CMD2 "neighbor (A.B.C.D|WORD) "
332#define NO_NEIGHBOR_CMD2 "no neighbor (A.B.C.D|WORD) "
333#define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor tag\n"
334#endif /* HAVE_IPV6 */
335
336/* Prototypes. */
337void install_node(struct cmd_node *, int (*)(struct vty *));
338void install_default(enum node_type);
339void install_element(enum node_type, struct cmd_element *);
340void install_element_ve(struct cmd_element *cmd);
Harald Welte95b2b472011-07-16 11:58:09 +0200341void sort_node(void);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200342
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200343/* This is similar to install_default() but it also creates
344 * 'exit' and 'end' commands.
345 */
346void vty_install_default(enum node_type);
347
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200348/* Concatenates argv[shift] through argv[argc-1] into a single NUL-terminated
349 string with a space between each element (allocated using
350 XMALLOC(MTYPE_TMP)). Returns NULL if shift >= argc. */
351char *argv_concat(const char **argv, int argc, int shift);
352
353vector cmd_make_strvec(const char *);
354void cmd_free_strvec(vector);
355vector cmd_describe_command();
356char **cmd_complete_command();
357const char *cmd_prompt(enum node_type);
358int config_from_file(struct vty *, FILE *);
359enum node_type node_parent(enum node_type);
360int cmd_execute_command(vector, struct vty *, struct cmd_element **, int);
361int cmd_execute_command_strict(vector, struct vty *, struct cmd_element **);
362void config_replace_string(struct cmd_element *, char *, ...);
363void cmd_init(int);
364
365/* Export typical functions. */
366extern struct cmd_element config_exit_cmd;
367extern struct cmd_element config_help_cmd;
368extern struct cmd_element config_list_cmd;
Harald Welte8ec7f902012-10-22 19:43:26 +0200369extern struct cmd_element config_end_cmd;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200370char *host_config_file();
371void host_config_set(const char *);
372
373/* This is called from main when a daemon is invoked with -v or --version. */
374void print_version(int print_copyright);
375
376extern void *tall_vty_cmd_ctx;
377
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200378/*! @} */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200379#endif /* _ZEBRA_COMMAND_H */