1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
|
/*
* Copyright (c) 1996 Joerg Reuter (jreuter@poboxes.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
* 02110-1301, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <net/if.h>
#ifdef __GLIBC__
#include <net/ethernet.h>
#else
#include <linux/if_ether.h>
#endif
#include <net/if_arp.h>
#include <config.h>
#include <netax25/ax25.h>
#include <netrose/rose.h>
#include <netax25/axlib.h>
#include <netax25/axconfig.h>
#include "../pathnames.h"
#include "ax25rtd.h"
static ax25_address *asc2ax(char *call)
{
static ax25_address callsign;
ax25_aton_entry(call, (char *) &callsign);
return &callsign;
}
static long asc2ip(char *s)
{
struct in_addr addr;
if (!inet_aton(s, &addr))
return 0;
return addr.s_addr;
}
static char *prepare_cmdline(char *buf)
{
char *p;
for (p = buf; *p; p++) {
if (*p == '\t')
*p = ' ';
*p = tolower(*p);
if (*p == '\n') {
*p = '\0';
break;
}
if (*p == '#') {
*p = '\0';
break;
}
}
return buf;
}
static char *get_next_arg(char **p)
{
char *p2;
if (p == NULL || *p == NULL)
return NULL;
p2 = *p;
for (; *p2 && *p2 == ' '; p2++);
if (!*p2)
return NULL;
*p = strchr(p2, ' ');
if (*p != NULL) {
**p = '\0';
(*p)++;
}
return p2;
}
static inline void invalid_arg(char *c, char *p)
{
fprintf(stderr, "%s: invalid argument %s\n", c, p);
}
static inline void missing_arg(char *c)
{
fprintf(stderr, "%s: argument missing\n", c);
}
static int yesno(char *arg)
{
if (!arg)
return 0;
if (!strcmp(arg, "yes") || !strcmp(arg, "1"))
return 1;
if (!strcmp(arg, "no") || !strcmp(arg, "0"))
return 0;
return -1;
}
static ax25_address *get_mycall(char *port)
{
char *addr;
addr = ax25_config_get_addr(port);
if (addr == NULL)
return NULL;
return asc2ax(addr);
}
static void load_ports(void)
{
config *config, *cfg, *ncfg;
char buf[1024];
struct ifconf ifc;
struct ifreq ifr, *ifrp;
int k, fd;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
fprintf(stderr, "Unable to open socket\n");
exit(1);
}
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));
return;
}
ifrp = ifc.ifc_req;
for (k = ifc.ifc_len / sizeof(struct ifreq); k > 0; k--, ifrp++) {
strcpy(ifr.ifr_name, ifrp->ifr_name);
if (strcmp(ifr.ifr_name, "lo") == 0)
continue;
if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
fprintf(stderr, "SIOCGIFFLAGS: %s\n",
strerror(errno));
exit(1);
}
if (!(ifr.ifr_flags & IFF_UP))
continue;
ioctl(fd, SIOCGIFHWADDR, &ifr);
if (ifr.ifr_hwaddr.sa_family != ARPHRD_AX25)
continue;
for (config = Config; config; config = config->next)
if (!memcmp
(&config->mycalls[0], ifr.ifr_hwaddr.sa_data,
AXLEN) && !*config->dev) {
strcpy(config->dev, ifr.ifr_name);
ioctl(fd, SIOCGIFADDR, &ifr);
config->ip =
((struct sockaddr_in *) &ifr.
ifr_addr)->sin_addr.s_addr;
strcpy(ifr.ifr_name, config->dev);
ioctl(fd, SIOCGIFNETMASK, &ifr);
config->netmask =
((struct sockaddr_in *) &ifr.
ifr_netmask)->sin_addr.s_addr;
break;
}
}
close(fd);
config = cfg = Config;
while (config) {
if (!*config->dev) {
if (config == Config) {
Config = config->next;
cfg = Config;
} else
cfg->next = config->next;
ncfg = config->next;
free(config);
config = ncfg;
} else {
cfg = config;
config = config->next;
}
}
}
static void load_listeners(void)
{
config *config;
char buf[1024], device[14], call[10], dcall[10];
char dummy[1024];
int k;
FILE *fp;
ax25_address *axcall;
fp = fopen(PROC_AX25_FILE, "r");
if (fp == NULL) {
fprintf(stderr, "No AX.25 in kernel. Tss, tss...\n");
exit(1);
}
while (fgets(buf, sizeof(buf) - 1, fp) != NULL) {
k = sscanf(buf, "%s %s %s %s", dummy, device, call, dcall);
if (k == 4 && !strcmp(dcall, "*")) {
axcall = asc2ax(call);
if (!strcmp("*", device)) {
for (config = Config; config;
config = config->next) {
if (call_is_mycall(config, axcall)
|| config->nmycalls >
AX25_MAXCALLS)
continue;
memcpy(&config->
mycalls[config->nmycalls++],
axcall, AXLEN);
}
} else {
config = dev_get_config(device);
if (config == NULL
|| call_is_mycall(config, axcall)
|| config->nmycalls > AX25_MAXCALLS)
continue;
memcpy(&config->
mycalls[config->nmycalls++], axcall,
AXLEN);
}
}
}
fclose(fp);
}
void load_config(void)
{
FILE *fp;
char buf[1024], *p, *cmd, *arg;
config *config, *cfg;
ax25_address *axcall;
config = NULL;
fp = fopen(CONF_AX25ROUTED_FILE, "r");
if (fp == NULL) {
fprintf(stderr, "config file %s not found\n",
CONF_AX25ROUTED_FILE);
exit(1);
}
while (fgets(buf, sizeof(buf) - 1, fp) != NULL) {
p = prepare_cmdline(buf);
if (!*p)
continue;
cmd = get_next_arg(&p);
if (cmd == NULL)
continue;
arg = get_next_arg(&p);
if (*cmd == '[') {
cmd++;
p = strrchr(cmd, ']');
if (p == NULL) {
fprintf(stderr, "syntax error: [%s\n",
cmd);
continue;
}
*p = '\0';
axcall = get_mycall(cmd);
if (axcall == NULL)
continue;
cfg =
(struct config_ *)
malloc(sizeof(struct config_));
if (cfg == NULL) {
fprintf(stderr, "out of memory\n");
exit(1);
}
memset(cfg, 0, sizeof(struct config_));
if (config)
config->next = cfg;
else
Config = cfg;
cfg->next = NULL;
config = cfg;
strcpy(config->port, cmd);
memcpy(&config->mycalls[0], axcall, AXLEN);
config->nmycalls = 1;
} else if (config && !strcmp(cmd, "ax25-learn-routes")) {
/* ax25-learn-routes no|yes: learn digipeater path */
if (arg) {
int k = yesno(arg);
if (k == -1) {
invalid_arg(cmd, arg);
continue;
} else {
config->ax25_add_route = k;
}
} else
missing_arg(cmd);
} else if (config && !strcmp(cmd, "ax25-learn-only-mine")) {
/* ax25-learn-only-mine no|yes: learn only if addressed to me */
if (arg) {
int k = yesno(arg);
if (k == -1) {
invalid_arg(cmd, arg);
continue;
} else {
config->ax25_for_me = k;
}
} else
missing_arg(cmd);
} else if (config && !strcmp(cmd, "ax25-add-path")) {
/* ax25-add-path [no|<digis>]: add digis if digi-less frame rvd */
int k = 0;
if (!arg || (arg && yesno(arg) == 0))
continue;
config->ax25_add_default = 1;
while (arg && k < AX25_MAX_DIGIS) {
memcpy(&config->ax25_default_path.
fsa_digipeater[k], asc2ax(arg),
AXLEN);
arg = get_next_arg(&p);
k++;
}
config->ax25_default_path.fsa_ax25.sax25_ndigis =
k;
} else if (config && !strcmp(cmd, "ax25-more-mycalls")) {
/* ax25-more-mycalls call1 call2: frames to this calls are for "me", too. */
if (arg) {
while (arg
&& config->nmycalls <
AX25_MAXCALLS) {
axcall = asc2ax(arg);
if (call_is_mycall(config, axcall)) {
arg = get_next_arg(&p);
continue;
}
memcpy(&config->
mycalls[config->nmycalls++],
axcall, AXLEN);
arg = get_next_arg(&p);
}
} else
missing_arg(cmd);
} else if (config && !strcmp(cmd, "ip-learn-routes")) {
/* ip-learn-routes no|yes: learn ip routes */
if (arg) {
int k = yesno(arg);
if (k == -1) {
invalid_arg(cmd, arg);
continue;
} else {
config->ip_add_route = k;
}
} else
missing_arg(cmd);
} else if (config && !strcmp(cmd, "irtt")) {
/* irtt <irtt>: new routes will get this IRTT in msec */
if (arg) {
int k = atoi(arg);
if (k == 0) {
invalid_arg(cmd, arg);
continue;
} else {
config->tcp_irtt = k;
}
} else
missing_arg(cmd);
} else if (config && !strcmp(cmd, "dg-mtu")) {
/* dg-mtu <mtu>: MTU for datagram mode routes (unused) */
if (arg) {
int k = atoi(arg);
if (k == 0) {
invalid_arg(cmd, arg);
continue;
} else {
config->dg_mtu = k;
}
} else
missing_arg(cmd);
} else if (config && !strcmp(cmd, "vc-mtu")) {
/* vc-mtu <mtu>: MTU for virtual connect mode routes (unused) */
if (arg) {
int k = atoi(arg);
if (k == 0) {
invalid_arg(cmd, arg);
continue;
} else {
config->vc_mtu = k;
}
} else
missing_arg(cmd);
} else if (config && !strcmp(cmd, "ip-adjust-mode")) {
/* ip-adjust-mode no|yes: adjust ip-mode? (dg or vc) */
if (arg) {
int k = yesno(arg);
if (k == -1) {
invalid_arg(cmd, arg);
continue;
} else {
config->ip_adjust_mode = k;
}
} else
missing_arg(cmd);
} else if (config && !strcmp(cmd, "arp-add")) {
/* arp-add no|yes: adjust arp table? */
if (arg) {
int k = yesno(arg);
if (k == -1) {
invalid_arg(cmd, arg);
continue;
} else {
config->ip_add_arp = k;
}
} else
missing_arg(cmd);
} else if (!strcmp(cmd, "ip-encaps-dev")) {
if (arg)
strcpy(ip_encaps_dev, arg);
else
missing_arg(cmd);
} else if (!strcmp(cmd, "ax25-maxroutes")) {
if (arg)
ax25_maxroutes = atoi(arg);
else
missing_arg(cmd);
} else if (!strcmp(cmd, "ip-maxroutes")) {
if (arg)
ip_maxroutes = atoi(arg);
else
missing_arg(cmd);
} else if (!strcmp(cmd, "iproute2-table")) {
if (arg)
strcpy(iproute2_table, arg);
else
missing_arg(cmd);
} else
fprintf(stderr, "invalid command %s\n", cmd);
}
fclose(fp);
load_ports();
load_listeners();
reload = 0;
}
void reload_config(void)
{
config *cfg, *config = Config;
while (config) {
cfg = config->next;
free(config);
config = cfg;
}
Config = NULL;
load_config();
}
/* commands:
---------
add ax25 <callsign> <dev> <time> [<digipeater>] # Add an AX.25 route
add ip <ip> <dev> <time> <call> <mode> # Add an IP route & mode
del ax25 <callsign> <dev> # Remove an AX.25 route (from cache)
del ip <ip> # Remove an IP route (from cache)
list [ax25|ip] # List cache entries
reload # Reload config
save # Save cache
expire <minutes> # Expire cache entries
shutdown # Save cache and exit
There's a difference between 'list heard' and 'heard':
The 'list' commands will output the symbolic port names as defined in
/usr/local/etc/axports (i.e. 9k6 for scc3), while 'heard' shows the
'real' network device name (i.e. scc3). All commands accept either the
port or the network device name. The expample
add ax25 dl0tha scc3 0 db0pra
is equivalent to
add ax25 dl0tha 9k6 0 dbpra
Note that in conflicting cases the network device name has precedence
over the port name.
*/
void interpret_command(int fd, char *buf)
{
char *p, *cmd, *arg, *arg2, *dev, *time;
ax25_address digipeater[AX25_MAX_DIGIS];
ax25_rt_entry *ax25rt;
int ndigi, ipmode, action;
time_t stamp;
config *config;
long ip;
p = prepare_cmdline(buf);
if (!*p)
return;
cmd = get_next_arg(&p);
arg = get_next_arg(&p);
if (!strcmp(cmd, "add")) {
if (arg == NULL)
return;
arg2 = get_next_arg(&p);
if (arg2 == NULL)
return;
dev = get_next_arg(&p);
if (dev == NULL)
return;
time = get_next_arg(&p);
if (time == NULL)
return;
config = dev_get_config(dev);
if (config == NULL)
return;
sscanf(time, "%lx", &stamp);
if (!strcmp(arg, "ax25")) {
ndigi = 0;
arg = get_next_arg(&p);
while (arg != NULL) {
memcpy(&digipeater[ndigi++], asc2ax(arg),
AXLEN);
if (ndigi == AX25_MAX_DIGIS)
break;
arg = get_next_arg(&p);
}
ax25rt =
update_ax25_route(config, asc2ax(arg2), ndigi,
digipeater, stamp);
if (ax25rt != NULL)
set_ax25_route(config, ax25rt);
} else if (!strcmp(arg, "ip")) {
ip = asc2ip(arg2);
arg2 = get_next_arg(&p);
if (arg2 == NULL)
return;
arg = get_next_arg(&p);
if (arg == NULL)
return;
if (*arg == 'x')
return;
ipmode = (*arg == 'v');
if (*ip_encaps_dev && (config =
dev_get_config(ip_encaps_dev)) == NULL) {
printf("no config for %s\n",
ip_encaps_dev);
return;
}
action =
update_ip_route(config, ip, ipmode,
asc2ax(arg2), stamp);
if (action & NEW_ROUTE)
if (set_route(config, ip))
return;
if (action & NEW_ARP)
if (set_arp(config, ip, asc2ax(arg2)))
return;
if (action & NEW_IPMODE)
if (set_ipmode
(config, asc2ax(arg2), ipmode))
return;
}
} else if (!strcmp(cmd, "del")) {
if (arg == NULL)
return;
arg2 = get_next_arg(&p);
if (arg2 == NULL)
return;
if (!strcmp(arg, "ax25")) {
arg = get_next_arg(&p);
if (arg == NULL
|| (config = dev_get_config(arg)) == NULL)
return;
del_ax25_route(config, asc2ax(arg2));
} else if (!strcmp(arg, "ip")) {
del_ip_route(asc2ip(arg2));
}
} else if (!strcmp(cmd, "expire")) {
if (arg == NULL)
return;
sscanf(arg, "%ld", &stamp);
if (stamp != 0) {
stamp *= 60;
expire_ax25_route(stamp);
expire_ip_route(stamp);
}
} else if (!strcmp(cmd, "reload")) {
reload_config();
} else if (!strcmp(cmd, "list")) {
if (arg == NULL)
return;
if (!strcmp(arg, "ax25"))
dump_ax25_routes(fd, 0);
else if (!strcmp(arg, "ip"))
dump_ip_routes(fd, 0);
} else if (!strcmp(cmd, "shutdown")) {
save_cache();
daemon_shutdown(0);
} else if (!strcmp(cmd, "save")) {
save_cache();
} else if (!strcmp(cmd, "version")) {
char buf[] = "ax25rtd version " VERSION "\n";
write(fd, buf, strlen(buf));
} else if (!strcmp(cmd, "quit")) {
close(fd);
}
}
void load_cache(void)
{
FILE *fp;
char buf[512];
fp = fopen(DATA_AX25ROUTED_AXRT_FILE, "r");
if (fp != NULL) {
while (fgets(buf, sizeof(buf), fp) != NULL)
interpret_command(2, buf);
fclose(fp);
} else
perror("open AX.25 route cache file");
fp = fopen(DATA_AX25ROUTED_IPRT_FILE, "r");
if (fp != NULL) {
while (fgets(buf, sizeof(buf), fp) != NULL)
interpret_command(2, buf);
fclose(fp);
} else
perror("open IP route cache file");
}
void save_cache(void)
{
int fd;
fd = creat(DATA_AX25ROUTED_AXRT_FILE, 0664);
dump_ax25_routes(fd, 1);
close(fd);
fd = creat(DATA_AX25ROUTED_IPRT_FILE, 0664);
dump_ip_routes(fd, 1);
close(fd);
}
|