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