blob: 1b6e0a7b77aa2ca2de0464535b086e251918bc23 [file] [log] [blame]
Harald Welte955049f2009-03-10 12:16:51 +00001/*
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>
Harald Welte12247c62009-05-21 07:23:02 +000027#include <sys/types.h>
Harald Welte955049f2009-03-10 12:16:51 +000028#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};
61
62/* There are some command levels which called from command node. */
63enum node_type {
Harald Welte955049f2009-03-10 12:16:51 +000064 AUTH_NODE, /* Authentication mode of vty interface. */
65 VIEW_NODE, /* View node. Default mode of vty interface. */
66 AUTH_ENABLE_NODE, /* Authentication mode for change enable. */
67 ENABLE_NODE, /* Enable node. */
68 CONFIG_NODE, /* Config node. Default mode of config file. */
69 SERVICE_NODE, /* Service node. */
70 DEBUG_NODE, /* Debug node. */
Harald Welte42581822009-08-08 16:12:58 +020071#if 0
Harald Welte955049f2009-03-10 12:16:51 +000072 AAA_NODE, /* AAA node. */
73 KEYCHAIN_NODE, /* Key-chain node. */
74 KEYCHAIN_KEY_NODE, /* Key-chain key node. */
75 INTERFACE_NODE, /* Interface mode node. */
76 ZEBRA_NODE, /* zebra connection node. */
77 TABLE_NODE, /* rtm_table selection node. */
78 RIP_NODE, /* RIP protocol mode node. */
79 RIPNG_NODE, /* RIPng protocol mode node. */
80 BGP_NODE, /* BGP protocol mode which includes BGP4+ */
81 BGP_VPNV4_NODE, /* BGP MPLS-VPN PE exchange. */
82 BGP_IPV4_NODE, /* BGP IPv4 unicast address family. */
83 BGP_IPV4M_NODE, /* BGP IPv4 multicast address family. */
84 BGP_IPV6_NODE, /* BGP IPv6 address family */
85 OSPF_NODE, /* OSPF protocol mode */
86 OSPF6_NODE, /* OSPF protocol for IPv6 mode */
87 ISIS_NODE, /* ISIS protocol mode */
88 MASC_NODE, /* MASC for multicast. */
89 IRDP_NODE, /* ICMP Router Discovery Protocol mode. */
90 IP_NODE, /* Static ip route node. */
91 ACCESS_NODE, /* Access list node. */
92 PREFIX_NODE, /* Prefix list node. */
93 ACCESS_IPV6_NODE, /* Access list node. */
94 PREFIX_IPV6_NODE, /* Prefix list node. */
95 AS_LIST_NODE, /* AS list node. */
96 COMMUNITY_LIST_NODE, /* Community list node. */
97 RMAP_NODE, /* Route map node. */
98 SMUX_NODE, /* SNMP configuration node. */
99 DUMP_NODE, /* Packet dump node. */
100 FORWARDING_NODE, /* IP forwarding node. */
Harald Welte42581822009-08-08 16:12:58 +0200101#endif
102 VTY_NODE, /* Vty node. */
103
104 GSMNET_NODE,
105 BTS_NODE,
106 TRX_NODE,
107 TS_NODE,
108 SUBSCR_NODE,
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200109 MGCP_NODE,
Harald Weltea977b3d2010-04-30 21:14:05 +0200110 GBPROXY_NODE,
Harald Weltea67cbd62010-05-01 16:46:11 +0200111 SGSN_NODE,
Harald Welte1194b582010-05-12 15:55:23 +0000112 NS_NODE,
Harald Welte955049f2009-03-10 12:16:51 +0000113};
114
115/* Node which has some commands and prompt string and configuration
116 function pointer . */
117struct cmd_node {
118 /* Node index. */
119 enum node_type node;
120
121 /* Prompt character at vty interface. */
122 const char *prompt;
123
124 /* Is this node's configuration goes to vtysh ? */
125 int vtysh;
126
127 /* Node's configuration write function */
128 int (*func) (struct vty *);
129
130 /* Vector of this node's command list. */
131 vector cmd_vector;
132};
133
134enum {
135 CMD_ATTR_DEPRECATED = 1,
136 CMD_ATTR_HIDDEN,
137};
138
139/* Structure of command element. */
140struct cmd_element {
141 const char *string; /* Command specification by string. */
142 int (*func) (struct cmd_element *, struct vty *, int, const char *[]);
143 const char *doc; /* Documentation of this command. */
144 int daemon; /* Daemon to which this command belong. */
145 vector strvec; /* Pointing out each description vector. */
146 unsigned int cmdsize; /* Command index count. */
147 char *config; /* Configuration string */
148 vector subconfig; /* Sub configuration string */
149 u_char attr; /* Command attributes */
150};
151
152/* Command description structure. */
153struct desc {
154 const char *cmd; /* Command string. */
155 const char *str; /* Command's description. */
156};
157
158/* Return value of the commands. */
159#define CMD_SUCCESS 0
160#define CMD_WARNING 1
161#define CMD_ERR_NO_MATCH 2
162#define CMD_ERR_AMBIGUOUS 3
163#define CMD_ERR_INCOMPLETE 4
164#define CMD_ERR_EXEED_ARGC_MAX 5
165#define CMD_ERR_NOTHING_TODO 6
166#define CMD_COMPLETE_FULL_MATCH 7
167#define CMD_COMPLETE_MATCH 8
168#define CMD_COMPLETE_LIST_MATCH 9
169#define CMD_SUCCESS_DAEMON 10
170
171/* Argc max counts. */
172#define CMD_ARGC_MAX 25
173
174/* Turn off these macros when uisng cpp with extract.pl */
175#ifndef VTYSH_EXTRACT_PL
176
177/* helper defines for end-user DEFUN* macros */
178#define DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
Harald Weltef32cc4b2010-05-01 11:01:46 +0200179 static struct cmd_element cmdname = \
180 { \
181 .string = cmdstr, \
182 .func = funcname, \
183 .doc = helpstr, \
184 .attr = attrs, \
185 .daemon = dnum, \
186 };
187
188/* global (non static) cmd_element */
189#define gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
Harald Welte955049f2009-03-10 12:16:51 +0000190 struct cmd_element cmdname = \
191 { \
192 .string = cmdstr, \
193 .func = funcname, \
194 .doc = helpstr, \
195 .attr = attrs, \
196 .daemon = dnum, \
197 };
198
199#define DEFUN_CMD_FUNC_DECL(funcname) \
200 static int funcname (struct cmd_element *, struct vty *, int, const char *[]); \
201
202#define DEFUN_CMD_FUNC_TEXT(funcname) \
203 static int funcname \
204 (struct cmd_element *self, struct vty *vty, int argc, const char *argv[])
205
206/* DEFUN for vty command interafce. Little bit hacky ;-). */
207#define DEFUN(funcname, cmdname, cmdstr, helpstr) \
208 DEFUN_CMD_FUNC_DECL(funcname) \
209 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
210 DEFUN_CMD_FUNC_TEXT(funcname)
211
Harald Weltef32cc4b2010-05-01 11:01:46 +0200212/* global (non static) cmd_element */
213#define gDEFUN(funcname, cmdname, cmdstr, helpstr) \
214 DEFUN_CMD_FUNC_DECL(funcname) \
215 gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
216 DEFUN_CMD_FUNC_TEXT(funcname)
217
Harald Welte955049f2009-03-10 12:16:51 +0000218#define DEFUN_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
219 DEFUN_CMD_FUNC_DECL(funcname) \
220 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0) \
221 DEFUN_CMD_FUNC_TEXT(funcname)
222
223#define DEFUN_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
224 DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
225
226#define DEFUN_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
227 DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED) \
228
229/* DEFUN_NOSH for commands that vtysh should ignore */
230#define DEFUN_NOSH(funcname, cmdname, cmdstr, helpstr) \
231 DEFUN(funcname, cmdname, cmdstr, helpstr)
232
233/* DEFSH for vtysh. */
234#define DEFSH(daemon, cmdname, cmdstr, helpstr) \
235 DEFUN_CMD_ELEMENT(NULL, cmdname, cmdstr, helpstr, 0, daemon) \
236
237/* DEFUN + DEFSH */
238#define DEFUNSH(daemon, funcname, cmdname, cmdstr, helpstr) \
239 DEFUN_CMD_FUNC_DECL(funcname) \
240 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon) \
241 DEFUN_CMD_FUNC_TEXT(funcname)
242
243/* DEFUN + DEFSH with attributes */
244#define DEFUNSH_ATTR(daemon, funcname, cmdname, cmdstr, helpstr, attr) \
245 DEFUN_CMD_FUNC_DECL(funcname) \
246 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, daemon) \
247 DEFUN_CMD_FUNC_TEXT(funcname)
248
249#define DEFUNSH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
250 DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
251
252#define DEFUNSH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
253 DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED)
254
255/* ALIAS macro which define existing command's alias. */
256#define ALIAS(funcname, cmdname, cmdstr, helpstr) \
257 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
258
Harald Weltef32cc4b2010-05-01 11:01:46 +0200259/* global (non static) cmd_element */
260#define gALIAS(funcname, cmdname, cmdstr, helpstr) \
261 gDEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
262
Harald Welte955049f2009-03-10 12:16:51 +0000263#define ALIAS_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
264 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0)
265
266#define ALIAS_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
267 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, 0)
268
269#define ALIAS_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
270 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, 0)
271
272#define ALIAS_SH(daemon, funcname, cmdname, cmdstr, helpstr) \
273 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon)
274
275#define ALIAS_SH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
276 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, daemon)
277
278#define ALIAS_SH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
279 DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, daemon)
280
281#endif /* VTYSH_EXTRACT_PL */
282
283/* Some macroes */
284#define CMD_OPTION(S) ((S[0]) == '[')
285#define CMD_VARIABLE(S) (((S[0]) >= 'A' && (S[0]) <= 'Z') || ((S[0]) == '<'))
286#define CMD_VARARG(S) ((S[0]) == '.')
287#define CMD_RANGE(S) ((S[0] == '<'))
288
289#define CMD_IPV4(S) ((strcmp ((S), "A.B.C.D") == 0))
290#define CMD_IPV4_PREFIX(S) ((strcmp ((S), "A.B.C.D/M") == 0))
291#define CMD_IPV6(S) ((strcmp ((S), "X:X::X:X") == 0))
292#define CMD_IPV6_PREFIX(S) ((strcmp ((S), "X:X::X:X/M") == 0))
293
294/* Common descriptions. */
295#define SHOW_STR "Show running system information\n"
296#define IP_STR "IP information\n"
297#define IPV6_STR "IPv6 information\n"
298#define NO_STR "Negate a command or set its defaults\n"
299#define CLEAR_STR "Reset functions\n"
300#define RIP_STR "RIP information\n"
301#define BGP_STR "BGP information\n"
302#define OSPF_STR "OSPF information\n"
303#define NEIGHBOR_STR "Specify neighbor router\n"
304#define DEBUG_STR "Debugging functions (see also 'undebug')\n"
305#define UNDEBUG_STR "Disable debugging functions (see also 'debug')\n"
306#define ROUTER_STR "Enable a routing process\n"
307#define AS_STR "AS number\n"
308#define MBGP_STR "MBGP information\n"
309#define MATCH_STR "Match values from routing table\n"
310#define SET_STR "Set values in destination routing protocol\n"
311#define OUT_STR "Filter outgoing routing updates\n"
312#define IN_STR "Filter incoming routing updates\n"
313#define V4NOTATION_STR "specify by IPv4 address notation(e.g. 0.0.0.0)\n"
314#define OSPF6_NUMBER_STR "Specify by number\n"
315#define INTERFACE_STR "Interface infomation\n"
316#define IFNAME_STR "Interface name(e.g. ep0)\n"
317#define IP6_STR "IPv6 Information\n"
318#define OSPF6_STR "Open Shortest Path First (OSPF) for IPv6\n"
319#define OSPF6_ROUTER_STR "Enable a routing process\n"
320#define OSPF6_INSTANCE_STR "<1-65535> Instance ID\n"
321#define SECONDS_STR "<1-65535> Seconds\n"
322#define ROUTE_STR "Routing Table\n"
323#define PREFIX_LIST_STR "Build a prefix list\n"
324#define OSPF6_DUMP_TYPE_LIST \
325"(neighbor|interface|area|lsa|zebra|config|dbex|spf|route|lsdb|redistribute|hook|asbr|prefix|abr)"
326#define ISIS_STR "IS-IS information\n"
327#define AREA_TAG_STR "[area tag]\n"
328
329#define CONF_BACKUP_EXT ".sav"
330
331/* IPv4 only machine should not accept IPv6 address for peer's IP
332 address. So we replace VTY command string like below. */
333#ifdef HAVE_IPV6
334#define NEIGHBOR_CMD "neighbor (A.B.C.D|X:X::X:X) "
335#define NO_NEIGHBOR_CMD "no neighbor (A.B.C.D|X:X::X:X) "
336#define NEIGHBOR_ADDR_STR "Neighbor address\nIPv6 address\n"
337#define NEIGHBOR_CMD2 "neighbor (A.B.C.D|X:X::X:X|WORD) "
338#define NO_NEIGHBOR_CMD2 "no neighbor (A.B.C.D|X:X::X:X|WORD) "
339#define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor IPv6 address\nNeighbor tag\n"
340#else
341#define NEIGHBOR_CMD "neighbor A.B.C.D "
342#define NO_NEIGHBOR_CMD "no neighbor A.B.C.D "
343#define NEIGHBOR_ADDR_STR "Neighbor address\n"
344#define NEIGHBOR_CMD2 "neighbor (A.B.C.D|WORD) "
345#define NO_NEIGHBOR_CMD2 "no neighbor (A.B.C.D|WORD) "
346#define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor tag\n"
347#endif /* HAVE_IPV6 */
348
349/* Prototypes. */
350void install_node(struct cmd_node *, int (*)(struct vty *));
351void install_default(enum node_type);
352void install_element(enum node_type, struct cmd_element *);
Harald Welteb4d5b172010-05-12 16:10:35 +0000353void install_element_ve(struct cmd_element *cmd);
Harald Welte955049f2009-03-10 12:16:51 +0000354void sort_node();
355
356/* Concatenates argv[shift] through argv[argc-1] into a single NUL-terminated
357 string with a space between each element (allocated using
358 XMALLOC(MTYPE_TMP)). Returns NULL if shift >= argc. */
359char *argv_concat(const char **argv, int argc, int shift);
360
361vector cmd_make_strvec(const char *);
362void cmd_free_strvec(vector);
363vector cmd_describe_command();
364char **cmd_complete_command();
365const char *cmd_prompt(enum node_type);
366int config_from_file(struct vty *, FILE *);
367enum node_type node_parent(enum node_type);
368int cmd_execute_command(vector, struct vty *, struct cmd_element **, int);
369int cmd_execute_command_strict(vector, struct vty *, struct cmd_element **);
370void config_replace_string(struct cmd_element *, char *, ...);
371void cmd_init(int);
372
373/* Export typical functions. */
374extern struct cmd_element config_end_cmd;
375extern struct cmd_element config_exit_cmd;
376extern struct cmd_element config_quit_cmd;
377extern struct cmd_element config_help_cmd;
378extern struct cmd_element config_list_cmd;
379char *host_config_file();
Holger Hans Peter Freyther656b1292009-08-10 08:09:54 +0200380void host_config_set(const char *);
Harald Welte955049f2009-03-10 12:16:51 +0000381
382void print_version(const char *);
383
Harald Welte3cefa9a2009-12-24 10:51:56 +0100384extern void *tall_vty_cmd_ctx;
385
Harald Welte955049f2009-03-10 12:16:51 +0000386#endif /* _ZEBRA_COMMAND_H */