]> git.proxmox.com Git - mirror_frr.git/blame - pimd/pim_static.c
lib: enforce vrf_name_to_id by returning default_vrf when name is null
[mirror_frr.git] / pimd / pim_static.c
CommitLineData
6250610a 1/*
896014f4
DL
2 * PIM for Quagga: add the ability to configure multicast static routes
3 * Copyright (C) 2014 Nathan Bahr, ATCorp
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 */
6250610a 19
f91f89bc
DS
20#include <zebra.h>
21
22#include "vty.h"
744d91b3 23#include "if.h"
9867746a
DS
24#include "log.h"
25#include "memory.h"
26#include "linklist.h"
f91f89bc 27
9867746a
DS
28#include "pimd.h"
29#include "pim_oil.h"
6250610a
JAG
30#include "pim_static.h"
31#include "pim_time.h"
32#include "pim_str.h"
6250610a 33#include "pim_iface.h"
6250610a
JAG
34
35void pim_static_route_free(struct static_route *s_route)
36{
d62a17ae 37 XFREE(MTYPE_PIM_STATIC_ROUTE, s_route);
6250610a
JAG
38}
39
d62a17ae 40static struct static_route *static_route_alloc()
6250610a 41{
2b57b948 42 return XCALLOC(MTYPE_PIM_STATIC_ROUTE, sizeof(struct static_route));
6250610a
JAG
43}
44
d62a17ae 45static struct static_route *static_route_new(unsigned int iif, unsigned int oif,
46 struct in_addr group,
47 struct in_addr source)
6250610a 48{
d62a17ae 49 struct static_route *s_route;
50 s_route = static_route_alloc();
d62a17ae 51
52 s_route->group = group;
53 s_route->source = source;
54 s_route->iif = iif;
55 s_route->oif_ttls[oif] = 1;
56 s_route->c_oil.oil_ref_count = 1;
57 s_route->c_oil.oil.mfcc_origin = source;
58 s_route->c_oil.oil.mfcc_mcastgrp = group;
59 s_route->c_oil.oil.mfcc_parent = iif;
60 s_route->c_oil.oil.mfcc_ttls[oif] = 1;
61 s_route->c_oil.oif_creation[oif] = pim_time_monotonic_sec();
62
63 return s_route;
6250610a
JAG
64}
65
66
4e0bc0f0
DS
67int pim_static_add(struct pim_instance *pim, struct interface *iif,
68 struct interface *oif, struct in_addr group,
69 struct in_addr source)
6250610a 70{
d62a17ae 71 struct listnode *node = NULL;
72 struct static_route *s_route = NULL;
73 struct static_route *original_s_route = NULL;
74 struct pim_interface *pim_iif = iif ? iif->info : NULL;
75 struct pim_interface *pim_oif = oif ? oif->info : NULL;
76 ifindex_t iif_index = pim_iif ? pim_iif->mroute_vif_index : 0;
77 ifindex_t oif_index = pim_oif ? pim_oif->mroute_vif_index : 0;
78
79 if (!iif_index || !oif_index) {
80 zlog_warn(
81 "%s %s: Unable to add static route: Invalid interface index(iif=%d,oif=%d)",
82 __FILE__, __PRETTY_FUNCTION__, iif_index, oif_index);
83 return -2;
84 }
6250610a
JAG
85
86#ifdef PIM_ENFORCE_LOOPFREE_MFC
d62a17ae 87 if (iif_index == oif_index) {
88 /* looped MFC entry */
89 zlog_warn(
90 "%s %s: Unable to add static route: Looped MFC entry(iif=%d,oif=%d)",
91 __FILE__, __PRETTY_FUNCTION__, iif_index, oif_index);
92 return -4;
93 }
6250610a 94#endif
72ec4672
DS
95 if (iif->vrf_id != oif->vrf_id) {
96 return -3;
97 }
98
4e0bc0f0 99 for (ALL_LIST_ELEMENTS_RO(pim->static_routes, node, s_route)) {
d62a17ae 100 if (s_route->group.s_addr == group.s_addr
101 && s_route->source.s_addr == source.s_addr) {
102 if (s_route->iif == iif_index
103 && s_route->oif_ttls[oif_index]) {
104 char gifaddr_str[INET_ADDRSTRLEN];
105 char sifaddr_str[INET_ADDRSTRLEN];
106 pim_inet4_dump("<ifaddr?>", group, gifaddr_str,
107 sizeof(gifaddr_str));
108 pim_inet4_dump("<ifaddr?>", source, sifaddr_str,
109 sizeof(sifaddr_str));
110 zlog_warn(
111 "%s %s: Unable to add static route: Route already exists (iif=%d,oif=%d,group=%s,source=%s)",
112 __FILE__, __PRETTY_FUNCTION__,
113 iif_index, oif_index, gifaddr_str,
114 sifaddr_str);
115 return -3;
116 }
117
118 /* Ok, from here on out we will be making changes to the
119 * s_route structure, but if
120 * for some reason we fail to commit these changes to
121 * the kernel, we want to be able
122 * restore the state of the list. So copy the node data
123 * and if need be, we can copy
124 * back if it fails.
125 */
126 original_s_route = static_route_alloc();
127 if (!original_s_route) {
128 return -5;
129 }
130 memcpy(original_s_route, s_route,
131 sizeof(struct static_route));
132
133 /* Route exists and has the same input interface, but
134 * adding a new output interface */
135 if (s_route->iif == iif_index) {
136 s_route->oif_ttls[oif_index] = 1;
137 s_route->c_oil.oil.mfcc_ttls[oif_index] = 1;
138 s_route->c_oil.oif_creation[oif_index] =
139 pim_time_monotonic_sec();
140 ++s_route->c_oil.oil_ref_count;
141 } else {
142 /* input interface changed */
143 s_route->iif = iif_index;
144 s_route->c_oil.oil.mfcc_parent = iif_index;
6250610a
JAG
145
146#ifdef PIM_ENFORCE_LOOPFREE_MFC
d62a17ae 147 /* check to make sure the new input was not an
148 * old output */
149 if (s_route->oif_ttls[iif_index]) {
150 s_route->oif_ttls[iif_index] = 0;
151 s_route->c_oil.oif_creation[iif_index] =
152 0;
153 s_route->c_oil.oil
154 .mfcc_ttls[iif_index] = 0;
155 --s_route->c_oil.oil_ref_count;
156 }
6250610a
JAG
157#endif
158
d62a17ae 159 /* now add the new output, if it is new */
160 if (!s_route->oif_ttls[oif_index]) {
161 s_route->oif_ttls[oif_index] = 1;
162 s_route->c_oil.oif_creation[oif_index] =
163 pim_time_monotonic_sec();
164 s_route->c_oil.oil
165 .mfcc_ttls[oif_index] = 1;
166 ++s_route->c_oil.oil_ref_count;
167 }
168 }
169
170 break;
171 }
172 }
173
174 /* If node is null then we reached the end of the list without finding a
175 * match */
176 if (!node) {
177 s_route = static_route_new(iif_index, oif_index, group, source);
4e0bc0f0 178 listnode_add(pim->static_routes, s_route);
d62a17ae 179 }
180
2e4e8571
DS
181 s_route->c_oil.pim = pim;
182
d62a17ae 183 if (pim_mroute_add(&s_route->c_oil, __PRETTY_FUNCTION__)) {
184 char gifaddr_str[INET_ADDRSTRLEN];
185 char sifaddr_str[INET_ADDRSTRLEN];
186 pim_inet4_dump("<ifaddr?>", group, gifaddr_str,
187 sizeof(gifaddr_str));
188 pim_inet4_dump("<ifaddr?>", source, sifaddr_str,
189 sizeof(sifaddr_str));
190 zlog_warn(
191 "%s %s: Unable to add static route(iif=%d,oif=%d,group=%s,source=%s)",
192 __FILE__, __PRETTY_FUNCTION__, iif_index, oif_index,
193 gifaddr_str, sifaddr_str);
194
195 /* Need to put s_route back to the way it was */
196 if (original_s_route) {
197 memcpy(s_route, original_s_route,
198 sizeof(struct static_route));
199 } else {
200 /* we never stored off a copy, so it must have been a
201 * fresh new route */
4e0bc0f0 202 listnode_delete(pim->static_routes, s_route);
d62a17ae 203 pim_static_route_free(s_route);
204 }
205
206 if (original_s_route) {
207 pim_static_route_free(original_s_route);
208 }
209
210 return -1;
211 }
212
213 /* Make sure we free the memory for the route copy if used */
214 if (original_s_route) {
215 pim_static_route_free(original_s_route);
216 }
217
218 if (PIM_DEBUG_STATIC) {
219 char gifaddr_str[INET_ADDRSTRLEN];
220 char sifaddr_str[INET_ADDRSTRLEN];
221 pim_inet4_dump("<ifaddr?>", group, gifaddr_str,
222 sizeof(gifaddr_str));
223 pim_inet4_dump("<ifaddr?>", source, sifaddr_str,
224 sizeof(sifaddr_str));
225 zlog_debug(
226 "%s: Static route added(iif=%d,oif=%d,group=%s,source=%s)",
227 __PRETTY_FUNCTION__, iif_index, oif_index, gifaddr_str,
228 sifaddr_str);
229 }
230
231 return 0;
6250610a
JAG
232}
233
4e0bc0f0
DS
234int pim_static_del(struct pim_instance *pim, struct interface *iif,
235 struct interface *oif, struct in_addr group,
236 struct in_addr source)
6250610a 237{
d62a17ae 238 struct listnode *node = NULL;
239 struct listnode *nextnode = NULL;
240 struct static_route *s_route = NULL;
241 struct pim_interface *pim_iif = iif ? iif->info : 0;
242 struct pim_interface *pim_oif = oif ? oif->info : 0;
243 ifindex_t iif_index = pim_iif ? pim_iif->mroute_vif_index : 0;
244 ifindex_t oif_index = pim_oif ? pim_oif->mroute_vif_index : 0;
245
246 if (!iif_index || !oif_index) {
247 zlog_warn(
248 "%s %s: Unable to remove static route: Invalid interface index(iif=%d,oif=%d)",
249 __FILE__, __PRETTY_FUNCTION__, iif_index, oif_index);
250 return -2;
251 }
252
4e0bc0f0 253 for (ALL_LIST_ELEMENTS(pim->static_routes, node, nextnode, s_route)) {
d62a17ae 254 if (s_route->iif == iif_index
255 && s_route->group.s_addr == group.s_addr
256 && s_route->source.s_addr == source.s_addr
257 && s_route->oif_ttls[oif_index]) {
258 s_route->oif_ttls[oif_index] = 0;
259 s_route->c_oil.oil.mfcc_ttls[oif_index] = 0;
260 --s_route->c_oil.oil_ref_count;
261
262 /* If there are no more outputs then delete the whole
263 * route, otherwise set the route with the new outputs
264 */
265 if (s_route->c_oil.oil_ref_count <= 0
266 ? pim_mroute_del(&s_route->c_oil,
267 __PRETTY_FUNCTION__)
268 : pim_mroute_add(&s_route->c_oil,
269 __PRETTY_FUNCTION__)) {
270 char gifaddr_str[INET_ADDRSTRLEN];
271 char sifaddr_str[INET_ADDRSTRLEN];
272 pim_inet4_dump("<ifaddr?>", group, gifaddr_str,
273 sizeof(gifaddr_str));
274 pim_inet4_dump("<ifaddr?>", source, sifaddr_str,
275 sizeof(sifaddr_str));
276 zlog_warn(
277 "%s %s: Unable to remove static route(iif=%d,oif=%d,group=%s,source=%s)",
278 __FILE__, __PRETTY_FUNCTION__,
279 iif_index, oif_index, gifaddr_str,
280 sifaddr_str);
281
282 s_route->oif_ttls[oif_index] = 1;
283 s_route->c_oil.oil.mfcc_ttls[oif_index] = 1;
284 ++s_route->c_oil.oil_ref_count;
285
286 return -1;
287 }
288
289 s_route->c_oil.oif_creation[oif_index] = 0;
290
291 if (s_route->c_oil.oil_ref_count <= 0) {
4e0bc0f0 292 listnode_delete(pim->static_routes, s_route);
d62a17ae 293 pim_static_route_free(s_route);
294 }
295
296 if (PIM_DEBUG_STATIC) {
297 char gifaddr_str[INET_ADDRSTRLEN];
298 char sifaddr_str[INET_ADDRSTRLEN];
299 pim_inet4_dump("<ifaddr?>", group, gifaddr_str,
300 sizeof(gifaddr_str));
301 pim_inet4_dump("<ifaddr?>", source, sifaddr_str,
302 sizeof(sifaddr_str));
303 zlog_debug(
304 "%s: Static route removed(iif=%d,oif=%d,group=%s,source=%s)",
305 __PRETTY_FUNCTION__, iif_index,
306 oif_index, gifaddr_str, sifaddr_str);
307 }
308
309 break;
310 }
311 }
312
313 if (!node) {
314 char gifaddr_str[INET_ADDRSTRLEN];
315 char sifaddr_str[INET_ADDRSTRLEN];
316 pim_inet4_dump("<ifaddr?>", group, gifaddr_str,
317 sizeof(gifaddr_str));
318 pim_inet4_dump("<ifaddr?>", source, sifaddr_str,
319 sizeof(sifaddr_str));
320 zlog_warn(
321 "%s %s: Unable to remove static route: Route does not exist(iif=%d,oif=%d,group=%s,source=%s)",
322 __FILE__, __PRETTY_FUNCTION__, iif_index, oif_index,
323 gifaddr_str, sifaddr_str);
324 return -3;
325 }
326
327 return 0;
6250610a 328}
f91f89bc 329
64c86530 330int pim_static_write_mroute(struct pim_instance *pim, struct vty *vty,
4e0bc0f0 331 struct interface *ifp)
f91f89bc 332{
d62a17ae 333 struct pim_interface *pim_ifp = ifp->info;
334 struct listnode *node;
335 struct static_route *sroute;
336 int count = 0;
337 char sbuf[INET_ADDRSTRLEN];
338 char gbuf[INET_ADDRSTRLEN];
339
340 if (!pim_ifp)
341 return 0;
342
4e0bc0f0 343 for (ALL_LIST_ELEMENTS_RO(pim->static_routes, node, sroute)) {
d62a17ae 344 pim_inet4_dump("<ifaddr?>", sroute->group, gbuf, sizeof(gbuf));
345 pim_inet4_dump("<ifaddr?>", sroute->source, sbuf, sizeof(sbuf));
346 if (sroute->iif == pim_ifp->mroute_vif_index) {
347 int i;
348 for (i = 0; i < MAXVIFS; i++)
349 if (sroute->oif_ttls[i]) {
350 struct interface *oifp =
7cfc7bcf
DS
351 pim_if_find_by_vif_index(pim,
352 i);
d62a17ae 353 if (sroute->source.s_addr == 0)
354 vty_out(vty,
355 " ip mroute %s %s\n",
356 oifp->name, gbuf);
357 else
358 vty_out(vty,
359 " ip mroute %s %s %s\n",
360 oifp->name, gbuf, sbuf);
361 count++;
362 }
363 }
364 }
365
366 return count;
f91f89bc 367}