]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Interface related function for RIP. |
2 | * Copyright (C) 1997, 98 Kunihiro Ishiguro <kunihiro@zebra.org> | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 19 | */ |
20 | ||
21 | #include <zebra.h> | |
22 | ||
23 | #include "command.h" | |
24 | #include "if.h" | |
25 | #include "sockunion.h" | |
26 | #include "prefix.h" | |
27 | #include "memory.h" | |
28 | #include "network.h" | |
29 | #include "table.h" | |
30 | #include "log.h" | |
31 | #include "stream.h" | |
32 | #include "thread.h" | |
33 | #include "zclient.h" | |
34 | #include "filter.h" | |
35 | #include "sockopt.h" | |
edd7c245 | 36 | #include "privs.h" |
77cbe4a1 | 37 | #include "lib_errors.h" |
f0ab22fb | 38 | #include "northbound_cli.h" |
718e3744 | 39 | |
40 | #include "zebra/connected.h" | |
41 | ||
42 | #include "ripd/ripd.h" | |
43 | #include "ripd/rip_debug.h" | |
dc63bfd4 | 44 | #include "ripd/rip_interface.h" |
6b0655a2 | 45 | |
814a2585 DL |
46 | DEFINE_MTYPE_STATIC(RIPD, RIP_INTERFACE, "RIP interface") |
47 | DEFINE_MTYPE(RIPD, RIP_INTERFACE_STRING, "RIP Interface String") | |
d62a17ae | 48 | DEFINE_HOOK(rip_ifaddr_add, (struct connected * ifc), (ifc)) |
49 | DEFINE_HOOK(rip_ifaddr_del, (struct connected * ifc), (ifc)) | |
3012671f | 50 | |
dc63bfd4 | 51 | /* static prototypes */ |
d62a17ae | 52 | static void rip_enable_apply(struct interface *); |
53 | static void rip_passive_interface_apply(struct interface *); | |
dc63bfd4 | 54 | static int rip_if_down(struct interface *ifp); |
045c5389 | 55 | static int rip_enable_if_lookup(struct rip *rip, const char *ifname); |
d62a17ae | 56 | static int rip_enable_network_lookup2(struct connected *connected); |
045c5389 | 57 | static void rip_enable_apply_all(struct rip *rip); |
6b0655a2 | 58 | |
d62a17ae | 59 | const struct message ri_version_msg[] = {{RI_RIP_VERSION_1, "1"}, |
60 | {RI_RIP_VERSION_2, "2"}, | |
61 | {RI_RIP_VERSION_1_AND_2, "1 2"}, | |
62 | {RI_RIP_VERSION_NONE, "none"}, | |
63 | {0}}; | |
718e3744 | 64 | |
718e3744 | 65 | /* Join to the RIP version 2 multicast group. */ |
d62a17ae | 66 | static int ipv4_multicast_join(int sock, struct in_addr group, |
67 | struct in_addr ifa, ifindex_t ifindex) | |
718e3744 | 68 | { |
d62a17ae | 69 | int ret; |
718e3744 | 70 | |
d62a17ae | 71 | ret = setsockopt_ipv4_multicast(sock, IP_ADD_MEMBERSHIP, ifa, |
72 | group.s_addr, ifindex); | |
718e3744 | 73 | |
d62a17ae | 74 | if (ret < 0) |
75 | zlog_info("can't setsockopt IP_ADD_MEMBERSHIP %s", | |
76 | safe_strerror(errno)); | |
718e3744 | 77 | |
d62a17ae | 78 | return ret; |
718e3744 | 79 | } |
80 | ||
81 | /* Leave from the RIP version 2 multicast group. */ | |
d62a17ae | 82 | static int ipv4_multicast_leave(int sock, struct in_addr group, |
83 | struct in_addr ifa, ifindex_t ifindex) | |
718e3744 | 84 | { |
d62a17ae | 85 | int ret; |
718e3744 | 86 | |
d62a17ae | 87 | ret = setsockopt_ipv4_multicast(sock, IP_DROP_MEMBERSHIP, ifa, |
88 | group.s_addr, ifindex); | |
718e3744 | 89 | |
d62a17ae | 90 | if (ret < 0) |
91 | zlog_info("can't setsockopt IP_DROP_MEMBERSHIP"); | |
718e3744 | 92 | |
d62a17ae | 93 | return ret; |
718e3744 | 94 | } |
6b0655a2 | 95 | |
d62a17ae | 96 | static void rip_interface_reset(struct rip_interface *); |
1dec2166 | 97 | |
718e3744 | 98 | /* Allocate new RIP's interface configuration. */ |
d62a17ae | 99 | static struct rip_interface *rip_interface_new(void) |
718e3744 | 100 | { |
d62a17ae | 101 | struct rip_interface *ri; |
718e3744 | 102 | |
d62a17ae | 103 | ri = XCALLOC(MTYPE_RIP_INTERFACE, sizeof(struct rip_interface)); |
718e3744 | 104 | |
d62a17ae | 105 | rip_interface_reset(ri); |
718e3744 | 106 | |
d62a17ae | 107 | return ri; |
718e3744 | 108 | } |
109 | ||
d62a17ae | 110 | void rip_interface_multicast_set(int sock, struct connected *connected) |
718e3744 | 111 | { |
d62a17ae | 112 | struct in_addr addr; |
4a3867d0 | 113 | |
d62a17ae | 114 | assert(connected != NULL); |
4a3867d0 | 115 | |
d62a17ae | 116 | addr = CONNECTED_ID(connected)->u.prefix4; |
117 | ||
118 | if (setsockopt_ipv4_multicast_if(sock, addr, connected->ifp->ifindex) | |
119 | < 0) { | |
120 | zlog_warn( | |
121 | "Can't setsockopt IP_MULTICAST_IF on fd %d to " | |
122 | "ifindex %d for interface %s", | |
123 | sock, connected->ifp->ifindex, connected->ifp->name); | |
124 | } | |
4a3867d0 | 125 | |
d62a17ae | 126 | return; |
3fb9cd6e | 127 | } |
718e3744 | 128 | |
129 | /* Send RIP request packet to specified interface. */ | |
d7c0a89a | 130 | static void rip_request_interface_send(struct interface *ifp, uint8_t version) |
718e3744 | 131 | { |
d62a17ae | 132 | struct sockaddr_in to; |
718e3744 | 133 | |
d62a17ae | 134 | /* RIPv2 support multicast. */ |
135 | if (version == RIPv2 && if_is_multicast(ifp)) { | |
718e3744 | 136 | |
d62a17ae | 137 | if (IS_RIP_DEBUG_EVENT) |
138 | zlog_debug("multicast request on %s", ifp->name); | |
718e3744 | 139 | |
d62a17ae | 140 | rip_request_send(NULL, ifp, version, NULL); |
141 | return; | |
142 | } | |
718e3744 | 143 | |
d62a17ae | 144 | /* RIPv1 and non multicast interface. */ |
145 | if (if_is_pointopoint(ifp) || if_is_broadcast(ifp)) { | |
146 | struct listnode *cnode, *cnnode; | |
147 | struct connected *connected; | |
148 | ||
149 | if (IS_RIP_DEBUG_EVENT) | |
150 | zlog_debug("broadcast request to %s", ifp->name); | |
151 | ||
152 | for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, | |
153 | connected)) { | |
2e37ad7f RW |
154 | if (connected->address->family != AF_INET) |
155 | continue; | |
156 | ||
157 | memset(&to, 0, sizeof(struct sockaddr_in)); | |
158 | to.sin_port = htons(RIP_PORT_DEFAULT); | |
159 | if (connected->destination) | |
160 | /* use specified broadcast or peer | |
161 | * destination addr */ | |
162 | to.sin_addr = connected->destination->u.prefix4; | |
163 | else if (connected->address->prefixlen | |
164 | < IPV4_MAX_PREFIXLEN) | |
165 | /* calculate the appropriate broadcast | |
166 | * address */ | |
167 | to.sin_addr.s_addr = ipv4_broadcast_addr( | |
168 | connected->address->u.prefix4.s_addr, | |
169 | connected->address->prefixlen); | |
170 | else | |
171 | /* do not know where to send the packet | |
172 | */ | |
173 | continue; | |
174 | ||
175 | if (IS_RIP_DEBUG_EVENT) | |
176 | zlog_debug("SEND request to %s", | |
177 | inet_ntoa(to.sin_addr)); | |
178 | ||
179 | rip_request_send(&to, ifp, version, connected); | |
d62a17ae | 180 | } |
718e3744 | 181 | } |
718e3744 | 182 | } |
183 | ||
184 | /* This will be executed when interface goes up. */ | |
d62a17ae | 185 | static void rip_request_interface(struct interface *ifp) |
718e3744 | 186 | { |
d62a17ae | 187 | struct rip_interface *ri; |
2e37ad7f | 188 | int vsend; |
718e3744 | 189 | |
d62a17ae | 190 | /* In default ripd doesn't send RIP_REQUEST to the loopback interface. |
191 | */ | |
192 | if (if_is_loopback(ifp)) | |
193 | return; | |
718e3744 | 194 | |
d62a17ae | 195 | /* If interface is down, don't send RIP packet. */ |
196 | if (!if_is_operative(ifp)) | |
197 | return; | |
718e3744 | 198 | |
d62a17ae | 199 | /* Fetch RIP interface information. */ |
200 | ri = ifp->info; | |
718e3744 | 201 | |
d62a17ae | 202 | /* If there is no version configuration in the interface, |
203 | use rip's version setting. */ | |
045c5389 | 204 | vsend = ((ri->ri_send == RI_RIP_UNSPEC) ? ri->rip->version_send |
2e37ad7f RW |
205 | : ri->ri_send); |
206 | if (vsend & RIPv1) | |
207 | rip_request_interface_send(ifp, RIPv1); | |
208 | if (vsend & RIPv2) | |
209 | rip_request_interface_send(ifp, RIPv2); | |
718e3744 | 210 | } |
211 | ||
2c239705 | 212 | #if 0 |
718e3744 | 213 | /* Send RIP request to the neighbor. */ |
dc63bfd4 | 214 | static void |
718e3744 | 215 | rip_request_neighbor (struct in_addr addr) |
216 | { | |
217 | struct sockaddr_in to; | |
218 | ||
219 | memset (&to, 0, sizeof (struct sockaddr_in)); | |
220 | to.sin_port = htons (RIP_PORT_DEFAULT); | |
221 | to.sin_addr = addr; | |
222 | ||
931cd54d | 223 | rip_request_send (&to, NULL, rip->version_send, NULL); |
718e3744 | 224 | } |
225 | ||
226 | /* Request routes at all interfaces. */ | |
dc63bfd4 | 227 | static void |
228 | rip_request_neighbor_all (void) | |
718e3744 | 229 | { |
230 | struct route_node *rp; | |
231 | ||
232 | if (! rip) | |
233 | return; | |
234 | ||
235 | if (IS_RIP_DEBUG_EVENT) | |
5d6c3779 | 236 | zlog_debug ("request to the all neighbor"); |
718e3744 | 237 | |
238 | /* Send request to all neighbor. */ | |
239 | for (rp = route_top (rip->neighbor); rp; rp = route_next (rp)) | |
240 | if (rp->info) | |
241 | rip_request_neighbor (rp->p.u.prefix4); | |
242 | } | |
2c239705 | 243 | #endif |
718e3744 | 244 | |
245 | /* Multicast packet receive socket. */ | |
d62a17ae | 246 | static int rip_multicast_join(struct interface *ifp, int sock) |
718e3744 | 247 | { |
d62a17ae | 248 | struct listnode *cnode; |
249 | struct connected *ifc; | |
718e3744 | 250 | |
d62a17ae | 251 | if (if_is_operative(ifp) && if_is_multicast(ifp)) { |
252 | if (IS_RIP_DEBUG_EVENT) | |
253 | zlog_debug("multicast join at %s", ifp->name); | |
718e3744 | 254 | |
d62a17ae | 255 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, ifc)) { |
256 | struct prefix_ipv4 *p; | |
257 | struct in_addr group; | |
258 | ||
259 | p = (struct prefix_ipv4 *)ifc->address; | |
260 | ||
261 | if (p->family != AF_INET) | |
262 | continue; | |
263 | ||
264 | group.s_addr = htonl(INADDR_RIP_GROUP); | |
265 | if (ipv4_multicast_join(sock, group, p->prefix, | |
266 | ifp->ifindex) | |
267 | < 0) | |
268 | return -1; | |
269 | else | |
270 | return 0; | |
271 | } | |
718e3744 | 272 | } |
d62a17ae | 273 | return 0; |
718e3744 | 274 | } |
275 | ||
276 | /* Leave from multicast group. */ | |
d62a17ae | 277 | static void rip_multicast_leave(struct interface *ifp, int sock) |
718e3744 | 278 | { |
d62a17ae | 279 | struct listnode *cnode; |
280 | struct connected *connected; | |
718e3744 | 281 | |
d62a17ae | 282 | if (if_is_up(ifp) && if_is_multicast(ifp)) { |
283 | if (IS_RIP_DEBUG_EVENT) | |
284 | zlog_debug("multicast leave from %s", ifp->name); | |
718e3744 | 285 | |
d62a17ae | 286 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) { |
287 | struct prefix_ipv4 *p; | |
288 | struct in_addr group; | |
289 | ||
290 | p = (struct prefix_ipv4 *)connected->address; | |
291 | ||
292 | if (p->family != AF_INET) | |
293 | continue; | |
294 | ||
295 | group.s_addr = htonl(INADDR_RIP_GROUP); | |
296 | if (ipv4_multicast_leave(sock, group, p->prefix, | |
297 | ifp->ifindex) | |
298 | == 0) | |
299 | return; | |
300 | } | |
301 | } | |
718e3744 | 302 | } |
303 | ||
304 | /* Is there and address on interface that I could use ? */ | |
d62a17ae | 305 | static int rip_if_ipv4_address_check(struct interface *ifp) |
718e3744 | 306 | { |
d62a17ae | 307 | struct listnode *nn; |
308 | struct connected *connected; | |
309 | int count = 0; | |
718e3744 | 310 | |
d62a17ae | 311 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, nn, connected)) { |
312 | struct prefix *p; | |
718e3744 | 313 | |
d62a17ae | 314 | p = connected->address; |
315 | ||
316 | if (p->family == AF_INET) | |
317 | count++; | |
318 | } | |
718e3744 | 319 | |
d62a17ae | 320 | return count; |
718e3744 | 321 | } |
d62a17ae | 322 | |
31a476c7 | 323 | |
324 | /* Does this address belongs to me ? */ | |
045c5389 | 325 | int if_check_address(struct rip *rip, struct in_addr addr) |
d62a17ae | 326 | { |
d62a17ae | 327 | struct interface *ifp; |
31a476c7 | 328 | |
ae7b826a | 329 | FOR_ALL_INTERFACES (rip->vrf, ifp) { |
d62a17ae | 330 | struct listnode *cnode; |
331 | struct connected *connected; | |
31a476c7 | 332 | |
d62a17ae | 333 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, connected)) { |
334 | struct prefix_ipv4 *p; | |
31a476c7 | 335 | |
d62a17ae | 336 | p = (struct prefix_ipv4 *)connected->address; |
337 | ||
338 | if (p->family != AF_INET) | |
339 | continue; | |
340 | ||
341 | if (IPV4_ADDR_CMP(&p->prefix, &addr) == 0) | |
342 | return 1; | |
343 | } | |
31a476c7 | 344 | } |
d62a17ae | 345 | return 0; |
31a476c7 | 346 | } |
347 | ||
718e3744 | 348 | /* Inteface link down message processing. */ |
121f9dee | 349 | int rip_interface_down(ZAPI_CALLBACK_ARGS) |
718e3744 | 350 | { |
d62a17ae | 351 | struct interface *ifp; |
352 | struct stream *s; | |
718e3744 | 353 | |
d62a17ae | 354 | s = zclient->ibuf; |
718e3744 | 355 | |
d62a17ae | 356 | /* zebra_interface_state_read() updates interface structure in |
357 | iflist. */ | |
358 | ifp = zebra_interface_state_read(s, vrf_id); | |
718e3744 | 359 | |
d62a17ae | 360 | if (ifp == NULL) |
361 | return 0; | |
718e3744 | 362 | |
ae7b826a | 363 | rip_interface_sync(ifp); |
d62a17ae | 364 | rip_if_down(ifp); |
718e3744 | 365 | |
d62a17ae | 366 | if (IS_RIP_DEBUG_ZEBRA) |
367 | zlog_debug( | |
ae7b826a | 368 | "interface %s vrf %u index %d flags %llx metric %d mtu %d is down", |
a36898e7 | 369 | ifp->name, ifp->vrf_id, ifp->ifindex, |
ae7b826a | 370 | (unsigned long long)ifp->flags, ifp->metric, ifp->mtu); |
d62a17ae | 371 | |
372 | return 0; | |
718e3744 | 373 | } |
374 | ||
375 | /* Inteface link up message processing */ | |
121f9dee | 376 | int rip_interface_up(ZAPI_CALLBACK_ARGS) |
718e3744 | 377 | { |
d62a17ae | 378 | struct interface *ifp; |
379 | ||
380 | /* zebra_interface_state_read () updates interface structure in | |
381 | iflist. */ | |
382 | ifp = zebra_interface_state_read(zclient->ibuf, vrf_id); | |
718e3744 | 383 | |
d62a17ae | 384 | if (ifp == NULL) |
385 | return 0; | |
718e3744 | 386 | |
d62a17ae | 387 | if (IS_RIP_DEBUG_ZEBRA) |
388 | zlog_debug( | |
ae7b826a | 389 | "interface %s vrf %u index %d flags %#llx metric %d mtu %d is up", |
a36898e7 | 390 | ifp->name, ifp->vrf_id, ifp->ifindex, |
ae7b826a RW |
391 | (unsigned long long)ifp->flags, ifp->metric, ifp->mtu); |
392 | ||
393 | rip_interface_sync(ifp); | |
718e3744 | 394 | |
d62a17ae | 395 | /* Check if this interface is RIP enabled or not.*/ |
396 | rip_enable_apply(ifp); | |
718e3744 | 397 | |
d62a17ae | 398 | /* Check for a passive interface */ |
399 | rip_passive_interface_apply(ifp); | |
718e3744 | 400 | |
d62a17ae | 401 | /* Apply distribute list to the all interface. */ |
402 | rip_distribute_update_interface(ifp); | |
718e3744 | 403 | |
d62a17ae | 404 | return 0; |
718e3744 | 405 | } |
406 | ||
407 | /* Inteface addition message from zebra. */ | |
121f9dee | 408 | int rip_interface_add(ZAPI_CALLBACK_ARGS) |
718e3744 | 409 | { |
d62a17ae | 410 | struct interface *ifp; |
718e3744 | 411 | |
d62a17ae | 412 | ifp = zebra_interface_add_read(zclient->ibuf, vrf_id); |
ae7b826a | 413 | rip_interface_sync(ifp); |
718e3744 | 414 | |
d62a17ae | 415 | if (IS_RIP_DEBUG_ZEBRA) |
416 | zlog_debug( | |
ae7b826a | 417 | "interface add %s vrf %u index %d flags %#llx metric %d mtu %d", |
a36898e7 | 418 | ifp->name, ifp->vrf_id, ifp->ifindex, |
ae7b826a | 419 | (unsigned long long)ifp->flags, ifp->metric, ifp->mtu); |
718e3744 | 420 | |
d62a17ae | 421 | /* Check if this interface is RIP enabled or not.*/ |
422 | rip_enable_apply(ifp); | |
718e3744 | 423 | |
d62a17ae | 424 | /* Check for a passive interface */ |
425 | rip_passive_interface_apply(ifp); | |
718e3744 | 426 | |
d62a17ae | 427 | /* Apply distribute list to the all interface. */ |
428 | rip_distribute_update_interface(ifp); | |
718e3744 | 429 | |
d62a17ae | 430 | /* rip_request_neighbor_all (); */ |
16705130 | 431 | |
d62a17ae | 432 | /* Check interface routemap. */ |
433 | rip_if_rmap_update_interface(ifp); | |
434 | ||
435 | return 0; | |
718e3744 | 436 | } |
437 | ||
121f9dee | 438 | int rip_interface_delete(ZAPI_CALLBACK_ARGS) |
718e3744 | 439 | { |
d62a17ae | 440 | struct interface *ifp; |
441 | struct stream *s; | |
442 | ||
718e3744 | 443 | |
d62a17ae | 444 | s = zclient->ibuf; |
445 | /* zebra_interface_state_read() updates interface structure in iflist */ | |
446 | ifp = zebra_interface_state_read(s, vrf_id); | |
718e3744 | 447 | |
d62a17ae | 448 | if (ifp == NULL) |
449 | return 0; | |
450 | ||
ae7b826a | 451 | rip_interface_sync(ifp); |
d62a17ae | 452 | if (if_is_up(ifp)) { |
453 | rip_if_down(ifp); | |
454 | } | |
718e3744 | 455 | |
ae7b826a RW |
456 | zlog_info( |
457 | "interface delete %s vrf %u index %d flags %#llx metric %d mtu %d", | |
a36898e7 | 458 | ifp->name, ifp->vrf_id, ifp->ifindex, |
ae7b826a | 459 | (unsigned long long)ifp->flags, ifp->metric, ifp->mtu); |
718e3744 | 460 | |
d62a17ae | 461 | /* To support pseudo interface do not free interface structure. */ |
462 | /* if_delete(ifp); */ | |
ff880b78 | 463 | if_set_index(ifp, IFINDEX_INTERNAL); |
718e3744 | 464 | |
d62a17ae | 465 | return 0; |
718e3744 | 466 | } |
467 | ||
ae7b826a | 468 | /* VRF update for an interface. */ |
121f9dee | 469 | int rip_interface_vrf_update(ZAPI_CALLBACK_ARGS) |
ae7b826a RW |
470 | { |
471 | struct interface *ifp; | |
472 | vrf_id_t new_vrf_id; | |
473 | ||
474 | ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id, | |
475 | &new_vrf_id); | |
476 | if (!ifp) | |
477 | return 0; | |
478 | ||
479 | if (IS_RIP_DEBUG_ZEBRA) | |
480 | zlog_debug("interface %s VRF change vrf_id %u new vrf id %u", | |
481 | ifp->name, vrf_id, new_vrf_id); | |
482 | ||
a36898e7 | 483 | if_update_to_new_vrf(ifp, new_vrf_id); |
ae7b826a RW |
484 | rip_interface_sync(ifp); |
485 | ||
486 | return 0; | |
487 | } | |
488 | ||
d62a17ae | 489 | static void rip_interface_clean(struct rip_interface *ri) |
4305dfd1 | 490 | { |
d62a17ae | 491 | ri->enable_network = 0; |
492 | ri->enable_interface = 0; | |
493 | ri->running = 0; | |
718e3744 | 494 | |
d62a17ae | 495 | if (ri->t_wakeup) { |
496 | thread_cancel(ri->t_wakeup); | |
497 | ri->t_wakeup = NULL; | |
498 | } | |
4305dfd1 | 499 | } |
718e3744 | 500 | |
045c5389 | 501 | void rip_interfaces_clean(struct rip *rip) |
718e3744 | 502 | { |
d62a17ae | 503 | struct interface *ifp; |
718e3744 | 504 | |
ae7b826a | 505 | FOR_ALL_INTERFACES (rip->vrf, ifp) |
d62a17ae | 506 | rip_interface_clean(ifp->info); |
1dec2166 | 507 | } |
718e3744 | 508 | |
d62a17ae | 509 | static void rip_interface_reset(struct rip_interface *ri) |
4305dfd1 | 510 | { |
94b117b2 RW |
511 | ri->auth_type = yang_get_default_enum("%s/authentication-scheme/mode", |
512 | RIP_IFACE); | |
513 | ri->md5_auth_len = yang_get_default_enum( | |
514 | "%s/authentication-scheme/md5-auth-length", RIP_IFACE); | |
718e3744 | 515 | |
d62a17ae | 516 | /* Set default split-horizon behavior. If the interface is Frame |
517 | Relay or SMDS is enabled, the default value for split-horizon is | |
518 | off. But currently Zebra does detect Frame Relay or SMDS | |
519 | interface. So all interface is set to split horizon. */ | |
94b117b2 RW |
520 | ri->split_horizon = |
521 | yang_get_default_enum("%s/split-horizon", RIP_IFACE); | |
718e3744 | 522 | |
94b117b2 RW |
523 | ri->ri_send = yang_get_default_enum("%s/version-send", RIP_IFACE); |
524 | ri->ri_receive = yang_get_default_enum("%s/version-receive", RIP_IFACE); | |
525 | ri->v2_broadcast = yang_get_default_bool("%s/v2-broadcast", RIP_IFACE); | |
f90310cf | 526 | |
0a22ddfb | 527 | XFREE(MTYPE_RIP_INTERFACE_STRING, ri->auth_str); |
03c20031 | 528 | |
0a22ddfb | 529 | XFREE(MTYPE_RIP_INTERFACE_STRING, ri->key_chain); |
03c20031 | 530 | |
d62a17ae | 531 | ri->list[RIP_FILTER_IN] = NULL; |
532 | ri->list[RIP_FILTER_OUT] = NULL; | |
718e3744 | 533 | |
d62a17ae | 534 | ri->prefix[RIP_FILTER_IN] = NULL; |
535 | ri->prefix[RIP_FILTER_OUT] = NULL; | |
718e3744 | 536 | |
d62a17ae | 537 | ri->recv_badpackets = 0; |
538 | ri->recv_badroutes = 0; | |
539 | ri->sent_updates = 0; | |
4305dfd1 | 540 | |
d62a17ae | 541 | ri->passive = 0; |
718e3744 | 542 | |
d62a17ae | 543 | rip_interface_clean(ri); |
4305dfd1 | 544 | } |
718e3744 | 545 | |
d62a17ae | 546 | int rip_if_down(struct interface *ifp) |
718e3744 | 547 | { |
045c5389 | 548 | struct rip *rip; |
d62a17ae | 549 | struct route_node *rp; |
550 | struct rip_info *rinfo; | |
551 | struct rip_interface *ri = NULL; | |
552 | struct list *list = NULL; | |
553 | struct listnode *listnode = NULL, *nextnode = NULL; | |
045c5389 RW |
554 | |
555 | ri = ifp->info; | |
556 | rip = ri->rip; | |
d62a17ae | 557 | if (rip) { |
558 | for (rp = route_top(rip->table); rp; rp = route_next(rp)) | |
559 | if ((list = rp->info) != NULL) | |
560 | for (ALL_LIST_ELEMENTS(list, listnode, nextnode, | |
561 | rinfo)) | |
dd127197 | 562 | if (rinfo->nh.ifindex == ifp->ifindex) |
045c5389 | 563 | rip_ecmp_delete(rip, rinfo); |
d62a17ae | 564 | |
565 | if (ri->running) { | |
566 | if (IS_RIP_DEBUG_EVENT) | |
567 | zlog_debug("turn off %s", ifp->name); | |
568 | ||
569 | /* Leave from multicast group. */ | |
570 | rip_multicast_leave(ifp, rip->sock); | |
571 | ||
572 | ri->running = 0; | |
573 | } | |
574 | } | |
575 | ||
576 | return 0; | |
718e3744 | 577 | } |
578 | ||
d62a17ae | 579 | static void rip_apply_address_add(struct connected *ifc) |
dc63bfd4 | 580 | { |
045c5389 RW |
581 | struct rip_interface *ri = ifc->ifp->info; |
582 | struct rip *rip = ri->rip; | |
d62a17ae | 583 | struct prefix_ipv4 address; |
3f5682c8 | 584 | struct nexthop nh; |
d62a17ae | 585 | struct prefix *p; |
16705130 | 586 | |
d62a17ae | 587 | if (!rip) |
588 | return; | |
16705130 | 589 | |
d62a17ae | 590 | if (!if_is_up(ifc->ifp)) |
591 | return; | |
16705130 | 592 | |
d62a17ae | 593 | p = ifc->address; |
16705130 | 594 | |
d62a17ae | 595 | memset(&address, 0, sizeof(address)); |
3f5682c8 DS |
596 | memset(&nh, 0, sizeof(nh)); |
597 | ||
d62a17ae | 598 | address.family = p->family; |
599 | address.prefix = p->u.prefix4; | |
600 | address.prefixlen = p->prefixlen; | |
601 | apply_mask_ipv4(&address); | |
16705130 | 602 | |
3f5682c8 DS |
603 | nh.ifindex = ifc->ifp->ifindex; |
604 | nh.type = NEXTHOP_TYPE_IFINDEX; | |
605 | ||
d62a17ae | 606 | /* Check if this interface is RIP enabled or not |
607 | or Check if this address's prefix is RIP enabled */ | |
045c5389 | 608 | if ((rip_enable_if_lookup(rip, ifc->ifp->name) >= 0) |
d62a17ae | 609 | || (rip_enable_network_lookup2(ifc) >= 0)) |
045c5389 RW |
610 | rip_redistribute_add(rip, ZEBRA_ROUTE_CONNECT, |
611 | RIP_ROUTE_INTERFACE, &address, &nh, 0, 0, | |
612 | 0); | |
16705130 | 613 | } |
614 | ||
121f9dee | 615 | int rip_interface_address_add(ZAPI_CALLBACK_ARGS) |
718e3744 | 616 | { |
d62a17ae | 617 | struct connected *ifc; |
618 | struct prefix *p; | |
718e3744 | 619 | |
d62a17ae | 620 | ifc = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD, |
621 | zclient->ibuf, vrf_id); | |
718e3744 | 622 | |
d62a17ae | 623 | if (ifc == NULL) |
624 | return 0; | |
718e3744 | 625 | |
d62a17ae | 626 | p = ifc->address; |
718e3744 | 627 | |
d62a17ae | 628 | if (p->family == AF_INET) { |
629 | if (IS_RIP_DEBUG_ZEBRA) | |
630 | zlog_debug("connected address %s/%d is added", | |
631 | inet_ntoa(p->u.prefix4), p->prefixlen); | |
16705130 | 632 | |
d62a17ae | 633 | rip_enable_apply(ifc->ifp); |
634 | /* Check if this prefix needs to be redistributed */ | |
635 | rip_apply_address_add(ifc); | |
718e3744 | 636 | |
d62a17ae | 637 | hook_call(rip_ifaddr_add, ifc); |
638 | } | |
718e3744 | 639 | |
d62a17ae | 640 | return 0; |
718e3744 | 641 | } |
642 | ||
d62a17ae | 643 | static void rip_apply_address_del(struct connected *ifc) |
644 | { | |
045c5389 RW |
645 | struct rip_interface *ri = ifc->ifp->info; |
646 | struct rip *rip = ri->rip; | |
d62a17ae | 647 | struct prefix_ipv4 address; |
648 | struct prefix *p; | |
16705130 | 649 | |
d62a17ae | 650 | if (!rip) |
651 | return; | |
16705130 | 652 | |
d62a17ae | 653 | if (!if_is_up(ifc->ifp)) |
654 | return; | |
16705130 | 655 | |
d62a17ae | 656 | p = ifc->address; |
16705130 | 657 | |
d62a17ae | 658 | memset(&address, 0, sizeof(address)); |
659 | address.family = p->family; | |
660 | address.prefix = p->u.prefix4; | |
661 | address.prefixlen = p->prefixlen; | |
662 | apply_mask_ipv4(&address); | |
16705130 | 663 | |
045c5389 | 664 | rip_redistribute_delete(rip, ZEBRA_ROUTE_CONNECT, RIP_ROUTE_INTERFACE, |
d62a17ae | 665 | &address, ifc->ifp->ifindex); |
16705130 | 666 | } |
667 | ||
121f9dee | 668 | int rip_interface_address_delete(ZAPI_CALLBACK_ARGS) |
718e3744 | 669 | { |
d62a17ae | 670 | struct connected *ifc; |
671 | struct prefix *p; | |
718e3744 | 672 | |
d62a17ae | 673 | ifc = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE, |
674 | zclient->ibuf, vrf_id); | |
718e3744 | 675 | |
d62a17ae | 676 | if (ifc) { |
677 | p = ifc->address; | |
678 | if (p->family == AF_INET) { | |
679 | if (IS_RIP_DEBUG_ZEBRA) | |
680 | zlog_debug("connected address %s/%d is deleted", | |
681 | inet_ntoa(p->u.prefix4), | |
682 | p->prefixlen); | |
16705130 | 683 | |
d62a17ae | 684 | hook_call(rip_ifaddr_del, ifc); |
718e3744 | 685 | |
d62a17ae | 686 | /* Chech wether this prefix needs to be removed */ |
687 | rip_apply_address_del(ifc); | |
688 | } | |
718e3744 | 689 | |
d62a17ae | 690 | connected_free(ifc); |
691 | } | |
718e3744 | 692 | |
d62a17ae | 693 | return 0; |
718e3744 | 694 | } |
6b0655a2 | 695 | |
718e3744 | 696 | /* Check interface is enabled by network statement. */ |
16705130 | 697 | /* Check wether the interface has at least a connected prefix that |
698 | * is within the ripng_enable_network table. */ | |
d62a17ae | 699 | static int rip_enable_network_lookup_if(struct interface *ifp) |
700 | { | |
045c5389 RW |
701 | struct rip_interface *ri = ifp->info; |
702 | struct rip *rip = ri->rip; | |
d62a17ae | 703 | struct listnode *node, *nnode; |
704 | struct connected *connected; | |
705 | struct prefix_ipv4 address; | |
706 | ||
1205fdc4 RW |
707 | if (!rip) |
708 | return -1; | |
709 | ||
d62a17ae | 710 | for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, connected)) { |
711 | struct prefix *p; | |
dc7204b7 | 712 | struct route_node *n; |
d62a17ae | 713 | |
714 | p = connected->address; | |
715 | ||
716 | if (p->family == AF_INET) { | |
717 | address.family = AF_INET; | |
718 | address.prefix = p->u.prefix4; | |
719 | address.prefixlen = IPV4_MAX_BITLEN; | |
720 | ||
1205fdc4 | 721 | n = route_node_match(rip->enable_network, |
dc7204b7 A |
722 | (struct prefix *)&address); |
723 | if (n) { | |
724 | route_unlock_node(n); | |
d62a17ae | 725 | return 1; |
726 | } | |
727 | } | |
728 | } | |
729 | return -1; | |
718e3744 | 730 | } |
731 | ||
16705130 | 732 | /* Check wether connected is within the ripng_enable_network table. */ |
045c5389 | 733 | static int rip_enable_network_lookup2(struct connected *connected) |
16705130 | 734 | { |
045c5389 RW |
735 | struct rip_interface *ri = connected->ifp->info; |
736 | struct rip *rip = ri->rip; | |
d62a17ae | 737 | struct prefix_ipv4 address; |
738 | struct prefix *p; | |
16705130 | 739 | |
d62a17ae | 740 | p = connected->address; |
16705130 | 741 | |
d62a17ae | 742 | if (p->family == AF_INET) { |
743 | struct route_node *node; | |
16705130 | 744 | |
d62a17ae | 745 | address.family = p->family; |
746 | address.prefix = p->u.prefix4; | |
747 | address.prefixlen = IPV4_MAX_BITLEN; | |
16705130 | 748 | |
d62a17ae | 749 | /* LPM on p->family, p->u.prefix4/IPV4_MAX_BITLEN within |
1205fdc4 RW |
750 | * rip->enable_network */ |
751 | node = route_node_match(rip->enable_network, | |
d62a17ae | 752 | (struct prefix *)&address); |
16705130 | 753 | |
d62a17ae | 754 | if (node) { |
755 | route_unlock_node(node); | |
756 | return 1; | |
757 | } | |
758 | } | |
16705130 | 759 | |
d62a17ae | 760 | return -1; |
16705130 | 761 | } |
718e3744 | 762 | /* Add RIP enable network. */ |
045c5389 | 763 | int rip_enable_network_add(struct rip *rip, struct prefix *p) |
718e3744 | 764 | { |
d62a17ae | 765 | struct route_node *node; |
718e3744 | 766 | |
1205fdc4 | 767 | node = route_node_get(rip->enable_network, p); |
718e3744 | 768 | |
d62a17ae | 769 | if (node->info) { |
770 | route_unlock_node(node); | |
3d7a1be8 | 771 | return NB_ERR_INCONSISTENCY; |
d62a17ae | 772 | } else |
773 | node->info = (void *)1; | |
718e3744 | 774 | |
d62a17ae | 775 | /* XXX: One should find a better solution than a generic one */ |
045c5389 | 776 | rip_enable_apply_all(rip); |
16705130 | 777 | |
3d7a1be8 | 778 | return NB_OK; |
718e3744 | 779 | } |
780 | ||
781 | /* Delete RIP enable network. */ | |
045c5389 | 782 | int rip_enable_network_delete(struct rip *rip, struct prefix *p) |
718e3744 | 783 | { |
d62a17ae | 784 | struct route_node *node; |
718e3744 | 785 | |
1205fdc4 | 786 | node = route_node_lookup(rip->enable_network, p); |
d62a17ae | 787 | if (node) { |
788 | node->info = NULL; | |
718e3744 | 789 | |
d62a17ae | 790 | /* Unlock info lock. */ |
791 | route_unlock_node(node); | |
718e3744 | 792 | |
d62a17ae | 793 | /* Unlock lookup lock. */ |
794 | route_unlock_node(node); | |
718e3744 | 795 | |
d62a17ae | 796 | /* XXX: One should find a better solution than a generic one */ |
045c5389 | 797 | rip_enable_apply_all(rip); |
16705130 | 798 | |
3d7a1be8 | 799 | return NB_OK; |
d62a17ae | 800 | } |
3d7a1be8 RW |
801 | |
802 | return NB_ERR_INCONSISTENCY; | |
718e3744 | 803 | } |
804 | ||
805 | /* Check interface is enabled by ifname statement. */ | |
045c5389 | 806 | static int rip_enable_if_lookup(struct rip *rip, const char *ifname) |
718e3744 | 807 | { |
d62a17ae | 808 | unsigned int i; |
809 | char *str; | |
718e3744 | 810 | |
ca046902 RW |
811 | if (!rip) |
812 | return -1; | |
813 | ||
814 | for (i = 0; i < vector_active(rip->enable_interface); i++) | |
815 | if ((str = vector_slot(rip->enable_interface, i)) != NULL) | |
d62a17ae | 816 | if (strcmp(str, ifname) == 0) |
817 | return i; | |
818 | return -1; | |
718e3744 | 819 | } |
820 | ||
821 | /* Add interface to rip_enable_if. */ | |
045c5389 | 822 | int rip_enable_if_add(struct rip *rip, const char *ifname) |
718e3744 | 823 | { |
d62a17ae | 824 | int ret; |
718e3744 | 825 | |
045c5389 | 826 | ret = rip_enable_if_lookup(rip, ifname); |
d62a17ae | 827 | if (ret >= 0) |
3d7a1be8 | 828 | return NB_ERR_INCONSISTENCY; |
718e3744 | 829 | |
ca046902 | 830 | vector_set(rip->enable_interface, |
03c20031 | 831 | XSTRDUP(MTYPE_RIP_INTERFACE_STRING, ifname)); |
718e3744 | 832 | |
045c5389 | 833 | rip_enable_apply_all(rip); /* TODOVJ */ |
16705130 | 834 | |
3d7a1be8 | 835 | return NB_OK; |
718e3744 | 836 | } |
837 | ||
838 | /* Delete interface from rip_enable_if. */ | |
045c5389 | 839 | int rip_enable_if_delete(struct rip *rip, const char *ifname) |
718e3744 | 840 | { |
d62a17ae | 841 | int index; |
842 | char *str; | |
718e3744 | 843 | |
045c5389 | 844 | index = rip_enable_if_lookup(rip, ifname); |
d62a17ae | 845 | if (index < 0) |
3d7a1be8 | 846 | return NB_ERR_INCONSISTENCY; |
718e3744 | 847 | |
ca046902 | 848 | str = vector_slot(rip->enable_interface, index); |
03c20031 | 849 | XFREE(MTYPE_RIP_INTERFACE_STRING, str); |
ca046902 | 850 | vector_unset(rip->enable_interface, index); |
718e3744 | 851 | |
045c5389 | 852 | rip_enable_apply_all(rip); /* TODOVJ */ |
16705130 | 853 | |
3d7a1be8 | 854 | return NB_OK; |
718e3744 | 855 | } |
856 | ||
857 | /* Join to multicast group and send request to the interface. */ | |
d62a17ae | 858 | static int rip_interface_wakeup(struct thread *t) |
718e3744 | 859 | { |
d62a17ae | 860 | struct interface *ifp; |
861 | struct rip_interface *ri; | |
718e3744 | 862 | |
d62a17ae | 863 | /* Get interface. */ |
864 | ifp = THREAD_ARG(t); | |
718e3744 | 865 | |
d62a17ae | 866 | ri = ifp->info; |
867 | ri->t_wakeup = NULL; | |
718e3744 | 868 | |
d62a17ae | 869 | /* Join to multicast group. */ |
045c5389 | 870 | if (rip_multicast_join(ifp, ri->rip->sock) < 0) { |
450971aa | 871 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
872 | "multicast join failed, interface %s not running", |
873 | ifp->name); | |
d62a17ae | 874 | return 0; |
875 | } | |
718e3744 | 876 | |
d62a17ae | 877 | /* Set running flag. */ |
878 | ri->running = 1; | |
879 | ||
880 | /* Send RIP request to the interface. */ | |
881 | rip_request_interface(ifp); | |
882 | ||
883 | return 0; | |
884 | } | |
885 | ||
886 | static void rip_connect_set(struct interface *ifp, int set) | |
887 | { | |
045c5389 RW |
888 | struct rip_interface *ri = ifp->info; |
889 | struct rip *rip = ri->rip; | |
d62a17ae | 890 | struct listnode *node, *nnode; |
891 | struct connected *connected; | |
892 | struct prefix_ipv4 address; | |
3f5682c8 DS |
893 | struct nexthop nh; |
894 | ||
895 | memset(&nh, 0, sizeof(nh)); | |
d62a17ae | 896 | |
897 | for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, connected)) { | |
898 | struct prefix *p; | |
899 | p = connected->address; | |
900 | ||
901 | if (p->family != AF_INET) | |
902 | continue; | |
903 | ||
904 | address.family = AF_INET; | |
905 | address.prefix = p->u.prefix4; | |
906 | address.prefixlen = p->prefixlen; | |
907 | apply_mask_ipv4(&address); | |
908 | ||
3f5682c8 DS |
909 | nh.ifindex = connected->ifp->ifindex; |
910 | nh.type = NEXTHOP_TYPE_IFINDEX; | |
d62a17ae | 911 | if (set) { |
912 | /* Check once more wether this prefix is within a | |
913 | * "network IF_OR_PREF" one */ | |
045c5389 RW |
914 | if ((rip_enable_if_lookup(rip, connected->ifp->name) |
915 | >= 0) | |
d62a17ae | 916 | || (rip_enable_network_lookup2(connected) >= 0)) |
045c5389 | 917 | rip_redistribute_add(rip, ZEBRA_ROUTE_CONNECT, |
996c9314 LB |
918 | RIP_ROUTE_INTERFACE, |
919 | &address, &nh, 0, 0, 0); | |
d62a17ae | 920 | } else { |
045c5389 | 921 | rip_redistribute_delete(rip, ZEBRA_ROUTE_CONNECT, |
d62a17ae | 922 | RIP_ROUTE_INTERFACE, &address, |
923 | connected->ifp->ifindex); | |
045c5389 RW |
924 | if (rip_redistribute_check(rip, ZEBRA_ROUTE_CONNECT)) |
925 | rip_redistribute_add(rip, ZEBRA_ROUTE_CONNECT, | |
996c9314 LB |
926 | RIP_ROUTE_REDISTRIBUTE, |
927 | &address, &nh, 0, 0, 0); | |
d62a17ae | 928 | } |
929 | } | |
718e3744 | 930 | } |
931 | ||
932 | /* Update interface status. */ | |
d62a17ae | 933 | void rip_enable_apply(struct interface *ifp) |
718e3744 | 934 | { |
d62a17ae | 935 | int ret; |
936 | struct rip_interface *ri = NULL; | |
718e3744 | 937 | |
d62a17ae | 938 | /* Check interface. */ |
939 | if (!if_is_operative(ifp)) | |
940 | return; | |
718e3744 | 941 | |
d62a17ae | 942 | ri = ifp->info; |
943 | ||
944 | /* Check network configuration. */ | |
945 | ret = rip_enable_network_lookup_if(ifp); | |
946 | ||
947 | /* If the interface is matched. */ | |
948 | if (ret > 0) | |
949 | ri->enable_network = 1; | |
950 | else | |
951 | ri->enable_network = 0; | |
952 | ||
953 | /* Check interface name configuration. */ | |
045c5389 | 954 | ret = rip_enable_if_lookup(ri->rip, ifp->name); |
d62a17ae | 955 | if (ret >= 0) |
956 | ri->enable_interface = 1; | |
957 | else | |
958 | ri->enable_interface = 0; | |
718e3744 | 959 | |
d62a17ae | 960 | /* any interface MUST have an IPv4 address */ |
961 | if (!rip_if_ipv4_address_check(ifp)) { | |
962 | ri->enable_network = 0; | |
963 | ri->enable_interface = 0; | |
718e3744 | 964 | } |
718e3744 | 965 | |
d62a17ae | 966 | /* Update running status of the interface. */ |
967 | if (ri->enable_network || ri->enable_interface) { | |
2e37ad7f RW |
968 | if (IS_RIP_DEBUG_EVENT) |
969 | zlog_debug("turn on %s", ifp->name); | |
970 | ||
971 | /* Add interface wake up thread. */ | |
972 | thread_add_timer(master, rip_interface_wakeup, ifp, 1, | |
973 | &ri->t_wakeup); | |
974 | rip_connect_set(ifp, 1); | |
975 | } else if (ri->running) { | |
976 | /* Might as well clean up the route table as well | |
977 | * rip_if_down sets to 0 ri->running, and displays "turn | |
978 | *off %s" | |
979 | **/ | |
980 | rip_if_down(ifp); | |
d62a17ae | 981 | |
2e37ad7f | 982 | rip_connect_set(ifp, 0); |
718e3744 | 983 | } |
718e3744 | 984 | } |
985 | ||
986 | /* Apply network configuration to all interface. */ | |
045c5389 | 987 | static void rip_enable_apply_all(struct rip *rip) |
718e3744 | 988 | { |
d62a17ae | 989 | struct interface *ifp; |
718e3744 | 990 | |
d62a17ae | 991 | /* Check each interface. */ |
ae7b826a | 992 | FOR_ALL_INTERFACES (rip->vrf, ifp) |
d62a17ae | 993 | rip_enable_apply(ifp); |
718e3744 | 994 | } |
995 | ||
045c5389 | 996 | int rip_neighbor_lookup(struct rip *rip, struct sockaddr_in *from) |
718e3744 | 997 | { |
d62a17ae | 998 | struct prefix_ipv4 p; |
999 | struct route_node *node; | |
718e3744 | 1000 | |
d62a17ae | 1001 | memset(&p, 0, sizeof(struct prefix_ipv4)); |
1002 | p.family = AF_INET; | |
1003 | p.prefix = from->sin_addr; | |
1004 | p.prefixlen = IPV4_MAX_BITLEN; | |
718e3744 | 1005 | |
d62a17ae | 1006 | node = route_node_lookup(rip->neighbor, (struct prefix *)&p); |
1007 | if (node) { | |
1008 | route_unlock_node(node); | |
1009 | return 1; | |
1010 | } | |
1011 | return 0; | |
718e3744 | 1012 | } |
1013 | ||
1014 | /* Add new RIP neighbor to the neighbor tree. */ | |
045c5389 | 1015 | int rip_neighbor_add(struct rip *rip, struct prefix_ipv4 *p) |
718e3744 | 1016 | { |
d62a17ae | 1017 | struct route_node *node; |
718e3744 | 1018 | |
d62a17ae | 1019 | node = route_node_get(rip->neighbor, (struct prefix *)p); |
718e3744 | 1020 | |
d62a17ae | 1021 | if (node->info) |
f0ab22fb | 1022 | return NB_ERR_INCONSISTENCY; |
718e3744 | 1023 | |
d62a17ae | 1024 | node->info = rip->neighbor; |
718e3744 | 1025 | |
f0ab22fb | 1026 | return NB_OK; |
718e3744 | 1027 | } |
1028 | ||
1029 | /* Delete RIP neighbor from the neighbor tree. */ | |
045c5389 | 1030 | int rip_neighbor_delete(struct rip *rip, struct prefix_ipv4 *p) |
718e3744 | 1031 | { |
d62a17ae | 1032 | struct route_node *node; |
718e3744 | 1033 | |
d62a17ae | 1034 | /* Lock for look up. */ |
1035 | node = route_node_lookup(rip->neighbor, (struct prefix *)p); | |
1036 | if (!node) | |
f0ab22fb | 1037 | return NB_ERR_INCONSISTENCY; |
718e3744 | 1038 | |
d62a17ae | 1039 | node->info = NULL; |
718e3744 | 1040 | |
d62a17ae | 1041 | /* Unlock lookup lock. */ |
1042 | route_unlock_node(node); | |
718e3744 | 1043 | |
d62a17ae | 1044 | /* Unlock real neighbor information lock. */ |
1045 | route_unlock_node(node); | |
1046 | ||
f0ab22fb | 1047 | return NB_OK; |
718e3744 | 1048 | } |
1049 | ||
1050 | /* Clear all network and neighbor configuration. */ | |
045c5389 | 1051 | void rip_clean_network(struct rip *rip) |
d62a17ae | 1052 | { |
1053 | unsigned int i; | |
1054 | char *str; | |
1055 | struct route_node *rn; | |
1056 | ||
1205fdc4 RW |
1057 | /* rip->enable_network. */ |
1058 | for (rn = route_top(rip->enable_network); rn; rn = route_next(rn)) | |
d62a17ae | 1059 | if (rn->info) { |
1060 | rn->info = NULL; | |
1061 | route_unlock_node(rn); | |
1062 | } | |
1063 | ||
ca046902 RW |
1064 | /* rip->enable_interface. */ |
1065 | for (i = 0; i < vector_active(rip->enable_interface); i++) | |
1066 | if ((str = vector_slot(rip->enable_interface, i)) != NULL) { | |
03c20031 | 1067 | XFREE(MTYPE_RIP_INTERFACE_STRING, str); |
ca046902 | 1068 | vector_slot(rip->enable_interface, i) = NULL; |
d62a17ae | 1069 | } |
718e3744 | 1070 | } |
6b0655a2 | 1071 | |
718e3744 | 1072 | /* Utility function for looking up passive interface settings. */ |
045c5389 | 1073 | static int rip_passive_nondefault_lookup(struct rip *rip, const char *ifname) |
718e3744 | 1074 | { |
d62a17ae | 1075 | unsigned int i; |
1076 | char *str; | |
718e3744 | 1077 | |
5a29c0d5 RW |
1078 | for (i = 0; i < vector_active(rip->passive_nondefault); i++) |
1079 | if ((str = vector_slot(rip->passive_nondefault, i)) != NULL) | |
d62a17ae | 1080 | if (strcmp(str, ifname) == 0) |
1081 | return i; | |
1082 | return -1; | |
718e3744 | 1083 | } |
1084 | ||
045c5389 | 1085 | static void rip_passive_interface_apply(struct interface *ifp) |
718e3744 | 1086 | { |
045c5389 | 1087 | struct rip *rip; |
d62a17ae | 1088 | struct rip_interface *ri; |
718e3744 | 1089 | |
045c5389 RW |
1090 | ri = ifp->info; |
1091 | rip = ri->rip; | |
44f2f852 RW |
1092 | if (rip == NULL) |
1093 | return; | |
1094 | ||
045c5389 | 1095 | ri->passive = ((rip_passive_nondefault_lookup(rip, ifp->name) < 0) |
44f2f852 RW |
1096 | ? rip->passive_default |
1097 | : !rip->passive_default); | |
4aaff3f8 | 1098 | |
d62a17ae | 1099 | if (IS_RIP_DEBUG_ZEBRA) |
1100 | zlog_debug("interface %s: passive = %d", ifp->name, | |
1101 | ri->passive); | |
718e3744 | 1102 | } |
1103 | ||
045c5389 | 1104 | static void rip_passive_interface_apply_all(struct rip *rip) |
718e3744 | 1105 | { |
d62a17ae | 1106 | struct interface *ifp; |
718e3744 | 1107 | |
ae7b826a | 1108 | FOR_ALL_INTERFACES (rip->vrf, ifp) |
d62a17ae | 1109 | rip_passive_interface_apply(ifp); |
718e3744 | 1110 | } |
1111 | ||
1112 | /* Passive interface. */ | |
045c5389 | 1113 | int rip_passive_nondefault_set(struct rip *rip, const char *ifname) |
718e3744 | 1114 | { |
045c5389 | 1115 | if (rip_passive_nondefault_lookup(rip, ifname) >= 0) |
44f2f852 RW |
1116 | /* |
1117 | * Don't return an error, this can happen after changing | |
1118 | * 'passive-default'. | |
1119 | */ | |
1120 | return NB_OK; | |
718e3744 | 1121 | |
5a29c0d5 | 1122 | vector_set(rip->passive_nondefault, |
03c20031 | 1123 | XSTRDUP(MTYPE_RIP_INTERFACE_STRING, ifname)); |
718e3744 | 1124 | |
045c5389 | 1125 | rip_passive_interface_apply_all(rip); |
718e3744 | 1126 | |
44f2f852 | 1127 | return NB_OK; |
718e3744 | 1128 | } |
1129 | ||
045c5389 | 1130 | int rip_passive_nondefault_unset(struct rip *rip, const char *ifname) |
718e3744 | 1131 | { |
d62a17ae | 1132 | int i; |
1133 | char *str; | |
718e3744 | 1134 | |
045c5389 | 1135 | i = rip_passive_nondefault_lookup(rip, ifname); |
d62a17ae | 1136 | if (i < 0) |
44f2f852 RW |
1137 | /* |
1138 | * Don't return an error, this can happen after changing | |
1139 | * 'passive-default'. | |
1140 | */ | |
1141 | return NB_OK; | |
718e3744 | 1142 | |
5a29c0d5 | 1143 | str = vector_slot(rip->passive_nondefault, i); |
03c20031 | 1144 | XFREE(MTYPE_RIP_INTERFACE_STRING, str); |
5a29c0d5 | 1145 | vector_unset(rip->passive_nondefault, i); |
718e3744 | 1146 | |
045c5389 | 1147 | rip_passive_interface_apply_all(rip); |
718e3744 | 1148 | |
44f2f852 | 1149 | return NB_OK; |
718e3744 | 1150 | } |
1151 | ||
1152 | /* Free all configured RIP passive-interface settings. */ | |
045c5389 | 1153 | void rip_passive_nondefault_clean(struct rip *rip) |
718e3744 | 1154 | { |
d62a17ae | 1155 | unsigned int i; |
1156 | char *str; | |
718e3744 | 1157 | |
5a29c0d5 RW |
1158 | for (i = 0; i < vector_active(rip->passive_nondefault); i++) |
1159 | if ((str = vector_slot(rip->passive_nondefault, i)) != NULL) { | |
03c20031 | 1160 | XFREE(MTYPE_RIP_INTERFACE_STRING, str); |
5a29c0d5 | 1161 | vector_slot(rip->passive_nondefault, i) = NULL; |
d62a17ae | 1162 | } |
045c5389 | 1163 | rip_passive_interface_apply_all(rip); |
718e3744 | 1164 | } |
6b0655a2 | 1165 | |
718e3744 | 1166 | /* Write rip configuration of each interface. */ |
d62a17ae | 1167 | static int rip_interface_config_write(struct vty *vty) |
1168 | { | |
ae7b826a | 1169 | struct vrf *vrf; |
94b117b2 | 1170 | int write = 0; |
d62a17ae | 1171 | |
ae7b826a RW |
1172 | RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) { |
1173 | struct interface *ifp; | |
d62a17ae | 1174 | |
ae7b826a RW |
1175 | FOR_ALL_INTERFACES (vrf, ifp) { |
1176 | struct lyd_node *dnode; | |
1177 | ||
1178 | dnode = yang_dnode_get( | |
1179 | running_config->dnode, | |
1180 | "/frr-interface:lib/interface[name='%s'][vrf='%s']", | |
1181 | ifp->name, vrf->name); | |
1182 | if (dnode == NULL) | |
1183 | continue; | |
d62a17ae | 1184 | |
ae7b826a RW |
1185 | write = 1; |
1186 | nb_cli_show_dnode_cmds(vty, dnode, false); | |
1187 | } | |
d62a17ae | 1188 | } |
94b117b2 RW |
1189 | |
1190 | return write; | |
d62a17ae | 1191 | } |
1192 | ||
045c5389 | 1193 | int rip_show_network_config(struct vty *vty, struct rip *rip) |
d62a17ae | 1194 | { |
1195 | unsigned int i; | |
1196 | char *ifname; | |
1197 | struct route_node *node; | |
1198 | ||
1199 | /* Network type RIP enable interface statement. */ | |
1205fdc4 | 1200 | for (node = route_top(rip->enable_network); node; |
d62a17ae | 1201 | node = route_next(node)) |
1202 | if (node->info) | |
44f2f852 | 1203 | vty_out(vty, " %s/%u\n", |
d62a17ae | 1204 | inet_ntoa(node->p.u.prefix4), |
1205 | node->p.prefixlen); | |
1206 | ||
1207 | /* Interface name RIP enable statement. */ | |
ca046902 RW |
1208 | for (i = 0; i < vector_active(rip->enable_interface); i++) |
1209 | if ((ifname = vector_slot(rip->enable_interface, i)) != NULL) | |
44f2f852 | 1210 | vty_out(vty, " %s\n", ifname); |
d62a17ae | 1211 | |
1212 | /* RIP neighbors listing. */ | |
1213 | for (node = route_top(rip->neighbor); node; node = route_next(node)) | |
1214 | if (node->info) | |
44f2f852 | 1215 | vty_out(vty, " %s\n", inet_ntoa(node->p.u.prefix4)); |
718e3744 | 1216 | |
d62a17ae | 1217 | return 0; |
1218 | } | |
1219 | ||
1220 | static struct cmd_node interface_node = { | |
9d303b37 | 1221 | INTERFACE_NODE, "%s(config-if)# ", 1, |
718e3744 | 1222 | }; |
1223 | ||
ae7b826a RW |
1224 | void rip_interface_sync(struct interface *ifp) |
1225 | { | |
1226 | struct vrf *vrf; | |
1227 | ||
a36898e7 | 1228 | vrf = vrf_lookup_by_id(ifp->vrf_id); |
ae7b826a RW |
1229 | if (vrf) { |
1230 | struct rip_interface *ri; | |
1231 | ||
1232 | ri = ifp->info; | |
1233 | if (ri) | |
1234 | ri->rip = vrf->info; | |
1235 | } | |
1236 | } | |
1237 | ||
718e3744 | 1238 | /* Called when interface structure allocated. */ |
d62a17ae | 1239 | static int rip_interface_new_hook(struct interface *ifp) |
718e3744 | 1240 | { |
d62a17ae | 1241 | ifp->info = rip_interface_new(); |
ae7b826a RW |
1242 | rip_interface_sync(ifp); |
1243 | ||
d62a17ae | 1244 | return 0; |
718e3744 | 1245 | } |
1246 | ||
1247 | /* Called when interface structure deleted. */ | |
d62a17ae | 1248 | static int rip_interface_delete_hook(struct interface *ifp) |
718e3744 | 1249 | { |
72010aca | 1250 | rip_interface_reset(ifp->info); |
d62a17ae | 1251 | XFREE(MTYPE_RIP_INTERFACE, ifp->info); |
1252 | ifp->info = NULL; | |
1253 | return 0; | |
718e3744 | 1254 | } |
1255 | ||
1256 | /* Allocate and initialize interface vector. */ | |
d62a17ae | 1257 | void rip_if_init(void) |
718e3744 | 1258 | { |
d62a17ae | 1259 | /* Default initial size of interface vector. */ |
ce19a04a DL |
1260 | hook_register_prio(if_add, 0, rip_interface_new_hook); |
1261 | hook_register_prio(if_del, 0, rip_interface_delete_hook); | |
d62a17ae | 1262 | |
d62a17ae | 1263 | /* Install interface node. */ |
1264 | install_node(&interface_node, rip_interface_config_write); | |
1265 | if_cmd_init(); | |
718e3744 | 1266 | } |