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