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