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