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
27 #include "pim_instance.h"
34 #define PIM_MSG_ADDRESS_FAMILY PIM_MSG_ADDRESS_FAMILY_IPV4
36 #define PIM_MSG_ADDRESS_FAMILY PIM_MSG_ADDRESS_FAMILY_IPV6
39 uint8_t *pim_tlv_append_uint16(uint8_t *buf
, const uint8_t *buf_pastend
,
40 uint16_t option_type
, uint16_t option_value
)
42 uint16_t option_len
= 2;
44 if ((buf
+ PIM_TLV_OPTION_SIZE(option_len
)) > buf_pastend
)
47 *(uint16_t *)buf
= htons(option_type
);
49 *(uint16_t *)buf
= htons(option_len
);
51 *(uint16_t *)buf
= htons(option_value
);
57 uint8_t *pim_tlv_append_2uint16(uint8_t *buf
, const uint8_t *buf_pastend
,
58 uint16_t option_type
, uint16_t option_value1
,
59 uint16_t option_value2
)
61 uint16_t option_len
= 4;
63 if ((buf
+ PIM_TLV_OPTION_SIZE(option_len
)) > buf_pastend
)
66 *(uint16_t *)buf
= htons(option_type
);
68 *(uint16_t *)buf
= htons(option_len
);
70 *(uint16_t *)buf
= htons(option_value1
);
72 *(uint16_t *)buf
= htons(option_value2
);
78 uint8_t *pim_tlv_append_uint32(uint8_t *buf
, const uint8_t *buf_pastend
,
79 uint16_t option_type
, uint32_t option_value
)
81 uint16_t option_len
= 4;
83 if ((buf
+ PIM_TLV_OPTION_SIZE(option_len
)) > buf_pastend
)
86 *(uint16_t *)buf
= htons(option_type
);
88 *(uint16_t *)buf
= htons(option_len
);
90 pim_write_uint32(buf
, option_value
);
96 #define ucast_ipv4_encoding_len (2 + sizeof(struct in_addr))
97 #define ucast_ipv6_encoding_len (2 + sizeof(struct in6_addr))
100 * An Encoded-Unicast address takes the following format:
103 * 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
104 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 * | Addr Family | Encoding Type | Unicast Address
106 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+...
109 * The PIM address family of the 'Unicast Address' field of this
112 * Values 0-127 are as assigned by the IANA for Internet Address *
113 * Families in [7]. Values 128-250 are reserved to be assigned by
114 * the IANA for PIM-specific Address Families. Values 251 though
115 * 255 are designated for private use. As there is no assignment
116 * authority for this space, collisions should be expected.
119 * The type of encoding used within a specific Address Family. The
120 * value '0' is reserved for this field and represents the native
121 * encoding of the Address Family.
124 * The unicast address as represented by the given Address Family
127 int pim_encode_addr_ucast(uint8_t *buf
, pim_addr addr
)
129 uint8_t *start
= buf
;
131 *buf
++ = PIM_MSG_ADDRESS_FAMILY
;
133 memcpy(buf
, &addr
, sizeof(addr
));
139 int pim_encode_addr_ucast_prefix(uint8_t *buf
, struct prefix
*p
)
143 *buf
= PIM_MSG_ADDRESS_FAMILY_IPV4
; /* notice: AF_INET !=
144 PIM_MSG_ADDRESS_FAMILY_IPV4
147 *buf
= 0; /* ucast IPv4 native encoding type (RFC
150 memcpy(buf
, &p
->u
.prefix4
, sizeof(struct in_addr
));
151 return ucast_ipv4_encoding_len
;
153 *buf
= PIM_MSG_ADDRESS_FAMILY_IPV6
;
157 memcpy(buf
, &p
->u
.prefix6
, sizeof(struct in6_addr
));
158 return ucast_ipv6_encoding_len
;
164 #define group_ipv4_encoding_len (4 + sizeof(struct in_addr))
167 * Encoded-Group addresses take the following format:
170 * 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
171 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
172 * | Addr Family | Encoding Type |B| Reserved |Z| Mask Len |
173 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
174 * | Group multicast Address
175 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+...
183 * [B]idirectional PIM
184 * Indicates the group range should use Bidirectional PIM [13].
185 * For PIM-SM defined in this specification, this bit MUST be zero.
188 * Transmitted as zero. Ignored upon receipt.
191 * indicates the group range is an admin scope zone. This is used
192 * in the Bootstrap Router Mechanism [11] only. For all other
193 * purposes, this bit is set to zero and ignored on receipt.
196 * The Mask length field is 8 bits. The value is the number of
197 * contiguous one bits that are left justified and used as a mask;
198 * when combined with the group address, it describes a range of
199 * groups. It is less than or equal to the address length in bits
200 * for the given Address Family and Encoding Type. If the message
201 * is sent for a single group, then the Mask length must equal the
202 * address length in bits for the given Address Family and Encoding
203 * Type (e.g., 32 for IPv4 native encoding, 128 for IPv6 native
206 * Group multicast Address
207 * Contains the group address.
209 int pim_encode_addr_group(uint8_t *buf
, afi_t afi
, int bidir
, int scope
,
212 uint8_t *start
= buf
;
218 *buf
++ = PIM_MSG_ADDRESS_FAMILY
;
221 *buf
++ = sizeof(group
) * 8;
222 memcpy(buf
, &group
, sizeof(group
));
223 buf
+= sizeof(group
);
228 uint8_t *pim_tlv_append_addrlist_ucast(uint8_t *buf
, const uint8_t *buf_pastend
,
229 struct list
*ifconnected
, int family
)
231 struct listnode
*node
;
232 uint16_t option_len
= 0;
236 node
= listhead(ifconnected
);
238 /* Empty address list ? */
243 if (family
== AF_INET
)
244 uel
= ucast_ipv4_encoding_len
;
246 uel
= ucast_ipv6_encoding_len
;
248 /* Scan secondary address list */
249 curr
= buf
+ 4; /* skip T and L */
250 for (; node
; node
= listnextnode(node
)) {
251 struct connected
*ifc
= listgetdata(node
);
252 struct prefix
*p
= ifc
->address
;
255 if (!CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
))
258 if ((curr
+ uel
) > buf_pastend
)
261 if (p
->family
!= family
)
264 l_encode
= pim_encode_addr_ucast_prefix(curr
, p
);
266 option_len
+= l_encode
;
269 if (PIM_DEBUG_PIM_TRACE_DETAIL
) {
271 "%s: number of encoded secondary unicast IPv4 addresses: %zu",
272 __func__
, option_len
/ uel
);
275 if (option_len
< 1) {
276 /* Empty secondary unicast IPv4 address list */
283 *(uint16_t *)buf
= htons(PIM_MSG_OPTION_TYPE_ADDRESS_LIST
);
284 *(uint16_t *)(buf
+ 2) = htons(option_len
);
289 static int check_tlv_length(const char *label
, const char *tlv_name
,
290 const char *ifname
, pim_addr src_addr
,
291 int correct_len
, int option_len
)
293 if (option_len
!= correct_len
) {
295 "%s: PIM hello %s TLV with incorrect value size=%d correct=%d from %pPAs on interface %s",
296 label
, tlv_name
, option_len
, correct_len
, &src_addr
,
304 static void check_tlv_redefinition_uint16(const char *label
,
305 const char *tlv_name
,
306 const char *ifname
, pim_addr src_addr
,
307 pim_hello_options options
,
308 pim_hello_options opt_mask
,
309 uint16_t new, uint16_t old
)
311 if (PIM_OPTION_IS_SET(options
, opt_mask
))
313 "%s: PIM hello TLV redefined %s=%u old=%u from %pPAs on interface %s",
314 label
, tlv_name
, new, old
, &src_addr
, ifname
);
317 static void check_tlv_redefinition_uint32(const char *label
,
318 const char *tlv_name
,
319 const char *ifname
, pim_addr src_addr
,
320 pim_hello_options options
,
321 pim_hello_options opt_mask
,
322 uint32_t new, uint32_t old
)
324 if (PIM_OPTION_IS_SET(options
, opt_mask
))
326 "%s: PIM hello TLV redefined %s=%u old=%u from %pPAs on interface %s",
327 label
, tlv_name
, new, old
, &src_addr
, ifname
);
330 static void check_tlv_redefinition_uint32_hex(
331 const char *label
, const char *tlv_name
, const char *ifname
,
332 pim_addr src_addr
, pim_hello_options options
,
333 pim_hello_options opt_mask
, uint32_t new, uint32_t old
)
335 if (PIM_OPTION_IS_SET(options
, opt_mask
))
337 "%s: PIM hello TLV redefined %s=%08x old=%08x from %pPAs on interface %s",
338 label
, tlv_name
, new, old
, &src_addr
, ifname
);
341 int pim_tlv_parse_holdtime(const char *ifname
, pim_addr src_addr
,
342 pim_hello_options
*hello_options
,
343 uint16_t *hello_option_holdtime
, uint16_t option_len
,
344 const uint8_t *tlv_curr
)
346 const char *label
= "holdtime";
348 if (check_tlv_length(__func__
, label
, ifname
, src_addr
,
349 sizeof(uint16_t), option_len
)) {
353 check_tlv_redefinition_uint16(__func__
, label
, ifname
, src_addr
,
354 *hello_options
, PIM_OPTION_MASK_HOLDTIME
,
355 PIM_TLV_GET_HOLDTIME(tlv_curr
),
356 *hello_option_holdtime
);
358 PIM_OPTION_SET(*hello_options
, PIM_OPTION_MASK_HOLDTIME
);
360 *hello_option_holdtime
= PIM_TLV_GET_HOLDTIME(tlv_curr
);
365 int pim_tlv_parse_lan_prune_delay(const char *ifname
, pim_addr src_addr
,
366 pim_hello_options
*hello_options
,
367 uint16_t *hello_option_propagation_delay
,
368 uint16_t *hello_option_override_interval
,
369 uint16_t option_len
, const uint8_t *tlv_curr
)
371 if (check_tlv_length(__func__
, "lan_prune_delay", ifname
, src_addr
,
372 sizeof(uint32_t), option_len
)) {
376 check_tlv_redefinition_uint16(__func__
, "propagation_delay", ifname
,
377 src_addr
, *hello_options
,
378 PIM_OPTION_MASK_LAN_PRUNE_DELAY
,
379 PIM_TLV_GET_PROPAGATION_DELAY(tlv_curr
),
380 *hello_option_propagation_delay
);
382 PIM_OPTION_SET(*hello_options
, PIM_OPTION_MASK_LAN_PRUNE_DELAY
);
384 *hello_option_propagation_delay
=
385 PIM_TLV_GET_PROPAGATION_DELAY(tlv_curr
);
386 if (PIM_TLV_GET_CAN_DISABLE_JOIN_SUPPRESSION(tlv_curr
)) {
387 PIM_OPTION_SET(*hello_options
,
388 PIM_OPTION_MASK_CAN_DISABLE_JOIN_SUPPRESSION
);
390 PIM_OPTION_UNSET(*hello_options
,
391 PIM_OPTION_MASK_CAN_DISABLE_JOIN_SUPPRESSION
);
395 *hello_option_override_interval
=
396 PIM_TLV_GET_OVERRIDE_INTERVAL(tlv_curr
);
401 int pim_tlv_parse_dr_priority(const char *ifname
, pim_addr src_addr
,
402 pim_hello_options
*hello_options
,
403 uint32_t *hello_option_dr_priority
,
404 uint16_t option_len
, const uint8_t *tlv_curr
)
406 const char *label
= "dr_priority";
408 if (check_tlv_length(__func__
, label
, ifname
, src_addr
,
409 sizeof(uint32_t), option_len
)) {
413 check_tlv_redefinition_uint32(
414 __func__
, label
, ifname
, src_addr
, *hello_options
,
415 PIM_OPTION_MASK_DR_PRIORITY
, PIM_TLV_GET_DR_PRIORITY(tlv_curr
),
416 *hello_option_dr_priority
);
418 PIM_OPTION_SET(*hello_options
, PIM_OPTION_MASK_DR_PRIORITY
);
420 *hello_option_dr_priority
= PIM_TLV_GET_DR_PRIORITY(tlv_curr
);
425 int pim_tlv_parse_generation_id(const char *ifname
, pim_addr src_addr
,
426 pim_hello_options
*hello_options
,
427 uint32_t *hello_option_generation_id
,
428 uint16_t option_len
, const uint8_t *tlv_curr
)
430 const char *label
= "generation_id";
432 if (check_tlv_length(__func__
, label
, ifname
, src_addr
,
433 sizeof(uint32_t), option_len
)) {
437 check_tlv_redefinition_uint32_hex(__func__
, label
, ifname
, src_addr
,
439 PIM_OPTION_MASK_GENERATION_ID
,
440 PIM_TLV_GET_GENERATION_ID(tlv_curr
),
441 *hello_option_generation_id
);
443 PIM_OPTION_SET(*hello_options
, PIM_OPTION_MASK_GENERATION_ID
);
445 *hello_option_generation_id
= PIM_TLV_GET_GENERATION_ID(tlv_curr
);
450 int pim_parse_addr_ucast_prefix(struct prefix
*p
, const uint8_t *buf
,
453 const int ucast_encoding_min_len
= 3; /* 1 family + 1 type + 1 addr */
455 const uint8_t *pastend
;
459 if (buf_size
< ucast_encoding_min_len
) {
461 "%s: unicast address encoding overflow: left=%d needed=%d",
462 __func__
, buf_size
, ucast_encoding_min_len
);
467 pastend
= buf
+ buf_size
;
473 zlog_warn("%s: unknown unicast address encoding type=%d",
479 case PIM_MSG_ADDRESS_FAMILY_IPV4
:
480 if ((addr
+ sizeof(struct in_addr
)) > pastend
) {
482 "%s: IPv4 unicast address overflow: left=%td needed=%zu",
483 __func__
, pastend
- addr
,
484 sizeof(struct in_addr
));
488 p
->family
= AF_INET
; /* notice: AF_INET !=
489 PIM_MSG_ADDRESS_FAMILY_IPV4 */
490 memcpy(&p
->u
.prefix4
, addr
, sizeof(struct in_addr
));
491 p
->prefixlen
= IPV4_MAX_BITLEN
;
492 addr
+= sizeof(struct in_addr
);
495 case PIM_MSG_ADDRESS_FAMILY_IPV6
:
496 if ((addr
+ sizeof(struct in6_addr
)) > pastend
) {
498 "%s: IPv6 unicast address overflow: left=%td needed %zu",
499 __func__
, pastend
- addr
,
500 sizeof(struct in6_addr
));
504 p
->family
= AF_INET6
;
505 p
->prefixlen
= IPV6_MAX_BITLEN
;
506 memcpy(&p
->u
.prefix6
, addr
, sizeof(struct in6_addr
));
507 addr
+= sizeof(struct in6_addr
);
511 zlog_warn("%s: unknown unicast address encoding family=%d from",
520 int pim_parse_addr_ucast(pim_addr
*out
, const uint8_t *buf
, int buf_size
,
526 ret
= pim_parse_addr_ucast_prefix(&p
, buf
, buf_size
);
530 if (p
.family
!= PIM_AF
) {
535 memcpy(out
, &p
.u
.val
, sizeof(*out
));
539 int pim_parse_addr_group(pim_sgaddr
*sg
, const uint8_t *buf
, int buf_size
)
541 const int grp_encoding_min_len
=
542 4; /* 1 family + 1 type + 1 reserved + 1 addr */
544 const uint8_t *pastend
;
549 if (buf_size
< grp_encoding_min_len
) {
551 "%s: group address encoding overflow: left=%d needed=%d",
552 __func__
, buf_size
, grp_encoding_min_len
);
557 pastend
= buf
+ buf_size
;
561 ++addr
; /* skip b_reserved_z fields */
565 zlog_warn("%s: unknown group address encoding type=%d from",
570 if (family
!= PIM_MSG_ADDRESS_FAMILY
) {
572 "%s: unknown group address encoding family=%d mask_len=%d from",
573 __func__
, family
, mask_len
);
577 if ((addr
+ sizeof(sg
->grp
)) > pastend
) {
579 "%s: group address overflow: left=%td needed=%zu from",
580 __func__
, pastend
- addr
, sizeof(sg
->grp
));
584 memcpy(&sg
->grp
, addr
, sizeof(sg
->grp
));
585 addr
+= sizeof(sg
->grp
);
590 int pim_parse_addr_source(pim_sgaddr
*sg
, uint8_t *flags
, const uint8_t *buf
,
593 const int src_encoding_min_len
=
594 4; /* 1 family + 1 type + 1 reserved + 1 addr */
596 const uint8_t *pastend
;
601 if (buf_size
< src_encoding_min_len
) {
603 "%s: source address encoding overflow: left=%d needed=%d",
604 __func__
, buf_size
, src_encoding_min_len
);
609 pastend
= buf
+ buf_size
;
618 "%s: unknown source address encoding type=%d: %02x%02x%02x%02x",
619 __func__
, type
, buf
[0], buf
[1], buf
[2], buf
[3]);
624 case PIM_MSG_ADDRESS_FAMILY
:
625 if ((addr
+ sizeof(sg
->src
)) > pastend
) {
627 "%s: IP source address overflow: left=%td needed=%zu",
628 __func__
, pastend
- addr
, sizeof(sg
->src
));
632 memcpy(&sg
->src
, addr
, sizeof(sg
->src
));
635 RFC 4601: 4.9.1 Encoded Source and Group Address Formats
637 Encoded-Source Address
639 The mask length MUST be equal to the mask length in bits for
640 the given Address Family and Encoding Type (32 for IPv4
641 native and 128 for IPv6 native). A router SHOULD ignore any
642 messages received with any other mask length.
644 if (mask_len
!= PIM_MAX_BITLEN
) {
645 zlog_warn("%s: IP bad source address mask: %d",
650 addr
+= sizeof(sg
->src
);
655 "%s: unknown source address encoding family=%d: %02x%02x%02x%02x",
656 __func__
, family
, buf
[0], buf
[1], buf
[2], buf
[3]);
663 #define FREE_ADDR_LIST(hello_option_addr_list) \
665 if (hello_option_addr_list) { \
666 list_delete(&hello_option_addr_list); \
667 hello_option_addr_list = 0; \
671 int pim_tlv_parse_addr_list(const char *ifname
, pim_addr src_addr
,
672 pim_hello_options
*hello_options
,
673 struct list
**hello_option_addr_list
,
674 uint16_t option_len
, const uint8_t *tlv_curr
)
677 const uint8_t *pastend
;
679 assert(hello_option_addr_list
);
685 pastend
= tlv_curr
+ option_len
;
686 while (addr
< pastend
) {
687 struct prefix tmp
, src_pfx
;
694 pim_parse_addr_ucast_prefix(&tmp
, addr
, pastend
- addr
);
695 if (addr_offset
< 1) {
697 "%s: pim_parse_addr_ucast() failure: from %pPAs on %s",
698 __func__
, &src_addr
, ifname
);
699 FREE_ADDR_LIST(*hello_option_addr_list
);
707 if (PIM_DEBUG_PIM_TRACE
) {
708 switch (tmp
.family
) {
710 char addr_str
[INET_ADDRSTRLEN
];
711 pim_inet4_dump("<addr?>", tmp
.u
.prefix4
,
712 addr_str
, sizeof(addr_str
));
714 "%s: PIM hello TLV option: list_old_size=%d IPv4 address %s from %pPAs on %s",
716 *hello_option_addr_list
718 *hello_option_addr_list
))
720 addr_str
, &src_addr
, ifname
);
726 "%s: PIM hello TLV option: list_old_size=%d UNKNOWN address family from %pPAs on %s",
728 *hello_option_addr_list
730 *hello_option_addr_list
))
737 Exclude neighbor's primary address if incorrectly included in
738 the secondary address list
740 pim_addr_to_prefix(&src_pfx
, src_addr
);
741 if (!prefix_cmp(&tmp
, &src_pfx
)) {
743 "%s: ignoring primary address in secondary list from %pPAs on %s",
744 __func__
, &src_addr
, ifname
);
749 Allocate list if needed
751 if (!*hello_option_addr_list
) {
752 *hello_option_addr_list
= list_new();
753 (*hello_option_addr_list
)->del
= prefix_free_lists
;
762 prefix_copy(p
, &tmp
);
763 listnode_add(*hello_option_addr_list
, p
);
766 } /* while (addr < pastend) */
771 PIM_OPTION_SET(*hello_options
, PIM_OPTION_MASK_ADDRESS_LIST
);