3 Copyright (C) 2008 Everton da Silva Marques
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.
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.
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,
35 #include "pim_iface.h"
39 void pim_msg_build_header(uint8_t *pim_msg
, int pim_msg_size
,
44 zassert(pim_msg_size
>= PIM_PIM_MIN_LEN
);
50 *(uint8_t *) PIM_MSG_HDR_OFFSET_VERSION(pim_msg
) = (PIM_PROTO_VERSION
<< 4) | pim_msg_type
;
51 *(uint8_t *) PIM_MSG_HDR_OFFSET_RESERVED(pim_msg
) = 0;
57 *(uint16_t *) PIM_MSG_HDR_OFFSET_CHECKSUM(pim_msg
) = 0;
58 checksum
= in_cksum(pim_msg
, pim_msg_size
);
59 *(uint16_t *) PIM_MSG_HDR_OFFSET_CHECKSUM(pim_msg
) = checksum
;
62 uint8_t *pim_msg_addr_encode_ipv4_ucast(uint8_t *buf
,
66 const int ENCODED_IPV4_UCAST_SIZE
= 6;
68 if (buf_size
< ENCODED_IPV4_UCAST_SIZE
) {
72 buf
[0] = PIM_MSG_ADDRESS_FAMILY_IPV4
; /* addr family */
73 buf
[1] = '\0'; /* native encoding */
74 memcpy(buf
+2, &addr
, sizeof(struct in_addr
));
76 return buf
+ ENCODED_IPV4_UCAST_SIZE
;
79 uint8_t *pim_msg_addr_encode_ipv4_group(uint8_t *buf
,
83 const int ENCODED_IPV4_GROUP_SIZE
= 8;
85 if (buf_size
< ENCODED_IPV4_GROUP_SIZE
) {
89 buf
[0] = PIM_MSG_ADDRESS_FAMILY_IPV4
; /* addr family */
90 buf
[1] = '\0'; /* native encoding */
91 buf
[2] = '\0'; /* reserved */
92 buf
[3] = 32; /* mask len */
93 memcpy(buf
+4, &addr
, sizeof(struct in_addr
));
95 return buf
+ ENCODED_IPV4_GROUP_SIZE
;
99 pim_msg_addr_encode_ipv4_source(uint8_t *buf
, int buf_size
,
100 struct in_addr addr
, uint8_t bits
)
102 const int ENCODED_IPV4_SOURCE_SIZE
= 8;
104 if (buf_size
< ENCODED_IPV4_SOURCE_SIZE
) {
108 buf
[0] = PIM_MSG_ADDRESS_FAMILY_IPV4
; /* addr family */
109 buf
[1] = '\0'; /* native encoding */
111 buf
[3] = 32; /* mask len */
112 memcpy(buf
+4, &addr
, sizeof(struct in_addr
));
114 return buf
+ ENCODED_IPV4_SOURCE_SIZE
;
118 pim_msg_join_prune_encode (uint8_t *buf
, int buf_size
, int is_join
,
119 struct pim_upstream
*up
,
120 struct in_addr upstream
, int holdtime
)
122 uint8_t *pim_msg
= buf
;
123 uint8_t *pim_msg_curr
= buf
+ PIM_MSG_HEADER_LEN
;
124 uint8_t *end
= buf
+ buf_size
;
125 uint16_t *prunes
= NULL
;
126 uint16_t *joins
= NULL
;
127 struct in_addr stosend
;
131 remain
= end
- pim_msg_curr
;
132 pim_msg_curr
= pim_msg_addr_encode_ipv4_ucast (pim_msg_curr
, buf_size
- PIM_MSG_HEADER_LEN
, upstream
);
134 char dst_str
[INET_ADDRSTRLEN
];
135 pim_inet4_dump("<dst?>", upstream
, dst_str
, sizeof(dst_str
));
136 zlog_warn("%s: failure encoding destination address %s: space left=%d",
137 __PRETTY_FUNCTION__
, dst_str
, remain
);
141 remain
= end
- pim_msg_curr
;
143 zlog_warn("%s: group will not fit: space left=%d",
144 __PRETTY_FUNCTION__
, remain
);
148 *pim_msg_curr
= 0; /* reserved */
150 *pim_msg_curr
= 1; /* number of groups */
153 *((uint16_t *) pim_msg_curr
) = htons(holdtime
);
157 remain
= end
- pim_msg_curr
;
158 pim_msg_curr
= pim_msg_addr_encode_ipv4_group (pim_msg_curr
, remain
,
161 char group_str
[INET_ADDRSTRLEN
];
162 pim_inet4_dump("<grp?>", up
->sg
.grp
, group_str
, sizeof(group_str
));
163 zlog_warn("%s: failure encoding group address %s: space left=%d",
164 __PRETTY_FUNCTION__
, group_str
, remain
);
168 remain
= end
- pim_msg_curr
;
170 zlog_warn("%s: sources will not fit: space left=%d",
171 __PRETTY_FUNCTION__
, remain
);
175 /* number of joined sources */
176 joins
= (uint16_t *)pim_msg_curr
;
177 *joins
= htons(is_join
? 1 : 0);
181 /* number of pruned sources */
182 prunes
= (uint16_t *)pim_msg_curr
;
183 *prunes
= htons(is_join
? 0 : 1);
187 remain
= end
- pim_msg_curr
;
188 if (up
->sg
.src
.s_addr
== INADDR_ANY
)
190 struct pim_rpf
*rpf
= pim_rp_g (up
->sg
.grp
);
191 bits
= PIM_ENCODE_SPARSE_BIT
| PIM_ENCODE_WC_BIT
| PIM_ENCODE_RPT_BIT
;
192 stosend
= rpf
->rpf_addr
.u
.prefix4
;
196 bits
= PIM_ENCODE_SPARSE_BIT
;
197 stosend
= up
->sg
.src
;
199 pim_msg_curr
= pim_msg_addr_encode_ipv4_source (pim_msg_curr
, remain
, stosend
, bits
);
201 char source_str
[INET_ADDRSTRLEN
];
202 pim_inet4_dump("<src?>", up
->sg
.src
, source_str
, sizeof(source_str
));
203 zlog_warn("%s: failure encoding source address %s: space left=%d",
204 __PRETTY_FUNCTION__
, source_str
, remain
);
207 remain
= pim_msg_curr
- pim_msg
;
210 * This is not implemented correctly at this point in time
214 if (up
->sg
.src
.s_addr
== INADDR_ANY
)
216 struct pim_upstream
*child
;
217 struct listnode
*up_node
;
220 zlog_debug ("%s: Considering (%s) children for (S,G,rpt) prune",
221 __PRETTY_FUNCTION__
, up
->sg_str
);
222 for (ALL_LIST_ELEMENTS_RO (up
->sources
, up_node
, child
))
224 if (child
->sptbit
== PIM_UPSTREAM_SPTBIT_TRUE
)
226 if (!pim_rpf_is_same(&up
->rpf
, &child
->rpf
))
229 if (PIM_DEBUG_PIM_PACKETS
)
230 zlog_debug ("%s: SPT Bit and RPF'(%s) != RPF'(S,G): Add Prune (%s,rpt) to compound message",
231 __PRETTY_FUNCTION__
, up
->sg_str
, child
->sg_str
);
234 if (PIM_DEBUG_PIM_PACKETS
)
235 zlog_debug ("%s: SPT Bit and RPF'(%s) == RPF'(S,G): Not adding Prune for (%s,rpt)",
236 __PRETTY_FUNCTION__
, up
->sg_str
, child
->sg_str
);
238 else if (pim_upstream_is_sg_rpt (child
))
240 if (pim_upstream_empty_inherited_olist (child
))
243 if (PIM_DEBUG_PIM_PACKETS
)
244 zlog_debug ("%s: inherited_olist(%s,rpt) is NULL, Add Prune to compound message",
245 __PRETTY_FUNCTION__
, child
->sg_str
);
247 else if (!pim_rpf_is_same (&up
->rpf
, &child
->rpf
))
250 if (PIM_DEBUG_PIM_PACKETS
)
251 zlog_debug ("%s: RPF'(%s) != RPF'(%s,rpt), Add Prune to compound message",
252 __PRETTY_FUNCTION__
, up
->sg_str
, child
->sg_str
);
255 if (PIM_DEBUG_PIM_PACKETS
)
256 zlog_debug ("%s: RPF'(%s) == RPF'(%s,rpt), Do not add Prune to compound message",
257 __PRETTY_FUNCTION__
, up
->sg_str
, child
->sg_str
);
260 if (PIM_DEBUG_PIM_PACKETS
)
261 zlog_debug ("%s: SPT bit is not set for (%s)",
262 __PRETTY_FUNCTION__
, child
->sg_str
);
265 pim_msg_curr
= pim_msg_addr_encode_ipv4_source (pim_msg_curr
, remain
,
267 PIM_ENCODE_SPARSE_BIT
| PIM_ENCODE_RPT_BIT
);
268 remain
= pim_msg_curr
- pim_msg
;
269 *prunes
= htons(ntohs(*prunes
) + 1);
275 pim_msg_build_header (pim_msg
, remain
, PIM_MSG_TYPE_JOIN_PRUNE
);