]> git.proxmox.com Git - mirror_frr.git/blob - ripd/rip_interface.c
Merge remote-tracking branch 'frr/master' into rip-vrf
[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(int command, struct zclient *zclient,
348 zebra_size_t length, vrf_id_t vrf_id)
349 {
350 struct interface *ifp;
351 struct stream *s;
352
353 s = zclient->ibuf;
354
355 /* zebra_interface_state_read() updates interface structure in
356 iflist. */
357 ifp = zebra_interface_state_read(s, vrf_id);
358
359 if (ifp == NULL)
360 return 0;
361
362 rip_interface_sync(ifp);
363 rip_if_down(ifp);
364
365 if (IS_RIP_DEBUG_ZEBRA)
366 zlog_debug(
367 "interface %s vrf %u index %d flags %llx metric %d mtu %d is down",
368 ifp->name, ifp->vrf_id, ifp->ifindex,
369 (unsigned long long)ifp->flags, ifp->metric, ifp->mtu);
370
371 return 0;
372 }
373
374 /* Inteface link up message processing */
375 int rip_interface_up(int command, struct zclient *zclient, zebra_size_t length,
376 vrf_id_t vrf_id)
377 {
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);
383
384 if (ifp == NULL)
385 return 0;
386
387 if (IS_RIP_DEBUG_ZEBRA)
388 zlog_debug(
389 "interface %s vrf %u index %d flags %#llx metric %d mtu %d is up",
390 ifp->name, ifp->vrf_id, ifp->ifindex,
391 (unsigned long long)ifp->flags, ifp->metric, ifp->mtu);
392
393 rip_interface_sync(ifp);
394
395 /* Check if this interface is RIP enabled or not.*/
396 rip_enable_apply(ifp);
397
398 /* Check for a passive interface */
399 rip_passive_interface_apply(ifp);
400
401 /* Apply distribute list to the all interface. */
402 rip_distribute_update_interface(ifp);
403
404 return 0;
405 }
406
407 /* Inteface addition message from zebra. */
408 int rip_interface_add(int command, struct zclient *zclient, zebra_size_t length,
409 vrf_id_t vrf_id)
410 {
411 struct interface *ifp;
412
413 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
414 rip_interface_sync(ifp);
415
416 if (IS_RIP_DEBUG_ZEBRA)
417 zlog_debug(
418 "interface add %s vrf %u index %d flags %#llx metric %d mtu %d",
419 ifp->name, ifp->vrf_id, ifp->ifindex,
420 (unsigned long long)ifp->flags, ifp->metric, ifp->mtu);
421
422 /* Check if this interface is RIP enabled or not.*/
423 rip_enable_apply(ifp);
424
425 /* Check for a passive interface */
426 rip_passive_interface_apply(ifp);
427
428 /* Apply distribute list to the all interface. */
429 rip_distribute_update_interface(ifp);
430
431 /* rip_request_neighbor_all (); */
432
433 /* Check interface routemap. */
434 rip_if_rmap_update_interface(ifp);
435
436 return 0;
437 }
438
439 int rip_interface_delete(int command, struct zclient *zclient,
440 zebra_size_t length, vrf_id_t vrf_id)
441 {
442 struct interface *ifp;
443 struct stream *s;
444
445
446 s = zclient->ibuf;
447 /* zebra_interface_state_read() updates interface structure in iflist */
448 ifp = zebra_interface_state_read(s, vrf_id);
449
450 if (ifp == NULL)
451 return 0;
452
453 rip_interface_sync(ifp);
454 if (if_is_up(ifp)) {
455 rip_if_down(ifp);
456 }
457
458 zlog_info(
459 "interface delete %s vrf %u index %d flags %#llx metric %d mtu %d",
460 ifp->name, ifp->vrf_id, ifp->ifindex,
461 (unsigned long long)ifp->flags, ifp->metric, ifp->mtu);
462
463 /* To support pseudo interface do not free interface structure. */
464 /* if_delete(ifp); */
465 if_set_index(ifp, IFINDEX_INTERNAL);
466
467 return 0;
468 }
469
470 /* VRF update for an interface. */
471 int rip_interface_vrf_update(int command, struct zclient *zclient,
472 zebra_size_t length, vrf_id_t vrf_id)
473 {
474 struct interface *ifp;
475 vrf_id_t new_vrf_id;
476
477 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
478 &new_vrf_id);
479 if (!ifp)
480 return 0;
481
482 if (IS_RIP_DEBUG_ZEBRA)
483 zlog_debug("interface %s VRF change vrf_id %u new vrf id %u",
484 ifp->name, vrf_id, new_vrf_id);
485
486 if_update_to_new_vrf(ifp, new_vrf_id);
487 rip_interface_sync(ifp);
488
489 return 0;
490 }
491
492 static void rip_interface_clean(struct rip_interface *ri)
493 {
494 ri->enable_network = 0;
495 ri->enable_interface = 0;
496 ri->running = 0;
497
498 if (ri->t_wakeup) {
499 thread_cancel(ri->t_wakeup);
500 ri->t_wakeup = NULL;
501 }
502 }
503
504 void rip_interfaces_clean(struct rip *rip)
505 {
506 struct interface *ifp;
507
508 FOR_ALL_INTERFACES (rip->vrf, ifp)
509 rip_interface_clean(ifp->info);
510 }
511
512 static void rip_interface_reset(struct rip_interface *ri)
513 {
514 ri->auth_type = yang_get_default_enum("%s/authentication-scheme/mode",
515 RIP_IFACE);
516 ri->md5_auth_len = yang_get_default_enum(
517 "%s/authentication-scheme/md5-auth-length", RIP_IFACE);
518
519 /* Set default split-horizon behavior. If the interface is Frame
520 Relay or SMDS is enabled, the default value for split-horizon is
521 off. But currently Zebra does detect Frame Relay or SMDS
522 interface. So all interface is set to split horizon. */
523 ri->split_horizon =
524 yang_get_default_enum("%s/split-horizon", RIP_IFACE);
525
526 ri->ri_send = yang_get_default_enum("%s/version-send", RIP_IFACE);
527 ri->ri_receive = yang_get_default_enum("%s/version-receive", RIP_IFACE);
528 ri->v2_broadcast = yang_get_default_bool("%s/v2-broadcast", RIP_IFACE);
529
530 if (ri->auth_str)
531 XFREE(MTYPE_RIP_INTERFACE_STRING, ri->auth_str);
532
533 if (ri->key_chain)
534 XFREE(MTYPE_RIP_INTERFACE_STRING, ri->key_chain);
535
536 ri->list[RIP_FILTER_IN] = NULL;
537 ri->list[RIP_FILTER_OUT] = NULL;
538
539 ri->prefix[RIP_FILTER_IN] = NULL;
540 ri->prefix[RIP_FILTER_OUT] = NULL;
541
542 ri->recv_badpackets = 0;
543 ri->recv_badroutes = 0;
544 ri->sent_updates = 0;
545
546 ri->passive = 0;
547
548 rip_interface_clean(ri);
549 }
550
551 int rip_if_down(struct interface *ifp)
552 {
553 struct rip *rip;
554 struct route_node *rp;
555 struct rip_info *rinfo;
556 struct rip_interface *ri = NULL;
557 struct list *list = NULL;
558 struct listnode *listnode = NULL, *nextnode = NULL;
559
560 ri = ifp->info;
561 rip = ri->rip;
562 if (rip) {
563 for (rp = route_top(rip->table); rp; rp = route_next(rp))
564 if ((list = rp->info) != NULL)
565 for (ALL_LIST_ELEMENTS(list, listnode, nextnode,
566 rinfo))
567 if (rinfo->nh.ifindex == ifp->ifindex)
568 rip_ecmp_delete(rip, rinfo);
569
570 if (ri->running) {
571 if (IS_RIP_DEBUG_EVENT)
572 zlog_debug("turn off %s", ifp->name);
573
574 /* Leave from multicast group. */
575 rip_multicast_leave(ifp, rip->sock);
576
577 ri->running = 0;
578 }
579 }
580
581 return 0;
582 }
583
584 static void rip_apply_address_add(struct connected *ifc)
585 {
586 struct rip_interface *ri = ifc->ifp->info;
587 struct rip *rip = ri->rip;
588 struct prefix_ipv4 address;
589 struct nexthop nh;
590 struct prefix *p;
591
592 if (!rip)
593 return;
594
595 if (!if_is_up(ifc->ifp))
596 return;
597
598 p = ifc->address;
599
600 memset(&address, 0, sizeof(address));
601 memset(&nh, 0, sizeof(nh));
602
603 address.family = p->family;
604 address.prefix = p->u.prefix4;
605 address.prefixlen = p->prefixlen;
606 apply_mask_ipv4(&address);
607
608 nh.ifindex = ifc->ifp->ifindex;
609 nh.type = NEXTHOP_TYPE_IFINDEX;
610
611 /* Check if this interface is RIP enabled or not
612 or Check if this address's prefix is RIP enabled */
613 if ((rip_enable_if_lookup(rip, ifc->ifp->name) >= 0)
614 || (rip_enable_network_lookup2(ifc) >= 0))
615 rip_redistribute_add(rip, ZEBRA_ROUTE_CONNECT,
616 RIP_ROUTE_INTERFACE, &address, &nh, 0, 0,
617 0);
618 }
619
620 int rip_interface_address_add(int command, struct zclient *zclient,
621 zebra_size_t length, vrf_id_t vrf_id)
622 {
623 struct connected *ifc;
624 struct prefix *p;
625
626 ifc = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD,
627 zclient->ibuf, vrf_id);
628
629 if (ifc == NULL)
630 return 0;
631
632 p = ifc->address;
633
634 if (p->family == AF_INET) {
635 if (IS_RIP_DEBUG_ZEBRA)
636 zlog_debug("connected address %s/%d is added",
637 inet_ntoa(p->u.prefix4), p->prefixlen);
638
639 rip_enable_apply(ifc->ifp);
640 /* Check if this prefix needs to be redistributed */
641 rip_apply_address_add(ifc);
642
643 hook_call(rip_ifaddr_add, ifc);
644 }
645
646 return 0;
647 }
648
649 static void rip_apply_address_del(struct connected *ifc)
650 {
651 struct rip_interface *ri = ifc->ifp->info;
652 struct rip *rip = ri->rip;
653 struct prefix_ipv4 address;
654 struct prefix *p;
655
656 if (!rip)
657 return;
658
659 if (!if_is_up(ifc->ifp))
660 return;
661
662 p = ifc->address;
663
664 memset(&address, 0, sizeof(address));
665 address.family = p->family;
666 address.prefix = p->u.prefix4;
667 address.prefixlen = p->prefixlen;
668 apply_mask_ipv4(&address);
669
670 rip_redistribute_delete(rip, ZEBRA_ROUTE_CONNECT, RIP_ROUTE_INTERFACE,
671 &address, ifc->ifp->ifindex);
672 }
673
674 int rip_interface_address_delete(int command, struct zclient *zclient,
675 zebra_size_t length, vrf_id_t vrf_id)
676 {
677 struct connected *ifc;
678 struct prefix *p;
679
680 ifc = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE,
681 zclient->ibuf, vrf_id);
682
683 if (ifc) {
684 p = ifc->address;
685 if (p->family == AF_INET) {
686 if (IS_RIP_DEBUG_ZEBRA)
687 zlog_debug("connected address %s/%d is deleted",
688 inet_ntoa(p->u.prefix4),
689 p->prefixlen);
690
691 hook_call(rip_ifaddr_del, ifc);
692
693 /* Chech wether this prefix needs to be removed */
694 rip_apply_address_del(ifc);
695 }
696
697 connected_free(ifc);
698 }
699
700 return 0;
701 }
702
703 /* Check interface is enabled by network statement. */
704 /* Check wether the interface has at least a connected prefix that
705 * is within the ripng_enable_network table. */
706 static int rip_enable_network_lookup_if(struct interface *ifp)
707 {
708 struct rip_interface *ri = ifp->info;
709 struct rip *rip = ri->rip;
710 struct listnode *node, *nnode;
711 struct connected *connected;
712 struct prefix_ipv4 address;
713
714 if (!rip)
715 return -1;
716
717 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, connected)) {
718 struct prefix *p;
719 struct route_node *n;
720
721 p = connected->address;
722
723 if (p->family == AF_INET) {
724 address.family = AF_INET;
725 address.prefix = p->u.prefix4;
726 address.prefixlen = IPV4_MAX_BITLEN;
727
728 n = route_node_match(rip->enable_network,
729 (struct prefix *)&address);
730 if (n) {
731 route_unlock_node(n);
732 return 1;
733 }
734 }
735 }
736 return -1;
737 }
738
739 /* Check wether connected is within the ripng_enable_network table. */
740 static int rip_enable_network_lookup2(struct connected *connected)
741 {
742 struct rip_interface *ri = connected->ifp->info;
743 struct rip *rip = ri->rip;
744 struct prefix_ipv4 address;
745 struct prefix *p;
746
747 p = connected->address;
748
749 if (p->family == AF_INET) {
750 struct route_node *node;
751
752 address.family = p->family;
753 address.prefix = p->u.prefix4;
754 address.prefixlen = IPV4_MAX_BITLEN;
755
756 /* LPM on p->family, p->u.prefix4/IPV4_MAX_BITLEN within
757 * rip->enable_network */
758 node = route_node_match(rip->enable_network,
759 (struct prefix *)&address);
760
761 if (node) {
762 route_unlock_node(node);
763 return 1;
764 }
765 }
766
767 return -1;
768 }
769 /* Add RIP enable network. */
770 int rip_enable_network_add(struct rip *rip, struct prefix *p)
771 {
772 struct route_node *node;
773
774 node = route_node_get(rip->enable_network, p);
775
776 if (node->info) {
777 route_unlock_node(node);
778 return NB_ERR_INCONSISTENCY;
779 } else
780 node->info = (void *)1;
781
782 /* XXX: One should find a better solution than a generic one */
783 rip_enable_apply_all(rip);
784
785 return NB_OK;
786 }
787
788 /* Delete RIP enable network. */
789 int rip_enable_network_delete(struct rip *rip, struct prefix *p)
790 {
791 struct route_node *node;
792
793 node = route_node_lookup(rip->enable_network, p);
794 if (node) {
795 node->info = NULL;
796
797 /* Unlock info lock. */
798 route_unlock_node(node);
799
800 /* Unlock lookup lock. */
801 route_unlock_node(node);
802
803 /* XXX: One should find a better solution than a generic one */
804 rip_enable_apply_all(rip);
805
806 return NB_OK;
807 }
808
809 return NB_ERR_INCONSISTENCY;
810 }
811
812 /* Check interface is enabled by ifname statement. */
813 static int rip_enable_if_lookup(struct rip *rip, const char *ifname)
814 {
815 unsigned int i;
816 char *str;
817
818 if (!rip)
819 return -1;
820
821 for (i = 0; i < vector_active(rip->enable_interface); i++)
822 if ((str = vector_slot(rip->enable_interface, i)) != NULL)
823 if (strcmp(str, ifname) == 0)
824 return i;
825 return -1;
826 }
827
828 /* Add interface to rip_enable_if. */
829 int rip_enable_if_add(struct rip *rip, const char *ifname)
830 {
831 int ret;
832
833 ret = rip_enable_if_lookup(rip, ifname);
834 if (ret >= 0)
835 return NB_ERR_INCONSISTENCY;
836
837 vector_set(rip->enable_interface,
838 XSTRDUP(MTYPE_RIP_INTERFACE_STRING, ifname));
839
840 rip_enable_apply_all(rip); /* TODOVJ */
841
842 return NB_OK;
843 }
844
845 /* Delete interface from rip_enable_if. */
846 int rip_enable_if_delete(struct rip *rip, const char *ifname)
847 {
848 int index;
849 char *str;
850
851 index = rip_enable_if_lookup(rip, ifname);
852 if (index < 0)
853 return NB_ERR_INCONSISTENCY;
854
855 str = vector_slot(rip->enable_interface, index);
856 XFREE(MTYPE_RIP_INTERFACE_STRING, str);
857 vector_unset(rip->enable_interface, index);
858
859 rip_enable_apply_all(rip); /* TODOVJ */
860
861 return NB_OK;
862 }
863
864 /* Join to multicast group and send request to the interface. */
865 static int rip_interface_wakeup(struct thread *t)
866 {
867 struct interface *ifp;
868 struct rip_interface *ri;
869
870 /* Get interface. */
871 ifp = THREAD_ARG(t);
872
873 ri = ifp->info;
874 ri->t_wakeup = NULL;
875
876 /* Join to multicast group. */
877 if (rip_multicast_join(ifp, ri->rip->sock) < 0) {
878 flog_err_sys(EC_LIB_SOCKET,
879 "multicast join failed, interface %s not running",
880 ifp->name);
881 return 0;
882 }
883
884 /* Set running flag. */
885 ri->running = 1;
886
887 /* Send RIP request to the interface. */
888 rip_request_interface(ifp);
889
890 return 0;
891 }
892
893 static void rip_connect_set(struct interface *ifp, int set)
894 {
895 struct rip_interface *ri = ifp->info;
896 struct rip *rip = ri->rip;
897 struct listnode *node, *nnode;
898 struct connected *connected;
899 struct prefix_ipv4 address;
900 struct nexthop nh;
901
902 memset(&nh, 0, sizeof(nh));
903
904 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, connected)) {
905 struct prefix *p;
906 p = connected->address;
907
908 if (p->family != AF_INET)
909 continue;
910
911 address.family = AF_INET;
912 address.prefix = p->u.prefix4;
913 address.prefixlen = p->prefixlen;
914 apply_mask_ipv4(&address);
915
916 nh.ifindex = connected->ifp->ifindex;
917 nh.type = NEXTHOP_TYPE_IFINDEX;
918 if (set) {
919 /* Check once more wether this prefix is within a
920 * "network IF_OR_PREF" one */
921 if ((rip_enable_if_lookup(rip, connected->ifp->name)
922 >= 0)
923 || (rip_enable_network_lookup2(connected) >= 0))
924 rip_redistribute_add(rip, ZEBRA_ROUTE_CONNECT,
925 RIP_ROUTE_INTERFACE,
926 &address, &nh, 0, 0, 0);
927 } else {
928 rip_redistribute_delete(rip, ZEBRA_ROUTE_CONNECT,
929 RIP_ROUTE_INTERFACE, &address,
930 connected->ifp->ifindex);
931 if (rip_redistribute_check(rip, ZEBRA_ROUTE_CONNECT))
932 rip_redistribute_add(rip, ZEBRA_ROUTE_CONNECT,
933 RIP_ROUTE_REDISTRIBUTE,
934 &address, &nh, 0, 0, 0);
935 }
936 }
937 }
938
939 /* Update interface status. */
940 void rip_enable_apply(struct interface *ifp)
941 {
942 int ret;
943 struct rip_interface *ri = NULL;
944
945 /* Check interface. */
946 if (!if_is_operative(ifp))
947 return;
948
949 ri = ifp->info;
950
951 /* Check network configuration. */
952 ret = rip_enable_network_lookup_if(ifp);
953
954 /* If the interface is matched. */
955 if (ret > 0)
956 ri->enable_network = 1;
957 else
958 ri->enable_network = 0;
959
960 /* Check interface name configuration. */
961 ret = rip_enable_if_lookup(ri->rip, ifp->name);
962 if (ret >= 0)
963 ri->enable_interface = 1;
964 else
965 ri->enable_interface = 0;
966
967 /* any interface MUST have an IPv4 address */
968 if (!rip_if_ipv4_address_check(ifp)) {
969 ri->enable_network = 0;
970 ri->enable_interface = 0;
971 }
972
973 /* Update running status of the interface. */
974 if (ri->enable_network || ri->enable_interface) {
975 if (IS_RIP_DEBUG_EVENT)
976 zlog_debug("turn on %s", ifp->name);
977
978 /* Add interface wake up thread. */
979 thread_add_timer(master, rip_interface_wakeup, ifp, 1,
980 &ri->t_wakeup);
981 rip_connect_set(ifp, 1);
982 } else if (ri->running) {
983 /* Might as well clean up the route table as well
984 * rip_if_down sets to 0 ri->running, and displays "turn
985 *off %s"
986 **/
987 rip_if_down(ifp);
988
989 rip_connect_set(ifp, 0);
990 }
991 }
992
993 /* Apply network configuration to all interface. */
994 static void rip_enable_apply_all(struct rip *rip)
995 {
996 struct interface *ifp;
997
998 /* Check each interface. */
999 FOR_ALL_INTERFACES (rip->vrf, ifp)
1000 rip_enable_apply(ifp);
1001 }
1002
1003 int rip_neighbor_lookup(struct rip *rip, struct sockaddr_in *from)
1004 {
1005 struct prefix_ipv4 p;
1006 struct route_node *node;
1007
1008 memset(&p, 0, sizeof(struct prefix_ipv4));
1009 p.family = AF_INET;
1010 p.prefix = from->sin_addr;
1011 p.prefixlen = IPV4_MAX_BITLEN;
1012
1013 node = route_node_lookup(rip->neighbor, (struct prefix *)&p);
1014 if (node) {
1015 route_unlock_node(node);
1016 return 1;
1017 }
1018 return 0;
1019 }
1020
1021 /* Add new RIP neighbor to the neighbor tree. */
1022 int rip_neighbor_add(struct rip *rip, struct prefix_ipv4 *p)
1023 {
1024 struct route_node *node;
1025
1026 node = route_node_get(rip->neighbor, (struct prefix *)p);
1027
1028 if (node->info)
1029 return NB_ERR_INCONSISTENCY;
1030
1031 node->info = rip->neighbor;
1032
1033 return NB_OK;
1034 }
1035
1036 /* Delete RIP neighbor from the neighbor tree. */
1037 int rip_neighbor_delete(struct rip *rip, struct prefix_ipv4 *p)
1038 {
1039 struct route_node *node;
1040
1041 /* Lock for look up. */
1042 node = route_node_lookup(rip->neighbor, (struct prefix *)p);
1043 if (!node)
1044 return NB_ERR_INCONSISTENCY;
1045
1046 node->info = NULL;
1047
1048 /* Unlock lookup lock. */
1049 route_unlock_node(node);
1050
1051 /* Unlock real neighbor information lock. */
1052 route_unlock_node(node);
1053
1054 return NB_OK;
1055 }
1056
1057 /* Clear all network and neighbor configuration. */
1058 void rip_clean_network(struct rip *rip)
1059 {
1060 unsigned int i;
1061 char *str;
1062 struct route_node *rn;
1063
1064 /* rip->enable_network. */
1065 for (rn = route_top(rip->enable_network); rn; rn = route_next(rn))
1066 if (rn->info) {
1067 rn->info = NULL;
1068 route_unlock_node(rn);
1069 }
1070
1071 /* rip->enable_interface. */
1072 for (i = 0; i < vector_active(rip->enable_interface); i++)
1073 if ((str = vector_slot(rip->enable_interface, i)) != NULL) {
1074 XFREE(MTYPE_RIP_INTERFACE_STRING, str);
1075 vector_slot(rip->enable_interface, i) = NULL;
1076 }
1077 }
1078
1079 /* Utility function for looking up passive interface settings. */
1080 static int rip_passive_nondefault_lookup(struct rip *rip, const char *ifname)
1081 {
1082 unsigned int i;
1083 char *str;
1084
1085 for (i = 0; i < vector_active(rip->passive_nondefault); i++)
1086 if ((str = vector_slot(rip->passive_nondefault, i)) != NULL)
1087 if (strcmp(str, ifname) == 0)
1088 return i;
1089 return -1;
1090 }
1091
1092 static void rip_passive_interface_apply(struct interface *ifp)
1093 {
1094 struct rip *rip;
1095 struct rip_interface *ri;
1096
1097 ri = ifp->info;
1098 rip = ri->rip;
1099 if (rip == NULL)
1100 return;
1101
1102 ri->passive = ((rip_passive_nondefault_lookup(rip, ifp->name) < 0)
1103 ? rip->passive_default
1104 : !rip->passive_default);
1105
1106 if (IS_RIP_DEBUG_ZEBRA)
1107 zlog_debug("interface %s: passive = %d", ifp->name,
1108 ri->passive);
1109 }
1110
1111 static void rip_passive_interface_apply_all(struct rip *rip)
1112 {
1113 struct interface *ifp;
1114
1115 FOR_ALL_INTERFACES (rip->vrf, ifp)
1116 rip_passive_interface_apply(ifp);
1117 }
1118
1119 /* Passive interface. */
1120 int rip_passive_nondefault_set(struct rip *rip, const char *ifname)
1121 {
1122 if (rip_passive_nondefault_lookup(rip, ifname) >= 0)
1123 /*
1124 * Don't return an error, this can happen after changing
1125 * 'passive-default'.
1126 */
1127 return NB_OK;
1128
1129 vector_set(rip->passive_nondefault,
1130 XSTRDUP(MTYPE_RIP_INTERFACE_STRING, ifname));
1131
1132 rip_passive_interface_apply_all(rip);
1133
1134 return NB_OK;
1135 }
1136
1137 int rip_passive_nondefault_unset(struct rip *rip, const char *ifname)
1138 {
1139 int i;
1140 char *str;
1141
1142 i = rip_passive_nondefault_lookup(rip, ifname);
1143 if (i < 0)
1144 /*
1145 * Don't return an error, this can happen after changing
1146 * 'passive-default'.
1147 */
1148 return NB_OK;
1149
1150 str = vector_slot(rip->passive_nondefault, i);
1151 XFREE(MTYPE_RIP_INTERFACE_STRING, str);
1152 vector_unset(rip->passive_nondefault, i);
1153
1154 rip_passive_interface_apply_all(rip);
1155
1156 return NB_OK;
1157 }
1158
1159 /* Free all configured RIP passive-interface settings. */
1160 void rip_passive_nondefault_clean(struct rip *rip)
1161 {
1162 unsigned int i;
1163 char *str;
1164
1165 for (i = 0; i < vector_active(rip->passive_nondefault); i++)
1166 if ((str = vector_slot(rip->passive_nondefault, i)) != NULL) {
1167 XFREE(MTYPE_RIP_INTERFACE_STRING, str);
1168 vector_slot(rip->passive_nondefault, i) = NULL;
1169 }
1170 rip_passive_interface_apply_all(rip);
1171 }
1172
1173 /* Write rip configuration of each interface. */
1174 static int rip_interface_config_write(struct vty *vty)
1175 {
1176 struct vrf *vrf;
1177 int write = 0;
1178
1179 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
1180 struct interface *ifp;
1181
1182 FOR_ALL_INTERFACES (vrf, ifp) {
1183 struct lyd_node *dnode;
1184
1185 dnode = yang_dnode_get(
1186 running_config->dnode,
1187 "/frr-interface:lib/interface[name='%s'][vrf='%s']",
1188 ifp->name, vrf->name);
1189 if (dnode == NULL)
1190 continue;
1191
1192 write = 1;
1193 nb_cli_show_dnode_cmds(vty, dnode, false);
1194 }
1195 }
1196
1197 return write;
1198 }
1199
1200 int rip_show_network_config(struct vty *vty, struct rip *rip)
1201 {
1202 unsigned int i;
1203 char *ifname;
1204 struct route_node *node;
1205
1206 /* Network type RIP enable interface statement. */
1207 for (node = route_top(rip->enable_network); node;
1208 node = route_next(node))
1209 if (node->info)
1210 vty_out(vty, " %s/%u\n",
1211 inet_ntoa(node->p.u.prefix4),
1212 node->p.prefixlen);
1213
1214 /* Interface name RIP enable statement. */
1215 for (i = 0; i < vector_active(rip->enable_interface); i++)
1216 if ((ifname = vector_slot(rip->enable_interface, i)) != NULL)
1217 vty_out(vty, " %s\n", ifname);
1218
1219 /* RIP neighbors listing. */
1220 for (node = route_top(rip->neighbor); node; node = route_next(node))
1221 if (node->info)
1222 vty_out(vty, " %s\n", inet_ntoa(node->p.u.prefix4));
1223
1224 return 0;
1225 }
1226
1227 static struct cmd_node interface_node = {
1228 INTERFACE_NODE, "%s(config-if)# ", 1,
1229 };
1230
1231 void rip_interface_sync(struct interface *ifp)
1232 {
1233 struct vrf *vrf;
1234
1235 vrf = vrf_lookup_by_id(ifp->vrf_id);
1236 if (vrf) {
1237 struct rip_interface *ri;
1238
1239 ri = ifp->info;
1240 if (ri)
1241 ri->rip = vrf->info;
1242 }
1243 }
1244
1245 /* Called when interface structure allocated. */
1246 static int rip_interface_new_hook(struct interface *ifp)
1247 {
1248 ifp->info = rip_interface_new();
1249 rip_interface_sync(ifp);
1250
1251 return 0;
1252 }
1253
1254 /* Called when interface structure deleted. */
1255 static int rip_interface_delete_hook(struct interface *ifp)
1256 {
1257 rip_interface_reset(ifp->info);
1258 XFREE(MTYPE_RIP_INTERFACE, ifp->info);
1259 ifp->info = NULL;
1260 return 0;
1261 }
1262
1263 /* Allocate and initialize interface vector. */
1264 void rip_if_init(void)
1265 {
1266 /* Default initial size of interface vector. */
1267 hook_register_prio(if_add, 0, rip_interface_new_hook);
1268 hook_register_prio(if_del, 0, rip_interface_delete_hook);
1269
1270 /* Install interface node. */
1271 install_node(&interface_node, rip_interface_config_write);
1272 if_cmd_init();
1273 }