forked from opensourcerouting/quagga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ripngd.c
3015 lines (2546 loc) · 79.3 KB
/
ripngd.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
/* RIPng daemon
* Copyright (C) 1998, 1999 Kunihiro Ishiguro
*
* 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 GNU Zebra; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include <zebra.h>
#include "prefix.h"
#include "filter.h"
#include "log.h"
#include "thread.h"
#include "memory.h"
#include "if.h"
#include "stream.h"
#include "table.h"
#include "command.h"
#include "sockopt.h"
#include "distribute.h"
#include "plist.h"
#include "routemap.h"
#include "if_rmap.h"
#include "privs.h"
#include "ripngd/ripngd.h"
#include "ripngd/ripng_route.h"
#include "ripngd/ripng_debug.h"
#include "ripngd/ripng_nexthop.h"
/* RIPng structure which includes many parameters related to RIPng
protocol. If ripng couldn't active or ripng doesn't configured,
ripng->fd must be negative value. */
struct ripng *ripng = NULL;
enum
{
ripng_all_route,
ripng_changed_route,
};
extern struct zebra_privs_t ripngd_privs;
/* Prototypes. */
void
ripng_output_process (struct interface *, struct sockaddr_in6 *, int);
int
ripng_triggered_update (struct thread *);
/* RIPng next hop specification. */
struct ripng_nexthop
{
enum ripng_nexthop_type
{
RIPNG_NEXTHOP_UNSPEC,
RIPNG_NEXTHOP_ADDRESS
} flag;
struct in6_addr address;
};
static int
ripng_route_rte (struct ripng_info *rinfo)
{
return (rinfo->type == ZEBRA_ROUTE_RIPNG && rinfo->sub_type == RIPNG_ROUTE_RTE);
}
/* Allocate new ripng information. */
struct ripng_info *
ripng_info_new ()
{
struct ripng_info *new;
new = XCALLOC (MTYPE_RIPNG_ROUTE, sizeof (struct ripng_info));
return new;
}
/* Free ripng information. */
void
ripng_info_free (struct ripng_info *rinfo)
{
XFREE (MTYPE_RIPNG_ROUTE, rinfo);
}
/* Create ripng socket. */
static int
ripng_make_socket (void)
{
int ret;
int sock;
struct sockaddr_in6 ripaddr;
sock = socket (AF_INET6, SOCK_DGRAM, 0);
if (sock < 0)
{
zlog (NULL, LOG_ERR, "Can't make ripng socket");
return sock;
}
ret = setsockopt_so_recvbuf (sock, 8096);
if (ret < 0)
return ret;
ret = setsockopt_ipv6_pktinfo (sock, 1);
if (ret < 0)
return ret;
#ifdef IPTOS_PREC_INTERNETCONTROL
ret = setsockopt_ipv6_tclass (sock, IPTOS_PREC_INTERNETCONTROL);
if (ret < 0)
return ret;
#endif
ret = setsockopt_ipv6_multicast_hops (sock, 255);
if (ret < 0)
return ret;
ret = setsockopt_ipv6_multicast_loop (sock, 0);
if (ret < 0)
return ret;
ret = setsockopt_ipv6_hoplimit (sock, 1);
if (ret < 0)
return ret;
memset (&ripaddr, 0, sizeof (ripaddr));
ripaddr.sin6_family = AF_INET6;
#ifdef SIN6_LEN
ripaddr.sin6_len = sizeof (struct sockaddr_in6);
#endif /* SIN6_LEN */
ripaddr.sin6_port = htons (RIPNG_PORT_DEFAULT);
if (ripngd_privs.change (ZPRIVS_RAISE))
zlog_err ("ripng_make_socket: could not raise privs");
ret = bind (sock, (struct sockaddr *) &ripaddr, sizeof (ripaddr));
if (ret < 0)
{
zlog (NULL, LOG_ERR, "Can't bind ripng socket: %s.", safe_strerror (errno));
if (ripngd_privs.change (ZPRIVS_LOWER))
zlog_err ("ripng_make_socket: could not lower privs");
return ret;
}
if (ripngd_privs.change (ZPRIVS_LOWER))
zlog_err ("ripng_make_socket: could not lower privs");
return sock;
}
/* Send RIPng packet. */
int
ripng_send_packet (caddr_t buf, int bufsize, struct sockaddr_in6 *to,
struct interface *ifp)
{
int ret;
struct msghdr msg;
struct iovec iov;
struct cmsghdr *cmsgptr;
char adata [256];
struct in6_pktinfo *pkt;
struct sockaddr_in6 addr;
if (IS_RIPNG_DEBUG_SEND) {
if (to)
zlog_debug ("send to %s", inet6_ntoa (to->sin6_addr));
zlog_debug (" send interface %s", ifp->name);
zlog_debug (" send packet size %d", bufsize);
}
memset (&addr, 0, sizeof (struct sockaddr_in6));
addr.sin6_family = AF_INET6;
#ifdef SIN6_LEN
addr.sin6_len = sizeof (struct sockaddr_in6);
#endif /* SIN6_LEN */
addr.sin6_flowinfo = htonl (RIPNG_PRIORITY_DEFAULT);
/* When destination is specified. */
if (to != NULL)
{
addr.sin6_addr = to->sin6_addr;
addr.sin6_port = to->sin6_port;
}
else
{
inet_pton(AF_INET6, RIPNG_GROUP, &addr.sin6_addr);
addr.sin6_port = htons (RIPNG_PORT_DEFAULT);
}
msg.msg_name = (void *) &addr;
msg.msg_namelen = sizeof (struct sockaddr_in6);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = (void *) adata;
msg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
iov.iov_base = buf;
iov.iov_len = bufsize;
cmsgptr = (struct cmsghdr *)adata;
cmsgptr->cmsg_len = CMSG_LEN(sizeof (struct in6_pktinfo));
cmsgptr->cmsg_level = IPPROTO_IPV6;
cmsgptr->cmsg_type = IPV6_PKTINFO;
pkt = (struct in6_pktinfo *) CMSG_DATA (cmsgptr);
memset (&pkt->ipi6_addr, 0, sizeof (struct in6_addr));
pkt->ipi6_ifindex = ifp->ifindex;
ret = sendmsg (ripng->sock, &msg, 0);
if (ret < 0) {
if (to)
zlog_err ("RIPng send fail on %s to %s: %s", ifp->name,
inet6_ntoa (to->sin6_addr), safe_strerror (errno));
else
zlog_err ("RIPng send fail on %s: %s", ifp->name, safe_strerror (errno));
}
return ret;
}
/* Receive UDP RIPng packet from socket. */
static int
ripng_recv_packet (int sock, u_char *buf, int bufsize,
struct sockaddr_in6 *from, unsigned int *ifindex,
int *hoplimit)
{
int ret;
struct msghdr msg;
struct iovec iov;
struct cmsghdr *cmsgptr;
struct in6_addr dst;
/* Ancillary data. This store cmsghdr and in6_pktinfo. But at this
point I can't determine size of cmsghdr */
char adata[1024];
/* Fill in message and iovec. */
msg.msg_name = (void *) from;
msg.msg_namelen = sizeof (struct sockaddr_in6);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = (void *) adata;
msg.msg_controllen = sizeof adata;
iov.iov_base = buf;
iov.iov_len = bufsize;
/* If recvmsg fail return minus value. */
ret = recvmsg (sock, &msg, 0);
if (ret < 0)
return ret;
for (cmsgptr = ZCMSG_FIRSTHDR(&msg); cmsgptr != NULL;
cmsgptr = CMSG_NXTHDR(&msg, cmsgptr))
{
/* I want interface index which this packet comes from. */
if (cmsgptr->cmsg_level == IPPROTO_IPV6 &&
cmsgptr->cmsg_type == IPV6_PKTINFO)
{
struct in6_pktinfo *ptr;
ptr = (struct in6_pktinfo *) CMSG_DATA (cmsgptr);
*ifindex = ptr->ipi6_ifindex;
dst = ptr->ipi6_addr;
if (*ifindex == 0)
zlog_warn ("Interface index returned by IPV6_PKTINFO is zero");
}
/* Incoming packet's multicast hop limit. */
if (cmsgptr->cmsg_level == IPPROTO_IPV6 &&
cmsgptr->cmsg_type == IPV6_HOPLIMIT)
{
int *phoplimit = (int *) CMSG_DATA (cmsgptr);
*hoplimit = *phoplimit;
}
}
/* Hoplimit check shold be done when destination address is
multicast address. */
if (! IN6_IS_ADDR_MULTICAST (&dst))
*hoplimit = -1;
return ret;
}
/* Dump rip packet */
void
ripng_packet_dump (struct ripng_packet *packet, int size, const char *sndrcv)
{
caddr_t lim;
struct rte *rte;
const char *command_str;
/* Set command string. */
if (packet->command == RIPNG_REQUEST)
command_str = "request";
else if (packet->command == RIPNG_RESPONSE)
command_str = "response";
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 (rte->metric == RIPNG_METRIC_NEXTHOP)
zlog_debug (" nexthop %s/%d", inet6_ntoa (rte->addr), rte->prefixlen);
else
zlog_debug (" %s/%d metric %d tag %d",
inet6_ntoa (rte->addr), rte->prefixlen,
rte->metric, ntohs (rte->tag));
}
}
/* RIPng next hop address RTE (Route Table Entry). */
static void
ripng_nexthop_rte (struct rte *rte,
struct sockaddr_in6 *from,
struct ripng_nexthop *nexthop)
{
char buf[INET6_BUFSIZ];
/* Logging before checking RTE. */
if (IS_RIPNG_DEBUG_RECV)
zlog_debug ("RIPng nexthop RTE address %s tag %d prefixlen %d",
inet6_ntoa (rte->addr), ntohs (rte->tag), rte->prefixlen);
/* RFC2080 2.1.1 Next Hop:
The route tag and prefix length in the next hop RTE must be
set to zero on sending and ignored on receiption. */
if (ntohs (rte->tag) != 0)
zlog_warn ("RIPng nexthop RTE with non zero tag value %d from %s",
ntohs (rte->tag), inet6_ntoa (from->sin6_addr));
if (rte->prefixlen != 0)
zlog_warn ("RIPng nexthop RTE with non zero prefixlen value %d from %s",
rte->prefixlen, inet6_ntoa (from->sin6_addr));
/* Specifying a value of 0:0:0:0:0:0:0:0 in the prefix field of a
next hop RTE indicates that the next hop address should be the
originator of the RIPng advertisement. An address specified as a
next hop must be a link-local address. */
if (IN6_IS_ADDR_UNSPECIFIED (&rte->addr))
{
nexthop->flag = RIPNG_NEXTHOP_UNSPEC;
memset (&nexthop->address, 0, sizeof (struct in6_addr));
return;
}
if (IN6_IS_ADDR_LINKLOCAL (&rte->addr))
{
nexthop->flag = RIPNG_NEXTHOP_ADDRESS;
IPV6_ADDR_COPY (&nexthop->address, &rte->addr);
return;
}
/* The purpose of the next hop RTE is to eliminate packets being
routed through extra hops in the system. It is particularly useful
when RIPng is not being run on all of the routers on a network.
Note that next hop RTE is "advisory". That is, if the provided
information is ignored, a possibly sub-optimal, but absolutely
valid, route may be taken. If the received next hop address is not
a link-local address, it should be treated as 0:0:0:0:0:0:0:0. */
zlog_warn ("RIPng nexthop RTE with non link-local address %s from %s",
inet6_ntoa (rte->addr),
inet_ntop (AF_INET6, &from->sin6_addr, buf, INET6_BUFSIZ));
nexthop->flag = RIPNG_NEXTHOP_UNSPEC;
memset (&nexthop->address, 0, sizeof (struct in6_addr));
return;
}
/* If ifp has same link-local address then return 1. */
static int
ripng_lladdr_check (struct interface *ifp, struct in6_addr *addr)
{
struct listnode *node;
struct connected *connected;
struct prefix *p;
for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
{
p = connected->address;
if (p->family == AF_INET6 &&
IN6_IS_ADDR_LINKLOCAL (&p->u.prefix6) &&
IN6_ARE_ADDR_EQUAL (&p->u.prefix6, addr))
return 1;
}
return 0;
}
/* RIPng route garbage collect timer. */
static int
ripng_garbage_collect (struct thread *t)
{
struct ripng_info *rinfo;
struct route_node *rp;
rinfo = THREAD_ARG (t);
rinfo->t_garbage_collect = NULL;
/* Off timeout timer. */
RIPNG_TIMER_OFF (rinfo->t_timeout);
/* Get route_node pointer. */
rp = rinfo->rp;
/* Unlock route_node. */
rp->info = NULL;
route_unlock_node (rp);
/* Free RIPng routing information. */
ripng_info_free (rinfo);
return 0;
}
/* Timeout RIPng routes. */
static int
ripng_timeout (struct thread *t)
{
struct ripng_info *rinfo;
struct route_node *rp;
rinfo = THREAD_ARG (t);
rinfo->t_timeout = NULL;
/* Get route_node pointer. */
rp = rinfo->rp;
/* - The garbage-collection timer is set for 120 seconds. */
RIPNG_TIMER_ON (rinfo->t_garbage_collect, ripng_garbage_collect,
ripng->garbage_time);
/* Delete this route from the kernel. */
ripng_zebra_ipv6_delete ((struct prefix_ipv6 *)&rp->p, &rinfo->nexthop,
rinfo->ifindex);
/* - The metric for the route is set to 16 (infinity). This causes
the route to be removed from service. */
rinfo->metric = RIPNG_METRIC_INFINITY;
rinfo->flags &= ~RIPNG_RTF_FIB;
/* Aggregate count decrement. */
ripng_aggregate_decrement (rp, rinfo);
/* - The route change flag is to indicate that this entry has been
changed. */
rinfo->flags |= RIPNG_RTF_CHANGED;
/* - The output process is signalled to trigger a response. */
ripng_event (RIPNG_TRIGGERED_UPDATE, 0);
return 0;
}
static void
ripng_timeout_update (struct ripng_info *rinfo)
{
if (rinfo->metric != RIPNG_METRIC_INFINITY)
{
RIPNG_TIMER_OFF (rinfo->t_timeout);
RIPNG_TIMER_ON (rinfo->t_timeout, ripng_timeout, ripng->timeout_time);
}
}
static int
ripng_incoming_filter (struct prefix_ipv6 *p, struct ripng_interface *ri)
{
struct distribute *dist;
struct access_list *alist;
struct prefix_list *plist;
/* Input distribute-list filtering. */
if (ri->list[RIPNG_FILTER_IN])
{
if (access_list_apply (ri->list[RIPNG_FILTER_IN],
(struct prefix *) p) == FILTER_DENY)
{
if (IS_RIPNG_DEBUG_PACKET)
zlog_debug ("%s/%d filtered by distribute in",
inet6_ntoa (p->prefix), p->prefixlen);
return -1;
}
}
if (ri->prefix[RIPNG_FILTER_IN])
{
if (prefix_list_apply (ri->prefix[RIPNG_FILTER_IN],
(struct prefix *) p) == PREFIX_DENY)
{
if (IS_RIPNG_DEBUG_PACKET)
zlog_debug ("%s/%d filtered by prefix-list in",
inet6_ntoa (p->prefix), p->prefixlen);
return -1;
}
}
/* All interface filter check. */
dist = distribute_lookup (NULL);
if (dist)
{
if (dist->list[DISTRIBUTE_IN])
{
alist = access_list_lookup (AFI_IP6, dist->list[DISTRIBUTE_IN]);
if (alist)
{
if (access_list_apply (alist,
(struct prefix *) p) == FILTER_DENY)
{
if (IS_RIPNG_DEBUG_PACKET)
zlog_debug ("%s/%d filtered by distribute in",
inet6_ntoa (p->prefix), p->prefixlen);
return -1;
}
}
}
if (dist->prefix[DISTRIBUTE_IN])
{
plist = prefix_list_lookup (AFI_IP6, dist->prefix[DISTRIBUTE_IN]);
if (plist)
{
if (prefix_list_apply (plist,
(struct prefix *) p) == PREFIX_DENY)
{
if (IS_RIPNG_DEBUG_PACKET)
zlog_debug ("%s/%d filtered by prefix-list in",
inet6_ntoa (p->prefix), p->prefixlen);
return -1;
}
}
}
}
return 0;
}
static int
ripng_outgoing_filter (struct prefix_ipv6 *p, struct ripng_interface *ri)
{
struct distribute *dist;
struct access_list *alist;
struct prefix_list *plist;
if (ri->list[RIPNG_FILTER_OUT])
{
if (access_list_apply (ri->list[RIPNG_FILTER_OUT],
(struct prefix *) p) == FILTER_DENY)
{
if (IS_RIPNG_DEBUG_PACKET)
zlog_debug ("%s/%d is filtered by distribute out",
inet6_ntoa (p->prefix), p->prefixlen);
return -1;
}
}
if (ri->prefix[RIPNG_FILTER_OUT])
{
if (prefix_list_apply (ri->prefix[RIPNG_FILTER_OUT],
(struct prefix *) p) == PREFIX_DENY)
{
if (IS_RIPNG_DEBUG_PACKET)
zlog_debug ("%s/%d is filtered by prefix-list out",
inet6_ntoa (p->prefix), p->prefixlen);
return -1;
}
}
/* All interface filter check. */
dist = distribute_lookup (NULL);
if (dist)
{
if (dist->list[DISTRIBUTE_OUT])
{
alist = access_list_lookup (AFI_IP6, dist->list[DISTRIBUTE_OUT]);
if (alist)
{
if (access_list_apply (alist,
(struct prefix *) p) == FILTER_DENY)
{
if (IS_RIPNG_DEBUG_PACKET)
zlog_debug ("%s/%d filtered by distribute out",
inet6_ntoa (p->prefix), p->prefixlen);
return -1;
}
}
}
if (dist->prefix[DISTRIBUTE_OUT])
{
plist = prefix_list_lookup (AFI_IP6, dist->prefix[DISTRIBUTE_OUT]);
if (plist)
{
if (prefix_list_apply (plist,
(struct prefix *) p) == PREFIX_DENY)
{
if (IS_RIPNG_DEBUG_PACKET)
zlog_debug ("%s/%d filtered by prefix-list out",
inet6_ntoa (p->prefix), p->prefixlen);
return -1;
}
}
}
}
return 0;
}
/* Process RIPng route according to RFC2080. */
static void
ripng_route_process (struct rte *rte, struct sockaddr_in6 *from,
struct ripng_nexthop *ripng_nexthop,
struct interface *ifp)
{
int ret;
struct prefix_ipv6 p;
struct route_node *rp;
struct ripng_info *rinfo;
struct ripng_interface *ri;
struct in6_addr *nexthop;
u_char oldmetric;
int same = 0;
/* Make prefix structure. */
memset (&p, 0, sizeof (struct prefix_ipv6));
p.family = AF_INET6;
/* p.prefix = rte->addr; */
IPV6_ADDR_COPY (&p.prefix, &rte->addr);
p.prefixlen = rte->prefixlen;
/* Make sure mask is applied. */
/* XXX We have to check the prefix is valid or not before call
apply_mask_ipv6. */
apply_mask_ipv6 (&p);
/* Apply input filters. */
ri = ifp->info;
ret = ripng_incoming_filter (&p, ri);
if (ret < 0)
return;
/* Modify entry. */
if (ri->routemap[RIPNG_FILTER_IN])
{
int ret;
struct ripng_info newinfo;
memset (&newinfo, 0, sizeof (struct ripng_info));
newinfo.type = ZEBRA_ROUTE_RIPNG;
newinfo.sub_type = RIPNG_ROUTE_RTE;
if (ripng_nexthop->flag == RIPNG_NEXTHOP_ADDRESS)
newinfo.nexthop = ripng_nexthop->address;
else
newinfo.nexthop = from->sin6_addr;
newinfo.from = from->sin6_addr;
newinfo.ifindex = ifp->ifindex;
newinfo.metric = rte->metric;
newinfo.metric_out = rte->metric; /* XXX */
newinfo.tag = ntohs(rte->tag); /* XXX */
ret = route_map_apply (ri->routemap[RIPNG_FILTER_IN],
(struct prefix *)&p, RMAP_RIPNG, &newinfo);
if (ret == RMAP_DENYMATCH)
{
if (IS_RIPNG_DEBUG_PACKET)
zlog_debug ("RIPng %s/%d is filtered by route-map in",
inet6_ntoa (p.prefix), p.prefixlen);
return;
}
/* Get back the object */
if (ripng_nexthop->flag == RIPNG_NEXTHOP_ADDRESS) {
if (! IPV6_ADDR_SAME(&newinfo.nexthop, &ripng_nexthop->address) ) {
/* the nexthop get changed by the routemap */
if (IN6_IS_ADDR_LINKLOCAL(&newinfo.nexthop))
ripng_nexthop->address = newinfo.nexthop;
else
ripng_nexthop->address = in6addr_any;
}
} else {
if (! IPV6_ADDR_SAME(&newinfo.nexthop, &from->sin6_addr) ) {
/* the nexthop get changed by the routemap */
if (IN6_IS_ADDR_LINKLOCAL(&newinfo.nexthop)) {
ripng_nexthop->flag = RIPNG_NEXTHOP_ADDRESS;
ripng_nexthop->address = newinfo.nexthop;
}
}
}
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 ripngd can handle offset-list in. */
ret = ripng_offset_list_apply_in (&p, ifp, &rte->metric);
/* If offset-list does not modify the metric use interface's
* one. */
if (! ret)
rte->metric += ifp->metric;
if (rte->metric > RIPNG_METRIC_INFINITY)
rte->metric = RIPNG_METRIC_INFINITY;
/* Set nexthop pointer. */
if (ripng_nexthop->flag == RIPNG_NEXTHOP_ADDRESS)
nexthop = &ripng_nexthop->address;
else
nexthop = &from->sin6_addr;
/* Lookup RIPng routing table. */
rp = route_node_get (ripng->table, (struct prefix *) &p);
/* Sanity check */
rinfo = rp->info;
if (rinfo)
{
/* Redistributed route check. */
if (rinfo->type != ZEBRA_ROUTE_RIPNG
&& rinfo->metric != RIPNG_METRIC_INFINITY)
return;
/* Local static route. */
if (rinfo->type == ZEBRA_ROUTE_RIPNG
&& ((rinfo->sub_type == RIPNG_ROUTE_STATIC) ||
(rinfo->sub_type == RIPNG_ROUTE_DEFAULT))
&& rinfo->metric != RIPNG_METRIC_INFINITY)
return;
}
if (rp->info == NULL)
{
/* 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 != RIPNG_METRIC_INFINITY)
{
rinfo = ripng_info_new ();
/* - Setting the destination prefix and length to those in
the RTE. */
rp->info = rinfo;
rinfo->rp = rp;
/* - Setting the metric to the newly calculated metric (as
described above). */
rinfo->metric = rte->metric;
rinfo->tag = ntohs (rte->tag);
/* - Set the next hop address to be the address of the router
from which the datagram came or the next hop address
specified by a next hop RTE. */
IPV6_ADDR_COPY (&rinfo->nexthop, nexthop);
IPV6_ADDR_COPY (&rinfo->from, &from->sin6_addr);
rinfo->ifindex = ifp->ifindex;
/* - Initialize the timeout for the route. If the
garbage-collection timer is running for this route, stop it. */
ripng_timeout_update (rinfo);
/* - Set the route change flag. */
rinfo->flags |= RIPNG_RTF_CHANGED;
/* - Signal the output process to trigger an update (see section
2.5). */
ripng_event (RIPNG_TRIGGERED_UPDATE, 0);
/* Finally, route goes into the kernel. */
rinfo->type = ZEBRA_ROUTE_RIPNG;
rinfo->sub_type = RIPNG_ROUTE_RTE;
ripng_zebra_ipv6_add (&p, &rinfo->nexthop, rinfo->ifindex,
rinfo->metric);
rinfo->flags |= RIPNG_RTF_FIB;
/* Aggregate check. */
ripng_aggregate_increment (rp, rinfo);
}
}
else
{
rinfo = rp->info;
/* 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 = (IN6_ARE_ADDR_EQUAL (&rinfo->from, &from->sin6_addr)
&& (rinfo->ifindex == ifp->ifindex));
if (same)
ripng_timeout_update (rinfo);
/* 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; do the following actions: */
if ((same && rinfo->metric != rte->metric) ||
rte->metric < rinfo->metric)
{
/* - Adopt the route from the datagram. That is, put the
new metric in, and adjust the next hop address (if
necessary). */
oldmetric = rinfo->metric;
rinfo->metric = rte->metric;
rinfo->tag = ntohs (rte->tag);
IPV6_ADDR_COPY (&rinfo->from, &from->sin6_addr);
rinfo->ifindex = ifp->ifindex;
/* Should a new route to this network be established
while the garbage-collection timer is running, the
new route will replace the one that is about to be
deleted. In this case the garbage-collection timer
must be cleared. */
if (oldmetric == RIPNG_METRIC_INFINITY &&
rinfo->metric < RIPNG_METRIC_INFINITY)
{
rinfo->type = ZEBRA_ROUTE_RIPNG;
rinfo->sub_type = RIPNG_ROUTE_RTE;
RIPNG_TIMER_OFF (rinfo->t_garbage_collect);
if (! IPV6_ADDR_SAME (&rinfo->nexthop, nexthop))
IPV6_ADDR_COPY (&rinfo->nexthop, nexthop);
ripng_zebra_ipv6_add (&p, nexthop, ifp->ifindex, rinfo->metric);
rinfo->flags |= RIPNG_RTF_FIB;
/* The aggregation counter needs to be updated because
the prefixes, which are into the gc, have been
removed from the aggregator (see ripng_timout). */
ripng_aggregate_increment (rp, rinfo);
}
/* Update nexthop and/or metric value. */
if (oldmetric != RIPNG_METRIC_INFINITY)
{
ripng_zebra_ipv6_delete (&p, &rinfo->nexthop, rinfo->ifindex);
ripng_zebra_ipv6_add (&p, nexthop, ifp->ifindex, rinfo->metric);
rinfo->flags |= RIPNG_RTF_FIB;
if (! IPV6_ADDR_SAME (&rinfo->nexthop, nexthop))
IPV6_ADDR_COPY (&rinfo->nexthop, nexthop);
}
/* - Set the route change flag and signal the output process
to trigger an update. */
rinfo->flags |= RIPNG_RTF_CHANGED;
ripng_event (RIPNG_TRIGGERED_UPDATE, 0);
/* - If the new metric is infinity, start the deletion
process (described above); */
if (rinfo->metric == RIPNG_METRIC_INFINITY)
{
/* If the new metric is infinity, the deletion process
begins for the route, which is no longer used for
routing packets. Note that the deletion process is
started only when the metric is first set to
infinity. If the metric was already infinity, then a
new deletion process is not started. */
if (oldmetric != RIPNG_METRIC_INFINITY)
{
/* - The garbage-collection timer is set for 120 seconds. */
RIPNG_TIMER_ON (rinfo->t_garbage_collect,
ripng_garbage_collect, ripng->garbage_time);
RIPNG_TIMER_OFF (rinfo->t_timeout);
/* - The metric for the route is set to 16
(infinity). This causes the route to be removed
from service.*/
ripng_zebra_ipv6_delete (&p, &rinfo->nexthop, rinfo->ifindex);
rinfo->flags &= ~RIPNG_RTF_FIB;
/* Aggregate count decrement. */
ripng_aggregate_decrement (rp, rinfo);
/* - The route change flag is to indicate that this
entry has been changed. */
/* - The output process is signalled to trigger a
response. */
; /* Above processes are already done previously. */
}
}
else
{
/* otherwise, re-initialize the timeout. */
ripng_timeout_update (rinfo);
}
}
/* Unlock tempolary lock of the route. */
route_unlock_node (rp);
}
}
/* Add redistributed route to RIPng table. */
void
ripng_redistribute_add (int type, int sub_type, struct prefix_ipv6 *p,
unsigned int ifindex, struct in6_addr *nexthop)
{
struct route_node *rp;
struct ripng_info *rinfo;
/* Redistribute route */
if (IN6_IS_ADDR_LINKLOCAL (&p->prefix))
return;
if (IN6_IS_ADDR_LOOPBACK (&p->prefix))
return;
#if defined (MUSICA) || defined (LINUX)
/* XXX As long as the RIPng redistribution is applied to all the connected
* routes, one needs to filter the ::/96 prefixes.
* However it could be a wanted case, it will be removed soon.
*/
if ((IN6_IS_ADDR_V4COMPAT(&p->prefix)) ||
(IN6_IS_ADDR_UNSPECIFIED (&p->prefix) && (p->prefixlen == 96)))
return;
#endif /* MUSICA or LINUX */
rp = route_node_get (ripng->table, (struct prefix *) p);
rinfo = rp->info;
if (rinfo)
{
if (rinfo->type == ZEBRA_ROUTE_CONNECT
&& rinfo->sub_type == RIPNG_ROUTE_INTERFACE
&& rinfo->metric != RIPNG_METRIC_INFINITY) {
route_unlock_node (rp);
return;
}
/* Manually configured RIPng route check.
* They have the precedence on all the other entries.
**/
if (rinfo->type == ZEBRA_ROUTE_RIPNG
&& ((rinfo->sub_type == RIPNG_ROUTE_STATIC) ||
(rinfo->sub_type == RIPNG_ROUTE_DEFAULT)) ) {
if (type != ZEBRA_ROUTE_RIPNG || ((sub_type != RIPNG_ROUTE_STATIC) &&
(sub_type != RIPNG_ROUTE_DEFAULT))) {
route_unlock_node (rp);
return;
}
}
RIPNG_TIMER_OFF (rinfo->t_timeout);
RIPNG_TIMER_OFF (rinfo->t_garbage_collect);
/* Tells the other daemons about the deletion of
* this RIPng route
**/
if (ripng_route_rte (rinfo))
ripng_zebra_ipv6_delete ((struct prefix_ipv6 *)&rp->p, &rinfo->nexthop,
rinfo->metric);
rp->info = NULL;
ripng_info_free (rinfo);
route_unlock_node (rp);
}
rinfo = ripng_info_new ();
rinfo->type = type;
rinfo->sub_type = sub_type;
rinfo->ifindex = ifindex;
rinfo->metric = 1;
rinfo->rp = rp;
if (nexthop && IN6_IS_ADDR_LINKLOCAL(nexthop))
rinfo->nexthop = *nexthop;
rinfo->flags |= RIPNG_RTF_FIB;
rp->info = rinfo;
/* Aggregate check. */
ripng_aggregate_increment (rp, rinfo);
rinfo->flags |= RIPNG_RTF_CHANGED;