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