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
33 #include "pim_iface.h"
34 #include "pim_hello.h"
35 #include "pim_macro.h"
36 #include "pim_assert.h"
37 #include "pim_ifchannel.h"
39 static int assert_action_a3(struct pim_ifchannel
*ch
);
40 static void assert_action_a2(struct pim_ifchannel
*ch
,
41 struct pim_assert_metric winner_metric
);
42 static void assert_action_a6(struct pim_ifchannel
*ch
,
43 struct pim_assert_metric winner_metric
);
45 void pim_ifassert_winner_set(struct pim_ifchannel
*ch
,
46 enum pim_ifassert_state new_state
, pim_addr winner
,
47 struct pim_assert_metric winner_metric
)
49 struct pim_interface
*pim_ifp
= ch
->interface
->info
;
50 int winner_changed
= !!pim_addr_cmp(ch
->ifassert_winner
, winner
);
51 int metric_changed
= !pim_assert_metric_match(
52 &ch
->ifassert_winner_metric
, &winner_metric
);
54 if (PIM_DEBUG_PIM_EVENTS
) {
55 if (ch
->ifassert_state
!= new_state
) {
57 "%s: (S,G)=%s assert state changed from %s to %s on interface %s",
59 pim_ifchannel_ifassert_name(ch
->ifassert_state
),
60 pim_ifchannel_ifassert_name(new_state
),
65 char was_str
[INET_ADDRSTRLEN
];
66 char winner_str
[INET_ADDRSTRLEN
];
67 pim_inet4_dump("<was?>", ch
->ifassert_winner
, was_str
,
69 pim_inet4_dump("<winner?>", winner
, winner_str
,
72 "%s: (S,G)=%s assert winner changed from %s to %s on interface %s",
73 __func__
, ch
->sg_str
, was_str
, winner_str
,
76 } /* PIM_DEBUG_PIM_EVENTS */
78 ch
->ifassert_state
= new_state
;
79 ch
->ifassert_winner
= winner
;
80 ch
->ifassert_winner_metric
= winner_metric
;
81 ch
->ifassert_creation
= pim_time_monotonic_sec();
83 if (winner_changed
|| metric_changed
) {
84 pim_upstream_update_join_desired(pim_ifp
->pim
, ch
->upstream
);
85 pim_ifchannel_update_could_assert(ch
);
86 pim_ifchannel_update_assert_tracking_desired(ch
);
90 static void on_trace(const char *label
, struct interface
*ifp
,
93 if (PIM_DEBUG_PIM_TRACE
) {
94 char src_str
[INET_ADDRSTRLEN
];
95 pim_inet4_dump("<src?>", src
, src_str
, sizeof(src_str
));
96 zlog_debug("%s: from %s on %s", label
, src_str
, ifp
->name
);
100 static int preferred_assert(const struct pim_ifchannel
*ch
,
101 const struct pim_assert_metric
*recv_metric
)
103 return pim_assert_metric_better(recv_metric
,
104 &ch
->ifassert_winner_metric
);
107 static int acceptable_assert(const struct pim_assert_metric
*my_metric
,
108 const struct pim_assert_metric
*recv_metric
)
110 return pim_assert_metric_better(recv_metric
, my_metric
);
113 static int inferior_assert(const struct pim_assert_metric
*my_metric
,
114 const struct pim_assert_metric
*recv_metric
)
116 return pim_assert_metric_better(my_metric
, recv_metric
);
119 static int cancel_assert(const struct pim_assert_metric
*recv_metric
)
121 return (recv_metric
->metric_preference
122 == PIM_ASSERT_METRIC_PREFERENCE_MAX
)
123 && (recv_metric
->route_metric
== PIM_ASSERT_ROUTE_METRIC_MAX
);
126 static void if_could_assert_do_a1(const char *caller
, struct pim_ifchannel
*ch
)
128 if (PIM_IF_FLAG_TEST_COULD_ASSERT(ch
->flags
)) {
129 if (assert_action_a1(ch
)) {
131 "%s: %s: (S,G)=%s assert_action_a1 failure on interface %s",
132 __func__
, caller
, ch
->sg_str
,
133 ch
->interface
->name
);
134 /* log warning only */
139 static int dispatch_assert(struct interface
*ifp
, struct in_addr source_addr
,
140 struct in_addr group_addr
,
141 struct pim_assert_metric recv_metric
)
143 struct pim_ifchannel
*ch
;
146 memset(&sg
, 0, sizeof(sg
));
147 sg
.src
= source_addr
;
149 ch
= pim_ifchannel_add(ifp
, &sg
, 0, 0);
151 switch (ch
->ifassert_state
) {
152 case PIM_IFASSERT_NOINFO
:
153 if (recv_metric
.rpt_bit_flag
) {
155 if_could_assert_do_a1(__func__
, ch
);
158 if (inferior_assert(&ch
->ifassert_my_metric
,
160 if_could_assert_do_a1(__func__
, ch
);
161 } else if (acceptable_assert(&ch
->ifassert_my_metric
,
163 if (PIM_IF_FLAG_TEST_ASSERT_TRACKING_DESIRED(
165 assert_action_a6(ch
, recv_metric
);
170 case PIM_IFASSERT_I_AM_WINNER
:
171 if (preferred_assert(ch
, &recv_metric
)) {
172 assert_action_a2(ch
, recv_metric
);
174 if (inferior_assert(&ch
->ifassert_my_metric
,
176 assert_action_a3(ch
);
180 case PIM_IFASSERT_I_AM_LOSER
:
181 if (!pim_addr_cmp(recv_metric
.ip_address
,
182 ch
->ifassert_winner
)) {
183 /* Assert from current winner */
185 if (cancel_assert(&recv_metric
)) {
186 assert_action_a5(ch
);
188 if (inferior_assert(&ch
->ifassert_my_metric
,
190 assert_action_a5(ch
);
191 } else if (acceptable_assert(
192 &ch
->ifassert_my_metric
,
194 if (!recv_metric
.rpt_bit_flag
) {
200 } else if (preferred_assert(ch
, &recv_metric
)) {
201 assert_action_a2(ch
, recv_metric
);
206 "%s: (S,G)=%s invalid assert state %d on interface %s",
207 __func__
, ch
->sg_str
, ch
->ifassert_state
, ifp
->name
);
215 int pim_assert_recv(struct interface
*ifp
, struct pim_neighbor
*neigh
,
216 struct in_addr src_addr
, uint8_t *buf
, int buf_size
)
219 struct prefix msg_source_addr
;
220 struct pim_assert_metric msg_metric
;
224 struct pim_interface
*pim_ifp
= NULL
;
226 on_trace(__func__
, ifp
, src_addr
);
229 curr_size
= buf_size
;
232 Parse assert group addr
234 memset(&sg
, 0, sizeof(sg
));
235 offset
= pim_parse_addr_group(&sg
, curr
, curr_size
);
237 char src_str
[INET_ADDRSTRLEN
];
238 pim_inet4_dump("<src?>", src_addr
, src_str
, sizeof(src_str
));
239 zlog_warn("%s: pim_parse_addr_group() failure: from %s on %s",
240 __func__
, src_str
, ifp
->name
);
247 Parse assert source addr
249 offset
= pim_parse_addr_ucast(&msg_source_addr
, curr
, curr_size
);
251 char src_str
[INET_ADDRSTRLEN
];
252 pim_inet4_dump("<src?>", src_addr
, src_str
, sizeof(src_str
));
253 zlog_warn("%s: pim_parse_addr_ucast() failure: from %s on %s",
254 __func__
, src_str
, ifp
->name
);
261 char src_str
[INET_ADDRSTRLEN
];
262 pim_inet4_dump("<src?>", src_addr
, src_str
, sizeof(src_str
));
264 "%s: preference/metric size is less than 8 bytes: size=%d from %s on interface %s",
265 __func__
, curr_size
, src_str
, ifp
->name
);
270 Parse assert metric preference
273 msg_metric
.metric_preference
= pim_read_uint32_host(curr
);
275 msg_metric
.rpt_bit_flag
= msg_metric
.metric_preference
276 & 0x80000000; /* save highest bit */
277 msg_metric
.metric_preference
&= ~0x80000000; /* clear highest bit */
282 Parse assert route metric
285 msg_metric
.route_metric
= pim_read_uint32_host(curr
);
287 if (PIM_DEBUG_PIM_TRACE
) {
288 char neigh_str
[INET_ADDRSTRLEN
];
289 char source_str
[INET_ADDRSTRLEN
];
290 pim_inet4_dump("<neigh?>", src_addr
, neigh_str
,
292 pim_inet4_dump("<src?>", msg_source_addr
.u
.prefix4
, source_str
,
295 "%s: from %s on %s: (S,G)=(%s,%pPAs) pref=%u metric=%u rpt_bit=%u",
296 __func__
, neigh_str
, ifp
->name
, source_str
, &sg
.grp
,
297 msg_metric
.metric_preference
, msg_metric
.route_metric
,
298 PIM_FORCE_BOOLEAN(msg_metric
.rpt_bit_flag
));
301 msg_metric
.ip_address
= src_addr
;
305 ++pim_ifp
->pim_ifstat_assert_recv
;
307 return dispatch_assert(ifp
, msg_source_addr
.u
.prefix4
, sg
.grp
,
312 RFC 4601: 4.6.3. Assert Metrics
314 Assert metrics are defined as:
316 When comparing assert_metrics, the rpt_bit_flag, metric_preference,
317 and route_metric field are compared in order, where the first lower
318 value wins. If all fields are equal, the primary IP address of the
319 router that sourced the Assert message is used as a tie-breaker,
320 with the highest IP address winning.
322 int pim_assert_metric_better(const struct pim_assert_metric
*m1
,
323 const struct pim_assert_metric
*m2
)
325 if (m1
->rpt_bit_flag
< m2
->rpt_bit_flag
)
327 if (m1
->rpt_bit_flag
> m2
->rpt_bit_flag
)
330 if (m1
->metric_preference
< m2
->metric_preference
)
332 if (m1
->metric_preference
> m2
->metric_preference
)
335 if (m1
->route_metric
< m2
->route_metric
)
337 if (m1
->route_metric
> m2
->route_metric
)
340 return pim_addr_cmp(m1
->ip_address
, m2
->ip_address
) > 0;
343 int pim_assert_metric_match(const struct pim_assert_metric
*m1
,
344 const struct pim_assert_metric
*m2
)
346 if (m1
->rpt_bit_flag
!= m2
->rpt_bit_flag
)
348 if (m1
->metric_preference
!= m2
->metric_preference
)
350 if (m1
->route_metric
!= m2
->route_metric
)
353 return !pim_addr_cmp(m1
->ip_address
, m2
->ip_address
);
356 int pim_assert_build_msg(uint8_t *pim_msg
, int buf_size
, struct interface
*ifp
,
357 struct in_addr group_addr
, struct in_addr source_addr
,
358 uint32_t metric_preference
, uint32_t route_metric
,
359 uint32_t rpt_bit_flag
)
361 uint8_t *buf_pastend
= pim_msg
+ buf_size
;
362 uint8_t *pim_msg_curr
;
367 pim_msg
+ PIM_MSG_HEADER_LEN
; /* skip room for pim header */
370 remain
= buf_pastend
- pim_msg_curr
;
371 pim_msg_curr
= pim_msg_addr_encode_ipv4_group(pim_msg_curr
, group_addr
);
373 char group_str
[INET_ADDRSTRLEN
];
374 pim_inet4_dump("<grp?>", group_addr
, group_str
,
377 "%s: failure encoding group address %s: space left=%d",
378 __func__
, group_str
, remain
);
383 remain
= buf_pastend
- pim_msg_curr
;
385 pim_msg_addr_encode_ipv4_ucast(pim_msg_curr
, source_addr
);
387 char source_str
[INET_ADDRSTRLEN
];
388 pim_inet4_dump("<src?>", source_addr
, source_str
,
391 "%s: failure encoding source address %s: space left=%d",
392 __func__
, source_str
, remain
);
396 /* Metric preference */
397 pim_write_uint32(pim_msg_curr
,
398 rpt_bit_flag
? metric_preference
| 0x80000000
399 : metric_preference
);
403 pim_write_uint32(pim_msg_curr
, route_metric
);
409 pim_msg_size
= pim_msg_curr
- pim_msg
;
410 pim_msg_build_header(pim_msg
, pim_msg_size
, PIM_MSG_TYPE_ASSERT
, false);
415 static int pim_assert_do(struct pim_ifchannel
*ch
,
416 struct pim_assert_metric metric
)
418 struct interface
*ifp
;
419 struct pim_interface
*pim_ifp
;
420 uint8_t pim_msg
[1000];
425 if (PIM_DEBUG_PIM_TRACE
)
426 zlog_debug("%s: channel%s has no associated interface!",
427 __func__
, ch
->sg_str
);
432 if (PIM_DEBUG_PIM_TRACE
)
434 "%s: channel %s pim not enabled on interface: %s",
435 __func__
, ch
->sg_str
, ifp
->name
);
440 pim_assert_build_msg(pim_msg
, sizeof(pim_msg
), ifp
, ch
->sg
.grp
,
441 ch
->sg
.src
, metric
.metric_preference
,
442 metric
.route_metric
, metric
.rpt_bit_flag
);
443 if (pim_msg_size
< 1) {
445 "%s: failure building PIM assert message: msg_size=%d",
446 __func__
, pim_msg_size
);
451 RFC 4601: 4.3.1. Sending Hello Messages
453 Thus, if a router needs to send a Join/Prune or Assert message on
454 an interface on which it has not yet sent a Hello message with the
455 currently configured IP address, then it MUST immediately send the
456 relevant Hello message without waiting for the Hello Timer to
457 expire, followed by the Join/Prune or Assert message.
459 pim_hello_require(ifp
);
461 if (PIM_DEBUG_PIM_TRACE
) {
462 zlog_debug("%s: to %s: (S,G)=%s pref=%u metric=%u rpt_bit=%u",
463 __func__
, ifp
->name
, ch
->sg_str
,
464 metric
.metric_preference
, metric
.route_metric
,
465 PIM_FORCE_BOOLEAN(metric
.rpt_bit_flag
));
467 ++pim_ifp
->pim_ifstat_assert_send
;
469 if (pim_msg_send(pim_ifp
->pim_sock_fd
, pim_ifp
->primary_address
,
470 qpim_all_pim_routers_addr
, pim_msg
, pim_msg_size
,
472 zlog_warn("%s: could not send PIM message on interface %s",
473 __func__
, ifp
->name
);
480 int pim_assert_send(struct pim_ifchannel
*ch
)
482 return pim_assert_do(ch
, ch
->ifassert_my_metric
);
486 RFC 4601: 4.6.4. AssertCancel Messages
488 An AssertCancel(S,G) is an infinite metric assert with the RPT bit
489 set that names S as the source.
491 static int pim_assert_cancel(struct pim_ifchannel
*ch
)
493 struct pim_assert_metric metric
;
495 metric
.rpt_bit_flag
= 0;
496 metric
.metric_preference
= PIM_ASSERT_METRIC_PREFERENCE_MAX
;
497 metric
.route_metric
= PIM_ASSERT_ROUTE_METRIC_MAX
;
498 metric
.ip_address
= ch
->sg
.src
;
500 return pim_assert_do(ch
, metric
);
503 static int on_assert_timer(struct thread
*t
)
505 struct pim_ifchannel
*ch
;
506 struct interface
*ifp
;
512 if (PIM_DEBUG_PIM_TRACE
) {
513 zlog_debug("%s: (S,G)=%s timer expired on interface %s",
514 __func__
, ch
->sg_str
, ifp
->name
);
517 ch
->t_ifassert_timer
= NULL
;
519 switch (ch
->ifassert_state
) {
520 case PIM_IFASSERT_I_AM_WINNER
:
521 assert_action_a3(ch
);
523 case PIM_IFASSERT_I_AM_LOSER
:
524 assert_action_a5(ch
);
527 if (PIM_DEBUG_PIM_EVENTS
)
529 "%s: (S,G)=%s invalid assert state %d on interface %s",
530 __func__
, ch
->sg_str
, ch
->ifassert_state
,
538 static void assert_timer_off(struct pim_ifchannel
*ch
)
540 if (PIM_DEBUG_PIM_TRACE
) {
541 if (ch
->t_ifassert_timer
) {
543 "%s: (S,G)=%s cancelling timer on interface %s",
544 __func__
, ch
->sg_str
, ch
->interface
->name
);
547 THREAD_OFF(ch
->t_ifassert_timer
);
550 static void pim_assert_timer_set(struct pim_ifchannel
*ch
, int interval
)
552 assert_timer_off(ch
);
554 if (PIM_DEBUG_PIM_TRACE
) {
555 zlog_debug("%s: (S,G)=%s starting %u sec timer on interface %s",
556 __func__
, ch
->sg_str
, interval
, ch
->interface
->name
);
559 thread_add_timer(router
->master
, on_assert_timer
, ch
, interval
,
560 &ch
->t_ifassert_timer
);
563 static void pim_assert_timer_reset(struct pim_ifchannel
*ch
)
565 pim_assert_timer_set(ch
,
566 PIM_ASSERT_TIME
- PIM_ASSERT_OVERRIDE_INTERVAL
);
570 RFC 4601: 4.6.1. (S,G) Assert Message State Machine
572 (S,G) Assert State machine Actions
574 A1: Send Assert(S,G).
575 Set Assert Timer to (Assert_Time - Assert_Override_Interval).
576 Store self as AssertWinner(S,G,I).
577 Store spt_assert_metric(S,I) as AssertWinnerMetric(S,G,I).
579 int assert_action_a1(struct pim_ifchannel
*ch
)
581 struct interface
*ifp
= ch
->interface
;
582 struct pim_interface
*pim_ifp
;
586 zlog_warn("%s: (S,G)=%s multicast not enabled on interface %s",
587 __func__
, ch
->sg_str
, ifp
->name
);
588 return -1; /* must return since pim_ifp is used below */
591 /* Switch to I_AM_WINNER before performing action_a3 below */
592 pim_ifassert_winner_set(
593 ch
, PIM_IFASSERT_I_AM_WINNER
, pim_ifp
->primary_address
,
594 pim_macro_spt_assert_metric(&ch
->upstream
->rpf
,
595 pim_ifp
->primary_address
));
597 if (assert_action_a3(ch
)) {
599 "%s: (S,G)=%s assert_action_a3 failure on interface %s",
600 __func__
, ch
->sg_str
, ifp
->name
);
604 if (ch
->ifassert_state
!= PIM_IFASSERT_I_AM_WINNER
) {
605 if (PIM_DEBUG_PIM_EVENTS
)
607 "%s: channel%s not in expected PIM_IFASSERT_I_AM_WINNER state",
608 __func__
, ch
->sg_str
);
615 RFC 4601: 4.6.1. (S,G) Assert Message State Machine
617 (S,G) Assert State machine Actions
619 A2: Store new assert winner as AssertWinner(S,G,I) and assert
620 winner metric as AssertWinnerMetric(S,G,I).
621 Set Assert Timer to Assert_Time.
623 static void assert_action_a2(struct pim_ifchannel
*ch
,
624 struct pim_assert_metric winner_metric
)
626 pim_ifassert_winner_set(ch
, PIM_IFASSERT_I_AM_LOSER
,
627 winner_metric
.ip_address
, winner_metric
);
629 pim_assert_timer_set(ch
, PIM_ASSERT_TIME
);
631 if (ch
->ifassert_state
!= PIM_IFASSERT_I_AM_LOSER
) {
632 if (PIM_DEBUG_PIM_EVENTS
)
634 "%s: channel%s not in expected PIM_IFASSERT_I_AM_LOSER state",
635 __func__
, ch
->sg_str
);
640 RFC 4601: 4.6.1. (S,G) Assert Message State Machine
642 (S,G) Assert State machine Actions
644 A3: Send Assert(S,G).
645 Set Assert Timer to (Assert_Time - Assert_Override_Interval).
647 static int assert_action_a3(struct pim_ifchannel
*ch
)
649 if (ch
->ifassert_state
!= PIM_IFASSERT_I_AM_WINNER
) {
650 if (PIM_DEBUG_PIM_EVENTS
)
652 "%s: channel%s expected to be in PIM_IFASSERT_I_AM_WINNER state",
653 __func__
, ch
->sg_str
);
657 pim_assert_timer_reset(ch
);
659 if (pim_assert_send(ch
)) {
660 zlog_warn("%s: (S,G)=%s failure sending assert on interface %s",
661 __func__
, ch
->sg_str
, ch
->interface
->name
);
669 RFC 4601: 4.6.1. (S,G) Assert Message State Machine
671 (S,G) Assert State machine Actions
673 A4: Send AssertCancel(S,G).
674 Delete assert info (AssertWinner(S,G,I) and
675 AssertWinnerMetric(S,G,I) will then return their default
678 void assert_action_a4(struct pim_ifchannel
*ch
)
680 if (pim_assert_cancel(ch
)) {
681 zlog_warn("%s: failure sending AssertCancel%s on interface %s",
682 __func__
, ch
->sg_str
, ch
->interface
->name
);
683 /* log warning only */
686 assert_action_a5(ch
);
688 if (ch
->ifassert_state
!= PIM_IFASSERT_NOINFO
) {
689 if (PIM_DEBUG_PIM_EVENTS
)
691 "%s: channel%s not in PIM_IFASSERT_NOINFO state as expected",
692 __func__
, ch
->sg_str
);
697 RFC 4601: 4.6.1. (S,G) Assert Message State Machine
699 (S,G) Assert State machine Actions
701 A5: Delete assert info (AssertWinner(S,G,I) and
702 AssertWinnerMetric(S,G,I) will then return their default values).
704 void assert_action_a5(struct pim_ifchannel
*ch
)
706 reset_ifassert_state(ch
);
707 if (ch
->ifassert_state
!= PIM_IFASSERT_NOINFO
) {
708 if (PIM_DEBUG_PIM_EVENTS
)
710 "%s: channel%s not in PIM_IFSSERT_NOINFO state as expected",
711 __func__
, ch
->sg_str
);
716 RFC 4601: 4.6.1. (S,G) Assert Message State Machine
718 (S,G) Assert State machine Actions
720 A6: Store new assert winner as AssertWinner(S,G,I) and assert
721 winner metric as AssertWinnerMetric(S,G,I).
722 Set Assert Timer to Assert_Time.
723 If (I is RPF_interface(S)) AND (UpstreamJPState(S,G) == true)
724 set SPTbit(S,G) to true.
726 static void assert_action_a6(struct pim_ifchannel
*ch
,
727 struct pim_assert_metric winner_metric
)
729 assert_action_a2(ch
, winner_metric
);
732 If (I is RPF_interface(S)) AND (UpstreamJPState(S,G) == true) set
735 if (ch
->upstream
->rpf
.source_nexthop
.interface
== ch
->interface
)
736 if (ch
->upstream
->join_state
== PIM_UPSTREAM_JOINED
)
737 ch
->upstream
->sptbit
= PIM_UPSTREAM_SPTBIT_TRUE
;
739 if (ch
->ifassert_state
!= PIM_IFASSERT_I_AM_LOSER
) {
740 if (PIM_DEBUG_PIM_EVENTS
)
742 "%s: channel%s not in PIM_IFASSERT_I_AM_LOSER state as expected",
743 __func__
, ch
->sg_str
);