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