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