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