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