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
29 #include "pim_instance.h"
35 #include "pim_iface.h"
38 #include "pim_register.h"
39 #include "pim_jp_agg.h"
42 void pim_msg_build_header(pim_addr src
, pim_addr dst
, uint8_t *pim_msg
,
43 size_t pim_msg_size
, uint8_t pim_msg_type
,
46 struct pim_msg_header
*header
= (struct pim_msg_header
*)pim_msg
;
47 struct iovec iov
[2], *iovp
= iov
;
50 * The checksum for Registers is done only on the first 8 bytes of the
51 * packet, including the PIM header and the next 4 bytes, excluding the
54 * for IPv6, the pseudoheader upper-level protocol length is also
55 * truncated, so let's just set it here before everything else.
57 if (pim_msg_type
== PIM_MSG_TYPE_REGISTER
)
58 pim_msg_size
= PIM_MSG_REGISTER_LEN
;
61 struct ipv6_ph phdr
= {
64 .ulpl
= htonl(pim_msg_size
),
65 .next_hdr
= IPPROTO_PIM
,
68 iovp
->iov_base
= &phdr
;
69 iovp
->iov_len
= sizeof(phdr
);
76 header
->ver
= PIM_PROTO_VERSION
;
77 header
->type
= pim_msg_type
;
78 header
->Nbit
= no_fwd
;
82 iovp
->iov_base
= header
;
83 iovp
->iov_len
= pim_msg_size
;
86 header
->checksum
= in_cksumv(iov
, iovp
- iov
);
89 uint8_t *pim_msg_addr_encode_ipv4_ucast(uint8_t *buf
, struct in_addr addr
)
91 buf
[0] = PIM_MSG_ADDRESS_FAMILY_IPV4
; /* addr family */
92 buf
[1] = '\0'; /* native encoding */
93 memcpy(buf
+ 2, &addr
, sizeof(struct in_addr
));
95 return buf
+ PIM_ENCODED_IPV4_UCAST_SIZE
;
98 uint8_t *pim_msg_addr_encode_ipv4_group(uint8_t *buf
, struct in_addr addr
)
100 buf
[0] = PIM_MSG_ADDRESS_FAMILY_IPV4
; /* addr family */
101 buf
[1] = '\0'; /* native encoding */
102 buf
[2] = '\0'; /* reserved */
103 buf
[3] = 32; /* mask len */
104 memcpy(buf
+ 4, &addr
, sizeof(struct in_addr
));
106 return buf
+ PIM_ENCODED_IPV4_GROUP_SIZE
;
109 uint8_t *pim_msg_addr_encode_ipv4_source(uint8_t *buf
, struct in_addr addr
,
112 buf
[0] = PIM_MSG_ADDRESS_FAMILY_IPV4
; /* addr family */
113 buf
[1] = '\0'; /* native encoding */
115 buf
[3] = 32; /* mask len */
116 memcpy(buf
+ 4, &addr
, sizeof(struct in_addr
));
118 return buf
+ PIM_ENCODED_IPV4_SOURCE_SIZE
;
121 uint8_t *pim_msg_addr_encode_ipv6_source(uint8_t *buf
, struct in6_addr addr
,
124 buf
[0] = PIM_MSG_ADDRESS_FAMILY_IPV6
; /* addr family */
125 buf
[1] = '\0'; /* native encoding */
127 buf
[3] = 128; /* mask len */
130 memcpy(buf
, &addr
, sizeof(addr
));
136 uint8_t *pim_msg_addr_encode_ipv6_ucast(uint8_t *buf
, struct in6_addr addr
)
138 buf
[0] = PIM_MSG_ADDRESS_FAMILY_IPV6
; /* addr family */
139 buf
[1] = '\0'; /* native encoding */
142 memcpy(buf
, &addr
, sizeof(addr
));
148 uint8_t *pim_msg_addr_encode_ipv6_group(uint8_t *buf
, struct in6_addr addr
)
150 buf
[0] = PIM_MSG_ADDRESS_FAMILY_IPV6
; /* addr family */
151 buf
[1] = '\0'; /* native encoding */
152 buf
[2] = '\0'; /* reserved */
153 buf
[3] = 128; /* mask len */
156 memcpy(buf
, &addr
, sizeof(addr
));
163 #define pim_msg_addr_encode(what) pim_msg_addr_encode_ipv4_##what
165 #define pim_msg_addr_encode(what) pim_msg_addr_encode_ipv6_##what
168 uint8_t *pim_msg_addr_encode_ucast(uint8_t *buf
, pim_addr addr
)
170 return pim_msg_addr_encode(ucast
)(buf
, addr
);
173 uint8_t *pim_msg_addr_encode_group(uint8_t *buf
, pim_addr addr
)
175 return pim_msg_addr_encode(group
)(buf
, addr
);
178 uint8_t *pim_msg_addr_encode_source(uint8_t *buf
, pim_addr addr
, uint8_t bits
)
180 return pim_msg_addr_encode(source
)(buf
, addr
, bits
);
184 * For the given 'struct pim_jp_sources' list
185 * determine the size_t it would take up.
187 size_t pim_msg_get_jp_group_size(struct list
*sources
)
189 struct pim_jp_sources
*js
;
195 size
+= sizeof(pim_encoded_group
);
196 size
+= 4; // Joined sources (2) + Pruned Sources (2)
198 size
+= sizeof(pim_encoded_source
) * sources
->count
;
200 js
= listgetdata(listhead(sources
));
201 if (js
&& pim_addr_is_any(js
->up
->sg
.src
) && js
->is_join
) {
202 struct pim_upstream
*child
, *up
;
203 struct listnode
*up_node
;
206 if (PIM_DEBUG_PIM_PACKETS
)
208 "%s: Considering (%s) children for (S,G,rpt) prune",
209 __func__
, up
->sg_str
);
211 for (ALL_LIST_ELEMENTS_RO(up
->sources
, up_node
, child
)) {
212 if (!PIM_UPSTREAM_FLAG_TEST_USE_RPT(child
->flags
)) {
213 /* If we are using SPT and the SPT and RPT IIFs
214 * are different we can prune the source off
216 * If RPF_interface(S) is not resolved hold
217 * decision to prune as SPT may end up on the
218 * same IIF as RPF_interface(RP).
220 if (child
->rpf
.source_nexthop
.interface
&&
221 !pim_rpf_is_same(&up
->rpf
,
223 size
+= sizeof(pim_encoded_source
);
224 PIM_UPSTREAM_FLAG_SET_SEND_SG_RPT_PRUNE(
226 if (PIM_DEBUG_PIM_PACKETS
)
228 "%s: SPT Bit and RPF'(%s) != RPF'(S,G): Add Prune (%s,rpt) to compound message",
229 __func__
, up
->sg_str
,
231 } else if (PIM_DEBUG_PIM_PACKETS
)
233 "%s: SPT Bit and RPF'(%s) == RPF'(S,G): Not adding Prune for (%s,rpt)",
234 __func__
, up
->sg_str
,
236 } else if (pim_upstream_empty_inherited_olist(child
)) {
237 /* S is supposed to be forwarded along the RPT
238 * but it's inherited OIL is empty. So just
241 size
+= sizeof(pim_encoded_source
);
242 PIM_UPSTREAM_FLAG_SET_SEND_SG_RPT_PRUNE(
244 if (PIM_DEBUG_PIM_PACKETS
)
246 "%s: inherited_olist(%s,rpt) is NULL, Add Prune to compound message",
247 __func__
, child
->sg_str
);
248 } else if (PIM_DEBUG_PIM_PACKETS
)
250 "%s: Do not add Prune %s to compound message %s",
251 __func__
, child
->sg_str
, up
->sg_str
);
257 size_t pim_msg_build_jp_groups(struct pim_jp_groups
*grp
,
258 struct pim_jp_agg_group
*sgs
, size_t size
)
260 struct listnode
*node
, *nnode
;
261 struct pim_jp_sources
*source
;
262 struct pim_upstream
*up
= NULL
;
267 memset(grp
, 0, size
);
268 pim_msg_addr_encode_group((uint8_t *)&grp
->g
, sgs
->group
);
270 for (ALL_LIST_ELEMENTS(sgs
->sources
, node
, nnode
, source
)) {
271 /* number of joined/pruned sources */
277 if (pim_addr_is_any(source
->up
->sg
.src
)) {
278 struct pim_instance
*pim
= source
->up
->channel_oil
->pim
;
279 struct pim_rpf
*rpf
= pim_rp_g(pim
, source
->up
->sg
.grp
);
280 bits
= PIM_ENCODE_SPARSE_BIT
| PIM_ENCODE_WC_BIT
281 | PIM_ENCODE_RPT_BIT
;
282 stosend
= rpf
->rpf_addr
;
283 /* Only Send SGRpt in case of *,G Join */
287 bits
= PIM_ENCODE_SPARSE_BIT
;
288 stosend
= source
->up
->sg
.src
;
291 pim_msg_addr_encode_source((uint8_t *)&grp
->s
[tgroups
], stosend
,
297 struct pim_upstream
*child
;
299 for (ALL_LIST_ELEMENTS(up
->sources
, node
, nnode
, child
)) {
300 if (PIM_UPSTREAM_FLAG_TEST_SEND_SG_RPT_PRUNE(
302 pim_msg_addr_encode_source(
303 (uint8_t *)&grp
->s
[tgroups
],
305 PIM_ENCODE_SPARSE_BIT
|
308 PIM_UPSTREAM_FLAG_UNSET_SEND_SG_RPT_PRUNE(
315 grp
->joins
= htons(grp
->joins
);
316 grp
->prunes
= htons(grp
->prunes
);