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