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