]>
git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_static.c
2 PIM for Quagga: add the ability to configure multicast static routes
3 Copyright (C) 2014 Nathan Bahr, ATCorp
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,
32 #include "pim_static.h"
35 #include "pim_iface.h"
37 void pim_static_route_free(struct static_route
*s_route
)
39 XFREE(MTYPE_PIM_STATIC_ROUTE
, s_route
);
42 static struct static_route
* static_route_alloc()
44 struct static_route
*s_route
;
46 s_route
= XCALLOC(MTYPE_PIM_STATIC_ROUTE
, sizeof(*s_route
));
48 zlog_err("PIM XCALLOC(%zu) failure", sizeof(*s_route
));
54 static struct static_route
*static_route_new(unsigned int iif
,
57 struct in_addr source
)
59 struct static_route
* s_route
;
60 s_route
= static_route_alloc();
65 s_route
->group
= group
;
66 s_route
->source
= source
;
68 s_route
->oif_ttls
[oif
] = 1;
69 s_route
->c_oil
.oil_ref_count
= 1;
70 s_route
->c_oil
.oil
.mfcc_origin
= source
;
71 s_route
->c_oil
.oil
.mfcc_mcastgrp
= group
;
72 s_route
->c_oil
.oil
.mfcc_parent
= iif
;
73 s_route
->c_oil
.oil
.mfcc_ttls
[oif
] = 1;
74 s_route
->c_oil
.oif_creation
[oif
] = pim_time_monotonic_sec();
80 int pim_static_add(struct interface
*iif
, struct interface
*oif
, struct in_addr group
, struct in_addr source
)
82 struct listnode
*node
= NULL
;
83 struct static_route
*s_route
= NULL
;
84 struct static_route
*original_s_route
= NULL
;
85 struct pim_interface
*pim_iif
= iif
? iif
->info
: NULL
;
86 struct pim_interface
*pim_oif
= oif
? oif
->info
: NULL
;
87 ifindex_t iif_index
= pim_iif
? pim_iif
->mroute_vif_index
: 0;
88 ifindex_t oif_index
= pim_oif
? pim_oif
->mroute_vif_index
: 0;
90 if (!iif_index
|| !oif_index
) {
91 zlog_warn("%s %s: Unable to add static route: Invalid interface index(iif=%d,oif=%d)",
92 __FILE__
, __PRETTY_FUNCTION__
,
98 #ifdef PIM_ENFORCE_LOOPFREE_MFC
99 if (iif_index
== oif_index
) {
100 /* looped MFC entry */
101 zlog_warn("%s %s: Unable to add static route: Looped MFC entry(iif=%d,oif=%d)",
102 __FILE__
, __PRETTY_FUNCTION__
,
109 for (ALL_LIST_ELEMENTS_RO(qpim_static_route_list
, node
, s_route
)) {
110 if (s_route
->group
.s_addr
== group
.s_addr
&&
111 s_route
->source
.s_addr
== source
.s_addr
) {
112 if (s_route
->iif
== iif_index
&&
113 s_route
->oif_ttls
[oif_index
]) {
114 char gifaddr_str
[INET_ADDRSTRLEN
];
115 char sifaddr_str
[INET_ADDRSTRLEN
];
116 pim_inet4_dump("<ifaddr?>", group
, gifaddr_str
, sizeof(gifaddr_str
));
117 pim_inet4_dump("<ifaddr?>", source
, sifaddr_str
, sizeof(sifaddr_str
));
118 zlog_warn("%s %s: Unable to add static route: Route already exists (iif=%d,oif=%d,group=%s,source=%s)",
119 __FILE__
, __PRETTY_FUNCTION__
,
127 /* Ok, from here on out we will be making changes to the s_route structure, but if
128 * for some reason we fail to commit these changes to the kernel, we want to be able
129 * restore the state of the list. So copy the node data and if need be, we can copy
132 original_s_route
= static_route_alloc();
133 if (!original_s_route
) {
136 memcpy(original_s_route
, s_route
, sizeof(struct static_route
));
138 /* Route exists and has the same input interface, but adding a new output interface */
139 if (s_route
->iif
== iif_index
) {
140 s_route
->oif_ttls
[oif_index
] = 1;
141 s_route
->c_oil
.oil
.mfcc_ttls
[oif_index
] = 1;
142 s_route
->c_oil
.oif_creation
[oif_index
] = pim_time_monotonic_sec();
143 ++s_route
->c_oil
.oil_ref_count
;
145 /* input interface changed */
146 s_route
->iif
= iif_index
;
147 s_route
->c_oil
.oil
.mfcc_parent
= iif_index
;
149 #ifdef PIM_ENFORCE_LOOPFREE_MFC
150 /* check to make sure the new input was not an old output */
151 if (s_route
->oif_ttls
[iif_index
]) {
152 s_route
->oif_ttls
[iif_index
] = 0;
153 s_route
->c_oil
.oif_creation
[iif_index
] = 0;
154 s_route
->c_oil
.oil
.mfcc_ttls
[iif_index
] = 0;
155 --s_route
->c_oil
.oil_ref_count
;
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
] = pim_time_monotonic_sec();
163 s_route
->c_oil
.oil
.mfcc_ttls
[oif_index
] = 1;
164 ++s_route
->c_oil
.oil_ref_count
;
172 /* If node is null then we reached the end of the list without finding a match */
174 s_route
= static_route_new(iif_index
, oif_index
, group
, source
);
175 listnode_add(qpim_static_route_list
, s_route
);
178 if (pim_mroute_add(&s_route
->c_oil
, __PRETTY_FUNCTION__
))
180 char gifaddr_str
[INET_ADDRSTRLEN
];
181 char sifaddr_str
[INET_ADDRSTRLEN
];
182 pim_inet4_dump("<ifaddr?>", group
, gifaddr_str
, sizeof(gifaddr_str
));
183 pim_inet4_dump("<ifaddr?>", source
, sifaddr_str
, sizeof(sifaddr_str
));
184 zlog_warn("%s %s: Unable to add static route(iif=%d,oif=%d,group=%s,source=%s)",
185 __FILE__
, __PRETTY_FUNCTION__
,
191 /* Need to put s_route back to the way it was */
192 if (original_s_route
) {
193 memcpy(s_route
, original_s_route
, sizeof(struct static_route
));
195 /* we never stored off a copy, so it must have been a fresh new route */
196 listnode_delete(qpim_static_route_list
, s_route
);
197 pim_static_route_free(s_route
);
200 if (original_s_route
) {
201 pim_static_route_free(original_s_route
);
207 /* Make sure we free the memory for the route copy if used */
208 if (original_s_route
) {
209 pim_static_route_free(original_s_route
);
212 if (PIM_DEBUG_STATIC
) {
213 char gifaddr_str
[INET_ADDRSTRLEN
];
214 char sifaddr_str
[INET_ADDRSTRLEN
];
215 pim_inet4_dump("<ifaddr?>", group
, gifaddr_str
, sizeof(gifaddr_str
));
216 pim_inet4_dump("<ifaddr?>", source
, sifaddr_str
, sizeof(sifaddr_str
));
217 zlog_debug("%s: Static route added(iif=%d,oif=%d,group=%s,source=%s)",
228 int pim_static_del(struct interface
*iif
, struct interface
*oif
, struct in_addr group
, struct in_addr source
)
230 struct listnode
*node
= NULL
;
231 struct listnode
*nextnode
= NULL
;
232 struct static_route
*s_route
= NULL
;
233 struct pim_interface
*pim_iif
= iif
? iif
->info
: 0;
234 struct pim_interface
*pim_oif
= oif
? oif
->info
: 0;
235 ifindex_t iif_index
= pim_iif
? pim_iif
->mroute_vif_index
: 0;
236 ifindex_t oif_index
= pim_oif
? pim_oif
->mroute_vif_index
: 0;
238 if (!iif_index
|| !oif_index
) {
239 zlog_warn("%s %s: Unable to remove static route: Invalid interface index(iif=%d,oif=%d)",
240 __FILE__
, __PRETTY_FUNCTION__
,
246 for (ALL_LIST_ELEMENTS(qpim_static_route_list
, node
, nextnode
, s_route
)) {
247 if (s_route
->iif
== iif_index
&&
248 s_route
->group
.s_addr
== group
.s_addr
&&
249 s_route
->source
.s_addr
== source
.s_addr
&&
250 s_route
->oif_ttls
[oif_index
]) {
251 s_route
->oif_ttls
[oif_index
] = 0;
252 s_route
->c_oil
.oil
.mfcc_ttls
[oif_index
] = 0;
253 --s_route
->c_oil
.oil_ref_count
;
255 /* If there are no more outputs then delete the whole route, otherwise set the route with the new outputs */
256 if (s_route
->c_oil
.oil_ref_count
<= 0 ?
257 pim_mroute_del(&s_route
->c_oil
, __PRETTY_FUNCTION__
) : pim_mroute_add(&s_route
->c_oil
, __PRETTY_FUNCTION__
)) {
258 char gifaddr_str
[INET_ADDRSTRLEN
];
259 char sifaddr_str
[INET_ADDRSTRLEN
];
260 pim_inet4_dump("<ifaddr?>", group
, gifaddr_str
, sizeof(gifaddr_str
));
261 pim_inet4_dump("<ifaddr?>", source
, sifaddr_str
, sizeof(sifaddr_str
));
262 zlog_warn("%s %s: Unable to remove static route(iif=%d,oif=%d,group=%s,source=%s)",
263 __FILE__
, __PRETTY_FUNCTION__
,
269 s_route
->oif_ttls
[oif_index
] = 1;
270 s_route
->c_oil
.oil
.mfcc_ttls
[oif_index
] = 1;
271 ++s_route
->c_oil
.oil_ref_count
;
276 s_route
->c_oil
.oif_creation
[oif_index
] = 0;
278 if (s_route
->c_oil
.oil_ref_count
<= 0) {
279 listnode_delete(qpim_static_route_list
, s_route
);
280 pim_static_route_free(s_route
);
283 if (PIM_DEBUG_STATIC
) {
284 char gifaddr_str
[INET_ADDRSTRLEN
];
285 char sifaddr_str
[INET_ADDRSTRLEN
];
286 pim_inet4_dump("<ifaddr?>", group
, gifaddr_str
, sizeof(gifaddr_str
));
287 pim_inet4_dump("<ifaddr?>", source
, sifaddr_str
, sizeof(sifaddr_str
));
288 zlog_debug("%s: Static route removed(iif=%d,oif=%d,group=%s,source=%s)",
301 char gifaddr_str
[INET_ADDRSTRLEN
];
302 char sifaddr_str
[INET_ADDRSTRLEN
];
303 pim_inet4_dump("<ifaddr?>", group
, gifaddr_str
, sizeof(gifaddr_str
));
304 pim_inet4_dump("<ifaddr?>", source
, sifaddr_str
, sizeof(sifaddr_str
));
305 zlog_warn("%s %s: Unable to remove static route: Route does not exist(iif=%d,oif=%d,group=%s,source=%s)",
306 __FILE__
, __PRETTY_FUNCTION__
,
318 pim_static_write_mroute (struct vty
*vty
, struct interface
*ifp
)
320 struct listnode
*node
;
321 struct static_route
*sroute
;
323 char sbuf
[INET_ADDRSTRLEN
];
324 char gbuf
[INET_ADDRSTRLEN
];
326 for (ALL_LIST_ELEMENTS_RO (qpim_static_route_list
, node
, sroute
))
328 pim_inet4_dump ("<ifaddr?>", sroute
->group
, gbuf
, sizeof (gbuf
));
329 pim_inet4_dump ("<ifaddr?>", sroute
->source
, sbuf
, sizeof (sbuf
));
330 if (sroute
->iif
== ifp
->ifindex
)
333 for (i
= 0; i
< MAXVIFS
; i
++)
334 if (sroute
->oif_ttls
[i
])
336 struct interface
*oifp
= if_lookup_by_index (i
);
337 vty_out (vty
, " ip mroute %s %s %s%s", oifp
->name
, gbuf
, sbuf
, VTY_NEWLINE
);