operstates.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. 1. Introduction
  2. Linux distinguishes between administrative and operational state of an
  3. interface. Administrative state is the result of "ip link set dev
  4. <dev> up or down" and reflects whether the administrator wants to use
  5. the device for traffic.
  6. However, an interface is not usable just because the admin enabled it
  7. - ethernet requires to be plugged into the switch and, depending on
  8. a site's networking policy and configuration, an 802.1X authentication
  9. to be performed before user data can be transferred. Operational state
  10. shows the ability of an interface to transmit this user data.
  11. Thanks to 802.1X, userspace must be granted the possibility to
  12. influence operational state. To accommodate this, operational state is
  13. split into two parts: Two flags that can be set by the driver only, and
  14. a RFC2863 compatible state that is derived from these flags, a policy,
  15. and changeable from userspace under certain rules.
  16. 2. Querying from userspace
  17. Both admin and operational state can be queried via the netlink
  18. operation RTM_GETLINK. It is also possible to subscribe to RTMGRP_LINK
  19. to be notified of updates. This is important for setting from userspace.
  20. These values contain interface state:
  21. ifinfomsg::if_flags & IFF_UP:
  22. Interface is admin up
  23. ifinfomsg::if_flags & IFF_RUNNING:
  24. Interface is in RFC2863 operational state UP or UNKNOWN. This is for
  25. backward compatibility, routing daemons, dhcp clients can use this
  26. flag to determine whether they should use the interface.
  27. ifinfomsg::if_flags & IFF_LOWER_UP:
  28. Driver has signaled netif_carrier_on()
  29. ifinfomsg::if_flags & IFF_DORMANT:
  30. Driver has signaled netif_dormant_on()
  31. TLV IFLA_OPERSTATE
  32. contains RFC2863 state of the interface in numeric representation:
  33. IF_OPER_UNKNOWN (0):
  34. Interface is in unknown state, neither driver nor userspace has set
  35. operational state. Interface must be considered for user data as
  36. setting operational state has not been implemented in every driver.
  37. IF_OPER_NOTPRESENT (1):
  38. Unused in current kernel (notpresent interfaces normally disappear),
  39. just a numerical placeholder.
  40. IF_OPER_DOWN (2):
  41. Interface is unable to transfer data on L1, f.e. ethernet is not
  42. plugged or interface is ADMIN down.
  43. IF_OPER_LOWERLAYERDOWN (3):
  44. Interfaces stacked on an interface that is IF_OPER_DOWN show this
  45. state (f.e. VLAN).
  46. IF_OPER_TESTING (4):
  47. Unused in current kernel.
  48. IF_OPER_DORMANT (5):
  49. Interface is L1 up, but waiting for an external event, f.e. for a
  50. protocol to establish. (802.1X)
  51. IF_OPER_UP (6):
  52. Interface is operational up and can be used.
  53. This TLV can also be queried via sysfs.
  54. TLV IFLA_LINKMODE
  55. contains link policy. This is needed for userspace interaction
  56. described below.
  57. This TLV can also be queried via sysfs.
  58. 3. Kernel driver API
  59. Kernel drivers have access to two flags that map to IFF_LOWER_UP and
  60. IFF_DORMANT. These flags can be set from everywhere, even from
  61. interrupts. It is guaranteed that only the driver has write access,
  62. however, if different layers of the driver manipulate the same flag,
  63. the driver has to provide the synchronisation needed.
  64. __LINK_STATE_NOCARRIER, maps to !IFF_LOWER_UP:
  65. The driver uses netif_carrier_on() to clear and netif_carrier_off() to
  66. set this flag. On netif_carrier_off(), the scheduler stops sending
  67. packets. The name 'carrier' and the inversion are historical, think of
  68. it as lower layer.
  69. Note that for certain kind of soft-devices, which are not managing any
  70. real hardware, it is possible to set this bit from userspace. One
  71. should use TVL IFLA_CARRIER to do so.
  72. netif_carrier_ok() can be used to query that bit.
  73. __LINK_STATE_DORMANT, maps to IFF_DORMANT:
  74. Set by the driver to express that the device cannot yet be used
  75. because some driver controlled protocol establishment has to
  76. complete. Corresponding functions are netif_dormant_on() to set the
  77. flag, netif_dormant_off() to clear it and netif_dormant() to query.
  78. On device allocation, networking core sets the flags equivalent to
  79. netif_carrier_ok() and !netif_dormant().
  80. Whenever the driver CHANGES one of these flags, a workqueue event is
  81. scheduled to translate the flag combination to IFLA_OPERSTATE as
  82. follows:
  83. !netif_carrier_ok():
  84. IF_OPER_LOWERLAYERDOWN if the interface is stacked, IF_OPER_DOWN
  85. otherwise. Kernel can recognise stacked interfaces because their
  86. ifindex != iflink.
  87. netif_carrier_ok() && netif_dormant():
  88. IF_OPER_DORMANT
  89. netif_carrier_ok() && !netif_dormant():
  90. IF_OPER_UP if userspace interaction is disabled. Otherwise
  91. IF_OPER_DORMANT with the possibility for userspace to initiate the
  92. IF_OPER_UP transition afterwards.
  93. 4. Setting from userspace
  94. Applications have to use the netlink interface to influence the
  95. RFC2863 operational state of an interface. Setting IFLA_LINKMODE to 1
  96. via RTM_SETLINK instructs the kernel that an interface should go to
  97. IF_OPER_DORMANT instead of IF_OPER_UP when the combination
  98. netif_carrier_ok() && !netif_dormant() is set by the
  99. driver. Afterwards, the userspace application can set IFLA_OPERSTATE
  100. to IF_OPER_DORMANT or IF_OPER_UP as long as the driver does not set
  101. netif_carrier_off() or netif_dormant_on(). Changes made by userspace
  102. are multicasted on the netlink group RTMGRP_LINK.
  103. So basically a 802.1X supplicant interacts with the kernel like this:
  104. -subscribe to RTMGRP_LINK
  105. -set IFLA_LINKMODE to 1 via RTM_SETLINK
  106. -query RTM_GETLINK once to get initial state
  107. -if initial flags are not (IFF_LOWER_UP && !IFF_DORMANT), wait until
  108. netlink multicast signals this state
  109. -do 802.1X, eventually abort if flags go down again
  110. -send RTM_SETLINK to set operstate to IF_OPER_UP if authentication
  111. succeeds, IF_OPER_DORMANT otherwise
  112. -see how operstate and IFF_RUNNING is echoed via netlink multicast
  113. -set interface back to IF_OPER_DORMANT if 802.1X reauthentication
  114. fails
  115. -restart if kernel changes IFF_LOWER_UP or IFF_DORMANT flag
  116. if supplicant goes down, bring back IFLA_LINKMODE to 0 and
  117. IFLA_OPERSTATE to a sane value.
  118. A routing daemon or dhcp client just needs to care for IFF_RUNNING or
  119. waiting for operstate to go IF_OPER_UP/IF_OPER_UNKNOWN before
  120. considering the interface / querying a DHCP address.
  121. For technical questions and/or comments please e-mail to Stefan Rompf
  122. (stefan at loplof.de).