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