]>
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 | |
46 | #define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP | |
47 | #endif | |
48 | #ifndef IPV6_LEAVE_GROUP | |
49 | #define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP | |
50 | #endif | |
51 | ||
eaf58ba9 DL |
52 | DEFINE_MTYPE_STATIC(RIPNGD, RIPNG_IF, "ripng interface") |
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 | */ | |
01b9e3fd | 78 | frr_elevate_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 RW |
171 | ri = ifp->info; |
172 | ripng = ri->ripng; | |
173 | ||
d62a17ae | 174 | if (ripng) |
fe08ba7e DS |
175 | for (rp = agg_route_top(ripng->table); rp; |
176 | rp = agg_route_next(rp)) | |
d62a17ae | 177 | if ((list = rp->info) != NULL) |
178 | for (ALL_LIST_ELEMENTS(list, listnode, nextnode, | |
179 | rinfo)) | |
180 | if (rinfo->ifindex == ifp->ifindex) | |
5c84b9a5 | 181 | ripng_ecmp_delete(ripng, rinfo); |
d62a17ae | 182 | |
d62a17ae | 183 | |
184 | if (ri->running) { | |
185 | if (IS_RIPNG_DEBUG_EVENT) | |
186 | zlog_debug("turn off %s", ifp->name); | |
187 | ||
188 | /* Leave from multicast group. */ | |
fa3bf3a2 MS |
189 | if (ripng) |
190 | ripng_multicast_leave(ifp, ripng->sock); | |
d62a17ae | 191 | |
192 | ri->running = 0; | |
193 | } | |
194 | ||
195 | return 0; | |
718e3744 | 196 | } |
197 | ||
198 | /* Inteface link up message processing. */ | |
121f9dee | 199 | int ripng_interface_up(ZAPI_CALLBACK_ARGS) |
718e3744 | 200 | { |
d62a17ae | 201 | struct stream *s; |
202 | struct interface *ifp; | |
718e3744 | 203 | |
d62a17ae | 204 | /* zebra_interface_state_read() updates interface structure in iflist. |
205 | */ | |
206 | s = zclient->ibuf; | |
207 | ifp = zebra_interface_state_read(s, vrf_id); | |
718e3744 | 208 | |
d62a17ae | 209 | if (ifp == NULL) |
210 | return 0; | |
718e3744 | 211 | |
d62a17ae | 212 | if (IS_RIPNG_DEBUG_ZEBRA) |
213 | zlog_debug( | |
dde7b15b | 214 | "interface up %s vrf %u index %d flags %llx metric %d mtu %d", |
a41c4e1b | 215 | ifp->name, ifp->vrf->vrf_id, ifp->ifindex, |
dde7b15b RW |
216 | (unsigned long long)ifp->flags, ifp->metric, ifp->mtu6); |
217 | ||
218 | ripng_interface_sync(ifp); | |
718e3744 | 219 | |
d62a17ae | 220 | /* Check if this interface is RIPng enabled or not. */ |
221 | ripng_enable_apply(ifp); | |
718e3744 | 222 | |
d62a17ae | 223 | /* Check for a passive interface. */ |
224 | ripng_passive_interface_apply(ifp); | |
718e3744 | 225 | |
d62a17ae | 226 | /* Apply distribute list to the all interface. */ |
227 | ripng_distribute_update_interface(ifp); | |
718e3744 | 228 | |
d62a17ae | 229 | return 0; |
718e3744 | 230 | } |
231 | ||
232 | /* Inteface link down message processing. */ | |
121f9dee | 233 | int ripng_interface_down(ZAPI_CALLBACK_ARGS) |
718e3744 | 234 | { |
d62a17ae | 235 | struct stream *s; |
236 | struct interface *ifp; | |
718e3744 | 237 | |
d62a17ae | 238 | /* zebra_interface_state_read() updates interface structure in iflist. |
239 | */ | |
240 | s = zclient->ibuf; | |
241 | ifp = zebra_interface_state_read(s, vrf_id); | |
718e3744 | 242 | |
d62a17ae | 243 | if (ifp == NULL) |
244 | return 0; | |
718e3744 | 245 | |
dde7b15b | 246 | ripng_interface_sync(ifp); |
d62a17ae | 247 | ripng_if_down(ifp); |
718e3744 | 248 | |
d62a17ae | 249 | if (IS_RIPNG_DEBUG_ZEBRA) |
250 | zlog_debug( | |
dde7b15b | 251 | "interface down %s vrf %u index %d flags %#llx metric %d mtu %d", |
a41c4e1b | 252 | ifp->name, ifp->vrf->vrf_id, ifp->ifindex, |
dde7b15b | 253 | (unsigned long long)ifp->flags, ifp->metric, ifp->mtu6); |
718e3744 | 254 | |
d62a17ae | 255 | return 0; |
718e3744 | 256 | } |
257 | ||
258 | /* Inteface addition message from zebra. */ | |
121f9dee | 259 | int ripng_interface_add(ZAPI_CALLBACK_ARGS) |
718e3744 | 260 | { |
d62a17ae | 261 | struct interface *ifp; |
718e3744 | 262 | |
d62a17ae | 263 | ifp = zebra_interface_add_read(zclient->ibuf, vrf_id); |
dde7b15b | 264 | ripng_interface_sync(ifp); |
718e3744 | 265 | |
d62a17ae | 266 | if (IS_RIPNG_DEBUG_ZEBRA) |
267 | zlog_debug( | |
dde7b15b | 268 | "RIPng interface add %s vrf %u index %d flags %#llx metric %d mtu %d", |
a41c4e1b | 269 | ifp->name, ifp->vrf->vrf_id, ifp->ifindex, |
dde7b15b | 270 | (unsigned long long)ifp->flags, ifp->metric, ifp->mtu6); |
718e3744 | 271 | |
d62a17ae | 272 | /* Check is this interface is RIP enabled or not.*/ |
273 | ripng_enable_apply(ifp); | |
718e3744 | 274 | |
d62a17ae | 275 | /* Apply distribute list to the interface. */ |
276 | ripng_distribute_update_interface(ifp); | |
718e3744 | 277 | |
d62a17ae | 278 | /* Check interface routemap. */ |
279 | ripng_if_rmap_update_interface(ifp); | |
718e3744 | 280 | |
d62a17ae | 281 | return 0; |
718e3744 | 282 | } |
283 | ||
121f9dee | 284 | int ripng_interface_delete(ZAPI_CALLBACK_ARGS) |
718e3744 | 285 | { |
d62a17ae | 286 | struct interface *ifp; |
287 | struct stream *s; | |
a94434b6 | 288 | |
d62a17ae | 289 | s = zclient->ibuf; |
290 | /* zebra_interface_state_read() updates interface structure in iflist | |
291 | */ | |
292 | ifp = zebra_interface_state_read(s, vrf_id); | |
a94434b6 | 293 | |
d62a17ae | 294 | if (ifp == NULL) |
295 | return 0; | |
a94434b6 | 296 | |
dde7b15b | 297 | ripng_interface_sync(ifp); |
d62a17ae | 298 | if (if_is_up(ifp)) { |
299 | ripng_if_down(ifp); | |
300 | } | |
a94434b6 | 301 | |
dde7b15b RW |
302 | zlog_info( |
303 | "interface delete %s vrf %u index %d flags %#llx metric %d mtu %d", | |
a41c4e1b | 304 | ifp->name, ifp->vrf->vrf_id, ifp->ifindex, |
dde7b15b | 305 | (unsigned long long)ifp->flags, ifp->metric, ifp->mtu6); |
a94434b6 | 306 | |
d62a17ae | 307 | /* To support pseudo interface do not free interface structure. */ |
308 | /* if_delete(ifp); */ | |
ff880b78 | 309 | if_set_index(ifp, IFINDEX_INTERNAL); |
a94434b6 | 310 | |
d62a17ae | 311 | return 0; |
718e3744 | 312 | } |
313 | ||
dde7b15b | 314 | /* VRF update for an interface. */ |
121f9dee | 315 | int ripng_interface_vrf_update(ZAPI_CALLBACK_ARGS) |
dde7b15b RW |
316 | { |
317 | struct interface *ifp; | |
318 | vrf_id_t new_vrf_id; | |
da85f5e0 | 319 | struct vrf *new_vrf; |
dde7b15b RW |
320 | |
321 | ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id, | |
322 | &new_vrf_id); | |
323 | if (!ifp) | |
324 | return 0; | |
325 | ||
da85f5e0 PG |
326 | new_vrf = vrf_lookup_by_id(new_vrf_id); |
327 | ||
dde7b15b RW |
328 | if (IS_RIPNG_DEBUG_ZEBRA) |
329 | zlog_debug("interface %s VRF change vrf_id %u new vrf id %u", | |
330 | ifp->name, vrf_id, new_vrf_id); | |
331 | ||
da85f5e0 | 332 | if_update_to_new_vrf(ifp, new_vrf); |
a41c4e1b | 333 | |
dde7b15b RW |
334 | ripng_interface_sync(ifp); |
335 | ||
336 | return 0; | |
337 | } | |
338 | ||
5c84b9a5 | 339 | void ripng_interface_clean(struct ripng *ripng) |
a94434b6 | 340 | { |
d62a17ae | 341 | struct interface *ifp; |
342 | struct ripng_interface *ri; | |
a94434b6 | 343 | |
dde7b15b | 344 | FOR_ALL_INTERFACES (ripng->vrf, ifp) { |
d62a17ae | 345 | ri = ifp->info; |
a94434b6 | 346 | |
d62a17ae | 347 | ri->enable_network = 0; |
348 | ri->enable_interface = 0; | |
349 | ri->running = 0; | |
a94434b6 | 350 | |
d62a17ae | 351 | if (ri->t_wakeup) { |
352 | thread_cancel(ri->t_wakeup); | |
353 | ri->t_wakeup = NULL; | |
354 | } | |
355 | } | |
356 | } | |
a94434b6 | 357 | |
d62a17ae | 358 | static void ripng_apply_address_add(struct connected *ifc) |
359 | { | |
5c84b9a5 RW |
360 | struct ripng_interface *ri = ifc->ifp->info; |
361 | struct ripng *ripng = ri->ripng; | |
d62a17ae | 362 | struct prefix_ipv6 address; |
363 | struct prefix *p; | |
364 | ||
365 | if (!ripng) | |
366 | return; | |
367 | ||
368 | if (!if_is_up(ifc->ifp)) | |
369 | return; | |
370 | ||
371 | p = ifc->address; | |
372 | ||
373 | memset(&address, 0, sizeof(address)); | |
374 | address.family = p->family; | |
375 | address.prefix = p->u.prefix6; | |
376 | address.prefixlen = p->prefixlen; | |
377 | apply_mask_ipv6(&address); | |
378 | ||
379 | /* Check if this interface is RIP enabled or not | |
380 | or Check if this address's prefix is RIP enabled */ | |
5c84b9a5 | 381 | if ((ripng_enable_if_lookup(ripng, ifc->ifp->name) >= 0) |
d62a17ae | 382 | || (ripng_enable_network_lookup2(ifc) >= 0)) |
5c84b9a5 | 383 | ripng_redistribute_add(ripng, ZEBRA_ROUTE_CONNECT, |
d62a17ae | 384 | RIPNG_ROUTE_INTERFACE, &address, |
385 | ifc->ifp->ifindex, NULL, 0); | |
a94434b6 | 386 | } |
387 | ||
121f9dee | 388 | int ripng_interface_address_add(ZAPI_CALLBACK_ARGS) |
718e3744 | 389 | { |
d62a17ae | 390 | struct connected *c; |
391 | struct prefix *p; | |
718e3744 | 392 | |
d62a17ae | 393 | c = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD, |
394 | zclient->ibuf, vrf_id); | |
718e3744 | 395 | |
d62a17ae | 396 | if (c == NULL) |
397 | return 0; | |
718e3744 | 398 | |
d62a17ae | 399 | p = c->address; |
718e3744 | 400 | |
d62a17ae | 401 | if (p->family == AF_INET6) { |
402 | struct ripng_interface *ri = c->ifp->info; | |
a94434b6 | 403 | |
d62a17ae | 404 | if (IS_RIPNG_DEBUG_ZEBRA) |
405 | zlog_debug("RIPng connected address %s/%d add", | |
406 | inet6_ntoa(p->u.prefix6), p->prefixlen); | |
a94434b6 | 407 | |
d62a17ae | 408 | /* Check is this prefix needs to be redistributed. */ |
409 | ripng_apply_address_add(c); | |
a94434b6 | 410 | |
d62a17ae | 411 | /* Let's try once again whether the interface could be activated |
412 | */ | |
413 | if (!ri->running) { | |
414 | /* Check if this interface is RIP enabled or not.*/ | |
415 | ripng_enable_apply(c->ifp); | |
a94434b6 | 416 | |
d62a17ae | 417 | /* Apply distribute list to the interface. */ |
418 | ripng_distribute_update_interface(c->ifp); | |
718e3744 | 419 | |
d62a17ae | 420 | /* Check interface routemap. */ |
421 | ripng_if_rmap_update_interface(c->ifp); | |
422 | } | |
423 | } | |
424 | ||
425 | return 0; | |
718e3744 | 426 | } |
427 | ||
d62a17ae | 428 | static void ripng_apply_address_del(struct connected *ifc) |
429 | { | |
5c84b9a5 RW |
430 | struct ripng_interface *ri = ifc->ifp->info; |
431 | struct ripng *ripng = ri->ripng; | |
d62a17ae | 432 | struct prefix_ipv6 address; |
433 | struct prefix *p; | |
a94434b6 | 434 | |
d62a17ae | 435 | if (!ripng) |
436 | return; | |
a94434b6 | 437 | |
d62a17ae | 438 | if (!if_is_up(ifc->ifp)) |
439 | return; | |
a94434b6 | 440 | |
d62a17ae | 441 | p = ifc->address; |
a94434b6 | 442 | |
d62a17ae | 443 | memset(&address, 0, sizeof(address)); |
444 | address.family = p->family; | |
445 | address.prefix = p->u.prefix6; | |
446 | address.prefixlen = p->prefixlen; | |
447 | apply_mask_ipv6(&address); | |
a94434b6 | 448 | |
5c84b9a5 RW |
449 | ripng_redistribute_delete(ripng, ZEBRA_ROUTE_CONNECT, |
450 | RIPNG_ROUTE_INTERFACE, &address, | |
451 | ifc->ifp->ifindex); | |
a94434b6 | 452 | } |
453 | ||
121f9dee | 454 | int ripng_interface_address_delete(ZAPI_CALLBACK_ARGS) |
718e3744 | 455 | { |
d62a17ae | 456 | struct connected *ifc; |
457 | struct prefix *p; | |
458 | char buf[INET6_ADDRSTRLEN]; | |
459 | ||
460 | ifc = zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE, | |
461 | zclient->ibuf, vrf_id); | |
462 | ||
463 | if (ifc) { | |
464 | p = ifc->address; | |
465 | ||
466 | if (p->family == AF_INET6) { | |
467 | if (IS_RIPNG_DEBUG_ZEBRA) | |
468 | zlog_debug( | |
469 | "RIPng connected address %s/%d delete", | |
470 | inet_ntop(AF_INET6, &p->u.prefix6, buf, | |
471 | INET6_ADDRSTRLEN), | |
472 | p->prefixlen); | |
473 | ||
474 | /* Check wether this prefix needs to be removed. */ | |
475 | ripng_apply_address_del(ifc); | |
476 | } | |
477 | connected_free(ifc); | |
718e3744 | 478 | } |
718e3744 | 479 | |
d62a17ae | 480 | return 0; |
718e3744 | 481 | } |
6b0655a2 | 482 | |
718e3744 | 483 | /* Lookup RIPng enable network. */ |
a94434b6 | 484 | /* Check wether the interface has at least a connected prefix that |
29b94d58 | 485 | * is within the ripng->enable_network table. */ |
d62a17ae | 486 | static int ripng_enable_network_lookup_if(struct interface *ifp) |
718e3744 | 487 | { |
5c84b9a5 RW |
488 | struct ripng_interface *ri = ifp->info; |
489 | struct ripng *ripng = ri->ripng; | |
d62a17ae | 490 | struct listnode *node; |
491 | struct connected *connected; | |
492 | struct prefix_ipv6 address; | |
493 | ||
29b94d58 RW |
494 | if (!ripng) |
495 | return -1; | |
496 | ||
d62a17ae | 497 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) { |
498 | struct prefix *p; | |
fe08ba7e | 499 | struct agg_node *n; |
d62a17ae | 500 | |
501 | p = connected->address; | |
502 | ||
503 | if (p->family == AF_INET6) { | |
504 | address.family = AF_INET6; | |
505 | address.prefix = p->u.prefix6; | |
506 | address.prefixlen = IPV6_MAX_BITLEN; | |
507 | ||
29b94d58 | 508 | n = agg_node_match(ripng->enable_network, |
fe08ba7e | 509 | (struct prefix *)&address); |
ee6f7757 | 510 | if (n) { |
fe08ba7e | 511 | agg_unlock_node(n); |
d62a17ae | 512 | return 1; |
513 | } | |
514 | } | |
515 | } | |
516 | return -1; | |
718e3744 | 517 | } |
518 | ||
29b94d58 | 519 | /* Check wether connected is within the ripng->enable_network table. */ |
d62a17ae | 520 | static int ripng_enable_network_lookup2(struct connected *connected) |
a94434b6 | 521 | { |
5c84b9a5 RW |
522 | struct ripng_interface *ri = connected->ifp->info; |
523 | struct ripng *ripng = ri->ripng; | |
d62a17ae | 524 | struct prefix_ipv6 address; |
525 | struct prefix *p; | |
a94434b6 | 526 | |
29b94d58 RW |
527 | if (!ripng) |
528 | return -1; | |
529 | ||
d62a17ae | 530 | p = connected->address; |
a94434b6 | 531 | |
d62a17ae | 532 | if (p->family == AF_INET6) { |
fe08ba7e | 533 | struct agg_node *node; |
a94434b6 | 534 | |
d62a17ae | 535 | address.family = p->family; |
536 | address.prefix = p->u.prefix6; | |
537 | address.prefixlen = IPV6_MAX_BITLEN; | |
a94434b6 | 538 | |
d62a17ae | 539 | /* LPM on p->family, p->u.prefix6/IPV6_MAX_BITLEN within |
29b94d58 RW |
540 | * ripng->enable_network */ |
541 | node = agg_node_match(ripng->enable_network, | |
fe08ba7e | 542 | (struct prefix *)&address); |
a94434b6 | 543 | |
d62a17ae | 544 | if (node) { |
fe08ba7e | 545 | agg_unlock_node(node); |
d62a17ae | 546 | return 1; |
547 | } | |
548 | } | |
a94434b6 | 549 | |
d62a17ae | 550 | return -1; |
a94434b6 | 551 | } |
552 | ||
718e3744 | 553 | /* Add RIPng enable network. */ |
5c84b9a5 | 554 | int ripng_enable_network_add(struct ripng *ripng, struct prefix *p) |
718e3744 | 555 | { |
fe08ba7e | 556 | struct agg_node *node; |
718e3744 | 557 | |
29b94d58 | 558 | node = agg_node_get(ripng->enable_network, p); |
718e3744 | 559 | |
d62a17ae | 560 | if (node->info) { |
fe08ba7e | 561 | agg_unlock_node(node); |
cc48702b | 562 | return NB_ERR_INCONSISTENCY; |
d62a17ae | 563 | } else |
564 | node->info = (void *)1; | |
718e3744 | 565 | |
d62a17ae | 566 | /* XXX: One should find a better solution than a generic one */ |
5c84b9a5 | 567 | ripng_enable_apply_all(ripng); |
a94434b6 | 568 | |
cc48702b | 569 | return NB_OK; |
718e3744 | 570 | } |
571 | ||
572 | /* Delete RIPng enable network. */ | |
5c84b9a5 | 573 | int ripng_enable_network_delete(struct ripng *ripng, struct prefix *p) |
718e3744 | 574 | { |
fe08ba7e | 575 | struct agg_node *node; |
718e3744 | 576 | |
29b94d58 | 577 | node = agg_node_lookup(ripng->enable_network, p); |
d62a17ae | 578 | if (node) { |
579 | node->info = NULL; | |
718e3744 | 580 | |
d62a17ae | 581 | /* Unlock info lock. */ |
fe08ba7e | 582 | agg_unlock_node(node); |
718e3744 | 583 | |
d62a17ae | 584 | /* Unlock lookup lock. */ |
fe08ba7e | 585 | agg_unlock_node(node); |
718e3744 | 586 | |
cc48702b | 587 | return NB_OK; |
d62a17ae | 588 | } |
cc48702b RW |
589 | |
590 | return NB_ERR_INCONSISTENCY; | |
718e3744 | 591 | } |
592 | ||
593 | /* Lookup function. */ | |
5c84b9a5 | 594 | static int ripng_enable_if_lookup(struct ripng *ripng, const char *ifname) |
718e3744 | 595 | { |
d62a17ae | 596 | unsigned int i; |
597 | char *str; | |
598 | ||
b0ba762f RW |
599 | if (!ripng) |
600 | return -1; | |
601 | ||
602 | for (i = 0; i < vector_active(ripng->enable_if); i++) | |
603 | if ((str = vector_slot(ripng->enable_if, i)) != NULL) | |
d62a17ae | 604 | if (strcmp(str, ifname) == 0) |
605 | return i; | |
606 | return -1; | |
718e3744 | 607 | } |
608 | ||
5c84b9a5 | 609 | int ripng_enable_if_add(struct ripng *ripng, const char *ifname) |
718e3744 | 610 | { |
d62a17ae | 611 | int ret; |
718e3744 | 612 | |
5c84b9a5 | 613 | ret = ripng_enable_if_lookup(ripng, ifname); |
d62a17ae | 614 | if (ret >= 0) |
cc48702b | 615 | return NB_ERR_INCONSISTENCY; |
718e3744 | 616 | |
b0ba762f | 617 | vector_set(ripng->enable_if, strdup(ifname)); |
718e3744 | 618 | |
5c84b9a5 | 619 | ripng_enable_apply_all(ripng); |
a94434b6 | 620 | |
cc48702b | 621 | return NB_OK; |
718e3744 | 622 | } |
623 | ||
5c84b9a5 | 624 | int ripng_enable_if_delete(struct ripng *ripng, const char *ifname) |
718e3744 | 625 | { |
d62a17ae | 626 | int index; |
627 | char *str; | |
718e3744 | 628 | |
5c84b9a5 | 629 | index = ripng_enable_if_lookup(ripng, ifname); |
d62a17ae | 630 | if (index < 0) |
cc48702b | 631 | return NB_ERR_INCONSISTENCY; |
718e3744 | 632 | |
b0ba762f | 633 | str = vector_slot(ripng->enable_if, index); |
d62a17ae | 634 | free(str); |
b0ba762f | 635 | vector_unset(ripng->enable_if, index); |
718e3744 | 636 | |
5c84b9a5 | 637 | ripng_enable_apply_all(ripng); |
a94434b6 | 638 | |
cc48702b | 639 | return NB_OK; |
718e3744 | 640 | } |
641 | ||
642 | /* Wake up interface. */ | |
d62a17ae | 643 | static int ripng_interface_wakeup(struct thread *t) |
718e3744 | 644 | { |
d62a17ae | 645 | struct interface *ifp; |
646 | struct ripng_interface *ri; | |
718e3744 | 647 | |
d62a17ae | 648 | /* Get interface. */ |
649 | ifp = THREAD_ARG(t); | |
718e3744 | 650 | |
d62a17ae | 651 | ri = ifp->info; |
652 | ri->t_wakeup = NULL; | |
718e3744 | 653 | |
d62a17ae | 654 | /* Join to multicast group. */ |
5c84b9a5 | 655 | if (ripng_multicast_join(ifp, ri->ripng->sock) < 0) { |
450971aa | 656 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
657 | "multicast join failed, interface %s not running", |
658 | ifp->name); | |
d62a17ae | 659 | return 0; |
660 | } | |
661 | ||
662 | /* Set running flag. */ | |
663 | ri->running = 1; | |
718e3744 | 664 | |
d62a17ae | 665 | /* Send RIP request to the interface. */ |
666 | ripng_request(ifp); | |
718e3744 | 667 | |
d62a17ae | 668 | return 0; |
718e3744 | 669 | } |
670 | ||
d62a17ae | 671 | static void ripng_connect_set(struct interface *ifp, int set) |
a94434b6 | 672 | { |
5c84b9a5 RW |
673 | struct ripng_interface *ri = ifp->info; |
674 | struct ripng *ripng = ri->ripng; | |
d62a17ae | 675 | struct listnode *node, *nnode; |
676 | struct connected *connected; | |
677 | struct prefix_ipv6 address; | |
678 | ||
679 | for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, connected)) { | |
680 | struct prefix *p; | |
681 | p = connected->address; | |
682 | ||
683 | if (p->family != AF_INET6) | |
684 | continue; | |
685 | ||
686 | address.family = AF_INET6; | |
687 | address.prefix = p->u.prefix6; | |
688 | address.prefixlen = p->prefixlen; | |
689 | apply_mask_ipv6(&address); | |
690 | ||
691 | if (set) { | |
692 | /* Check once more wether this prefix is within a | |
693 | * "network IF_OR_PREF" one */ | |
5c84b9a5 RW |
694 | if ((ripng_enable_if_lookup(ripng, connected->ifp->name) |
695 | >= 0) | |
d62a17ae | 696 | || (ripng_enable_network_lookup2(connected) >= 0)) |
697 | ripng_redistribute_add( | |
5c84b9a5 | 698 | ripng, ZEBRA_ROUTE_CONNECT, |
d62a17ae | 699 | RIPNG_ROUTE_INTERFACE, &address, |
700 | connected->ifp->ifindex, NULL, 0); | |
701 | } else { | |
5c84b9a5 RW |
702 | ripng_redistribute_delete(ripng, ZEBRA_ROUTE_CONNECT, |
703 | RIPNG_ROUTE_INTERFACE, | |
704 | &address, | |
705 | connected->ifp->ifindex); | |
706 | if (ripng_redistribute_check(ripng, | |
707 | ZEBRA_ROUTE_CONNECT)) | |
d62a17ae | 708 | ripng_redistribute_add( |
5c84b9a5 | 709 | ripng, ZEBRA_ROUTE_CONNECT, |
d62a17ae | 710 | RIPNG_ROUTE_REDISTRIBUTE, &address, |
711 | connected->ifp->ifindex, NULL, 0); | |
712 | } | |
713 | } | |
a94434b6 | 714 | } |
715 | ||
718e3744 | 716 | /* Check RIPng is enabed on this interface. */ |
d62a17ae | 717 | void ripng_enable_apply(struct interface *ifp) |
718e3744 | 718 | { |
d62a17ae | 719 | int ret; |
720 | struct ripng_interface *ri = NULL; | |
721 | ||
722 | /* Check interface. */ | |
723 | if (!if_is_up(ifp)) | |
724 | return; | |
725 | ||
726 | ri = ifp->info; | |
727 | ||
728 | /* Is this interface a candidate for RIPng ? */ | |
729 | ret = ripng_enable_network_lookup_if(ifp); | |
730 | ||
731 | /* If the interface is matched. */ | |
732 | if (ret > 0) | |
733 | ri->enable_network = 1; | |
734 | else | |
735 | ri->enable_network = 0; | |
736 | ||
737 | /* Check interface name configuration. */ | |
5c84b9a5 | 738 | ret = ripng_enable_if_lookup(ri->ripng, ifp->name); |
d62a17ae | 739 | if (ret >= 0) |
740 | ri->enable_interface = 1; | |
741 | else | |
742 | ri->enable_interface = 0; | |
743 | ||
744 | /* any candidate interface MUST have a link-local IPv6 address */ | |
745 | if ((!ripng_if_ipv6_lladdress_check(ifp)) | |
746 | && (ri->enable_network || ri->enable_interface)) { | |
747 | ri->enable_network = 0; | |
748 | ri->enable_interface = 0; | |
749 | zlog_warn("Interface %s does not have any link-local address", | |
750 | ifp->name); | |
751 | } | |
752 | ||
753 | /* Update running status of the interface. */ | |
754 | if (ri->enable_network || ri->enable_interface) { | |
755 | zlog_info("RIPng INTERFACE ON %s", ifp->name); | |
756 | ||
757 | /* Add interface wake up thread. */ | |
758 | thread_add_timer(master, ripng_interface_wakeup, ifp, 1, | |
759 | &ri->t_wakeup); | |
760 | ||
761 | ripng_connect_set(ifp, 1); | |
762 | } else { | |
763 | if (ri->running) { | |
764 | /* Might as well clean up the route table as well | |
765 | * ripng_if_down sets to 0 ri->running, and displays | |
766 | *"turn off %s" | |
767 | **/ | |
768 | ripng_if_down(ifp); | |
769 | ||
770 | ripng_connect_set(ifp, 0); | |
771 | } | |
718e3744 | 772 | } |
718e3744 | 773 | } |
774 | ||
775 | /* Set distribute list to all interfaces. */ | |
5c84b9a5 | 776 | static void ripng_enable_apply_all(struct ripng *ripng) |
718e3744 | 777 | { |
d62a17ae | 778 | struct interface *ifp; |
718e3744 | 779 | |
dde7b15b | 780 | FOR_ALL_INTERFACES (ripng->vrf, ifp) |
d62a17ae | 781 | ripng_enable_apply(ifp); |
718e3744 | 782 | } |
6b0655a2 | 783 | |
a94434b6 | 784 | /* Clear all network and neighbor configuration */ |
5c84b9a5 | 785 | void ripng_clean_network(struct ripng *ripng) |
a94434b6 | 786 | { |
d62a17ae | 787 | unsigned int i; |
788 | char *str; | |
fe08ba7e | 789 | struct agg_node *rn; |
d62a17ae | 790 | |
29b94d58 RW |
791 | /* ripng->enable_network */ |
792 | for (rn = agg_route_top(ripng->enable_network); rn; | |
fe08ba7e | 793 | rn = agg_route_next(rn)) |
d62a17ae | 794 | if (rn->info) { |
795 | rn->info = NULL; | |
fe08ba7e | 796 | agg_unlock_node(rn); |
d62a17ae | 797 | } |
798 | ||
b0ba762f RW |
799 | /* ripng->enable_if */ |
800 | for (i = 0; i < vector_active(ripng->enable_if); i++) | |
801 | if ((str = vector_slot(ripng->enable_if, i)) != NULL) { | |
d62a17ae | 802 | free(str); |
b0ba762f | 803 | vector_slot(ripng->enable_if, i) = NULL; |
d62a17ae | 804 | } |
a94434b6 | 805 | } |
6b0655a2 | 806 | |
718e3744 | 807 | /* Utility function for looking up passive interface settings. */ |
5c84b9a5 RW |
808 | static int ripng_passive_interface_lookup(struct ripng *ripng, |
809 | const char *ifname) | |
718e3744 | 810 | { |
d62a17ae | 811 | unsigned int i; |
812 | char *str; | |
813 | ||
0c32404f RW |
814 | for (i = 0; i < vector_active(ripng->passive_interface); i++) |
815 | if ((str = vector_slot(ripng->passive_interface, i)) != NULL) | |
d62a17ae | 816 | if (strcmp(str, ifname) == 0) |
817 | return i; | |
818 | return -1; | |
718e3744 | 819 | } |
820 | ||
d62a17ae | 821 | void ripng_passive_interface_apply(struct interface *ifp) |
718e3744 | 822 | { |
d62a17ae | 823 | int ret; |
824 | struct ripng_interface *ri; | |
5c84b9a5 | 825 | struct ripng *ripng; |
718e3744 | 826 | |
d62a17ae | 827 | ri = ifp->info; |
5c84b9a5 RW |
828 | ripng = ri->ripng; |
829 | if (!ripng) | |
830 | return; | |
718e3744 | 831 | |
5c84b9a5 | 832 | ret = ripng_passive_interface_lookup(ripng, ifp->name); |
d62a17ae | 833 | if (ret < 0) |
834 | ri->passive = 0; | |
835 | else | |
836 | ri->passive = 1; | |
718e3744 | 837 | } |
838 | ||
5c84b9a5 | 839 | static void ripng_passive_interface_apply_all(struct ripng *ripng) |
718e3744 | 840 | { |
d62a17ae | 841 | struct interface *ifp; |
718e3744 | 842 | |
dde7b15b | 843 | FOR_ALL_INTERFACES (ripng->vrf, ifp) |
d62a17ae | 844 | ripng_passive_interface_apply(ifp); |
718e3744 | 845 | } |
846 | ||
847 | /* Passive interface. */ | |
5c84b9a5 | 848 | int ripng_passive_interface_set(struct ripng *ripng, const char *ifname) |
718e3744 | 849 | { |
5c84b9a5 | 850 | if (ripng_passive_interface_lookup(ripng, ifname) >= 0) |
22e8c7ae | 851 | return NB_ERR_INCONSISTENCY; |
718e3744 | 852 | |
0c32404f | 853 | vector_set(ripng->passive_interface, strdup(ifname)); |
718e3744 | 854 | |
5c84b9a5 | 855 | ripng_passive_interface_apply_all(ripng); |
718e3744 | 856 | |
22e8c7ae | 857 | return NB_OK; |
718e3744 | 858 | } |
859 | ||
5c84b9a5 | 860 | int ripng_passive_interface_unset(struct ripng *ripng, const char *ifname) |
718e3744 | 861 | { |
d62a17ae | 862 | int i; |
863 | char *str; | |
718e3744 | 864 | |
5c84b9a5 | 865 | i = ripng_passive_interface_lookup(ripng, ifname); |
d62a17ae | 866 | if (i < 0) |
22e8c7ae | 867 | return NB_ERR_INCONSISTENCY; |
718e3744 | 868 | |
0c32404f | 869 | str = vector_slot(ripng->passive_interface, i); |
d62a17ae | 870 | free(str); |
0c32404f | 871 | vector_unset(ripng->passive_interface, i); |
718e3744 | 872 | |
5c84b9a5 | 873 | ripng_passive_interface_apply_all(ripng); |
718e3744 | 874 | |
22e8c7ae | 875 | return NB_OK; |
718e3744 | 876 | } |
877 | ||
878 | /* Free all configured RIP passive-interface settings. */ | |
5c84b9a5 | 879 | void ripng_passive_interface_clean(struct ripng *ripng) |
718e3744 | 880 | { |
d62a17ae | 881 | unsigned int i; |
882 | char *str; | |
883 | ||
0c32404f RW |
884 | for (i = 0; i < vector_active(ripng->passive_interface); i++) |
885 | if ((str = vector_slot(ripng->passive_interface, i)) != NULL) { | |
d62a17ae | 886 | free(str); |
0c32404f | 887 | vector_slot(ripng->passive_interface, i) = NULL; |
d62a17ae | 888 | } |
5c84b9a5 | 889 | ripng_passive_interface_apply_all(ripng); |
718e3744 | 890 | } |
891 | ||
892 | /* Write RIPng enable network and interface to the vty. */ | |
5c84b9a5 | 893 | int ripng_network_write(struct vty *vty, struct ripng *ripng) |
718e3744 | 894 | { |
d62a17ae | 895 | unsigned int i; |
896 | const char *ifname; | |
fe08ba7e | 897 | struct agg_node *node; |
d62a17ae | 898 | char buf[BUFSIZ]; |
899 | ||
900 | /* Write enable network. */ | |
29b94d58 | 901 | for (node = agg_route_top(ripng->enable_network); node; |
fe08ba7e | 902 | node = agg_route_next(node)) |
d62a17ae | 903 | if (node->info) { |
904 | struct prefix *p = &node->p; | |
22e8c7ae | 905 | vty_out(vty, " %s/%d\n", |
d62a17ae | 906 | inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), |
907 | p->prefixlen); | |
908 | } | |
909 | ||
910 | /* Write enable interface. */ | |
b0ba762f RW |
911 | for (i = 0; i < vector_active(ripng->enable_if); i++) |
912 | if ((ifname = vector_slot(ripng->enable_if, i)) != NULL) | |
22e8c7ae | 913 | vty_out(vty, " %s\n", ifname); |
d62a17ae | 914 | |
915 | return 0; | |
718e3744 | 916 | } |
917 | ||
d62a17ae | 918 | static struct ripng_interface *ri_new(void) |
718e3744 | 919 | { |
d62a17ae | 920 | struct ripng_interface *ri; |
5c84b9a5 | 921 | |
eaf58ba9 | 922 | ri = XCALLOC(MTYPE_RIPNG_IF, sizeof(struct ripng_interface)); |
a94434b6 | 923 | |
d62a17ae | 924 | /* Set default split-horizon behavior. If the interface is Frame |
925 | Relay or SMDS is enabled, the default value for split-horizon is | |
926 | off. But currently Zebra does detect Frame Relay or SMDS | |
927 | interface. So all interface is set to split horizon. */ | |
d406db4c RW |
928 | ri->split_horizon = |
929 | yang_get_default_enum("%s/split-horizon", RIPNG_IFACE); | |
a94434b6 | 930 | |
d62a17ae | 931 | return ri; |
718e3744 | 932 | } |
933 | ||
dde7b15b RW |
934 | void ripng_interface_sync(struct interface *ifp) |
935 | { | |
936 | struct vrf *vrf; | |
937 | ||
a41c4e1b | 938 | vrf = ifp->vrf; |
dde7b15b RW |
939 | if (vrf) { |
940 | struct ripng_interface *ri; | |
941 | ||
942 | ri = ifp->info; | |
943 | if (ri) | |
944 | ri->ripng = vrf->info; | |
945 | } | |
946 | } | |
947 | ||
d62a17ae | 948 | static int ripng_if_new_hook(struct interface *ifp) |
718e3744 | 949 | { |
d62a17ae | 950 | ifp->info = ri_new(); |
dde7b15b RW |
951 | ripng_interface_sync(ifp); |
952 | ||
d62a17ae | 953 | return 0; |
718e3744 | 954 | } |
955 | ||
a94434b6 | 956 | /* Called when interface structure deleted. */ |
d62a17ae | 957 | static int ripng_if_delete_hook(struct interface *ifp) |
a94434b6 | 958 | { |
eaf58ba9 | 959 | XFREE(MTYPE_RIPNG_IF, ifp->info); |
d62a17ae | 960 | ifp->info = NULL; |
961 | return 0; | |
a94434b6 | 962 | } |
963 | ||
718e3744 | 964 | /* Configuration write function for ripngd. */ |
d62a17ae | 965 | static int interface_config_write(struct vty *vty) |
718e3744 | 966 | { |
dde7b15b | 967 | struct vrf *vrf; |
d62a17ae | 968 | int write = 0; |
969 | ||
dde7b15b RW |
970 | RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) { |
971 | struct interface *ifp; | |
d62a17ae | 972 | |
dde7b15b RW |
973 | FOR_ALL_INTERFACES (vrf, ifp) { |
974 | struct lyd_node *dnode; | |
d62a17ae | 975 | |
dde7b15b RW |
976 | dnode = yang_dnode_get( |
977 | running_config->dnode, | |
978 | "/frr-interface:lib/interface[name='%s'][vrf='%s']", | |
979 | ifp->name, vrf->name); | |
980 | if (dnode == NULL) | |
981 | continue; | |
d62a17ae | 982 | |
dde7b15b RW |
983 | write = 1; |
984 | nb_cli_show_dnode_cmds(vty, dnode, false); | |
985 | } | |
a94434b6 | 986 | } |
d406db4c | 987 | |
d62a17ae | 988 | return write; |
718e3744 | 989 | } |
990 | ||
991 | /* ripngd's interface node. */ | |
d62a17ae | 992 | static struct cmd_node interface_node = { |
993 | INTERFACE_NODE, "%s(config-if)# ", 1 /* VTYSH */ | |
718e3744 | 994 | }; |
995 | ||
996 | /* Initialization of interface. */ | |
4d762f26 | 997 | void ripng_if_init(void) |
718e3744 | 998 | { |
d62a17ae | 999 | /* Interface initialize. */ |
ce19a04a DL |
1000 | hook_register_prio(if_add, 0, ripng_if_new_hook); |
1001 | hook_register_prio(if_del, 0, ripng_if_delete_hook); | |
718e3744 | 1002 | |
d62a17ae | 1003 | /* Install interface node. */ |
1004 | install_node(&interface_node, interface_config_write); | |
1005 | if_cmd_init(); | |
718e3744 | 1006 | } |