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