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 #define PIM_MSG_ADDRESS_FAMILY PIM_MSG_ADDRESS_FAMILY_IPV4
35 #define PIM_MSG_ADDRESS_FAMILY PIM_MSG_ADDRESS_FAMILY_IPV6
38 uint8_t *pim_tlv_append_uint16(uint8_t *buf
, const uint8_t *buf_pastend
,
39 uint16_t option_type
, uint16_t option_value
)
41 uint16_t option_len
= 2;
43 if ((buf
+ PIM_TLV_OPTION_SIZE(option_len
)) > buf_pastend
)
46 *(uint16_t *)buf
= htons(option_type
);
48 *(uint16_t *)buf
= htons(option_len
);
50 *(uint16_t *)buf
= htons(option_value
);
56 uint8_t *pim_tlv_append_2uint16(uint8_t *buf
, const uint8_t *buf_pastend
,
57 uint16_t option_type
, uint16_t option_value1
,
58 uint16_t option_value2
)
60 uint16_t option_len
= 4;
62 if ((buf
+ PIM_TLV_OPTION_SIZE(option_len
)) > buf_pastend
)
65 *(uint16_t *)buf
= htons(option_type
);
67 *(uint16_t *)buf
= htons(option_len
);
69 *(uint16_t *)buf
= htons(option_value1
);
71 *(uint16_t *)buf
= htons(option_value2
);
77 uint8_t *pim_tlv_append_uint32(uint8_t *buf
, const uint8_t *buf_pastend
,
78 uint16_t option_type
, uint32_t option_value
)
80 uint16_t option_len
= 4;
82 if ((buf
+ PIM_TLV_OPTION_SIZE(option_len
)) > buf_pastend
)
85 *(uint16_t *)buf
= htons(option_type
);
87 *(uint16_t *)buf
= htons(option_len
);
89 pim_write_uint32(buf
, option_value
);
95 #define ucast_ipv4_encoding_len (2 + sizeof(struct in_addr))
96 #define ucast_ipv6_encoding_len (2 + sizeof(struct in6_addr))
99 * An Encoded-Unicast address takes the following format:
102 * 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
103 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104 * | Addr Family | Encoding Type | Unicast Address
105 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+...
108 * The PIM address family of the 'Unicast Address' field of this
111 * Values 0-127 are as assigned by the IANA for Internet Address *
112 * Families in [7]. Values 128-250 are reserved to be assigned by
113 * the IANA for PIM-specific Address Families. Values 251 though
114 * 255 are designated for private use. As there is no assignment
115 * authority for this space, collisions should be expected.
118 * The type of encoding used within a specific Address Family. The
119 * value '0' is reserved for this field and represents the native
120 * encoding of the Address Family.
123 * The unicast address as represented by the given Address Family
126 int pim_encode_addr_ucast(uint8_t *buf
, pim_addr addr
)
128 uint8_t *start
= buf
;
130 *buf
++ = PIM_MSG_ADDRESS_FAMILY
;
132 memcpy(buf
, &addr
, sizeof(addr
));
138 int pim_encode_addr_ucast_prefix(uint8_t *buf
, struct prefix
*p
)
142 *buf
= PIM_MSG_ADDRESS_FAMILY_IPV4
; /* notice: AF_INET !=
143 PIM_MSG_ADDRESS_FAMILY_IPV4
146 *buf
= 0; /* ucast IPv4 native encoding type (RFC
149 memcpy(buf
, &p
->u
.prefix4
, sizeof(struct in_addr
));
150 return ucast_ipv4_encoding_len
;
152 *buf
= PIM_MSG_ADDRESS_FAMILY_IPV6
;
156 memcpy(buf
, &p
->u
.prefix6
, sizeof(struct in6_addr
));
157 return ucast_ipv6_encoding_len
;
163 #define group_ipv4_encoding_len (4 + sizeof(struct in_addr))
166 * Encoded-Group addresses take the following format:
169 * 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
170 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
171 * | Addr Family | Encoding Type |B| Reserved |Z| Mask Len |
172 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
173 * | Group multicast Address
174 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+...
182 * [B]idirectional PIM
183 * Indicates the group range should use Bidirectional PIM [13].
184 * For PIM-SM defined in this specification, this bit MUST be zero.
187 * Transmitted as zero. Ignored upon receipt.
190 * indicates the group range is an admin scope zone. This is used
191 * in the Bootstrap Router Mechanism [11] only. For all other
192 * purposes, this bit is set to zero and ignored on receipt.
195 * The Mask length field is 8 bits. The value is the number of
196 * contiguous one bits that are left justified and used as a mask;
197 * when combined with the group address, it describes a range of
198 * groups. It is less than or equal to the address length in bits
199 * for the given Address Family and Encoding Type. If the message
200 * is sent for a single group, then the Mask length must equal the
201 * address length in bits for the given Address Family and Encoding
202 * Type (e.g., 32 for IPv4 native encoding, 128 for IPv6 native
205 * Group multicast Address
206 * Contains the group address.
208 int pim_encode_addr_group(uint8_t *buf
, afi_t afi
, int bidir
, int scope
,
211 uint8_t *start
= buf
;
217 *buf
++ = PIM_MSG_ADDRESS_FAMILY
;
220 *buf
++ = sizeof(group
) / 8;
221 memcpy(buf
, &group
, sizeof(group
));
222 buf
+= sizeof(group
);
227 uint8_t *pim_tlv_append_addrlist_ucast(uint8_t *buf
, const uint8_t *buf_pastend
,
228 struct list
*ifconnected
, int family
)
230 struct listnode
*node
;
231 uint16_t option_len
= 0;
235 node
= listhead(ifconnected
);
237 /* Empty address list ? */
242 if (family
== AF_INET
)
243 uel
= ucast_ipv4_encoding_len
;
245 uel
= ucast_ipv6_encoding_len
;
247 /* Scan secondary address list */
248 curr
= buf
+ 4; /* skip T and L */
249 for (; node
; node
= listnextnode(node
)) {
250 struct connected
*ifc
= listgetdata(node
);
251 struct prefix
*p
= ifc
->address
;
254 if (!CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
))
257 if ((curr
+ uel
) > buf_pastend
)
260 if (p
->family
!= family
)
263 l_encode
= pim_encode_addr_ucast_prefix(curr
, p
);
265 option_len
+= l_encode
;
268 if (PIM_DEBUG_PIM_TRACE_DETAIL
) {
270 "%s: number of encoded secondary unicast IPv4 addresses: %zu",
271 __func__
, option_len
/ uel
);
274 if (option_len
< 1) {
275 /* Empty secondary unicast IPv4 address list */
282 *(uint16_t *)buf
= htons(PIM_MSG_OPTION_TYPE_ADDRESS_LIST
);
283 *(uint16_t *)(buf
+ 2) = htons(option_len
);
288 static int check_tlv_length(const char *label
, const char *tlv_name
,
289 const char *ifname
, pim_addr src_addr
,
290 int correct_len
, int option_len
)
292 if (option_len
!= correct_len
) {
294 "%s: PIM hello %s TLV with incorrect value size=%d correct=%d from %pPAs on interface %s",
295 label
, tlv_name
, option_len
, correct_len
, &src_addr
,
303 static void check_tlv_redefinition_uint16(const char *label
,
304 const char *tlv_name
,
305 const char *ifname
, pim_addr src_addr
,
306 pim_hello_options options
,
307 pim_hello_options opt_mask
,
308 uint16_t new, uint16_t old
)
310 if (PIM_OPTION_IS_SET(options
, opt_mask
))
312 "%s: PIM hello TLV redefined %s=%u old=%u from %pPAs on interface %s",
313 label
, tlv_name
, new, old
, &src_addr
, ifname
);
316 static void check_tlv_redefinition_uint32(const char *label
,
317 const char *tlv_name
,
318 const char *ifname
, pim_addr src_addr
,
319 pim_hello_options options
,
320 pim_hello_options opt_mask
,
321 uint32_t new, uint32_t old
)
323 if (PIM_OPTION_IS_SET(options
, opt_mask
))
325 "%s: PIM hello TLV redefined %s=%u old=%u from %pPAs on interface %s",
326 label
, tlv_name
, new, old
, &src_addr
, ifname
);
329 static void check_tlv_redefinition_uint32_hex(
330 const char *label
, const char *tlv_name
, const char *ifname
,
331 pim_addr src_addr
, pim_hello_options options
,
332 pim_hello_options opt_mask
, uint32_t new, uint32_t old
)
334 if (PIM_OPTION_IS_SET(options
, opt_mask
))
336 "%s: PIM hello TLV redefined %s=%08x old=%08x from %pPAs on interface %s",
337 label
, tlv_name
, new, old
, &src_addr
, ifname
);
340 int pim_tlv_parse_holdtime(const char *ifname
, pim_addr src_addr
,
341 pim_hello_options
*hello_options
,
342 uint16_t *hello_option_holdtime
, uint16_t option_len
,
343 const uint8_t *tlv_curr
)
345 const char *label
= "holdtime";
347 if (check_tlv_length(__func__
, label
, ifname
, src_addr
,
348 sizeof(uint16_t), option_len
)) {
352 check_tlv_redefinition_uint16(__func__
, label
, ifname
, src_addr
,
353 *hello_options
, PIM_OPTION_MASK_HOLDTIME
,
354 PIM_TLV_GET_HOLDTIME(tlv_curr
),
355 *hello_option_holdtime
);
357 PIM_OPTION_SET(*hello_options
, PIM_OPTION_MASK_HOLDTIME
);
359 *hello_option_holdtime
= PIM_TLV_GET_HOLDTIME(tlv_curr
);
364 int pim_tlv_parse_lan_prune_delay(const char *ifname
, pim_addr src_addr
,
365 pim_hello_options
*hello_options
,
366 uint16_t *hello_option_propagation_delay
,
367 uint16_t *hello_option_override_interval
,
368 uint16_t option_len
, const uint8_t *tlv_curr
)
370 if (check_tlv_length(__func__
, "lan_prune_delay", ifname
, src_addr
,
371 sizeof(uint32_t), option_len
)) {
375 check_tlv_redefinition_uint16(__func__
, "propagation_delay", ifname
,
376 src_addr
, *hello_options
,
377 PIM_OPTION_MASK_LAN_PRUNE_DELAY
,
378 PIM_TLV_GET_PROPAGATION_DELAY(tlv_curr
),
379 *hello_option_propagation_delay
);
381 PIM_OPTION_SET(*hello_options
, PIM_OPTION_MASK_LAN_PRUNE_DELAY
);
383 *hello_option_propagation_delay
=
384 PIM_TLV_GET_PROPAGATION_DELAY(tlv_curr
);
385 if (PIM_TLV_GET_CAN_DISABLE_JOIN_SUPPRESSION(tlv_curr
)) {
386 PIM_OPTION_SET(*hello_options
,
387 PIM_OPTION_MASK_CAN_DISABLE_JOIN_SUPPRESSION
);
389 PIM_OPTION_UNSET(*hello_options
,
390 PIM_OPTION_MASK_CAN_DISABLE_JOIN_SUPPRESSION
);
394 *hello_option_override_interval
=
395 PIM_TLV_GET_OVERRIDE_INTERVAL(tlv_curr
);
400 int pim_tlv_parse_dr_priority(const char *ifname
, pim_addr src_addr
,
401 pim_hello_options
*hello_options
,
402 uint32_t *hello_option_dr_priority
,
403 uint16_t option_len
, const uint8_t *tlv_curr
)
405 const char *label
= "dr_priority";
407 if (check_tlv_length(__func__
, label
, ifname
, src_addr
,
408 sizeof(uint32_t), option_len
)) {
412 check_tlv_redefinition_uint32(
413 __func__
, label
, ifname
, src_addr
, *hello_options
,
414 PIM_OPTION_MASK_DR_PRIORITY
, PIM_TLV_GET_DR_PRIORITY(tlv_curr
),
415 *hello_option_dr_priority
);
417 PIM_OPTION_SET(*hello_options
, PIM_OPTION_MASK_DR_PRIORITY
);
419 *hello_option_dr_priority
= PIM_TLV_GET_DR_PRIORITY(tlv_curr
);
424 int pim_tlv_parse_generation_id(const char *ifname
, pim_addr src_addr
,
425 pim_hello_options
*hello_options
,
426 uint32_t *hello_option_generation_id
,
427 uint16_t option_len
, const uint8_t *tlv_curr
)
429 const char *label
= "generation_id";
431 if (check_tlv_length(__func__
, label
, ifname
, src_addr
,
432 sizeof(uint32_t), option_len
)) {
436 check_tlv_redefinition_uint32_hex(__func__
, label
, ifname
, src_addr
,
438 PIM_OPTION_MASK_GENERATION_ID
,
439 PIM_TLV_GET_GENERATION_ID(tlv_curr
),
440 *hello_option_generation_id
);
442 PIM_OPTION_SET(*hello_options
, PIM_OPTION_MASK_GENERATION_ID
);
444 *hello_option_generation_id
= PIM_TLV_GET_GENERATION_ID(tlv_curr
);
449 int pim_parse_addr_ucast_prefix(struct prefix
*p
, const uint8_t *buf
,
452 const int ucast_encoding_min_len
= 3; /* 1 family + 1 type + 1 addr */
454 const uint8_t *pastend
;
458 if (buf_size
< ucast_encoding_min_len
) {
460 "%s: unicast address encoding overflow: left=%d needed=%d",
461 __func__
, buf_size
, ucast_encoding_min_len
);
466 pastend
= buf
+ buf_size
;
472 zlog_warn("%s: unknown unicast address encoding type=%d",
478 case PIM_MSG_ADDRESS_FAMILY_IPV4
:
479 if ((addr
+ sizeof(struct in_addr
)) > pastend
) {
481 "%s: IPv4 unicast address overflow: left=%td needed=%zu",
482 __func__
, pastend
- addr
,
483 sizeof(struct in_addr
));
487 p
->family
= AF_INET
; /* notice: AF_INET !=
488 PIM_MSG_ADDRESS_FAMILY_IPV4 */
489 memcpy(&p
->u
.prefix4
, addr
, sizeof(struct in_addr
));
490 p
->prefixlen
= IPV4_MAX_BITLEN
;
491 addr
+= sizeof(struct in_addr
);
494 case PIM_MSG_ADDRESS_FAMILY_IPV6
:
495 if ((addr
+ sizeof(struct in6_addr
)) > pastend
) {
497 "%s: IPv6 unicast address overflow: left=%td needed %zu",
498 __func__
, pastend
- addr
,
499 sizeof(struct in6_addr
));
503 p
->family
= AF_INET6
;
504 p
->prefixlen
= IPV6_MAX_BITLEN
;
505 memcpy(&p
->u
.prefix6
, addr
, sizeof(struct in6_addr
));
506 addr
+= sizeof(struct in6_addr
);
510 zlog_warn("%s: unknown unicast address encoding family=%d from",
519 int pim_parse_addr_ucast(pim_addr
*out
, const uint8_t *buf
, int buf_size
,
525 ret
= pim_parse_addr_ucast_prefix(&p
, buf
, buf_size
);
529 if (p
.family
!= PIM_AF
) {
534 memcpy(out
, &p
.u
.val
, sizeof(*out
));
538 int pim_parse_addr_group(pim_sgaddr
*sg
, const uint8_t *buf
, int buf_size
)
540 const int grp_encoding_min_len
=
541 4; /* 1 family + 1 type + 1 reserved + 1 addr */
543 const uint8_t *pastend
;
548 if (buf_size
< grp_encoding_min_len
) {
550 "%s: group address encoding overflow: left=%d needed=%d",
551 __func__
, buf_size
, grp_encoding_min_len
);
556 pastend
= buf
+ buf_size
;
560 ++addr
; /* skip b_reserved_z fields */
564 zlog_warn("%s: unknown group address encoding type=%d from",
569 if (family
!= PIM_MSG_ADDRESS_FAMILY
) {
571 "%s: unknown group address encoding family=%d mask_len=%d from",
572 __func__
, family
, mask_len
);
576 if ((addr
+ sizeof(sg
->grp
)) > pastend
) {
578 "%s: group address overflow: left=%td needed=%zu from",
579 __func__
, pastend
- addr
, sizeof(sg
->grp
));
583 memcpy(&sg
->grp
, addr
, sizeof(sg
->grp
));
584 addr
+= sizeof(sg
->grp
);
589 int pim_parse_addr_source(pim_sgaddr
*sg
, uint8_t *flags
, const uint8_t *buf
,
592 const int src_encoding_min_len
=
593 4; /* 1 family + 1 type + 1 reserved + 1 addr */
595 const uint8_t *pastend
;
600 if (buf_size
< src_encoding_min_len
) {
602 "%s: source address encoding overflow: left=%d needed=%d",
603 __func__
, buf_size
, src_encoding_min_len
);
608 pastend
= buf
+ buf_size
;
617 "%s: unknown source address encoding type=%d: %02x%02x%02x%02x",
618 __func__
, type
, buf
[0], buf
[1], buf
[2], buf
[3]);
623 case PIM_MSG_ADDRESS_FAMILY
:
624 if ((addr
+ sizeof(sg
->src
)) > pastend
) {
626 "%s: IP source address overflow: left=%td needed=%zu",
627 __func__
, pastend
- addr
, sizeof(sg
->src
));
631 memcpy(&sg
->src
, addr
, sizeof(sg
->src
));
634 RFC 4601: 4.9.1 Encoded Source and Group Address Formats
636 Encoded-Source Address
638 The mask length MUST be equal to the mask length in bits for
639 the given Address Family and Encoding Type (32 for IPv4
640 native and 128 for IPv6 native). A router SHOULD ignore any
641 messages received with any other mask length.
643 if (mask_len
!= PIM_MAX_BITLEN
) {
644 zlog_warn("%s: IP bad source address mask: %d",
649 addr
+= sizeof(sg
->src
);
654 "%s: unknown source address encoding family=%d: %02x%02x%02x%02x",
655 __func__
, family
, buf
[0], buf
[1], buf
[2], buf
[3]);
662 #define FREE_ADDR_LIST(hello_option_addr_list) \
664 if (hello_option_addr_list) { \
665 list_delete(&hello_option_addr_list); \
666 hello_option_addr_list = 0; \
670 int pim_tlv_parse_addr_list(const char *ifname
, pim_addr src_addr
,
671 pim_hello_options
*hello_options
,
672 struct list
**hello_option_addr_list
,
673 uint16_t option_len
, const uint8_t *tlv_curr
)
676 const uint8_t *pastend
;
678 assert(hello_option_addr_list
);
684 pastend
= tlv_curr
+ option_len
;
685 while (addr
< pastend
) {
686 struct prefix tmp
, src_pfx
;
693 pim_parse_addr_ucast_prefix(&tmp
, addr
, pastend
- addr
);
694 if (addr_offset
< 1) {
696 "%s: pim_parse_addr_ucast() failure: from %pPAs on %s",
697 __func__
, &src_addr
, ifname
);
698 FREE_ADDR_LIST(*hello_option_addr_list
);
706 if (PIM_DEBUG_PIM_TRACE
) {
707 switch (tmp
.family
) {
709 char addr_str
[INET_ADDRSTRLEN
];
710 pim_inet4_dump("<addr?>", tmp
.u
.prefix4
,
711 addr_str
, sizeof(addr_str
));
713 "%s: PIM hello TLV option: list_old_size=%d IPv4 address %s from %pPAs on %s",
715 *hello_option_addr_list
717 *hello_option_addr_list
))
719 addr_str
, &src_addr
, ifname
);
725 "%s: PIM hello TLV option: list_old_size=%d UNKNOWN address family from %pPAs on %s",
727 *hello_option_addr_list
729 *hello_option_addr_list
))
736 Exclude neighbor's primary address if incorrectly included in
737 the secondary address list
739 pim_addr_to_prefix(&src_pfx
, src_addr
);
740 if (!prefix_cmp(&tmp
, &src_pfx
)) {
742 "%s: ignoring primary address in secondary list from %pPAs on %s",
743 __func__
, &src_addr
, ifname
);
748 Allocate list if needed
750 if (!*hello_option_addr_list
) {
751 *hello_option_addr_list
= list_new();
752 (*hello_option_addr_list
)->del
= prefix_free_lists
;
761 prefix_copy(p
, &tmp
);
762 listnode_add(*hello_option_addr_list
, p
);
765 } /* while (addr < pastend) */
770 PIM_OPTION_SET(*hello_options
, PIM_OPTION_MASK_ADDRESS_LIST
);