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