Repository navigation
Expand file tree
/
Copy pathipr.c
More file actions
1247 lines (1117 loc) · 26.7 KB
/
Copy pathipr.c
File metadata and controls
1247 lines (1117 loc) · 26.7 KB
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
/*
ipr.c
Copyright 1995 Philip Homburg
*/
#include "inet.h"
#include "clock.h"
#include "type.h"
#include "assert.h"
#include "buf.h"
#include "event.h"
#include "io.h"
#include "ip_int.h"
#include "ipr.h"
THIS_FILE
#define OROUTE_NR 128
#define OROUTE_STATIC_NR 16
#define OROUTE_HASH_ASS_NR 4
#define OROUTE_HASH_NR 32
#define OROUTE_HASH_MASK (OROUTE_HASH_NR-1)
#define hash_oroute(port_nr, ipaddr, hash_tmp) (hash_tmp= (ipaddr), \
hash_tmp= (hash_tmp >> 20) ^ hash_tmp, \
hash_tmp= (hash_tmp >> 10) ^ hash_tmp, \
hash_tmp= (hash_tmp >> 5) ^ hash_tmp, \
(hash_tmp + (port_nr)) & OROUTE_HASH_MASK)
typedef struct oroute_hash
{
ipaddr_t orh_addr;
oroute_t *orh_route;
} oroute_hash_t;
static oroute_t oroute_table[OROUTE_NR];
static oroute_t *oroute_head;
static int static_oroute_nr;
static oroute_hash_t oroute_hash_table[OROUTE_HASH_NR][OROUTE_HASH_ASS_NR];
#define IROUTE_NR 512
#define IROUTE_HASH_ASS_NR 4
#define IROUTE_HASH_NR 32
#define IROUTE_HASH_MASK (IROUTE_HASH_NR-1)
#define hash_iroute(port_nr, ipaddr, hash_tmp) (hash_tmp= (ipaddr), \
hash_tmp= (hash_tmp >> 20) ^ hash_tmp, \
hash_tmp= (hash_tmp >> 10) ^ hash_tmp, \
hash_tmp= (hash_tmp >> 5) ^ hash_tmp, \
(hash_tmp + (port_nr)) & IROUTE_HASH_MASK)
typedef struct iroute_hash
{
ipaddr_t irh_addr;
iroute_t *irh_route;
} iroute_hash_t;
static iroute_t iroute_table[IROUTE_NR];
static iroute_hash_t iroute_hash_table[IROUTE_HASH_NR][IROUTE_HASH_ASS_NR];
static oroute_t *oroute_find_ent ARGS(( int port_nr, ipaddr_t dest ));
static void oroute_del ARGS(( oroute_t *oroute ));
static oroute_t *sort_dists ARGS(( oroute_t *oroute ));
static oroute_t *sort_gws ARGS(( oroute_t *oroute ));
static void oroute_uncache_nw ARGS(( ipaddr_t dest, ipaddr_t netmask ));
static void iroute_uncache_nw ARGS(( ipaddr_t dest, ipaddr_t netmask ));
void ipr_init()
{
int i;
oroute_t *oroute;
iroute_t *iroute;
for (i= 0, oroute= oroute_table; i<OROUTE_NR; i++, oroute++)
oroute->ort_flags= ORTF_EMPTY;
static_oroute_nr= 0;
assert(OROUTE_HASH_ASS_NR == 4);
for (i= 0, iroute= iroute_table; i<IROUTE_NR; i++, iroute++)
iroute->irt_flags= IRTF_EMPTY;
assert(IROUTE_HASH_ASS_NR == 4);
}
iroute_t *iroute_frag(port_nr, dest)
int port_nr;
ipaddr_t dest;
{
int hash, i;
iroute_hash_t *iroute_hash;
iroute_hash_t tmp_hash;
iroute_t *iroute, *bestroute;
unsigned long hash_tmp;
u32_t tmp_mask;
hash= hash_iroute(port_nr, dest, hash_tmp);
iroute_hash= &iroute_hash_table[hash][0];
if (iroute_hash[0].irh_addr == dest)
iroute= iroute_hash[0].irh_route;
else if (iroute_hash[1].irh_addr == dest)
{
tmp_hash= iroute_hash[1];
iroute_hash[1]= iroute_hash[0];
iroute_hash[0]= tmp_hash;
iroute= tmp_hash.irh_route;
}
else if (iroute_hash[2].irh_addr == dest)
{
tmp_hash= iroute_hash[2];
iroute_hash[2]= iroute_hash[1];
iroute_hash[1]= iroute_hash[0];
iroute_hash[0]= tmp_hash;
iroute= tmp_hash.irh_route;
}
else if (iroute_hash[3].irh_addr == dest)
{
tmp_hash= iroute_hash[3];
iroute_hash[3]= iroute_hash[2];
iroute_hash[2]= iroute_hash[1];
iroute_hash[1]= iroute_hash[0];
iroute_hash[0]= tmp_hash;
iroute= tmp_hash.irh_route;
}
else
iroute= NULL;
if (iroute)
return iroute;
bestroute= NULL;
for (i= 0, iroute= iroute_table; i < IROUTE_NR; i++, iroute++)
{
if (!(iroute->irt_flags & IRTF_INUSE))
continue;
if (((dest ^ iroute->irt_dest) & iroute->irt_subnetmask) != 0)
continue;
if (!bestroute)
{
bestroute= iroute;
continue;
}
/* More specific netmasks are better */
if (iroute->irt_subnetmask != bestroute->irt_subnetmask)
{
/* Using two ntohl macros in one expression
* is not allowed (tmp_l is modified twice)
*/
tmp_mask= ntohl(iroute->irt_subnetmask);
if (tmp_mask > ntohl(bestroute->irt_subnetmask))
bestroute= iroute;
continue;
}
/* Dynamic routes override static routes */
if ((iroute->irt_flags & IRTF_STATIC) !=
(bestroute->irt_flags & IRTF_STATIC))
{
if (bestroute->irt_flags & IRTF_STATIC)
bestroute= iroute;
continue;
}
/* A route to the local interface give an opportunity
* to send redirects.
*/
if (iroute->irt_port != bestroute->irt_port)
{
if (iroute->irt_port == port_nr)
bestroute= iroute;
continue;
}
}
if (bestroute == NULL)
return NULL;
iroute_hash[3]= iroute_hash[2];
iroute_hash[2]= iroute_hash[1];
iroute_hash[1]= iroute_hash[0];
iroute_hash[0].irh_addr= dest;
iroute_hash[0].irh_route= bestroute;
return bestroute;
}
int oroute_frag(port_nr, dest, ttl, msgsize, nexthop)
int port_nr;
ipaddr_t dest;
int ttl;
size_t msgsize;
ipaddr_t *nexthop;
{
oroute_t *oroute;
oroute= oroute_find_ent(port_nr, dest);
if (!oroute || oroute->ort_dist > ttl)
return ENETUNREACH;
if (msgsize && oroute->ort_mtu &&
oroute->ort_mtu < msgsize)
{
return EPACKSIZE;
}
*nexthop= oroute->ort_gateway;
return NW_OK;
}
int ipr_add_oroute(port_nr, dest, subnetmask, gateway,
timeout, dist, mtu, static_route, preference, oroute_p)
int port_nr;
ipaddr_t dest;
ipaddr_t subnetmask;
ipaddr_t gateway;
time_t timeout;
int dist;
int mtu;
int static_route;
i32_t preference;
oroute_t **oroute_p;
{
int i;
ip_port_t *ip_port;
oroute_t *oroute, *oldest_route, *prev, *nw_route, *gw_route,
*prev_route;
time_t currtim, exp_tim, exp_tim_orig;
oldest_route= 0;
currtim= get_time();
if (timeout)
exp_tim= timeout+currtim;
else
exp_tim= 0;
DBLOCK(0x10,
printf("ip[%d]: adding oroute to ", port_nr);
writeIpAddr(dest);
printf("["); writeIpAddr(subnetmask); printf("] through ");
writeIpAddr(gateway);
printf(" timeout: %lds, distance %d, pref %ld, mtu %d\n",
(long)timeout/HZ, dist, (long)preference, mtu));
ip_port= &ip_port_table[port_nr];
/* Validate gateway */
if (((gateway ^ ip_port->ip_ipaddr) & ip_port->ip_subnetmask) != 0)
{
DBLOCK(1, printf("ip[%d]: (ipr_add_oroute) invalid gateway: ",
port_nr); writeIpAddr(gateway); printf("\n"));
return EINVAL;
}
if (static_route)
{
if (static_oroute_nr >= OROUTE_STATIC_NR)
return ENOMEM;
static_oroute_nr++;
}
else
{
/* Try to track down any old routes. */
for(oroute= oroute_head; oroute; oroute= oroute->ort_nextnw)
{
if (oroute->ort_port != port_nr)
continue;
if (oroute->ort_dest == dest &&
oroute->ort_subnetmask == subnetmask)
{
break;
}
}
for(; oroute; oroute= oroute->ort_nextgw)
{
if (oroute->ort_gateway == gateway)
break;
}
for(; oroute; oroute= oroute->ort_nextdist)
{
if ((oroute->ort_flags & ORTF_STATIC) != 0)
continue;
if (oroute->ort_dist > dist)
continue;
break;
}
if (oroute)
{
assert(oroute->ort_port == port_nr);
if (dest != 0)
{
/* The new expire should not be later
* than the old expire time. Except for
* default routes, where the expire time
* is simple set to the new value.
*/
exp_tim_orig= oroute->ort_exp_tim;
if (!exp_tim)
exp_tim= exp_tim_orig;
else if (exp_tim_orig &&
exp_tim > exp_tim_orig)
{
exp_tim= exp_tim_orig;
}
}
oroute_del(oroute);
oroute->ort_flags= 0;
oldest_route= oroute;
}
}
if (oldest_route == NULL)
{
/* Look for an unused entry, or remove an existing one */
for (i= 0, oroute= oroute_table; i<OROUTE_NR; i++, oroute++)
{
if ((oroute->ort_flags & ORTF_INUSE) == 0)
break;
if (oroute->ort_exp_tim && oroute->ort_exp_tim <
currtim)
{
oroute_del(oroute);
oroute->ort_flags= 0;
break;
}
if (oroute->ort_flags & ORTF_STATIC)
continue;
if (oroute->ort_dest == 0)
{
/* Never remove default routes. */
continue;
}
if (oldest_route == NULL)
{
oldest_route= oroute;
continue;
}
if (oroute->ort_timestamp < oldest_route->ort_timestamp)
{
oldest_route= oroute;
}
}
if (i < OROUTE_NR)
oldest_route= oroute;
else
{
assert(oldest_route);
oroute_del(oldest_route);
oldest_route->ort_flags= 0;
}
}
oldest_route->ort_dest= dest;
oldest_route->ort_gateway= gateway;
oldest_route->ort_subnetmask= subnetmask;
oldest_route->ort_exp_tim= exp_tim;
oldest_route->ort_timestamp= currtim;
oldest_route->ort_dist= dist;
oldest_route->ort_mtu= mtu;
oldest_route->ort_port= port_nr;
oldest_route->ort_flags= ORTF_INUSE;
oldest_route->ort_pref= preference;
if (static_route)
oldest_route->ort_flags |= ORTF_STATIC;
/* Insert the route by tearing apart the routing table,
* and insert the entry during the reconstruction.
*/
for (prev= 0, nw_route= oroute_head; nw_route;
prev= nw_route, nw_route= nw_route->ort_nextnw)
{
if (nw_route->ort_port != port_nr)
continue;
if (nw_route->ort_dest == dest &&
nw_route->ort_subnetmask == subnetmask)
{
if (prev)
prev->ort_nextnw= nw_route->ort_nextnw;
else
oroute_head= nw_route->ort_nextnw;
break;
}
}
prev_route= nw_route;
for(prev= NULL, gw_route= nw_route; gw_route;
prev= gw_route, gw_route= gw_route->ort_nextgw)
{
if (gw_route->ort_gateway == gateway)
{
if (prev)
prev->ort_nextgw= gw_route->ort_nextgw;
else
nw_route= gw_route->ort_nextgw;
break;
}
}
oldest_route->ort_nextdist= gw_route;
gw_route= oldest_route;
gw_route= sort_dists(gw_route);
gw_route->ort_nextgw= nw_route;
nw_route= gw_route;
nw_route= sort_gws(nw_route);
nw_route->ort_nextnw= oroute_head;
oroute_head= nw_route;
if (nw_route != prev_route)
oroute_uncache_nw(nw_route->ort_dest, nw_route->ort_subnetmask);
if (oroute_p != NULL)
*oroute_p= oldest_route;
return NW_OK;
}
int ipr_del_oroute(port_nr, dest, subnetmask, gateway, static_route)
int port_nr;
ipaddr_t dest;
ipaddr_t subnetmask;
ipaddr_t gateway;
int static_route;
{
int i;
oroute_t *oroute;
for(i= 0, oroute= oroute_table; i<OROUTE_NR; i++, oroute++)
{
if ((oroute->ort_flags & ORTF_INUSE) == 0)
continue;
if (oroute->ort_port != port_nr ||
oroute->ort_dest != dest ||
oroute->ort_subnetmask != subnetmask ||
oroute->ort_gateway != gateway)
{
continue;
}
if (!!(oroute->ort_flags & ORTF_STATIC) != static_route)
continue;
break;
}
if (i == OROUTE_NR)
return ESRCH;
if (static_route)
static_oroute_nr--;
oroute_del(oroute);
oroute->ort_flags &= ~ORTF_INUSE;
return NW_OK;
}
void ipr_chk_otab(port_nr, addr, mask)
int port_nr;
ipaddr_t addr;
ipaddr_t mask;
{
int i;
oroute_t *oroute;
DBLOCK(1,
printf("ip[%d] (ipr_chk_otab): addr ", port_nr);
writeIpAddr(addr);
printf(" mask ");
writeIpAddr(mask);
printf("\n");
);
if (addr == 0)
{
/* Special hack to flush entries for an interface that
* goes down.
*/
addr= mask= HTONL(0xffffffff);
}
for(i= 0, oroute= oroute_table; i<OROUTE_NR; i++, oroute++)
{
if ((oroute->ort_flags & ORTF_INUSE) == 0)
continue;
if (oroute->ort_port != port_nr ||
((oroute->ort_gateway ^ addr) & mask) == 0)
{
continue;
}
DBLOCK(1, printf("ip[%d] (ipr_chk_otab): deleting route to ",
port_nr);
writeIpAddr(oroute->ort_dest);
printf(" gw ");
writeIpAddr(oroute->ort_gateway);
printf("\n"));
if (oroute->ort_flags & ORTF_STATIC)
static_oroute_nr--;
oroute_del(oroute);
oroute->ort_flags &= ~ORTF_INUSE;
}
}
void ipr_gateway_down(port_nr, gateway, timeout)
int port_nr;
ipaddr_t gateway;
time_t timeout;
{
oroute_t *route_ind;
time_t currtim;
int i;
int result;
currtim= get_time();
for (i= 0, route_ind= oroute_table; i<OROUTE_NR; i++, route_ind++)
{
if (!(route_ind->ort_flags & ORTF_INUSE))
continue;
if (route_ind->ort_gateway != gateway)
continue;
if (route_ind->ort_exp_tim && route_ind->ort_exp_tim < currtim)
continue;
result= ipr_add_oroute(port_nr, route_ind->ort_dest,
route_ind->ort_subnetmask, gateway,
timeout, ORTD_UNREACHABLE, route_ind->ort_mtu,
FALSE, 0, NULL);
assert(result == NW_OK);
}
}
void ipr_destunrch(port_nr, dest, netmask, timeout)
int port_nr;
ipaddr_t dest;
ipaddr_t netmask;
time_t timeout;
{
oroute_t *oroute;
int result;
oroute= oroute_find_ent(port_nr, dest);
if (!oroute)
{
DBLOCK(1, printf("ip[%d]: got a dest unreachable for ",
port_nr);
writeIpAddr(dest); printf("but no route present\n"));
return;
}
result= ipr_add_oroute(port_nr, dest, netmask, oroute->ort_gateway,
timeout, ORTD_UNREACHABLE, oroute->ort_mtu, FALSE, 0, NULL);
assert(result == NW_OK);
}
void ipr_redirect(port_nr, dest, netmask, old_gateway, new_gateway,
timeout)
int port_nr;
ipaddr_t dest;
ipaddr_t netmask;
ipaddr_t old_gateway;
ipaddr_t new_gateway;
time_t timeout;
{
oroute_t *oroute;
ip_port_t *ip_port;
int result;
ip_port= &ip_port_table[port_nr];
oroute= oroute_find_ent(port_nr, dest);
if (!oroute)
{
DBLOCK(1, printf("ip[%d]: got a redirect for ", port_nr);
writeIpAddr(dest); printf("but no route present\n"));
return;
}
if (oroute->ort_gateway != old_gateway)
{
DBLOCK(1, printf("ip[%d]: got a redirect from ", port_nr);
writeIpAddr(old_gateway); printf(" for ");
writeIpAddr(dest); printf(" but curr gateway is ");
writeIpAddr(oroute->ort_gateway); printf("\n"));
return;
}
if ((new_gateway ^ ip_port->ip_ipaddr) & ip_port->ip_subnetmask)
{
DBLOCK(1, printf("ip[%d]: redirect from ", port_nr);
writeIpAddr(old_gateway); printf(" for ");
writeIpAddr(dest); printf(" but new gateway ");
writeIpAddr(new_gateway);
printf(" is not on local subnet\n"));
return;
}
if (oroute->ort_flags & ORTF_STATIC)
{
if (oroute->ort_dest == dest)
{
DBLOCK(1, printf("ip[%d]: got a redirect for ",
port_nr);
writeIpAddr(dest);
printf("but route is fixed\n"));
return;
}
}
else
{
result= ipr_add_oroute(port_nr, dest, netmask,
oroute->ort_gateway, HZ, ORTD_UNREACHABLE,
oroute->ort_mtu, FALSE, 0, NULL);
assert(result == NW_OK);
}
result= ipr_add_oroute(port_nr, dest, netmask, new_gateway,
timeout, 1, oroute->ort_mtu, FALSE, 0, NULL);
assert(result == NW_OK);
}
void ipr_ttl_exc(port_nr, dest, netmask, timeout)
int port_nr;
ipaddr_t dest;
ipaddr_t netmask;
time_t timeout;
{
oroute_t *oroute;
int new_dist;
int result;
oroute= oroute_find_ent(port_nr, dest);
if (!oroute)
{
DBLOCK(1, printf("ip[%d]: got a ttl exceeded for ",
port_nr);
writeIpAddr(dest); printf("but no route present\n"));
return;
}
new_dist= oroute->ort_dist * 2;
if (new_dist > IP_DEF_TTL)
{
new_dist= oroute->ort_dist+1;
if (new_dist >= IP_DEF_TTL)
{
DBLOCK(1, printf("ip[%d]: got a ttl exceeded for ",
port_nr);
writeIpAddr(dest);
printf(" but dist is %d\n",
oroute->ort_dist));
return;
}
}
result= ipr_add_oroute(port_nr, dest, netmask, oroute->ort_gateway,
timeout, new_dist, oroute->ort_mtu, FALSE, 0, NULL);
assert(result == NW_OK);
}
void ipr_mtu(
int port_nr,
ipaddr_t dest,
u16_t mtu,
time_t timeout
)
{
oroute_t *oroute;
int result;
oroute= oroute_find_ent(port_nr, dest);
if (!oroute)
{
DBLOCK(1, printf("ip[%d]: got a mtu exceeded for ",
port_nr);
writeIpAddr(dest); printf("but no route present\n"));
return;
}
if (mtu < IP_MIN_MTU)
return;
if (oroute->ort_mtu && mtu >= oroute->ort_mtu)
return; /* Only decrease mtu */
result= ipr_add_oroute(port_nr, dest, HTONL(0xffffffff),
oroute->ort_gateway, timeout, oroute->ort_dist, mtu,
FALSE, 0, NULL);
assert(result == NW_OK);
}
int ipr_get_oroute(ent_no, route_ent)
int ent_no;
nwio_route_t *route_ent;
{
oroute_t *oroute;
if (ent_no<0 || ent_no>= OROUTE_NR)
return ENOENT;
oroute= &oroute_table[ent_no];
if ((oroute->ort_flags & ORTF_INUSE) && oroute->ort_exp_tim &&
oroute->ort_exp_tim < get_time())
{
oroute_del(oroute);
oroute->ort_flags &= ~ORTF_INUSE;
}
route_ent->nwr_ent_no= ent_no;
route_ent->nwr_ent_count= OROUTE_NR;
route_ent->nwr_dest= oroute->ort_dest;
route_ent->nwr_netmask= oroute->ort_subnetmask;
route_ent->nwr_gateway= oroute->ort_gateway;
route_ent->nwr_dist= oroute->ort_dist;
route_ent->nwr_flags= NWRF_EMPTY;
if (oroute->ort_flags & ORTF_INUSE)
{
route_ent->nwr_flags |= NWRF_INUSE;
if (oroute->ort_flags & ORTF_STATIC)
route_ent->nwr_flags |= NWRF_STATIC;
}
route_ent->nwr_pref= oroute->ort_pref;
route_ent->nwr_mtu= oroute->ort_mtu;
route_ent->nwr_ifaddr= ip_get_ifaddr(oroute->ort_port);
return NW_OK;
}
static oroute_t *oroute_find_ent(port_nr, dest)
int port_nr;
ipaddr_t dest;
{
int hash;
oroute_hash_t *oroute_hash;
oroute_hash_t tmp_hash;
oroute_t *oroute, *bestroute;
time_t currtim;
unsigned long hash_tmp;
u32_t tmp_mask;
currtim= get_time();
hash= hash_oroute(port_nr, dest, hash_tmp);
oroute_hash= &oroute_hash_table[hash][0];
if (oroute_hash[0].orh_addr == dest)
oroute= oroute_hash[0].orh_route;
else if (oroute_hash[1].orh_addr == dest)
{
tmp_hash= oroute_hash[1];
oroute_hash[1]= oroute_hash[0];
oroute_hash[0]= tmp_hash;
oroute= tmp_hash.orh_route;
}
else if (oroute_hash[2].orh_addr == dest)
{
tmp_hash= oroute_hash[2];
oroute_hash[2]= oroute_hash[1];
oroute_hash[1]= oroute_hash[0];
oroute_hash[0]= tmp_hash;
oroute= tmp_hash.orh_route;
}
else if (oroute_hash[3].orh_addr == dest)
{
tmp_hash= oroute_hash[3];
oroute_hash[3]= oroute_hash[2];
oroute_hash[2]= oroute_hash[1];
oroute_hash[1]= oroute_hash[0];
oroute_hash[0]= tmp_hash;
oroute= tmp_hash.orh_route;
}
else
oroute= NULL;
if (oroute)
{
assert(oroute->ort_port == port_nr);
if (oroute->ort_exp_tim && oroute->ort_exp_tim<currtim)
{
oroute_del(oroute);
oroute->ort_flags &= ~ORTF_INUSE;
}
else
return oroute;
}
bestroute= NULL;
for (oroute= oroute_head; oroute; oroute= oroute->ort_nextnw)
{
if (((dest ^ oroute->ort_dest) & oroute->ort_subnetmask) != 0)
continue;
if (oroute->ort_port != port_nr)
continue;
if (!bestroute)
{
bestroute= oroute;
continue;
}
assert(oroute->ort_dest != bestroute->ort_dest);
/* Using two ntohl macros in one expression
* is not allowed (tmp_l is modified twice)
*/
tmp_mask= ntohl(oroute->ort_subnetmask);
if (tmp_mask > ntohl(bestroute->ort_subnetmask))
{
bestroute= oroute;
continue;
}
}
if (bestroute == NULL)
return NULL;
oroute_hash[3]= oroute_hash[2];
oroute_hash[2]= oroute_hash[1];
oroute_hash[1]= oroute_hash[0];
oroute_hash[0].orh_addr= dest;
oroute_hash[0].orh_route= bestroute;
return bestroute;
}
static void oroute_del(oroute)
oroute_t *oroute;
{
oroute_t *prev, *nw_route, *gw_route, *dist_route, *prev_route;
DBLOCK(0x10,
printf("ip[%d]: deleting oroute to ", oroute->ort_port);
writeIpAddr(oroute->ort_dest);
printf("["); writeIpAddr(oroute->ort_subnetmask);
printf("] through ");
writeIpAddr(oroute->ort_gateway);
printf(
" timestamp %lds, timeout: %lds, distance %d pref %ld mtu %ld ",
(long)oroute->ort_timestamp/HZ,
(long)oroute->ort_exp_tim/HZ, oroute->ort_dist,
(long)oroute->ort_pref, (long)oroute->ort_mtu);
printf("flags 0x%x\n", oroute->ort_flags));
for (prev= NULL, nw_route= oroute_head; nw_route;
prev= nw_route, nw_route= nw_route->ort_nextnw)
{
if (oroute->ort_port == nw_route->ort_port &&
oroute->ort_dest == nw_route->ort_dest &&
oroute->ort_subnetmask == nw_route->ort_subnetmask)
{
break;
}
}
assert(nw_route);
if (prev)
prev->ort_nextnw= nw_route->ort_nextnw;
else
oroute_head= nw_route->ort_nextnw;
prev_route= nw_route;
for (prev= NULL, gw_route= nw_route; gw_route;
prev= gw_route, gw_route= gw_route->ort_nextgw)
{
if (oroute->ort_gateway == gw_route->ort_gateway)
break;
}
assert(gw_route);
if (prev)
prev->ort_nextgw= gw_route->ort_nextgw;
else
nw_route= gw_route->ort_nextgw;
for (prev= NULL, dist_route= gw_route; dist_route;
prev= dist_route, dist_route= dist_route->ort_nextdist)
{
if (oroute == dist_route)
break;
}
assert(dist_route);
if (prev)
prev->ort_nextdist= dist_route->ort_nextdist;
else
gw_route= dist_route->ort_nextdist;
gw_route= sort_dists(gw_route);
if (gw_route != NULL)
{
gw_route->ort_nextgw= nw_route;
nw_route= gw_route;
}
nw_route= sort_gws(nw_route);
if (nw_route != NULL)
{
nw_route->ort_nextnw= oroute_head;
oroute_head= nw_route;
}
if (nw_route != prev_route)
{
oroute_uncache_nw(prev_route->ort_dest,
prev_route->ort_subnetmask);
}
}
static oroute_t *sort_dists(oroute)
oroute_t *oroute;
{
oroute_t *r, *prev, *best, *best_prev;
int best_dist, best_pref;
best= NULL;
best_dist= best_pref= 0;
best_prev= NULL;
for (prev= NULL, r= oroute; r; prev= r, r= r->ort_nextdist)
{
if (best == NULL)
; /* Force assignment to best */
else if (r->ort_dist != best_dist)
{
if (r->ort_dist > best_dist)
continue;
}
else
{
if (r->ort_pref <= best_pref)
continue;
}
best= r;
best_prev= prev;
best_dist= r->ort_dist;
best_pref= r->ort_pref;
}
if (!best)
{
assert(oroute == NULL);
return oroute;
}
if (!best_prev)
{
assert(best == oroute);
return oroute;
}
best_prev->ort_nextdist= best->ort_nextdist;
best->ort_nextdist= oroute;
return best;
}
static oroute_t *sort_gws(oroute)
oroute_t *oroute;
{
oroute_t *r, *prev, *best, *best_prev;
int best_dist, best_pref;
best= NULL;
best_dist= best_pref= 0;
best_prev= NULL;
for (prev= NULL, r= oroute; r; prev= r, r= r->ort_nextgw)
{
if (best == NULL)
; /* Force assignment to best */
else if (r->ort_dist != best_dist)
{
if (r->ort_dist > best_dist)
continue;
}
else
{
if (r->ort_pref <= best_pref)
continue;
}
best= r;
best_prev= prev;
best_dist= r->ort_dist;
best_pref= r->ort_pref;
}
if (!best)
{
assert(oroute == NULL);
return oroute;
}
if (!best_prev)
{
assert(best == oroute);
return oroute;
}
best_prev->ort_nextgw= best->ort_nextgw;
best->ort_nextgw= oroute;
return best;
}
static void oroute_uncache_nw(dest, netmask)
ipaddr_t dest;
ipaddr_t netmask;
{
int i, j;
oroute_hash_t *oroute_hash;
for (i= 0, oroute_hash= &oroute_hash_table[0][0];
i<OROUTE_HASH_NR; i++, oroute_hash += OROUTE_HASH_ASS_NR)
{
for (j= 0; j<OROUTE_HASH_ASS_NR; j++)
{
if (((oroute_hash[j].orh_addr ^ dest) & netmask) == 0)
{
oroute_hash[j].orh_addr= 0;
oroute_hash[j].orh_route= NULL;
}
}
}
}