operstates.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. These interface flags can also be queried without netlink using the
  32. SIOCGIFFLAGS ioctl.
  33. TLV IFLA_OPERSTATE
  34. contains RFC2863 state of the interface in numeric representation:
  35. IF_OPER_UNKNOWN (0):
  36. Interface is in unknown state, neither driver nor userspace has set
  37. operational state. Interface must be considered for user data as
  38. setting operational state has not been implemented in every driver.
  39. IF_OPER_NOTPRESENT (1):
  40. Unused in current kernel (notpresent interfaces normally disappear),
  41. just a numerical placeholder.
  42. IF_OPER_DOWN (2):
  43. Interface is unable to transfer data on L1, f.e. ethernet is not
  44. plugged or interface is ADMIN down.
  45. IF_OPER_LOWERLAYERDOWN (3):
  46. Interfaces stacked on an interface that is IF_OPER_DOWN show this
  47. state (f.e. VLAN).
  48. IF_OPER_TESTING (4):
  49. Unused in current kernel.
  50. IF_OPER_DORMANT (5):
  51. Interface is L1 up, but waiting for an external event, f.e. for a
  52. protocol to establish. (802.1X)
  53. IF_OPER_UP (6):
  54. Interface is operational up and can be used.
  55. This TLV can also be queried via sysfs.
  56. TLV IFLA_LINKMODE
  57. contains link policy. This is needed for userspace interaction
  58. described below.
  59. This TLV can also be queried via sysfs.
  60. 3. Kernel driver API
  61. Kernel drivers have access to two flags that map to IFF_LOWER_UP and
  62. IFF_DORMANT. These flags can be set from everywhere, even from
  63. interrupts. It is guaranteed that only the driver has write access,
  64. however, if different layers of the driver manipulate the same flag,
  65. the driver has to provide the synchronisation needed.
  66. __LINK_STATE_NOCARRIER, maps to !IFF_LOWER_UP:
  67. The driver uses netif_carrier_on() to clear and netif_carrier_off() to
  68. set this flag. On netif_carrier_off(), the scheduler stops sending
  69. packets. The name 'carrier' and the inversion are historical, think of
  70. it as lower layer.
  71. netif_carrier_ok() can be used to query that bit.
  72. __LINK_STATE_DORMANT, maps to IFF_DORMANT:
  73. Set by the driver to express that the device cannot yet be used
  74. because some driver controlled protocol establishment has to
  75. complete. Corresponding functions are netif_dormant_on() to set the
  76. flag, netif_dormant_off() to clear it and netif_dormant() to query.
  77. On device allocation, networking core sets the flags equivalent to
  78. netif_carrier_ok() and !netif_dormant().
  79. Whenever the driver CHANGES one of these flags, a workqueue event is
  80. scheduled to translate the flag combination to IFLA_OPERSTATE as
  81. follows:
  82. !netif_carrier_ok():
  83. IF_OPER_LOWERLAYERDOWN if the interface is stacked, IF_OPER_DOWN
  84. otherwise. Kernel can recognise stacked interfaces because their
  85. ifindex != iflink.
  86. netif_carrier_ok() && netif_dormant():
  87. IF_OPER_DORMANT
  88. netif_carrier_ok() && !netif_dormant():
  89. IF_OPER_UP if userspace interaction is disabled. Otherwise
  90. IF_OPER_DORMANT with the possibility for userspace to initiate the
  91. IF_OPER_UP transition afterwards.
  92. 4. Setting from userspace
  93. Applications have to use the netlink interface to influence the
  94. RFC2863 operational state of an interface. Setting IFLA_LINKMODE to 1
  95. via RTM_SETLINK instructs the kernel that an interface should go to
  96. IF_OPER_DORMANT instead of IF_OPER_UP when the combination
  97. netif_carrier_ok() && !netif_dormant() is set by the
  98. driver. Afterwards, the userspace application can set IFLA_OPERSTATE
  99. to IF_OPER_DORMANT or IF_OPER_UP as long as the driver does not set
  100. netif_carrier_off() or netif_dormant_on(). Changes made by userspace
  101. are multicasted on the netlink group RTMGRP_LINK.
  102. So basically a 802.1X supplicant interacts with the kernel like this:
  103. -subscribe to RTMGRP_LINK
  104. -set IFLA_LINKMODE to 1 via RTM_SETLINK
  105. -query RTM_GETLINK once to get initial state
  106. -if initial flags are not (IFF_LOWER_UP && !IFF_DORMANT), wait until
  107. netlink multicast signals this state
  108. -do 802.1X, eventually abort if flags go down again
  109. -send RTM_SETLINK to set operstate to IF_OPER_UP if authentication
  110. succeeds, IF_OPER_DORMANT otherwise
  111. -see how operstate and IFF_RUNNING is echoed via netlink multicast
  112. -set interface back to IF_OPER_DORMANT if 802.1X reauthentication
  113. fails
  114. -restart if kernel changes IFF_LOWER_UP or IFF_DORMANT flag
  115. if supplicant goes down, bring back IFLA_LINKMODE to 0 and
  116. IFLA_OPERSTATE to a sane value.
  117. A routing daemon or dhcp client just needs to care for IFF_RUNNING or
  118. waiting for operstate to go IF_OPER_UP/IF_OPER_UNKNOWN before
  119. considering the interface / querying a DHCP address.
  120. For technical questions and/or comments please e-mail to Stefan Rompf
  121. (stefan at loplof.de).