]> git.proxmox.com Git - mirror_frr.git/blame - zebra/interface.c
zebra/vty: use prefix2str and unify show ip/ipv6 route code
[mirror_frr.git] / zebra / interface.c
CommitLineData
718e3744 1/*
2 * Interface function.
3 * Copyright (C) 1997, 1999 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 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "if.h"
26#include "vty.h"
27#include "sockunion.h"
28#include "prefix.h"
29#include "command.h"
30#include "memory.h"
31#include "ioctl.h"
32#include "connected.h"
33#include "log.h"
34#include "zclient.h"
cd2a8a42 35#include "vrf.h"
718e3744 36
718e3744 37#include "zebra/rtadv.h"
7c551956 38#include "zebra_ns.h"
c1c747a8 39#include "zebra_vrf.h"
7c551956 40#include "zebra/interface.h"
718e3744 41#include "zebra/rib.h"
42#include "zebra/zserv.h"
43#include "zebra/redistribute.h"
44#include "zebra/debug.h"
ca776988 45#include "zebra/irdp.h"
244c1cdc 46#include "zebra/zebra_ptm.h"
5c610faf 47#include "zebra/rt_netlink.h"
88177fe3 48#include "zebra/interface.h"
244c1cdc
DS
49
50#define ZEBRA_PTM_SUPPORT
718e3744 51
8da4e946 52#if defined (HAVE_RTADV)
b60668d0
CC
53/* Order is intentional. Matches RFC4191. This array is also used for
54 command matching, so only modify with care. */
55const char *rtadv_pref_strs[] = { "medium", "high", "INVALID", "low", 0 };
8da4e946 56#endif /* HAVE_RTADV */
718e3744 57
c8e264b6 58static void if_down_del_nbr_connected (struct interface *ifp);
59
718e3744 60/* Called when new interface is added. */
a1ac18c4 61static int
718e3744 62if_zebra_new_hook (struct interface *ifp)
63{
64 struct zebra_if *zebra_if;
65
393deb9b 66 zebra_if = XCALLOC (MTYPE_TMP, sizeof (struct zebra_if));
718e3744 67
68 zebra_if->multicast = IF_ZEBRA_MULTICAST_UNSPEC;
bfac8dcd 69 zebra_if->shutdown = IF_ZEBRA_SHUTDOWN_OFF;
986aa00f 70 zebra_ptm_if_init(zebra_if);
718e3744 71
c8ed14dd 72 ifp->ptm_enable = zebra_ptm_get_enable_state();
8da4e946 73#if defined (HAVE_RTADV)
718e3744 74 {
75 /* Set default router advertise values. */
76 struct rtadvconf *rtadv;
77
78 rtadv = &zebra_if->rtadv;
79
80 rtadv->AdvSendAdvertisements = 0;
81 rtadv->MaxRtrAdvInterval = RTADV_MAX_RTR_ADV_INTERVAL;
82 rtadv->MinRtrAdvInterval = RTADV_MIN_RTR_ADV_INTERVAL;
83 rtadv->AdvIntervalTimer = 0;
84 rtadv->AdvManagedFlag = 0;
85 rtadv->AdvOtherConfigFlag = 0;
7cee1bb1 86 rtadv->AdvHomeAgentFlag = 0;
718e3744 87 rtadv->AdvLinkMTU = 0;
88 rtadv->AdvReachableTime = 0;
89 rtadv->AdvRetransTimer = 0;
90 rtadv->AdvCurHopLimit = 0;
d660f698 91 rtadv->AdvDefaultLifetime = -1; /* derive from MaxRtrAdvInterval */
7cee1bb1 92 rtadv->HomeAgentPreference = 0;
d660f698 93 rtadv->HomeAgentLifetime = -1; /* derive from AdvDefaultLifetime */
7cee1bb1 94 rtadv->AdvIntervalOption = 0;
b60668d0 95 rtadv->DefaultPreference = RTADV_PREF_MEDIUM;
718e3744 96
97 rtadv->AdvPrefixList = list_new ();
98 }
8da4e946 99#endif /* HAVE_RTADV */
718e3744 100
eef1fe11 101 /* Initialize installed address chains tree. */
102 zebra_if->ipv4_subnets = route_table_init ();
103
718e3744 104 ifp->info = zebra_if;
a3d21ef3
DS
105
106 zebra_vrf_static_route_interface_fixup (ifp);
718e3744 107 return 0;
108}
109
110/* Called when interface is deleted. */
a1ac18c4 111static int
718e3744 112if_zebra_delete_hook (struct interface *ifp)
113{
eef1fe11 114 struct zebra_if *zebra_if;
115
718e3744 116 if (ifp->info)
eef1fe11 117 {
118 zebra_if = ifp->info;
119
120 /* Free installed address chains tree. */
121 if (zebra_if->ipv4_subnets)
122 route_table_finish (zebra_if->ipv4_subnets);
123
124 XFREE (MTYPE_TMP, zebra_if);
125 }
126
127 return 0;
128}
129
12f6fb97
DS
130/* Build the table key */
131static void
132if_build_key (u_int32_t ifindex, struct prefix *p)
133{
134 p->family = AF_INET;
135 p->prefixlen = IPV4_MAX_BITLEN;
136 p->u.prefix4.s_addr = ifindex;
137}
138
139/* Link an interface in a per NS interface tree */
140struct interface *
141if_link_per_ns (struct zebra_ns *ns, struct interface *ifp)
142{
143 struct prefix p;
144 struct route_node *rn;
145
146 if (ifp->ifindex == IFINDEX_INTERNAL)
147 return NULL;
148
149 if_build_key (ifp->ifindex, &p);
150 rn = route_node_get (ns->if_table, &p);
151 if (rn->info)
152 {
153 ifp = (struct interface *)rn->info;
154 route_unlock_node (rn); /* get */
155 return ifp;
156 }
157
158 rn->info = ifp;
159 ifp->node = rn;
160
161 return ifp;
162}
163
164/* Delete a VRF. This is called in vrf_terminate(). */
165void
166if_unlink_per_ns (struct interface *ifp)
167{
168 ifp->node->info = NULL;
169 route_unlock_node(ifp->node);
170}
171
172/* Look up an interface by identifier within a NS */
173struct interface *
174if_lookup_by_index_per_ns (struct zebra_ns *ns, u_int32_t ifindex)
175{
176 struct prefix p;
177 struct route_node *rn;
178 struct interface *ifp = NULL;
179
180 if_build_key (ifindex, &p);
181 rn = route_node_lookup (ns->if_table, &p);
182 if (rn)
183 {
184 ifp = (struct interface *)rn->info;
185 route_unlock_node (rn); /* lookup */
186 }
187 return ifp;
188}
189
a815b788 190const char *
191ifindex2ifname_per_ns (struct zebra_ns *zns, unsigned int ifindex)
192{
193 struct interface *ifp;
194
195 return ((ifp = if_lookup_by_index_per_ns (zns, ifindex)) != NULL) ?
196 ifp->name : "unknown";
197}
12f6fb97 198
eef1fe11 199/* Tie an interface address to its derived subnet list of addresses. */
200int
201if_subnet_add (struct interface *ifp, struct connected *ifc)
202{
203 struct route_node *rn;
204 struct zebra_if *zebra_if;
205 struct prefix cp;
206 struct list *addr_list;
207
208 assert (ifp && ifp->info && ifc);
209 zebra_if = ifp->info;
210
211 /* Get address derived subnet node and associated address list, while marking
212 address secondary attribute appropriately. */
213 cp = *ifc->address;
214 apply_mask (&cp);
215 rn = route_node_get (zebra_if->ipv4_subnets, &cp);
216
217 if ((addr_list = rn->info))
218 SET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
219 else
220 {
221 UNSET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
222 rn->info = addr_list = list_new ();
223 route_lock_node (rn);
224 }
225
226 /* Tie address at the tail of address list. */
227 listnode_add (addr_list, ifc);
228
229 /* Return list element count. */
230 return (addr_list->count);
231}
232
233/* Untie an interface address from its derived subnet list of addresses. */
234int
235if_subnet_delete (struct interface *ifp, struct connected *ifc)
236{
237 struct route_node *rn;
238 struct zebra_if *zebra_if;
239 struct list *addr_list;
240
241 assert (ifp && ifp->info && ifc);
242 zebra_if = ifp->info;
243
244 /* Get address derived subnet node. */
245 rn = route_node_lookup (zebra_if->ipv4_subnets, ifc->address);
246 if (! (rn && rn->info))
9db047fc
CF
247 {
248 zlog_warn("Trying to remove an address from an unknown subnet."
249 " (please report this bug)");
250 return -1;
251 }
eef1fe11 252 route_unlock_node (rn);
253
254 /* Untie address from subnet's address list. */
255 addr_list = rn->info;
9db047fc
CF
256
257 /* Deleting an address that is not registered is a bug.
258 * In any case, we shouldn't decrement the lock counter if the address
259 * is unknown. */
260 if (!listnode_lookup(addr_list, ifc))
261 {
262 zlog_warn("Trying to remove an address from a subnet where it is not"
263 " currently registered. (please report this bug)");
264 return -1;
265 }
266
eef1fe11 267 listnode_delete (addr_list, ifc);
268 route_unlock_node (rn);
269
270 /* Return list element count, if not empty. */
271 if (addr_list->count)
272 {
273 /* If deleted address is primary, mark subsequent one as such and distribute. */
274 if (! CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY))
275 {
1eb8ef25 276 ifc = listgetdata (listhead (addr_list));
eef1fe11 277 zebra_interface_address_delete_update (ifp, ifc);
278 UNSET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
02b4805f
CF
279 /* XXX: Linux kernel removes all the secondary addresses when the primary
280 * address is removed. We could try to work around that, though this is
281 * non-trivial. */
eef1fe11 282 zebra_interface_address_add_update (ifp, ifc);
283 }
284
285 return addr_list->count;
286 }
287
288 /* Otherwise, free list and route node. */
289 list_free (addr_list);
290 rn->info = NULL;
291 route_unlock_node (rn);
292
718e3744 293 return 0;
294}
295
5c78b3d0 296/* if_flags_mangle: A place for hacks that require mangling
297 * or tweaking the interface flags.
298 *
299 * ******************** Solaris flags hacks **************************
300 *
301 * Solaris IFF_UP flag reflects only the primary interface as the
302 * routing socket only sends IFINFO for the primary interface. Hence
303 * ~IFF_UP does not per se imply all the logical interfaces are also
304 * down - which we only know of as addresses. Instead we must determine
305 * whether the interface really is up or not according to how many
306 * addresses are still attached. (Solaris always sends RTM_DELADDR if
307 * an interface, logical or not, goes ~IFF_UP).
308 *
309 * Ie, we mangle IFF_UP to *additionally* reflect whether or not there
310 * are addresses left in struct connected, not just the actual underlying
311 * IFF_UP flag.
312 *
313 * We must hence remember the real state of IFF_UP, which we do in
314 * struct zebra_if.primary_state.
315 *
316 * Setting IFF_UP within zebra to administratively shutdown the
317 * interface will affect only the primary interface/address on Solaris.
318 ************************End Solaris flags hacks ***********************
319 */
f63f06da 320static void
5c78b3d0 321if_flags_mangle (struct interface *ifp, uint64_t *newflags)
322{
323#ifdef SUNOS_5
324 struct zebra_if *zif = ifp->info;
325
326 zif->primary_state = *newflags & (IFF_UP & 0xff);
327
328 if (CHECK_FLAG (zif->primary_state, IFF_UP)
329 || listcount(ifp->connected) > 0)
330 SET_FLAG (*newflags, IFF_UP);
331 else
332 UNSET_FLAG (*newflags, IFF_UP);
333#endif /* SUNOS_5 */
334}
335
336/* Update the flags field of the ifp with the new flag set provided.
337 * Take whatever actions are required for any changes in flags we care
338 * about.
339 *
340 * newflags should be the raw value, as obtained from the OS.
341 */
342void
343if_flags_update (struct interface *ifp, uint64_t newflags)
344{
345 if_flags_mangle (ifp, &newflags);
346
244c1cdc 347 if (if_is_no_ptm_operative (ifp))
5c78b3d0 348 {
349 /* operative -> inoperative? */
350 ifp->flags = newflags;
351 if (!if_is_operative (ifp))
352 if_down (ifp);
353 }
354 else
355 {
356 /* inoperative -> operative? */
357 ifp->flags = newflags;
358 if (if_is_operative (ifp))
359 if_up (ifp);
360 }
361}
362
718e3744 363/* Wake up configured address if it is not in current kernel
364 address. */
a1ac18c4 365static void
718e3744 366if_addr_wakeup (struct interface *ifp)
367{
1eb8ef25 368 struct listnode *node, *nnode;
718e3744 369 struct connected *ifc;
370 struct prefix *p;
371 int ret;
372
1eb8ef25 373 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, ifc))
718e3744 374 {
718e3744 375 p = ifc->address;
376
377 if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED)
f7f740fe 378 && ! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED))
718e3744 379 {
380 /* Address check. */
381 if (p->family == AF_INET)
382 {
383 if (! if_is_up (ifp))
384 {
02b4805f
CF
385 /* Assume zebra is configured like following:
386 *
387 * interface gre0
388 * ip addr 192.0.2.1/24
389 * !
390 *
391 * As soon as zebra becomes first aware that gre0 exists in the
392 * kernel, it will set gre0 up and configure its addresses.
393 *
394 * (This may happen at startup when the interface already exists
395 * or during runtime when the interface is added to the kernel)
396 *
397 * XXX: IRDP code is calling here via if_add_update - this seems
398 * somewhat weird.
399 * XXX: RUNNING is not a settable flag on any system
400 * I (paulj) am aware of.
401 */
718e3744 402 if_set_flags (ifp, IFF_UP | IFF_RUNNING);
403 if_refresh (ifp);
404 }
405
406 ret = if_set_prefix (ifp, ifc);
407 if (ret < 0)
408 {
409 zlog_warn ("Can't set interface's address: %s",
6099b3b5 410 safe_strerror(errno));
718e3744 411 continue;
412 }
eef1fe11 413
f7f740fe 414 SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
02b4805f
CF
415 /* The address will be advertised to zebra clients when the notification
416 * from the kernel has been received.
417 * It will also be added to the interface's subnet list then. */
718e3744 418 }
419#ifdef HAVE_IPV6
420 if (p->family == AF_INET6)
421 {
422 if (! if_is_up (ifp))
423 {
02b4805f 424 /* See long comment above */
718e3744 425 if_set_flags (ifp, IFF_UP | IFF_RUNNING);
426 if_refresh (ifp);
427 }
428
429 ret = if_prefix_add_ipv6 (ifp, ifc);
430 if (ret < 0)
431 {
432 zlog_warn ("Can't set interface's address: %s",
6099b3b5 433 safe_strerror(errno));
718e3744 434 continue;
435 }
718e3744 436
02b4805f
CF
437 SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
438 /* The address will be advertised to zebra clients when the notification
439 * from the kernel has been received. */
718e3744 440 }
441#endif /* HAVE_IPV6 */
442 }
443 }
444}
445
446/* Handle interface addition */
447void
448if_add_update (struct interface *ifp)
449{
48b33aaf 450 struct zebra_if *if_data;
451
fe18ee2d 452 if_link_per_ns(zebra_ns_lookup (NS_DEFAULT), ifp);
12f6fb97 453
48b33aaf 454 if_data = ifp->info;
06b420a4
MS
455 assert(if_data);
456
48b33aaf 457 if (if_data->multicast == IF_ZEBRA_MULTICAST_ON)
458 if_set_flags (ifp, IFF_MULTICAST);
459 else if (if_data->multicast == IF_ZEBRA_MULTICAST_OFF)
460 if_unset_flags (ifp, IFF_MULTICAST);
461
986aa00f 462 zebra_ptm_if_set_ptm_state(ifp, if_data);
463
718e3744 464 zebra_interface_add_update (ifp);
465
466 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
467 {
468 SET_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE);
469
bfac8dcd
CF
470 if (if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)
471 {
472 if (IS_ZEBRA_DEBUG_KERNEL)
3968dbf8
FL
473 zlog_debug ("interface %s vrf %u index %d is shutdown. "
474 "Won't wake it up.",
475 ifp->name, ifp->vrf_id, ifp->ifindex);
bfac8dcd
CF
476 return;
477 }
478
718e3744 479 if_addr_wakeup (ifp);
480
481 if (IS_ZEBRA_DEBUG_KERNEL)
3968dbf8
FL
482 zlog_debug ("interface %s vrf %u index %d becomes active.",
483 ifp->name, ifp->vrf_id, ifp->ifindex);
718e3744 484 }
485 else
486 {
487 if (IS_ZEBRA_DEBUG_KERNEL)
3968dbf8
FL
488 zlog_debug ("interface %s vrf %u index %d is added.",
489 ifp->name, ifp->vrf_id, ifp->ifindex);
718e3744 490 }
491}
492
c8e264b6 493/* Install connected routes corresponding to an interface. */
494static void
495if_install_connected (struct interface *ifp)
718e3744 496{
c8e264b6 497 struct listnode *node;
498 struct listnode *next;
718e3744 499 struct connected *ifc;
500 struct prefix *p;
eef1fe11 501
c8e264b6 502 if (ifp->connected)
503 {
504 for (ALL_LIST_ELEMENTS (ifp->connected, node, next, ifc))
505 {
506 p = ifc->address;
99ffac18 507 zebra_interface_address_add_update (ifp, ifc);
718e3744 508
c8e264b6 509 if (p->family == AF_INET)
510 connected_up_ipv4 (ifp, ifc);
511 else if (p->family == AF_INET6)
512 connected_up_ipv6 (ifp, ifc);
513 }
514 }
515}
516
517/* Uninstall connected routes corresponding to an interface. */
518static void
519if_uninstall_connected (struct interface *ifp)
520{
521 struct listnode *node;
522 struct listnode *next;
523 struct connected *ifc;
524 struct prefix *p;
525
526 if (ifp->connected)
718e3744 527 {
c8e264b6 528 for (ALL_LIST_ELEMENTS (ifp->connected, node, next, ifc))
529 {
530 p = ifc->address;
99ffac18 531 zebra_interface_address_delete_update (ifp, ifc);
c8e264b6 532
533 if (p->family == AF_INET)
534 connected_down_ipv4 (ifp, ifc);
535 else if (p->family == AF_INET6)
536 connected_down_ipv6 (ifp, ifc);
537 }
718e3744 538 }
c8e264b6 539}
718e3744 540
c8e264b6 541/* Uninstall and delete connected routes corresponding to an interface. */
542/* TODO - Check why IPv4 handling here is different from install or if_down */
543static void
544if_delete_connected (struct interface *ifp)
545{
546 struct connected *ifc;
547 struct prefix *p;
548 struct route_node *rn;
549 struct zebra_if *zebra_if;
550
551 zebra_if = ifp->info;
718e3744 552
718e3744 553 if (ifp->connected)
554 {
d9a18f11
PJ
555 struct listnode *node;
556 struct listnode *last = NULL;
557
eef1fe11 558 while ((node = (last ? last->next : listhead (ifp->connected))))
718e3744 559 {
1eb8ef25 560 ifc = listgetdata (node);
718e3744 561 p = ifc->address;
eef1fe11 562
beb56336
PJ
563 if (p->family == AF_INET
564 && (rn = route_node_lookup (zebra_if->ipv4_subnets, p)))
eef1fe11 565 {
d9a18f11
PJ
566 struct listnode *anode;
567 struct listnode *next;
568 struct listnode *first;
569 struct list *addr_list;
570
eef1fe11 571 route_unlock_node (rn);
572 addr_list = (struct list *) rn->info;
573
574 /* Remove addresses, secondaries first. */
575 first = listhead (addr_list);
d9a18f11 576 for (anode = first->next; anode || first; anode = next)
eef1fe11 577 {
d9a18f11 578 if (!anode)
eef1fe11 579 {
d9a18f11 580 anode = first;
eef1fe11 581 first = NULL;
582 }
d9a18f11 583 next = anode->next;
eef1fe11 584
d9a18f11 585 ifc = listgetdata (anode);
eef1fe11 586 connected_down_ipv4 (ifp, ifc);
587
02b4805f
CF
588 /* XXX: We have to send notifications here explicitly, because we destroy
589 * the ifc before receiving the notification about the address being deleted.
590 */
eef1fe11 591 zebra_interface_address_delete_update (ifp, ifc);
592
593 UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
f7f740fe 594 UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
eef1fe11 595
596 /* Remove from subnet chain. */
d9a18f11 597 list_delete_node (addr_list, anode);
eef1fe11 598 route_unlock_node (rn);
599
600 /* Remove from interface address list (unconditionally). */
d9a18f11
PJ
601 if (!CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
602 {
603 listnode_delete (ifp->connected, ifc);
604 connected_free (ifc);
605 }
606 else
607 last = node;
eef1fe11 608 }
609
610 /* Free chain list and respective route node. */
611 list_delete (addr_list);
612 rn->info = NULL;
613 route_unlock_node (rn);
614 }
718e3744 615 else if (p->family == AF_INET6)
eef1fe11 616 {
617 connected_down_ipv6 (ifp, ifc);
718e3744 618
eef1fe11 619 zebra_interface_address_delete_update (ifp, ifc);
718e3744 620
eef1fe11 621 UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
f7f740fe 622 UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
eef1fe11 623
624 if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
625 last = node;
626 else
627 {
628 listnode_delete (ifp->connected, ifc);
629 connected_free (ifc);
630 }
718e3744 631 }
e26873fd
RHA
632 else
633 {
634 last = node;
635 }
718e3744 636 }
637 }
c8e264b6 638}
639
640/* Handle an interface delete event */
641void
642if_delete_update (struct interface *ifp)
643{
644 if (if_is_up(ifp))
645 {
646 zlog_err ("interface %s vrf %u index %d is still up while being deleted.",
647 ifp->name, ifp->vrf_id, ifp->ifindex);
648 return;
649 }
650
651 /* Mark interface as inactive */
652 UNSET_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE);
653
654 if (IS_ZEBRA_DEBUG_KERNEL)
655 zlog_debug ("interface %s vrf %u index %d is now inactive.",
656 ifp->name, ifp->vrf_id, ifp->ifindex);
657
658 /* Delete connected routes from the kernel. */
659 if_delete_connected (ifp);
660
661 /* Send out notification on interface delete. */
718e3744 662 zebra_interface_delete_update (ifp);
d2fc8896 663
12f6fb97
DS
664 if_unlink_per_ns(ifp);
665
d2fc8896 666 /* Update ifindex after distributing the delete message. This is in
667 case any client needs to have the old value of ifindex available
668 while processing the deletion. Each client daemon is responsible
669 for setting ifindex to IFINDEX_INTERNAL after processing the
670 interface deletion message. */
671 ifp->ifindex = IFINDEX_INTERNAL;
718e3744 672}
673
c8e264b6 674/* VRF change for an interface */
675void
676if_handle_vrf_change (struct interface *ifp, vrf_id_t vrf_id)
677{
678 vrf_id_t old_vrf_id;
679
680 old_vrf_id = ifp->vrf_id;
681
682 /* Uninstall connected routes. */
683 if_uninstall_connected (ifp);
684
685 /* Delete any IPv4 neighbors created to implement RFC 5549 */
686 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all (ifp);
687
688 /* Delete all neighbor addresses learnt through IPv6 RA */
689 if_down_del_nbr_connected (ifp);
690
c8e264b6 691 /* Send out notification on interface VRF change. */
692 /* This is to issue an UPDATE or a DELETE, as appropriate. */
693 zebra_interface_vrf_update_del (ifp, vrf_id);
694
695 /* update VRF */
696 if_update_vrf (ifp, ifp->name, strlen (ifp->name), vrf_id);
697
698 /* Send out notification on interface VRF change. */
699 /* This is to issue an ADD, if needed. */
700 zebra_interface_vrf_update_add (ifp, old_vrf_id);
701
702 /* Install connected routes (in new VRF). */
703 if_install_connected (ifp);
704
c8e264b6 705 /* Due to connected route change, schedule RIB processing for both old
706 * and new VRF.
707 */
708 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
709 zlog_debug ("%u: IF %s VRF change, scheduling RIB processing",
710 ifp->vrf_id, ifp->name);
711 rib_update (old_vrf_id, RIB_UPDATE_IF_CHANGE);
712 rib_update (ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
fb148af4
DS
713
714 zebra_vrf_static_route_interface_fixup (ifp);
c8e264b6 715}
716
88177fe3 717static void
5c610faf
DS
718ipv6_ll_address_to_mac (struct in6_addr *address, u_char *mac)
719{
eb4b1830 720 mac[0] = address->s6_addr[8] ^ 0x02;
5c610faf
DS
721 mac[1] = address->s6_addr[9];
722 mac[2] = address->s6_addr[10];
723 mac[3] = address->s6_addr[13];
724 mac[4] = address->s6_addr[14];
725 mac[5] = address->s6_addr[15];
726}
727
728void
729if_nbr_ipv6ll_to_ipv4ll_neigh_update (struct interface *ifp,
730 struct in6_addr *address,
731 int add)
732{
733 char buf[16] = "169.254.0.1";
734 struct in_addr ipv4_ll;
88177fe3 735 char mac[6];
5c610faf
DS
736
737 inet_pton (AF_INET, buf, &ipv4_ll);
738
88177fe3 739 ipv6_ll_address_to_mac(address, (u_char *)mac);
5c610faf
DS
740 netlink_neigh_update (add ? RTM_NEWNEIGH : RTM_DELNEIGH,
741 ifp->ifindex, ipv4_ll.s_addr, mac, 6);
742}
743
88177fe3 744static void
5c610faf
DS
745if_nbr_ipv6ll_to_ipv4ll_neigh_add_all (struct interface *ifp)
746{
747 if (listhead(ifp->nbr_connected))
748 {
749 struct nbr_connected *nbr_connected;
750 struct listnode *node;
751
752 for (ALL_LIST_ELEMENTS_RO (ifp->nbr_connected, node, nbr_connected))
753 if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp,
754 &nbr_connected->address->u.prefix6,
755 1);
756 }
757}
758
759void
760if_nbr_ipv6ll_to_ipv4ll_neigh_del_all (struct interface *ifp)
761{
762 if (listhead(ifp->nbr_connected))
763 {
764 struct nbr_connected *nbr_connected;
765 struct listnode *node;
766
767 for (ALL_LIST_ELEMENTS_RO (ifp->nbr_connected, node, nbr_connected))
768 if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp,
769 &nbr_connected->address->u.prefix6,
770 0);
771 }
772}
773
88177fe3 774static void
a197c47c
DS
775if_down_del_nbr_connected (struct interface *ifp)
776{
777 struct nbr_connected *nbr_connected;
778 struct listnode *node, *nnode;
779
780 for (ALL_LIST_ELEMENTS (ifp->nbr_connected, node, nnode, nbr_connected))
781 {
782 listnode_delete (ifp->nbr_connected, nbr_connected);
783 nbr_connected_free (nbr_connected);
784 }
785}
786
718e3744 787/* Interface is up. */
788void
789if_up (struct interface *ifp)
790{
55ce4d3a
CF
791 struct zebra_if *zif;
792
793 zif = ifp->info;
794 zif->up_count++;
795 quagga_timestamp (2, zif->up_last, sizeof (zif->up_last));
796
718e3744 797 /* Notify the protocol daemons. */
950bd436 798 if (ifp->ptm_enable && (ifp->ptm_status == ZEBRA_PTM_STATUS_DOWN)) {
244c1cdc
DS
799 zlog_warn("%s: interface %s hasn't passed ptm check\n", __func__,
800 ifp->name);
801 return;
802 }
718e3744 803 zebra_interface_up_update (ifp);
804
5c610faf 805 if_nbr_ipv6ll_to_ipv4ll_neigh_add_all (ifp);
244c1cdc 806
6c9678b4
DD
807 /* Enable fast tx of RA if enabled && RA interval is not in msecs */
808 if (zif->rtadv.AdvSendAdvertisements &&
809 (zif->rtadv.MaxRtrAdvInterval >= 1000))
810 {
811 zif->rtadv.inFastRexmit = 1;
812 zif->rtadv.NumFastReXmitsRemain = RTADV_NUM_FAST_REXMITS;
813 }
814
718e3744 815 /* Install connected routes to the kernel. */
c8e264b6 816 if_install_connected (ifp);
718e3744 817
41ec9222 818 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1c848137 819 zlog_debug ("%u: IF %s up, scheduling RIB processing",
820 ifp->vrf_id, ifp->name);
821 rib_update (ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
a3d21ef3
DS
822
823 zebra_vrf_static_route_interface_fixup (ifp);
718e3744 824}
825
826/* Interface goes down. We have to manage different behavior of based
827 OS. */
828void
829if_down (struct interface *ifp)
830{
55ce4d3a
CF
831 struct zebra_if *zif;
832
833 zif = ifp->info;
834 zif->down_count++;
835 quagga_timestamp (2, zif->down_last, sizeof (zif->down_last));
836
718e3744 837 /* Notify to the protocol daemons. */
838 zebra_interface_down_update (ifp);
839
c8e264b6 840 /* Uninstall connected routes from the kernel. */
841 if_uninstall_connected (ifp);
718e3744 842
41ec9222 843 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1c848137 844 zlog_debug ("%u: IF %s down, scheduling RIB processing",
845 ifp->vrf_id, ifp->name);
846 rib_update (ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
5c610faf
DS
847
848 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all (ifp);
a197c47c
DS
849
850 /* Delete all neighbor addresses learnt through IPv6 RA */
851 if_down_del_nbr_connected (ifp);
718e3744 852}
853
854void
855if_refresh (struct interface *ifp)
856{
5c78b3d0 857 if_get_flags (ifp);
718e3744 858}
859
d5a5c8f0 860
718e3744 861/* Output prefix string to vty. */
a1ac18c4 862static int
718e3744 863prefix_vty_out (struct vty *vty, struct prefix *p)
864{
865 char str[INET6_ADDRSTRLEN];
866
867 inet_ntop (p->family, &p->u.prefix, str, sizeof (str));
868 vty_out (vty, "%s", str);
869 return strlen (str);
870}
871
872/* Dump if address information to vty. */
a1ac18c4 873static void
718e3744 874connected_dump_vty (struct vty *vty, struct connected *connected)
875{
876 struct prefix *p;
718e3744 877
878 /* Print interface address. */
879 p = connected->address;
880 vty_out (vty, " %s ", prefix_family_str (p));
881 prefix_vty_out (vty, p);
882 vty_out (vty, "/%d", p->prefixlen);
883
884 /* If there is destination address, print it. */
e4529636 885 if (connected->destination)
718e3744 886 {
e4529636
AS
887 vty_out (vty, (CONNECTED_PEER(connected) ? " peer " : " broadcast "));
888 prefix_vty_out (vty, connected->destination);
718e3744 889 }
890
891 if (CHECK_FLAG (connected->flags, ZEBRA_IFA_SECONDARY))
892 vty_out (vty, " secondary");
893
525c1839
DS
894 if (CHECK_FLAG (connected->flags, ZEBRA_IFA_UNNUMBERED))
895 vty_out (vty, " unnumbered");
896
718e3744 897 if (connected->label)
898 vty_out (vty, " %s", connected->label);
899
900 vty_out (vty, "%s", VTY_NEWLINE);
901}
902
a80beece
DS
903/* Dump interface neighbor address information to vty. */
904static void
905nbr_connected_dump_vty (struct vty *vty, struct nbr_connected *connected)
906{
907 struct prefix *p;
908
909 /* Print interface address. */
910 p = connected->address;
911 vty_out (vty, " %s ", prefix_family_str (p));
912 prefix_vty_out (vty, p);
913 vty_out (vty, "/%d", p->prefixlen);
914
915 vty_out (vty, "%s", VTY_NEWLINE);
916}
917
8da4e946 918#if defined (HAVE_RTADV)
718e3744 919/* Dump interface ND information to vty. */
a1ac18c4 920static void
718e3744 921nd_dump_vty (struct vty *vty, struct interface *ifp)
922{
923 struct zebra_if *zif;
924 struct rtadvconf *rtadv;
7cee1bb1 925 int interval;
718e3744 926
927 zif = (struct zebra_if *) ifp->info;
928 rtadv = &zif->rtadv;
929
930 if (rtadv->AdvSendAdvertisements)
931 {
932 vty_out (vty, " ND advertised reachable time is %d milliseconds%s",
933 rtadv->AdvReachableTime, VTY_NEWLINE);
934 vty_out (vty, " ND advertised retransmit interval is %d milliseconds%s",
935 rtadv->AdvRetransTimer, VTY_NEWLINE);
7cee1bb1 936 interval = rtadv->MaxRtrAdvInterval;
937 if (interval % 1000)
938 vty_out (vty, " ND router advertisements are sent every "
939 "%d milliseconds%s", interval,
940 VTY_NEWLINE);
941 else
942 vty_out (vty, " ND router advertisements are sent every "
943 "%d seconds%s", interval / 1000,
944 VTY_NEWLINE);
d660f698
DO
945 if (rtadv->AdvDefaultLifetime != -1)
946 vty_out (vty, " ND router advertisements live for %d seconds%s",
947 rtadv->AdvDefaultLifetime, VTY_NEWLINE);
948 else
949 vty_out (vty, " ND router advertisements lifetime tracks ra-interval%s",
950 VTY_NEWLINE);
b60668d0
CC
951 vty_out (vty, " ND router advertisement default router preference is "
952 "%s%s", rtadv_pref_strs[rtadv->DefaultPreference],
953 VTY_NEWLINE);
718e3744 954 if (rtadv->AdvManagedFlag)
955 vty_out (vty, " Hosts use DHCP to obtain routable addresses.%s",
956 VTY_NEWLINE);
957 else
958 vty_out (vty, " Hosts use stateless autoconfig for addresses.%s",
959 VTY_NEWLINE);
7cee1bb1 960 if (rtadv->AdvHomeAgentFlag)
d660f698 961 {
7cee1bb1 962 vty_out (vty, " ND router advertisements with "
963 "Home Agent flag bit set.%s",
964 VTY_NEWLINE);
d660f698
DO
965 if (rtadv->HomeAgentLifetime != -1)
966 vty_out (vty, " Home Agent lifetime is %u seconds%s",
967 rtadv->HomeAgentLifetime, VTY_NEWLINE);
968 else
969 vty_out (vty, " Home Agent lifetime tracks ra-lifetime%s",
970 VTY_NEWLINE);
971 vty_out (vty, " Home Agent preference is %u%s",
972 rtadv->HomeAgentPreference, VTY_NEWLINE);
973 }
7cee1bb1 974 if (rtadv->AdvIntervalOption)
975 vty_out (vty, " ND router advertisements with Adv. Interval option.%s",
976 VTY_NEWLINE);
718e3744 977 }
978}
8da4e946 979#endif /* HAVE_RTADV */
718e3744 980
981/* Interface's information print out to vty interface. */
a1ac18c4 982static void
718e3744 983if_dump_vty (struct vty *vty, struct interface *ifp)
984{
6f0e3f6e 985#ifdef HAVE_STRUCT_SOCKADDR_DL
718e3744 986 struct sockaddr_dl *sdl;
6f0e3f6e 987#endif /* HAVE_STRUCT_SOCKADDR_DL */
718e3744 988 struct connected *connected;
a80beece 989 struct nbr_connected *nbr_connected;
52dc7ee6 990 struct listnode *node;
eef1fe11 991 struct route_node *rn;
992 struct zebra_if *zebra_if;
e6a59b35 993 struct vrf *vrf;
eef1fe11 994
995 zebra_if = ifp->info;
718e3744 996
2e3b2e47 997 vty_out (vty, "Interface %s is ", ifp->name);
998 if (if_is_up(ifp)) {
999 vty_out (vty, "up, line protocol ");
1000
1001 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)) {
1002 if (if_is_running(ifp))
1003 vty_out (vty, "is up%s", VTY_NEWLINE);
1004 else
1005 vty_out (vty, "is down%s", VTY_NEWLINE);
1006 } else {
1007 vty_out (vty, "detection is disabled%s", VTY_NEWLINE);
1008 }
1009 } else {
1010 vty_out (vty, "down%s", VTY_NEWLINE);
1011 }
1012
55ce4d3a
CF
1013 vty_out (vty, " Link ups: %5u last: %s%s", zebra_if->up_count,
1014 zebra_if->up_last[0] ? zebra_if->up_last : "(never)", VTY_NEWLINE);
1015 vty_out (vty, " Link downs: %5u last: %s%s", zebra_if->down_count,
1016 zebra_if->down_last[0] ? zebra_if->down_last : "(never)", VTY_NEWLINE);
1017
950bd436 1018 zebra_ptm_show_status(vty, ifp);
244c1cdc 1019
e6a59b35
DS
1020 vrf = vrf_lookup(ifp->vrf_id);
1021 vty_out (vty, " vrf: %s%s", vrf->name, VTY_NEWLINE);
3968dbf8 1022
718e3744 1023 if (ifp->desc)
1024 vty_out (vty, " Description: %s%s", ifp->desc,
1025 VTY_NEWLINE);
d2fc8896 1026 if (ifp->ifindex == IFINDEX_INTERNAL)
718e3744 1027 {
d2fc8896 1028 vty_out(vty, " pseudo interface%s", VTY_NEWLINE);
718e3744 1029 return;
1030 }
1031 else if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
1032 {
1033 vty_out(vty, " index %d inactive interface%s",
1034 ifp->ifindex,
1035 VTY_NEWLINE);
1036 return;
1037 }
1038
1039 vty_out (vty, " index %d metric %d mtu %d ",
1040 ifp->ifindex, ifp->metric, ifp->mtu);
44145db3 1041#ifdef HAVE_IPV6
1042 if (ifp->mtu6 != ifp->mtu)
1043 vty_out (vty, "mtu6 %d ", ifp->mtu6);
1044#endif
630c97ce
PJ
1045 vty_out (vty, "%s flags: %s%s", VTY_NEWLINE,
1046 if_flag_dump (ifp->flags), VTY_NEWLINE);
3a570c8b 1047
718e3744 1048 /* Hardware address. */
6f0e3f6e 1049#ifdef HAVE_STRUCT_SOCKADDR_DL
718e3744 1050 sdl = &ifp->sdl;
1051 if (sdl != NULL && sdl->sdl_alen != 0)
1052 {
1053 int i;
1054 u_char *ptr;
1055
1056 vty_out (vty, " HWaddr: ");
5b73a671 1057 for (i = 0, ptr = (u_char *)LLADDR (sdl); i < sdl->sdl_alen; i++, ptr++)
1058 vty_out (vty, "%s%02x", i == 0 ? "" : ":", *ptr);
718e3744 1059 vty_out (vty, "%s", VTY_NEWLINE);
1060 }
1061#else
1062 if (ifp->hw_addr_len != 0)
1063 {
1064 int i;
1065
1066 vty_out (vty, " HWaddr: ");
1067 for (i = 0; i < ifp->hw_addr_len; i++)
1068 vty_out (vty, "%s%02x", i == 0 ? "" : ":", ifp->hw_addr[i]);
1069 vty_out (vty, "%s", VTY_NEWLINE);
1070 }
6f0e3f6e 1071#endif /* HAVE_STRUCT_SOCKADDR_DL */
718e3744 1072
1073 /* Bandwidth in kbps */
1074 if (ifp->bandwidth != 0)
1075 {
1076 vty_out(vty, " bandwidth %u kbps", ifp->bandwidth);
1077 vty_out(vty, "%s", VTY_NEWLINE);
1078 }
1079
eef1fe11 1080 for (rn = route_top (zebra_if->ipv4_subnets); rn; rn = route_next (rn))
718e3744 1081 {
eef1fe11 1082 if (! rn->info)
1083 continue;
1084
1eb8ef25 1085 for (ALL_LIST_ELEMENTS_RO ((struct list *)rn->info, node, connected))
1086 connected_dump_vty (vty, connected);
718e3744 1087 }
1088
1eb8ef25 1089 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
39db97e4 1090 {
39db97e4 1091 if (CHECK_FLAG (connected->conf, ZEBRA_IFC_REAL) &&
1092 (connected->address->family == AF_INET6))
1093 connected_dump_vty (vty, connected);
1094 }
1095
8da4e946 1096#if defined (HAVE_RTADV)
718e3744 1097 nd_dump_vty (vty, ifp);
8da4e946 1098#endif /* HAVE_RTADV */
a80beece
DS
1099 if (listhead(ifp->nbr_connected))
1100 vty_out (vty, " Neighbor address(s):%s", VTY_NEWLINE);
1101 for (ALL_LIST_ELEMENTS_RO (ifp->nbr_connected, node, nbr_connected))
1102 nbr_connected_dump_vty (vty, nbr_connected);
718e3744 1103
1104#ifdef HAVE_PROC_NET_DEV
1105 /* Statistics print out using proc file system. */
6f2c27af 1106 vty_out (vty, " %lu input packets (%lu multicast), %lu bytes, "
1107 "%lu dropped%s",
1108 ifp->stats.rx_packets, ifp->stats.rx_multicast,
1109 ifp->stats.rx_bytes, ifp->stats.rx_dropped, VTY_NEWLINE);
718e3744 1110
6f2c27af 1111 vty_out (vty, " %lu input errors, %lu length, %lu overrun,"
3452d475 1112 " %lu CRC, %lu frame%s",
718e3744 1113 ifp->stats.rx_errors, ifp->stats.rx_length_errors,
1114 ifp->stats.rx_over_errors, ifp->stats.rx_crc_errors,
6f2c27af 1115 ifp->stats.rx_frame_errors, VTY_NEWLINE);
1116
1117 vty_out (vty, " %lu fifo, %lu missed%s", ifp->stats.rx_fifo_errors,
718e3744 1118 ifp->stats.rx_missed_errors, VTY_NEWLINE);
1119
6f2c27af 1120 vty_out (vty, " %lu output packets, %lu bytes, %lu dropped%s",
718e3744 1121 ifp->stats.tx_packets, ifp->stats.tx_bytes,
1122 ifp->stats.tx_dropped, VTY_NEWLINE);
1123
6f2c27af 1124 vty_out (vty, " %lu output errors, %lu aborted, %lu carrier,"
1125 " %lu fifo, %lu heartbeat%s",
718e3744 1126 ifp->stats.tx_errors, ifp->stats.tx_aborted_errors,
1127 ifp->stats.tx_carrier_errors, ifp->stats.tx_fifo_errors,
6f2c27af 1128 ifp->stats.tx_heartbeat_errors, VTY_NEWLINE);
718e3744 1129
6f2c27af 1130 vty_out (vty, " %lu window, %lu collisions%s",
1131 ifp->stats.tx_window_errors, ifp->stats.collisions, VTY_NEWLINE);
718e3744 1132#endif /* HAVE_PROC_NET_DEV */
1133
1134#ifdef HAVE_NET_RT_IFLIST
1135#if defined (__bsdi__) || defined (__NetBSD__)
1136 /* Statistics print out using sysctl (). */
24b46333
DL
1137 vty_out (vty, " input packets %llu, bytes %llu, dropped %llu,"
1138 " multicast packets %llu%s",
1139 (unsigned long long)ifp->stats.ifi_ipackets,
1140 (unsigned long long)ifp->stats.ifi_ibytes,
1141 (unsigned long long)ifp->stats.ifi_iqdrops,
1142 (unsigned long long)ifp->stats.ifi_imcasts,
1143 VTY_NEWLINE);
1144
1145 vty_out (vty, " input errors %llu%s",
1146 (unsigned long long)ifp->stats.ifi_ierrors, VTY_NEWLINE);
1147
1148 vty_out (vty, " output packets %llu, bytes %llu,"
1149 " multicast packets %llu%s",
1150 (unsigned long long)ifp->stats.ifi_opackets,
1151 (unsigned long long)ifp->stats.ifi_obytes,
1152 (unsigned long long)ifp->stats.ifi_omcasts,
1153 VTY_NEWLINE);
1154
1155 vty_out (vty, " output errors %llu%s",
1156 (unsigned long long)ifp->stats.ifi_oerrors, VTY_NEWLINE);
1157
1158 vty_out (vty, " collisions %llu%s",
1159 (unsigned long long)ifp->stats.ifi_collisions, VTY_NEWLINE);
718e3744 1160#else
1161 /* Statistics print out using sysctl (). */
1162 vty_out (vty, " input packets %lu, bytes %lu, dropped %lu,"
1163 " multicast packets %lu%s",
1164 ifp->stats.ifi_ipackets, ifp->stats.ifi_ibytes,
1165 ifp->stats.ifi_iqdrops, ifp->stats.ifi_imcasts,
1166 VTY_NEWLINE);
1167
1168 vty_out (vty, " input errors %lu%s",
1169 ifp->stats.ifi_ierrors, VTY_NEWLINE);
1170
1171 vty_out (vty, " output packets %lu, bytes %lu, multicast packets %lu%s",
1172 ifp->stats.ifi_opackets, ifp->stats.ifi_obytes,
1173 ifp->stats.ifi_omcasts, VTY_NEWLINE);
1174
1175 vty_out (vty, " output errors %lu%s",
1176 ifp->stats.ifi_oerrors, VTY_NEWLINE);
1177
1178 vty_out (vty, " collisions %lu%s",
1179 ifp->stats.ifi_collisions, VTY_NEWLINE);
1180#endif /* __bsdi__ || __NetBSD__ */
1181#endif /* HAVE_NET_RT_IFLIST */
1182}
1183
718e3744 1184/* Wrapper hook point for zebra daemon so that ifindex can be set
1185 * DEFUN macro not used as extract.pl HAS to ignore this
1186 * See also interface_cmd in lib/if.c
1187 */
1188DEFUN_NOSH (zebra_interface,
1189 zebra_interface_cmd,
1190 "interface IFNAME",
1191 "Select an interface to configure\n"
1192 "Interface's name\n")
1193{
1194 int ret;
1195 struct interface * ifp;
1196
1197 /* Call lib interface() */
d2fc8896 1198 if ((ret = interface_cmd.func (self, vty, argc, argv)) != CMD_SUCCESS)
1199 return ret;
718e3744 1200
1201 ifp = vty->index;
1202
d2fc8896 1203 if (ifp->ifindex == IFINDEX_INTERNAL)
1204 /* Is this really necessary? Shouldn't status be initialized to 0
1205 in that case? */
1206 UNSET_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE);
718e3744 1207
1208 return ret;
1209}
1210
cd2a8a42
FL
1211ALIAS (zebra_interface,
1212 zebra_interface_vrf_cmd,
1213 "interface IFNAME " VRF_CMD_STR,
1214 "Select an interface to configure\n"
1215 "Interface's name\n"
1216 VRF_CMD_HELP_STR)
1217
718e3744 1218struct cmd_node interface_node =
1219{
1220 INTERFACE_NODE,
1221 "%s(config-if)# ",
1222 1
1223};
1224
12f6fb97
DS
1225/* Wrapper hook point for zebra daemon so that ifindex can be set
1226 * DEFUN macro not used as extract.pl HAS to ignore this
1227 * See also interface_cmd in lib/if.c
1228 */
1229DEFUN_NOSH (zebra_vrf,
1230 zebra_vrf_cmd,
1231 "vrf NAME",
1232 "Select a VRF to configure\n"
1233 "VRF's name\n")
1234{
1235 int ret;
1236
1237 /* Call lib vrf() */
1238 if ((ret = vrf_cmd.func (self, vty, argc, argv)) != CMD_SUCCESS)
1239 return ret;
1240
1241 // vrfp = vty->index;
1242
1243 return ret;
1244}
1245
1246struct cmd_node vrf_node =
1247{
1248 VRF_NODE,
1249 "%s(config-vrf)# ",
1250 1
1251};
1252
8b87bdf4 1253/* Show all interfaces to vty. */
718e3744 1254DEFUN (show_interface, show_interface_cmd,
8b87bdf4
FL
1255 "show interface",
1256 SHOW_STR
1257 "Interface status and configuration\n")
1258{
1259 struct listnode *node;
1260 struct interface *ifp;
1261 vrf_id_t vrf_id = VRF_DEFAULT;
1262
1263#ifdef HAVE_PROC_NET_DEV
1264 /* If system has interface statistics via proc file system, update
1265 statistics. */
1266 ifstat_update_proc ();
1267#endif /* HAVE_PROC_NET_DEV */
1268#ifdef HAVE_NET_RT_IFLIST
1269 ifstat_update_sysctl ();
1270#endif /* HAVE_NET_RT_IFLIST */
1271
1272 if (argc > 0)
12f6fb97 1273 VRF_GET_ID (vrf_id, argv[0]);
8b87bdf4
FL
1274
1275 /* All interface print. */
1276 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
1277 if_dump_vty (vty, ifp);
1278
1279 return CMD_SUCCESS;
1280}
1281
1282ALIAS (show_interface,
1283 show_interface_vrf_cmd,
1284 "show interface " VRF_CMD_STR,
718e3744 1285 SHOW_STR
1286 "Interface status and configuration\n"
8b87bdf4
FL
1287 VRF_CMD_HELP_STR)
1288
1289/* Show all interfaces to vty. */
1290DEFUN (show_interface_vrf_all, show_interface_vrf_all_cmd,
1291 "show interface " VRF_ALL_CMD_STR,
1292 SHOW_STR
1293 "Interface status and configuration\n"
1294 VRF_ALL_CMD_HELP_STR)
718e3744 1295{
52dc7ee6 1296 struct listnode *node;
718e3744 1297 struct interface *ifp;
8b87bdf4
FL
1298 vrf_iter_t iter;
1299
718e3744 1300#ifdef HAVE_PROC_NET_DEV
1301 /* If system has interface statistics via proc file system, update
1302 statistics. */
1303 ifstat_update_proc ();
1304#endif /* HAVE_PROC_NET_DEV */
1305#ifdef HAVE_NET_RT_IFLIST
1306 ifstat_update_sysctl ();
1307#endif /* HAVE_NET_RT_IFLIST */
1308
8b87bdf4
FL
1309 /* All interface print. */
1310 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
1311 for (ALL_LIST_ELEMENTS_RO (vrf_iter2iflist (iter), node, ifp))
1312 if_dump_vty (vty, ifp);
1313
1314 return CMD_SUCCESS;
1315}
1316
1317/* Show specified interface to vty. */
1721646e
DS
1318
1319DEFUN (show_interface_name_vrf,
1320 show_interface_name_vrf_cmd,
1321 "show interface IFNAME " VRF_CMD_STR,
8b87bdf4
FL
1322 SHOW_STR
1323 "Interface status and configuration\n"
1721646e
DS
1324 "Interface name\n"
1325 VRF_CMD_HELP_STR)
8b87bdf4
FL
1326{
1327 struct interface *ifp;
1328 vrf_id_t vrf_id = VRF_DEFAULT;
1329
1330#ifdef HAVE_PROC_NET_DEV
1331 /* If system has interface statistics via proc file system, update
1332 statistics. */
1333 ifstat_update_proc ();
1334#endif /* HAVE_PROC_NET_DEV */
1335#ifdef HAVE_NET_RT_IFLIST
1336 ifstat_update_sysctl ();
1337#endif /* HAVE_NET_RT_IFLIST */
1338
1339 if (argc > 1)
12f6fb97 1340 VRF_GET_ID (vrf_id, argv[1]);
8b87bdf4 1341
718e3744 1342 /* Specified interface print. */
8b87bdf4
FL
1343 ifp = if_lookup_by_name_vrf (argv[0], vrf_id);
1344 if (ifp == NULL)
718e3744 1345 {
8b87bdf4
FL
1346 vty_out (vty, "%% Can't find interface %s%s", argv[0],
1347 VTY_NEWLINE);
1348 return CMD_WARNING;
718e3744 1349 }
8b87bdf4 1350 if_dump_vty (vty, ifp);
718e3744 1351
1352 return CMD_SUCCESS;
1353}
1354
8b87bdf4
FL
1355/* Show specified interface to vty. */
1356DEFUN (show_interface_name_vrf_all, show_interface_name_vrf_all_cmd,
1357 "show interface IFNAME " VRF_ALL_CMD_STR,
1358 SHOW_STR
1359 "Interface status and configuration\n"
1721646e 1360 "Interface name\n"
8b87bdf4
FL
1361 VRF_ALL_CMD_HELP_STR)
1362{
1363 struct interface *ifp;
1364 vrf_iter_t iter;
1365 int found = 0;
1366
1367#ifdef HAVE_PROC_NET_DEV
1368 /* If system has interface statistics via proc file system, update
1369 statistics. */
1370 ifstat_update_proc ();
1371#endif /* HAVE_PROC_NET_DEV */
1372#ifdef HAVE_NET_RT_IFLIST
1373 ifstat_update_sysctl ();
1374#endif /* HAVE_NET_RT_IFLIST */
1375
1376 /* All interface print. */
1377 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
1378 {
1379 /* Specified interface print. */
1380 ifp = if_lookup_by_name_vrf (argv[0], vrf_iter2id (iter));
1381 if (ifp)
1382 {
1383 if_dump_vty (vty, ifp);
1384 found++;
1385 }
1386 }
1387
1388 if (!found)
1389 {
1390 vty_out (vty, "%% Can't find interface %s%s", argv[0], VTY_NEWLINE);
1391 return CMD_WARNING;
1392 }
1393
1394 return CMD_SUCCESS;
1395}
1396
1721646e
DS
1397ALIAS (show_interface_name_vrf_all, show_interface_name_cmd,
1398 "show interface IFNAME",
1399 SHOW_STR
1400 "Interface status and configuration\n"
1401 "Interface name\n")
1402
8b87bdf4
FL
1403static void
1404if_show_description (struct vty *vty, vrf_id_t vrf_id)
ed9bb6d5 1405{
1406 struct listnode *node;
1407 struct interface *ifp;
1408
1409 vty_out (vty, "Interface Status Protocol Description%s", VTY_NEWLINE);
8b87bdf4 1410 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
ed9bb6d5 1411 {
1412 int len;
ed9bb6d5 1413
1414 len = vty_out (vty, "%s", ifp->name);
1415 vty_out (vty, "%*s", (16 - len), " ");
1416
1417 if (if_is_up(ifp))
1418 {
1419 vty_out (vty, "up ");
1420 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION))
1421 {
1422 if (if_is_running(ifp))
1423 vty_out (vty, "up ");
1424 else
1425 vty_out (vty, "down ");
1426 }
1427 else
1428 {
1429 vty_out (vty, "unknown ");
1430 }
1431 }
1432 else
1433 {
1434 vty_out (vty, "down down ");
1435 }
1436
1437 if (ifp->desc)
1438 vty_out (vty, "%s", ifp->desc);
1439 vty_out (vty, "%s", VTY_NEWLINE);
1440 }
8b87bdf4
FL
1441}
1442
1443DEFUN (show_interface_desc,
1444 show_interface_desc_cmd,
1445 "show interface description",
1446 SHOW_STR
1447 "Interface status and configuration\n"
1448 "Interface description\n")
1449{
1450 vrf_id_t vrf_id = VRF_DEFAULT;
1451
1452 if (argc > 0)
12f6fb97 1453 VRF_GET_ID (vrf_id, argv[0]);
8b87bdf4
FL
1454
1455 if_show_description (vty, vrf_id);
1456
1457 return CMD_SUCCESS;
1458}
1459
1460ALIAS (show_interface_desc,
1461 show_interface_desc_vrf_cmd,
1462 "show interface description " VRF_CMD_STR,
1463 SHOW_STR
1464 "Interface status and configuration\n"
1465 "Interface description\n"
1466 VRF_CMD_HELP_STR)
1467
1468DEFUN (show_interface_desc_vrf_all,
1469 show_interface_desc_vrf_all_cmd,
1470 "show interface description " VRF_ALL_CMD_STR,
1471 SHOW_STR
1472 "Interface status and configuration\n"
1473 "Interface description\n"
1474 VRF_ALL_CMD_HELP_STR)
1475{
1476 vrf_iter_t iter;
1477
1478 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
1479 if (!list_isempty (vrf_iter2iflist (iter)))
1480 {
1481 vty_out (vty, "%s\tVRF %u%s%s", VTY_NEWLINE,
1482 vrf_iter2id (iter),
1483 VTY_NEWLINE, VTY_NEWLINE);
1484 if_show_description (vty, vrf_iter2id (iter));
1485 }
1486
ed9bb6d5 1487 return CMD_SUCCESS;
1488}
1489
718e3744 1490DEFUN (multicast,
1491 multicast_cmd,
1492 "multicast",
1493 "Set multicast flag to interface\n")
1494{
1495 int ret;
1496 struct interface *ifp;
1497 struct zebra_if *if_data;
1498
1499 ifp = (struct interface *) vty->index;
48b33aaf 1500 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
718e3744 1501 {
48b33aaf 1502 ret = if_set_flags (ifp, IFF_MULTICAST);
1503 if (ret < 0)
1504 {
1505 vty_out (vty, "Can't set multicast flag%s", VTY_NEWLINE);
1506 return CMD_WARNING;
1507 }
1508 if_refresh (ifp);
718e3744 1509 }
718e3744 1510 if_data = ifp->info;
1511 if_data->multicast = IF_ZEBRA_MULTICAST_ON;
48b33aaf 1512
718e3744 1513 return CMD_SUCCESS;
1514}
1515
1516DEFUN (no_multicast,
1517 no_multicast_cmd,
1518 "no multicast",
1519 NO_STR
1520 "Unset multicast flag to interface\n")
1521{
1522 int ret;
1523 struct interface *ifp;
1524 struct zebra_if *if_data;
1525
1526 ifp = (struct interface *) vty->index;
48b33aaf 1527 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
718e3744 1528 {
48b33aaf 1529 ret = if_unset_flags (ifp, IFF_MULTICAST);
1530 if (ret < 0)
1531 {
1532 vty_out (vty, "Can't unset multicast flag%s", VTY_NEWLINE);
1533 return CMD_WARNING;
1534 }
1535 if_refresh (ifp);
718e3744 1536 }
718e3744 1537 if_data = ifp->info;
1538 if_data->multicast = IF_ZEBRA_MULTICAST_OFF;
1539
1540 return CMD_SUCCESS;
1541}
1542
2e3b2e47 1543DEFUN (linkdetect,
1544 linkdetect_cmd,
1545 "link-detect",
1546 "Enable link detection on interface\n")
1547{
2e3b2e47 1548 struct interface *ifp;
1549 int if_was_operative;
1550
1551 ifp = (struct interface *) vty->index;
244c1cdc 1552 if_was_operative = if_is_no_ptm_operative(ifp);
2e3b2e47 1553 SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
1554
1555 /* When linkdetection is enabled, if might come down */
244c1cdc 1556 if (!if_is_no_ptm_operative(ifp) && if_was_operative) if_down(ifp);
2e3b2e47 1557
1558 /* FIXME: Will defer status change forwarding if interface
1559 does not come down! */
1560
1561 return CMD_SUCCESS;
1562}
1563
1564
1565DEFUN (no_linkdetect,
1566 no_linkdetect_cmd,
1567 "no link-detect",
1568 NO_STR
1569 "Disable link detection on interface\n")
1570{
2e3b2e47 1571 struct interface *ifp;
1572 int if_was_operative;
1573
1574 ifp = (struct interface *) vty->index;
244c1cdc 1575 if_was_operative = if_is_no_ptm_operative(ifp);
2e3b2e47 1576 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
1577
1578 /* Interface may come up after disabling link detection */
1579 if (if_is_operative(ifp) && !if_was_operative) if_up(ifp);
1580
1581 /* FIXME: see linkdetect_cmd */
1582
1583 return CMD_SUCCESS;
1584}
1585
718e3744 1586DEFUN (shutdown_if,
1587 shutdown_if_cmd,
1588 "shutdown",
1589 "Shutdown the selected interface\n")
1590{
1591 int ret;
1592 struct interface *ifp;
1593 struct zebra_if *if_data;
1594
1595 ifp = (struct interface *) vty->index;
bfac8dcd 1596 if (ifp->ifindex != IFINDEX_INTERNAL)
718e3744 1597 {
bfac8dcd
CF
1598 ret = if_unset_flags (ifp, IFF_UP);
1599 if (ret < 0)
1600 {
1601 vty_out (vty, "Can't shutdown interface%s", VTY_NEWLINE);
1602 return CMD_WARNING;
1603 }
1604 if_refresh (ifp);
718e3744 1605 }
718e3744 1606 if_data = ifp->info;
1607 if_data->shutdown = IF_ZEBRA_SHUTDOWN_ON;
1608
1609 return CMD_SUCCESS;
1610}
1611
1612DEFUN (no_shutdown_if,
1613 no_shutdown_if_cmd,
1614 "no shutdown",
1615 NO_STR
1616 "Shutdown the selected interface\n")
1617{
1618 int ret;
1619 struct interface *ifp;
1620 struct zebra_if *if_data;
1621
1622 ifp = (struct interface *) vty->index;
bfac8dcd
CF
1623
1624 if (ifp->ifindex != IFINDEX_INTERNAL)
718e3744 1625 {
bfac8dcd
CF
1626 ret = if_set_flags (ifp, IFF_UP | IFF_RUNNING);
1627 if (ret < 0)
1628 {
1629 vty_out (vty, "Can't up interface%s", VTY_NEWLINE);
1630 return CMD_WARNING;
1631 }
1632 if_refresh (ifp);
1633
1634 /* Some addresses (in particular, IPv6 addresses on Linux) get
1635 * removed when the interface goes down. They need to be readded.
1636 */
1637 if_addr_wakeup(ifp);
718e3744 1638 }
bfac8dcd 1639
718e3744 1640 if_data = ifp->info;
1641 if_data->shutdown = IF_ZEBRA_SHUTDOWN_OFF;
1642
1643 return CMD_SUCCESS;
1644}
1645
1646DEFUN (bandwidth_if,
1647 bandwidth_if_cmd,
1648 "bandwidth <1-10000000>",
1649 "Set bandwidth informational parameter\n"
1650 "Bandwidth in kilobits\n")
1651{
1652 struct interface *ifp;
1653 unsigned int bandwidth;
1654
1655 ifp = (struct interface *) vty->index;
1656 bandwidth = strtol(argv[0], NULL, 10);
1657
1658 /* bandwidth range is <1-10000000> */
1659 if (bandwidth < 1 || bandwidth > 10000000)
1660 {
1661 vty_out (vty, "Bandwidth is invalid%s", VTY_NEWLINE);
1662 return CMD_WARNING;
1663 }
1664
1665 ifp->bandwidth = bandwidth;
1666
1667 /* force protocols to recalculate routes due to cost change */
2e3b2e47 1668 if (if_is_operative (ifp))
718e3744 1669 zebra_interface_up_update (ifp);
1670
1671 return CMD_SUCCESS;
1672}
1673
1674DEFUN (no_bandwidth_if,
1675 no_bandwidth_if_cmd,
1676 "no bandwidth",
1677 NO_STR
1678 "Set bandwidth informational parameter\n")
1679{
1680 struct interface *ifp;
1681
1682 ifp = (struct interface *) vty->index;
1683
1684 ifp->bandwidth = 0;
1685
1686 /* force protocols to recalculate routes due to cost change */
2e3b2e47 1687 if (if_is_operative (ifp))
718e3744 1688 zebra_interface_up_update (ifp);
1689
1690 return CMD_SUCCESS;
1691}
1692
1693ALIAS (no_bandwidth_if,
1694 no_bandwidth_if_val_cmd,
1695 "no bandwidth <1-10000000>",
1696 NO_STR
1697 "Set bandwidth informational parameter\n"
1698 "Bandwidth in kilobits\n")
6b0655a2 1699
a1ac18c4 1700static int
39db97e4 1701ip_address_install (struct vty *vty, struct interface *ifp,
1702 const char *addr_str, const char *peer_str,
1703 const char *label)
718e3744 1704{
bfac8dcd 1705 struct zebra_if *if_data;
718e3744 1706 struct prefix_ipv4 cp;
1707 struct connected *ifc;
1708 struct prefix_ipv4 *p;
718e3744 1709 int ret;
1710
bfac8dcd
CF
1711 if_data = ifp->info;
1712
718e3744 1713 ret = str2prefix_ipv4 (addr_str, &cp);
1714 if (ret <= 0)
1715 {
1716 vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
1717 return CMD_WARNING;
1718 }
1719
d914d5ff
DS
1720 if (ipv4_martian(&cp.prefix))
1721 {
1722 vty_out (vty, "%% Invalid address%s", VTY_NEWLINE);
1723 return CMD_WARNING;
1724 }
1725
ca16218d 1726 ifc = connected_check (ifp, (struct prefix *) &cp);
718e3744 1727 if (! ifc)
1728 {
1729 ifc = connected_new ();
1730 ifc->ifp = ifp;
1731
1732 /* Address. */
1733 p = prefix_ipv4_new ();
1734 *p = cp;
1735 ifc->address = (struct prefix *) p;
1736
1737 /* Broadcast. */
3fb9cd6e 1738 if (p->prefixlen <= IPV4_MAX_PREFIXLEN-2)
718e3744 1739 {
1740 p = prefix_ipv4_new ();
1741 *p = cp;
3fb9cd6e 1742 p->prefix.s_addr = ipv4_broadcast_addr(p->prefix.s_addr,p->prefixlen);
718e3744 1743 ifc->destination = (struct prefix *) p;
1744 }
1745
718e3744 1746 /* Label. */
1747 if (label)
0752ef0b 1748 ifc->label = XSTRDUP (MTYPE_CONNECTED_LABEL, label);
718e3744 1749
1750 /* Add to linked list. */
1751 listnode_add (ifp->connected, ifc);
1752 }
1753
1754 /* This address is configured from zebra. */
1755 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
1756 SET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
1757
1758 /* In case of this route need to install kernel. */
f7f740fe 1759 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
bfac8dcd
CF
1760 && CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)
1761 && !(if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON))
718e3744 1762 {
1763 /* Some system need to up the interface to set IP address. */
1764 if (! if_is_up (ifp))
1765 {
1766 if_set_flags (ifp, IFF_UP | IFF_RUNNING);
1767 if_refresh (ifp);
1768 }
1769
1770 ret = if_set_prefix (ifp, ifc);
1771 if (ret < 0)
1772 {
1773 vty_out (vty, "%% Can't set interface IP address: %s.%s",
6099b3b5 1774 safe_strerror(errno), VTY_NEWLINE);
718e3744 1775 return CMD_WARNING;
1776 }
1777
f7f740fe 1778 SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
02b4805f
CF
1779 /* The address will be advertised to zebra clients when the notification
1780 * from the kernel has been received.
1781 * It will also be added to the subnet chain list, then. */
718e3744 1782 }
1783
1784 return CMD_SUCCESS;
1785}
1786
a1ac18c4 1787static int
39db97e4 1788ip_address_uninstall (struct vty *vty, struct interface *ifp,
1789 const char *addr_str, const char *peer_str,
1790 const char *label)
718e3744 1791{
1792 struct prefix_ipv4 cp;
1793 struct connected *ifc;
1794 int ret;
1795
1796 /* Convert to prefix structure. */
1797 ret = str2prefix_ipv4 (addr_str, &cp);
1798 if (ret <= 0)
1799 {
1800 vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
1801 return CMD_WARNING;
1802 }
1803
1804 /* Check current interface address. */
ca16218d 1805 ifc = connected_check (ifp, (struct prefix *) &cp);
718e3744 1806 if (! ifc)
1807 {
1808 vty_out (vty, "%% Can't find address%s", VTY_NEWLINE);
1809 return CMD_WARNING;
1810 }
1811
1812 /* This is not configured address. */
1813 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
1814 return CMD_WARNING;
1815
74ecdc9e
PJ
1816 UNSET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
1817
718e3744 1818 /* This is not real address or interface is not active. */
f7f740fe 1819 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
718e3744 1820 || ! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
1821 {
1822 listnode_delete (ifp->connected, ifc);
1823 connected_free (ifc);
1824 return CMD_WARNING;
1825 }
1826
1827 /* This is real route. */
1828 ret = if_unset_prefix (ifp, ifc);
1829 if (ret < 0)
1830 {
1831 vty_out (vty, "%% Can't unset interface IP address: %s.%s",
6099b3b5 1832 safe_strerror(errno), VTY_NEWLINE);
718e3744 1833 return CMD_WARNING;
1834 }
f7f740fe 1835 UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
02b4805f
CF
1836 /* we will receive a kernel notification about this route being removed.
1837 * this will trigger its removal from the connected list. */
718e3744 1838 return CMD_SUCCESS;
1839}
1840
1841DEFUN (ip_address,
1842 ip_address_cmd,
1843 "ip address A.B.C.D/M",
1844 "Interface Internet Protocol config commands\n"
1845 "Set the IP address of an interface\n"
1846 "IP address (e.g. 10.0.0.1/8)\n")
1847{
eef1fe11 1848 return ip_address_install (vty, vty->index, argv[0], NULL, NULL);
718e3744 1849}
1850
1851DEFUN (no_ip_address,
1852 no_ip_address_cmd,
1853 "no ip address A.B.C.D/M",
1854 NO_STR
1855 "Interface Internet Protocol config commands\n"
1856 "Set the IP address of an interface\n"
1857 "IP Address (e.g. 10.0.0.1/8)")
1858{
eef1fe11 1859 return ip_address_uninstall (vty, vty->index, argv[0], NULL, NULL);
718e3744 1860}
1861
986aa00f 1862
718e3744 1863#ifdef HAVE_NETLINK
718e3744 1864DEFUN (ip_address_label,
1865 ip_address_label_cmd,
1866 "ip address A.B.C.D/M label LINE",
1867 "Interface Internet Protocol config commands\n"
1868 "Set the IP address of an interface\n"
1869 "IP address (e.g. 10.0.0.1/8)\n"
1870 "Label of this address\n"
1871 "Label\n")
1872{
eef1fe11 1873 return ip_address_install (vty, vty->index, argv[0], NULL, argv[1]);
718e3744 1874}
1875
1876DEFUN (no_ip_address_label,
1877 no_ip_address_label_cmd,
1878 "no ip address A.B.C.D/M label LINE",
1879 NO_STR
1880 "Interface Internet Protocol config commands\n"
1881 "Set the IP address of an interface\n"
1882 "IP address (e.g. 10.0.0.1/8)\n"
1883 "Label of this address\n"
1884 "Label\n")
1885{
eef1fe11 1886 return ip_address_uninstall (vty, vty->index, argv[0], NULL, argv[1]);
718e3744 1887}
1888#endif /* HAVE_NETLINK */
1889
1890#ifdef HAVE_IPV6
a1ac18c4 1891static int
39db97e4 1892ipv6_address_install (struct vty *vty, struct interface *ifp,
1893 const char *addr_str, const char *peer_str,
1894 const char *label, int secondary)
718e3744 1895{
bfac8dcd 1896 struct zebra_if *if_data;
718e3744 1897 struct prefix_ipv6 cp;
1898 struct connected *ifc;
1899 struct prefix_ipv6 *p;
1900 int ret;
1901
bfac8dcd
CF
1902 if_data = ifp->info;
1903
718e3744 1904 ret = str2prefix_ipv6 (addr_str, &cp);
1905 if (ret <= 0)
1906 {
1907 vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
1908 return CMD_WARNING;
1909 }
1910
d914d5ff
DS
1911 if (ipv6_martian(&cp.prefix))
1912 {
1913 vty_out (vty, "%% Invalid address%s", VTY_NEWLINE);
1914 return CMD_WARNING;
1915 }
1916
ca16218d 1917 ifc = connected_check (ifp, (struct prefix *) &cp);
718e3744 1918 if (! ifc)
1919 {
1920 ifc = connected_new ();
1921 ifc->ifp = ifp;
1922
1923 /* Address. */
1924 p = prefix_ipv6_new ();
1925 *p = cp;
1926 ifc->address = (struct prefix *) p;
1927
1928 /* Secondary. */
1929 if (secondary)
1930 SET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
1931
1932 /* Label. */
1933 if (label)
0752ef0b 1934 ifc->label = XSTRDUP (MTYPE_CONNECTED_LABEL, label);
718e3744 1935
1936 /* Add to linked list. */
1937 listnode_add (ifp->connected, ifc);
1938 }
1939
1940 /* This address is configured from zebra. */
1941 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
1942 SET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
1943
1944 /* In case of this route need to install kernel. */
f7f740fe 1945 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
bfac8dcd
CF
1946 && CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)
1947 && !(if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON))
718e3744 1948 {
1949 /* Some system need to up the interface to set IP address. */
1950 if (! if_is_up (ifp))
1951 {
1952 if_set_flags (ifp, IFF_UP | IFF_RUNNING);
1953 if_refresh (ifp);
1954 }
1955
1956 ret = if_prefix_add_ipv6 (ifp, ifc);
1957
1958 if (ret < 0)
1959 {
1960 vty_out (vty, "%% Can't set interface IP address: %s.%s",
6099b3b5 1961 safe_strerror(errno), VTY_NEWLINE);
718e3744 1962 return CMD_WARNING;
1963 }
1964
f7f740fe 1965 SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
02b4805f
CF
1966 /* The address will be advertised to zebra clients when the notification
1967 * from the kernel has been received. */
718e3744 1968 }
1969
1970 return CMD_SUCCESS;
1971}
1972
b6120505
DW
1973/* Return true if an ipv6 address is configured on ifp */
1974int
1975ipv6_address_configured (struct interface *ifp)
1976{
1977 struct connected *connected;
1978 struct listnode *node;
1979
1980 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
1981 if (CHECK_FLAG (connected->conf, ZEBRA_IFC_REAL) && (connected->address->family == AF_INET6))
1982 return 1;
1983
1984 return 0;
1985}
1986
a1ac18c4 1987static int
39db97e4 1988ipv6_address_uninstall (struct vty *vty, struct interface *ifp,
1989 const char *addr_str, const char *peer_str,
1990 const char *label, int secondry)
718e3744 1991{
1992 struct prefix_ipv6 cp;
1993 struct connected *ifc;
1994 int ret;
1995
1996 /* Convert to prefix structure. */
1997 ret = str2prefix_ipv6 (addr_str, &cp);
1998 if (ret <= 0)
1999 {
2000 vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
2001 return CMD_WARNING;
2002 }
2003
2004 /* Check current interface address. */
ca16218d 2005 ifc = connected_check (ifp, (struct prefix *) &cp);
718e3744 2006 if (! ifc)
2007 {
2008 vty_out (vty, "%% Can't find address%s", VTY_NEWLINE);
2009 return CMD_WARNING;
2010 }
2011
2012 /* This is not configured address. */
2013 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
2014 return CMD_WARNING;
2015
676e1a01
CF
2016 UNSET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
2017
718e3744 2018 /* This is not real address or interface is not active. */
f7f740fe 2019 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
718e3744 2020 || ! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
2021 {
2022 listnode_delete (ifp->connected, ifc);
2023 connected_free (ifc);
2024 return CMD_WARNING;
2025 }
2026
2027 /* This is real route. */
2028 ret = if_prefix_delete_ipv6 (ifp, ifc);
2029 if (ret < 0)
2030 {
2031 vty_out (vty, "%% Can't unset interface IP address: %s.%s",
6099b3b5 2032 safe_strerror(errno), VTY_NEWLINE);
718e3744 2033 return CMD_WARNING;
2034 }
2035
f7f740fe 2036 UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
02b4805f
CF
2037 /* This information will be propagated to the zclients when the
2038 * kernel notification is received. */
718e3744 2039 return CMD_SUCCESS;
2040}
2041
2042DEFUN (ipv6_address,
2043 ipv6_address_cmd,
2044 "ipv6 address X:X::X:X/M",
e23949c0 2045 "Interface IPv6 config commands\n"
718e3744 2046 "Set the IP address of an interface\n"
2047 "IPv6 address (e.g. 3ffe:506::1/48)\n")
2048{
2049 return ipv6_address_install (vty, vty->index, argv[0], NULL, NULL, 0);
2050}
2051
2052DEFUN (no_ipv6_address,
2053 no_ipv6_address_cmd,
2054 "no ipv6 address X:X::X:X/M",
2055 NO_STR
e23949c0 2056 "Interface IPv6 config commands\n"
718e3744 2057 "Set the IP address of an interface\n"
2058 "IPv6 address (e.g. 3ffe:506::1/48)\n")
2059{
2060 return ipv6_address_uninstall (vty, vty->index, argv[0], NULL, NULL, 0);
2061}
2062#endif /* HAVE_IPV6 */
2063
a1ac18c4 2064static int
718e3744 2065if_config_write (struct vty *vty)
2066{
52dc7ee6 2067 struct listnode *node;
718e3744 2068 struct interface *ifp;
cd2a8a42 2069 vrf_iter_t iter;
718e3744 2070
244c1cdc
DS
2071 zebra_ptm_write (vty);
2072
cd2a8a42
FL
2073 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
2074 for (ALL_LIST_ELEMENTS_RO (vrf_iter2iflist (iter), node, ifp))
718e3744 2075 {
2076 struct zebra_if *if_data;
52dc7ee6 2077 struct listnode *addrnode;
718e3744 2078 struct connected *ifc;
2079 struct prefix *p;
12f6fb97 2080 struct vrf *vrf;
718e3744 2081
718e3744 2082 if_data = ifp->info;
12f6fb97 2083 vrf = vrf_lookup(ifp->vrf_id);
cd2a8a42
FL
2084
2085 if (ifp->vrf_id == VRF_DEFAULT)
2086 vty_out (vty, "interface %s%s", ifp->name, VTY_NEWLINE);
2087 else
12f6fb97 2088 vty_out (vty, "interface %s vrf %s%s", ifp->name, vrf->name,
cd2a8a42 2089 VTY_NEWLINE);
718e3744 2090
bfac8dcd
CF
2091 if (if_data)
2092 {
2093 if (if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)
2094 vty_out (vty, " shutdown%s", VTY_NEWLINE);
986aa00f 2095
2096 zebra_ptm_if_write(vty, if_data);
bfac8dcd
CF
2097 }
2098
718e3744 2099 if (ifp->desc)
2100 vty_out (vty, " description %s%s", ifp->desc,
2101 VTY_NEWLINE);
2102
2103 /* Assign bandwidth here to avoid unnecessary interface flap
2104 while processing config script */
2105 if (ifp->bandwidth != 0)
2106 vty_out(vty, " bandwidth %u%s", ifp->bandwidth, VTY_NEWLINE);
2107
2e3b2e47 2108 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION))
2109 vty_out(vty, " link-detect%s", VTY_NEWLINE);
b71cd0fc
DL
2110 else
2111 vty_out(vty, " no link-detect%s", VTY_NEWLINE);
2e3b2e47 2112
1eb8ef25 2113 for (ALL_LIST_ELEMENTS_RO (ifp->connected, addrnode, ifc))
718e3744 2114 {
718e3744 2115 if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
2116 {
81cce018 2117 char buf[INET6_ADDRSTRLEN];
718e3744 2118 p = ifc->address;
2119 vty_out (vty, " ip%s address %s/%d",
2120 p->family == AF_INET ? "" : "v6",
81cce018 2121 inet_ntop (p->family, &p->u.prefix, buf, sizeof(buf)),
718e3744 2122 p->prefixlen);
2123
718e3744 2124 if (ifc->label)
2125 vty_out (vty, " label %s", ifc->label);
2126
2127 vty_out (vty, "%s", VTY_NEWLINE);
2128 }
2129 }
2130
2131 if (if_data)
2132 {
718e3744 2133 if (if_data->multicast != IF_ZEBRA_MULTICAST_UNSPEC)
2134 vty_out (vty, " %smulticast%s",
2135 if_data->multicast == IF_ZEBRA_MULTICAST_ON ? "" : "no ",
2136 VTY_NEWLINE);
2137 }
2138
8da4e946 2139#if defined (HAVE_RTADV)
718e3744 2140 rtadv_config_write (vty, ifp);
8da4e946 2141#endif /* HAVE_RTADV */
718e3744 2142
ca776988 2143#ifdef HAVE_IRDP
2144 irdp_config_write (vty, ifp);
2145#endif /* IRDP */
2146
718e3744 2147 vty_out (vty, "!%s", VTY_NEWLINE);
2148 }
2149 return 0;
2150}
2151
12f6fb97
DS
2152static int
2153vrf_config_write (struct vty *vty)
2154{
2155 struct listnode *node;
c1c747a8 2156 struct zebra_vrf *zvrf;
12f6fb97 2157
c1c747a8 2158 for (ALL_LIST_ELEMENTS_RO (zvrf_list, node, zvrf))
12f6fb97 2159 {
c1c747a8 2160 if (strcmp(zvrf->name, VRF_DEFAULT_NAME))
12f6fb97 2161 {
c1c747a8 2162 vty_out (vty, "vrf %s%s", zvrf->name, VTY_NEWLINE);
12f6fb97
DS
2163 vty_out (vty, "!%s", VTY_NEWLINE);
2164 }
2165 }
2166 return 0;
2167}
2168
718e3744 2169/* Allocate and initialize interface vector. */
2170void
a1ac18c4 2171zebra_if_init (void)
718e3744 2172{
2173 /* Initialize interface and new hook. */
718e3744 2174 if_add_hook (IF_NEW_HOOK, if_zebra_new_hook);
2175 if_add_hook (IF_DELETE_HOOK, if_zebra_delete_hook);
2176
2177 /* Install configuration write function. */
2178 install_node (&interface_node, if_config_write);
12f6fb97 2179 install_node (&vrf_node, vrf_config_write);
718e3744 2180
2181 install_element (VIEW_NODE, &show_interface_cmd);
8b87bdf4
FL
2182 install_element (VIEW_NODE, &show_interface_vrf_cmd);
2183 install_element (VIEW_NODE, &show_interface_vrf_all_cmd);
2184 install_element (VIEW_NODE, &show_interface_name_cmd);
2185 install_element (VIEW_NODE, &show_interface_name_vrf_cmd);
2186 install_element (VIEW_NODE, &show_interface_name_vrf_all_cmd);
718e3744 2187 install_element (ENABLE_NODE, &show_interface_cmd);
8b87bdf4
FL
2188 install_element (ENABLE_NODE, &show_interface_vrf_cmd);
2189 install_element (ENABLE_NODE, &show_interface_vrf_all_cmd);
2190 install_element (ENABLE_NODE, &show_interface_name_cmd);
2191 install_element (ENABLE_NODE, &show_interface_name_vrf_cmd);
2192 install_element (ENABLE_NODE, &show_interface_name_vrf_all_cmd);
ed9bb6d5 2193 install_element (ENABLE_NODE, &show_interface_desc_cmd);
8b87bdf4
FL
2194 install_element (ENABLE_NODE, &show_interface_desc_vrf_cmd);
2195 install_element (ENABLE_NODE, &show_interface_desc_vrf_all_cmd);
718e3744 2196 install_element (CONFIG_NODE, &zebra_interface_cmd);
cd2a8a42 2197 install_element (CONFIG_NODE, &zebra_interface_vrf_cmd);
bfc13532 2198 install_element (CONFIG_NODE, &no_interface_cmd);
cd2a8a42 2199 install_element (CONFIG_NODE, &no_interface_vrf_cmd);
718e3744 2200 install_default (INTERFACE_NODE);
2201 install_element (INTERFACE_NODE, &interface_desc_cmd);
2202 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
2203 install_element (INTERFACE_NODE, &multicast_cmd);
2204 install_element (INTERFACE_NODE, &no_multicast_cmd);
2e3b2e47 2205 install_element (INTERFACE_NODE, &linkdetect_cmd);
2206 install_element (INTERFACE_NODE, &no_linkdetect_cmd);
718e3744 2207 install_element (INTERFACE_NODE, &shutdown_if_cmd);
2208 install_element (INTERFACE_NODE, &no_shutdown_if_cmd);
2209 install_element (INTERFACE_NODE, &bandwidth_if_cmd);
2210 install_element (INTERFACE_NODE, &no_bandwidth_if_cmd);
2211 install_element (INTERFACE_NODE, &no_bandwidth_if_val_cmd);
2212 install_element (INTERFACE_NODE, &ip_address_cmd);
2213 install_element (INTERFACE_NODE, &no_ip_address_cmd);
2214#ifdef HAVE_IPV6
2215 install_element (INTERFACE_NODE, &ipv6_address_cmd);
2216 install_element (INTERFACE_NODE, &no_ipv6_address_cmd);
2217#endif /* HAVE_IPV6 */
718e3744 2218#ifdef HAVE_NETLINK
718e3744 2219 install_element (INTERFACE_NODE, &ip_address_label_cmd);
718e3744 2220 install_element (INTERFACE_NODE, &no_ip_address_label_cmd);
2221#endif /* HAVE_NETLINK */
12f6fb97
DS
2222
2223 install_element (CONFIG_NODE, &zebra_vrf_cmd);
2224 install_element (CONFIG_NODE, &no_vrf_cmd);
2225 install_default (VRF_NODE);
2226
718e3744 2227}