]> git.proxmox.com Git - mirror_frr.git/blob - zebra/interface.c
staticd: Start the addition of a staticd
[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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <zebra.h>
23
24 #include "if.h"
25 #include "vty.h"
26 #include "sockunion.h"
27 #include "prefix.h"
28 #include "command.h"
29 #include "memory.h"
30 #include "zebra_memory.h"
31 #include "ioctl.h"
32 #include "connected.h"
33 #include "log.h"
34 #include "zclient.h"
35 #include "vrf.h"
36
37 #include "zebra/rtadv.h"
38 #include "zebra_ns.h"
39 #include "zebra_vrf.h"
40 #include "zebra/interface.h"
41 #include "zebra/rib.h"
42 #include "zebra/rt.h"
43 #include "zebra/zserv.h"
44 #include "zebra/redistribute.h"
45 #include "zebra/debug.h"
46 #include "zebra/irdp.h"
47 #include "zebra/zebra_ptm.h"
48 #include "zebra/rt_netlink.h"
49 #include "zebra/interface.h"
50 #include "zebra/zebra_vxlan.h"
51 #include "zebra/zebra_static.h"
52
53 #define ZEBRA_PTM_SUPPORT
54
55 DEFINE_HOOK(zebra_if_extra_info, (struct vty * vty, struct interface *ifp),
56 (vty, ifp))
57 DEFINE_HOOK(zebra_if_config_wr, (struct vty * vty, struct interface *ifp),
58 (vty, ifp))
59
60
61 static void if_down_del_nbr_connected(struct interface *ifp);
62
63 static int if_zebra_speed_update(struct thread *thread)
64 {
65 struct interface *ifp = THREAD_ARG(thread);
66 struct zebra_if *zif = ifp->info;
67 uint32_t new_speed;
68
69 zif->speed_update = NULL;
70
71 new_speed = kernel_get_speed(ifp);
72 if (new_speed != ifp->speed) {
73 zlog_info("%s: %s old speed: %u new speed: %u",
74 __PRETTY_FUNCTION__, ifp->name, ifp->speed,
75 new_speed);
76 ifp->speed = new_speed;
77 if_add_update(ifp);
78 }
79
80 return 1;
81 }
82
83 static void zebra_if_node_destroy(route_table_delegate_t *delegate,
84 struct route_table *table,
85 struct route_node *node)
86 {
87 if (node->info)
88 list_delete_and_null((struct list **)&node->info);
89 route_node_destroy(delegate, table, node);
90 }
91
92 route_table_delegate_t zebra_if_table_delegate = {
93 .create_node = route_node_create,
94 .destroy_node = zebra_if_node_destroy};
95
96 /* Called when new interface is added. */
97 static int if_zebra_new_hook(struct interface *ifp)
98 {
99 struct zebra_if *zebra_if;
100
101 zebra_if = XCALLOC(MTYPE_TMP, sizeof(struct zebra_if));
102
103 zebra_if->multicast = IF_ZEBRA_MULTICAST_UNSPEC;
104 zebra_if->shutdown = IF_ZEBRA_SHUTDOWN_OFF;
105 zebra_ptm_if_init(zebra_if);
106
107 ifp->ptm_enable = zebra_ptm_get_enable_state();
108 #if defined(HAVE_RTADV)
109 {
110 /* Set default router advertise values. */
111 struct rtadvconf *rtadv;
112
113 rtadv = &zebra_if->rtadv;
114
115 rtadv->AdvSendAdvertisements = 0;
116 rtadv->MaxRtrAdvInterval = RTADV_MAX_RTR_ADV_INTERVAL;
117 rtadv->MinRtrAdvInterval = RTADV_MIN_RTR_ADV_INTERVAL;
118 rtadv->AdvIntervalTimer = 0;
119 rtadv->AdvManagedFlag = 0;
120 rtadv->AdvOtherConfigFlag = 0;
121 rtadv->AdvHomeAgentFlag = 0;
122 rtadv->AdvLinkMTU = 0;
123 rtadv->AdvReachableTime = 0;
124 rtadv->AdvRetransTimer = 0;
125 rtadv->AdvCurHopLimit = 0;
126 rtadv->AdvDefaultLifetime =
127 -1; /* derive from MaxRtrAdvInterval */
128 rtadv->HomeAgentPreference = 0;
129 rtadv->HomeAgentLifetime =
130 -1; /* derive from AdvDefaultLifetime */
131 rtadv->AdvIntervalOption = 0;
132 rtadv->DefaultPreference = RTADV_PREF_MEDIUM;
133
134 rtadv->AdvPrefixList = list_new();
135 }
136 #endif /* HAVE_RTADV */
137
138 /* Initialize installed address chains tree. */
139 zebra_if->ipv4_subnets =
140 route_table_init_with_delegate(&zebra_if_table_delegate);
141
142 ifp->info = zebra_if;
143
144 /*
145 * Some platforms are telling us that the interface is
146 * up and ready to go. When we check the speed we
147 * sometimes get the wrong value. Wait a couple
148 * of seconds and ask again. Hopefully it's all settled
149 * down upon startup.
150 */
151 thread_add_timer(zebrad.master, if_zebra_speed_update, ifp, 15,
152 &zebra_if->speed_update);
153 return 0;
154 }
155
156 /* Called when interface is deleted. */
157 static int if_zebra_delete_hook(struct interface *ifp)
158 {
159 struct zebra_if *zebra_if;
160
161 if (ifp->info) {
162 zebra_if = ifp->info;
163
164 /* Free installed address chains tree. */
165 if (zebra_if->ipv4_subnets)
166 route_table_finish(zebra_if->ipv4_subnets);
167 #if defined(HAVE_RTADV)
168
169 struct rtadvconf *rtadv;
170
171 rtadv = &zebra_if->rtadv;
172 list_delete_and_null(&rtadv->AdvPrefixList);
173 #endif /* HAVE_RTADV */
174
175 THREAD_OFF(zebra_if->speed_update);
176
177 XFREE(MTYPE_TMP, zebra_if);
178 }
179
180 return 0;
181 }
182
183 /* Build the table key */
184 static void if_build_key(uint32_t ifindex, struct prefix *p)
185 {
186 p->family = AF_INET;
187 p->prefixlen = IPV4_MAX_BITLEN;
188 p->u.prefix4.s_addr = ifindex;
189 }
190
191 /* Link an interface in a per NS interface tree */
192 struct interface *if_link_per_ns(struct zebra_ns *ns, struct interface *ifp)
193 {
194 struct prefix p;
195 struct route_node *rn;
196
197 if (ifp->ifindex == IFINDEX_INTERNAL)
198 return NULL;
199
200 if_build_key(ifp->ifindex, &p);
201 rn = route_node_get(ns->if_table, &p);
202 if (rn->info) {
203 ifp = (struct interface *)rn->info;
204 route_unlock_node(rn); /* get */
205 ifp->node = rn;
206 return ifp;
207 }
208
209 rn->info = ifp;
210 ifp->node = rn;
211
212 return ifp;
213 }
214
215 /* Delete a VRF. This is called in vrf_terminate(). */
216 void if_unlink_per_ns(struct interface *ifp)
217 {
218 ifp->node->info = NULL;
219 route_unlock_node(ifp->node);
220 ifp->node = NULL;
221 }
222
223 /* Look up an interface by identifier within a NS */
224 struct interface *if_lookup_by_index_per_ns(struct zebra_ns *ns,
225 uint32_t ifindex)
226 {
227 struct prefix p;
228 struct route_node *rn;
229 struct interface *ifp = NULL;
230
231 if_build_key(ifindex, &p);
232 rn = route_node_lookup(ns->if_table, &p);
233 if (rn) {
234 ifp = (struct interface *)rn->info;
235 route_unlock_node(rn); /* lookup */
236 }
237 return ifp;
238 }
239
240 /* Look up an interface by name within a NS */
241 struct interface *if_lookup_by_name_per_ns(struct zebra_ns *ns,
242 const char *ifname)
243 {
244 struct route_node *rn;
245 struct interface *ifp;
246
247 for (rn = route_top(ns->if_table); rn; rn = route_next(rn)) {
248 ifp = (struct interface *)rn->info;
249 if (ifp && strcmp(ifp->name, ifname) == 0)
250 return (ifp);
251 }
252
253 return NULL;
254 }
255
256 /* this function must be used only if the vrf backend
257 * is a netns backend
258 */
259 struct interface *if_lookup_by_name_not_ns(ns_id_t ns_id,
260 const char *ifname)
261 {
262 struct interface *ifp;
263 struct ns *ns;
264
265 RB_FOREACH (ns, ns_head, &ns_tree) {
266 if (ns->ns_id == ns_id)
267 continue;
268 /* if_delete_update has removed interface
269 * from zns->if_table
270 * so to look for interface, use the vrf list
271 */
272 ifp = if_lookup_by_name(ifname, (vrf_id_t)ns->ns_id);
273 if (!ifp)
274 continue;
275 return ifp;
276 }
277 return NULL;
278 }
279
280 const char *ifindex2ifname_per_ns(struct zebra_ns *zns, unsigned int ifindex)
281 {
282 struct interface *ifp;
283
284 return ((ifp = if_lookup_by_index_per_ns(zns, ifindex)) != NULL)
285 ? ifp->name
286 : "unknown";
287 }
288
289 /* Tie an interface address to its derived subnet list of addresses. */
290 int if_subnet_add(struct interface *ifp, struct connected *ifc)
291 {
292 struct route_node *rn;
293 struct zebra_if *zebra_if;
294 struct prefix cp;
295 struct list *addr_list;
296
297 assert(ifp && ifp->info && ifc);
298 zebra_if = ifp->info;
299
300 /* Get address derived subnet node and associated address list, while
301 marking
302 address secondary attribute appropriately. */
303 cp = *CONNECTED_PREFIX(ifc);
304 apply_mask(&cp);
305 rn = route_node_get(zebra_if->ipv4_subnets, &cp);
306
307 if ((addr_list = rn->info))
308 SET_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY);
309 else {
310 UNSET_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY);
311 rn->info = addr_list = list_new();
312 route_lock_node(rn);
313 }
314
315 /* Tie address at the tail of address list. */
316 listnode_add(addr_list, ifc);
317
318 /* Return list element count. */
319 return (addr_list->count);
320 }
321
322 /* Untie an interface address from its derived subnet list of addresses. */
323 int if_subnet_delete(struct interface *ifp, struct connected *ifc)
324 {
325 struct route_node *rn;
326 struct zebra_if *zebra_if;
327 struct list *addr_list;
328 struct prefix cp;
329
330 assert(ifp && ifp->info && ifc);
331 zebra_if = ifp->info;
332
333 cp = *CONNECTED_PREFIX(ifc);
334 apply_mask(&cp);
335
336 /* Get address derived subnet node. */
337 rn = route_node_lookup(zebra_if->ipv4_subnets, &cp);
338 if (!(rn && rn->info)) {
339 zlog_warn(
340 "Trying to remove an address from an unknown subnet."
341 " (please report this bug)");
342 return -1;
343 }
344 route_unlock_node(rn);
345
346 /* Untie address from subnet's address list. */
347 addr_list = rn->info;
348
349 /* Deleting an address that is not registered is a bug.
350 * In any case, we shouldn't decrement the lock counter if the address
351 * is unknown. */
352 if (!listnode_lookup(addr_list, ifc)) {
353 zlog_warn(
354 "Trying to remove an address from a subnet where it is not"
355 " currently registered. (please report this bug)");
356 return -1;
357 }
358
359 listnode_delete(addr_list, ifc);
360 route_unlock_node(rn);
361
362 /* Return list element count, if not empty. */
363 if (addr_list->count) {
364 /* If deleted address is primary, mark subsequent one as such
365 * and distribute. */
366 if (!CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY)) {
367 ifc = listgetdata(
368 (struct listnode *)listhead(addr_list));
369 zebra_interface_address_delete_update(ifp, ifc);
370 UNSET_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY);
371 /* XXX: Linux kernel removes all the secondary addresses
372 * when the primary
373 * address is removed. We could try to work around that,
374 * though this is
375 * non-trivial. */
376 zebra_interface_address_add_update(ifp, ifc);
377 }
378
379 return addr_list->count;
380 }
381
382 /* Otherwise, free list and route node. */
383 list_delete_and_null(&addr_list);
384 rn->info = NULL;
385 route_unlock_node(rn);
386
387 return 0;
388 }
389
390 /* if_flags_mangle: A place for hacks that require mangling
391 * or tweaking the interface flags.
392 *
393 * ******************** Solaris flags hacks **************************
394 *
395 * Solaris IFF_UP flag reflects only the primary interface as the
396 * routing socket only sends IFINFO for the primary interface. Hence
397 * ~IFF_UP does not per se imply all the logical interfaces are also
398 * down - which we only know of as addresses. Instead we must determine
399 * whether the interface really is up or not according to how many
400 * addresses are still attached. (Solaris always sends RTM_DELADDR if
401 * an interface, logical or not, goes ~IFF_UP).
402 *
403 * Ie, we mangle IFF_UP to *additionally* reflect whether or not there
404 * are addresses left in struct connected, not just the actual underlying
405 * IFF_UP flag.
406 *
407 * We must hence remember the real state of IFF_UP, which we do in
408 * struct zebra_if.primary_state.
409 *
410 * Setting IFF_UP within zebra to administratively shutdown the
411 * interface will affect only the primary interface/address on Solaris.
412 ************************End Solaris flags hacks ***********************
413 */
414 static void if_flags_mangle(struct interface *ifp, uint64_t *newflags)
415 {
416 #ifdef SUNOS_5
417 struct zebra_if *zif = ifp->info;
418
419 zif->primary_state = *newflags & (IFF_UP & 0xff);
420
421 if (CHECK_FLAG(zif->primary_state, IFF_UP)
422 || listcount(ifp->connected) > 0)
423 SET_FLAG(*newflags, IFF_UP);
424 else
425 UNSET_FLAG(*newflags, IFF_UP);
426 #endif /* SUNOS_5 */
427 }
428
429 /* Update the flags field of the ifp with the new flag set provided.
430 * Take whatever actions are required for any changes in flags we care
431 * about.
432 *
433 * newflags should be the raw value, as obtained from the OS.
434 */
435 void if_flags_update(struct interface *ifp, uint64_t newflags)
436 {
437 if_flags_mangle(ifp, &newflags);
438
439 if (if_is_no_ptm_operative(ifp)) {
440 /* operative -> inoperative? */
441 ifp->flags = newflags;
442 if (!if_is_operative(ifp))
443 if_down(ifp);
444 } else {
445 /* inoperative -> operative? */
446 ifp->flags = newflags;
447 if (if_is_operative(ifp))
448 if_up(ifp);
449 }
450 }
451
452 /* Wake up configured address if it is not in current kernel
453 address. */
454 static void if_addr_wakeup(struct interface *ifp)
455 {
456 struct listnode *node, *nnode;
457 struct connected *ifc;
458 struct prefix *p;
459 int ret;
460
461 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, ifc)) {
462 p = ifc->address;
463
464 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED)
465 && !CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)) {
466 /* Address check. */
467 if (p->family == AF_INET) {
468 if (!if_is_up(ifp)) {
469 /* Assume zebra is configured like
470 * following:
471 *
472 * interface gre0
473 * ip addr 192.0.2.1/24
474 * !
475 *
476 * As soon as zebra becomes first aware
477 * that gre0 exists in the
478 * kernel, it will set gre0 up and
479 * configure its addresses.
480 *
481 * (This may happen at startup when the
482 * interface already exists
483 * or during runtime when the interface
484 * is added to the kernel)
485 *
486 * XXX: IRDP code is calling here via
487 * if_add_update - this seems
488 * somewhat weird.
489 * XXX: RUNNING is not a settable flag
490 * on any system
491 * I (paulj) am aware of.
492 */
493 if_set_flags(ifp, IFF_UP | IFF_RUNNING);
494 if_refresh(ifp);
495 }
496
497 ret = if_set_prefix(ifp, ifc);
498 if (ret < 0) {
499 zlog_warn(
500 "Can't set interface's address: %s",
501 safe_strerror(errno));
502 continue;
503 }
504
505 SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
506 /* The address will be advertised to zebra
507 * clients when the notification
508 * from the kernel has been received.
509 * It will also be added to the interface's
510 * subnet list then. */
511 }
512 if (p->family == AF_INET6) {
513 if (!if_is_up(ifp)) {
514 /* See long comment above */
515 if_set_flags(ifp, IFF_UP | IFF_RUNNING);
516 if_refresh(ifp);
517 }
518
519 ret = if_prefix_add_ipv6(ifp, ifc);
520 if (ret < 0) {
521 zlog_warn(
522 "Can't set interface's address: %s",
523 safe_strerror(errno));
524 continue;
525 }
526
527 SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
528 /* The address will be advertised to zebra
529 * clients when the notification
530 * from the kernel has been received. */
531 }
532 }
533 }
534 }
535
536 /* Handle interface addition */
537 void if_add_update(struct interface *ifp)
538 {
539 struct zebra_if *if_data;
540 struct zebra_ns *zns;
541 struct zebra_vrf *zvrf = vrf_info_lookup(ifp->vrf_id);
542
543 /* case interface populate before vrf enabled */
544 if (zvrf->zns)
545 zns = zvrf->zns;
546 else
547 zns = zebra_ns_lookup(NS_DEFAULT);
548 if_link_per_ns(zns, ifp);
549 if_data = ifp->info;
550 assert(if_data);
551
552 if (if_data->multicast == IF_ZEBRA_MULTICAST_ON)
553 if_set_flags(ifp, IFF_MULTICAST);
554 else if (if_data->multicast == IF_ZEBRA_MULTICAST_OFF)
555 if_unset_flags(ifp, IFF_MULTICAST);
556
557 zebra_ptm_if_set_ptm_state(ifp, if_data);
558
559 zebra_interface_add_update(ifp);
560
561 if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
562 SET_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE);
563
564 if (if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON) {
565 if (IS_ZEBRA_DEBUG_KERNEL)
566 zlog_debug(
567 "interface %s vrf %u index %d is shutdown. "
568 "Won't wake it up.",
569 ifp->name, ifp->vrf_id, ifp->ifindex);
570 return;
571 }
572
573 if_addr_wakeup(ifp);
574
575 if (IS_ZEBRA_DEBUG_KERNEL)
576 zlog_debug(
577 "interface %s vrf %u index %d becomes active.",
578 ifp->name, ifp->vrf_id, ifp->ifindex);
579
580 } else {
581 if (IS_ZEBRA_DEBUG_KERNEL)
582 zlog_debug("interface %s vrf %u index %d is added.",
583 ifp->name, ifp->vrf_id, ifp->ifindex);
584 }
585 }
586
587 /* Install connected routes corresponding to an interface. */
588 static void if_install_connected(struct interface *ifp)
589 {
590 struct listnode *node;
591 struct listnode *next;
592 struct connected *ifc;
593
594 if (ifp->connected) {
595 for (ALL_LIST_ELEMENTS(ifp->connected, node, next, ifc)) {
596 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
597 zebra_interface_address_add_update(ifp, ifc);
598
599 connected_up(ifp, ifc);
600 }
601 }
602 }
603
604 /* Uninstall connected routes corresponding to an interface. */
605 static void if_uninstall_connected(struct interface *ifp)
606 {
607 struct listnode *node;
608 struct listnode *next;
609 struct connected *ifc;
610
611 if (ifp->connected) {
612 for (ALL_LIST_ELEMENTS(ifp->connected, node, next, ifc)) {
613 zebra_interface_address_delete_update(ifp, ifc);
614 connected_down(ifp, ifc);
615 }
616 }
617 }
618
619 /* Uninstall and delete connected routes corresponding to an interface. */
620 /* TODO - Check why IPv4 handling here is different from install or if_down */
621 static void if_delete_connected(struct interface *ifp)
622 {
623 struct connected *ifc;
624 struct prefix cp;
625 struct route_node *rn;
626 struct zebra_if *zebra_if;
627 struct listnode *node;
628 struct listnode *last = NULL;
629
630 zebra_if = ifp->info;
631
632 if (!ifp->connected)
633 return;
634
635 while ((node = (last ? last->next : listhead(ifp->connected)))) {
636 ifc = listgetdata(node);
637
638 cp = *CONNECTED_PREFIX(ifc);
639 apply_mask(&cp);
640
641 if (cp.family == AF_INET
642 && (rn = route_node_lookup(zebra_if->ipv4_subnets, &cp))) {
643 struct listnode *anode;
644 struct listnode *next;
645 struct listnode *first;
646 struct list *addr_list;
647
648 route_unlock_node(rn);
649 addr_list = (struct list *)rn->info;
650
651 /* Remove addresses, secondaries first. */
652 first = listhead(addr_list);
653 if (first)
654 for (anode = first->next; anode || first;
655 anode = next) {
656 if (!anode) {
657 anode = first;
658 first = NULL;
659 }
660 next = anode->next;
661
662 ifc = listgetdata(anode);
663 connected_down(ifp, ifc);
664
665 /* XXX: We have to send notifications
666 * here explicitly, because we destroy
667 * the ifc before receiving the
668 * notification about the address being
669 * deleted.
670 */
671 zebra_interface_address_delete_update(
672 ifp, ifc);
673
674 UNSET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
675 UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
676
677 /* Remove from subnet chain. */
678 list_delete_node(addr_list, anode);
679 route_unlock_node(rn);
680
681 /* Remove from interface address list
682 * (unconditionally). */
683 if (!CHECK_FLAG(ifc->conf,
684 ZEBRA_IFC_CONFIGURED)) {
685 listnode_delete(ifp->connected,
686 ifc);
687 connected_free(ifc);
688 } else
689 last = node;
690 }
691
692 /* Free chain list and respective route node. */
693 list_delete_and_null(&addr_list);
694 rn->info = NULL;
695 route_unlock_node(rn);
696 } else if (cp.family == AF_INET6) {
697 connected_down(ifp, ifc);
698
699 zebra_interface_address_delete_update(ifp, ifc);
700
701 UNSET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
702 UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
703
704 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
705 last = node;
706 else {
707 listnode_delete(ifp->connected, ifc);
708 connected_free(ifc);
709 }
710 } else {
711 last = node;
712 }
713 }
714 }
715
716 /* Handle an interface delete event */
717 void if_delete_update(struct interface *ifp)
718 {
719 struct zebra_if *zif;
720
721 if (if_is_up(ifp)) {
722 zlog_err(
723 "interface %s vrf %u index %d is still up while being deleted.",
724 ifp->name, ifp->vrf_id, ifp->ifindex);
725 return;
726 }
727
728 if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
729 return;
730
731 /* Mark interface as inactive */
732 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE);
733
734 if (IS_ZEBRA_DEBUG_KERNEL)
735 zlog_debug("interface %s vrf %u index %d is now inactive.",
736 ifp->name, ifp->vrf_id, ifp->ifindex);
737
738 /* Delete connected routes from the kernel. */
739 if_delete_connected(ifp);
740
741 /* Send out notification on interface delete. */
742 zebra_interface_delete_update(ifp);
743
744 if_unlink_per_ns(ifp);
745
746 /* Update ifindex after distributing the delete message. This is in
747 case any client needs to have the old value of ifindex available
748 while processing the deletion. Each client daemon is responsible
749 for setting ifindex to IFINDEX_INTERNAL after processing the
750 interface deletion message. */
751 if_set_index(ifp, IFINDEX_INTERNAL);
752 ifp->node = NULL;
753
754 /* if the ifp is in a vrf, move it to default so vrf can be deleted if
755 * desired */
756 if (ifp->vrf_id)
757 if_handle_vrf_change(ifp, VRF_DEFAULT);
758
759 /* Reset some zebra interface params to default values. */
760 zif = ifp->info;
761 if (zif) {
762 zif->zif_type = ZEBRA_IF_OTHER;
763 zif->zif_slave_type = ZEBRA_IF_SLAVE_NONE;
764 memset(&zif->l2info, 0, sizeof(union zebra_l2if_info));
765 memset(&zif->brslave_info, 0,
766 sizeof(struct zebra_l2info_brslave));
767 }
768 }
769
770 /* VRF change for an interface */
771 void if_handle_vrf_change(struct interface *ifp, vrf_id_t vrf_id)
772 {
773 vrf_id_t old_vrf_id;
774
775 old_vrf_id = ifp->vrf_id;
776
777 /* Uninstall connected routes. */
778 if_uninstall_connected(ifp);
779
780 /* Delete any IPv4 neighbors created to implement RFC 5549 */
781 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
782
783 /* Delete all neighbor addresses learnt through IPv6 RA */
784 if_down_del_nbr_connected(ifp);
785
786 /* Send out notification on interface VRF change. */
787 /* This is to issue an UPDATE or a DELETE, as appropriate. */
788 zebra_interface_vrf_update_del(ifp, vrf_id);
789
790 /* update VRF */
791 if_update_to_new_vrf(ifp, vrf_id);
792
793 /* Send out notification on interface VRF change. */
794 /* This is to issue an ADD, if needed. */
795 zebra_interface_vrf_update_add(ifp, old_vrf_id);
796
797 /* Install connected routes (in new VRF). */
798 if (if_is_operative(ifp))
799 if_install_connected(ifp);
800
801 /* Due to connected route change, schedule RIB processing for both old
802 * and new VRF.
803 */
804 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
805 zlog_debug("%u: IF %s VRF change, scheduling RIB processing",
806 ifp->vrf_id, ifp->name);
807 rib_update(old_vrf_id, RIB_UPDATE_IF_CHANGE);
808 rib_update(ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
809 }
810
811 static void ipv6_ll_address_to_mac(struct in6_addr *address, uint8_t *mac)
812 {
813 mac[0] = address->s6_addr[8] ^ 0x02;
814 mac[1] = address->s6_addr[9];
815 mac[2] = address->s6_addr[10];
816 mac[3] = address->s6_addr[13];
817 mac[4] = address->s6_addr[14];
818 mac[5] = address->s6_addr[15];
819 }
820
821 void if_nbr_ipv6ll_to_ipv4ll_neigh_update(struct interface *ifp,
822 struct in6_addr *address, int add)
823 {
824 struct zebra_vrf *zvrf = vrf_info_lookup(ifp->vrf_id);
825 struct zebra_if *zif = ifp->info;
826 char buf[16] = "169.254.0.1";
827 struct in_addr ipv4_ll;
828 char mac[6];
829 ns_id_t ns_id;
830
831 inet_pton(AF_INET, buf, &ipv4_ll);
832
833 ipv6_ll_address_to_mac(address, (uint8_t *)mac);
834 ns_id = zvrf->zns->ns_id;
835
836 /*
837 * Remove existed arp record for the interface as netlink
838 * protocol does not have update message types
839 *
840 * supported message types are RTM_NEWNEIGH and RTM_DELNEIGH
841 */
842 kernel_neigh_update(0, ifp->ifindex, ipv4_ll.s_addr, mac, 6, ns_id);
843
844 /* Add arp record */
845 kernel_neigh_update(add, ifp->ifindex, ipv4_ll.s_addr, mac, 6, ns_id);
846
847 /*
848 * We need to note whether or not we originated a v6
849 * neighbor entry for this interface. So that when
850 * someone unwisely accidently deletes this entry
851 * we can shove it back in.
852 */
853 zif->v6_2_v4_ll_neigh_entry = !!add;
854 memcpy(&zif->v6_2_v4_ll_addr6, address, sizeof(*address));
855
856 zvrf->neigh_updates++;
857 }
858
859 static void if_nbr_ipv6ll_to_ipv4ll_neigh_add_all(struct interface *ifp)
860 {
861 if (listhead(ifp->nbr_connected)) {
862 struct nbr_connected *nbr_connected;
863 struct listnode *node;
864
865 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node,
866 nbr_connected))
867 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
868 ifp, &nbr_connected->address->u.prefix6, 1);
869 }
870 }
871
872 void if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(struct interface *ifp)
873 {
874 if (listhead(ifp->nbr_connected)) {
875 struct nbr_connected *nbr_connected;
876 struct listnode *node;
877
878 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node,
879 nbr_connected))
880 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
881 ifp, &nbr_connected->address->u.prefix6, 0);
882 }
883 }
884
885 static void if_down_del_nbr_connected(struct interface *ifp)
886 {
887 struct nbr_connected *nbr_connected;
888 struct listnode *node, *nnode;
889
890 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode,
891 nbr_connected)) {
892 listnode_delete(ifp->nbr_connected, nbr_connected);
893 nbr_connected_free(nbr_connected);
894 }
895 }
896
897 /* Interface is up. */
898 void if_up(struct interface *ifp)
899 {
900 struct zebra_if *zif;
901 struct interface *link_if;
902 struct zebra_vrf *zvrf = vrf_info_lookup(ifp->vrf_id);
903
904 zif = ifp->info;
905 zif->up_count++;
906 quagga_timestamp(2, zif->up_last, sizeof(zif->up_last));
907
908 /* Notify the protocol daemons. */
909 if (ifp->ptm_enable && (ifp->ptm_status == ZEBRA_PTM_STATUS_DOWN)) {
910 zlog_warn("%s: interface %s hasn't passed ptm check\n",
911 __func__, ifp->name);
912 return;
913 }
914 zebra_interface_up_update(ifp);
915
916 if_nbr_ipv6ll_to_ipv4ll_neigh_add_all(ifp);
917
918 #if defined(HAVE_RTADV)
919 /* Enable fast tx of RA if enabled && RA interval is not in msecs */
920 if (zif->rtadv.AdvSendAdvertisements
921 && (zif->rtadv.MaxRtrAdvInterval >= 1000)) {
922 zif->rtadv.inFastRexmit = 1;
923 zif->rtadv.NumFastReXmitsRemain = RTADV_NUM_FAST_REXMITS;
924 }
925 #endif
926
927 /* Install connected routes to the kernel. */
928 if_install_connected(ifp);
929
930 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
931 zlog_debug("%u: IF %s up, scheduling RIB processing",
932 ifp->vrf_id, ifp->name);
933 rib_update(ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
934
935 /* Handle interface up for specific types for EVPN. Non-VxLAN interfaces
936 * are checked to see if (remote) neighbor entries need to be installed
937 * on them for ARP suppression.
938 */
939 if (IS_ZEBRA_IF_VXLAN(ifp))
940 zebra_vxlan_if_up(ifp);
941 else if (IS_ZEBRA_IF_BRIDGE(ifp)) {
942 link_if = ifp;
943 zebra_vxlan_svi_up(ifp, link_if);
944 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
945 link_if = if_lookup_by_index_per_ns(zvrf->zns,
946 zif->link_ifindex);
947 if (link_if)
948 zebra_vxlan_svi_up(ifp, link_if);
949 }
950 }
951
952 /* Interface goes down. We have to manage different behavior of based
953 OS. */
954 void if_down(struct interface *ifp)
955 {
956 struct zebra_if *zif;
957 struct interface *link_if;
958 struct zebra_vrf *zvrf = vrf_info_lookup(ifp->vrf_id);
959
960 zif = ifp->info;
961 zif->down_count++;
962 quagga_timestamp(2, zif->down_last, sizeof(zif->down_last));
963
964 /* Handle interface down for specific types for EVPN. Non-VxLAN
965 * interfaces
966 * are checked to see if (remote) neighbor entries need to be purged
967 * for ARP suppression.
968 */
969 if (IS_ZEBRA_IF_VXLAN(ifp))
970 zebra_vxlan_if_down(ifp);
971 else if (IS_ZEBRA_IF_BRIDGE(ifp)) {
972 link_if = ifp;
973 zebra_vxlan_svi_down(ifp, link_if);
974 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
975 link_if = if_lookup_by_index_per_ns(zvrf->zns,
976 zif->link_ifindex);
977 if (link_if)
978 zebra_vxlan_svi_down(ifp, link_if);
979 }
980
981
982 /* Notify to the protocol daemons. */
983 zebra_interface_down_update(ifp);
984
985 /* Uninstall connected routes from the kernel. */
986 if_uninstall_connected(ifp);
987
988 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
989 zlog_debug("%u: IF %s down, scheduling RIB processing",
990 ifp->vrf_id, ifp->name);
991 rib_update(ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
992
993 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
994
995 /* Delete all neighbor addresses learnt through IPv6 RA */
996 if_down_del_nbr_connected(ifp);
997 }
998
999 void if_refresh(struct interface *ifp)
1000 {
1001 if_get_flags(ifp);
1002 }
1003
1004 void zebra_if_update_link(struct interface *ifp, ifindex_t link_ifindex)
1005 {
1006 struct zebra_if *zif;
1007
1008 zif = (struct zebra_if *)ifp->info;
1009 zif->link_ifindex = link_ifindex;
1010 zif->link = if_lookup_by_index_per_ns(zebra_ns_lookup(NS_DEFAULT),
1011 link_ifindex);
1012 }
1013
1014
1015 /* Output prefix string to vty. */
1016 static int prefix_vty_out(struct vty *vty, struct prefix *p)
1017 {
1018 char str[INET6_ADDRSTRLEN];
1019
1020 inet_ntop(p->family, &p->u.prefix, str, sizeof(str));
1021 vty_out(vty, "%s", str);
1022 return strlen(str);
1023 }
1024
1025 /* Dump if address information to vty. */
1026 static void connected_dump_vty(struct vty *vty, struct connected *connected)
1027 {
1028 struct prefix *p;
1029
1030 /* Print interface address. */
1031 p = connected->address;
1032 vty_out(vty, " %s ", prefix_family_str(p));
1033 prefix_vty_out(vty, p);
1034 vty_out(vty, "/%d", p->prefixlen);
1035
1036 /* If there is destination address, print it. */
1037 if (connected->destination) {
1038 vty_out(vty,
1039 (CONNECTED_PEER(connected) ? " peer " : " broadcast "));
1040 prefix_vty_out(vty, connected->destination);
1041 if (CONNECTED_PEER(connected))
1042 vty_out(vty, "/%d", connected->destination->prefixlen);
1043 }
1044
1045 if (CHECK_FLAG(connected->flags, ZEBRA_IFA_SECONDARY))
1046 vty_out(vty, " secondary");
1047
1048 if (CHECK_FLAG(connected->flags, ZEBRA_IFA_UNNUMBERED))
1049 vty_out(vty, " unnumbered");
1050
1051 if (connected->label)
1052 vty_out(vty, " %s", connected->label);
1053
1054 vty_out(vty, "\n");
1055 }
1056
1057 /* Dump interface neighbor address information to vty. */
1058 static void nbr_connected_dump_vty(struct vty *vty,
1059 struct nbr_connected *connected)
1060 {
1061 struct prefix *p;
1062
1063 /* Print interface address. */
1064 p = connected->address;
1065 vty_out(vty, " %s ", prefix_family_str(p));
1066 prefix_vty_out(vty, p);
1067 vty_out(vty, "/%d", p->prefixlen);
1068
1069 vty_out(vty, "\n");
1070 }
1071
1072 static const char *zebra_ziftype_2str(zebra_iftype_t zif_type)
1073 {
1074 switch (zif_type) {
1075 case ZEBRA_IF_OTHER:
1076 return "Other";
1077 break;
1078
1079 case ZEBRA_IF_BRIDGE:
1080 return "Bridge";
1081 break;
1082
1083 case ZEBRA_IF_VLAN:
1084 return "Vlan";
1085 break;
1086
1087 case ZEBRA_IF_VXLAN:
1088 return "Vxlan";
1089 break;
1090
1091 case ZEBRA_IF_VRF:
1092 return "VRF";
1093 break;
1094
1095 default:
1096 return "Unknown";
1097 break;
1098 }
1099 }
1100
1101 /* Interface's information print out to vty interface. */
1102 static void if_dump_vty(struct vty *vty, struct interface *ifp)
1103 {
1104 struct connected *connected;
1105 struct nbr_connected *nbr_connected;
1106 struct listnode *node;
1107 struct route_node *rn;
1108 struct zebra_if *zebra_if;
1109 struct vrf *vrf;
1110
1111 zebra_if = ifp->info;
1112
1113 vty_out(vty, "Interface %s is ", ifp->name);
1114 if (if_is_up(ifp)) {
1115 vty_out(vty, "up, line protocol ");
1116
1117 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)) {
1118 if (if_is_running(ifp))
1119 vty_out(vty, "is up\n");
1120 else
1121 vty_out(vty, "is down\n");
1122 } else {
1123 vty_out(vty, "detection is disabled\n");
1124 }
1125 } else {
1126 vty_out(vty, "down\n");
1127 }
1128
1129 vty_out(vty, " Link ups: %5u last: %s\n", zebra_if->up_count,
1130 zebra_if->up_last[0] ? zebra_if->up_last : "(never)");
1131 vty_out(vty, " Link downs: %5u last: %s\n", zebra_if->down_count,
1132 zebra_if->down_last[0] ? zebra_if->down_last : "(never)");
1133
1134 zebra_ptm_show_status(vty, ifp);
1135
1136 vrf = vrf_lookup_by_id(ifp->vrf_id);
1137 vty_out(vty, " vrf: %s\n", vrf->name);
1138
1139 if (ifp->desc)
1140 vty_out(vty, " Description: %s\n", ifp->desc);
1141 if (ifp->ifindex == IFINDEX_INTERNAL) {
1142 vty_out(vty, " pseudo interface\n");
1143 return;
1144 } else if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1145 vty_out(vty, " index %d inactive interface\n", ifp->ifindex);
1146 return;
1147 }
1148
1149 vty_out(vty, " index %d metric %d mtu %d speed %u ", ifp->ifindex,
1150 ifp->metric, ifp->mtu, ifp->speed);
1151 if (ifp->mtu6 != ifp->mtu)
1152 vty_out(vty, "mtu6 %d ", ifp->mtu6);
1153 vty_out(vty, "\n flags: %s\n", if_flag_dump(ifp->flags));
1154
1155 /* Hardware address. */
1156 vty_out(vty, " Type: %s\n", if_link_type_str(ifp->ll_type));
1157 if (ifp->hw_addr_len != 0) {
1158 int i;
1159
1160 vty_out(vty, " HWaddr: ");
1161 for (i = 0; i < ifp->hw_addr_len; i++)
1162 vty_out(vty, "%s%02x", i == 0 ? "" : ":",
1163 ifp->hw_addr[i]);
1164 vty_out(vty, "\n");
1165 }
1166
1167 /* Bandwidth in Mbps */
1168 if (ifp->bandwidth != 0) {
1169 vty_out(vty, " bandwidth %u Mbps", ifp->bandwidth);
1170 vty_out(vty, "\n");
1171 }
1172
1173 for (rn = route_top(zebra_if->ipv4_subnets); rn; rn = route_next(rn)) {
1174 if (!rn->info)
1175 continue;
1176
1177 for (ALL_LIST_ELEMENTS_RO((struct list *)rn->info, node,
1178 connected))
1179 connected_dump_vty(vty, connected);
1180 }
1181
1182 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
1183 if (CHECK_FLAG(connected->conf, ZEBRA_IFC_REAL)
1184 && (connected->address->family == AF_INET6))
1185 connected_dump_vty(vty, connected);
1186 }
1187
1188 vty_out(vty, " Interface Type %s\n",
1189 zebra_ziftype_2str(zebra_if->zif_type));
1190 if (IS_ZEBRA_IF_BRIDGE(ifp)) {
1191 struct zebra_l2info_bridge *bridge_info;
1192
1193 bridge_info = &zebra_if->l2info.br;
1194 vty_out(vty, " Bridge VLAN-aware: %s\n",
1195 bridge_info->vlan_aware ? "yes" : "no");
1196 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
1197 struct zebra_l2info_vlan *vlan_info;
1198
1199 vlan_info = &zebra_if->l2info.vl;
1200 vty_out(vty, " VLAN Id %u\n", vlan_info->vid);
1201 } else if (IS_ZEBRA_IF_VXLAN(ifp)) {
1202 struct zebra_l2info_vxlan *vxlan_info;
1203
1204 vxlan_info = &zebra_if->l2info.vxl;
1205 vty_out(vty, " VxLAN Id %u", vxlan_info->vni);
1206 if (vxlan_info->vtep_ip.s_addr != INADDR_ANY)
1207 vty_out(vty, " VTEP IP: %s",
1208 inet_ntoa(vxlan_info->vtep_ip));
1209 if (vxlan_info->access_vlan)
1210 vty_out(vty, " Access VLAN Id %u",
1211 vxlan_info->access_vlan);
1212 vty_out(vty, "\n");
1213 }
1214
1215 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) {
1216 struct zebra_l2info_brslave *br_slave;
1217
1218 br_slave = &zebra_if->brslave_info;
1219 if (br_slave->bridge_ifindex != IFINDEX_INTERNAL)
1220 vty_out(vty, " Master (bridge) ifindex %u\n",
1221 br_slave->bridge_ifindex);
1222 }
1223
1224 if (zebra_if->link_ifindex != IFINDEX_INTERNAL) {
1225 vty_out(vty, " Link ifindex %u", zebra_if->link_ifindex);
1226 if (zebra_if->link)
1227 vty_out(vty, "(%s)\n", zebra_if->link->name);
1228 else
1229 vty_out(vty, "(Unknown)\n");
1230 }
1231
1232 if (HAS_LINK_PARAMS(ifp)) {
1233 int i;
1234 struct if_link_params *iflp = ifp->link_params;
1235 vty_out(vty, " Traffic Engineering Link Parameters:\n");
1236 if (IS_PARAM_SET(iflp, LP_TE_METRIC))
1237 vty_out(vty, " TE metric %u\n", iflp->te_metric);
1238 if (IS_PARAM_SET(iflp, LP_MAX_BW))
1239 vty_out(vty, " Maximum Bandwidth %g (Byte/s)\n",
1240 iflp->max_bw);
1241 if (IS_PARAM_SET(iflp, LP_MAX_RSV_BW))
1242 vty_out(vty,
1243 " Maximum Reservable Bandwidth %g (Byte/s)\n",
1244 iflp->max_rsv_bw);
1245 if (IS_PARAM_SET(iflp, LP_UNRSV_BW)) {
1246 vty_out(vty,
1247 " Unreserved Bandwidth per Class Type in Byte/s:\n");
1248 for (i = 0; i < MAX_CLASS_TYPE; i += 2)
1249 vty_out(vty,
1250 " [%d]: %g (Bytes/sec),\t[%d]: %g (Bytes/sec)\n",
1251 i, iflp->unrsv_bw[i], i + 1,
1252 iflp->unrsv_bw[i + 1]);
1253 }
1254
1255 if (IS_PARAM_SET(iflp, LP_ADM_GRP))
1256 vty_out(vty, " Administrative Group:%u\n",
1257 iflp->admin_grp);
1258 if (IS_PARAM_SET(iflp, LP_DELAY)) {
1259 vty_out(vty, " Link Delay Average: %u (micro-sec.)",
1260 iflp->av_delay);
1261 if (IS_PARAM_SET(iflp, LP_MM_DELAY)) {
1262 vty_out(vty, " Min: %u (micro-sec.)",
1263 iflp->min_delay);
1264 vty_out(vty, " Max: %u (micro-sec.)",
1265 iflp->max_delay);
1266 }
1267 vty_out(vty, "\n");
1268 }
1269 if (IS_PARAM_SET(iflp, LP_DELAY_VAR))
1270 vty_out(vty,
1271 " Link Delay Variation %u (micro-sec.)\n",
1272 iflp->delay_var);
1273 if (IS_PARAM_SET(iflp, LP_PKT_LOSS))
1274 vty_out(vty, " Link Packet Loss %g (in %%)\n",
1275 iflp->pkt_loss);
1276 if (IS_PARAM_SET(iflp, LP_AVA_BW))
1277 vty_out(vty, " Available Bandwidth %g (Byte/s)\n",
1278 iflp->ava_bw);
1279 if (IS_PARAM_SET(iflp, LP_RES_BW))
1280 vty_out(vty, " Residual Bandwidth %g (Byte/s)\n",
1281 iflp->res_bw);
1282 if (IS_PARAM_SET(iflp, LP_USE_BW))
1283 vty_out(vty, " Utilized Bandwidth %g (Byte/s)\n",
1284 iflp->use_bw);
1285 if (IS_PARAM_SET(iflp, LP_RMT_AS))
1286 vty_out(vty, " Neighbor ASBR IP: %s AS: %u \n",
1287 inet_ntoa(iflp->rmt_ip), iflp->rmt_as);
1288 }
1289
1290 hook_call(zebra_if_extra_info, vty, ifp);
1291
1292 if (listhead(ifp->nbr_connected))
1293 vty_out(vty, " Neighbor address(s):\n");
1294 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node, nbr_connected))
1295 nbr_connected_dump_vty(vty, nbr_connected);
1296
1297 #ifdef HAVE_PROC_NET_DEV
1298 /* Statistics print out using proc file system. */
1299 vty_out(vty,
1300 " %lu input packets (%lu multicast), %lu bytes, "
1301 "%lu dropped\n",
1302 ifp->stats.rx_packets, ifp->stats.rx_multicast,
1303 ifp->stats.rx_bytes, ifp->stats.rx_dropped);
1304
1305 vty_out(vty,
1306 " %lu input errors, %lu length, %lu overrun,"
1307 " %lu CRC, %lu frame\n",
1308 ifp->stats.rx_errors, ifp->stats.rx_length_errors,
1309 ifp->stats.rx_over_errors, ifp->stats.rx_crc_errors,
1310 ifp->stats.rx_frame_errors);
1311
1312 vty_out(vty, " %lu fifo, %lu missed\n", ifp->stats.rx_fifo_errors,
1313 ifp->stats.rx_missed_errors);
1314
1315 vty_out(vty, " %lu output packets, %lu bytes, %lu dropped\n",
1316 ifp->stats.tx_packets, ifp->stats.tx_bytes,
1317 ifp->stats.tx_dropped);
1318
1319 vty_out(vty,
1320 " %lu output errors, %lu aborted, %lu carrier,"
1321 " %lu fifo, %lu heartbeat\n",
1322 ifp->stats.tx_errors, ifp->stats.tx_aborted_errors,
1323 ifp->stats.tx_carrier_errors, ifp->stats.tx_fifo_errors,
1324 ifp->stats.tx_heartbeat_errors);
1325
1326 vty_out(vty, " %lu window, %lu collisions\n",
1327 ifp->stats.tx_window_errors, ifp->stats.collisions);
1328 #endif /* HAVE_PROC_NET_DEV */
1329
1330 #ifdef HAVE_NET_RT_IFLIST
1331 #if defined(__bsdi__) || defined(__NetBSD__)
1332 /* Statistics print out using sysctl (). */
1333 vty_out(vty,
1334 " input packets %llu, bytes %llu, dropped %llu,"
1335 " multicast packets %llu\n",
1336 (unsigned long long)ifp->stats.ifi_ipackets,
1337 (unsigned long long)ifp->stats.ifi_ibytes,
1338 (unsigned long long)ifp->stats.ifi_iqdrops,
1339 (unsigned long long)ifp->stats.ifi_imcasts);
1340
1341 vty_out(vty, " input errors %llu\n",
1342 (unsigned long long)ifp->stats.ifi_ierrors);
1343
1344 vty_out(vty,
1345 " output packets %llu, bytes %llu,"
1346 " multicast packets %llu\n",
1347 (unsigned long long)ifp->stats.ifi_opackets,
1348 (unsigned long long)ifp->stats.ifi_obytes,
1349 (unsigned long long)ifp->stats.ifi_omcasts);
1350
1351 vty_out(vty, " output errors %llu\n",
1352 (unsigned long long)ifp->stats.ifi_oerrors);
1353
1354 vty_out(vty, " collisions %llu\n",
1355 (unsigned long long)ifp->stats.ifi_collisions);
1356 #else
1357 /* Statistics print out using sysctl (). */
1358 vty_out(vty,
1359 " input packets %lu, bytes %lu, dropped %lu,"
1360 " multicast packets %lu\n",
1361 ifp->stats.ifi_ipackets, ifp->stats.ifi_ibytes,
1362 ifp->stats.ifi_iqdrops, ifp->stats.ifi_imcasts);
1363
1364 vty_out(vty, " input errors %lu\n", ifp->stats.ifi_ierrors);
1365
1366 vty_out(vty,
1367 " output packets %lu, bytes %lu, multicast packets %lu\n",
1368 ifp->stats.ifi_opackets, ifp->stats.ifi_obytes,
1369 ifp->stats.ifi_omcasts);
1370
1371 vty_out(vty, " output errors %lu\n", ifp->stats.ifi_oerrors);
1372
1373 vty_out(vty, " collisions %lu\n", ifp->stats.ifi_collisions);
1374 #endif /* __bsdi__ || __NetBSD__ */
1375 #endif /* HAVE_NET_RT_IFLIST */
1376 }
1377
1378 static void interface_update_stats(void)
1379 {
1380 #ifdef HAVE_PROC_NET_DEV
1381 /* If system has interface statistics via proc file system, update
1382 statistics. */
1383 ifstat_update_proc();
1384 #endif /* HAVE_PROC_NET_DEV */
1385 #ifdef HAVE_NET_RT_IFLIST
1386 ifstat_update_sysctl();
1387 #endif /* HAVE_NET_RT_IFLIST */
1388 }
1389
1390 struct cmd_node interface_node = {INTERFACE_NODE, "%s(config-if)# ", 1};
1391
1392 /* Show all interfaces to vty. */
1393 DEFUN (show_interface,
1394 show_interface_cmd,
1395 "show interface [vrf NAME]",
1396 SHOW_STR
1397 "Interface status and configuration\n"
1398 VRF_CMD_HELP_STR)
1399 {
1400 struct vrf *vrf;
1401 struct interface *ifp;
1402 vrf_id_t vrf_id = VRF_DEFAULT;
1403
1404 interface_update_stats();
1405
1406 if (argc > 2)
1407 VRF_GET_ID(vrf_id, argv[3]->arg, false);
1408
1409 /* All interface print. */
1410 vrf = vrf_lookup_by_id(vrf_id);
1411 FOR_ALL_INTERFACES (vrf, ifp)
1412 if_dump_vty(vty, ifp);
1413
1414 return CMD_SUCCESS;
1415 }
1416
1417
1418 /* Show all interfaces to vty. */
1419 DEFUN (show_interface_vrf_all,
1420 show_interface_vrf_all_cmd,
1421 "show interface vrf all",
1422 SHOW_STR
1423 "Interface status and configuration\n"
1424 VRF_ALL_CMD_HELP_STR)
1425 {
1426 struct vrf *vrf;
1427 struct interface *ifp;
1428
1429 interface_update_stats();
1430
1431 /* All interface print. */
1432 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
1433 FOR_ALL_INTERFACES (vrf, ifp)
1434 if_dump_vty(vty, ifp);
1435
1436 return CMD_SUCCESS;
1437 }
1438
1439 /* Show specified interface to vty. */
1440
1441 DEFUN (show_interface_name_vrf,
1442 show_interface_name_vrf_cmd,
1443 "show interface IFNAME vrf NAME",
1444 SHOW_STR
1445 "Interface status and configuration\n"
1446 "Interface name\n"
1447 VRF_CMD_HELP_STR)
1448 {
1449 int idx_ifname = 2;
1450 int idx_name = 4;
1451 struct interface *ifp;
1452 vrf_id_t vrf_id;
1453
1454 interface_update_stats();
1455
1456 VRF_GET_ID(vrf_id, argv[idx_name]->arg, false);
1457
1458 /* Specified interface print. */
1459 ifp = if_lookup_by_name(argv[idx_ifname]->arg, vrf_id);
1460 if (ifp == NULL) {
1461 vty_out(vty, "%% Can't find interface %s\n",
1462 argv[idx_ifname]->arg);
1463 return CMD_WARNING;
1464 }
1465 if_dump_vty(vty, ifp);
1466
1467 return CMD_SUCCESS;
1468 }
1469
1470 /* Show specified interface to vty. */
1471 DEFUN (show_interface_name_vrf_all,
1472 show_interface_name_vrf_all_cmd,
1473 "show interface IFNAME [vrf all]",
1474 SHOW_STR
1475 "Interface status and configuration\n"
1476 "Interface name\n"
1477 VRF_ALL_CMD_HELP_STR)
1478 {
1479 int idx_ifname = 2;
1480 struct vrf *vrf;
1481 struct interface *ifp;
1482 int found = 0;
1483
1484 interface_update_stats();
1485
1486 /* All interface print. */
1487 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
1488 /* Specified interface print. */
1489 ifp = if_lookup_by_name(argv[idx_ifname]->arg, vrf->vrf_id);
1490 if (ifp) {
1491 if_dump_vty(vty, ifp);
1492 found++;
1493 }
1494 }
1495
1496 if (!found) {
1497 vty_out(vty, "%% Can't find interface %s\n",
1498 argv[idx_ifname]->arg);
1499 return CMD_WARNING;
1500 }
1501
1502 return CMD_SUCCESS;
1503 }
1504
1505
1506 static void if_show_description(struct vty *vty, vrf_id_t vrf_id)
1507 {
1508 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
1509 struct interface *ifp;
1510
1511 vty_out(vty, "Interface Status Protocol Description\n");
1512 FOR_ALL_INTERFACES (vrf, ifp) {
1513 int len;
1514
1515 len = vty_out(vty, "%s", ifp->name);
1516 vty_out(vty, "%*s", (16 - len), " ");
1517
1518 if (if_is_up(ifp)) {
1519 vty_out(vty, "up ");
1520 if (CHECK_FLAG(ifp->status,
1521 ZEBRA_INTERFACE_LINKDETECTION)) {
1522 if (if_is_running(ifp))
1523 vty_out(vty, "up ");
1524 else
1525 vty_out(vty, "down ");
1526 } else {
1527 vty_out(vty, "unknown ");
1528 }
1529 } else {
1530 vty_out(vty, "down down ");
1531 }
1532
1533 if (ifp->desc)
1534 vty_out(vty, "%s", ifp->desc);
1535 vty_out(vty, "\n");
1536 }
1537 }
1538
1539 DEFUN (show_interface_desc,
1540 show_interface_desc_cmd,
1541 "show interface description [vrf NAME]",
1542 SHOW_STR
1543 "Interface status and configuration\n"
1544 "Interface description\n"
1545 VRF_CMD_HELP_STR)
1546 {
1547 vrf_id_t vrf_id = VRF_DEFAULT;
1548
1549 if (argc > 3)
1550 VRF_GET_ID(vrf_id, argv[4]->arg, false);
1551
1552 if_show_description(vty, vrf_id);
1553
1554 return CMD_SUCCESS;
1555 }
1556
1557
1558 DEFUN (show_interface_desc_vrf_all,
1559 show_interface_desc_vrf_all_cmd,
1560 "show interface description vrf all",
1561 SHOW_STR
1562 "Interface status and configuration\n"
1563 "Interface description\n"
1564 VRF_ALL_CMD_HELP_STR)
1565 {
1566 struct vrf *vrf;
1567
1568 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
1569 if (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) {
1570 vty_out(vty, "\n\tVRF %u\n\n", vrf->vrf_id);
1571 if_show_description(vty, vrf->vrf_id);
1572 }
1573
1574 return CMD_SUCCESS;
1575 }
1576
1577 DEFUN (multicast,
1578 multicast_cmd,
1579 "multicast",
1580 "Set multicast flag to interface\n")
1581 {
1582 VTY_DECLVAR_CONTEXT(interface, ifp);
1583 int ret;
1584 struct zebra_if *if_data;
1585
1586 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1587 ret = if_set_flags(ifp, IFF_MULTICAST);
1588 if (ret < 0) {
1589 vty_out(vty, "Can't set multicast flag\n");
1590 return CMD_WARNING_CONFIG_FAILED;
1591 }
1592 if_refresh(ifp);
1593 }
1594 if_data = ifp->info;
1595 if_data->multicast = IF_ZEBRA_MULTICAST_ON;
1596
1597 return CMD_SUCCESS;
1598 }
1599
1600 DEFUN (no_multicast,
1601 no_multicast_cmd,
1602 "no multicast",
1603 NO_STR
1604 "Unset multicast flag to interface\n")
1605 {
1606 VTY_DECLVAR_CONTEXT(interface, ifp);
1607 int ret;
1608 struct zebra_if *if_data;
1609
1610 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1611 ret = if_unset_flags(ifp, IFF_MULTICAST);
1612 if (ret < 0) {
1613 vty_out(vty, "Can't unset multicast flag\n");
1614 return CMD_WARNING_CONFIG_FAILED;
1615 }
1616 if_refresh(ifp);
1617 }
1618 if_data = ifp->info;
1619 if_data->multicast = IF_ZEBRA_MULTICAST_OFF;
1620
1621 return CMD_SUCCESS;
1622 }
1623
1624 DEFUN (linkdetect,
1625 linkdetect_cmd,
1626 "link-detect",
1627 "Enable link detection on interface\n")
1628 {
1629 VTY_DECLVAR_CONTEXT(interface, ifp);
1630 int if_was_operative;
1631
1632 if_was_operative = if_is_no_ptm_operative(ifp);
1633 SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
1634
1635 /* When linkdetection is enabled, if might come down */
1636 if (!if_is_no_ptm_operative(ifp) && if_was_operative)
1637 if_down(ifp);
1638
1639 /* FIXME: Will defer status change forwarding if interface
1640 does not come down! */
1641
1642 return CMD_SUCCESS;
1643 }
1644
1645
1646 DEFUN (no_linkdetect,
1647 no_linkdetect_cmd,
1648 "no link-detect",
1649 NO_STR
1650 "Disable link detection on interface\n")
1651 {
1652 VTY_DECLVAR_CONTEXT(interface, ifp);
1653 int if_was_operative;
1654
1655 if_was_operative = if_is_no_ptm_operative(ifp);
1656 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
1657
1658 /* Interface may come up after disabling link detection */
1659 if (if_is_operative(ifp) && !if_was_operative)
1660 if_up(ifp);
1661
1662 /* FIXME: see linkdetect_cmd */
1663
1664 return CMD_SUCCESS;
1665 }
1666
1667 DEFUN (shutdown_if,
1668 shutdown_if_cmd,
1669 "shutdown",
1670 "Shutdown the selected interface\n")
1671 {
1672 VTY_DECLVAR_CONTEXT(interface, ifp);
1673 int ret;
1674 struct zebra_if *if_data;
1675
1676 if (ifp->ifindex != IFINDEX_INTERNAL) {
1677 ret = if_unset_flags(ifp, IFF_UP);
1678 if (ret < 0) {
1679 vty_out(vty, "Can't shutdown interface\n");
1680 return CMD_WARNING_CONFIG_FAILED;
1681 }
1682 if_refresh(ifp);
1683 }
1684 if_data = ifp->info;
1685 if_data->shutdown = IF_ZEBRA_SHUTDOWN_ON;
1686
1687 return CMD_SUCCESS;
1688 }
1689
1690 DEFUN (no_shutdown_if,
1691 no_shutdown_if_cmd,
1692 "no shutdown",
1693 NO_STR
1694 "Shutdown the selected interface\n")
1695 {
1696 VTY_DECLVAR_CONTEXT(interface, ifp);
1697 int ret;
1698 struct zebra_if *if_data;
1699
1700 if (ifp->ifindex != IFINDEX_INTERNAL) {
1701 ret = if_set_flags(ifp, IFF_UP | IFF_RUNNING);
1702 if (ret < 0) {
1703 vty_out(vty, "Can't up interface\n");
1704 return CMD_WARNING_CONFIG_FAILED;
1705 }
1706 if_refresh(ifp);
1707
1708 /* Some addresses (in particular, IPv6 addresses on Linux) get
1709 * removed when the interface goes down. They need to be
1710 * readded.
1711 */
1712 if_addr_wakeup(ifp);
1713 }
1714
1715 if_data = ifp->info;
1716 if_data->shutdown = IF_ZEBRA_SHUTDOWN_OFF;
1717
1718 return CMD_SUCCESS;
1719 }
1720
1721 DEFUN (bandwidth_if,
1722 bandwidth_if_cmd,
1723 "bandwidth (1-100000)",
1724 "Set bandwidth informational parameter\n"
1725 "Bandwidth in megabits\n")
1726 {
1727 int idx_number = 1;
1728 VTY_DECLVAR_CONTEXT(interface, ifp);
1729 unsigned int bandwidth;
1730
1731 bandwidth = strtol(argv[idx_number]->arg, NULL, 10);
1732
1733 /* bandwidth range is <1-100000> */
1734 if (bandwidth < 1 || bandwidth > 100000) {
1735 vty_out(vty, "Bandwidth is invalid\n");
1736 return CMD_WARNING_CONFIG_FAILED;
1737 }
1738
1739 ifp->bandwidth = bandwidth;
1740
1741 /* force protocols to recalculate routes due to cost change */
1742 if (if_is_operative(ifp))
1743 zebra_interface_up_update(ifp);
1744
1745 return CMD_SUCCESS;
1746 }
1747
1748 DEFUN (no_bandwidth_if,
1749 no_bandwidth_if_cmd,
1750 "no bandwidth [(1-100000)]",
1751 NO_STR
1752 "Set bandwidth informational parameter\n"
1753 "Bandwidth in megabits\n")
1754 {
1755 VTY_DECLVAR_CONTEXT(interface, ifp);
1756
1757 ifp->bandwidth = 0;
1758
1759 /* force protocols to recalculate routes due to cost change */
1760 if (if_is_operative(ifp))
1761 zebra_interface_up_update(ifp);
1762
1763 return CMD_SUCCESS;
1764 }
1765
1766
1767 struct cmd_node link_params_node = {
1768 LINK_PARAMS_NODE, "%s(config-link-params)# ", 1,
1769 };
1770
1771 static void link_param_cmd_set_uint32(struct interface *ifp, uint32_t *field,
1772 uint32_t type, uint32_t value)
1773 {
1774 /* Update field as needed */
1775 if (IS_PARAM_UNSET(ifp->link_params, type) || *field != value) {
1776 *field = value;
1777 SET_PARAM(ifp->link_params, type);
1778
1779 /* force protocols to update LINK STATE due to parameters change
1780 */
1781 if (if_is_operative(ifp))
1782 zebra_interface_parameters_update(ifp);
1783 }
1784 }
1785 static void link_param_cmd_set_float(struct interface *ifp, float *field,
1786 uint32_t type, float value)
1787 {
1788
1789 /* Update field as needed */
1790 if (IS_PARAM_UNSET(ifp->link_params, type) || *field != value) {
1791 *field = value;
1792 SET_PARAM(ifp->link_params, type);
1793
1794 /* force protocols to update LINK STATE due to parameters change
1795 */
1796 if (if_is_operative(ifp))
1797 zebra_interface_parameters_update(ifp);
1798 }
1799 }
1800
1801 static void link_param_cmd_unset(struct interface *ifp, uint32_t type)
1802 {
1803 if (ifp->link_params == NULL)
1804 return;
1805
1806 /* Unset field */
1807 UNSET_PARAM(ifp->link_params, type);
1808
1809 /* force protocols to update LINK STATE due to parameters change */
1810 if (if_is_operative(ifp))
1811 zebra_interface_parameters_update(ifp);
1812 }
1813
1814 DEFUN_NOSH (link_params,
1815 link_params_cmd,
1816 "link-params",
1817 LINK_PARAMS_STR)
1818 {
1819 /* vty->qobj_index stays the same @ interface pointer */
1820 vty->node = LINK_PARAMS_NODE;
1821
1822 return CMD_SUCCESS;
1823 }
1824
1825 DEFUN_NOSH (exit_link_params,
1826 exit_link_params_cmd,
1827 "exit-link-params",
1828 "Exit from Link Params configuration mode\n")
1829 {
1830 if (vty->node == LINK_PARAMS_NODE)
1831 vty->node = INTERFACE_NODE;
1832 return CMD_SUCCESS;
1833 }
1834
1835 /* Specific Traffic Engineering parameters commands */
1836 DEFUN (link_params_enable,
1837 link_params_enable_cmd,
1838 "enable",
1839 "Activate link parameters on this interface\n")
1840 {
1841 VTY_DECLVAR_CONTEXT(interface, ifp);
1842
1843 /* This command could be issue at startup, when activate MPLS TE */
1844 /* on a new interface or after a ON / OFF / ON toggle */
1845 /* In all case, TE parameters are reset to their default factory */
1846 if (IS_ZEBRA_DEBUG_EVENT)
1847 zlog_debug(
1848 "Link-params: enable TE link parameters on interface %s",
1849 ifp->name);
1850
1851 if (!if_link_params_get(ifp)) {
1852 if (IS_ZEBRA_DEBUG_EVENT)
1853 zlog_debug(
1854 "Link-params: failed to init TE link parameters %s",
1855 ifp->name);
1856
1857 return CMD_WARNING_CONFIG_FAILED;
1858 }
1859
1860 /* force protocols to update LINK STATE due to parameters change */
1861 if (if_is_operative(ifp))
1862 zebra_interface_parameters_update(ifp);
1863
1864 return CMD_SUCCESS;
1865 }
1866
1867 DEFUN (no_link_params_enable,
1868 no_link_params_enable_cmd,
1869 "no enable",
1870 NO_STR
1871 "Disable link parameters on this interface\n")
1872 {
1873 VTY_DECLVAR_CONTEXT(interface, ifp);
1874
1875 zlog_debug("MPLS-TE: disable TE link parameters on interface %s",
1876 ifp->name);
1877
1878 if_link_params_free(ifp);
1879
1880 /* force protocols to update LINK STATE due to parameters change */
1881 if (if_is_operative(ifp))
1882 zebra_interface_parameters_update(ifp);
1883
1884 return CMD_SUCCESS;
1885 }
1886
1887 /* STANDARD TE metrics */
1888 DEFUN (link_params_metric,
1889 link_params_metric_cmd,
1890 "metric (0-4294967295)",
1891 "Link metric for MPLS-TE purpose\n"
1892 "Metric value in decimal\n")
1893 {
1894 int idx_number = 1;
1895 VTY_DECLVAR_CONTEXT(interface, ifp);
1896 struct if_link_params *iflp = if_link_params_get(ifp);
1897 uint32_t metric;
1898
1899 metric = strtoul(argv[idx_number]->arg, NULL, 10);
1900
1901 /* Update TE metric if needed */
1902 link_param_cmd_set_uint32(ifp, &iflp->te_metric, LP_TE_METRIC, metric);
1903
1904 return CMD_SUCCESS;
1905 }
1906
1907 DEFUN (no_link_params_metric,
1908 no_link_params_metric_cmd,
1909 "no metric",
1910 NO_STR
1911 "Disable Link Metric on this interface\n")
1912 {
1913 VTY_DECLVAR_CONTEXT(interface, ifp);
1914
1915 /* Unset TE Metric */
1916 link_param_cmd_unset(ifp, LP_TE_METRIC);
1917
1918 return CMD_SUCCESS;
1919 }
1920
1921 DEFUN (link_params_maxbw,
1922 link_params_maxbw_cmd,
1923 "max-bw BANDWIDTH",
1924 "Maximum bandwidth that can be used\n"
1925 "Bytes/second (IEEE floating point format)\n")
1926 {
1927 int idx_bandwidth = 1;
1928 VTY_DECLVAR_CONTEXT(interface, ifp);
1929 struct if_link_params *iflp = if_link_params_get(ifp);
1930
1931 float bw;
1932
1933 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
1934 vty_out(vty, "link_params_maxbw: fscanf: %s\n",
1935 safe_strerror(errno));
1936 return CMD_WARNING_CONFIG_FAILED;
1937 }
1938
1939 /* Check that Maximum bandwidth is not lower than other bandwidth
1940 * parameters */
1941 if ((bw <= iflp->max_rsv_bw) || (bw <= iflp->unrsv_bw[0])
1942 || (bw <= iflp->unrsv_bw[1]) || (bw <= iflp->unrsv_bw[2])
1943 || (bw <= iflp->unrsv_bw[3]) || (bw <= iflp->unrsv_bw[4])
1944 || (bw <= iflp->unrsv_bw[5]) || (bw <= iflp->unrsv_bw[6])
1945 || (bw <= iflp->unrsv_bw[7]) || (bw <= iflp->ava_bw)
1946 || (bw <= iflp->res_bw) || (bw <= iflp->use_bw)) {
1947 vty_out(vty,
1948 "Maximum Bandwidth could not be lower than others bandwidth\n");
1949 return CMD_WARNING_CONFIG_FAILED;
1950 }
1951
1952 /* Update Maximum Bandwidth if needed */
1953 link_param_cmd_set_float(ifp, &iflp->max_bw, LP_MAX_BW, bw);
1954
1955 return CMD_SUCCESS;
1956 }
1957
1958 DEFUN (link_params_max_rsv_bw,
1959 link_params_max_rsv_bw_cmd,
1960 "max-rsv-bw BANDWIDTH",
1961 "Maximum bandwidth that may be reserved\n"
1962 "Bytes/second (IEEE floating point format)\n")
1963 {
1964 int idx_bandwidth = 1;
1965 VTY_DECLVAR_CONTEXT(interface, ifp);
1966 struct if_link_params *iflp = if_link_params_get(ifp);
1967 float bw;
1968
1969 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
1970 vty_out(vty, "link_params_max_rsv_bw: fscanf: %s\n",
1971 safe_strerror(errno));
1972 return CMD_WARNING_CONFIG_FAILED;
1973 }
1974
1975 /* Check that bandwidth is not greater than maximum bandwidth parameter
1976 */
1977 if (bw > iflp->max_bw) {
1978 vty_out(vty,
1979 "Maximum Reservable Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
1980 iflp->max_bw);
1981 return CMD_WARNING_CONFIG_FAILED;
1982 }
1983
1984 /* Update Maximum Reservable Bandwidth if needed */
1985 link_param_cmd_set_float(ifp, &iflp->max_rsv_bw, LP_MAX_RSV_BW, bw);
1986
1987 return CMD_SUCCESS;
1988 }
1989
1990 DEFUN (link_params_unrsv_bw,
1991 link_params_unrsv_bw_cmd,
1992 "unrsv-bw (0-7) BANDWIDTH",
1993 "Unreserved bandwidth at each priority level\n"
1994 "Priority\n"
1995 "Bytes/second (IEEE floating point format)\n")
1996 {
1997 int idx_number = 1;
1998 int idx_bandwidth = 2;
1999 VTY_DECLVAR_CONTEXT(interface, ifp);
2000 struct if_link_params *iflp = if_link_params_get(ifp);
2001 int priority;
2002 float bw;
2003
2004 /* We don't have to consider about range check here. */
2005 if (sscanf(argv[idx_number]->arg, "%d", &priority) != 1) {
2006 vty_out(vty, "link_params_unrsv_bw: fscanf: %s\n",
2007 safe_strerror(errno));
2008 return CMD_WARNING_CONFIG_FAILED;
2009 }
2010
2011 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
2012 vty_out(vty, "link_params_unrsv_bw: fscanf: %s\n",
2013 safe_strerror(errno));
2014 return CMD_WARNING_CONFIG_FAILED;
2015 }
2016
2017 /* Check that bandwidth is not greater than maximum bandwidth parameter
2018 */
2019 if (bw > iflp->max_bw) {
2020 vty_out(vty,
2021 "UnReserved Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
2022 iflp->max_bw);
2023 return CMD_WARNING_CONFIG_FAILED;
2024 }
2025
2026 /* Update Unreserved Bandwidth if needed */
2027 link_param_cmd_set_float(ifp, &iflp->unrsv_bw[priority], LP_UNRSV_BW,
2028 bw);
2029
2030 return CMD_SUCCESS;
2031 }
2032
2033 DEFUN (link_params_admin_grp,
2034 link_params_admin_grp_cmd,
2035 "admin-grp BITPATTERN",
2036 "Administrative group membership\n"
2037 "32-bit Hexadecimal value (e.g. 0xa1)\n")
2038 {
2039 int idx_bitpattern = 1;
2040 VTY_DECLVAR_CONTEXT(interface, ifp);
2041 struct if_link_params *iflp = if_link_params_get(ifp);
2042 unsigned long value;
2043
2044 if (sscanf(argv[idx_bitpattern]->arg, "0x%lx", &value) != 1) {
2045 vty_out(vty, "link_params_admin_grp: fscanf: %s\n",
2046 safe_strerror(errno));
2047 return CMD_WARNING_CONFIG_FAILED;
2048 }
2049
2050 /* Update Administrative Group if needed */
2051 link_param_cmd_set_uint32(ifp, &iflp->admin_grp, LP_ADM_GRP, value);
2052
2053 return CMD_SUCCESS;
2054 }
2055
2056 DEFUN (no_link_params_admin_grp,
2057 no_link_params_admin_grp_cmd,
2058 "no admin-grp",
2059 NO_STR
2060 "Disable Administrative group membership on this interface\n")
2061 {
2062 VTY_DECLVAR_CONTEXT(interface, ifp);
2063
2064 /* Unset Admin Group */
2065 link_param_cmd_unset(ifp, LP_ADM_GRP);
2066
2067 return CMD_SUCCESS;
2068 }
2069
2070 /* RFC5392 & RFC5316: INTER-AS */
2071 DEFUN (link_params_inter_as,
2072 link_params_inter_as_cmd,
2073 "neighbor A.B.C.D as (1-4294967295)",
2074 "Configure remote ASBR information (Neighbor IP address and AS number)\n"
2075 "Remote IP address in dot decimal A.B.C.D\n"
2076 "Remote AS number\n"
2077 "AS number in the range <1-4294967295>\n")
2078 {
2079 int idx_ipv4 = 1;
2080 int idx_number = 3;
2081
2082 VTY_DECLVAR_CONTEXT(interface, ifp);
2083 struct if_link_params *iflp = if_link_params_get(ifp);
2084 struct in_addr addr;
2085 uint32_t as;
2086
2087 if (!inet_aton(argv[idx_ipv4]->arg, &addr)) {
2088 vty_out(vty, "Please specify Router-Addr by A.B.C.D\n");
2089 return CMD_WARNING_CONFIG_FAILED;
2090 }
2091
2092 as = strtoul(argv[idx_number]->arg, NULL, 10);
2093
2094 /* Update Remote IP and Remote AS fields if needed */
2095 if (IS_PARAM_UNSET(iflp, LP_RMT_AS) || iflp->rmt_as != as
2096 || iflp->rmt_ip.s_addr != addr.s_addr) {
2097
2098 iflp->rmt_as = as;
2099 iflp->rmt_ip.s_addr = addr.s_addr;
2100 SET_PARAM(iflp, LP_RMT_AS);
2101
2102 /* force protocols to update LINK STATE due to parameters change
2103 */
2104 if (if_is_operative(ifp))
2105 zebra_interface_parameters_update(ifp);
2106 }
2107 return CMD_SUCCESS;
2108 }
2109
2110 DEFUN (no_link_params_inter_as,
2111 no_link_params_inter_as_cmd,
2112 "no neighbor",
2113 NO_STR
2114 "Remove Neighbor IP address and AS number for Inter-AS TE\n")
2115 {
2116 VTY_DECLVAR_CONTEXT(interface, ifp);
2117 struct if_link_params *iflp = if_link_params_get(ifp);
2118
2119 /* Reset Remote IP and AS neighbor */
2120 iflp->rmt_as = 0;
2121 iflp->rmt_ip.s_addr = 0;
2122 UNSET_PARAM(iflp, LP_RMT_AS);
2123
2124 /* force protocols to update LINK STATE due to parameters change */
2125 if (if_is_operative(ifp))
2126 zebra_interface_parameters_update(ifp);
2127
2128 return CMD_SUCCESS;
2129 }
2130
2131 /* RFC7471: OSPF Traffic Engineering (TE) Metric extensions &
2132 * draft-ietf-isis-metric-extensions-07.txt */
2133 DEFUN (link_params_delay,
2134 link_params_delay_cmd,
2135 "delay (0-16777215) [min (0-16777215) max (0-16777215)]",
2136 "Unidirectional Average Link Delay\n"
2137 "Average delay in micro-second as decimal (0...16777215)\n"
2138 "Minimum delay\n"
2139 "Minimum delay in micro-second as decimal (0...16777215)\n"
2140 "Maximum delay\n"
2141 "Maximum delay in micro-second as decimal (0...16777215)\n")
2142 {
2143 /* Get and Check new delay values */
2144 uint32_t delay = 0, low = 0, high = 0;
2145 delay = strtoul(argv[1]->arg, NULL, 10);
2146 if (argc == 6) {
2147 low = strtoul(argv[3]->arg, NULL, 10);
2148 high = strtoul(argv[5]->arg, NULL, 10);
2149 }
2150
2151 VTY_DECLVAR_CONTEXT(interface, ifp);
2152 struct if_link_params *iflp = if_link_params_get(ifp);
2153 uint8_t update = 0;
2154
2155 if (argc == 2) {
2156 /* Check new delay value against old Min and Max delays if set
2157 */
2158 if (IS_PARAM_SET(iflp, LP_MM_DELAY)
2159 && (delay <= iflp->min_delay || delay >= iflp->max_delay)) {
2160 vty_out(vty,
2161 "Average delay should be comprise between Min (%d) and Max (%d) delay\n",
2162 iflp->min_delay, iflp->max_delay);
2163 return CMD_WARNING_CONFIG_FAILED;
2164 }
2165 /* Update delay if value is not set or change */
2166 if (IS_PARAM_UNSET(iflp, LP_DELAY) || iflp->av_delay != delay) {
2167 iflp->av_delay = delay;
2168 SET_PARAM(iflp, LP_DELAY);
2169 update = 1;
2170 }
2171 /* Unset Min and Max delays if already set */
2172 if (IS_PARAM_SET(iflp, LP_MM_DELAY)) {
2173 iflp->min_delay = 0;
2174 iflp->max_delay = 0;
2175 UNSET_PARAM(iflp, LP_MM_DELAY);
2176 update = 1;
2177 }
2178 } else {
2179 /* Check new delays value coherency */
2180 if (delay <= low || delay >= high) {
2181 vty_out(vty,
2182 "Average delay should be comprise between Min (%d) and Max (%d) delay\n",
2183 low, high);
2184 return CMD_WARNING_CONFIG_FAILED;
2185 }
2186 /* Update Delays if needed */
2187 if (IS_PARAM_UNSET(iflp, LP_DELAY)
2188 || IS_PARAM_UNSET(iflp, LP_MM_DELAY)
2189 || iflp->av_delay != delay || iflp->min_delay != low
2190 || iflp->max_delay != high) {
2191 iflp->av_delay = delay;
2192 SET_PARAM(iflp, LP_DELAY);
2193 iflp->min_delay = low;
2194 iflp->max_delay = high;
2195 SET_PARAM(iflp, LP_MM_DELAY);
2196 update = 1;
2197 }
2198 }
2199
2200 /* force protocols to update LINK STATE due to parameters change */
2201 if (update == 1 && if_is_operative(ifp))
2202 zebra_interface_parameters_update(ifp);
2203
2204 return CMD_SUCCESS;
2205 }
2206
2207 DEFUN (no_link_params_delay,
2208 no_link_params_delay_cmd,
2209 "no delay",
2210 NO_STR
2211 "Disable Unidirectional Average, Min & Max Link Delay on this interface\n")
2212 {
2213 VTY_DECLVAR_CONTEXT(interface, ifp);
2214 struct if_link_params *iflp = if_link_params_get(ifp);
2215
2216 /* Unset Delays */
2217 iflp->av_delay = 0;
2218 UNSET_PARAM(iflp, LP_DELAY);
2219 iflp->min_delay = 0;
2220 iflp->max_delay = 0;
2221 UNSET_PARAM(iflp, LP_MM_DELAY);
2222
2223 /* force protocols to update LINK STATE due to parameters change */
2224 if (if_is_operative(ifp))
2225 zebra_interface_parameters_update(ifp);
2226
2227 return CMD_SUCCESS;
2228 }
2229
2230 DEFUN (link_params_delay_var,
2231 link_params_delay_var_cmd,
2232 "delay-variation (0-16777215)",
2233 "Unidirectional Link Delay Variation\n"
2234 "delay variation in micro-second as decimal (0...16777215)\n")
2235 {
2236 int idx_number = 1;
2237 VTY_DECLVAR_CONTEXT(interface, ifp);
2238 struct if_link_params *iflp = if_link_params_get(ifp);
2239 uint32_t value;
2240
2241 value = strtoul(argv[idx_number]->arg, NULL, 10);
2242
2243 /* Update Delay Variation if needed */
2244 link_param_cmd_set_uint32(ifp, &iflp->delay_var, LP_DELAY_VAR, value);
2245
2246 return CMD_SUCCESS;
2247 }
2248
2249 DEFUN (no_link_params_delay_var,
2250 no_link_params_delay_var_cmd,
2251 "no delay-variation",
2252 NO_STR
2253 "Disable Unidirectional Delay Variation on this interface\n")
2254 {
2255 VTY_DECLVAR_CONTEXT(interface, ifp);
2256
2257 /* Unset Delay Variation */
2258 link_param_cmd_unset(ifp, LP_DELAY_VAR);
2259
2260 return CMD_SUCCESS;
2261 }
2262
2263 DEFUN (link_params_pkt_loss,
2264 link_params_pkt_loss_cmd,
2265 "packet-loss PERCENTAGE",
2266 "Unidirectional Link Packet Loss\n"
2267 "percentage of total traffic by 0.000003% step and less than 50.331642%\n")
2268 {
2269 int idx_percentage = 1;
2270 VTY_DECLVAR_CONTEXT(interface, ifp);
2271 struct if_link_params *iflp = if_link_params_get(ifp);
2272 float fval;
2273
2274 if (sscanf(argv[idx_percentage]->arg, "%g", &fval) != 1) {
2275 vty_out(vty, "link_params_pkt_loss: fscanf: %s\n",
2276 safe_strerror(errno));
2277 return CMD_WARNING_CONFIG_FAILED;
2278 }
2279
2280 if (fval > MAX_PKT_LOSS)
2281 fval = MAX_PKT_LOSS;
2282
2283 /* Update Packet Loss if needed */
2284 link_param_cmd_set_float(ifp, &iflp->pkt_loss, LP_PKT_LOSS, fval);
2285
2286 return CMD_SUCCESS;
2287 }
2288
2289 DEFUN (no_link_params_pkt_loss,
2290 no_link_params_pkt_loss_cmd,
2291 "no packet-loss",
2292 NO_STR
2293 "Disable Unidirectional Link Packet Loss on this interface\n")
2294 {
2295 VTY_DECLVAR_CONTEXT(interface, ifp);
2296
2297 /* Unset Packet Loss */
2298 link_param_cmd_unset(ifp, LP_PKT_LOSS);
2299
2300 return CMD_SUCCESS;
2301 }
2302
2303 DEFUN (link_params_res_bw,
2304 link_params_res_bw_cmd,
2305 "res-bw BANDWIDTH",
2306 "Unidirectional Residual Bandwidth\n"
2307 "Bytes/second (IEEE floating point format)\n")
2308 {
2309 int idx_bandwidth = 1;
2310 VTY_DECLVAR_CONTEXT(interface, ifp);
2311 struct if_link_params *iflp = if_link_params_get(ifp);
2312 float bw;
2313
2314 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
2315 vty_out(vty, "link_params_res_bw: fscanf: %s\n",
2316 safe_strerror(errno));
2317 return CMD_WARNING_CONFIG_FAILED;
2318 }
2319
2320 /* Check that bandwidth is not greater than maximum bandwidth parameter
2321 */
2322 if (bw > iflp->max_bw) {
2323 vty_out(vty,
2324 "Residual Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
2325 iflp->max_bw);
2326 return CMD_WARNING_CONFIG_FAILED;
2327 }
2328
2329 /* Update Residual Bandwidth if needed */
2330 link_param_cmd_set_float(ifp, &iflp->res_bw, LP_RES_BW, bw);
2331
2332 return CMD_SUCCESS;
2333 }
2334
2335 DEFUN (no_link_params_res_bw,
2336 no_link_params_res_bw_cmd,
2337 "no res-bw",
2338 NO_STR
2339 "Disable Unidirectional Residual Bandwidth on this interface\n")
2340 {
2341 VTY_DECLVAR_CONTEXT(interface, ifp);
2342
2343 /* Unset Residual Bandwidth */
2344 link_param_cmd_unset(ifp, LP_RES_BW);
2345
2346 return CMD_SUCCESS;
2347 }
2348
2349 DEFUN (link_params_ava_bw,
2350 link_params_ava_bw_cmd,
2351 "ava-bw BANDWIDTH",
2352 "Unidirectional Available Bandwidth\n"
2353 "Bytes/second (IEEE floating point format)\n")
2354 {
2355 int idx_bandwidth = 1;
2356 VTY_DECLVAR_CONTEXT(interface, ifp);
2357 struct if_link_params *iflp = if_link_params_get(ifp);
2358 float bw;
2359
2360 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
2361 vty_out(vty, "link_params_ava_bw: fscanf: %s\n",
2362 safe_strerror(errno));
2363 return CMD_WARNING_CONFIG_FAILED;
2364 }
2365
2366 /* Check that bandwidth is not greater than maximum bandwidth parameter
2367 */
2368 if (bw > iflp->max_bw) {
2369 vty_out(vty,
2370 "Available Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
2371 iflp->max_bw);
2372 return CMD_WARNING_CONFIG_FAILED;
2373 }
2374
2375 /* Update Residual Bandwidth if needed */
2376 link_param_cmd_set_float(ifp, &iflp->ava_bw, LP_AVA_BW, bw);
2377
2378 return CMD_SUCCESS;
2379 }
2380
2381 DEFUN (no_link_params_ava_bw,
2382 no_link_params_ava_bw_cmd,
2383 "no ava-bw",
2384 NO_STR
2385 "Disable Unidirectional Available Bandwidth on this interface\n")
2386 {
2387 VTY_DECLVAR_CONTEXT(interface, ifp);
2388
2389 /* Unset Available Bandwidth */
2390 link_param_cmd_unset(ifp, LP_AVA_BW);
2391
2392 return CMD_SUCCESS;
2393 }
2394
2395 DEFUN (link_params_use_bw,
2396 link_params_use_bw_cmd,
2397 "use-bw BANDWIDTH",
2398 "Unidirectional Utilised Bandwidth\n"
2399 "Bytes/second (IEEE floating point format)\n")
2400 {
2401 int idx_bandwidth = 1;
2402 VTY_DECLVAR_CONTEXT(interface, ifp);
2403 struct if_link_params *iflp = if_link_params_get(ifp);
2404 float bw;
2405
2406 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
2407 vty_out(vty, "link_params_use_bw: fscanf: %s\n",
2408 safe_strerror(errno));
2409 return CMD_WARNING_CONFIG_FAILED;
2410 }
2411
2412 /* Check that bandwidth is not greater than maximum bandwidth parameter
2413 */
2414 if (bw > iflp->max_bw) {
2415 vty_out(vty,
2416 "Utilised Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
2417 iflp->max_bw);
2418 return CMD_WARNING_CONFIG_FAILED;
2419 }
2420
2421 /* Update Utilized Bandwidth if needed */
2422 link_param_cmd_set_float(ifp, &iflp->use_bw, LP_USE_BW, bw);
2423
2424 return CMD_SUCCESS;
2425 }
2426
2427 DEFUN (no_link_params_use_bw,
2428 no_link_params_use_bw_cmd,
2429 "no use-bw",
2430 NO_STR
2431 "Disable Unidirectional Utilised Bandwidth on this interface\n")
2432 {
2433 VTY_DECLVAR_CONTEXT(interface, ifp);
2434
2435 /* Unset Utilised Bandwidth */
2436 link_param_cmd_unset(ifp, LP_USE_BW);
2437
2438 return CMD_SUCCESS;
2439 }
2440
2441 static int ip_address_install(struct vty *vty, struct interface *ifp,
2442 const char *addr_str, const char *peer_str,
2443 const char *label)
2444 {
2445 struct zebra_if *if_data;
2446 struct prefix_ipv4 lp, pp;
2447 struct connected *ifc;
2448 struct prefix_ipv4 *p;
2449 int ret;
2450
2451 if_data = ifp->info;
2452
2453 ret = str2prefix_ipv4(addr_str, &lp);
2454 if (ret <= 0) {
2455 vty_out(vty, "%% Malformed address \n");
2456 return CMD_WARNING_CONFIG_FAILED;
2457 }
2458
2459 if (ipv4_martian(&lp.prefix)) {
2460 vty_out(vty, "%% Invalid address\n");
2461 return CMD_WARNING_CONFIG_FAILED;
2462 }
2463
2464 if (peer_str) {
2465 if (lp.prefixlen != 32) {
2466 vty_out(vty,
2467 "%% Local prefix length for P-t-P address must be /32\n");
2468 return CMD_WARNING_CONFIG_FAILED;
2469 }
2470
2471 ret = str2prefix_ipv4(peer_str, &pp);
2472 if (ret <= 0) {
2473 vty_out(vty, "%% Malformed peer address\n");
2474 return CMD_WARNING_CONFIG_FAILED;
2475 }
2476 }
2477
2478 ifc = connected_check_ptp(ifp, &lp, peer_str ? &pp : NULL);
2479 if (!ifc) {
2480 ifc = connected_new();
2481 ifc->ifp = ifp;
2482
2483 /* Address. */
2484 p = prefix_ipv4_new();
2485 *p = lp;
2486 ifc->address = (struct prefix *)p;
2487
2488 if (peer_str) {
2489 SET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
2490 p = prefix_ipv4_new();
2491 *p = pp;
2492 ifc->destination = (struct prefix *)p;
2493 } else if (p->prefixlen <= IPV4_MAX_PREFIXLEN - 2) {
2494 p = prefix_ipv4_new();
2495 *p = lp;
2496 p->prefix.s_addr = ipv4_broadcast_addr(p->prefix.s_addr,
2497 p->prefixlen);
2498 ifc->destination = (struct prefix *)p;
2499 }
2500
2501 /* Label. */
2502 if (label)
2503 ifc->label = XSTRDUP(MTYPE_CONNECTED_LABEL, label);
2504
2505 /* Add to linked list. */
2506 listnode_add(ifp->connected, ifc);
2507 }
2508
2509 /* This address is configured from zebra. */
2510 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
2511 SET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);
2512
2513 /* In case of this route need to install kernel. */
2514 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)
2515 && CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)
2516 && !(if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)) {
2517 /* Some system need to up the interface to set IP address. */
2518 if (!if_is_up(ifp)) {
2519 if_set_flags(ifp, IFF_UP | IFF_RUNNING);
2520 if_refresh(ifp);
2521 }
2522
2523 ret = if_set_prefix(ifp, ifc);
2524 if (ret < 0) {
2525 vty_out(vty, "%% Can't set interface IP address: %s.\n",
2526 safe_strerror(errno));
2527 return CMD_WARNING_CONFIG_FAILED;
2528 }
2529
2530 SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
2531 /* The address will be advertised to zebra clients when the
2532 * notification
2533 * from the kernel has been received.
2534 * It will also be added to the subnet chain list, then. */
2535 }
2536
2537 return CMD_SUCCESS;
2538 }
2539
2540 static int ip_address_uninstall(struct vty *vty, struct interface *ifp,
2541 const char *addr_str, const char *peer_str,
2542 const char *label)
2543 {
2544 struct prefix_ipv4 lp, pp;
2545 struct connected *ifc;
2546 int ret;
2547
2548 /* Convert to prefix structure. */
2549 ret = str2prefix_ipv4(addr_str, &lp);
2550 if (ret <= 0) {
2551 vty_out(vty, "%% Malformed address \n");
2552 return CMD_WARNING_CONFIG_FAILED;
2553 }
2554
2555 if (peer_str) {
2556 if (lp.prefixlen != 32) {
2557 vty_out(vty,
2558 "%% Local prefix length for P-t-P address must be /32\n");
2559 return CMD_WARNING_CONFIG_FAILED;
2560 }
2561
2562 ret = str2prefix_ipv4(peer_str, &pp);
2563 if (ret <= 0) {
2564 vty_out(vty, "%% Malformed peer address\n");
2565 return CMD_WARNING_CONFIG_FAILED;
2566 }
2567 }
2568
2569 /* Check current interface address. */
2570 ifc = connected_check_ptp(ifp, &lp, peer_str ? &pp : NULL);
2571 if (!ifc) {
2572 vty_out(vty, "%% Can't find address\n");
2573 return CMD_WARNING_CONFIG_FAILED;
2574 }
2575
2576 /* This is not configured address. */
2577 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
2578 return CMD_WARNING_CONFIG_FAILED;
2579
2580 UNSET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);
2581
2582 /* This is not real address or interface is not active. */
2583 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)
2584 || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
2585 listnode_delete(ifp->connected, ifc);
2586 connected_free(ifc);
2587 return CMD_WARNING_CONFIG_FAILED;
2588 }
2589
2590 /* This is real route. */
2591 ret = if_unset_prefix(ifp, ifc);
2592 if (ret < 0) {
2593 vty_out(vty, "%% Can't unset interface IP address: %s.\n",
2594 safe_strerror(errno));
2595 return CMD_WARNING_CONFIG_FAILED;
2596 }
2597 UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
2598 /* we will receive a kernel notification about this route being removed.
2599 * this will trigger its removal from the connected list. */
2600 return CMD_SUCCESS;
2601 }
2602
2603 DEFUN (ip_address,
2604 ip_address_cmd,
2605 "ip address A.B.C.D/M",
2606 "Interface Internet Protocol config commands\n"
2607 "Set the IP address of an interface\n"
2608 "IP address (e.g. 10.0.0.1/8)\n")
2609 {
2610 int idx_ipv4_prefixlen = 2;
2611 VTY_DECLVAR_CONTEXT(interface, ifp);
2612 return ip_address_install(vty, ifp, argv[idx_ipv4_prefixlen]->arg, NULL,
2613 NULL);
2614 }
2615
2616 DEFUN (no_ip_address,
2617 no_ip_address_cmd,
2618 "no ip address A.B.C.D/M",
2619 NO_STR
2620 "Interface Internet Protocol config commands\n"
2621 "Set the IP address of an interface\n"
2622 "IP Address (e.g. 10.0.0.1/8)\n")
2623 {
2624 int idx_ipv4_prefixlen = 3;
2625 VTY_DECLVAR_CONTEXT(interface, ifp);
2626 return ip_address_uninstall(vty, ifp, argv[idx_ipv4_prefixlen]->arg,
2627 NULL, NULL);
2628 }
2629
2630 DEFUN(ip_address_peer,
2631 ip_address_peer_cmd,
2632 "ip address A.B.C.D peer A.B.C.D/M",
2633 "Interface Internet Protocol config commands\n"
2634 "Set the IP address of an interface\n"
2635 "Local IP (e.g. 10.0.0.1) for P-t-P address\n"
2636 "Specify P-t-P address\n"
2637 "Peer IP address (e.g. 10.0.0.1/8)\n")
2638 {
2639 VTY_DECLVAR_CONTEXT(interface, ifp);
2640 return ip_address_install(vty, ifp, argv[2]->arg, argv[4]->arg, NULL);
2641 }
2642
2643 DEFUN(no_ip_address_peer,
2644 no_ip_address_peer_cmd,
2645 "no ip address A.B.C.D peer A.B.C.D/M",
2646 NO_STR
2647 "Interface Internet Protocol config commands\n"
2648 "Set the IP address of an interface\n"
2649 "Local IP (e.g. 10.0.0.1) for P-t-P address\n"
2650 "Specify P-t-P address\n"
2651 "Peer IP address (e.g. 10.0.0.1/8)\n")
2652 {
2653 VTY_DECLVAR_CONTEXT(interface, ifp);
2654 return ip_address_uninstall(vty, ifp, argv[3]->arg, argv[5]->arg, NULL);
2655 }
2656
2657 #ifdef HAVE_NETLINK
2658 DEFUN (ip_address_label,
2659 ip_address_label_cmd,
2660 "ip address A.B.C.D/M label LINE",
2661 "Interface Internet Protocol config commands\n"
2662 "Set the IP address of an interface\n"
2663 "IP address (e.g. 10.0.0.1/8)\n"
2664 "Label of this address\n"
2665 "Label\n")
2666 {
2667 int idx_ipv4_prefixlen = 2;
2668 int idx_line = 4;
2669 VTY_DECLVAR_CONTEXT(interface, ifp);
2670 return ip_address_install(vty, ifp, argv[idx_ipv4_prefixlen]->arg, NULL,
2671 argv[idx_line]->arg);
2672 }
2673
2674 DEFUN (no_ip_address_label,
2675 no_ip_address_label_cmd,
2676 "no ip address A.B.C.D/M label LINE",
2677 NO_STR
2678 "Interface Internet Protocol config commands\n"
2679 "Set the IP address of an interface\n"
2680 "IP address (e.g. 10.0.0.1/8)\n"
2681 "Label of this address\n"
2682 "Label\n")
2683 {
2684 int idx_ipv4_prefixlen = 3;
2685 int idx_line = 5;
2686 VTY_DECLVAR_CONTEXT(interface, ifp);
2687 return ip_address_uninstall(vty, ifp, argv[idx_ipv4_prefixlen]->arg,
2688 NULL, argv[idx_line]->arg);
2689 }
2690 #endif /* HAVE_NETLINK */
2691
2692 static int ipv6_address_install(struct vty *vty, struct interface *ifp,
2693 const char *addr_str, const char *peer_str,
2694 const char *label, int secondary)
2695 {
2696 struct zebra_if *if_data;
2697 struct prefix_ipv6 cp;
2698 struct connected *ifc;
2699 struct prefix_ipv6 *p;
2700 int ret;
2701
2702 if_data = ifp->info;
2703
2704 ret = str2prefix_ipv6(addr_str, &cp);
2705 if (ret <= 0) {
2706 vty_out(vty, "%% Malformed address \n");
2707 return CMD_WARNING_CONFIG_FAILED;
2708 }
2709
2710 if (ipv6_martian(&cp.prefix)) {
2711 vty_out(vty, "%% Invalid address\n");
2712 return CMD_WARNING_CONFIG_FAILED;
2713 }
2714
2715 ifc = connected_check(ifp, (struct prefix *)&cp);
2716 if (!ifc) {
2717 ifc = connected_new();
2718 ifc->ifp = ifp;
2719
2720 /* Address. */
2721 p = prefix_ipv6_new();
2722 *p = cp;
2723 ifc->address = (struct prefix *)p;
2724
2725 /* Secondary. */
2726 if (secondary)
2727 SET_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY);
2728
2729 /* Label. */
2730 if (label)
2731 ifc->label = XSTRDUP(MTYPE_CONNECTED_LABEL, label);
2732
2733 /* Add to linked list. */
2734 listnode_add(ifp->connected, ifc);
2735 }
2736
2737 /* This address is configured from zebra. */
2738 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
2739 SET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);
2740
2741 /* In case of this route need to install kernel. */
2742 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)
2743 && CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)
2744 && !(if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)) {
2745 /* Some system need to up the interface to set IP address. */
2746 if (!if_is_up(ifp)) {
2747 if_set_flags(ifp, IFF_UP | IFF_RUNNING);
2748 if_refresh(ifp);
2749 }
2750
2751 ret = if_prefix_add_ipv6(ifp, ifc);
2752
2753 if (ret < 0) {
2754 vty_out(vty, "%% Can't set interface IP address: %s.\n",
2755 safe_strerror(errno));
2756 return CMD_WARNING_CONFIG_FAILED;
2757 }
2758
2759 SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
2760 /* The address will be advertised to zebra clients when the
2761 * notification
2762 * from the kernel has been received. */
2763 }
2764
2765 return CMD_SUCCESS;
2766 }
2767
2768 /* Return true if an ipv6 address is configured on ifp */
2769 int ipv6_address_configured(struct interface *ifp)
2770 {
2771 struct connected *connected;
2772 struct listnode *node;
2773
2774 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected))
2775 if (CHECK_FLAG(connected->conf, ZEBRA_IFC_REAL)
2776 && (connected->address->family == AF_INET6))
2777 return 1;
2778
2779 return 0;
2780 }
2781
2782 static int ipv6_address_uninstall(struct vty *vty, struct interface *ifp,
2783 const char *addr_str, const char *peer_str,
2784 const char *label, int secondry)
2785 {
2786 struct prefix_ipv6 cp;
2787 struct connected *ifc;
2788 int ret;
2789
2790 /* Convert to prefix structure. */
2791 ret = str2prefix_ipv6(addr_str, &cp);
2792 if (ret <= 0) {
2793 vty_out(vty, "%% Malformed address \n");
2794 return CMD_WARNING_CONFIG_FAILED;
2795 }
2796
2797 /* Check current interface address. */
2798 ifc = connected_check(ifp, (struct prefix *)&cp);
2799 if (!ifc) {
2800 vty_out(vty, "%% Can't find address\n");
2801 return CMD_WARNING_CONFIG_FAILED;
2802 }
2803
2804 /* This is not configured address. */
2805 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
2806 return CMD_WARNING_CONFIG_FAILED;
2807
2808 UNSET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);
2809
2810 /* This is not real address or interface is not active. */
2811 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)
2812 || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
2813 listnode_delete(ifp->connected, ifc);
2814 connected_free(ifc);
2815 return CMD_WARNING_CONFIG_FAILED;
2816 }
2817
2818 /* This is real route. */
2819 ret = if_prefix_delete_ipv6(ifp, ifc);
2820 if (ret < 0) {
2821 vty_out(vty, "%% Can't unset interface IP address: %s.\n",
2822 safe_strerror(errno));
2823 return CMD_WARNING_CONFIG_FAILED;
2824 }
2825
2826 UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
2827 /* This information will be propagated to the zclients when the
2828 * kernel notification is received. */
2829 return CMD_SUCCESS;
2830 }
2831
2832 DEFUN (ipv6_address,
2833 ipv6_address_cmd,
2834 "ipv6 address X:X::X:X/M",
2835 "Interface IPv6 config commands\n"
2836 "Set the IP address of an interface\n"
2837 "IPv6 address (e.g. 3ffe:506::1/48)\n")
2838 {
2839 int idx_ipv6_prefixlen = 2;
2840 VTY_DECLVAR_CONTEXT(interface, ifp);
2841 return ipv6_address_install(vty, ifp, argv[idx_ipv6_prefixlen]->arg,
2842 NULL, NULL, 0);
2843 }
2844
2845 DEFUN (no_ipv6_address,
2846 no_ipv6_address_cmd,
2847 "no ipv6 address X:X::X:X/M",
2848 NO_STR
2849 "Interface IPv6 config commands\n"
2850 "Set the IP address of an interface\n"
2851 "IPv6 address (e.g. 3ffe:506::1/48)\n")
2852 {
2853 int idx_ipv6_prefixlen = 3;
2854 VTY_DECLVAR_CONTEXT(interface, ifp);
2855 return ipv6_address_uninstall(vty, ifp, argv[idx_ipv6_prefixlen]->arg,
2856 NULL, NULL, 0);
2857 }
2858
2859 static int link_params_config_write(struct vty *vty, struct interface *ifp)
2860 {
2861 int i;
2862
2863 if ((ifp == NULL) || !HAS_LINK_PARAMS(ifp))
2864 return -1;
2865
2866 struct if_link_params *iflp = ifp->link_params;
2867
2868 vty_out(vty, " link-params\n");
2869 vty_out(vty, " enable\n");
2870 if (IS_PARAM_SET(iflp, LP_TE_METRIC) && iflp->te_metric != ifp->metric)
2871 vty_out(vty, " metric %u\n", iflp->te_metric);
2872 if (IS_PARAM_SET(iflp, LP_MAX_BW) && iflp->max_bw != iflp->default_bw)
2873 vty_out(vty, " max-bw %g\n", iflp->max_bw);
2874 if (IS_PARAM_SET(iflp, LP_MAX_RSV_BW)
2875 && iflp->max_rsv_bw != iflp->default_bw)
2876 vty_out(vty, " max-rsv-bw %g\n", iflp->max_rsv_bw);
2877 if (IS_PARAM_SET(iflp, LP_UNRSV_BW)) {
2878 for (i = 0; i < 8; i++)
2879 if (iflp->unrsv_bw[i] != iflp->default_bw)
2880 vty_out(vty, " unrsv-bw %d %g\n", i,
2881 iflp->unrsv_bw[i]);
2882 }
2883 if (IS_PARAM_SET(iflp, LP_ADM_GRP))
2884 vty_out(vty, " admin-grp 0x%x\n", iflp->admin_grp);
2885 if (IS_PARAM_SET(iflp, LP_DELAY)) {
2886 vty_out(vty, " delay %u", iflp->av_delay);
2887 if (IS_PARAM_SET(iflp, LP_MM_DELAY)) {
2888 vty_out(vty, " min %u", iflp->min_delay);
2889 vty_out(vty, " max %u", iflp->max_delay);
2890 }
2891 vty_out(vty, "\n");
2892 }
2893 if (IS_PARAM_SET(iflp, LP_DELAY_VAR))
2894 vty_out(vty, " delay-variation %u\n", iflp->delay_var);
2895 if (IS_PARAM_SET(iflp, LP_PKT_LOSS))
2896 vty_out(vty, " packet-loss %g\n", iflp->pkt_loss);
2897 if (IS_PARAM_SET(iflp, LP_AVA_BW))
2898 vty_out(vty, " ava-bw %g\n", iflp->ava_bw);
2899 if (IS_PARAM_SET(iflp, LP_RES_BW))
2900 vty_out(vty, " res-bw %g\n", iflp->res_bw);
2901 if (IS_PARAM_SET(iflp, LP_USE_BW))
2902 vty_out(vty, " use-bw %g\n", iflp->use_bw);
2903 if (IS_PARAM_SET(iflp, LP_RMT_AS))
2904 vty_out(vty, " neighbor %s as %u\n", inet_ntoa(iflp->rmt_ip),
2905 iflp->rmt_as);
2906 vty_out(vty, " exit-link-params\n");
2907 return 0;
2908 }
2909
2910 static int if_config_write(struct vty *vty)
2911 {
2912 struct vrf *vrf;
2913 struct interface *ifp;
2914
2915 zebra_ptm_write(vty);
2916
2917 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
2918 FOR_ALL_INTERFACES (vrf, ifp) {
2919 struct zebra_if *if_data;
2920 struct listnode *addrnode;
2921 struct connected *ifc;
2922 struct prefix *p;
2923 struct vrf *vrf;
2924
2925 if_data = ifp->info;
2926 vrf = vrf_lookup_by_id(ifp->vrf_id);
2927
2928 if (ifp->vrf_id == VRF_DEFAULT)
2929 vty_frame(vty, "interface %s\n", ifp->name);
2930 else
2931 vty_frame(vty, "interface %s vrf %s\n",
2932 ifp->name, vrf->name);
2933
2934 if (if_data) {
2935 if (if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)
2936 vty_out(vty, " shutdown\n");
2937
2938 zebra_ptm_if_write(vty, if_data);
2939 }
2940
2941 if (ifp->desc)
2942 vty_out(vty, " description %s\n", ifp->desc);
2943
2944 /* Assign bandwidth here to avoid unnecessary interface
2945 flap
2946 while processing config script */
2947 if (ifp->bandwidth != 0)
2948 vty_out(vty, " bandwidth %u\n", ifp->bandwidth);
2949
2950 if (!CHECK_FLAG(ifp->status,
2951 ZEBRA_INTERFACE_LINKDETECTION))
2952 vty_out(vty, " no link-detect\n");
2953
2954 for (ALL_LIST_ELEMENTS_RO(ifp->connected, addrnode,
2955 ifc)) {
2956 if (CHECK_FLAG(ifc->conf,
2957 ZEBRA_IFC_CONFIGURED)) {
2958 char buf[INET6_ADDRSTRLEN];
2959 p = ifc->address;
2960 vty_out(vty, " ip%s address %s",
2961 p->family == AF_INET ? ""
2962 : "v6",
2963 inet_ntop(p->family,
2964 &p->u.prefix, buf,
2965 sizeof(buf)));
2966 if (CONNECTED_PEER(ifc)) {
2967 p = ifc->destination;
2968 vty_out(vty, " peer %s",
2969 inet_ntop(p->family,
2970 &p->u.prefix,
2971 buf,
2972 sizeof(buf)));
2973 }
2974 vty_out(vty, "/%d", p->prefixlen);
2975
2976 if (ifc->label)
2977 vty_out(vty, " label %s",
2978 ifc->label);
2979
2980 vty_out(vty, "\n");
2981 }
2982 }
2983
2984 if (if_data) {
2985 if (if_data->multicast
2986 != IF_ZEBRA_MULTICAST_UNSPEC)
2987 vty_out(vty, " %smulticast\n",
2988 if_data->multicast
2989 == IF_ZEBRA_MULTICAST_ON
2990 ? ""
2991 : "no ");
2992 }
2993
2994 hook_call(zebra_if_config_wr, vty, ifp);
2995
2996 link_params_config_write(vty, ifp);
2997
2998 vty_endframe(vty, "!\n");
2999 }
3000 return 0;
3001 }
3002
3003 /* Allocate and initialize interface vector. */
3004 void zebra_if_init(void)
3005 {
3006 /* Initialize interface and new hook. */
3007 hook_register_prio(if_add, 0, if_zebra_new_hook);
3008 hook_register_prio(if_del, 0, if_zebra_delete_hook);
3009
3010 /* Install configuration write function. */
3011 install_node(&interface_node, if_config_write);
3012 install_node(&link_params_node, NULL);
3013 if_cmd_init();
3014
3015 install_element(VIEW_NODE, &show_interface_cmd);
3016 install_element(VIEW_NODE, &show_interface_vrf_all_cmd);
3017 install_element(VIEW_NODE, &show_interface_name_vrf_cmd);
3018 install_element(VIEW_NODE, &show_interface_name_vrf_all_cmd);
3019
3020 install_element(ENABLE_NODE, &show_interface_desc_cmd);
3021 install_element(ENABLE_NODE, &show_interface_desc_vrf_all_cmd);
3022 install_element(INTERFACE_NODE, &multicast_cmd);
3023 install_element(INTERFACE_NODE, &no_multicast_cmd);
3024 install_element(INTERFACE_NODE, &linkdetect_cmd);
3025 install_element(INTERFACE_NODE, &no_linkdetect_cmd);
3026 install_element(INTERFACE_NODE, &shutdown_if_cmd);
3027 install_element(INTERFACE_NODE, &no_shutdown_if_cmd);
3028 install_element(INTERFACE_NODE, &bandwidth_if_cmd);
3029 install_element(INTERFACE_NODE, &no_bandwidth_if_cmd);
3030 install_element(INTERFACE_NODE, &ip_address_cmd);
3031 install_element(INTERFACE_NODE, &no_ip_address_cmd);
3032 install_element(INTERFACE_NODE, &ip_address_peer_cmd);
3033 install_element(INTERFACE_NODE, &no_ip_address_peer_cmd);
3034 install_element(INTERFACE_NODE, &ipv6_address_cmd);
3035 install_element(INTERFACE_NODE, &no_ipv6_address_cmd);
3036 #ifdef HAVE_NETLINK
3037 install_element(INTERFACE_NODE, &ip_address_label_cmd);
3038 install_element(INTERFACE_NODE, &no_ip_address_label_cmd);
3039 #endif /* HAVE_NETLINK */
3040 install_element(INTERFACE_NODE, &link_params_cmd);
3041 install_default(LINK_PARAMS_NODE);
3042 install_element(LINK_PARAMS_NODE, &link_params_enable_cmd);
3043 install_element(LINK_PARAMS_NODE, &no_link_params_enable_cmd);
3044 install_element(LINK_PARAMS_NODE, &link_params_metric_cmd);
3045 install_element(LINK_PARAMS_NODE, &no_link_params_metric_cmd);
3046 install_element(LINK_PARAMS_NODE, &link_params_maxbw_cmd);
3047 install_element(LINK_PARAMS_NODE, &link_params_max_rsv_bw_cmd);
3048 install_element(LINK_PARAMS_NODE, &link_params_unrsv_bw_cmd);
3049 install_element(LINK_PARAMS_NODE, &link_params_admin_grp_cmd);
3050 install_element(LINK_PARAMS_NODE, &no_link_params_admin_grp_cmd);
3051 install_element(LINK_PARAMS_NODE, &link_params_inter_as_cmd);
3052 install_element(LINK_PARAMS_NODE, &no_link_params_inter_as_cmd);
3053 install_element(LINK_PARAMS_NODE, &link_params_delay_cmd);
3054 install_element(LINK_PARAMS_NODE, &no_link_params_delay_cmd);
3055 install_element(LINK_PARAMS_NODE, &link_params_delay_var_cmd);
3056 install_element(LINK_PARAMS_NODE, &no_link_params_delay_var_cmd);
3057 install_element(LINK_PARAMS_NODE, &link_params_pkt_loss_cmd);
3058 install_element(LINK_PARAMS_NODE, &no_link_params_pkt_loss_cmd);
3059 install_element(LINK_PARAMS_NODE, &link_params_ava_bw_cmd);
3060 install_element(LINK_PARAMS_NODE, &no_link_params_ava_bw_cmd);
3061 install_element(LINK_PARAMS_NODE, &link_params_res_bw_cmd);
3062 install_element(LINK_PARAMS_NODE, &no_link_params_res_bw_cmd);
3063 install_element(LINK_PARAMS_NODE, &link_params_use_bw_cmd);
3064 install_element(LINK_PARAMS_NODE, &no_link_params_use_bw_cmd);
3065 install_element(LINK_PARAMS_NODE, &exit_link_params_cmd);
3066 }