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