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