command.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  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. #ifndef _ZEBRA_COMMAND_H
  23. #define _ZEBRA_COMMAND_H
  24. #include "vector.h"
  25. #include "vty.h"
  26. #include "lib/route_types.h"
  27. /* Host configuration variable */
  28. struct host
  29. {
  30. /* Host name of this router. */
  31. char *name;
  32. /* Password for vty interface. */
  33. char *password;
  34. char *password_encrypt;
  35. /* Enable password */
  36. char *enable;
  37. char *enable_encrypt;
  38. /* System wide terminal lines. */
  39. int lines;
  40. /* Log filename. */
  41. char *logfile;
  42. /* config file name of this host */
  43. char *config;
  44. /* Flags for services */
  45. int advanced;
  46. int encrypt;
  47. /* Banner configuration. */
  48. const char *motd;
  49. char *motdfile;
  50. };
  51. /* There are some command levels which called from command node. */
  52. enum node_type
  53. {
  54. AUTH_NODE, /* Authentication mode of vty interface. */
  55. RESTRICTED_NODE, /* Restricted view mode */
  56. VIEW_NODE, /* View node. Default mode of vty interface. */
  57. AUTH_ENABLE_NODE, /* Authentication mode for change enable. */
  58. ENABLE_NODE, /* Enable node. */
  59. CONFIG_NODE, /* Config node. Default mode of config file. */
  60. SERVICE_NODE, /* Service node. */
  61. DEBUG_NODE, /* Debug node. */
  62. AAA_NODE, /* AAA node. */
  63. KEYCHAIN_NODE, /* Key-chain node. */
  64. KEYCHAIN_KEY_NODE, /* Key-chain key node. */
  65. INTERFACE_NODE, /* Interface mode node. */
  66. ZEBRA_NODE, /* zebra connection node. */
  67. TABLE_NODE, /* rtm_table selection node. */
  68. RIP_NODE, /* RIP protocol mode node. */
  69. RIPNG_NODE, /* RIPng protocol mode node. */
  70. BGP_NODE, /* BGP protocol mode which includes BGP4+ */
  71. BGP_VPNV4_NODE, /* BGP MPLS-VPN PE exchange. */
  72. BGP_IPV4_NODE, /* BGP IPv4 unicast address family. */
  73. BGP_IPV4M_NODE, /* BGP IPv4 multicast address family. */
  74. BGP_IPV6_NODE, /* BGP IPv6 address family */
  75. BGP_IPV6M_NODE, /* BGP IPv6 multicast address family. */
  76. OSPF_NODE, /* OSPF protocol mode */
  77. OSPF6_NODE, /* OSPF protocol for IPv6 mode */
  78. ISIS_NODE, /* ISIS protocol mode */
  79. MASC_NODE, /* MASC for multicast. */
  80. IRDP_NODE, /* ICMP Router Discovery Protocol mode. */
  81. IP_NODE, /* Static ip route node. */
  82. ACCESS_NODE, /* Access list node. */
  83. PREFIX_NODE, /* Prefix list node. */
  84. ACCESS_IPV6_NODE, /* Access list node. */
  85. PREFIX_IPV6_NODE, /* Prefix list node. */
  86. AS_LIST_NODE, /* AS list node. */
  87. COMMUNITY_LIST_NODE, /* Community list node. */
  88. RMAP_NODE, /* Route map node. */
  89. SMUX_NODE, /* SNMP configuration node. */
  90. DUMP_NODE, /* Packet dump node. */
  91. FORWARDING_NODE, /* IP forwarding node. */
  92. PROTOCOL_NODE, /* protocol filtering node */
  93. VTY_NODE, /* Vty node. */
  94. };
  95. /* Node which has some commands and prompt string and configuration
  96. function pointer . */
  97. struct cmd_node
  98. {
  99. /* Node index. */
  100. enum node_type node;
  101. /* Prompt character at vty interface. */
  102. const char *prompt;
  103. /* Is this node's configuration goes to vtysh ? */
  104. int vtysh;
  105. /* Node's configuration write function */
  106. int (*func) (struct vty *);
  107. /* Vector of this node's command list. */
  108. vector cmd_vector;
  109. };
  110. enum
  111. {
  112. CMD_ATTR_DEPRECATED = 1,
  113. CMD_ATTR_HIDDEN,
  114. };
  115. /* Structure of command element. */
  116. struct cmd_element
  117. {
  118. const char *string; /* Command specification by string. */
  119. int (*func) (struct cmd_element *, struct vty *, int, const char *[]);
  120. const char *doc; /* Documentation of this command. */
  121. int daemon; /* Daemon to which this command belong. */
  122. vector strvec; /* Pointing out each description vector. */
  123. unsigned int cmdsize; /* Command index count. */
  124. char *config; /* Configuration string */
  125. vector subconfig; /* Sub configuration string */
  126. u_char attr; /* Command attributes */
  127. };
  128. /* Command description structure. */
  129. struct desc
  130. {
  131. char *cmd; /* Command string. */
  132. char *str; /* Command's description. */
  133. };
  134. /* Return value of the commands. */
  135. #define CMD_SUCCESS 0
  136. #define CMD_WARNING 1
  137. #define CMD_ERR_NO_MATCH 2
  138. #define CMD_ERR_AMBIGUOUS 3
  139. #define CMD_ERR_INCOMPLETE 4
  140. #define CMD_ERR_EXEED_ARGC_MAX 5
  141. #define CMD_ERR_NOTHING_TODO 6
  142. #define CMD_COMPLETE_FULL_MATCH 7
  143. #define CMD_COMPLETE_MATCH 8
  144. #define CMD_COMPLETE_LIST_MATCH 9
  145. #define CMD_SUCCESS_DAEMON 10
  146. /* Argc max counts. */
  147. #define CMD_ARGC_MAX 25
  148. /* Turn off these macros when uisng cpp with extract.pl */
  149. #ifndef VTYSH_EXTRACT_PL
  150. /* helper defines for end-user DEFUN* macros */
  151. #define DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
  152. struct cmd_element cmdname = \
  153. { \
  154. .string = cmdstr, \
  155. .func = funcname, \
  156. .doc = helpstr, \
  157. .attr = attrs, \
  158. .daemon = dnum, \
  159. };
  160. #define DEFUN_CMD_FUNC_DECL(funcname) \
  161. static int funcname (struct cmd_element *, struct vty *, int, const char *[]);
  162. #define DEFUN_CMD_FUNC_TEXT(funcname) \
  163. static int funcname \
  164. (struct cmd_element *self __attribute__ ((unused)), \
  165. struct vty *vty __attribute__ ((unused)), \
  166. int argc __attribute__ ((unused)), \
  167. const char *argv[] __attribute__ ((unused)) )
  168. /* DEFUN for vty command interafce. Little bit hacky ;-). */
  169. #define DEFUN(funcname, cmdname, cmdstr, helpstr) \
  170. DEFUN_CMD_FUNC_DECL(funcname) \
  171. DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
  172. DEFUN_CMD_FUNC_TEXT(funcname)
  173. #define DEFUN_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
  174. DEFUN_CMD_FUNC_DECL(funcname) \
  175. DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0) \
  176. DEFUN_CMD_FUNC_TEXT(funcname)
  177. #define DEFUN_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
  178. DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
  179. #define DEFUN_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
  180. DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED) \
  181. /* DEFUN_NOSH for commands that vtysh should ignore */
  182. #define DEFUN_NOSH(funcname, cmdname, cmdstr, helpstr) \
  183. DEFUN(funcname, cmdname, cmdstr, helpstr)
  184. /* DEFSH for vtysh. */
  185. #define DEFSH(daemon, cmdname, cmdstr, helpstr) \
  186. DEFUN_CMD_ELEMENT(NULL, cmdname, cmdstr, helpstr, 0, daemon) \
  187. /* DEFUN + DEFSH */
  188. #define DEFUNSH(daemon, funcname, cmdname, cmdstr, helpstr) \
  189. DEFUN_CMD_FUNC_DECL(funcname) \
  190. DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon) \
  191. DEFUN_CMD_FUNC_TEXT(funcname)
  192. /* DEFUN + DEFSH with attributes */
  193. #define DEFUNSH_ATTR(daemon, funcname, cmdname, cmdstr, helpstr, attr) \
  194. DEFUN_CMD_FUNC_DECL(funcname) \
  195. DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, daemon) \
  196. DEFUN_CMD_FUNC_TEXT(funcname)
  197. #define DEFUNSH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
  198. DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)
  199. #define DEFUNSH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
  200. DEFUNSH_ATTR (daemon, funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED)
  201. /* ALIAS macro which define existing command's alias. */
  202. #define ALIAS(funcname, cmdname, cmdstr, helpstr) \
  203. DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)
  204. #define ALIAS_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
  205. DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0)
  206. #define ALIAS_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
  207. DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, 0)
  208. #define ALIAS_DEPRECATED(funcname, cmdname, cmdstr, helpstr) \
  209. DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, 0)
  210. #define ALIAS_SH(daemon, funcname, cmdname, cmdstr, helpstr) \
  211. DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, daemon)
  212. #define ALIAS_SH_HIDDEN(daemon, funcname, cmdname, cmdstr, helpstr) \
  213. DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN, daemon)
  214. #define ALIAS_SH_DEPRECATED(daemon, funcname, cmdname, cmdstr, helpstr) \
  215. DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, CMD_ATTR_DEPRECATED, daemon)
  216. #endif /* VTYSH_EXTRACT_PL */
  217. /* Some macroes */
  218. #define CMD_OPTION(S) ((S[0]) == '[')
  219. #define CMD_VARIABLE(S) (((S[0]) >= 'A' && (S[0]) <= 'Z') || ((S[0]) == '<'))
  220. #define CMD_VARARG(S) ((S[0]) == '.')
  221. #define CMD_RANGE(S) ((S[0] == '<'))
  222. #define CMD_IPV4(S) ((strcmp ((S), "A.B.C.D") == 0))
  223. #define CMD_IPV4_PREFIX(S) ((strcmp ((S), "A.B.C.D/M") == 0))
  224. #define CMD_IPV6(S) ((strcmp ((S), "X:X::X:X") == 0))
  225. #define CMD_IPV6_PREFIX(S) ((strcmp ((S), "X:X::X:X/M") == 0))
  226. /* Common descriptions. */
  227. #define SHOW_STR "Show running system information\n"
  228. #define IP_STR "IP information\n"
  229. #define IPV6_STR "IPv6 information\n"
  230. #define NO_STR "Negate a command or set its defaults\n"
  231. #define REDIST_STR "Redistribute information from another routing protocol\n"
  232. #define CLEAR_STR "Reset functions\n"
  233. #define RIP_STR "RIP information\n"
  234. #define BGP_STR "BGP information\n"
  235. #define OSPF_STR "OSPF information\n"
  236. #define NEIGHBOR_STR "Specify neighbor router\n"
  237. #define DEBUG_STR "Debugging functions (see also 'undebug')\n"
  238. #define UNDEBUG_STR "Disable debugging functions (see also 'debug')\n"
  239. #define ROUTER_STR "Enable a routing process\n"
  240. #define AS_STR "AS number\n"
  241. #define MBGP_STR "MBGP information\n"
  242. #define MATCH_STR "Match values from routing table\n"
  243. #define SET_STR "Set values in destination routing protocol\n"
  244. #define OUT_STR "Filter outgoing routing updates\n"
  245. #define IN_STR "Filter incoming routing updates\n"
  246. #define V4NOTATION_STR "specify by IPv4 address notation(e.g. 0.0.0.0)\n"
  247. #define OSPF6_NUMBER_STR "Specify by number\n"
  248. #define INTERFACE_STR "Interface infomation\n"
  249. #define IFNAME_STR "Interface name(e.g. ep0)\n"
  250. #define IP6_STR "IPv6 Information\n"
  251. #define OSPF6_STR "Open Shortest Path First (OSPF) for IPv6\n"
  252. #define OSPF6_ROUTER_STR "Enable a routing process\n"
  253. #define OSPF6_INSTANCE_STR "<1-65535> Instance ID\n"
  254. #define SECONDS_STR "<1-65535> Seconds\n"
  255. #define ROUTE_STR "Routing Table\n"
  256. #define PREFIX_LIST_STR "Build a prefix list\n"
  257. #define OSPF6_DUMP_TYPE_LIST \
  258. "(neighbor|interface|area|lsa|zebra|config|dbex|spf|route|lsdb|redistribute|hook|asbr|prefix|abr)"
  259. #define ISIS_STR "IS-IS information\n"
  260. #define AREA_TAG_STR "[area tag]\n"
  261. #define CONF_BACKUP_EXT ".sav"
  262. /* IPv4 only machine should not accept IPv6 address for peer's IP
  263. address. So we replace VTY command string like below. */
  264. #ifdef HAVE_IPV6
  265. #define NEIGHBOR_CMD "neighbor (A.B.C.D|X:X::X:X) "
  266. #define NO_NEIGHBOR_CMD "no neighbor (A.B.C.D|X:X::X:X) "
  267. #define NEIGHBOR_ADDR_STR "Neighbor address\nIPv6 address\n"
  268. #define NEIGHBOR_CMD2 "neighbor (A.B.C.D|X:X::X:X|WORD) "
  269. #define NO_NEIGHBOR_CMD2 "no neighbor (A.B.C.D|X:X::X:X|WORD) "
  270. #define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor IPv6 address\nNeighbor tag\n"
  271. #else
  272. #define NEIGHBOR_CMD "neighbor A.B.C.D "
  273. #define NO_NEIGHBOR_CMD "no neighbor A.B.C.D "
  274. #define NEIGHBOR_ADDR_STR "Neighbor address\n"
  275. #define NEIGHBOR_CMD2 "neighbor (A.B.C.D|WORD) "
  276. #define NO_NEIGHBOR_CMD2 "no neighbor (A.B.C.D|WORD) "
  277. #define NEIGHBOR_ADDR_STR2 "Neighbor address\nNeighbor tag\n"
  278. #endif /* HAVE_IPV6 */
  279. /* Prototypes. */
  280. extern void install_node (struct cmd_node *, int (*) (struct vty *));
  281. extern void install_default (enum node_type);
  282. extern void install_element (enum node_type, struct cmd_element *);
  283. extern void sort_node (void);
  284. /* Concatenates argv[shift] through argv[argc-1] into a single NUL-terminated
  285. string with a space between each element (allocated using
  286. XMALLOC(MTYPE_TMP)). Returns NULL if shift >= argc. */
  287. extern char *argv_concat (const char **argv, int argc, int shift);
  288. extern vector cmd_make_strvec (const char *);
  289. extern void cmd_free_strvec (vector);
  290. extern vector cmd_describe_command (vector, struct vty *, int *status);
  291. extern char **cmd_complete_command (vector, struct vty *, int *status);
  292. extern const char *cmd_prompt (enum node_type);
  293. extern int config_from_file (struct vty *, FILE *);
  294. extern enum node_type node_parent (enum node_type);
  295. extern int cmd_execute_command (vector, struct vty *, struct cmd_element **, int);
  296. extern int cmd_execute_command_strict (vector, struct vty *, struct cmd_element **);
  297. extern void config_replace_string (struct cmd_element *, char *, ...);
  298. extern void cmd_init (int);
  299. extern void cmd_terminate (void);
  300. /* Export typical functions. */
  301. extern struct cmd_element config_end_cmd;
  302. extern struct cmd_element config_exit_cmd;
  303. extern struct cmd_element config_quit_cmd;
  304. extern struct cmd_element config_help_cmd;
  305. extern struct cmd_element config_list_cmd;
  306. extern char *host_config_file (void);
  307. extern void host_config_set (char *);
  308. extern void print_version (const char *);
  309. /* struct host global, ick */
  310. extern struct host host;
  311. /* "<cr>" global */
  312. extern char *command_cr;
  313. #endif /* _ZEBRA_COMMAND_H */