]> git.proxmox.com Git - mirror_frr.git/blob - zebra/interface.c
Merge pull request #2939 from netravnen/feature/git-issue-teplate
[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 "lib_errors.h"
26 #include "vty.h"
27 #include "sockunion.h"
28 #include "prefix.h"
29 #include "command.h"
30 #include "memory.h"
31 #include "zebra_memory.h"
32 #include "ioctl.h"
33 #include "connected.h"
34 #include "log.h"
35 #include "zclient.h"
36 #include "vrf.h"
37
38 #include "zebra/rtadv.h"
39 #include "zebra_ns.h"
40 #include "zebra_vrf.h"
41 #include "zebra/interface.h"
42 #include "zebra/rib.h"
43 #include "zebra/rt.h"
44 #include "zebra/zserv.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/debug.h"
47 #include "zebra/irdp.h"
48 #include "zebra/zebra_ptm.h"
49 #include "zebra/rt_netlink.h"
50 #include "zebra/interface.h"
51 #include "zebra/zebra_vxlan.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 flog_err(
723 LIB_ERR_INTERFACE,
724 "interface %s vrf %u index %d is still up while being deleted.",
725 ifp->name, ifp->vrf_id, ifp->ifindex);
726 return;
727 }
728
729 if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
730 return;
731
732 /* Mark interface as inactive */
733 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE);
734
735 if (IS_ZEBRA_DEBUG_KERNEL)
736 zlog_debug("interface %s vrf %u index %d is now inactive.",
737 ifp->name, ifp->vrf_id, ifp->ifindex);
738
739 /* Delete connected routes from the kernel. */
740 if_delete_connected(ifp);
741
742 /* Send out notification on interface delete. */
743 zebra_interface_delete_update(ifp);
744
745 if_unlink_per_ns(ifp);
746
747 /* Update ifindex after distributing the delete message. This is in
748 case any client needs to have the old value of ifindex available
749 while processing the deletion. Each client daemon is responsible
750 for setting ifindex to IFINDEX_INTERNAL after processing the
751 interface deletion message. */
752 if_set_index(ifp, IFINDEX_INTERNAL);
753 ifp->node = NULL;
754
755 /* if the ifp is in a vrf, move it to default so vrf can be deleted if
756 * desired */
757 if (ifp->vrf_id)
758 if_handle_vrf_change(ifp, VRF_DEFAULT);
759
760 /* Reset some zebra interface params to default values. */
761 zif = ifp->info;
762 if (zif) {
763 zif->zif_type = ZEBRA_IF_OTHER;
764 zif->zif_slave_type = ZEBRA_IF_SLAVE_NONE;
765 memset(&zif->l2info, 0, sizeof(union zebra_l2if_info));
766 memset(&zif->brslave_info, 0,
767 sizeof(struct zebra_l2info_brslave));
768 }
769 }
770
771 /* VRF change for an interface */
772 void if_handle_vrf_change(struct interface *ifp, vrf_id_t vrf_id)
773 {
774 vrf_id_t old_vrf_id;
775
776 old_vrf_id = ifp->vrf_id;
777
778 /* Uninstall connected routes. */
779 if_uninstall_connected(ifp);
780
781 /* Delete any IPv4 neighbors created to implement RFC 5549 */
782 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
783
784 /* Delete all neighbor addresses learnt through IPv6 RA */
785 if_down_del_nbr_connected(ifp);
786
787 /* Send out notification on interface VRF change. */
788 /* This is to issue an UPDATE or a DELETE, as appropriate. */
789 zebra_interface_vrf_update_del(ifp, vrf_id);
790
791 /* update VRF */
792 if_update_to_new_vrf(ifp, vrf_id);
793
794 /* Send out notification on interface VRF change. */
795 /* This is to issue an ADD, if needed. */
796 zebra_interface_vrf_update_add(ifp, old_vrf_id);
797
798 /* Install connected routes (in new VRF). */
799 if (if_is_operative(ifp))
800 if_install_connected(ifp);
801
802 /* Due to connected route change, schedule RIB processing for both old
803 * and new VRF.
804 */
805 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
806 zlog_debug("%u: IF %s VRF change, scheduling RIB processing",
807 ifp->vrf_id, ifp->name);
808 rib_update(old_vrf_id, RIB_UPDATE_IF_CHANGE);
809 rib_update(ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
810 }
811
812 static void ipv6_ll_address_to_mac(struct in6_addr *address, uint8_t *mac)
813 {
814 mac[0] = address->s6_addr[8] ^ 0x02;
815 mac[1] = address->s6_addr[9];
816 mac[2] = address->s6_addr[10];
817 mac[3] = address->s6_addr[13];
818 mac[4] = address->s6_addr[14];
819 mac[5] = address->s6_addr[15];
820 }
821
822 void if_nbr_ipv6ll_to_ipv4ll_neigh_update(struct interface *ifp,
823 struct in6_addr *address, int add)
824 {
825 struct zebra_vrf *zvrf = vrf_info_lookup(ifp->vrf_id);
826 struct zebra_if *zif = ifp->info;
827 char buf[16] = "169.254.0.1";
828 struct in_addr ipv4_ll;
829 char mac[6];
830 ns_id_t ns_id;
831
832 inet_pton(AF_INET, buf, &ipv4_ll);
833
834 ipv6_ll_address_to_mac(address, (uint8_t *)mac);
835 ns_id = zvrf->zns->ns_id;
836
837 /*
838 * Remove existed arp record for the interface as netlink
839 * protocol does not have update message types
840 *
841 * supported message types are RTM_NEWNEIGH and RTM_DELNEIGH
842 */
843 kernel_neigh_update(0, ifp->ifindex, ipv4_ll.s_addr, mac, 6, ns_id);
844
845 /* Add arp record */
846 kernel_neigh_update(add, ifp->ifindex, ipv4_ll.s_addr, mac, 6, ns_id);
847
848 /*
849 * We need to note whether or not we originated a v6
850 * neighbor entry for this interface. So that when
851 * someone unwisely accidently deletes this entry
852 * we can shove it back in.
853 */
854 zif->v6_2_v4_ll_neigh_entry = !!add;
855 memcpy(&zif->v6_2_v4_ll_addr6, address, sizeof(*address));
856
857 zvrf->neigh_updates++;
858 }
859
860 static void if_nbr_ipv6ll_to_ipv4ll_neigh_add_all(struct interface *ifp)
861 {
862 if (listhead(ifp->nbr_connected)) {
863 struct nbr_connected *nbr_connected;
864 struct listnode *node;
865
866 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node,
867 nbr_connected))
868 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
869 ifp, &nbr_connected->address->u.prefix6, 1);
870 }
871 }
872
873 void if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(struct interface *ifp)
874 {
875 if (listhead(ifp->nbr_connected)) {
876 struct nbr_connected *nbr_connected;
877 struct listnode *node;
878
879 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node,
880 nbr_connected))
881 if_nbr_ipv6ll_to_ipv4ll_neigh_update(
882 ifp, &nbr_connected->address->u.prefix6, 0);
883 }
884 }
885
886 static void if_down_del_nbr_connected(struct interface *ifp)
887 {
888 struct nbr_connected *nbr_connected;
889 struct listnode *node, *nnode;
890
891 for (ALL_LIST_ELEMENTS(ifp->nbr_connected, node, nnode,
892 nbr_connected)) {
893 listnode_delete(ifp->nbr_connected, nbr_connected);
894 nbr_connected_free(nbr_connected);
895 }
896 }
897
898 /* Interface is up. */
899 void if_up(struct interface *ifp)
900 {
901 struct zebra_if *zif;
902 struct interface *link_if;
903 struct zebra_vrf *zvrf = vrf_info_lookup(ifp->vrf_id);
904
905 zif = ifp->info;
906 zif->up_count++;
907 quagga_timestamp(2, zif->up_last, sizeof(zif->up_last));
908
909 /* Notify the protocol daemons. */
910 if (ifp->ptm_enable && (ifp->ptm_status == ZEBRA_PTM_STATUS_DOWN)) {
911 zlog_warn("%s: interface %s hasn't passed ptm check\n",
912 __func__, ifp->name);
913 return;
914 }
915 zebra_interface_up_update(ifp);
916
917 if_nbr_ipv6ll_to_ipv4ll_neigh_add_all(ifp);
918
919 #if defined(HAVE_RTADV)
920 /* Enable fast tx of RA if enabled && RA interval is not in msecs */
921 if (zif->rtadv.AdvSendAdvertisements
922 && (zif->rtadv.MaxRtrAdvInterval >= 1000)) {
923 zif->rtadv.inFastRexmit = 1;
924 zif->rtadv.NumFastReXmitsRemain = RTADV_NUM_FAST_REXMITS;
925 }
926 #endif
927
928 /* Install connected routes to the kernel. */
929 if_install_connected(ifp);
930
931 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
932 zlog_debug("%u: IF %s up, scheduling RIB processing",
933 ifp->vrf_id, ifp->name);
934 rib_update(ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
935
936 /* Handle interface up for specific types for EVPN. Non-VxLAN interfaces
937 * are checked to see if (remote) neighbor entries need to be installed
938 * on them for ARP suppression.
939 */
940 if (IS_ZEBRA_IF_VXLAN(ifp))
941 zebra_vxlan_if_up(ifp);
942 else if (IS_ZEBRA_IF_BRIDGE(ifp)) {
943 link_if = ifp;
944 zebra_vxlan_svi_up(ifp, link_if);
945 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
946 link_if = if_lookup_by_index_per_ns(zvrf->zns,
947 zif->link_ifindex);
948 if (link_if)
949 zebra_vxlan_svi_up(ifp, link_if);
950 }
951 }
952
953 /* Interface goes down. We have to manage different behavior of based
954 OS. */
955 void if_down(struct interface *ifp)
956 {
957 struct zebra_if *zif;
958 struct interface *link_if;
959 struct zebra_vrf *zvrf = vrf_info_lookup(ifp->vrf_id);
960
961 zif = ifp->info;
962 zif->down_count++;
963 quagga_timestamp(2, zif->down_last, sizeof(zif->down_last));
964
965 /* Handle interface down for specific types for EVPN. Non-VxLAN
966 * interfaces
967 * are checked to see if (remote) neighbor entries need to be purged
968 * for ARP suppression.
969 */
970 if (IS_ZEBRA_IF_VXLAN(ifp))
971 zebra_vxlan_if_down(ifp);
972 else if (IS_ZEBRA_IF_BRIDGE(ifp)) {
973 link_if = ifp;
974 zebra_vxlan_svi_down(ifp, link_if);
975 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
976 link_if = if_lookup_by_index_per_ns(zvrf->zns,
977 zif->link_ifindex);
978 if (link_if)
979 zebra_vxlan_svi_down(ifp, link_if);
980 }
981
982
983 /* Notify to the protocol daemons. */
984 zebra_interface_down_update(ifp);
985
986 /* Uninstall connected routes from the kernel. */
987 if_uninstall_connected(ifp);
988
989 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
990 zlog_debug("%u: IF %s down, scheduling RIB processing",
991 ifp->vrf_id, ifp->name);
992 rib_update(ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
993
994 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
995
996 /* Delete all neighbor addresses learnt through IPv6 RA */
997 if_down_del_nbr_connected(ifp);
998 }
999
1000 void if_refresh(struct interface *ifp)
1001 {
1002 if_get_flags(ifp);
1003 }
1004
1005 void zebra_if_update_link(struct interface *ifp, ifindex_t link_ifindex,
1006 ns_id_t ns_id)
1007 {
1008 struct zebra_if *zif;
1009
1010 if (IS_ZEBRA_IF_VETH(ifp))
1011 return;
1012 zif = (struct zebra_if *)ifp->info;
1013 zif->link_ifindex = link_ifindex;
1014 zif->link = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
1015 link_ifindex);
1016 }
1017
1018
1019 /* Output prefix string to vty. */
1020 static int prefix_vty_out(struct vty *vty, struct prefix *p)
1021 {
1022 char str[INET6_ADDRSTRLEN];
1023
1024 inet_ntop(p->family, &p->u.prefix, str, sizeof(str));
1025 vty_out(vty, "%s", str);
1026 return strlen(str);
1027 }
1028
1029 /* Dump if address information to vty. */
1030 static void connected_dump_vty(struct vty *vty, struct connected *connected)
1031 {
1032 struct prefix *p;
1033
1034 /* Print interface address. */
1035 p = connected->address;
1036 vty_out(vty, " %s ", prefix_family_str(p));
1037 prefix_vty_out(vty, p);
1038 vty_out(vty, "/%d", p->prefixlen);
1039
1040 /* If there is destination address, print it. */
1041 if (connected->destination) {
1042 vty_out(vty,
1043 (CONNECTED_PEER(connected) ? " peer " : " broadcast "));
1044 prefix_vty_out(vty, connected->destination);
1045 if (CONNECTED_PEER(connected))
1046 vty_out(vty, "/%d", connected->destination->prefixlen);
1047 }
1048
1049 if (CHECK_FLAG(connected->flags, ZEBRA_IFA_SECONDARY))
1050 vty_out(vty, " secondary");
1051
1052 if (CHECK_FLAG(connected->flags, ZEBRA_IFA_UNNUMBERED))
1053 vty_out(vty, " unnumbered");
1054
1055 if (connected->label)
1056 vty_out(vty, " %s", connected->label);
1057
1058 vty_out(vty, "\n");
1059 }
1060
1061 /* Dump interface neighbor address information to vty. */
1062 static void nbr_connected_dump_vty(struct vty *vty,
1063 struct nbr_connected *connected)
1064 {
1065 struct prefix *p;
1066
1067 /* Print interface address. */
1068 p = connected->address;
1069 vty_out(vty, " %s ", prefix_family_str(p));
1070 prefix_vty_out(vty, p);
1071 vty_out(vty, "/%d", p->prefixlen);
1072
1073 vty_out(vty, "\n");
1074 }
1075
1076 static const char *zebra_ziftype_2str(zebra_iftype_t zif_type)
1077 {
1078 switch (zif_type) {
1079 case ZEBRA_IF_OTHER:
1080 return "Other";
1081 break;
1082
1083 case ZEBRA_IF_BRIDGE:
1084 return "Bridge";
1085 break;
1086
1087 case ZEBRA_IF_VLAN:
1088 return "Vlan";
1089 break;
1090
1091 case ZEBRA_IF_VXLAN:
1092 return "Vxlan";
1093 break;
1094
1095 case ZEBRA_IF_VRF:
1096 return "VRF";
1097 break;
1098
1099 case ZEBRA_IF_VETH:
1100 return "VETH";
1101 break;
1102
1103 default:
1104 return "Unknown";
1105 break;
1106 }
1107 }
1108
1109 /* Interface's information print out to vty interface. */
1110 static void if_dump_vty(struct vty *vty, struct interface *ifp)
1111 {
1112 struct connected *connected;
1113 struct nbr_connected *nbr_connected;
1114 struct listnode *node;
1115 struct route_node *rn;
1116 struct zebra_if *zebra_if;
1117 struct vrf *vrf;
1118
1119 zebra_if = ifp->info;
1120
1121 vty_out(vty, "Interface %s is ", ifp->name);
1122 if (if_is_up(ifp)) {
1123 vty_out(vty, "up, line protocol ");
1124
1125 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)) {
1126 if (if_is_running(ifp))
1127 vty_out(vty, "is up\n");
1128 else
1129 vty_out(vty, "is down\n");
1130 } else {
1131 vty_out(vty, "detection is disabled\n");
1132 }
1133 } else {
1134 vty_out(vty, "down\n");
1135 }
1136
1137 vty_out(vty, " Link ups: %5u last: %s\n", zebra_if->up_count,
1138 zebra_if->up_last[0] ? zebra_if->up_last : "(never)");
1139 vty_out(vty, " Link downs: %5u last: %s\n", zebra_if->down_count,
1140 zebra_if->down_last[0] ? zebra_if->down_last : "(never)");
1141
1142 zebra_ptm_show_status(vty, ifp);
1143
1144 vrf = vrf_lookup_by_id(ifp->vrf_id);
1145 vty_out(vty, " vrf: %s\n", vrf->name);
1146
1147 if (ifp->desc)
1148 vty_out(vty, " Description: %s\n", ifp->desc);
1149 if (ifp->ifindex == IFINDEX_INTERNAL) {
1150 vty_out(vty, " pseudo interface\n");
1151 return;
1152 } else if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1153 vty_out(vty, " index %d inactive interface\n", ifp->ifindex);
1154 return;
1155 }
1156
1157 vty_out(vty, " index %d metric %d mtu %d speed %u ", ifp->ifindex,
1158 ifp->metric, ifp->mtu, ifp->speed);
1159 if (ifp->mtu6 != ifp->mtu)
1160 vty_out(vty, "mtu6 %d ", ifp->mtu6);
1161 vty_out(vty, "\n flags: %s\n", if_flag_dump(ifp->flags));
1162
1163 /* Hardware address. */
1164 vty_out(vty, " Type: %s\n", if_link_type_str(ifp->ll_type));
1165 if (ifp->hw_addr_len != 0) {
1166 int i;
1167
1168 vty_out(vty, " HWaddr: ");
1169 for (i = 0; i < ifp->hw_addr_len; i++)
1170 vty_out(vty, "%s%02x", i == 0 ? "" : ":",
1171 ifp->hw_addr[i]);
1172 vty_out(vty, "\n");
1173 }
1174
1175 /* Bandwidth in Mbps */
1176 if (ifp->bandwidth != 0) {
1177 vty_out(vty, " bandwidth %u Mbps", ifp->bandwidth);
1178 vty_out(vty, "\n");
1179 }
1180
1181 for (rn = route_top(zebra_if->ipv4_subnets); rn; rn = route_next(rn)) {
1182 if (!rn->info)
1183 continue;
1184
1185 for (ALL_LIST_ELEMENTS_RO((struct list *)rn->info, node,
1186 connected))
1187 connected_dump_vty(vty, connected);
1188 }
1189
1190 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected)) {
1191 if (CHECK_FLAG(connected->conf, ZEBRA_IFC_REAL)
1192 && (connected->address->family == AF_INET6))
1193 connected_dump_vty(vty, connected);
1194 }
1195
1196 vty_out(vty, " Interface Type %s\n",
1197 zebra_ziftype_2str(zebra_if->zif_type));
1198 if (IS_ZEBRA_IF_BRIDGE(ifp)) {
1199 struct zebra_l2info_bridge *bridge_info;
1200
1201 bridge_info = &zebra_if->l2info.br;
1202 vty_out(vty, " Bridge VLAN-aware: %s\n",
1203 bridge_info->vlan_aware ? "yes" : "no");
1204 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
1205 struct zebra_l2info_vlan *vlan_info;
1206
1207 vlan_info = &zebra_if->l2info.vl;
1208 vty_out(vty, " VLAN Id %u\n", vlan_info->vid);
1209 } else if (IS_ZEBRA_IF_VXLAN(ifp)) {
1210 struct zebra_l2info_vxlan *vxlan_info;
1211
1212 vxlan_info = &zebra_if->l2info.vxl;
1213 vty_out(vty, " VxLAN Id %u", vxlan_info->vni);
1214 if (vxlan_info->vtep_ip.s_addr != INADDR_ANY)
1215 vty_out(vty, " VTEP IP: %s",
1216 inet_ntoa(vxlan_info->vtep_ip));
1217 if (vxlan_info->access_vlan)
1218 vty_out(vty, " Access VLAN Id %u",
1219 vxlan_info->access_vlan);
1220 vty_out(vty, "\n");
1221 }
1222
1223 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) {
1224 struct zebra_l2info_brslave *br_slave;
1225
1226 br_slave = &zebra_if->brslave_info;
1227 if (br_slave->bridge_ifindex != IFINDEX_INTERNAL)
1228 vty_out(vty, " Master (bridge) ifindex %u\n",
1229 br_slave->bridge_ifindex);
1230 }
1231
1232 if (zebra_if->link_ifindex != IFINDEX_INTERNAL) {
1233 vty_out(vty, " Link ifindex %u", zebra_if->link_ifindex);
1234 if (zebra_if->link)
1235 vty_out(vty, "(%s)\n", zebra_if->link->name);
1236 else
1237 vty_out(vty, "(Unknown)\n");
1238 }
1239
1240 if (HAS_LINK_PARAMS(ifp)) {
1241 int i;
1242 struct if_link_params *iflp = ifp->link_params;
1243 vty_out(vty, " Traffic Engineering Link Parameters:\n");
1244 if (IS_PARAM_SET(iflp, LP_TE_METRIC))
1245 vty_out(vty, " TE metric %u\n", iflp->te_metric);
1246 if (IS_PARAM_SET(iflp, LP_MAX_BW))
1247 vty_out(vty, " Maximum Bandwidth %g (Byte/s)\n",
1248 iflp->max_bw);
1249 if (IS_PARAM_SET(iflp, LP_MAX_RSV_BW))
1250 vty_out(vty,
1251 " Maximum Reservable Bandwidth %g (Byte/s)\n",
1252 iflp->max_rsv_bw);
1253 if (IS_PARAM_SET(iflp, LP_UNRSV_BW)) {
1254 vty_out(vty,
1255 " Unreserved Bandwidth per Class Type in Byte/s:\n");
1256 for (i = 0; i < MAX_CLASS_TYPE; i += 2)
1257 vty_out(vty,
1258 " [%d]: %g (Bytes/sec),\t[%d]: %g (Bytes/sec)\n",
1259 i, iflp->unrsv_bw[i], i + 1,
1260 iflp->unrsv_bw[i + 1]);
1261 }
1262
1263 if (IS_PARAM_SET(iflp, LP_ADM_GRP))
1264 vty_out(vty, " Administrative Group:%u\n",
1265 iflp->admin_grp);
1266 if (IS_PARAM_SET(iflp, LP_DELAY)) {
1267 vty_out(vty, " Link Delay Average: %u (micro-sec.)",
1268 iflp->av_delay);
1269 if (IS_PARAM_SET(iflp, LP_MM_DELAY)) {
1270 vty_out(vty, " Min: %u (micro-sec.)",
1271 iflp->min_delay);
1272 vty_out(vty, " Max: %u (micro-sec.)",
1273 iflp->max_delay);
1274 }
1275 vty_out(vty, "\n");
1276 }
1277 if (IS_PARAM_SET(iflp, LP_DELAY_VAR))
1278 vty_out(vty,
1279 " Link Delay Variation %u (micro-sec.)\n",
1280 iflp->delay_var);
1281 if (IS_PARAM_SET(iflp, LP_PKT_LOSS))
1282 vty_out(vty, " Link Packet Loss %g (in %%)\n",
1283 iflp->pkt_loss);
1284 if (IS_PARAM_SET(iflp, LP_AVA_BW))
1285 vty_out(vty, " Available Bandwidth %g (Byte/s)\n",
1286 iflp->ava_bw);
1287 if (IS_PARAM_SET(iflp, LP_RES_BW))
1288 vty_out(vty, " Residual Bandwidth %g (Byte/s)\n",
1289 iflp->res_bw);
1290 if (IS_PARAM_SET(iflp, LP_USE_BW))
1291 vty_out(vty, " Utilized Bandwidth %g (Byte/s)\n",
1292 iflp->use_bw);
1293 if (IS_PARAM_SET(iflp, LP_RMT_AS))
1294 vty_out(vty, " Neighbor ASBR IP: %s AS: %u \n",
1295 inet_ntoa(iflp->rmt_ip), iflp->rmt_as);
1296 }
1297
1298 hook_call(zebra_if_extra_info, vty, ifp);
1299
1300 if (listhead(ifp->nbr_connected))
1301 vty_out(vty, " Neighbor address(s):\n");
1302 for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node, nbr_connected))
1303 nbr_connected_dump_vty(vty, nbr_connected);
1304
1305 #ifdef HAVE_PROC_NET_DEV
1306 /* Statistics print out using proc file system. */
1307 vty_out(vty,
1308 " %lu input packets (%lu multicast), %lu bytes, "
1309 "%lu dropped\n",
1310 ifp->stats.rx_packets, ifp->stats.rx_multicast,
1311 ifp->stats.rx_bytes, ifp->stats.rx_dropped);
1312
1313 vty_out(vty,
1314 " %lu input errors, %lu length, %lu overrun,"
1315 " %lu CRC, %lu frame\n",
1316 ifp->stats.rx_errors, ifp->stats.rx_length_errors,
1317 ifp->stats.rx_over_errors, ifp->stats.rx_crc_errors,
1318 ifp->stats.rx_frame_errors);
1319
1320 vty_out(vty, " %lu fifo, %lu missed\n", ifp->stats.rx_fifo_errors,
1321 ifp->stats.rx_missed_errors);
1322
1323 vty_out(vty, " %lu output packets, %lu bytes, %lu dropped\n",
1324 ifp->stats.tx_packets, ifp->stats.tx_bytes,
1325 ifp->stats.tx_dropped);
1326
1327 vty_out(vty,
1328 " %lu output errors, %lu aborted, %lu carrier,"
1329 " %lu fifo, %lu heartbeat\n",
1330 ifp->stats.tx_errors, ifp->stats.tx_aborted_errors,
1331 ifp->stats.tx_carrier_errors, ifp->stats.tx_fifo_errors,
1332 ifp->stats.tx_heartbeat_errors);
1333
1334 vty_out(vty, " %lu window, %lu collisions\n",
1335 ifp->stats.tx_window_errors, ifp->stats.collisions);
1336 #endif /* HAVE_PROC_NET_DEV */
1337
1338 #ifdef HAVE_NET_RT_IFLIST
1339 #if defined(__bsdi__) || defined(__NetBSD__)
1340 /* Statistics print out using sysctl (). */
1341 vty_out(vty,
1342 " input packets %llu, bytes %llu, dropped %llu,"
1343 " multicast packets %llu\n",
1344 (unsigned long long)ifp->stats.ifi_ipackets,
1345 (unsigned long long)ifp->stats.ifi_ibytes,
1346 (unsigned long long)ifp->stats.ifi_iqdrops,
1347 (unsigned long long)ifp->stats.ifi_imcasts);
1348
1349 vty_out(vty, " input errors %llu\n",
1350 (unsigned long long)ifp->stats.ifi_ierrors);
1351
1352 vty_out(vty,
1353 " output packets %llu, bytes %llu,"
1354 " multicast packets %llu\n",
1355 (unsigned long long)ifp->stats.ifi_opackets,
1356 (unsigned long long)ifp->stats.ifi_obytes,
1357 (unsigned long long)ifp->stats.ifi_omcasts);
1358
1359 vty_out(vty, " output errors %llu\n",
1360 (unsigned long long)ifp->stats.ifi_oerrors);
1361
1362 vty_out(vty, " collisions %llu\n",
1363 (unsigned long long)ifp->stats.ifi_collisions);
1364 #else
1365 /* Statistics print out using sysctl (). */
1366 vty_out(vty,
1367 " input packets %lu, bytes %lu, dropped %lu,"
1368 " multicast packets %lu\n",
1369 ifp->stats.ifi_ipackets, ifp->stats.ifi_ibytes,
1370 ifp->stats.ifi_iqdrops, ifp->stats.ifi_imcasts);
1371
1372 vty_out(vty, " input errors %lu\n", ifp->stats.ifi_ierrors);
1373
1374 vty_out(vty,
1375 " output packets %lu, bytes %lu, multicast packets %lu\n",
1376 ifp->stats.ifi_opackets, ifp->stats.ifi_obytes,
1377 ifp->stats.ifi_omcasts);
1378
1379 vty_out(vty, " output errors %lu\n", ifp->stats.ifi_oerrors);
1380
1381 vty_out(vty, " collisions %lu\n", ifp->stats.ifi_collisions);
1382 #endif /* __bsdi__ || __NetBSD__ */
1383 #endif /* HAVE_NET_RT_IFLIST */
1384 }
1385
1386 static void interface_update_stats(void)
1387 {
1388 #ifdef HAVE_PROC_NET_DEV
1389 /* If system has interface statistics via proc file system, update
1390 statistics. */
1391 ifstat_update_proc();
1392 #endif /* HAVE_PROC_NET_DEV */
1393 #ifdef HAVE_NET_RT_IFLIST
1394 ifstat_update_sysctl();
1395 #endif /* HAVE_NET_RT_IFLIST */
1396 }
1397
1398 struct cmd_node interface_node = {INTERFACE_NODE, "%s(config-if)# ", 1};
1399
1400 /* Show all interfaces to vty. */
1401 DEFUN (show_interface,
1402 show_interface_cmd,
1403 "show interface [vrf NAME]",
1404 SHOW_STR
1405 "Interface status and configuration\n"
1406 VRF_CMD_HELP_STR)
1407 {
1408 struct vrf *vrf;
1409 struct interface *ifp;
1410 vrf_id_t vrf_id = VRF_DEFAULT;
1411
1412 interface_update_stats();
1413
1414 if (argc > 2)
1415 VRF_GET_ID(vrf_id, argv[3]->arg, false);
1416
1417 /* All interface print. */
1418 vrf = vrf_lookup_by_id(vrf_id);
1419 FOR_ALL_INTERFACES (vrf, ifp)
1420 if_dump_vty(vty, ifp);
1421
1422 return CMD_SUCCESS;
1423 }
1424
1425
1426 /* Show all interfaces to vty. */
1427 DEFUN (show_interface_vrf_all,
1428 show_interface_vrf_all_cmd,
1429 "show interface vrf all",
1430 SHOW_STR
1431 "Interface status and configuration\n"
1432 VRF_ALL_CMD_HELP_STR)
1433 {
1434 struct vrf *vrf;
1435 struct interface *ifp;
1436
1437 interface_update_stats();
1438
1439 /* All interface print. */
1440 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
1441 FOR_ALL_INTERFACES (vrf, ifp)
1442 if_dump_vty(vty, ifp);
1443
1444 return CMD_SUCCESS;
1445 }
1446
1447 /* Show specified interface to vty. */
1448
1449 DEFUN (show_interface_name_vrf,
1450 show_interface_name_vrf_cmd,
1451 "show interface IFNAME vrf NAME",
1452 SHOW_STR
1453 "Interface status and configuration\n"
1454 "Interface name\n"
1455 VRF_CMD_HELP_STR)
1456 {
1457 int idx_ifname = 2;
1458 int idx_name = 4;
1459 struct interface *ifp;
1460 vrf_id_t vrf_id;
1461
1462 interface_update_stats();
1463
1464 VRF_GET_ID(vrf_id, argv[idx_name]->arg, false);
1465
1466 /* Specified interface print. */
1467 ifp = if_lookup_by_name(argv[idx_ifname]->arg, vrf_id);
1468 if (ifp == NULL) {
1469 vty_out(vty, "%% Can't find interface %s\n",
1470 argv[idx_ifname]->arg);
1471 return CMD_WARNING;
1472 }
1473 if_dump_vty(vty, ifp);
1474
1475 return CMD_SUCCESS;
1476 }
1477
1478 /* Show specified interface to vty. */
1479 DEFUN (show_interface_name_vrf_all,
1480 show_interface_name_vrf_all_cmd,
1481 "show interface IFNAME [vrf all]",
1482 SHOW_STR
1483 "Interface status and configuration\n"
1484 "Interface name\n"
1485 VRF_ALL_CMD_HELP_STR)
1486 {
1487 int idx_ifname = 2;
1488 struct vrf *vrf;
1489 struct interface *ifp;
1490 int found = 0;
1491
1492 interface_update_stats();
1493
1494 /* All interface print. */
1495 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
1496 /* Specified interface print. */
1497 ifp = if_lookup_by_name(argv[idx_ifname]->arg, vrf->vrf_id);
1498 if (ifp) {
1499 if_dump_vty(vty, ifp);
1500 found++;
1501 }
1502 }
1503
1504 if (!found) {
1505 vty_out(vty, "%% Can't find interface %s\n",
1506 argv[idx_ifname]->arg);
1507 return CMD_WARNING;
1508 }
1509
1510 return CMD_SUCCESS;
1511 }
1512
1513
1514 static void if_show_description(struct vty *vty, vrf_id_t vrf_id)
1515 {
1516 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
1517 struct interface *ifp;
1518
1519 vty_out(vty, "Interface Status Protocol Description\n");
1520 FOR_ALL_INTERFACES (vrf, ifp) {
1521 int len;
1522
1523 len = vty_out(vty, "%s", ifp->name);
1524 vty_out(vty, "%*s", (16 - len), " ");
1525
1526 if (if_is_up(ifp)) {
1527 vty_out(vty, "up ");
1528 if (CHECK_FLAG(ifp->status,
1529 ZEBRA_INTERFACE_LINKDETECTION)) {
1530 if (if_is_running(ifp))
1531 vty_out(vty, "up ");
1532 else
1533 vty_out(vty, "down ");
1534 } else {
1535 vty_out(vty, "unknown ");
1536 }
1537 } else {
1538 vty_out(vty, "down down ");
1539 }
1540
1541 if (ifp->desc)
1542 vty_out(vty, "%s", ifp->desc);
1543 vty_out(vty, "\n");
1544 }
1545 }
1546
1547 DEFUN (show_interface_desc,
1548 show_interface_desc_cmd,
1549 "show interface description [vrf NAME]",
1550 SHOW_STR
1551 "Interface status and configuration\n"
1552 "Interface description\n"
1553 VRF_CMD_HELP_STR)
1554 {
1555 vrf_id_t vrf_id = VRF_DEFAULT;
1556
1557 if (argc > 3)
1558 VRF_GET_ID(vrf_id, argv[4]->arg, false);
1559
1560 if_show_description(vty, vrf_id);
1561
1562 return CMD_SUCCESS;
1563 }
1564
1565
1566 DEFUN (show_interface_desc_vrf_all,
1567 show_interface_desc_vrf_all_cmd,
1568 "show interface description vrf all",
1569 SHOW_STR
1570 "Interface status and configuration\n"
1571 "Interface description\n"
1572 VRF_ALL_CMD_HELP_STR)
1573 {
1574 struct vrf *vrf;
1575
1576 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
1577 if (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) {
1578 vty_out(vty, "\n\tVRF %u\n\n", vrf->vrf_id);
1579 if_show_description(vty, vrf->vrf_id);
1580 }
1581
1582 return CMD_SUCCESS;
1583 }
1584
1585 DEFUN (multicast,
1586 multicast_cmd,
1587 "multicast",
1588 "Set multicast flag to interface\n")
1589 {
1590 VTY_DECLVAR_CONTEXT(interface, ifp);
1591 int ret;
1592 struct zebra_if *if_data;
1593
1594 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1595 ret = if_set_flags(ifp, IFF_MULTICAST);
1596 if (ret < 0) {
1597 vty_out(vty, "Can't set multicast flag\n");
1598 return CMD_WARNING_CONFIG_FAILED;
1599 }
1600 if_refresh(ifp);
1601 }
1602 if_data = ifp->info;
1603 if_data->multicast = IF_ZEBRA_MULTICAST_ON;
1604
1605 return CMD_SUCCESS;
1606 }
1607
1608 DEFUN (no_multicast,
1609 no_multicast_cmd,
1610 "no multicast",
1611 NO_STR
1612 "Unset multicast flag to interface\n")
1613 {
1614 VTY_DECLVAR_CONTEXT(interface, ifp);
1615 int ret;
1616 struct zebra_if *if_data;
1617
1618 if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1619 ret = if_unset_flags(ifp, IFF_MULTICAST);
1620 if (ret < 0) {
1621 vty_out(vty, "Can't unset multicast flag\n");
1622 return CMD_WARNING_CONFIG_FAILED;
1623 }
1624 if_refresh(ifp);
1625 }
1626 if_data = ifp->info;
1627 if_data->multicast = IF_ZEBRA_MULTICAST_OFF;
1628
1629 return CMD_SUCCESS;
1630 }
1631
1632 DEFUN (linkdetect,
1633 linkdetect_cmd,
1634 "link-detect",
1635 "Enable link detection on interface\n")
1636 {
1637 VTY_DECLVAR_CONTEXT(interface, ifp);
1638 int if_was_operative;
1639
1640 if_was_operative = if_is_no_ptm_operative(ifp);
1641 SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
1642
1643 /* When linkdetection is enabled, if might come down */
1644 if (!if_is_no_ptm_operative(ifp) && if_was_operative)
1645 if_down(ifp);
1646
1647 /* FIXME: Will defer status change forwarding if interface
1648 does not come down! */
1649
1650 return CMD_SUCCESS;
1651 }
1652
1653
1654 DEFUN (no_linkdetect,
1655 no_linkdetect_cmd,
1656 "no link-detect",
1657 NO_STR
1658 "Disable link detection on interface\n")
1659 {
1660 VTY_DECLVAR_CONTEXT(interface, ifp);
1661 int if_was_operative;
1662
1663 if_was_operative = if_is_no_ptm_operative(ifp);
1664 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
1665
1666 /* Interface may come up after disabling link detection */
1667 if (if_is_operative(ifp) && !if_was_operative)
1668 if_up(ifp);
1669
1670 /* FIXME: see linkdetect_cmd */
1671
1672 return CMD_SUCCESS;
1673 }
1674
1675 DEFUN (shutdown_if,
1676 shutdown_if_cmd,
1677 "shutdown",
1678 "Shutdown the selected interface\n")
1679 {
1680 VTY_DECLVAR_CONTEXT(interface, ifp);
1681 int ret;
1682 struct zebra_if *if_data;
1683
1684 if (ifp->ifindex != IFINDEX_INTERNAL) {
1685 ret = if_unset_flags(ifp, IFF_UP);
1686 if (ret < 0) {
1687 vty_out(vty, "Can't shutdown interface\n");
1688 return CMD_WARNING_CONFIG_FAILED;
1689 }
1690 if_refresh(ifp);
1691 }
1692 if_data = ifp->info;
1693 if_data->shutdown = IF_ZEBRA_SHUTDOWN_ON;
1694
1695 return CMD_SUCCESS;
1696 }
1697
1698 DEFUN (no_shutdown_if,
1699 no_shutdown_if_cmd,
1700 "no shutdown",
1701 NO_STR
1702 "Shutdown the selected interface\n")
1703 {
1704 VTY_DECLVAR_CONTEXT(interface, ifp);
1705 int ret;
1706 struct zebra_if *if_data;
1707
1708 if (ifp->ifindex != IFINDEX_INTERNAL) {
1709 ret = if_set_flags(ifp, IFF_UP | IFF_RUNNING);
1710 if (ret < 0) {
1711 vty_out(vty, "Can't up interface\n");
1712 return CMD_WARNING_CONFIG_FAILED;
1713 }
1714 if_refresh(ifp);
1715
1716 /* Some addresses (in particular, IPv6 addresses on Linux) get
1717 * removed when the interface goes down. They need to be
1718 * readded.
1719 */
1720 if_addr_wakeup(ifp);
1721 }
1722
1723 if_data = ifp->info;
1724 if_data->shutdown = IF_ZEBRA_SHUTDOWN_OFF;
1725
1726 return CMD_SUCCESS;
1727 }
1728
1729 DEFUN (bandwidth_if,
1730 bandwidth_if_cmd,
1731 "bandwidth (1-100000)",
1732 "Set bandwidth informational parameter\n"
1733 "Bandwidth in megabits\n")
1734 {
1735 int idx_number = 1;
1736 VTY_DECLVAR_CONTEXT(interface, ifp);
1737 unsigned int bandwidth;
1738
1739 bandwidth = strtol(argv[idx_number]->arg, NULL, 10);
1740
1741 /* bandwidth range is <1-100000> */
1742 if (bandwidth < 1 || bandwidth > 100000) {
1743 vty_out(vty, "Bandwidth is invalid\n");
1744 return CMD_WARNING_CONFIG_FAILED;
1745 }
1746
1747 ifp->bandwidth = bandwidth;
1748
1749 /* force protocols to recalculate routes due to cost change */
1750 if (if_is_operative(ifp))
1751 zebra_interface_up_update(ifp);
1752
1753 return CMD_SUCCESS;
1754 }
1755
1756 DEFUN (no_bandwidth_if,
1757 no_bandwidth_if_cmd,
1758 "no bandwidth [(1-100000)]",
1759 NO_STR
1760 "Set bandwidth informational parameter\n"
1761 "Bandwidth in megabits\n")
1762 {
1763 VTY_DECLVAR_CONTEXT(interface, ifp);
1764
1765 ifp->bandwidth = 0;
1766
1767 /* force protocols to recalculate routes due to cost change */
1768 if (if_is_operative(ifp))
1769 zebra_interface_up_update(ifp);
1770
1771 return CMD_SUCCESS;
1772 }
1773
1774
1775 struct cmd_node link_params_node = {
1776 LINK_PARAMS_NODE, "%s(config-link-params)# ", 1,
1777 };
1778
1779 static void link_param_cmd_set_uint32(struct interface *ifp, uint32_t *field,
1780 uint32_t type, uint32_t value)
1781 {
1782 /* Update field as needed */
1783 if (IS_PARAM_UNSET(ifp->link_params, type) || *field != value) {
1784 *field = value;
1785 SET_PARAM(ifp->link_params, type);
1786
1787 /* force protocols to update LINK STATE due to parameters change
1788 */
1789 if (if_is_operative(ifp))
1790 zebra_interface_parameters_update(ifp);
1791 }
1792 }
1793 static void link_param_cmd_set_float(struct interface *ifp, float *field,
1794 uint32_t type, float value)
1795 {
1796
1797 /* Update field as needed */
1798 if (IS_PARAM_UNSET(ifp->link_params, type) || *field != value) {
1799 *field = value;
1800 SET_PARAM(ifp->link_params, type);
1801
1802 /* force protocols to update LINK STATE due to parameters change
1803 */
1804 if (if_is_operative(ifp))
1805 zebra_interface_parameters_update(ifp);
1806 }
1807 }
1808
1809 static void link_param_cmd_unset(struct interface *ifp, uint32_t type)
1810 {
1811 if (ifp->link_params == NULL)
1812 return;
1813
1814 /* Unset field */
1815 UNSET_PARAM(ifp->link_params, type);
1816
1817 /* force protocols to update LINK STATE due to parameters change */
1818 if (if_is_operative(ifp))
1819 zebra_interface_parameters_update(ifp);
1820 }
1821
1822 DEFUN_NOSH (link_params,
1823 link_params_cmd,
1824 "link-params",
1825 LINK_PARAMS_STR)
1826 {
1827 /* vty->qobj_index stays the same @ interface pointer */
1828 vty->node = LINK_PARAMS_NODE;
1829
1830 return CMD_SUCCESS;
1831 }
1832
1833 DEFUN_NOSH (exit_link_params,
1834 exit_link_params_cmd,
1835 "exit-link-params",
1836 "Exit from Link Params configuration mode\n")
1837 {
1838 if (vty->node == LINK_PARAMS_NODE)
1839 vty->node = INTERFACE_NODE;
1840 return CMD_SUCCESS;
1841 }
1842
1843 /* Specific Traffic Engineering parameters commands */
1844 DEFUN (link_params_enable,
1845 link_params_enable_cmd,
1846 "enable",
1847 "Activate link parameters on this interface\n")
1848 {
1849 VTY_DECLVAR_CONTEXT(interface, ifp);
1850
1851 /* This command could be issue at startup, when activate MPLS TE */
1852 /* on a new interface or after a ON / OFF / ON toggle */
1853 /* In all case, TE parameters are reset to their default factory */
1854 if (IS_ZEBRA_DEBUG_EVENT)
1855 zlog_debug(
1856 "Link-params: enable TE link parameters on interface %s",
1857 ifp->name);
1858
1859 if (!if_link_params_get(ifp)) {
1860 if (IS_ZEBRA_DEBUG_EVENT)
1861 zlog_debug(
1862 "Link-params: failed to init TE link parameters %s",
1863 ifp->name);
1864
1865 return CMD_WARNING_CONFIG_FAILED;
1866 }
1867
1868 /* force protocols to update LINK STATE due to parameters change */
1869 if (if_is_operative(ifp))
1870 zebra_interface_parameters_update(ifp);
1871
1872 return CMD_SUCCESS;
1873 }
1874
1875 DEFUN (no_link_params_enable,
1876 no_link_params_enable_cmd,
1877 "no enable",
1878 NO_STR
1879 "Disable link parameters on this interface\n")
1880 {
1881 VTY_DECLVAR_CONTEXT(interface, ifp);
1882
1883 zlog_debug("MPLS-TE: disable TE link parameters on interface %s",
1884 ifp->name);
1885
1886 if_link_params_free(ifp);
1887
1888 /* force protocols to update LINK STATE due to parameters change */
1889 if (if_is_operative(ifp))
1890 zebra_interface_parameters_update(ifp);
1891
1892 return CMD_SUCCESS;
1893 }
1894
1895 /* STANDARD TE metrics */
1896 DEFUN (link_params_metric,
1897 link_params_metric_cmd,
1898 "metric (0-4294967295)",
1899 "Link metric for MPLS-TE purpose\n"
1900 "Metric value in decimal\n")
1901 {
1902 int idx_number = 1;
1903 VTY_DECLVAR_CONTEXT(interface, ifp);
1904 struct if_link_params *iflp = if_link_params_get(ifp);
1905 uint32_t metric;
1906
1907 metric = strtoul(argv[idx_number]->arg, NULL, 10);
1908
1909 /* Update TE metric if needed */
1910 link_param_cmd_set_uint32(ifp, &iflp->te_metric, LP_TE_METRIC, metric);
1911
1912 return CMD_SUCCESS;
1913 }
1914
1915 DEFUN (no_link_params_metric,
1916 no_link_params_metric_cmd,
1917 "no metric",
1918 NO_STR
1919 "Disable Link Metric on this interface\n")
1920 {
1921 VTY_DECLVAR_CONTEXT(interface, ifp);
1922
1923 /* Unset TE Metric */
1924 link_param_cmd_unset(ifp, LP_TE_METRIC);
1925
1926 return CMD_SUCCESS;
1927 }
1928
1929 DEFUN (link_params_maxbw,
1930 link_params_maxbw_cmd,
1931 "max-bw BANDWIDTH",
1932 "Maximum bandwidth that can be used\n"
1933 "Bytes/second (IEEE floating point format)\n")
1934 {
1935 int idx_bandwidth = 1;
1936 VTY_DECLVAR_CONTEXT(interface, ifp);
1937 struct if_link_params *iflp = if_link_params_get(ifp);
1938
1939 float bw;
1940
1941 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
1942 vty_out(vty, "link_params_maxbw: fscanf: %s\n",
1943 safe_strerror(errno));
1944 return CMD_WARNING_CONFIG_FAILED;
1945 }
1946
1947 /* Check that Maximum bandwidth is not lower than other bandwidth
1948 * parameters */
1949 if ((bw <= iflp->max_rsv_bw) || (bw <= iflp->unrsv_bw[0])
1950 || (bw <= iflp->unrsv_bw[1]) || (bw <= iflp->unrsv_bw[2])
1951 || (bw <= iflp->unrsv_bw[3]) || (bw <= iflp->unrsv_bw[4])
1952 || (bw <= iflp->unrsv_bw[5]) || (bw <= iflp->unrsv_bw[6])
1953 || (bw <= iflp->unrsv_bw[7]) || (bw <= iflp->ava_bw)
1954 || (bw <= iflp->res_bw) || (bw <= iflp->use_bw)) {
1955 vty_out(vty,
1956 "Maximum Bandwidth could not be lower than others bandwidth\n");
1957 return CMD_WARNING_CONFIG_FAILED;
1958 }
1959
1960 /* Update Maximum Bandwidth if needed */
1961 link_param_cmd_set_float(ifp, &iflp->max_bw, LP_MAX_BW, bw);
1962
1963 return CMD_SUCCESS;
1964 }
1965
1966 DEFUN (link_params_max_rsv_bw,
1967 link_params_max_rsv_bw_cmd,
1968 "max-rsv-bw BANDWIDTH",
1969 "Maximum bandwidth that may be reserved\n"
1970 "Bytes/second (IEEE floating point format)\n")
1971 {
1972 int idx_bandwidth = 1;
1973 VTY_DECLVAR_CONTEXT(interface, ifp);
1974 struct if_link_params *iflp = if_link_params_get(ifp);
1975 float bw;
1976
1977 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
1978 vty_out(vty, "link_params_max_rsv_bw: fscanf: %s\n",
1979 safe_strerror(errno));
1980 return CMD_WARNING_CONFIG_FAILED;
1981 }
1982
1983 /* Check that bandwidth is not greater than maximum bandwidth parameter
1984 */
1985 if (bw > iflp->max_bw) {
1986 vty_out(vty,
1987 "Maximum Reservable Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
1988 iflp->max_bw);
1989 return CMD_WARNING_CONFIG_FAILED;
1990 }
1991
1992 /* Update Maximum Reservable Bandwidth if needed */
1993 link_param_cmd_set_float(ifp, &iflp->max_rsv_bw, LP_MAX_RSV_BW, bw);
1994
1995 return CMD_SUCCESS;
1996 }
1997
1998 DEFUN (link_params_unrsv_bw,
1999 link_params_unrsv_bw_cmd,
2000 "unrsv-bw (0-7) BANDWIDTH",
2001 "Unreserved bandwidth at each priority level\n"
2002 "Priority\n"
2003 "Bytes/second (IEEE floating point format)\n")
2004 {
2005 int idx_number = 1;
2006 int idx_bandwidth = 2;
2007 VTY_DECLVAR_CONTEXT(interface, ifp);
2008 struct if_link_params *iflp = if_link_params_get(ifp);
2009 int priority;
2010 float bw;
2011
2012 /* We don't have to consider about range check here. */
2013 if (sscanf(argv[idx_number]->arg, "%d", &priority) != 1) {
2014 vty_out(vty, "link_params_unrsv_bw: fscanf: %s\n",
2015 safe_strerror(errno));
2016 return CMD_WARNING_CONFIG_FAILED;
2017 }
2018
2019 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
2020 vty_out(vty, "link_params_unrsv_bw: fscanf: %s\n",
2021 safe_strerror(errno));
2022 return CMD_WARNING_CONFIG_FAILED;
2023 }
2024
2025 /* Check that bandwidth is not greater than maximum bandwidth parameter
2026 */
2027 if (bw > iflp->max_bw) {
2028 vty_out(vty,
2029 "UnReserved Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
2030 iflp->max_bw);
2031 return CMD_WARNING_CONFIG_FAILED;
2032 }
2033
2034 /* Update Unreserved Bandwidth if needed */
2035 link_param_cmd_set_float(ifp, &iflp->unrsv_bw[priority], LP_UNRSV_BW,
2036 bw);
2037
2038 return CMD_SUCCESS;
2039 }
2040
2041 DEFUN (link_params_admin_grp,
2042 link_params_admin_grp_cmd,
2043 "admin-grp BITPATTERN",
2044 "Administrative group membership\n"
2045 "32-bit Hexadecimal value (e.g. 0xa1)\n")
2046 {
2047 int idx_bitpattern = 1;
2048 VTY_DECLVAR_CONTEXT(interface, ifp);
2049 struct if_link_params *iflp = if_link_params_get(ifp);
2050 unsigned long value;
2051
2052 if (sscanf(argv[idx_bitpattern]->arg, "0x%lx", &value) != 1) {
2053 vty_out(vty, "link_params_admin_grp: fscanf: %s\n",
2054 safe_strerror(errno));
2055 return CMD_WARNING_CONFIG_FAILED;
2056 }
2057
2058 /* Update Administrative Group if needed */
2059 link_param_cmd_set_uint32(ifp, &iflp->admin_grp, LP_ADM_GRP, value);
2060
2061 return CMD_SUCCESS;
2062 }
2063
2064 DEFUN (no_link_params_admin_grp,
2065 no_link_params_admin_grp_cmd,
2066 "no admin-grp",
2067 NO_STR
2068 "Disable Administrative group membership on this interface\n")
2069 {
2070 VTY_DECLVAR_CONTEXT(interface, ifp);
2071
2072 /* Unset Admin Group */
2073 link_param_cmd_unset(ifp, LP_ADM_GRP);
2074
2075 return CMD_SUCCESS;
2076 }
2077
2078 /* RFC5392 & RFC5316: INTER-AS */
2079 DEFUN (link_params_inter_as,
2080 link_params_inter_as_cmd,
2081 "neighbor A.B.C.D as (1-4294967295)",
2082 "Configure remote ASBR information (Neighbor IP address and AS number)\n"
2083 "Remote IP address in dot decimal A.B.C.D\n"
2084 "Remote AS number\n"
2085 "AS number in the range <1-4294967295>\n")
2086 {
2087 int idx_ipv4 = 1;
2088 int idx_number = 3;
2089
2090 VTY_DECLVAR_CONTEXT(interface, ifp);
2091 struct if_link_params *iflp = if_link_params_get(ifp);
2092 struct in_addr addr;
2093 uint32_t as;
2094
2095 if (!inet_aton(argv[idx_ipv4]->arg, &addr)) {
2096 vty_out(vty, "Please specify Router-Addr by A.B.C.D\n");
2097 return CMD_WARNING_CONFIG_FAILED;
2098 }
2099
2100 as = strtoul(argv[idx_number]->arg, NULL, 10);
2101
2102 /* Update Remote IP and Remote AS fields if needed */
2103 if (IS_PARAM_UNSET(iflp, LP_RMT_AS) || iflp->rmt_as != as
2104 || iflp->rmt_ip.s_addr != addr.s_addr) {
2105
2106 iflp->rmt_as = as;
2107 iflp->rmt_ip.s_addr = addr.s_addr;
2108 SET_PARAM(iflp, LP_RMT_AS);
2109
2110 /* force protocols to update LINK STATE due to parameters change
2111 */
2112 if (if_is_operative(ifp))
2113 zebra_interface_parameters_update(ifp);
2114 }
2115 return CMD_SUCCESS;
2116 }
2117
2118 DEFUN (no_link_params_inter_as,
2119 no_link_params_inter_as_cmd,
2120 "no neighbor",
2121 NO_STR
2122 "Remove Neighbor IP address and AS number for Inter-AS TE\n")
2123 {
2124 VTY_DECLVAR_CONTEXT(interface, ifp);
2125 struct if_link_params *iflp = if_link_params_get(ifp);
2126
2127 /* Reset Remote IP and AS neighbor */
2128 iflp->rmt_as = 0;
2129 iflp->rmt_ip.s_addr = 0;
2130 UNSET_PARAM(iflp, LP_RMT_AS);
2131
2132 /* force protocols to update LINK STATE due to parameters change */
2133 if (if_is_operative(ifp))
2134 zebra_interface_parameters_update(ifp);
2135
2136 return CMD_SUCCESS;
2137 }
2138
2139 /* RFC7471: OSPF Traffic Engineering (TE) Metric extensions &
2140 * draft-ietf-isis-metric-extensions-07.txt */
2141 DEFUN (link_params_delay,
2142 link_params_delay_cmd,
2143 "delay (0-16777215) [min (0-16777215) max (0-16777215)]",
2144 "Unidirectional Average Link Delay\n"
2145 "Average delay in micro-second as decimal (0...16777215)\n"
2146 "Minimum delay\n"
2147 "Minimum delay in micro-second as decimal (0...16777215)\n"
2148 "Maximum delay\n"
2149 "Maximum delay in micro-second as decimal (0...16777215)\n")
2150 {
2151 /* Get and Check new delay values */
2152 uint32_t delay = 0, low = 0, high = 0;
2153 delay = strtoul(argv[1]->arg, NULL, 10);
2154 if (argc == 6) {
2155 low = strtoul(argv[3]->arg, NULL, 10);
2156 high = strtoul(argv[5]->arg, NULL, 10);
2157 }
2158
2159 VTY_DECLVAR_CONTEXT(interface, ifp);
2160 struct if_link_params *iflp = if_link_params_get(ifp);
2161 uint8_t update = 0;
2162
2163 if (argc == 2) {
2164 /* Check new delay value against old Min and Max delays if set
2165 */
2166 if (IS_PARAM_SET(iflp, LP_MM_DELAY)
2167 && (delay <= iflp->min_delay || delay >= iflp->max_delay)) {
2168 vty_out(vty,
2169 "Average delay should be comprise between Min (%d) and Max (%d) delay\n",
2170 iflp->min_delay, iflp->max_delay);
2171 return CMD_WARNING_CONFIG_FAILED;
2172 }
2173 /* Update delay if value is not set or change */
2174 if (IS_PARAM_UNSET(iflp, LP_DELAY) || iflp->av_delay != delay) {
2175 iflp->av_delay = delay;
2176 SET_PARAM(iflp, LP_DELAY);
2177 update = 1;
2178 }
2179 /* Unset Min and Max delays if already set */
2180 if (IS_PARAM_SET(iflp, LP_MM_DELAY)) {
2181 iflp->min_delay = 0;
2182 iflp->max_delay = 0;
2183 UNSET_PARAM(iflp, LP_MM_DELAY);
2184 update = 1;
2185 }
2186 } else {
2187 /* Check new delays value coherency */
2188 if (delay <= low || delay >= high) {
2189 vty_out(vty,
2190 "Average delay should be comprise between Min (%d) and Max (%d) delay\n",
2191 low, high);
2192 return CMD_WARNING_CONFIG_FAILED;
2193 }
2194 /* Update Delays if needed */
2195 if (IS_PARAM_UNSET(iflp, LP_DELAY)
2196 || IS_PARAM_UNSET(iflp, LP_MM_DELAY)
2197 || iflp->av_delay != delay || iflp->min_delay != low
2198 || iflp->max_delay != high) {
2199 iflp->av_delay = delay;
2200 SET_PARAM(iflp, LP_DELAY);
2201 iflp->min_delay = low;
2202 iflp->max_delay = high;
2203 SET_PARAM(iflp, LP_MM_DELAY);
2204 update = 1;
2205 }
2206 }
2207
2208 /* force protocols to update LINK STATE due to parameters change */
2209 if (update == 1 && if_is_operative(ifp))
2210 zebra_interface_parameters_update(ifp);
2211
2212 return CMD_SUCCESS;
2213 }
2214
2215 DEFUN (no_link_params_delay,
2216 no_link_params_delay_cmd,
2217 "no delay",
2218 NO_STR
2219 "Disable Unidirectional Average, Min & Max Link Delay on this interface\n")
2220 {
2221 VTY_DECLVAR_CONTEXT(interface, ifp);
2222 struct if_link_params *iflp = if_link_params_get(ifp);
2223
2224 /* Unset Delays */
2225 iflp->av_delay = 0;
2226 UNSET_PARAM(iflp, LP_DELAY);
2227 iflp->min_delay = 0;
2228 iflp->max_delay = 0;
2229 UNSET_PARAM(iflp, LP_MM_DELAY);
2230
2231 /* force protocols to update LINK STATE due to parameters change */
2232 if (if_is_operative(ifp))
2233 zebra_interface_parameters_update(ifp);
2234
2235 return CMD_SUCCESS;
2236 }
2237
2238 DEFUN (link_params_delay_var,
2239 link_params_delay_var_cmd,
2240 "delay-variation (0-16777215)",
2241 "Unidirectional Link Delay Variation\n"
2242 "delay variation in micro-second as decimal (0...16777215)\n")
2243 {
2244 int idx_number = 1;
2245 VTY_DECLVAR_CONTEXT(interface, ifp);
2246 struct if_link_params *iflp = if_link_params_get(ifp);
2247 uint32_t value;
2248
2249 value = strtoul(argv[idx_number]->arg, NULL, 10);
2250
2251 /* Update Delay Variation if needed */
2252 link_param_cmd_set_uint32(ifp, &iflp->delay_var, LP_DELAY_VAR, value);
2253
2254 return CMD_SUCCESS;
2255 }
2256
2257 DEFUN (no_link_params_delay_var,
2258 no_link_params_delay_var_cmd,
2259 "no delay-variation",
2260 NO_STR
2261 "Disable Unidirectional Delay Variation on this interface\n")
2262 {
2263 VTY_DECLVAR_CONTEXT(interface, ifp);
2264
2265 /* Unset Delay Variation */
2266 link_param_cmd_unset(ifp, LP_DELAY_VAR);
2267
2268 return CMD_SUCCESS;
2269 }
2270
2271 DEFUN (link_params_pkt_loss,
2272 link_params_pkt_loss_cmd,
2273 "packet-loss PERCENTAGE",
2274 "Unidirectional Link Packet Loss\n"
2275 "percentage of total traffic by 0.000003% step and less than 50.331642%\n")
2276 {
2277 int idx_percentage = 1;
2278 VTY_DECLVAR_CONTEXT(interface, ifp);
2279 struct if_link_params *iflp = if_link_params_get(ifp);
2280 float fval;
2281
2282 if (sscanf(argv[idx_percentage]->arg, "%g", &fval) != 1) {
2283 vty_out(vty, "link_params_pkt_loss: fscanf: %s\n",
2284 safe_strerror(errno));
2285 return CMD_WARNING_CONFIG_FAILED;
2286 }
2287
2288 if (fval > MAX_PKT_LOSS)
2289 fval = MAX_PKT_LOSS;
2290
2291 /* Update Packet Loss if needed */
2292 link_param_cmd_set_float(ifp, &iflp->pkt_loss, LP_PKT_LOSS, fval);
2293
2294 return CMD_SUCCESS;
2295 }
2296
2297 DEFUN (no_link_params_pkt_loss,
2298 no_link_params_pkt_loss_cmd,
2299 "no packet-loss",
2300 NO_STR
2301 "Disable Unidirectional Link Packet Loss on this interface\n")
2302 {
2303 VTY_DECLVAR_CONTEXT(interface, ifp);
2304
2305 /* Unset Packet Loss */
2306 link_param_cmd_unset(ifp, LP_PKT_LOSS);
2307
2308 return CMD_SUCCESS;
2309 }
2310
2311 DEFUN (link_params_res_bw,
2312 link_params_res_bw_cmd,
2313 "res-bw BANDWIDTH",
2314 "Unidirectional Residual Bandwidth\n"
2315 "Bytes/second (IEEE floating point format)\n")
2316 {
2317 int idx_bandwidth = 1;
2318 VTY_DECLVAR_CONTEXT(interface, ifp);
2319 struct if_link_params *iflp = if_link_params_get(ifp);
2320 float bw;
2321
2322 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
2323 vty_out(vty, "link_params_res_bw: fscanf: %s\n",
2324 safe_strerror(errno));
2325 return CMD_WARNING_CONFIG_FAILED;
2326 }
2327
2328 /* Check that bandwidth is not greater than maximum bandwidth parameter
2329 */
2330 if (bw > iflp->max_bw) {
2331 vty_out(vty,
2332 "Residual Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
2333 iflp->max_bw);
2334 return CMD_WARNING_CONFIG_FAILED;
2335 }
2336
2337 /* Update Residual Bandwidth if needed */
2338 link_param_cmd_set_float(ifp, &iflp->res_bw, LP_RES_BW, bw);
2339
2340 return CMD_SUCCESS;
2341 }
2342
2343 DEFUN (no_link_params_res_bw,
2344 no_link_params_res_bw_cmd,
2345 "no res-bw",
2346 NO_STR
2347 "Disable Unidirectional Residual Bandwidth on this interface\n")
2348 {
2349 VTY_DECLVAR_CONTEXT(interface, ifp);
2350
2351 /* Unset Residual Bandwidth */
2352 link_param_cmd_unset(ifp, LP_RES_BW);
2353
2354 return CMD_SUCCESS;
2355 }
2356
2357 DEFUN (link_params_ava_bw,
2358 link_params_ava_bw_cmd,
2359 "ava-bw BANDWIDTH",
2360 "Unidirectional Available Bandwidth\n"
2361 "Bytes/second (IEEE floating point format)\n")
2362 {
2363 int idx_bandwidth = 1;
2364 VTY_DECLVAR_CONTEXT(interface, ifp);
2365 struct if_link_params *iflp = if_link_params_get(ifp);
2366 float bw;
2367
2368 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
2369 vty_out(vty, "link_params_ava_bw: fscanf: %s\n",
2370 safe_strerror(errno));
2371 return CMD_WARNING_CONFIG_FAILED;
2372 }
2373
2374 /* Check that bandwidth is not greater than maximum bandwidth parameter
2375 */
2376 if (bw > iflp->max_bw) {
2377 vty_out(vty,
2378 "Available Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
2379 iflp->max_bw);
2380 return CMD_WARNING_CONFIG_FAILED;
2381 }
2382
2383 /* Update Residual Bandwidth if needed */
2384 link_param_cmd_set_float(ifp, &iflp->ava_bw, LP_AVA_BW, bw);
2385
2386 return CMD_SUCCESS;
2387 }
2388
2389 DEFUN (no_link_params_ava_bw,
2390 no_link_params_ava_bw_cmd,
2391 "no ava-bw",
2392 NO_STR
2393 "Disable Unidirectional Available Bandwidth on this interface\n")
2394 {
2395 VTY_DECLVAR_CONTEXT(interface, ifp);
2396
2397 /* Unset Available Bandwidth */
2398 link_param_cmd_unset(ifp, LP_AVA_BW);
2399
2400 return CMD_SUCCESS;
2401 }
2402
2403 DEFUN (link_params_use_bw,
2404 link_params_use_bw_cmd,
2405 "use-bw BANDWIDTH",
2406 "Unidirectional Utilised Bandwidth\n"
2407 "Bytes/second (IEEE floating point format)\n")
2408 {
2409 int idx_bandwidth = 1;
2410 VTY_DECLVAR_CONTEXT(interface, ifp);
2411 struct if_link_params *iflp = if_link_params_get(ifp);
2412 float bw;
2413
2414 if (sscanf(argv[idx_bandwidth]->arg, "%g", &bw) != 1) {
2415 vty_out(vty, "link_params_use_bw: fscanf: %s\n",
2416 safe_strerror(errno));
2417 return CMD_WARNING_CONFIG_FAILED;
2418 }
2419
2420 /* Check that bandwidth is not greater than maximum bandwidth parameter
2421 */
2422 if (bw > iflp->max_bw) {
2423 vty_out(vty,
2424 "Utilised Bandwidth could not be greater than Maximum Bandwidth (%g)\n",
2425 iflp->max_bw);
2426 return CMD_WARNING_CONFIG_FAILED;
2427 }
2428
2429 /* Update Utilized Bandwidth if needed */
2430 link_param_cmd_set_float(ifp, &iflp->use_bw, LP_USE_BW, bw);
2431
2432 return CMD_SUCCESS;
2433 }
2434
2435 DEFUN (no_link_params_use_bw,
2436 no_link_params_use_bw_cmd,
2437 "no use-bw",
2438 NO_STR
2439 "Disable Unidirectional Utilised Bandwidth on this interface\n")
2440 {
2441 VTY_DECLVAR_CONTEXT(interface, ifp);
2442
2443 /* Unset Utilised Bandwidth */
2444 link_param_cmd_unset(ifp, LP_USE_BW);
2445
2446 return CMD_SUCCESS;
2447 }
2448
2449 static int ip_address_install(struct vty *vty, struct interface *ifp,
2450 const char *addr_str, const char *peer_str,
2451 const char *label)
2452 {
2453 struct zebra_if *if_data;
2454 struct prefix_ipv4 lp, pp;
2455 struct connected *ifc;
2456 struct prefix_ipv4 *p;
2457 int ret;
2458
2459 if_data = ifp->info;
2460
2461 ret = str2prefix_ipv4(addr_str, &lp);
2462 if (ret <= 0) {
2463 vty_out(vty, "%% Malformed address \n");
2464 return CMD_WARNING_CONFIG_FAILED;
2465 }
2466
2467 if (ipv4_martian(&lp.prefix)) {
2468 vty_out(vty, "%% Invalid address\n");
2469 return CMD_WARNING_CONFIG_FAILED;
2470 }
2471
2472 if (peer_str) {
2473 if (lp.prefixlen != 32) {
2474 vty_out(vty,
2475 "%% Local prefix length for P-t-P address must be /32\n");
2476 return CMD_WARNING_CONFIG_FAILED;
2477 }
2478
2479 ret = str2prefix_ipv4(peer_str, &pp);
2480 if (ret <= 0) {
2481 vty_out(vty, "%% Malformed peer address\n");
2482 return CMD_WARNING_CONFIG_FAILED;
2483 }
2484 }
2485
2486 ifc = connected_check_ptp(ifp, &lp, peer_str ? &pp : NULL);
2487 if (!ifc) {
2488 ifc = connected_new();
2489 ifc->ifp = ifp;
2490
2491 /* Address. */
2492 p = prefix_ipv4_new();
2493 *p = lp;
2494 ifc->address = (struct prefix *)p;
2495
2496 if (peer_str) {
2497 SET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
2498 p = prefix_ipv4_new();
2499 *p = pp;
2500 ifc->destination = (struct prefix *)p;
2501 } else if (p->prefixlen <= IPV4_MAX_PREFIXLEN - 2) {
2502 p = prefix_ipv4_new();
2503 *p = lp;
2504 p->prefix.s_addr = ipv4_broadcast_addr(p->prefix.s_addr,
2505 p->prefixlen);
2506 ifc->destination = (struct prefix *)p;
2507 }
2508
2509 /* Label. */
2510 if (label)
2511 ifc->label = XSTRDUP(MTYPE_CONNECTED_LABEL, label);
2512
2513 /* Add to linked list. */
2514 listnode_add(ifp->connected, ifc);
2515 }
2516
2517 /* This address is configured from zebra. */
2518 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
2519 SET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);
2520
2521 /* In case of this route need to install kernel. */
2522 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)
2523 && CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)
2524 && !(if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)) {
2525 /* Some system need to up the interface to set IP address. */
2526 if (!if_is_up(ifp)) {
2527 if_set_flags(ifp, IFF_UP | IFF_RUNNING);
2528 if_refresh(ifp);
2529 }
2530
2531 ret = if_set_prefix(ifp, ifc);
2532 if (ret < 0) {
2533 vty_out(vty, "%% Can't set interface IP address: %s.\n",
2534 safe_strerror(errno));
2535 return CMD_WARNING_CONFIG_FAILED;
2536 }
2537
2538 SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
2539 /* The address will be advertised to zebra clients when the
2540 * notification
2541 * from the kernel has been received.
2542 * It will also be added to the subnet chain list, then. */
2543 }
2544
2545 return CMD_SUCCESS;
2546 }
2547
2548 static int ip_address_uninstall(struct vty *vty, struct interface *ifp,
2549 const char *addr_str, const char *peer_str,
2550 const char *label)
2551 {
2552 struct prefix_ipv4 lp, pp;
2553 struct connected *ifc;
2554 int ret;
2555
2556 /* Convert to prefix structure. */
2557 ret = str2prefix_ipv4(addr_str, &lp);
2558 if (ret <= 0) {
2559 vty_out(vty, "%% Malformed address \n");
2560 return CMD_WARNING_CONFIG_FAILED;
2561 }
2562
2563 if (peer_str) {
2564 if (lp.prefixlen != 32) {
2565 vty_out(vty,
2566 "%% Local prefix length for P-t-P address must be /32\n");
2567 return CMD_WARNING_CONFIG_FAILED;
2568 }
2569
2570 ret = str2prefix_ipv4(peer_str, &pp);
2571 if (ret <= 0) {
2572 vty_out(vty, "%% Malformed peer address\n");
2573 return CMD_WARNING_CONFIG_FAILED;
2574 }
2575 }
2576
2577 /* Check current interface address. */
2578 ifc = connected_check_ptp(ifp, &lp, peer_str ? &pp : NULL);
2579 if (!ifc) {
2580 vty_out(vty, "%% Can't find address\n");
2581 return CMD_WARNING_CONFIG_FAILED;
2582 }
2583
2584 /* This is not configured address. */
2585 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
2586 return CMD_WARNING_CONFIG_FAILED;
2587
2588 UNSET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);
2589
2590 /* This is not real address or interface is not active. */
2591 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)
2592 || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
2593 listnode_delete(ifp->connected, ifc);
2594 connected_free(ifc);
2595 return CMD_WARNING_CONFIG_FAILED;
2596 }
2597
2598 /* This is real route. */
2599 ret = if_unset_prefix(ifp, ifc);
2600 if (ret < 0) {
2601 vty_out(vty, "%% Can't unset interface IP address: %s.\n",
2602 safe_strerror(errno));
2603 return CMD_WARNING_CONFIG_FAILED;
2604 }
2605 UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
2606 /* we will receive a kernel notification about this route being removed.
2607 * this will trigger its removal from the connected list. */
2608 return CMD_SUCCESS;
2609 }
2610
2611 DEFUN (ip_address,
2612 ip_address_cmd,
2613 "ip address A.B.C.D/M",
2614 "Interface Internet Protocol config commands\n"
2615 "Set the IP address of an interface\n"
2616 "IP address (e.g. 10.0.0.1/8)\n")
2617 {
2618 int idx_ipv4_prefixlen = 2;
2619 VTY_DECLVAR_CONTEXT(interface, ifp);
2620 return ip_address_install(vty, ifp, argv[idx_ipv4_prefixlen]->arg, NULL,
2621 NULL);
2622 }
2623
2624 DEFUN (no_ip_address,
2625 no_ip_address_cmd,
2626 "no ip address A.B.C.D/M",
2627 NO_STR
2628 "Interface Internet Protocol config commands\n"
2629 "Set the IP address of an interface\n"
2630 "IP Address (e.g. 10.0.0.1/8)\n")
2631 {
2632 int idx_ipv4_prefixlen = 3;
2633 VTY_DECLVAR_CONTEXT(interface, ifp);
2634 return ip_address_uninstall(vty, ifp, argv[idx_ipv4_prefixlen]->arg,
2635 NULL, NULL);
2636 }
2637
2638 DEFUN(ip_address_peer,
2639 ip_address_peer_cmd,
2640 "ip address A.B.C.D peer A.B.C.D/M",
2641 "Interface Internet Protocol config commands\n"
2642 "Set the IP address of an interface\n"
2643 "Local IP (e.g. 10.0.0.1) for P-t-P address\n"
2644 "Specify P-t-P address\n"
2645 "Peer IP address (e.g. 10.0.0.1/8)\n")
2646 {
2647 VTY_DECLVAR_CONTEXT(interface, ifp);
2648 return ip_address_install(vty, ifp, argv[2]->arg, argv[4]->arg, NULL);
2649 }
2650
2651 DEFUN(no_ip_address_peer,
2652 no_ip_address_peer_cmd,
2653 "no ip address A.B.C.D peer A.B.C.D/M",
2654 NO_STR
2655 "Interface Internet Protocol config commands\n"
2656 "Set the IP address of an interface\n"
2657 "Local IP (e.g. 10.0.0.1) for P-t-P address\n"
2658 "Specify P-t-P address\n"
2659 "Peer IP address (e.g. 10.0.0.1/8)\n")
2660 {
2661 VTY_DECLVAR_CONTEXT(interface, ifp);
2662 return ip_address_uninstall(vty, ifp, argv[3]->arg, argv[5]->arg, NULL);
2663 }
2664
2665 #ifdef HAVE_NETLINK
2666 DEFUN (ip_address_label,
2667 ip_address_label_cmd,
2668 "ip address A.B.C.D/M label LINE",
2669 "Interface Internet Protocol config commands\n"
2670 "Set the IP address of an interface\n"
2671 "IP address (e.g. 10.0.0.1/8)\n"
2672 "Label of this address\n"
2673 "Label\n")
2674 {
2675 int idx_ipv4_prefixlen = 2;
2676 int idx_line = 4;
2677 VTY_DECLVAR_CONTEXT(interface, ifp);
2678 return ip_address_install(vty, ifp, argv[idx_ipv4_prefixlen]->arg, NULL,
2679 argv[idx_line]->arg);
2680 }
2681
2682 DEFUN (no_ip_address_label,
2683 no_ip_address_label_cmd,
2684 "no ip address A.B.C.D/M label LINE",
2685 NO_STR
2686 "Interface Internet Protocol config commands\n"
2687 "Set the IP address of an interface\n"
2688 "IP address (e.g. 10.0.0.1/8)\n"
2689 "Label of this address\n"
2690 "Label\n")
2691 {
2692 int idx_ipv4_prefixlen = 3;
2693 int idx_line = 5;
2694 VTY_DECLVAR_CONTEXT(interface, ifp);
2695 return ip_address_uninstall(vty, ifp, argv[idx_ipv4_prefixlen]->arg,
2696 NULL, argv[idx_line]->arg);
2697 }
2698 #endif /* HAVE_NETLINK */
2699
2700 static int ipv6_address_install(struct vty *vty, struct interface *ifp,
2701 const char *addr_str, const char *peer_str,
2702 const char *label, int secondary)
2703 {
2704 struct zebra_if *if_data;
2705 struct prefix_ipv6 cp;
2706 struct connected *ifc;
2707 struct prefix_ipv6 *p;
2708 int ret;
2709
2710 if_data = ifp->info;
2711
2712 ret = str2prefix_ipv6(addr_str, &cp);
2713 if (ret <= 0) {
2714 vty_out(vty, "%% Malformed address \n");
2715 return CMD_WARNING_CONFIG_FAILED;
2716 }
2717
2718 if (ipv6_martian(&cp.prefix)) {
2719 vty_out(vty, "%% Invalid address\n");
2720 return CMD_WARNING_CONFIG_FAILED;
2721 }
2722
2723 ifc = connected_check(ifp, (struct prefix *)&cp);
2724 if (!ifc) {
2725 ifc = connected_new();
2726 ifc->ifp = ifp;
2727
2728 /* Address. */
2729 p = prefix_ipv6_new();
2730 *p = cp;
2731 ifc->address = (struct prefix *)p;
2732
2733 /* Secondary. */
2734 if (secondary)
2735 SET_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY);
2736
2737 /* Label. */
2738 if (label)
2739 ifc->label = XSTRDUP(MTYPE_CONNECTED_LABEL, label);
2740
2741 /* Add to linked list. */
2742 listnode_add(ifp->connected, ifc);
2743 }
2744
2745 /* This address is configured from zebra. */
2746 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
2747 SET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);
2748
2749 /* In case of this route need to install kernel. */
2750 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)
2751 && CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)
2752 && !(if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)) {
2753 /* Some system need to up the interface to set IP address. */
2754 if (!if_is_up(ifp)) {
2755 if_set_flags(ifp, IFF_UP | IFF_RUNNING);
2756 if_refresh(ifp);
2757 }
2758
2759 ret = if_prefix_add_ipv6(ifp, ifc);
2760
2761 if (ret < 0) {
2762 vty_out(vty, "%% Can't set interface IP address: %s.\n",
2763 safe_strerror(errno));
2764 return CMD_WARNING_CONFIG_FAILED;
2765 }
2766
2767 SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
2768 /* The address will be advertised to zebra clients when the
2769 * notification
2770 * from the kernel has been received. */
2771 }
2772
2773 return CMD_SUCCESS;
2774 }
2775
2776 /* Return true if an ipv6 address is configured on ifp */
2777 int ipv6_address_configured(struct interface *ifp)
2778 {
2779 struct connected *connected;
2780 struct listnode *node;
2781
2782 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, connected))
2783 if (CHECK_FLAG(connected->conf, ZEBRA_IFC_REAL)
2784 && (connected->address->family == AF_INET6))
2785 return 1;
2786
2787 return 0;
2788 }
2789
2790 static int ipv6_address_uninstall(struct vty *vty, struct interface *ifp,
2791 const char *addr_str, const char *peer_str,
2792 const char *label, int secondry)
2793 {
2794 struct prefix_ipv6 cp;
2795 struct connected *ifc;
2796 int ret;
2797
2798 /* Convert to prefix structure. */
2799 ret = str2prefix_ipv6(addr_str, &cp);
2800 if (ret <= 0) {
2801 vty_out(vty, "%% Malformed address \n");
2802 return CMD_WARNING_CONFIG_FAILED;
2803 }
2804
2805 /* Check current interface address. */
2806 ifc = connected_check(ifp, (struct prefix *)&cp);
2807 if (!ifc) {
2808 vty_out(vty, "%% Can't find address\n");
2809 return CMD_WARNING_CONFIG_FAILED;
2810 }
2811
2812 /* This is not configured address. */
2813 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
2814 return CMD_WARNING_CONFIG_FAILED;
2815
2816 UNSET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);
2817
2818 /* This is not real address or interface is not active. */
2819 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)
2820 || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
2821 listnode_delete(ifp->connected, ifc);
2822 connected_free(ifc);
2823 return CMD_WARNING_CONFIG_FAILED;
2824 }
2825
2826 /* This is real route. */
2827 ret = if_prefix_delete_ipv6(ifp, ifc);
2828 if (ret < 0) {
2829 vty_out(vty, "%% Can't unset interface IP address: %s.\n",
2830 safe_strerror(errno));
2831 return CMD_WARNING_CONFIG_FAILED;
2832 }
2833
2834 UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
2835 /* This information will be propagated to the zclients when the
2836 * kernel notification is received. */
2837 return CMD_SUCCESS;
2838 }
2839
2840 DEFUN (ipv6_address,
2841 ipv6_address_cmd,
2842 "ipv6 address X:X::X:X/M",
2843 "Interface IPv6 config commands\n"
2844 "Set the IP address of an interface\n"
2845 "IPv6 address (e.g. 3ffe:506::1/48)\n")
2846 {
2847 int idx_ipv6_prefixlen = 2;
2848 VTY_DECLVAR_CONTEXT(interface, ifp);
2849 return ipv6_address_install(vty, ifp, argv[idx_ipv6_prefixlen]->arg,
2850 NULL, NULL, 0);
2851 }
2852
2853 DEFUN (no_ipv6_address,
2854 no_ipv6_address_cmd,
2855 "no ipv6 address X:X::X:X/M",
2856 NO_STR
2857 "Interface IPv6 config commands\n"
2858 "Set the IP address of an interface\n"
2859 "IPv6 address (e.g. 3ffe:506::1/48)\n")
2860 {
2861 int idx_ipv6_prefixlen = 3;
2862 VTY_DECLVAR_CONTEXT(interface, ifp);
2863 return ipv6_address_uninstall(vty, ifp, argv[idx_ipv6_prefixlen]->arg,
2864 NULL, NULL, 0);
2865 }
2866
2867 static int link_params_config_write(struct vty *vty, struct interface *ifp)
2868 {
2869 int i;
2870
2871 if ((ifp == NULL) || !HAS_LINK_PARAMS(ifp))
2872 return -1;
2873
2874 struct if_link_params *iflp = ifp->link_params;
2875
2876 vty_out(vty, " link-params\n");
2877 vty_out(vty, " enable\n");
2878 if (IS_PARAM_SET(iflp, LP_TE_METRIC) && iflp->te_metric != ifp->metric)
2879 vty_out(vty, " metric %u\n", iflp->te_metric);
2880 if (IS_PARAM_SET(iflp, LP_MAX_BW) && iflp->max_bw != iflp->default_bw)
2881 vty_out(vty, " max-bw %g\n", iflp->max_bw);
2882 if (IS_PARAM_SET(iflp, LP_MAX_RSV_BW)
2883 && iflp->max_rsv_bw != iflp->default_bw)
2884 vty_out(vty, " max-rsv-bw %g\n", iflp->max_rsv_bw);
2885 if (IS_PARAM_SET(iflp, LP_UNRSV_BW)) {
2886 for (i = 0; i < 8; i++)
2887 if (iflp->unrsv_bw[i] != iflp->default_bw)
2888 vty_out(vty, " unrsv-bw %d %g\n", i,
2889 iflp->unrsv_bw[i]);
2890 }
2891 if (IS_PARAM_SET(iflp, LP_ADM_GRP))
2892 vty_out(vty, " admin-grp 0x%x\n", iflp->admin_grp);
2893 if (IS_PARAM_SET(iflp, LP_DELAY)) {
2894 vty_out(vty, " delay %u", iflp->av_delay);
2895 if (IS_PARAM_SET(iflp, LP_MM_DELAY)) {
2896 vty_out(vty, " min %u", iflp->min_delay);
2897 vty_out(vty, " max %u", iflp->max_delay);
2898 }
2899 vty_out(vty, "\n");
2900 }
2901 if (IS_PARAM_SET(iflp, LP_DELAY_VAR))
2902 vty_out(vty, " delay-variation %u\n", iflp->delay_var);
2903 if (IS_PARAM_SET(iflp, LP_PKT_LOSS))
2904 vty_out(vty, " packet-loss %g\n", iflp->pkt_loss);
2905 if (IS_PARAM_SET(iflp, LP_AVA_BW))
2906 vty_out(vty, " ava-bw %g\n", iflp->ava_bw);
2907 if (IS_PARAM_SET(iflp, LP_RES_BW))
2908 vty_out(vty, " res-bw %g\n", iflp->res_bw);
2909 if (IS_PARAM_SET(iflp, LP_USE_BW))
2910 vty_out(vty, " use-bw %g\n", iflp->use_bw);
2911 if (IS_PARAM_SET(iflp, LP_RMT_AS))
2912 vty_out(vty, " neighbor %s as %u\n", inet_ntoa(iflp->rmt_ip),
2913 iflp->rmt_as);
2914 vty_out(vty, " exit-link-params\n");
2915 return 0;
2916 }
2917
2918 static int if_config_write(struct vty *vty)
2919 {
2920 struct vrf *vrf;
2921 struct interface *ifp;
2922
2923 zebra_ptm_write(vty);
2924
2925 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
2926 FOR_ALL_INTERFACES (vrf, ifp) {
2927 struct zebra_if *if_data;
2928 struct listnode *addrnode;
2929 struct connected *ifc;
2930 struct prefix *p;
2931 struct vrf *vrf;
2932
2933 if_data = ifp->info;
2934 vrf = vrf_lookup_by_id(ifp->vrf_id);
2935
2936 if (ifp->vrf_id == VRF_DEFAULT)
2937 vty_frame(vty, "interface %s\n", ifp->name);
2938 else
2939 vty_frame(vty, "interface %s vrf %s\n",
2940 ifp->name, vrf->name);
2941
2942 if (if_data) {
2943 if (if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)
2944 vty_out(vty, " shutdown\n");
2945
2946 zebra_ptm_if_write(vty, if_data);
2947 }
2948
2949 if (ifp->desc)
2950 vty_out(vty, " description %s\n", ifp->desc);
2951
2952 /* Assign bandwidth here to avoid unnecessary interface
2953 flap
2954 while processing config script */
2955 if (ifp->bandwidth != 0)
2956 vty_out(vty, " bandwidth %u\n", ifp->bandwidth);
2957
2958 if (!CHECK_FLAG(ifp->status,
2959 ZEBRA_INTERFACE_LINKDETECTION))
2960 vty_out(vty, " no link-detect\n");
2961
2962 for (ALL_LIST_ELEMENTS_RO(ifp->connected, addrnode,
2963 ifc)) {
2964 if (CHECK_FLAG(ifc->conf,
2965 ZEBRA_IFC_CONFIGURED)) {
2966 char buf[INET6_ADDRSTRLEN];
2967 p = ifc->address;
2968 vty_out(vty, " ip%s address %s",
2969 p->family == AF_INET ? ""
2970 : "v6",
2971 inet_ntop(p->family,
2972 &p->u.prefix, buf,
2973 sizeof(buf)));
2974 if (CONNECTED_PEER(ifc)) {
2975 p = ifc->destination;
2976 vty_out(vty, " peer %s",
2977 inet_ntop(p->family,
2978 &p->u.prefix,
2979 buf,
2980 sizeof(buf)));
2981 }
2982 vty_out(vty, "/%d", p->prefixlen);
2983
2984 if (ifc->label)
2985 vty_out(vty, " label %s",
2986 ifc->label);
2987
2988 vty_out(vty, "\n");
2989 }
2990 }
2991
2992 if (if_data) {
2993 if (if_data->multicast
2994 != IF_ZEBRA_MULTICAST_UNSPEC)
2995 vty_out(vty, " %smulticast\n",
2996 if_data->multicast
2997 == IF_ZEBRA_MULTICAST_ON
2998 ? ""
2999 : "no ");
3000 }
3001
3002 hook_call(zebra_if_config_wr, vty, ifp);
3003
3004 link_params_config_write(vty, ifp);
3005
3006 vty_endframe(vty, "!\n");
3007 }
3008 return 0;
3009 }
3010
3011 /* Allocate and initialize interface vector. */
3012 void zebra_if_init(void)
3013 {
3014 /* Initialize interface and new hook. */
3015 hook_register_prio(if_add, 0, if_zebra_new_hook);
3016 hook_register_prio(if_del, 0, if_zebra_delete_hook);
3017
3018 /* Install configuration write function. */
3019 install_node(&interface_node, if_config_write);
3020 install_node(&link_params_node, NULL);
3021 if_cmd_init();
3022
3023 install_element(VIEW_NODE, &show_interface_cmd);
3024 install_element(VIEW_NODE, &show_interface_vrf_all_cmd);
3025 install_element(VIEW_NODE, &show_interface_name_vrf_cmd);
3026 install_element(VIEW_NODE, &show_interface_name_vrf_all_cmd);
3027
3028 install_element(ENABLE_NODE, &show_interface_desc_cmd);
3029 install_element(ENABLE_NODE, &show_interface_desc_vrf_all_cmd);
3030 install_element(INTERFACE_NODE, &multicast_cmd);
3031 install_element(INTERFACE_NODE, &no_multicast_cmd);
3032 install_element(INTERFACE_NODE, &linkdetect_cmd);
3033 install_element(INTERFACE_NODE, &no_linkdetect_cmd);
3034 install_element(INTERFACE_NODE, &shutdown_if_cmd);
3035 install_element(INTERFACE_NODE, &no_shutdown_if_cmd);
3036 install_element(INTERFACE_NODE, &bandwidth_if_cmd);
3037 install_element(INTERFACE_NODE, &no_bandwidth_if_cmd);
3038 install_element(INTERFACE_NODE, &ip_address_cmd);
3039 install_element(INTERFACE_NODE, &no_ip_address_cmd);
3040 install_element(INTERFACE_NODE, &ip_address_peer_cmd);
3041 install_element(INTERFACE_NODE, &no_ip_address_peer_cmd);
3042 install_element(INTERFACE_NODE, &ipv6_address_cmd);
3043 install_element(INTERFACE_NODE, &no_ipv6_address_cmd);
3044 #ifdef HAVE_NETLINK
3045 install_element(INTERFACE_NODE, &ip_address_label_cmd);
3046 install_element(INTERFACE_NODE, &no_ip_address_label_cmd);
3047 #endif /* HAVE_NETLINK */
3048 install_element(INTERFACE_NODE, &link_params_cmd);
3049 install_default(LINK_PARAMS_NODE);
3050 install_element(LINK_PARAMS_NODE, &link_params_enable_cmd);
3051 install_element(LINK_PARAMS_NODE, &no_link_params_enable_cmd);
3052 install_element(LINK_PARAMS_NODE, &link_params_metric_cmd);
3053 install_element(LINK_PARAMS_NODE, &no_link_params_metric_cmd);
3054 install_element(LINK_PARAMS_NODE, &link_params_maxbw_cmd);
3055 install_element(LINK_PARAMS_NODE, &link_params_max_rsv_bw_cmd);
3056 install_element(LINK_PARAMS_NODE, &link_params_unrsv_bw_cmd);
3057 install_element(LINK_PARAMS_NODE, &link_params_admin_grp_cmd);
3058 install_element(LINK_PARAMS_NODE, &no_link_params_admin_grp_cmd);
3059 install_element(LINK_PARAMS_NODE, &link_params_inter_as_cmd);
3060 install_element(LINK_PARAMS_NODE, &no_link_params_inter_as_cmd);
3061 install_element(LINK_PARAMS_NODE, &link_params_delay_cmd);
3062 install_element(LINK_PARAMS_NODE, &no_link_params_delay_cmd);
3063 install_element(LINK_PARAMS_NODE, &link_params_delay_var_cmd);
3064 install_element(LINK_PARAMS_NODE, &no_link_params_delay_var_cmd);
3065 install_element(LINK_PARAMS_NODE, &link_params_pkt_loss_cmd);
3066 install_element(LINK_PARAMS_NODE, &no_link_params_pkt_loss_cmd);
3067 install_element(LINK_PARAMS_NODE, &link_params_ava_bw_cmd);
3068 install_element(LINK_PARAMS_NODE, &no_link_params_ava_bw_cmd);
3069 install_element(LINK_PARAMS_NODE, &link_params_res_bw_cmd);
3070 install_element(LINK_PARAMS_NODE, &no_link_params_res_bw_cmd);
3071 install_element(LINK_PARAMS_NODE, &link_params_use_bw_cmd);
3072 install_element(LINK_PARAMS_NODE, &no_link_params_use_bw_cmd);
3073 install_element(LINK_PARAMS_NODE, &exit_link_params_cmd);
3074 }