-
Notifications
You must be signed in to change notification settings - Fork 5
/
cbridge.c
2423 lines (2226 loc) · 75.5 KB
/
cbridge.c
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
1000
/* Copyright © 2005, 2017-2023 Björn Victor ([email protected]) */
/* Bridge program for various Chaosnet implementations. */
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* Based on echaos (by Björn), and chudprt (by Björn, partly based on
one of Brad's chaosd clients). Some code from LambdaDelta (the LMI
Lambda emulator by Daniel Seagraves), and some from klh10 (the
PDP10 emulator by Ken Harrenstien). */
/* Bridge program for various Chaosnet implementations.
Support many link-layer implementations:
- Chaos-over-Ethernet
- CHUDP (Chaos-over-UDP, used by klh10/its)
- Chaos-over-TLS (using two bytes of record length)
- chaosd (Unix socket protocol, used by the usim CADR emulator)
- Chaos-over-IP (IP address mapping, used by pdp10x)
see also "Cisco's implementation of Chaosnet", same type of mapping
https://docstore.mik.ua/univercd/cc/td/doc/product/software/ssr83/rpc_r/48381.htm
Also implements the transport layer (Network Control Program), see ncp.c and NCP.md
*/
/* Read MIT AIM 628, in particular secions 3.6 and 3.7. */
// TODO
// #### clean up routing table issues
// invent host-route protocol (RUTH? RBG?)
// autoconf please
// logging:
// - lock to avoid mixed output from different threads
// -- need two output fns, one assuming a lock is held, one with built-in locking.
// -- or recursive mutex.
// - improve granularity, e.g. to only log "major" events
// -- define "levels" as in LambdaDelta (higher for more details, 0 for no output, 1 for "major stuff")
// -- levels: emergency, major, minor, info, debug
// - turn on/off module-specific tracing (similar to LambdaDelta again)
// - consider including thread id, include time
// "tls trace 5" or "routing trace 1" or "arp trace 2" etc?
// CHUDP version 2, using network order (big-endian). Also fix klh10/CH11 for this.
// Can this be modular enough to keep within the chudp "module"? Needs per-link config.
// validate conf (subnets vs bridges etc)
// - multiple links/routes to same chaddr
// - make sure thois host has a subnet-specific chaos addr on all defined links
// detect unexpected traffic
// - e.g. source addr 3150 from link with addr 3160 (which has another link)
// - or traffic from a defined subnet arriving on a different link
// add parameters for various constants (arp age limit, reparsing interval...)
// minimize copying
// - now net order is swapped to host order when receiving from Ether and Unix,
// and then swapped back before forwarding,
// while CHUDP is not swapped but needs copying anyway (because of chudp header)
// - better to not swap Ether/Unix, of course, but need to rewrite chaos.h
// and make it endian dependent - ugh (but all processors are Intel these days... ;-o)
// - or separate header from data; swap header like now, but keep data intact
// notify if more routes are known than fit in a RUT pkt (but it's 122, come on...)
#include <time.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/poll.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include "cbridge.h"
#include "chudp.h" /* chudp pkt format etc */
#include "firewall.h"
int verbose, debug, stats, tls_debug = 0;
#if CHAOS_ETHERP
void print_arp_table();
#endif
// Route table, indexed by subnet
struct chroute rttbl_net[256];
// and for individual hosts, simple array, where rt_braddr is the dest
struct chroute rttbl_host[RTTBL_HOST_MAX];
int rttbl_host_len = 0;
// array indexed first by net, then by host. Second level dynamically allocated.
struct hostat *hosttab[256];
pthread_mutex_t hosttab_lock = PTHREAD_MUTEX_INITIALIZER;
// simple array indexed by subnet, updated for send/receives on routes with direct link
struct linkstat linktab[256];
pthread_mutex_t rttbl_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t linktab_lock = PTHREAD_MUTEX_INITIALIZER;
// @@@@ move configuration of this, and then move declaration
struct chudest chudpdest[CHUDPDEST_MAX];
int chudpdest_len = 0;
// Private, non-routed subnets
static u_short private_subnet[256];
static u_short number_of_private_subnets = 0;
// Hosts file for private subnets
static char *private_hosts_file = NULL;
#if CHAOS_TLS
char tls_ca_file[PATH_MAX] = "ca-chain.cert.pem"; /* trust chain */
char tls_key_file[PATH_MAX]; /* private key */
char tls_cert_file[PATH_MAX]; /* certificate */
char tls_crl_file[PATH_MAX]; /* certificate revocation list */
// allow clients from different subnets to connect, here are my different addrs
u_short tls_myaddrs[TLSDEST_MAX]; /* my chaos addresses on TLS server links */
int tls_n_myaddrs = 0;
int tls_server_port = 42042;
int do_tls = 0, do_tls_server = 0;
int do_tls_ipv6 = 0;
pthread_mutex_t tlsdest_lock = PTHREAD_MUTEX_INITIALIZER; /* for locking tlsdest */
struct tls_dest tlsdest[TLSDEST_MAX]; /* table of tls_dest entries */
int tlsdest_len = 0;
int parse_tls_config_line(void);
#endif // CHAOS_TLS
int parse_chudp_config_line(void);
int parse_usockets_config(void);
#if CHAOS_IP
int do_chip = 0;
int parse_chip_config_line(void);
void print_config_chip(void);
#endif
#if CHAOS_DNS
extern int do_dns_forwarding;
void init_chaos_dns(int do_forwarding);
int parse_dns_config_line(void);
void print_config_dns(void);
void *dns_forwarder_thread(void *v);
#endif
// NCP
extern int ncp_enabled;
void *ncp_user_server(void *v);
int parse_ncp_config_line(void);
void packet_to_conn_handler(u_char *pkt, int len);
void print_ncp_stats(void);
int parse_private_hosts_file(char *f);
void print_private_hosts_config(void);
time_t boottime;
// Config stuff @@@@ some to be moved to respective module
char myname[32]; /* my chaosnet host name (look it up!). Note size limit by STATUS prot */
#define NCHADDR 8
int nchaddr = 0;
u_short mychaddr[NCHADDR]; /* My Chaos address (only for ARP) */
int chudp_port = 42042; // default UDP port
int chudp_dynamic = 0; // dynamically add CHUDP entries for new receptions
int do_unix = 0, do_udp = 0, do_udp6 = 0, do_ether = 0;
// for each implementation
void forward_on_chudp(struct chroute *rt, u_short schad, u_short dchad, struct chaos_header *ch, u_char *data, int dlen);
void forward_on_usocket(struct chroute *rt, u_short schad, u_short dchad, struct chaos_header *ch, u_char *data, int dlen);
#if CHAOS_ETHERP
void forward_on_ether(struct chroute *rt, u_short schad, u_short dchad, struct chaos_header *ch, u_char *data, int dlen);
#endif
#if CHAOS_TLS
void forward_on_tls(struct chroute *rt, u_short schad, u_short dchad, struct chaos_header *ch, u_char *data, int dlen);
#endif
#if CHAOS_IP
void forward_on_ip(struct chroute *rt, u_short schad, u_short dchad, struct chaos_header *ch, u_char *data, int dlen);
#endif
// contacts
int handle_rfc(struct chaos_header *ch, u_char *data, int dlen);
int make_routing_table_pkt(u_short dest, u_char *pkt, int pklen);
// chudp
void init_chaos_udp(int v6, int v4);
void print_chudp_config(void);
void reparse_chudp_names(void);
#if CHAOS_ETHERP
// chether
void print_arp_table(void);
void print_config_ether(void);
int parse_ether_config_line(void);
int parse_ether_link_config(void);
int postparse_ether_link_config(struct chroute *rt);
#endif
#if CHAOS_TLS
// chtls
void init_chaos_tls();
void print_tlsdest_config(void);
#endif
#if CHAOS_IP
// chip
void reparse_chip_names();
void print_chipdest_config();
#endif
// usockets
void print_config_usockets(void);
// threads @@@@ document args?
void *unix_input(void *v);
void *chudp_input(void *v);
#if CHAOS_ETHERP
void *ether_input(void *v);
#endif
#if CHAOS_IP
void *chip_input(void *v);
#endif
#if CHAOS_TLS
void *tls_server(void *v);
void *tls_input(void *v);
void *tls_connector(void *arg);
#endif
int is_mychaddr(u_short addr)
{
int i;
for (i = 0; i < nchaddr && i < NCHADDR; i++)
if (mychaddr[i] == addr)
return 1;
return 0;
}
int mychaddr_on_net(u_short addr)
{
int i;
for (i = 0; i < nchaddr && i < NCHADDR; i++)
if ((mychaddr[i] & 0xff00) == (addr & 0xff00))
return mychaddr[i];
return 0;
}
u_short find_closest_addr(u_short haddrs[], int naddrs)
{
// search haddrs for the address closest to one of mine
// Simplest: just look for an addr on the same subnet as one of mine
int i, a;
if (naddrs <= 0) {
fprintf(stderr,"find_closest_addr called with %d addresses to search\n", naddrs);
exit(1);
}
if (naddrs == 1)
// only one choice
return haddrs[0];
for (i = 0; i < naddrs; i++) {
if ((a = mychaddr_on_net(haddrs[i])) != 0)
return haddrs[i];
}
// then find a route and use the local end of that
struct chroute *rt = find_in_routing_table(haddrs[0], 0, 0);
if ((rt != NULL) && (rt->rt_myaddr > 0))
return rt->rt_myaddr;
// default
return haddrs[0];
}
u_short find_my_closest_addr(u_short addr)
{
// search mychaddrs for the address closest to the one given
// Simplest: just look for an addr on the same subnet
int a;
if (nchaddr == 1)
// only one choice
return mychaddr[0];
if ((a = mychaddr_on_net(addr)) != 0)
return a;
// then find a route and use the local end of that
struct chroute *rt = find_in_routing_table(addr, 0, 0);
if ((rt != NULL) && (rt->rt_myaddr > 0))
return rt->rt_myaddr;
// default
return mychaddr[0];
}
void add_mychaddr(u_short addr)
{
if (!is_mychaddr(addr)) {
if (nchaddr < NCHADDR)
mychaddr[nchaddr++] = addr;
else
fprintf(stderr,"out of local chaos addresses, please increase NCHADDR from %d\n", NCHADDR);
}
}
int valid_chaos_host_address(u_short addr)
{
// both subnet and host part must be non-zero
return ((addr > 0xff) && ((addr & 0xff) != 0));
}
int is_private_subnet(u_short subnet)
{
return private_subnet[subnet];
}
int valid_opcode(int opc)
{
return !((opc == 0) || ((opc > CHOP_BRD) && (opc < CHOP_DAT)));
}
void print_link_stats()
{
int i;
PTLOCKN(linktab_lock,"linktab_lock");
printf("Link stats:\n"
"Subnet\t In\t Out\t Abort\t Lost\t CRC\t Ram\t Bitc\t Rej\n");
for (i = 0; i < 256; i++) {
if (linktab[i].pkt_in != 0 || linktab[i].pkt_out != 0 || linktab[i].pkt_crcerr != 0) {
printf("%#o\t%7d\t%7d\t%7d\t%7d\t%7d\t%7d\t%7d\t%7d\n", i,
linktab[i].pkt_in, linktab[i].pkt_out, linktab[i].pkt_aborted, linktab[i].pkt_lost,
linktab[i].pkt_crcerr, linktab[i].pkt_crcerr_post,
linktab[i].pkt_badlen, linktab[i].pkt_rejected);
}
}
PTUNLOCKN(linktab_lock,"linktab_lock");
}
// call this with hosttab_lock held
struct hostat *
find_hostat_entry(u_short addr)
{
struct hostat *net = hosttab[addr>>8];
if (net == NULL) {
if ((net = malloc(sizeof(struct hostat)*256)) == NULL)
perror("malloc(hosttab entry)");
memset((char *)net, 0, sizeof(struct hostat)*256);
hosttab[addr>>8] = net;
}
return &hosttab[addr>>8][addr&0xff];
}
void print_host_stats()
{
int n, i;
PTLOCKN(hosttab_lock,"hosttab_lock");
printf("Host stats:\n"
"Host\t In\t Via\t Last seen\t FC\n");
for (n = 0; n < 256; n++) {
struct hostat *he = hosttab[n];
if (he != NULL) {
for (i = 0; i < 256; i++) {
// fprintf(stderr,"hosttab[%d][%i] = %p\n", n, i, he[i]);
if (he[i].hst_in != 0 || he[i].hst_last_hop != 0 || he[i].hst_last_seen != 0) {
printf("%#o\t%7d\t%#o\t%ld\t%d\n", (n << 8) | i,
he[i].hst_in, he[i].hst_last_hop,
he[i].hst_last_seen > 0 ? time(NULL) - he[i].hst_last_seen : 0,
he[i].hst_last_fc);
}
}
}
}
PTUNLOCKN(hosttab_lock,"hosttab_lock");
}
char *rt_linkname(u_char linktype)
{
switch (linktype) {
case LINK_UNIXSOCK: return "Unix";
case LINK_CHUDP: return "CHUDP";
#if CHAOS_TLS
case LINK_TLS: return "TLS";
#endif
#if CHAOS_IP
case LINK_IP: return "CHIP";
#endif
#if CHAOS_ETHERP
case LINK_ETHER: return "Ether";
#endif
default: return "Unknown?";
}
}
char *rt_typename(u_char type)
{
switch (type) {
case RT_NOPATH: return "NoPath";
case RT_STATIC: return "Static";
case RT_DYNAMIC: return "Dynamic";
default: return "Unknown?";
}
}
void
print_routing_table()
{
int i;
printf("Routing tables follow:\n");
if (rttbl_host_len > 0) {
printf("Host\tBridge\tType\tLink\tMyAddr\tCost\tAge\n");
for (i = 0; i < rttbl_host_len; i++)
if (rttbl_host[i].rt_link != LINK_NOLINK) {
printf("%#o\t%#o\t%s\t%s\t%#o\t%d\t%ld",
rttbl_host[i].rt_dest, rttbl_host[i].rt_braddr, rt_typename(rttbl_host[i].rt_type),
rt_linkname(rttbl_host[i].rt_link),
rttbl_host[i].rt_myaddr,
rttbl_host[i].rt_cost,
rttbl_host[i].rt_cost_updated > 0 ? time(NULL) - rttbl_host[i].rt_cost_updated : 0);
#if CHAOS_TLS
if (rttbl_host[i].rt_tls_muxed[0] != 0) {
int j;
printf("\tmux: %o", rttbl_host[i].rt_tls_muxed[0]);
for (j = 1; j < CHTLS_MAXMUX && rttbl_host[i].rt_tls_muxed[j] != 0; j++) {
printf(",%o",rttbl_host[i].rt_tls_muxed[j]);
}
}
#endif
printf("\n");
}
}
printf("Net\tBridge\tType\tLink\tMyAddr\tCost\tAge\n");
for (i = 0; i < 0xff; i++)
if (rttbl_net[i].rt_link != LINK_NOLINK)
printf("%#o\t%#o\t%s\t%s\t%#o\t%d\t%ld\n",
i, rttbl_net[i].rt_braddr, rt_typename(rttbl_net[i].rt_type), rt_linkname(rttbl_net[i].rt_link),
rttbl_net[i].rt_myaddr,
rttbl_net[i].rt_cost,
rttbl_net[i].rt_cost_updated > 0 ? time(NULL) - rttbl_net[i].rt_cost_updated : 0);
}
struct chroute *
add_to_routing_table(u_short dest, u_short braddr, u_short myaddr, int type, int link, int cost)
{
// !!!! call this with rttbl_lock already locked
struct chroute *r = NULL;
// make a routing entry for host dest through braddr using myaddr, route type, link type, cost
// @@@@ perhaps validate data?
if (rttbl_host_len < RTTBL_HOST_MAX) {
rttbl_host[rttbl_host_len].rt_dest = dest;
rttbl_host[rttbl_host_len].rt_braddr = braddr; /* direct, not through a bridge */
// find a "myaddr" entry for the subnet of the bridge
if ((myaddr == 0) && (braddr != 0)) {
int i;
for (i = 0; i < nchaddr; i++) {
if ((mychaddr[i] & 0xff00) == (braddr & 0xff00)) {
myaddr = mychaddr[i];
if (verbose) fprintf(stderr,"%%%% Fill in myaddr %#o for new route to %#o via %#o (%s)\n",
myaddr, dest, braddr, rt_linkname(link));
break;
}
}
if (myaddr == 0)
fprintf(stderr,"%%%% Don't know my address for net %#o when adding route to %#o via %#o (%s)\n",
(braddr & 0xff00)>>8, dest, braddr, rt_linkname(link));
}
rttbl_host[rttbl_host_len].rt_myaddr = myaddr; /* if 0, the main address is used */
rttbl_host[rttbl_host_len].rt_type = type;
rttbl_host[rttbl_host_len].rt_link = link;
rttbl_host[rttbl_host_len].rt_cost = cost;
rttbl_host[rttbl_host_len].rt_cost_updated = time(NULL); // cost doesn't change but keep track of when it came up
r = &rttbl_host[rttbl_host_len];
rttbl_host_len++;
if (verbose) print_routing_table();
} else
fprintf(stderr,"%%%% host route table full (NOT adding %s for %#o), increase RTTBL_HOST_MAX!\n",
rt_linkname(link), dest);
return r;
}
void *
reparse_link_host_names_thread(void *v)
{
while (1) {
sleep(60*5); // Hmm, how often really?
reparse_chudp_names(); // occasionally re-parse chu_name strings
#if CHAOS_IP
reparse_chip_names();
#endif
}
}
// Look at an incoming RUT pkt given a connection type and a cost,
// and update our routing table if appropriate.
void
peek_routing(u_char *pkt, int pklen, int cost, u_short linktype)
{
struct chaos_header *cha = (struct chaos_header *)pkt;
u_short src, pksrc = ch_srcaddr(cha);
u_char *data = &pkt[CHAOS_HEADERSIZE];
u_short rsub, rcost;
int i, pkdlen = ch_nbytes(cha);
if (is_mychaddr(pksrc)) {
// Already checked by caller
if (debug) fprintf(stderr,"Got my pkt back (%#o), ignoring\n", pksrc);
return;
}
if ((ch_opcode(cha) == 0) || ((ch_opcode(cha) > CHOP_BRD) && (ch_opcode(cha) < CHOP_DAT))) {
fprintf(stderr,"BAD PACKET opcode %#o (wrong byte order?)\n", ch_opcode(cha));
htons_buf((u_short *)pkt,(u_short *)pkt,pklen);
ch_dumpkt(pkt,pklen);
ntohs_buf((u_short *)pkt,(u_short *)pkt,pklen);
return;
}
if (pklen >= CHAOS_HEADERSIZE + pkdlen + CHAOS_HW_TRAILERSIZE) {
struct chaos_hw_trailer *tr = (struct chaos_hw_trailer *)&pkt[pklen-6];
src = ntohs(tr->ch_hw_srcaddr);
if (src == 0)
src = ch_srcaddr(cha);
} else {
src = ch_srcaddr(cha);
}
/* Check for RUT pkt */
int rttbl_updated = 0;
if (ch_opcode(cha) == CHOP_RUT) {
if (verbose) fprintf(stderr,"RUT pkt analysis...\n");
/* See AIM 628 sec 3.7 p15 */
for (i = 0; i < pkdlen; i += 4) {
rsub = WORD16(&data[i]); /* subnet nr */
rcost = WORD16(&data[i+2]); /* cost from that bridge */
if (is_private_subnet(rsub)) {
// Never mind about somebody's private network
// They should not announce it: print warning, @@@@ but not every time
fprintf(stderr," Received RUT info for subnet %#o from host %#o,\n"
" but that subnet is private and should not be announced.\n",
rsub, src);
continue;
}
if (rttbl_net[rsub].rt_type == RT_STATIC && (verbose||debug) )
fprintf(stderr,"DEBUG: Received RUT info for subnet %#o from host %#o.\n"
" We have a STATIC route to that subnet - "
" bug in network structure or sender's software?\n",
rsub, src);
if ((rcost + cost) > RTCOST_HIGH) {
// Don't add routes which are immediately stale
if (verbose) fprintf(stderr," Received RUT for subnet %#o from host %#o with high cost already: %d+%d > %d",
rsub, src, rcost, cost, RTCOST_HIGH);
}
else if ((rttbl_net[rsub].rt_type == RT_NOPATH) /* we have no path currently */
/* we had a higher (or equal, to update age) cost */
|| ((rttbl_net[rsub].rt_cost >= (rcost + cost))
/* but don't update if we have a static route */
&& (rttbl_net[rsub].rt_type != RT_STATIC))
) {
if (rttbl_net[rsub].rt_type == RT_NOPATH) {
if (verbose) fprintf(stderr," Adding new route to %#o type %d cost %d via %#o\n",
rsub, RT_DYNAMIC, (rcost + cost), src);
} else if ((rcost + cost) != rttbl_net[rsub].rt_cost) {
if (verbose) fprintf(stderr," Updating cost for route to %#o type %d cost %d -> %d via %#o\n",
rsub, RT_DYNAMIC, rttbl_net[rsub].rt_cost, (rcost + cost), src);
} else
if (verbose) fprintf(stderr," Updating age for route to %#o type %d cost %d via %#o\n",
rsub, RT_DYNAMIC, rttbl_net[rsub].rt_cost, src);
rttbl_updated = 1;
PTLOCKN(rttbl_lock,"rttbl_lock");
rttbl_net[rsub].rt_type = RT_DYNAMIC; // type;
rttbl_net[rsub].rt_cost = (rcost + cost); /* add the cost to go to that bridge */
rttbl_net[rsub].rt_link = linktype;
// Note the bridge: the sender of the RUT pkt
rttbl_net[rsub].rt_braddr = src;
// Note it's a subnet route
rttbl_net[rsub].rt_dest = src & 0xff00;
rttbl_net[rsub].rt_cost_updated = time(NULL);
PTUNLOCKN(rttbl_lock,"rttbl_lock");
}
else if (debug) {
fprintf(stderr," not updating net %#o (cost %d): have type %s, cost %d\n",
rsub, rcost, rt_typename(rttbl_net[rsub].rt_type), rttbl_net[rsub].rt_cost);
}
}
if (verbose && rttbl_updated) print_routing_table();
}
}
void
update_route_costs()
{
// update the cost of all dynamic routes by their age,
// according to AIM 628 p15.
int i;
u_int costinc = 0;
static time_t lasttime = 0;
// last time we updated the routing costs
if (lasttime == 0) lasttime = time(NULL);
PTLOCKN(rttbl_lock,"rttbl_lock");
for (i = 0; i < 256; i++) {
if (rttbl_net[i].rt_type == RT_DYNAMIC) {
/* Age by 1 every 4 seconds, max limit, but not for direct or asynch (cf AIM 628 p15) */
costinc = (time(NULL) - lasttime)/4;
if (costinc > 0) {
if (debug) fprintf(stderr,"RUT to %d, cost %d => %d\n",i,
rttbl_net[i].rt_cost, rttbl_net[i].rt_cost+costinc);
if ((rttbl_net[i].rt_cost + costinc) > RTCOST_HIGH)
rttbl_net[i].rt_cost = RTCOST_HIGH;
else
rttbl_net[i].rt_cost += costinc;
}
}
}
if (costinc > 0)
lasttime = time(NULL);
PTUNLOCKN(rttbl_lock,"rttbl_lock");
}
struct chroute *
find_in_routing_table(u_short dchad, int only_host, int also_nopath)
{
int i;
if (is_mychaddr(dchad)) {
// The only reason for defining a route with "self" as destination
// is when you have a subnet where all hosts are connected by
// CHUDP links, and you want to announce the subnet by RUT.
#if 0
if (debug || verbose)
fprintf(stderr,"Warning: find route: Looking for self in routing table.\n");
#endif
return NULL;
}
/* Check host routing table first */
for (i = 0; i < rttbl_host_len; i++) {
if ((rttbl_host[i].rt_dest == dchad)
&&
(also_nopath || /* look for old routes */
((rttbl_host[i].rt_type != RT_NOPATH)
&&
// Check the cost, too.
(rttbl_host[i].rt_cost < RTCOST_HIGH)))) {
struct chroute *rt = &rttbl_host[i];
if (debug) fprintf(stderr,"Found host route to dest %#o: %s dest %#o %s bridge %#o myaddr %#o\n", dchad,
rt_linkname(rt->rt_link), rt->rt_dest, rt_typename(rt->rt_type), rt->rt_braddr, rt->rt_myaddr);
return rt;
}
}
if (only_host) return NULL;
/* Then check subnet routing table */
u_short sub = (dchad>>8)&0xff;
if ((rttbl_net[sub].rt_type != RT_NOPATH)
// Check the cost, too.
&& (rttbl_net[sub].rt_cost < RTCOST_HIGH)) {
struct chroute *rt = &rttbl_net[sub];
if (rttbl_net[sub].rt_braddr != 0) {
if (is_mychaddr(rttbl_net[sub].rt_braddr))
// This is an announce-only route, when we have individual
// links to all hosts on the subnet - but apparently not to
// this one. Drop it.
return NULL;
if (debug) fprintf(stderr,"Found subnet %#o route to dest %#o: %s dest %#o %s bridge %#o myaddr %#o\n", sub, dchad,
rt_linkname(rt->rt_link), rt->rt_dest, rt_typename(rt->rt_type), rt->rt_braddr, rt->rt_myaddr);
return &rttbl_net[sub];
} else {
// No bridge, so directly connected subnet route, e.g. Ether
if (debug) fprintf(stderr,"Found directly connected subnet %#o route to dest %#o: %s dest %#o %s bridge %#o myaddr %#o\n",
sub, dchad,
rt_linkname(rt->rt_link), rt->rt_dest, rt_typename(rt->rt_type), rt->rt_braddr, rt->rt_myaddr);
return &rttbl_net[sub];
}
}
// no route found
return NULL;
}
void
htons_buf(u_short *ibuf, u_short *obuf, int len)
{
int i;
for (i = 0; i < len; i += 2)
*obuf++ = htons(*ibuf++);
}
void
ntohs_buf(u_short *ibuf, u_short *obuf, int len)
{
int i;
for (i = 0; i < len; i += 2)
*obuf++ = ntohs(*ibuf++);
}
// Note: this is for strings, not general data.
int
get_packet_string(struct chaos_header *pkt, u_char *out, int outsize)
{
u_short *dataw = (u_short *)&((u_char *)pkt)[CHAOS_HEADERSIZE];
int len;
if (outsize <= (ch_nbytes(pkt) + (ch_nbytes(pkt) % 2))) {
fprintf(stderr,"%%%% Warning: get_packet_string called with small outsize %d\n", outsize);
len = outsize-1;
} else {
len = ch_nbytes(pkt);
}
if (ch_opcode(pkt) == CHOP_BRD) {
// skip bitmask
if (debug) printf("BRD get_packet_string: ackno %d nbytes %d outsize %d\n",
ch_ackno(pkt), ch_nbytes(pkt), outsize);
if (ch_ackno(pkt) < len) {
dataw = (u_short *)&((u_char *)pkt)[CHAOS_HEADERSIZE+ch_ackno(pkt)];
// @@@@ check all similar pointer arithmetic for "underflow"
len -= ch_ackno(pkt);
}
}
ntohs_buf(dataw, (u_short *)out, len % 2 ? len+1 : len);
out[len] = '\0';
return len;
}
void
handle_pkt_for_me(struct chaos_header *ch, u_char *data, int dlen, u_short dchad)
{
if (ch_opcode(ch) == CHOP_RUT)
// RUT is handled separately
return;
if ((ch_opcode(ch) == CHOP_RFC) || (ch_opcode(ch) == CHOP_BRD)) {
if (verbose)
fprintf(stderr,"%s pkt for self (%#o) from <%#o,%#x> received, checking if we handle it\n",
ch_opcode_name(ch_opcode(ch)),
dchad, ch_srcaddr(ch), ch_srcindex(ch));
// see what contact they look for
if (!handle_rfc(ch, data, dlen)) {
packet_to_conn_handler(data, dlen);
}
}
else {
if (verbose) {
fprintf(stderr,"%s pkt for self (%#o) received from <%#o,%#x>",
ch_opcode_name(ch_opcode(ch)),
dchad, ch_srcaddr(ch), ch_srcindex(ch));
if (ch_opcode(ch) == CHOP_RFC) {
fprintf(stderr,"; Contact: ");
// @@@@ use generic fcn for this
int max = ch_nbytes(ch);
u_char *cp = &data[CHAOS_HEADERSIZE];
u_char *cont = (u_char *)calloc(max+1, sizeof(u_char));
if (cont != NULL) {
ch_11_gets(cp, cont, max);
char *space = index((char *)cont, ' ');
if (space) *space = '\0'; // show only contact name, not args
fprintf(stderr,"%s\n", cont);
free(cont);
} else
fprintf(stderr,"calloc(%d) failed\n", max+1);
} else
fprintf(stderr,"\n");
}
packet_to_conn_handler(data, dlen);
}
}
// **** Bridging between links
void
forward_chaos_pkt_on_route(struct chroute *rt, u_char *data, int dlen)
{
struct chaos_header *ch = (struct chaos_header *)data;
u_short dchad = ch_destaddr(ch);
u_short schad = ch_srcaddr(ch);
// Don't forward between private and non-private subnets (but OK within the same (private) subnet)
if (((schad >> 8) != (dchad >> 8)) &&
((is_private_subnet(schad >> 8) && !is_private_subnet(dchad >> 8)) ||
(is_private_subnet(dchad >> 8) && !is_private_subnet(schad >> 8)))) {
if (debug || verbose)
fprintf(stderr,"Not forwarding between private and non-private subnets: src %#o dst %#o\n",
schad, dchad);
// incf dropped packets
PTLOCKN(linktab_lock,"linktab_lock");
linktab[schad>>8].pkt_rejected++;
PTUNLOCKN(linktab_lock,"linktab_lock");
return;
}
// round up to full 16-bit word
dlen += (dlen % 2);
// Update/add trailer here.
if (dlen < CHAOS_HEADERSIZE + ch_nbytes(ch) + CHAOS_HW_TRAILERSIZE)
dlen += CHAOS_HW_TRAILERSIZE; /* add trailer if needed */
struct chaos_hw_trailer *tr = (struct chaos_hw_trailer *)&data[dlen-CHAOS_HW_TRAILERSIZE];
// HW dest is next-hop destination
if (ch_destaddr(ch) == 0)
// unless it's broadcast
tr->ch_hw_destaddr = 0;
else
tr->ch_hw_destaddr = rt->rt_braddr > 0 ? htons(rt->rt_braddr) : (rt->rt_dest > 0 ? htons(rt->rt_dest) : htons(ch_destaddr(ch)));
// HW sender is me!
// Find proper mychaddr entry if none given
if (rt->rt_myaddr <= 0) {
// Should not happen, add_to_routing_table now tries to fill it in.
tr->ch_hw_srcaddr = tr->ch_hw_destaddr > 0 ? htons(mychaddr_on_net(ntohs(tr->ch_hw_destaddr))) : htons(mychaddr[0]);
} else
tr->ch_hw_srcaddr = htons(rt->rt_myaddr);
int cks = ch_checksum(data,dlen-2); /* Don't checksum the checksum field */
tr->ch_hw_checksum = htons(cks);
switch (rt->rt_link) {
#if CHAOS_ETHERP
case LINK_ETHER:
forward_on_ether(rt, schad, dchad, ch, data, dlen);
break;
#endif // CHAOS_ETHERP
case LINK_UNIXSOCK:
forward_on_usocket(rt, schad, dchad, ch, data, dlen);
break;
#if CHAOS_TLS
case LINK_TLS:
forward_on_tls(rt, schad, dchad, ch, data, dlen);
break;
#endif
#if CHAOS_IP
case LINK_IP:
forward_on_ip(rt, schad, dchad, ch, data, dlen);
break;
#endif
case LINK_CHUDP:
forward_on_chudp(rt, schad, dchad, ch, data, dlen);
break;
default:
if (verbose) fprintf(stderr,"Can't forward pkt on bad link type %d\n", rt->rt_link);
}
}
void
forward_chaos_broadcast_on_route(struct chroute *rt, int sn, u_char *data, int dlen)
{
u_char copy[CH_PK_MAXLEN];
struct chaos_header *ch = (struct chaos_header *)data;
u_char mask[32];
memset(mask, 0, sizeof(mask));
htons_buf((u_short *)&data[CHAOS_HEADERSIZE], (u_short *)mask, ch_ackno(ch));
if (verbose) fprintf(stderr,"Forwarding %s (fc %d) from %#o to subnet %#o on %#o bridge/subnet %#o (%s)\n",
ch_opcode_name(ch_opcode(ch)),
ch_fc(ch),
ch_srcaddr(ch),
sn, rt->rt_dest, rt->rt_braddr,
rt_linkname(rt->rt_link));
#if 0
// @@@@ instead: clear bit N when receiving from subnet link for N
// clear bit if we're sending to a subnet link,
// but if it's a host link, keep it and let the other end resend it if it has further host links to that subnet
if (RT_SUBNETP(rt) && (sn < ch_ackno(ch)*8)) {
mask[sn/8] = mask[sn/8] & ~(1<<(sn % 8));
ntohs_buf((u_short *)mask, (u_short *)&data[CHAOS_HEADERSIZE], ch_ackno(ch));
}
#endif
// forward
PTLOCKN(linktab_lock,"linktab_lock");
linktab[rt->rt_dest >>8 ].pkt_out++;
PTUNLOCKN(linktab_lock,"linktab_lock");
// send a copy, since it may be swapped in the process of sending
memcpy(copy, data, dlen);
forward_chaos_pkt_on_route(rt, copy, dlen);
}
void
forward_chaos_broadcast_pkt(struct chroute *src, u_char *data, int dlen)
{
struct chaos_header *ch = (struct chaos_header *)data;
// forward on all direct links matching the requested subnets, after clearing that subnet in the mask
// EXCEPT the link it came in on
int i, sn, nsubn = ch_ackno(ch)*8;
u_short ssubnet = ch_srcaddr(ch) >> 8;
u_char mask[32];
htons_buf((u_short *)&data[CHAOS_HEADERSIZE], (u_short *)mask, ch_ackno(ch));
if (debug)
fprintf(stderr,"BRD mask %d in %02x%02x%02x%02x => %02x%02x%02x%02x\n", nsubn,
data[CHAOS_HEADERSIZE],data[CHAOS_HEADERSIZE+1],data[CHAOS_HEADERSIZE+2],data[CHAOS_HEADERSIZE+3],
mask[0], mask[1], mask[2], mask[3]);
struct chroute *rt;
PTLOCKN(rttbl_lock, "rttbl_lock");
// for all rttbl_host entries except the source, which are direct links,
// if its subnet is in the mask, forward there (after clearing bit)
for (i = 0; i < rttbl_host_len; i++) {
rt = &rttbl_host[i];
sn = (rt->rt_dest)>>8;
if ((sn != ssubnet) &&
((is_private_subnet(sn) && !is_private_subnet(ssubnet)) ||
(!is_private_subnet(sn) && is_private_subnet(ssubnet)))) {
// Don't forward between private and non-private subnets
if (debug || verbose)
fprintf(stderr,"BRD not forwarded (private) from subnet %#o to host %#o on subnet %#o\n",
ssubnet, rt->rt_dest, sn);
continue;
}
if ((sn < nsubn) && (RT_DIRECT(rt)) && (src != rt) && (mask[sn/8] & (1<<(sn % 8)))) {
forward_chaos_broadcast_on_route(rt, sn, data, dlen);
} else if (debug)
fprintf(stderr,"BRD not forwarded to %s host route %d (sn %#o, dest %#o, bit %d)\n",
RT_DIRECT(rt) ? "direct" : "indirect",
i, sn, rt->rt_dest, (mask[sn/8] & (1<<(sn % 8))));
}
// for all rttbl_net entries except the source, which are direct links
// if the subnet is set, forward there (after clearing bit)
for (sn = 1; sn < 256 && sn < nsubn; sn++) {
if ((sn != ssubnet) &&
((is_private_subnet(sn) && !is_private_subnet(ssubnet)) ||
(!is_private_subnet(sn) && is_private_subnet(ssubnet)))) {
// Don't forward between private and non-private subnets
if (debug || verbose)
fprintf(stderr,"BRD not forwarded (private) from subnet %#o to subnet %#o\n",
ssubnet, sn);
continue;
}
rt = &rttbl_net[sn];
if ((src != rt) && (rttbl_net[sn].rt_link != LINK_NOLINK) &&
(RT_DIRECT(rt)) && (mask[sn/8] & (1<<(sn % 8)))) {
forward_chaos_broadcast_on_route(rt, sn, data, dlen);
} else if (debug && rttbl_net[sn].rt_link != LINK_NOLINK)
fprintf(stderr,"BRD not forwarded to subnet %#o %s route (bit %d)\n",
sn, RT_DIRECT(rt) ? "direct" : "indirect",
(mask[sn/8] & (1<<(sn % 8))));
}
PTUNLOCKN(rttbl_lock, "rttbl_lock");
}
void
forward_chaos_pkt(struct chroute *src, u_char cost, u_char *data, int dlen, u_char src_linktype)
{
struct chaos_header *ch = (struct chaos_header *)data;
struct chaos_hw_trailer *tr = (struct chaos_hw_trailer *)&data[dlen-CHAOS_HW_TRAILERSIZE];
u_short schad = 0; /* source (for hosttab) */
u_short dchad = ch_destaddr(ch); /* destination */
u_char fwc = ch_fc(ch); /* forwarding count */
if (dlen >= CHAOS_HEADERSIZE + ch_nbytes(ch) + CHAOS_HW_TRAILERSIZE) {
// use hw trailer if available
schad = htons(tr->ch_hw_srcaddr);
} else if (src != NULL) {
// if it's a direct link, and the source is on my subnet, use it
if (RT_DIRECT(src) && ((ch_srcaddr(ch) & 0xff00) == (src->rt_myaddr & 0xff00)))
schad = ch_srcaddr(ch);
else if (RT_BRIDGED(src))
// else use the bridge's address
schad = src->rt_braddr;
else {
// don't know an address, so use the link type instead as an indication (cf dump-routing-table)
schad = (u_short)src->rt_link;
// assert(schad < 0400);
}
}
if (src != NULL) {
PTLOCKN(linktab_lock,"linktab_lock");
linktab[(src->rt_dest)>>8].pkt_in++;
PTUNLOCKN(linktab_lock,"linktab_lock");
PTLOCKN(hosttab_lock,"hosttab_lock");
struct hostat *he = find_hostat_entry(ch_srcaddr(ch));
he->hst_in++;
he->hst_last_seen = time(NULL);
he->hst_last_hop = schad;
he->hst_last_fc = ch_fc(ch);
PTUNLOCKN(hosttab_lock,"hosttab_lock");
} else if (debug)
fprintf(stderr,"No source route given in forward from %#o to %#o\n",
schad, dchad);
fwc++;
if (fwc > CH_FORWARD_MAX) { /* over-forwarded */
if (verbose) fprintf(stderr,"%%%% Dropping over-forwarded pkt for %#o\n", dchad);
if (src != NULL) {
PTLOCKN(linktab_lock,"linktab_lock");
linktab[(src->rt_dest)>>8].pkt_rejected++;
PTUNLOCKN(linktab_lock,"linktab_lock");
}
return;
}
set_ch_fc(ch,fwc); /* update */
struct chroute *rt = find_in_routing_table(dchad, 0, 0);