]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_msg.c
doc: Add `show ipv6 rpf X:X::X:X` command to docs
[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_instance.h"
30 #include "pim_vty.h"
31 #include "pim_pim.h"
32 #include "pim_msg.h"
33 #include "pim_util.h"
34 #include "pim_str.h"
35 #include "pim_iface.h"
36 #include "pim_rp.h"
37 #include "pim_rpf.h"
38 #include "pim_register.h"
39 #include "pim_jp_agg.h"
40 #include "pim_oil.h"
41
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,
44 bool no_fwd)
45 {
46 struct pim_msg_header *header = (struct pim_msg_header *)pim_msg;
47 struct iovec iov[2], *iovp = iov;
48
49 /*
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
52 * data packet portion
53 *
54 * for IPv6, the pseudoheader upper-level protocol length is also
55 * truncated, so let's just set it here before everything else.
56 */
57 if (pim_msg_type == PIM_MSG_TYPE_REGISTER)
58 pim_msg_size = PIM_MSG_REGISTER_LEN;
59
60 #if PIM_IPV == 6
61 struct ipv6_ph phdr = {
62 .src = src,
63 .dst = dst,
64 .ulpl = htonl(pim_msg_size),
65 .next_hdr = IPPROTO_PIM,
66 };
67
68 iovp->iov_base = &phdr;
69 iovp->iov_len = sizeof(phdr);
70 iovp++;
71 #endif
72
73 /*
74 * Write header
75 */
76 header->ver = PIM_PROTO_VERSION;
77 header->type = pim_msg_type;
78 header->Nbit = no_fwd;
79 header->reserved = 0;
80
81 header->checksum = 0;
82 iovp->iov_base = header;
83 iovp->iov_len = pim_msg_size;
84 iovp++;
85
86 header->checksum = in_cksumv(iov, iovp - iov);
87 }
88
89 uint8_t *pim_msg_addr_encode_ipv4_ucast(uint8_t *buf, struct in_addr addr)
90 {
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));
94
95 return buf + PIM_ENCODED_IPV4_UCAST_SIZE;
96 }
97
98 uint8_t *pim_msg_addr_encode_ipv4_group(uint8_t *buf, struct in_addr addr)
99 {
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));
105
106 return buf + PIM_ENCODED_IPV4_GROUP_SIZE;
107 }
108
109 uint8_t *pim_msg_addr_encode_ipv4_source(uint8_t *buf, struct in_addr addr,
110 uint8_t bits)
111 {
112 buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV4; /* addr family */
113 buf[1] = '\0'; /* native encoding */
114 buf[2] = bits;
115 buf[3] = 32; /* mask len */
116 memcpy(buf + 4, &addr, sizeof(struct in_addr));
117
118 return buf + PIM_ENCODED_IPV4_SOURCE_SIZE;
119 }
120
121 uint8_t *pim_msg_addr_encode_ipv6_source(uint8_t *buf, struct in6_addr addr,
122 uint8_t bits)
123 {
124 buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV6; /* addr family */
125 buf[1] = '\0'; /* native encoding */
126 buf[2] = bits;
127 buf[3] = 128; /* mask len */
128 buf += 4;
129
130 memcpy(buf, &addr, sizeof(addr));
131 buf += sizeof(addr);
132
133 return buf;
134 }
135
136 uint8_t *pim_msg_addr_encode_ipv6_ucast(uint8_t *buf, struct in6_addr addr)
137 {
138 buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV6; /* addr family */
139 buf[1] = '\0'; /* native encoding */
140 buf += 2;
141
142 memcpy(buf, &addr, sizeof(addr));
143 buf += sizeof(addr);
144
145 return buf;
146 }
147
148 uint8_t *pim_msg_addr_encode_ipv6_group(uint8_t *buf, struct in6_addr addr)
149 {
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 */
154 buf += 4;
155
156 memcpy(buf, &addr, sizeof(addr));
157 buf += sizeof(addr);
158
159 return buf;
160 }
161
162 #if PIM_IPV == 4
163 #define pim_msg_addr_encode(what) pim_msg_addr_encode_ipv4_##what
164 #else
165 #define pim_msg_addr_encode(what) pim_msg_addr_encode_ipv6_##what
166 #endif
167
168 uint8_t *pim_msg_addr_encode_ucast(uint8_t *buf, pim_addr addr)
169 {
170 return pim_msg_addr_encode(ucast)(buf, addr);
171 }
172
173 uint8_t *pim_msg_addr_encode_group(uint8_t *buf, pim_addr addr)
174 {
175 return pim_msg_addr_encode(group)(buf, addr);
176 }
177
178 uint8_t *pim_msg_addr_encode_source(uint8_t *buf, pim_addr addr, uint8_t bits)
179 {
180 return pim_msg_addr_encode(source)(buf, addr, bits);
181 }
182
183 /*
184 * For the given 'struct pim_jp_sources' list
185 * determine the size_t it would take up.
186 */
187 size_t pim_msg_get_jp_group_size(struct list *sources)
188 {
189 struct pim_jp_sources *js;
190 size_t size = 0;
191
192 if (!sources)
193 return 0;
194
195 size += sizeof(pim_encoded_group);
196 size += 4; // Joined sources (2) + Pruned Sources (2)
197
198 size += sizeof(pim_encoded_source) * sources->count;
199
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;
204
205 up = js->up;
206 if (PIM_DEBUG_PIM_PACKETS)
207 zlog_debug(
208 "%s: Considering (%s) children for (S,G,rpt) prune",
209 __func__, up->sg_str);
210
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
215 * of the RPT.
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).
219 */
220 if (child->rpf.source_nexthop.interface &&
221 !pim_rpf_is_same(&up->rpf,
222 &child->rpf)) {
223 size += sizeof(pim_encoded_source);
224 PIM_UPSTREAM_FLAG_SET_SEND_SG_RPT_PRUNE(
225 child->flags);
226 if (PIM_DEBUG_PIM_PACKETS)
227 zlog_debug(
228 "%s: SPT Bit and RPF'(%s) != RPF'(S,G): Add Prune (%s,rpt) to compound message",
229 __func__, up->sg_str,
230 child->sg_str);
231 } else if (PIM_DEBUG_PIM_PACKETS)
232 zlog_debug(
233 "%s: SPT Bit and RPF'(%s) == RPF'(S,G): Not adding Prune for (%s,rpt)",
234 __func__, up->sg_str,
235 child->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
239 * prune it off.
240 */
241 size += sizeof(pim_encoded_source);
242 PIM_UPSTREAM_FLAG_SET_SEND_SG_RPT_PRUNE(
243 child->flags);
244 if (PIM_DEBUG_PIM_PACKETS)
245 zlog_debug(
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)
249 zlog_debug(
250 "%s: Do not add Prune %s to compound message %s",
251 __func__, child->sg_str, up->sg_str);
252 }
253 }
254 return size;
255 }
256
257 size_t pim_msg_build_jp_groups(struct pim_jp_groups *grp,
258 struct pim_jp_agg_group *sgs, size_t size)
259 {
260 struct listnode *node, *nnode;
261 struct pim_jp_sources *source;
262 struct pim_upstream *up = NULL;
263 pim_addr stosend;
264 uint8_t bits;
265 uint8_t tgroups = 0;
266
267 memset(grp, 0, size);
268 pim_msg_addr_encode_group((uint8_t *)&grp->g, sgs->group);
269
270 for (ALL_LIST_ELEMENTS(sgs->sources, node, nnode, source)) {
271 /* number of joined/pruned sources */
272 if (source->is_join)
273 grp->joins++;
274 else
275 grp->prunes++;
276
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 */
284 if (source->is_join)
285 up = source->up;
286 } else {
287 bits = PIM_ENCODE_SPARSE_BIT;
288 stosend = source->up->sg.src;
289 }
290
291 pim_msg_addr_encode_source((uint8_t *)&grp->s[tgroups], stosend,
292 bits);
293 tgroups++;
294 }
295
296 if (up) {
297 struct pim_upstream *child;
298
299 for (ALL_LIST_ELEMENTS(up->sources, node, nnode, child)) {
300 if (PIM_UPSTREAM_FLAG_TEST_SEND_SG_RPT_PRUNE(
301 child->flags)) {
302 pim_msg_addr_encode_source(
303 (uint8_t *)&grp->s[tgroups],
304 child->sg.src,
305 PIM_ENCODE_SPARSE_BIT |
306 PIM_ENCODE_RPT_BIT);
307 tgroups++;
308 PIM_UPSTREAM_FLAG_UNSET_SEND_SG_RPT_PRUNE(
309 child->flags);
310 grp->prunes++;
311 }
312 }
313 }
314
315 grp->joins = htons(grp->joins);
316 grp->prunes = htons(grp->prunes);
317
318 return size;
319 }