-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
rtadv.c
1968 lines (1633 loc) · 51.9 KB
/
rtadv.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
// SPDX-License-Identifier: GPL-2.0-or-later
/* Router advertisement
* Copyright (C) 2016 Cumulus Networks
* Copyright (C) 2005 6WIND <jean-mickael.guerin@6wind.com>
* Copyright (C) 1999 Kunihiro Ishiguro
*/
#include <zebra.h>
#include <netinet/icmp6.h>
#include "memory.h"
#include "sockopt.h"
#include "frrevent.h"
#include "if.h"
#include "stream.h"
#include "log.h"
#include "prefix.h"
#include "linklist.h"
#include "command.h"
#include "privs.h"
#include "vrf.h"
#include "ns.h"
#include "lib_errors.h"
#include "zebra/interface.h"
#include "zebra/rtadv.h"
#include "zebra/debug.h"
#include "zebra/rib.h"
#include "zebra/zapi_msg.h"
#include "zebra/zebra_vrf.h"
#include "zebra/zebra_errors.h"
#include "zebra/zebra_router.h"
extern struct zebra_privs_t zserv_privs;
static uint32_t interfaces_configured_for_ra_from_bgp;
#define RTADV_ADATA_SIZE 1024
#if defined(HAVE_RTADV)
#include "zebra/rtadv_clippy.c"
DEFINE_MTYPE_STATIC(ZEBRA, RTADV_PREFIX, "Router Advertisement Prefix");
DEFINE_MTYPE_STATIC(ZEBRA, ADV_IF, "Advertised Interface");
#ifdef OPEN_BSD
#include <netinet/icmp6.h>
#endif
/* If RFC2133 definition is used. */
#ifndef IPV6_JOIN_GROUP
#define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
#endif
#ifndef IPV6_LEAVE_GROUP
#define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
#endif
#define ALLNODE "ff02::1"
#define ALLROUTER "ff02::2"
/* adv list node */
struct adv_if {
char name[IFNAMSIZ];
struct adv_if_list_item list_item;
};
static int adv_if_cmp(const struct adv_if *a, const struct adv_if *b)
{
return if_cmp_name_func(a->name, b->name);
}
DECLARE_SORTLIST_UNIQ(adv_if_list, struct adv_if, list_item, adv_if_cmp);
static int rtadv_prefix_cmp(const struct rtadv_prefix *a,
const struct rtadv_prefix *b)
{
return prefix_cmp(&a->prefix, &b->prefix);
}
DECLARE_RBTREE_UNIQ(rtadv_prefixes, struct rtadv_prefix, item,
rtadv_prefix_cmp);
DEFINE_MTYPE_STATIC(ZEBRA, RTADV_RDNSS, "Router Advertisement RDNSS");
DEFINE_MTYPE_STATIC(ZEBRA, RTADV_DNSSL, "Router Advertisement DNSSL");
/* Order is intentional. Matches RFC4191. This array is also used for
command matching, so only modify with care. */
static const char *const rtadv_pref_strs[] = {
"medium", "high", "INVALID", "low", 0
};
enum rtadv_event {
RTADV_START,
RTADV_STOP,
RTADV_TIMER,
RTADV_TIMER_MSEC,
RTADV_READ
};
static void rtadv_event(struct zebra_vrf *, enum rtadv_event, int);
static int if_join_all_router(int, struct interface *);
static int if_leave_all_router(int, struct interface *);
static struct zebra_vrf *rtadv_interface_get_zvrf(const struct interface *ifp)
{
/* We use the default vrf for rtadv handling except in netns */
if (!vrf_is_backend_netns())
return vrf_info_lookup(VRF_DEFAULT);
return ifp->vrf->info;
}
static int rtadv_increment_received(struct zebra_vrf *zvrf, ifindex_t *ifindex)
{
int ret = -1;
struct interface *iface;
struct zebra_if *zif;
iface = if_lookup_by_index(*ifindex, zvrf->vrf->vrf_id);
if (iface && iface->info) {
zif = iface->info;
zif->ra_rcvd++;
ret = 0;
}
return ret;
}
static int rtadv_recv_packet(struct zebra_vrf *zvrf, int sock, uint8_t *buf,
int buflen, struct sockaddr_in6 *from,
ifindex_t *ifindex, int *hoplimit)
{
int ret;
struct msghdr msg;
struct iovec iov;
struct cmsghdr *cmsgptr;
struct in6_addr dst;
char adata[1024];
/* Fill in message and iovec. */
memset(&msg, 0, sizeof(msg));
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 = buflen;
/* If recvmsg fail return minus value. */
ret = recvmsg(sock, &msg, 0);
if (ret < 0)
return ret;
for (cmsgptr = CMSG_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;
memcpy(&dst, &ptr->ipi6_addr, sizeof(ptr->ipi6_addr));
}
/* Incoming packet's hop limit. */
if (cmsgptr->cmsg_level == IPPROTO_IPV6
&& cmsgptr->cmsg_type == IPV6_HOPLIMIT) {
int *hoptr = (int *)CMSG_DATA(cmsgptr);
*hoplimit = *hoptr;
}
}
rtadv_increment_received(zvrf, ifindex);
return ret;
}
#define RTADV_MSG_SIZE 4096
/* Send router advertisement packet. */
static void rtadv_send_packet(int sock, struct interface *ifp,
enum ipv6_nd_suppress_ra_status stop)
{
struct msghdr msg = { 0 };
struct iovec iov = { 0 };
struct cmsghdr *cmsgptr;
struct in6_pktinfo *pkt;
struct sockaddr_in6 addr = { 0 };
unsigned char buf[RTADV_MSG_SIZE] = { 0 };
char adata[RTADV_ADATA_SIZE] = { 0 };
struct nd_router_advert *rtadv;
int ret;
int len = 0;
struct zebra_if *zif;
struct rtadv_prefix *rprefix;
uint8_t all_nodes_addr[] = {0xff, 0x02, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1};
struct listnode *node;
uint16_t pkt_RouterLifetime;
/* Logging of packet. */
if (IS_ZEBRA_DEBUG_PACKET)
zlog_debug("%s(%s:%u): Tx RA, socket %u", ifp->name,
ifp->vrf->name, ifp->ifindex, sock);
/* Fill in sockaddr_in6. */
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_port = htons(IPPROTO_ICMPV6);
IPV6_ADDR_COPY(&addr.sin6_addr, all_nodes_addr);
/* Fetch interface information. */
zif = ifp->info;
/* Make router advertisement message. */
rtadv = (struct nd_router_advert *)buf;
rtadv->nd_ra_type = ND_ROUTER_ADVERT;
rtadv->nd_ra_code = 0;
rtadv->nd_ra_cksum = 0;
rtadv->nd_ra_curhoplimit = zif->rtadv.AdvCurHopLimit;
/* RFC4191: Default Router Preference is 0 if Router Lifetime is 0. */
rtadv->nd_ra_flags_reserved = zif->rtadv.AdvDefaultLifetime == 0
? 0
: zif->rtadv.DefaultPreference;
rtadv->nd_ra_flags_reserved <<= 3;
if (zif->rtadv.AdvManagedFlag)
rtadv->nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED;
if (zif->rtadv.AdvOtherConfigFlag)
rtadv->nd_ra_flags_reserved |= ND_RA_FLAG_OTHER;
if (zif->rtadv.AdvHomeAgentFlag)
rtadv->nd_ra_flags_reserved |= ND_RA_FLAG_HOME_AGENT;
/* Note that according to Neighbor Discovery (RFC 4861 [18]),
* AdvDefaultLifetime is by default based on the value of
* MaxRtrAdvInterval. AdvDefaultLifetime is used in the Router Lifetime
* field of Router Advertisements. Given that this field is expressed
* in seconds, a small MaxRtrAdvInterval value can result in a zero
* value for this field. To prevent this, routers SHOULD keep
* AdvDefaultLifetime in at least one second, even if the use of
* MaxRtrAdvInterval would result in a smaller value. -- RFC6275, 7.5 */
pkt_RouterLifetime =
zif->rtadv.AdvDefaultLifetime != -1
? zif->rtadv.AdvDefaultLifetime
: MAX(1, 0.003 * zif->rtadv.MaxRtrAdvInterval);
/* send RA lifetime of 0 before stopping. rfc4861/6.2.5 */
rtadv->nd_ra_router_lifetime =
(stop == RA_SUPPRESS) ? htons(0) : htons(pkt_RouterLifetime);
rtadv->nd_ra_reachable = htonl(zif->rtadv.AdvReachableTime);
rtadv->nd_ra_retransmit = htonl(zif->rtadv.AdvRetransTimer);
len = sizeof(struct nd_router_advert);
/* If both the Home Agent Preference and Home Agent Lifetime are set to
* their default values specified above, this option SHOULD NOT be
* included in the Router Advertisement messages sent by this home
* agent. -- RFC6275, 7.4 */
if (zif->rtadv.AdvHomeAgentFlag
&& (zif->rtadv.HomeAgentPreference
|| zif->rtadv.HomeAgentLifetime != -1)) {
struct nd_opt_homeagent_info *ndopt_hai =
(struct nd_opt_homeagent_info *)(buf + len);
ndopt_hai->nd_opt_hai_type = ND_OPT_HA_INFORMATION;
ndopt_hai->nd_opt_hai_len = 1;
ndopt_hai->nd_opt_hai_reserved = 0;
ndopt_hai->nd_opt_hai_preference =
htons(zif->rtadv.HomeAgentPreference);
/* 16-bit unsigned integer. The lifetime associated with the
* home
* agent in units of seconds. The default value is the same as
* the
* Router Lifetime, as specified in the main body of the Router
* Advertisement. The maximum value corresponds to 18.2 hours.
* A
* value of 0 MUST NOT be used. -- RFC6275, 7.5 */
ndopt_hai->nd_opt_hai_lifetime =
htons(zif->rtadv.HomeAgentLifetime != -1
? zif->rtadv.HomeAgentLifetime
: MAX(1, pkt_RouterLifetime) /* 0 is OK
for RL,
but not
for HAL*/
);
len += sizeof(struct nd_opt_homeagent_info);
}
if (zif->rtadv.AdvIntervalOption) {
struct nd_opt_adv_interval *ndopt_adv =
(struct nd_opt_adv_interval *)(buf + len);
ndopt_adv->nd_opt_ai_type = ND_OPT_ADV_INTERVAL;
ndopt_adv->nd_opt_ai_len = 1;
ndopt_adv->nd_opt_ai_reserved = 0;
ndopt_adv->nd_opt_ai_interval =
htonl(zif->rtadv.MaxRtrAdvInterval);
len += sizeof(struct nd_opt_adv_interval);
}
/* Fill in prefix. */
frr_each (rtadv_prefixes, zif->rtadv.prefixes, rprefix) {
struct nd_opt_prefix_info *pinfo;
pinfo = (struct nd_opt_prefix_info *)(buf + len);
pinfo->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
pinfo->nd_opt_pi_len = 4;
pinfo->nd_opt_pi_prefix_len = rprefix->prefix.prefixlen;
pinfo->nd_opt_pi_flags_reserved = 0;
if (rprefix->AdvOnLinkFlag)
pinfo->nd_opt_pi_flags_reserved |=
ND_OPT_PI_FLAG_ONLINK;
if (rprefix->AdvAutonomousFlag)
pinfo->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO;
if (rprefix->AdvRouterAddressFlag)
pinfo->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_RADDR;
pinfo->nd_opt_pi_valid_time = htonl(rprefix->AdvValidLifetime);
pinfo->nd_opt_pi_preferred_time =
htonl(rprefix->AdvPreferredLifetime);
pinfo->nd_opt_pi_reserved2 = 0;
IPV6_ADDR_COPY(&pinfo->nd_opt_pi_prefix,
&rprefix->prefix.prefix);
len += sizeof(struct nd_opt_prefix_info);
}
/* Hardware address. */
if (ifp->hw_addr_len != 0) {
buf[len++] = ND_OPT_SOURCE_LINKADDR;
/* Option length should be rounded up to next octet if
the link address does not end on an octet boundary. */
buf[len++] = (ifp->hw_addr_len + 9) >> 3;
memcpy(buf + len, ifp->hw_addr, ifp->hw_addr_len);
len += ifp->hw_addr_len;
/* Pad option to end on an octet boundary. */
memset(buf + len, 0, -(ifp->hw_addr_len + 2) & 0x7);
len += -(ifp->hw_addr_len + 2) & 0x7;
}
/* MTU */
if (zif->rtadv.AdvLinkMTU) {
struct nd_opt_mtu *opt = (struct nd_opt_mtu *)(buf + len);
opt->nd_opt_mtu_type = ND_OPT_MTU;
opt->nd_opt_mtu_len = 1;
opt->nd_opt_mtu_reserved = 0;
opt->nd_opt_mtu_mtu = htonl(zif->rtadv.AdvLinkMTU);
len += sizeof(struct nd_opt_mtu);
}
/*
* There is no limit on the number of configurable recursive DNS
* servers or search list entries. We don't want the RA message
* to exceed the link's MTU (risking fragmentation) or even
* blow the stack buffer allocated for it.
*/
size_t max_len = MIN(ifp->mtu6 - 40, sizeof(buf));
/* Recursive DNS servers */
struct rtadv_rdnss *rdnss;
for (ALL_LIST_ELEMENTS_RO(zif->rtadv.AdvRDNSSList, node, rdnss)) {
size_t opt_len =
sizeof(struct nd_opt_rdnss) + sizeof(struct in6_addr);
if (len + opt_len > max_len) {
zlog_warn(
"%s(%s:%u): Tx RA: RDNSS option would exceed MTU, omitting it",
ifp->name, ifp->vrf->name, ifp->ifindex);
goto no_more_opts;
}
struct nd_opt_rdnss *opt = (struct nd_opt_rdnss *)(buf + len);
opt->nd_opt_rdnss_type = ND_OPT_RDNSS;
opt->nd_opt_rdnss_len = opt_len / 8;
opt->nd_opt_rdnss_reserved = 0;
opt->nd_opt_rdnss_lifetime = htonl(
rdnss->lifetime_set
? rdnss->lifetime
: MAX(1, 0.003 * zif->rtadv.MaxRtrAdvInterval));
len += sizeof(struct nd_opt_rdnss);
IPV6_ADDR_COPY(buf + len, &rdnss->addr);
len += sizeof(struct in6_addr);
}
/* DNS search list */
struct rtadv_dnssl *dnssl;
for (ALL_LIST_ELEMENTS_RO(zif->rtadv.AdvDNSSLList, node, dnssl)) {
size_t opt_len = sizeof(struct nd_opt_dnssl)
+ ((dnssl->encoded_len + 7) & ~7);
if (len + opt_len > max_len) {
zlog_warn(
"%s(%u): Tx RA: DNSSL option would exceed MTU, omitting it",
ifp->name, ifp->ifindex);
goto no_more_opts;
}
struct nd_opt_dnssl *opt = (struct nd_opt_dnssl *)(buf + len);
opt->nd_opt_dnssl_type = ND_OPT_DNSSL;
opt->nd_opt_dnssl_len = opt_len / 8;
opt->nd_opt_dnssl_reserved = 0;
opt->nd_opt_dnssl_lifetime = htonl(
dnssl->lifetime_set
? dnssl->lifetime
: MAX(1, 0.003 * zif->rtadv.MaxRtrAdvInterval));
len += sizeof(struct nd_opt_dnssl);
memcpy(buf + len, dnssl->encoded_name, dnssl->encoded_len);
len += dnssl->encoded_len;
/* Zero-pad to 8-octet boundary */
while (len % 8)
buf[len++] = '\0';
}
no_more_opts:
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));
msg.msg_flags = 0;
iov.iov_base = buf;
iov.iov_len = len;
cmsgptr = CMSG_FIRSTHDR(&msg);
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(sock, &msg, 0);
if (ret < 0) {
flog_err_sys(EC_LIB_SOCKET,
"%s(%u): Tx RA failed, socket %u error %d (%s)",
ifp->name, ifp->ifindex, sock, errno,
safe_strerror(errno));
} else
zif->ra_sent++;
}
static void rtadv_timer(struct event *thread)
{
struct zebra_vrf *zvrf = EVENT_ARG(thread);
struct vrf *vrf;
struct interface *ifp;
struct zebra_if *zif;
int period;
zvrf->rtadv.ra_timer = NULL;
if (adv_if_list_count(&zvrf->rtadv.adv_msec_if) == 0) {
period = 1000; /* 1 s */
rtadv_event(zvrf, RTADV_TIMER, 1 /* 1 s */);
} else {
period = 10; /* 10 ms */
rtadv_event(zvrf, RTADV_TIMER_MSEC, 10 /* 10 ms */);
}
RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
FOR_ALL_INTERFACES (vrf, ifp) {
if (if_is_loopback(ifp) || !if_is_operative(ifp) ||
IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) ||
!connected_get_linklocal(ifp) ||
(vrf_is_backend_netns() &&
ifp->vrf->vrf_id != zvrf->vrf->vrf_id))
continue;
zif = ifp->info;
if (zif->rtadv.AdvSendAdvertisements) {
if (zif->rtadv.inFastRexmit
&& zif->rtadv.UseFastRexmit) {
/* We assume we fast rexmit every sec so
* no
* additional vars */
if (--zif->rtadv.NumFastReXmitsRemain
<= 0)
zif->rtadv.inFastRexmit = 0;
if (IS_ZEBRA_DEBUG_SEND)
zlog_debug(
"Fast RA Rexmit on interface %s(%s:%u)",
ifp->name,
ifp->vrf->name,
ifp->ifindex);
rtadv_send_packet(zvrf->rtadv.sock, ifp,
RA_ENABLE);
} else {
zif->rtadv.AdvIntervalTimer -= period;
if (zif->rtadv.AdvIntervalTimer <= 0) {
/* FIXME: using
MaxRtrAdvInterval each
time isn't what section
6.2.4 of RFC4861 tells to do.
*/
zif->rtadv.AdvIntervalTimer =
zif->rtadv
.MaxRtrAdvInterval;
rtadv_send_packet(
zvrf->rtadv.sock, ifp,
RA_ENABLE);
}
}
}
}
}
static void rtadv_process_solicit(struct interface *ifp)
{
struct zebra_vrf *zvrf;
struct zebra_if *zif;
zvrf = rtadv_interface_get_zvrf(ifp);
assert(zvrf);
zif = ifp->info;
/*
* If FastRetransmit is enabled, send the RA immediately.
* If not enabled but it has been more than MIN_DELAY_BETWEEN_RAS
* (3 seconds) since the last RA was sent, send it now and reset
* the timer to start at the max (configured) again.
* If not enabled and it is less than 3 seconds since the last
* RA packet was sent, set the timer for 3 seconds so the next
* one will be sent with a minimum of 3 seconds between RAs.
* RFC4861 sec 6.2.6
*/
if ((zif->rtadv.UseFastRexmit)
|| (zif->rtadv.AdvIntervalTimer <=
(zif->rtadv.MaxRtrAdvInterval - MIN_DELAY_BETWEEN_RAS))) {
rtadv_send_packet(zvrf->rtadv.sock, ifp, RA_ENABLE);
zif->rtadv.AdvIntervalTimer = zif->rtadv.MaxRtrAdvInterval;
} else
zif->rtadv.AdvIntervalTimer = MIN_DELAY_BETWEEN_RAS;
}
static const char *rtadv_optionalhdr2str(uint8_t opt_type)
{
switch (opt_type) {
case ND_OPT_SOURCE_LINKADDR:
return "Optional Source Link Address";
case ND_OPT_TARGET_LINKADDR:
return "Optional Target Link Address";
case ND_OPT_PREFIX_INFORMATION:
return "Optional Prefix Information";
case ND_OPT_REDIRECTED_HEADER:
return "Optional Redirected Header";
case ND_OPT_MTU:
return "Optional MTU";
case ND_OPT_RTR_ADV_INTERVAL:
return "Optional Advertisement Interval";
case ND_OPT_HOME_AGENT_INFO:
return "Optional Home Agent Information";
}
return "Unknown Optional Type";
}
/*
* This function processes optional attributes off of
* end of a RA packet received. At this point in
* time we only care about this in one situation
* which is when a interface does not have a LL
* v6 address. We still need to be able to install
* the mac address for v4 to v6 resolution
*/
static void rtadv_process_optional(uint8_t *optional, unsigned int len,
struct interface *ifp,
struct sockaddr_in6 *addr)
{
char *mac;
while (len > 0) {
struct nd_opt_hdr *opt_hdr = (struct nd_opt_hdr *)optional;
switch(opt_hdr->nd_opt_type) {
case ND_OPT_SOURCE_LINKADDR:
mac = (char *)(optional+2);
if_nbr_mac_to_ipv4ll_neigh_update(ifp, mac,
&addr->sin6_addr, 1);
break;
default:
if (IS_ZEBRA_DEBUG_PACKET)
zlog_debug(
"%s:Received Packet with optional Header type %s(%u) that is being ignored",
__func__,
rtadv_optionalhdr2str(
opt_hdr->nd_opt_type),
opt_hdr->nd_opt_type);
break;
}
len -= 8 * opt_hdr->nd_opt_len;
optional += 8 * opt_hdr->nd_opt_len;
}
}
static void rtadv_process_advert(uint8_t *msg, unsigned int len,
struct interface *ifp,
struct sockaddr_in6 *addr)
{
struct nd_router_advert *radvert;
char addr_str[INET6_ADDRSTRLEN];
struct zebra_if *zif;
struct prefix p;
zif = ifp->info;
inet_ntop(AF_INET6, &addr->sin6_addr, addr_str, INET6_ADDRSTRLEN);
if (len < sizeof(struct nd_router_advert)) {
if (IS_ZEBRA_DEBUG_PACKET)
zlog_debug(
"%s(%s:%u): Rx RA with invalid length %d from %s",
ifp->name, ifp->vrf->name, ifp->ifindex, len,
addr_str);
return;
}
if (!IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
rtadv_process_optional(msg + sizeof(struct nd_router_advert),
len - sizeof(struct nd_router_advert),
ifp, addr);
if (IS_ZEBRA_DEBUG_PACKET)
zlog_debug(
"%s(%s:%u): Rx RA with non-linklocal source address from %s",
ifp->name, ifp->vrf->name, ifp->ifindex,
addr_str);
return;
}
radvert = (struct nd_router_advert *)msg;
#define SIXHOUR2USEC (int64_t)6 * 60 * 60 * 1000000
if ((radvert->nd_ra_curhoplimit && zif->rtadv.AdvCurHopLimit) &&
(radvert->nd_ra_curhoplimit != zif->rtadv.AdvCurHopLimit) &&
(monotime_since(&zif->rtadv.lastadvcurhoplimit, NULL) >
SIXHOUR2USEC ||
zif->rtadv.lastadvcurhoplimit.tv_sec == 0)) {
flog_warn(
EC_ZEBRA_RA_PARAM_MISMATCH,
"%s(%u): Rx RA - our AdvCurHopLimit (%u) doesn't agree with %s (%u)",
ifp->name, ifp->ifindex, zif->rtadv.AdvCurHopLimit,
addr_str, radvert->nd_ra_curhoplimit);
monotime(&zif->rtadv.lastadvcurhoplimit);
}
if ((radvert->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) &&
!zif->rtadv.AdvManagedFlag &&
(monotime_since(&zif->rtadv.lastadvmanagedflag, NULL) >
SIXHOUR2USEC ||
zif->rtadv.lastadvmanagedflag.tv_sec == 0)) {
flog_warn(
EC_ZEBRA_RA_PARAM_MISMATCH,
"%s(%u): Rx RA - our AdvManagedFlag (%u) doesn't agree with %s (%u)",
ifp->name, ifp->ifindex, zif->rtadv.AdvManagedFlag,
addr_str,
!!CHECK_FLAG(radvert->nd_ra_flags_reserved,
ND_RA_FLAG_MANAGED));
monotime(&zif->rtadv.lastadvmanagedflag);
}
if ((radvert->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) &&
!zif->rtadv.AdvOtherConfigFlag &&
(monotime_since(&zif->rtadv.lastadvotherconfigflag, NULL) >
SIXHOUR2USEC ||
zif->rtadv.lastadvotherconfigflag.tv_sec == 0)) {
flog_warn(
EC_ZEBRA_RA_PARAM_MISMATCH,
"%s(%u): Rx RA - our AdvOtherConfigFlag (%u) doesn't agree with %s (%u)",
ifp->name, ifp->ifindex, zif->rtadv.AdvOtherConfigFlag,
addr_str,
!!CHECK_FLAG(radvert->nd_ra_flags_reserved,
ND_RA_FLAG_OTHER));
monotime(&zif->rtadv.lastadvotherconfigflag);
}
if ((radvert->nd_ra_reachable && zif->rtadv.AdvReachableTime) &&
(ntohl(radvert->nd_ra_reachable) != zif->rtadv.AdvReachableTime) &&
(monotime_since(&zif->rtadv.lastadvreachabletime, NULL) >
SIXHOUR2USEC ||
zif->rtadv.lastadvreachabletime.tv_sec == 0)) {
flog_warn(
EC_ZEBRA_RA_PARAM_MISMATCH,
"%s(%u): Rx RA - our AdvReachableTime (%u) doesn't agree with %s (%u)",
ifp->name, ifp->ifindex, zif->rtadv.AdvReachableTime,
addr_str, ntohl(radvert->nd_ra_reachable));
monotime(&zif->rtadv.lastadvreachabletime);
}
if ((radvert->nd_ra_retransmit && zif->rtadv.AdvRetransTimer) &&
(ntohl(radvert->nd_ra_retransmit) !=
(unsigned int)zif->rtadv.AdvRetransTimer) &&
(monotime_since(&zif->rtadv.lastadvretranstimer, NULL) >
SIXHOUR2USEC ||
zif->rtadv.lastadvretranstimer.tv_sec == 0)) {
flog_warn(
EC_ZEBRA_RA_PARAM_MISMATCH,
"%s(%u): Rx RA - our AdvRetransTimer (%u) doesn't agree with %s (%u)",
ifp->name, ifp->ifindex, zif->rtadv.AdvRetransTimer,
addr_str, ntohl(radvert->nd_ra_retransmit));
monotime(&zif->rtadv.lastadvretranstimer);
}
/* Create entry for neighbor if not known. */
p.family = AF_INET6;
IPV6_ADDR_COPY(&p.u.prefix6, &addr->sin6_addr);
p.prefixlen = IPV6_MAX_BITLEN;
if (!nbr_connected_check(ifp, &p))
nbr_connected_add_ipv6(ifp, &addr->sin6_addr);
}
static void rtadv_process_packet(uint8_t *buf, unsigned int len,
ifindex_t ifindex, int hoplimit,
struct sockaddr_in6 *from,
struct zebra_vrf *zvrf)
{
struct icmp6_hdr *icmph;
struct interface *ifp;
struct zebra_if *zif;
char addr_str[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &from->sin6_addr, addr_str, INET6_ADDRSTRLEN);
/* Interface search. */
ifp = if_lookup_by_index(ifindex, zvrf->vrf->vrf_id);
if (ifp == NULL) {
flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE,
"RA/RS received on unknown IF %u from %s", ifindex,
addr_str);
return;
}
if (IS_ZEBRA_DEBUG_PACKET)
zlog_debug("%s(%s:%u): Rx RA/RS len %d from %s", ifp->name,
ifp->vrf->name, ifp->ifindex, len, addr_str);
if (if_is_loopback(ifp))
return;
/* Check interface configuration. */
zif = ifp->info;
if (!zif->rtadv.AdvSendAdvertisements)
return;
/* ICMP message length check. */
if (len < sizeof(struct icmp6_hdr)) {
zlog_debug(
"%s(%s:%u): Rx RA with Invalid ICMPV6 packet length %d",
ifp->name, ifp->vrf->name, ifp->ifindex, len);
return;
}
icmph = (struct icmp6_hdr *)buf;
/* ICMP message type check. */
if (icmph->icmp6_type != ND_ROUTER_SOLICIT
&& icmph->icmp6_type != ND_ROUTER_ADVERT) {
zlog_debug("%s(%s:%u): Rx RA - Unwanted ICMPV6 message type %d",
ifp->name, ifp->vrf->name, ifp->ifindex,
icmph->icmp6_type);
return;
}
/* Hoplimit check. */
if (hoplimit >= 0 && hoplimit != 255) {
zlog_debug("%s(%s:%u): Rx RA - Invalid hoplimit %d", ifp->name,
ifp->vrf->name, ifp->ifindex, hoplimit);
return;
}
/* Check ICMP message type. */
if (icmph->icmp6_type == ND_ROUTER_SOLICIT)
rtadv_process_solicit(ifp);
else if (icmph->icmp6_type == ND_ROUTER_ADVERT)
rtadv_process_advert(buf, len, ifp, from);
return;
}
static void rtadv_read(struct event *thread)
{
int sock;
int len;
uint8_t buf[RTADV_MSG_SIZE];
struct sockaddr_in6 from;
ifindex_t ifindex = 0;
int hoplimit = -1;
struct zebra_vrf *zvrf = EVENT_ARG(thread);
sock = EVENT_FD(thread);
zvrf->rtadv.ra_read = NULL;
/* Register myself. */
rtadv_event(zvrf, RTADV_READ, 0);
len = rtadv_recv_packet(zvrf, sock, buf, sizeof(buf), &from, &ifindex,
&hoplimit);
if (len < 0) {
flog_err_sys(EC_LIB_SOCKET,
"RA/RS recv failed, socket %u error %s", sock,
safe_strerror(errno));
return;
}
rtadv_process_packet(buf, (unsigned)len, ifindex, hoplimit, &from, zvrf);
}
static int rtadv_make_socket(ns_id_t ns_id)
{
int sock = -1;
int ret = 0;
struct icmp6_filter filter;
int error;
frr_with_privs(&zserv_privs) {
sock = ns_socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6, ns_id);
/*
* with privs might set errno too if it fails save
* to the side
*/
error = errno;
}
if (sock < 0) {
zlog_warn("RTADV socket for ns: %u failure to create: %s(%u)",
ns_id, safe_strerror(error), error);
return -1;
}
ret = setsockopt_ipv6_pktinfo(sock, 1);
if (ret < 0) {
zlog_warn("RTADV failure to set Packet Information");
close(sock);
return ret;
}
ret = setsockopt_ipv6_multicast_loop(sock, 0);
if (ret < 0) {
zlog_warn("RTADV failure to set multicast Loop detection");
close(sock);
return ret;
}
ret = setsockopt_ipv6_unicast_hops(sock, 255);
if (ret < 0) {
zlog_warn("RTADV failure to set maximum unicast hops");
close(sock);
return ret;
}
ret = setsockopt_ipv6_multicast_hops(sock, 255);
if (ret < 0) {
zlog_warn("RTADV failure to set maximum multicast hops");
close(sock);
return ret;
}
ret = setsockopt_ipv6_hoplimit(sock, 1);
if (ret < 0) {
zlog_warn("RTADV failure to set maximum incoming hop limit");
close(sock);
return ret;
}
ICMP6_FILTER_SETBLOCKALL(&filter);
ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);
ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter);
ret = setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filter,
sizeof(struct icmp6_filter));
if (ret < 0) {
zlog_info("ICMP6_FILTER set fail: %s", safe_strerror(errno));
close(sock);
return ret;
}
return sock;
}
static struct adv_if *adv_if_new(const char *name)
{
struct adv_if *new;
new = XCALLOC(MTYPE_ADV_IF, sizeof(struct adv_if));
strlcpy(new->name, name, sizeof(new->name));
return new;
}
static void adv_if_free(struct adv_if *adv_if)
{
XFREE(MTYPE_ADV_IF, adv_if);
}
static bool adv_if_is_empty_internal(const struct adv_if_list_head *adv_if_head)
{
return adv_if_list_count(adv_if_head) ? false : true;
}
static struct adv_if *adv_if_add_internal(struct adv_if_list_head *adv_if_head,
const char *name)
{
struct adv_if adv_if_lookup = {};
struct adv_if *adv_if = NULL;
strlcpy(adv_if_lookup.name, name, sizeof(adv_if_lookup.name));
adv_if = adv_if_list_find(adv_if_head, &adv_if_lookup);
if (adv_if != NULL)
return adv_if;
adv_if = adv_if_new(adv_if_lookup.name);
adv_if_list_add(adv_if_head, adv_if);
return NULL;
}
static struct adv_if *adv_if_del_internal(struct adv_if_list_head *adv_if_head,
const char *name)
{
struct adv_if adv_if_lookup = {};
struct adv_if *adv_if = NULL;
strlcpy(adv_if_lookup.name, name, sizeof(adv_if_lookup.name));
adv_if = adv_if_list_find(adv_if_head, &adv_if_lookup);
if (adv_if == NULL)
return NULL;
adv_if_list_del(adv_if_head, adv_if);
return adv_if;
}
static void adv_if_clean_internal(struct adv_if_list_head *adv_if_head)
{
struct adv_if *node = NULL;
if (!adv_if_is_empty_internal(adv_if_head)) {
frr_each_safe (adv_if_list, adv_if_head, node) {
adv_if_list_del(adv_if_head, node);
adv_if_free(node);
}
}
adv_if_list_fini(adv_if_head);
}
/*
* Add to list. On Success, return NULL, otherwise return already existing
* adv_if.
*/
static struct adv_if *adv_if_add(struct zebra_vrf *zvrf, const char *name)
{
struct adv_if *adv_if = NULL;
adv_if = adv_if_add_internal(&zvrf->rtadv.adv_if, name);
if (adv_if != NULL)
return adv_if;
if (IS_ZEBRA_DEBUG_EVENT) {
struct vrf *vrf = zvrf->vrf;
zlog_debug("%s: %s:%u IF %s count: %zu", __func__,
VRF_LOGNAME(vrf), zvrf_id(zvrf), name,
adv_if_list_count(&zvrf->rtadv.adv_if));
}
return NULL;
}
/*