]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* |
2 | * Interface related function for RIPng. | |
3 | * Copyright (C) 1998 Kunihiro Ishiguro | |
4 | * | |
5 | * This file is part of GNU Zebra. | |
6 | * | |
7 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the | |
9 | * Free Software Foundation; either version 2, or (at your option) any | |
10 | * later version. | |
11 | * | |
12 | * GNU Zebra is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
896014f4 DL |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 20 | */ |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #include "linklist.h" | |
25 | #include "if.h" | |
26 | #include "prefix.h" | |
27 | #include "memory.h" | |
28 | #include "network.h" | |
29 | #include "filter.h" | |
30 | #include "log.h" | |
31 | #include "stream.h" | |
32 | #include "zclient.h" | |
33 | #include "command.h" | |
fe08ba7e | 34 | #include "agg_table.h" |
718e3744 | 35 | #include "thread.h" |
4d4653af | 36 | #include "privs.h" |
6a69b354 | 37 | #include "vrf.h" |
7f9a4fd7 | 38 | #include "lib_errors.h" |
d406db4c | 39 | #include "northbound_cli.h" |
718e3744 | 40 | |
41 | #include "ripngd/ripngd.h" | |
42 | #include "ripngd/ripng_debug.h" | |
6b0655a2 | 43 | |
718e3744 | 44 | /* If RFC2133 definition is used. */ |
45 | #ifndef IPV6_JOIN_GROUP | |
c258527b | 46 | #define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP |
718e3744 | 47 | #endif |
48 | #ifndef IPV6_LEAVE_GROUP | |
c258527b | 49 | #define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP |
718e3744 | 50 | #endif |
51 | ||
bf8d3d6a | 52 | DEFINE_MTYPE_STATIC(RIPNGD, RIPNG_IF, "ripng interface"); |
eaf58ba9 | 53 | |
718e3744 | 54 | /* Static utility function. */ |
d62a17ae | 55 | static void ripng_enable_apply(struct interface *); |
56 | static void ripng_passive_interface_apply(struct interface *); | |
5c84b9a5 | 57 | static int ripng_enable_if_lookup(struct ripng *ripng, const char *ifname); |
d62a17ae | 58 | static int ripng_enable_network_lookup2(struct connected *); |
5c84b9a5 | 59 | static void ripng_enable_apply_all(struct ripng *ripng); |
718e3744 | 60 | |
61 | /* Join to the all rip routers multicast group. */ | |
5c84b9a5 | 62 | static int ripng_multicast_join(struct interface *ifp, int sock) |
718e3744 | 63 | { |
d62a17ae | 64 | int ret; |
65 | struct ipv6_mreq mreq; | |
66 | int save_errno; | |
67 | ||
68 | if (if_is_multicast(ifp)) { | |
69 | memset(&mreq, 0, sizeof(mreq)); | |
70 | inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr); | |
71 | mreq.ipv6mr_interface = ifp->ifindex; | |
72 | ||
73 | /* | |
74 | * NetBSD 1.6.2 requires root to join groups on gif(4). | |
75 | * While this is bogus, privs are available and easy to use | |
76 | * for this call as a workaround. | |
77 | */ | |
0cf6db21 | 78 | frr_with_privs(&ripngd_privs) { |
d62a17ae | 79 | |
5c84b9a5 | 80 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, |
01b9e3fd DL |
81 | (char *)&mreq, sizeof(mreq)); |
82 | save_errno = errno; | |
d62a17ae | 83 | |
01b9e3fd | 84 | } |
d62a17ae | 85 | |
86 | if (ret < 0 && save_errno == EADDRINUSE) { | |
87 | /* | |
88 | * Group is already joined. This occurs due to sloppy | |
89 | * group | |
90 | * management, in particular declining to leave the | |
91 | * group on | |
92 | * an interface that has just gone down. | |
93 | */ | |
9165c5f5 | 94 | zlog_warn("ripng join on %s EADDRINUSE (ignoring)", |
d62a17ae | 95 | ifp->name); |
96 | return 0; /* not an error */ | |
97 | } | |
98 | ||
99 | if (ret < 0) | |
100 | zlog_warn("can't setsockopt IPV6_JOIN_GROUP: %s", | |
101 | safe_strerror(save_errno)); | |
102 | ||
103 | if (IS_RIPNG_DEBUG_EVENT) | |
104 | zlog_debug( | |
105 | "RIPng %s join to all-rip-routers multicast group", | |
106 | ifp->name); | |
107 | ||
108 | if (ret < 0) | |
109 | return -1; | |
110 | } | |
111 | return 0; | |
718e3744 | 112 | } |
113 | ||
114 | /* Leave from the all rip routers multicast group. */ | |
5c84b9a5 | 115 | static int ripng_multicast_leave(struct interface *ifp, int sock) |
718e3744 | 116 | { |
d62a17ae | 117 | int ret; |
118 | struct ipv6_mreq mreq; | |
119 | ||
120 | if (if_is_multicast(ifp)) { | |
121 | memset(&mreq, 0, sizeof(mreq)); | |
122 | inet_pton(AF_INET6, RIPNG_GROUP, &mreq.ipv6mr_multiaddr); | |
123 | mreq.ipv6mr_interface = ifp->ifindex; | |
124 | ||
5c84b9a5 | 125 | ret = setsockopt(sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP, |
d62a17ae | 126 | (char *)&mreq, sizeof(mreq)); |
127 | if (ret < 0) | |
9165c5f5 | 128 | zlog_warn("can't setsockopt IPV6_LEAVE_GROUP: %s", |
d62a17ae | 129 | safe_strerror(errno)); |
130 | ||
131 | if (IS_RIPNG_DEBUG_EVENT) | |
132 | zlog_debug( | |
133 | "RIPng %s leave from all-rip-routers multicast group", | |
134 | ifp->name); | |
135 | ||
136 | if (ret < 0) | |
137 | return -1; | |
138 | } | |
a94434b6 | 139 | |
d62a17ae | 140 | return 0; |
a94434b6 | 141 | } |
142 | ||
143 | /* How many link local IPv6 address could be used on the interface ? */ | |
d62a17ae | 144 | static int ripng_if_ipv6_lladdress_check(struct interface *ifp) |
a94434b6 | 145 | { |
d62a17ae | 146 | struct listnode *nn; |
147 | struct connected *connected; | |
148 | int count = 0; | |
a94434b6 | 149 | |
d62a17ae | 150 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, nn, connected)) { |
151 | struct prefix *p; | |
152 | p = connected->address; | |
718e3744 | 153 | |
d62a17ae | 154 | if ((p->family == AF_INET6) |
155 | && IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6)) | |
156 | count++; | |
157 | } | |
718e3744 | 158 | |
d62a17ae | 159 | return count; |
718e3744 | 160 | } |
161 | ||
d62a17ae | 162 | static int ripng_if_down(struct interface *ifp) |
718e3744 | 163 | { |
fe08ba7e | 164 | struct agg_node *rp; |
d62a17ae | 165 | struct ripng_info *rinfo; |
166 | struct ripng_interface *ri; | |
5c84b9a5 | 167 | struct ripng *ripng; |
d62a17ae | 168 | struct list *list = NULL; |
169 | struct listnode *listnode = NULL, *nextnode = NULL; | |
170 | ||
5c84b9a5 | 171 | ri = ifp->info; |
29ceb848 IR |
172 | |
173 | THREAD_OFF(ri->t_wakeup); | |
174 | ||
5c84b9a5 RW |
175 | ripng = ri->ripng; |
176 | ||
d62a17ae | 177 | if (ripng) |
fe08ba7e DS |
178 | for (rp = agg_route_top(ripng->table); rp; |
179 | rp = agg_route_next(rp)) | |
d62a17ae | 180 | if ((list = rp->info) != NULL) |
181 | for (ALL_LIST_ELEMENTS(list, listnode, nextnode, | |
182 | rinfo)) | |
183 | if (rinfo->ifindex == ifp->ifindex) | |
5c84b9a5 | 184 | ripng_ecmp_delete(ripng, rinfo); |
d62a17ae | 185 | |
d62a17ae | 186 | |
187 | if (ri->running) { | |
188 | if (IS_RIPNG_DEBUG_EVENT) | |
189 | zlog_debug("turn off %s", ifp->name); | |
190 | ||
191 | /* Leave from multicast group. */ | |
fa3bf3a2 MS |
192 | if (ripng) |
193 | ripng_multicast_leave(ifp, ripng->sock); | |
d62a17ae | 194 | |
195 | ri->running = 0; | |
196 | } | |
197 | ||
198 | return 0; | |
718e3744 | 199 | } |
200 | ||
201 | /* Inteface link up message processing. */ | |
ddbf3e60 | 202 | static int ripng_ifp_up(struct interface *ifp) |
718e3744 | 203 | { |
24c4ee4a DS |
204 | if (IS_RIPNG_DEBUG_ZEBRA) { |
205 | struct vrf *vrf = vrf_lookup_by_id(ifp->vrf_id); | |
206 | ||
d62a17ae | 207 | zlog_debug( |
24c4ee4a DS |
208 | "interface up %s vrf %s(%u) index %d flags %llx metric %d mtu %d", |
209 | ifp->name, VRF_LOGNAME(vrf), ifp->vrf_id, ifp->ifindex, | |
dde7b15b | 210 | (unsigned long long)ifp->flags, ifp->metric, ifp->mtu6); |
24c4ee4a | 211 | } |
dde7b15b RW |
212 | |
213 | ripng_interface_sync(ifp); | |
718e3744 | 214 | |
d62a17ae | 215 | /* Check if this interface is RIPng enabled or not. */ |
216 | ripng_enable_apply(ifp); | |
718e3744 | 217 | |
d62a17ae | 218 | /* Check for a passive interface. */ |
219 | ripng_passive_interface_apply(ifp); | |
718e3744 | 220 | |
d62a17ae | 221 | /* Apply distribute list to the all interface. */ |
222 | ripng_distribute_update_interface(ifp); | |
718e3744 | 223 | |
d62a17ae | 224 | return 0; |
718e3744 | 225 | } |
226 | ||
227 | /* Inteface link down message processing. */ | |
b0b69e59 | 228 | static int ripng_ifp_down(struct interface *ifp) |
718e3744 | 229 | { |
dde7b15b | 230 | ripng_interface_sync(ifp); |
d62a17ae | 231 | ripng_if_down(ifp); |
718e3744 | 232 | |
24c4ee4a DS |
233 | if (IS_RIPNG_DEBUG_ZEBRA) { |
234 | struct vrf *vrf = vrf_lookup_by_id(ifp->vrf_id); | |
235 | ||
d62a17ae | 236 | zlog_debug( |
24c4ee4a DS |
237 | "interface down %s vrf %s(%u) index %d flags %#llx metric %d mtu %d", |
238 | ifp->name, VRF_LOGNAME(vrf), ifp->vrf_id, ifp->ifindex, | |
dde7b15b | 239 | (unsigned long long)ifp->flags, ifp->metric, ifp->mtu6); |
24c4ee4a | 240 | } |
718e3744 | 241 | |
d62a17ae | 242 | return 0; |
718e3744 | 243 | } |
244 | ||
245 | /* Inteface addition message from zebra. */ | |
ef7bd2a3 | 246 | static int ripng_ifp_create(struct interface *ifp) |
718e3744 | 247 | { |
dde7b15b | 248 | ripng_interface_sync(ifp); |
718e3744 | 249 | |
24c4ee4a DS |
250 | if (IS_RIPNG_DEBUG_ZEBRA) { |
251 | struct vrf *vrf = vrf_lookup_by_id(ifp->vrf_id); | |
252 | ||
d62a17ae | 253 | zlog_debug( |
24c4ee4a DS |
254 | "RIPng interface add %s vrf %s(%u) index %d flags %#llx metric %d mtu %d", |
255 | ifp->name, VRF_LOGNAME(vrf), ifp->vrf_id, ifp->ifindex, | |
dde7b15b | 256 | (unsigned long long)ifp->flags, ifp->metric, ifp->mtu6); |
24c4ee4a | 257 | } |
718e3744 | 258 | |
d62a17ae | 259 | /* Check is this interface is RIP enabled or not.*/ |
260 | ripng_enable_apply(ifp); | |
718e3744 | 261 | |
d62a17ae | 262 | /* Apply distribute list to the interface. */ |
263 | ripng_distribute_update_interface(ifp); | |
718e3744 | 264 | |
d62a17ae | 265 | /* Check interface routemap. */ |
266 | ripng_if_rmap_update_interface(ifp); | |
718e3744 | 267 | |
d62a17ae | 268 | return 0; |
718e3744 | 269 | } |
270 | ||
3c3c3252 | 271 | static int ripng_ifp_destroy(struct interface *ifp) |
718e3744 | 272 | { |
24c4ee4a DS |
273 | struct vrf *vrf = vrf_lookup_by_id(ifp->vrf_id); |
274 | ||
dde7b15b | 275 | ripng_interface_sync(ifp); |
d62a17ae | 276 | if (if_is_up(ifp)) { |
277 | ripng_if_down(ifp); | |
278 | } | |
a94434b6 | 279 | |
13c5f95e DS |
280 | if (IS_RIPNG_DEBUG_ZEBRA) |
281 | zlog_debug( | |
282 | "interface delete %s vrf %s(%u) index %d flags %#llx metric %d mtu %d", | |
283 | ifp->name, VRF_LOGNAME(vrf), ifp->vrf_id, ifp->ifindex, | |
284 | (unsigned long long)ifp->flags, ifp->metric, ifp->mtu6); | |
a94434b6 | 285 | |
d62a17ae | 286 | return 0; |
718e3744 | 287 | } |
288 | ||
dde7b15b | 289 | /* VRF update for an interface. */ |
121f9dee | 290 | int ripng_interface_vrf_update(ZAPI_CALLBACK_ARGS) |
dde7b15b RW |
291 | { |
292 | struct interface *ifp; | |
293 | vrf_id_t new_vrf_id; | |
294 | ||
295 | ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id, | |
296 | &new_vrf_id); | |
297 | if (!ifp) | |
298 | return 0; | |
299 | ||
24c4ee4a DS |
300 | if (IS_RIPNG_DEBUG_ZEBRA) { |
301 | struct vrf *vrf = vrf_lookup_by_id(ifp->vrf_id); | |
302 | struct vrf *nvrf = vrf_lookup_by_id(new_vrf_id); | |
303 | ||
304 | zlog_debug("interface %s VRF change vrf %s(%u) new vrf %s(%u)", | |
305 | ifp->name, VRF_LOGNAME(vrf), vrf_id, | |
306 | VRF_LOGNAME(nvrf), new_vrf_id); | |
307 | } | |
dde7b15b | 308 | |
a36898e7 | 309 | if_update_to_new_vrf(ifp, new_vrf_id); |
dde7b15b RW |
310 | ripng_interface_sync(ifp); |
311 | ||
312 | return 0; | |
313 | } | |
314 | ||
5c84b9a5 | 315 | void ripng_interface_clean(struct ripng *ripng) |
a94434b6 | 316 | { |
d62a17ae | 317 | struct interface *ifp; |
318 | struct ripng_interface *ri; | |
a94434b6 | 319 | |
dde7b15b | 320 | FOR_ALL_INTERFACES (ripng->vrf, ifp) { |
d62a17ae | 321 | ri = ifp->info; |
a94434b6 | 322 | |
d62a17ae | 323 | ri->enable_network = 0; |
324 | ri->enable_interface = 0; | |
325 | ri->running = 0; | |
a94434b6 | 326 | |
b3d6bc6e | 327 | thread_cancel(&ri->t_wakeup); |
d62a17ae | 328 | } |
329 | } | |
a94434b6 | 330 | |
d62a17ae | 331 | static void ripng_apply_address_add(struct connected *ifc) |
332 | { | |
5c84b9a5 RW |
333 | struct ripng_interface *ri = ifc->ifp->info; |
334 | struct ripng *ripng = ri->ripng; | |
d62a17ae | 335 | struct prefix_ipv6 address; |
336 | struct prefix *p; | |
337 | ||
338 | if (!ripng) | |
339 | return; | |
340 | ||
341 | if (!if_is_up(ifc->ifp)) | |
342 | return; | |
343 | ||
344 | p = ifc->address; | |
345 | ||
346 | memset(&address, 0, sizeof(address)); | |
347 | address.family = p->family; | |
348 | address.prefix = p->u.prefix6; | |
349 | address.prefixlen = p->prefixlen; | |
350 | apply_mask_ipv6(&address); | |
351 | ||
352 | /* Check if this interface is RIP enabled or not | |
353 | or Check if this address's prefix is RIP enabled */ | |
5c84b9a5 | 354 | if ((ripng_enable_if_lookup(ripng, ifc->ifp->name) >= 0) |
d62a17ae | 355 | || (ripng_enable_network_lookup2(ifc) >= 0)) |
5c84b9a5 | 356 | ripng_redistribute_add(ripng, ZEBRA_ROUTE_CONNECT, |
d62a17ae | 357 | RIPNG_ROUTE_INTERFACE, &address, |
358 | ifc->ifp->ifindex, NULL, 0); | |
a94434b6 | 359 | } |
360 | ||
121f9dee | 361 | int ripng_interface_address_add(ZAPI_CALLBACK_ARGS) |
718e3744 | 362 | { |
d62a17ae | 363 | struct connected *c; |
364 | struct prefix *p; | |
718e3744 | 365 | |
d62a17ae | 366 | c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD, |
367 | zclient->ibuf, vrf_id); | |
718e3744 | 368 | |
d62a17ae | 369 | if (c == NULL) |
370 | return 0; | |
718e3744 | 371 | |
d62a17ae | 372 | p = c->address; |
718e3744 | 373 | |
d62a17ae | 374 | if (p->family == AF_INET6) { |
375 | struct ripng_interface *ri = c->ifp->info; | |
a94434b6 | 376 | |
d62a17ae | 377 | if (IS_RIPNG_DEBUG_ZEBRA) |
e0259674 | 378 | zlog_debug("RIPng connected address %pFX add", p); |
a94434b6 | 379 | |
d62a17ae | 380 | /* Check is this prefix needs to be redistributed. */ |
381 | ripng_apply_address_add(c); | |
a94434b6 | 382 | |
d62a17ae | 383 | /* Let's try once again whether the interface could be activated |
384 | */ | |
385 | if (!ri->running) { | |
386 | /* Check if this interface is RIP enabled or not.*/ | |
387 | ripng_enable_apply(c->ifp); | |
a94434b6 | 388 | |
d62a17ae | 389 | /* Apply distribute list to the interface. */ |
390 | ripng_distribute_update_interface(c->ifp); | |
718e3744 | 391 | |
d62a17ae | 392 | /* Check interface routemap. */ |
393 | ripng_if_rmap_update_interface(c->ifp); | |
394 | } | |
395 | } | |
396 | ||
397 | return 0; | |
718e3744 | 398 | } |
399 | ||
d62a17ae | 400 | static void ripng_apply_address_del(struct connected *ifc) |
401 | { | |
5c84b9a5 RW |
402 | struct ripng_interface *ri = ifc->ifp->info; |
403 | struct ripng *ripng = ri->ripng; | |
d62a17ae | 404 | struct prefix_ipv6 address; |
405 | struct prefix *p; | |
a94434b6 | 406 | |
d62a17ae | 407 | if (!ripng) |
408 | return; | |
a94434b6 | 409 | |
d62a17ae | 410 | if (!if_is_up(ifc->ifp)) |
411 | return; | |
a94434b6 | 412 | |
d62a17ae | 413 | p = ifc->address; |
a94434b6 | 414 | |
d62a17ae | 415 | memset(&address, 0, sizeof(address)); |
416 | address.family = p->family; | |
417 | address.prefix = p->u.prefix6; | |
418 | address.prefixlen = p->prefixlen; | |
419 | apply_mask_ipv6(&address); | |
a94434b6 | 420 | |
5c84b9a5 RW |
421 | ripng_redistribute_delete(ripng, ZEBRA_ROUTE_CONNECT, |
422 | RIPNG_ROUTE_INTERFACE, &address, | |
423 | ifc->ifp->ifindex); | |
a94434b6 | 424 | } |
425 | ||
121f9dee | 426 | int ripng_interface_address_delete(ZAPI_CALLBACK_ARGS) |
718e3744 | 427 | { |
d62a17ae | 428 | struct connected *ifc; |
429 | struct prefix *p; | |
d62a17ae | 430 | |
431 | ifc = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE, | |
432 | zclient->ibuf, vrf_id); | |
433 | ||
434 | if (ifc) { | |
435 | p = ifc->address; | |
436 | ||
437 | if (p->family == AF_INET6) { | |
438 | if (IS_RIPNG_DEBUG_ZEBRA) | |
439 | zlog_debug( | |
e0259674 DS |
440 | "RIPng connected address %pFX delete", |
441 | p); | |
d62a17ae | 442 | |
443 | /* Check wether this prefix needs to be removed. */ | |
444 | ripng_apply_address_del(ifc); | |
445 | } | |
721c0857 | 446 | connected_free(&ifc); |
718e3744 | 447 | } |
718e3744 | 448 | |
d62a17ae | 449 | return 0; |
718e3744 | 450 | } |
6b0655a2 | 451 | |
718e3744 | 452 | /* Lookup RIPng enable network. */ |
a94434b6 | 453 | /* Check wether the interface has at least a connected prefix that |
29b94d58 | 454 | * is within the ripng->enable_network table. */ |
d62a17ae | 455 | static int ripng_enable_network_lookup_if(struct interface *ifp) |
718e3744 | 456 | { |
5c84b9a5 RW |
457 | struct ripng_interface *ri = ifp->info; |
458 | struct ripng *ripng = ri->ripng; | |
d62a17ae | 459 | struct listnode *node; |
460 | struct connected *connected; | |
461 | struct prefix_ipv6 address; | |
462 | ||
29b94d58 RW |
463 | if (!ripng) |
464 | return -1; | |
465 | ||
d62a17ae | 466 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) { |
467 | struct prefix *p; | |
fe08ba7e | 468 | struct agg_node *n; |
d62a17ae | 469 | |
470 | p = connected->address; | |
471 | ||
472 | if (p->family == AF_INET6) { | |
473 | address.family = AF_INET6; | |
474 | address.prefix = p->u.prefix6; | |
475 | address.prefixlen = IPV6_MAX_BITLEN; | |
476 | ||
29b94d58 | 477 | n = agg_node_match(ripng->enable_network, |
fe08ba7e | 478 | (struct prefix *)&address); |
ee6f7757 | 479 | if (n) { |
fe08ba7e | 480 | agg_unlock_node(n); |
d62a17ae | 481 | return 1; |
482 | } | |
483 | } | |
484 | } | |
485 | return -1; | |
718e3744 | 486 | } |
487 | ||
29b94d58 | 488 | /* Check wether connected is within the ripng->enable_network table. */ |
d62a17ae | 489 | static int ripng_enable_network_lookup2(struct connected *connected) |
a94434b6 | 490 | { |
5c84b9a5 RW |
491 | struct ripng_interface *ri = connected->ifp->info; |
492 | struct ripng *ripng = ri->ripng; | |
d62a17ae | 493 | struct prefix_ipv6 address; |
494 | struct prefix *p; | |
a94434b6 | 495 | |
29b94d58 RW |
496 | if (!ripng) |
497 | return -1; | |
498 | ||
d62a17ae | 499 | p = connected->address; |
a94434b6 | 500 | |
d62a17ae | 501 | if (p->family == AF_INET6) { |
fe08ba7e | 502 | struct agg_node *node; |
a94434b6 | 503 | |
d62a17ae | 504 | address.family = p->family; |
505 | address.prefix = p->u.prefix6; | |
506 | address.prefixlen = IPV6_MAX_BITLEN; | |
a94434b6 | 507 | |
d62a17ae | 508 | /* LPM on p->family, p->u.prefix6/IPV6_MAX_BITLEN within |
29b94d58 RW |
509 | * ripng->enable_network */ |
510 | node = agg_node_match(ripng->enable_network, | |
fe08ba7e | 511 | (struct prefix *)&address); |
a94434b6 | 512 | |
d62a17ae | 513 | if (node) { |
fe08ba7e | 514 | agg_unlock_node(node); |
d62a17ae | 515 | return 1; |
516 | } | |
517 | } | |
a94434b6 | 518 | |
d62a17ae | 519 | return -1; |
a94434b6 | 520 | } |
521 | ||
718e3744 | 522 | /* Add RIPng enable network. */ |
5c84b9a5 | 523 | int ripng_enable_network_add(struct ripng *ripng, struct prefix *p) |
718e3744 | 524 | { |
fe08ba7e | 525 | struct agg_node *node; |
718e3744 | 526 | |
29b94d58 | 527 | node = agg_node_get(ripng->enable_network, p); |
718e3744 | 528 | |
d62a17ae | 529 | if (node->info) { |
fe08ba7e | 530 | agg_unlock_node(node); |
cc48702b | 531 | return NB_ERR_INCONSISTENCY; |
d62a17ae | 532 | } else |
533 | node->info = (void *)1; | |
718e3744 | 534 | |
d62a17ae | 535 | /* XXX: One should find a better solution than a generic one */ |
5c84b9a5 | 536 | ripng_enable_apply_all(ripng); |
a94434b6 | 537 | |
cc48702b | 538 | return NB_OK; |
718e3744 | 539 | } |
540 | ||
541 | /* Delete RIPng enable network. */ | |
5c84b9a5 | 542 | int ripng_enable_network_delete(struct ripng *ripng, struct prefix *p) |
718e3744 | 543 | { |
fe08ba7e | 544 | struct agg_node *node; |
718e3744 | 545 | |
29b94d58 | 546 | node = agg_node_lookup(ripng->enable_network, p); |
d62a17ae | 547 | if (node) { |
548 | node->info = NULL; | |
718e3744 | 549 | |
d62a17ae | 550 | /* Unlock info lock. */ |
fe08ba7e | 551 | agg_unlock_node(node); |
718e3744 | 552 | |
d62a17ae | 553 | /* Unlock lookup lock. */ |
fe08ba7e | 554 | agg_unlock_node(node); |
718e3744 | 555 | |
cc48702b | 556 | return NB_OK; |
d62a17ae | 557 | } |
cc48702b RW |
558 | |
559 | return NB_ERR_INCONSISTENCY; | |
718e3744 | 560 | } |
561 | ||
562 | /* Lookup function. */ | |
5c84b9a5 | 563 | static int ripng_enable_if_lookup(struct ripng *ripng, const char *ifname) |
718e3744 | 564 | { |
d62a17ae | 565 | unsigned int i; |
566 | char *str; | |
567 | ||
b0ba762f RW |
568 | if (!ripng) |
569 | return -1; | |
570 | ||
571 | for (i = 0; i < vector_active(ripng->enable_if); i++) | |
572 | if ((str = vector_slot(ripng->enable_if, i)) != NULL) | |
d62a17ae | 573 | if (strcmp(str, ifname) == 0) |
574 | return i; | |
575 | return -1; | |
718e3744 | 576 | } |
577 | ||
5c84b9a5 | 578 | int ripng_enable_if_add(struct ripng *ripng, const char *ifname) |
718e3744 | 579 | { |
d62a17ae | 580 | int ret; |
718e3744 | 581 | |
5c84b9a5 | 582 | ret = ripng_enable_if_lookup(ripng, ifname); |
d62a17ae | 583 | if (ret >= 0) |
cc48702b | 584 | return NB_ERR_INCONSISTENCY; |
718e3744 | 585 | |
b0ba762f | 586 | vector_set(ripng->enable_if, strdup(ifname)); |
718e3744 | 587 | |
5c84b9a5 | 588 | ripng_enable_apply_all(ripng); |
a94434b6 | 589 | |
cc48702b | 590 | return NB_OK; |
718e3744 | 591 | } |
592 | ||
5c84b9a5 | 593 | int ripng_enable_if_delete(struct ripng *ripng, const char *ifname) |
718e3744 | 594 | { |
d62a17ae | 595 | int index; |
596 | char *str; | |
718e3744 | 597 | |
5c84b9a5 | 598 | index = ripng_enable_if_lookup(ripng, ifname); |
d62a17ae | 599 | if (index < 0) |
cc48702b | 600 | return NB_ERR_INCONSISTENCY; |
718e3744 | 601 | |
b0ba762f | 602 | str = vector_slot(ripng->enable_if, index); |
d62a17ae | 603 | free(str); |
b0ba762f | 604 | vector_unset(ripng->enable_if, index); |
718e3744 | 605 | |
5c84b9a5 | 606 | ripng_enable_apply_all(ripng); |
a94434b6 | 607 | |
cc48702b | 608 | return NB_OK; |
718e3744 | 609 | } |
610 | ||
611 | /* Wake up interface. */ | |
d62a17ae | 612 | static int ripng_interface_wakeup(struct thread *t) |
718e3744 | 613 | { |
d62a17ae | 614 | struct interface *ifp; |
615 | struct ripng_interface *ri; | |
718e3744 | 616 | |
d62a17ae | 617 | /* Get interface. */ |
618 | ifp = THREAD_ARG(t); | |
718e3744 | 619 | |
d62a17ae | 620 | ri = ifp->info; |
718e3744 | 621 | |
d62a17ae | 622 | /* Join to multicast group. */ |
5c84b9a5 | 623 | if (ripng_multicast_join(ifp, ri->ripng->sock) < 0) { |
450971aa | 624 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
625 | "multicast join failed, interface %s not running", |
626 | ifp->name); | |
d62a17ae | 627 | return 0; |
628 | } | |
629 | ||
630 | /* Set running flag. */ | |
631 | ri->running = 1; | |
718e3744 | 632 | |
d62a17ae | 633 | /* Send RIP request to the interface. */ |
634 | ripng_request(ifp); | |
718e3744 | 635 | |
d62a17ae | 636 | return 0; |
718e3744 | 637 | } |
638 | ||
d62a17ae | 639 | static void ripng_connect_set(struct interface *ifp, int set) |
a94434b6 | 640 | { |
5c84b9a5 RW |
641 | struct ripng_interface *ri = ifp->info; |
642 | struct ripng *ripng = ri->ripng; | |
d62a17ae | 643 | struct listnode *node, *nnode; |
644 | struct connected *connected; | |
645 | struct prefix_ipv6 address; | |
646 | ||
647 | for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, connected)) { | |
648 | struct prefix *p; | |
649 | p = connected->address; | |
650 | ||
651 | if (p->family != AF_INET6) | |
652 | continue; | |
653 | ||
654 | address.family = AF_INET6; | |
655 | address.prefix = p->u.prefix6; | |
656 | address.prefixlen = p->prefixlen; | |
657 | apply_mask_ipv6(&address); | |
658 | ||
659 | if (set) { | |
660 | /* Check once more wether this prefix is within a | |
661 | * "network IF_OR_PREF" one */ | |
5c84b9a5 RW |
662 | if ((ripng_enable_if_lookup(ripng, connected->ifp->name) |
663 | >= 0) | |
d62a17ae | 664 | || (ripng_enable_network_lookup2(connected) >= 0)) |
665 | ripng_redistribute_add( | |
5c84b9a5 | 666 | ripng, ZEBRA_ROUTE_CONNECT, |
d62a17ae | 667 | RIPNG_ROUTE_INTERFACE, &address, |
668 | connected->ifp->ifindex, NULL, 0); | |
669 | } else { | |
5c84b9a5 RW |
670 | ripng_redistribute_delete(ripng, ZEBRA_ROUTE_CONNECT, |
671 | RIPNG_ROUTE_INTERFACE, | |
672 | &address, | |
673 | connected->ifp->ifindex); | |
674 | if (ripng_redistribute_check(ripng, | |
675 | ZEBRA_ROUTE_CONNECT)) | |
d62a17ae | 676 | ripng_redistribute_add( |
5c84b9a5 | 677 | ripng, ZEBRA_ROUTE_CONNECT, |
d62a17ae | 678 | RIPNG_ROUTE_REDISTRIBUTE, &address, |
679 | connected->ifp->ifindex, NULL, 0); | |
680 | } | |
681 | } | |
a94434b6 | 682 | } |
683 | ||
718e3744 | 684 | /* Check RIPng is enabed on this interface. */ |
d62a17ae | 685 | void ripng_enable_apply(struct interface *ifp) |
718e3744 | 686 | { |
d62a17ae | 687 | int ret; |
688 | struct ripng_interface *ri = NULL; | |
689 | ||
690 | /* Check interface. */ | |
691 | if (!if_is_up(ifp)) | |
692 | return; | |
693 | ||
694 | ri = ifp->info; | |
695 | ||
696 | /* Is this interface a candidate for RIPng ? */ | |
697 | ret = ripng_enable_network_lookup_if(ifp); | |
698 | ||
699 | /* If the interface is matched. */ | |
700 | if (ret > 0) | |
701 | ri->enable_network = 1; | |
702 | else | |
703 | ri->enable_network = 0; | |
704 | ||
705 | /* Check interface name configuration. */ | |
5c84b9a5 | 706 | ret = ripng_enable_if_lookup(ri->ripng, ifp->name); |
d62a17ae | 707 | if (ret >= 0) |
708 | ri->enable_interface = 1; | |
709 | else | |
710 | ri->enable_interface = 0; | |
711 | ||
712 | /* any candidate interface MUST have a link-local IPv6 address */ | |
713 | if ((!ripng_if_ipv6_lladdress_check(ifp)) | |
714 | && (ri->enable_network || ri->enable_interface)) { | |
715 | ri->enable_network = 0; | |
716 | ri->enable_interface = 0; | |
717 | zlog_warn("Interface %s does not have any link-local address", | |
718 | ifp->name); | |
719 | } | |
720 | ||
721 | /* Update running status of the interface. */ | |
722 | if (ri->enable_network || ri->enable_interface) { | |
723 | zlog_info("RIPng INTERFACE ON %s", ifp->name); | |
724 | ||
725 | /* Add interface wake up thread. */ | |
726 | thread_add_timer(master, ripng_interface_wakeup, ifp, 1, | |
727 | &ri->t_wakeup); | |
728 | ||
729 | ripng_connect_set(ifp, 1); | |
730 | } else { | |
731 | if (ri->running) { | |
732 | /* Might as well clean up the route table as well | |
733 | * ripng_if_down sets to 0 ri->running, and displays | |
734 | *"turn off %s" | |
735 | **/ | |
736 | ripng_if_down(ifp); | |
737 | ||
738 | ripng_connect_set(ifp, 0); | |
739 | } | |
718e3744 | 740 | } |
718e3744 | 741 | } |
742 | ||
743 | /* Set distribute list to all interfaces. */ | |
5c84b9a5 | 744 | static void ripng_enable_apply_all(struct ripng *ripng) |
718e3744 | 745 | { |
d62a17ae | 746 | struct interface *ifp; |
718e3744 | 747 | |
dde7b15b | 748 | FOR_ALL_INTERFACES (ripng->vrf, ifp) |
d62a17ae | 749 | ripng_enable_apply(ifp); |
718e3744 | 750 | } |
6b0655a2 | 751 | |
a94434b6 | 752 | /* Clear all network and neighbor configuration */ |
5c84b9a5 | 753 | void ripng_clean_network(struct ripng *ripng) |
a94434b6 | 754 | { |
d62a17ae | 755 | unsigned int i; |
756 | char *str; | |
fe08ba7e | 757 | struct agg_node *rn; |
d62a17ae | 758 | |
29b94d58 RW |
759 | /* ripng->enable_network */ |
760 | for (rn = agg_route_top(ripng->enable_network); rn; | |
fe08ba7e | 761 | rn = agg_route_next(rn)) |
d62a17ae | 762 | if (rn->info) { |
763 | rn->info = NULL; | |
fe08ba7e | 764 | agg_unlock_node(rn); |
d62a17ae | 765 | } |
766 | ||
b0ba762f RW |
767 | /* ripng->enable_if */ |
768 | for (i = 0; i < vector_active(ripng->enable_if); i++) | |
769 | if ((str = vector_slot(ripng->enable_if, i)) != NULL) { | |
d62a17ae | 770 | free(str); |
b0ba762f | 771 | vector_slot(ripng->enable_if, i) = NULL; |
d62a17ae | 772 | } |
a94434b6 | 773 | } |
6b0655a2 | 774 | |
718e3744 | 775 | /* Utility function for looking up passive interface settings. */ |
5c84b9a5 RW |
776 | static int ripng_passive_interface_lookup(struct ripng *ripng, |
777 | const char *ifname) | |
718e3744 | 778 | { |
d62a17ae | 779 | unsigned int i; |
780 | char *str; | |
781 | ||
0c32404f RW |
782 | for (i = 0; i < vector_active(ripng->passive_interface); i++) |
783 | if ((str = vector_slot(ripng->passive_interface, i)) != NULL) | |
d62a17ae | 784 | if (strcmp(str, ifname) == 0) |
785 | return i; | |
786 | return -1; | |
718e3744 | 787 | } |
788 | ||
d62a17ae | 789 | void ripng_passive_interface_apply(struct interface *ifp) |
718e3744 | 790 | { |
d62a17ae | 791 | int ret; |
792 | struct ripng_interface *ri; | |
5c84b9a5 | 793 | struct ripng *ripng; |
718e3744 | 794 | |
d62a17ae | 795 | ri = ifp->info; |
5c84b9a5 RW |
796 | ripng = ri->ripng; |
797 | if (!ripng) | |
798 | return; | |
718e3744 | 799 | |
5c84b9a5 | 800 | ret = ripng_passive_interface_lookup(ripng, ifp->name); |
d62a17ae | 801 | if (ret < 0) |
802 | ri->passive = 0; | |
803 | else | |
804 | ri->passive = 1; | |
718e3744 | 805 | } |
806 | ||
5c84b9a5 | 807 | static void ripng_passive_interface_apply_all(struct ripng *ripng) |
718e3744 | 808 | { |
d62a17ae | 809 | struct interface *ifp; |
718e3744 | 810 | |
dde7b15b | 811 | FOR_ALL_INTERFACES (ripng->vrf, ifp) |
d62a17ae | 812 | ripng_passive_interface_apply(ifp); |
718e3744 | 813 | } |
814 | ||
815 | /* Passive interface. */ | |
5c84b9a5 | 816 | int ripng_passive_interface_set(struct ripng *ripng, const char *ifname) |
718e3744 | 817 | { |
5c84b9a5 | 818 | if (ripng_passive_interface_lookup(ripng, ifname) >= 0) |
22e8c7ae | 819 | return NB_ERR_INCONSISTENCY; |
718e3744 | 820 | |
0c32404f | 821 | vector_set(ripng->passive_interface, strdup(ifname)); |
718e3744 | 822 | |
5c84b9a5 | 823 | ripng_passive_interface_apply_all(ripng); |
718e3744 | 824 | |
22e8c7ae | 825 | return NB_OK; |
718e3744 | 826 | } |
827 | ||
5c84b9a5 | 828 | int ripng_passive_interface_unset(struct ripng *ripng, const char *ifname) |
718e3744 | 829 | { |
d62a17ae | 830 | int i; |
831 | char *str; | |
718e3744 | 832 | |
5c84b9a5 | 833 | i = ripng_passive_interface_lookup(ripng, ifname); |
d62a17ae | 834 | if (i < 0) |
22e8c7ae | 835 | return NB_ERR_INCONSISTENCY; |
718e3744 | 836 | |
0c32404f | 837 | str = vector_slot(ripng->passive_interface, i); |
d62a17ae | 838 | free(str); |
0c32404f | 839 | vector_unset(ripng->passive_interface, i); |
718e3744 | 840 | |
5c84b9a5 | 841 | ripng_passive_interface_apply_all(ripng); |
718e3744 | 842 | |
22e8c7ae | 843 | return NB_OK; |
718e3744 | 844 | } |
845 | ||
846 | /* Free all configured RIP passive-interface settings. */ | |
5c84b9a5 | 847 | void ripng_passive_interface_clean(struct ripng *ripng) |
718e3744 | 848 | { |
d62a17ae | 849 | unsigned int i; |
850 | char *str; | |
851 | ||
0c32404f RW |
852 | for (i = 0; i < vector_active(ripng->passive_interface); i++) |
853 | if ((str = vector_slot(ripng->passive_interface, i)) != NULL) { | |
d62a17ae | 854 | free(str); |
0c32404f | 855 | vector_slot(ripng->passive_interface, i) = NULL; |
d62a17ae | 856 | } |
5c84b9a5 | 857 | ripng_passive_interface_apply_all(ripng); |
718e3744 | 858 | } |
859 | ||
860 | /* Write RIPng enable network and interface to the vty. */ | |
5c84b9a5 | 861 | int ripng_network_write(struct vty *vty, struct ripng *ripng) |
718e3744 | 862 | { |
d62a17ae | 863 | unsigned int i; |
864 | const char *ifname; | |
fe08ba7e | 865 | struct agg_node *node; |
d62a17ae | 866 | |
867 | /* Write enable network. */ | |
29b94d58 | 868 | for (node = agg_route_top(ripng->enable_network); node; |
fe08ba7e | 869 | node = agg_route_next(node)) |
26a3ffd6 DS |
870 | if (node->info) |
871 | vty_out(vty, " %pRN\n", node); | |
d62a17ae | 872 | |
873 | /* Write enable interface. */ | |
b0ba762f RW |
874 | for (i = 0; i < vector_active(ripng->enable_if); i++) |
875 | if ((ifname = vector_slot(ripng->enable_if, i)) != NULL) | |
22e8c7ae | 876 | vty_out(vty, " %s\n", ifname); |
d62a17ae | 877 | |
878 | return 0; | |
718e3744 | 879 | } |
880 | ||
d62a17ae | 881 | static struct ripng_interface *ri_new(void) |
718e3744 | 882 | { |
d62a17ae | 883 | struct ripng_interface *ri; |
5c84b9a5 | 884 | |
eaf58ba9 | 885 | ri = XCALLOC(MTYPE_RIPNG_IF, sizeof(struct ripng_interface)); |
a94434b6 | 886 | |
d62a17ae | 887 | /* Set default split-horizon behavior. If the interface is Frame |
888 | Relay or SMDS is enabled, the default value for split-horizon is | |
889 | off. But currently Zebra does detect Frame Relay or SMDS | |
890 | interface. So all interface is set to split horizon. */ | |
d406db4c RW |
891 | ri->split_horizon = |
892 | yang_get_default_enum("%s/split-horizon", RIPNG_IFACE); | |
a94434b6 | 893 | |
d62a17ae | 894 | return ri; |
718e3744 | 895 | } |
896 | ||
dde7b15b RW |
897 | void ripng_interface_sync(struct interface *ifp) |
898 | { | |
899 | struct vrf *vrf; | |
900 | ||
a36898e7 | 901 | vrf = vrf_lookup_by_id(ifp->vrf_id); |
dde7b15b RW |
902 | if (vrf) { |
903 | struct ripng_interface *ri; | |
904 | ||
905 | ri = ifp->info; | |
906 | if (ri) | |
907 | ri->ripng = vrf->info; | |
908 | } | |
909 | } | |
910 | ||
d62a17ae | 911 | static int ripng_if_new_hook(struct interface *ifp) |
718e3744 | 912 | { |
d62a17ae | 913 | ifp->info = ri_new(); |
dde7b15b RW |
914 | ripng_interface_sync(ifp); |
915 | ||
d62a17ae | 916 | return 0; |
718e3744 | 917 | } |
918 | ||
a94434b6 | 919 | /* Called when interface structure deleted. */ |
d62a17ae | 920 | static int ripng_if_delete_hook(struct interface *ifp) |
a94434b6 | 921 | { |
eaf58ba9 | 922 | XFREE(MTYPE_RIPNG_IF, ifp->info); |
d62a17ae | 923 | return 0; |
a94434b6 | 924 | } |
925 | ||
718e3744 | 926 | /* Initialization of interface. */ |
4d762f26 | 927 | void ripng_if_init(void) |
718e3744 | 928 | { |
d62a17ae | 929 | /* Interface initialize. */ |
ce19a04a DL |
930 | hook_register_prio(if_add, 0, ripng_if_new_hook); |
931 | hook_register_prio(if_del, 0, ripng_if_delete_hook); | |
718e3744 | 932 | |
d62a17ae | 933 | /* Install interface node. */ |
104fd767 | 934 | if_cmd_init_default(); |
138c5a74 DS |
935 | if_zapi_callbacks(ripng_ifp_create, ripng_ifp_up, |
936 | ripng_ifp_down, ripng_ifp_destroy); | |
718e3744 | 937 | } |