blob: 135ef2fed6ffc6315832c06a69c4cc41ed48cc35 [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
Jaroslav Škarvada2b82c1c2015-11-11 16:02:54 +010019 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
Harald Welte3fb0b6f2010-05-19 19:02:52 +020021 */
22
Sylvain Munaut12ba7782014-06-16 10:13:40 +020023#pragma once
Harald Welte3fb0b6f2010-05-19 19:02:52 +020024
25#include <stdio.h>
26#include <sys/types.h>
27#include "vector.h"
Harald Welte3fb0b6f2010-05-19 19:02:52 +020028
Harald Weltee881b1b2011-08-17 18:52:30 +020029/*! \defgroup command VTY Command
Harald Welte7acb30c2011-08-17 17:13:48 +020030 * @{
31 */
32/*! \file command.h */
33
34/*! \brief Host configuration variable */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020035struct host {
Harald Welte7acb30c2011-08-17 17:13:48 +020036 /*! \brief Host name of this router. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020037 char *name;
38
Harald Welte7acb30c2011-08-17 17:13:48 +020039 /*! \brief Password for vty interface. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020040 char *password;
41 char *password_encrypt;
42
Harald Welte7acb30c2011-08-17 17:13:48 +020043 /*! \brief Enable password */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020044 char *enable;
45 char *enable_encrypt;
46
Harald Welte7acb30c2011-08-17 17:13:48 +020047 /*! \brief System wide terminal lines. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020048 int lines;
49
Harald Welte7acb30c2011-08-17 17:13:48 +020050 /*! \brief Log filename. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020051 char *logfile;
52
Harald Welte7acb30c2011-08-17 17:13:48 +020053 /*! \brief config file name of this host */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020054 char *config;
55
Harald Welte7acb30c2011-08-17 17:13:48 +020056 /*! \brief Flags for services */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020057 int advanced;
58 int encrypt;
59
Harald Welte7acb30c2011-08-17 17:13:48 +020060 /*! \brief Banner configuration. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020061 const char *motd;
62 char *motdfile;
63
Harald Welte7acb30c2011-08-17 17:13:48 +020064 /*! \brief VTY application information */
Harald Welte237f6242010-05-25 23:00:45 +020065 const struct vty_app_info *app_info;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020066};
67
Harald Welte7acb30c2011-08-17 17:13:48 +020068/*! \brief There are some command levels which called from command node. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020069enum node_type {
Harald Welte7acb30c2011-08-17 17:13:48 +020070 AUTH_NODE, /*!< \brief Authentication mode of vty interface. */
71 VIEW_NODE, /*!< \brief View node. Default mode of vty interface. */
72 AUTH_ENABLE_NODE, /*!< \brief Authentication mode for change enable. */
73 ENABLE_NODE, /*!< \brief Enable node. */
74 CONFIG_NODE, /*!< \brief Config node. Default mode of config file. */
75 SERVICE_NODE, /*!< \brief Service node. */
76 DEBUG_NODE, /*!< \brief Debug node. */
77 CFG_LOG_NODE, /*!< \brief Configure the logging */
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020078 CFG_STATS_NODE, /*!< \brief Configure the statistics */
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. */
Neels Hofmeyrf81eb322016-02-24 00:10:41 +010086 L_CTRL_NODE, /*!< \brief Control interface node. */
Pablo Neira Ayuso2ade3a02011-07-07 19:46:50 +020087
Jacob Erlbeck34eec7d2015-11-02 10:50:50 +010088 /*
89 * When adding new nodes to the libosmocore project, these nodes can be
90 * used to avoid ABI changes for unrelated projects.
91 */
92 RESERVED1_NODE, /*!< \brief Reserved for later extensions */
93 RESERVED2_NODE, /*!< \brief Reserved for later extensions */
94 RESERVED3_NODE, /*!< \brief Reserved for later extensions */
Jacob Erlbeck34eec7d2015-11-02 10:50:50 +010095
Harald Welte4c053012010-05-31 16:01:59 +020096 _LAST_OSMOVTY_NODE
Harald Welte3fb0b6f2010-05-19 19:02:52 +020097};
98
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +020099#include "vty.h"
100
Harald Welte7acb30c2011-08-17 17:13:48 +0200101/*! \brief Node which has some commands and prompt string and
102 * configuration function pointer . */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200103struct cmd_node {
Harald Welte7acb30c2011-08-17 17:13:48 +0200104 /*! \brief Node index */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000105 int node;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200106
Harald Welte7acb30c2011-08-17 17:13:48 +0200107 /*! \brief Prompt character at vty interface. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200108 const char *prompt;
109
Harald Welte7acb30c2011-08-17 17:13:48 +0200110 /*! \brief Is this node's configuration goes to vtysh ? */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200111 int vtysh;
112
Harald Welte7acb30c2011-08-17 17:13:48 +0200113 /*! \brief Node's configuration write function */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200114 int (*func) (struct vty *);
115
Harald Welte7acb30c2011-08-17 17:13:48 +0200116 /*! \brief Vector of this node's command list. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200117 vector cmd_vector;
118};
119
120enum {
121 CMD_ATTR_DEPRECATED = 1,
122 CMD_ATTR_HIDDEN,
123};
124
Harald Welte7acb30c2011-08-17 17:13:48 +0200125/*! \brief Structure of a command element */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200126struct cmd_element {
Harald Welte7acb30c2011-08-17 17:13:48 +0200127 const char *string; /*!< \brief Command specification by string. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200128 int (*func) (struct cmd_element *, struct vty *, int, const char *[]);
Harald Welte7acb30c2011-08-17 17:13:48 +0200129 const char *doc; /*!< \brief Documentation of this command. */
130 int daemon; /*!< \brief Daemon to which this command belong. */
131 vector strvec; /*!< \brief Pointing out each description vector. */
132 unsigned int cmdsize; /*!< \brief Command index count. */
133 char *config; /*!< \brief Configuration string */
134 vector subconfig; /*!< \brief Sub configuration string */
Harald Welte7b74dda2014-03-11 10:31:19 +0100135 unsigned char attr; /*!< \brief Command attributes */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200136};
137
Harald Welte7acb30c2011-08-17 17:13:48 +0200138/*! \brief Command description structure. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200139struct desc {
Harald Welte7acb30c2011-08-17 17:13:48 +0200140 const char *cmd; /*!< \brief Command string. */
141 const char *str; /*!< \brief Command's description. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200142};
143
Harald Welte7acb30c2011-08-17 17:13:48 +0200144/*! \brief Return value of the commands. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200145#define CMD_SUCCESS 0
146#define CMD_WARNING 1
147#define CMD_ERR_NO_MATCH 2
148#define CMD_ERR_AMBIGUOUS 3
149#define CMD_ERR_INCOMPLETE 4
150#define CMD_ERR_EXEED_ARGC_MAX 5
151#define CMD_ERR_NOTHING_TODO 6
152#define CMD_COMPLETE_FULL_MATCH 7
153#define CMD_COMPLETE_MATCH 8
154#define CMD_COMPLETE_LIST_MATCH 9
155#define CMD_SUCCESS_DAEMON 10
156
157/* Argc max counts. */
Holger Hans Peter Freytherc0dbe0b2011-07-24 19:58:06 +0200158#define CMD_ARGC_MAX 256
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200159
160/* Turn off these macros when uisng cpp with extract.pl */
161#ifndef VTYSH_EXTRACT_PL
162
163/* helper defines for end-user DEFUN* macros */
164#define DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
165 static struct cmd_element cmdname = \
166 { \
167 .string = cmdstr, \
168 .func = funcname, \
169 .doc = helpstr, \
170 .attr = attrs, \
171 .daemon = dnum, \
172 };
173
174/* global (non static) cmd_element */
175#define gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
176 struct cmd_element cmdname = \
177 { \
178 .string = cmdstr, \
179 .func = funcname, \
180 .doc = helpstr, \
181 .attr = attrs, \
182 .daemon = dnum, \
183 };
184
185#define DEFUN_CMD_FUNC_DECL(funcname) \
186 static int funcname (struct cmd_element *, struct vty *, int, const char *[]); \
187
188#define DEFUN_CMD_FUNC_TEXT(funcname) \
189 static int funcname \
190 (struct cmd_element *self, struct vty *vty, int argc, const char *argv[])
191
Harald Welte7acb30c2011-08-17 17:13:48 +0200192/*! \brief Macro for defining a VTY node and function
193 * \param[in] funcname Name of the function implementing the node
194 * \param[in] cmdname Name of the command node
195 * \param[in] cmdstr String with syntax of node
196 * \param[in] helpstr String with help message of node
197 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200198#define DEFUN(funcname, cmdname, cmdstr, helpstr) \
199 DEFUN_CMD_FUNC_DECL(funcname) \
200 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
201 DEFUN_CMD_FUNC_TEXT(funcname)
202
Harald Welte7acb30c2011-08-17 17:13:48 +0200203/*! \brief Macro for defining a non-static (global) VTY node and function
204 * \param[in] funcname Name of the function implementing the node
205 * \param[in] cmdname Name of the command node
206 * \param[in] cmdstr String with syntax of node
207 * \param[in] helpstr String with help message of node
208 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200209#define gDEFUN(funcname, cmdname, cmdstr, helpstr) \
210 DEFUN_CMD_FUNC_DECL(funcname) \
211 gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
212 DEFUN_CMD_FUNC_TEXT(funcname)
213
214#define DEFUN_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
215 DEFUN_CMD_FUNC_DECL(funcname) \
216 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0) \
217 DEFUN_CMD_FUNC_TEXT(funcname)
218
219#define DEFUN_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
220 DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
221
222#define DEFUN_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
223 DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED) \
224
225/* DEFUN_NOSH for commands that vtysh should ignore */
226#define DEFUN_NOSH(funcname, cmdname, cmdstr, helpstr) \
227 DEFUN(funcname, cmdname, cmdstr, helpstr)
228
229/* DEFSH for vtysh. */
230#define DEFSH(daemon, cmdname, cmdstr, helpstr) \
231 DEFUN_CMD_ELEMENT(NULL, cmdname, cmdstr, helpstr, 0, daemon) \
232
233/* DEFUN + DEFSH */
234#define DEFUNSH(daemon, funcname, cmdname, cmdstr, helpstr) \
235 DEFUN_CMD_FUNC_DECL(funcname) \
236 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon) \
237 DEFUN_CMD_FUNC_TEXT(funcname)
238
239/* DEFUN + DEFSH with attributes */
240#define DEFUNSH_ATTR(daemon, funcname, cmdname, cmdstr, helpstr, attr) \
241 DEFUN_CMD_FUNC_DECL(funcname) \
242 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, daemon) \
243 DEFUN_CMD_FUNC_TEXT(funcname)
244
245#define DEFUNSH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
246 DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
247
248#define DEFUNSH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
249 DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED)
250
251/* ALIAS macro which define existing command's alias. */
252#define ALIAS(funcname, cmdname, cmdstr, helpstr) \
253 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
254
255/* global (non static) cmd_element */
256#define gALIAS(funcname, cmdname, cmdstr, helpstr) \
257 gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
258
259#define ALIAS_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
260 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0)
261
262#define ALIAS_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
263 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, 0)
264
265#define ALIAS_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
266 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, 0)
267
268#define ALIAS_SH(daemon, funcname, cmdname, cmdstr, helpstr) \
269 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon)
270
271#define ALIAS_SH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
272 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, daemon)
273
274#define ALIAS_SH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
275 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, daemon)
276
277#endif /* VTYSH_EXTRACT_PL */
278
279/* Some macroes */
280#define CMD_OPTION(S) ((S[0]) == '[')
281#define CMD_VARIABLE(S) (((S[0]) >= 'A' && (S[0]) <= 'Z') || ((S[0]) == '<'))
282#define CMD_VARARG(S) ((S[0]) == '.')
283#define CMD_RANGE(S) ((S[0] == '<'))
284
285#define CMD_IPV4(S) ((strcmp ((S), "A.B.C.D") == 0))
286#define CMD_IPV4_PREFIX(S) ((strcmp ((S), "A.B.C.D/M") == 0))
287#define CMD_IPV6(S) ((strcmp ((S), "X:X::X:X") == 0))
288#define CMD_IPV6_PREFIX(S) ((strcmp ((S), "X:X::X:X/M") == 0))
289
290/* Common descriptions. */
291#define SHOW_STR "Show running system information\n"
292#define IP_STR "IP information\n"
293#define IPV6_STR "IPv6 information\n"
294#define NO_STR "Negate a command or set its defaults\n"
295#define CLEAR_STR "Reset functions\n"
296#define RIP_STR "RIP information\n"
297#define BGP_STR "BGP information\n"
298#define OSPF_STR "OSPF information\n"
299#define NEIGHBOR_STR "Specify neighbor router\n"
300#define DEBUG_STR "Debugging functions (see also 'undebug')\n"
301#define UNDEBUG_STR "Disable debugging functions (see also 'debug')\n"
302#define ROUTER_STR "Enable a routing process\n"
303#define AS_STR "AS number\n"
304#define MBGP_STR "MBGP information\n"
305#define MATCH_STR "Match values from routing table\n"
306#define SET_STR "Set values in destination routing protocol\n"
307#define OUT_STR "Filter outgoing routing updates\n"
308#define IN_STR "Filter incoming routing updates\n"
309#define V4NOTATION_STR "specify by IPv4 address notation(e.g. 0.0.0.0)\n"
310#define OSPF6_NUMBER_STR "Specify by number\n"
311#define INTERFACE_STR "Interface infomation\n"
312#define IFNAME_STR "Interface name(e.g. ep0)\n"
313#define IP6_STR "IPv6 Information\n"
314#define OSPF6_STR "Open Shortest Path First (OSPF) for IPv6\n"
315#define OSPF6_ROUTER_STR "Enable a routing process\n"
316#define OSPF6_INSTANCE_STR "<1-65535> Instance ID\n"
317#define SECONDS_STR "<1-65535> Seconds\n"
318#define ROUTE_STR "Routing Table\n"
319#define PREFIX_LIST_STR "Build a prefix list\n"
320#define OSPF6_DUMP_TYPE_LIST \
321"(neighbor|interface|area|lsa|zebra|config|dbex|spf|route|lsdb|redistribute|hook|asbr|prefix|abr)"
322#define ISIS_STR "IS-IS information\n"
323#define AREA_TAG_STR "[area tag]\n"
324
325#define CONF_BACKUP_EXT ".sav"
326
327/* IPv4 only machine should not accept IPv6 address for peer's IP
328 address. So we replace VTY command string like below. */
329#ifdef HAVE_IPV6
330#define NEIGHBOR_CMD "neighbor (A.B.C.D|X:X::X:X) "
331#define NO_NEIGHBOR_CMD "no neighbor (A.B.C.D|X:X::X:X) "
332#define NEIGHBOR_ADDR_STR "Neighbor address\nIPv6 address\n"
333#define NEIGHBOR_CMD2 "neighbor (A.B.C.D|X:X::X:X|WORD) "
334#define NO_NEIGHBOR_CMD2 "no neighbor (A.B.C.D|X:X::X:X|WORD) "
335#define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor IPv6 address\nNeighbor tag\n"
336#else
337#define NEIGHBOR_CMD "neighbor A.B.C.D "
338#define NO_NEIGHBOR_CMD "no neighbor A.B.C.D "
339#define NEIGHBOR_ADDR_STR "Neighbor address\n"
340#define NEIGHBOR_CMD2 "neighbor (A.B.C.D|WORD) "
341#define NO_NEIGHBOR_CMD2 "no neighbor (A.B.C.D|WORD) "
342#define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor tag\n"
343#endif /* HAVE_IPV6 */
344
345/* Prototypes. */
346void install_node(struct cmd_node *, int (*)(struct vty *));
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000347void install_default(int node_type);
348void install_element(int node_type, struct cmd_element *);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200349void install_element_ve(struct cmd_element *cmd);
Harald Welte95b2b472011-07-16 11:58:09 +0200350void sort_node(void);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200351
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200352/* This is similar to install_default() but it also creates
353 * 'exit' and 'end' commands.
354 */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000355void vty_install_default(int node_type);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200356
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200357/* Concatenates argv[shift] through argv[argc-1] into a single NUL-terminated
358 string with a space between each element (allocated using
359 XMALLOC(MTYPE_TMP)). Returns NULL if shift >= argc. */
360char *argv_concat(const char **argv, int argc, int shift);
361
362vector cmd_make_strvec(const char *);
363void cmd_free_strvec(vector);
364vector cmd_describe_command();
365char **cmd_complete_command();
366const char *cmd_prompt(enum node_type);
367int config_from_file(struct vty *, FILE *);
368enum node_type node_parent(enum node_type);
369int cmd_execute_command(vector, struct vty *, struct cmd_element **, int);
370int cmd_execute_command_strict(vector, struct vty *, struct cmd_element **);
371void config_replace_string(struct cmd_element *, char *, ...);
372void cmd_init(int);
373
374/* Export typical functions. */
375extern struct cmd_element config_exit_cmd;
376extern struct cmd_element config_help_cmd;
377extern struct cmd_element config_list_cmd;
Harald Welte8ec7f902012-10-22 19:43:26 +0200378extern struct cmd_element config_end_cmd;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200379char *host_config_file();
380void host_config_set(const char *);
381
Alexander Couzensad580ba2016-05-16 16:01:45 +0200382char *osmo_asciidoc_escape(const char *inp);
383
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200384/* This is called from main when a daemon is invoked with -v or --version. */
385void print_version(int print_copyright);
386
387extern void *tall_vty_cmd_ctx;
388
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200389/*! @} */