]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_msg.c
merge with upstream
[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
40 void pim_msg_build_header(uint8_t *pim_msg, size_t pim_msg_size,
41 uint8_t pim_msg_type)
42 {
43 struct pim_msg_header *header = (struct pim_msg_header *)pim_msg;
44
45 /*
46 * Write header
47 */
48 header->ver = PIM_PROTO_VERSION;
49 header->type = pim_msg_type;
50 header->reserved = 0;
51
52
53 header->checksum = 0;
54 /*
55 * The checksum for Registers is done only on the first 8 bytes of the
56 * packet,
57 * including the PIM header and the next 4 bytes, excluding the data
58 * packet portion
59 */
60 if (pim_msg_type == PIM_MSG_TYPE_REGISTER)
61 header->checksum = in_cksum(pim_msg, PIM_MSG_REGISTER_LEN);
62 else
63 header->checksum = in_cksum(pim_msg, pim_msg_size);
64 }
65
66 uint8_t *pim_msg_addr_encode_ipv4_ucast(uint8_t *buf, struct in_addr addr)
67 {
68 buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV4; /* addr family */
69 buf[1] = '\0'; /* native encoding */
70 memcpy(buf + 2, &addr, sizeof(struct in_addr));
71
72 return buf + PIM_ENCODED_IPV4_UCAST_SIZE;
73 }
74
75 uint8_t *pim_msg_addr_encode_ipv4_group(uint8_t *buf, struct in_addr addr)
76 {
77 buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV4; /* addr family */
78 buf[1] = '\0'; /* native encoding */
79 buf[2] = '\0'; /* reserved */
80 buf[3] = 32; /* mask len */
81 memcpy(buf + 4, &addr, sizeof(struct in_addr));
82
83 return buf + PIM_ENCODED_IPV4_GROUP_SIZE;
84 }
85
86 uint8_t *pim_msg_addr_encode_ipv4_source(uint8_t *buf, struct in_addr addr,
87 uint8_t bits)
88 {
89 buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV4; /* addr family */
90 buf[1] = '\0'; /* native encoding */
91 buf[2] = bits;
92 buf[3] = 32; /* mask len */
93 memcpy(buf + 4, &addr, sizeof(struct in_addr));
94
95 return buf + PIM_ENCODED_IPV4_SOURCE_SIZE;
96 }
97
98 /*
99 * For the given 'struct pim_jp_sources' list
100 * determine the size_t it would take up.
101 */
102 size_t pim_msg_get_jp_group_size(struct list *sources)
103 {
104 struct pim_jp_sources *js;
105 size_t size = 0;
106
107 size += sizeof(struct pim_encoded_group_ipv4);
108 size += 4; // Joined sources (2) + Pruned Sources (2)
109
110 size += sizeof(struct pim_encoded_source_ipv4) * sources->count;
111
112 js = listgetdata(listhead(sources));
113 if (js && js->up->sg.src.s_addr == INADDR_ANY) {
114 struct pim_upstream *child, *up;
115 struct listnode *up_node;
116
117 up = js->up;
118 if (PIM_DEBUG_PIM_PACKETS)
119 zlog_debug(
120 "%s: Considering (%s) children for (S,G,rpt) prune",
121 __PRETTY_FUNCTION__, up->sg_str);
122
123 for (ALL_LIST_ELEMENTS_RO(up->sources, up_node, child)) {
124 if (child->sptbit == PIM_UPSTREAM_SPTBIT_TRUE) {
125 if (!pim_rpf_is_same(&up->rpf, &child->rpf)) {
126 size += sizeof(
127 struct pim_encoded_source_ipv4);
128 PIM_UPSTREAM_FLAG_SET_SEND_SG_RPT_PRUNE(
129 child->flags);
130 if (PIM_DEBUG_PIM_PACKETS)
131 zlog_debug(
132 "%s: SPT Bit and RPF'(%s) != RPF'(S,G): Add Prune (%s,rpt) to compound message",
133 __PRETTY_FUNCTION__,
134 up->sg_str,
135 child->sg_str);
136 } else if (PIM_DEBUG_PIM_PACKETS)
137 zlog_debug(
138 "%s: SPT Bit and RPF'(%s) == RPF'(S,G): Not adding Prune for (%s,rpt)",
139 __PRETTY_FUNCTION__, up->sg_str,
140 child->sg_str);
141 } else if (pim_upstream_is_sg_rpt(child)) {
142 if (pim_upstream_empty_inherited_olist(child)) {
143 size += sizeof(
144 struct pim_encoded_source_ipv4);
145 PIM_UPSTREAM_FLAG_SET_SEND_SG_RPT_PRUNE(
146 child->flags);
147 if (PIM_DEBUG_PIM_PACKETS)
148 zlog_debug(
149 "%s: inherited_olist(%s,rpt) is NULL, Add Prune to compound message",
150 __PRETTY_FUNCTION__,
151 child->sg_str);
152 } else if (!pim_rpf_is_same(&up->rpf,
153 &child->rpf)) {
154 size += sizeof(
155 struct pim_encoded_source_ipv4);
156 PIM_UPSTREAM_FLAG_SET_SEND_SG_RPT_PRUNE(
157 child->flags);
158 if (PIM_DEBUG_PIM_PACKETS)
159 zlog_debug(
160 "%s: RPF'(%s) != RPF'(%s,rpt), Add Prune to compound message",
161 __PRETTY_FUNCTION__,
162 up->sg_str,
163 child->sg_str);
164 } else if (PIM_DEBUG_PIM_PACKETS)
165 zlog_debug(
166 "%s: RPF'(%s) == RPF'(%s,rpt), Do not add Prune to compound message",
167 __PRETTY_FUNCTION__, up->sg_str,
168 child->sg_str);
169 } else if (PIM_DEBUG_PIM_PACKETS)
170 zlog_debug("%s: SPT bit is not set for (%s)",
171 __PRETTY_FUNCTION__, child->sg_str);
172 }
173 }
174 return size;
175 }
176
177 size_t pim_msg_build_jp_groups(struct pim_jp_groups *grp,
178 struct pim_jp_agg_group *sgs, size_t size)
179 {
180 struct listnode *node, *nnode;
181 struct pim_jp_sources *source;
182 struct pim_upstream *up = NULL;
183 struct in_addr stosend;
184 uint8_t bits;
185 uint8_t tgroups = 0;
186
187 memset(grp, 0, size);
188 pim_msg_addr_encode_ipv4_group((uint8_t *)&grp->g, sgs->group);
189
190 for (ALL_LIST_ELEMENTS(sgs->sources, node, nnode, source)) {
191 /* number of joined/pruned sources */
192 if (source->is_join)
193 grp->joins++;
194 else
195 grp->prunes++;
196
197 if (source->up->sg.src.s_addr == INADDR_ANY) {
198 struct pim_rpf *rpf = pim_rp_g(source->up->sg.grp);
199 bits = PIM_ENCODE_SPARSE_BIT | PIM_ENCODE_WC_BIT
200 | PIM_ENCODE_RPT_BIT;
201 stosend = rpf->rpf_addr.u.prefix4;
202 /* Only Send SGRpt in case of *,G Join */
203 if (source->is_join)
204 up = source->up;
205 } else {
206 bits = PIM_ENCODE_SPARSE_BIT;
207 stosend = source->up->sg.src;
208 }
209
210 pim_msg_addr_encode_ipv4_source((uint8_t *)&grp->s[tgroups],
211 stosend, bits);
212 tgroups++;
213 }
214
215 if (up) {
216 struct pim_upstream *child;
217
218 for (ALL_LIST_ELEMENTS(up->sources, node, nnode, child)) {
219 if (PIM_UPSTREAM_FLAG_TEST_SEND_SG_RPT_PRUNE(
220 child->flags)) {
221 pim_msg_addr_encode_ipv4_source(
222 (uint8_t *)&grp->s[tgroups],
223 child->sg.src,
224 PIM_ENCODE_SPARSE_BIT
225 | PIM_ENCODE_RPT_BIT);
226 tgroups++;
227 PIM_UPSTREAM_FLAG_UNSET_SEND_SG_RPT_PRUNE(
228 child->flags);
229 grp->prunes++;
230 }
231 }
232 }
233
234 grp->joins = htons(grp->joins);
235 grp->prunes = htons(grp->prunes);
236
237 return size;
238 }