]> git.proxmox.com Git - mirror_frr.git/blob - lib/if.c
ecb74631685f826266d35f01338c77c2d4e1392f
[mirror_frr.git] / lib / if.c
1
2 /*
3 * Interface functions.
4 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 *
6 * This file is part of GNU Zebra.
7 *
8 * GNU Zebra is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 2, or (at your
11 * option) any later version.
12 *
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with GNU Zebra; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
23
24 #include <zebra.h>
25
26 #include "linklist.h"
27 #include "vector.h"
28 #include "vty.h"
29 #include "command.h"
30 #include "vrf.h"
31 #include "if.h"
32 #include "sockunion.h"
33 #include "prefix.h"
34 #include "memory.h"
35 #include "table.h"
36 #include "buffer.h"
37 #include "log.h"
38
39 DEFINE_MTYPE( LIB, IF, "Interface")
40 DEFINE_MTYPE_STATIC(LIB, CONNECTED, "Connected")
41 DEFINE_MTYPE_STATIC(LIB, NBR_CONNECTED, "Neighbor Connected")
42 DEFINE_MTYPE( LIB, CONNECTED_LABEL, "Connected interface label")
43 DEFINE_MTYPE_STATIC(LIB, IF_LINK_PARAMS, "Informational Link Parameters")
44
45 DEFINE_QOBJ_TYPE(interface)
46
47 /* List of interfaces in only the default VRF */
48 int ptm_enable = 0;
49
50 /* One for each program. This structure is needed to store hooks. */
51 struct if_master
52 {
53 int (*if_new_hook) (struct interface *);
54 int (*if_delete_hook) (struct interface *);
55 } if_master = {0,};
56
57 /* Compare interface names, returning an integer greater than, equal to, or
58 * less than 0, (following the strcmp convention), according to the
59 * relationship between ifp1 and ifp2. Interface names consist of an
60 * alphabetic prefix and a numeric suffix. The primary sort key is
61 * lexicographic by name, and then numeric by number. No number sorts
62 * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
63 * devpty0, de0 < del0
64 */
65 int
66 if_cmp_name_func (char *p1, char *p2)
67 {
68 unsigned int l1, l2;
69 long int x1, x2;
70 int res;
71
72 while (*p1 && *p2) {
73 /* look up to any number */
74 l1 = strcspn(p1, "0123456789");
75 l2 = strcspn(p2, "0123456789");
76
77 /* name lengths are different -> compare names */
78 if (l1 != l2)
79 return (strcmp(p1, p2));
80
81 /* Note that this relies on all numbers being less than all letters, so
82 * that de0 < del0.
83 */
84 res = strncmp(p1, p2, l1);
85
86 /* names are different -> compare them */
87 if (res)
88 return res;
89
90 /* with identical name part, go to numeric part */
91 p1 += l1;
92 p2 += l1;
93
94 if (!*p1)
95 return -1;
96 if (!*p2)
97 return 1;
98
99 x1 = strtol(p1, &p1, 10);
100 x2 = strtol(p2, &p2, 10);
101
102 /* let's compare numbers now */
103 if (x1 < x2)
104 return -1;
105 if (x1 > x2)
106 return 1;
107
108 /* numbers were equal, lets do it again..
109 (it happens with name like "eth123.456:789") */
110 }
111 if (*p1)
112 return 1;
113 if (*p2)
114 return -1;
115 return 0;
116 }
117
118 static int
119 if_cmp_func (struct interface *ifp1, struct interface *ifp2)
120 {
121 return if_cmp_name_func (ifp1->name, ifp2->name);
122 }
123
124 /* Create new interface structure. */
125 struct interface *
126 if_create (const char *name, int namelen, vrf_id_t vrf_id)
127 {
128 struct interface *ifp;
129 struct list *intf_list = vrf_iflist_get (vrf_id);
130
131 ifp = XCALLOC (MTYPE_IF, sizeof (struct interface));
132 ifp->ifindex = IFINDEX_INTERNAL;
133
134 assert (name);
135 assert (namelen <= INTERFACE_NAMSIZ); /* Need space for '\0' at end. */
136 strncpy (ifp->name, name, namelen);
137 ifp->name[namelen] = '\0';
138 ifp->vrf_id = vrf_id;
139 if (if_lookup_by_name (ifp->name, vrf_id) == NULL)
140 listnode_add_sort (intf_list, ifp);
141 else
142 zlog_err("if_create(%s): corruption detected -- interface with this "
143 "name exists already in VRF %u!", ifp->name, vrf_id);
144 ifp->connected = list_new ();
145 ifp->connected->del = (void (*) (void *)) connected_free;
146
147 ifp->nbr_connected = list_new ();
148 ifp->nbr_connected->del = (void (*) (void *)) nbr_connected_free;
149
150 /* Enable Link-detection by default */
151 SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
152
153 QOBJ_REG (ifp, interface);
154
155 if (if_master.if_new_hook)
156 (*if_master.if_new_hook) (ifp);
157
158 return ifp;
159 }
160
161 /* Create new interface structure. */
162 void
163 if_update (struct interface *ifp, const char *name, int namelen, vrf_id_t vrf_id)
164 {
165 struct list *intf_list = vrf_iflist_get (vrf_id);
166
167 /* remove interface from old master vrf list */
168 if (vrf_iflist (ifp->vrf_id))
169 listnode_delete (vrf_iflist (ifp->vrf_id), ifp);
170
171 assert (name);
172 assert (namelen <= INTERFACE_NAMSIZ); /* Need space for '\0' at end. */
173 strncpy (ifp->name, name, namelen);
174 ifp->name[namelen] = '\0';
175 ifp->vrf_id = vrf_id;
176 if (if_lookup_by_name (ifp->name, vrf_id) == NULL)
177 listnode_add_sort (intf_list, ifp);
178 else
179 zlog_err("if_create(%s): corruption detected -- interface with this "
180 "name exists already in VRF %u!", ifp->name, vrf_id);
181
182 return;
183 }
184
185
186 /* Delete interface structure. */
187 void
188 if_delete_retain (struct interface *ifp)
189 {
190 if (if_master.if_delete_hook)
191 (*if_master.if_delete_hook) (ifp);
192
193 QOBJ_UNREG (ifp);
194
195 /* Free connected address list */
196 list_delete_all_node (ifp->connected);
197
198 /* Free connected nbr address list */
199 list_delete_all_node (ifp->nbr_connected);
200 }
201
202 /* Delete and free interface structure. */
203 void
204 if_delete (struct interface *ifp)
205 {
206 listnode_delete (vrf_iflist (ifp->vrf_id), ifp);
207
208 if_delete_retain(ifp);
209
210 list_free (ifp->connected);
211 list_free (ifp->nbr_connected);
212
213 if_link_params_free (ifp);
214
215 XFREE (MTYPE_IF, ifp);
216 }
217
218 /* Add hook to interface master. */
219 void
220 if_add_hook (int type, int (*func)(struct interface *ifp))
221 {
222 switch (type) {
223 case IF_NEW_HOOK:
224 if_master.if_new_hook = func;
225 break;
226 case IF_DELETE_HOOK:
227 if_master.if_delete_hook = func;
228 break;
229 default:
230 break;
231 }
232 }
233
234 /* Interface existance check by index. */
235 struct interface *
236 if_lookup_by_index (ifindex_t ifindex, vrf_id_t vrf_id)
237 {
238 struct listnode *node;
239 struct interface *ifp;
240
241 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
242 {
243 if (ifp->ifindex == ifindex)
244 return ifp;
245 }
246 return NULL;
247 }
248
249 const char *
250 ifindex2ifname (ifindex_t ifindex, vrf_id_t vrf_id)
251 {
252 struct interface *ifp;
253
254 return ((ifp = if_lookup_by_index (ifindex, vrf_id)) != NULL) ?
255 ifp->name : "unknown";
256 }
257
258 ifindex_t
259 ifname2ifindex (const char *name, vrf_id_t vrf_id)
260 {
261 struct interface *ifp;
262
263 return ((ifp = if_lookup_by_name (name, vrf_id)) != NULL) ? ifp->ifindex
264 : IFINDEX_INTERNAL;
265 }
266
267 /* Interface existance check by interface name. */
268 struct interface *
269 if_lookup_by_name (const char *name, vrf_id_t vrf_id)
270 {
271 struct listnode *node;
272 struct interface *ifp;
273
274 if (name)
275 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
276 {
277 if (strcmp(name, ifp->name) == 0)
278 return ifp;
279 }
280 return NULL;
281 }
282
283 struct interface *
284 if_lookup_by_name_all_vrf (const char *name)
285 {
286 struct vrf *vrf;
287 struct interface *ifp;
288
289 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
290 {
291 ifp = if_lookup_by_name (name, vrf->vrf_id);
292 if (ifp)
293 return ifp;
294 }
295
296 return NULL;
297 }
298
299 struct interface *
300 if_lookup_by_name_len (const char *name, size_t namelen, vrf_id_t vrf_id)
301 {
302 struct listnode *node;
303 struct interface *ifp;
304
305 if (namelen > INTERFACE_NAMSIZ)
306 return NULL;
307
308 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
309 {
310 if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0'))
311 return ifp;
312 }
313 return NULL;
314 }
315
316 /* Lookup interface by IPv4 address. */
317 struct interface *
318 if_lookup_exact_address (void *src, int family, vrf_id_t vrf_id)
319 {
320 struct listnode *node;
321 struct listnode *cnode;
322 struct interface *ifp;
323 struct prefix *p;
324 struct connected *c;
325
326 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
327 {
328 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
329 {
330 p = c->address;
331
332 if (p && (p->family == family))
333 {
334 if (family == AF_INET)
335 {
336 if (IPV4_ADDR_SAME (&p->u.prefix4, (struct in_addr *)src))
337 return ifp;
338 }
339 else if (family == AF_INET6)
340 {
341 if (IPV6_ADDR_SAME (&p->u.prefix6, (struct in6_addr *)src))
342 return ifp;
343 }
344 }
345 }
346 }
347 return NULL;
348 }
349
350 /* Lookup interface by IPv4 address. */
351 struct connected *
352 if_lookup_address (void *matchaddr, int family, vrf_id_t vrf_id)
353 {
354 struct listnode *node;
355 struct prefix addr;
356 int bestlen = 0;
357 struct listnode *cnode;
358 struct interface *ifp;
359 struct connected *c;
360 struct connected *match;
361
362 if (family == AF_INET)
363 {
364 addr.family = AF_INET;
365 addr.u.prefix4 = *((struct in_addr *)matchaddr);
366 addr.prefixlen = IPV4_MAX_BITLEN;
367 }
368 else if (family == AF_INET6)
369 {
370 addr.family = AF_INET6;
371 addr.u.prefix6 = *((struct in6_addr *)matchaddr);
372 addr.prefixlen = IPV6_MAX_BITLEN;
373 }
374
375 match = NULL;
376
377 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
378 {
379 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
380 {
381 if (c->address && (c->address->family == AF_INET) &&
382 prefix_match(CONNECTED_PREFIX(c), &addr) &&
383 (c->address->prefixlen > bestlen))
384 {
385 bestlen = c->address->prefixlen;
386 match = c;
387 }
388 }
389 }
390 return match;
391 }
392
393 /* Lookup interface by prefix */
394 struct interface *
395 if_lookup_prefix (struct prefix *prefix, vrf_id_t vrf_id)
396 {
397 struct listnode *node;
398 struct listnode *cnode;
399 struct interface *ifp;
400 struct connected *c;
401
402 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
403 {
404 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
405 {
406 if (prefix_cmp(c->address, prefix) == 0)
407 {
408 return ifp;
409 }
410 }
411 }
412 return NULL;
413 }
414
415 /* Get interface by name if given name interface doesn't exist create
416 one. */
417 struct interface *
418 if_get_by_name (const char *name, vrf_id_t vrf_id)
419 {
420 struct interface *ifp;
421
422 return ((ifp = if_lookup_by_name (name, vrf_id)) != NULL) ? ifp :
423 if_create (name, strlen(name), vrf_id);
424 }
425
426 struct interface *
427 if_get_by_name_len (const char *name, size_t namelen, vrf_id_t vrf_id, int vty)
428 {
429 struct interface *ifp;
430 struct vrf *vrf;
431 struct listnode *node;
432
433 ifp = if_lookup_by_name_len (name, namelen, vrf_id);
434 if (ifp)
435 return ifp;
436
437 /* Didn't find the interface on that vrf. Defined on a different one? */
438 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
439 {
440 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf->vrf_id), node, ifp))
441 {
442 if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0'))
443 {
444 /* Found a match. If the interface command was entered in vty without a
445 * VRF (passed as VRF_DEFAULT), accept the ifp we found. If a vrf was
446 * entered and there is a mismatch, reject it if from vty. If it came
447 * from the kernel by way of zclient, believe it and update
448 * the ifp accordingly.
449 */
450 if (vty)
451 {
452 if (vrf_id == VRF_DEFAULT)
453 return ifp;
454 return NULL;
455 }
456 else
457 {
458 if_update (ifp, name, namelen, vrf_id);
459 return ifp;
460 }
461 }
462 }
463 }
464 return (if_create (name, namelen, vrf_id));
465 }
466
467 /* Does interface up ? */
468 int
469 if_is_up (struct interface *ifp)
470 {
471 return ifp->flags & IFF_UP;
472 }
473
474 /* Is interface running? */
475 int
476 if_is_running (struct interface *ifp)
477 {
478 return ifp->flags & IFF_RUNNING;
479 }
480
481 /* Is the interface operative, eg. either UP & RUNNING
482 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
483 if ptm checking is enabled, then ptm check has passed */
484 int
485 if_is_operative (struct interface *ifp)
486 {
487 return ((ifp->flags & IFF_UP) &&
488 (((ifp->flags & IFF_RUNNING) &&
489 (ifp->ptm_status || !ifp->ptm_enable)) ||
490 !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
491 }
492
493 /* Is the interface operative, eg. either UP & RUNNING
494 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
495 int
496 if_is_no_ptm_operative (struct interface *ifp)
497 {
498 return ((ifp->flags & IFF_UP) &&
499 ((ifp->flags & IFF_RUNNING) ||
500 !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
501 }
502
503 /* Is this loopback interface ? */
504 int
505 if_is_loopback (struct interface *ifp)
506 {
507 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
508 * but Y on platform N?
509 */
510 return (ifp->flags & (IFF_LOOPBACK|IFF_NOXMIT|IFF_VIRTUAL));
511 }
512
513 /* Does this interface support broadcast ? */
514 int
515 if_is_broadcast (struct interface *ifp)
516 {
517 return ifp->flags & IFF_BROADCAST;
518 }
519
520 /* Does this interface support broadcast ? */
521 int
522 if_is_pointopoint (struct interface *ifp)
523 {
524 return ifp->flags & IFF_POINTOPOINT;
525 }
526
527 /* Does this interface support multicast ? */
528 int
529 if_is_multicast (struct interface *ifp)
530 {
531 return ifp->flags & IFF_MULTICAST;
532 }
533
534 /* Printout flag information into log */
535 const char *
536 if_flag_dump (unsigned long flag)
537 {
538 int separator = 0;
539 static char logbuf[BUFSIZ];
540
541 #define IFF_OUT_LOG(X,STR) \
542 if (flag & (X)) \
543 { \
544 if (separator) \
545 strlcat (logbuf, ",", BUFSIZ); \
546 else \
547 separator = 1; \
548 strlcat (logbuf, STR, BUFSIZ); \
549 }
550
551 strlcpy (logbuf, "<", BUFSIZ);
552 IFF_OUT_LOG (IFF_UP, "UP");
553 IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST");
554 IFF_OUT_LOG (IFF_DEBUG, "DEBUG");
555 IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK");
556 IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT");
557 IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS");
558 IFF_OUT_LOG (IFF_RUNNING, "RUNNING");
559 IFF_OUT_LOG (IFF_NOARP, "NOARP");
560 IFF_OUT_LOG (IFF_PROMISC, "PROMISC");
561 IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI");
562 IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE");
563 IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX");
564 IFF_OUT_LOG (IFF_LINK0, "LINK0");
565 IFF_OUT_LOG (IFF_LINK1, "LINK1");
566 IFF_OUT_LOG (IFF_LINK2, "LINK2");
567 IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST");
568 IFF_OUT_LOG (IFF_NOXMIT, "NOXMIT");
569 IFF_OUT_LOG (IFF_NORTEXCH, "NORTEXCH");
570 IFF_OUT_LOG (IFF_VIRTUAL, "VIRTUAL");
571 IFF_OUT_LOG (IFF_IPV4, "IPv4");
572 IFF_OUT_LOG (IFF_IPV6, "IPv6");
573
574 strlcat (logbuf, ">", BUFSIZ);
575
576 return logbuf;
577 #undef IFF_OUT_LOG
578 }
579
580 /* For debugging */
581 static void
582 if_dump (const struct interface *ifp)
583 {
584 struct listnode *node;
585 struct connected *c __attribute__((unused));
586
587 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, c))
588 zlog_info ("Interface %s vrf %u index %d metric %d mtu %d "
589 "mtu6 %d %s",
590 ifp->name, ifp->vrf_id, ifp->ifindex, ifp->metric, ifp->mtu,
591 ifp->mtu6, if_flag_dump (ifp->flags));
592 }
593
594 /* Interface printing for all interface. */
595 void
596 if_dump_all (void)
597 {
598 struct vrf *vrf;
599 struct listnode *node;
600 void *p;
601
602 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
603 if (vrf->iflist != NULL)
604 for (ALL_LIST_ELEMENTS_RO (vrf->iflist, node, p))
605 if_dump (p);
606 }
607
608 DEFUN (interface_desc,
609 interface_desc_cmd,
610 "description LINE...",
611 "Interface specific description\n"
612 "Characters describing this interface\n")
613 {
614 int idx_line = 1;
615 VTY_DECLVAR_CONTEXT (interface, ifp);
616
617 if (ifp->desc)
618 XFREE (MTYPE_TMP, ifp->desc);
619 ifp->desc = argv_concat(argv, argc, idx_line);
620
621 return CMD_SUCCESS;
622 }
623
624 DEFUN (no_interface_desc,
625 no_interface_desc_cmd,
626 "no description",
627 NO_STR
628 "Interface specific description\n")
629 {
630 VTY_DECLVAR_CONTEXT (interface, ifp);
631
632 if (ifp->desc)
633 XFREE (MTYPE_TMP, ifp->desc);
634 ifp->desc = NULL;
635
636 return CMD_SUCCESS;
637 }
638
639 #ifdef SUNOS_5
640 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
641 * a seperate struct interface for each logical interface, so config
642 * file may be full of 'interface fooX:Y'. Solaris however does not
643 * expose logical interfaces via PF_ROUTE, so trying to track logical
644 * interfaces can be fruitless, for that reason Quagga only tracks
645 * the primary IP interface.
646 *
647 * We try accomodate SUNWzebra by:
648 * - looking up the interface name, to see whether it exists, if so
649 * its useable
650 * - for protocol daemons, this could only because zebra told us of
651 * the interface
652 * - for zebra, only because it learnt from kernel
653 * - if not:
654 * - search the name to see if it contains a sub-ipif / logical interface
655 * seperator, the ':' char. If it does:
656 * - text up to that char must be the primary name - get that name.
657 * if not:
658 * - no idea, just get the name in its entirety.
659 */
660 static struct interface *
661 if_sunwzebra_get (const char *name, size_t nlen, vrf_id_t vrf_id)
662 {
663 struct interface *ifp;
664 size_t seppos = 0;
665
666 if ( (ifp = if_lookup_by_name_len (name, nlen, vrf_id)) != NULL)
667 return ifp;
668
669 /* hunt the primary interface name... */
670 while (seppos < nlen && name[seppos] != ':')
671 seppos++;
672
673 /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */
674 if (seppos < nlen)
675 return if_get_by_name_len (name, seppos, vrf_id, 1);
676 else
677 return if_get_by_name_len (name, nlen, vrf_id, 1);
678 }
679 #endif /* SUNOS_5 */
680
681 DEFUN (interface,
682 interface_cmd,
683 "interface IFNAME [vrf NAME]",
684 "Select an interface to configure\n"
685 "Interface's name\n"
686 VRF_CMD_HELP_STR)
687 {
688 int idx_ifname = 1;
689 int idx_vrf = 3;
690 const char *ifname = argv[idx_ifname]->arg;
691 const char *vrfname = (argc > 2) ? argv[idx_vrf]->arg : NULL;
692
693 struct interface *ifp;
694 size_t sl;
695 vrf_id_t vrf_id = VRF_DEFAULT;
696
697 if ((sl = strlen(ifname)) > INTERFACE_NAMSIZ)
698 {
699 vty_out (vty, "%% Interface name %s is invalid: length exceeds "
700 "%d characters%s",
701 ifname, INTERFACE_NAMSIZ, VTY_NEWLINE);
702 return CMD_WARNING;
703 }
704
705 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
706 Imagine forward reference of a vrf by name in this interface config */
707 if (vrfname)
708 VRF_GET_ID (vrf_id, vrfname);
709
710 #ifdef SUNOS_5
711 ifp = if_sunwzebra_get (ifname, sl, vrf_id);
712 #else
713 ifp = if_get_by_name_len (ifname, sl, vrf_id, 1);
714 #endif /* SUNOS_5 */
715
716 if (!ifp)
717 {
718 vty_out (vty, "%% interface %s not in %s%s", ifname, vrfname, VTY_NEWLINE);
719 return CMD_WARNING;
720 }
721 VTY_PUSH_CONTEXT (INTERFACE_NODE, ifp);
722
723 return CMD_SUCCESS;
724 }
725
726 DEFUN_NOSH (no_interface,
727 no_interface_cmd,
728 "no interface IFNAME [vrf NAME]",
729 NO_STR
730 "Delete a pseudo interface's configuration\n"
731 "Interface's name\n"
732 VRF_CMD_HELP_STR)
733 {
734 const char *ifname = argv[2]->arg;
735 const char *vrfname = (argc > 3) ? argv[3]->arg : NULL;
736
737 // deleting interface
738 struct interface *ifp;
739 vrf_id_t vrf_id = VRF_DEFAULT;
740
741 if (argc > 3)
742 VRF_GET_ID (vrf_id, vrfname);
743
744 ifp = if_lookup_by_name (ifname, vrf_id);
745
746 if (ifp == NULL)
747 {
748 vty_out (vty, "%% Interface %s does not exist%s", ifname, VTY_NEWLINE);
749 return CMD_WARNING;
750 }
751
752 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
753 {
754 vty_out (vty, "%% Only inactive interfaces can be deleted%s",
755 VTY_NEWLINE);
756 return CMD_WARNING;
757 }
758
759 if_delete(ifp);
760
761 return CMD_SUCCESS;
762 }
763
764 void
765 if_cmd_init (void)
766 {
767 install_element (CONFIG_NODE, &interface_cmd);
768 install_element (CONFIG_NODE, &no_interface_cmd);
769
770 install_default (INTERFACE_NODE);
771 install_element (INTERFACE_NODE, &interface_desc_cmd);
772 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
773 }
774
775 #if 0
776 /* For debug purpose. */
777 DEFUN (show_address,
778 show_address_cmd,
779 "show address [vrf NAME]",
780 SHOW_STR
781 "address\n"
782 VRF_CMD_HELP_STR)
783 {
784 int idx_vrf = 3;
785 struct listnode *node;
786 struct listnode *node2;
787 struct interface *ifp;
788 struct connected *ifc;
789 struct prefix *p;
790 vrf_id_t vrf_id = VRF_DEFAULT;
791
792 if (argc > 2)
793 VRF_GET_ID (vrf_id, argv[idx_vrf]->arg);
794
795 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
796 {
797 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
798 {
799 p = ifc->address;
800
801 if (p->family == AF_INET)
802 vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
803 VTY_NEWLINE);
804 }
805 }
806 return CMD_SUCCESS;
807 }
808
809 DEFUN (show_address_vrf_all,
810 show_address_vrf_all_cmd,
811 "show address vrf all",
812 SHOW_STR
813 "address\n"
814 VRF_ALL_CMD_HELP_STR)
815 {
816 struct vrf *vrf;
817 struct listnode *node;
818 struct listnode *node2;
819 struct interface *ifp;
820 struct connected *ifc;
821 struct prefix *p;
822
823 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
824 {
825 if (!vrf->iflist || !listcount (vrf->iflist))
826 continue;
827
828 vty_out (vty, "%sVRF %u%s%s", VTY_NEWLINE, vrf->vrf_id, VTY_NEWLINE,
829 VTY_NEWLINE);
830
831 for (ALL_LIST_ELEMENTS_RO (vrf->iflist, node, ifp))
832 {
833 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
834 {
835 p = ifc->address;
836
837 if (p->family == AF_INET)
838 vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
839 VTY_NEWLINE);
840 }
841 }
842 }
843 return CMD_SUCCESS;
844 }
845 #endif
846
847 /* Allocate connected structure. */
848 struct connected *
849 connected_new (void)
850 {
851 return XCALLOC (MTYPE_CONNECTED, sizeof (struct connected));
852 }
853
854 /* Allocate nbr connected structure. */
855 struct nbr_connected *
856 nbr_connected_new (void)
857 {
858 return XCALLOC (MTYPE_NBR_CONNECTED, sizeof (struct nbr_connected));
859 }
860
861 /* Free connected structure. */
862 void
863 connected_free (struct connected *connected)
864 {
865 if (connected->address)
866 prefix_free (connected->address);
867
868 if (connected->destination)
869 prefix_free (connected->destination);
870
871 if (connected->label)
872 XFREE (MTYPE_CONNECTED_LABEL, connected->label);
873
874 XFREE (MTYPE_CONNECTED, connected);
875 }
876
877 /* Free nbr connected structure. */
878 void
879 nbr_connected_free (struct nbr_connected *connected)
880 {
881 if (connected->address)
882 prefix_free (connected->address);
883
884 XFREE (MTYPE_NBR_CONNECTED, connected);
885 }
886
887 /* If same interface nbr address already exists... */
888 struct nbr_connected *
889 nbr_connected_check (struct interface *ifp, struct prefix *p)
890 {
891 struct nbr_connected *ifc;
892 struct listnode *node;
893
894 for (ALL_LIST_ELEMENTS_RO (ifp->nbr_connected, node, ifc))
895 if (prefix_same (ifc->address, p))
896 return ifc;
897
898 return NULL;
899 }
900
901 /* Print if_addr structure. */
902 static void __attribute__ ((unused))
903 connected_log (struct connected *connected, char *str)
904 {
905 struct prefix *p;
906 struct interface *ifp;
907 char logbuf[BUFSIZ];
908 char buf[BUFSIZ];
909
910 ifp = connected->ifp;
911 p = connected->address;
912
913 snprintf (logbuf, BUFSIZ, "%s interface %s vrf %u %s %s/%d ",
914 str, ifp->name, ifp->vrf_id, prefix_family_str (p),
915 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
916 p->prefixlen);
917
918 p = connected->destination;
919 if (p)
920 {
921 strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
922 BUFSIZ - strlen(logbuf));
923 }
924 zlog_info("%s", logbuf);
925 }
926
927 /* Print if_addr structure. */
928 static void __attribute__ ((unused))
929 nbr_connected_log (struct nbr_connected *connected, char *str)
930 {
931 struct prefix *p;
932 struct interface *ifp;
933 char logbuf[BUFSIZ];
934 char buf[BUFSIZ];
935
936 ifp = connected->ifp;
937 p = connected->address;
938
939 snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ",
940 str, ifp->name, prefix_family_str (p),
941 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
942 p->prefixlen);
943
944 zlog_info("%s", logbuf);
945 }
946
947 /* If two connected address has same prefix return 1. */
948 static int
949 connected_same_prefix (struct prefix *p1, struct prefix *p2)
950 {
951 if (p1->family == p2->family)
952 {
953 if (p1->family == AF_INET &&
954 IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4))
955 return 1;
956 if (p1->family == AF_INET6 &&
957 IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6))
958 return 1;
959 }
960 return 0;
961 }
962
963 struct connected *
964 connected_lookup_prefix_exact (struct interface *ifp, struct prefix *p)
965 {
966 struct listnode *node;
967 struct listnode *next;
968 struct connected *ifc;
969
970 for (node = listhead (ifp->connected); node; node = next)
971 {
972 ifc = listgetdata (node);
973 next = node->next;
974
975 if (connected_same_prefix (ifc->address, p))
976 return ifc;
977 }
978 return NULL;
979 }
980
981 struct connected *
982 connected_delete_by_prefix (struct interface *ifp, struct prefix *p)
983 {
984 struct listnode *node;
985 struct listnode *next;
986 struct connected *ifc;
987
988 /* In case of same prefix come, replace it with new one. */
989 for (node = listhead (ifp->connected); node; node = next)
990 {
991 ifc = listgetdata (node);
992 next = node->next;
993
994 if (connected_same_prefix (ifc->address, p))
995 {
996 listnode_delete (ifp->connected, ifc);
997 return ifc;
998 }
999 }
1000 return NULL;
1001 }
1002
1003 /* Find the address on our side that will be used when packets
1004 are sent to dst. */
1005 struct connected *
1006 connected_lookup_prefix (struct interface *ifp, struct prefix *addr)
1007 {
1008 struct listnode *cnode;
1009 struct connected *c;
1010 struct connected *match;
1011
1012 match = NULL;
1013
1014 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
1015 {
1016 if (c->address && (c->address->family == addr->family) &&
1017 prefix_match(CONNECTED_PREFIX(c), addr) &&
1018 (!match || (c->address->prefixlen > match->address->prefixlen)))
1019 match = c;
1020 }
1021 return match;
1022 }
1023
1024 struct connected *
1025 connected_add_by_prefix (struct interface *ifp, struct prefix *p,
1026 struct prefix *destination)
1027 {
1028 struct connected *ifc;
1029
1030 /* Allocate new connected address. */
1031 ifc = connected_new ();
1032 ifc->ifp = ifp;
1033
1034 /* Fetch interface address */
1035 ifc->address = prefix_new();
1036 memcpy (ifc->address, p, sizeof(struct prefix));
1037
1038 /* Fetch dest address */
1039 if (destination)
1040 {
1041 ifc->destination = prefix_new();
1042 memcpy (ifc->destination, destination, sizeof(struct prefix));
1043 }
1044
1045 /* Add connected address to the interface. */
1046 listnode_add (ifp->connected, ifc);
1047 return ifc;
1048 }
1049
1050 #if 0 /* this route_table of struct connected's is unused
1051 * however, it would be good to use a route_table rather than
1052 * a list..
1053 */
1054 /* Interface looking up by interface's address. */
1055 /* Interface's IPv4 address reverse lookup table. */
1056 struct route_table *ifaddr_ipv4_table;
1057 /* struct route_table *ifaddr_ipv6_table; */
1058
1059 static void
1060 ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
1061 {
1062 struct route_node *rn;
1063 struct prefix_ipv4 p;
1064
1065 p.family = AF_INET;
1066 p.prefixlen = IPV4_MAX_PREFIXLEN;
1067 p.prefix = *ifaddr;
1068
1069 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
1070 if (rn)
1071 {
1072 route_unlock_node (rn);
1073 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1074 inet_ntoa (*ifaddr));
1075 return;
1076 }
1077 rn->info = ifp;
1078 }
1079
1080 static void
1081 ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
1082 {
1083 struct route_node *rn;
1084 struct prefix_ipv4 p;
1085
1086 p.family = AF_INET;
1087 p.prefixlen = IPV4_MAX_PREFIXLEN;
1088 p.prefix = *ifaddr;
1089
1090 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1091 if (! rn)
1092 {
1093 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1094 inet_ntoa (*ifaddr));
1095 return;
1096 }
1097 rn->info = NULL;
1098 route_unlock_node (rn);
1099 route_unlock_node (rn);
1100 }
1101
1102 /* Lookup interface by interface's IP address or interface index. */
1103 static struct interface *
1104 ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex)
1105 {
1106 struct prefix_ipv4 p;
1107 struct route_node *rn;
1108 struct interface *ifp;
1109
1110 if (addr)
1111 {
1112 p.family = AF_INET;
1113 p.prefixlen = IPV4_MAX_PREFIXLEN;
1114 p.prefix = *addr;
1115
1116 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1117 if (! rn)
1118 return NULL;
1119
1120 ifp = rn->info;
1121 route_unlock_node (rn);
1122 return ifp;
1123 }
1124 else
1125 return if_lookup_by_index(ifindex, VRF_DEFAULT);
1126 }
1127 #endif /* ifaddr_ipv4_table */
1128
1129 /* Initialize interface list. */
1130 void
1131 if_init (struct list **intf_list)
1132 {
1133 *intf_list = list_new ();
1134 #if 0
1135 ifaddr_ipv4_table = route_table_init ();
1136 #endif /* ifaddr_ipv4_table */
1137
1138 (*intf_list)->cmp = (int (*)(void *, void *))if_cmp_func;
1139 }
1140
1141 void
1142 if_terminate (struct list **intf_list)
1143 {
1144 for (;;)
1145 {
1146 struct interface *ifp;
1147
1148 ifp = listnode_head (*intf_list);
1149 if (ifp == NULL)
1150 break;
1151
1152 if (ifp->node)
1153 {
1154 ifp->node->info = NULL;
1155 route_unlock_node (ifp->node);
1156 }
1157
1158 if_delete (ifp);
1159 }
1160
1161 list_delete (*intf_list);
1162 *intf_list = NULL;
1163 }
1164
1165 const char *
1166 if_link_type_str (enum zebra_link_type llt)
1167 {
1168 switch (llt)
1169 {
1170 #define llts(T,S) case (T): return (S)
1171 llts(ZEBRA_LLT_UNKNOWN, "Unknown");
1172 llts(ZEBRA_LLT_ETHER, "Ethernet");
1173 llts(ZEBRA_LLT_EETHER, "Experimental Ethernet");
1174 llts(ZEBRA_LLT_AX25, "AX.25 Level 2");
1175 llts(ZEBRA_LLT_PRONET, "PROnet token ring");
1176 llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB");
1177 llts(ZEBRA_LLT_ARCNET, "ARCnet");
1178 llts(ZEBRA_LLT_APPLETLK, "AppleTalk");
1179 llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI");
1180 llts(ZEBRA_LLT_ATM, "ATM");
1181 llts(ZEBRA_LLT_METRICOM, "Metricom STRIP");
1182 llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4");
1183 llts(ZEBRA_LLT_EUI64, "EUI-64");
1184 llts(ZEBRA_LLT_INFINIBAND, "InfiniBand");
1185 llts(ZEBRA_LLT_SLIP, "SLIP");
1186 llts(ZEBRA_LLT_CSLIP, "Compressed SLIP");
1187 llts(ZEBRA_LLT_SLIP6, "SLIPv6");
1188 llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6");
1189 llts(ZEBRA_LLT_ROSE, "ROSE packet radio");
1190 llts(ZEBRA_LLT_X25, "CCITT X.25");
1191 llts(ZEBRA_LLT_PPP, "PPP");
1192 llts(ZEBRA_LLT_CHDLC, "Cisco HDLC");
1193 llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC");
1194 llts(ZEBRA_LLT_LAPB, "LAPB");
1195 llts(ZEBRA_LLT_IPIP, "IPIP Tunnel");
1196 llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel");
1197 llts(ZEBRA_LLT_FRAD, "FRAD");
1198 llts(ZEBRA_LLT_SKIP, "SKIP vif");
1199 llts(ZEBRA_LLT_LOOPBACK, "Loopback");
1200 llts(ZEBRA_LLT_LOCALTLK, "Localtalk");
1201 llts(ZEBRA_LLT_FDDI, "FDDI");
1202 llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT");
1203 llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel");
1204 llts(ZEBRA_LLT_IPGRE, "GRE over IP");
1205 llts(ZEBRA_LLT_PIMREG, "PIMSM registration");
1206 llts(ZEBRA_LLT_HIPPI, "HiPPI");
1207 llts(ZEBRA_LLT_IRDA, "IrDA");
1208 llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP");
1209 llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop");
1210 llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop");
1211 llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric");
1212 llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring");
1213 llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11");
1214 llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap");
1215 llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4");
1216 llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy");
1217 default:
1218 zlog_warn ("Unknown value %d", llt);
1219 return "Unknown type!";
1220 #undef llts
1221 }
1222 return NULL;
1223 }
1224
1225 struct if_link_params *
1226 if_link_params_get (struct interface *ifp)
1227 {
1228 int i;
1229
1230 if (ifp->link_params != NULL)
1231 return ifp->link_params;
1232
1233 struct if_link_params *iflp = XCALLOC(MTYPE_IF_LINK_PARAMS,
1234 sizeof (struct if_link_params));
1235 if (iflp == NULL) return NULL;
1236
1237 /* Set TE metric equal to standard metric */
1238 iflp->te_metric = ifp->metric;
1239
1240 /* Compute default bandwidth based on interface */
1241 iflp->default_bw = ((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH)
1242 * TE_KILO_BIT / TE_BYTE);
1243
1244 /* Set Max, Reservable and Unreserved Bandwidth */
1245 iflp->max_bw = iflp->default_bw;
1246 iflp->max_rsv_bw = iflp->default_bw;
1247 for (i = 0; i < MAX_CLASS_TYPE; i++)
1248 iflp->unrsv_bw[i] = iflp->default_bw;
1249
1250 /* Update Link parameters status */
1251 iflp->lp_status = LP_TE_METRIC | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW;
1252
1253 /* Finally attach newly created Link Parameters */
1254 ifp->link_params = iflp;
1255
1256 return iflp;
1257 }
1258
1259 void
1260 if_link_params_free (struct interface *ifp)
1261 {
1262 if (ifp->link_params == NULL) return;
1263 XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params);
1264 ifp->link_params = NULL;
1265 }