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