operstates.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. Interface is in testing mode, for example executing driver self-tests
  56. or media (cable) test. It can't be used for normal traffic until tests
  57. complete.
  58. IF_OPER_DORMANT (5):
  59. Interface is L1 up, but waiting for an external event, f.e. for a
  60. protocol to establish. (802.1X)
  61. IF_OPER_UP (6):
  62. Interface is operational up and can be used.
  63. This TLV can also be queried via sysfs.
  64. TLV IFLA_LINKMODE
  65. -----------------
  66. contains link policy. This is needed for userspace interaction
  67. described below.
  68. This TLV can also be queried via sysfs.
  69. 3. Kernel driver API
  70. ====================
  71. Kernel drivers have access to two flags that map to IFF_LOWER_UP and
  72. IFF_DORMANT. These flags can be set from everywhere, even from
  73. interrupts. It is guaranteed that only the driver has write access,
  74. however, if different layers of the driver manipulate the same flag,
  75. the driver has to provide the synchronisation needed.
  76. __LINK_STATE_NOCARRIER, maps to !IFF_LOWER_UP:
  77. The driver uses netif_carrier_on() to clear and netif_carrier_off() to
  78. set this flag. On netif_carrier_off(), the scheduler stops sending
  79. packets. The name 'carrier' and the inversion are historical, think of
  80. it as lower layer.
  81. Note that for certain kind of soft-devices, which are not managing any
  82. real hardware, it is possible to set this bit from userspace. One
  83. should use TLV IFLA_CARRIER to do so.
  84. netif_carrier_ok() can be used to query that bit.
  85. __LINK_STATE_DORMANT, maps to IFF_DORMANT:
  86. Set by the driver to express that the device cannot yet be used
  87. because some driver controlled protocol establishment has to
  88. complete. Corresponding functions are netif_dormant_on() to set the
  89. flag, netif_dormant_off() to clear it and netif_dormant() to query.
  90. On device allocation, both flags __LINK_STATE_NOCARRIER and
  91. __LINK_STATE_DORMANT are cleared, so the effective state is equivalent
  92. to netif_carrier_ok() and !netif_dormant().
  93. Whenever the driver CHANGES one of these flags, a workqueue event is
  94. scheduled to translate the flag combination to IFLA_OPERSTATE as
  95. follows:
  96. !netif_carrier_ok():
  97. IF_OPER_LOWERLAYERDOWN if the interface is stacked, IF_OPER_DOWN
  98. otherwise. Kernel can recognise stacked interfaces because their
  99. ifindex != iflink.
  100. netif_carrier_ok() && netif_dormant():
  101. IF_OPER_DORMANT
  102. netif_carrier_ok() && !netif_dormant():
  103. IF_OPER_UP if userspace interaction is disabled. Otherwise
  104. IF_OPER_DORMANT with the possibility for userspace to initiate the
  105. IF_OPER_UP transition afterwards.
  106. 4. Setting from userspace
  107. =========================
  108. Applications have to use the netlink interface to influence the
  109. RFC2863 operational state of an interface. Setting IFLA_LINKMODE to 1
  110. via RTM_SETLINK instructs the kernel that an interface should go to
  111. IF_OPER_DORMANT instead of IF_OPER_UP when the combination
  112. netif_carrier_ok() && !netif_dormant() is set by the
  113. driver. Afterwards, the userspace application can set IFLA_OPERSTATE
  114. to IF_OPER_DORMANT or IF_OPER_UP as long as the driver does not set
  115. netif_carrier_off() or netif_dormant_on(). Changes made by userspace
  116. are multicasted on the netlink group RTNLGRP_LINK.
  117. So basically a 802.1X supplicant interacts with the kernel like this:
  118. - subscribe to RTNLGRP_LINK
  119. - set IFLA_LINKMODE to 1 via RTM_SETLINK
  120. - query RTM_GETLINK once to get initial state
  121. - if initial flags are not (IFF_LOWER_UP && !IFF_DORMANT), wait until
  122. netlink multicast signals this state
  123. - do 802.1X, eventually abort if flags go down again
  124. - send RTM_SETLINK to set operstate to IF_OPER_UP if authentication
  125. succeeds, IF_OPER_DORMANT otherwise
  126. - see how operstate and IFF_RUNNING is echoed via netlink multicast
  127. - set interface back to IF_OPER_DORMANT if 802.1X reauthentication
  128. fails
  129. - restart if kernel changes IFF_LOWER_UP or IFF_DORMANT flag
  130. if supplicant goes down, bring back IFLA_LINKMODE to 0 and
  131. IFLA_OPERSTATE to a sane value.
  132. A routing daemon or dhcp client just needs to care for IFF_RUNNING or
  133. waiting for operstate to go IF_OPER_UP/IF_OPER_UNKNOWN before
  134. considering the interface / querying a DHCP address.
  135. For technical questions and/or comments please e-mail to Stefan Rompf
  136. (stefan at loplof.de).