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"
29 #include "mgmt_fe_client.h"
37 #define VTY_BUFSIZ 4096
38 #define VTY_MAXHIST 20
39 #define VTY_MAXDEPTH 8
41 #define VTY_MAXCFGCHANGES 16
44 char error_buf
[VTY_BUFSIZ
];
48 struct vty_cfg_change
{
49 char xpath
[XPATH_MAXLEN
];
50 enum nb_operation operation
;
60 /* File descripter of this vty. */
63 /* output FD, to support stdin/stdout combination */
66 /* File output, used for VTYSH only */
70 /* whether we are using pager or not */
73 /* Is this vty connect to file or not */
74 enum { VTY_TERM
, VTY_FILE
, VTY_SHELL
, VTY_SHELL_SERV
} type
;
76 /* Node status of this vty */
82 /* Output filer regex */
92 /* Command input buffer */
95 /* Command input error buffer */
98 /* Command cursor point */
104 /* Command max length. */
107 /* Histry of command */
108 char *hist
[VTY_MAXHIST
];
110 /* History lookup current point */
113 /* History insert end point */
116 /* Changes enqueued to be applied in the candidate configuration. */
117 size_t num_cfg_changes
;
118 struct nb_cfg_change cfg_changes
[VTY_MAXCFGCHANGES
];
120 /* XPath of the current node */
122 char xpath
[VTY_MAXDEPTH
][XPATH_MAXLEN
];
125 * Keep track of how many SET_CFG requests has been sent so far that
126 * has not been committed yet.
128 size_t mgmt_num_pending_setcfg
;
130 /* In configure mode. */
133 /* Private candidate configuration mode. */
136 /* Candidate configuration. */
137 struct nb_config
*candidate_config
;
139 /* Base candidate configuration. */
140 struct nb_config
*candidate_config_base
;
142 /* Dynamic transaction information. */
143 bool pending_allowed
;
145 bool no_implicit_commit
;
146 char *pending_cmds_buf
;
147 size_t pending_cmds_buflen
;
148 size_t pending_cmds_bufpos
;
150 /* Confirmed-commit timeout and rollback configuration. */
151 struct thread
*t_confirmed_commit_timeout
;
152 struct nb_config
*confirmed_commit_rollback
;
154 /* qobj object ID (replacement for "index") */
157 /* qobj second-level object ID (replacement for "index_sub") */
158 uint64_t qobj_index_sub
;
160 /* For escape character. */
161 unsigned char escape
;
163 /* Current vty status. */
172 /* vtysh socket/fd passing (for terminal monitor) */
175 /* CLI command return value (likely CMD_SUCCESS) when pass_fd != -1 */
176 uint8_t pass_fd_status
[4];
178 /* live logging target / terminal monitor */
179 struct zlog_live_cfg live_log
;
181 /* IAC handling: was the last character received the
182 IAC (interpret-as-command) escape character (and therefore the next
183 character will be the command code)? Refer to Telnet RFC 854. */
186 /* IAC SB (option subnegotiation) handling */
187 unsigned char iac_sb_in_progress
;
188 /* At the moment, we care only about the NAWS (window size) negotiation,
189 and that requires just a 5-character buffer (RFC 1073):
190 <NAWS char> <16-bit width> <16-bit height> */
191 #define TELNET_NAWS_SB_LEN 5
192 unsigned char sb_buf
[TELNET_NAWS_SB_LEN
];
193 /* How many subnegotiation characters have we received? We just drop
194 those that do not fit in the buffer. */
197 /* Window width/height. */
201 /* Configure lines. */
204 /* Read and write thread. */
205 struct thread
*t_read
;
206 struct thread
*t_write
;
208 /* Timeout seconds and thread. */
209 unsigned long v_timeout
;
210 struct thread
*t_timeout
;
212 /* What address is this vty comming from. */
213 char address
[SU_ADDRSTRLEN
];
215 /* "frame" output. This is buffered and will be printed if some
216 * actual output follows, or will be discarded if the frame ends
217 * without any output. */
221 uintptr_t mgmt_session_id
;
222 uint64_t mgmt_client_id
;
223 uint64_t mgmt_req_id
;
224 bool mgmt_req_pending
;
225 bool mgmt_locked_candidate_ds
;
228 static inline void vty_push_context(struct vty
*vty
, int node
, uint64_t id
)
231 vty
->qobj_index
= id
;
234 /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
235 * dereference "NULL->qobj_node.nid" */
236 #define VTY_PUSH_CONTEXT(nodeval, ptr) \
237 vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
238 #define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL)
239 #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) \
241 vty->node = nodeval; \
242 /* qobj_index stays untouched */ \
243 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
246 /* can return NULL if context is invalid! */
247 #define VTY_GET_CONTEXT(structname) \
248 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
249 #define VTY_GET_CONTEXT_SUB(structname) \
250 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
252 /* will return if ptr is NULL. */
253 #define VTY_CHECK_CONTEXT(ptr) \
256 "Current configuration object was deleted " \
257 "by another process.\n"); \
258 return CMD_WARNING; \
261 /* struct structname *ptr = <context>; ptr will never be NULL. */
262 #define VTY_DECLVAR_CONTEXT(structname, ptr) \
263 struct structname *ptr = VTY_GET_CONTEXT(structname); \
264 VTY_CHECK_CONTEXT(ptr);
265 #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
266 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
267 VTY_CHECK_CONTEXT(ptr);
268 #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \
269 if (vty->qobj_index == 0) \
270 return CMD_NOT_MY_INSTANCE; \
271 struct structname *ptr = VTY_GET_CONTEXT(structname); \
272 VTY_CHECK_CONTEXT(ptr);
274 #define VTY_DECLVAR_CONTEXT_VRF(vrfptr) \
275 struct vrf *vrfptr; \
276 if (vty->node == CONFIG_NODE) \
277 vrfptr = vrf_lookup_by_id(VRF_DEFAULT); \
279 vrfptr = VTY_GET_CONTEXT(vrf); \
280 VTY_CHECK_CONTEXT(vrfptr); \
281 MACRO_REQUIRE_SEMICOLON() /* end */
284 #define VTY_PUSH_XPATH(nodeval, value) \
286 if (vty->xpath_index >= VTY_MAXDEPTH) { \
287 vty_out(vty, "%% Reached maximum CLI depth (%u)\n", \
289 return CMD_WARNING; \
291 vty->node = nodeval; \
292 strlcpy(vty->xpath[vty->xpath_index], value, \
293 sizeof(vty->xpath[0])); \
294 vty->xpath_index++; \
297 #define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1]
299 #define VTY_CHECK_XPATH \
301 if (vty->type != VTY_FILE && !vty->private_config \
302 && vty->xpath_index > 0 \
303 && !yang_dnode_exists(vty->candidate_config->dnode, \
306 "Current configuration object was deleted " \
307 "by another process.\n\n"); \
308 return CMD_WARNING; \
319 /* Integrated configuration file. */
320 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
322 /* Default time out value */
323 #define VTY_TIMEOUT_DEFAULT 600
325 /* Vty read buffer size. */
326 #define VTY_READ_BUFSIZ 512
328 /* Directory separator. */
329 #ifndef DIRECTORY_SEP
330 #define DIRECTORY_SEP '/'
331 #endif /* DIRECTORY_SEP */
333 #ifndef IS_DIRECTORY_SEP
334 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
337 extern struct nb_config
*vty_mgmt_candidate_config
;
340 extern void vty_init(struct thread_master
*, bool do_command_logging
);
341 extern void vty_init_vtysh(void);
342 extern void vty_terminate(void);
343 extern void vty_reset(void);
344 extern struct vty
*vty_new(void);
345 extern struct vty
*vty_stdio(void (*atclose
)(int isexit
));
347 /* - vty_frame() output goes to a buffer (for context-begin markers)
348 * - vty_out() will first print this buffer, and clear it
349 * - vty_endframe() clears the buffer without printing it, and prints an
350 * extra string if the buffer was empty before (for context-end markers)
352 extern int vty_out(struct vty
*, const char *, ...) PRINTFRR(2, 3);
353 extern void vty_frame(struct vty
*, const char *, ...) PRINTFRR(2, 3);
354 extern void vty_endframe(struct vty
*, const char *);
355 extern bool vty_set_include(struct vty
*vty
, const char *regexp
);
356 /* returns CMD_SUCCESS so you can do a one-line "return vty_json(...)"
357 * NULL check and json_object_free() is included.
359 * _no_pretty means do not add a bunch of newlines and dump the output
360 * as densely as possible.
362 extern int vty_json(struct vty
*vty
, struct json_object
*json
);
363 extern int vty_json_no_pretty(struct vty
*vty
, struct json_object
*json
);
364 extern void vty_json_empty(struct vty
*vty
);
365 /* post fd to be passed to the vtysh client
366 * fd is owned by the VTY code after this and will be closed when done
368 extern void vty_pass_fd(struct vty
*vty
, int fd
);
370 extern bool vty_read_config(struct nb_config
*config
, const char *config_file
,
371 char *config_default_dir
);
372 extern void vty_time_print(struct vty
*, int);
373 extern void vty_serv_sock(const char *, unsigned short, const char *);
374 extern void vty_close(struct vty
*);
375 extern char *vty_get_cwd(void);
376 extern void vty_update_xpath(const char *oldpath
, const char *newpath
);
377 extern int vty_config_enter(struct vty
*vty
, bool private_config
,
379 extern void vty_config_exit(struct vty
*);
380 extern int vty_config_node_exit(struct vty
*);
381 extern int vty_shell(struct vty
*);
382 extern int vty_shell_serv(struct vty
*);
383 extern void vty_hello(struct vty
*);
385 /* ^Z / SIGTSTP handling */
386 extern void vty_stdio_suspend(void);
387 extern void vty_stdio_resume(void);
388 extern void vty_stdio_close(void);
390 extern void vty_init_mgmt_fe(void);
391 extern bool vty_mgmt_fe_enabled(void);
392 extern int vty_mgmt_send_config_data(struct vty
*vty
);
393 extern int vty_mgmt_send_commit_config(struct vty
*vty
, bool validate_only
,
395 extern int vty_mgmt_send_get_config(struct vty
*vty
,
396 Mgmtd__DatastoreId datastore
,
397 const char **xpath_list
, int num_req
);
398 extern int vty_mgmt_send_get_data(struct vty
*vty
, Mgmtd__DatastoreId datastore
,
399 const char **xpath_list
, int num_req
);
400 extern int vty_mgmt_send_lockds_req(struct vty
*vty
, Mgmtd__DatastoreId ds_id
,
402 extern void vty_mgmt_resume_response(struct vty
*vty
, bool success
);
404 static inline bool vty_needs_implicit_commit(struct vty
*vty
)
406 return (frr_get_cli_mode() == FRR_CLI_CLASSIC
407 ? ((vty
->pending_allowed
|| vty
->no_implicit_commit
)
417 #endif /* _ZEBRA_VTY_H */