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