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