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