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