]> git.proxmox.com Git - mirror_frr.git/blob - lib/if.c
lib, ospfd: Refactor if_lookup_by_name_len
[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_vrf (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_vrf (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_vrf (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_vrf (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 const char *
259 ifindex2ifname (ifindex_t ifindex)
260 {
261 return ifindex2ifname_vrf (ifindex, VRF_DEFAULT);
262 }
263
264 ifindex_t
265 ifname2ifindex_vrf (const char *name, vrf_id_t vrf_id)
266 {
267 struct interface *ifp;
268
269 return ((ifp = if_lookup_by_name_vrf (name, vrf_id)) != NULL) ? ifp->ifindex
270 : IFINDEX_INTERNAL;
271 }
272
273 ifindex_t
274 ifname2ifindex (const char *name)
275 {
276 return ifname2ifindex_vrf (name, VRF_DEFAULT);
277 }
278
279 /* Interface existance check by interface name. */
280 struct interface *
281 if_lookup_by_name_vrf (const char *name, vrf_id_t vrf_id)
282 {
283 struct listnode *node;
284 struct interface *ifp;
285
286 if (name)
287 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
288 {
289 if (strcmp(name, ifp->name) == 0)
290 return ifp;
291 }
292 return NULL;
293 }
294
295 struct interface *
296 if_lookup_by_name_all_vrf (const char *name)
297 {
298 struct vrf *vrf;
299 struct interface *ifp;
300
301 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
302 {
303 ifp = if_lookup_by_name_vrf (name, vrf->vrf_id);
304 if (ifp)
305 return ifp;
306 }
307
308 return NULL;
309 }
310
311 struct interface *
312 if_lookup_by_name (const char *name)
313 {
314 return if_lookup_by_name_vrf (name, VRF_DEFAULT);
315 }
316
317 struct interface *
318 if_lookup_by_name_len (const char *name, size_t namelen, vrf_id_t vrf_id)
319 {
320 struct listnode *node;
321 struct interface *ifp;
322
323 if (namelen > INTERFACE_NAMSIZ)
324 return NULL;
325
326 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
327 {
328 if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0'))
329 return ifp;
330 }
331 return NULL;
332 }
333
334 /* Lookup interface by IPv4 address. */
335 struct interface *
336 if_lookup_exact_address (void *src, int family, vrf_id_t vrf_id)
337 {
338 struct listnode *node;
339 struct listnode *cnode;
340 struct interface *ifp;
341 struct prefix *p;
342 struct connected *c;
343
344 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
345 {
346 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
347 {
348 p = c->address;
349
350 if (p && (p->family == family))
351 {
352 if (family == AF_INET)
353 {
354 if (IPV4_ADDR_SAME (&p->u.prefix4, (struct in_addr *)src))
355 return ifp;
356 }
357 else if (family == AF_INET6)
358 {
359 if (IPV6_ADDR_SAME (&p->u.prefix6, (struct in6_addr *)src))
360 return ifp;
361 }
362 }
363 }
364 }
365 return NULL;
366 }
367
368 /* Lookup interface by IPv4 address. */
369 struct connected *
370 if_lookup_address (void *matchaddr, int family, vrf_id_t vrf_id)
371 {
372 struct listnode *node;
373 struct prefix addr;
374 int bestlen = 0;
375 struct listnode *cnode;
376 struct interface *ifp;
377 struct connected *c;
378 struct connected *match;
379
380 if (family == AF_INET)
381 {
382 addr.family = AF_INET;
383 addr.u.prefix4 = *((struct in_addr *)matchaddr);
384 addr.prefixlen = IPV4_MAX_BITLEN;
385 }
386 else if (family == AF_INET6)
387 {
388 addr.family = AF_INET6;
389 addr.u.prefix6 = *((struct in6_addr *)matchaddr);
390 addr.prefixlen = IPV6_MAX_BITLEN;
391 }
392
393 match = NULL;
394
395 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
396 {
397 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
398 {
399 if (c->address && (c->address->family == AF_INET) &&
400 prefix_match(CONNECTED_PREFIX(c), &addr) &&
401 (c->address->prefixlen > bestlen))
402 {
403 bestlen = c->address->prefixlen;
404 match = c;
405 }
406 }
407 }
408 return match;
409 }
410
411 /* Lookup interface by prefix */
412 struct interface *
413 if_lookup_prefix_vrf (struct prefix *prefix, vrf_id_t vrf_id)
414 {
415 struct listnode *node;
416 struct listnode *cnode;
417 struct interface *ifp;
418 struct connected *c;
419
420 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
421 {
422 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
423 {
424 if (prefix_cmp(c->address, prefix) == 0)
425 {
426 return ifp;
427 }
428 }
429 }
430 return NULL;
431 }
432
433 struct interface *
434 if_lookup_prefix (struct prefix *prefix)
435 {
436 return if_lookup_prefix_vrf (prefix, VRF_DEFAULT);
437 }
438
439 /* Get interface by name if given name interface doesn't exist create
440 one. */
441 struct interface *
442 if_get_by_name_vrf (const char *name, vrf_id_t vrf_id)
443 {
444 struct interface *ifp;
445
446 return ((ifp = if_lookup_by_name_vrf (name, vrf_id)) != NULL) ? ifp :
447 if_create (name, strlen(name), vrf_id);
448 }
449
450 struct interface *
451 if_get_by_name (const char *name)
452 {
453 return if_get_by_name_vrf (name, VRF_DEFAULT);
454 }
455
456 struct interface *
457 if_get_by_name_len_vrf (const char *name, size_t namelen, vrf_id_t vrf_id, int vty)
458 {
459 struct interface *ifp;
460 struct vrf *vrf;
461 struct listnode *node;
462
463 ifp = if_lookup_by_name_len (name, namelen, vrf_id);
464 if (ifp)
465 return ifp;
466
467 /* Didn't find the interface on that vrf. Defined on a different one? */
468 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
469 {
470 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf->vrf_id), node, ifp))
471 {
472 if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0'))
473 {
474 /* Found a match. If the interface command was entered in vty without a
475 * VRF (passed as VRF_DEFAULT), accept the ifp we found. If a vrf was
476 * entered and there is a mismatch, reject it if from vty. If it came
477 * from the kernel by way of zclient, believe it and update
478 * the ifp accordingly.
479 */
480 if (vty)
481 {
482 if (vrf_id == VRF_DEFAULT)
483 return ifp;
484 return NULL;
485 }
486 else
487 {
488 if_update_vrf (ifp, name, namelen, vrf_id);
489 return ifp;
490 }
491 }
492 }
493 }
494 return (if_create (name, namelen, vrf_id));
495 }
496
497 struct interface *
498 if_get_by_name_len (const char *name, size_t namelen)
499 {
500 return if_get_by_name_len_vrf (name, namelen, VRF_DEFAULT, 0);
501 }
502
503 /* Does interface up ? */
504 int
505 if_is_up (struct interface *ifp)
506 {
507 return ifp->flags & IFF_UP;
508 }
509
510 /* Is interface running? */
511 int
512 if_is_running (struct interface *ifp)
513 {
514 return ifp->flags & IFF_RUNNING;
515 }
516
517 /* Is the interface operative, eg. either UP & RUNNING
518 or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
519 if ptm checking is enabled, then ptm check has passed */
520 int
521 if_is_operative (struct interface *ifp)
522 {
523 return ((ifp->flags & IFF_UP) &&
524 (((ifp->flags & IFF_RUNNING) &&
525 (ifp->ptm_status || !ifp->ptm_enable)) ||
526 !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
527 }
528
529 /* Is the interface operative, eg. either UP & RUNNING
530 or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
531 int
532 if_is_no_ptm_operative (struct interface *ifp)
533 {
534 return ((ifp->flags & IFF_UP) &&
535 ((ifp->flags & IFF_RUNNING) ||
536 !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
537 }
538
539 /* Is this loopback interface ? */
540 int
541 if_is_loopback (struct interface *ifp)
542 {
543 /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
544 * but Y on platform N?
545 */
546 return (ifp->flags & (IFF_LOOPBACK|IFF_NOXMIT|IFF_VIRTUAL));
547 }
548
549 /* Does this interface support broadcast ? */
550 int
551 if_is_broadcast (struct interface *ifp)
552 {
553 return ifp->flags & IFF_BROADCAST;
554 }
555
556 /* Does this interface support broadcast ? */
557 int
558 if_is_pointopoint (struct interface *ifp)
559 {
560 return ifp->flags & IFF_POINTOPOINT;
561 }
562
563 /* Does this interface support multicast ? */
564 int
565 if_is_multicast (struct interface *ifp)
566 {
567 return ifp->flags & IFF_MULTICAST;
568 }
569
570 /* Printout flag information into log */
571 const char *
572 if_flag_dump (unsigned long flag)
573 {
574 int separator = 0;
575 static char logbuf[BUFSIZ];
576
577 #define IFF_OUT_LOG(X,STR) \
578 if (flag & (X)) \
579 { \
580 if (separator) \
581 strlcat (logbuf, ",", BUFSIZ); \
582 else \
583 separator = 1; \
584 strlcat (logbuf, STR, BUFSIZ); \
585 }
586
587 strlcpy (logbuf, "<", BUFSIZ);
588 IFF_OUT_LOG (IFF_UP, "UP");
589 IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST");
590 IFF_OUT_LOG (IFF_DEBUG, "DEBUG");
591 IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK");
592 IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT");
593 IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS");
594 IFF_OUT_LOG (IFF_RUNNING, "RUNNING");
595 IFF_OUT_LOG (IFF_NOARP, "NOARP");
596 IFF_OUT_LOG (IFF_PROMISC, "PROMISC");
597 IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI");
598 IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE");
599 IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX");
600 IFF_OUT_LOG (IFF_LINK0, "LINK0");
601 IFF_OUT_LOG (IFF_LINK1, "LINK1");
602 IFF_OUT_LOG (IFF_LINK2, "LINK2");
603 IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST");
604 IFF_OUT_LOG (IFF_NOXMIT, "NOXMIT");
605 IFF_OUT_LOG (IFF_NORTEXCH, "NORTEXCH");
606 IFF_OUT_LOG (IFF_VIRTUAL, "VIRTUAL");
607 IFF_OUT_LOG (IFF_IPV4, "IPv4");
608 IFF_OUT_LOG (IFF_IPV6, "IPv6");
609
610 strlcat (logbuf, ">", BUFSIZ);
611
612 return logbuf;
613 #undef IFF_OUT_LOG
614 }
615
616 /* For debugging */
617 static void
618 if_dump (const struct interface *ifp)
619 {
620 struct listnode *node;
621 struct connected *c __attribute__((unused));
622
623 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, c))
624 zlog_info ("Interface %s vrf %u index %d metric %d mtu %d "
625 "mtu6 %d %s",
626 ifp->name, ifp->vrf_id, ifp->ifindex, ifp->metric, ifp->mtu,
627 ifp->mtu6, if_flag_dump (ifp->flags));
628 }
629
630 /* Interface printing for all interface. */
631 void
632 if_dump_all (void)
633 {
634 struct vrf *vrf;
635 struct listnode *node;
636 void *p;
637
638 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
639 if (vrf->iflist != NULL)
640 for (ALL_LIST_ELEMENTS_RO (vrf->iflist, node, p))
641 if_dump (p);
642 }
643
644 DEFUN (interface_desc,
645 interface_desc_cmd,
646 "description LINE...",
647 "Interface specific description\n"
648 "Characters describing this interface\n")
649 {
650 int idx_line = 1;
651 VTY_DECLVAR_CONTEXT (interface, ifp);
652
653 if (ifp->desc)
654 XFREE (MTYPE_TMP, ifp->desc);
655 ifp->desc = argv_concat(argv, argc, idx_line);
656
657 return CMD_SUCCESS;
658 }
659
660 DEFUN (no_interface_desc,
661 no_interface_desc_cmd,
662 "no description",
663 NO_STR
664 "Interface specific description\n")
665 {
666 VTY_DECLVAR_CONTEXT (interface, ifp);
667
668 if (ifp->desc)
669 XFREE (MTYPE_TMP, ifp->desc);
670 ifp->desc = NULL;
671
672 return CMD_SUCCESS;
673 }
674
675 #ifdef SUNOS_5
676 /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
677 * a seperate struct interface for each logical interface, so config
678 * file may be full of 'interface fooX:Y'. Solaris however does not
679 * expose logical interfaces via PF_ROUTE, so trying to track logical
680 * interfaces can be fruitless, for that reason Quagga only tracks
681 * the primary IP interface.
682 *
683 * We try accomodate SUNWzebra by:
684 * - looking up the interface name, to see whether it exists, if so
685 * its useable
686 * - for protocol daemons, this could only because zebra told us of
687 * the interface
688 * - for zebra, only because it learnt from kernel
689 * - if not:
690 * - search the name to see if it contains a sub-ipif / logical interface
691 * seperator, the ':' char. If it does:
692 * - text up to that char must be the primary name - get that name.
693 * if not:
694 * - no idea, just get the name in its entirety.
695 */
696 static struct interface *
697 if_sunwzebra_get (const char *name, size_t nlen, vrf_id_t vrf_id)
698 {
699 struct interface *ifp;
700 size_t seppos = 0;
701
702 if ( (ifp = if_lookup_by_name_len (name, nlen, vrf_id)) != NULL)
703 return ifp;
704
705 /* hunt the primary interface name... */
706 while (seppos < nlen && name[seppos] != ':')
707 seppos++;
708
709 /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */
710 if (seppos < nlen)
711 return if_get_by_name_len_vrf (name, seppos, vrf_id, 1);
712 else
713 return if_get_by_name_len_vrf (name, nlen, vrf_id, 1);
714 }
715 #endif /* SUNOS_5 */
716
717 DEFUN (interface,
718 interface_cmd,
719 "interface IFNAME [vrf NAME]",
720 "Select an interface to configure\n"
721 "Interface's name\n"
722 VRF_CMD_HELP_STR)
723 {
724 int idx_ifname = 1;
725 int idx_vrf = 3;
726 const char *ifname = argv[idx_ifname]->arg;
727 const char *vrfname = (argc > 2) ? argv[idx_vrf]->arg : NULL;
728
729 struct interface *ifp;
730 size_t sl;
731 vrf_id_t vrf_id = VRF_DEFAULT;
732
733 if ((sl = strlen(ifname)) > INTERFACE_NAMSIZ)
734 {
735 vty_out (vty, "%% Interface name %s is invalid: length exceeds "
736 "%d characters%s",
737 ifname, INTERFACE_NAMSIZ, VTY_NEWLINE);
738 return CMD_WARNING;
739 }
740
741 /*Pending: need proper vrf name based lookup/(possible creation of VRF)
742 Imagine forward reference of a vrf by name in this interface config */
743 if (vrfname)
744 VRF_GET_ID (vrf_id, vrfname);
745
746 #ifdef SUNOS_5
747 ifp = if_sunwzebra_get (ifname, sl, vrf_id);
748 #else
749 ifp = if_get_by_name_len_vrf (ifname, sl, vrf_id, 1);
750 #endif /* SUNOS_5 */
751
752 if (!ifp)
753 {
754 vty_out (vty, "%% interface %s not in %s%s", ifname, vrfname, VTY_NEWLINE);
755 return CMD_WARNING;
756 }
757 VTY_PUSH_CONTEXT (INTERFACE_NODE, ifp);
758
759 return CMD_SUCCESS;
760 }
761
762 DEFUN_NOSH (no_interface,
763 no_interface_cmd,
764 "no interface IFNAME [vrf NAME]",
765 NO_STR
766 "Delete a pseudo interface's configuration\n"
767 "Interface's name\n"
768 VRF_CMD_HELP_STR)
769 {
770 const char *ifname = argv[2]->arg;
771 const char *vrfname = (argc > 3) ? argv[3]->arg : NULL;
772
773 // deleting interface
774 struct interface *ifp;
775 vrf_id_t vrf_id = VRF_DEFAULT;
776
777 if (argc > 3)
778 VRF_GET_ID (vrf_id, vrfname);
779
780 ifp = if_lookup_by_name_vrf (ifname, vrf_id);
781
782 if (ifp == NULL)
783 {
784 vty_out (vty, "%% Interface %s does not exist%s", ifname, VTY_NEWLINE);
785 return CMD_WARNING;
786 }
787
788 if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
789 {
790 vty_out (vty, "%% Only inactive interfaces can be deleted%s",
791 VTY_NEWLINE);
792 return CMD_WARNING;
793 }
794
795 if_delete(ifp);
796
797 return CMD_SUCCESS;
798 }
799
800 void
801 if_cmd_init (void)
802 {
803 install_element (CONFIG_NODE, &interface_cmd);
804 install_element (CONFIG_NODE, &no_interface_cmd);
805
806 install_default (INTERFACE_NODE);
807 install_element (INTERFACE_NODE, &interface_desc_cmd);
808 install_element (INTERFACE_NODE, &no_interface_desc_cmd);
809 }
810
811 #if 0
812 /* For debug purpose. */
813 DEFUN (show_address,
814 show_address_cmd,
815 "show address [vrf NAME]",
816 SHOW_STR
817 "address\n"
818 VRF_CMD_HELP_STR)
819 {
820 int idx_vrf = 3;
821 struct listnode *node;
822 struct listnode *node2;
823 struct interface *ifp;
824 struct connected *ifc;
825 struct prefix *p;
826 vrf_id_t vrf_id = VRF_DEFAULT;
827
828 if (argc > 2)
829 VRF_GET_ID (vrf_id, argv[idx_vrf]->arg);
830
831 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), 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 return CMD_SUCCESS;
843 }
844
845 DEFUN (show_address_vrf_all,
846 show_address_vrf_all_cmd,
847 "show address vrf all",
848 SHOW_STR
849 "address\n"
850 VRF_ALL_CMD_HELP_STR)
851 {
852 struct vrf *vrf;
853 struct listnode *node;
854 struct listnode *node2;
855 struct interface *ifp;
856 struct connected *ifc;
857 struct prefix *p;
858
859 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
860 {
861 if (!vrf->iflist || !listcount (vrf->iflist))
862 continue;
863
864 vty_out (vty, "%sVRF %u%s%s", VTY_NEWLINE, vrf->vrf_id, VTY_NEWLINE,
865 VTY_NEWLINE);
866
867 for (ALL_LIST_ELEMENTS_RO (vrf->iflist, node, ifp))
868 {
869 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
870 {
871 p = ifc->address;
872
873 if (p->family == AF_INET)
874 vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
875 VTY_NEWLINE);
876 }
877 }
878 }
879 return CMD_SUCCESS;
880 }
881 #endif
882
883 /* Allocate connected structure. */
884 struct connected *
885 connected_new (void)
886 {
887 return XCALLOC (MTYPE_CONNECTED, sizeof (struct connected));
888 }
889
890 /* Allocate nbr connected structure. */
891 struct nbr_connected *
892 nbr_connected_new (void)
893 {
894 return XCALLOC (MTYPE_NBR_CONNECTED, sizeof (struct nbr_connected));
895 }
896
897 /* Free connected structure. */
898 void
899 connected_free (struct connected *connected)
900 {
901 if (connected->address)
902 prefix_free (connected->address);
903
904 if (connected->destination)
905 prefix_free (connected->destination);
906
907 if (connected->label)
908 XFREE (MTYPE_CONNECTED_LABEL, connected->label);
909
910 XFREE (MTYPE_CONNECTED, connected);
911 }
912
913 /* Free nbr connected structure. */
914 void
915 nbr_connected_free (struct nbr_connected *connected)
916 {
917 if (connected->address)
918 prefix_free (connected->address);
919
920 XFREE (MTYPE_NBR_CONNECTED, connected);
921 }
922
923 /* If same interface nbr address already exists... */
924 struct nbr_connected *
925 nbr_connected_check (struct interface *ifp, struct prefix *p)
926 {
927 struct nbr_connected *ifc;
928 struct listnode *node;
929
930 for (ALL_LIST_ELEMENTS_RO (ifp->nbr_connected, node, ifc))
931 if (prefix_same (ifc->address, p))
932 return ifc;
933
934 return NULL;
935 }
936
937 /* Print if_addr structure. */
938 static void __attribute__ ((unused))
939 connected_log (struct connected *connected, char *str)
940 {
941 struct prefix *p;
942 struct interface *ifp;
943 char logbuf[BUFSIZ];
944 char buf[BUFSIZ];
945
946 ifp = connected->ifp;
947 p = connected->address;
948
949 snprintf (logbuf, BUFSIZ, "%s interface %s vrf %u %s %s/%d ",
950 str, ifp->name, ifp->vrf_id, prefix_family_str (p),
951 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
952 p->prefixlen);
953
954 p = connected->destination;
955 if (p)
956 {
957 strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
958 BUFSIZ - strlen(logbuf));
959 }
960 zlog_info("%s", logbuf);
961 }
962
963 /* Print if_addr structure. */
964 static void __attribute__ ((unused))
965 nbr_connected_log (struct nbr_connected *connected, char *str)
966 {
967 struct prefix *p;
968 struct interface *ifp;
969 char logbuf[BUFSIZ];
970 char buf[BUFSIZ];
971
972 ifp = connected->ifp;
973 p = connected->address;
974
975 snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ",
976 str, ifp->name, prefix_family_str (p),
977 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
978 p->prefixlen);
979
980 zlog_info("%s", logbuf);
981 }
982
983 /* If two connected address has same prefix return 1. */
984 static int
985 connected_same_prefix (struct prefix *p1, struct prefix *p2)
986 {
987 if (p1->family == p2->family)
988 {
989 if (p1->family == AF_INET &&
990 IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4))
991 return 1;
992 if (p1->family == AF_INET6 &&
993 IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6))
994 return 1;
995 }
996 return 0;
997 }
998
999 struct connected *
1000 connected_lookup_prefix_exact (struct interface *ifp, struct prefix *p)
1001 {
1002 struct listnode *node;
1003 struct listnode *next;
1004 struct connected *ifc;
1005
1006 for (node = listhead (ifp->connected); node; node = next)
1007 {
1008 ifc = listgetdata (node);
1009 next = node->next;
1010
1011 if (connected_same_prefix (ifc->address, p))
1012 return ifc;
1013 }
1014 return NULL;
1015 }
1016
1017 struct connected *
1018 connected_delete_by_prefix (struct interface *ifp, struct prefix *p)
1019 {
1020 struct listnode *node;
1021 struct listnode *next;
1022 struct connected *ifc;
1023
1024 /* In case of same prefix come, replace it with new one. */
1025 for (node = listhead (ifp->connected); node; node = next)
1026 {
1027 ifc = listgetdata (node);
1028 next = node->next;
1029
1030 if (connected_same_prefix (ifc->address, p))
1031 {
1032 listnode_delete (ifp->connected, ifc);
1033 return ifc;
1034 }
1035 }
1036 return NULL;
1037 }
1038
1039 /* Find the address on our side that will be used when packets
1040 are sent to dst. */
1041 struct connected *
1042 connected_lookup_prefix (struct interface *ifp, struct prefix *addr)
1043 {
1044 struct listnode *cnode;
1045 struct connected *c;
1046 struct connected *match;
1047
1048 match = NULL;
1049
1050 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
1051 {
1052 if (c->address && (c->address->family == addr->family) &&
1053 prefix_match(CONNECTED_PREFIX(c), addr) &&
1054 (!match || (c->address->prefixlen > match->address->prefixlen)))
1055 match = c;
1056 }
1057 return match;
1058 }
1059
1060 struct connected *
1061 connected_add_by_prefix (struct interface *ifp, struct prefix *p,
1062 struct prefix *destination)
1063 {
1064 struct connected *ifc;
1065
1066 /* Allocate new connected address. */
1067 ifc = connected_new ();
1068 ifc->ifp = ifp;
1069
1070 /* Fetch interface address */
1071 ifc->address = prefix_new();
1072 memcpy (ifc->address, p, sizeof(struct prefix));
1073
1074 /* Fetch dest address */
1075 if (destination)
1076 {
1077 ifc->destination = prefix_new();
1078 memcpy (ifc->destination, destination, sizeof(struct prefix));
1079 }
1080
1081 /* Add connected address to the interface. */
1082 listnode_add (ifp->connected, ifc);
1083 return ifc;
1084 }
1085
1086 #if 0 /* this route_table of struct connected's is unused
1087 * however, it would be good to use a route_table rather than
1088 * a list..
1089 */
1090 /* Interface looking up by interface's address. */
1091 /* Interface's IPv4 address reverse lookup table. */
1092 struct route_table *ifaddr_ipv4_table;
1093 /* struct route_table *ifaddr_ipv6_table; */
1094
1095 static void
1096 ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
1097 {
1098 struct route_node *rn;
1099 struct prefix_ipv4 p;
1100
1101 p.family = AF_INET;
1102 p.prefixlen = IPV4_MAX_PREFIXLEN;
1103 p.prefix = *ifaddr;
1104
1105 rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
1106 if (rn)
1107 {
1108 route_unlock_node (rn);
1109 zlog_info ("ifaddr_ipv4_add(): address %s is already added",
1110 inet_ntoa (*ifaddr));
1111 return;
1112 }
1113 rn->info = ifp;
1114 }
1115
1116 static void
1117 ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
1118 {
1119 struct route_node *rn;
1120 struct prefix_ipv4 p;
1121
1122 p.family = AF_INET;
1123 p.prefixlen = IPV4_MAX_PREFIXLEN;
1124 p.prefix = *ifaddr;
1125
1126 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1127 if (! rn)
1128 {
1129 zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
1130 inet_ntoa (*ifaddr));
1131 return;
1132 }
1133 rn->info = NULL;
1134 route_unlock_node (rn);
1135 route_unlock_node (rn);
1136 }
1137
1138 /* Lookup interface by interface's IP address or interface index. */
1139 static struct interface *
1140 ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex)
1141 {
1142 struct prefix_ipv4 p;
1143 struct route_node *rn;
1144 struct interface *ifp;
1145
1146 if (addr)
1147 {
1148 p.family = AF_INET;
1149 p.prefixlen = IPV4_MAX_PREFIXLEN;
1150 p.prefix = *addr;
1151
1152 rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
1153 if (! rn)
1154 return NULL;
1155
1156 ifp = rn->info;
1157 route_unlock_node (rn);
1158 return ifp;
1159 }
1160 else
1161 return if_lookup_by_index(ifindex, VRF_DEFAULT);
1162 }
1163 #endif /* ifaddr_ipv4_table */
1164
1165 /* Initialize interface list. */
1166 void
1167 if_init (struct list **intf_list)
1168 {
1169 *intf_list = list_new ();
1170 #if 0
1171 ifaddr_ipv4_table = route_table_init ();
1172 #endif /* ifaddr_ipv4_table */
1173
1174 (*intf_list)->cmp = (int (*)(void *, void *))if_cmp_func;
1175 }
1176
1177 void
1178 if_terminate (struct list **intf_list)
1179 {
1180 for (;;)
1181 {
1182 struct interface *ifp;
1183
1184 ifp = listnode_head (*intf_list);
1185 if (ifp == NULL)
1186 break;
1187
1188 if (ifp->node)
1189 {
1190 ifp->node->info = NULL;
1191 route_unlock_node (ifp->node);
1192 }
1193
1194 if_delete (ifp);
1195 }
1196
1197 list_delete (*intf_list);
1198 *intf_list = NULL;
1199 }
1200
1201 const char *
1202 if_link_type_str (enum zebra_link_type llt)
1203 {
1204 switch (llt)
1205 {
1206 #define llts(T,S) case (T): return (S)
1207 llts(ZEBRA_LLT_UNKNOWN, "Unknown");
1208 llts(ZEBRA_LLT_ETHER, "Ethernet");
1209 llts(ZEBRA_LLT_EETHER, "Experimental Ethernet");
1210 llts(ZEBRA_LLT_AX25, "AX.25 Level 2");
1211 llts(ZEBRA_LLT_PRONET, "PROnet token ring");
1212 llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB");
1213 llts(ZEBRA_LLT_ARCNET, "ARCnet");
1214 llts(ZEBRA_LLT_APPLETLK, "AppleTalk");
1215 llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI");
1216 llts(ZEBRA_LLT_ATM, "ATM");
1217 llts(ZEBRA_LLT_METRICOM, "Metricom STRIP");
1218 llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4");
1219 llts(ZEBRA_LLT_EUI64, "EUI-64");
1220 llts(ZEBRA_LLT_INFINIBAND, "InfiniBand");
1221 llts(ZEBRA_LLT_SLIP, "SLIP");
1222 llts(ZEBRA_LLT_CSLIP, "Compressed SLIP");
1223 llts(ZEBRA_LLT_SLIP6, "SLIPv6");
1224 llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6");
1225 llts(ZEBRA_LLT_ROSE, "ROSE packet radio");
1226 llts(ZEBRA_LLT_X25, "CCITT X.25");
1227 llts(ZEBRA_LLT_PPP, "PPP");
1228 llts(ZEBRA_LLT_CHDLC, "Cisco HDLC");
1229 llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC");
1230 llts(ZEBRA_LLT_LAPB, "LAPB");
1231 llts(ZEBRA_LLT_IPIP, "IPIP Tunnel");
1232 llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel");
1233 llts(ZEBRA_LLT_FRAD, "FRAD");
1234 llts(ZEBRA_LLT_SKIP, "SKIP vif");
1235 llts(ZEBRA_LLT_LOOPBACK, "Loopback");
1236 llts(ZEBRA_LLT_LOCALTLK, "Localtalk");
1237 llts(ZEBRA_LLT_FDDI, "FDDI");
1238 llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT");
1239 llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel");
1240 llts(ZEBRA_LLT_IPGRE, "GRE over IP");
1241 llts(ZEBRA_LLT_PIMREG, "PIMSM registration");
1242 llts(ZEBRA_LLT_HIPPI, "HiPPI");
1243 llts(ZEBRA_LLT_IRDA, "IrDA");
1244 llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP");
1245 llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop");
1246 llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop");
1247 llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric");
1248 llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring");
1249 llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11");
1250 llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap");
1251 llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4");
1252 llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy");
1253 default:
1254 zlog_warn ("Unknown value %d", llt);
1255 return "Unknown type!";
1256 #undef llts
1257 }
1258 return NULL;
1259 }
1260
1261 struct if_link_params *
1262 if_link_params_get (struct interface *ifp)
1263 {
1264 int i;
1265
1266 if (ifp->link_params != NULL)
1267 return ifp->link_params;
1268
1269 struct if_link_params *iflp = XCALLOC(MTYPE_IF_LINK_PARAMS,
1270 sizeof (struct if_link_params));
1271 if (iflp == NULL) return NULL;
1272
1273 /* Set TE metric equal to standard metric */
1274 iflp->te_metric = ifp->metric;
1275
1276 /* Compute default bandwidth based on interface */
1277 iflp->default_bw = ((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH)
1278 * TE_KILO_BIT / TE_BYTE);
1279
1280 /* Set Max, Reservable and Unreserved Bandwidth */
1281 iflp->max_bw = iflp->default_bw;
1282 iflp->max_rsv_bw = iflp->default_bw;
1283 for (i = 0; i < MAX_CLASS_TYPE; i++)
1284 iflp->unrsv_bw[i] = iflp->default_bw;
1285
1286 /* Update Link parameters status */
1287 iflp->lp_status = LP_TE_METRIC | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW;
1288
1289 /* Finally attach newly created Link Parameters */
1290 ifp->link_params = iflp;
1291
1292 return iflp;
1293 }
1294
1295 void
1296 if_link_params_free (struct interface *ifp)
1297 {
1298 if (ifp->link_params == NULL) return;
1299 XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params);
1300 ifp->link_params = NULL;
1301 }