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