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
23 #include <netinet/in.h>
25 #include <netinet/ip6.h>
28 #include "pim_jp_agg.h"
30 #define PIM_HDR_LEN sizeof(struct pim_msg_header)
33 ---------- ------------------
39 http://www.iana.org/assignments/address-family-numbers
41 enum pim_msg_address_family
{
42 PIM_MSG_ADDRESS_FAMILY_RESERVED
,
43 PIM_MSG_ADDRESS_FAMILY_IPV4
,
44 PIM_MSG_ADDRESS_FAMILY_IPV6
,
49 * =========================
50 * PIM Header definition as per RFC 5059. N bit introduced to indicate
51 * do-not-forward option in PIM Boot strap Message.
53 * 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
54 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 * |PIM Ver| Type |N| Reserved | Checksum |
56 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 struct pim_msg_header
{
59 #if (BYTE_ORDER == LITTLE_ENDIAN)
63 uint8_t Nbit
: 1; /* No Fwd Bit */
64 #elif (BYTE_ORDER == BIG_ENDIAN)
67 uint8_t Nbit
: 1; /* No Fwd Bit */
70 #error"Please set byte order"
73 } __attribute__((packed
));
75 struct pim_encoded_ipv4_unicast
{
79 } __attribute__((packed
));
81 struct pim_encoded_ipv6_unicast
{
85 } __attribute__((packed
));
88 * Encoded Group format. RFC 4601 Sec 4.9.1
90 * 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
91 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92 * | Addr Family | Encoding Type |B| Reserved |Z| Mask Len |
93 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 * | Group multicast Address
95 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+...
97 struct pim_encoded_group_ipv4
{
100 #if (BYTE_ORDER == LITTLE_ENDIAN)
101 uint8_t sz
: 1; /* scope zone bit */
102 uint8_t reserved
: 6; /* Reserved */
103 uint8_t bidir
: 1; /* Bidir bit */
104 #elif (BYTE_ORDER == BIG_ENDIAN)
105 uint8_t bidir
: 1; /* Bidir bit */
106 uint8_t reserved
: 6; /* Reserved */
107 uint8_t sz
: 1; /* scope zone bit */
109 #error"Please set byte order"
113 } __attribute__((packed
));
115 struct pim_encoded_group_ipv6
{
118 #if (BYTE_ORDER == LITTLE_ENDIAN)
119 uint8_t sz
: 1; /* scope zone bit */
120 uint8_t reserved
: 6; /* Reserved */
121 uint8_t bidir
: 1; /* Bidir bit */
122 #elif (BYTE_ORDER == BIG_ENDIAN)
123 uint8_t bidir
: 1; /* Bidir bit */
124 uint8_t reserved
: 6; /* Reserved */
125 uint8_t sz
: 1; /* scope zone bit */
127 #error "Please set byte order"
130 struct in6_addr addr
;
131 } __attribute__((packed
));
134 * Encoded Source format. RFC 4601 Sec 4.9.1
136 * 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
137 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
138 * | Addr Family | Encoding Type | Rsrvd |S|W|R| Mask Len |
139 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
141 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-...
143 struct pim_encoded_source_ipv4
{
149 } __attribute__((packed
));
151 struct pim_encoded_source_ipv6
{
156 struct in6_addr addr
;
157 } __attribute__((packed
));
159 /* clang-format off */
161 typedef struct pim_encoded_ipv4_unicast pim_encoded_unicast
;
162 typedef struct pim_encoded_group_ipv4 pim_encoded_group
;
163 typedef struct pim_encoded_source_ipv4 pim_encoded_source
;
165 typedef struct pim_encoded_ipv6_unicast pim_encoded_unicast
;
166 typedef struct pim_encoded_group_ipv6 pim_encoded_group
;
167 typedef struct pim_encoded_source_ipv6 pim_encoded_source
;
169 /* clang-format on */
171 struct pim_jp_groups
{
175 pim_encoded_source s
[1];
176 } __attribute__((packed
));
179 struct pim_msg_header header
;
180 pim_encoded_unicast addr
;
184 struct pim_jp_groups groups
[1];
185 } __attribute__((packed
));
188 static inline pim_sgaddr
pim_sgaddr_from_iphdr(const void *iphdr
)
190 const struct ip
*ipv4_hdr
= iphdr
;
193 sg
.src
= ipv4_hdr
->ip_src
;
194 sg
.grp
= ipv4_hdr
->ip_dst
;
199 static inline pim_sgaddr
pim_sgaddr_from_iphdr(const void *iphdr
)
201 const struct ip6_hdr
*ipv6_hdr
= iphdr
;
204 sg
.src
= ipv6_hdr
->ip6_src
;
205 sg
.grp
= ipv6_hdr
->ip6_dst
;
211 void pim_msg_build_header(uint8_t *pim_msg
, size_t pim_msg_size
,
212 uint8_t pim_msg_type
, bool no_fwd
);
213 uint8_t *pim_msg_addr_encode_ipv4_ucast(uint8_t *buf
, struct in_addr addr
);
214 uint8_t *pim_msg_addr_encode_ipv4_group(uint8_t *buf
, struct in_addr addr
);
216 #define PIM_ENCODE_SPARSE_BIT 0x04
217 #define PIM_ENCODE_WC_BIT 0x02
218 #define PIM_ENCODE_RPT_BIT 0x01
219 uint8_t *pim_msg_addr_encode_ipv4_source(uint8_t *buf
, struct in_addr addr
,
222 uint8_t *pim_msg_addr_encode_ipv6_ucast(uint8_t *buf
, struct in6_addr addr
);
223 uint8_t *pim_msg_addr_encode_ipv6_group(uint8_t *buf
, struct in6_addr addr
);
224 uint8_t *pim_msg_addr_encode_ipv6_source(uint8_t *buf
, struct in6_addr addr
,
227 uint8_t *pim_msg_addr_encode_ucast(uint8_t *buf
, pim_addr addr
);
228 uint8_t *pim_msg_addr_encode_group(uint8_t *buf
, pim_addr addr
);
229 uint8_t *pim_msg_addr_encode_source(uint8_t *buf
, pim_addr addr
, uint8_t bits
);
231 size_t pim_msg_get_jp_group_size(struct list
*sources
);
232 size_t pim_msg_build_jp_groups(struct pim_jp_groups
*grp
,
233 struct pim_jp_agg_group
*sgs
, size_t size
);
234 #endif /* PIM_MSG_H */