forked from troglobit/pimd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pim_proto.c
3194 lines (2797 loc) · 99.6 KB
/
pim_proto.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1998-2001
* University of Southern California/Information Sciences Institute.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* $Id: pim_proto.c,v 1.47 2003/05/28 22:57:16 pavlin Exp $
*/
#include <arpa/inet.h>
#include "defs.h"
/*
* Local functions definitions.
*/
static int parse_pim_hello (char *pim_message, size_t len, uint32_t src, uint16_t *holdtime);
static int send_pim_register_stop (uint32_t reg_src, uint32_t reg_dst, uint32_t inner_source, uint32_t inner_grp);
static build_jp_message_t *get_jp_working_buff (void);
static void return_jp_working_buff (pim_nbr_entry_t *pim_nbr);
static void pack_jp_message (pim_nbr_entry_t *pim_nbr);
static void send_jp_message (pim_nbr_entry_t *pim_nbr);
static int compare_metrics (uint32_t local_preference,
uint32_t local_metric,
uint32_t local_address,
uint32_t remote_preference,
uint32_t remote_metric,
uint32_t remote_address);
build_jp_message_t *build_jp_message_pool;
int build_jp_message_pool_counter;
/************************************************************************
* PIM_HELLO
************************************************************************/
int receive_pim_hello(uint32_t src, uint32_t dst __attribute__((unused)), char *pim_message, size_t len)
{
vifi_t vifi;
struct uvif *v;
pim_nbr_entry_t *nbr, *prev_nbr, *new_nbr;
uint16_t holdtime = 0;
int bsr_length;
uint8_t *data __attribute__((unused));
srcentry_t *srcentry;
mrtentry_t *mrtentry;
/* Checksum */
if (inet_cksum((uint16_t *)pim_message, len))
return FALSE;
if ((vifi = find_vif_direct(src)) == NO_VIF) {
/* Either a local vif or somehow received PIM_HELLO from
* non-directly connected router. Ignore it.
*/
if (local_address(src) == NO_VIF)
logit(LOG_INFO, 0, "Ignoring PIM_HELLO from non-neighbor router %s",
inet_fmt(src, s1, sizeof(s1)));
return FALSE;
}
v = &uvifs[vifi];
if (v->uv_flags & (VIFF_DOWN | VIFF_DISABLED | VIFF_REGISTER))
return FALSE; /* Shoudn't come on this interface */
data = (uint8_t *)(pim_message + sizeof(pim_header_t));
/* Get the Holdtime (in seconds) from the message. Return if error. */
if (parse_pim_hello(pim_message, len, src, &holdtime) == FALSE)
return FALSE;
IF_DEBUG(DEBUG_PIM_HELLO | DEBUG_PIM_TIMER) {
logit(LOG_DEBUG, 0, "PIM HELLO holdtime from %s is %u",
inet_fmt(src, s1, sizeof(s1)), holdtime);
}
for (prev_nbr = NULL, nbr = v->uv_pim_neighbors; nbr; prev_nbr = nbr, nbr = nbr->next) {
/* The PIM neighbors are sorted in decreasing order of the
* network addresses (note that to be able to compare them
* correctly we must translate the addresses in host order.
*/
if (ntohl(src) < ntohl(nbr->address))
continue;
if (src == nbr->address) {
/* We already have an entry for this host */
if (0 == holdtime) {
/* Looks like we have a nice neighbor who is going down
* and wants to inform us by sending "holdtime=0". Thanks
* buddy and see you again!
*/
logit(LOG_INFO, 0, "PIM HELLO received: neighbor %s going down",
inet_fmt(src, s1, sizeof(s1)));
delete_pim_nbr(nbr);
return TRUE;
}
SET_TIMER(nbr->timer, holdtime);
return TRUE;
} else {
/* No entry for this neighbor. Exit the loop and create an
* entry for it. */
break;
}
}
/*
* This is a new neighbor. Create a new entry for it.
* It must be added right after `prev_nbr`
*/
new_nbr = calloc(1, sizeof(pim_nbr_entry_t));
if (!new_nbr)
logit(LOG_ERR, 0, "Ran out of memory in receive_pim_hello()");
new_nbr->address = src;
new_nbr->vifi = vifi;
SET_TIMER(new_nbr->timer, holdtime);
new_nbr->build_jp_message = NULL;
new_nbr->next = nbr;
new_nbr->prev = prev_nbr;
if (prev_nbr)
prev_nbr->next = new_nbr;
else
v->uv_pim_neighbors = new_nbr;
if (new_nbr->next)
new_nbr->next->prev = new_nbr;
v->uv_flags &= ~VIFF_NONBRS;
v->uv_flags |= VIFF_PIM_NBR;
/* Since a new neighbour has come up, let it know your existence */
/* XXX: TODO: not in the spec,
* but probably should send the message after a short random period?
*/
send_pim_hello(v, PIM_TIMER_HELLO_HOLDTIME);
if (v->uv_flags & VIFF_DR) {
/*
* If I am the current DR on that interface, so
* send an RP-Set message to the new neighbor.
*/
if ((bsr_length = create_pim_bootstrap_message(pim_send_buf)))
send_pim_unicast(pim_send_buf, v->uv_mtu, v->uv_lcl_addr, src, PIM_BOOTSTRAP, bsr_length);
/* The router with highest network address is the elected DR */
if (ntohl(v->uv_lcl_addr) < ntohl(src)) {
/* I was the DR, but not anymore. Remove all register_vif from
* oif list for all directly connected sources (for vifi).
*/
/* TODO: XXX: first entry is not used! */
for (srcentry = srclist->next; srcentry; srcentry = srcentry->next) {
/* If not directly connected source for vifi */
if ((srcentry->incoming != vifi) || srcentry->upstream)
continue;
for (mrtentry = srcentry->mrtlink; mrtentry; mrtentry = mrtentry->srcnext) {
if (!(mrtentry->flags & MRTF_SG))
continue; /* This is not (S,G) entry */
/* Remove the register oif */
VIFM_CLR(reg_vif_num, mrtentry->joined_oifs);
change_interfaces(mrtentry,
mrtentry->incoming,
mrtentry->joined_oifs,
mrtentry->pruned_oifs,
mrtentry->leaves,
mrtentry->asserted_oifs, 0);
}
}
v->uv_flags &= ~VIFF_DR;
}
}
/*
* TODO: XXX: does a new neighbor change any routing entries info?
* Need to trigger joins?
*/
IF_DEBUG(DEBUG_PIM_HELLO)
dump_vifs(stderr); /* Show we got a new neighbor */
return TRUE;
}
void delete_pim_nbr(pim_nbr_entry_t *nbr_delete)
{
srcentry_t *src;
srcentry_t *src_next;
mrtentry_t *mrt;
mrtentry_t *mrt_srcs;
grpentry_t *grp;
cand_rp_t *cand_rp;
rp_grp_entry_t *rp_grp;
rpentry_t *rp;
struct uvif *v;
v = &uvifs[nbr_delete->vifi];
/* Delete the entry from the pim_nbrs chain */
if (nbr_delete->prev)
nbr_delete->prev->next = nbr_delete->next;
else
v->uv_pim_neighbors = nbr_delete->next;
if (nbr_delete->next)
nbr_delete->next->prev = nbr_delete->prev;
return_jp_working_buff(nbr_delete);
if (!v->uv_pim_neighbors) {
/* This was our last neighbor. */
v->uv_flags &= ~VIFF_PIM_NBR;
v->uv_flags |= (VIFF_NONBRS | VIFF_DR);
} else {
if (ntohl(v->uv_lcl_addr) > ntohl(v->uv_pim_neighbors->address))
/* The first address is the new potential remote
* DR address, but the local address is the winner.
*/
v->uv_flags |= VIFF_DR;
}
/* Update the source entries */
for (src = srclist; src; src = src_next) {
src_next = src->next;
if (src->upstream != nbr_delete)
continue;
/* Reset the next hop (PIM) router */
if (set_incoming(src, PIM_IIF_SOURCE) == FALSE) {
/* Coudn't reset it. Sorry, the hext hop router toward that
* source is probably not a PIM router, or cannot find route
* at all, hence I cannot handle this source and have to
* delete it.
*/
delete_srcentry(src);
} else if (src->upstream) {
/* Ignore the local or directly connected sources */
/* Browse all MRT entries for this source and reset the
* upstream router. Note that the upstream router is not always
* toward the source: it could be toward the RP for example.
*/
for (mrt = src->mrtlink; mrt; mrt = mrt->srcnext) {
if (!(mrt->flags & MRTF_RP)) {
mrt->upstream = src->upstream;
mrt->metric = src->metric;
mrt->preference = src->preference;
change_interfaces(mrt, src->incoming,
mrt->joined_oifs,
mrt->pruned_oifs,
mrt->leaves,
mrt->asserted_oifs, 0);
}
}
}
}
/* Update the RP entries */
for (cand_rp = cand_rp_list; cand_rp; cand_rp = cand_rp->next) {
if (cand_rp->rpentry->upstream != nbr_delete)
continue;
rp = cand_rp->rpentry;
/* Reset the RP entry iif
* TODO: check if error setting the iif! */
if (local_address(rp->address) == NO_VIF) {
set_incoming(rp, PIM_IIF_RP);
} else {
rp->incoming = reg_vif_num;
rp->upstream = NULL;
}
mrt = rp->mrtlink;
if (mrt) {
mrt->upstream = rp->upstream;
mrt->metric = rp->metric;
mrt->preference = rp->preference;
change_interfaces(mrt,
rp->incoming,
mrt->joined_oifs,
mrt->pruned_oifs,
mrt->leaves,
mrt->asserted_oifs, 0);
}
/* Update the group entries for this RP */
for (rp_grp = cand_rp->rp_grp_next; rp_grp; rp_grp = rp_grp->rp_grp_next) {
for (grp = rp_grp->grplink; grp; grp = grp->rpnext) {
mrt = grp->grp_route;
if (mrt) {
mrt->upstream = rp->upstream;
mrt->metric = rp->metric;
mrt->preference = rp->preference;
change_interfaces(mrt,
rp->incoming,
mrt->joined_oifs,
mrt->pruned_oifs,
mrt->leaves,
mrt->asserted_oifs, 0);
}
/* Update only the (S,G)RPbit entries for this group */
for (mrt_srcs = grp->mrtlink; mrt_srcs; mrt_srcs = mrt_srcs->grpnext) {
if (mrt_srcs->flags & MRTF_RP) {
mrt_srcs->upstream = rp->upstream;
mrt_srcs->metric = rp->metric;
mrt_srcs->preference = rp->preference;
change_interfaces(mrt_srcs,
rp->incoming,
mrt_srcs->joined_oifs,
mrt_srcs->pruned_oifs,
mrt_srcs->leaves,
mrt_srcs->asserted_oifs, 0);
}
}
}
}
}
/* Fix GitHub issue #22: Crash in (S,G) state when neighbor is lost */
for (cand_rp = cand_rp_list; cand_rp; cand_rp = cand_rp->next) {
for (rp_grp = cand_rp->rp_grp_next; rp_grp; rp_grp = rp_grp->rp_grp_next) {
for (grp = rp_grp->grplink; grp; grp = grp->next) {
mrt = grp->grp_route;
if (mrt && mrt->upstream) {
if (mrt->upstream == nbr_delete)
mrt->upstream = NULL;
}
}
}
}
free(nbr_delete);
}
/* TODO: simplify it! */
static int parse_pim_hello(char *pim_message, size_t len, uint32_t src, uint16_t *holdtime)
{
uint8_t *pim_hello_message;
uint8_t *data;
uint16_t option_type;
uint16_t option_length;
int holdtime_received_ok = FALSE;
int option_total_length;
pim_hello_message = (uint8_t *)(pim_message + sizeof(pim_header_t));
len -= sizeof(pim_header_t);
for ( ; len >= sizeof(pim_hello_t); ) {
/* Ignore any data if shorter than (pim_hello header) */
data = pim_hello_message;
GET_HOSTSHORT(option_type, data);
GET_HOSTSHORT(option_length, data);
if (option_type == PIM_MESSAGE_HELLO_HOLDTIME) {
if (PIM_MESSAGE_HELLO_HOLDTIME_LENGTH != option_length) {
IF_DEBUG(DEBUG_PIM_HELLO) {
logit(LOG_DEBUG, 0, "PIM HELLO Holdtime from %s: invalid OptionLength = %u",
inet_fmt(src, s1, sizeof(s1)), option_length);
}
return FALSE;
}
GET_HOSTSHORT(*holdtime, data);
holdtime_received_ok = TRUE;
} else {
/* Ignore any unknown options */
}
/* Move to the next option */
/* XXX: TODO: If we are padding to the end of the 32 bit boundary,
* use the first method to move to the next option, otherwise
* simply (sizeof(pim_hello_t) + option_length).
*/
#ifdef BOUNDARY_32_BIT
option_total_length = (sizeof(pim_hello_t) + (option_length & ~0x3) +
((option_length & 0x3) ? 4 : 0));
#else
option_total_length = (sizeof(pim_hello_t) + option_length);
#endif /* BOUNDARY_32_BIT */
len -= option_total_length;
pim_hello_message += option_total_length;
}
return holdtime_received_ok;
}
int send_pim_hello(struct uvif *v, uint16_t holdtime)
{
char *buf;
uint8_t *data;
size_t len;
buf = pim_send_buf + sizeof(struct ip) + sizeof(pim_header_t);
data = (uint8_t *)buf;
PUT_HOSTSHORT(PIM_MESSAGE_HELLO_HOLDTIME, data);
PUT_HOSTSHORT(PIM_MESSAGE_HELLO_HOLDTIME_LENGTH, data);
PUT_HOSTSHORT(holdtime, data);
len = data - (uint8_t *)buf;
send_pim(pim_send_buf, v->uv_lcl_addr, allpimrouters_group, PIM_HELLO, len);
SET_TIMER(v->uv_pim_hello_timer, PIM_TIMER_HELLO_PERIOD);
return TRUE;
}
/************************************************************************
* PIM_REGISTER
************************************************************************/
/* TODO: XXX: IF THE BORDER BIT IS SET, THEN
* FORWARD THE WHOLE PACKET FROM USER SPACE
* AND AT THE SAME TIME IGNORE ANY CACHE_MISS
* SIGNALS FROM THE KERNEL.
*/
int receive_pim_register(uint32_t reg_src, uint32_t reg_dst, char *pim_message, size_t len)
{
uint32_t inner_src, inner_grp;
pim_register_t *reg;
struct ip *ip;
uint32_t is_border, is_null;
mrtentry_t *mrtentry;
mrtentry_t *mrtentry2;
vifbitmap_t oifs;
IF_DEBUG(DEBUG_PIM_REGISTER) {
logit(LOG_INFO, 0, "Received PIM register: len = %d from %s",
len, inet_fmt(reg_src, s1, sizeof(s1)));
}
/*
* Message length validation.
* This is suppose to be done in the kernel, but some older kernel
* versions do not pefrorm the check for the NULL register messages.
*/
if (len < sizeof(pim_header_t) + sizeof(pim_register_t) + sizeof(struct ip)) {
IF_DEBUG(DEBUG_PIM_REGISTER) {
logit(LOG_INFO, 0, "PIM register: short packet (len = %d) from %s",
len, inet_fmt(reg_src, s1, sizeof(s1)));
}
return FALSE;
}
/*
* XXX: For PIM_REGISTER the checksum does not include
* the inner IP packet. However, some older routers might
* create the checksum over the whole packet. Hence,
* verify the checksum over the first 8 bytes, and if fails,
* then over the whole Register
*/
if ((inet_cksum((uint16_t *)pim_message, sizeof(pim_header_t) + sizeof(pim_register_t)))
&& (inet_cksum((uint16_t *)pim_message, len))) {
IF_DEBUG(DEBUG_PIM_REGISTER) {
logit(LOG_DEBUG, 0, "PIM REGISTER from DR %s: invalid PIM header checksum",
inet_fmt(reg_src, s1, sizeof(s1)));
}
return FALSE;
}
/* Lookup register message flags */
reg = (pim_register_t *)(pim_message + sizeof(pim_header_t));
is_border = ntohl(reg->reg_flags) & PIM_MESSAGE_REGISTER_BORDER_BIT;
is_null = ntohl(reg->reg_flags) & PIM_MESSAGE_REGISTER_NULL_REGISTER_BIT;
/* initialize the pointer to the encapsulated packet */
ip = (struct ip *)(reg + 1);
/* check the IP version (especially for the NULL register...see above) */
if (ip->ip_v != IPVERSION && (! is_null)) {
IF_DEBUG(DEBUG_PIM_REGISTER)
logit(LOG_INFO, 0, "PIM register: incorrect IP version (%d) of the inner packet from %s",
ip->ip_v, inet_fmt(reg_src, s1, sizeof(s1)));
return FALSE;
}
/* We are keeping all addresses in network order, so no need for ntohl()*/
inner_src = ip->ip_src.s_addr;
inner_grp = ip->ip_dst.s_addr;
/*
* inner_src and inner_grp must be valid IP unicast and multicast address
* respectively. XXX: not in the spec.
*/
if ((!inet_valid_host(inner_src)) || (!IN_MULTICAST(ntohl(inner_grp)))) {
if (!inet_valid_host(inner_src)) {
logit(LOG_WARNING, 0, "Inner source address of register message by %s is invalid: %s",
inet_fmt(reg_src, s1, sizeof(s1)), inet_fmt(inner_src, s2, sizeof(s2)));
}
if (!IN_MULTICAST(ntohl(inner_grp))) {
logit(LOG_WARNING, 0, "Inner group address of register message by %s is invalid: %s",
inet_fmt(reg_src, s1, sizeof(s1)), inet_fmt(inner_grp, s2, sizeof(s2)));
}
send_pim_register_stop(reg_dst, reg_src, inner_grp, inner_src);
return FALSE;
}
mrtentry = find_route(inner_src, inner_grp, MRTF_SG | MRTF_WC | MRTF_PMBR, DONT_CREATE);
if (!mrtentry) {
/* No routing entry. Send REGISTER_STOP and return. */
IF_DEBUG(DEBUG_PIM_REGISTER) {
logit(LOG_DEBUG, 0, "No routing entry for source %s and/or group %s" ,
inet_fmt(inner_src, s1, sizeof(s1)), inet_fmt(inner_grp, s2, sizeof(s2)));
}
/* TODO: XXX: shouldn't it be inner_src=INADDR_ANY? Not in the spec. */
send_pim_register_stop(reg_dst, reg_src, inner_grp, inner_src);
return TRUE;
}
/* Check if I am the RP for that group */
if ((local_address(reg_dst) == NO_VIF) || !check_mrtentry_rp(mrtentry, reg_dst)) {
send_pim_register_stop(reg_dst, reg_src, inner_grp, inner_src);
return TRUE;
}
/* I am the RP */
if (mrtentry->flags & MRTF_SG) {
/* (S,G) found */
/* TODO: check the timer again */
SET_TIMER(mrtentry->timer, PIM_DATA_TIMEOUT); /* restart timer */
if (!(mrtentry->flags & MRTF_SPT)) { /* The SPT bit is not set */
if (!is_null) {
calc_oifs(mrtentry, &oifs);
if (VIFM_ISEMPTY(oifs) && (mrtentry->incoming == reg_vif_num)) {
send_pim_register_stop(reg_dst, reg_src, inner_grp, inner_src);
return TRUE;
}
/*
* TODO: XXX: BUG!!!
* The data will be forwarded by the kernel MFC!!!
* Need to set a special flag for this routing entry so after
* a cache miss occur, the multicast packet will be forwarded
* from user space and won't install entry in the kernel MFC.
* The problem is that the kernel MFC doesn't know the
* PMBR address and simply sets the multicast forwarding
* cache to accept/forward all data coming from the
* register_vif.
*/
if (is_border) {
if (mrtentry->pmbr_addr != reg_src) {
send_pim_register_stop(reg_dst, reg_src, inner_grp, inner_src);
return TRUE;
}
}
return TRUE;
}
/* TODO: XXX: if NULL_REGISTER and has (S,G) with SPT=0, then..?*/
return TRUE;
}
else {
/* The SPT bit is set */
send_pim_register_stop(reg_dst, reg_src, inner_grp, inner_src);
return TRUE;
}
}
if (mrtentry->flags & (MRTF_WC | MRTF_PMBR)) {
if (is_border) {
/* Create (S,G) state. The oifs will be the copied from the
* existing (*,G) or (*,*,RP) entry. */
mrtentry2 = find_route(inner_src, inner_grp, MRTF_SG, CREATE);
if (mrtentry2) {
mrtentry2->pmbr_addr = reg_src;
/* Clear the SPT flag */
mrtentry2->flags &= ~(MRTF_SPT | MRTF_NEW);
SET_TIMER(mrtentry2->timer, PIM_DATA_TIMEOUT);
/* TODO: explicitly call the Join/Prune send function? */
FIRE_TIMER(mrtentry2->jp_timer); /* Send the Join immediately */
/* TODO: explicitly call this function?
send_pim_join_prune(mrtentry2->upstream->vifi,
mrtentry2->upstream,
PIM_JOIN_PRUNE_HOLDTIME);
*/
}
}
}
if (mrtentry->flags & MRTF_WC) {
/* (*,G) entry */
calc_oifs(mrtentry, &oifs);
if (VIFM_ISEMPTY(oifs)) {
send_pim_register_stop(reg_dst, reg_src, inner_grp, INADDR_ANY_N);
return FALSE;
}
/* XXX: TODO: check with the spec again */
else {
if (!is_null) {
/* Install cache entry in the kernel */
/* TODO: XXX: probably redundant here, because the
* decapsulated mcast packet in the kernel will
* result in CACHE_MISS
*/
uint32_t mfc_source = inner_src;
#ifdef KERNEL_MFC_WC_G
if (!(mrtentry->flags & MRTF_MFC_CLONE_SG))
mfc_source = INADDR_ANY_N;
#endif /* KERNEL_MFC_WC_G */
add_kernel_cache(mrtentry, mfc_source, inner_grp, 0);
k_chg_mfc(igmp_socket, mfc_source, inner_grp,
mrtentry->incoming, mrtentry->oifs,
mrtentry->group->rpaddr);
return TRUE;
}
}
return TRUE;
}
if (mrtentry->flags & MRTF_PMBR) {
/* (*,*,RP) entry */
if (!is_null) {
uint32_t mfc_source = inner_src;
/* XXX: have to create either (S,G) or (*,G).
* The choice below is (*,G)
*/
mrtentry2 = find_route(INADDR_ANY_N, inner_grp, MRTF_WC, CREATE);
if (!mrtentry2)
return FALSE;
if (mrtentry2->flags & MRTF_NEW) {
/* TODO: something else? Have the feeling sth is missing */
mrtentry2->flags &= ~MRTF_NEW;
/* TODO: XXX: copy the timer from the (*,*,RP) entry? */
COPY_TIMER(mrtentry->timer, mrtentry2->timer);
}
/* Install cache entry in the kernel */
#ifdef KERNEL_MFC_WC_G
if (!(mrtentry->flags & MRTF_MFC_CLONE_SG))
mfc_source = INADDR_ANY_N;
#endif /* KERNEL_MFC_WC_G */
add_kernel_cache(mrtentry, mfc_source, inner_grp, 0);
k_chg_mfc(igmp_socket, mfc_source, inner_grp,
mrtentry->incoming, mrtentry->oifs,
mrtentry2->group->rpaddr);
return TRUE;
}
}
/* Shoudn't happen: invalid routing entry? */
/* XXX: TODO: shoudn't be inner_src=INADDR_ANY? Not in the spec. */
send_pim_register_stop(reg_dst, reg_src, inner_grp, inner_src);
return TRUE;
}
int send_pim_register(char *packet)
{
struct ip *ip;
uint32_t source, group;
vifi_t vifi;
rpentry_t *rpentry;
mrtentry_t *mrtentry;
mrtentry_t *mrtentry2;
uint32_t reg_src, reg_dst;
int reg_mtu, pktlen = 0;
char *buf;
ip = (struct ip *)packet;
source = ip->ip_src.s_addr;
group = ip->ip_dst.s_addr;
if ((vifi = find_vif_direct_local(source)) == NO_VIF)
return FALSE;
if (!(uvifs[vifi].uv_flags & VIFF_DR))
return FALSE; /* I am not the DR for that subnet */
rpentry = rp_match(group);
if (!rpentry)
return FALSE; /* No RP for this group */
if (local_address(rpentry->address) != NO_VIF) {
/* TODO: XXX: not sure it is working! */
return FALSE; /* I am the RP for this group */
}
mrtentry = find_route(source, group, MRTF_SG, CREATE);
if (!mrtentry)
return FALSE; /* Cannot create (S,G) state */
if (mrtentry->flags & MRTF_NEW) {
/* A new entry */
mrtentry->flags &= ~MRTF_NEW;
RESET_TIMER(mrtentry->rs_timer); /* Reset the Register-Suppression timer */
mrtentry2 = mrtentry->group->grp_route;
if (!mrtentry2)
mrtentry2 = mrtentry->group->active_rp_grp->rp->rpentry->mrtlink;
if (mrtentry2) {
FIRE_TIMER(mrtentry2->jp_timer); /* Timeout the Join/Prune timer */
/* TODO: explicitly call this function?
send_pim_join_prune(mrtentry2->upstream->vifi,
mrtentry2->upstream,
PIM_JOIN_PRUNE_HOLDTIME);
*/
}
}
/* Restart the (S,G) Entry-timer */
SET_TIMER(mrtentry->timer, PIM_DATA_TIMEOUT);
IF_TIMER_NOT_SET(mrtentry->rs_timer) {
/* The Register-Suppression Timer is not running.
* Encapsulate the data and send to the RP.
*/
buf = pim_send_buf + sizeof(struct ip) + sizeof(pim_header_t);
memset(buf, 0, sizeof(pim_register_t)); /* No flags set */
buf += sizeof(pim_register_t);
/* Copy the data packet at the back of the register packet */
#ifdef HAVE_IP_HDRINCL_BSD_ORDER
pktlen = ip->ip_len;
#else
pktlen = ntohs(ip->ip_len);
#endif
memcpy(buf, ip, pktlen);
pktlen += sizeof(pim_register_t); /* 'sizeof(struct ip) + sizeof(pim_header_t)' added by send_pim() */
reg_mtu = uvifs[vifi].uv_mtu; /* XXX: Use PMTU to RP instead! */
reg_src = uvifs[vifi].uv_lcl_addr;
reg_dst = mrtentry->group->rpaddr;
IF_DEBUG(DEBUG_PIM_REGISTER) {
logit(LOG_DEBUG, 0, "Composing PIM_REGISTER (%zd + %zd + %zd) %d bytes to RP %s for src = %s and group = %s",
sizeof(struct ip), sizeof(pim_header_t) + sizeof(pim_register_t),
htons(ip->ip_len), pktlen,
inet_fmt(reg_dst, s1, sizeof(s1)),
inet_fmt(reg_src, s2, sizeof(s2)),
inet_fmt(group, s3, sizeof(s3)));
}
send_pim_unicast(pim_send_buf, reg_mtu, reg_src, reg_dst, PIM_REGISTER, pktlen);
return TRUE;
}
return TRUE;
}
int send_pim_null_register(mrtentry_t *mrtentry)
{
struct ip *ip;
pim_register_t *pim_register;
int reg_mtu, pktlen;
vifi_t vifi;
uint32_t reg_src, reg_dst;
/* No directly connected source; no local address */
if ((vifi = find_vif_direct_local(mrtentry->source->address))== NO_VIF)
return FALSE;
pim_register = (pim_register_t *)(pim_send_buf + sizeof(struct ip) +
sizeof(pim_header_t));
memset(pim_register, 0, sizeof(pim_register_t));
pim_register->reg_flags = htonl(pim_register->reg_flags
| PIM_MESSAGE_REGISTER_NULL_REGISTER_BIT);
ip = (struct ip *)(pim_register + 1);
/* set src/dst in dummy hdr */
ip->ip_v = IPVERSION;
ip->ip_hl = (sizeof(struct ip) >> 2);
ip->ip_tos = 0;
ip->ip_id = 0;
ip->ip_off = 0;
ip->ip_p = IPPROTO_UDP; /* XXX: bogus */
#ifdef HAVE_IP_HDRINCL_BSD_ORDER
ip->ip_len = sizeof(struct ip);
#else
ip->ip_len = htons(sizeof(struct ip));
#endif
ip->ip_ttl = MINTTL; /* TODO: XXX: check whether need to setup the ttl */
ip->ip_src.s_addr = mrtentry->source->address;
ip->ip_dst.s_addr = mrtentry->group->group;
ip->ip_sum = 0;
ip->ip_sum = inet_cksum((uint16_t *)ip, sizeof(struct ip));
/* include the dummy ip header */
pktlen = sizeof(pim_register_t) + sizeof(struct ip);
reg_mtu = uvifs[vifi].uv_mtu;
reg_dst = mrtentry->group->rpaddr;
reg_src = uvifs[vifi].uv_lcl_addr;
send_pim_unicast(pim_send_buf, reg_mtu, reg_src, reg_dst, PIM_REGISTER, pktlen);
return TRUE;
}
/************************************************************************
* PIM_REGISTER_STOP
************************************************************************/
int receive_pim_register_stop(uint32_t reg_src, uint32_t reg_dst, char *pim_message, size_t len)
{
pim_encod_grp_addr_t egaddr;
pim_encod_uni_addr_t eusaddr;
char *data;
mrtentry_t *mrtentry;
vifbitmap_t pruned_oifs;
/* Checksum */
if (inet_cksum((uint16_t *)pim_message, len))
return FALSE;
data = pim_message + sizeof(pim_header_t);
GET_EGADDR(&egaddr, data);
GET_EUADDR(&eusaddr, data);
IF_DEBUG(DEBUG_PIM_REGISTER) {
logit(LOG_DEBUG, 0, "Received PIM_REGISTER_STOP from RP %s to %s for src = %s and group = %s", inet_fmt(reg_src, s1, sizeof(s1)), inet_fmt(reg_dst, s2, sizeof(s2)),
inet_fmt(eusaddr.unicast_addr, s3, sizeof(s3)),
inet_fmt(egaddr.mcast_addr, s4, sizeof(s4)));
}
/* TODO: apply the group mask and do register_stop for all grp addresses */
/* TODO: check for SourceAddress == 0 */
mrtentry = find_route(eusaddr.unicast_addr, egaddr.mcast_addr, MRTF_SG, DONT_CREATE);
if (!mrtentry)
return FALSE;
/* XXX: not in the spec: check if the PIM_REGISTER_STOP originator is
* really the RP
*/
if (check_mrtentry_rp(mrtentry, reg_src) == FALSE)
return FALSE;
/* restart the Register-Suppression timer */
SET_TIMER(mrtentry->rs_timer, (0.5 * PIM_REGISTER_SUPPRESSION_TIMEOUT)
+ (RANDOM() % (PIM_REGISTER_SUPPRESSION_TIMEOUT + 1)));
/* Prune the register_vif from the outgoing list */
VIFM_COPY(mrtentry->pruned_oifs, pruned_oifs);
VIFM_SET(reg_vif_num, pruned_oifs);
change_interfaces(mrtentry, mrtentry->incoming,
mrtentry->joined_oifs, pruned_oifs,
mrtentry->leaves,
mrtentry->asserted_oifs, 0);
return TRUE;
}
/* TODO: optional rate limiting is not implemented yet */
/* Unicasts a REGISTER_STOP message to the DR */
static int
send_pim_register_stop(uint32_t reg_src, uint32_t reg_dst, uint32_t inner_grp, uint32_t inner_src)
{
char *buf;
uint8_t *data;
buf = pim_send_buf + sizeof(struct ip) + sizeof(pim_header_t);
data = (uint8_t *)buf;
PUT_EGADDR(inner_grp, SINGLE_GRP_MSKLEN, 0, data);
PUT_EUADDR(inner_src, data);
send_pim_unicast(pim_send_buf, 0, reg_src, reg_dst, PIM_REGISTER_STOP, data - (uint8_t *)buf);
return TRUE;
}
/************************************************************************
* PIM_JOIN_PRUNE
************************************************************************/
int join_or_prune(mrtentry_t *mrtentry, pim_nbr_entry_t *upstream_router)
{
vifbitmap_t entry_oifs;
mrtentry_t *mrtentry_grp;
if (!mrtentry || !upstream_router)
return PIM_ACTION_NOTHING;
calc_oifs(mrtentry, &entry_oifs);
if (mrtentry->flags & (MRTF_PMBR | MRTF_WC)) {
/* (*,*,RP) or (*,G) entry */
/* The (*,*,RP) or (*,G) J/P messages are sent only toward the RP */
if (upstream_router != mrtentry->upstream)
return PIM_ACTION_NOTHING;
/* TODO: XXX: Can we have (*,*,RP) prune? */
if (VIFM_ISEMPTY(entry_oifs)) {
/* NULL oifs */
if (!(uvifs[mrtentry->incoming].uv_flags & VIFF_DR))
/* I am not the DR for that subnet. */
return PIM_ACTION_PRUNE;
if (VIFM_ISSET(mrtentry->incoming, mrtentry->leaves))
/* I am the DR and have local leaves */
return PIM_ACTION_JOIN;
/* Probably the last local member hast timeout */
return PIM_ACTION_PRUNE;
}
return PIM_ACTION_JOIN;
}
if (mrtentry->flags & MRTF_SG) {
/* (S,G) entry */
/* TODO: check again */
if (mrtentry->upstream == upstream_router) {
if (!(mrtentry->flags & MRTF_RP)) {
/* Upstream router toward S */
if (VIFM_ISEMPTY(entry_oifs)) {
if (mrtentry->group->active_rp_grp &&
mrtentry->group->rpaddr == my_cand_rp_address) {
/* (S,G) at the RP. Don't send Join/Prune
* (see the end of Section 3.3.2)
*/
return PIM_ACTION_NOTHING;
}
return PIM_ACTION_PRUNE;
}
else {
return PIM_ACTION_JOIN;
}
}
else {
/* Upstream router toward RP */
if (VIFM_ISEMPTY(entry_oifs))
return PIM_ACTION_PRUNE;
}
}
/* Looks like the case when the upstream router toward S is
* different from the upstream router toward RP
*/
if (!mrtentry->group->active_rp_grp)
return PIM_ACTION_NOTHING;
mrtentry_grp = mrtentry->group->grp_route;
if (!mrtentry_grp) {
mrtentry_grp = mrtentry->group->active_rp_grp->rp->rpentry->mrtlink;
if (!mrtentry_grp)
return PIM_ACTION_NOTHING;
}
if (mrtentry_grp->upstream != upstream_router)
return PIM_ACTION_NOTHING; /* XXX: shoudn't happen */
if (!(mrtentry->flags & MRTF_RP) && (mrtentry->flags & MRTF_SPT))
return PIM_ACTION_PRUNE;
}
return PIM_ACTION_NOTHING;
}
/* TODO: when parsing, check if we go beyong message size */
/* TODO: too long, simplify it! */
#define PIM_JOIN_PRUNE_MINLEN (4 + PIM_ENCODE_UNI_ADDR_LEN + 4)
int receive_pim_join_prune(uint32_t src, uint32_t dst __attribute__((unused)), char *pim_message, size_t len)
{
vifi_t vifi;
struct uvif *v;
pim_encod_uni_addr_t eutaddr;