1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Virtual terminal [aka TeletYpe] interface routine
3 * Copyright (C) 1997 Kunihiro Ishiguro
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)
16 #include <pcreposix.h>
19 #endif /* HAVE_LIBPCRE2_POSIX */
23 #include "sockunion.h"
26 #include "northbound.h"
27 #include "zlog_live.h"
35 #define VTY_BUFSIZ 4096
36 #define VTY_MAXHIST 20
37 #define VTY_MAXDEPTH 8
39 #define VTY_MAXCFGCHANGES 16
42 char error_buf
[VTY_BUFSIZ
];
46 struct vty_cfg_change
{
47 char xpath
[XPATH_MAXLEN
];
48 enum nb_operation operation
;
58 /* File descripter of this vty. */
61 /* output FD, to support stdin/stdout combination */
64 /* File output, used for VTYSH only */
68 /* whether we are using pager or not */
71 /* Is this vty connect to file or not */
72 enum { VTY_TERM
, VTY_FILE
, VTY_SHELL
, VTY_SHELL_SERV
} type
;
74 /* Node status of this vty */
80 /* Output filer regex */
90 /* Command input buffer */
93 /* Command input error buffer */
96 /* Command cursor point */
102 /* Command max length. */
105 /* Histry of command */
106 char *hist
[VTY_MAXHIST
];
108 /* History lookup current point */
111 /* History insert end point */
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
];
118 /* XPath of the current node */
120 char xpath
[VTY_MAXDEPTH
][XPATH_MAXLEN
];
122 /* In configure mode. */
125 /* Private candidate configuration mode. */
128 /* Candidate configuration. */
129 struct nb_config
*candidate_config
;
131 /* Base candidate configuration. */
132 struct nb_config
*candidate_config_base
;
134 /* Dynamic transaction information. */
135 bool pending_allowed
;
137 char *pending_cmds_buf
;
138 size_t pending_cmds_buflen
;
139 size_t pending_cmds_bufpos
;
141 /* Confirmed-commit timeout and rollback configuration. */
142 struct thread
*t_confirmed_commit_timeout
;
143 struct nb_config
*confirmed_commit_rollback
;
145 /* qobj object ID (replacement for "index") */
148 /* qobj second-level object ID (replacement for "index_sub") */
149 uint64_t qobj_index_sub
;
151 /* For escape character. */
152 unsigned char escape
;
154 /* Current vty status. */
163 /* vtysh socket/fd passing (for terminal monitor) */
166 /* CLI command return value (likely CMD_SUCCESS) when pass_fd != -1 */
167 uint8_t pass_fd_status
[4];
169 /* live logging target / terminal monitor */
170 struct zlog_live_cfg live_log
;
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. */
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> */
182 #define TELNET_NAWS_SB_LEN 5
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. */
188 /* Window width/height. */
192 /* Configure lines. */
195 /* Read and write thread. */
196 struct thread
*t_read
;
197 struct thread
*t_write
;
199 /* Timeout seconds and thread. */
200 unsigned long v_timeout
;
201 struct thread
*t_timeout
;
203 /* What address is this vty comming from. */
204 char address
[SU_ADDRSTRLEN
];
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. */
213 static inline void vty_push_context(struct vty
*vty
, int node
, uint64_t id
)
216 vty
->qobj_index
= id
;
219 /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
220 * dereference "NULL->qobj_node.nid" */
221 #define VTY_PUSH_CONTEXT(nodeval, ptr) \
222 vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
223 #define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL)
224 #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) \
226 vty->node = nodeval; \
227 /* qobj_index stays untouched */ \
228 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
231 /* can return NULL if context is invalid! */
232 #define VTY_GET_CONTEXT(structname) \
233 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
234 #define VTY_GET_CONTEXT_SUB(structname) \
235 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
237 /* will return if ptr is NULL. */
238 #define VTY_CHECK_CONTEXT(ptr) \
241 "Current configuration object was deleted " \
242 "by another process.\n"); \
243 return CMD_WARNING; \
246 /* struct structname *ptr = <context>; ptr will never be NULL. */
247 #define VTY_DECLVAR_CONTEXT(structname, ptr) \
248 struct structname *ptr = VTY_GET_CONTEXT(structname); \
249 VTY_CHECK_CONTEXT(ptr);
250 #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
251 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
252 VTY_CHECK_CONTEXT(ptr);
253 #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \
254 if (vty->qobj_index == 0) \
255 return CMD_NOT_MY_INSTANCE; \
256 struct structname *ptr = VTY_GET_CONTEXT(structname); \
257 VTY_CHECK_CONTEXT(ptr);
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); \
264 vrfptr = VTY_GET_CONTEXT(vrf); \
265 VTY_CHECK_CONTEXT(vrfptr); \
266 MACRO_REQUIRE_SEMICOLON() /* end */
269 #define VTY_PUSH_XPATH(nodeval, value) \
271 if (vty->xpath_index >= VTY_MAXDEPTH) { \
272 vty_out(vty, "%% Reached maximum CLI depth (%u)\n", \
274 return CMD_WARNING; \
276 vty->node = nodeval; \
277 strlcpy(vty->xpath[vty->xpath_index], value, \
278 sizeof(vty->xpath[0])); \
279 vty->xpath_index++; \
282 #define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1]
284 #define VTY_CHECK_XPATH \
286 if (vty->type != VTY_FILE && !vty->private_config \
287 && vty->xpath_index > 0 \
288 && !yang_dnode_exists(vty->candidate_config->dnode, \
291 "Current configuration object was deleted " \
292 "by another process.\n\n"); \
293 return CMD_WARNING; \
304 /* Integrated configuration file. */
305 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
307 /* Default time out value */
308 #define VTY_TIMEOUT_DEFAULT 600
310 /* Vty read buffer size. */
311 #define VTY_READ_BUFSIZ 512
313 /* Directory separator. */
314 #ifndef DIRECTORY_SEP
315 #define DIRECTORY_SEP '/'
316 #endif /* DIRECTORY_SEP */
318 #ifndef IS_DIRECTORY_SEP
319 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
323 extern void vty_init(struct thread_master
*, bool do_command_logging
);
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);
328 extern struct vty
*vty_stdio(void (*atclose
)(int isexit
));
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)
335 extern int vty_out(struct vty
*, const char *, ...) PRINTFRR(2, 3);
336 extern void vty_frame(struct vty
*, const char *, ...) PRINTFRR(2, 3);
337 extern void vty_endframe(struct vty
*, const char *);
338 extern bool vty_set_include(struct vty
*vty
, const char *regexp
);
339 /* returns CMD_SUCCESS so you can do a one-line "return vty_json(...)"
340 * NULL check and json_object_free() is included.
342 * _no_pretty means do not add a bunch of newlines and dump the output
343 * as densely as possible.
345 extern int vty_json(struct vty
*vty
, struct json_object
*json
);
346 extern int vty_json_no_pretty(struct vty
*vty
, struct json_object
*json
);
347 extern void vty_json_empty(struct vty
*vty
);
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
351 extern void vty_pass_fd(struct vty
*vty
, int fd
);
353 extern bool vty_read_config(struct nb_config
*config
, const char *config_file
,
354 char *config_default_dir
);
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);
359 extern void vty_update_xpath(const char *oldpath
, const char *newpath
);
360 extern int vty_config_enter(struct vty
*vty
, bool private_config
,
362 extern void vty_config_exit(struct vty
*);
363 extern int vty_config_node_exit(struct vty
*);
364 extern int vty_shell(struct vty
*);
365 extern int vty_shell_serv(struct vty
*);
366 extern void vty_hello(struct vty
*);
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);
377 #endif /* _ZEBRA_VTY_H */