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