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