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