]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_msg.c
Merge pull request #10770 from chiragshah6/evpn_dev3
[mirror_frr.git] / pimd / pim_msg.c
1 /*
2 * PIM for Quagga
3 * Copyright (C) 2008 Everton da Silva Marques
4 *
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.
9 *
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.
14 *
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
18 */
19
20 #include <zebra.h>
21
22 #include "if.h"
23 #include "log.h"
24 #include "prefix.h"
25 #include "vty.h"
26 #include "plist.h"
27
28 #include "pimd.h"
29 #include "pim_vty.h"
30 #include "pim_pim.h"
31 #include "pim_msg.h"
32 #include "pim_util.h"
33 #include "pim_str.h"
34 #include "pim_iface.h"
35 #include "pim_rp.h"
36 #include "pim_rpf.h"
37 #include "pim_register.h"
38 #include "pim_jp_agg.h"
39 #include "pim_oil.h"
40
41 void pim_msg_build_header(uint8_t *pim_msg, size_t pim_msg_size,
42 uint8_t pim_msg_type, bool no_fwd)
43 {
44 struct pim_msg_header *header = (struct pim_msg_header *)pim_msg;
45
46 /*
47 * Write header
48 */
49 header->ver = PIM_PROTO_VERSION;
50 header->type = pim_msg_type;
51 header->Nbit = no_fwd;
52 header->reserved = 0;
53
54
55 header->checksum = 0;
56 /*
57 * The checksum for Registers is done only on the first 8 bytes of the
58 * packet,
59 * including the PIM header and the next 4 bytes, excluding the data
60 * packet portion
61 */
62 if (pim_msg_type == PIM_MSG_TYPE_REGISTER)
63 header->checksum = in_cksum(pim_msg, PIM_MSG_REGISTER_LEN);
64 else
65 header->checksum = in_cksum(pim_msg, pim_msg_size);
66 }
67
68 uint8_t *pim_msg_addr_encode_ipv4_ucast(uint8_t *buf, struct in_addr addr)
69 {
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));
73
74 return buf + PIM_ENCODED_IPV4_UCAST_SIZE;
75 }
76
77 uint8_t *pim_msg_addr_encode_ipv4_group(uint8_t *buf, struct in_addr addr)
78 {
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));
84
85 return buf + PIM_ENCODED_IPV4_GROUP_SIZE;
86 }
87
88 uint8_t *pim_msg_addr_encode_ipv4_source(uint8_t *buf, struct in_addr addr,
89 uint8_t bits)
90 {
91 buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV4; /* addr family */
92 buf[1] = '\0'; /* native encoding */
93 buf[2] = bits;
94 buf[3] = 32; /* mask len */
95 memcpy(buf + 4, &addr, sizeof(struct in_addr));
96
97 return buf + PIM_ENCODED_IPV4_SOURCE_SIZE;
98 }
99
100 uint8_t *pim_msg_addr_encode_ipv6_source(uint8_t *buf, struct in6_addr addr,
101 uint8_t bits)
102 {
103 buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV6; /* addr family */
104 buf[1] = '\0'; /* native encoding */
105 buf[2] = bits;
106 buf[3] = 128; /* mask len */
107 buf += 4;
108
109 memcpy(buf, &addr, sizeof(addr));
110 buf += sizeof(addr);
111
112 return buf;
113 }
114
115 uint8_t *pim_msg_addr_encode_ipv6_ucast(uint8_t *buf, struct in6_addr addr)
116 {
117 buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV6; /* addr family */
118 buf[1] = '\0'; /* native encoding */
119 buf += 2;
120
121 memcpy(buf, &addr, sizeof(addr));
122 buf += sizeof(addr);
123
124 return buf;
125 }
126
127 uint8_t *pim_msg_addr_encode_ipv6_group(uint8_t *buf, struct in6_addr addr)
128 {
129 buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV6; /* addr family */
130 buf[1] = '\0'; /* native encoding */
131 buf[2] = '\0'; /* reserved */
132 buf[3] = 128; /* mask len */
133 buf += 4;
134
135 memcpy(buf, &addr, sizeof(addr));
136 buf += sizeof(addr);
137
138 return buf;
139 }
140
141 #if PIM_IPV == 4
142 #define pim_msg_addr_encode(what) pim_msg_addr_encode_ipv4_##what
143 #else
144 #define pim_msg_addr_encode(what) pim_msg_addr_encode_ipv6_##what
145 #endif
146
147 uint8_t *pim_msg_addr_encode_ucast(uint8_t *buf, pim_addr addr)
148 {
149 return pim_msg_addr_encode(ucast)(buf, addr);
150 }
151
152 uint8_t *pim_msg_addr_encode_group(uint8_t *buf, pim_addr addr)
153 {
154 return pim_msg_addr_encode(group)(buf, addr);
155 }
156
157 uint8_t *pim_msg_addr_encode_source(uint8_t *buf, pim_addr addr, uint8_t bits)
158 {
159 return pim_msg_addr_encode(source)(buf, addr, bits);
160 }
161
162 /*
163 * For the given 'struct pim_jp_sources' list
164 * determine the size_t it would take up.
165 */
166 size_t pim_msg_get_jp_group_size(struct list *sources)
167 {
168 struct pim_jp_sources *js;
169 size_t size = 0;
170
171 if (!sources)
172 return 0;
173
174 size += sizeof(pim_encoded_group);
175 size += 4; // Joined sources (2) + Pruned Sources (2)
176
177 size += sizeof(pim_encoded_source) * sources->count;
178
179 js = listgetdata(listhead(sources));
180 if (js && pim_addr_is_any(js->up->sg.src) && js->is_join) {
181 struct pim_upstream *child, *up;
182 struct listnode *up_node;
183
184 up = js->up;
185 if (PIM_DEBUG_PIM_PACKETS)
186 zlog_debug(
187 "%s: Considering (%s) children for (S,G,rpt) prune",
188 __func__, up->sg_str);
189
190 for (ALL_LIST_ELEMENTS_RO(up->sources, up_node, child)) {
191 if (!PIM_UPSTREAM_FLAG_TEST_USE_RPT(child->flags)) {
192 /* If we are using SPT and the SPT and RPT IIFs
193 * are different we can prune the source off
194 * of the RPT.
195 * If RPF_interface(S) is not resolved hold
196 * decision to prune as SPT may end up on the
197 * same IIF as RPF_interface(RP).
198 */
199 if (child->rpf.source_nexthop.interface &&
200 !pim_rpf_is_same(&up->rpf,
201 &child->rpf)) {
202 size += sizeof(pim_encoded_source);
203 PIM_UPSTREAM_FLAG_SET_SEND_SG_RPT_PRUNE(
204 child->flags);
205 if (PIM_DEBUG_PIM_PACKETS)
206 zlog_debug(
207 "%s: SPT Bit and RPF'(%s) != RPF'(S,G): Add Prune (%s,rpt) to compound message",
208 __func__, up->sg_str,
209 child->sg_str);
210 } else if (PIM_DEBUG_PIM_PACKETS)
211 zlog_debug(
212 "%s: SPT Bit and RPF'(%s) == RPF'(S,G): Not adding Prune for (%s,rpt)",
213 __func__, up->sg_str,
214 child->sg_str);
215 } else if (pim_upstream_empty_inherited_olist(child)) {
216 /* S is supposed to be forwarded along the RPT
217 * but it's inherited OIL is empty. So just
218 * prune it off.
219 */
220 size += sizeof(pim_encoded_source);
221 PIM_UPSTREAM_FLAG_SET_SEND_SG_RPT_PRUNE(
222 child->flags);
223 if (PIM_DEBUG_PIM_PACKETS)
224 zlog_debug(
225 "%s: inherited_olist(%s,rpt) is NULL, Add Prune to compound message",
226 __func__, child->sg_str);
227 } else if (PIM_DEBUG_PIM_PACKETS)
228 zlog_debug(
229 "%s: Do not add Prune %s to compound message %s",
230 __func__, child->sg_str, up->sg_str);
231 }
232 }
233 return size;
234 }
235
236 size_t pim_msg_build_jp_groups(struct pim_jp_groups *grp,
237 struct pim_jp_agg_group *sgs, size_t size)
238 {
239 struct listnode *node, *nnode;
240 struct pim_jp_sources *source;
241 struct pim_upstream *up = NULL;
242 pim_addr stosend;
243 uint8_t bits;
244 uint8_t tgroups = 0;
245
246 memset(grp, 0, size);
247 pim_msg_addr_encode_group((uint8_t *)&grp->g, sgs->group);
248
249 for (ALL_LIST_ELEMENTS(sgs->sources, node, nnode, source)) {
250 /* number of joined/pruned sources */
251 if (source->is_join)
252 grp->joins++;
253 else
254 grp->prunes++;
255
256 if (pim_addr_is_any(source->up->sg.src)) {
257 struct pim_instance *pim = source->up->channel_oil->pim;
258 struct pim_rpf *rpf = pim_rp_g(pim, source->up->sg.grp);
259 bits = PIM_ENCODE_SPARSE_BIT | PIM_ENCODE_WC_BIT
260 | PIM_ENCODE_RPT_BIT;
261 stosend = pim_addr_from_prefix(&rpf->rpf_addr);
262 /* Only Send SGRpt in case of *,G Join */
263 if (source->is_join)
264 up = source->up;
265 } else {
266 bits = PIM_ENCODE_SPARSE_BIT;
267 stosend = source->up->sg.src;
268 }
269
270 pim_msg_addr_encode_source((uint8_t *)&grp->s[tgroups], stosend,
271 bits);
272 tgroups++;
273 }
274
275 if (up) {
276 struct pim_upstream *child;
277
278 for (ALL_LIST_ELEMENTS(up->sources, node, nnode, child)) {
279 if (PIM_UPSTREAM_FLAG_TEST_SEND_SG_RPT_PRUNE(
280 child->flags)) {
281 pim_msg_addr_encode_source(
282 (uint8_t *)&grp->s[tgroups],
283 child->sg.src,
284 PIM_ENCODE_SPARSE_BIT |
285 PIM_ENCODE_RPT_BIT);
286 tgroups++;
287 PIM_UPSTREAM_FLAG_UNSET_SEND_SG_RPT_PRUNE(
288 child->flags);
289 grp->prunes++;
290 }
291 }
292 }
293
294 grp->joins = htons(grp->joins);
295 grp->prunes = htons(grp->prunes);
296
297 return size;
298 }