Update changelog
[l2tpns.git] / cli.c
1 // L2TPNS Command Line Interface
2 // vim: sw=8 ts=8
3
4 #include <stdio.h>
5 #include <stddef.h>
6 #include <stdarg.h>
7 #include <unistd.h>
8 #include <sys/file.h>
9 #include <sys/stat.h>
10 #include <syslog.h>
11 #include <malloc.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <stdlib.h>
15 #include <time.h>
16 #include <arpa/inet.h>
17 #include <errno.h>
18 #include <sys/socket.h>
19 #include <sys/types.h>
20 #include <signal.h>
21 #include <dlfcn.h>
22 #include <netdb.h>
23 #include <libcli.h>
24 #include <linux/rtnetlink.h>
25
26 #include "dhcp6.h"
27 #include "l2tpns.h"
28 #include "constants.h"
29 #include "util.h"
30 #include "cluster.h"
31 #include "tbf.h"
32 #include "ll.h"
33 #ifdef BGP
34 #include "bgp.h"
35 #endif
36 #include "l2tplac.h"
37
38 extern tunnelt *tunnel;
39 extern bundlet *bundle;
40 extern sessiont *session;
41 extern radiust *radius;
42 extern ippoolt *ip_address_pool;
43 extern struct Tstats *_statistics;
44 static struct cli_def *cli = NULL;
45 extern configt *config;
46 extern config_descriptt config_values[];
47 #ifdef RINGBUFFER
48 extern struct Tringbuffer *ringbuffer;
49 #endif
50 extern struct cli_session_actions *cli_session_actions;
51 extern struct cli_tunnel_actions *cli_tunnel_actions;
52 extern tbft *filter_list;
53 extern ip_filtert *ip_filters;
54
55 struct
56 {
57 char critical;
58 char error;
59 char warning;
60 char info;
61 char calls;
62 char data;
63 } debug_flags;
64
65 #ifdef RINGBUFFER
66
67 static int debug_rb_tail;
68 static char *debug_levels[] = {
69 "CRIT",
70 "ERROR",
71 "WARN",
72 "INFO",
73 "CALL",
74 "DATA",
75 };
76
77 #endif
78
79 static int cmd_show_session(struct cli_def *cli, const char *command, char **argv, int argc);
80 static int cmd_show_group(struct cli_def *cli, const char *command, char **argv, int argc);
81 static int cmd_show_tunnels(struct cli_def *cli, const char *command, char **argv, int argc);
82 static int cmd_show_users(struct cli_def *cli, const char *command, char **argv, int argc);
83 static int cmd_show_radius(struct cli_def *cli, const char *command, char **argv, int argc);
84 static int cmd_show_version(struct cli_def *cli, const char *command, char **argv, int argc);
85 static int cmd_show_pool(struct cli_def *cli, const char *command, char **argv, int argc);
86 static int cmd_show_run(struct cli_def *cli, const char *command, char **argv, int argc);
87 static int cmd_show_banana(struct cli_def *cli, const char *command, char **argv, int argc);
88 static int cmd_show_plugins(struct cli_def *cli, const char *command, char **argv, int argc);
89 static int cmd_show_throttle(struct cli_def *cli, const char *command, char **argv, int argc);
90 static int cmd_write_memory(struct cli_def *cli, const char *command, char **argv, int argc);
91 static int cmd_drop_user(struct cli_def *cli, const char *command, char **argv, int argc);
92 static int cmd_drop_tunnel(struct cli_def *cli, const char *command, char **argv, int argc);
93 static int cmd_drop_session(struct cli_def *cli, const char *command, char **argv, int argc);
94 static int cmd_snoop(struct cli_def *cli, const char *command, char **argv, int argc);
95 static int cmd_no_snoop(struct cli_def *cli, const char *command, char **argv, int argc);
96 static int cmd_throttle(struct cli_def *cli, const char *command, char **argv, int argc);
97 static int cmd_no_throttle(struct cli_def *cli, const char *command, char **argv, int argc);
98 static int cmd_debug(struct cli_def *cli, const char *command, char **argv, int argc);
99 static int cmd_no_debug(struct cli_def *cli, const char *command, char **argv, int argc);
100 static int cmd_set(struct cli_def *cli, const char *command, char **argv, int argc);
101 static int cmd_load_plugin(struct cli_def *cli, const char *command, char **argv, int argc);
102 static int cmd_remove_plugin(struct cli_def *cli, const char *command, char **argv, int argc);
103 static int cmd_uptime(struct cli_def *cli, const char *command, char **argv, int argc);
104 static int cmd_shutdown(struct cli_def *cli, const char *command, char **argv, int argc);
105 static int cmd_reload(struct cli_def *cli, const char *command, char **argv, int argc);
106 static int cmd_setforward(struct cli_def *cli, const char *command, char **argv, int argc);
107 static int cmd_show_rmtlnsconf(struct cli_def *cli, const char *command, char **argv, int argc);
108
109 static int regular_stuff(struct cli_def *cli);
110
111 #ifdef STATISTICS
112 static int cmd_show_counters(struct cli_def *cli, const char *command, char **argv, int argc);
113 static int cmd_clear_counters(struct cli_def *cli, const char *command, char **argv, int argc);
114 #endif /* STATISTICS */
115
116 #ifdef BGP
117 #define MODE_CONFIG_BGP 8
118 static int cmd_router_bgp(struct cli_def *cli, const char *command, char **argv, int argc);
119 static int cmd_router_bgp_neighbour(struct cli_def *cli, const char *command, char **argv, int argc);
120 static int cmd_router_bgp_no_neighbour(struct cli_def *cli, const char *command, char **argv, int argc);
121 static int cmd_show_bgp(struct cli_def *cli, const char *command, char **argv, int argc);
122 static int cmd_suspend_bgp(struct cli_def *cli, const char *command, char **argv, int argc);
123 static int cmd_no_suspend_bgp(struct cli_def *cli, const char *command, char **argv, int argc);
124 static int cmd_restart_bgp(struct cli_def *cli, const char *command, char **argv, int argc);
125 #endif /* BGP */
126
127 #define MODE_CONFIG_NACL 9
128 static int cmd_ip_access_list(struct cli_def *cli, const char *command, char **argv, int argc);
129 static int cmd_no_ip_access_list(struct cli_def *cli, const char *command, char **argv, int argc);
130 static int cmd_ip_access_list_rule(struct cli_def *cli, const char *command, char **argv, int argc);
131 static int cmd_filter(struct cli_def *cli, const char *command, char **argv, int argc);
132 static int cmd_no_filter(struct cli_def *cli, const char *command, char **argv, int argc);
133 static int cmd_show_access_list(struct cli_def *cli, const char *command, char **argv, int argc);
134
135 /* match if b is a substr of a */
136 #define MATCH(a,b) (!strncmp((a), (b), strlen(b)))
137
138 void init_cli()
139 {
140 FILE *f;
141 char buf[4096];
142 struct cli_command *c;
143 struct cli_command *c2;
144
145 cli = cli_init();
146
147 c = cli_register_command(cli, NULL, "show", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
148 cli_register_command(cli, c, "banana", cmd_show_banana, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a banana");
149 #ifdef BGP
150 cli_register_command(cli, c, "bgp", cmd_show_bgp, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show BGP status");
151 #endif /* BGP */
152 cli_register_command(cli, c, "cluster", cmd_show_cluster, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show cluster information");
153 cli_register_command(cli, c, "ipcache", cmd_show_ipcache, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show contents of the IP cache");
154 cli_register_command(cli, c, "plugins", cmd_show_plugins, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all installed plugins");
155 cli_register_command(cli, c, "pool", cmd_show_pool, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the IP address allocation pool");
156 cli_register_command(cli, c, "radius", cmd_show_radius, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show active radius queries");
157 cli_register_command(cli, c, "running-config", cmd_show_run, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Show the currently running configuration");
158 cli_register_command(cli, c, "remotelns-conf", cmd_show_rmtlnsconf, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Show a list of remote LNS configuration");
159 cli_register_command(cli, c, "session", cmd_show_session, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of sessions or details for a single session");
160 cli_register_command(cli, c, "group", cmd_show_group, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of groups or details for a single group");
161 cli_register_command(cli, c, "tbf", cmd_show_tbf, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all token bucket filters in use");
162 cli_register_command(cli, c, "throttle", cmd_show_throttle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all throttled sessions and associated TBFs");
163 cli_register_command(cli, c, "tunnels", cmd_show_tunnels, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of tunnels or details for a single tunnel");
164 cli_register_command(cli, c, "users", cmd_show_users, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of all connected users or details of selected user");
165 cli_register_command(cli, c, "version", cmd_show_version, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show currently running software version");
166 cli_register_command(cli, c, "access-list", cmd_show_access_list, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show named access-list");
167
168 c2 = cli_register_command(cli, c, "histogram", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
169 cli_register_command(cli, c2, "idle", cmd_show_hist_idle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session idle times");
170 cli_register_command(cli, c2, "open", cmd_show_hist_open, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show histogram of session durations");
171
172 #ifdef STATISTICS
173 cli_register_command(cli, c, "counters", cmd_show_counters, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Display all the internal counters and running totals");
174
175 c = cli_register_command(cli, NULL, "clear", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
176 cli_register_command(cli, c, "counters", cmd_clear_counters, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Clear internal counters");
177 #endif
178
179 cli_register_command(cli, NULL, "uptime", cmd_uptime, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show uptime and bandwidth utilisation");
180 cli_register_command(cli, NULL, "shutdown", cmd_shutdown, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Shutdown l2tpns daemon and exit");
181 cli_register_command(cli, NULL, "reload", cmd_reload, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Reload configuration");
182
183 c = cli_register_command(cli, NULL, "write", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
184 cli_register_command(cli, c, "memory", cmd_write_memory, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Save the running config to flash");
185 cli_register_command(cli, c, "terminal", cmd_show_run, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show the running config");
186
187 cli_register_command(cli, NULL, "snoop", cmd_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Enable interception of a session");
188 cli_register_command(cli, NULL, "throttle", cmd_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Enable throttling of a session");
189 cli_register_command(cli, NULL, "filter", cmd_filter, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Add filtering to a session");
190 cli_register_command(cli, NULL, "debug", cmd_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Set the level of logging that is shown on the console");
191
192 #ifdef BGP
193 c = cli_register_command(cli, NULL, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
194 cli_register_command(cli, c, "bgp", cmd_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Withdraw routes from BGP neighbour");
195 #endif /* BGP */
196
197 c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
198 cli_register_command(cli, c, "snoop", cmd_no_snoop, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disable interception of a session");
199 cli_register_command(cli, c, "throttle", cmd_no_throttle, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disable throttling of a session");
200 cli_register_command(cli, c, "filter", cmd_no_filter, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Remove filtering from a session");
201 cli_register_command(cli, c, "debug", cmd_no_debug, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Turn off logging of a certain level of debugging");
202
203 #ifdef BGP
204 c2 = cli_register_command(cli, c, "suspend", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
205 cli_register_command(cli, c2, "bgp", cmd_no_suspend_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Advertise routes to BGP neighbour");
206
207 c = cli_register_command(cli, NULL, "restart", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
208 cli_register_command(cli, c, "bgp", cmd_restart_bgp, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Restart BGP");
209
210 c = cli_register_command(cli, NULL, "router", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
211 cli_register_command(cli, c, "bgp", cmd_router_bgp, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Configure BGP");
212
213 cli_register_command(cli, NULL, "neighbour", cmd_router_bgp_neighbour, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, "Configure BGP neighbour");
214
215 c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, NULL);
216 cli_register_command(cli, c, "neighbour", cmd_router_bgp_no_neighbour, PRIVILEGE_PRIVILEGED, MODE_CONFIG_BGP, "Remove BGP neighbour");
217 #endif /* BGP */
218
219 c = cli_register_command(cli, NULL, "drop", NULL, PRIVILEGE_PRIVILEGED, MODE_EXEC, NULL);
220 cli_register_command(cli, c, "user", cmd_drop_user, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a user");
221 cli_register_command(cli, c, "tunnel", cmd_drop_tunnel, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a tunnel and all sessions on that tunnel");
222 cli_register_command(cli, c, "session", cmd_drop_session, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Disconnect a session");
223
224 c = cli_register_command(cli, NULL, "load", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
225 cli_register_command(cli, c, "plugin", cmd_load_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Load a plugin");
226
227 c = cli_register_command(cli, NULL, "remove", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
228 cli_register_command(cli, c, "plugin", cmd_remove_plugin, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Remove a plugin");
229
230 cli_register_command(cli, NULL, "set", cmd_set, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Set a configuration variable");
231
232 cli_register_command(cli, NULL, "setforward", cmd_setforward, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Set the Remote LNS Forward");
233
234 c = cli_register_command(cli, NULL, "ip", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
235 cli_register_command(cli, c, "access-list", cmd_ip_access_list, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Add named access-list");
236
237 cli_register_command(cli, NULL, "permit", cmd_ip_access_list_rule, PRIVILEGE_PRIVILEGED, MODE_CONFIG_NACL, "Permit rule");
238 cli_register_command(cli, NULL, "deny", cmd_ip_access_list_rule, PRIVILEGE_PRIVILEGED, MODE_CONFIG_NACL, "Deny rule");
239
240 c = cli_register_command(cli, NULL, "no", NULL, PRIVILEGE_UNPRIVILEGED, MODE_CONFIG, NULL);
241 c2 = cli_register_command(cli, c, "ip", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, NULL);
242 cli_register_command(cli, c2, "access-list", cmd_no_ip_access_list, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "Remove named access-list");
243
244 // Enable regular processing
245 cli_regular(cli, regular_stuff);
246
247 if (!(f = fopen(CLIUSERS, "r")))
248 {
249 LOG(0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
250 }
251 else
252 {
253 while (fgets(buf, 4096, f))
254 {
255 char *p;
256 if (*buf == '#') continue;
257 if ((p = strchr(buf, '\r'))) *p = 0;
258 if ((p = strchr(buf, '\n'))) *p = 0;
259 if (!*buf) continue;
260 if (!(p = strchr((char *)buf, ':'))) continue;
261 *p++ = 0;
262 if (!strcmp(buf, "enable"))
263 {
264 cli_allow_enable(cli, p);
265 LOG(3, 0, 0, "Setting enable password\n");
266 }
267 else
268 {
269 cli_allow_user(cli, buf, p);
270 LOG(3, 0, 0, "Allowing user %s to connect to the CLI\n", buf);
271 }
272 }
273 fclose(f);
274 }
275 }
276
277 void cli_init_complete(char *hostname)
278 {
279 int on = 1;
280 struct sockaddr_in addr;
281
282 if (hostname && *hostname)
283 cli_set_hostname(cli, hostname);
284 else
285 cli_set_hostname(cli, "l2tpns");
286
287 memset(&addr, 0, sizeof(addr));
288 clifd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
289 setsockopt(clifd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
290 {
291 int flags;
292 // Set cli fd as non-blocking
293 flags = fcntl(clifd, F_GETFL, 0);
294 fcntl(clifd, F_SETFL, flags | O_NONBLOCK);
295 }
296 addr.sin_family = AF_INET;
297 addr.sin_addr.s_addr = config->cli_bind_address; /* defaults to INADDR_ANY */
298 addr.sin_port = htons(23);
299 if (bind(clifd, (void *) &addr, sizeof(addr)) < 0)
300 {
301 LOG(0, 0, 0, "Error binding cli on port 23: %s\n", strerror(errno));
302 close(clifd);
303 clifd = -1;
304 return;
305 }
306 if (listen(clifd, 10) < 0)
307 {
308 LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno));
309 close(clifd);
310 clifd = -1;
311 return;
312 }
313 }
314
315 void cli_do(int sockfd)
316 {
317 int require_auth = 1;
318 struct sockaddr_in addr;
319 socklen_t l = sizeof(addr);
320
321 if (fork_and_close()) return;
322 if (getpeername(sockfd, (struct sockaddr *) &addr, &l) == 0)
323 {
324 require_auth = addr.sin_addr.s_addr != inet_addr("127.0.0.1");
325 LOG(require_auth ? 3 : 4, 0, 0, "Accepted connection to CLI from %s\n",
326 fmtaddr(addr.sin_addr.s_addr, 0));
327 }
328 else
329 LOG(0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno));
330
331 if (require_auth)
332 {
333 LOG(3, 0, 0, "CLI is remote, requiring authentication\n");
334 if (!cli->users) /* paranoia */
335 {
336 LOG(0, 0, 0, "No users for remote authentication! Exiting CLI\n");
337 exit(0);
338 }
339 }
340 else
341 {
342 /* no username/pass required */
343 cli->users = 0;
344 }
345
346 #ifdef RINGBUFFER
347 debug_rb_tail = ringbuffer->tail;
348 #endif
349 memset(&debug_flags, 0, sizeof(debug_flags));
350 debug_flags.critical = 1;
351
352 cli_loop(cli, sockfd);
353
354 close(sockfd);
355 LOG(require_auth ? 3 : 4, 0, 0, "Closed CLI connection from %s\n",
356 fmtaddr(addr.sin_addr.s_addr, 0));
357
358 exit(0);
359 }
360
361 static void cli_print_log(struct cli_def *cli, const char *string)
362 {
363 LOG(3, 0, 0, "%s\n", string);
364 }
365
366 void cli_do_file(FILE *fh)
367 {
368 LOG(3, 0, 0, "Reading configuration file\n");
369 cli_print_callback(cli, cli_print_log);
370 cli_file(cli, fh, PRIVILEGE_PRIVILEGED, MODE_CONFIG);
371 cli_print_callback(cli, NULL);
372 }
373
374 int cli_arg_help(struct cli_def *cli, int cr_ok, char *entry, ...)
375 {
376 va_list ap;
377 char *desc;
378 char buf[16];
379 char *p;
380
381 va_start(ap, entry);
382 while (entry)
383 {
384 /* allow one %d */
385 if ((p = strchr(entry, '%')) && !strchr(p+1, '%') && p[1] == 'd')
386 {
387 int v = va_arg(ap, int);
388 snprintf(buf, sizeof(buf), entry, v);
389 p = buf;
390 }
391 else
392 p = entry;
393
394 desc = va_arg(ap, char *);
395 if (desc && *desc)
396 cli_error(cli, " %-20s %s", p, desc);
397 else
398 cli_error(cli, " %s", p);
399
400 entry = desc ? va_arg(ap, char *) : 0;
401 }
402
403 va_end(ap);
404 if (cr_ok)
405 cli_error(cli, " <cr>");
406
407 return CLI_OK;
408 }
409
410 static int cmd_show_session(struct cli_def *cli, const char *command, char **argv, int argc)
411 {
412 int i;
413
414 if (CLI_HELP_REQUESTED)
415 return cli_arg_help(cli, 1,
416 "<1-%d>", MAXSESSION-1, "Show specific session by id",
417 NULL);
418
419 time(&time_now);
420 if (argc > 0)
421 {
422 // Show individual session
423 for (i = 0; i < argc; i++)
424 {
425 unsigned int s, b_in, b_out;
426 s = atoi(argv[i]);
427 if (s <= 0 || s >= MAXSESSION)
428 {
429 cli_print(cli, "Invalid session id \"%s\"", argv[i]);
430 continue;
431 }
432 cli_print(cli, "\r\nSession %d:", s);
433 cli_print(cli, "\tUser:\t\t%s", session[s].user[0] ? session[s].user : "none");
434 cli_print(cli, "\tCalling Num:\t%s", session[s].calling);
435 cli_print(cli, "\tCalled Num:\t%s", session[s].called);
436 cli_print(cli, "\tTunnel ID:\t%d", session[s].tunnel);
437 cli_print(cli, "\tPPP Phase:\t%s", ppp_phase(session[s].ppp.phase));
438 switch (session[s].ppp.phase)
439 {
440 case Establish:
441 cli_print(cli, "\t LCP state:\t%s", ppp_state(session[s].ppp.lcp));
442 break;
443
444 case Authenticate:
445 case Network:
446 cli_print(cli, "\t IPCP state:\t%s", ppp_state(session[s].ppp.ipcp));
447 cli_print(cli, "\t IPV6CP state:\t%s", ppp_state(session[s].ppp.ipv6cp));
448 cli_print(cli, "\t CCP state:\t%s", ppp_state(session[s].ppp.ccp));
449 }
450 cli_print(cli, "\tIP address:\t%s", fmtaddr(htonl(session[s].ip), 0));
451 cli_print(cli, "\tUnique SID:\t%u", session[s].unique_id);
452 cli_print(cli, "\tOpened:\t\t%u seconds", session[s].opened ? abs(time_now - session[s].opened) : 0);
453 cli_print(cli, "\tIdle time:\t%u seconds", session[s].last_packet ? abs(time_now - session[s].last_packet) : 0);
454 if (session[s].session_timeout)
455 {
456 clockt opened = session[s].opened;
457 if (session[s].bundle && bundle[session[s].bundle].num_of_links > 1)
458 opened = bundle[session[s].bundle].online_time;
459
460 cli_print(cli, "\tSess Timeout:\t%u seconds", session[s].session_timeout - (opened ? abs(time_now - opened) : 0));
461 }
462
463 if (session[s].idle_timeout)
464 cli_print(cli, "\tIdle Timeout:\t%u seconds", session[s].idle_timeout - (session[s].last_data ? abs(time_now - session[s].last_data) : 0));
465
466 if (session[s].timeout)
467 {
468 cli_print(cli, "\tRemaining time:\t%u",
469 (session[s].bundle && bundle[session[s].bundle].num_of_links > 1)
470 ? (unsigned) (session[s].timeout - bundle[session[s].bundle].online_time)
471 : (unsigned) (session[s].timeout - (time_now - session[s].opened)));
472 }
473
474 cli_print(cli, "\tBytes In/Out:\t%u/%u", session[s].cout, session[s].cin);
475 cli_print(cli, "\tPkts In/Out:\t%u/%u", session[s].pout, session[s].pin);
476 cli_print(cli, "\tMRU:\t\t%d", session[s].mru);
477 cli_print(cli, "\tRx Speed:\t%u", session[s].rx_connect_speed);
478 cli_print(cli, "\tTx Speed:\t%u", session[s].tx_connect_speed);
479 if (session[s].filter_in && session[s].filter_in <= MAXFILTER)
480 cli_print(cli, "\tFilter in:\t%u (%s)", session[s].filter_in, ip_filters[session[s].filter_in - 1].name);
481 if (session[s].filter_out && session[s].filter_out <= MAXFILTER)
482 cli_print(cli, "\tFilter out:\t%u (%s)", session[s].filter_out, ip_filters[session[s].filter_out - 1].name);
483 if (session[s].snoop_ip && session[s].snoop_port)
484 cli_print(cli, "\tIntercepted:\t%s:%d", fmtaddr(session[s].snoop_ip, 0), session[s] .snoop_port);
485 else
486 cli_print(cli, "\tIntercepted:\tno");
487
488 cli_print(cli, "\tWalled Garden:\t%s", session[s].walled_garden ? "YES" : "no");
489 {
490 int t = (session[s].throttle_in || session[s].throttle_out);
491 cli_print(cli, "\tThrottled:\t%s%s%.0d%s%s%.0d%s%s",
492 t ? "YES" : "no", t ? " (" : "",
493 session[s].throttle_in, session[s].throttle_in ? "kbps" : t ? "-" : "",
494 t ? "/" : "",
495 session[s].throttle_out, session[s].throttle_out ? "kbps" : t ? "-" : "",
496 t ? ")" : "");
497 }
498
499 b_in = session[s].tbf_in;
500 b_out = session[s].tbf_out;
501 if (b_in || b_out)
502 cli_print(cli, "\t\t\t%5s %6s %6s | %7s %7s %8s %8s %8s %8s",
503 "Rate", "Credit", "Queued", "ByteIn", "PackIn",
504 "ByteSent", "PackSent", "PackDrop", "PackDelay");
505
506 if (b_in)
507 cli_print(cli, "\tTBFI#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
508 b_in,
509 (filter_list[b_in].next ? "*" : " "),
510 (b_in < 100 ? "\t" : ""),
511 filter_list[b_in].rate * 8,
512 filter_list[b_in].credit,
513 filter_list[b_in].queued,
514 filter_list[b_in].b_queued,
515 filter_list[b_in].p_queued,
516 filter_list[b_in].b_sent,
517 filter_list[b_in].p_sent,
518 filter_list[b_in].p_dropped,
519 filter_list[b_in].p_delayed);
520
521 if (b_out)
522 cli_print(cli, "\tTBFO#%d%1s%s\t%5d %6d %6d | %7d %7d %8d %8d %8d %8d",
523 b_out,
524 (filter_list[b_out].next ? "*" : " "),
525 (b_out < 100 ? "\t" : ""),
526 filter_list[b_out].rate * 8,
527 filter_list[b_out].credit,
528 filter_list[b_out].queued,
529 filter_list[b_out].b_queued,
530 filter_list[b_out].p_queued,
531 filter_list[b_out].b_sent,
532 filter_list[b_out].p_sent,
533 filter_list[b_out].p_dropped,
534 filter_list[b_out].p_delayed);
535
536 }
537 return CLI_OK;
538 }
539
540 // Show Summary
541 cli_print(cli, "%5s %7s %4s %-32s %-15s %s %s %s %s %10s %10s %10s %4s %10s %-18s %s",
542 "SID",
543 "LkToSID",
544 "TID",
545 "Username",
546 "IP",
547 "I",
548 "T",
549 "G",
550 "6",
551 "opened",
552 "downloaded",
553 "uploaded",
554 "idle",
555 "Rem.Time",
556 "LAC(L)/RLNS(R)/PPPOE(P)",
557 "CLI");
558
559 for (i = 1; i < MAXSESSION; i++)
560 {
561 uint32_t rem_time;
562 if (!session[i].opened) continue;
563 if (session[i].bundle && bundle[session[i].bundle].num_of_links > 1)
564 rem_time = session[i].timeout ? (session[i].timeout - bundle[session[i].bundle].online_time) : 0;
565 else
566 rem_time = session[i].timeout ? (session[i].timeout - (time_now-session[i].opened)) : 0;
567 cli_print(cli, "%5d %7d %4d %-32s %-15s %s %s %s %s %10u %10lu %10lu %4u %10lu %3s%-20s %s",
568 i,
569 session[i].forwardtosession,
570 session[i].tunnel,
571 session[i].user[0] ? session[i].user : "*",
572 fmtaddr(htonl(session[i].ip), 0),
573 (session[i].snoop_ip && session[i].snoop_port) ? "Y" : "N",
574 (session[i].throttle_in || session[i].throttle_out) ? "Y" : "N",
575 (session[i].walled_garden) ? "Y" : "N",
576 (session[i].ppp.ipv6cp == Opened) ? "Y" : "N",
577 abs(time_now - (unsigned long)session[i].opened),
578 (unsigned long)session[i].cout,
579 (unsigned long)session[i].cin,
580 abs(time_now - (session[i].last_packet ? session[i].last_packet : time_now)),
581 (unsigned long)(rem_time),
582 (session[i].tunnel == TUNNEL_ID_PPPOE)?"(P)":(tunnel[session[i].tunnel].isremotelns?"(R)":"(L)"),
583 (session[i].tunnel == TUNNEL_ID_PPPOE)?fmtMacAddr(session[i].src_hwaddr):fmtaddr(htonl(tunnel[session[i].tunnel].ip), 1),
584 session[i].calling[0] ? session[i].calling : "*");
585 }
586 return CLI_OK;
587 }
588
589 static int cmd_show_tunnels(struct cli_def *cli, const char *command, char **argv, int argc)
590 {
591 int i, x, show_all = 0;
592 char *states[] = {
593 "Free",
594 "Open",
595 "Closing",
596 "Opening",
597 };
598
599 if (CLI_HELP_REQUESTED)
600 {
601 if (argc > 1)
602 return cli_arg_help(cli, 1,
603 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
604 NULL);
605
606 return cli_arg_help(cli, 1,
607 "all", "Show all tunnels, including unused",
608 "<1-%d>", MAXTUNNEL-1, "Show specific tunnel by id",
609 NULL);
610 }
611
612 time(&time_now);
613 if (argc > 0)
614 {
615 if (strcmp(argv[0], "all") == 0)
616 {
617 show_all = 1;
618 }
619 else
620 {
621 // Show individual tunnel
622 for (i = 0; i < argc; i++)
623 {
624 char s[65535] = {0};
625 unsigned int t;
626 t = atoi(argv[i]);
627 if (t <= 0 || t >= MAXTUNNEL)
628 {
629 cli_print(cli, "Invalid tunnel id \"%s\"", argv[i]);
630 continue;
631 }
632 cli_print(cli, "\r\nTunnel %d:", t);
633 cli_print(cli, "\tState:\t\t%s", states[tunnel[t].state]);
634 cli_print(cli, "\tHostname:\t%s", tunnel[t].hostname[0] ? tunnel[t].hostname : "(none)");
635 cli_print(cli, "\tRemote IP:\t%s", fmtaddr(htonl(tunnel[t].ip), 0));
636 cli_print(cli, "\tRemote Port:\t%d", tunnel[t].port);
637 cli_print(cli, "\tRx Window:\t%u", tunnel[t].window);
638 cli_print(cli, "\tNext Recv:\t%u", tunnel[t].nr);
639 cli_print(cli, "\tNext Send:\t%u", tunnel[t].ns);
640 cli_print(cli, "\tQueue Len:\t%u", tunnel[t].controlc);
641 cli_print(cli, "\tLast Packet Age:%u", (unsigned)(time_now - tunnel[t].last));
642
643 for (x = 0; x < MAXSESSION; x++)
644 if (session[x].tunnel == t && session[x].opened && !session[x].die)
645 sprintf(s, "%s%u ", s, x);
646
647 cli_print(cli, "\tSessions:\t%s", s);
648 }
649 return CLI_OK;
650 }
651 }
652
653 // Show tunnel summary
654 cli_print(cli, "%4s %20s %20s %6s %s",
655 "TID",
656 "Hostname",
657 "IP",
658 "State",
659 "Sessions");
660
661 for (i = 1; i < MAXTUNNEL; i++)
662 {
663 int sessions = 0;
664 if (!show_all && (!tunnel[i].ip || tunnel[i].die)) continue;
665
666 for (x = 0; x < MAXSESSION; x++) if (session[x].tunnel == i && session[x].opened && !session[x].die) sessions++;
667 cli_print(cli, "%4d %20s %20s %6s %6d %s",
668 i,
669 *tunnel[i].hostname ? tunnel[i].hostname : "(null)",
670 fmtaddr(htonl(tunnel[i].ip), 0),
671 states[tunnel[i].state],
672 sessions
673 ,(i == TUNNEL_ID_PPPOE)?"Tunnel pppoe":(tunnel[i].isremotelns?"Tunnel To Remote LNS":"Tunnel To LAC")
674 );
675 }
676
677 return CLI_OK;
678 }
679
680 static int cmd_show_users(struct cli_def *cli, const char *command, char **argv, int argc)
681 {
682 char sid[32][8];
683 char *sargv[32];
684 int sargc = 0;
685 int i;
686
687 if (CLI_HELP_REQUESTED)
688 return cli_arg_help(cli, 1,
689 "USER", "Show details for specific username",
690 NULL);
691
692 for (i = 0; i < MAXSESSION; i++)
693 {
694 if (!session[i].opened) continue;
695 if (!session[i].user[0]) continue;
696 if (argc > 0)
697 {
698 int j;
699 for (j = 0; j < argc && sargc < 32; j++)
700 {
701 if (strcmp(argv[j], session[i].user) == 0)
702 {
703 snprintf(sid[sargc], sizeof(sid[0]), "%d", i);
704 sargv[sargc] = sid[sargc];
705 sargc++;
706 }
707 }
708
709 continue;
710 }
711
712 cli_print(cli, "%s", session[i].user);
713 }
714
715 if (sargc > 0)
716 return cmd_show_session(cli, "users", sargv, sargc);
717
718 return CLI_OK;
719 }
720
721 #ifdef STATISTICS
722 static int cmd_show_counters(struct cli_def *cli, const char *command, char **argv, int argc)
723 {
724 if (CLI_HELP_REQUESTED)
725 return CLI_HELP_NO_ARGS;
726
727 cli_print(cli, "%-10s %10s %10s %10s %10s", "Ethernet", "Bytes", "Packets", "Errors", "Dropped");
728 cli_print(cli, "%-10s %10u %10u %10u %10u", "RX",
729 GET_STAT(tun_rx_bytes),
730 GET_STAT(tun_rx_packets),
731 GET_STAT(tun_rx_errors),
732 GET_STAT(tun_rx_dropped));
733 cli_print(cli, "%-10s %10u %10u %10u", "TX",
734 GET_STAT(tun_tx_bytes),
735 GET_STAT(tun_tx_packets),
736 GET_STAT(tun_tx_errors));
737 cli_print(cli, " ");
738
739 cli_print(cli, "%-10s %10s %10s %10s %10s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
740 cli_print(cli, "%-10s %10u %10u %10u", "RX",
741 GET_STAT(tunnel_rx_bytes),
742 GET_STAT(tunnel_rx_packets),
743 GET_STAT(tunnel_rx_errors));
744 cli_print(cli, "%-10s %10u %10u %10u %10u", "TX",
745 GET_STAT(tunnel_tx_bytes),
746 GET_STAT(tunnel_tx_packets),
747 GET_STAT(tunnel_tx_errors),
748 GET_STAT(tunnel_retries));
749 cli_print(cli, " ");
750
751 cli_print(cli, "%-30s%-10s", "Counter", "Value");
752 cli_print(cli, "-----------------------------------------");
753 cli_print(cli, "%-30s%u", "radius_retries", GET_STAT(radius_retries));
754 cli_print(cli, "%-30s%u", "arp_sent", GET_STAT(arp_sent));
755 cli_print(cli, "%-30s%u", "packets_snooped", GET_STAT(packets_snooped));
756 cli_print(cli, "%-30s%u", "tunnel_created", GET_STAT(tunnel_created));
757 cli_print(cli, "%-30s%u", "session_created", GET_STAT(session_created));
758 cli_print(cli, "%-30s%u", "tunnel_timeout", GET_STAT(tunnel_timeout));
759 cli_print(cli, "%-30s%u", "session_timeout", GET_STAT(session_timeout));
760 cli_print(cli, "%-30s%u", "radius_timeout", GET_STAT(radius_timeout));
761 cli_print(cli, "%-30s%u", "radius_overflow", GET_STAT(radius_overflow));
762 cli_print(cli, "%-30s%u", "tunnel_overflow", GET_STAT(tunnel_overflow));
763 cli_print(cli, "%-30s%u", "session_overflow", GET_STAT(session_overflow));
764 cli_print(cli, "%-30s%u", "ip_allocated", GET_STAT(ip_allocated));
765 cli_print(cli, "%-30s%u", "ip_freed", GET_STAT(ip_freed));
766 cli_print(cli, "%-30s%u", "cluster_forwarded", GET_STAT(c_forwarded));
767 cli_print(cli, "%-30s%u", "recv_forward", GET_STAT(recv_forward));
768 cli_print(cli, "%-30s%u", "select_called", GET_STAT(select_called));
769 cli_print(cli, "%-30s%u", "multi_read_used", GET_STAT(multi_read_used));
770 cli_print(cli, "%-30s%u", "multi_read_exceeded", GET_STAT(multi_read_exceeded));
771
772
773 #ifdef STAT_CALLS
774 cli_print(cli, "\n%-30s%-10s", "Counter", "Value");
775 cli_print(cli, "-----------------------------------------");
776 cli_print(cli, "%-30s%u", "call_processtun", GET_STAT(call_processtun));
777 cli_print(cli, "%-30s%u", "call_processipout", GET_STAT(call_processipout));
778 cli_print(cli, "%-30s%u", "call_processipv6out", GET_STAT(call_processipv6out));
779 cli_print(cli, "%-30s%u", "call_processudp", GET_STAT(call_processudp));
780 cli_print(cli, "%-30s%u", "call_processpap", GET_STAT(call_processpap));
781 cli_print(cli, "%-30s%u", "call_processchap", GET_STAT(call_processchap));
782 cli_print(cli, "%-30s%u", "call_processlcp", GET_STAT(call_processlcp));
783 cli_print(cli, "%-30s%u", "call_processipcp", GET_STAT(call_processipcp));
784 cli_print(cli, "%-30s%u", "call_processipv6cp", GET_STAT(call_processipv6cp));
785 cli_print(cli, "%-30s%u", "call_processipin", GET_STAT(call_processipin));
786 cli_print(cli, "%-30s%u", "call_processipv6in", GET_STAT(call_processipv6in));
787 cli_print(cli, "%-30s%u", "call_processccp", GET_STAT(call_processccp));
788 cli_print(cli, "%-30s%u", "call_processrad", GET_STAT(call_processrad));
789 cli_print(cli, "%-30s%u", "call_sendarp", GET_STAT(call_sendarp));
790 cli_print(cli, "%-30s%u", "call_sendipcp", GET_STAT(call_sendipcp));
791 cli_print(cli, "%-30s%u", "call_sendchap", GET_STAT(call_sendchap));
792 cli_print(cli, "%-30s%u", "call_sessionbyip", GET_STAT(call_sessionbyip));
793 cli_print(cli, "%-30s%u", "call_sessionbyipv6", GET_STAT(call_sessionbyipv6));
794 cli_print(cli, "%-30s%u", "call_sessionbyuser", GET_STAT(call_sessionbyuser));
795 cli_print(cli, "%-30s%u", "call_tunnelsend", GET_STAT(call_tunnelsend));
796 cli_print(cli, "%-30s%u", "call_tunnelkill", GET_STAT(call_tunnelkill));
797 cli_print(cli, "%-30s%u", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown));
798 cli_print(cli, "%-30s%u", "call_sessionkill", GET_STAT(call_sessionkill));
799 cli_print(cli, "%-30s%u", "call_sessionshutdown", GET_STAT(call_sessionshutdown));
800 cli_print(cli, "%-30s%u", "call_sessionsetup", GET_STAT(call_sessionsetup));
801 cli_print(cli, "%-30s%u", "call_assign_ip_address", GET_STAT(call_assign_ip_address));
802 cli_print(cli, "%-30s%u", "call_free_ip_address", GET_STAT(call_free_ip_address));
803 cli_print(cli, "%-30s%u", "call_dump_acct_info", GET_STAT(call_dump_acct_info));
804 cli_print(cli, "%-30s%u", "call_radiussend", GET_STAT(call_radiussend));
805 cli_print(cli, "%-30s%u", "call_radiusretry", GET_STAT(call_radiusretry));
806 cli_print(cli, "%-30s%u", "call_random_data", GET_STAT(call_random_data));
807 #endif /* STAT_CALLS */
808
809 {
810 time_t l = GET_STAT(last_reset);
811 char *t = ctime(&l);
812 char *p = strchr(t, '\n');
813 if (p) *p = 0;
814
815 cli_print(cli, " ");
816 cli_print(cli, "Last counter reset %s", t);
817 }
818
819 return CLI_OK;
820 }
821
822 static int cmd_clear_counters(struct cli_def *cli, const char *command, char **argv, int argc)
823 {
824 if (CLI_HELP_REQUESTED)
825 return CLI_HELP_NO_ARGS;
826
827 memset(_statistics, 0, sizeof(struct Tstats));
828 SET_STAT(last_reset, time(NULL));
829
830 cli_print(cli, "Counters cleared");
831 return CLI_OK;
832 }
833 #endif /* STATISTICS */
834
835 static int cmd_show_version(struct cli_def *cli, const char *command, char **argv, int argc)
836 {
837 if (CLI_HELP_REQUESTED)
838 return CLI_HELP_NO_ARGS;
839
840 cli_print(cli, "L2TPNS %s", VERSION);
841 return CLI_OK;
842 }
843
844 static int cmd_show_pool(struct cli_def *cli, const char *command, char **argv, int argc)
845 {
846 int i;
847 int used = 0, free = 0, show_all = 0;
848
849 if (!config->cluster_iam_master)
850 {
851 cli_print(cli, "Can't do this on a slave. Do it on %s",
852 fmtaddr(config->cluster_master_address, 0));
853
854 return CLI_OK;
855 }
856
857 if (CLI_HELP_REQUESTED)
858 {
859 if (argc > 1)
860 return cli_arg_help(cli, 1, NULL);
861
862 return cli_arg_help(cli, 1,
863 "all", "Show all pool addresses, including unused",
864 NULL);
865 }
866
867 if (argc > 0 && strcmp(argv[0], "all") == 0)
868 show_all = 1;
869
870 time(&time_now);
871 cli_print(cli, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
872 for (i = 0; i < MAXIPPOOL; i++)
873 {
874 if (!ip_address_pool[i].address) continue;
875 if (ip_address_pool[i].assigned)
876 {
877 cli_print(cli, "%-15s\tY %8d %s",
878 fmtaddr(htonl(ip_address_pool[i].address), 0),
879 ip_address_pool[i].session,
880 session[ip_address_pool[i].session].user);
881
882 used++;
883 }
884 else
885 {
886 if (ip_address_pool[i].last)
887 cli_print(cli, "%-15s\tN %8s [%s] %ds",
888 fmtaddr(htonl(ip_address_pool[i].address), 0), "",
889 ip_address_pool[i].user, (int) time_now - ip_address_pool[i].last);
890
891 else if (show_all)
892 cli_print(cli, "%-15s\tN", fmtaddr(htonl(ip_address_pool[i].address), 0));
893
894 free++;
895 }
896 }
897
898 if (!show_all)
899 cli_print(cli, "(Not displaying unused addresses)");
900
901 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
902 return CLI_OK;
903 }
904
905 static FILE *save_config_fh = 0;
906 static void print_save_config(struct cli_def *cli, const char *string)
907 {
908 if (save_config_fh)
909 fprintf(save_config_fh, "%s\n", string);
910 }
911
912 static int cmd_write_memory(struct cli_def *cli, const char *command, char **argv, int argc)
913 {
914 if (CLI_HELP_REQUESTED)
915 return CLI_HELP_NO_ARGS;
916
917 if ((save_config_fh = fopen(config->config_file, "w")))
918 {
919 cli_print(cli, "Writing configuration");
920 cli_print_callback(cli, print_save_config);
921 cmd_show_run(cli, command, argv, argc);
922 cli_print_callback(cli, NULL);
923 fclose(save_config_fh);
924 save_config_fh = 0;
925 }
926 else
927 {
928 cli_error(cli, "Error writing configuration: %s", strerror(errno));
929 }
930 return CLI_OK;
931 }
932
933 static char const *show_access_list_rule(int extended, ip_filter_rulet *rule);
934
935 static int cmd_show_run(struct cli_def *cli, const char *command, char **argv, int argc)
936 {
937 int i;
938 char ipv6addr[INET6_ADDRSTRLEN];
939
940 if (CLI_HELP_REQUESTED)
941 return CLI_HELP_NO_ARGS;
942
943 cli_print(cli, "# Current configuration:");
944
945 for (i = 0; config_values[i].key; i++)
946 {
947 void *value = ((void *)config) + config_values[i].offset;
948 if (config_values[i].type == STRING)
949 cli_print(cli, "set %s \"%.*s\"", config_values[i].key, config_values[i].size, (char *) value);
950 else if (config_values[i].type == IPv4)
951 cli_print(cli, "set %s %s", config_values[i].key, fmtaddr(*(in_addr_t *) value, 0));
952 else if (config_values[i].type == IPv6)
953 cli_print(cli, "set %s %s", config_values[i].key, inet_ntop(AF_INET6, value, ipv6addr, INET6_ADDRSTRLEN));
954 else if (config_values[i].type == SHORT)
955 cli_print(cli, "set %s %hu", config_values[i].key, *(short *) value);
956 else if (config_values[i].type == BOOL)
957 cli_print(cli, "set %s %s", config_values[i].key, (*(int *) value) ? "yes" : "no");
958 else if (config_values[i].type == INT)
959 cli_print(cli, "set %s %d", config_values[i].key, *(int *) value);
960 else if (config_values[i].type == UNSIGNED_LONG)
961 cli_print(cli, "set %s %lu", config_values[i].key, *(unsigned long *) value);
962 }
963
964 cli_print(cli, "# Plugins");
965 for (i = 0; i < MAXPLUGINS; i++)
966 {
967 if (*config->plugins[i])
968 {
969 cli_print(cli, "load plugin \"%s\"", config->plugins[i]);
970 }
971 }
972
973 #ifdef BGP
974 if (config->as_number)
975 {
976 int k;
977 int h;
978
979 cli_print(cli, "# BGP");
980 cli_print(cli, "router bgp %u", config->as_number);
981 for (i = 0; i < BGP_NUM_PEERS; i++)
982 {
983 if (!config->neighbour[i].name[0])
984 continue;
985
986 cli_print(cli, " neighbour %s remote-as %u", config->neighbour[i].name, config->neighbour[i].as);
987
988 k = config->neighbour[i].keepalive;
989 h = config->neighbour[i].hold;
990
991 if (k == -1)
992 {
993 if (h == -1)
994 continue;
995
996 k = BGP_KEEPALIVE_TIME;
997 }
998
999 if (h == -1)
1000 h = BGP_HOLD_TIME;
1001
1002 cli_print(cli, " neighbour %s timers %d %d", config->neighbour[i].name, k, h);
1003
1004 if (config->neighbour[i].update_source.s_addr != INADDR_ANY)
1005 cli_print(cli, " neighbour %s update-source %s",
1006 config->neighbour[i].name,
1007 inet_ntoa(config->neighbour[i].update_source));
1008 }
1009 }
1010 #endif
1011
1012 cli_print(cli, "# Filters");
1013 for (i = 0; i < MAXFILTER; i++)
1014 {
1015 ip_filter_rulet *rules;
1016 if (!*ip_filters[i].name)
1017 continue;
1018
1019 cli_print(cli, "ip access-list %s %s",
1020 ip_filters[i].extended ? "extended" : "standard",
1021 ip_filters[i].name);
1022
1023 rules = ip_filters[i].rules;
1024 while (rules->action)
1025 cli_print(cli, "%s", show_access_list_rule(ip_filters[i].extended, rules++));
1026 }
1027
1028 cli_print(cli, "# end");
1029 return CLI_OK;
1030 }
1031
1032 static int cmd_show_radius(struct cli_def *cli, const char *command, char **argv, int argc)
1033 {
1034 int i, free = 0, used = 0, show_all = 0;
1035 char *states[] = {
1036 "NULL",
1037 "CHAP",
1038 "AUTH",
1039 "IPCP",
1040 "START",
1041 "STOP",
1042 "INTRM",
1043 "WAIT",
1044 };
1045
1046 if (CLI_HELP_REQUESTED)
1047 {
1048 if (argc > 1)
1049 return cli_arg_help(cli, 1, NULL);
1050
1051 return cli_arg_help(cli, 1,
1052 "all", "Show all RADIUS sessions, including unused",
1053 NULL);
1054 }
1055
1056 cli_print(cli, "%6s%7s%5s%6s%9s%9s%4s", "ID", "Radius", "Sock", "State", "Session", "Retry", "Try");
1057
1058 time(&time_now);
1059
1060 if (argc > 0 && strcmp(argv[0], "all") == 0)
1061 show_all = 1;
1062
1063 for (i = 1; i < MAXRADIUS; i++)
1064 {
1065 if (radius[i].state == RADIUSNULL)
1066 free++;
1067 else
1068 used++;
1069
1070 if (!show_all && radius[i].state == RADIUSNULL) continue;
1071
1072 cli_print(cli, "%6d%7d%5d%6s%9d%9u%4d",
1073 i,
1074 i >> RADIUS_SHIFT,
1075 i & RADIUS_MASK,
1076 states[radius[i].state],
1077 radius[i].session,
1078 radius[i].retry,
1079 radius[i].try);
1080 }
1081
1082 cli_print(cli, "\r\nFree: %d\r\nUsed: %d", free, used);
1083
1084 return CLI_OK;
1085 }
1086
1087 static int cmd_show_plugins(struct cli_def *cli, const char *command, char **argv, int argc)
1088 {
1089 int i;
1090
1091 if (CLI_HELP_REQUESTED)
1092 return CLI_HELP_NO_ARGS;
1093
1094 cli_print(cli, "Plugins currently loaded:");
1095 for (i = 0; i < MAXPLUGINS; i++)
1096 if (*config->plugins[i])
1097 cli_print(cli, " %s", config->plugins[i]);
1098
1099 return CLI_OK;
1100 }
1101
1102 static int cmd_show_throttle(struct cli_def *cli, const char *command, char **argv, int argc)
1103 {
1104 int i;
1105
1106 if (CLI_HELP_REQUESTED)
1107 return CLI_HELP_NO_ARGS;
1108
1109 cli_print(cli, "%5s %4s %-32s %7s %6s %6s %6s",
1110 "SID",
1111 "TID",
1112 "Username",
1113 "Rate In",
1114 "Out",
1115 "TBFI",
1116 "TBFO");
1117
1118 for (i = 0; i < MAXSESSION; i++)
1119 {
1120 if (session[i].throttle_in || session[i].throttle_out)
1121 cli_print(cli, "%5d %4d %-32s %6d %6d %6d %6d",
1122 i,
1123 session[i].tunnel,
1124 session[i].user,
1125 session[i].throttle_in,
1126 session[i].throttle_out,
1127 session[i].tbf_in,
1128 session[i].tbf_out);
1129 }
1130
1131 return CLI_OK;
1132 }
1133
1134 static int cmd_show_banana(struct cli_def *cli, const char *command, char **argv, int argc)
1135 {
1136 if (CLI_HELP_REQUESTED)
1137 return CLI_HELP_NO_ARGS;
1138
1139 cli_print(cli, " _\n"
1140 "//\\\n"
1141 "V \\\n"
1142 " \\ \\_\n"
1143 " \\,'.`-.\n"
1144 " |\\ `. `.\n"
1145 " ( \\ `. `-. _,.-:\\\n"
1146 " \\ \\ `. `-._ __..--' ,-';/\n"
1147 " \\ `. `-. `-..___..---' _.--' ,'/\n"
1148 " `. `. `-._ __..--' ,' /\n"
1149 " `. `-_ ``--..'' _.-' ,'\n"
1150 " `-_ `-.___ __,--' ,'\n"
1151 " `-.__ `----\"\"\" __.-'\n"
1152 "hh `--..____..--'");
1153
1154 return CLI_OK;
1155 }
1156
1157 static int cmd_drop_user(struct cli_def *cli, const char *command, char **argv, int argc)
1158 {
1159 int i;
1160 sessionidt s;
1161
1162 if (CLI_HELP_REQUESTED)
1163 return cli_arg_help(cli, argc > 1,
1164 "USER", "Username of session to drop", NULL);
1165
1166 if (!config->cluster_iam_master)
1167 {
1168 cli_error(cli, "Can't do this on a slave. Do it on %s",
1169 fmtaddr(config->cluster_master_address, 0));
1170
1171 return CLI_OK;
1172 }
1173
1174 if (!argc)
1175 {
1176 cli_error(cli, "Specify a user to drop");
1177 return CLI_OK;
1178 }
1179
1180 for (i = 0; i < argc; i++)
1181 {
1182 if (!(s = sessionbyuser(argv[i])))
1183 {
1184 cli_error(cli, "User %s is not connected", argv[i]);
1185 continue;
1186 }
1187
1188 if (session[s].ip && session[s].opened && !session[s].die)
1189 {
1190 cli_print(cli, "Dropping user %s", session[s].user);
1191 cli_session_actions[s].action |= CLI_SESS_KILL;
1192 }
1193 }
1194
1195 return CLI_OK;
1196 }
1197
1198 static int cmd_drop_tunnel(struct cli_def *cli, const char *command, char **argv, int argc)
1199 {
1200 int i;
1201 tunnelidt t;
1202
1203 if (CLI_HELP_REQUESTED)
1204 return cli_arg_help(cli, argc > 1,
1205 "<1-%d>", MAXTUNNEL-1, "Tunnel id to drop", NULL);
1206
1207 if (!config->cluster_iam_master)
1208 {
1209 cli_error(cli, "Can't do this on a slave. Do it on %s",
1210 fmtaddr(config->cluster_master_address, 0));
1211
1212 return CLI_OK;
1213 }
1214
1215 if (!argc)
1216 {
1217 cli_error(cli, "Specify a tunnel to drop");
1218 return CLI_OK;
1219 }
1220
1221 for (i = 0; i < argc; i++)
1222 {
1223 if ((t = atol(argv[i])) <= 0 || (t >= MAXTUNNEL))
1224 {
1225 cli_error(cli, "Invalid tunnel ID (1-%d)", MAXTUNNEL-1);
1226 continue;
1227 }
1228
1229 if (!tunnel[t].ip)
1230 {
1231 cli_error(cli, "Tunnel %d is not connected", t);
1232 continue;
1233 }
1234
1235 if (tunnel[t].die)
1236 {
1237 cli_error(cli, "Tunnel %d is already being shut down", t);
1238 continue;
1239 }
1240
1241 cli_print(cli, "Tunnel %d shut down (%s)", t, tunnel[t].hostname);
1242 cli_tunnel_actions[t].action |= CLI_TUN_KILL;
1243 }
1244
1245 return CLI_OK;
1246 }
1247
1248 static int cmd_drop_session(struct cli_def *cli, const char *command, char **argv, int argc)
1249 {
1250 int i;
1251 sessionidt s;
1252
1253 if (CLI_HELP_REQUESTED)
1254 return cli_arg_help(cli, argc > 1,
1255 "<1-%d>", MAXSESSION-1, "Session id to drop", NULL);
1256
1257 if (!config->cluster_iam_master)
1258 {
1259 cli_error(cli, "Can't do this on a slave. Do it on %s",
1260 fmtaddr(config->cluster_master_address, 0));
1261
1262 return CLI_OK;
1263 }
1264
1265 if (!argc)
1266 {
1267 cli_error(cli, "Specify a session id to drop");
1268 return CLI_OK;
1269 }
1270
1271 for (i = 0; i < argc; i++)
1272 {
1273 if ((s = atol(argv[i])) <= 0 || (s > MAXSESSION))
1274 {
1275 cli_error(cli, "Invalid session ID (1-%d)", MAXSESSION-1);
1276 continue;
1277 }
1278
1279 if (session[s].ip && session[s].opened && !session[s].die)
1280 {
1281 cli_print(cli, "Dropping session %d", s);
1282 cli_session_actions[s].action |= CLI_SESS_KILL;
1283 }
1284 else if (session[s].forwardtosession && session[s].opened && !session[s].die)
1285 {
1286 cli_print(cli, "Dropping session %d", s);
1287 cli_session_actions[s].action |= CLI_SESS_KILL;
1288 }
1289 else
1290 {
1291 cli_error(cli, "Session %d is not active.", s);
1292 }
1293 }
1294
1295 return CLI_OK;
1296 }
1297
1298 static int cmd_snoop(struct cli_def *cli, const char *command, char **argv, int argc)
1299 {
1300 in_addr_t ip;
1301 uint16_t port;
1302 sessionidt s;
1303
1304 if (CLI_HELP_REQUESTED)
1305 {
1306 switch (argc)
1307 {
1308 case 1:
1309 return cli_arg_help(cli, 0,
1310 "USER", "Username of session to snoop", NULL);
1311
1312 case 2:
1313 return cli_arg_help(cli, 0,
1314 "A.B.C.D", "IP address of snoop destination", NULL);
1315
1316 case 3:
1317 return cli_arg_help(cli, 0,
1318 "N", "Port of snoop destination", NULL);
1319
1320 case 4:
1321 if (!argv[3][1])
1322 return cli_arg_help(cli, 1, NULL);
1323
1324 default:
1325 return CLI_OK;
1326 }
1327 }
1328
1329 if (!config->cluster_iam_master)
1330 {
1331 cli_error(cli, "Can't do this on a slave. Do it on %s",
1332 fmtaddr(config->cluster_master_address, 0));
1333
1334 return CLI_OK;
1335 }
1336
1337 if (argc < 3)
1338 {
1339 cli_error(cli, "Specify username, ip and port");
1340 return CLI_OK;
1341 }
1342
1343 if (!(s = sessionbyuser(argv[0])))
1344 {
1345 cli_error(cli, "User %s is not connected", argv[0]);
1346 return CLI_OK;
1347 }
1348
1349 ip = inet_addr(argv[1]);
1350 if (!ip || ip == INADDR_NONE)
1351 {
1352 cli_error(cli, "Cannot parse IP \"%s\"", argv[1]);
1353 return CLI_OK;
1354 }
1355
1356 port = atoi(argv[2]);
1357 if (!port)
1358 {
1359 cli_error(cli, "Invalid port %s", argv[2]);
1360 return CLI_OK;
1361 }
1362
1363 cli_print(cli, "Snooping user %s to %s:%d", argv[0], fmtaddr(ip, 0), port);
1364 cli_session_actions[s].snoop_ip = ip;
1365 cli_session_actions[s].snoop_port = port;
1366 cli_session_actions[s].action |= CLI_SESS_SNOOP;
1367
1368 return CLI_OK;
1369 }
1370
1371 static int cmd_no_snoop(struct cli_def *cli, const char *command, char **argv, int argc)
1372 {
1373 int i;
1374 sessionidt s;
1375
1376 if (CLI_HELP_REQUESTED)
1377 return cli_arg_help(cli, argc > 1,
1378 "USER", "Username of session to unsnoop", NULL);
1379
1380 if (!config->cluster_iam_master)
1381 {
1382 cli_error(cli, "Can't do this on a slave. Do it on %s",
1383 fmtaddr(config->cluster_master_address, 0));
1384
1385 return CLI_OK;
1386 }
1387
1388 if (!argc)
1389 {
1390 cli_error(cli, "Specify a user to unsnoop");
1391 return CLI_OK;
1392 }
1393
1394 for (i = 0; i < argc; i++)
1395 {
1396 if (!(s = sessionbyuser(argv[i])))
1397 {
1398 cli_error(cli, "User %s is not connected", argv[i]);
1399 continue;
1400 }
1401
1402 cli_print(cli, "Not snooping user %s", argv[i]);
1403 cli_session_actions[s].action |= CLI_SESS_NOSNOOP;
1404 }
1405
1406 return CLI_OK;
1407 }
1408
1409 static int cmd_throttle(struct cli_def *cli, const char *command, char **argv, int argc)
1410 {
1411 int rate_in = 0;
1412 int rate_out = 0;
1413 sessionidt s;
1414
1415 /*
1416 throttle USER - throttle in/out to default rate
1417 throttle USER RATE - throttle in/out to default rate
1418 throttle USER in RATE - throttle input only
1419 throttle USER out RATE - throttle output only
1420 throttle USER in RATE out RATE - throttle both
1421 */
1422
1423 if (CLI_HELP_REQUESTED)
1424 {
1425 switch (argc)
1426 {
1427 case 1:
1428 return cli_arg_help(cli, 0,
1429 "USER", "Username of session to throttle", NULL);
1430
1431 case 2:
1432 return cli_arg_help(cli, 1,
1433 "RATE", "Rate in kbps (in and out)",
1434 "in", "Select incoming rate",
1435 "out", "Select outgoing rate", NULL);
1436
1437 case 4:
1438 return cli_arg_help(cli, 1,
1439 "in", "Select incoming rate",
1440 "out", "Select outgoing rate", NULL);
1441
1442 case 3:
1443 if (isdigit(argv[1][0]))
1444 return cli_arg_help(cli, 1, NULL);
1445
1446 case 5:
1447 return cli_arg_help(cli, 0, "RATE", "Rate in kbps", NULL);
1448
1449 default:
1450 return cli_arg_help(cli, argc > 1, NULL);
1451 }
1452 }
1453
1454 if (!config->cluster_iam_master)
1455 {
1456 cli_error(cli, "Can't do this on a slave. Do it on %s",
1457 fmtaddr(config->cluster_master_address, 0));
1458
1459 return CLI_OK;
1460 }
1461
1462 if (argc == 0)
1463 {
1464 cli_error(cli, "Specify a user to throttle");
1465 return CLI_OK;
1466 }
1467
1468 if (!(s = sessionbyuser(argv[0])))
1469 {
1470 cli_error(cli, "User %s is not connected", argv[0]);
1471 return CLI_OK;
1472 }
1473
1474 if (argc == 1)
1475 {
1476 rate_in = rate_out = config->rl_rate;
1477 }
1478 else if (argc == 2)
1479 {
1480 rate_in = rate_out = atoi(argv[1]);
1481 if (rate_in < 1)
1482 {
1483 cli_error(cli, "Invalid rate \"%s\"", argv[1]);
1484 return CLI_OK;
1485 }
1486 }
1487 else if (argc == 3 || argc == 5)
1488 {
1489 int i;
1490 for (i = 1; i < argc - 1; i += 2)
1491 {
1492 int r = 0;
1493 if (MATCH("in", argv[i]))
1494 r = rate_in = atoi(argv[i+1]);
1495 else if (MATCH("out", argv[i]))
1496 r = rate_out = atoi(argv[i+1]);
1497
1498 if (r < 1)
1499 {
1500 cli_error(cli, "Invalid rate specification \"%s %s\"", argv[i], argv[i+1]);
1501 return CLI_OK;
1502 }
1503 }
1504 }
1505 else
1506 {
1507 cli_error(cli, "Invalid arguments");
1508 return CLI_OK;
1509 }
1510
1511 if ((rate_in && session[s].throttle_in) || (rate_out && session[s].throttle_out))
1512 {
1513 cli_error(cli, "User %s already throttled, unthrottle first", argv[0]);
1514 return CLI_OK;
1515 }
1516
1517 cli_session_actions[s].throttle_in = cli_session_actions[s].throttle_out = -1;
1518 if (rate_in && session[s].throttle_in != rate_in)
1519 cli_session_actions[s].throttle_in = rate_in;
1520
1521 if (rate_out && session[s].throttle_out != rate_out)
1522 cli_session_actions[s].throttle_out = rate_out;
1523
1524 if (cli_session_actions[s].throttle_in == -1 &&
1525 cli_session_actions[s].throttle_out == -1)
1526 {
1527 cli_error(cli, "User %s already throttled at this rate", argv[0]);
1528 return CLI_OK;
1529 }
1530
1531 cli_print(cli, "Throttling user %s", argv[0]);
1532 cli_session_actions[s].action |= CLI_SESS_THROTTLE;
1533
1534 return CLI_OK;
1535 }
1536
1537 static int cmd_no_throttle(struct cli_def *cli, const char *command, char **argv, int argc)
1538 {
1539 int i;
1540 sessionidt s;
1541
1542 if (CLI_HELP_REQUESTED)
1543 return cli_arg_help(cli, argc > 1,
1544 "USER", "Username of session to unthrottle", NULL);
1545
1546 if (!config->cluster_iam_master)
1547 {
1548 cli_error(cli, "Can't do this on a slave. Do it on %s",
1549 fmtaddr(config->cluster_master_address, 0));
1550
1551 return CLI_OK;
1552 }
1553
1554 if (!argc)
1555 {
1556 cli_error(cli, "Specify a user to unthrottle");
1557 return CLI_OK;
1558 }
1559
1560 for (i = 0; i < argc; i++)
1561 {
1562 if (!(s = sessionbyuser(argv[i])))
1563 {
1564 cli_error(cli, "User %s is not connected", argv[i]);
1565 continue;
1566 }
1567
1568 if (session[s].throttle_in || session[s].throttle_out)
1569 {
1570 cli_print(cli, "Unthrottling user %s", argv[i]);
1571 cli_session_actions[s].action |= CLI_SESS_NOTHROTTLE;
1572 }
1573 else
1574 {
1575 cli_error(cli, "User %s not throttled", argv[i]);
1576 }
1577 }
1578
1579 return CLI_OK;
1580 }
1581
1582 static int cmd_debug(struct cli_def *cli, const char *command, char **argv, int argc)
1583 {
1584 int i;
1585
1586 if (CLI_HELP_REQUESTED)
1587 return cli_arg_help(cli, 1,
1588 "all", "Enable debugging for all except \"data\"",
1589 "critical", "", // FIXME: add descriptions
1590 "error", "",
1591 "warning", "",
1592 "info", "",
1593 "calls", "",
1594 "data", "",
1595 NULL);
1596
1597 if (!argc)
1598 {
1599 char *p = (char *) &debug_flags;
1600 for (i = 0; i < sizeof(debug_flags); i++)
1601 {
1602 if (p[i])
1603 {
1604 cli_print(cli, "Currently debugging:%s%s%s%s%s%s",
1605 (debug_flags.critical) ? " critical" : "",
1606 (debug_flags.error) ? " error" : "",
1607 (debug_flags.warning) ? " warning" : "",
1608 (debug_flags.info) ? " info" : "",
1609 (debug_flags.calls) ? " calls" : "",
1610 (debug_flags.data) ? " data" : "");
1611
1612 return CLI_OK;
1613 }
1614 }
1615
1616 cli_print(cli, "Debugging off");
1617 return CLI_OK;
1618 }
1619
1620 for (i = 0; i < argc; i++)
1621 {
1622 int len = strlen(argv[i]);
1623
1624 if (argv[i][0] == 'c' && len < 2)
1625 len = 2; /* distinguish [cr]itical from [ca]lls */
1626
1627 if (!strncmp("critical", argv[i], len)) { debug_flags.critical = 1; continue; }
1628 if (!strncmp("error", argv[i], len)) { debug_flags.error = 1; continue; }
1629 if (!strncmp("warning", argv[i], len)) { debug_flags.warning = 1; continue; }
1630 if (!strncmp("info", argv[i], len)) { debug_flags.info = 1; continue; }
1631 if (!strncmp("calls", argv[i], len)) { debug_flags.calls = 1; continue; }
1632 if (!strncmp("data", argv[i], len)) { debug_flags.data = 1; continue; }
1633 if (!strncmp("all", argv[i], len))
1634 {
1635 memset(&debug_flags, 1, sizeof(debug_flags));
1636 debug_flags.data = 0;
1637 continue;
1638 }
1639
1640 cli_error(cli, "Invalid debugging flag \"%s\"", argv[i]);
1641 }
1642
1643 return CLI_OK;
1644 }
1645
1646 static int cmd_no_debug(struct cli_def *cli, const char *command, char **argv, int argc)
1647 {
1648 int i;
1649
1650 if (CLI_HELP_REQUESTED)
1651 return cli_arg_help(cli, 1,
1652 "all", "Disable all debugging",
1653 "critical", "", // FIXME: add descriptions
1654 "error", "",
1655 "warning", "",
1656 "info", "",
1657 "calls", "",
1658 "data", "",
1659 NULL);
1660
1661 if (!argc)
1662 {
1663 memset(&debug_flags, 0, sizeof(debug_flags));
1664 return CLI_OK;
1665 }
1666
1667 for (i = 0; i < argc; i++)
1668 {
1669 int len = strlen(argv[i]);
1670
1671 if (argv[i][0] == 'c' && len < 2)
1672 len = 2; /* distinguish [cr]itical from [ca]lls */
1673
1674 if (!strncmp("critical", argv[i], len)) { debug_flags.critical = 0; continue; }
1675 if (!strncmp("error", argv[i], len)) { debug_flags.error = 0; continue; }
1676 if (!strncmp("warning", argv[i], len)) { debug_flags.warning = 0; continue; }
1677 if (!strncmp("info", argv[i], len)) { debug_flags.info = 0; continue; }
1678 if (!strncmp("calls", argv[i], len)) { debug_flags.calls = 0; continue; }
1679 if (!strncmp("data", argv[i], len)) { debug_flags.data = 0; continue; }
1680 if (!strncmp("all", argv[i], len))
1681 {
1682 memset(&debug_flags, 0, sizeof(debug_flags));
1683 continue;
1684 }
1685
1686 cli_error(cli, "Invalid debugging flag \"%s\"", argv[i]);
1687 }
1688
1689 return CLI_OK;
1690 }
1691
1692 static int cmd_load_plugin(struct cli_def *cli, const char *command, char **argv, int argc)
1693 {
1694 int i, firstfree = 0;
1695
1696 if (CLI_HELP_REQUESTED)
1697 return cli_arg_help(cli, argc > 1,
1698 "PLUGIN", "Name of plugin to load", NULL);
1699
1700 if (argc != 1)
1701 {
1702 cli_error(cli, "Specify a plugin to load");
1703 return CLI_OK;
1704 }
1705
1706 for (i = 0; i < MAXPLUGINS; i++)
1707 {
1708 if (!*config->plugins[i] && !firstfree)
1709 firstfree = i;
1710 if (strcmp(config->plugins[i], argv[0]) == 0)
1711 {
1712 cli_error(cli, "Plugin is already loaded");
1713 return CLI_OK;
1714 }
1715 }
1716
1717 if (firstfree)
1718 {
1719 strncpy(config->plugins[firstfree], argv[0], sizeof(config->plugins[firstfree]) - 1);
1720 config->reload_config = 1;
1721 cli_print(cli, "Loading plugin %s", argv[0]);
1722 }
1723
1724 return CLI_OK;
1725 }
1726
1727 static int cmd_remove_plugin(struct cli_def *cli, const char *command, char **argv, int argc)
1728 {
1729 int i;
1730
1731 if (CLI_HELP_REQUESTED)
1732 return cli_arg_help(cli, argc > 1,
1733 "PLUGIN", "Name of plugin to unload", NULL);
1734
1735 if (argc != 1)
1736 {
1737 cli_error(cli, "Specify a plugin to remove");
1738 return CLI_OK;
1739 }
1740
1741 for (i = 0; i < MAXPLUGINS; i++)
1742 {
1743 if (strcmp(config->plugins[i], argv[0]) == 0)
1744 {
1745 config->reload_config = 1;
1746 memset(config->plugins[i], 0, sizeof(config->plugins[i]));
1747 return CLI_OK;
1748 }
1749 }
1750
1751 cli_error(cli, "Plugin is not loaded");
1752 return CLI_OK;
1753 }
1754
1755 static char *duration(time_t secs)
1756 {
1757 static char *buf = NULL;
1758 int p = 0;
1759
1760 if (!buf) buf = calloc(64, 1);
1761
1762 if (secs >= 86400)
1763 {
1764 int days = secs / 86400;
1765 p = sprintf(buf, "%d day%s, ", days, days > 1 ? "s" : "");
1766 secs %= 86400;
1767 }
1768
1769 if (secs >= 3600)
1770 {
1771 int mins = secs / 60;
1772 int hrs = mins / 60;
1773
1774 mins %= 60;
1775 sprintf(buf + p, "%d:%02d", hrs, mins);
1776 }
1777 else if (secs >= 60)
1778 {
1779 int mins = secs / 60;
1780 sprintf(buf + p, "%d min%s", mins, mins > 1 ? "s" : "");
1781 }
1782 else
1783 sprintf(buf, "%ld sec%s", secs, secs > 1 ? "s" : "");
1784
1785 return buf;
1786 }
1787
1788 static int cmd_uptime(struct cli_def *cli, const char *command, char **argv, int argc)
1789 {
1790 FILE *fh;
1791 char buf[100], *p = buf, *loads[3];
1792 int i, num_sessions = 0;
1793
1794 if (CLI_HELP_REQUESTED)
1795 return CLI_HELP_NO_ARGS;
1796
1797 fh = fopen("/proc/loadavg", "r");
1798 p = fgets(buf, 100, fh);
1799 fclose(fh);
1800
1801 for (i = 0; i < 3; i++)
1802 loads[i] = strdup(strsep(&p, " "));
1803
1804 time(&time_now);
1805 strftime(buf, 99, "%H:%M:%S", localtime(&time_now));
1806
1807 for (i = 1; i < MAXSESSION; i++)
1808 if (session[i].opened) num_sessions++;
1809
1810 cli_print(cli, "%s up %s, %d users, load average: %s, %s, %s",
1811 buf,
1812 duration(time_now - config->start_time),
1813 num_sessions,
1814 loads[0], loads[1], loads[2]
1815 );
1816 for (i = 0; i < 3; i++)
1817 if (loads[i]) free(loads[i]);
1818
1819 cli_print(cli, "Bandwidth: %s", config->bandwidth);
1820
1821 return CLI_OK;
1822 }
1823
1824 static int cmd_set(struct cli_def *cli, const char *command, char **argv, int argc)
1825 {
1826 int i;
1827
1828 if (CLI_HELP_REQUESTED)
1829 {
1830 switch (argc)
1831 {
1832 case 1:
1833 {
1834 int len = strlen(argv[0])-1;
1835 for (i = 0; config_values[i].key; i++)
1836 if (!len || !strncmp(config_values[i].key, argv[0], len))
1837 cli_error(cli, " %s", config_values[i].key);
1838 }
1839
1840 return CLI_OK;
1841
1842 case 2:
1843 return cli_arg_help(cli, 0,
1844 "VALUE", "Value for variable", NULL);
1845
1846 case 3:
1847 if (!argv[2][1])
1848 return cli_arg_help(cli, 1, NULL);
1849
1850 default:
1851 return CLI_OK;
1852 }
1853 }
1854
1855 if (argc != 2)
1856 {
1857 cli_error(cli, "Specify variable and value");
1858 return CLI_OK;
1859 }
1860
1861 for (i = 0; config_values[i].key; i++)
1862 {
1863 void *value = ((void *) config) + config_values[i].offset;
1864 if (strcmp(config_values[i].key, argv[0]) == 0)
1865 {
1866 // Found a value to set
1867 cli_print(cli, "Setting \"%s\" to \"%s\"", argv[0], argv[1]);
1868 switch (config_values[i].type)
1869 {
1870 case STRING:
1871 snprintf((char *) value, config_values[i].size, "%s", argv[1]);
1872 break;
1873 case INT:
1874 *(int *) value = atoi(argv[1]);
1875 break;
1876 case UNSIGNED_LONG:
1877 *(unsigned long *) value = atol(argv[1]);
1878 break;
1879 case SHORT:
1880 *(short *) value = atoi(argv[1]);
1881 break;
1882 case IPv4:
1883 *(in_addr_t *) value = inet_addr(argv[1]);
1884 break;
1885 case IPv6:
1886 inet_pton(AF_INET6, argv[1], value);
1887 break;
1888 case BOOL:
1889 if (strcasecmp(argv[1], "yes") == 0 || strcasecmp(argv[1], "true") == 0 || strcasecmp(argv[1], "1") == 0)
1890 *(int *) value = 1;
1891 else
1892 *(int *) value = 0;
1893 break;
1894 default:
1895 cli_error(cli, "Unknown variable type");
1896 break;
1897 }
1898 config->reload_config = 1;
1899 return CLI_OK;
1900 }
1901 }
1902
1903 cli_error(cli, "Unknown variable \"%s\"", argv[0]);
1904 return CLI_OK;
1905 }
1906
1907 int regular_stuff(struct cli_def *cli)
1908 {
1909 #ifdef RINGBUFFER
1910 int out = 0;
1911 int i;
1912
1913 for (i = debug_rb_tail; i != ringbuffer->tail; i = (i + 1) % RINGBUFFER_SIZE)
1914 {
1915 char *m = ringbuffer->buffer[i].message;
1916 char *p;
1917 int show = 0;
1918
1919 if (!*m) continue;
1920
1921 switch (ringbuffer->buffer[i].level)
1922 {
1923 case 0: show = debug_flags.critical; break;
1924 case 1: show = debug_flags.error; break;
1925 case 2: show = debug_flags.warning; break;
1926 case 3: show = debug_flags.info; break;
1927 case 4: show = debug_flags.calls; break;
1928 case 5: show = debug_flags.data; break;
1929 }
1930
1931 if (!show) continue;
1932
1933 if (!(p = strchr(m, '\n')))
1934 p = m + strlen(m);
1935
1936 cli_error(cli, "\r%s-%u-%u %.*s",
1937 debug_levels[(int)ringbuffer->buffer[i].level],
1938 ringbuffer->buffer[i].tunnel,
1939 ringbuffer->buffer[i].session,
1940 (int) (p - m), m);
1941
1942 out++;
1943 }
1944
1945 debug_rb_tail = ringbuffer->tail;
1946 if (out)
1947 cli_reprompt(cli);
1948 #endif
1949 return CLI_OK;
1950 }
1951
1952 #ifdef BGP
1953 static int cmd_router_bgp(struct cli_def *cli, const char *command, char **argv, int argc)
1954 {
1955 int as;
1956
1957 if (CLI_HELP_REQUESTED)
1958 return cli_arg_help(cli, argc > 1,
1959 "<1-65535>", "Autonomous system number", NULL);
1960
1961 if (argc != 1 || (as = atoi(argv[0])) < 1 || as > 65535)
1962 {
1963 cli_error(cli, "Invalid autonomous system number");
1964 return CLI_OK;
1965 }
1966
1967 if (bgp_configured && as != config->as_number)
1968 {
1969 cli_error(cli, "Can't change local AS on a running system");
1970 return CLI_OK;
1971 }
1972
1973 config->as_number = as;
1974 cli_set_configmode(cli, MODE_CONFIG_BGP, "router");
1975
1976 return CLI_OK;
1977 }
1978
1979 static int find_bgp_neighbour(char const *name)
1980 {
1981 int i;
1982 int new = -1;
1983 struct hostent *h;
1984 in_addr_t addrs[4] = { 0 };
1985 char **a;
1986
1987 if (!(h = gethostbyname(name)) || h->h_addrtype != AF_INET)
1988 return -2;
1989
1990 for (i = 0; i < sizeof(addrs) / sizeof(*addrs) && h->h_addr_list[i]; i++)
1991 memcpy(&addrs[i], h->h_addr_list[i], sizeof(*addrs));
1992
1993 for (i = 0; i < BGP_NUM_PEERS; i++)
1994 {
1995 if (!config->neighbour[i].name[0])
1996 {
1997 if (new == -1) new = i;
1998 continue;
1999 }
2000
2001 if (!strcmp(name, config->neighbour[i].name))
2002 return i;
2003
2004 if (!(h = gethostbyname(config->neighbour[i].name)) || h->h_addrtype != AF_INET)
2005 continue;
2006
2007 for (a = h->h_addr_list; *a; a++)
2008 {
2009 int j;
2010 for (j = 0; j < sizeof(addrs) / sizeof(*addrs) && addrs[j]; j++)
2011 if (!memcmp(&addrs[j], *a, sizeof(*addrs)))
2012 return i;
2013 }
2014 }
2015
2016 return new;
2017 }
2018
2019 static int cmd_router_bgp_neighbour(struct cli_def *cli, const char *command, char **argv, int argc)
2020 {
2021 int i;
2022 int keepalive;
2023 int hold;
2024
2025 if (CLI_HELP_REQUESTED)
2026 {
2027 switch (argc)
2028 {
2029 case 1:
2030 return cli_arg_help(cli, 0,
2031 "A.B.C.D", "BGP neighbour address",
2032 "NAME", "BGP neighbour name",
2033 NULL);
2034
2035 case 2:
2036 return cli_arg_help(cli, 0,
2037 "remote-as", "Set remote autonomous system number",
2038 "timers", "Set timers",
2039 "update-source", "Set source address to use for the BGP session",
2040 NULL);
2041
2042 default:
2043 if (MATCH("remote-as", argv[1]))
2044 return cli_arg_help(cli, argv[2][1], "<1-65535>", "Autonomous system number", NULL);
2045
2046 if (MATCH("timers", argv[1]))
2047 {
2048 if (argc == 3)
2049 return cli_arg_help(cli, 0, "<1-65535>", "Keepalive time", NULL);
2050
2051 if (argc == 4)
2052 return cli_arg_help(cli, argv[3][1], "<3-65535>", "Hold time", NULL);
2053
2054 if (argc == 5 && !argv[4][1])
2055 return cli_arg_help(cli, 1, NULL);
2056 }
2057
2058 if (MATCH("update-source", argv[1]))
2059 return cli_arg_help(cli, argc > 3, "A.B.C.D", "Source IP address", NULL);
2060
2061 return CLI_OK;
2062 }
2063 }
2064
2065 if (argc < 3)
2066 {
2067 cli_error(cli, "Invalid arguments");
2068 return CLI_OK;
2069 }
2070
2071 if ((i = find_bgp_neighbour(argv[0])) == -2)
2072 {
2073 cli_error(cli, "Invalid neighbour");
2074 return CLI_OK;
2075 }
2076
2077 if (i == -1)
2078 {
2079 cli_error(cli, "Too many neighbours (max %d)", BGP_NUM_PEERS);
2080 return CLI_OK;
2081 }
2082
2083 if (MATCH("remote-as", argv[1]))
2084 {
2085 int as = atoi(argv[2]);
2086 if (as < 0 || as > 65535)
2087 {
2088 cli_error(cli, "Invalid autonomous system number");
2089 return CLI_OK;
2090 }
2091
2092 if (!config->neighbour[i].name[0])
2093 {
2094 snprintf(config->neighbour[i].name, sizeof(config->neighbour[i].name), "%s", argv[0]);
2095 config->neighbour[i].keepalive = -1;
2096 config->neighbour[i].hold = -1;
2097 config->neighbour[i].update_source.s_addr = INADDR_ANY;
2098 }
2099
2100 config->neighbour[i].as = as;
2101 return CLI_OK;
2102 }
2103
2104 if (MATCH("update-source", argv[1]))
2105 {
2106 struct in_addr addr;
2107
2108 if (!config->neighbour[i].name[0])
2109 {
2110 cli_error(cli, "Specify remote-as first");
2111 return CLI_OK;
2112 }
2113
2114 if (!inet_aton(argv[2], &addr))
2115 {
2116 cli_error(cli, "Cannot parse IP \"%s\"", argv[2]);
2117 return CLI_OK;
2118 }
2119
2120 config->neighbour[i].update_source = addr;
2121 return CLI_OK;
2122 }
2123
2124 if (argc != 4 || !MATCH("timers", argv[1]))
2125 {
2126 cli_error(cli, "Invalid arguments");
2127 return CLI_OK;
2128 }
2129
2130 if (!config->neighbour[i].name[0])
2131 {
2132 cli_error(cli, "Specify remote-as first");
2133 return CLI_OK;
2134 }
2135
2136 keepalive = atoi(argv[2]);
2137 hold = atoi(argv[3]);
2138
2139 if (keepalive < 1 || keepalive > 65535)
2140 {
2141 cli_error(cli, "Invalid keepalive time");
2142 return CLI_OK;
2143 }
2144
2145 if (hold < 3 || hold > 65535)
2146 {
2147 cli_error(cli, "Invalid hold time");
2148 return CLI_OK;
2149 }
2150
2151 if (keepalive == BGP_KEEPALIVE_TIME)
2152 keepalive = -1; // using default value
2153
2154 if (hold == BGP_HOLD_TIME)
2155 hold = -1;
2156
2157 config->neighbour[i].keepalive = keepalive;
2158 config->neighbour[i].hold = hold;
2159
2160 return CLI_OK;
2161 }
2162
2163 static int cmd_router_bgp_no_neighbour(struct cli_def *cli, const char *command, char **argv, int argc)
2164 {
2165 int i;
2166
2167 if (CLI_HELP_REQUESTED)
2168 return cli_arg_help(cli, argc > 0,
2169 "A.B.C.D", "BGP neighbour address",
2170 "NAME", "BGP neighbour name",
2171 NULL);
2172
2173 if (argc != 1)
2174 {
2175 cli_error(cli, "Specify a BGP neighbour");
2176 return CLI_OK;
2177 }
2178
2179 if ((i = find_bgp_neighbour(argv[0])) == -2)
2180 {
2181 cli_error(cli, "Invalid neighbour");
2182 return CLI_OK;
2183 }
2184
2185 if (i < 0 || !config->neighbour[i].name[0])
2186 {
2187 cli_error(cli, "Neighbour %s not configured", argv[0]);
2188 return CLI_OK;
2189 }
2190
2191 memset(&config->neighbour[i], 0, sizeof(config->neighbour[i]));
2192 return CLI_OK;
2193 }
2194
2195 static int cmd_show_bgp(struct cli_def *cli, const char *command, char **argv, int argc)
2196 {
2197 int i;
2198 int hdr = 0;
2199 char *addr;
2200
2201 if (!bgp_configured)
2202 return CLI_OK;
2203
2204 if (CLI_HELP_REQUESTED)
2205 return cli_arg_help(cli, 1,
2206 "A.B.C.D", "BGP neighbour address",
2207 "NAME", "BGP neighbour name",
2208 NULL);
2209
2210 cli_print(cli, "BGPv%d router identifier %s, local AS number %d",
2211 BGP_VERSION, fmtaddr(my_address, 0), (int) config->as_number);
2212
2213 time(&time_now);
2214
2215 for (i = 0; i < BGP_NUM_PEERS; i++)
2216 {
2217 if (!*bgp_peers[i].name)
2218 continue;
2219
2220 addr = fmtaddr(bgp_peers[i].addr, 0);
2221 if (argc && strcmp(addr, argv[0]) &&
2222 strncmp(bgp_peers[i].name, argv[0], strlen(argv[0])))
2223 continue;
2224
2225 if (!hdr++)
2226 {
2227 cli_print(cli, " ");
2228 cli_print(cli, "Peer AS Address "
2229 "State Retries Retry in Route Pend Timers");
2230 cli_print(cli, "------------------ ----- --------------- "
2231 "----------- ------- -------- ----- ---- ---------");
2232 }
2233
2234 cli_print(cli, "%-18.18s %5d %15s %-11s %7d %7lds %5s %4s %4d %4d",
2235 bgp_peers[i].name,
2236 bgp_peers[i].as,
2237 addr,
2238 bgp_state_str(bgp_peers[i].state),
2239 bgp_peers[i].retry_count,
2240 bgp_peers[i].retry_time ? bgp_peers[i].retry_time - time_now : 0,
2241 bgp_peers[i].routing ? "yes" : "no",
2242 bgp_peers[i].update_routes ? "yes" : "no",
2243 bgp_peers[i].keepalive,
2244 bgp_peers[i].hold);
2245 }
2246
2247 return CLI_OK;
2248 }
2249
2250 static int cmd_suspend_bgp(struct cli_def *cli, const char *command, char **argv, int argc)
2251 {
2252 int i;
2253 char *addr;
2254
2255 if (!bgp_configured)
2256 return CLI_OK;
2257
2258 if (CLI_HELP_REQUESTED)
2259 return cli_arg_help(cli, 1,
2260 "A.B.C.D", "BGP neighbour address",
2261 "NAME", "BGP neighbour name",
2262 NULL);
2263
2264 for (i = 0; i < BGP_NUM_PEERS; i++)
2265 {
2266 if (bgp_peers[i].state != Established)
2267 continue;
2268
2269 if (!bgp_peers[i].routing)
2270 continue;
2271
2272 addr = fmtaddr(bgp_peers[i].addr, 0);
2273 if (argc && strcmp(addr, argv[0]) && strcmp(bgp_peers[i].name, argv[0]))
2274 continue;
2275
2276 bgp_peers[i].cli_flag = BGP_CLI_SUSPEND;
2277 cli_print(cli, "Suspending peer %s", bgp_peers[i].name);
2278 }
2279
2280 return CLI_OK;
2281 }
2282
2283 static int cmd_no_suspend_bgp(struct cli_def *cli, const char *command, char **argv, int argc)
2284 {
2285 int i;
2286 char *addr;
2287
2288 if (!bgp_configured)
2289 return CLI_OK;
2290
2291 if (CLI_HELP_REQUESTED)
2292 return cli_arg_help(cli, 1,
2293 "A.B.C.D", "BGP neighbour address",
2294 "NAME", "BGP neighbour name",
2295 NULL);
2296
2297 for (i = 0; i < BGP_NUM_PEERS; i++)
2298 {
2299 if (bgp_peers[i].state != Established)
2300 continue;
2301
2302 if (bgp_peers[i].routing)
2303 continue;
2304
2305 addr = fmtaddr(bgp_peers[i].addr, 0);
2306 if (argc && strcmp(addr, argv[0]) &&
2307 strncmp(bgp_peers[i].name, argv[0], strlen(argv[0])))
2308 continue;
2309
2310 bgp_peers[i].cli_flag = BGP_CLI_ENABLE;
2311 cli_print(cli, "Un-suspending peer %s", bgp_peers[i].name);
2312 }
2313
2314 return CLI_OK;
2315 }
2316
2317 static int cmd_restart_bgp(struct cli_def *cli, const char *command, char **argv, int argc)
2318 {
2319 int i;
2320 char *addr;
2321
2322 if (!bgp_configured)
2323 return CLI_OK;
2324
2325 if (CLI_HELP_REQUESTED)
2326 return cli_arg_help(cli, 1,
2327 "A.B.C.D", "BGP neighbour address",
2328 "NAME", "BGP neighbour name",
2329 NULL);
2330
2331 for (i = 0; i < BGP_NUM_PEERS; i++)
2332 {
2333 if (!*bgp_peers[i].name)
2334 continue;
2335
2336 addr = fmtaddr(bgp_peers[i].addr, 0);
2337 if (argc && strcmp(addr, argv[0]) &&
2338 strncmp(bgp_peers[i].name, argv[0], strlen(argv[0])))
2339 continue;
2340
2341 bgp_peers[i].cli_flag = BGP_CLI_RESTART;
2342 cli_print(cli, "Restarting peer %s", bgp_peers[i].name);
2343 }
2344
2345 return CLI_OK;
2346 }
2347 #endif /* BGP*/
2348
2349 static int filt;
2350 static int access_list(struct cli_def *cli, char **argv, int argc, int add)
2351 {
2352 int extended;
2353
2354 if (CLI_HELP_REQUESTED)
2355 {
2356 switch (argc)
2357 {
2358 case 1:
2359 return cli_arg_help(cli, 0,
2360 "standard", "Standard syntax",
2361 "extended", "Extended syntax",
2362 NULL);
2363
2364 case 2:
2365 return cli_arg_help(cli, argv[1][1],
2366 "NAME", "Access-list name",
2367 NULL);
2368
2369 default:
2370 if (argc == 3 && !argv[2][1])
2371 return cli_arg_help(cli, 1, NULL);
2372
2373 return CLI_OK;
2374 }
2375 }
2376
2377 if (argc != 2)
2378 {
2379 cli_error(cli, "Specify access-list type and name");
2380 return CLI_OK;
2381 }
2382
2383 if (MATCH("standard", argv[0]))
2384 extended = 0;
2385 else if (MATCH("extended", argv[0]))
2386 extended = 1;
2387 else
2388 {
2389 cli_error(cli, "Invalid access-list type");
2390 return CLI_OK;
2391 }
2392
2393 if (strlen(argv[1]) > sizeof(ip_filters[0].name) - 1 ||
2394 strspn(argv[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-") != strlen(argv[1]))
2395 {
2396 cli_error(cli, "Invalid access-list name");
2397 return CLI_OK;
2398 }
2399
2400 filt = find_filter(argv[1], strlen(argv[1]));
2401 if (add)
2402 {
2403 if (filt < 0)
2404 {
2405 cli_error(cli, "Too many access-lists");
2406 return CLI_OK;
2407 }
2408
2409 // racy
2410 if (!*ip_filters[filt].name)
2411 {
2412 memset(&ip_filters[filt], 0, sizeof(ip_filters[filt]));
2413 strcpy(ip_filters[filt].name, argv[1]);
2414 ip_filters[filt].extended = extended;
2415 }
2416 else if (ip_filters[filt].extended != extended)
2417 {
2418 cli_error(cli, "Access-list is %s",
2419 ip_filters[filt].extended ? "extended" : "standard");
2420
2421 return CLI_OK;
2422 }
2423
2424 cli_set_configmode(cli, MODE_CONFIG_NACL, extended ? "ext-nacl" : "std-nacl");
2425 return CLI_OK;
2426 }
2427
2428 if (filt < 0 || !*ip_filters[filt].name)
2429 {
2430 cli_error(cli, "Access-list not defined");
2431 return CLI_OK;
2432 }
2433
2434 // racy
2435 if (ip_filters[filt].used)
2436 {
2437 cli_error(cli, "Access-list in use");
2438 return CLI_OK;
2439 }
2440
2441 memset(&ip_filters[filt], 0, sizeof(ip_filters[filt]));
2442 return CLI_OK;
2443 }
2444
2445 static int cmd_ip_access_list(struct cli_def *cli, const char *command, char **argv, int argc)
2446 {
2447 return access_list(cli, argv, argc, 1);
2448 }
2449
2450 static int cmd_no_ip_access_list(struct cli_def *cli, const char *command, char **argv, int argc)
2451 {
2452 return access_list(cli, argv, argc, 0);
2453 }
2454
2455 static int show_ip_wild(char *buf, in_addr_t ip, in_addr_t wild)
2456 {
2457 if (ip == INADDR_ANY && wild == INADDR_BROADCAST)
2458 return sprintf(buf, " any");
2459
2460 if (wild == INADDR_ANY)
2461 return sprintf(buf, " host %s", fmtaddr(ip, 0));
2462
2463 return sprintf(buf, " %s %s", fmtaddr(ip, 0), fmtaddr(wild, 1));
2464 }
2465
2466 static int show_ports(char *buf, ip_filter_portt *ports)
2467 {
2468 switch (ports->op)
2469 {
2470 case FILTER_PORT_OP_EQ: return sprintf(buf, " eq %u", ports->port);
2471 case FILTER_PORT_OP_NEQ: return sprintf(buf, " neq %u", ports->port);
2472 case FILTER_PORT_OP_GT: return sprintf(buf, " gt %u", ports->port);
2473 case FILTER_PORT_OP_LT: return sprintf(buf, " lt %u", ports->port);
2474 case FILTER_PORT_OP_RANGE: return sprintf(buf, " range %u %u", ports->port, ports->port2);
2475 }
2476
2477 return 0;
2478 }
2479
2480 static char const *show_access_list_rule(int extended, ip_filter_rulet *rule)
2481 {
2482 static char buf[256];
2483 char *p = buf;
2484
2485 p += sprintf(p, " %s", rule->action == FILTER_ACTION_PERMIT ? "permit" : "deny");
2486 if (extended)
2487 {
2488 struct protoent *proto = getprotobynumber(rule->proto);
2489 p += sprintf(p, " %s", proto ? proto->p_name : "ERR");
2490 }
2491
2492 p += show_ip_wild(p, rule->src_ip, rule->src_wild);
2493 if (!extended)
2494 return buf;
2495
2496 if (rule->proto == IPPROTO_TCP || rule->proto == IPPROTO_UDP)
2497 p += show_ports(p, &rule->src_ports);
2498
2499 p += show_ip_wild(p, rule->dst_ip, rule->dst_wild);
2500 if (rule->proto == IPPROTO_TCP || rule->proto == IPPROTO_UDP)
2501 p += show_ports(p, &rule->dst_ports);
2502
2503 if (rule->proto == IPPROTO_TCP && rule->tcp_flag_op)
2504 {
2505 switch (rule->tcp_flag_op)
2506 {
2507 case FILTER_FLAG_OP_EST:
2508 p += sprintf(p, " established");
2509 break;
2510
2511 case FILTER_FLAG_OP_ANY:
2512 case FILTER_FLAG_OP_ALL:
2513 p += sprintf(p, " match-%s", rule->tcp_flag_op == FILTER_FLAG_OP_ALL ? "all" : "any");
2514 if (rule->tcp_sflags & TCP_FLAG_FIN) p += sprintf(p, " +fin");
2515 if (rule->tcp_cflags & TCP_FLAG_FIN) p += sprintf(p, " -fin");
2516 if (rule->tcp_sflags & TCP_FLAG_SYN) p += sprintf(p, " +syn");
2517 if (rule->tcp_cflags & TCP_FLAG_SYN) p += sprintf(p, " -syn");
2518 if (rule->tcp_sflags & TCP_FLAG_RST) p += sprintf(p, " +rst");
2519 if (rule->tcp_cflags & TCP_FLAG_RST) p += sprintf(p, " -rst");
2520 if (rule->tcp_sflags & TCP_FLAG_PSH) p += sprintf(p, " +psh");
2521 if (rule->tcp_cflags & TCP_FLAG_PSH) p += sprintf(p, " -psh");
2522 if (rule->tcp_sflags & TCP_FLAG_ACK) p += sprintf(p, " +ack");
2523 if (rule->tcp_cflags & TCP_FLAG_ACK) p += sprintf(p, " -ack");
2524 if (rule->tcp_sflags & TCP_FLAG_URG) p += sprintf(p, " +urg");
2525 if (rule->tcp_cflags & TCP_FLAG_URG) p += sprintf(p, " -urg");
2526 break;
2527 }
2528 }
2529
2530 if (rule->frag)
2531 p += sprintf(p, " fragments");
2532
2533 return buf;
2534 }
2535
2536 static ip_filter_rulet *access_list_rule_ext(struct cli_def *cli, const char *command, char **argv, int argc)
2537 {
2538 static ip_filter_rulet rule;
2539 struct in_addr addr;
2540 int i;
2541 int a;
2542
2543 if (CLI_HELP_REQUESTED)
2544 {
2545 if (argc == 1)
2546 {
2547 cli_arg_help(cli, 0,
2548 "ip", "Match IP packets",
2549 "tcp", "Match TCP packets",
2550 "udp", "Match UDP packets",
2551 NULL);
2552
2553 return NULL;
2554 }
2555
2556 // *sigh*, too darned complex
2557 cli_arg_help(cli, 0, "RULE", "SOURCE [PORTS] DEST [PORTS] FLAGS", NULL);
2558 return NULL;
2559 }
2560
2561 if (argc < 3)
2562 {
2563 cli_error(cli, "Specify rule details");
2564 return NULL;
2565 }
2566
2567 memset(&rule, 0, sizeof(rule));
2568 rule.action = (command[0] == 'p')
2569 ? FILTER_ACTION_PERMIT
2570 : FILTER_ACTION_DENY;
2571
2572 if (MATCH("ip", argv[0]))
2573 rule.proto = IPPROTO_IP;
2574 else if (MATCH("udp", argv[0]))
2575 rule.proto = IPPROTO_UDP;
2576 else if (MATCH("tcp", argv[0]))
2577 rule.proto = IPPROTO_TCP;
2578 else
2579 {
2580 cli_error(cli, "Invalid protocol \"%s\"", argv[0]);
2581 return NULL;
2582 }
2583
2584 for (a = 1, i = 0; i < 2; i++)
2585 {
2586 in_addr_t *ip;
2587 in_addr_t *wild;
2588 ip_filter_portt *port;
2589
2590 if (i == 0)
2591 {
2592 ip = &rule.src_ip;
2593 wild = &rule.src_wild;
2594 port = &rule.src_ports;
2595 }
2596 else
2597 {
2598 ip = &rule.dst_ip;
2599 wild = &rule.dst_wild;
2600 port = &rule.dst_ports;
2601 if (a >= argc)
2602 {
2603 cli_error(cli, "Specify destination");
2604 return NULL;
2605 }
2606 }
2607
2608 if (MATCH("any", argv[a]))
2609 {
2610 *ip = INADDR_ANY;
2611 *wild = INADDR_BROADCAST;
2612 a++;
2613 }
2614 else if (MATCH("host", argv[a]))
2615 {
2616 if (++a >= argc)
2617 {
2618 cli_error(cli, "Specify host ip address");
2619 return NULL;
2620 }
2621
2622 if (!inet_aton(argv[a], &addr))
2623 {
2624 cli_error(cli, "Cannot parse IP \"%s\"", argv[a]);
2625 return NULL;
2626 }
2627
2628 *ip = addr.s_addr;
2629 *wild = INADDR_ANY;
2630 a++;
2631 }
2632 else
2633 {
2634 if (a >= argc - 1)
2635 {
2636 cli_error(cli, "Specify %s ip address and wildcard", i ? "destination" : "source");
2637 return NULL;
2638 }
2639
2640 if (!inet_aton(argv[a], &addr))
2641 {
2642 cli_error(cli, "Cannot parse IP \"%s\"", argv[a]);
2643 return NULL;
2644 }
2645
2646 *ip = addr.s_addr;
2647
2648 if (!inet_aton(argv[++a], &addr))
2649 {
2650 cli_error(cli, "Cannot parse IP \"%s\"", argv[a]);
2651 return NULL;
2652 }
2653
2654 *wild = addr.s_addr;
2655 a++;
2656 }
2657
2658 if (rule.proto == IPPROTO_IP || a >= argc)
2659 continue;
2660
2661 port->op = 0;
2662 if (MATCH("eq", argv[a]))
2663 port->op = FILTER_PORT_OP_EQ;
2664 else if (MATCH("neq", argv[a]))
2665 port->op = FILTER_PORT_OP_NEQ;
2666 else if (MATCH("gt", argv[a]))
2667 port->op = FILTER_PORT_OP_GT;
2668 else if (MATCH("lt", argv[a]))
2669 port->op = FILTER_PORT_OP_LT;
2670 else if (MATCH("range", argv[a]))
2671 port->op = FILTER_PORT_OP_RANGE;
2672
2673 if (!port->op)
2674 continue;
2675
2676 if (++a >= argc)
2677 {
2678 cli_error(cli, "Specify port");
2679 return NULL;
2680 }
2681
2682 if (!(port->port = atoi(argv[a])))
2683 {
2684 cli_error(cli, "Invalid port \"%s\"", argv[a]);
2685 return NULL;
2686 }
2687
2688 a++;
2689 if (port->op != FILTER_PORT_OP_RANGE)
2690 continue;
2691
2692 if (a >= argc)
2693 {
2694 cli_error(cli, "Specify port");
2695 return NULL;
2696 }
2697
2698 if (!(port->port2 = atoi(argv[a])) || port->port2 < port->port)
2699 {
2700 cli_error(cli, "Invalid port \"%s\"", argv[a]);
2701 return NULL;
2702 }
2703
2704 a++;
2705 }
2706
2707 if (rule.proto == IPPROTO_TCP && a < argc)
2708 {
2709 if (MATCH("established", argv[a]))
2710 {
2711 rule.tcp_flag_op = FILTER_FLAG_OP_EST;
2712 a++;
2713 }
2714 else if (!strcmp(argv[a], "match-any") || !strcmp(argv[a], "match-an") ||
2715 !strcmp(argv[a], "match-all") || !strcmp(argv[a], "match-al"))
2716 {
2717 rule.tcp_flag_op = argv[a][7] == 'n'
2718 ? FILTER_FLAG_OP_ANY
2719 : FILTER_FLAG_OP_ALL;
2720
2721 if (++a >= argc)
2722 {
2723 cli_error(cli, "Specify tcp flags");
2724 return NULL;
2725 }
2726
2727 while (a < argc && (argv[a][0] == '+' || argv[a][0] == '-'))
2728 {
2729 uint8_t *f;
2730
2731 f = (argv[a][0] == '+') ? &rule.tcp_sflags : &rule.tcp_cflags;
2732
2733 if (MATCH("fin", &argv[a][1])) *f |= TCP_FLAG_FIN;
2734 else if (MATCH("syn", &argv[a][1])) *f |= TCP_FLAG_SYN;
2735 else if (MATCH("rst", &argv[a][1])) *f |= TCP_FLAG_RST;
2736 else if (MATCH("psh", &argv[a][1])) *f |= TCP_FLAG_PSH;
2737 else if (MATCH("ack", &argv[a][1])) *f |= TCP_FLAG_ACK;
2738 else if (MATCH("urg", &argv[a][1])) *f |= TCP_FLAG_URG;
2739 else
2740 {
2741 cli_error(cli, "Invalid tcp flag \"%s\"", argv[a]);
2742 return NULL;
2743 }
2744
2745 a++;
2746 }
2747 }
2748 }
2749
2750 if (a < argc && MATCH("fragments", argv[a]))
2751 {
2752 if (rule.src_ports.op || rule.dst_ports.op || rule.tcp_flag_op)
2753 {
2754 cli_error(cli, "Can't specify \"fragments\" on rules with layer 4 matches");
2755 return NULL;
2756 }
2757
2758 rule.frag = 1;
2759 a++;
2760 }
2761
2762 if (a < argc)
2763 {
2764 cli_error(cli, "Invalid flag \"%s\"", argv[a]);
2765 return NULL;
2766 }
2767
2768 return &rule;
2769 }
2770
2771 static ip_filter_rulet *access_list_rule_std(struct cli_def *cli, const char *command, char **argv, int argc)
2772 {
2773 static ip_filter_rulet rule;
2774 struct in_addr addr;
2775
2776 if (CLI_HELP_REQUESTED)
2777 {
2778 if (argc == 1)
2779 {
2780 cli_arg_help(cli, argv[0][1],
2781 "A.B.C.D", "Source address",
2782 "any", "Any source address",
2783 "host", "Source host",
2784 NULL);
2785
2786 return NULL;
2787 }
2788
2789 if (MATCH("any", argv[0]))
2790 {
2791 if (argc == 2 && !argv[1][1])
2792 cli_arg_help(cli, 1, NULL);
2793 }
2794 else if (MATCH("host", argv[0]))
2795 {
2796 if (argc == 2)
2797 {
2798 cli_arg_help(cli, argv[1][1],
2799 "A.B.C.D", "Host address",
2800 NULL);
2801 }
2802 else if (argc == 3 && !argv[2][1])
2803 cli_arg_help(cli, 1, NULL);
2804 }
2805 else
2806 {
2807 if (argc == 2)
2808 {
2809 cli_arg_help(cli, 1,
2810 "A.B.C.D", "Wildcard bits",
2811 NULL);
2812 }
2813 else if (argc == 3 && !argv[2][1])
2814 cli_arg_help(cli, 1, NULL);
2815 }
2816
2817 return NULL;
2818 }
2819
2820 if (argc < 1)
2821 {
2822 cli_error(cli, "Specify rule details");
2823 return NULL;
2824 }
2825
2826 memset(&rule, 0, sizeof(rule));
2827 rule.action = (command[0] == 'p')
2828 ? FILTER_ACTION_PERMIT
2829 : FILTER_ACTION_DENY;
2830
2831 rule.proto = IPPROTO_IP;
2832 if (MATCH("any", argv[0]))
2833 {
2834 rule.src_ip = INADDR_ANY;
2835 rule.src_wild = INADDR_BROADCAST;
2836 }
2837 else if (MATCH("host", argv[0]))
2838 {
2839 if (argc != 2)
2840 {
2841 cli_error(cli, "Specify host ip address");
2842 return NULL;
2843 }
2844
2845 if (!inet_aton(argv[1], &addr))
2846 {
2847 cli_error(cli, "Cannot parse IP \"%s\"", argv[1]);
2848 return NULL;
2849 }
2850
2851 rule.src_ip = addr.s_addr;
2852 rule.src_wild = INADDR_ANY;
2853 }
2854 else
2855 {
2856 if (argc > 2)
2857 {
2858 cli_error(cli, "Specify source ip address and wildcard");
2859 return NULL;
2860 }
2861
2862 if (!inet_aton(argv[0], &addr))
2863 {
2864 cli_error(cli, "Cannot parse IP \"%s\"", argv[0]);
2865 return NULL;
2866 }
2867
2868 rule.src_ip = addr.s_addr;
2869
2870 if (argc > 1)
2871 {
2872 if (!inet_aton(argv[1], &addr))
2873 {
2874 cli_error(cli, "Cannot parse IP \"%s\"", argv[1]);
2875 return NULL;
2876 }
2877
2878 rule.src_wild = addr.s_addr;
2879 }
2880 else
2881 rule.src_wild = INADDR_ANY;
2882 }
2883
2884 return &rule;
2885 }
2886
2887 static int cmd_ip_access_list_rule(struct cli_def *cli, const char *command, char **argv, int argc)
2888 {
2889 int i;
2890 ip_filter_rulet *rule = ip_filters[filt].extended
2891 ? access_list_rule_ext(cli, command, argv, argc)
2892 : access_list_rule_std(cli, command, argv, argc);
2893
2894 if (!rule)
2895 return CLI_OK;
2896
2897 for (i = 0; i < MAXFILTER_RULES - 1; i++) // -1: list always terminated by empty rule
2898 {
2899 if (!ip_filters[filt].rules[i].action)
2900 {
2901 memcpy(&ip_filters[filt].rules[i], rule, sizeof(*rule));
2902 return CLI_OK;
2903 }
2904
2905 if (!memcmp(&ip_filters[filt].rules[i], rule, offsetof(ip_filter_rulet, counter)))
2906 return CLI_OK;
2907 }
2908
2909 cli_error(cli, "Too many rules");
2910 return CLI_OK;
2911 }
2912
2913 static int cmd_filter(struct cli_def *cli, const char *command, char **argv, int argc)
2914 {
2915 sessionidt s;
2916 int i;
2917
2918 /* filter USER {in|out} FILTER ... */
2919 if (CLI_HELP_REQUESTED)
2920 {
2921 switch (argc)
2922 {
2923 case 1:
2924 return cli_arg_help(cli, 0,
2925 "USER", "Username of session to filter", NULL);
2926
2927 case 2:
2928 case 4:
2929 return cli_arg_help(cli, 0,
2930 "in", "Set incoming filter",
2931 "out", "Set outgoing filter", NULL);
2932
2933 case 3:
2934 case 5:
2935 return cli_arg_help(cli, argc == 5 && argv[4][1],
2936 "NAME", "Filter name", NULL);
2937
2938 default:
2939 return cli_arg_help(cli, argc > 1, NULL);
2940 }
2941 }
2942
2943 if (!config->cluster_iam_master)
2944 {
2945 cli_error(cli, "Can't do this on a slave. Do it on %s",
2946 fmtaddr(config->cluster_master_address, 0));
2947
2948 return CLI_OK;
2949 }
2950
2951 if (argc != 3 && argc != 5)
2952 {
2953 cli_error(cli, "Specify a user and filters");
2954 return CLI_OK;
2955 }
2956
2957 if (!(s = sessionbyuser(argv[0])))
2958 {
2959 cli_error(cli, "User %s is not connected", argv[0]);
2960 return CLI_OK;
2961 }
2962
2963 cli_session_actions[s].filter_in = cli_session_actions[s].filter_out = -1;
2964 for (i = 1; i < argc; i += 2)
2965 {
2966 int *f = 0;
2967 int v;
2968
2969 if (MATCH("in", argv[i]))
2970 {
2971 if (session[s].filter_in)
2972 {
2973 cli_error(cli, "Input already filtered");
2974 return CLI_OK;
2975 }
2976 f = &cli_session_actions[s].filter_in;
2977 }
2978 else if (MATCH("out", argv[i]))
2979 {
2980 if (session[s].filter_out)
2981 {
2982 cli_error(cli, "Output already filtered");
2983 return CLI_OK;
2984 }
2985 f = &cli_session_actions[s].filter_out;
2986 }
2987 else
2988 {
2989 cli_error(cli, "Invalid filter specification");
2990 return CLI_OK;
2991 }
2992
2993 v = find_filter(argv[i+1], strlen(argv[i+1]));
2994 if (v < 0 || !*ip_filters[v].name)
2995 {
2996 cli_error(cli, "Access-list %s not defined", argv[i+1]);
2997 return CLI_OK;
2998 }
2999
3000 *f = v + 1;
3001 }
3002
3003 cli_print(cli, "Filtering user %s", argv[0]);
3004 cli_session_actions[s].action |= CLI_SESS_FILTER;
3005
3006 return CLI_OK;
3007 }
3008
3009 static int cmd_no_filter(struct cli_def *cli, const char *command, char **argv, int argc)
3010 {
3011 int i;
3012 sessionidt s;
3013
3014 if (CLI_HELP_REQUESTED)
3015 return cli_arg_help(cli, argc > 1,
3016 "USER", "Username of session to remove filters from", NULL);
3017
3018 if (!config->cluster_iam_master)
3019 {
3020 cli_error(cli, "Can't do this on a slave. Do it on %s",
3021 fmtaddr(config->cluster_master_address, 0));
3022
3023 return CLI_OK;
3024 }
3025
3026 if (!argc)
3027 {
3028 cli_error(cli, "Specify a user to remove filters from");
3029 return CLI_OK;
3030 }
3031
3032 for (i = 0; i < argc; i++)
3033 {
3034 if (!(s = sessionbyuser(argv[i])))
3035 {
3036 cli_error(cli, "User %s is not connected", argv[i]);
3037 continue;
3038 }
3039
3040 if (session[s].filter_in || session[s].filter_out)
3041 {
3042 cli_print(cli, "Removing filters from user %s", argv[i]);
3043 cli_session_actions[s].action |= CLI_SESS_NOFILTER;
3044 }
3045 else
3046 {
3047 cli_error(cli, "User %s not filtered", argv[i]);
3048 }
3049 }
3050
3051 return CLI_OK;
3052 }
3053
3054 static int cmd_show_access_list(struct cli_def *cli, const char *command, char **argv, int argc)
3055 {
3056 int i;
3057
3058 if (CLI_HELP_REQUESTED)
3059 return cli_arg_help(cli, argc > 1, "NAME", "Filter name", NULL);
3060
3061 if (argc < 1)
3062 {
3063 cli_error(cli, "Specify a filter name");
3064 return CLI_OK;
3065 }
3066
3067 for (i = 0; i < argc; i++)
3068 {
3069 int f = find_filter(argv[i], strlen(argv[i]));
3070 ip_filter_rulet *rules;
3071
3072 if (f < 0 || !*ip_filters[f].name)
3073 {
3074 cli_error(cli, "Access-list %s not defined", argv[i]);
3075 return CLI_OK;
3076 }
3077
3078 if (i)
3079 cli_print(cli, " ");
3080
3081 cli_print(cli, "%s IP access list %s",
3082 ip_filters[f].extended ? "Extended" : "Standard",
3083 ip_filters[f].name);
3084
3085 for (rules = ip_filters[f].rules; rules->action; rules++)
3086 {
3087 char const *r = show_access_list_rule(ip_filters[f].extended, rules);
3088 if (rules->counter)
3089 cli_print(cli, "%s (%u match%s)", r,
3090 rules->counter, rules->counter > 1 ? "es" : "");
3091 else
3092 cli_print(cli, "%s", r);
3093 }
3094 }
3095
3096 return CLI_OK;
3097 }
3098
3099 static int cmd_shutdown(struct cli_def *cli, const char *command, char **argv, int argc)
3100 {
3101 if (CLI_HELP_REQUESTED)
3102 return CLI_HELP_NO_ARGS;
3103
3104 kill(getppid(), SIGQUIT);
3105 return CLI_OK;
3106 }
3107
3108 static int cmd_reload(struct cli_def *cli, const char *command, char **argv, int argc)
3109 {
3110 if (CLI_HELP_REQUESTED)
3111 return CLI_HELP_NO_ARGS;
3112
3113 kill(getppid(), SIGHUP);
3114 return CLI_OK;
3115 }
3116
3117 static int cmd_show_group(struct cli_def *cli, const char *command, char **argv, int argc)
3118 {
3119 int i;
3120 groupidt g;
3121
3122 if (CLI_HELP_REQUESTED)
3123 return cli_arg_help(cli, 1,
3124 "<1-%d>", MAXGROUPE-1, "Show specific group by id",
3125 NULL);
3126
3127 time(&time_now);
3128 if (argc > 0)
3129 {
3130 // Show individual group
3131 for (i = 0; i < argc; i++)
3132 {
3133 sessionidt s;
3134
3135 g = atoi(argv[i]);
3136 if (g <= 0 || g >= MAXGROUPE)
3137 {
3138 cli_print(cli, "Invalid group id \"%s\"", argv[i]);
3139 continue;
3140 }
3141
3142 cli_print(cli, "\r\nGroup %d:", g);
3143 cli_print(cli, "\tNb Session:\t\t%d", grpsession[g].nbsession);
3144 cli_print(cli, "\tNb Routes:\t\t%d", grpsession[g].nbroutesgrp);
3145 cli_print(cli, "\ttime_changed:\t\t%d\n", grpsession[g].time_changed);
3146
3147 for (i = 0; i < grpsession[g].nbsession; i++)
3148 {
3149 if ((s = grpsession[g].sesslist[i].sid))
3150 {
3151 cli_print(cli, "\tSession:\t%d\tTx Rate:%d Kbps\t\t\tweight:\t%d",
3152 s,
3153 grpsession[g].sesslist[i].tx_rate/(1024/8),
3154 grpsession[g].sesslist[i].weight);
3155 }
3156 }
3157
3158 for (i = 0; i < grpsession[g].nbroutesgrp; i++)
3159 {
3160 if (grpsession[g].route[i].ip != 0)
3161 {
3162 cli_print(cli, "\tRoute:\t%s/%d", fmtaddr(htonl(grpsession[g].route[i].ip), 0), grpsession[g].route[i].prefixlen);
3163 }
3164 }
3165 }
3166 return CLI_OK;
3167 }
3168
3169 // Show Summary
3170 cli_print(cli, "%5s %7s %9s %12s",
3171 "GID",
3172 "Nb Sess",
3173 "Nb Routes",
3174 "Time changed");
3175
3176 for (g = gnextgrpid; g != 0; g = grpsession[g].prev)
3177 {
3178 cli_print(cli, "%5d %7d %9d %12d",
3179 g,
3180 grpsession[g].nbsession,
3181 grpsession[g].nbroutesgrp,
3182 grpsession[g].time_changed);
3183 }
3184
3185 return CLI_OK;
3186 }
3187
3188 static int cmd_setforward(struct cli_def *cli, const char *command, char **argv, int argc)
3189 {
3190 int ret;
3191
3192 if (CLI_HELP_REQUESTED)
3193 {
3194 switch (argc)
3195 {
3196 case 1:
3197 return cli_arg_help(cli, 0,
3198 "MASK", "Users mask to forward (ex: myISP@operator.com)", NULL);
3199
3200 case 2:
3201 return cli_arg_help(cli, 0,
3202 "IP", "IP of the remote LNS(ex: 64.64.64.64)", NULL);
3203
3204 case 3:
3205 return cli_arg_help(cli, 0,
3206 "PORT", "Port of the remote LNS (ex: 1701)", NULL);
3207
3208 case 4:
3209 return cli_arg_help(cli, 0,
3210 "SECRET", "l2tp secret of the remote LNS (ex: mysecretpsw)", NULL);
3211
3212 default:
3213 return cli_arg_help(cli, argc > 1, NULL);
3214 }
3215 }
3216
3217 if (argc != 4)
3218 {
3219 cli_error(cli, "Specify variable and value");
3220 return CLI_OK;
3221 }
3222
3223 // lac_addremotelns(mask, IP_RemoteLNS, Port_RemoteLNS, SecretRemoteLNS)
3224 ret = lac_addremotelns(argv[0], argv[1], argv[2], argv[3]);
3225
3226 if (ret)
3227 {
3228 cli_print(cli, "setforward %s %s %s %s", argv[0], argv[1], argv[2], argv[3]);
3229 if (ret == 2)
3230 cli_print(cli, "%s Updated, the tunnel must be dropped", argv[0]);
3231 }
3232 else
3233 cli_error(cli, "ERROR setforward %s %s %s %s", argv[0], argv[1], argv[2], argv[3]);
3234
3235 return CLI_OK;
3236 }
3237
3238 static int cmd_show_rmtlnsconf(struct cli_def *cli, const char *command, char **argv, int argc)
3239 {
3240 confrlnsidt idrlns;
3241 char strdisp[1024];
3242
3243 if (CLI_HELP_REQUESTED)
3244 {
3245 return cli_arg_help(cli, 0, "remotelns-conf", "Show a list of remote LNS configurations", NULL);
3246 }
3247
3248 for (idrlns = 0; idrlns < MAXRLNSTUNNEL; idrlns++)
3249 {
3250 if (lac_cli_show_remotelns(idrlns, strdisp) != 0)
3251 cli_print(cli, "%s", strdisp);
3252 else
3253 break;
3254 }
3255
3256 return CLI_OK;
3257 }