3 * Copyright (C) 2008 Everton da Silva Marques
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #include "pim_iface.h"
36 #include "pim_hello.h"
37 #include "pim_ifchannel.h"
40 #include "pim_jp_agg.h"
43 static void on_trace(const char *label
, struct interface
*ifp
,
46 if (PIM_DEBUG_PIM_TRACE
) {
47 char src_str
[INET_ADDRSTRLEN
];
48 pim_inet4_dump("<src?>", src
, src_str
, sizeof(src_str
));
49 zlog_debug("%s: from %s on %s", label
, src_str
, ifp
->name
);
53 static void recv_join(struct interface
*ifp
, struct pim_neighbor
*neigh
,
54 uint16_t holdtime
, struct in_addr upstream
,
55 struct prefix_sg
*sg
, uint8_t source_flags
)
57 struct pim_interface
*pim_ifp
= NULL
;
59 if (PIM_DEBUG_PIM_TRACE
) {
60 char up_str
[INET_ADDRSTRLEN
];
61 char neigh_str
[INET_ADDRSTRLEN
];
62 pim_inet4_dump("<upstream?>", upstream
, up_str
, sizeof(up_str
));
63 pim_inet4_dump("<neigh?>", neigh
->source_addr
, neigh_str
,
66 "%s: join (S,G)=%s rpt=%d wc=%d upstream=%s holdtime=%d from %s on %s",
67 __PRETTY_FUNCTION__
, pim_str_sg_dump(sg
),
68 !!(source_flags
& PIM_RPT_BIT_MASK
),
69 !!(source_flags
& PIM_WILDCARD_BIT_MASK
), up_str
,
70 holdtime
, neigh_str
, ifp
->name
);
76 ++pim_ifp
->pim_ifstat_join_recv
;
79 * If the RPT and WC are set it's a (*,G)
80 * and the source is the RP
82 if ((source_flags
& PIM_RPT_BIT_MASK
)
83 && (source_flags
& PIM_WILDCARD_BIT_MASK
)) {
84 struct pim_rpf
*rp
= RP(pim_ifp
->pim
, sg
->grp
);
87 zlog_warn("%s: Lookup of RP failed for %pSG4",
88 __PRETTY_FUNCTION__
, sg
);
92 * If the RP sent in the message is not
93 * our RP for the group, drop the message
95 if (sg
->src
.s_addr
!= rp
->rpf_addr
.u
.prefix4
.s_addr
) {
96 char received_rp
[INET_ADDRSTRLEN
];
97 char local_rp
[INET_ADDRSTRLEN
];
98 pim_inet4_dump("<received?>", sg
->src
, received_rp
,
100 pim_inet4_dump("<local?>", rp
->rpf_addr
.u
.prefix4
,
101 local_rp
, sizeof(local_rp
));
102 if (PIM_DEBUG_PIM_TRACE
)
104 "%s: Specified RP(%s) in join is different than our configured RP(%s)",
105 __PRETTY_FUNCTION__
, received_rp
,
110 sg
->src
.s_addr
= INADDR_ANY
;
113 /* Restart join expiry timer */
114 pim_ifchannel_join_add(ifp
, neigh
->source_addr
, upstream
, sg
,
115 source_flags
, holdtime
);
118 static void recv_prune(struct interface
*ifp
, struct pim_neighbor
*neigh
,
119 uint16_t holdtime
, struct in_addr upstream
,
120 struct prefix_sg
*sg
, uint8_t source_flags
)
122 struct pim_interface
*pim_ifp
= NULL
;
124 if (PIM_DEBUG_PIM_TRACE
) {
125 char up_str
[INET_ADDRSTRLEN
];
126 char neigh_str
[INET_ADDRSTRLEN
];
127 pim_inet4_dump("<upstream?>", upstream
, up_str
, sizeof(up_str
));
128 pim_inet4_dump("<neigh?>", neigh
->source_addr
, neigh_str
,
131 "%s: prune (S,G)=%s rpt=%d wc=%d upstream=%s holdtime=%d from %s on %s",
132 __PRETTY_FUNCTION__
, pim_str_sg_dump(sg
),
133 source_flags
& PIM_RPT_BIT_MASK
,
134 source_flags
& PIM_WILDCARD_BIT_MASK
, up_str
, holdtime
,
135 neigh_str
, ifp
->name
);
141 ++pim_ifp
->pim_ifstat_prune_recv
;
143 if ((source_flags
& PIM_RPT_BIT_MASK
)
144 && (source_flags
& PIM_WILDCARD_BIT_MASK
)) {
145 struct pim_rpf
*rp
= RP(pim_ifp
->pim
, sg
->grp
);
148 if (PIM_DEBUG_PIM_TRACE
)
149 zlog_debug("%s: RP for %pSG4 completely failed lookup",
150 __PRETTY_FUNCTION__
, sg
);
153 // Ignoring Prune *,G's at the moment.
154 if (sg
->src
.s_addr
!= rp
->rpf_addr
.u
.prefix4
.s_addr
)
157 sg
->src
.s_addr
= INADDR_ANY
;
160 pim_ifchannel_prune(ifp
, upstream
, sg
, source_flags
, holdtime
);
163 int pim_joinprune_recv(struct interface
*ifp
, struct pim_neighbor
*neigh
,
164 struct in_addr src_addr
, uint8_t *tlv_buf
,
167 struct prefix msg_upstream_addr
;
168 struct pim_interface
*pim_ifp
;
169 uint8_t msg_num_groups
;
170 uint16_t msg_holdtime
;
178 pastend
= tlv_buf
+ tlv_buf_size
;
185 pim_parse_addr_ucast(&msg_upstream_addr
, buf
, pastend
- buf
);
186 if (addr_offset
< 1) {
187 char src_str
[INET_ADDRSTRLEN
];
188 pim_inet4_dump("<src?>", src_addr
, src_str
, sizeof(src_str
));
189 zlog_warn("%s: pim_parse_addr_ucast() failure: from %s on %s",
190 __PRETTY_FUNCTION__
, src_str
, ifp
->name
);
196 Check upstream address family
198 if (msg_upstream_addr
.family
!= AF_INET
) {
199 if (PIM_DEBUG_PIM_J_P
) {
200 char src_str
[INET_ADDRSTRLEN
];
201 pim_inet4_dump("<src?>", src_addr
, src_str
,
204 "%s: ignoring join/prune directed to unexpected addr family=%d from %s on %s",
205 __PRETTY_FUNCTION__
, msg_upstream_addr
.family
,
211 remain
= pastend
- buf
;
213 char src_str
[INET_ADDRSTRLEN
];
214 pim_inet4_dump("<src?>", src_addr
, src_str
, sizeof(src_str
));
216 "%s: short join/prune message buffer for group list: size=%d minimum=%d from %s on %s",
217 __PRETTY_FUNCTION__
, remain
, 4, src_str
, ifp
->name
);
221 ++buf
; /* skip reserved byte */
222 msg_num_groups
= *(const uint8_t *)buf
;
224 msg_holdtime
= ntohs(*(const uint16_t *)buf
);
228 if (PIM_DEBUG_PIM_J_P
) {
229 char src_str
[INET_ADDRSTRLEN
];
230 char upstream_str
[INET_ADDRSTRLEN
];
231 pim_inet4_dump("<src?>", src_addr
, src_str
, sizeof(src_str
));
232 pim_inet4_dump("<addr?>", msg_upstream_addr
.u
.prefix4
,
233 upstream_str
, sizeof(upstream_str
));
235 "%s: join/prune upstream=%s groups=%d holdtime=%d from %s on %s",
236 __PRETTY_FUNCTION__
, upstream_str
, msg_num_groups
,
237 msg_holdtime
, src_str
, ifp
->name
);
241 for (group
= 0; group
< msg_num_groups
; ++group
) {
243 uint8_t msg_source_flags
;
244 uint16_t msg_num_joined_sources
;
245 uint16_t msg_num_pruned_sources
;
247 struct pim_ifchannel
*starg_ch
= NULL
, *sg_ch
= NULL
;
248 bool filtered
= false;
250 memset(&sg
, 0, sizeof(struct prefix_sg
));
251 addr_offset
= pim_parse_addr_group(&sg
, buf
, pastend
- buf
);
252 if (addr_offset
< 1) {
257 remain
= pastend
- buf
;
259 char src_str
[INET_ADDRSTRLEN
];
260 pim_inet4_dump("<src?>", src_addr
, src_str
,
263 "%s: short join/prune buffer for source list: size=%d minimum=%d from %s on %s",
264 __PRETTY_FUNCTION__
, remain
, 4, src_str
,
269 msg_num_joined_sources
= ntohs(*(const uint16_t *)buf
);
271 msg_num_pruned_sources
= ntohs(*(const uint16_t *)buf
);
274 if (PIM_DEBUG_PIM_J_P
) {
275 char src_str
[INET_ADDRSTRLEN
];
276 char upstream_str
[INET_ADDRSTRLEN
];
277 char group_str
[INET_ADDRSTRLEN
];
278 pim_inet4_dump("<src?>", src_addr
, src_str
,
280 pim_inet4_dump("<addr?>", msg_upstream_addr
.u
.prefix4
,
281 upstream_str
, sizeof(upstream_str
));
282 pim_inet4_dump("<grp?>", sg
.grp
, group_str
,
285 "%s: join/prune upstream=%s group=%s/32 join_src=%d prune_src=%d from %s on %s",
286 __PRETTY_FUNCTION__
, upstream_str
, group_str
,
287 msg_num_joined_sources
, msg_num_pruned_sources
,
292 filtered
= pim_is_group_filtered(pim_ifp
, &sg
.grp
);
294 /* Scan joined sources */
295 for (source
= 0; source
< msg_num_joined_sources
; ++source
) {
296 addr_offset
= pim_parse_addr_source(
297 &sg
, &msg_source_flags
, buf
, pastend
- buf
);
298 if (addr_offset
< 1) {
304 /* if we are filtering this group, skip the join */
308 recv_join(ifp
, neigh
, msg_holdtime
,
309 msg_upstream_addr
.u
.prefix4
, &sg
,
312 if (sg
.src
.s_addr
== INADDR_ANY
) {
313 starg_ch
= pim_ifchannel_find(ifp
, &sg
);
315 pim_ifchannel_set_star_g_join_state(
320 /* Scan pruned sources */
321 for (source
= 0; source
< msg_num_pruned_sources
; ++source
) {
322 addr_offset
= pim_parse_addr_source(
323 &sg
, &msg_source_flags
, buf
, pastend
- buf
);
324 if (addr_offset
< 1) {
330 /* if we are filtering this group, skip the prune */
334 recv_prune(ifp
, neigh
, msg_holdtime
,
335 msg_upstream_addr
.u
.prefix4
, &sg
,
339 * So if we are receiving a S,G,RPT prune
340 * before we have any data for that S,G
341 * We need to retrieve the sg_ch after
342 * we parse the prune.
344 sg_ch
= pim_ifchannel_find(ifp
, &sg
);
346 /* Received SG-RPT Prune delete oif from specific S,G */
347 if (starg_ch
&& sg_ch
348 && (msg_source_flags
& PIM_RPT_BIT_MASK
)
349 && !(msg_source_flags
& PIM_WILDCARD_BIT_MASK
)) {
350 struct pim_upstream
*up
= sg_ch
->upstream
;
351 PIM_IF_FLAG_SET_S_G_RPT(sg_ch
->flags
);
355 "%s: SGRpt flag is set, del inherit oif from up %s",
361 PIM_OIF_FLAG_PROTO_STAR
);
365 if (starg_ch
&& !filtered
)
366 pim_ifchannel_set_star_g_join_state(starg_ch
, 1, 0);
376 * While the RFC clearly states that this is 32 bits wide, it
377 * is cheating. These fields:
378 * Encoded-Unicast format (6 bytes MIN)
379 * Encoded-Group format (8 bytes MIN)
380 * Encoded-Source format (8 bytes MIN)
381 * are *not* 32 bits wide.
383 * Nor does the RFC explicitly call out the size for:
385 * Num Groups (1 byte)
387 * Number of Joined Sources (2 bytes)
388 * Number of Pruned Sources (2 bytes)
390 * This leads to a missleading representation from casual
391 * reading and making assumptions. Be careful!
394 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
395 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
396 * |PIM Ver| Type | Reserved | Checksum |
397 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
398 * | Upstream Neighbor Address (Encoded-Unicast format) |
399 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
400 * | Reserved | Num groups | Holdtime |
401 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
402 * | Multicast Group Address 1 (Encoded-Group format) |
403 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
404 * | Number of Joined Sources | Number of Pruned Sources |
405 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
406 * | Joined Source Address 1 (Encoded-Source format) |
407 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
410 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
411 * | Joined Source Address n (Encoded-Source format) |
412 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
413 * | Pruned Source Address 1 (Encoded-Source format) |
414 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
417 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
418 * | Pruned Source Address n (Encoded-Source format) |
419 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
420 * | Multicast Group Address m (Encoded-Group format) |
421 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
422 * | Number of Joined Sources | Number of Pruned Sources |
423 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
424 * | Joined Source Address 1 (Encoded-Source format) |
425 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
428 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
429 * | Joined Source Address n (Encoded-Source format) |
430 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
431 * | Pruned Source Address 1 (Encoded-Source format) |
432 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
435 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
436 * | Pruned Source Address n (Encoded-Source format) |
437 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
439 int pim_joinprune_send(struct pim_rpf
*rpf
, struct list
*groups
)
441 struct pim_jp_agg_group
*group
;
442 struct pim_interface
*pim_ifp
= NULL
;
443 struct pim_jp_groups
*grp
= NULL
;
444 struct pim_jp
*msg
= NULL
;
445 struct listnode
*node
, *nnode
;
446 uint8_t pim_msg
[10000];
447 uint8_t *curr_ptr
= pim_msg
;
448 bool new_packet
= true;
449 size_t packet_left
= 0;
450 size_t packet_size
= 0;
451 size_t group_size
= 0;
453 if (rpf
->source_nexthop
.interface
)
454 pim_ifp
= rpf
->source_nexthop
.interface
->info
;
456 zlog_warn("%s: RPF interface is not present",
457 __PRETTY_FUNCTION__
);
461 on_trace(__PRETTY_FUNCTION__
, rpf
->source_nexthop
.interface
,
462 rpf
->rpf_addr
.u
.prefix4
);
465 zlog_warn("%s: multicast not enabled on interface %s",
467 rpf
->source_nexthop
.interface
->name
);
471 if (PIM_INADDR_IS_ANY(rpf
->rpf_addr
.u
.prefix4
)) {
472 if (PIM_DEBUG_PIM_J_P
) {
473 char dst_str
[INET_ADDRSTRLEN
];
474 pim_inet4_dump("<dst?>", rpf
->rpf_addr
.u
.prefix4
,
475 dst_str
, sizeof(dst_str
));
476 zlog_debug("%s: upstream=%s is myself on interface %s",
477 __PRETTY_FUNCTION__
, dst_str
,
478 rpf
->source_nexthop
.interface
->name
);
484 RFC 4601: 4.3.1. Sending Hello Messages
486 Thus, if a router needs to send a Join/Prune or Assert message on
487 an interface on which it has not yet sent a Hello message with the
488 currently configured IP address, then it MUST immediately send the
489 relevant Hello message without waiting for the Hello Timer to
490 expire, followed by the Join/Prune or Assert message.
492 pim_hello_require(rpf
->source_nexthop
.interface
);
494 for (ALL_LIST_ELEMENTS(groups
, node
, nnode
, group
)) {
496 msg
= (struct pim_jp
*)pim_msg
;
498 memset(msg
, 0, sizeof(*msg
));
500 pim_msg_addr_encode_ipv4_ucast((uint8_t *)&msg
->addr
,
501 rpf
->rpf_addr
.u
.prefix4
);
503 msg
->holdtime
= htons(PIM_JP_HOLDTIME
);
507 grp
= &msg
->groups
[0];
508 curr_ptr
= (uint8_t *)grp
;
509 packet_size
= sizeof(struct pim_msg_header
);
510 packet_size
+= sizeof(struct pim_encoded_ipv4_unicast
);
512 4; // reserved (1) + groups (1) + holdtime (2)
514 packet_left
= rpf
->source_nexthop
.interface
->mtu
- 24;
515 packet_left
-= packet_size
;
517 if (PIM_DEBUG_PIM_J_P
) {
518 char dst_str
[INET_ADDRSTRLEN
];
519 char grp_str
[INET_ADDRSTRLEN
];
520 pim_inet4_dump("<dst?>", rpf
->rpf_addr
.u
.prefix4
,
521 dst_str
, sizeof(dst_str
));
522 pim_inet4_dump("<grp?>", group
->group
, grp_str
,
525 "%s: sending (G)=%s to upstream=%s on interface %s",
526 __PRETTY_FUNCTION__
, grp_str
, dst_str
,
527 rpf
->source_nexthop
.interface
->name
);
530 group_size
= pim_msg_get_jp_group_size(group
->sources
);
531 if (group_size
> packet_left
) {
532 pim_msg_build_header(pim_msg
, packet_size
,
533 PIM_MSG_TYPE_JOIN_PRUNE
);
534 if (pim_msg_send(pim_ifp
->pim_sock_fd
,
535 pim_ifp
->primary_address
,
536 qpim_all_pim_routers_addr
, pim_msg
,
538 rpf
->source_nexthop
.interface
->name
)) {
540 "%s: could not send PIM message on interface %s",
542 rpf
->source_nexthop
.interface
->name
);
545 msg
= (struct pim_jp
*)pim_msg
;
546 memset(msg
, 0, sizeof(*msg
));
548 pim_msg_addr_encode_ipv4_ucast((uint8_t *)&msg
->addr
,
549 rpf
->rpf_addr
.u
.prefix4
);
551 msg
->holdtime
= htons(PIM_JP_HOLDTIME
);
555 grp
= &msg
->groups
[0];
556 curr_ptr
= (uint8_t *)grp
;
557 packet_size
= sizeof(struct pim_msg_header
);
558 packet_size
+= sizeof(struct pim_encoded_ipv4_unicast
);
560 4; // reserved (1) + groups (1) + holdtime (2)
562 packet_left
= rpf
->source_nexthop
.interface
->mtu
- 24;
563 packet_left
-= packet_size
;
571 curr_ptr
+= group_size
;
572 packet_left
-= group_size
;
573 packet_size
+= group_size
;
574 pim_msg_build_jp_groups(grp
, group
, group_size
);
576 pim_ifp
->pim_ifstat_join_send
+= ntohs(grp
->joins
);
577 pim_ifp
->pim_ifstat_prune_send
+= ntohs(grp
->prunes
);
579 if (PIM_DEBUG_PIM_TRACE
)
581 "%s: interface %s num_joins %u num_prunes %u",
583 rpf
->source_nexthop
.interface
->name
,
584 ntohs(grp
->joins
), ntohs(grp
->prunes
));
586 grp
= (struct pim_jp_groups
*)curr_ptr
;
587 if (packet_left
< sizeof(struct pim_jp_groups
)
588 || msg
->num_groups
== 255) {
589 pim_msg_build_header(pim_msg
, packet_size
,
590 PIM_MSG_TYPE_JOIN_PRUNE
);
591 if (pim_msg_send(pim_ifp
->pim_sock_fd
,
592 pim_ifp
->primary_address
,
593 qpim_all_pim_routers_addr
, pim_msg
,
595 rpf
->source_nexthop
.interface
->name
)) {
597 "%s: could not send PIM message on interface %s",
599 rpf
->source_nexthop
.interface
->name
);
608 // msg->num_groups = htons (msg->num_groups);
609 pim_msg_build_header(pim_msg
, packet_size
,
610 PIM_MSG_TYPE_JOIN_PRUNE
);
611 if (pim_msg_send(pim_ifp
->pim_sock_fd
, pim_ifp
->primary_address
,
612 qpim_all_pim_routers_addr
, pim_msg
,
614 rpf
->source_nexthop
.interface
->name
)) {
616 "%s: could not send PIM message on interface %s",
618 rpf
->source_nexthop
.interface
->name
);