operstates.rst 6.4 KB

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