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