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
30 #include "pim_hello.h"
31 #include "pim_iface.h"
32 #include "pim_neighbor.h"
33 #include "pim_upstream.h"
36 static void on_trace(const char *label
, struct interface
*ifp
,
39 if (PIM_DEBUG_PIM_TRACE
) {
40 char src_str
[INET_ADDRSTRLEN
];
41 pim_inet4_dump("<src?>", src
, src_str
, sizeof(src_str
));
42 zlog_debug("%s: from %s on %s", label
, src_str
, ifp
->name
);
46 static void tlv_trace_bool(const char *label
, const char *tlv_name
,
47 const char *ifname
, struct in_addr src_addr
,
51 char src_str
[INET_ADDRSTRLEN
];
52 pim_inet4_dump("<src?>", src_addr
, src_str
, sizeof(src_str
));
54 "%s: PIM hello option from %s on interface %s: %s=%d",
55 label
, src_str
, ifname
, tlv_name
, value
);
59 static void tlv_trace_uint16(const char *label
, const char *tlv_name
,
60 const char *ifname
, struct in_addr src_addr
,
61 int isset
, uint16_t value
)
64 char src_str
[INET_ADDRSTRLEN
];
65 pim_inet4_dump("<src?>", src_addr
, src_str
, sizeof(src_str
));
67 "%s: PIM hello option from %s on interface %s: %s=%u",
68 label
, src_str
, ifname
, tlv_name
, value
);
72 static void tlv_trace_uint32(const char *label
, const char *tlv_name
,
73 const char *ifname
, struct in_addr src_addr
,
74 int isset
, uint32_t value
)
77 char src_str
[INET_ADDRSTRLEN
];
78 pim_inet4_dump("<src?>", src_addr
, src_str
, sizeof(src_str
));
80 "%s: PIM hello option from %s on interface %s: %s=%u",
81 label
, src_str
, ifname
, tlv_name
, value
);
85 static void tlv_trace_uint32_hex(const char *label
, const char *tlv_name
,
86 const char *ifname
, struct in_addr src_addr
,
87 int isset
, uint32_t value
)
90 char src_str
[INET_ADDRSTRLEN
];
91 pim_inet4_dump("<src?>", src_addr
, src_str
, sizeof(src_str
));
93 "%s: PIM hello option from %s on interface %s: %s=%08x",
94 label
, src_str
, ifname
, tlv_name
, value
);
99 static void tlv_trace(const char *label
, const char *tlv_name
,
100 const char *ifname
, struct in_addr src_addr
,
104 char src_str
[INET_ADDRSTRLEN
];
105 pim_inet4_dump("<src?>", src_addr
, src_str
, sizeof(src_str
));
106 zlog_debug("%s: PIM hello option from %s on interface %s: %s",
114 static void tlv_trace_list(const char *label
, const char *tlv_name
,
115 const char *ifname
, struct in_addr src_addr
,
116 int isset
, struct list
*addr_list
)
119 char src_str
[INET_ADDRSTRLEN
];
120 pim_inet4_dump("<src?>", src_addr
, src_str
, sizeof(src_str
));
122 "%s: PIM hello option from %s on interface %s: %s size=%d list=%p",
123 label
, src_str
, ifname
, tlv_name
,
124 addr_list
? ((int)listcount(addr_list
)) : -1,
129 #define FREE_ADDR_LIST \
130 if (hello_option_addr_list) { \
131 list_delete(&hello_option_addr_list); \
134 #define FREE_ADDR_LIST_THEN_RETURN(code) \
140 int pim_hello_recv(struct interface
*ifp
, struct in_addr src_addr
,
141 uint8_t *tlv_buf
, int tlv_buf_size
)
143 struct pim_interface
*pim_ifp
;
144 struct pim_neighbor
*neigh
;
146 uint8_t *tlv_pastend
;
147 pim_hello_options hello_options
=
148 0; /* bit array recording options found */
149 uint16_t hello_option_holdtime
= 0;
150 uint16_t hello_option_propagation_delay
= 0;
151 uint16_t hello_option_override_interval
= 0;
152 uint32_t hello_option_dr_priority
= 0;
153 uint32_t hello_option_generation_id
= 0;
154 struct list
*hello_option_addr_list
= 0;
156 if (PIM_DEBUG_PIM_HELLO
)
157 on_trace(__PRETTY_FUNCTION__
, ifp
, src_addr
);
162 ++pim_ifp
->pim_ifstat_hello_recv
;
167 zassert(tlv_buf_size
>= 0);
169 tlv_pastend
= tlv_buf
+ tlv_buf_size
;
171 while (tlv_curr
< tlv_pastend
) {
172 uint16_t option_type
;
174 int remain
= tlv_pastend
- tlv_curr
;
176 if (remain
< PIM_TLV_MIN_SIZE
) {
177 if (PIM_DEBUG_PIM_HELLO
) {
178 char src_str
[INET_ADDRSTRLEN
];
179 pim_inet4_dump("<src?>", src_addr
, src_str
,
182 "%s: short PIM hello TLV size=%d < min=%d from %s on interface %s",
183 __PRETTY_FUNCTION__
, remain
,
184 PIM_TLV_MIN_SIZE
, src_str
, ifp
->name
);
186 FREE_ADDR_LIST_THEN_RETURN(-1);
189 option_type
= PIM_TLV_GET_TYPE(tlv_curr
);
190 tlv_curr
+= PIM_TLV_TYPE_SIZE
;
191 option_len
= PIM_TLV_GET_LENGTH(tlv_curr
);
192 tlv_curr
+= PIM_TLV_LENGTH_SIZE
;
194 if ((tlv_curr
+ option_len
) > tlv_pastend
) {
195 if (PIM_DEBUG_PIM_HELLO
) {
196 char src_str
[INET_ADDRSTRLEN
];
197 pim_inet4_dump("<src?>", src_addr
, src_str
,
200 "%s: long PIM hello TLV type=%d length=%d > left=%td from %s on interface %s",
201 __PRETTY_FUNCTION__
, option_type
,
202 option_len
, tlv_pastend
- tlv_curr
,
205 FREE_ADDR_LIST_THEN_RETURN(-2);
208 if (PIM_DEBUG_PIM_HELLO
) {
209 char src_str
[INET_ADDRSTRLEN
];
210 pim_inet4_dump("<src?>", src_addr
, src_str
,
213 "%s: parse left_size=%d: PIM hello TLV type=%d length=%d from %s on %s",
214 __PRETTY_FUNCTION__
, remain
, option_type
,
215 option_len
, src_str
, ifp
->name
);
218 switch (option_type
) {
219 case PIM_MSG_OPTION_TYPE_HOLDTIME
:
220 if (pim_tlv_parse_holdtime(ifp
->name
, src_addr
,
222 &hello_option_holdtime
,
223 option_len
, tlv_curr
)) {
224 FREE_ADDR_LIST_THEN_RETURN(-3);
227 case PIM_MSG_OPTION_TYPE_LAN_PRUNE_DELAY
:
228 if (pim_tlv_parse_lan_prune_delay(
229 ifp
->name
, src_addr
, &hello_options
,
230 &hello_option_propagation_delay
,
231 &hello_option_override_interval
, option_len
,
233 FREE_ADDR_LIST_THEN_RETURN(-4);
236 case PIM_MSG_OPTION_TYPE_DR_PRIORITY
:
237 if (pim_tlv_parse_dr_priority(ifp
->name
, src_addr
,
239 &hello_option_dr_priority
,
240 option_len
, tlv_curr
)) {
241 FREE_ADDR_LIST_THEN_RETURN(-5);
244 case PIM_MSG_OPTION_TYPE_GENERATION_ID
:
245 if (pim_tlv_parse_generation_id(
246 ifp
->name
, src_addr
, &hello_options
,
247 &hello_option_generation_id
, option_len
,
249 FREE_ADDR_LIST_THEN_RETURN(-6);
252 case PIM_MSG_OPTION_TYPE_ADDRESS_LIST
:
253 if (pim_tlv_parse_addr_list(ifp
->name
, src_addr
,
255 &hello_option_addr_list
,
256 option_len
, tlv_curr
)) {
260 case PIM_MSG_OPTION_TYPE_DM_STATE_REFRESH
:
261 if (PIM_DEBUG_PIM_HELLO
) {
262 char src_str
[INET_ADDRSTRLEN
];
263 pim_inet4_dump("<src?>", src_addr
, src_str
,
266 "%s: ignoring PIM hello dense-mode state refresh TLV option type=%d length=%d from %s on interface %s",
267 __PRETTY_FUNCTION__
, option_type
,
268 option_len
, src_str
, ifp
->name
);
272 if (PIM_DEBUG_PIM_HELLO
) {
273 char src_str
[INET_ADDRSTRLEN
];
274 pim_inet4_dump("<src?>", src_addr
, src_str
,
277 "%s: ignoring unknown PIM hello TLV type=%d length=%d from %s on interface %s",
278 __PRETTY_FUNCTION__
, option_type
,
279 option_len
, src_str
, ifp
->name
);
283 tlv_curr
+= option_len
;
287 Check received PIM hello options
290 if (PIM_DEBUG_PIM_HELLO
) {
291 tlv_trace_uint16(__PRETTY_FUNCTION__
, "holdtime", ifp
->name
,
293 PIM_OPTION_IS_SET(hello_options
,
294 PIM_OPTION_MASK_HOLDTIME
),
295 hello_option_holdtime
);
297 __PRETTY_FUNCTION__
, "propagation_delay", ifp
->name
,
299 PIM_OPTION_IS_SET(hello_options
,
300 PIM_OPTION_MASK_LAN_PRUNE_DELAY
),
301 hello_option_propagation_delay
);
303 __PRETTY_FUNCTION__
, "override_interval", ifp
->name
,
305 PIM_OPTION_IS_SET(hello_options
,
306 PIM_OPTION_MASK_LAN_PRUNE_DELAY
),
307 hello_option_override_interval
);
309 __PRETTY_FUNCTION__
, "can_disable_join_suppression",
311 PIM_OPTION_IS_SET(hello_options
,
312 PIM_OPTION_MASK_LAN_PRUNE_DELAY
),
315 PIM_OPTION_MASK_CAN_DISABLE_JOIN_SUPPRESSION
));
316 tlv_trace_uint32(__PRETTY_FUNCTION__
, "dr_priority", ifp
->name
,
318 PIM_OPTION_IS_SET(hello_options
,
319 PIM_OPTION_MASK_DR_PRIORITY
),
320 hello_option_dr_priority
);
321 tlv_trace_uint32_hex(
322 __PRETTY_FUNCTION__
, "generation_id", ifp
->name
,
324 PIM_OPTION_IS_SET(hello_options
,
325 PIM_OPTION_MASK_GENERATION_ID
),
326 hello_option_generation_id
);
327 tlv_trace_list(__PRETTY_FUNCTION__
, "address_list", ifp
->name
,
329 PIM_OPTION_IS_SET(hello_options
,
330 PIM_OPTION_MASK_ADDRESS_LIST
),
331 hello_option_addr_list
);
334 if (!PIM_OPTION_IS_SET(hello_options
, PIM_OPTION_MASK_HOLDTIME
)) {
335 if (PIM_DEBUG_PIM_HELLO
) {
336 char src_str
[INET_ADDRSTRLEN
];
337 pim_inet4_dump("<src?>", src_addr
, src_str
,
340 "%s: PIM hello missing holdtime from %s on interface %s",
341 __PRETTY_FUNCTION__
, src_str
, ifp
->name
);
349 neigh
= pim_neighbor_find(ifp
, src_addr
);
351 /* Add as new neighbor */
353 neigh
= pim_neighbor_add(
354 ifp
, src_addr
, hello_options
, hello_option_holdtime
,
355 hello_option_propagation_delay
,
356 hello_option_override_interval
,
357 hello_option_dr_priority
, hello_option_generation_id
,
358 hello_option_addr_list
, PIM_NEIGHBOR_SEND_DELAY
);
360 if (PIM_DEBUG_PIM_HELLO
) {
361 char src_str
[INET_ADDRSTRLEN
];
362 pim_inet4_dump("<src?>", src_addr
, src_str
,
365 "%s: failure creating PIM neighbor %s on interface %s",
366 __PRETTY_FUNCTION__
, src_str
,
369 FREE_ADDR_LIST_THEN_RETURN(-8);
371 /* Forward BSM if required */
372 if (!pim_bsm_new_nbr_fwd(neigh
, ifp
)) {
373 if (PIM_DEBUG_PIM_HELLO
)
374 zlog_debug("%s: forwarding bsm to new nbr failed",
375 __PRETTY_FUNCTION__
);
378 /* actual addr list has been saved under neighbor */
383 Received generation ID ?
386 if (PIM_OPTION_IS_SET(hello_options
, PIM_OPTION_MASK_GENERATION_ID
)) {
387 /* GenID mismatch ? */
388 if (!PIM_OPTION_IS_SET(neigh
->hello_options
,
389 PIM_OPTION_MASK_GENERATION_ID
)
390 || (hello_option_generation_id
!= neigh
->generation_id
)) {
391 /* GenID mismatch, then replace neighbor */
393 if (PIM_DEBUG_PIM_HELLO
) {
394 char src_str
[INET_ADDRSTRLEN
];
395 pim_inet4_dump("<src?>", src_addr
, src_str
,
398 "%s: GenId mismatch new=%08x old=%08x: replacing neighbor %s on %s",
400 hello_option_generation_id
,
401 neigh
->generation_id
, src_str
,
405 pim_upstream_rpf_genid_changed(pim_ifp
->pim
,
408 pim_neighbor_delete(ifp
, neigh
, "GenID mismatch");
409 neigh
= pim_neighbor_add(ifp
, src_addr
, hello_options
,
410 hello_option_holdtime
,
411 hello_option_propagation_delay
,
412 hello_option_override_interval
,
413 hello_option_dr_priority
,
414 hello_option_generation_id
,
415 hello_option_addr_list
,
416 PIM_NEIGHBOR_SEND_NOW
);
418 if (PIM_DEBUG_PIM_HELLO
) {
419 char src_str
[INET_ADDRSTRLEN
];
420 pim_inet4_dump("<src?>", src_addr
,
424 "%s: failure re-creating PIM neighbor %s on interface %s",
425 __PRETTY_FUNCTION__
, src_str
,
428 FREE_ADDR_LIST_THEN_RETURN(-9);
430 /* Forward BSM if required */
431 if (!pim_bsm_new_nbr_fwd(neigh
, ifp
)) {
432 if (PIM_DEBUG_PIM_HELLO
)
433 zlog_debug("%s: forwarding bsm to new nbr failed",
434 __PRETTY_FUNCTION__
);
436 /* actual addr list is saved under neighbor */
439 } /* GenId mismatch: replace neighbor */
441 } /* GenId received */
444 Update existing neighbor
447 pim_neighbor_update(neigh
, hello_options
, hello_option_holdtime
,
448 hello_option_dr_priority
, hello_option_addr_list
);
449 /* actual addr list is saved under neighbor */
453 int pim_hello_build_tlv(struct interface
*ifp
, uint8_t *tlv_buf
,
454 int tlv_buf_size
, uint16_t holdtime
,
455 uint32_t dr_priority
, uint32_t generation_id
,
456 uint16_t propagation_delay
, uint16_t override_interval
,
457 int can_disable_join_suppression
)
459 uint8_t *curr
= tlv_buf
;
460 uint8_t *pastend
= tlv_buf
+ tlv_buf_size
;
462 struct pim_interface
*pim_ifp
= ifp
->info
;
463 struct pim_instance
*pim
= pim_ifp
->pim
;
470 curr
= pim_tlv_append_uint16(curr
, pastend
,
471 PIM_MSG_OPTION_TYPE_HOLDTIME
, holdtime
);
473 if (PIM_DEBUG_PIM_HELLO
) {
475 "%s: could not set PIM hello Holdtime option for interface %s",
476 __PRETTY_FUNCTION__
, ifp
->name
);
481 /* LAN Prune Delay */
482 tmp
= pim_tlv_append_2uint16(curr
, pastend
,
483 PIM_MSG_OPTION_TYPE_LAN_PRUNE_DELAY
,
484 propagation_delay
, override_interval
);
486 if (PIM_DEBUG_PIM_HELLO
) {
488 "%s: could not set PIM LAN Prune Delay option for interface %s",
489 __PRETTY_FUNCTION__
, ifp
->name
);
493 if (can_disable_join_suppression
) {
494 *((uint8_t *)(curr
) + 4) |= 0x80; /* enable T bit */
499 curr
= pim_tlv_append_uint32(
500 curr
, pastend
, PIM_MSG_OPTION_TYPE_DR_PRIORITY
, dr_priority
);
502 if (PIM_DEBUG_PIM_HELLO
) {
504 "%s: could not set PIM hello DR Priority option for interface %s",
505 __PRETTY_FUNCTION__
, ifp
->name
);
511 curr
= pim_tlv_append_uint32(curr
, pastend
,
512 PIM_MSG_OPTION_TYPE_GENERATION_ID
,
515 if (PIM_DEBUG_PIM_HELLO
) {
517 "%s: could not set PIM hello Generation ID option for interface %s",
518 __PRETTY_FUNCTION__
, ifp
->name
);
523 /* Secondary Address List */
524 if (ifp
->connected
->count
) {
525 curr
= pim_tlv_append_addrlist_ucast(curr
, pastend
,
526 ifp
->connected
, AF_INET
);
528 if (PIM_DEBUG_PIM_HELLO
) {
530 "%s: could not set PIM hello v4 Secondary Address List option for interface %s",
531 __PRETTY_FUNCTION__
, ifp
->name
);
535 if (pim
->send_v6_secondary
) {
536 curr
= pim_tlv_append_addrlist_ucast(
537 curr
, pastend
, ifp
->connected
, AF_INET6
);
539 if (PIM_DEBUG_PIM_HELLO
) {
541 "%s: could not sent PIM hello v6 secondary Address List option for interface %s",
542 __PRETTY_FUNCTION__
, ifp
->name
);
549 return curr
- tlv_buf
;
553 RFC 4601: 4.3.1. Sending Hello Messages
555 Thus, if a router needs to send a Join/Prune or Assert message on an
556 interface on which it has not yet sent a Hello message with the
557 currently configured IP address, then it MUST immediately send the
558 relevant Hello message without waiting for the Hello Timer to
559 expire, followed by the Join/Prune or Assert message.
561 void pim_hello_require(struct interface
*ifp
)
563 struct pim_interface
*pim_ifp
;
571 if (pim_ifp
->pim_ifstat_hello_sent
)
574 pim_hello_restart_now(ifp
); /* Send hello and restart timer */