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