]> git.proxmox.com Git - mirror_frr.git/blob - zebra/interface.c
This patch adds support for a new BFD session down message from zebra to
[mirror_frr.git] / zebra / interface.c
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"
35
36 #include "zebra/interface.h"
37 #include "zebra/rtadv.h"
38 #include "zebra/rib.h"
39 #include "zebra/zserv.h"
40 #include "zebra/redistribute.h"
41 #include "zebra/debug.h"
42 #include "zebra/irdp.h"
43 #include "zebra/zebra_ptm.h"
44
45 #define ZEBRA_PTM_SUPPORT
46
47 #ifdef RTADV
48 /* Order is intentional. Matches RFC4191. This array is also used for
49 command matching, so only modify with care. */
50 const char *rtadv_pref_strs[] = { "medium", "high", "INVALID", "low", 0 };
51 #endif /* RTADV */
52
53 /* Called when new interface is added. */
54 static int
55 if_zebra_new_hook (struct interface *ifp)
56 {
57 struct zebra_if *zebra_if;
58
59 zebra_if = XCALLOC (MTYPE_TMP, sizeof (struct zebra_if));
60
61 zebra_if->multicast = IF_ZEBRA_MULTICAST_UNSPEC;
62 zebra_if->shutdown = IF_ZEBRA_SHUTDOWN_OFF;
63
64 ifp->ptm_enable = ptm_enable;
65 #ifdef RTADV
66 {
67 /* Set default router advertise values. */
68 struct rtadvconf *rtadv;
69
70 rtadv = &zebra_if->rtadv;
71
72 rtadv->AdvSendAdvertisements = 0;
73 rtadv->MaxRtrAdvInterval = RTADV_MAX_RTR_ADV_INTERVAL;
74 rtadv->MinRtrAdvInterval = RTADV_MIN_RTR_ADV_INTERVAL;
75 rtadv->AdvIntervalTimer = 0;
76 rtadv->AdvManagedFlag = 0;
77 rtadv->AdvOtherConfigFlag = 0;
78 rtadv->AdvHomeAgentFlag = 0;
79 rtadv->AdvLinkMTU = 0;
80 rtadv->AdvReachableTime = 0;
81 rtadv->AdvRetransTimer = 0;
82 rtadv->AdvCurHopLimit = 0;
83 rtadv->AdvDefaultLifetime = -1; /* derive from MaxRtrAdvInterval */
84 rtadv->HomeAgentPreference = 0;
85 rtadv->HomeAgentLifetime = -1; /* derive from AdvDefaultLifetime */
86 rtadv->AdvIntervalOption = 0;
87 rtadv->DefaultPreference = RTADV_PREF_MEDIUM;
88
89 rtadv->AdvPrefixList = list_new ();
90 }
91 #endif /* RTADV */
92
93 /* Initialize installed address chains tree. */
94 zebra_if->ipv4_subnets = route_table_init ();
95
96 ifp->info = zebra_if;
97 return 0;
98 }
99
100 /* Called when interface is deleted. */
101 static int
102 if_zebra_delete_hook (struct interface *ifp)
103 {
104 struct zebra_if *zebra_if;
105
106 if (ifp->info)
107 {
108 zebra_if = ifp->info;
109
110 /* Free installed address chains tree. */
111 if (zebra_if->ipv4_subnets)
112 route_table_finish (zebra_if->ipv4_subnets);
113
114 XFREE (MTYPE_TMP, zebra_if);
115 }
116
117 return 0;
118 }
119
120 /* Tie an interface address to its derived subnet list of addresses. */
121 int
122 if_subnet_add (struct interface *ifp, struct connected *ifc)
123 {
124 struct route_node *rn;
125 struct zebra_if *zebra_if;
126 struct prefix cp;
127 struct list *addr_list;
128
129 assert (ifp && ifp->info && ifc);
130 zebra_if = ifp->info;
131
132 /* Get address derived subnet node and associated address list, while marking
133 address secondary attribute appropriately. */
134 cp = *ifc->address;
135 apply_mask (&cp);
136 rn = route_node_get (zebra_if->ipv4_subnets, &cp);
137
138 if ((addr_list = rn->info))
139 SET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
140 else
141 {
142 UNSET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
143 rn->info = addr_list = list_new ();
144 route_lock_node (rn);
145 }
146
147 /* Tie address at the tail of address list. */
148 listnode_add (addr_list, ifc);
149
150 /* Return list element count. */
151 return (addr_list->count);
152 }
153
154 /* Untie an interface address from its derived subnet list of addresses. */
155 int
156 if_subnet_delete (struct interface *ifp, struct connected *ifc)
157 {
158 struct route_node *rn;
159 struct zebra_if *zebra_if;
160 struct list *addr_list;
161
162 assert (ifp && ifp->info && ifc);
163 zebra_if = ifp->info;
164
165 /* Get address derived subnet node. */
166 rn = route_node_lookup (zebra_if->ipv4_subnets, ifc->address);
167 if (! (rn && rn->info))
168 {
169 zlog_warn("Trying to remove an address from an unknown subnet."
170 " (please report this bug)");
171 return -1;
172 }
173 route_unlock_node (rn);
174
175 /* Untie address from subnet's address list. */
176 addr_list = rn->info;
177
178 /* Deleting an address that is not registered is a bug.
179 * In any case, we shouldn't decrement the lock counter if the address
180 * is unknown. */
181 if (!listnode_lookup(addr_list, ifc))
182 {
183 zlog_warn("Trying to remove an address from a subnet where it is not"
184 " currently registered. (please report this bug)");
185 return -1;
186 }
187
188 listnode_delete (addr_list, ifc);
189 route_unlock_node (rn);
190
191 /* Return list element count, if not empty. */
192 if (addr_list->count)
193 {
194 /* If deleted address is primary, mark subsequent one as such and distribute. */
195 if (! CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY))
196 {
197 ifc = listgetdata (listhead (addr_list));
198 zebra_interface_address_delete_update (ifp, ifc);
199 UNSET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
200 /* XXX: Linux kernel removes all the secondary addresses when the primary
201 * address is removed. We could try to work around that, though this is
202 * non-trivial. */
203 zebra_interface_address_add_update (ifp, ifc);
204 }
205
206 return addr_list->count;
207 }
208
209 /* Otherwise, free list and route node. */
210 list_free (addr_list);
211 rn->info = NULL;
212 route_unlock_node (rn);
213
214 return 0;
215 }
216
217 /* if_flags_mangle: A place for hacks that require mangling
218 * or tweaking the interface flags.
219 *
220 * ******************** Solaris flags hacks **************************
221 *
222 * Solaris IFF_UP flag reflects only the primary interface as the
223 * routing socket only sends IFINFO for the primary interface. Hence
224 * ~IFF_UP does not per se imply all the logical interfaces are also
225 * down - which we only know of as addresses. Instead we must determine
226 * whether the interface really is up or not according to how many
227 * addresses are still attached. (Solaris always sends RTM_DELADDR if
228 * an interface, logical or not, goes ~IFF_UP).
229 *
230 * Ie, we mangle IFF_UP to *additionally* reflect whether or not there
231 * are addresses left in struct connected, not just the actual underlying
232 * IFF_UP flag.
233 *
234 * We must hence remember the real state of IFF_UP, which we do in
235 * struct zebra_if.primary_state.
236 *
237 * Setting IFF_UP within zebra to administratively shutdown the
238 * interface will affect only the primary interface/address on Solaris.
239 ************************End Solaris flags hacks ***********************
240 */
241 static void
242 if_flags_mangle (struct interface *ifp, uint64_t *newflags)
243 {
244 #ifdef SUNOS_5
245 struct zebra_if *zif = ifp->info;
246
247 zif->primary_state = *newflags & (IFF_UP & 0xff);
248
249 if (CHECK_FLAG (zif->primary_state, IFF_UP)
250 || listcount(ifp->connected) > 0)
251 SET_FLAG (*newflags, IFF_UP);
252 else
253 UNSET_FLAG (*newflags, IFF_UP);
254 #endif /* SUNOS_5 */
255 }
256
257 /* Update the flags field of the ifp with the new flag set provided.
258 * Take whatever actions are required for any changes in flags we care
259 * about.
260 *
261 * newflags should be the raw value, as obtained from the OS.
262 */
263 void
264 if_flags_update (struct interface *ifp, uint64_t newflags)
265 {
266 if_flags_mangle (ifp, &newflags);
267
268 if (if_is_no_ptm_operative (ifp))
269 {
270 /* operative -> inoperative? */
271 ifp->flags = newflags;
272 if (!if_is_operative (ifp))
273 if_down (ifp);
274 }
275 else
276 {
277 /* inoperative -> operative? */
278 ifp->flags = newflags;
279 if (if_is_operative (ifp))
280 if_up (ifp);
281 }
282 }
283
284 /* Wake up configured address if it is not in current kernel
285 address. */
286 static void
287 if_addr_wakeup (struct interface *ifp)
288 {
289 struct listnode *node, *nnode;
290 struct connected *ifc;
291 struct prefix *p;
292 int ret;
293
294 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, ifc))
295 {
296 p = ifc->address;
297
298 if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED)
299 && ! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED))
300 {
301 /* Address check. */
302 if (p->family == AF_INET)
303 {
304 if (! if_is_up (ifp))
305 {
306 /* Assume zebra is configured like following:
307 *
308 * interface gre0
309 * ip addr 192.0.2.1/24
310 * !
311 *
312 * As soon as zebra becomes first aware that gre0 exists in the
313 * kernel, it will set gre0 up and configure its addresses.
314 *
315 * (This may happen at startup when the interface already exists
316 * or during runtime when the interface is added to the kernel)
317 *
318 * XXX: IRDP code is calling here via if_add_update - this seems
319 * somewhat weird.
320 * XXX: RUNNING is not a settable flag on any system
321 * I (paulj) am aware of.
322 */
323 if_set_flags (ifp, IFF_UP | IFF_RUNNING);
324 if_refresh (ifp);
325 }
326
327 ret = if_set_prefix (ifp, ifc);
328 if (ret < 0)
329 {
330 zlog_warn ("Can't set interface's address: %s",
331 safe_strerror(errno));
332 continue;
333 }
334
335 SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
336 /* The address will be advertised to zebra clients when the notification
337 * from the kernel has been received.
338 * It will also be added to the interface's subnet list then. */
339 }
340 #ifdef HAVE_IPV6
341 if (p->family == AF_INET6)
342 {
343 if (! if_is_up (ifp))
344 {
345 /* See long comment above */
346 if_set_flags (ifp, IFF_UP | IFF_RUNNING);
347 if_refresh (ifp);
348 }
349
350 ret = if_prefix_add_ipv6 (ifp, ifc);
351 if (ret < 0)
352 {
353 zlog_warn ("Can't set interface's address: %s",
354 safe_strerror(errno));
355 continue;
356 }
357
358 SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
359 /* The address will be advertised to zebra clients when the notification
360 * from the kernel has been received. */
361 }
362 #endif /* HAVE_IPV6 */
363 }
364 }
365 }
366
367 /* Handle interface addition */
368 void
369 if_add_update (struct interface *ifp)
370 {
371 struct zebra_if *if_data;
372
373 if_data = ifp->info;
374 if (if_data->multicast == IF_ZEBRA_MULTICAST_ON)
375 if_set_flags (ifp, IFF_MULTICAST);
376 else if (if_data->multicast == IF_ZEBRA_MULTICAST_OFF)
377 if_unset_flags (ifp, IFF_MULTICAST);
378
379 zebra_interface_add_update (ifp);
380
381 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
382 {
383 SET_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE);
384
385 if (if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)
386 {
387 if (IS_ZEBRA_DEBUG_KERNEL)
388 zlog_debug ("interface %s index %d is shutdown. Won't wake it up.",
389 ifp->name, ifp->ifindex);
390 return;
391 }
392
393 if_addr_wakeup (ifp);
394
395 if (IS_ZEBRA_DEBUG_KERNEL)
396 zlog_debug ("interface %s index %d becomes active.",
397 ifp->name, ifp->ifindex);
398 }
399 else
400 {
401 if (IS_ZEBRA_DEBUG_KERNEL)
402 zlog_debug ("interface %s index %d is added.", ifp->name, ifp->ifindex);
403 }
404 }
405
406 /* Handle an interface delete event */
407 void
408 if_delete_update (struct interface *ifp)
409 {
410 struct connected *ifc;
411 struct prefix *p;
412 struct route_node *rn;
413 struct zebra_if *zebra_if;
414
415 zebra_if = ifp->info;
416
417 if (if_is_up(ifp))
418 {
419 zlog_err ("interface %s index %d is still up while being deleted.",
420 ifp->name, ifp->ifindex);
421 return;
422 }
423
424 /* Mark interface as inactive */
425 UNSET_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE);
426
427 if (IS_ZEBRA_DEBUG_KERNEL)
428 zlog_debug ("interface %s index %d is now inactive.",
429 ifp->name, ifp->ifindex);
430
431 /* Delete connected routes from the kernel. */
432 if (ifp->connected)
433 {
434 struct listnode *node;
435 struct listnode *last = NULL;
436
437 while ((node = (last ? last->next : listhead (ifp->connected))))
438 {
439 ifc = listgetdata (node);
440 p = ifc->address;
441
442 if (p->family == AF_INET
443 && (rn = route_node_lookup (zebra_if->ipv4_subnets, p)))
444 {
445 struct listnode *anode;
446 struct listnode *next;
447 struct listnode *first;
448 struct list *addr_list;
449
450 route_unlock_node (rn);
451 addr_list = (struct list *) rn->info;
452
453 /* Remove addresses, secondaries first. */
454 first = listhead (addr_list);
455 for (anode = first->next; anode || first; anode = next)
456 {
457 if (!anode)
458 {
459 anode = first;
460 first = NULL;
461 }
462 next = anode->next;
463
464 ifc = listgetdata (anode);
465 p = ifc->address;
466 connected_down_ipv4 (ifp, ifc);
467
468 /* XXX: We have to send notifications here explicitly, because we destroy
469 * the ifc before receiving the notification about the address being deleted.
470 */
471 zebra_interface_address_delete_update (ifp, ifc);
472
473 UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
474 UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
475
476 /* Remove from subnet chain. */
477 list_delete_node (addr_list, anode);
478 route_unlock_node (rn);
479
480 /* Remove from interface address list (unconditionally). */
481 if (!CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
482 {
483 listnode_delete (ifp->connected, ifc);
484 connected_free (ifc);
485 }
486 else
487 last = node;
488 }
489
490 /* Free chain list and respective route node. */
491 list_delete (addr_list);
492 rn->info = NULL;
493 route_unlock_node (rn);
494 }
495 #ifdef HAVE_IPV6
496 else if (p->family == AF_INET6)
497 {
498 connected_down_ipv6 (ifp, ifc);
499
500 zebra_interface_address_delete_update (ifp, ifc);
501
502 UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
503 UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
504
505 if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
506 last = node;
507 else
508 {
509 listnode_delete (ifp->connected, ifc);
510 connected_free (ifc);
511 }
512 }
513 #endif /* HAVE_IPV6 */
514 else
515 {
516 last = node;
517 }
518 }
519 }
520 zebra_interface_delete_update (ifp);
521
522 /* Update ifindex after distributing the delete message. This is in
523 case any client needs to have the old value of ifindex available
524 while processing the deletion. Each client daemon is responsible
525 for setting ifindex to IFINDEX_INTERNAL after processing the
526 interface deletion message. */
527 ifp->ifindex = IFINDEX_INTERNAL;
528 }
529
530 /* Interface is up. */
531 void
532 if_up (struct interface *ifp)
533 {
534 struct listnode *node;
535 struct listnode *next;
536 struct connected *ifc;
537 struct prefix *p;
538
539 /* Notify the protocol daemons. */
540 if (ifp->ptm_enable && !ifp->ptm_status) {
541 zlog_warn("%s: interface %s hasn't passed ptm check\n", __func__,
542 ifp->name);
543 return;
544 }
545 zebra_interface_up_update (ifp);
546
547
548 /* Install connected routes to the kernel. */
549 if (ifp->connected)
550 {
551 for (ALL_LIST_ELEMENTS (ifp->connected, node, next, ifc))
552 {
553 p = ifc->address;
554
555 if (p->family == AF_INET)
556 connected_up_ipv4 (ifp, ifc);
557 #ifdef HAVE_IPV6
558 else if (p->family == AF_INET6)
559 connected_up_ipv6 (ifp, ifc);
560 #endif /* HAVE_IPV6 */
561 }
562 }
563
564 /* Examine all static routes. */
565 rib_update ();
566 }
567
568 /* Interface goes down. We have to manage different behavior of based
569 OS. */
570 void
571 if_down (struct interface *ifp)
572 {
573 struct listnode *node;
574 struct listnode *next;
575 struct connected *ifc;
576 struct prefix *p;
577
578 /* Notify to the protocol daemons. */
579 zebra_interface_down_update (ifp);
580
581 /* Delete connected routes from the kernel. */
582 if (ifp->connected)
583 {
584 for (ALL_LIST_ELEMENTS (ifp->connected, node, next, ifc))
585 {
586 p = ifc->address;
587
588 if (p->family == AF_INET)
589 connected_down_ipv4 (ifp, ifc);
590 #ifdef HAVE_IPV6
591 else if (p->family == AF_INET6)
592 connected_down_ipv6 (ifp, ifc);
593 #endif /* HAVE_IPV6 */
594 }
595 }
596
597 /* Examine all static routes which direct to the interface. */
598 rib_update ();
599 }
600
601 void
602 if_refresh (struct interface *ifp)
603 {
604 if_get_flags (ifp);
605 }
606
607 /* BFD session goes down, send message to the protocols. */
608 void
609 if_bfd_session_down (struct interface *ifp, struct prefix *p)
610 {
611 if (IS_ZEBRA_DEBUG_EVENT)
612 {
613 char buf[INET6_ADDRSTRLEN];
614
615 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_BFD_DEST_DOWN %s/%d on %s",
616 inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN),
617 p->prefixlen, ifp->name);
618 }
619
620 zebra_interface_bfd_update (ifp, p);
621 }
622
623
624 /* Output prefix string to vty. */
625 static int
626 prefix_vty_out (struct vty *vty, struct prefix *p)
627 {
628 char str[INET6_ADDRSTRLEN];
629
630 inet_ntop (p->family, &p->u.prefix, str, sizeof (str));
631 vty_out (vty, "%s", str);
632 return strlen (str);
633 }
634
635 /* Dump if address information to vty. */
636 static void
637 connected_dump_vty (struct vty *vty, struct connected *connected)
638 {
639 struct prefix *p;
640
641 /* Print interface address. */
642 p = connected->address;
643 vty_out (vty, " %s ", prefix_family_str (p));
644 prefix_vty_out (vty, p);
645 vty_out (vty, "/%d", p->prefixlen);
646
647 /* If there is destination address, print it. */
648 if (connected->destination)
649 {
650 vty_out (vty, (CONNECTED_PEER(connected) ? " peer " : " broadcast "));
651 prefix_vty_out (vty, connected->destination);
652 }
653
654 if (CHECK_FLAG (connected->flags, ZEBRA_IFA_SECONDARY))
655 vty_out (vty, " secondary");
656
657 if (connected->label)
658 vty_out (vty, " %s", connected->label);
659
660 vty_out (vty, "%s", VTY_NEWLINE);
661 }
662
663 /* Dump interface neighbor address information to vty. */
664 static void
665 nbr_connected_dump_vty (struct vty *vty, struct nbr_connected *connected)
666 {
667 struct prefix *p;
668
669 /* Print interface address. */
670 p = connected->address;
671 vty_out (vty, " %s ", prefix_family_str (p));
672 prefix_vty_out (vty, p);
673 vty_out (vty, "/%d", p->prefixlen);
674
675 vty_out (vty, "%s", VTY_NEWLINE);
676 }
677
678 #ifdef RTADV
679 /* Dump interface ND information to vty. */
680 static void
681 nd_dump_vty (struct vty *vty, struct interface *ifp)
682 {
683 struct zebra_if *zif;
684 struct rtadvconf *rtadv;
685 int interval;
686
687 zif = (struct zebra_if *) ifp->info;
688 rtadv = &zif->rtadv;
689
690 if (rtadv->AdvSendAdvertisements)
691 {
692 vty_out (vty, " ND advertised reachable time is %d milliseconds%s",
693 rtadv->AdvReachableTime, VTY_NEWLINE);
694 vty_out (vty, " ND advertised retransmit interval is %d milliseconds%s",
695 rtadv->AdvRetransTimer, VTY_NEWLINE);
696 interval = rtadv->MaxRtrAdvInterval;
697 if (interval % 1000)
698 vty_out (vty, " ND router advertisements are sent every "
699 "%d milliseconds%s", interval,
700 VTY_NEWLINE);
701 else
702 vty_out (vty, " ND router advertisements are sent every "
703 "%d seconds%s", interval / 1000,
704 VTY_NEWLINE);
705 if (rtadv->AdvDefaultLifetime != -1)
706 vty_out (vty, " ND router advertisements live for %d seconds%s",
707 rtadv->AdvDefaultLifetime, VTY_NEWLINE);
708 else
709 vty_out (vty, " ND router advertisements lifetime tracks ra-interval%s",
710 VTY_NEWLINE);
711 vty_out (vty, " ND router advertisement default router preference is "
712 "%s%s", rtadv_pref_strs[rtadv->DefaultPreference],
713 VTY_NEWLINE);
714 if (rtadv->AdvManagedFlag)
715 vty_out (vty, " Hosts use DHCP to obtain routable addresses.%s",
716 VTY_NEWLINE);
717 else
718 vty_out (vty, " Hosts use stateless autoconfig for addresses.%s",
719 VTY_NEWLINE);
720 if (rtadv->AdvHomeAgentFlag)
721 {
722 vty_out (vty, " ND router advertisements with "
723 "Home Agent flag bit set.%s",
724 VTY_NEWLINE);
725 if (rtadv->HomeAgentLifetime != -1)
726 vty_out (vty, " Home Agent lifetime is %u seconds%s",
727 rtadv->HomeAgentLifetime, VTY_NEWLINE);
728 else
729 vty_out (vty, " Home Agent lifetime tracks ra-lifetime%s",
730 VTY_NEWLINE);
731 vty_out (vty, " Home Agent preference is %u%s",
732 rtadv->HomeAgentPreference, VTY_NEWLINE);
733 }
734 if (rtadv->AdvIntervalOption)
735 vty_out (vty, " ND router advertisements with Adv. Interval option.%s",
736 VTY_NEWLINE);
737 }
738 }
739 #endif /* RTADV */
740
741 /* Interface's information print out to vty interface. */
742 static void
743 if_dump_vty (struct vty *vty, struct interface *ifp)
744 {
745 #ifdef HAVE_STRUCT_SOCKADDR_DL
746 struct sockaddr_dl *sdl;
747 #endif /* HAVE_STRUCT_SOCKADDR_DL */
748 struct connected *connected;
749 struct nbr_connected *nbr_connected;
750 struct listnode *node;
751 struct route_node *rn;
752 struct zebra_if *zebra_if;
753
754 zebra_if = ifp->info;
755
756 vty_out (vty, "Interface %s is ", ifp->name);
757 if (if_is_up(ifp)) {
758 vty_out (vty, "up, line protocol ");
759
760 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)) {
761 if (if_is_running(ifp))
762 vty_out (vty, "is up%s", VTY_NEWLINE);
763 else
764 vty_out (vty, "is down%s", VTY_NEWLINE);
765 } else {
766 vty_out (vty, "detection is disabled%s", VTY_NEWLINE);
767 }
768 } else {
769 vty_out (vty, "down%s", VTY_NEWLINE);
770 }
771
772 vty_out (vty, " PTM status: ");
773 if (ifp->ptm_enable) {
774 if (ifp->ptm_status) {
775 vty_out (vty, "pass%s", VTY_NEWLINE);
776 } else {
777 vty_out (vty, "fail%s", VTY_NEWLINE);
778 }
779 } else {
780 vty_out (vty, "disabled%s", VTY_NEWLINE);
781 }
782
783 if (ifp->desc)
784 vty_out (vty, " Description: %s%s", ifp->desc,
785 VTY_NEWLINE);
786 if (ifp->ifindex == IFINDEX_INTERNAL)
787 {
788 vty_out(vty, " pseudo interface%s", VTY_NEWLINE);
789 return;
790 }
791 else if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
792 {
793 vty_out(vty, " index %d inactive interface%s",
794 ifp->ifindex,
795 VTY_NEWLINE);
796 return;
797 }
798
799 vty_out (vty, " index %d metric %d mtu %d ",
800 ifp->ifindex, ifp->metric, ifp->mtu);
801 #ifdef HAVE_IPV6
802 if (ifp->mtu6 != ifp->mtu)
803 vty_out (vty, "mtu6 %d ", ifp->mtu6);
804 #endif
805 vty_out (vty, "%s flags: %s%s", VTY_NEWLINE,
806 if_flag_dump (ifp->flags), VTY_NEWLINE);
807
808 /* Hardware address. */
809 #ifdef HAVE_STRUCT_SOCKADDR_DL
810 sdl = &ifp->sdl;
811 if (sdl != NULL && sdl->sdl_alen != 0)
812 {
813 int i;
814 u_char *ptr;
815
816 vty_out (vty, " HWaddr: ");
817 for (i = 0, ptr = (u_char *)LLADDR (sdl); i < sdl->sdl_alen; i++, ptr++)
818 vty_out (vty, "%s%02x", i == 0 ? "" : ":", *ptr);
819 vty_out (vty, "%s", VTY_NEWLINE);
820 }
821 #else
822 if (ifp->hw_addr_len != 0)
823 {
824 int i;
825
826 vty_out (vty, " HWaddr: ");
827 for (i = 0; i < ifp->hw_addr_len; i++)
828 vty_out (vty, "%s%02x", i == 0 ? "" : ":", ifp->hw_addr[i]);
829 vty_out (vty, "%s", VTY_NEWLINE);
830 }
831 #endif /* HAVE_STRUCT_SOCKADDR_DL */
832
833 /* Bandwidth in kbps */
834 if (ifp->bandwidth != 0)
835 {
836 vty_out(vty, " bandwidth %u kbps", ifp->bandwidth);
837 vty_out(vty, "%s", VTY_NEWLINE);
838 }
839
840 for (rn = route_top (zebra_if->ipv4_subnets); rn; rn = route_next (rn))
841 {
842 if (! rn->info)
843 continue;
844
845 for (ALL_LIST_ELEMENTS_RO ((struct list *)rn->info, node, connected))
846 connected_dump_vty (vty, connected);
847 }
848
849 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
850 {
851 if (CHECK_FLAG (connected->conf, ZEBRA_IFC_REAL) &&
852 (connected->address->family == AF_INET6))
853 connected_dump_vty (vty, connected);
854 }
855
856 #ifdef RTADV
857 nd_dump_vty (vty, ifp);
858 #endif /* RTADV */
859 if (listhead(ifp->nbr_connected))
860 vty_out (vty, " Neighbor address(s):%s", VTY_NEWLINE);
861 for (ALL_LIST_ELEMENTS_RO (ifp->nbr_connected, node, nbr_connected))
862 nbr_connected_dump_vty (vty, nbr_connected);
863
864 #ifdef HAVE_PROC_NET_DEV
865 /* Statistics print out using proc file system. */
866 vty_out (vty, " %lu input packets (%lu multicast), %lu bytes, "
867 "%lu dropped%s",
868 ifp->stats.rx_packets, ifp->stats.rx_multicast,
869 ifp->stats.rx_bytes, ifp->stats.rx_dropped, VTY_NEWLINE);
870
871 vty_out (vty, " %lu input errors, %lu length, %lu overrun,"
872 " %lu CRC, %lu frame%s",
873 ifp->stats.rx_errors, ifp->stats.rx_length_errors,
874 ifp->stats.rx_over_errors, ifp->stats.rx_crc_errors,
875 ifp->stats.rx_frame_errors, VTY_NEWLINE);
876
877 vty_out (vty, " %lu fifo, %lu missed%s", ifp->stats.rx_fifo_errors,
878 ifp->stats.rx_missed_errors, VTY_NEWLINE);
879
880 vty_out (vty, " %lu output packets, %lu bytes, %lu dropped%s",
881 ifp->stats.tx_packets, ifp->stats.tx_bytes,
882 ifp->stats.tx_dropped, VTY_NEWLINE);
883
884 vty_out (vty, " %lu output errors, %lu aborted, %lu carrier,"
885 " %lu fifo, %lu heartbeat%s",
886 ifp->stats.tx_errors, ifp->stats.tx_aborted_errors,
887 ifp->stats.tx_carrier_errors, ifp->stats.tx_fifo_errors,
888 ifp->stats.tx_heartbeat_errors, VTY_NEWLINE);
889
890 vty_out (vty, " %lu window, %lu collisions%s",
891 ifp->stats.tx_window_errors, ifp->stats.collisions, VTY_NEWLINE);
892 #endif /* HAVE_PROC_NET_DEV */
893
894 #ifdef HAVE_NET_RT_IFLIST
895 #if defined (__bsdi__) || defined (__NetBSD__)
896 /* Statistics print out using sysctl (). */
897 vty_out (vty, " input packets %qu, bytes %qu, dropped %qu,"
898 " multicast packets %qu%s",
899 ifp->stats.ifi_ipackets, ifp->stats.ifi_ibytes,
900 ifp->stats.ifi_iqdrops, ifp->stats.ifi_imcasts,
901 VTY_NEWLINE);
902
903 vty_out (vty, " input errors %qu%s",
904 ifp->stats.ifi_ierrors, VTY_NEWLINE);
905
906 vty_out (vty, " output packets %qu, bytes %qu, multicast packets %qu%s",
907 ifp->stats.ifi_opackets, ifp->stats.ifi_obytes,
908 ifp->stats.ifi_omcasts, VTY_NEWLINE);
909
910 vty_out (vty, " output errors %qu%s",
911 ifp->stats.ifi_oerrors, VTY_NEWLINE);
912
913 vty_out (vty, " collisions %qu%s",
914 ifp->stats.ifi_collisions, VTY_NEWLINE);
915 #else
916 /* Statistics print out using sysctl (). */
917 vty_out (vty, " input packets %lu, bytes %lu, dropped %lu,"
918 " multicast packets %lu%s",
919 ifp->stats.ifi_ipackets, ifp->stats.ifi_ibytes,
920 ifp->stats.ifi_iqdrops, ifp->stats.ifi_imcasts,
921 VTY_NEWLINE);
922
923 vty_out (vty, " input errors %lu%s",
924 ifp->stats.ifi_ierrors, VTY_NEWLINE);
925
926 vty_out (vty, " output packets %lu, bytes %lu, multicast packets %lu%s",
927 ifp->stats.ifi_opackets, ifp->stats.ifi_obytes,
928 ifp->stats.ifi_omcasts, VTY_NEWLINE);
929
930 vty_out (vty, " output errors %lu%s",
931 ifp->stats.ifi_oerrors, VTY_NEWLINE);
932
933 vty_out (vty, " collisions %lu%s",
934 ifp->stats.ifi_collisions, VTY_NEWLINE);
935 #endif /* __bsdi__ || __NetBSD__ */
936 #endif /* HAVE_NET_RT_IFLIST */
937 }
938
939 /* Wrapper hook point for zebra daemon so that ifindex can be set
940 * DEFUN macro not used as extract.pl HAS to ignore this
941 * See also interface_cmd in lib/if.c
942 */
943 DEFUN_NOSH (zebra_interface,
944 zebra_interface_cmd,
945 "interface IFNAME",
946 "Select an interface to configure\n"
947 "Interface's name\n")
948 {
949 int ret;
950 struct interface * ifp;
951
952 /* Call lib interface() */
953 if ((ret = interface_cmd.func (self, vty, argc, argv)) != CMD_SUCCESS)
954 return ret;
955
956 ifp = vty->index;
957
958 if (ifp->ifindex == IFINDEX_INTERNAL)
959 /* Is this really necessary? Shouldn't status be initialized to 0
960 in that case? */
961 UNSET_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE);
962
963 return ret;
964 }
965
966 struct cmd_node interface_node =
967 {
968 INTERFACE_NODE,
969 "%s(config-if)# ",
970 1
971 };
972
973 /* Show all or specified interface to vty. */
974 DEFUN (show_interface, show_interface_cmd,
975 "show interface [IFNAME]",
976 SHOW_STR
977 "Interface status and configuration\n"
978 "Inteface name\n")
979 {
980 struct listnode *node;
981 struct interface *ifp;
982
983 #ifdef HAVE_PROC_NET_DEV
984 /* If system has interface statistics via proc file system, update
985 statistics. */
986 ifstat_update_proc ();
987 #endif /* HAVE_PROC_NET_DEV */
988 #ifdef HAVE_NET_RT_IFLIST
989 ifstat_update_sysctl ();
990 #endif /* HAVE_NET_RT_IFLIST */
991
992 /* Specified interface print. */
993 if (argc != 0)
994 {
995 ifp = if_lookup_by_name (argv[0]);
996 if (ifp == NULL)
997 {
998 vty_out (vty, "%% Can't find interface %s%s", argv[0],
999 VTY_NEWLINE);
1000 return CMD_WARNING;
1001 }
1002 if_dump_vty (vty, ifp);
1003 return CMD_SUCCESS;
1004 }
1005
1006 /* All interface print. */
1007 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
1008 if_dump_vty (vty, ifp);
1009
1010 return CMD_SUCCESS;
1011 }
1012
1013 DEFUN (show_interface_desc,
1014 show_interface_desc_cmd,
1015 "show interface description",
1016 SHOW_STR
1017 "Interface status and configuration\n"
1018 "Interface description\n")
1019 {
1020 struct listnode *node;
1021 struct interface *ifp;
1022
1023 vty_out (vty, "Interface Status Protocol Description%s", VTY_NEWLINE);
1024 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
1025 {
1026 int len;
1027
1028 len = vty_out (vty, "%s", ifp->name);
1029 vty_out (vty, "%*s", (16 - len), " ");
1030
1031 if (if_is_up(ifp))
1032 {
1033 vty_out (vty, "up ");
1034 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION))
1035 {
1036 if (if_is_running(ifp))
1037 vty_out (vty, "up ");
1038 else
1039 vty_out (vty, "down ");
1040 }
1041 else
1042 {
1043 vty_out (vty, "unknown ");
1044 }
1045 }
1046 else
1047 {
1048 vty_out (vty, "down down ");
1049 }
1050
1051 if (ifp->desc)
1052 vty_out (vty, "%s", ifp->desc);
1053 vty_out (vty, "%s", VTY_NEWLINE);
1054 }
1055 return CMD_SUCCESS;
1056 }
1057
1058 DEFUN (multicast,
1059 multicast_cmd,
1060 "multicast",
1061 "Set multicast flag to interface\n")
1062 {
1063 int ret;
1064 struct interface *ifp;
1065 struct zebra_if *if_data;
1066
1067 ifp = (struct interface *) vty->index;
1068 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
1069 {
1070 ret = if_set_flags (ifp, IFF_MULTICAST);
1071 if (ret < 0)
1072 {
1073 vty_out (vty, "Can't set multicast flag%s", VTY_NEWLINE);
1074 return CMD_WARNING;
1075 }
1076 if_refresh (ifp);
1077 }
1078 if_data = ifp->info;
1079 if_data->multicast = IF_ZEBRA_MULTICAST_ON;
1080
1081 return CMD_SUCCESS;
1082 }
1083
1084 DEFUN (no_multicast,
1085 no_multicast_cmd,
1086 "no multicast",
1087 NO_STR
1088 "Unset multicast flag to interface\n")
1089 {
1090 int ret;
1091 struct interface *ifp;
1092 struct zebra_if *if_data;
1093
1094 ifp = (struct interface *) vty->index;
1095 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
1096 {
1097 ret = if_unset_flags (ifp, IFF_MULTICAST);
1098 if (ret < 0)
1099 {
1100 vty_out (vty, "Can't unset multicast flag%s", VTY_NEWLINE);
1101 return CMD_WARNING;
1102 }
1103 if_refresh (ifp);
1104 }
1105 if_data = ifp->info;
1106 if_data->multicast = IF_ZEBRA_MULTICAST_OFF;
1107
1108 return CMD_SUCCESS;
1109 }
1110
1111 DEFUN (linkdetect,
1112 linkdetect_cmd,
1113 "link-detect",
1114 "Enable link detection on interface\n")
1115 {
1116 struct interface *ifp;
1117 int if_was_operative;
1118
1119 ifp = (struct interface *) vty->index;
1120 if_was_operative = if_is_no_ptm_operative(ifp);
1121 SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
1122
1123 /* When linkdetection is enabled, if might come down */
1124 if (!if_is_no_ptm_operative(ifp) && if_was_operative) if_down(ifp);
1125
1126 /* FIXME: Will defer status change forwarding if interface
1127 does not come down! */
1128
1129 return CMD_SUCCESS;
1130 }
1131
1132
1133 DEFUN (no_linkdetect,
1134 no_linkdetect_cmd,
1135 "no link-detect",
1136 NO_STR
1137 "Disable link detection on interface\n")
1138 {
1139 struct interface *ifp;
1140 int if_was_operative;
1141
1142 ifp = (struct interface *) vty->index;
1143 if_was_operative = if_is_no_ptm_operative(ifp);
1144 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
1145
1146 /* Interface may come up after disabling link detection */
1147 if (if_is_operative(ifp) && !if_was_operative) if_up(ifp);
1148
1149 /* FIXME: see linkdetect_cmd */
1150
1151 return CMD_SUCCESS;
1152 }
1153
1154 DEFUN (shutdown_if,
1155 shutdown_if_cmd,
1156 "shutdown",
1157 "Shutdown the selected interface\n")
1158 {
1159 int ret;
1160 struct interface *ifp;
1161 struct zebra_if *if_data;
1162
1163 ifp = (struct interface *) vty->index;
1164 if (ifp->ifindex != IFINDEX_INTERNAL)
1165 {
1166 ret = if_unset_flags (ifp, IFF_UP);
1167 if (ret < 0)
1168 {
1169 vty_out (vty, "Can't shutdown interface%s", VTY_NEWLINE);
1170 return CMD_WARNING;
1171 }
1172 if_refresh (ifp);
1173 }
1174 if_data = ifp->info;
1175 if_data->shutdown = IF_ZEBRA_SHUTDOWN_ON;
1176
1177 return CMD_SUCCESS;
1178 }
1179
1180 DEFUN (no_shutdown_if,
1181 no_shutdown_if_cmd,
1182 "no shutdown",
1183 NO_STR
1184 "Shutdown the selected interface\n")
1185 {
1186 int ret;
1187 struct interface *ifp;
1188 struct zebra_if *if_data;
1189
1190 ifp = (struct interface *) vty->index;
1191
1192 if (ifp->ifindex != IFINDEX_INTERNAL)
1193 {
1194 ret = if_set_flags (ifp, IFF_UP | IFF_RUNNING);
1195 if (ret < 0)
1196 {
1197 vty_out (vty, "Can't up interface%s", VTY_NEWLINE);
1198 return CMD_WARNING;
1199 }
1200 if_refresh (ifp);
1201
1202 /* Some addresses (in particular, IPv6 addresses on Linux) get
1203 * removed when the interface goes down. They need to be readded.
1204 */
1205 if_addr_wakeup(ifp);
1206 }
1207
1208 if_data = ifp->info;
1209 if_data->shutdown = IF_ZEBRA_SHUTDOWN_OFF;
1210
1211 return CMD_SUCCESS;
1212 }
1213
1214 DEFUN (bandwidth_if,
1215 bandwidth_if_cmd,
1216 "bandwidth <1-10000000>",
1217 "Set bandwidth informational parameter\n"
1218 "Bandwidth in kilobits\n")
1219 {
1220 struct interface *ifp;
1221 unsigned int bandwidth;
1222
1223 ifp = (struct interface *) vty->index;
1224 bandwidth = strtol(argv[0], NULL, 10);
1225
1226 /* bandwidth range is <1-10000000> */
1227 if (bandwidth < 1 || bandwidth > 10000000)
1228 {
1229 vty_out (vty, "Bandwidth is invalid%s", VTY_NEWLINE);
1230 return CMD_WARNING;
1231 }
1232
1233 ifp->bandwidth = bandwidth;
1234
1235 /* force protocols to recalculate routes due to cost change */
1236 if (if_is_operative (ifp))
1237 zebra_interface_up_update (ifp);
1238
1239 return CMD_SUCCESS;
1240 }
1241
1242 DEFUN (no_bandwidth_if,
1243 no_bandwidth_if_cmd,
1244 "no bandwidth",
1245 NO_STR
1246 "Set bandwidth informational parameter\n")
1247 {
1248 struct interface *ifp;
1249
1250 ifp = (struct interface *) vty->index;
1251
1252 ifp->bandwidth = 0;
1253
1254 /* force protocols to recalculate routes due to cost change */
1255 if (if_is_operative (ifp))
1256 zebra_interface_up_update (ifp);
1257
1258 return CMD_SUCCESS;
1259 }
1260
1261 ALIAS (no_bandwidth_if,
1262 no_bandwidth_if_val_cmd,
1263 "no bandwidth <1-10000000>",
1264 NO_STR
1265 "Set bandwidth informational parameter\n"
1266 "Bandwidth in kilobits\n")
1267
1268 static int
1269 ip_address_install (struct vty *vty, struct interface *ifp,
1270 const char *addr_str, const char *peer_str,
1271 const char *label)
1272 {
1273 struct zebra_if *if_data;
1274 struct prefix_ipv4 cp;
1275 struct connected *ifc;
1276 struct prefix_ipv4 *p;
1277 int ret;
1278
1279 if_data = ifp->info;
1280
1281 ret = str2prefix_ipv4 (addr_str, &cp);
1282 if (ret <= 0)
1283 {
1284 vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
1285 return CMD_WARNING;
1286 }
1287
1288 ifc = connected_check (ifp, (struct prefix *) &cp);
1289 if (! ifc)
1290 {
1291 ifc = connected_new ();
1292 ifc->ifp = ifp;
1293
1294 /* Address. */
1295 p = prefix_ipv4_new ();
1296 *p = cp;
1297 ifc->address = (struct prefix *) p;
1298
1299 /* Broadcast. */
1300 if (p->prefixlen <= IPV4_MAX_PREFIXLEN-2)
1301 {
1302 p = prefix_ipv4_new ();
1303 *p = cp;
1304 p->prefix.s_addr = ipv4_broadcast_addr(p->prefix.s_addr,p->prefixlen);
1305 ifc->destination = (struct prefix *) p;
1306 }
1307
1308 /* Label. */
1309 if (label)
1310 ifc->label = XSTRDUP (MTYPE_CONNECTED_LABEL, label);
1311
1312 /* Add to linked list. */
1313 listnode_add (ifp->connected, ifc);
1314 }
1315
1316 /* This address is configured from zebra. */
1317 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
1318 SET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
1319
1320 /* In case of this route need to install kernel. */
1321 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
1322 && CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)
1323 && !(if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON))
1324 {
1325 /* Some system need to up the interface to set IP address. */
1326 if (! if_is_up (ifp))
1327 {
1328 if_set_flags (ifp, IFF_UP | IFF_RUNNING);
1329 if_refresh (ifp);
1330 }
1331
1332 ret = if_set_prefix (ifp, ifc);
1333 if (ret < 0)
1334 {
1335 vty_out (vty, "%% Can't set interface IP address: %s.%s",
1336 safe_strerror(errno), VTY_NEWLINE);
1337 return CMD_WARNING;
1338 }
1339
1340 SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
1341 /* The address will be advertised to zebra clients when the notification
1342 * from the kernel has been received.
1343 * It will also be added to the subnet chain list, then. */
1344 }
1345
1346 return CMD_SUCCESS;
1347 }
1348
1349 static int
1350 ip_address_uninstall (struct vty *vty, struct interface *ifp,
1351 const char *addr_str, const char *peer_str,
1352 const char *label)
1353 {
1354 struct prefix_ipv4 cp;
1355 struct connected *ifc;
1356 int ret;
1357
1358 /* Convert to prefix structure. */
1359 ret = str2prefix_ipv4 (addr_str, &cp);
1360 if (ret <= 0)
1361 {
1362 vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
1363 return CMD_WARNING;
1364 }
1365
1366 /* Check current interface address. */
1367 ifc = connected_check (ifp, (struct prefix *) &cp);
1368 if (! ifc)
1369 {
1370 vty_out (vty, "%% Can't find address%s", VTY_NEWLINE);
1371 return CMD_WARNING;
1372 }
1373
1374 /* This is not configured address. */
1375 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
1376 return CMD_WARNING;
1377
1378 UNSET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
1379
1380 /* This is not real address or interface is not active. */
1381 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
1382 || ! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
1383 {
1384 listnode_delete (ifp->connected, ifc);
1385 connected_free (ifc);
1386 return CMD_WARNING;
1387 }
1388
1389 /* This is real route. */
1390 ret = if_unset_prefix (ifp, ifc);
1391 if (ret < 0)
1392 {
1393 vty_out (vty, "%% Can't unset interface IP address: %s.%s",
1394 safe_strerror(errno), VTY_NEWLINE);
1395 return CMD_WARNING;
1396 }
1397 UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
1398 /* we will receive a kernel notification about this route being removed.
1399 * this will trigger its removal from the connected list. */
1400 return CMD_SUCCESS;
1401 }
1402
1403 DEFUN (ip_address,
1404 ip_address_cmd,
1405 "ip address A.B.C.D/M",
1406 "Interface Internet Protocol config commands\n"
1407 "Set the IP address of an interface\n"
1408 "IP address (e.g. 10.0.0.1/8)\n")
1409 {
1410 return ip_address_install (vty, vty->index, argv[0], NULL, NULL);
1411 }
1412
1413 DEFUN (no_ip_address,
1414 no_ip_address_cmd,
1415 "no ip address A.B.C.D/M",
1416 NO_STR
1417 "Interface Internet Protocol config commands\n"
1418 "Set the IP address of an interface\n"
1419 "IP Address (e.g. 10.0.0.1/8)")
1420 {
1421 return ip_address_uninstall (vty, vty->index, argv[0], NULL, NULL);
1422 }
1423
1424 #ifdef HAVE_NETLINK
1425 DEFUN (ip_address_label,
1426 ip_address_label_cmd,
1427 "ip address A.B.C.D/M label LINE",
1428 "Interface Internet Protocol config commands\n"
1429 "Set the IP address of an interface\n"
1430 "IP address (e.g. 10.0.0.1/8)\n"
1431 "Label of this address\n"
1432 "Label\n")
1433 {
1434 return ip_address_install (vty, vty->index, argv[0], NULL, argv[1]);
1435 }
1436
1437 DEFUN (no_ip_address_label,
1438 no_ip_address_label_cmd,
1439 "no ip address A.B.C.D/M label LINE",
1440 NO_STR
1441 "Interface Internet Protocol config commands\n"
1442 "Set the IP address of an interface\n"
1443 "IP address (e.g. 10.0.0.1/8)\n"
1444 "Label of this address\n"
1445 "Label\n")
1446 {
1447 return ip_address_uninstall (vty, vty->index, argv[0], NULL, argv[1]);
1448 }
1449 #endif /* HAVE_NETLINK */
1450
1451 #ifdef HAVE_IPV6
1452 static int
1453 ipv6_address_install (struct vty *vty, struct interface *ifp,
1454 const char *addr_str, const char *peer_str,
1455 const char *label, int secondary)
1456 {
1457 struct zebra_if *if_data;
1458 struct prefix_ipv6 cp;
1459 struct connected *ifc;
1460 struct prefix_ipv6 *p;
1461 int ret;
1462
1463 if_data = ifp->info;
1464
1465 ret = str2prefix_ipv6 (addr_str, &cp);
1466 if (ret <= 0)
1467 {
1468 vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
1469 return CMD_WARNING;
1470 }
1471
1472 ifc = connected_check (ifp, (struct prefix *) &cp);
1473 if (! ifc)
1474 {
1475 ifc = connected_new ();
1476 ifc->ifp = ifp;
1477
1478 /* Address. */
1479 p = prefix_ipv6_new ();
1480 *p = cp;
1481 ifc->address = (struct prefix *) p;
1482
1483 /* Secondary. */
1484 if (secondary)
1485 SET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
1486
1487 /* Label. */
1488 if (label)
1489 ifc->label = XSTRDUP (MTYPE_CONNECTED_LABEL, label);
1490
1491 /* Add to linked list. */
1492 listnode_add (ifp->connected, ifc);
1493 }
1494
1495 /* This address is configured from zebra. */
1496 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
1497 SET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
1498
1499 /* In case of this route need to install kernel. */
1500 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
1501 && CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)
1502 && !(if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON))
1503 {
1504 /* Some system need to up the interface to set IP address. */
1505 if (! if_is_up (ifp))
1506 {
1507 if_set_flags (ifp, IFF_UP | IFF_RUNNING);
1508 if_refresh (ifp);
1509 }
1510
1511 ret = if_prefix_add_ipv6 (ifp, ifc);
1512
1513 if (ret < 0)
1514 {
1515 vty_out (vty, "%% Can't set interface IP address: %s.%s",
1516 safe_strerror(errno), VTY_NEWLINE);
1517 return CMD_WARNING;
1518 }
1519
1520 SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
1521 /* The address will be advertised to zebra clients when the notification
1522 * from the kernel has been received. */
1523 }
1524
1525 return CMD_SUCCESS;
1526 }
1527
1528 static int
1529 ipv6_address_uninstall (struct vty *vty, struct interface *ifp,
1530 const char *addr_str, const char *peer_str,
1531 const char *label, int secondry)
1532 {
1533 struct prefix_ipv6 cp;
1534 struct connected *ifc;
1535 int ret;
1536
1537 /* Convert to prefix structure. */
1538 ret = str2prefix_ipv6 (addr_str, &cp);
1539 if (ret <= 0)
1540 {
1541 vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
1542 return CMD_WARNING;
1543 }
1544
1545 /* Check current interface address. */
1546 ifc = connected_check (ifp, (struct prefix *) &cp);
1547 if (! ifc)
1548 {
1549 vty_out (vty, "%% Can't find address%s", VTY_NEWLINE);
1550 return CMD_WARNING;
1551 }
1552
1553 /* This is not configured address. */
1554 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
1555 return CMD_WARNING;
1556
1557 UNSET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
1558
1559 /* This is not real address or interface is not active. */
1560 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
1561 || ! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
1562 {
1563 listnode_delete (ifp->connected, ifc);
1564 connected_free (ifc);
1565 return CMD_WARNING;
1566 }
1567
1568 /* This is real route. */
1569 ret = if_prefix_delete_ipv6 (ifp, ifc);
1570 if (ret < 0)
1571 {
1572 vty_out (vty, "%% Can't unset interface IP address: %s.%s",
1573 safe_strerror(errno), VTY_NEWLINE);
1574 return CMD_WARNING;
1575 }
1576
1577 UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
1578 /* This information will be propagated to the zclients when the
1579 * kernel notification is received. */
1580 return CMD_SUCCESS;
1581 }
1582
1583 DEFUN (ipv6_address,
1584 ipv6_address_cmd,
1585 "ipv6 address X:X::X:X/M",
1586 "Interface IPv6 config commands\n"
1587 "Set the IP address of an interface\n"
1588 "IPv6 address (e.g. 3ffe:506::1/48)\n")
1589 {
1590 return ipv6_address_install (vty, vty->index, argv[0], NULL, NULL, 0);
1591 }
1592
1593 DEFUN (no_ipv6_address,
1594 no_ipv6_address_cmd,
1595 "no ipv6 address X:X::X:X/M",
1596 NO_STR
1597 "Interface IPv6 config commands\n"
1598 "Set the IP address of an interface\n"
1599 "IPv6 address (e.g. 3ffe:506::1/48)\n")
1600 {
1601 return ipv6_address_uninstall (vty, vty->index, argv[0], NULL, NULL, 0);
1602 }
1603 #endif /* HAVE_IPV6 */
1604
1605 static int
1606 if_config_write (struct vty *vty)
1607 {
1608 struct listnode *node;
1609 struct interface *ifp;
1610
1611 zebra_ptm_write (vty);
1612
1613 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
1614 {
1615 struct zebra_if *if_data;
1616 struct listnode *addrnode;
1617 struct connected *ifc;
1618 struct prefix *p;
1619
1620 if_data = ifp->info;
1621
1622 vty_out (vty, "interface %s%s", ifp->name,
1623 VTY_NEWLINE);
1624
1625 if (if_data)
1626 {
1627 if (if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)
1628 vty_out (vty, " shutdown%s", VTY_NEWLINE);
1629 }
1630
1631 if (ifp->desc)
1632 vty_out (vty, " description %s%s", ifp->desc,
1633 VTY_NEWLINE);
1634
1635 /* Assign bandwidth here to avoid unnecessary interface flap
1636 while processing config script */
1637 if (ifp->bandwidth != 0)
1638 vty_out(vty, " bandwidth %u%s", ifp->bandwidth, VTY_NEWLINE);
1639
1640 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION))
1641 vty_out(vty, " link-detect%s", VTY_NEWLINE);
1642
1643 for (ALL_LIST_ELEMENTS_RO (ifp->connected, addrnode, ifc))
1644 {
1645 if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
1646 {
1647 char buf[INET6_ADDRSTRLEN];
1648 p = ifc->address;
1649 vty_out (vty, " ip%s address %s/%d",
1650 p->family == AF_INET ? "" : "v6",
1651 inet_ntop (p->family, &p->u.prefix, buf, sizeof(buf)),
1652 p->prefixlen);
1653
1654 if (ifc->label)
1655 vty_out (vty, " label %s", ifc->label);
1656
1657 vty_out (vty, "%s", VTY_NEWLINE);
1658 }
1659 }
1660
1661 if (if_data)
1662 {
1663 if (if_data->multicast != IF_ZEBRA_MULTICAST_UNSPEC)
1664 vty_out (vty, " %smulticast%s",
1665 if_data->multicast == IF_ZEBRA_MULTICAST_ON ? "" : "no ",
1666 VTY_NEWLINE);
1667 }
1668
1669 #ifdef RTADV
1670 rtadv_config_write (vty, ifp);
1671 #endif /* RTADV */
1672
1673 #ifdef HAVE_IRDP
1674 irdp_config_write (vty, ifp);
1675 #endif /* IRDP */
1676
1677 vty_out (vty, "!%s", VTY_NEWLINE);
1678 }
1679 return 0;
1680 }
1681
1682 /* Allocate and initialize interface vector. */
1683 void
1684 zebra_if_init (void)
1685 {
1686 /* Initialize interface and new hook. */
1687 if_init ();
1688 if_add_hook (IF_NEW_HOOK, if_zebra_new_hook);
1689 if_add_hook (IF_DELETE_HOOK, if_zebra_delete_hook);
1690
1691 /* Install configuration write function. */
1692 install_node (&interface_node, if_config_write);
1693
1694 install_element (VIEW_NODE, &show_interface_cmd);
1695 install_element (ENABLE_NODE, &show_interface_cmd);
1696 install_element (ENABLE_NODE, &show_interface_desc_cmd);
1697 install_element (CONFIG_NODE, &zebra_interface_cmd);
1698 install_element (CONFIG_NODE, &no_interface_cmd);
1699 install_default (INTERFACE_NODE);
1700 install_element (INTERFACE_NODE, &interface_desc_cmd);
1701 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
1702 install_element (INTERFACE_NODE, &multicast_cmd);
1703 install_element (INTERFACE_NODE, &no_multicast_cmd);
1704 install_element (INTERFACE_NODE, &linkdetect_cmd);
1705 install_element (INTERFACE_NODE, &no_linkdetect_cmd);
1706 install_element (INTERFACE_NODE, &shutdown_if_cmd);
1707 install_element (INTERFACE_NODE, &no_shutdown_if_cmd);
1708 install_element (INTERFACE_NODE, &bandwidth_if_cmd);
1709 install_element (INTERFACE_NODE, &no_bandwidth_if_cmd);
1710 install_element (INTERFACE_NODE, &no_bandwidth_if_val_cmd);
1711 install_element (INTERFACE_NODE, &ip_address_cmd);
1712 install_element (INTERFACE_NODE, &no_ip_address_cmd);
1713 #ifdef HAVE_IPV6
1714 install_element (INTERFACE_NODE, &ipv6_address_cmd);
1715 install_element (INTERFACE_NODE, &no_ipv6_address_cmd);
1716 #endif /* HAVE_IPV6 */
1717 #ifdef HAVE_NETLINK
1718 install_element (INTERFACE_NODE, &ip_address_label_cmd);
1719 install_element (INTERFACE_NODE, &no_ip_address_label_cmd);
1720 #endif /* HAVE_NETLINK */
1721 }