blob: 783a7a2d17a5c1ea3ad4fc7b98920da4b244c946 [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
31/* Host configuration variable */
32struct host {
33 /* Host name of this router. */
34 char *name;
35
36 /* Password for vty interface. */
37 char *password;
38 char *password_encrypt;
39
40 /* Enable password */
41 char *enable;
42 char *enable_encrypt;
43
44 /* System wide terminal lines. */
45 int lines;
46
47 /* Log filename. */
48 char *logfile;
49
50 /* config file name of this host */
51 char *config;
52
53 /* Flags for services */
54 int advanced;
55 int encrypt;
56
57 /* Banner configuration. */
58 const char *motd;
59 char *motdfile;
60
Harald Welte237f6242010-05-25 23:00:45 +020061 const struct vty_app_info *app_info;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020062};
63
64/* There are some command levels which called from command node. */
65enum node_type {
66 AUTH_NODE, /* Authentication mode of vty interface. */
67 VIEW_NODE, /* View node. Default mode of vty interface. */
68 AUTH_ENABLE_NODE, /* Authentication mode for change enable. */
69 ENABLE_NODE, /* Enable node. */
70 CONFIG_NODE, /* Config node. Default mode of config file. */
71 SERVICE_NODE, /* Service node. */
72 DEBUG_NODE, /* Debug node. */
Harald Welte28222962011-02-18 20:37:04 +010073 CFG_LOG_NODE, /* Configure the logging */
Harald Welte4c053012010-05-31 16:01:59 +020074
Harald Welte3fb0b6f2010-05-19 19:02:52 +020075 VTY_NODE, /* Vty node. */
76
Harald Welte892e6212011-07-19 14:31:44 +020077 L_E1INP_NODE, /* E1 line in libosmo-abis. */
78 L_IPA_NODE, /* IPA proxying commands in libosmo-abis. */
Pablo Neira Ayuso2ade3a02011-07-07 19:46:50 +020079
Harald Welte4c053012010-05-31 16:01:59 +020080 _LAST_OSMOVTY_NODE
Harald Welte3fb0b6f2010-05-19 19:02:52 +020081};
82
83/* Node which has some commands and prompt string and configuration
84 function pointer . */
85struct cmd_node {
86 /* Node index. */
87 enum node_type node;
88
89 /* Prompt character at vty interface. */
90 const char *prompt;
91
92 /* Is this node's configuration goes to vtysh ? */
93 int vtysh;
94
95 /* Node's configuration write function */
96 int (*func) (struct vty *);
97
98 /* Vector of this node's command list. */
99 vector cmd_vector;
100};
101
102enum {
103 CMD_ATTR_DEPRECATED = 1,
104 CMD_ATTR_HIDDEN,
105};
106
107/* Structure of command element. */
108struct cmd_element {
109 const char *string; /* Command specification by string. */
110 int (*func) (struct cmd_element *, struct vty *, int, const char *[]);
111 const char *doc; /* Documentation of this command. */
112 int daemon; /* Daemon to which this command belong. */
113 vector strvec; /* Pointing out each description vector. */
114 unsigned int cmdsize; /* Command index count. */
115 char *config; /* Configuration string */
116 vector subconfig; /* Sub configuration string */
117 u_char attr; /* Command attributes */
118};
119
120/* Command description structure. */
121struct desc {
122 const char *cmd; /* Command string. */
123 const char *str; /* Command's description. */
124};
125
126/* Return value of the commands. */
127#define CMD_SUCCESS 0
128#define CMD_WARNING 1
129#define CMD_ERR_NO_MATCH 2
130#define CMD_ERR_AMBIGUOUS 3
131#define CMD_ERR_INCOMPLETE 4
132#define CMD_ERR_EXEED_ARGC_MAX 5
133#define CMD_ERR_NOTHING_TODO 6
134#define CMD_COMPLETE_FULL_MATCH 7
135#define CMD_COMPLETE_MATCH 8
136#define CMD_COMPLETE_LIST_MATCH 9
137#define CMD_SUCCESS_DAEMON 10
138
139/* Argc max counts. */
Holger Hans Peter Freytherc0dbe0b2011-07-24 19:58:06 +0200140#define CMD_ARGC_MAX 256
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200141
142/* Turn off these macros when uisng cpp with extract.pl */
143#ifndef VTYSH_EXTRACT_PL
144
145/* helper defines for end-user DEFUN* macros */
146#define DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
147 static struct cmd_element cmdname = \
148 { \
149 .string = cmdstr, \
150 .func = funcname, \
151 .doc = helpstr, \
152 .attr = attrs, \
153 .daemon = dnum, \
154 };
155
156/* global (non static) cmd_element */
157#define gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
158 struct cmd_element cmdname = \
159 { \
160 .string = cmdstr, \
161 .func = funcname, \
162 .doc = helpstr, \
163 .attr = attrs, \
164 .daemon = dnum, \
165 };
166
167#define DEFUN_CMD_FUNC_DECL(funcname) \
168 static int funcname (struct cmd_element *, struct vty *, int, const char *[]); \
169
170#define DEFUN_CMD_FUNC_TEXT(funcname) \
171 static int funcname \
172 (struct cmd_element *self, struct vty *vty, int argc, const char *argv[])
173
174/* DEFUN for vty command interafce. Little bit hacky ;-). */
175#define DEFUN(funcname, cmdname, cmdstr, helpstr) \
176 DEFUN_CMD_FUNC_DECL(funcname) \
177 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
178 DEFUN_CMD_FUNC_TEXT(funcname)
179
180/* global (non static) cmd_element */
181#define gDEFUN(funcname, cmdname, cmdstr, helpstr) \
182 DEFUN_CMD_FUNC_DECL(funcname) \
183 gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
184 DEFUN_CMD_FUNC_TEXT(funcname)
185
186#define DEFUN_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
187 DEFUN_CMD_FUNC_DECL(funcname) \
188 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0) \
189 DEFUN_CMD_FUNC_TEXT(funcname)
190
191#define DEFUN_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
192 DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
193
194#define DEFUN_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
195 DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED) \
196
197/* DEFUN_NOSH for commands that vtysh should ignore */
198#define DEFUN_NOSH(funcname, cmdname, cmdstr, helpstr) \
199 DEFUN(funcname, cmdname, cmdstr, helpstr)
200
201/* DEFSH for vtysh. */
202#define DEFSH(daemon, cmdname, cmdstr, helpstr) \
203 DEFUN_CMD_ELEMENT(NULL, cmdname, cmdstr, helpstr, 0, daemon) \
204
205/* DEFUN + DEFSH */
206#define DEFUNSH(daemon, funcname, cmdname, cmdstr, helpstr) \
207 DEFUN_CMD_FUNC_DECL(funcname) \
208 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon) \
209 DEFUN_CMD_FUNC_TEXT(funcname)
210
211/* DEFUN + DEFSH with attributes */
212#define DEFUNSH_ATTR(daemon, funcname, cmdname, cmdstr, helpstr, attr) \
213 DEFUN_CMD_FUNC_DECL(funcname) \
214 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, daemon) \
215 DEFUN_CMD_FUNC_TEXT(funcname)
216
217#define DEFUNSH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
218 DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
219
220#define DEFUNSH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
221 DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED)
222
223/* ALIAS macro which define existing command's alias. */
224#define ALIAS(funcname, cmdname, cmdstr, helpstr) \
225 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
226
227/* global (non static) cmd_element */
228#define gALIAS(funcname, cmdname, cmdstr, helpstr) \
229 gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
230
231#define ALIAS_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
232 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0)
233
234#define ALIAS_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
235 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, 0)
236
237#define ALIAS_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
238 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, 0)
239
240#define ALIAS_SH(daemon, funcname, cmdname, cmdstr, helpstr) \
241 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon)
242
243#define ALIAS_SH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
244 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, daemon)
245
246#define ALIAS_SH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
247 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, daemon)
248
249#endif /* VTYSH_EXTRACT_PL */
250
251/* Some macroes */
252#define CMD_OPTION(S) ((S[0]) == '[')
253#define CMD_VARIABLE(S) (((S[0]) >= 'A' && (S[0]) <= 'Z') || ((S[0]) == '<'))
254#define CMD_VARARG(S) ((S[0]) == '.')
255#define CMD_RANGE(S) ((S[0] == '<'))
256
257#define CMD_IPV4(S) ((strcmp ((S), "A.B.C.D") == 0))
258#define CMD_IPV4_PREFIX(S) ((strcmp ((S), "A.B.C.D/M") == 0))
259#define CMD_IPV6(S) ((strcmp ((S), "X:X::X:X") == 0))
260#define CMD_IPV6_PREFIX(S) ((strcmp ((S), "X:X::X:X/M") == 0))
261
262/* Common descriptions. */
263#define SHOW_STR "Show running system information\n"
264#define IP_STR "IP information\n"
265#define IPV6_STR "IPv6 information\n"
266#define NO_STR "Negate a command or set its defaults\n"
267#define CLEAR_STR "Reset functions\n"
268#define RIP_STR "RIP information\n"
269#define BGP_STR "BGP information\n"
270#define OSPF_STR "OSPF information\n"
271#define NEIGHBOR_STR "Specify neighbor router\n"
272#define DEBUG_STR "Debugging functions (see also 'undebug')\n"
273#define UNDEBUG_STR "Disable debugging functions (see also 'debug')\n"
274#define ROUTER_STR "Enable a routing process\n"
275#define AS_STR "AS number\n"
276#define MBGP_STR "MBGP information\n"
277#define MATCH_STR "Match values from routing table\n"
278#define SET_STR "Set values in destination routing protocol\n"
279#define OUT_STR "Filter outgoing routing updates\n"
280#define IN_STR "Filter incoming routing updates\n"
281#define V4NOTATION_STR "specify by IPv4 address notation(e.g. 0.0.0.0)\n"
282#define OSPF6_NUMBER_STR "Specify by number\n"
283#define INTERFACE_STR "Interface infomation\n"
284#define IFNAME_STR "Interface name(e.g. ep0)\n"
285#define IP6_STR "IPv6 Information\n"
286#define OSPF6_STR "Open Shortest Path First (OSPF) for IPv6\n"
287#define OSPF6_ROUTER_STR "Enable a routing process\n"
288#define OSPF6_INSTANCE_STR "<1-65535> Instance ID\n"
289#define SECONDS_STR "<1-65535> Seconds\n"
290#define ROUTE_STR "Routing Table\n"
291#define PREFIX_LIST_STR "Build a prefix list\n"
292#define OSPF6_DUMP_TYPE_LIST \
293"(neighbor|interface|area|lsa|zebra|config|dbex|spf|route|lsdb|redistribute|hook|asbr|prefix|abr)"
294#define ISIS_STR "IS-IS information\n"
295#define AREA_TAG_STR "[area tag]\n"
296
297#define CONF_BACKUP_EXT ".sav"
298
299/* IPv4 only machine should not accept IPv6 address for peer's IP
300 address. So we replace VTY command string like below. */
301#ifdef HAVE_IPV6
302#define NEIGHBOR_CMD "neighbor (A.B.C.D|X:X::X:X) "
303#define NO_NEIGHBOR_CMD "no neighbor (A.B.C.D|X:X::X:X) "
304#define NEIGHBOR_ADDR_STR "Neighbor address\nIPv6 address\n"
305#define NEIGHBOR_CMD2 "neighbor (A.B.C.D|X:X::X:X|WORD) "
306#define NO_NEIGHBOR_CMD2 "no neighbor (A.B.C.D|X:X::X:X|WORD) "
307#define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor IPv6 address\nNeighbor tag\n"
308#else
309#define NEIGHBOR_CMD "neighbor A.B.C.D "
310#define NO_NEIGHBOR_CMD "no neighbor A.B.C.D "
311#define NEIGHBOR_ADDR_STR "Neighbor address\n"
312#define NEIGHBOR_CMD2 "neighbor (A.B.C.D|WORD) "
313#define NO_NEIGHBOR_CMD2 "no neighbor (A.B.C.D|WORD) "
314#define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor tag\n"
315#endif /* HAVE_IPV6 */
316
317/* Prototypes. */
318void install_node(struct cmd_node *, int (*)(struct vty *));
319void install_default(enum node_type);
320void install_element(enum node_type, struct cmd_element *);
321void install_element_ve(struct cmd_element *cmd);
Harald Welte95b2b472011-07-16 11:58:09 +0200322void sort_node(void);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200323
324/* Concatenates argv[shift] through argv[argc-1] into a single NUL-terminated
325 string with a space between each element (allocated using
326 XMALLOC(MTYPE_TMP)). Returns NULL if shift >= argc. */
327char *argv_concat(const char **argv, int argc, int shift);
328
329vector cmd_make_strvec(const char *);
330void cmd_free_strvec(vector);
331vector cmd_describe_command();
332char **cmd_complete_command();
333const char *cmd_prompt(enum node_type);
334int config_from_file(struct vty *, FILE *);
335enum node_type node_parent(enum node_type);
336int cmd_execute_command(vector, struct vty *, struct cmd_element **, int);
337int cmd_execute_command_strict(vector, struct vty *, struct cmd_element **);
338void config_replace_string(struct cmd_element *, char *, ...);
339void cmd_init(int);
340
341/* Export typical functions. */
342extern struct cmd_element config_exit_cmd;
343extern struct cmd_element config_help_cmd;
344extern struct cmd_element config_list_cmd;
345char *host_config_file();
346void host_config_set(const char *);
347
348/* This is called from main when a daemon is invoked with -v or --version. */
349void print_version(int print_copyright);
350
351extern void *tall_vty_cmd_ctx;
352
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200353#endif /* _ZEBRA_COMMAND_H */