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 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
|
/*
* $Id: rtmp.c,v 1.12.8.1 2004/05/12 21:21:48 didg Exp $
*
* Copyright (c) 1990,1993 Regents of The University of Michigan.
* All Rights Reserved. See COPYRIGHT.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <atalk/logger.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#ifdef TRU64
#include <sys/mbuf.h>
#include <net/route.h>
#endif /* TRU64 */
#include <net/if.h>
#include <net/route.h>
#include <netatalk/endian.h>
#include <netatalk/at.h>
#ifdef __svr4__
#include <sys/sockio.h>
#endif /* __svr4__ */
#include <atalk/ddp.h>
#include <atalk/atp.h>
#include <atalk/rtmp.h>
#include "interface.h"
#include "gate.h"
#include "rtmp.h"
#include "zip.h"
#include "list.h"
#include "atserv.h"
#include "route.h"
#include "main.h"
void rtmp_delzonemap(rtmp)
struct rtmptab *rtmp;
{
struct list *lz, *flz, *lr, *flr;
struct ziptab *zt;
lz = rtmp->rt_zt;
while ( lz ) { /* for each zone */
zt = (struct ziptab *)lz->l_data;
lr = zt->zt_rt;
while ( lr ) { /* for each route */
if ( (struct rtmptab *)lr->l_data == rtmp ) {
if ( lr->l_prev == NULL ) { /* head */
if ( lr->l_next == NULL ) { /* last route in zone */
if ( zt->zt_prev == NULL ) {
ziptab = zt->zt_next;
} else {
zt->zt_prev->zt_next = zt->zt_next;
}
if ( zt->zt_next == NULL ) {
ziplast = zt->zt_prev;
} else {
zt->zt_next->zt_prev = zt->zt_prev;
}
free( zt->zt_bcast );
free( zt->zt_name );
free( zt );
} else {
zt->zt_rt = lr->l_next;
}
} else {
lr->l_prev->l_next = lr->l_next;
}
if ( lr->l_next != NULL ) {
lr->l_next->l_prev = lr->l_prev;
}
flr = lr;
lr = lr->l_next;
free( flr );
} else {
lr = lr->l_next;
}
}
flz = lz;
lz = lz->l_next;
free( flz );
}
rtmp->rt_zt = NULL;
}
/*
* Complete configuration for phase 1 interface using RTMP information.
*/
static int rtmp_config( rh, iface )
struct rtmp_head *rh;
struct interface *iface;
{
extern int stabletimer;
int cc;
/*
* If we're configuring a phase 2 interface, don't complete
* configuration with RTMP.
*/
if ( iface->i_flags & IFACE_PHASE2 ) {
LOG(log_info, logtype_atalkd, "rtmp_config ignoring data" );
return 0;
}
/*
* Check our seed information, and reconfigure.
*/
if ( rh->rh_net != iface->i_addr.sat_addr.s_net ) {
if (( iface->i_flags & IFACE_SEED ) &&
rh->rh_net != iface->i_caddr.sat_addr.s_net) {
LOG(log_error, logtype_atalkd, "rtmp_config net mismatch %u != %u",
ntohs( rh->rh_net ),
ntohs( iface->i_addr.sat_addr.s_net ));
return 1;
}
iface->i_addr.sat_addr.s_net = rh->rh_net;
/*
* It is possible that we will corrupt our route database
* by just forcing this change. XXX
*/
iface->i_rt->rt_firstnet = iface->i_rt->rt_lastnet = rh->rh_net;
setaddr( iface, IFACE_PHASE1, iface->i_addr.sat_addr.s_net,
iface->i_addr.sat_addr.s_node, rh->rh_net, rh->rh_net );
stabletimer = UNSTABLE;
}
/* add addr to loopback route */
if ((cc = looproute( iface, RTMP_ADD )) < 0 )
return -1;
if (cc) {
LOG(log_error, logtype_atalkd, "rtmp_config: can't route %u.%u to loopback: %s",
ntohs( iface->i_addr.sat_addr.s_net ),
iface->i_addr.sat_addr.s_node,
strerror(errno) );
}
LOG(log_info, logtype_atalkd, "rtmp_config configured %s", iface->i_name );
iface->i_flags |= IFACE_CONFIG;
if ( iface == ciface ) {
ciface = ciface->i_next;
bootaddr( ciface );
}
return 0;
}
/*
* Delete rtmp from the per-interface in-use table, remove all
* zone references, and remove the route from the kernel.
*/
static void rtmp_delinuse( rtmp )
struct rtmptab *rtmp;
{
struct rtmptab *irt;
irt = rtmp->rt_gate->g_iface->i_rt;
if ( irt->rt_inext == rtmp ) { /* first */
if ( rtmp->rt_iprev == rtmp ) { /* only */
irt->rt_inext = NULL;
} else {
irt->rt_inext = rtmp->rt_inext;
rtmp->rt_inext->rt_iprev = rtmp->rt_iprev;
}
} else {
if ( rtmp->rt_inext == NULL ) { /* last */
rtmp->rt_iprev->rt_inext = NULL;
irt->rt_inext->rt_iprev = rtmp->rt_iprev;
} else {
rtmp->rt_iprev->rt_inext = rtmp->rt_inext;
rtmp->rt_inext->rt_iprev = rtmp->rt_iprev;
}
}
rtmp->rt_iprev = NULL;
rtmp->rt_inext = NULL;
/* remove zone map */
rtmp_delzonemap(rtmp);
/* remove old route */
gateroute( RTMP_DEL, rtmp );
}
/*
* Add rtmp to the per-interface in-use table. No verification is done...
*/
static void rtmp_addinuse( rtmp )
struct rtmptab *rtmp;
{
struct rtmptab *irt;
gateroute( RTMP_ADD, rtmp );
irt = rtmp->rt_gate->g_iface->i_rt;
if ( irt->rt_inext == NULL ) { /* empty list */
rtmp->rt_inext = NULL;
rtmp->rt_iprev = rtmp;
irt->rt_inext = rtmp;
} else {
rtmp->rt_inext = irt->rt_inext;
rtmp->rt_iprev = irt->rt_inext->rt_iprev;
irt->rt_inext->rt_iprev = rtmp;
irt->rt_inext = rtmp;
}
}
/*
* Change the zone mapping to replace "from" with "to". This code assumes
* the consistency of both the route -> zone map and the zone -> route map.
* This is probably a bad idea. How can we insure that the data is good
* at this point? What do we do if we get several copies of a route in
* an RTMP packet?
*/
static int rtmp_copyzones( to, from )
struct rtmptab *to, *from;
{
struct list *lz, *lr;
to->rt_zt = from->rt_zt;
from->rt_zt = NULL;
if ( from->rt_flags & RTMPTAB_HASZONES ) {
to->rt_flags |= RTMPTAB_HASZONES;
}
for ( lz = to->rt_zt; lz; lz = lz->l_next ) {
for ( lr = ((struct ziptab *)lz->l_data)->zt_rt; lr; lr = lr->l_next ) {
if ( (struct rtmptab *)lr->l_data == from ) {
lr->l_data = (void *)to; /* cast BS */
break;
}
}
if ( lr == NULL ) {
LOG(log_error, logtype_atalkd, "rtmp_copyzones z -> r without r -> z, abort" );
return -1;
}
}
return 0;
}
/*
* Remove rtmp from the in-use table and the per-gate table.
* Free any associated space.
*/
void rtmp_free( rtmp )
struct rtmptab *rtmp;
{
struct gate *gate;
LOG(log_info, logtype_atalkd, "rtmp_free: %u-%u", ntohs(rtmp->rt_firstnet),
ntohs(rtmp->rt_lastnet));
if ( rtmp->rt_iprev ) {
rtmp_delinuse( rtmp );
}
/* remove from per-gate */
gate = rtmp->rt_gate;
if ( gate->g_rt == rtmp ) { /* first */
if ( rtmp->rt_prev == rtmp ) { /* only */
gate->g_rt = NULL;
} else {
gate->g_rt = rtmp->rt_next;
rtmp->rt_next->rt_prev = rtmp->rt_prev;
}
} else {
if ( rtmp->rt_next == NULL ) { /* last */
rtmp->rt_prev->rt_next = NULL;
gate->g_rt->rt_prev = rtmp->rt_prev;
} else {
rtmp->rt_prev->rt_next = rtmp->rt_next;
rtmp->rt_next->rt_prev = rtmp->rt_prev;
}
}
free( rtmp );
}
/*
* Find a replacement for "replace". If we can't find a replacement,
* return 1. If we do find a replacement, return 0. -1 on error.
*/
int rtmp_replace( replace )
struct rtmptab *replace;
{
struct interface *iface;
struct gate *gate;
struct rtmptab *rtmp, *found = NULL;
LOG(log_info, logtype_atalkd, "rtmp_replace %u-%u", ntohs(replace->rt_firstnet),
ntohs(replace->rt_lastnet));
for ( iface = interfaces; iface; iface = iface->i_next ) {
if ((replace->rt_iface != iface) &&
((iface->i_flags & IFACE_ISROUTER) == 0))
continue;
for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
for ( rtmp = gate->g_rt; rtmp; rtmp = rtmp->rt_next ) {
if ( rtmp->rt_firstnet == replace->rt_firstnet &&
rtmp->rt_lastnet == replace->rt_lastnet ) {
if ( found == NULL || rtmp->rt_hops < found->rt_hops ) {
found = rtmp;
}
break;
}
}
}
}
if ( found != replace ) {
if (rtmp_copyzones( found, replace ) < 0)
return -1;
rtmp_delinuse( replace );
rtmp_addinuse( found );
if ( replace->rt_state == RTMPTAB_BAD ) {
rtmp_free( replace );
}
return( 0 );
} else {
if ( replace->rt_hops == RTMPHOPS_POISON ) {
gateroute( RTMP_DEL, replace );
}
return( 1 );
}
}
static int rtmp_new( rtmp )
struct rtmptab *rtmp;
{
struct interface *i;
struct rtmptab *r;
extern int newrtmpdata;
newrtmpdata = 1;
/*
* Do we already have a gateway for this route?
*/
for ( i = interfaces; i; i = i->i_next ) {
if ((rtmp->rt_iface != i) &&
((i->i_flags & IFACE_ISROUTER) == 0))
continue;
for ( r = i->i_rt; r; r = r->rt_inext ) {
/* Should check RTMPTAB_EXTENDED here. XXX */
if (( ntohs( r->rt_firstnet ) <= ntohs( rtmp->rt_firstnet ) &&
ntohs( r->rt_lastnet ) >= ntohs( rtmp->rt_firstnet )) ||
( ntohs( r->rt_firstnet ) <= ntohs( rtmp->rt_lastnet ) &&
ntohs( r->rt_lastnet ) >= ntohs( rtmp->rt_lastnet ))) {
break;
}
}
if ( r ) {
break;
}
}
/*
* This part of this routine is almost never run.
*/
if ( i ) { /* can we get here without r being set? */
if ( r->rt_firstnet != rtmp->rt_firstnet ||
r->rt_lastnet != rtmp->rt_lastnet ) {
LOG(log_info, logtype_atalkd, "rtmp_new netrange mismatch %u-%u != %u-%u",
ntohs( r->rt_firstnet ), ntohs( r->rt_lastnet ),
ntohs( rtmp->rt_firstnet ), ntohs( rtmp->rt_lastnet ));
return 1;
}
/*
* Note that our whole methodology is wrong, if we want to do
* route "load balancing." This entails changing our route
* each time we receive a tuple of equal value. In fact, we can't
* do this, using our method, since we only check against in-use
* routes when a tuple is new from a router.
*/
if ( r->rt_hops < rtmp->rt_hops ) {
return 1;
}
if (rtmp_copyzones( rtmp, r ) < 0)
return -1;
rtmp_delinuse( r );
}
rtmp_addinuse( rtmp );
return 0;
}
int rtmp_packet( ap, from, data, len )
struct atport *ap;
struct sockaddr_at *from;
char *data;
int len;
{
struct rtmp_head rh;
struct rtmp_tuple rt, xrt;
struct gate *gate;
struct interface *iface;
struct rtmptab *rtmp;
char *end, packet[ ATP_BUFSIZ ];
int cc;
end = data + len;
if ( data >= end ) {
LOG(log_info, logtype_atalkd, "rtmp_packet no data" );
return 1;
}
iface = ap->ap_iface;
/* ignore our own packets */
if ( from->sat_addr.s_net == iface->i_addr.sat_addr.s_net &&
from->sat_addr.s_node == iface->i_addr.sat_addr.s_node ) {
return 0;
}
switch( *data++ ) {
case DDPTYPE_RTMPRD :
/*
* Response and Data.
*/
if ( data + sizeof( struct rtmprdhdr ) > end ) {
LOG(log_info, logtype_atalkd, "rtmp_packet no data header" );
return 1;
}
memcpy( &rh, data, sizeof( struct rtmprdhdr ));
data += sizeof( struct rtmprdhdr );
/* check rh address against from address */
if ( rh.rh_nodelen != 8 ) {
LOG(log_info, logtype_atalkd, "rtmp_packet bad node len (%d)", rh.rh_nodelen );
return 1;
}
if (( from->sat_addr.s_net != 0 &&
from->sat_addr.s_net != rh.rh_net ) ||
from->sat_addr.s_node != rh.rh_node ) {
LOG(log_info, logtype_atalkd, "rtmp_packet address mismatch" );
return 1;
}
if (( iface->i_flags & ( IFACE_ADDR|IFACE_CONFIG )) == IFACE_ADDR ) {
if ( iface->i_flags & IFACE_NOROUTER ) {
/* remove addr to loopback route */
if ((cc = looproute( iface, RTMP_DEL )) < 0) {
LOG(log_error, logtype_atalkd, "rtmp_packet: looproute");
return -1;
}
if (cc)
LOG(log_error, logtype_atalkd, "rtmp_packet: can't remove loopback: %s",
strerror(errno) );
iface->i_flags &= ~IFACE_NOROUTER;
iface->i_time = 0;
LOG(log_info, logtype_atalkd, "rtmp_packet router has become available" );
}
if ( iface->i_flags & IFACE_PHASE1 ) {
if (rtmp_config( &rh, iface ) < 0) {
LOG(log_error, logtype_atalkd, "rtmp_packet: rtmp_config");
return -1;
}
} else if (zip_getnetinfo( iface ) < 0) {
LOG(log_error, logtype_atalkd, "rtmp_packet: zip_getnetinfo");
return -1;
}
return 0;
}
if (( iface->i_flags & IFACE_CONFIG ) == 0 ) {
return 0;
}
/*
* Parse first tuple. For phase 2, verify that net is correct.
*/
if ( data + SZ_RTMPTUPLE > end ) {
LOG(log_info, logtype_atalkd, "rtmp_packet missing first tuple" );
return 1;
}
memcpy( &rt, data, SZ_RTMPTUPLE );
data += SZ_RTMPTUPLE;
if ( rt.rt_net == 0 ) {
if ( rt.rt_dist != 0x82 ) {
LOG(log_info, logtype_atalkd, "rtmp_packet bad phase 1 version" );
return 1;
}
/*
* Grab the next tuple, since we don't want to pass the version
* number to the parsing code. We're assuming that there are
* no extended tuples in this packet.
*/
if ( data + SZ_RTMPTUPLE > end ) {
LOG(log_info, logtype_atalkd, "rtmp_packet missing second tuple" );
return 1;
}
memcpy( &rt, data, SZ_RTMPTUPLE );
data += SZ_RTMPTUPLE;
} else if ( rt.rt_dist & 0x80 ) {
if ( data + SZ_RTMPTUPLE > end ) {
LOG(log_info, logtype_atalkd, "rtmp_packet missing first range-end" );
return 1;
}
memcpy( &xrt, data, SZ_RTMPTUPLE );
data += SZ_RTMPTUPLE;
if ( xrt.rt_dist != 0x82 ) {
LOG(log_info, logtype_atalkd, "rtmp_packet bad phase 2 version" );
return 1;
}
/*
* Check for net range conflict.
*/
if ( rt.rt_net != iface->i_rt->rt_firstnet ||
xrt.rt_net != iface->i_rt->rt_lastnet ) {
LOG(log_info, logtype_atalkd, "rtmp_packet interface mismatch" );
return 1;
}
} else {
#ifdef PHASE1NET
/*
* Gatorboxes put a net number in the first tuple, even on
* phase 1 nets. This is wrong, but since we've got it, we
* might just as well check it.
if ( rt.rt_net != iface->i_rt->rt_firstnet ||
rt.rt_net != iface->i_rt->rt_lastnet ) {
LOG(log_info, logtype_atalkd, "rtmp_packet phase 1 interface mismatch" );
return 1;
}
*/
#else /* PHASE1NET */
LOG(log_info, logtype_atalkd, "rtmp_packet bad first tuple" );
return 1;
#endif /* PHASE1NET */
}
/*
* Find gateway.
*/
for ( gate = iface->i_gate; gate; gate = gate->g_next ) {
if ( gate->g_sat.sat_addr.s_net == from->sat_addr.s_net &&
gate->g_sat.sat_addr.s_node == from->sat_addr.s_node ) {
break;
}
}
if ( !gate ) { /* new gateway */
if (( gate = (struct gate *)malloc( sizeof( struct gate ))) == 0 ) {
LOG(log_error, logtype_atalkd, "rtmp_packet: malloc: %s", strerror(errno) );
return -1;
}
gate->g_next = iface->i_gate;
gate->g_prev = 0;
gate->g_rt = 0;
gate->g_iface = iface; /* need this? */
gate->g_sat = *from;
if ( iface->i_gate ) {
iface->i_gate->g_prev = gate;
}
iface->i_gate = gate;
LOG(log_info, logtype_atalkd, "rtmp_packet gateway %u.%u up",
ntohs( gate->g_sat.sat_addr.s_net ),
gate->g_sat.sat_addr.s_node );
}
/*
* Reset the timeout on this gateway. We'll remove the gateway
* entry, if the timeout gets to RTMPTAB_BAD.
*/
gate->g_state = RTMPTAB_GOOD;
/*
* Parse remaining tuples.
*/
for (;;) {
/*
* Is route on this gateway?
*/
for ( rtmp = gate->g_rt; rtmp; rtmp = rtmp->rt_next ) {
if ( ntohs( rtmp->rt_firstnet ) <= ntohs( rt.rt_net ) &&
ntohs( rtmp->rt_lastnet ) >= ntohs( rt.rt_net )) {
break;
}
if (( rt.rt_dist & 0x80 ) &&
ntohs( rtmp->rt_firstnet ) <= ntohs( xrt.rt_net ) &&
ntohs( rtmp->rt_lastnet ) >= ntohs( xrt.rt_net )) {
break;
}
}
if ( rtmp ) { /* found it */
/*
* Check for range conflicts. (This is getting a little
* ugly.)
*/
if ( rtmp->rt_firstnet != rt.rt_net ) {
LOG(log_info, logtype_atalkd, "rtmp_packet firstnet mismatch %u!=%u",
ntohs( rtmp->rt_firstnet ), ntohs( rt.rt_net ));
return 1;
}
if ( rt.rt_dist & 0x80 ) {
if (( rtmp->rt_flags & RTMPTAB_EXTENDED ) == 0 ) {
LOG(log_info, logtype_atalkd, "rtmp_packet extended mismatch %u",
ntohs( rtmp->rt_firstnet ));
return 1;
}
if ( rtmp->rt_lastnet != xrt.rt_net ) {
LOG(log_info, logtype_atalkd, "rtmp_packet lastnet mismatch %u!=%u",
ntohs( rtmp->rt_lastnet ), ntohs( xrt.rt_net ));
return 1;
}
} else {
if ( rtmp->rt_flags & RTMPTAB_EXTENDED ) {
LOG(log_info, logtype_atalkd, "rtmp_packet !extended mismatch %u",
ntohs( rtmp->rt_firstnet ));
return 1;
}
if ( rtmp->rt_lastnet != rt.rt_net ) {
LOG(log_info, logtype_atalkd, "rtmp_packet lastnet mismatch %u!=%u",
ntohs( rtmp->rt_lastnet ), ntohs( rt.rt_net ));
return 1;
}
}
rtmp->rt_state = RTMPTAB_GOOD;
/*
* Check hop count. If the count has changed, update
* the routing database.
*/
if (( rtmp->rt_hops != ( rt.rt_dist & 0x7f ) + 1 ) &&
( rtmp->rt_hops != RTMPHOPS_POISON ||
( rt.rt_dist & 0x7f ) + 1 <= RTMPHOPS_MAX )) {
if ( rtmp->rt_iprev ) { /* route is in use */
if ( rtmp->rt_hops > ( rt.rt_dist & 0x7f ) + 1 ) {
/*
* If this was POISON, we've deleted it from
* the kernel. Add it back in.
*/
if ( rtmp->rt_hops == RTMPHOPS_POISON ) {
gateroute( RTMP_ADD, rtmp );
}
rtmp->rt_hops = ( rt.rt_dist & 0x7f ) + 1;
} else {
/*
* Hop count has gone up for this route.
* Search for a new best route. If we can't
* find one, just keep this route. "poison"
* route are deleted in as_timer().
*/
if (( rt.rt_dist & 0x7f ) + 1 > RTMPHOPS_MAX ) {
rtmp->rt_hops = RTMPHOPS_POISON;
} else {
rtmp->rt_hops = ( rt.rt_dist & 0x7f ) + 1;
}
if (rtmp_replace( rtmp ) < 0) {
LOG(log_error, logtype_atalkd, "rtmp_packet: rtmp_replace");
return -1;
}
}
} else { /* route not in use */
rtmp->rt_hops = ( rt.rt_dist & 0x7f ) + 1;
if ( rtmp->rt_hops > ( rt.rt_dist & 0x7f ) + 1 ) {
if (rtmp_new( rtmp ) < 0) {
LOG(log_error, logtype_atalkd, "rtmp_packet: rtmp_new");
return -1;
}
}
}
}
/*
* Make the *next* node the head, since
* we're not likely to be asked for the same tuple twice
* in a row.
*/
if ( rtmp->rt_next != 0 ) {
gate->g_rt->rt_prev->rt_next = gate->g_rt;
gate->g_rt = rtmp->rt_next;
rtmp->rt_next = 0;
}
} else if (( rt.rt_dist & 0x7f ) + 1 > RTMPHOPS_MAX ) {
LOG(log_info, logtype_atalkd, "rtmp_packet bad hop count from %u.%u for %u",
ntohs( from->sat_addr.s_net ), from->sat_addr.s_node,
ntohs( rt.rt_net ));
} else { /* new for router */
if (( rtmp = newrt(iface)) == NULL ) {
LOG(log_error, logtype_atalkd, "rtmp_packet: newrt: %s", strerror(errno) );
return -1;
}
rtmp->rt_firstnet = rt.rt_net;
if ( rt.rt_dist & 0x80 ) {
rtmp->rt_lastnet = xrt.rt_net;
rtmp->rt_flags = RTMPTAB_EXTENDED;
} else {
rtmp->rt_lastnet = rt.rt_net;
}
rtmp->rt_hops = ( rt.rt_dist & 0x7f ) + 1;
rtmp->rt_state = RTMPTAB_GOOD;
rtmp->rt_gate = gate;
/*
* Add rtmptab entry to end of list (leave head alone).
*/
if ( gate->g_rt == 0 ) {
rtmp->rt_prev = rtmp;
gate->g_rt = rtmp;
} else {
rtmp->rt_prev = gate->g_rt->rt_prev;
gate->g_rt->rt_prev->rt_next = rtmp;
gate->g_rt->rt_prev = rtmp;
}
if (rtmp_new( rtmp ) < 0) {
LOG(log_error, logtype_atalkd, "rtmp_packet: rtmp_new");
return -1;
}
}
if ( data + SZ_RTMPTUPLE > end ) {
break;
}
memcpy( &rt, data, SZ_RTMPTUPLE );
data += SZ_RTMPTUPLE;
if ( rt.rt_dist & 0x80 ) {
if ( data + SZ_RTMPTUPLE > end ) {
LOG(log_info, logtype_atalkd, "rtmp_packet missing range-end" );
return 1;
}
memcpy( &xrt, data, SZ_RTMPTUPLE );
data += SZ_RTMPTUPLE;
}
}
/*
* Make sure we've processed the whole packet.
*/
if ( data != end ) {
LOG(log_info, logtype_atalkd, "rtmp_packet length and count mismatch" );
}
break;
case DDPTYPE_RTMPR :
/*
* Request and RDR.
*/
if (((iface->i_flags & IFACE_ISROUTER) == 0) ||
iface->i_rt->rt_zt == 0 ||
( iface->i_flags & IFACE_CONFIG ) == 0 ) {
return 0;
}
if ( *data == 1 ) {
data = packet;
*data++ = DDPTYPE_RTMPRD;
rh.rh_net = iface->i_addr.sat_addr.s_net;
rh.rh_nodelen = 8;
rh.rh_node = iface->i_addr.sat_addr.s_node;
memcpy( data, &rh, sizeof( struct rtmp_head ));
data += sizeof( struct rtmp_head );
if ( iface->i_flags & IFACE_PHASE2 ) {
rt.rt_net = iface->i_rt->rt_firstnet;
rt.rt_dist = 0x80;
memcpy( data, &rt, SZ_RTMPTUPLE );
data += SZ_RTMPTUPLE;
rt.rt_net = iface->i_rt->rt_lastnet;
rt.rt_dist = 0x82;
memcpy( data, &rt, SZ_RTMPTUPLE );
data += SZ_RTMPTUPLE;
}
if ( sendto( ap->ap_fd, packet, data - packet, 0,
(struct sockaddr *)from,
sizeof( struct sockaddr_at )) < 0 ) {
LOG(log_error, logtype_atalkd, "as_timer sendto: %s", strerror(errno) );
}
} else if ( *data == 2 || *data == 3 ) {
#ifdef DEBUG
printf( "rtmp_packet rdr (%d) from %u.%u\n",
*data, ntohs( from->sat_addr.s_net ),
from->sat_addr.s_node );
#endif /* DEBUG */
} else {
LOG(log_info, logtype_atalkd, "rtmp_packet unknown request from %u.%u",
ntohs( from->sat_addr.s_net ), from->sat_addr.s_node );
}
break;
default :
LOG(log_info, logtype_atalkd, "rtmp_packet bad ddp type from %u.%u",
ntohs( from->sat_addr.s_net ), from->sat_addr.s_node );
return 0;
}
return 0;
}
int rtmp_request( iface )
struct interface *iface;
{
struct sockaddr_at sat;
struct atport *ap;
char *data, packet[ 2 ];
LOG(log_info, logtype_atalkd, "rtmp_request for %s", iface->i_name );
for ( ap = iface->i_ports; ap; ap = ap->ap_next ) {
if ( ap->ap_packet == rtmp_packet ) {
break;
}
}
if ( ap == 0 ) {
LOG(log_error, logtype_atalkd, "rtmp_request can't find rtmp socket!" );
return -1;
}
data = packet;
*data++ = DDPTYPE_RTMPR;
*data++ = RTMPROP_REQUEST;
/*
* There is a problem with the net zero "hint" hack.
*/
memset( &sat, 0, sizeof( struct sockaddr_at ));
#ifdef BSD4_4
sat.sat_len = sizeof( struct sockaddr_at );
#endif /* BSD4_4 */
sat.sat_family = AF_APPLETALK;
sat.sat_addr.s_net = iface->i_addr.sat_addr.s_net;
sat.sat_addr.s_node = ATADDR_BCAST;
sat.sat_port = ap->ap_port;
if ( sendto( ap->ap_fd, packet, data - packet, 0, (struct sockaddr *)&sat,
sizeof( struct sockaddr_at )) < 0 ) {
LOG(log_error, logtype_atalkd, "rtmp_request sendto: %s", strerror(errno) );
return -1;
}
return 0;
}
int looproute( iface, cmd )
struct interface *iface;
unsigned int cmd;
{
struct sockaddr_at dst, loop;
if ( cmd == RTMP_DEL && ( iface->i_flags & IFACE_LOOP ) == 0 ) {
LOG(log_error, logtype_atalkd, "looproute panic no route" );
return -1;
}
if ( cmd == RTMP_ADD && ( iface->i_flags & IFACE_LOOP )) {
LOG(log_error, logtype_atalkd, "looproute panic two routes" );
return -1;
}
memset( &dst, 0, sizeof( struct sockaddr_at ));
#ifdef BSD4_4
dst.sat_len = sizeof( struct sockaddr_at );
#endif /* BSD4_4 */
dst.sat_family = AF_APPLETALK;
dst.sat_addr.s_net = iface->i_addr.sat_addr.s_net;
dst.sat_addr.s_node = iface->i_addr.sat_addr.s_node;
memset( &loop, 0, sizeof( struct sockaddr_at ));
#ifdef BSD4_4
loop.sat_len = sizeof( struct sockaddr_at );
#endif /* BSD4_4 */
loop.sat_family = AF_APPLETALK;
loop.sat_addr.s_net = htons( ATADDR_ANYNET );
loop.sat_addr.s_node = ATADDR_ANYNODE;
#ifndef BSD4_4
if ( route( cmd,
(struct sockaddr *) &dst,
(struct sockaddr *) &loop,
RTF_UP | RTF_HOST ) ) {
return( 1 );
}
#else /* ! BSD4_4 */
if ( route( cmd,
(struct sockaddr_at *) &dst,
(struct sockaddr_at *) &loop,
RTF_UP | RTF_HOST ) ) {
return ( 1);
}
#endif /* BSD4_4 */
if ( cmd == RTMP_ADD ) {
iface->i_flags |= IFACE_LOOP;
}
if ( cmd == RTMP_DEL ) {
iface->i_flags &= ~IFACE_LOOP;
}
return( 0 );
}
int gateroute( command, rtmp )
unsigned int command;
struct rtmptab *rtmp;
{
struct sockaddr_at dst, gate;
unsigned short net;
if ( command == RTMP_DEL && ( rtmp->rt_flags & RTMPTAB_ROUTE ) == 0 ) {
return( -1 );
}
if ( command == RTMP_ADD && ( rtmp->rt_flags & RTMPTAB_ROUTE )) {
return( -1 );
}
net = ntohs( rtmp->rt_firstnet );
/*
* Since we will accept routes from gateways who advertise their
* address as 0.YY, we must munge the gateway address we give to
* the kernel. Otherwise, we'll get a bunch of routes to the loop
* back interface, and who wants that?
*/
memset( &gate, 0, sizeof( struct sockaddr_at ));
#ifdef BSD4_4
gate.sat_len = sizeof( struct sockaddr_at );
#endif /* BSD4_4 */
gate.sat_family = AF_APPLETALK;
gate.sat_addr.s_net = rtmp->rt_gate->g_sat.sat_addr.s_net;
gate.sat_addr.s_node = rtmp->rt_gate->g_sat.sat_addr.s_node;
if ( gate.sat_addr.s_net == 0 ) {
gate.sat_addr.s_net = net;
}
memset( &dst, 0, sizeof( struct sockaddr_at ));
#ifdef BSD4_4
dst.sat_len = sizeof( struct sockaddr_at );
#endif /* BSD4_4 */
dst.sat_family = AF_APPLETALK;
dst.sat_addr.s_node = ATADDR_ANYNODE;
do {
dst.sat_addr.s_net = htons( net );
#ifndef BSD4_4
if ( route( command,
(struct sockaddr *) &dst,
(struct sockaddr *) &gate,
RTF_UP | RTF_GATEWAY )) {
LOG(log_error, logtype_atalkd, "route: %u -> %u.%u: %s", net,
ntohs( gate.sat_addr.s_net ), gate.sat_addr.s_node,
strerror(errno) );
continue;
}
#else /* ! BSD4_4 */
if ( route( command,
(struct sockaddr_at *) &dst,
(struct sockaddr_at *) &gate,
RTF_UP | RTF_GATEWAY )) {
LOG(log_error, logtype_atalkd, "route: %u -> %u.%u: %m", net,
ntohs( gate.sat_addr.s_net ), gate.sat_addr.s_node );
continue;
}
#endif /* ! BSD4_4 */
} while ( net++ < ntohs( rtmp->rt_lastnet ));
if ( command == RTMP_ADD ) {
rtmp->rt_flags |= RTMPTAB_ROUTE;
}
if ( command == RTMP_DEL ) {
rtmp->rt_flags &= ~RTMPTAB_ROUTE;
}
return( 0 );
}
struct rtmptab *
newrt(const struct interface *iface)
{
struct rtmptab *rtmp;
if (( rtmp = (struct rtmptab *)calloc(1, sizeof(struct rtmptab))) == 0 ) {
return( 0 );
}
rtmp->rt_iface = iface;
return( rtmp );
}
|