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