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"
28 #include "mgmt_fe_client.h"
36 #define VTY_BUFSIZ 4096
37 #define VTY_MAXHIST 20
38 #define VTY_MAXDEPTH 8
40 #define VTY_MAXCFGCHANGES 16
43 char error_buf
[VTY_BUFSIZ
];
47 struct vty_cfg_change
{
48 char xpath
[XPATH_MAXLEN
];
49 enum nb_operation operation
;
59 /* File descripter of this vty. */
62 /* output FD, to support stdin/stdout combination */
65 /* File output, used for VTYSH only */
69 /* whether we are using pager or not */
72 /* Is this vty connect to file or not */
73 enum { VTY_TERM
, VTY_FILE
, VTY_SHELL
, VTY_SHELL_SERV
} type
;
75 /* Node status of this vty */
81 /* Output filer regex */
91 /* Command input buffer */
94 /* Command input error buffer */
97 /* Command cursor point */
103 /* Command max length. */
106 /* Histry of command */
107 char *hist
[VTY_MAXHIST
];
109 /* History lookup current point */
112 /* History insert end point */
115 /* Changes enqueued to be applied in the candidate configuration. */
116 size_t num_cfg_changes
;
117 struct nb_cfg_change cfg_changes
[VTY_MAXCFGCHANGES
];
119 /* XPath of the current node */
121 char xpath
[VTY_MAXDEPTH
][XPATH_MAXLEN
];
123 /* In configure mode. */
126 /* Private candidate configuration mode. */
129 /* Candidate configuration. */
130 struct nb_config
*candidate_config
;
132 /* Base candidate configuration. */
133 struct nb_config
*candidate_config_base
;
135 /* Dynamic transaction information. */
136 bool pending_allowed
;
138 bool no_implicit_commit
;
139 char *pending_cmds_buf
;
140 size_t pending_cmds_buflen
;
141 size_t pending_cmds_bufpos
;
143 /* Confirmed-commit timeout and rollback configuration. */
144 struct thread
*t_confirmed_commit_timeout
;
145 struct nb_config
*confirmed_commit_rollback
;
147 /* qobj object ID (replacement for "index") */
150 /* qobj second-level object ID (replacement for "index_sub") */
151 uint64_t qobj_index_sub
;
153 /* For escape character. */
154 unsigned char escape
;
156 /* Current vty status. */
165 /* vtysh socket/fd passing (for terminal monitor) */
168 /* CLI command return value (likely CMD_SUCCESS) when pass_fd != -1 */
169 uint8_t pass_fd_status
[4];
171 /* live logging target / terminal monitor */
172 struct zlog_live_cfg live_log
;
174 /* IAC handling: was the last character received the
175 IAC (interpret-as-command) escape character (and therefore the next
176 character will be the command code)? Refer to Telnet RFC 854. */
179 /* IAC SB (option subnegotiation) handling */
180 unsigned char iac_sb_in_progress
;
181 /* At the moment, we care only about the NAWS (window size) negotiation,
182 and that requires just a 5-character buffer (RFC 1073):
183 <NAWS char> <16-bit width> <16-bit height> */
184 #define TELNET_NAWS_SB_LEN 5
185 unsigned char sb_buf
[TELNET_NAWS_SB_LEN
];
186 /* How many subnegotiation characters have we received? We just drop
187 those that do not fit in the buffer. */
190 /* Window width/height. */
194 /* Configure lines. */
197 /* Read and write thread. */
198 struct thread
*t_read
;
199 struct thread
*t_write
;
201 /* Timeout seconds and thread. */
202 unsigned long v_timeout
;
203 struct thread
*t_timeout
;
205 /* What address is this vty comming from. */
206 char address
[SU_ADDRSTRLEN
];
208 /* "frame" output. This is buffered and will be printed if some
209 * actual output follows, or will be discarded if the frame ends
210 * without any output. */
214 uintptr_t mgmt_session_id
;
215 uint64_t mgmt_client_id
;
216 uint64_t mgmt_req_id
;
217 bool mgmt_req_pending
;
218 bool mgmt_locked_candidate_ds
;
221 static inline void vty_push_context(struct vty
*vty
, int node
, uint64_t id
)
224 vty
->qobj_index
= id
;
227 /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
228 * dereference "NULL->qobj_node.nid" */
229 #define VTY_PUSH_CONTEXT(nodeval, ptr) \
230 vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
231 #define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL)
232 #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) \
234 vty->node = nodeval; \
235 /* qobj_index stays untouched */ \
236 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
239 /* can return NULL if context is invalid! */
240 #define VTY_GET_CONTEXT(structname) \
241 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
242 #define VTY_GET_CONTEXT_SUB(structname) \
243 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
245 /* will return if ptr is NULL. */
246 #define VTY_CHECK_CONTEXT(ptr) \
249 "Current configuration object was deleted " \
250 "by another process.\n"); \
251 return CMD_WARNING; \
254 /* struct structname *ptr = <context>; ptr will never be NULL. */
255 #define VTY_DECLVAR_CONTEXT(structname, ptr) \
256 struct structname *ptr = VTY_GET_CONTEXT(structname); \
257 VTY_CHECK_CONTEXT(ptr);
258 #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
259 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
260 VTY_CHECK_CONTEXT(ptr);
261 #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \
262 if (vty->qobj_index == 0) \
263 return CMD_NOT_MY_INSTANCE; \
264 struct structname *ptr = VTY_GET_CONTEXT(structname); \
265 VTY_CHECK_CONTEXT(ptr);
267 #define VTY_DECLVAR_CONTEXT_VRF(vrfptr) \
268 struct vrf *vrfptr; \
269 if (vty->node == CONFIG_NODE) \
270 vrfptr = vrf_lookup_by_id(VRF_DEFAULT); \
272 vrfptr = VTY_GET_CONTEXT(vrf); \
273 VTY_CHECK_CONTEXT(vrfptr); \
274 MACRO_REQUIRE_SEMICOLON() /* end */
277 #define VTY_PUSH_XPATH(nodeval, value) \
279 if (vty->xpath_index >= VTY_MAXDEPTH) { \
280 vty_out(vty, "%% Reached maximum CLI depth (%u)\n", \
282 return CMD_WARNING; \
284 vty->node = nodeval; \
285 strlcpy(vty->xpath[vty->xpath_index], value, \
286 sizeof(vty->xpath[0])); \
287 vty->xpath_index++; \
290 #define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1]
292 #define VTY_CHECK_XPATH \
294 if (vty->type != VTY_FILE && !vty->private_config \
295 && vty->xpath_index > 0 \
296 && !yang_dnode_exists(vty->candidate_config->dnode, \
299 "Current configuration object was deleted " \
300 "by another process.\n\n"); \
301 return CMD_WARNING; \
312 /* Integrated configuration file. */
313 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
315 /* Default time out value */
316 #define VTY_TIMEOUT_DEFAULT 600
318 /* Vty read buffer size. */
319 #define VTY_READ_BUFSIZ 512
321 /* Directory separator. */
322 #ifndef DIRECTORY_SEP
323 #define DIRECTORY_SEP '/'
324 #endif /* DIRECTORY_SEP */
326 #ifndef IS_DIRECTORY_SEP
327 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
330 extern struct nb_config
*vty_mgmt_candidate_config
;
333 extern void vty_init(struct thread_master
*, bool do_command_logging
);
334 extern void vty_init_vtysh(void);
335 extern void vty_terminate(void);
336 extern void vty_reset(void);
337 extern struct vty
*vty_new(void);
338 extern struct vty
*vty_stdio(void (*atclose
)(int isexit
));
340 /* - vty_frame() output goes to a buffer (for context-begin markers)
341 * - vty_out() will first print this buffer, and clear it
342 * - vty_endframe() clears the buffer without printing it, and prints an
343 * extra string if the buffer was empty before (for context-end markers)
345 extern int vty_out(struct vty
*, const char *, ...) PRINTFRR(2, 3);
346 extern void vty_frame(struct vty
*, const char *, ...) PRINTFRR(2, 3);
347 extern void vty_endframe(struct vty
*, const char *);
348 extern bool vty_set_include(struct vty
*vty
, const char *regexp
);
349 /* returns CMD_SUCCESS so you can do a one-line "return vty_json(...)"
350 * NULL check and json_object_free() is included.
352 * _no_pretty means do not add a bunch of newlines and dump the output
353 * as densely as possible.
355 extern int vty_json(struct vty
*vty
, struct json_object
*json
);
356 extern int vty_json_no_pretty(struct vty
*vty
, struct json_object
*json
);
357 extern void vty_json_empty(struct vty
*vty
);
358 /* post fd to be passed to the vtysh client
359 * fd is owned by the VTY code after this and will be closed when done
361 extern void vty_pass_fd(struct vty
*vty
, int fd
);
363 extern bool vty_read_config(struct nb_config
*config
, const char *config_file
,
364 char *config_default_dir
);
365 extern void vty_time_print(struct vty
*, int);
366 extern void vty_serv_sock(const char *, unsigned short, const char *);
367 extern void vty_close(struct vty
*);
368 extern char *vty_get_cwd(void);
369 extern void vty_update_xpath(const char *oldpath
, const char *newpath
);
370 extern int vty_config_enter(struct vty
*vty
, bool private_config
,
372 extern void vty_config_exit(struct vty
*);
373 extern int vty_config_node_exit(struct vty
*);
374 extern int vty_shell(struct vty
*);
375 extern int vty_shell_serv(struct vty
*);
376 extern void vty_hello(struct vty
*);
378 /* ^Z / SIGTSTP handling */
379 extern void vty_stdio_suspend(void);
380 extern void vty_stdio_resume(void);
381 extern void vty_stdio_close(void);
383 extern void vty_init_mgmt_fe(void);
384 extern bool vty_mgmt_fe_enabled(void);
385 extern int vty_mgmt_send_config_data(struct vty
*vty
);
386 extern int vty_mgmt_send_commit_config(struct vty
*vty
, bool validate_only
,
388 extern int vty_mgmt_send_get_config(struct vty
*vty
, Mgmtd__DatastoreId datastore
,
389 const char **xpath_list
, int num_req
);
390 extern int vty_mgmt_send_get_data(struct vty
*vty
, Mgmtd__DatastoreId datastore
,
391 const char **xpath_list
, int num_req
);
392 extern int vty_mgmt_send_lockds_req(struct vty
*vty
, Mgmtd__DatastoreId ds_id
,
399 #endif /* _ZEBRA_VTY_H */