]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
718e3744 | 2 | /* Virtual terminal [aka TeletYpe] interface routine |
896014f4 | 3 | * Copyright (C) 1997 Kunihiro Ishiguro |
896014f4 | 4 | */ |
718e3744 | 5 | |
6 | #ifndef _ZEBRA_VTY_H | |
7 | #define _ZEBRA_VTY_H | |
8 | ||
fe6b47b9 | 9 | #include <sys/types.h> |
061f5d1c DA |
10 | #ifdef HAVE_LIBPCRE2_POSIX |
11 | #ifndef _FRR_PCRE2_POSIX | |
12 | #define _FRR_PCRE2_POSIX | |
13 | #include <pcre2posix.h> | |
14 | #endif /* _FRR_PCRE2_POSIX */ | |
15 | #elif defined(HAVE_LIBPCREPOSIX) | |
beee9b4a DS |
16 | #include <pcreposix.h> |
17 | #else | |
fe6b47b9 | 18 | #include <regex.h> |
061f5d1c | 19 | #endif /* HAVE_LIBPCRE2_POSIX */ |
fe6b47b9 | 20 | |
b21b19c5 | 21 | #include "thread.h" |
1ed72e0b | 22 | #include "log.h" |
d227617a | 23 | #include "sockunion.h" |
0878c8d4 | 24 | #include "qobj.h" |
de1a880c | 25 | #include "compiler.h" |
1c2facd1 | 26 | #include "northbound.h" |
0798d276 | 27 | #include "zlog_live.h" |
b21b19c5 | 28 | |
5e244469 RW |
29 | #ifdef __cplusplus |
30 | extern "C" { | |
31 | #endif | |
32 | ||
a8dfd147 DL |
33 | struct json_object; |
34 | ||
2af38873 | 35 | #define VTY_BUFSIZ 4096 |
718e3744 | 36 | #define VTY_MAXHIST 20 |
1c2facd1 | 37 | #define VTY_MAXDEPTH 8 |
718e3744 | 38 | |
0b3eed38 | 39 | #define VTY_MAXCFGCHANGES 16 |
a6233bfc | 40 | |
7ab57d19 DS |
41 | struct vty_error { |
42 | char error_buf[VTY_BUFSIZ]; | |
43 | uint32_t line_num; | |
44 | }; | |
45 | ||
a6233bfc | 46 | struct vty_cfg_change { |
8427e0e6 | 47 | char xpath[XPATH_MAXLEN]; |
a6233bfc RW |
48 | enum nb_operation operation; |
49 | const char *value; | |
50 | }; | |
51 | ||
43dd8caf DL |
52 | PREDECL_DLIST(vtys); |
53 | ||
718e3744 | 54 | /* VTY struct. */ |
d62a17ae | 55 | struct vty { |
43dd8caf DL |
56 | struct vtys_item itm; |
57 | ||
d62a17ae | 58 | /* File descripter of this vty. */ |
59 | int fd; | |
718e3744 | 60 | |
d62a17ae | 61 | /* output FD, to support stdin/stdout combination */ |
62 | int wfd; | |
c5e69a02 | 63 | |
2cddf2ff QY |
64 | /* File output, used for VTYSH only */ |
65 | FILE *of; | |
66 | FILE *of_saved; | |
67 | ||
68 | /* whether we are using pager or not */ | |
69 | bool is_paged; | |
70 | ||
d62a17ae | 71 | /* Is this vty connect to file or not */ |
72 | enum { VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV } type; | |
718e3744 | 73 | |
d62a17ae | 74 | /* Node status of this vty */ |
75 | int node; | |
718e3744 | 76 | |
d62a17ae | 77 | /* Failure count */ |
78 | int fail; | |
718e3744 | 79 | |
fe6b47b9 QY |
80 | /* Output filer regex */ |
81 | bool filter; | |
82 | regex_t include; | |
83 | ||
0b42d81a QY |
84 | /* Line buffer */ |
85 | struct buffer *lbuf; | |
86 | ||
d62a17ae | 87 | /* Output buffer. */ |
88 | struct buffer *obuf; | |
718e3744 | 89 | |
d62a17ae | 90 | /* Command input buffer */ |
91 | char *buf; | |
718e3744 | 92 | |
d62a17ae | 93 | /* Command input error buffer */ |
7ab57d19 | 94 | struct list *error; |
5689fe5f | 95 | |
d62a17ae | 96 | /* Command cursor point */ |
97 | int cp; | |
718e3744 | 98 | |
d62a17ae | 99 | /* Command length */ |
100 | int length; | |
718e3744 | 101 | |
d62a17ae | 102 | /* Command max length. */ |
103 | int max; | |
718e3744 | 104 | |
d62a17ae | 105 | /* Histry of command */ |
106 | char *hist[VTY_MAXHIST]; | |
718e3744 | 107 | |
d62a17ae | 108 | /* History lookup current point */ |
109 | int hp; | |
718e3744 | 110 | |
d62a17ae | 111 | /* History insert end point */ |
112 | int hindex; | |
718e3744 | 113 | |
a6233bfc RW |
114 | /* Changes enqueued to be applied in the candidate configuration. */ |
115 | size_t num_cfg_changes; | |
116 | struct vty_cfg_change cfg_changes[VTY_MAXCFGCHANGES]; | |
117 | ||
1c2facd1 RW |
118 | /* XPath of the current node */ |
119 | int xpath_index; | |
120 | char xpath[VTY_MAXDEPTH][XPATH_MAXLEN]; | |
121 | ||
f344c66e RW |
122 | /* In configure mode. */ |
123 | bool config; | |
124 | ||
1c2facd1 RW |
125 | /* Private candidate configuration mode. */ |
126 | bool private_config; | |
127 | ||
128 | /* Candidate configuration. */ | |
129 | struct nb_config *candidate_config; | |
130 | ||
131 | /* Base candidate configuration. */ | |
132 | struct nb_config *candidate_config_base; | |
133 | ||
b855e95f | 134 | /* Dynamic transaction information. */ |
fd396924 CH |
135 | bool pending_allowed; |
136 | bool pending_commit; | |
b855e95f RW |
137 | char *pending_cmds_buf; |
138 | size_t pending_cmds_buflen; | |
139 | size_t pending_cmds_bufpos; | |
140 | ||
fbdc1c0a RW |
141 | /* Confirmed-commit timeout and rollback configuration. */ |
142 | struct thread *t_confirmed_commit_timeout; | |
143 | struct nb_config *confirmed_commit_rollback; | |
144 | ||
d62a17ae | 145 | /* qobj object ID (replacement for "index") */ |
146 | uint64_t qobj_index; | |
718e3744 | 147 | |
d62a17ae | 148 | /* qobj second-level object ID (replacement for "index_sub") */ |
149 | uint64_t qobj_index_sub; | |
718e3744 | 150 | |
d62a17ae | 151 | /* For escape character. */ |
152 | unsigned char escape; | |
718e3744 | 153 | |
d62a17ae | 154 | /* Current vty status. */ |
b2dde56b DL |
155 | enum { |
156 | VTY_NORMAL, | |
157 | VTY_CLOSE, | |
158 | VTY_MORE, | |
159 | VTY_MORELINE, | |
160 | VTY_PASSFD, | |
161 | } status; | |
162 | ||
163 | /* vtysh socket/fd passing (for terminal monitor) */ | |
164 | int pass_fd; | |
165 | ||
166 | /* CLI command return value (likely CMD_SUCCESS) when pass_fd != -1 */ | |
167 | uint8_t pass_fd_status[4]; | |
718e3744 | 168 | |
0798d276 DL |
169 | /* live logging target / terminal monitor */ |
170 | struct zlog_live_cfg live_log; | |
171 | ||
d62a17ae | 172 | /* IAC handling: was the last character received the |
173 | IAC (interpret-as-command) escape character (and therefore the next | |
174 | character will be the command code)? Refer to Telnet RFC 854. */ | |
175 | unsigned char iac; | |
718e3744 | 176 | |
d62a17ae | 177 | /* IAC SB (option subnegotiation) handling */ |
178 | unsigned char iac_sb_in_progress; | |
179 | /* At the moment, we care only about the NAWS (window size) negotiation, | |
180 | and that requires just a 5-character buffer (RFC 1073): | |
181 | <NAWS char> <16-bit width> <16-bit height> */ | |
9fc7ebf1 | 182 | #define TELNET_NAWS_SB_LEN 5 |
d62a17ae | 183 | unsigned char sb_buf[TELNET_NAWS_SB_LEN]; |
184 | /* How many subnegotiation characters have we received? We just drop | |
185 | those that do not fit in the buffer. */ | |
186 | size_t sb_len; | |
718e3744 | 187 | |
d62a17ae | 188 | /* Window width/height. */ |
189 | int width; | |
190 | int height; | |
718e3744 | 191 | |
d62a17ae | 192 | /* Configure lines. */ |
193 | int lines; | |
718e3744 | 194 | |
d62a17ae | 195 | /* Read and write thread. */ |
196 | struct thread *t_read; | |
197 | struct thread *t_write; | |
718e3744 | 198 | |
d62a17ae | 199 | /* Timeout seconds and thread. */ |
200 | unsigned long v_timeout; | |
201 | struct thread *t_timeout; | |
d227617a | 202 | |
d62a17ae | 203 | /* What address is this vty comming from. */ |
204 | char address[SU_ADDRSTRLEN]; | |
2071aa0e DL |
205 | |
206 | /* "frame" output. This is buffered and will be printed if some | |
207 | * actual output follows, or will be discarded if the frame ends | |
208 | * without any output. */ | |
209 | size_t frame_pos; | |
210 | char frame[1024]; | |
718e3744 | 211 | }; |
212 | ||
d62a17ae | 213 | static inline void vty_push_context(struct vty *vty, int node, uint64_t id) |
0878c8d4 | 214 | { |
d62a17ae | 215 | vty->node = node; |
216 | vty->qobj_index = id; | |
0878c8d4 DL |
217 | } |
218 | ||
3c5070be DL |
219 | /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to |
220 | * dereference "NULL->qobj_node.nid" */ | |
d62a17ae | 221 | #define VTY_PUSH_CONTEXT(nodeval, ptr) \ |
a50b7ceb | 222 | vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr)) |
d62a17ae | 223 | #define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL) |
224 | #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) \ | |
225 | do { \ | |
226 | vty->node = nodeval; \ | |
227 | /* qobj_index stays untouched */ \ | |
228 | vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \ | |
6a098b3a | 229 | } while (0) |
0878c8d4 DL |
230 | |
231 | /* can return NULL if context is invalid! */ | |
d62a17ae | 232 | #define VTY_GET_CONTEXT(structname) \ |
0878c8d4 | 233 | QOBJ_GET_TYPESAFE(vty->qobj_index, structname) |
d62a17ae | 234 | #define VTY_GET_CONTEXT_SUB(structname) \ |
6a098b3a | 235 | QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname) |
0878c8d4 DL |
236 | |
237 | /* will return if ptr is NULL. */ | |
d62a17ae | 238 | #define VTY_CHECK_CONTEXT(ptr) \ |
239 | if (!ptr) { \ | |
240 | vty_out(vty, \ | |
241 | "Current configuration object was deleted " \ | |
242 | "by another process.\n"); \ | |
243 | return CMD_WARNING; \ | |
0878c8d4 DL |
244 | } |
245 | ||
246 | /* struct structname *ptr = <context>; ptr will never be NULL. */ | |
d62a17ae | 247 | #define VTY_DECLVAR_CONTEXT(structname, ptr) \ |
248 | struct structname *ptr = VTY_GET_CONTEXT(structname); \ | |
0878c8d4 | 249 | VTY_CHECK_CONTEXT(ptr); |
d62a17ae | 250 | #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \ |
251 | struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \ | |
6a098b3a | 252 | VTY_CHECK_CONTEXT(ptr); |
a3d826f0 | 253 | #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \ |
996c9314 LB |
254 | if (vty->qobj_index == 0) \ |
255 | return CMD_NOT_MY_INSTANCE; \ | |
a3d826f0 CS |
256 | struct structname *ptr = VTY_GET_CONTEXT(structname); \ |
257 | VTY_CHECK_CONTEXT(ptr); | |
0878c8d4 | 258 | |
0cbed951 DL |
259 | #define VTY_DECLVAR_CONTEXT_VRF(vrfptr) \ |
260 | struct vrf *vrfptr; \ | |
261 | if (vty->node == CONFIG_NODE) \ | |
262 | vrfptr = vrf_lookup_by_id(VRF_DEFAULT); \ | |
263 | else \ | |
264 | vrfptr = VTY_GET_CONTEXT(vrf); \ | |
265 | VTY_CHECK_CONTEXT(vrfptr); \ | |
266 | MACRO_REQUIRE_SEMICOLON() /* end */ | |
267 | ||
1c2facd1 RW |
268 | /* XPath macros. */ |
269 | #define VTY_PUSH_XPATH(nodeval, value) \ | |
270 | do { \ | |
271 | if (vty->xpath_index >= VTY_MAXDEPTH) { \ | |
272 | vty_out(vty, "%% Reached maximum CLI depth (%u)\n", \ | |
273 | VTY_MAXDEPTH); \ | |
274 | return CMD_WARNING; \ | |
275 | } \ | |
276 | vty->node = nodeval; \ | |
277 | strlcpy(vty->xpath[vty->xpath_index], value, \ | |
278 | sizeof(vty->xpath[0])); \ | |
279 | vty->xpath_index++; \ | |
280 | } while (0) | |
281 | ||
282 | #define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1] | |
283 | ||
284 | #define VTY_CHECK_XPATH \ | |
285 | do { \ | |
caaa8b9c RW |
286 | if (vty->type != VTY_FILE && !vty->private_config \ |
287 | && vty->xpath_index > 0 \ | |
1c2facd1 RW |
288 | && !yang_dnode_exists(vty->candidate_config->dnode, \ |
289 | VTY_CURR_XPATH)) { \ | |
290 | vty_out(vty, \ | |
291 | "Current configuration object was deleted " \ | |
292 | "by another process.\n\n"); \ | |
293 | return CMD_WARNING; \ | |
294 | } \ | |
295 | } while (0) | |
296 | ||
d62a17ae | 297 | struct vty_arg { |
298 | const char *name; | |
299 | const char *value; | |
300 | const char **argv; | |
301 | int argc; | |
eac6e3f0 RW |
302 | }; |
303 | ||
718e3744 | 304 | /* Integrated configuration file. */ |
e20dc2ba | 305 | #define INTEGRATE_DEFAULT_CONFIG "frr.conf" |
718e3744 | 306 | |
718e3744 | 307 | /* Default time out value */ |
308 | #define VTY_TIMEOUT_DEFAULT 600 | |
309 | ||
310 | /* Vty read buffer size. */ | |
311 | #define VTY_READ_BUFSIZ 512 | |
312 | ||
313 | /* Directory separator. */ | |
314 | #ifndef DIRECTORY_SEP | |
315 | #define DIRECTORY_SEP '/' | |
316 | #endif /* DIRECTORY_SEP */ | |
317 | ||
318 | #ifndef IS_DIRECTORY_SEP | |
319 | #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP) | |
320 | #endif | |
321 | ||
718e3744 | 322 | /* Prototypes. */ |
2950f5da | 323 | extern void vty_init(struct thread_master *, bool do_command_logging); |
d62a17ae | 324 | extern void vty_init_vtysh(void); |
325 | extern void vty_terminate(void); | |
326 | extern void vty_reset(void); | |
327 | extern struct vty *vty_new(void); | |
154b9e8f | 328 | extern struct vty *vty_stdio(void (*atclose)(int isexit)); |
2071aa0e DL |
329 | |
330 | /* - vty_frame() output goes to a buffer (for context-begin markers) | |
331 | * - vty_out() will first print this buffer, and clear it | |
332 | * - vty_endframe() clears the buffer without printing it, and prints an | |
333 | * extra string if the buffer was empty before (for context-end markers) | |
334 | */ | |
afb35622 DL |
335 | extern int vty_out(struct vty *, const char *, ...) PRINTFRR(2, 3); |
336 | extern void vty_frame(struct vty *, const char *, ...) PRINTFRR(2, 3); | |
2071aa0e | 337 | extern void vty_endframe(struct vty *, const char *); |
30f0195d | 338 | extern bool vty_set_include(struct vty *vty, const char *regexp); |
a8dfd147 DL |
339 | /* returns CMD_SUCCESS so you can do a one-line "return vty_json(...)" |
340 | * NULL check and json_object_free() is included. | |
d7c6467b DS |
341 | * |
342 | * _no_pretty means do not add a bunch of newlines and dump the output | |
343 | * as densely as possible. | |
a8dfd147 DL |
344 | */ |
345 | extern int vty_json(struct vty *vty, struct json_object *json); | |
d7c6467b | 346 | extern int vty_json_no_pretty(struct vty *vty, struct json_object *json); |
2071aa0e | 347 | |
b2dde56b DL |
348 | /* post fd to be passed to the vtysh client |
349 | * fd is owned by the VTY code after this and will be closed when done | |
350 | */ | |
351 | extern void vty_pass_fd(struct vty *vty, int fd); | |
352 | ||
1c2facd1 RW |
353 | extern bool vty_read_config(struct nb_config *config, const char *config_file, |
354 | char *config_default_dir); | |
d62a17ae | 355 | extern void vty_time_print(struct vty *, int); |
356 | extern void vty_serv_sock(const char *, unsigned short, const char *); | |
357 | extern void vty_close(struct vty *); | |
358 | extern char *vty_get_cwd(void); | |
763725cd | 359 | extern void vty_update_xpath(const char *oldpath, const char *newpath); |
f344c66e RW |
360 | extern int vty_config_enter(struct vty *vty, bool private_config, |
361 | bool exclusive); | |
362 | extern void vty_config_exit(struct vty *); | |
791ded4a | 363 | extern int vty_config_node_exit(struct vty *); |
d62a17ae | 364 | extern int vty_shell(struct vty *); |
365 | extern int vty_shell_serv(struct vty *); | |
366 | extern void vty_hello(struct vty *); | |
718e3744 | 367 | |
154b9e8f DL |
368 | /* ^Z / SIGTSTP handling */ |
369 | extern void vty_stdio_suspend(void); | |
370 | extern void vty_stdio_resume(void); | |
371 | extern void vty_stdio_close(void); | |
372 | ||
5e244469 RW |
373 | #ifdef __cplusplus |
374 | } | |
375 | #endif | |
376 | ||
718e3744 | 377 | #endif /* _ZEBRA_VTY_H */ |