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