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