forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathripd.c
3774 lines (3226 loc) · 99.2 KB
/
ripd.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
/* RIP version 1 and 2.
* Copyright (C) 2005 6WIND <alain.ritoux@6wind.com>
* Copyright (C) 1997, 98, 99 Kunihiro Ishiguro <kunihiro@zebra.org>
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#include "vrf.h"
#include "if.h"
#include "command.h"
#include "prefix.h"
#include "table.h"
#include "thread.h"
#include "memory.h"
#include "log.h"
#include "stream.h"
#include "filter.h"
#include "sockunion.h"
#include "sockopt.h"
#include "routemap.h"
#include "if_rmap.h"
#include "plist.h"
#include "distribute.h"
#ifdef CRYPTO_INTERNAL
#include "md5.h"
#endif
#include "keychain.h"
#include "privs.h"
#include "lib_errors.h"
#include "northbound_cli.h"
#include "network.h"
#include "ripd/ripd.h"
#include "ripd/rip_nb.h"
#include "ripd/rip_debug.h"
#include "ripd/rip_errors.h"
#include "ripd/rip_interface.h"
/* UDP receive buffer size */
#define RIP_UDP_RCV_BUF 41600
DEFINE_MGROUP(RIPD, "ripd")
DEFINE_MTYPE_STATIC(RIPD, RIP, "RIP structure")
DEFINE_MTYPE_STATIC(RIPD, RIP_VRF_NAME, "RIP VRF name")
DEFINE_MTYPE_STATIC(RIPD, RIP_INFO, "RIP route info")
DEFINE_MTYPE_STATIC(RIPD, RIP_DISTANCE, "RIP distance")
/* Prototypes. */
static void rip_output_process(struct connected *, struct sockaddr_in *, int,
uint8_t);
static int rip_triggered_update(struct thread *);
static int rip_update_jitter(unsigned long);
static void rip_distance_table_node_cleanup(struct route_table *table,
struct route_node *node);
static void rip_instance_enable(struct rip *rip, struct vrf *vrf, int sock);
static void rip_instance_disable(struct rip *rip);
static void rip_distribute_update(struct distribute_ctx *ctx,
struct distribute *dist);
static void rip_if_rmap_update(struct if_rmap_ctx *ctx,
struct if_rmap *if_rmap);
/* RIP output routes type. */
enum { rip_all_route, rip_changed_route };
/* RIP command strings. */
static const struct message rip_msg[] = {{RIP_REQUEST, "REQUEST"},
{RIP_RESPONSE, "RESPONSE"},
{RIP_TRACEON, "TRACEON"},
{RIP_TRACEOFF, "TRACEOFF"},
{RIP_POLL, "POLL"},
{RIP_POLL_ENTRY, "POLL ENTRY"},
{0}};
/* Generate rb-tree of RIP instances. */
static inline int rip_instance_compare(const struct rip *a, const struct rip *b)
{
return strcmp(a->vrf_name, b->vrf_name);
}
RB_GENERATE(rip_instance_head, rip, entry, rip_instance_compare)
struct rip_instance_head rip_instances = RB_INITIALIZER(&rip_instances);
/* Utility function to set boradcast option to the socket. */
static int sockopt_broadcast(int sock)
{
int ret;
int on = 1;
ret = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&on,
sizeof(on));
if (ret < 0) {
zlog_warn("can't set sockopt SO_BROADCAST to socket %d", sock);
return -1;
}
return 0;
}
int rip_route_rte(struct rip_info *rinfo)
{
return (rinfo->type == ZEBRA_ROUTE_RIP
&& rinfo->sub_type == RIP_ROUTE_RTE);
}
static struct rip_info *rip_info_new(void)
{
return XCALLOC(MTYPE_RIP_INFO, sizeof(struct rip_info));
}
void rip_info_free(struct rip_info *rinfo)
{
XFREE(MTYPE_RIP_INFO, rinfo);
}
struct rip *rip_info_get_instance(const struct rip_info *rinfo)
{
return route_table_get_info(rinfo->rp->table);
}
/* RIP route garbage collect timer. */
static int rip_garbage_collect(struct thread *t)
{
struct rip_info *rinfo;
struct route_node *rp;
rinfo = THREAD_ARG(t);
rinfo->t_garbage_collect = NULL;
/* Off timeout timer. */
RIP_TIMER_OFF(rinfo->t_timeout);
/* Get route_node pointer. */
rp = rinfo->rp;
/* Unlock route_node. */
listnode_delete(rp->info, rinfo);
if (list_isempty((struct list *)rp->info)) {
list_delete((struct list **)&rp->info);
route_unlock_node(rp);
}
/* Free RIP routing information. */
rip_info_free(rinfo);
return 0;
}
static void rip_timeout_update(struct rip *rip, struct rip_info *rinfo);
/* Add new route to the ECMP list.
* RETURN: the new entry added in the list, or NULL if it is not the first
* entry and ECMP is not allowed.
*/
struct rip_info *rip_ecmp_add(struct rip *rip, struct rip_info *rinfo_new)
{
struct route_node *rp = rinfo_new->rp;
struct rip_info *rinfo = NULL;
struct list *list = NULL;
if (rp->info == NULL)
rp->info = list_new();
list = (struct list *)rp->info;
/* If ECMP is not allowed and some entry already exists in the list,
* do nothing. */
if (listcount(list) && !rip->ecmp)
return NULL;
rinfo = rip_info_new();
memcpy(rinfo, rinfo_new, sizeof(struct rip_info));
listnode_add(list, rinfo);
if (rip_route_rte(rinfo)) {
rip_timeout_update(rip, rinfo);
rip_zebra_ipv4_add(rip, rp);
}
/* Set the route change flag on the first entry. */
rinfo = listgetdata(listhead(list));
SET_FLAG(rinfo->flags, RIP_RTF_CHANGED);
/* Signal the output process to trigger an update (see section 2.5). */
rip_event(rip, RIP_TRIGGERED_UPDATE, 0);
return rinfo;
}
/* Replace the ECMP list with the new route.
* RETURN: the new entry added in the list
*/
struct rip_info *rip_ecmp_replace(struct rip *rip, struct rip_info *rinfo_new)
{
struct route_node *rp = rinfo_new->rp;
struct list *list = (struct list *)rp->info;
struct rip_info *rinfo = NULL, *tmp_rinfo = NULL;
struct listnode *node = NULL, *nextnode = NULL;
if (list == NULL || listcount(list) == 0)
return rip_ecmp_add(rip, rinfo_new);
/* Get the first entry */
rinfo = listgetdata(listhead(list));
/* Learnt route replaced by a local one. Delete it from zebra. */
if (rip_route_rte(rinfo) && !rip_route_rte(rinfo_new))
if (CHECK_FLAG(rinfo->flags, RIP_RTF_FIB))
rip_zebra_ipv4_delete(rip, rp);
/* Re-use the first entry, and delete the others. */
for (ALL_LIST_ELEMENTS(list, node, nextnode, tmp_rinfo))
if (tmp_rinfo != rinfo) {
RIP_TIMER_OFF(tmp_rinfo->t_timeout);
RIP_TIMER_OFF(tmp_rinfo->t_garbage_collect);
list_delete_node(list, node);
rip_info_free(tmp_rinfo);
}
RIP_TIMER_OFF(rinfo->t_timeout);
RIP_TIMER_OFF(rinfo->t_garbage_collect);
memcpy(rinfo, rinfo_new, sizeof(struct rip_info));
if (rip_route_rte(rinfo)) {
rip_timeout_update(rip, rinfo);
/* The ADD message implies an update. */
rip_zebra_ipv4_add(rip, rp);
}
/* Set the route change flag. */
SET_FLAG(rinfo->flags, RIP_RTF_CHANGED);
/* Signal the output process to trigger an update (see section 2.5). */
rip_event(rip, RIP_TRIGGERED_UPDATE, 0);
return rinfo;
}
/* Delete one route from the ECMP list.
* RETURN:
* null - the entry is freed, and other entries exist in the list
* the entry - the entry is the last one in the list; its metric is set
* to INFINITY, and the garbage collector is started for it
*/
struct rip_info *rip_ecmp_delete(struct rip *rip, struct rip_info *rinfo)
{
struct route_node *rp = rinfo->rp;
struct list *list = (struct list *)rp->info;
RIP_TIMER_OFF(rinfo->t_timeout);
if (listcount(list) > 1) {
/* Some other ECMP entries still exist. Just delete this entry.
*/
RIP_TIMER_OFF(rinfo->t_garbage_collect);
listnode_delete(list, rinfo);
if (rip_route_rte(rinfo)
&& CHECK_FLAG(rinfo->flags, RIP_RTF_FIB))
/* The ADD message implies the update. */
rip_zebra_ipv4_add(rip, rp);
rip_info_free(rinfo);
rinfo = NULL;
} else {
assert(rinfo == listgetdata(listhead(list)));
/* This is the only entry left in the list. We must keep it in
* the list for garbage collection time, with INFINITY metric.
*/
rinfo->metric = RIP_METRIC_INFINITY;
RIP_TIMER_ON(rinfo->t_garbage_collect, rip_garbage_collect,
rip->garbage_time);
if (rip_route_rte(rinfo)
&& CHECK_FLAG(rinfo->flags, RIP_RTF_FIB))
rip_zebra_ipv4_delete(rip, rp);
}
/* Set the route change flag on the first entry. */
rinfo = listgetdata(listhead(list));
SET_FLAG(rinfo->flags, RIP_RTF_CHANGED);
/* Signal the output process to trigger an update (see section 2.5). */
rip_event(rip, RIP_TRIGGERED_UPDATE, 0);
return rinfo;
}
/* Timeout RIP routes. */
static int rip_timeout(struct thread *t)
{
struct rip_info *rinfo = THREAD_ARG(t);
struct rip *rip = rip_info_get_instance(rinfo);
rip_ecmp_delete(rip, rinfo);
return 0;
}
static void rip_timeout_update(struct rip *rip, struct rip_info *rinfo)
{
if (rinfo->metric != RIP_METRIC_INFINITY) {
RIP_TIMER_OFF(rinfo->t_timeout);
thread_add_timer(master, rip_timeout, rinfo, rip->timeout_time,
&rinfo->t_timeout);
}
}
static int rip_filter(int rip_distribute, struct prefix_ipv4 *p,
struct rip_interface *ri)
{
struct distribute *dist;
struct access_list *alist;
struct prefix_list *plist;
int distribute = rip_distribute == RIP_FILTER_OUT ? DISTRIBUTE_V4_OUT
: DISTRIBUTE_V4_IN;
const char *inout = rip_distribute == RIP_FILTER_OUT ? "out" : "in";
/* Input distribute-list filtering. */
if (ri->list[rip_distribute]) {
if (access_list_apply(ri->list[rip_distribute],
(struct prefix *)p)
== FILTER_DENY) {
if (IS_RIP_DEBUG_PACKET)
zlog_debug("%s/%d filtered by distribute %s",
inet_ntoa(p->prefix), p->prefixlen,
inout);
return -1;
}
}
if (ri->prefix[rip_distribute]) {
if (prefix_list_apply(ri->prefix[rip_distribute],
(struct prefix *)p)
== PREFIX_DENY) {
if (IS_RIP_DEBUG_PACKET)
zlog_debug("%s/%d filtered by prefix-list %s",
inet_ntoa(p->prefix), p->prefixlen,
inout);
return -1;
}
}
/* All interface filter check. */
dist = distribute_lookup(ri->rip->distribute_ctx, NULL);
if (dist) {
if (dist->list[distribute]) {
alist = access_list_lookup(AFI_IP,
dist->list[distribute]);
if (alist) {
if (access_list_apply(alist, (struct prefix *)p)
== FILTER_DENY) {
if (IS_RIP_DEBUG_PACKET)
zlog_debug(
"%s/%d filtered by distribute %s",
inet_ntoa(p->prefix),
p->prefixlen, inout);
return -1;
}
}
}
if (dist->prefix[distribute]) {
plist = prefix_list_lookup(AFI_IP,
dist->prefix[distribute]);
if (plist) {
if (prefix_list_apply(plist, (struct prefix *)p)
== PREFIX_DENY) {
if (IS_RIP_DEBUG_PACKET)
zlog_debug(
"%s/%d filtered by prefix-list %s",
inet_ntoa(p->prefix),
p->prefixlen, inout);
return -1;
}
}
}
}
return 0;
}
/* Check nexthop address validity. */
static int rip_nexthop_check(struct rip *rip, struct in_addr *addr)
{
struct interface *ifp;
struct listnode *cnode;
struct connected *ifc;
struct prefix *p;
/* If nexthop address matches local configured address then it is
invalid nexthop. */
FOR_ALL_INTERFACES (rip->vrf, ifp) {
for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, ifc)) {
p = ifc->address;
if (p->family == AF_INET
&& IPV4_ADDR_SAME(&p->u.prefix4, addr))
return -1;
}
}
return 0;
}
/* RIP add route to routing table. */
static void rip_rte_process(struct rte *rte, struct sockaddr_in *from,
struct interface *ifp)
{
struct rip *rip;
int ret;
struct prefix_ipv4 p;
struct route_node *rp;
struct rip_info *rinfo = NULL, newinfo;
struct rip_interface *ri;
struct in_addr *nexthop;
int same = 0;
unsigned char old_dist, new_dist;
struct list *list = NULL;
struct listnode *node = NULL;
/* Make prefix structure. */
memset(&p, 0, sizeof(struct prefix_ipv4));
p.family = AF_INET;
p.prefix = rte->prefix;
p.prefixlen = ip_masklen(rte->mask);
/* Make sure mask is applied. */
apply_mask_ipv4(&p);
ri = ifp->info;
rip = ri->rip;
/* Apply input filters. */
ret = rip_filter(RIP_FILTER_IN, &p, ri);
if (ret < 0)
return;
memset(&newinfo, 0, sizeof(newinfo));
newinfo.type = ZEBRA_ROUTE_RIP;
newinfo.sub_type = RIP_ROUTE_RTE;
newinfo.nh.gate.ipv4 = rte->nexthop;
newinfo.from = from->sin_addr;
newinfo.nh.ifindex = ifp->ifindex;
newinfo.nh.type = NEXTHOP_TYPE_IPV4_IFINDEX;
newinfo.metric = rte->metric;
newinfo.metric_out = rte->metric; /* XXX */
newinfo.tag = ntohs(rte->tag); /* XXX */
/* Modify entry according to the interface routemap. */
if (ri->routemap[RIP_FILTER_IN]) {
/* The object should be of the type of rip_info */
ret = route_map_apply(ri->routemap[RIP_FILTER_IN],
(struct prefix *)&p, RMAP_RIP, &newinfo);
if (ret == RMAP_DENYMATCH) {
if (IS_RIP_DEBUG_PACKET)
zlog_debug(
"RIP %s/%d is filtered by route-map in",
inet_ntoa(p.prefix), p.prefixlen);
return;
}
/* Get back the object */
rte->nexthop = newinfo.nexthop_out;
rte->tag = htons(newinfo.tag_out); /* XXX */
rte->metric = newinfo.metric_out; /* XXX: the routemap uses the
metric_out field */
}
/* Once the entry has been validated, update the metric by
adding the cost of the network on wich the message
arrived. If the result is greater than infinity, use infinity
(RFC2453 Sec. 3.9.2) */
/* Zebra ripd can handle offset-list in. */
ret = rip_offset_list_apply_in(&p, ifp, &rte->metric);
/* If offset-list does not modify the metric use interface's
metric. */
if (!ret)
rte->metric += ifp->metric ? ifp->metric : 1;
if (rte->metric > RIP_METRIC_INFINITY)
rte->metric = RIP_METRIC_INFINITY;
/* Set nexthop pointer. */
if (rte->nexthop.s_addr == 0)
nexthop = &from->sin_addr;
else
nexthop = &rte->nexthop;
/* Check if nexthop address is myself, then do nothing. */
if (rip_nexthop_check(rip, nexthop) < 0) {
if (IS_RIP_DEBUG_PACKET)
zlog_debug("Nexthop address %s is myself",
inet_ntoa(*nexthop));
return;
}
/* Get index for the prefix. */
rp = route_node_get(rip->table, (struct prefix *)&p);
newinfo.rp = rp;
newinfo.nh.gate.ipv4 = *nexthop;
newinfo.nh.type = NEXTHOP_TYPE_IPV4;
newinfo.metric = rte->metric;
newinfo.tag = ntohs(rte->tag);
newinfo.distance = rip_distance_apply(rip, &newinfo);
new_dist = newinfo.distance ? newinfo.distance
: ZEBRA_RIP_DISTANCE_DEFAULT;
/* Check to see whether there is already RIP route on the table. */
if ((list = rp->info) != NULL)
for (ALL_LIST_ELEMENTS_RO(list, node, rinfo)) {
/* Need to compare with redistributed entry or local
* entry */
if (!rip_route_rte(rinfo))
break;
if (IPV4_ADDR_SAME(&rinfo->from, &from->sin_addr)
&& IPV4_ADDR_SAME(&rinfo->nh.gate.ipv4, nexthop))
break;
if (!listnextnode(node)) {
/* Not found in the list */
if (rte->metric > rinfo->metric) {
/* New route has a greater metric.
* Discard it. */
route_unlock_node(rp);
return;
}
if (rte->metric < rinfo->metric)
/* New route has a smaller metric.
* Replace the ECMP list
* with the new one in below. */
break;
/* Metrics are same. We compare the distances.
*/
old_dist = rinfo->distance
? rinfo->distance
: ZEBRA_RIP_DISTANCE_DEFAULT;
if (new_dist > old_dist) {
/* New route has a greater distance.
* Discard it. */
route_unlock_node(rp);
return;
}
if (new_dist < old_dist)
/* New route has a smaller distance.
* Replace the ECMP list
* with the new one in below. */
break;
/* Metrics and distances are both same. Keep
* "rinfo" null and
* the new route is added in the ECMP list in
* below. */
}
}
if (rinfo) {
/* Local static route. */
if (rinfo->type == ZEBRA_ROUTE_RIP
&& ((rinfo->sub_type == RIP_ROUTE_STATIC)
|| (rinfo->sub_type == RIP_ROUTE_DEFAULT))
&& rinfo->metric != RIP_METRIC_INFINITY) {
route_unlock_node(rp);
return;
}
/* Redistributed route check. */
if (rinfo->type != ZEBRA_ROUTE_RIP
&& rinfo->metric != RIP_METRIC_INFINITY) {
old_dist = rinfo->distance;
/* Only routes directly connected to an interface
* (nexthop == 0)
* may have a valid NULL distance */
if (rinfo->nh.gate.ipv4.s_addr != 0)
old_dist = old_dist
? old_dist
: ZEBRA_RIP_DISTANCE_DEFAULT;
/* If imported route does not have STRICT precedence,
mark it as a ghost */
if (new_dist <= old_dist
&& rte->metric != RIP_METRIC_INFINITY)
rip_ecmp_replace(rip, &newinfo);
route_unlock_node(rp);
return;
}
}
if (!rinfo) {
if (rp->info)
route_unlock_node(rp);
/* Now, check to see whether there is already an explicit route
for the destination prefix. If there is no such route, add
this route to the routing table, unless the metric is
infinity (there is no point in adding a route which
unusable). */
if (rte->metric != RIP_METRIC_INFINITY)
rip_ecmp_add(rip, &newinfo);
} else {
/* Route is there but we are not sure the route is RIP or not.
*/
/* If there is an existing route, compare the next hop address
to the address of the router from which the datagram came.
If this datagram is from the same router as the existing
route, reinitialize the timeout. */
same = (IPV4_ADDR_SAME(&rinfo->from, &from->sin_addr)
&& (rinfo->nh.ifindex == ifp->ifindex));
old_dist = rinfo->distance ? rinfo->distance
: ZEBRA_RIP_DISTANCE_DEFAULT;
/* Next, compare the metrics. If the datagram is from the same
router as the existing route, and the new metric is different
than the old one; or, if the new metric is lower than the old
one, or if the tag has been changed; or if there is a route
with a lower administrave distance; or an update of the
distance on the actual route; do the following actions: */
if ((same && rinfo->metric != rte->metric)
|| (rte->metric < rinfo->metric)
|| ((same) && (rinfo->metric == rte->metric)
&& (newinfo.tag != rinfo->tag))
|| (old_dist > new_dist)
|| ((old_dist != new_dist) && same)) {
if (listcount(list) == 1) {
if (newinfo.metric != RIP_METRIC_INFINITY)
rip_ecmp_replace(rip, &newinfo);
else
rip_ecmp_delete(rip, rinfo);
} else {
if (newinfo.metric < rinfo->metric)
rip_ecmp_replace(rip, &newinfo);
else if (newinfo.metric > rinfo->metric)
rip_ecmp_delete(rip, rinfo);
else if (new_dist < old_dist)
rip_ecmp_replace(rip, &newinfo);
else if (new_dist > old_dist)
rip_ecmp_delete(rip, rinfo);
else {
int update = CHECK_FLAG(rinfo->flags,
RIP_RTF_FIB)
? 1
: 0;
assert(newinfo.metric
!= RIP_METRIC_INFINITY);
RIP_TIMER_OFF(rinfo->t_timeout);
RIP_TIMER_OFF(rinfo->t_garbage_collect);
memcpy(rinfo, &newinfo,
sizeof(struct rip_info));
rip_timeout_update(rip, rinfo);
if (update)
rip_zebra_ipv4_add(rip, rp);
/* - Set the route change flag on the
* first entry. */
rinfo = listgetdata(listhead(list));
SET_FLAG(rinfo->flags, RIP_RTF_CHANGED);
rip_event(rip, RIP_TRIGGERED_UPDATE, 0);
}
}
} else /* same & no change */
rip_timeout_update(rip, rinfo);
/* Unlock tempolary lock of the route. */
route_unlock_node(rp);
}
}
/* Dump RIP packet */
static void rip_packet_dump(struct rip_packet *packet, int size,
const char *sndrcv)
{
caddr_t lim;
struct rte *rte;
const char *command_str;
char pbuf[BUFSIZ], nbuf[BUFSIZ];
uint8_t netmask = 0;
uint8_t *p;
/* Set command string. */
if (packet->command > 0 && packet->command < RIP_COMMAND_MAX)
command_str = lookup_msg(rip_msg, packet->command, NULL);
else
command_str = "unknown";
/* Dump packet header. */
zlog_debug("%s %s version %d packet size %d", sndrcv, command_str,
packet->version, size);
/* Dump each routing table entry. */
rte = packet->rte;
for (lim = (caddr_t)packet + size; (caddr_t)rte < lim; rte++) {
if (packet->version == RIPv2) {
netmask = ip_masklen(rte->mask);
if (rte->family == htons(RIP_FAMILY_AUTH)) {
if (rte->tag
== htons(RIP_AUTH_SIMPLE_PASSWORD)) {
p = (uint8_t *)&rte->prefix;
zlog_debug(
" family 0x%X type %d auth string: %s",
ntohs(rte->family),
ntohs(rte->tag), p);
} else if (rte->tag == htons(RIP_AUTH_MD5)) {
struct rip_md5_info *md5;
md5 = (struct rip_md5_info *)&packet
->rte;
zlog_debug(
" family 0x%X type %d (MD5 authentication)",
ntohs(md5->family),
ntohs(md5->type));
zlog_debug(
" RIP-2 packet len %d Key ID %d"
" Auth Data len %d",
ntohs(md5->packet_len),
md5->keyid, md5->auth_len);
zlog_debug(" Sequence Number %ld",
(unsigned long)ntohl(
md5->sequence));
} else if (rte->tag == htons(RIP_AUTH_DATA)) {
p = (uint8_t *)&rte->prefix;
zlog_debug(
" family 0x%X type %d (MD5 data)",
ntohs(rte->family),
ntohs(rte->tag));
zlog_debug(
" MD5: %02X%02X%02X%02X%02X%02X%02X%02X"
"%02X%02X%02X%02X%02X%02X%02X%02X",
p[0], p[1], p[2], p[3], p[4],
p[5], p[6], p[7], p[8], p[9],
p[10], p[11], p[12], p[13],
p[14], p[15]);
} else {
zlog_debug(
" family 0x%X type %d (Unknown auth type)",
ntohs(rte->family),
ntohs(rte->tag));
}
} else
zlog_debug(
" %s/%d -> %s family %d tag %" ROUTE_TAG_PRI
" metric %ld",
inet_ntop(AF_INET, &rte->prefix, pbuf,
BUFSIZ),
netmask,
inet_ntop(AF_INET, &rte->nexthop, nbuf,
BUFSIZ),
ntohs(rte->family),
(route_tag_t)ntohs(rte->tag),
(unsigned long)ntohl(rte->metric));
} else {
zlog_debug(
" %s family %d tag %" ROUTE_TAG_PRI
" metric %ld",
inet_ntop(AF_INET, &rte->prefix, pbuf, BUFSIZ),
ntohs(rte->family),
(route_tag_t)ntohs(rte->tag),
(unsigned long)ntohl(rte->metric));
}
}
}
/* Check if the destination address is valid (unicast; not net 0
or 127) (RFC2453 Section 3.9.2 - Page 26). But we don't
check net 0 because we accept default route. */
static int rip_destination_check(struct in_addr addr)
{
uint32_t destination;
/* Convert to host byte order. */
destination = ntohl(addr.s_addr);
if (IPV4_NET127(destination))
return 0;
/* Net 0 may match to the default route. */
if (IPV4_NET0(destination) && destination != 0)
return 0;
/* Unicast address must belong to class A, B, C. */
if (IN_CLASSA(destination))
return 1;
if (IN_CLASSB(destination))
return 1;
if (IN_CLASSC(destination))
return 1;
return 0;
}
/* RIP version 2 authentication. */
static int rip_auth_simple_password(struct rte *rte, struct sockaddr_in *from,
struct interface *ifp)
{
struct rip_interface *ri;
char *auth_str = (char *)rte + offsetof(struct rte, prefix);
int i;
/* reject passwords with zeros in the middle of the string */
for (i = strnlen(auth_str, 16); i < 16; i++) {
if (auth_str[i] != '\0')
return 0;
}
if (IS_RIP_DEBUG_EVENT)
zlog_debug("RIPv2 simple password authentication from %s",
inet_ntoa(from->sin_addr));
ri = ifp->info;
if (ri->auth_type != RIP_AUTH_SIMPLE_PASSWORD
|| rte->tag != htons(RIP_AUTH_SIMPLE_PASSWORD))
return 0;
/* Simple password authentication. */
if (ri->auth_str) {
if (strncmp(auth_str, ri->auth_str, 16) == 0)
return 1;
}
if (ri->key_chain) {
struct keychain *keychain;
struct key *key;
keychain = keychain_lookup(ri->key_chain);
if (keychain == NULL || keychain->key == NULL)
return 0;
key = key_match_for_accept(keychain, auth_str);
if (key)
return 1;
}
return 0;
}
/* RIP version 2 authentication with MD5. */
static int rip_auth_md5(struct rip_packet *packet, struct sockaddr_in *from,
int length, struct interface *ifp)
{
struct rip_interface *ri;
struct rip_md5_info *md5;
struct rip_md5_data *md5data;
struct keychain *keychain;
struct key *key;
#ifdef CRYPTO_OPENSSL
EVP_MD_CTX *ctx;
#elif CRYPTO_INTERNAL
MD5_CTX ctx;
#endif
uint8_t digest[RIP_AUTH_MD5_SIZE];
uint16_t packet_len;
char auth_str[RIP_AUTH_MD5_SIZE] = {};
if (IS_RIP_DEBUG_EVENT)
zlog_debug("RIPv2 MD5 authentication from %s",
inet_ntoa(from->sin_addr));
ri = ifp->info;
md5 = (struct rip_md5_info *)&packet->rte;
/* Check auth type. */
if (ri->auth_type != RIP_AUTH_MD5 || md5->type != htons(RIP_AUTH_MD5))
return 0;
/* If the authentication length is less than 16, then it must be wrong
* for
* any interpretation of rfc2082. Some implementations also interpret
* this as RIP_HEADER_SIZE+ RIP_AUTH_MD5_SIZE, aka
* RIP_AUTH_MD5_COMPAT_SIZE.
*/
if (!((md5->auth_len == RIP_AUTH_MD5_SIZE)
|| (md5->auth_len == RIP_AUTH_MD5_COMPAT_SIZE))) {
if (IS_RIP_DEBUG_EVENT)
zlog_debug(
"RIPv2 MD5 authentication, strange authentication "
"length field %d",
md5->auth_len);
return 0;
}
/* grab and verify check packet length */
packet_len = ntohs(md5->packet_len);
if (packet_len > (length - RIP_HEADER_SIZE - RIP_AUTH_MD5_SIZE)) {
if (IS_RIP_DEBUG_EVENT)
zlog_debug(
"RIPv2 MD5 authentication, packet length field %d "
"greater than received length %d!",
md5->packet_len, length);
return 0;
}
/* retrieve authentication data */
md5data = (struct rip_md5_data *)(((uint8_t *)packet) + packet_len);
if (ri->key_chain) {
keychain = keychain_lookup(ri->key_chain);
if (keychain == NULL)
return 0;
key = key_lookup_for_accept(keychain, md5->keyid);
if (key == NULL || key->string == NULL)
return 0;
strlcpy(auth_str, key->string, sizeof(auth_str));
} else if (ri->auth_str)
strlcpy(auth_str, ri->auth_str, sizeof(auth_str));
if (auth_str[0] == 0)
return 0;
/* MD5 digest authentication. */
#ifdef CRYPTO_OPENSSL
unsigned int md5_size = RIP_AUTH_MD5_SIZE;
ctx = EVP_MD_CTX_new();
EVP_DigestInit(ctx, EVP_md5());
EVP_DigestUpdate(ctx, packet, packet_len + RIP_HEADER_SIZE);
EVP_DigestUpdate(ctx, auth_str, RIP_AUTH_MD5_SIZE);
EVP_DigestFinal(ctx, digest, &md5_size);
EVP_MD_CTX_free(ctx);
#elif CRYPTO_INTERNAL
memset(&ctx, 0, sizeof(ctx));
MD5Init(&ctx);
MD5Update(&ctx, packet, packet_len + RIP_HEADER_SIZE);
MD5Update(&ctx, auth_str, RIP_AUTH_MD5_SIZE);
MD5Final(digest, &ctx);
#endif
if (memcmp(md5data->digest, digest, RIP_AUTH_MD5_SIZE) == 0)
return packet_len;
else
return 0;
}
/* Pick correct auth string for sends, prepare auth_str buffer for use.
* (left justified and padded).
*
* presumes one of ri or key is valid, and that the auth strings they point
* to are nul terminated. If neither are present, auth_str will be fully
* zero padded.
*
*/
static void rip_auth_prepare_str_send(struct rip_interface *ri, struct key *key,
char *auth_str, int len)
{
assert(ri || key);
memset(auth_str, 0, len);
if (key && key->string)
strlcpy(auth_str, key->string, len);
else if (ri->auth_str)
strlcpy(auth_str, ri->auth_str, len);
return;
}
/* Write RIPv2 simple password authentication information
*
* auth_str is presumed to be 2 bytes and correctly prepared
* (left justified and zero padded).
*/
static void rip_auth_simple_write(struct stream *s, char *auth_str, int len)
{
assert(s && len == RIP_AUTH_SIMPLE_SIZE);
stream_putw(s, RIP_FAMILY_AUTH);
stream_putw(s, RIP_AUTH_SIMPLE_PASSWORD);
stream_put(s, auth_str, RIP_AUTH_SIMPLE_SIZE);