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
34 #include "pim_iface.h"
37 #include "pim_register.h"
38 #include "pim_jp_agg.h"
41 void pim_msg_build_header(uint8_t *pim_msg
, size_t pim_msg_size
,
42 uint8_t pim_msg_type
, bool no_fwd
)
44 struct pim_msg_header
*header
= (struct pim_msg_header
*)pim_msg
;
49 header
->ver
= PIM_PROTO_VERSION
;
50 header
->type
= pim_msg_type
;
51 header
->Nbit
= no_fwd
;
57 * The checksum for Registers is done only on the first 8 bytes of the
59 * including the PIM header and the next 4 bytes, excluding the data
62 if (pim_msg_type
== PIM_MSG_TYPE_REGISTER
)
63 header
->checksum
= in_cksum(pim_msg
, PIM_MSG_REGISTER_LEN
);
65 header
->checksum
= in_cksum(pim_msg
, pim_msg_size
);
68 uint8_t *pim_msg_addr_encode_ipv4_ucast(uint8_t *buf
, struct in_addr addr
)
70 buf
[0] = PIM_MSG_ADDRESS_FAMILY_IPV4
; /* addr family */
71 buf
[1] = '\0'; /* native encoding */
72 memcpy(buf
+ 2, &addr
, sizeof(struct in_addr
));
74 return buf
+ PIM_ENCODED_IPV4_UCAST_SIZE
;
77 uint8_t *pim_msg_addr_encode_ipv4_group(uint8_t *buf
, struct in_addr addr
)
79 buf
[0] = PIM_MSG_ADDRESS_FAMILY_IPV4
; /* addr family */
80 buf
[1] = '\0'; /* native encoding */
81 buf
[2] = '\0'; /* reserved */
82 buf
[3] = 32; /* mask len */
83 memcpy(buf
+ 4, &addr
, sizeof(struct in_addr
));
85 return buf
+ PIM_ENCODED_IPV4_GROUP_SIZE
;
88 uint8_t *pim_msg_addr_encode_ipv4_source(uint8_t *buf
, struct in_addr addr
,
91 buf
[0] = PIM_MSG_ADDRESS_FAMILY_IPV4
; /* addr family */
92 buf
[1] = '\0'; /* native encoding */
94 buf
[3] = 32; /* mask len */
95 memcpy(buf
+ 4, &addr
, sizeof(struct in_addr
));
97 return buf
+ PIM_ENCODED_IPV4_SOURCE_SIZE
;
101 * For the given 'struct pim_jp_sources' list
102 * determine the size_t it would take up.
104 size_t pim_msg_get_jp_group_size(struct list
*sources
)
106 struct pim_jp_sources
*js
;
112 size
+= sizeof(struct pim_encoded_group_ipv4
);
113 size
+= 4; // Joined sources (2) + Pruned Sources (2)
115 size
+= sizeof(struct pim_encoded_source_ipv4
) * sources
->count
;
117 js
= listgetdata(listhead(sources
));
118 if (js
&& js
->up
->sg
.src
.s_addr
== INADDR_ANY
&& js
->is_join
) {
119 struct pim_upstream
*child
, *up
;
120 struct listnode
*up_node
;
123 if (PIM_DEBUG_PIM_PACKETS
)
125 "%s: Considering (%s) children for (S,G,rpt) prune",
126 __PRETTY_FUNCTION__
, up
->sg_str
);
128 for (ALL_LIST_ELEMENTS_RO(up
->sources
, up_node
, child
)) {
129 if (child
->sptbit
== PIM_UPSTREAM_SPTBIT_TRUE
) {
130 if (!pim_rpf_is_same(&up
->rpf
, &child
->rpf
)) {
132 struct pim_encoded_source_ipv4
);
133 PIM_UPSTREAM_FLAG_SET_SEND_SG_RPT_PRUNE(
135 if (PIM_DEBUG_PIM_PACKETS
)
137 "%s: SPT Bit and RPF'(%s) != RPF'(S,G): Add Prune (%s,rpt) to compound message",
141 } else if (PIM_DEBUG_PIM_PACKETS
)
143 "%s: SPT Bit and RPF'(%s) == RPF'(S,G): Not adding Prune for (%s,rpt)",
144 __PRETTY_FUNCTION__
, up
->sg_str
,
146 } else if (pim_upstream_is_sg_rpt(child
)) {
147 if (pim_upstream_empty_inherited_olist(child
)) {
149 struct pim_encoded_source_ipv4
);
150 PIM_UPSTREAM_FLAG_SET_SEND_SG_RPT_PRUNE(
152 if (PIM_DEBUG_PIM_PACKETS
)
154 "%s: inherited_olist(%s,rpt) is NULL, Add Prune to compound message",
157 } else if (!pim_rpf_is_same(&up
->rpf
,
160 struct pim_encoded_source_ipv4
);
161 PIM_UPSTREAM_FLAG_SET_SEND_SG_RPT_PRUNE(
163 if (PIM_DEBUG_PIM_PACKETS
)
165 "%s: RPF'(%s) != RPF'(%s,rpt), Add Prune to compound message",
169 } else if (PIM_DEBUG_PIM_PACKETS
)
171 "%s: RPF'(%s) == RPF'(%s,rpt), Do not add Prune to compound message",
172 __PRETTY_FUNCTION__
, up
->sg_str
,
174 } else if (PIM_DEBUG_PIM_PACKETS
)
175 zlog_debug("%s: SPT bit is not set for (%s)",
176 __PRETTY_FUNCTION__
, child
->sg_str
);
182 size_t pim_msg_build_jp_groups(struct pim_jp_groups
*grp
,
183 struct pim_jp_agg_group
*sgs
, size_t size
)
185 struct listnode
*node
, *nnode
;
186 struct pim_jp_sources
*source
;
187 struct pim_upstream
*up
= NULL
;
188 struct in_addr stosend
;
192 memset(grp
, 0, size
);
193 pim_msg_addr_encode_ipv4_group((uint8_t *)&grp
->g
, sgs
->group
);
195 for (ALL_LIST_ELEMENTS(sgs
->sources
, node
, nnode
, source
)) {
196 /* number of joined/pruned sources */
202 if (source
->up
->sg
.src
.s_addr
== INADDR_ANY
) {
203 struct pim_instance
*pim
= source
->up
->channel_oil
->pim
;
204 struct pim_rpf
*rpf
= pim_rp_g(pim
, source
->up
->sg
.grp
);
205 bits
= PIM_ENCODE_SPARSE_BIT
| PIM_ENCODE_WC_BIT
206 | PIM_ENCODE_RPT_BIT
;
207 stosend
= rpf
->rpf_addr
.u
.prefix4
;
208 /* Only Send SGRpt in case of *,G Join */
212 bits
= PIM_ENCODE_SPARSE_BIT
;
213 stosend
= source
->up
->sg
.src
;
216 pim_msg_addr_encode_ipv4_source((uint8_t *)&grp
->s
[tgroups
],
222 struct pim_upstream
*child
;
224 for (ALL_LIST_ELEMENTS(up
->sources
, node
, nnode
, child
)) {
225 if (PIM_UPSTREAM_FLAG_TEST_SEND_SG_RPT_PRUNE(
227 pim_msg_addr_encode_ipv4_source(
228 (uint8_t *)&grp
->s
[tgroups
],
230 PIM_ENCODE_SPARSE_BIT
231 | PIM_ENCODE_RPT_BIT
);
233 PIM_UPSTREAM_FLAG_UNSET_SEND_SG_RPT_PRUNE(
240 grp
->joins
= htons(grp
->joins
);
241 grp
->prunes
= htons(grp
->prunes
);