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