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.
diff --git a/lib/if.h b/lib/if.h
index f5efda6..df9ff60 100644
--- a/lib/if.h
+++ b/lib/if.h
@@ -36,10 +36,6 @@
 #define INTERFACE_NAMSIZ      20
 #define INTERFACE_HWADDR_MAX  20
 
-/* Internal If indexes start at 0xFFFFFFFF and go down to 1 greater
-   than this */
-#define IFINDEX_INTERNBASE 0x80000000
-
 #ifdef HAVE_PROC_NET_DEV
 struct if_stats
 {
@@ -75,11 +71,19 @@
 /* Interface structure */
 struct interface 
 {
-  /* Interface name. */
+  /* Interface name.  This should probably never be changed after the
+     interface is created, because the configuration info for this interface
+     is associated with this structure.  For that reason, the interface
+     should also never be deleted (to avoid losing configuration info).
+     To delete, just set ifindex to IFINDEX_INTERNAL to indicate that the
+     interface does not exist in the kernel.
+   */
   char name[INTERFACE_NAMSIZ + 1];
 
-  /* Interface index. */
+  /* Interface index (should be IFINDEX_INTERNAL for non-kernel or
+     deleted interfaces). */
   unsigned int ifindex;
+#define IFINDEX_INTERNAL	0
 
   /* Zebra internal interface status */
   u_char status;
@@ -208,14 +212,22 @@
 
 /* Prototypes. */
 int if_cmp_func (struct interface *, struct interface *);
-struct interface *if_new (void);
 struct interface *if_create (const char *name, int namelen);
 struct interface *if_lookup_by_index (unsigned int);
 struct interface *if_lookup_by_name (const char *);
 struct interface *if_lookup_exact_address (struct in_addr);
 struct interface *if_lookup_address (struct in_addr);
 struct interface *if_get_by_name (const char *);
-void if_delete (struct interface *);
+
+/* Delete the interface, but do not free the structure, and leave it in the
+   interface list.  It is often advisable to leave the pseudo interface 
+   structure because there may be configuration information attached. */
+extern void if_delete_retain (struct interface *);
+
+/* Delete and free the interface structure: calls if_delete_retain and then
+   deletes it from the interface list and frees the structure. */
+extern void if_delete (struct interface *);
+
 int if_is_up (struct interface *);
 int if_is_running (struct interface *);
 int if_is_operative (struct interface *);
@@ -226,9 +238,18 @@
 void if_add_hook (int, int (*)(struct interface *));
 void if_init ();
 void if_dump_all ();
-char *ifindex2ifname (unsigned int);
 extern const char *if_flag_dump(unsigned long);
 
+/* Please use ifindex2ifname instead of if_indextoname where possible;
+   ifindex2ifname uses internal interface info, whereas if_indextoname must
+   make a system call. */
+extern char *ifindex2ifname (unsigned int);
+
+/* Please use ifname2ifindex instead of if_nametoindex where possible;
+   ifname2ifindex uses internal interface info, whereas if_nametoindex must
+   make a system call. */
+extern unsigned int ifname2ifindex(const char *ifname);
+
 /* Connected address functions. */
 struct connected *connected_new ();
 void connected_free (struct connected *);