]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_msg.c
a9e0130905859acbeae47fd4efe370acc42c4e06
[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
16 along with this program; see the file COPYING; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18 MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "if.h"
24 #include "log.h"
25 #include "prefix.h"
26 #include "vty.h"
27 #include "plist.h"
28
29 #include "pimd.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
41 void pim_msg_build_header(uint8_t *pim_msg, size_t pim_msg_size, 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 packet,
56 * including the PIM header and the next 4 bytes, excluding the data packet portion
57 */
58 if (pim_msg_type == PIM_MSG_TYPE_REGISTER)
59 header->checksum = in_cksum (pim_msg, PIM_MSG_REGISTER_LEN);
60 else
61 header->checksum = in_cksum (pim_msg, pim_msg_size);
62 }
63
64 uint8_t *pim_msg_addr_encode_ipv4_ucast(uint8_t *buf, struct in_addr addr)
65 {
66 buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV4; /* addr family */
67 buf[1] = '\0'; /* native encoding */
68 memcpy(buf+2, &addr, sizeof(struct in_addr));
69
70 return buf + PIM_ENCODED_IPV4_UCAST_SIZE;
71 }
72
73 uint8_t *pim_msg_addr_encode_ipv4_group(uint8_t *buf, struct in_addr addr)
74 {
75 buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV4; /* addr family */
76 buf[1] = '\0'; /* native encoding */
77 buf[2] = '\0'; /* reserved */
78 buf[3] = 32; /* mask len */
79 memcpy(buf+4, &addr, sizeof(struct in_addr));
80
81 return buf + PIM_ENCODED_IPV4_GROUP_SIZE;
82 }
83
84 uint8_t *
85 pim_msg_addr_encode_ipv4_source(uint8_t *buf,
86 struct in_addr addr, uint8_t bits)
87 {
88 buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV4; /* addr family */
89 buf[1] = '\0'; /* native encoding */
90 buf[2] = bits;
91 buf[3] = 32; /* mask len */
92 memcpy(buf+4, &addr, sizeof(struct in_addr));
93
94 return buf + PIM_ENCODED_IPV4_SOURCE_SIZE;
95 }
96
97 /*
98 * For the given 'struct pim_jp_sources' list
99 * determine the size_t it would take up.
100 */
101 size_t
102 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 {
115 struct pim_upstream *child, *up;
116 struct listnode *up_node;
117
118 up = js->up;
119 if (PIM_DEBUG_PIM_PACKETS)
120 zlog_debug ("%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 {
125 if (child->sptbit == PIM_UPSTREAM_SPTBIT_TRUE)
126 {
127 if (!pim_rpf_is_same(&up->rpf, &child->rpf))
128 {
129 size += sizeof (struct pim_encoded_source_ipv4);
130 PIM_UPSTREAM_FLAG_SET_SEND_SG_RPT_PRUNE(child->flags);
131 if (PIM_DEBUG_PIM_PACKETS)
132 zlog_debug ("%s: SPT Bit and RPF'(%s) != RPF'(S,G): Add Prune (%s,rpt) to compound message",
133 __PRETTY_FUNCTION__, up->sg_str, child->sg_str);
134 }
135 else
136 if (PIM_DEBUG_PIM_PACKETS)
137 zlog_debug ("%s: SPT Bit and RPF'(%s) == RPF'(S,G): Not adding Prune for (%s,rpt)",
138 __PRETTY_FUNCTION__, up->sg_str, child->sg_str);
139 }
140 else if (pim_upstream_is_sg_rpt (child))
141 {
142 if (pim_upstream_empty_inherited_olist (child))
143 {
144 size += sizeof (struct pim_encoded_source_ipv4);
145 PIM_UPSTREAM_FLAG_SET_SEND_SG_RPT_PRUNE(child->flags);
146 if (PIM_DEBUG_PIM_PACKETS)
147 zlog_debug ("%s: inherited_olist(%s,rpt) is NULL, Add Prune to compound message",
148 __PRETTY_FUNCTION__, child->sg_str);
149 }
150 else if (!pim_rpf_is_same (&up->rpf, &child->rpf))
151 {
152 size += sizeof (struct pim_encoded_source_ipv4);
153 PIM_UPSTREAM_FLAG_SET_SEND_SG_RPT_PRUNE(child->flags);
154 if (PIM_DEBUG_PIM_PACKETS)
155 zlog_debug ("%s: RPF'(%s) != RPF'(%s,rpt), Add Prune to compound message",
156 __PRETTY_FUNCTION__, up->sg_str, child->sg_str);
157 }
158 else
159 if (PIM_DEBUG_PIM_PACKETS)
160 zlog_debug ("%s: RPF'(%s) == RPF'(%s,rpt), Do not add Prune to compound message",
161 __PRETTY_FUNCTION__, up->sg_str, child->sg_str);
162 }
163 else
164 if (PIM_DEBUG_PIM_PACKETS)
165 zlog_debug ("%s: SPT bit is not set for (%s)",
166 __PRETTY_FUNCTION__, child->sg_str);
167 }
168 }
169 return size;
170 }
171
172 size_t
173 pim_msg_build_jp_groups (struct pim_jp_groups *grp, struct pim_jp_agg_group *sgs, size_t size)
174 {
175 struct listnode *node, *nnode;
176 struct pim_jp_sources *source;
177 struct pim_upstream *up = NULL;
178 struct in_addr stosend;
179 uint8_t bits;
180 uint8_t tgroups = 0;
181
182 memset (grp, 0, size);
183 pim_msg_addr_encode_ipv4_group ((uint8_t *)&grp->g, sgs->group);
184
185 for (ALL_LIST_ELEMENTS(sgs->sources, node, nnode, source))
186 {
187 /* number of joined/pruned sources */
188 if (source->is_join)
189 grp->joins++;
190 else
191 grp->prunes++;
192
193 if (source->up->sg.src.s_addr == INADDR_ANY)
194 {
195 struct pim_rpf *rpf = pim_rp_g (source->up->sg.grp);
196 bits = PIM_ENCODE_SPARSE_BIT | PIM_ENCODE_WC_BIT | PIM_ENCODE_RPT_BIT;
197 stosend = rpf->rpf_addr.u.prefix4;
198 /* Only Send SGRpt in case of *,G Join */
199 if (source->is_join)
200 up = source->up;
201 }
202 else
203 {
204 bits = PIM_ENCODE_SPARSE_BIT;
205 stosend = source->up->sg.src;
206 }
207
208 pim_msg_addr_encode_ipv4_source ((uint8_t *)&grp->s[tgroups], stosend, bits);
209 tgroups++;
210 }
211
212 if (up)
213 {
214 struct pim_upstream *child;
215
216 for (ALL_LIST_ELEMENTS(up->sources, node, nnode, child))
217 {
218 if (PIM_UPSTREAM_FLAG_TEST_SEND_SG_RPT_PRUNE(child->flags))
219 {
220 pim_msg_addr_encode_ipv4_source ((uint8_t *)&grp->s[tgroups],
221 child->sg.src,
222 PIM_ENCODE_SPARSE_BIT | PIM_ENCODE_RPT_BIT);
223 tgroups++;
224 PIM_UPSTREAM_FLAG_UNSET_SEND_SG_RPT_PRUNE(child->flags);
225 grp->prunes++;
226 }
227 }
228 }
229
230 grp->joins = htons(grp->joins);
231 grp->prunes = htons(grp->prunes);
232
233 return size;
234 }