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