blob: 4864103b70f2c27e61e4650d7137ac935c02e288 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
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"
hassoca776982004-06-12 14:33:05 +000042#include "zebra/irdp.h"
paul718e3742002-12-13 20:15:29 +000043
44/* Allocate a new internal interface index
45 * This works done from the top so that %d macros
46 * print a - sign!
47 */
48static unsigned int
49if_new_intern_ifindex (void)
50{
51 /* Start here so that first one assigned is 0xFFFFFFFF */
52 static unsigned int ifindex = IFINDEX_INTERNBASE + 1;
53
54 for (;;)
55 {
56 ifindex--;
57 if ( ifindex <= IFINDEX_INTERNBASE )
58 ifindex = 0xFFFFFFFF;
59
60 if (if_lookup_by_index(ifindex) == NULL)
61 return ifindex;
62 }
63}
64
65/* Called when new interface is added. */
66int
67if_zebra_new_hook (struct interface *ifp)
68{
69 struct zebra_if *zebra_if;
70
71 zebra_if = XMALLOC (MTYPE_TMP, sizeof (struct zebra_if));
72 memset (zebra_if, 0, sizeof (struct zebra_if));
73
74 zebra_if->multicast = IF_ZEBRA_MULTICAST_UNSPEC;
75 zebra_if->shutdown = IF_ZEBRA_SHUTDOWN_UNSPEC;
76
77#ifdef RTADV
78 {
79 /* Set default router advertise values. */
80 struct rtadvconf *rtadv;
81
82 rtadv = &zebra_if->rtadv;
83
84 rtadv->AdvSendAdvertisements = 0;
85 rtadv->MaxRtrAdvInterval = RTADV_MAX_RTR_ADV_INTERVAL;
86 rtadv->MinRtrAdvInterval = RTADV_MIN_RTR_ADV_INTERVAL;
87 rtadv->AdvIntervalTimer = 0;
88 rtadv->AdvManagedFlag = 0;
89 rtadv->AdvOtherConfigFlag = 0;
90 rtadv->AdvLinkMTU = 0;
91 rtadv->AdvReachableTime = 0;
92 rtadv->AdvRetransTimer = 0;
93 rtadv->AdvCurHopLimit = 0;
94 rtadv->AdvDefaultLifetime = RTADV_ADV_DEFAULT_LIFETIME;
95
96 rtadv->AdvPrefixList = list_new ();
97 }
98#endif /* RTADV */
99
hassoeef1fe12004-10-03 18:46:08 +0000100 /* Initialize installed address chains tree. */
101 zebra_if->ipv4_subnets = route_table_init ();
102
paul718e3742002-12-13 20:15:29 +0000103 ifp->info = zebra_if;
104 return 0;
105}
106
107/* Called when interface is deleted. */
108int
109if_zebra_delete_hook (struct interface *ifp)
110{
hassoeef1fe12004-10-03 18:46:08 +0000111 struct zebra_if *zebra_if;
112
paul718e3742002-12-13 20:15:29 +0000113 if (ifp->info)
hassoeef1fe12004-10-03 18:46:08 +0000114 {
115 zebra_if = ifp->info;
116
117 /* Free installed address chains tree. */
118 if (zebra_if->ipv4_subnets)
119 route_table_finish (zebra_if->ipv4_subnets);
120
121 XFREE (MTYPE_TMP, zebra_if);
122 }
123
124 return 0;
125}
126
127/* Tie an interface address to its derived subnet list of addresses. */
128int
129if_subnet_add (struct interface *ifp, struct connected *ifc)
130{
131 struct route_node *rn;
132 struct zebra_if *zebra_if;
133 struct prefix cp;
134 struct list *addr_list;
135
136 assert (ifp && ifp->info && ifc);
137 zebra_if = ifp->info;
138
139 /* Get address derived subnet node and associated address list, while marking
140 address secondary attribute appropriately. */
141 cp = *ifc->address;
142 apply_mask (&cp);
143 rn = route_node_get (zebra_if->ipv4_subnets, &cp);
144
145 if ((addr_list = rn->info))
146 SET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
147 else
148 {
149 UNSET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
150 rn->info = addr_list = list_new ();
151 route_lock_node (rn);
152 }
153
154 /* Tie address at the tail of address list. */
155 listnode_add (addr_list, ifc);
156
157 /* Return list element count. */
158 return (addr_list->count);
159}
160
161/* Untie an interface address from its derived subnet list of addresses. */
162int
163if_subnet_delete (struct interface *ifp, struct connected *ifc)
164{
165 struct route_node *rn;
166 struct zebra_if *zebra_if;
167 struct list *addr_list;
168
169 assert (ifp && ifp->info && ifc);
170 zebra_if = ifp->info;
171
172 /* Get address derived subnet node. */
173 rn = route_node_lookup (zebra_if->ipv4_subnets, ifc->address);
174 if (! (rn && rn->info))
175 return -1;
176 route_unlock_node (rn);
177
178 /* Untie address from subnet's address list. */
179 addr_list = rn->info;
180 listnode_delete (addr_list, ifc);
181 route_unlock_node (rn);
182
183 /* Return list element count, if not empty. */
184 if (addr_list->count)
185 {
186 /* If deleted address is primary, mark subsequent one as such and distribute. */
187 if (! CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY))
188 {
189 ifc = (struct connected *) addr_list->head->data;
190 zebra_interface_address_delete_update (ifp, ifc);
191 UNSET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
192 zebra_interface_address_add_update (ifp, ifc);
193 }
194
195 return addr_list->count;
196 }
197
198 /* Otherwise, free list and route node. */
199 list_free (addr_list);
200 rn->info = NULL;
201 route_unlock_node (rn);
202
paul718e3742002-12-13 20:15:29 +0000203 return 0;
204}
205
206/* Wake up configured address if it is not in current kernel
207 address. */
208void
209if_addr_wakeup (struct interface *ifp)
210{
211 struct listnode *node;
212 struct connected *ifc;
213 struct prefix *p;
214 int ret;
215
216 for (node = listhead (ifp->connected); node; nextnode (node))
217 {
218 ifc = getdata (node);
219 p = ifc->address;
220
221 if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED)
222 && ! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
223 {
224 /* Address check. */
225 if (p->family == AF_INET)
226 {
227 if (! if_is_up (ifp))
228 {
229 if_set_flags (ifp, IFF_UP | IFF_RUNNING);
230 if_refresh (ifp);
231 }
232
233 ret = if_set_prefix (ifp, ifc);
234 if (ret < 0)
235 {
236 zlog_warn ("Can't set interface's address: %s",
ajs6099b3b2004-11-20 02:06:59 +0000237 safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +0000238 continue;
239 }
hassoeef1fe12004-10-03 18:46:08 +0000240
241 /* Add to subnet chain list. */
242 if_subnet_add (ifp, ifc);
243
paul718e3742002-12-13 20:15:29 +0000244 SET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
245
246 zebra_interface_address_add_update (ifp, ifc);
247
paul2e3b2e42002-12-13 21:03:13 +0000248 if (if_is_operative(ifp))
paul718e3742002-12-13 20:15:29 +0000249 connected_up_ipv4 (ifp, ifc);
250 }
251#ifdef HAVE_IPV6
252 if (p->family == AF_INET6)
253 {
254 if (! if_is_up (ifp))
255 {
256 if_set_flags (ifp, IFF_UP | IFF_RUNNING);
257 if_refresh (ifp);
258 }
259
260 ret = if_prefix_add_ipv6 (ifp, ifc);
261 if (ret < 0)
262 {
263 zlog_warn ("Can't set interface's address: %s",
ajs6099b3b2004-11-20 02:06:59 +0000264 safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +0000265 continue;
266 }
267 SET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
268
269 zebra_interface_address_add_update (ifp, ifc);
270
paul2e3b2e42002-12-13 21:03:13 +0000271 if (if_is_operative(ifp))
paul718e3742002-12-13 20:15:29 +0000272 connected_up_ipv6 (ifp, ifc);
273 }
274#endif /* HAVE_IPV6 */
275 }
276 }
277}
278
279/* Handle interface addition */
280void
281if_add_update (struct interface *ifp)
282{
paul48b33aa2002-12-13 20:52:52 +0000283 struct zebra_if *if_data;
284
285 if_data = ifp->info;
286 if (if_data->multicast == IF_ZEBRA_MULTICAST_ON)
287 if_set_flags (ifp, IFF_MULTICAST);
288 else if (if_data->multicast == IF_ZEBRA_MULTICAST_OFF)
289 if_unset_flags (ifp, IFF_MULTICAST);
290
paul718e3742002-12-13 20:15:29 +0000291 zebra_interface_add_update (ifp);
292
293 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
294 {
295 SET_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE);
296
297 if_addr_wakeup (ifp);
298
299 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000300 zlog_debug ("interface %s index %d becomes active.",
301 ifp->name, ifp->ifindex);
paul718e3742002-12-13 20:15:29 +0000302 }
303 else
304 {
305 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000306 zlog_debug ("interface %s index %d is added.", ifp->name, ifp->ifindex);
paul718e3742002-12-13 20:15:29 +0000307 }
308}
309
paul44145db2004-05-09 11:00:23 +0000310
311/* Handle an interface delete event
312 *
313 * This function is only called when support for
314 * RTM_IFANNOUNCE or AF_NETLINK sockets (RTM_DELLINK message)
315 * is available. It is not called on, eg, Solaris.
316 */
317#if (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK))
paul718e3742002-12-13 20:15:29 +0000318void
319if_delete_update (struct interface *ifp)
320{
321 struct listnode *node;
322 struct listnode *next;
hassoeef1fe12004-10-03 18:46:08 +0000323 struct listnode *first;
324 struct listnode *last;
paul718e3742002-12-13 20:15:29 +0000325 struct connected *ifc;
326 struct prefix *p;
hassoeef1fe12004-10-03 18:46:08 +0000327 struct route_node *rn;
328 struct zebra_if *zebra_if;
329 struct list *addr_list;
330
331 zebra_if = ifp->info;
paul718e3742002-12-13 20:15:29 +0000332
333 if (if_is_up(ifp))
334 {
335 zlog_err ("interface %s index %d is still up while being deleted.",
336 ifp->name, ifp->ifindex);
337 return;
338 }
339
340 /* Mark interface as inactive */
341 UNSET_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE);
342
343 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000344 zlog_debug ("interface %s index %d is now inactive.",
paul718e3742002-12-13 20:15:29 +0000345 ifp->name, ifp->ifindex);
346
347 /* Delete connected routes from the kernel. */
348 if (ifp->connected)
349 {
hassoeef1fe12004-10-03 18:46:08 +0000350 last = NULL;
351 while ((node = (last ? last->next : listhead (ifp->connected))))
paul718e3742002-12-13 20:15:29 +0000352 {
paul718e3742002-12-13 20:15:29 +0000353 ifc = getdata (node);
354 p = ifc->address;
hassoeef1fe12004-10-03 18:46:08 +0000355
paul718e3742002-12-13 20:15:29 +0000356 if (p->family == AF_INET)
hassoeef1fe12004-10-03 18:46:08 +0000357 {
358 rn = route_node_lookup (zebra_if->ipv4_subnets, p);
359 route_unlock_node (rn);
360 addr_list = (struct list *) rn->info;
361
362 /* Remove addresses, secondaries first. */
363 first = listhead (addr_list);
364 for (node = first->next; node || first; node = next)
365 {
366 if (! node)
367 {
368 node = first;
369 first = NULL;
370 }
371 next = node->next;
372
373 ifc = getdata (node);
374 p = ifc->address;
375
376 connected_down_ipv4 (ifp, ifc);
377
378 zebra_interface_address_delete_update (ifp, ifc);
379
380 UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
381
382 /* Remove from subnet chain. */
383 list_delete_node (addr_list, node);
384 route_unlock_node (rn);
385
386 /* Remove from interface address list (unconditionally). */
387 listnode_delete (ifp->connected, ifc);
388 connected_free (ifc);
389 }
390
391 /* Free chain list and respective route node. */
392 list_delete (addr_list);
393 rn->info = NULL;
394 route_unlock_node (rn);
395 }
paul718e3742002-12-13 20:15:29 +0000396#ifdef HAVE_IPV6
397 else if (p->family == AF_INET6)
hassoeef1fe12004-10-03 18:46:08 +0000398 {
399 connected_down_ipv6 (ifp, ifc);
paul718e3742002-12-13 20:15:29 +0000400
hassoeef1fe12004-10-03 18:46:08 +0000401 zebra_interface_address_delete_update (ifp, ifc);
paul718e3742002-12-13 20:15:29 +0000402
hassoeef1fe12004-10-03 18:46:08 +0000403 UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
404
405 if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
406 last = node;
407 else
408 {
409 listnode_delete (ifp->connected, ifc);
410 connected_free (ifc);
411 }
paul718e3742002-12-13 20:15:29 +0000412 }
hassoeef1fe12004-10-03 18:46:08 +0000413#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +0000414 }
415 }
416 zebra_interface_delete_update (ifp);
417}
paul44145db2004-05-09 11:00:23 +0000418#endif /* (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK) */
paul718e3742002-12-13 20:15:29 +0000419
420/* Interface is up. */
421void
422if_up (struct interface *ifp)
423{
hasso52dc7ee2004-09-23 19:18:23 +0000424 struct listnode *node;
425 struct listnode *next;
paul718e3742002-12-13 20:15:29 +0000426 struct connected *ifc;
427 struct prefix *p;
428
429 /* Notify the protocol daemons. */
430 zebra_interface_up_update (ifp);
431
432 /* Install connected routes to the kernel. */
433 if (ifp->connected)
434 {
435 for (node = listhead (ifp->connected); node; node = next)
436 {
437 next = node->next;
438 ifc = getdata (node);
439 p = ifc->address;
440
441 if (p->family == AF_INET)
442 connected_up_ipv4 (ifp, ifc);
443#ifdef HAVE_IPV6
444 else if (p->family == AF_INET6)
445 connected_up_ipv6 (ifp, ifc);
446#endif /* HAVE_IPV6 */
447 }
448 }
449
450 /* Examine all static routes. */
451 rib_update ();
452}
453
454/* Interface goes down. We have to manage different behavior of based
455 OS. */
456void
457if_down (struct interface *ifp)
458{
hasso52dc7ee2004-09-23 19:18:23 +0000459 struct listnode *node;
460 struct listnode *next;
paul718e3742002-12-13 20:15:29 +0000461 struct connected *ifc;
462 struct prefix *p;
463
464 /* Notify to the protocol daemons. */
465 zebra_interface_down_update (ifp);
466
467 /* Delete connected routes from the kernel. */
468 if (ifp->connected)
469 {
470 for (node = listhead (ifp->connected); node; node = next)
471 {
472 next = node->next;
473 ifc = getdata (node);
474 p = ifc->address;
475
476 if (p->family == AF_INET)
477 connected_down_ipv4 (ifp, ifc);
478#ifdef HAVE_IPV6
479 else if (p->family == AF_INET6)
480 connected_down_ipv6 (ifp, ifc);
481#endif /* HAVE_IPV6 */
482 }
483 }
484
485 /* Examine all static routes which direct to the interface. */
486 rib_update ();
487}
488
489void
490if_refresh (struct interface *ifp)
491{
paul2e3b2e42002-12-13 21:03:13 +0000492 if (if_is_operative (ifp))
paul718e3742002-12-13 20:15:29 +0000493 {
494 if_get_flags (ifp);
paul2e3b2e42002-12-13 21:03:13 +0000495 if (! if_is_operative (ifp))
paul718e3742002-12-13 20:15:29 +0000496 if_down (ifp);
497 }
498 else
499 {
500 if_get_flags (ifp);
paul2e3b2e42002-12-13 21:03:13 +0000501 if (if_is_operative (ifp))
paul718e3742002-12-13 20:15:29 +0000502 if_up (ifp);
503 }
504}
505
506/* Printout flag information into vty */
507void
508if_flag_dump_vty (struct vty *vty, unsigned long flag)
509{
510 int separator = 0;
511
512#define IFF_OUT_VTY(X, Y) \
513 if ((X) && (flag & (X))) \
514 { \
515 if (separator) \
516 vty_out (vty, ","); \
517 else \
518 separator = 1; \
519 vty_out (vty, Y); \
520 }
521
522 vty_out (vty, "<");
523 IFF_OUT_VTY (IFF_UP, "UP");
524 IFF_OUT_VTY (IFF_BROADCAST, "BROADCAST");
525 IFF_OUT_VTY (IFF_DEBUG, "DEBUG");
526 IFF_OUT_VTY (IFF_LOOPBACK, "LOOPBACK");
527 IFF_OUT_VTY (IFF_POINTOPOINT, "POINTOPOINT");
528 IFF_OUT_VTY (IFF_NOTRAILERS, "NOTRAILERS");
529 IFF_OUT_VTY (IFF_RUNNING, "RUNNING");
530 IFF_OUT_VTY (IFF_NOARP, "NOARP");
531 IFF_OUT_VTY (IFF_PROMISC, "PROMISC");
532 IFF_OUT_VTY (IFF_ALLMULTI, "ALLMULTI");
533 IFF_OUT_VTY (IFF_OACTIVE, "OACTIVE");
534 IFF_OUT_VTY (IFF_SIMPLEX, "SIMPLEX");
535 IFF_OUT_VTY (IFF_LINK0, "LINK0");
536 IFF_OUT_VTY (IFF_LINK1, "LINK1");
537 IFF_OUT_VTY (IFF_LINK2, "LINK2");
538 IFF_OUT_VTY (IFF_MULTICAST, "MULTICAST");
paul44145db2004-05-09 11:00:23 +0000539#ifdef SOLARIS_IPV6
540 IFF_OUT_VTY (IFF_IPV4, "IFF_IPv4");
541 IFF_OUT_VTY (IFF_IPV6, "IFF_IPv6");
542#endif /* SOLARIS_IPV6 */
paul718e3742002-12-13 20:15:29 +0000543 vty_out (vty, ">");
544}
545
546/* Output prefix string to vty. */
547int
548prefix_vty_out (struct vty *vty, struct prefix *p)
549{
550 char str[INET6_ADDRSTRLEN];
551
552 inet_ntop (p->family, &p->u.prefix, str, sizeof (str));
553 vty_out (vty, "%s", str);
554 return strlen (str);
555}
556
557/* Dump if address information to vty. */
558void
559connected_dump_vty (struct vty *vty, struct connected *connected)
560{
561 struct prefix *p;
562 struct interface *ifp;
563
564 /* Set interface pointer. */
565 ifp = connected->ifp;
566
567 /* Print interface address. */
568 p = connected->address;
569 vty_out (vty, " %s ", prefix_family_str (p));
570 prefix_vty_out (vty, p);
571 vty_out (vty, "/%d", p->prefixlen);
572
573 /* If there is destination address, print it. */
574 p = connected->destination;
575 if (p)
576 {
577 if (p->family == AF_INET)
578 if (ifp->flags & IFF_BROADCAST)
579 {
580 vty_out (vty, " broadcast ");
581 prefix_vty_out (vty, p);
582 }
583
584 if (ifp->flags & IFF_POINTOPOINT)
585 {
586 vty_out (vty, " pointopoint ");
587 prefix_vty_out (vty, p);
588 }
589 }
590
591 if (CHECK_FLAG (connected->flags, ZEBRA_IFA_SECONDARY))
592 vty_out (vty, " secondary");
593
594 if (connected->label)
595 vty_out (vty, " %s", connected->label);
596
597 vty_out (vty, "%s", VTY_NEWLINE);
598}
599
600#ifdef RTADV
601/* Dump interface ND information to vty. */
602void
603nd_dump_vty (struct vty *vty, struct interface *ifp)
604{
605 struct zebra_if *zif;
606 struct rtadvconf *rtadv;
607
608 zif = (struct zebra_if *) ifp->info;
609 rtadv = &zif->rtadv;
610
611 if (rtadv->AdvSendAdvertisements)
612 {
613 vty_out (vty, " ND advertised reachable time is %d milliseconds%s",
614 rtadv->AdvReachableTime, VTY_NEWLINE);
615 vty_out (vty, " ND advertised retransmit interval is %d milliseconds%s",
616 rtadv->AdvRetransTimer, VTY_NEWLINE);
617 vty_out (vty, " ND router advertisements are sent every %d seconds%s",
618 rtadv->MaxRtrAdvInterval, VTY_NEWLINE);
619 vty_out (vty, " ND router advertisements live for %d seconds%s",
620 rtadv->AdvDefaultLifetime, VTY_NEWLINE);
621 if (rtadv->AdvManagedFlag)
622 vty_out (vty, " Hosts use DHCP to obtain routable addresses.%s",
623 VTY_NEWLINE);
624 else
625 vty_out (vty, " Hosts use stateless autoconfig for addresses.%s",
626 VTY_NEWLINE);
627 }
628}
629#endif /* RTADV */
630
631/* Interface's information print out to vty interface. */
632void
633if_dump_vty (struct vty *vty, struct interface *ifp)
634{
635#ifdef HAVE_SOCKADDR_DL
636 struct sockaddr_dl *sdl;
637#endif /* HAVE_SOCKADDR_DL */
638 struct connected *connected;
hasso52dc7ee2004-09-23 19:18:23 +0000639 struct listnode *node;
hassoeef1fe12004-10-03 18:46:08 +0000640 struct route_node *rn;
641 struct zebra_if *zebra_if;
642
643 zebra_if = ifp->info;
paul718e3742002-12-13 20:15:29 +0000644
paul2e3b2e42002-12-13 21:03:13 +0000645 vty_out (vty, "Interface %s is ", ifp->name);
646 if (if_is_up(ifp)) {
647 vty_out (vty, "up, line protocol ");
648
649 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)) {
650 if (if_is_running(ifp))
651 vty_out (vty, "is up%s", VTY_NEWLINE);
652 else
653 vty_out (vty, "is down%s", VTY_NEWLINE);
654 } else {
655 vty_out (vty, "detection is disabled%s", VTY_NEWLINE);
656 }
657 } else {
658 vty_out (vty, "down%s", VTY_NEWLINE);
659 }
660
paul718e3742002-12-13 20:15:29 +0000661 if (ifp->desc)
662 vty_out (vty, " Description: %s%s", ifp->desc,
663 VTY_NEWLINE);
664 if (ifp->ifindex <= 0)
665 {
666 vty_out(vty, " index %d pseudo interface%s", ifp->ifindex, VTY_NEWLINE);
667 return;
668 }
669 else if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
670 {
671 vty_out(vty, " index %d inactive interface%s",
672 ifp->ifindex,
673 VTY_NEWLINE);
674 return;
675 }
676
677 vty_out (vty, " index %d metric %d mtu %d ",
678 ifp->ifindex, ifp->metric, ifp->mtu);
679 if_flag_dump_vty (vty, ifp->flags);
paul44145db2004-05-09 11:00:23 +0000680#ifdef HAVE_IPV6
681 if (ifp->mtu6 != ifp->mtu)
682 vty_out (vty, "mtu6 %d ", ifp->mtu6);
683#endif
684
paul718e3742002-12-13 20:15:29 +0000685 vty_out (vty, "%s", VTY_NEWLINE);
686
687 /* Hardware address. */
688#ifdef HAVE_SOCKADDR_DL
689 sdl = &ifp->sdl;
690 if (sdl != NULL && sdl->sdl_alen != 0)
691 {
692 int i;
693 u_char *ptr;
694
695 vty_out (vty, " HWaddr: ");
paul5b73a672004-07-23 15:26:14 +0000696 for (i = 0, ptr = (u_char *)LLADDR (sdl); i < sdl->sdl_alen; i++, ptr++)
697 vty_out (vty, "%s%02x", i == 0 ? "" : ":", *ptr);
paul718e3742002-12-13 20:15:29 +0000698 vty_out (vty, "%s", VTY_NEWLINE);
699 }
700#else
701 if (ifp->hw_addr_len != 0)
702 {
703 int i;
704
705 vty_out (vty, " HWaddr: ");
706 for (i = 0; i < ifp->hw_addr_len; i++)
707 vty_out (vty, "%s%02x", i == 0 ? "" : ":", ifp->hw_addr[i]);
708 vty_out (vty, "%s", VTY_NEWLINE);
709 }
710#endif /* HAVE_SOCKADDR_DL */
711
712 /* Bandwidth in kbps */
713 if (ifp->bandwidth != 0)
714 {
715 vty_out(vty, " bandwidth %u kbps", ifp->bandwidth);
716 vty_out(vty, "%s", VTY_NEWLINE);
717 }
718
hassoeef1fe12004-10-03 18:46:08 +0000719 for (rn = route_top (zebra_if->ipv4_subnets); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000720 {
hassoeef1fe12004-10-03 18:46:08 +0000721 if (! rn->info)
722 continue;
723
724 for (node = listhead ((struct list *) rn->info); node; nextnode (node))
725 {
726 connected = getdata (node);
727 connected_dump_vty (vty, connected);
728 }
paul718e3742002-12-13 20:15:29 +0000729 }
730
hasso39db97e2004-10-12 20:50:58 +0000731 for (node = listhead (ifp->connected); node; nextnode (node))
732 {
733 connected = getdata (node);
734 if (CHECK_FLAG (connected->conf, ZEBRA_IFC_REAL) &&
735 (connected->address->family == AF_INET6))
736 connected_dump_vty (vty, connected);
737 }
738
paul718e3742002-12-13 20:15:29 +0000739#ifdef RTADV
740 nd_dump_vty (vty, ifp);
741#endif /* RTADV */
742
743#ifdef HAVE_PROC_NET_DEV
744 /* Statistics print out using proc file system. */
hasso6f2c27a2005-01-18 13:44:35 +0000745 vty_out (vty, " %lu input packets (%lu multicast), %lu bytes, "
746 "%lu dropped%s",
747 ifp->stats.rx_packets, ifp->stats.rx_multicast,
748 ifp->stats.rx_bytes, ifp->stats.rx_dropped, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000749
hasso6f2c27a2005-01-18 13:44:35 +0000750 vty_out (vty, " %lu input errors, %lu length, %lu overrun,"
hasso3452d472005-03-06 13:42:05 +0000751 " %lu CRC, %lu frame%s",
paul718e3742002-12-13 20:15:29 +0000752 ifp->stats.rx_errors, ifp->stats.rx_length_errors,
753 ifp->stats.rx_over_errors, ifp->stats.rx_crc_errors,
hasso6f2c27a2005-01-18 13:44:35 +0000754 ifp->stats.rx_frame_errors, VTY_NEWLINE);
755
756 vty_out (vty, " %lu fifo, %lu missed%s", ifp->stats.rx_fifo_errors,
paul718e3742002-12-13 20:15:29 +0000757 ifp->stats.rx_missed_errors, VTY_NEWLINE);
758
hasso6f2c27a2005-01-18 13:44:35 +0000759 vty_out (vty, " %lu output packets, %lu bytes, %lu dropped%s",
paul718e3742002-12-13 20:15:29 +0000760 ifp->stats.tx_packets, ifp->stats.tx_bytes,
761 ifp->stats.tx_dropped, VTY_NEWLINE);
762
hasso6f2c27a2005-01-18 13:44:35 +0000763 vty_out (vty, " %lu output errors, %lu aborted, %lu carrier,"
764 " %lu fifo, %lu heartbeat%s",
paul718e3742002-12-13 20:15:29 +0000765 ifp->stats.tx_errors, ifp->stats.tx_aborted_errors,
766 ifp->stats.tx_carrier_errors, ifp->stats.tx_fifo_errors,
hasso6f2c27a2005-01-18 13:44:35 +0000767 ifp->stats.tx_heartbeat_errors, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000768
hasso6f2c27a2005-01-18 13:44:35 +0000769 vty_out (vty, " %lu window, %lu collisions%s",
770 ifp->stats.tx_window_errors, ifp->stats.collisions, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000771#endif /* HAVE_PROC_NET_DEV */
772
773#ifdef HAVE_NET_RT_IFLIST
774#if defined (__bsdi__) || defined (__NetBSD__)
775 /* Statistics print out using sysctl (). */
776 vty_out (vty, " input packets %qu, bytes %qu, dropped %qu,"
777 " multicast packets %qu%s",
778 ifp->stats.ifi_ipackets, ifp->stats.ifi_ibytes,
779 ifp->stats.ifi_iqdrops, ifp->stats.ifi_imcasts,
780 VTY_NEWLINE);
781
782 vty_out (vty, " input errors %qu%s",
783 ifp->stats.ifi_ierrors, VTY_NEWLINE);
784
785 vty_out (vty, " output packets %qu, bytes %qu, multicast packets %qu%s",
786 ifp->stats.ifi_opackets, ifp->stats.ifi_obytes,
787 ifp->stats.ifi_omcasts, VTY_NEWLINE);
788
789 vty_out (vty, " output errors %qu%s",
790 ifp->stats.ifi_oerrors, VTY_NEWLINE);
791
792 vty_out (vty, " collisions %qu%s",
793 ifp->stats.ifi_collisions, VTY_NEWLINE);
794#else
795 /* Statistics print out using sysctl (). */
796 vty_out (vty, " input packets %lu, bytes %lu, dropped %lu,"
797 " multicast packets %lu%s",
798 ifp->stats.ifi_ipackets, ifp->stats.ifi_ibytes,
799 ifp->stats.ifi_iqdrops, ifp->stats.ifi_imcasts,
800 VTY_NEWLINE);
801
802 vty_out (vty, " input errors %lu%s",
803 ifp->stats.ifi_ierrors, VTY_NEWLINE);
804
805 vty_out (vty, " output packets %lu, bytes %lu, multicast packets %lu%s",
806 ifp->stats.ifi_opackets, ifp->stats.ifi_obytes,
807 ifp->stats.ifi_omcasts, VTY_NEWLINE);
808
809 vty_out (vty, " output errors %lu%s",
810 ifp->stats.ifi_oerrors, VTY_NEWLINE);
811
812 vty_out (vty, " collisions %lu%s",
813 ifp->stats.ifi_collisions, VTY_NEWLINE);
814#endif /* __bsdi__ || __NetBSD__ */
815#endif /* HAVE_NET_RT_IFLIST */
816}
817
818/* Check supported address family. */
819int
820if_supported_family (int family)
821{
822 if (family == AF_INET)
823 return 1;
824#ifdef HAVE_IPV6
825 if (family == AF_INET6)
826 return 1;
827#endif /* HAVE_IPV6 */
828 return 0;
829}
830
831/* Wrapper hook point for zebra daemon so that ifindex can be set
832 * DEFUN macro not used as extract.pl HAS to ignore this
833 * See also interface_cmd in lib/if.c
834 */
835DEFUN_NOSH (zebra_interface,
836 zebra_interface_cmd,
837 "interface IFNAME",
838 "Select an interface to configure\n"
839 "Interface's name\n")
840{
841 int ret;
842 struct interface * ifp;
843
844 /* Call lib interface() */
845 ret = interface_cmd.func (self, vty, argc, argv);
846
847 ifp = vty->index;
848
849 /* Set ifindex
850 this only happens if interface is NOT in kernel */
851 if (ifp->ifindex == 0)
852 {
853 ifp->ifindex = if_new_intern_ifindex ();
854 UNSET_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE);
855 }
856
857 return ret;
858}
859
paul718e3742002-12-13 20:15:29 +0000860struct cmd_node interface_node =
861{
862 INTERFACE_NODE,
863 "%s(config-if)# ",
864 1
865};
866
867/* Show all or specified interface to vty. */
868DEFUN (show_interface, show_interface_cmd,
869 "show interface [IFNAME]",
870 SHOW_STR
871 "Interface status and configuration\n"
872 "Inteface name\n")
873{
hasso52dc7ee2004-09-23 19:18:23 +0000874 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000875 struct interface *ifp;
876
877#ifdef HAVE_PROC_NET_DEV
878 /* If system has interface statistics via proc file system, update
879 statistics. */
880 ifstat_update_proc ();
881#endif /* HAVE_PROC_NET_DEV */
882#ifdef HAVE_NET_RT_IFLIST
883 ifstat_update_sysctl ();
884#endif /* HAVE_NET_RT_IFLIST */
885
886 /* Specified interface print. */
887 if (argc != 0)
888 {
889 ifp = if_lookup_by_name (argv[0]);
890 if (ifp == NULL)
891 {
892 vty_out (vty, "%% Can't find interface %s%s", argv[0],
893 VTY_NEWLINE);
894 return CMD_WARNING;
895 }
896 if_dump_vty (vty, ifp);
897 return CMD_SUCCESS;
898 }
899
900 /* All interface print. */
901 for (node = listhead (iflist); node; nextnode (node))
902 if_dump_vty (vty, getdata (node));
903
904 return CMD_SUCCESS;
905}
906
907DEFUN (multicast,
908 multicast_cmd,
909 "multicast",
910 "Set multicast flag to interface\n")
911{
912 int ret;
913 struct interface *ifp;
914 struct zebra_if *if_data;
915
916 ifp = (struct interface *) vty->index;
paul48b33aa2002-12-13 20:52:52 +0000917 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
paul718e3742002-12-13 20:15:29 +0000918 {
paul48b33aa2002-12-13 20:52:52 +0000919 ret = if_set_flags (ifp, IFF_MULTICAST);
920 if (ret < 0)
921 {
922 vty_out (vty, "Can't set multicast flag%s", VTY_NEWLINE);
923 return CMD_WARNING;
924 }
925 if_refresh (ifp);
paul718e3742002-12-13 20:15:29 +0000926 }
paul718e3742002-12-13 20:15:29 +0000927 if_data = ifp->info;
928 if_data->multicast = IF_ZEBRA_MULTICAST_ON;
paul48b33aa2002-12-13 20:52:52 +0000929
paul718e3742002-12-13 20:15:29 +0000930 return CMD_SUCCESS;
931}
932
933DEFUN (no_multicast,
934 no_multicast_cmd,
935 "no multicast",
936 NO_STR
937 "Unset multicast flag to interface\n")
938{
939 int ret;
940 struct interface *ifp;
941 struct zebra_if *if_data;
942
943 ifp = (struct interface *) vty->index;
paul48b33aa2002-12-13 20:52:52 +0000944 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
paul718e3742002-12-13 20:15:29 +0000945 {
paul48b33aa2002-12-13 20:52:52 +0000946 ret = if_unset_flags (ifp, IFF_MULTICAST);
947 if (ret < 0)
948 {
949 vty_out (vty, "Can't unset multicast flag%s", VTY_NEWLINE);
950 return CMD_WARNING;
951 }
952 if_refresh (ifp);
paul718e3742002-12-13 20:15:29 +0000953 }
paul718e3742002-12-13 20:15:29 +0000954 if_data = ifp->info;
955 if_data->multicast = IF_ZEBRA_MULTICAST_OFF;
956
957 return CMD_SUCCESS;
958}
959
paul2e3b2e42002-12-13 21:03:13 +0000960DEFUN (linkdetect,
961 linkdetect_cmd,
962 "link-detect",
963 "Enable link detection on interface\n")
964{
paul2e3b2e42002-12-13 21:03:13 +0000965 struct interface *ifp;
966 int if_was_operative;
967
968 ifp = (struct interface *) vty->index;
969 if_was_operative = if_is_operative(ifp);
970 SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
971
972 /* When linkdetection is enabled, if might come down */
973 if (!if_is_operative(ifp) && if_was_operative) if_down(ifp);
974
975 /* FIXME: Will defer status change forwarding if interface
976 does not come down! */
977
978 return CMD_SUCCESS;
979}
980
981
982DEFUN (no_linkdetect,
983 no_linkdetect_cmd,
984 "no link-detect",
985 NO_STR
986 "Disable link detection on interface\n")
987{
paul2e3b2e42002-12-13 21:03:13 +0000988 struct interface *ifp;
989 int if_was_operative;
990
991 ifp = (struct interface *) vty->index;
992 if_was_operative = if_is_operative(ifp);
993 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
994
995 /* Interface may come up after disabling link detection */
996 if (if_is_operative(ifp) && !if_was_operative) if_up(ifp);
997
998 /* FIXME: see linkdetect_cmd */
999
1000 return CMD_SUCCESS;
1001}
1002
paul718e3742002-12-13 20:15:29 +00001003DEFUN (shutdown_if,
1004 shutdown_if_cmd,
1005 "shutdown",
1006 "Shutdown the selected interface\n")
1007{
1008 int ret;
1009 struct interface *ifp;
1010 struct zebra_if *if_data;
1011
1012 ifp = (struct interface *) vty->index;
1013 ret = if_unset_flags (ifp, IFF_UP);
1014 if (ret < 0)
1015 {
1016 vty_out (vty, "Can't shutdown interface%s", VTY_NEWLINE);
1017 return CMD_WARNING;
1018 }
1019 if_refresh (ifp);
1020 if_data = ifp->info;
1021 if_data->shutdown = IF_ZEBRA_SHUTDOWN_ON;
1022
1023 return CMD_SUCCESS;
1024}
1025
1026DEFUN (no_shutdown_if,
1027 no_shutdown_if_cmd,
1028 "no shutdown",
1029 NO_STR
1030 "Shutdown the selected interface\n")
1031{
1032 int ret;
1033 struct interface *ifp;
1034 struct zebra_if *if_data;
1035
1036 ifp = (struct interface *) vty->index;
1037 ret = if_set_flags (ifp, IFF_UP | IFF_RUNNING);
1038 if (ret < 0)
1039 {
1040 vty_out (vty, "Can't up interface%s", VTY_NEWLINE);
1041 return CMD_WARNING;
1042 }
1043 if_refresh (ifp);
1044 if_data = ifp->info;
1045 if_data->shutdown = IF_ZEBRA_SHUTDOWN_OFF;
1046
1047 return CMD_SUCCESS;
1048}
1049
1050DEFUN (bandwidth_if,
1051 bandwidth_if_cmd,
1052 "bandwidth <1-10000000>",
1053 "Set bandwidth informational parameter\n"
1054 "Bandwidth in kilobits\n")
1055{
1056 struct interface *ifp;
1057 unsigned int bandwidth;
1058
1059 ifp = (struct interface *) vty->index;
1060 bandwidth = strtol(argv[0], NULL, 10);
1061
1062 /* bandwidth range is <1-10000000> */
1063 if (bandwidth < 1 || bandwidth > 10000000)
1064 {
1065 vty_out (vty, "Bandwidth is invalid%s", VTY_NEWLINE);
1066 return CMD_WARNING;
1067 }
1068
1069 ifp->bandwidth = bandwidth;
1070
1071 /* force protocols to recalculate routes due to cost change */
paul2e3b2e42002-12-13 21:03:13 +00001072 if (if_is_operative (ifp))
paul718e3742002-12-13 20:15:29 +00001073 zebra_interface_up_update (ifp);
1074
1075 return CMD_SUCCESS;
1076}
1077
1078DEFUN (no_bandwidth_if,
1079 no_bandwidth_if_cmd,
1080 "no bandwidth",
1081 NO_STR
1082 "Set bandwidth informational parameter\n")
1083{
1084 struct interface *ifp;
1085
1086 ifp = (struct interface *) vty->index;
1087
1088 ifp->bandwidth = 0;
1089
1090 /* force protocols to recalculate routes due to cost change */
paul2e3b2e42002-12-13 21:03:13 +00001091 if (if_is_operative (ifp))
paul718e3742002-12-13 20:15:29 +00001092 zebra_interface_up_update (ifp);
1093
1094 return CMD_SUCCESS;
1095}
1096
1097ALIAS (no_bandwidth_if,
1098 no_bandwidth_if_val_cmd,
1099 "no bandwidth <1-10000000>",
1100 NO_STR
1101 "Set bandwidth informational parameter\n"
1102 "Bandwidth in kilobits\n")
1103
1104int
hasso39db97e2004-10-12 20:50:58 +00001105ip_address_install (struct vty *vty, struct interface *ifp,
1106 const char *addr_str, const char *peer_str,
1107 const char *label)
paul718e3742002-12-13 20:15:29 +00001108{
1109 struct prefix_ipv4 cp;
1110 struct connected *ifc;
1111 struct prefix_ipv4 *p;
paul718e3742002-12-13 20:15:29 +00001112 int ret;
1113
1114 ret = str2prefix_ipv4 (addr_str, &cp);
1115 if (ret <= 0)
1116 {
1117 vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
1118 return CMD_WARNING;
1119 }
1120
1121 ifc = connected_check_ipv4 (ifp, (struct prefix *) &cp);
1122 if (! ifc)
1123 {
1124 ifc = connected_new ();
1125 ifc->ifp = ifp;
1126
1127 /* Address. */
1128 p = prefix_ipv4_new ();
1129 *p = cp;
1130 ifc->address = (struct prefix *) p;
1131
1132 /* Broadcast. */
hasso3fb9cd62004-10-19 19:44:43 +00001133 if (p->prefixlen <= IPV4_MAX_PREFIXLEN-2)
paul718e3742002-12-13 20:15:29 +00001134 {
1135 p = prefix_ipv4_new ();
1136 *p = cp;
hasso3fb9cd62004-10-19 19:44:43 +00001137 p->prefix.s_addr = ipv4_broadcast_addr(p->prefix.s_addr,p->prefixlen);
paul718e3742002-12-13 20:15:29 +00001138 ifc->destination = (struct prefix *) p;
1139 }
1140
paul718e3742002-12-13 20:15:29 +00001141 /* Label. */
1142 if (label)
1143 ifc->label = strdup (label);
1144
1145 /* Add to linked list. */
1146 listnode_add (ifp->connected, ifc);
1147 }
1148
1149 /* This address is configured from zebra. */
1150 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
1151 SET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
1152
1153 /* In case of this route need to install kernel. */
1154 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)
1155 && CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
1156 {
1157 /* Some system need to up the interface to set IP address. */
1158 if (! if_is_up (ifp))
1159 {
1160 if_set_flags (ifp, IFF_UP | IFF_RUNNING);
1161 if_refresh (ifp);
1162 }
1163
1164 ret = if_set_prefix (ifp, ifc);
1165 if (ret < 0)
1166 {
1167 vty_out (vty, "%% Can't set interface IP address: %s.%s",
ajs6099b3b2004-11-20 02:06:59 +00001168 safe_strerror(errno), VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001169 return CMD_WARNING;
1170 }
1171
hassoeef1fe12004-10-03 18:46:08 +00001172 /* Add to subnet chain list (while marking secondary attribute). */
1173 if_subnet_add (ifp, ifc);
1174
paul718e3742002-12-13 20:15:29 +00001175 /* IP address propery set. */
1176 SET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
1177
1178 /* Update interface address information to protocol daemon. */
1179 zebra_interface_address_add_update (ifp, ifc);
1180
1181 /* If interface is up register connected route. */
paul2e3b2e42002-12-13 21:03:13 +00001182 if (if_is_operative(ifp))
paul718e3742002-12-13 20:15:29 +00001183 connected_up_ipv4 (ifp, ifc);
1184 }
1185
1186 return CMD_SUCCESS;
1187}
1188
1189int
hasso39db97e2004-10-12 20:50:58 +00001190ip_address_uninstall (struct vty *vty, struct interface *ifp,
1191 const char *addr_str, const char *peer_str,
1192 const char *label)
paul718e3742002-12-13 20:15:29 +00001193{
1194 struct prefix_ipv4 cp;
1195 struct connected *ifc;
1196 int ret;
1197
1198 /* Convert to prefix structure. */
1199 ret = str2prefix_ipv4 (addr_str, &cp);
1200 if (ret <= 0)
1201 {
1202 vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
1203 return CMD_WARNING;
1204 }
1205
1206 /* Check current interface address. */
1207 ifc = connected_check_ipv4 (ifp, (struct prefix *) &cp);
1208 if (! ifc)
1209 {
1210 vty_out (vty, "%% Can't find address%s", VTY_NEWLINE);
1211 return CMD_WARNING;
1212 }
1213
1214 /* This is not configured address. */
1215 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
1216 return CMD_WARNING;
1217
1218 /* This is not real address or interface is not active. */
1219 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)
1220 || ! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
1221 {
1222 listnode_delete (ifp->connected, ifc);
1223 connected_free (ifc);
1224 return CMD_WARNING;
1225 }
1226
1227 /* This is real route. */
1228 ret = if_unset_prefix (ifp, ifc);
1229 if (ret < 0)
1230 {
1231 vty_out (vty, "%% Can't unset interface IP address: %s.%s",
ajs6099b3b2004-11-20 02:06:59 +00001232 safe_strerror(errno), VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001233 return CMD_WARNING;
1234 }
1235
hassoeef1fe12004-10-03 18:46:08 +00001236#if 0
paul718e3742002-12-13 20:15:29 +00001237 /* Redistribute this information. */
1238 zebra_interface_address_delete_update (ifp, ifc);
1239
1240 /* Remove connected route. */
1241 connected_down_ipv4 (ifp, ifc);
1242
1243 /* Free address information. */
1244 listnode_delete (ifp->connected, ifc);
1245 connected_free (ifc);
hassoeef1fe12004-10-03 18:46:08 +00001246#endif
paul718e3742002-12-13 20:15:29 +00001247
1248 return CMD_SUCCESS;
1249}
1250
1251DEFUN (ip_address,
1252 ip_address_cmd,
1253 "ip address A.B.C.D/M",
1254 "Interface Internet Protocol config commands\n"
1255 "Set the IP address of an interface\n"
1256 "IP address (e.g. 10.0.0.1/8)\n")
1257{
hassoeef1fe12004-10-03 18:46:08 +00001258 return ip_address_install (vty, vty->index, argv[0], NULL, NULL);
paul718e3742002-12-13 20:15:29 +00001259}
1260
1261DEFUN (no_ip_address,
1262 no_ip_address_cmd,
1263 "no ip address A.B.C.D/M",
1264 NO_STR
1265 "Interface Internet Protocol config commands\n"
1266 "Set the IP address of an interface\n"
1267 "IP Address (e.g. 10.0.0.1/8)")
1268{
hassoeef1fe12004-10-03 18:46:08 +00001269 return ip_address_uninstall (vty, vty->index, argv[0], NULL, NULL);
paul718e3742002-12-13 20:15:29 +00001270}
1271
1272#ifdef HAVE_NETLINK
paul718e3742002-12-13 20:15:29 +00001273DEFUN (ip_address_label,
1274 ip_address_label_cmd,
1275 "ip address A.B.C.D/M label LINE",
1276 "Interface Internet Protocol config commands\n"
1277 "Set the IP address of an interface\n"
1278 "IP address (e.g. 10.0.0.1/8)\n"
1279 "Label of this address\n"
1280 "Label\n")
1281{
hassoeef1fe12004-10-03 18:46:08 +00001282 return ip_address_install (vty, vty->index, argv[0], NULL, argv[1]);
paul718e3742002-12-13 20:15:29 +00001283}
1284
1285DEFUN (no_ip_address_label,
1286 no_ip_address_label_cmd,
1287 "no ip address A.B.C.D/M label LINE",
1288 NO_STR
1289 "Interface Internet Protocol config commands\n"
1290 "Set the IP address of an interface\n"
1291 "IP address (e.g. 10.0.0.1/8)\n"
1292 "Label of this address\n"
1293 "Label\n")
1294{
hassoeef1fe12004-10-03 18:46:08 +00001295 return ip_address_uninstall (vty, vty->index, argv[0], NULL, argv[1]);
paul718e3742002-12-13 20:15:29 +00001296}
1297#endif /* HAVE_NETLINK */
1298
1299#ifdef HAVE_IPV6
1300int
hasso39db97e2004-10-12 20:50:58 +00001301ipv6_address_install (struct vty *vty, struct interface *ifp,
1302 const char *addr_str, const char *peer_str,
1303 const char *label, int secondary)
paul718e3742002-12-13 20:15:29 +00001304{
1305 struct prefix_ipv6 cp;
1306 struct connected *ifc;
1307 struct prefix_ipv6 *p;
1308 int ret;
1309
1310 ret = str2prefix_ipv6 (addr_str, &cp);
1311 if (ret <= 0)
1312 {
1313 vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
1314 return CMD_WARNING;
1315 }
1316
1317 ifc = connected_check_ipv6 (ifp, (struct prefix *) &cp);
1318 if (! ifc)
1319 {
1320 ifc = connected_new ();
1321 ifc->ifp = ifp;
1322
1323 /* Address. */
1324 p = prefix_ipv6_new ();
1325 *p = cp;
1326 ifc->address = (struct prefix *) p;
1327
1328 /* Secondary. */
1329 if (secondary)
1330 SET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
1331
1332 /* Label. */
1333 if (label)
1334 ifc->label = strdup (label);
1335
1336 /* Add to linked list. */
1337 listnode_add (ifp->connected, ifc);
1338 }
1339
1340 /* This address is configured from zebra. */
1341 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
1342 SET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
1343
1344 /* In case of this route need to install kernel. */
1345 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)
1346 && CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
1347 {
1348 /* Some system need to up the interface to set IP address. */
1349 if (! if_is_up (ifp))
1350 {
1351 if_set_flags (ifp, IFF_UP | IFF_RUNNING);
1352 if_refresh (ifp);
1353 }
1354
1355 ret = if_prefix_add_ipv6 (ifp, ifc);
1356
1357 if (ret < 0)
1358 {
1359 vty_out (vty, "%% Can't set interface IP address: %s.%s",
ajs6099b3b2004-11-20 02:06:59 +00001360 safe_strerror(errno), VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001361 return CMD_WARNING;
1362 }
1363
1364 /* IP address propery set. */
1365 SET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
1366
1367 /* Update interface address information to protocol daemon. */
1368 zebra_interface_address_add_update (ifp, ifc);
1369
1370 /* If interface is up register connected route. */
paul2e3b2e42002-12-13 21:03:13 +00001371 if (if_is_operative(ifp))
paul718e3742002-12-13 20:15:29 +00001372 connected_up_ipv6 (ifp, ifc);
1373 }
1374
1375 return CMD_SUCCESS;
1376}
1377
1378int
hasso39db97e2004-10-12 20:50:58 +00001379ipv6_address_uninstall (struct vty *vty, struct interface *ifp,
1380 const char *addr_str, const char *peer_str,
1381 const char *label, int secondry)
paul718e3742002-12-13 20:15:29 +00001382{
1383 struct prefix_ipv6 cp;
1384 struct connected *ifc;
1385 int ret;
1386
1387 /* Convert to prefix structure. */
1388 ret = str2prefix_ipv6 (addr_str, &cp);
1389 if (ret <= 0)
1390 {
1391 vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
1392 return CMD_WARNING;
1393 }
1394
1395 /* Check current interface address. */
1396 ifc = connected_check_ipv6 (ifp, (struct prefix *) &cp);
1397 if (! ifc)
1398 {
1399 vty_out (vty, "%% Can't find address%s", VTY_NEWLINE);
1400 return CMD_WARNING;
1401 }
1402
1403 /* This is not configured address. */
1404 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
1405 return CMD_WARNING;
1406
1407 /* This is not real address or interface is not active. */
1408 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)
1409 || ! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
1410 {
1411 listnode_delete (ifp->connected, ifc);
1412 connected_free (ifc);
1413 return CMD_WARNING;
1414 }
1415
1416 /* This is real route. */
1417 ret = if_prefix_delete_ipv6 (ifp, ifc);
1418 if (ret < 0)
1419 {
1420 vty_out (vty, "%% Can't unset interface IP address: %s.%s",
ajs6099b3b2004-11-20 02:06:59 +00001421 safe_strerror(errno), VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001422 return CMD_WARNING;
1423 }
1424
1425 /* Redistribute this information. */
1426 zebra_interface_address_delete_update (ifp, ifc);
1427
1428 /* Remove connected route. */
1429 connected_down_ipv6 (ifp, ifc);
1430
1431 /* Free address information. */
1432 listnode_delete (ifp->connected, ifc);
1433 connected_free (ifc);
1434
1435 return CMD_SUCCESS;
1436}
1437
1438DEFUN (ipv6_address,
1439 ipv6_address_cmd,
1440 "ipv6 address X:X::X:X/M",
hassoe23949c2004-03-11 15:54:02 +00001441 "Interface IPv6 config commands\n"
paul718e3742002-12-13 20:15:29 +00001442 "Set the IP address of an interface\n"
1443 "IPv6 address (e.g. 3ffe:506::1/48)\n")
1444{
1445 return ipv6_address_install (vty, vty->index, argv[0], NULL, NULL, 0);
1446}
1447
1448DEFUN (no_ipv6_address,
1449 no_ipv6_address_cmd,
1450 "no ipv6 address X:X::X:X/M",
1451 NO_STR
hassoe23949c2004-03-11 15:54:02 +00001452 "Interface IPv6 config commands\n"
paul718e3742002-12-13 20:15:29 +00001453 "Set the IP address of an interface\n"
1454 "IPv6 address (e.g. 3ffe:506::1/48)\n")
1455{
1456 return ipv6_address_uninstall (vty, vty->index, argv[0], NULL, NULL, 0);
1457}
1458#endif /* HAVE_IPV6 */
1459
paul718e3742002-12-13 20:15:29 +00001460int
1461if_config_write (struct vty *vty)
1462{
hasso52dc7ee2004-09-23 19:18:23 +00001463 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001464 struct interface *ifp;
1465 char buf[BUFSIZ];
1466
1467 for (node = listhead (iflist); node; nextnode (node))
1468 {
1469 struct zebra_if *if_data;
hasso52dc7ee2004-09-23 19:18:23 +00001470 struct listnode *addrnode;
paul718e3742002-12-13 20:15:29 +00001471 struct connected *ifc;
1472 struct prefix *p;
1473
1474 ifp = getdata (node);
1475 if_data = ifp->info;
1476
1477 vty_out (vty, "interface %s%s", ifp->name,
1478 VTY_NEWLINE);
1479
1480 if (ifp->desc)
1481 vty_out (vty, " description %s%s", ifp->desc,
1482 VTY_NEWLINE);
1483
1484 /* Assign bandwidth here to avoid unnecessary interface flap
1485 while processing config script */
1486 if (ifp->bandwidth != 0)
1487 vty_out(vty, " bandwidth %u%s", ifp->bandwidth, VTY_NEWLINE);
1488
paul2e3b2e42002-12-13 21:03:13 +00001489 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION))
1490 vty_out(vty, " link-detect%s", VTY_NEWLINE);
1491
paul718e3742002-12-13 20:15:29 +00001492 for (addrnode = listhead (ifp->connected); addrnode; nextnode (addrnode))
1493 {
1494 ifc = getdata (addrnode);
1495 if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
1496 {
1497 p = ifc->address;
1498 vty_out (vty, " ip%s address %s/%d",
1499 p->family == AF_INET ? "" : "v6",
1500 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
1501 p->prefixlen);
1502
paul718e3742002-12-13 20:15:29 +00001503 if (ifc->label)
1504 vty_out (vty, " label %s", ifc->label);
1505
1506 vty_out (vty, "%s", VTY_NEWLINE);
1507 }
1508 }
1509
1510 if (if_data)
1511 {
1512 if (if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)
1513 vty_out (vty, " shutdown%s", VTY_NEWLINE);
1514
1515 if (if_data->multicast != IF_ZEBRA_MULTICAST_UNSPEC)
1516 vty_out (vty, " %smulticast%s",
1517 if_data->multicast == IF_ZEBRA_MULTICAST_ON ? "" : "no ",
1518 VTY_NEWLINE);
1519 }
1520
1521#ifdef RTADV
1522 rtadv_config_write (vty, ifp);
1523#endif /* RTADV */
1524
hassoca776982004-06-12 14:33:05 +00001525#ifdef HAVE_IRDP
1526 irdp_config_write (vty, ifp);
1527#endif /* IRDP */
1528
paul718e3742002-12-13 20:15:29 +00001529 vty_out (vty, "!%s", VTY_NEWLINE);
1530 }
1531 return 0;
1532}
1533
1534/* Allocate and initialize interface vector. */
1535void
1536zebra_if_init ()
1537{
1538 /* Initialize interface and new hook. */
1539 if_init ();
1540 if_add_hook (IF_NEW_HOOK, if_zebra_new_hook);
1541 if_add_hook (IF_DELETE_HOOK, if_zebra_delete_hook);
1542
1543 /* Install configuration write function. */
1544 install_node (&interface_node, if_config_write);
1545
1546 install_element (VIEW_NODE, &show_interface_cmd);
1547 install_element (ENABLE_NODE, &show_interface_cmd);
1548 install_element (CONFIG_NODE, &zebra_interface_cmd);
paulbfc13532003-05-24 06:40:04 +00001549 install_element (CONFIG_NODE, &no_interface_cmd);
paul718e3742002-12-13 20:15:29 +00001550 install_default (INTERFACE_NODE);
1551 install_element (INTERFACE_NODE, &interface_desc_cmd);
1552 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
1553 install_element (INTERFACE_NODE, &multicast_cmd);
1554 install_element (INTERFACE_NODE, &no_multicast_cmd);
paul2e3b2e42002-12-13 21:03:13 +00001555 install_element (INTERFACE_NODE, &linkdetect_cmd);
1556 install_element (INTERFACE_NODE, &no_linkdetect_cmd);
paul718e3742002-12-13 20:15:29 +00001557 install_element (INTERFACE_NODE, &shutdown_if_cmd);
1558 install_element (INTERFACE_NODE, &no_shutdown_if_cmd);
1559 install_element (INTERFACE_NODE, &bandwidth_if_cmd);
1560 install_element (INTERFACE_NODE, &no_bandwidth_if_cmd);
1561 install_element (INTERFACE_NODE, &no_bandwidth_if_val_cmd);
1562 install_element (INTERFACE_NODE, &ip_address_cmd);
1563 install_element (INTERFACE_NODE, &no_ip_address_cmd);
1564#ifdef HAVE_IPV6
1565 install_element (INTERFACE_NODE, &ipv6_address_cmd);
1566 install_element (INTERFACE_NODE, &no_ipv6_address_cmd);
1567#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +00001568#ifdef HAVE_NETLINK
paul718e3742002-12-13 20:15:29 +00001569 install_element (INTERFACE_NODE, &ip_address_label_cmd);
paul718e3742002-12-13 20:15:29 +00001570 install_element (INTERFACE_NODE, &no_ip_address_label_cmd);
paul718e3742002-12-13 20:15:29 +00001571#endif /* HAVE_NETLINK */
1572}