summaryrefslogtreecommitdiff
path: root/lib/if.c
diff options
context:
space:
mode:
authorajs <ajs>2005-04-02 18:38:43 +0000
committerajs <ajs>2005-04-02 18:38:43 +0000
commitd2fc88962a9a494ecb34167871bb9e7273a25d33 (patch)
tree304a1c1a744972bc10b1f677a134907c404b361d /lib/if.c
parent386e61502d391adb985ac1487825349ec85e6a8c (diff)
2005-04-02 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
Fix problems when netlink interfaces are renamed (same ifindex used for a new interface). Start cleaning up some problems with the way interface names are handled. * interface.c: (if_new_intern_ifindex) Remove obsolete function. (if_delete_update) After distributing the interface deletion message, set ifp->ifindex to IFINDEX_INTERNAL. (if_dump_vty) Detect pseudo interface by checking if ifp->ifindex is IFINDEX_INTERNAL. (zebra_interface) Check return code from interface_cmd.func. Do not set internal ifindex values to if_new_intern_ifindex(), since we now use IFINDEX_INTERNAL for all pseudo interfaces. * kernel_socket.c: (ifm_read) Fix code and comments to reflect that all internal interfaces now have ifp->ifindex set to IFINDEX_INTERNAL. * rt_netlink.c: (set_ifindex) New function used to update ifp->ifindex. Detects interface rename events by checking if that ifindex is already being used. If it is, delete the old interface before assigning the ifindex to the new interface. (netlink_interface, netlink_link_change) Call set_ifindex to update the ifindex. * if.h: Remove define for IFINDEX_INTERNBASE and add define IFINDEX_INTERNAL 0, since all internal (i.e. non-kernel) pseudo- interfaces should have ifindex set to 0. (if_new) Remove function. (if_delete_retain) New function to delete an interface without removing from iflist and freeing the structure. (ifname2ifindex) New function. * if.c: (if_new) Remove function (absorb into if_create). (if_create) Replace function if_new with call to calloc. Set ifp->ifindex to IFINDEX_INTERNAL. Fix off-by-one error in assert to check length of interface name. Add error message if interface with this name already exists. (if_delete_retain) New function to delete an interface without removing from iflist and freeing the structure. (if_delete) Implement with help of if_delete_retain. (ifindex2ifname) Reimplement using if_lookup_by_index. (ifname2ifindex) New function to complement ifindex2ifname. (interface) The interface command should check the name length and fail with a warning message if it is too long. (no_interface) Fix spelling in warning message. (if_nametoindex) Reimplement using if_lookup_by_name. (if_indextoname, ifaddr_ipv4_lookup) Reimplement using if_lookup_by_index. * bgp_zebra.c: (bgp_interface_delete) After deleting, set ifp->ifindex to IFINDEX_INTERNAL. * isis_zebra.c: (isis_zebra_if_del) Call if_delete_retain instead of if_delete, since it is generally not safe to remove interface structures. After deleting, set ifp->ifindex to IFINDEX_INTERNAL. (zebra_interface_if_lookup) Tighten up code. * ospf6_zebra.c: (ospf6_zebra_if_del) Previously, this whole function was commented out. But this is not safe: we should at least update the ifindex when the interface is deleted. So the new version updates the interface status and sets ifp->ifindex to IFINDEX_INTERNAL. (ospf6_zebra_route_update) Use if_indextoname properly. * ospf_vty.c: (show_ip_ospf_interface_sub) Show ifindex and interface flags to help with debugging. * ospf_zebra.c: (ospf_interface_delete) After deleting, set ifp->ifindex to IFINDEX_INTERNAL. (zebra_interface_if_lookup) Make function static. Tighten up code. * rip_interface.c: (rip_interface_delete) After deleting, set ifp->ifindex to IFINDEX_INTERNAL. * ripng_interface.c: (ripng_interface_delete) After deleting, set ifp->ifindex to IFINDEX_INTERNAL.
Diffstat (limited to 'lib/if.c')
-rw-r--r--lib/if.c117
1 files changed, 53 insertions, 64 deletions
diff --git a/lib/if.c b/lib/if.c
index 7385ff6e..e35e3ed2 100644
--- a/lib/if.c
+++ b/lib/if.c
@@ -113,28 +113,22 @@ if_cmp_func (struct interface *ifp1, struct interface *ifp2)
/* Create new interface structure. */
struct interface *
-if_new ()
-{
- struct interface *ifp;
-
- ifp = XMALLOC (MTYPE_IF, sizeof (struct interface));
- memset (ifp, 0, sizeof (struct interface));
- return ifp;
-}
-
-struct interface *
if_create (const char *name, int namelen)
{
struct interface *ifp;
- ifp = if_new ();
+ ifp = XCALLOC (MTYPE_IF, sizeof (struct interface));
+ ifp->ifindex = IFINDEX_INTERNAL;
assert (name);
- assert (namelen <= (INTERFACE_NAMSIZ + 1));
+ assert (namelen <= INTERFACE_NAMSIZ); /* Need space for '\0' at end. */
strncpy (ifp->name, name, namelen);
- ifp->name[INTERFACE_NAMSIZ] = '\0';
+ ifp->name[namelen] = '\0';
if (if_lookup_by_name(ifp->name) == NULL)
listnode_add_sort (iflist, ifp);
+ else
+ zlog_err("if_create(%s): corruption detected -- interface with this "
+ "name exists already!", ifp->name);
ifp->connected = list_new ();
ifp->connected->del = (void (*) (void *)) connected_free;
@@ -144,17 +138,24 @@ if_create (const char *name, int namelen)
return ifp;
}
-/* Delete and free interface structure. */
+/* Delete interface structure. */
void
-if_delete (struct interface *ifp)
+if_delete_retain (struct interface *ifp)
{
- listnode_delete (iflist, ifp);
-
if (if_master.if_delete_hook)
(*if_master.if_delete_hook) (ifp);
/* Free connected address list */
list_delete (ifp->connected);
+}
+
+/* Delete and free interface structure. */
+void
+if_delete (struct interface *ifp)
+{
+ listnode_delete (iflist, ifp);
+
+ if_delete_retain(ifp);
XFREE (MTYPE_IF, ifp);
}
@@ -194,16 +195,18 @@ if_lookup_by_index (unsigned int index)
char *
ifindex2ifname (unsigned int index)
{
- struct listnode *node;
struct interface *ifp;
- for (node = listhead (iflist); node; nextnode (node))
- {
- ifp = getdata (node);
- if (ifp->ifindex == index)
- return ifp->name;
- }
- return (char *) "unknown";
+ return ((ifp = if_lookup_by_index(index)) != NULL) ?
+ ifp->name : (char *)"unknown";
+}
+
+unsigned int
+ifname2ifindex (const char *name)
+{
+ struct interface *ifp;
+
+ return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp->ifindex : 0;
}
/* Interface existance check by interface name. */
@@ -491,11 +494,20 @@ DEFUN (interface,
"Interface's name\n")
{
struct interface *ifp;
+ size_t sl;
+
+ if ((sl = strlen(argv[0])) > INTERFACE_NAMSIZ)
+ {
+ vty_out (vty, "%% Interface name %s is invalid: length exceeds "
+ "%d characters%s",
+ argv[0], INTERFACE_NAMSIZ, VTY_NEWLINE);
+ return CMD_WARNING;
+ }
ifp = if_lookup_by_name (argv[0]);
if (ifp == NULL)
- ifp = if_create (argv[0], INTERFACE_NAMSIZ);
+ ifp = if_create (argv[0], sl);
vty->index = ifp;
vty->node = INTERFACE_NODE;
@@ -515,17 +527,17 @@ DEFUN_NOSH (no_interface,
ifp = if_lookup_by_name (argv[0]);
if (ifp == NULL)
- {
- vty_out (vty, "%% Inteface %s does not exist%s", argv[0], VTY_NEWLINE);
- return CMD_WARNING;
- }
+ {
+ vty_out (vty, "%% Interface %s does not exist%s", argv[0], VTY_NEWLINE);
+ return CMD_WARNING;
+ }
if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
- {
- vty_out (vty, "%% Only inactive interfaces can be deleted%s",
- VTY_NEWLINE);
- return CMD_WARNING;
- }
+ {
+ vty_out (vty, "%% Only inactive interfaces can be deleted%s",
+ VTY_NEWLINE);
+ return CMD_WARNING;
+ }
if_delete(ifp);
@@ -726,16 +738,9 @@ connected_add_by_prefix (struct interface *ifp, struct prefix *p,
unsigned int
if_nametoindex (const char *name)
{
- struct listnode *node;
struct interface *ifp;
- for (node = listhead (iflist); node; nextnode (node))
- {
- ifp = getdata (node);
- if (strcmp (ifp->name, name) == 0)
- return ifp->ifindex;
- }
- return 0;
+ return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp->ifindex : 0;
}
#endif
@@ -743,19 +748,12 @@ if_nametoindex (const char *name)
char *
if_indextoname (unsigned int ifindex, char *name)
{
- struct listnode *node;
struct interface *ifp;
- for (node = listhead (iflist); node; nextnode (node))
- {
- ifp = getdata (node);
- if (ifp->ifindex == ifindex)
- {
- memcpy (name, ifp->name, IFNAMSIZ);
- return ifp->name;
- }
- }
- return NULL;
+ if (!(ifp = if_lookup_by_index(ifindex)))
+ return NULL;
+ strncpy (name, ifp->name, IFNAMSIZ);
+ return ifp->name;
}
#endif
@@ -832,16 +830,7 @@ ifaddr_ipv4_lookup (struct in_addr *addr, unsigned int ifindex)
return ifp;
}
else
- {
- for (node = listhead (iflist); node; nextnode (node))
- {
- ifp = getdata (node);
-
- if (ifp->ifindex == ifindex)
- return ifp;
- }
- }
- return NULL;
+ return if_lookup_by_index(ifindex);
}
/* Initialize interface list. */