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