[NET] core: add RFC2863 operstate
authorStefan Rompf <stefan@loplof.de>
Tue, 21 Mar 2006 01:09:11 +0000 (17:09 -0800)
committerDavid S. Miller <davem@davemloft.net>
Tue, 21 Mar 2006 01:09:11 +0000 (17:09 -0800)
this patch adds a dormant flag to network devices, RFC2863 operstate derived
from these flags and possibility for userspace interaction. It allows drivers
to signal that a device is unusable for user traffic without disabling
queueing (and therefore the possibility for protocol establishment traffic to
flow) and a userspace supplicant (WPA, 802.1X) to mark a device unusable
without changes to the driver.

It is the result of our long discussion. However I must admit that it
represents what Jamal and I agreed on with compromises towards Krzysztof, but
Thomas and Krzysztof still disagree with some parts. Anyway I think it should
be applied.

Signed-off-by: Stefan Rompf <stefan@loplof.de>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/if.h
include/linux/netdevice.h
include/linux/rtnetlink.h
net/core/dev.c
net/core/link_watch.c
net/core/net-sysfs.c
net/core/rtnetlink.c

index 12c6f6d157c324782ec6c31c78887bf927942eed..374e20ad8b0d02f15fbcaa5315e272eabd6c4f76 100644 (file)
@@ -33,7 +33,7 @@
 #define        IFF_LOOPBACK    0x8             /* is a loopback net            */
 #define        IFF_POINTOPOINT 0x10            /* interface is has p-p link    */
 #define        IFF_NOTRAILERS  0x20            /* avoid use of trailers        */
-#define        IFF_RUNNING     0x40            /* interface running and carrier ok */
+#define        IFF_RUNNING     0x40            /* interface RFC2863 OPER_UP    */
 #define        IFF_NOARP       0x80            /* no ARP protocol              */
 #define        IFF_PROMISC     0x100           /* receive all packets          */
 #define        IFF_ALLMULTI    0x200           /* receive all multicast packets*/
 
 #define IFF_MULTICAST  0x1000          /* Supports multicast           */
 
-#define IFF_VOLATILE   (IFF_LOOPBACK|IFF_POINTOPOINT|IFF_BROADCAST|IFF_MASTER|IFF_SLAVE|IFF_RUNNING)
-
 #define IFF_PORTSEL    0x2000          /* can set media type           */
 #define IFF_AUTOMEDIA  0x4000          /* auto media select active     */
 #define IFF_DYNAMIC    0x8000          /* dialup device with changing addresses*/
 
+#define IFF_LOWER_UP   0x10000         /* driver signals L1 up         */
+#define IFF_DORMANT    0x20000         /* driver signals dormant       */
+
+#define IFF_VOLATILE   (IFF_LOOPBACK|IFF_POINTOPOINT|IFF_BROADCAST|\
+               IFF_MASTER|IFF_SLAVE|IFF_RUNNING|IFF_LOWER_UP|IFF_DORMANT)
+
 /* Private (from user) interface flags (netdevice->priv_flags). */
 #define IFF_802_1Q_VLAN 0x1             /* 802.1Q VLAN device.          */
 #define IFF_EBRIDGE    0x2             /* Ethernet bridging device.    */
 #define IF_PROTO_FR_ETH_PVC 0x200B
 #define IF_PROTO_RAW    0x200C          /* RAW Socket                   */
 
+/* RFC 2863 operational status */
+enum {
+       IF_OPER_UNKNOWN,
+       IF_OPER_NOTPRESENT,
+       IF_OPER_DOWN,
+       IF_OPER_LOWERLAYERDOWN,
+       IF_OPER_TESTING,
+       IF_OPER_DORMANT,
+       IF_OPER_UP,
+};
+
+/* link modes */
+enum {
+       IF_LINK_MODE_DEFAULT,
+       IF_LINK_MODE_DORMANT,   /* limit upward transition to dormant */
+};
 
 /*
  *     Device mapping structure. I'd just gone off and designed a 
index 7fda03d338d1c40a76e2acd6d7a4c80d4910b198..b825be201bce0ee3504d0e2ae54e69fad77c005e 100644 (file)
@@ -230,7 +230,8 @@ enum netdev_state_t
        __LINK_STATE_SCHED,
        __LINK_STATE_NOCARRIER,
        __LINK_STATE_RX_SCHED,
-       __LINK_STATE_LINKWATCH_PENDING
+       __LINK_STATE_LINKWATCH_PENDING,
+       __LINK_STATE_DORMANT,
 };
 
 
@@ -335,11 +336,14 @@ struct net_device
         */
 
 
-       unsigned short          flags;  /* interface flags (a la BSD)   */
+       unsigned int            flags;  /* interface flags (a la BSD)   */
        unsigned short          gflags;
         unsigned short          priv_flags; /* Like 'flags' but invisible to userspace. */
        unsigned short          padded; /* How much padding added by alloc_netdev() */
 
+       unsigned char           operstate; /* RFC2863 operstate */
+       unsigned char           link_mode; /* mapping policy to operstate */
+
        unsigned                mtu;    /* interface MTU value          */
        unsigned short          type;   /* interface hardware type      */
        unsigned short          hard_header_len;        /* hardware hdr length  */
@@ -714,6 +718,10 @@ static inline void dev_put(struct net_device *dev)
 /* Carrier loss detection, dial on demand. The functions netif_carrier_on
  * and _off may be called from IRQ context, but it is caller
  * who is responsible for serialization of these calls.
+ *
+ * The name carrier is inappropriate, these functions should really be
+ * called netif_lowerlayer_*() because they represent the state of any
+ * kind of lower layer not just hardware media.
  */
 
 extern void linkwatch_fire_event(struct net_device *dev);
@@ -729,6 +737,29 @@ extern void netif_carrier_on(struct net_device *dev);
 
 extern void netif_carrier_off(struct net_device *dev);
 
+static inline void netif_dormant_on(struct net_device *dev)
+{
+       if (!test_and_set_bit(__LINK_STATE_DORMANT, &dev->state))
+               linkwatch_fire_event(dev);
+}
+
+static inline void netif_dormant_off(struct net_device *dev)
+{
+       if (test_and_clear_bit(__LINK_STATE_DORMANT, &dev->state))
+               linkwatch_fire_event(dev);
+}
+
+static inline int netif_dormant(const struct net_device *dev)
+{
+       return test_bit(__LINK_STATE_DORMANT, &dev->state);
+}
+
+
+static inline int netif_oper_up(const struct net_device *dev) {
+       return (dev->operstate == IF_OPER_UP ||
+               dev->operstate == IF_OPER_UNKNOWN /* backward compat */);
+}
+
 /* Hot-plugging. */
 static inline int netif_device_present(struct net_device *dev)
 {
index d50482ba27fe17dab4678df7c3e8aa586130955c..edccefb451882a628214c8b6e6e700504368b4a1 100644 (file)
@@ -733,6 +733,8 @@ enum
 #define IFLA_MAP IFLA_MAP
        IFLA_WEIGHT,
 #define IFLA_WEIGHT IFLA_WEIGHT
+       IFLA_OPERSTATE,
+       IFLA_LINKMODE,
        __IFLA_MAX
 };
 
index ef56c035d44e5dae7d448c6b27966dc62a5930f1..8763c99fcb84738a6035163db103f308fa969d20 100644 (file)
@@ -2174,12 +2174,20 @@ unsigned dev_get_flags(const struct net_device *dev)
 
        flags = (dev->flags & ~(IFF_PROMISC |
                                IFF_ALLMULTI |
-                               IFF_RUNNING)) | 
+                               IFF_RUNNING |
+                               IFF_LOWER_UP |
+                               IFF_DORMANT)) |
                (dev->gflags & (IFF_PROMISC |
                                IFF_ALLMULTI));
 
-       if (netif_running(dev) && netif_carrier_ok(dev))
-               flags |= IFF_RUNNING;
+       if (netif_running(dev)) {
+               if (netif_oper_up(dev))
+                       flags |= IFF_RUNNING;
+               if (netif_carrier_ok(dev))
+                       flags |= IFF_LOWER_UP;
+               if (netif_dormant(dev))
+                       flags |= IFF_DORMANT;
+       }
 
        return flags;
 }
index d43d1201275c19bf7aaf5a45126586d01b0f3960..e82a451d330b88d59cc8c0eeff3d0edde77dcef8 100644 (file)
@@ -49,6 +49,45 @@ struct lw_event {
 /* Avoid kmalloc() for most systems */
 static struct lw_event singleevent;
 
+static unsigned char default_operstate(const struct net_device *dev)
+{
+       if (!netif_carrier_ok(dev))
+               return (dev->ifindex != dev->iflink ?
+                       IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN);
+
+       if (netif_dormant(dev))
+               return IF_OPER_DORMANT;
+
+       return IF_OPER_UP;
+}
+
+
+static void rfc2863_policy(struct net_device *dev)
+{
+       unsigned char operstate = default_operstate(dev);
+
+       if (operstate == dev->operstate)
+               return;
+
+       write_lock_bh(&dev_base_lock);
+
+       switch(dev->link_mode) {
+       case IF_LINK_MODE_DORMANT:
+               if (operstate == IF_OPER_UP)
+                       operstate = IF_OPER_DORMANT;
+               break;
+
+       case IF_LINK_MODE_DEFAULT:
+       default:
+               break;
+       };
+
+       dev->operstate = operstate;
+
+       write_unlock_bh(&dev_base_lock);
+}
+
+
 /* Must be called with the rtnl semaphore held */
 void linkwatch_run_queue(void)
 {
@@ -74,6 +113,7 @@ void linkwatch_run_queue(void)
                 */
                clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
 
+               rfc2863_policy(dev);
                if (dev->flags & IFF_UP) {
                        if (netif_carrier_ok(dev)) {
                                WARN_ON(dev->qdisc_sleeping == &noop_qdisc);
index e8b2acbc8ea2b0473415c938db3f45e3b9233304..21b68464cabbd38530f14fee95dd875a71ad877e 100644 (file)
@@ -91,6 +91,7 @@ NETDEVICE_SHOW(iflink, fmt_dec);
 NETDEVICE_SHOW(ifindex, fmt_dec);
 NETDEVICE_SHOW(features, fmt_long_hex);
 NETDEVICE_SHOW(type, fmt_dec);
+NETDEVICE_SHOW(link_mode, fmt_dec);
 
 /* use same locking rules as GIFHWADDR ioctl's */
 static ssize_t format_addr(char *buf, const unsigned char *addr, int len)
@@ -133,6 +134,43 @@ static ssize_t show_carrier(struct class_device *dev, char *buf)
        return -EINVAL;
 }
 
+static ssize_t show_dormant(struct class_device *dev, char *buf)
+{
+       struct net_device *netdev = to_net_dev(dev);
+
+       if (netif_running(netdev))
+               return sprintf(buf, fmt_dec, !!netif_dormant(netdev));
+
+       return -EINVAL;
+}
+
+static const char *operstates[] = {
+       "unknown",
+       "notpresent", /* currently unused */
+       "down",
+       "lowerlayerdown",
+       "testing", /* currently unused */
+       "dormant",
+       "up"
+};
+
+static ssize_t show_operstate(struct class_device *dev, char *buf)
+{
+       const struct net_device *netdev = to_net_dev(dev);
+       unsigned char operstate;
+
+       read_lock(&dev_base_lock);
+       operstate = netdev->operstate;
+       if (!netif_running(netdev))
+               operstate = IF_OPER_DOWN;
+       read_unlock(&dev_base_lock);
+
+       if (operstate >= sizeof(operstates))
+               return -EINVAL; /* should not happen */
+
+       return sprintf(buf, "%s\n", operstates[operstate]);
+}
+
 /* read-write attributes */
 NETDEVICE_SHOW(mtu, fmt_dec);
 
@@ -190,9 +228,12 @@ static struct class_device_attribute net_class_attributes[] = {
        __ATTR(ifindex, S_IRUGO, show_ifindex, NULL),
        __ATTR(features, S_IRUGO, show_features, NULL),
        __ATTR(type, S_IRUGO, show_type, NULL),
+       __ATTR(link_mode, S_IRUGO, show_link_mode, NULL),
        __ATTR(address, S_IRUGO, show_address, NULL),
        __ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
        __ATTR(carrier, S_IRUGO, show_carrier, NULL),
+       __ATTR(dormant, S_IRUGO, show_dormant, NULL),
+       __ATTR(operstate, S_IRUGO, show_operstate, NULL),
        __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
        __ATTR(flags, S_IRUGO | S_IWUSR, show_flags, store_flags),
        __ATTR(tx_queue_len, S_IRUGO | S_IWUSR, show_tx_queue_len,
index eca2976abb25b5c2b727191b72c8f5dc861c8285..1c15a907066ff26f1e960771d39d70ea6ab7c6c8 100644 (file)
@@ -179,6 +179,33 @@ rtattr_failure:
 }
 
 
+static void set_operstate(struct net_device *dev, unsigned char transition)
+{
+       unsigned char operstate = dev->operstate;
+
+       switch(transition) {
+       case IF_OPER_UP:
+               if ((operstate == IF_OPER_DORMANT ||
+                    operstate == IF_OPER_UNKNOWN) &&
+                   !netif_dormant(dev))
+                       operstate = IF_OPER_UP;
+               break;
+
+       case IF_OPER_DORMANT:
+               if (operstate == IF_OPER_UP ||
+                   operstate == IF_OPER_UNKNOWN)
+                       operstate = IF_OPER_DORMANT;
+               break;
+       };
+
+       if (dev->operstate != operstate) {
+               write_lock_bh(&dev_base_lock);
+               dev->operstate = operstate;
+               write_unlock_bh(&dev_base_lock);
+               netdev_state_change(dev);
+       }
+}
+
 static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
                                 int type, u32 pid, u32 seq, u32 change, 
                                 unsigned int flags)
@@ -208,6 +235,13 @@ static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
                RTA_PUT(skb, IFLA_WEIGHT, sizeof(weight), &weight);
        }
 
+       if (1) {
+               u8 operstate = netif_running(dev)?dev->operstate:IF_OPER_DOWN;
+               u8 link_mode = dev->link_mode;
+               RTA_PUT(skb, IFLA_OPERSTATE, sizeof(operstate), &operstate);
+               RTA_PUT(skb, IFLA_LINKMODE, sizeof(link_mode), &link_mode);
+       }
+
        if (1) {
                struct rtnl_link_ifmap map = {
                        .mem_start   = dev->mem_start,
@@ -399,6 +433,22 @@ static int do_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
                dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1]));
        }
 
+       if (ida[IFLA_OPERSTATE - 1]) {
+               if (ida[IFLA_OPERSTATE - 1]->rta_len != RTA_LENGTH(sizeof(u8)))
+                       goto out;
+
+               set_operstate(dev, *((u8 *) RTA_DATA(ida[IFLA_OPERSTATE - 1])));
+       }
+
+       if (ida[IFLA_LINKMODE - 1]) {
+               if (ida[IFLA_LINKMODE - 1]->rta_len != RTA_LENGTH(sizeof(u8)))
+                       goto out;
+
+               write_lock_bh(&dev_base_lock);
+               dev->link_mode = *((u8 *) RTA_DATA(ida[IFLA_LINKMODE - 1]));
+               write_unlock_bh(&dev_base_lock);
+       }
+
        if (ifm->ifi_index >= 0 && ida[IFLA_IFNAME - 1]) {
                char ifname[IFNAMSIZ];