| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
#include <config.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <string.h> |
| 30 |
#include <unistd.h> |
| 31 |
#include <signal.h> |
| 32 |
#include <sys/ioctl.h> |
| 33 |
#ifdef TIME_WITH_SYS_TIME |
| 34 |
#include <time.h> |
| 35 |
#endif |
| 36 |
#include <sys/time.h> |
| 37 |
#include <netinet/in.h> |
| 38 |
#include <net/route.h> |
| 39 |
#include <net/if.h> |
| 40 |
#include <net/if_arp.h> |
| 41 |
|
| 42 |
#include <netax25/ax25.h> |
| 43 |
|
| 44 |
#include "../pathnames.h" |
| 45 |
#include "ax25rtd.h" |
| 46 |
|
| 47 |
#include <stdlib.h> |
| 48 |
|
| 49 |
|
| 50 |
static unsigned long get_from_arp(unsigned char *data, int size) |
| 51 |
{ |
| 52 |
if (size < 16) |
| 53 |
return 0; |
| 54 |
|
| 55 |
return ntohl(0); |
| 56 |
} |
| 57 |
|
| 58 |
static unsigned long get_from_ip(unsigned char *data, int size) |
| 59 |
{ |
| 60 |
unsigned long adr; |
| 61 |
|
| 62 |
if ((*data & 0xf) * 4 < IPLEN) |
| 63 |
return 0; |
| 64 |
|
| 65 |
adr = data[12] << 24; |
| 66 |
adr += data[13] << 16; |
| 67 |
adr += data[14] << 8; |
| 68 |
adr += data[15]; |
| 69 |
|
| 70 |
return ntohl(adr); |
| 71 |
} |
| 72 |
|
| 73 |
int call_is_mycall(config * config, ax25_address * call) |
| 74 |
{ |
| 75 |
int k; |
| 76 |
|
| 77 |
for (k = 0; k < config->nmycalls; k++) |
| 78 |
if (!memcmp(call, &config->mycalls[k], AXLEN)) |
| 79 |
return 1; |
| 80 |
return 0; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
static int check_ax25_addr(unsigned char *buf) |
| 88 |
{ |
| 89 |
int k; |
| 90 |
unsigned char c; |
| 91 |
|
| 92 |
|
| 93 |
for (k = 0; k < 6; k++) { |
| 94 |
c = buf[k] >> 1; |
| 95 |
if (c == ' ') |
| 96 |
break; |
| 97 |
if ((c < 'A' || c > 'Z') && (c < '0' || c > '9')) |
| 98 |
return 1; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
if (k == 0) |
| 103 |
return 1; |
| 104 |
|
| 105 |
|
| 106 |
for (k++; k < 6; k++) |
| 107 |
if (buf[k] >> 1 != ' ') |
| 108 |
return 1; |
| 109 |
|
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
static inline void invert_digipeater_path(ax25_address * digipeater, |
| 114 |
int ndigi) |
| 115 |
{ |
| 116 |
int k, m; |
| 117 |
ax25_address fdigi; |
| 118 |
|
| 119 |
if (ndigi == 0) |
| 120 |
return; |
| 121 |
|
| 122 |
for (m = 0, k = ndigi - 1; k > m; k--, m++) { |
| 123 |
memcpy(&fdigi, &digipeater[m], AXLEN); |
| 124 |
memcpy(&digipeater[m], &digipeater[k], AXLEN); |
| 125 |
memcpy(&digipeater[k], &fdigi, AXLEN); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
int set_arp(config * config, long ip, ax25_address * call) |
| 130 |
{ |
| 131 |
struct sockaddr_in *isa; |
| 132 |
struct sockaddr_ax25 *asa; |
| 133 |
struct arpreq arp; |
| 134 |
int fds; |
| 135 |
|
| 136 |
if (!config->ip_add_arp) |
| 137 |
return 0; |
| 138 |
|
| 139 |
fds = socket(AF_INET, SOCK_DGRAM, 0); |
| 140 |
|
| 141 |
memset(&arp, 0, sizeof(arp)); |
| 142 |
|
| 143 |
isa = (struct sockaddr_in *) &arp.arp_pa; |
| 144 |
asa = (struct sockaddr_ax25 *) &arp.arp_ha; |
| 145 |
|
| 146 |
isa->sin_family = AF_INET; |
| 147 |
isa->sin_port = 0; |
| 148 |
isa->sin_addr.s_addr = ip; |
| 149 |
|
| 150 |
asa->sax25_family = AF_AX25; |
| 151 |
asa->sax25_ndigis = 0; |
| 152 |
asa->sax25_call = *call; |
| 153 |
|
| 154 |
arp.arp_flags = ATF_PERM | ATF_COM; |
| 155 |
strcpy(arp.arp_dev, config->dev); |
| 156 |
|
| 157 |
if (ioctl(fds, SIOCSARP, &arp) < 0) { |
| 158 |
invalidate_ip_route(ip); |
| 159 |
perror("routspy: SIOCSARP"); |
| 160 |
close(fds); |
| 161 |
return 1; |
| 162 |
} |
| 163 |
close(fds); |
| 164 |
return 0; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
#define RT_DEL 0 |
| 171 |
#define RT_ADD 1 |
| 172 |
static int iproute2(long ip, char *dev, int what) |
| 173 |
{ |
| 174 |
char buffer[256]; |
| 175 |
char ipa[32]; |
| 176 |
int ret; |
| 177 |
|
| 178 |
sprintf(ipa, "%d.%d.%d.%d", |
| 179 |
(int) (ip & 0x000000FF), |
| 180 |
(int) ((ip & 0x0000FF00) >> 8), |
| 181 |
(int) ((ip & 0x00FF0000) >> 16), |
| 182 |
(int) ((ip & 0xFF000000) >> 24)); |
| 183 |
|
| 184 |
|
| 185 |
sprintf(buffer, "/sbin/ip route %s %s dev %s table %s proto ax25rtd", (what ? "add" : "del"), ipa, dev, iproute2_table); |
| 186 |
|
| 187 |
ret = system(buffer); |
| 188 |
return ret; |
| 189 |
} |
| 190 |
|
| 191 |
int set_route(config * config, long ip) |
| 192 |
{ |
| 193 |
struct rtentry rt; |
| 194 |
struct sockaddr_in *isa; |
| 195 |
char origdev[16], buf[1024]; |
| 196 |
|
| 197 |
long gwr; |
| 198 |
|
| 199 |
long ipr; |
| 200 |
int fds; |
| 201 |
FILE *fp; |
| 202 |
|
| 203 |
fp = fopen(PROC_IP_ROUTE_FILE, "r"); |
| 204 |
if (fp == NULL) { |
| 205 |
invalidate_ip_route(ip); |
| 206 |
return 1; |
| 207 |
} |
| 208 |
|
| 209 |
fgets(buf, sizeof(buf) - 1, fp); |
| 210 |
while (fgets(buf, sizeof(buf) - 1, fp) != NULL) { |
| 211 |
|
| 212 |
|
| 213 |
sscanf(buf, "%s %lx %lx", origdev, &ipr, &gwr); |
| 214 |
if (ipr == ip && gwr == 00000000) |
| 215 |
|
| 216 |
{ |
| 217 |
if (dev_get_config(origdev) == NULL) { |
| 218 |
invalidate_ip_route(ip); |
| 219 |
fclose(fp); |
| 220 |
return 1; |
| 221 |
} else { |
| 222 |
del_kernel_ip_route(origdev, ip); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
} |
| 227 |
fclose(fp); |
| 228 |
|
| 229 |
if (!config->ip_add_route) |
| 230 |
return 0; |
| 231 |
|
| 232 |
if (*iproute2_table) |
| 233 |
return iproute2(ip, config->dev, RT_ADD); |
| 234 |
|
| 235 |
fds = socket(AF_INET, SOCK_DGRAM, 0); |
| 236 |
|
| 237 |
memset(&rt, 0, sizeof(rt)); |
| 238 |
|
| 239 |
isa = (struct sockaddr_in *) &rt.rt_dst; |
| 240 |
|
| 241 |
isa->sin_family = AF_INET; |
| 242 |
isa->sin_port = 0; |
| 243 |
isa->sin_addr.s_addr = ip; |
| 244 |
|
| 245 |
rt.rt_flags = RTF_UP | RTF_HOST; |
| 246 |
rt.rt_dev = config->dev; |
| 247 |
|
| 248 |
if (config->tcp_irtt != 0) { |
| 249 |
rt.rt_irtt = config->tcp_irtt; |
| 250 |
rt.rt_flags |= RTF_IRTT; |
| 251 |
} |
| 252 |
|
| 253 |
isa = (struct sockaddr_in *) &rt.rt_genmask; |
| 254 |
isa->sin_addr.s_addr = 0xffffffff; |
| 255 |
|
| 256 |
if (ioctl(fds, SIOCADDRT, &rt) < 0) { |
| 257 |
invalidate_ip_route(ip); |
| 258 |
perror("ax25rtd: IP SIOCADDRT"); |
| 259 |
close(fds); |
| 260 |
return 1; |
| 261 |
} |
| 262 |
close(fds); |
| 263 |
|
| 264 |
return 0; |
| 265 |
} |
| 266 |
|
| 267 |
int del_kernel_ip_route(char *dev, long ip) |
| 268 |
{ |
| 269 |
int fds; |
| 270 |
struct rtentry rt; |
| 271 |
struct sockaddr_in *isa; |
| 272 |
config *config; |
| 273 |
|
| 274 |
config = dev_get_config(dev); |
| 275 |
if (config == NULL || !config->ip_add_route) |
| 276 |
return 0; |
| 277 |
|
| 278 |
if (*iproute2_table) |
| 279 |
return iproute2(ip, dev, RT_DEL); |
| 280 |
|
| 281 |
fds = socket(AF_INET, SOCK_DGRAM, 0); |
| 282 |
|
| 283 |
memset(&rt, 0, sizeof(struct rtentry)); |
| 284 |
|
| 285 |
isa = (struct sockaddr_in *) &rt.rt_dst; |
| 286 |
|
| 287 |
isa->sin_family = AF_INET; |
| 288 |
isa->sin_addr.s_addr = ip; |
| 289 |
|
| 290 |
rt.rt_flags = RTF_UP | RTF_HOST; |
| 291 |
rt.rt_dev = dev; |
| 292 |
|
| 293 |
if (ioctl(fds, SIOCDELRT, &rt) < 0) { |
| 294 |
perror("ax25rtd: IP SIOCDELRT"); |
| 295 |
close(fds); |
| 296 |
return 1; |
| 297 |
} |
| 298 |
close(fds); |
| 299 |
|
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
int set_ax25_route(config * config, ax25_rt_entry * rt) |
| 304 |
{ |
| 305 |
struct ax25_routes_struct ax25_route; |
| 306 |
int fds, k; |
| 307 |
|
| 308 |
if (!config->ax25_add_route) |
| 309 |
return 0; |
| 310 |
|
| 311 |
ax25_route.port_addr = config->mycalls[0]; |
| 312 |
ax25_route.dest_addr = rt->call; |
| 313 |
ax25_route.digi_count = rt->ndigi; |
| 314 |
|
| 315 |
for (k = 0; k < rt->ndigi; k++) |
| 316 |
ax25_route.digi_addr[k] = rt->digipeater[k]; |
| 317 |
|
| 318 |
fds = socket(AF_AX25, SOCK_SEQPACKET, 0); |
| 319 |
|
| 320 |
if (ioctl(fds, SIOCADDRT, &ax25_route) < 0) { |
| 321 |
perror("ax25rtd: AX.25 SIOCADDRT"); |
| 322 |
close(fds); |
| 323 |
return 1; |
| 324 |
} |
| 325 |
|
| 326 |
close(fds); |
| 327 |
return 0; |
| 328 |
} |
| 329 |
|
| 330 |
int del_kernel_ax25_route(char *dev, ax25_address * call) |
| 331 |
{ |
| 332 |
struct ax25_routes_struct ax25_route; |
| 333 |
int fds; |
| 334 |
config *config; |
| 335 |
|
| 336 |
config = dev_get_config(dev); |
| 337 |
if (config == NULL || !config->ax25_add_route) |
| 338 |
return 0; |
| 339 |
|
| 340 |
ax25_route.port_addr = config->mycalls[0]; |
| 341 |
ax25_route.dest_addr = *call; |
| 342 |
|
| 343 |
fds = socket(AF_AX25, SOCK_SEQPACKET, 0); |
| 344 |
|
| 345 |
if (ioctl(fds, SIOCDELRT, &ax25_route) < 0) { |
| 346 |
perror("ax25rtd: AX.25 SIOCDELRT"); |
| 347 |
close(fds); |
| 348 |
return 1; |
| 349 |
} |
| 350 |
|
| 351 |
close(fds); |
| 352 |
return 0; |
| 353 |
} |
| 354 |
|
| 355 |
int set_ipmode(config * config, ax25_address * call, int ipmode) |
| 356 |
{ |
| 357 |
struct ax25_route_opt_struct ax25_opt; |
| 358 |
int fds; |
| 359 |
|
| 360 |
if (!config->ip_adjust_mode) |
| 361 |
return 0; |
| 362 |
|
| 363 |
ax25_opt.port_addr = config->mycalls[0]; |
| 364 |
ax25_opt.dest_addr = *call; |
| 365 |
ax25_opt.cmd = AX25_SET_RT_IPMODE; |
| 366 |
ax25_opt.arg = ipmode ? 'V' : 'D'; |
| 367 |
|
| 368 |
fds = socket(AF_AX25, SOCK_SEQPACKET, 0); |
| 369 |
|
| 370 |
if (ioctl(fds, SIOCAX25OPTRT, &ax25_opt) < 0) { |
| 371 |
perror("ax25rtd: SIOCAX25OPTRT"); |
| 372 |
close(fds); |
| 373 |
return 1; |
| 374 |
} |
| 375 |
|
| 376 |
close(fds); |
| 377 |
return 0; |
| 378 |
|
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
#define SKIP(o) {data+=(o); size-=(o);} |
| 384 |
void ax25_receive(int sock) |
| 385 |
{ |
| 386 |
unsigned char buf[1500]; |
| 387 |
unsigned char *data; |
| 388 |
unsigned long ip; |
| 389 |
struct sockaddr sa; |
| 390 |
ax25_address srccall, destcall, digipeater[AX25_MAX_DIGIS]; |
| 391 |
char extseq = 0; |
| 392 |
int size, action, ipmode, ctl, pid, ndigi, kdigi, mine; |
| 393 |
time_t stamp; |
| 394 |
config *config; |
| 395 |
ax25_rt_entry *ax25rt; |
| 396 |
socklen_t asize; |
| 397 |
|
| 398 |
asize = sizeof(sa); |
| 399 |
size = recvfrom(sock, buf, sizeof(buf), 0, &sa, &asize); |
| 400 |
if (size < 0) { |
| 401 |
perror("recvfrom"); |
| 402 |
save_cache(); |
| 403 |
daemon_shutdown(1); |
| 404 |
} |
| 405 |
|
| 406 |
stamp = time(NULL); |
| 407 |
ip = 0; |
| 408 |
pid = ctl = 0; |
| 409 |
|
| 410 |
config = dev_get_config(sa.sa_data); |
| 411 |
|
| 412 |
if (config == NULL) |
| 413 |
return; |
| 414 |
|
| 415 |
data = buf; |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
if ((*data & 0x0f) != 0) |
| 422 |
return; |
| 423 |
|
| 424 |
SKIP(1); |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
if (size < (2 * AXLEN + 1)) |
| 429 |
return; |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
if (check_ax25_addr(data)) |
| 436 |
return; |
| 437 |
|
| 438 |
memcpy(&destcall, data, AXLEN); |
| 439 |
destcall.ax25_call[6] &= 0x1e; |
| 440 |
SKIP(AXLEN); |
| 441 |
|
| 442 |
mine = call_is_mycall(config, &destcall); |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
if (check_ax25_addr(data)) |
| 449 |
return; |
| 450 |
|
| 451 |
memcpy(&srccall, data, AXLEN); |
| 452 |
srccall.ax25_call[6] &= 0x1e; |
| 453 |
SKIP(ALEN); |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
extseq = ~(*data) & SSSID_SPARE; |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
ndigi = 0; |
| 466 |
while (((*data) & HDLCAEB) != HDLCAEB) { |
| 467 |
SKIP(1); |
| 468 |
if (size <= 0 || check_ax25_addr(data)) |
| 469 |
return; |
| 470 |
|
| 471 |
if (ndigi < AX25_MAX_DIGIS) |
| 472 |
memcpy(&digipeater[ndigi++], data, AXLEN); |
| 473 |
else |
| 474 |
return; |
| 475 |
|
| 476 |
SKIP(ALEN); |
| 477 |
} |
| 478 |
|
| 479 |
SKIP(1); |
| 480 |
if (size <= 0) |
| 481 |
return; |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
if ((*data & LAPB_S) == LAPB_I) |
| 488 |
ctl = LAPB_I; |
| 489 |
else { |
| 490 |
ctl = *data; |
| 491 |
if (extseq == 0) |
| 492 |
ctl &= ~LAPB_PF; |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
if (ctl == LAPB_I || ctl == LAPB_UI) { |
| 500 |
SKIP(extseq ? 2 : 1); |
| 501 |
if (size <= 0) |
| 502 |
return; |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
pid = *data; |
| 507 |
|
| 508 |
if (pid == PID_SEGMENT) { |
| 509 |
SKIP(1); |
| 510 |
if (size <= 0) |
| 511 |
return; |
| 512 |
pid = 0; |
| 513 |
|
| 514 |
if (*data && SEG_FIRST) { |
| 515 |
pid = *data; |
| 516 |
SKIP(1); |
| 517 |
if (size <= 0) |
| 518 |
return; |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
for (kdigi = 0; kdigi < ndigi; kdigi++) { |
| 528 |
if ((digipeater[kdigi].ax25_call[6] & AX25_REPEATED) != |
| 529 |
AX25_REPEATED) |
| 530 |
return; |
| 531 |
|
| 532 |
digipeater[kdigi].ax25_call[6] &= 0x1e; |
| 533 |
} |
| 534 |
|
| 535 |
invert_digipeater_path(digipeater, ndigi); |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
if (mine || !config->ax25_for_me) { |
| 542 |
if (!mine && ndigi == 0 && config->ax25_add_default) { |
| 543 |
ndigi = |
| 544 |
config->ax25_default_path.fsa_ax25. |
| 545 |
sax25_ndigis; |
| 546 |
for (kdigi = 0; kdigi < ndigi; kdigi++) |
| 547 |
if (!memcmp |
| 548 |
(&srccall, |
| 549 |
&config->ax25_default_path. |
| 550 |
fsa_digipeater[kdigi], AXLEN)) |
| 551 |
break; |
| 552 |
|
| 553 |
if (ndigi == kdigi) |
| 554 |
memcpy(digipeater, |
| 555 |
config->ax25_default_path. |
| 556 |
fsa_digipeater, ndigi * AXLEN); |
| 557 |
else |
| 558 |
ndigi = 0; |
| 559 |
} |
| 560 |
|
| 561 |
ax25rt = |
| 562 |
update_ax25_route(config, &srccall, ndigi, digipeater, |
| 563 |
stamp); |
| 564 |
|
| 565 |
if (ax25rt != NULL) |
| 566 |
set_ax25_route(config, ax25rt); |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
switch (pid) { |
| 574 |
case PID_ARP: |
| 575 |
SKIP(1); |
| 576 |
if (size > 0) |
| 577 |
ip = get_from_arp(data, size); |
| 578 |
break; |
| 579 |
case PID_IP: |
| 580 |
if (!mine) |
| 581 |
return; |
| 582 |
|
| 583 |
SKIP(1); |
| 584 |
if (size > 0) |
| 585 |
ip = get_from_ip(data, size); |
| 586 |
break; |
| 587 |
default: |
| 588 |
return; |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
ipmode = (ctl == LAPB_I); |
| 596 |
|
| 597 |
if (ip != 0) { |
| 598 |
if (*ip_encaps_dev && (config = dev_get_config(ip_encaps_dev)) == NULL) |
| 599 |
return; |
| 600 |
|
| 601 |
action = |
| 602 |
update_ip_route(config, ip, ipmode, &srccall, stamp); |
| 603 |
|
| 604 |
if (action & NEW_ROUTE) |
| 605 |
if (set_route(config, ip)) |
| 606 |
return; |
| 607 |
|
| 608 |
if (action & NEW_ARP) |
| 609 |
if (set_arp(config, ip, &srccall)) |
| 610 |
return; |
| 611 |
|
| 612 |
if (action & NEW_IPMODE) |
| 613 |
if (set_ipmode(config, &srccall, ipmode)) |
| 614 |
return; |
| 615 |
} |
| 616 |
} |