xref: /linux/Documentation/networking/xfrm/xfrm_migrate_state.rst (revision c13c0cc6f52e491186f6521dc80031c35737162d)
1*c13c0cc6SAntony Antony.. SPDX-License-Identifier: GPL-2.0
2*c13c0cc6SAntony Antony
3*c13c0cc6SAntony Antony=====================
4*c13c0cc6SAntony AntonyXFRM SA Migrate State
5*c13c0cc6SAntony Antony=====================
6*c13c0cc6SAntony Antony
7*c13c0cc6SAntony AntonyOverview
8*c13c0cc6SAntony Antony========
9*c13c0cc6SAntony Antony
10*c13c0cc6SAntony Antony``XFRM_MSG_MIGRATE_STATE`` migrates a single SA, looked up using SPI and
11*c13c0cc6SAntony Antonymark, without involving policies. Unlike ``XFRM_MSG_MIGRATE``, which couples
12*c13c0cc6SAntony AntonySA and policy migration and allows migrating multiple SAs in one call, this
13*c13c0cc6SAntony Antonyinterface identifies the SA unambiguously via SPI and supports changing
14*c13c0cc6SAntony Antonythe reqid, addresses, encapsulation, selector, and offload.
15*c13c0cc6SAntony Antony
16*c13c0cc6SAntony AntonyBecause IKE daemons can manage policies independently of
17*c13c0cc6SAntony Antonythe kernel, this interface allows precise per-SA migration without
18*c13c0cc6SAntony Antonyrequiring policy involvement. Optional netlink attributes follow an
19*c13c0cc6SAntony Antonyomit-to-inherit model: omitting an attribute preserves the value from
20*c13c0cc6SAntony Antonythe old SA. The ``flags`` field controls two exceptions: hardware offload
21*c13c0cc6SAntony Antonyis inherited by default and can be suppressed with
22*c13c0cc6SAntony Antony``XFRM_MIGRATE_STATE_CLEAR_OFFLOAD`` or overridden with ``XFRMA_OFFLOAD_DEV``;
23*c13c0cc6SAntony Antonythe new selector is taken from ``new_sel`` by default and can instead be
24*c13c0cc6SAntony Antonyderived from the new addresses with ``XFRM_MIGRATE_STATE_UPDATE_H2H_SEL``.
25*c13c0cc6SAntony Antony
26*c13c0cc6SAntony AntonySA Identification
27*c13c0cc6SAntony Antony=================
28*c13c0cc6SAntony Antony
29*c13c0cc6SAntony AntonyThe struct is defined in ``include/uapi/linux/xfrm.h``. The SA is looked
30*c13c0cc6SAntony Antonyup using ``xfrm_state_lookup()`` with ``id.spi``,
31*c13c0cc6SAntony Antony``id.daddr``, ``id.proto``, ``id.family``, and
32*c13c0cc6SAntony Antony``old_mark.v & old_mark.m`` as the mark key::
33*c13c0cc6SAntony Antony
34*c13c0cc6SAntony Antony    struct xfrm_user_migrate_state {
35*c13c0cc6SAntony Antony        struct xfrm_usersa_id  id;       /* spi, daddr, proto, family */
36*c13c0cc6SAntony Antony        xfrm_address_t         new_daddr;
37*c13c0cc6SAntony Antony        xfrm_address_t         new_saddr;
38*c13c0cc6SAntony Antony        struct xfrm_mark       old_mark; /* SA lookup: key = v & m */
39*c13c0cc6SAntony Antony        struct xfrm_selector   new_sel;  /* new selector (see Flags) */
40*c13c0cc6SAntony Antony        __u32                  new_reqid;
41*c13c0cc6SAntony Antony        __u32                  flags;    /* XFRM_MIGRATE_STATE_* */
42*c13c0cc6SAntony Antony        __u16                  new_family;
43*c13c0cc6SAntony Antony        __u16                  reserved;  /* must be zero */
44*c13c0cc6SAntony Antony    };
45*c13c0cc6SAntony Antony
46*c13c0cc6SAntony AntonyThe ``reserved`` field must be set to zero; the kernel rejects any
47*c13c0cc6SAntony Antonyother value with ``-EINVAL``.
48*c13c0cc6SAntony Antony
49*c13c0cc6SAntony AntonySupported Attributes
50*c13c0cc6SAntony Antony====================
51*c13c0cc6SAntony Antony
52*c13c0cc6SAntony AntonyThe following fields in ``xfrm_user_migrate_state`` are always explicit
53*c13c0cc6SAntony Antonyand are not inherited from the existing SA. Passing zero is not equivalent
54*c13c0cc6SAntony Antonyto "keep unchanged" — zero is used as-is:
55*c13c0cc6SAntony Antony
56*c13c0cc6SAntony Antony- ``new_daddr`` - new destination address
57*c13c0cc6SAntony Antony- ``new_saddr`` - new source address
58*c13c0cc6SAntony Antony- ``new_family`` - new address family
59*c13c0cc6SAntony Antony- ``new_reqid`` - new reqid (0 = no reqid)
60*c13c0cc6SAntony Antony- ``new_sel`` - new selector; used when ``XFRM_MIGRATE_STATE_UPDATE_H2H_SEL`` is
61*c13c0cc6SAntony Antony  not set (see `Flags`_ below)
62*c13c0cc6SAntony Antony- ``flags`` - bitmask of ``XFRM_MIGRATE_STATE_*`` flags (see `Flags`_ below)
63*c13c0cc6SAntony Antony
64*c13c0cc6SAntony AntonyThe following netlink attributes are also accepted. Omitting an attribute
65*c13c0cc6SAntony Antonyinherits the value from the existing SA (omit-to-inherit).
66*c13c0cc6SAntony Antony
67*c13c0cc6SAntony Antony.. list-table::
68*c13c0cc6SAntony Antony   :widths: 30 70
69*c13c0cc6SAntony Antony   :header-rows: 1
70*c13c0cc6SAntony Antony
71*c13c0cc6SAntony Antony   * - Attribute
72*c13c0cc6SAntony Antony     - Description
73*c13c0cc6SAntony Antony   * - ``XFRMA_MARK``
74*c13c0cc6SAntony Antony     - Mark on the migrated SA (``struct xfrm_mark``). Absent inherits
75*c13c0cc6SAntony Antony       ``old_mark``. To use no mark on the new SA, send ``XFRMA_MARK``
76*c13c0cc6SAntony Antony       with ``{0, 0}``.
77*c13c0cc6SAntony Antony   * - ``XFRMA_ENCAP``
78*c13c0cc6SAntony Antony     - UDP encapsulation template; only ``UDP_ENCAP_ESPINUDP`` is supported.
79*c13c0cc6SAntony Antony       Set ``encap_type=0`` to remove encap.
80*c13c0cc6SAntony Antony   * - ``XFRMA_OFFLOAD_DEV``
81*c13c0cc6SAntony Antony     - Hardware offload configuration (``struct xfrm_user_offload``). Absent
82*c13c0cc6SAntony Antony       copies offload from the existing SA. When
83*c13c0cc6SAntony Antony       ``XFRM_MIGRATE_STATE_CLEAR_OFFLOAD`` is set in ``flags``, the new SA has
84*c13c0cc6SAntony Antony       no offload; this flag is mutually exclusive with ``XFRMA_OFFLOAD_DEV``
85*c13c0cc6SAntony Antony       and sending both returns ``-EINVAL``.
86*c13c0cc6SAntony Antony   * - ``XFRMA_SET_MARK``
87*c13c0cc6SAntony Antony     - Output mark on the migrated SA; pair with ``XFRMA_SET_MARK_MASK``.
88*c13c0cc6SAntony Antony       Send 0 to clear.
89*c13c0cc6SAntony Antony   * - ``XFRMA_NAT_KEEPALIVE_INTERVAL``
90*c13c0cc6SAntony Antony     - NAT keepalive interval in seconds. Requires encap. Send 0 to clear.
91*c13c0cc6SAntony Antony       Automatically cleared when encap is removed; setting a non-zero
92*c13c0cc6SAntony Antony       value without encap returns ``-EINVAL``.
93*c13c0cc6SAntony Antony   * - ``XFRMA_MTIMER_THRESH``
94*c13c0cc6SAntony Antony     - Mapping maxage threshold. Only valid on input SAs; setting on an
95*c13c0cc6SAntony Antony       output SA returns ``-EINVAL``. Requires encap. Send 0 to clear.
96*c13c0cc6SAntony Antony       Automatically cleared when encap is removed; setting a non-zero
97*c13c0cc6SAntony Antony       value without encap returns ``-EINVAL``.
98*c13c0cc6SAntony Antony
99*c13c0cc6SAntony AntonyThe following SA properties are immutable and cannot be changed via
100*c13c0cc6SAntony Antony``XFRM_MSG_MIGRATE_STATE``: algorithms (``XFRMA_ALG_*``), replay state,
101*c13c0cc6SAntony Antonydirection (``XFRMA_SA_DIR``), and security context (``XFRMA_SEC_CTX``).
102*c13c0cc6SAntony Antony
103*c13c0cc6SAntony AntonyFlags
104*c13c0cc6SAntony Antony=====
105*c13c0cc6SAntony Antony
106*c13c0cc6SAntony AntonyThe ``flags`` field in ``xfrm_user_migrate_state`` controls optional
107*c13c0cc6SAntony Antonymigration behaviour. Unknown flag bits are rejected with ``-EINVAL``; the
108*c13c0cc6SAntony Antonyextended ACK message identifies the unrecognised bits (e.g. ``"Unknown flags:
109*c13c0cc6SAntony Antony0x4"``). Userspace can use ``XFRM_MIGRATE_STATE_KNOWN_FLAGS`` (defined in
110*c13c0cc6SAntony Antony``<linux/xfrm.h>``) to validate flags before sending; note that this constant
111*c13c0cc6SAntony Antonyreflects the flags known to the header version userspace was compiled against,
112*c13c0cc6SAntony Antonywhich may differ from what the running kernel accepts.
113*c13c0cc6SAntony Antony
114*c13c0cc6SAntony Antony.. list-table::
115*c13c0cc6SAntony Antony   :widths: 40 60
116*c13c0cc6SAntony Antony   :header-rows: 1
117*c13c0cc6SAntony Antony
118*c13c0cc6SAntony Antony   * - Flag
119*c13c0cc6SAntony Antony     - Description
120*c13c0cc6SAntony Antony   * - ``XFRM_MIGRATE_STATE_CLEAR_OFFLOAD``
121*c13c0cc6SAntony Antony     - When set, the new SA has no hardware offload even when
122*c13c0cc6SAntony Antony       ``XFRMA_OFFLOAD_DEV`` is absent. Without this flag, omitting
123*c13c0cc6SAntony Antony       ``XFRMA_OFFLOAD_DEV`` copies the existing offload to the new SA.
124*c13c0cc6SAntony Antony       Mutually exclusive with ``XFRMA_OFFLOAD_DEV``; sending both
125*c13c0cc6SAntony Antony       returns ``-EINVAL``.
126*c13c0cc6SAntony Antony   * - ``XFRM_MIGRATE_STATE_UPDATE_H2H_SEL``
127*c13c0cc6SAntony Antony     - When set, the kernel validates that the existing SA selector is a
128*c13c0cc6SAntony Antony       single-host entry matching the SA addresses (``prefixlen_s ==
129*c13c0cc6SAntony Antony       prefixlen_d`` equal to 32 for IPv4 or 128 for IPv6, and addresses
130*c13c0cc6SAntony Antony       matching ``id.daddr`` and ``props.saddr``). If the check passes,
131*c13c0cc6SAntony Antony       the new selector is derived from ``new_daddr`` and ``new_saddr``
132*c13c0cc6SAntony Antony       with the single-host mask for ``new_family``. A mismatch returns
133*c13c0cc6SAntony Antony       ``-EINVAL``. When this flag is not set, ``new_sel`` is used as-is
134*c13c0cc6SAntony Antony       for the migrated SA.
135*c13c0cc6SAntony Antony
136*c13c0cc6SAntony AntonyMigration Steps
137*c13c0cc6SAntony Antony===============
138*c13c0cc6SAntony Antony
139*c13c0cc6SAntony AntonyOutgoing SA
140*c13c0cc6SAntony Antony-----------
141*c13c0cc6SAntony Antony
142*c13c0cc6SAntony AntonyTo prevent cleartext traffic leaks, install a block policy before
143*c13c0cc6SAntony Antonymigrating:
144*c13c0cc6SAntony Antony
145*c13c0cc6SAntony Antony#. Install a block policy to drop traffic on the affected selector.
146*c13c0cc6SAntony Antony#. Remove the old policy.
147*c13c0cc6SAntony Antony#. Call ``XFRM_MSG_MIGRATE_STATE`` for each SA.
148*c13c0cc6SAntony Antony#. Reinstall the policies.
149*c13c0cc6SAntony Antony#. Remove the block policy.
150*c13c0cc6SAntony Antony
151*c13c0cc6SAntony AntonyIf AES-GCM is in use, the block policy also prevents IV reuse during
152*c13c0cc6SAntony Antonythe migration window. For other AEADs this step is not required for
153*c13c0cc6SAntony AntonyIV safety, but skipping it allows a brief cleartext window.
154*c13c0cc6SAntony Antony
155*c13c0cc6SAntony AntonyIncoming SA
156*c13c0cc6SAntony Antony-----------
157*c13c0cc6SAntony Antony
158*c13c0cc6SAntony AntonyNo block policy is needed. ``XFRM_MSG_MIGRATE_STATE`` atomically
159*c13c0cc6SAntony Antonytransfers the sequence number and replay window from the old SA to
160*c13c0cc6SAntony Antonythe new SA, so the new SA continues replay protection without a gap.
161*c13c0cc6SAntony AntonyCall ``XFRM_MSG_MIGRATE_STATE`` for each SA directly.
162*c13c0cc6SAntony Antony
163*c13c0cc6SAntony AntonyWhen accepting incoming traffic, be liberal during the migration
164*c13c0cc6SAntony Antonywindow: packets sent by the remote peer before it completed its own
165*c13c0cc6SAntony Antonymigration may arrive out of order or slightly late. Dropping them
166*c13c0cc6SAntony Antonyunnecessarily causes packet loss. A generous replay window reduces
167*c13c0cc6SAntony Antonythe impact of reordering during migration.
168*c13c0cc6SAntony Antony
169*c13c0cc6SAntony AntonyBlock Policy and IV Safety
170*c13c0cc6SAntony Antony--------------------------
171*c13c0cc6SAntony Antony
172*c13c0cc6SAntony AntonyAES-GCM IV uniqueness is critical: reusing a (key, IV) pair allows
173*c13c0cc6SAntony Antonyan attacker to recover the authentication subkey and forge
174*c13c0cc6SAntony Antonyauthentication tags, breaking both confidentiality and integrity.
175*c13c0cc6SAntony AntonyThis concern applies to outgoing SAs only — the remote peer controls
176*c13c0cc6SAntony AntonyIV generation on incoming traffic.
177*c13c0cc6SAntony Antony
178*c13c0cc6SAntony Antony``XFRM_MSG_MIGRATE_STATE`` atomically copies the sequence number and
179*c13c0cc6SAntony Antonyreplay window from the old SA to the new SA and deletes the old SA.
180*c13c0cc6SAntony AntonyThe block policy serves two purposes: it prevents cleartext traffic
181*c13c0cc6SAntony Antonyleaks during the migration window, and for AES-GCM it prevents IV
182*c13c0cc6SAntony Antonyreuse by ensuring no outgoing packets are sent under the same key.
183*c13c0cc6SAntony AntonyThe atomic copy of the sequence number and replay window complements
184*c13c0cc6SAntony Antonythis — together they eliminate both risks during migration.
185*c13c0cc6SAntony AntonyThe atomic copy also serves incoming SAs, ensuring replay protection
186*c13c0cc6SAntony Antonycontinues without a gap across the migration.
187*c13c0cc6SAntony Antony
188*c13c0cc6SAntony AntonyFeature Detection
189*c13c0cc6SAntony Antony=================
190*c13c0cc6SAntony Antony
191*c13c0cc6SAntony AntonyUserspace can probe for kernel support by sending a minimal
192*c13c0cc6SAntony Antony``XFRM_MSG_MIGRATE_STATE`` message with a non-zero non-existent SPI:
193*c13c0cc6SAntony Antony
194*c13c0cc6SAntony Antony- ``-EINVAL``: kernel predates ``XFRM_MSG_MIGRATE_STATE``; message type
195*c13c0cc6SAntony Antony  is out of range
196*c13c0cc6SAntony Antony- ``-ENOPROTOOPT``: message type is known but ``CONFIG_XFRM_MIGRATE``
197*c13c0cc6SAntony Antony  is not enabled
198*c13c0cc6SAntony Antony- ``-ESRCH``: supported (SPI not found)
199*c13c0cc6SAntony Antony
200*c13c0cc6SAntony AntonyUserspace Notification on Success
201*c13c0cc6SAntony Antony=================================
202*c13c0cc6SAntony Antony
203*c13c0cc6SAntony AntonyOn successful migration the kernel multicasts an
204*c13c0cc6SAntony Antony``XFRM_MSG_MIGRATE_STATE`` message to the ``XFRMNLGRP_MIGRATE`` group.
205*c13c0cc6SAntony AntonyThe fixed header is ``struct xfrm_user_migrate_state`` copied from the
206*c13c0cc6SAntony Antonyrequest, followed by the same set of netlink attributes that are
207*c13c0cc6SAntony Antonyaccepted as input, with the differences noted below.
208*c13c0cc6SAntony Antony
209*c13c0cc6SAntony AntonyDifferences from the request
210*c13c0cc6SAntony Antony-----------------------------
211*c13c0cc6SAntony Antony
212*c13c0cc6SAntony Antony.. list-table::
213*c13c0cc6SAntony Antony   :widths: 25 75
214*c13c0cc6SAntony Antony   :header-rows: 1
215*c13c0cc6SAntony Antony
216*c13c0cc6SAntony Antony   * - Field / Attribute
217*c13c0cc6SAntony Antony     - Difference
218*c13c0cc6SAntony Antony   * - ``new_sel``
219*c13c0cc6SAntony Antony     - Contains the actual selector of the newly installed SA, not the
220*c13c0cc6SAntony Antony       ``new_sel`` from the request. When
221*c13c0cc6SAntony Antony       ``XFRM_MIGRATE_STATE_UPDATE_H2H_SEL`` is set the kernel derives the
222*c13c0cc6SAntony Antony       selector from ``new_daddr`` / ``new_saddr``; the caller's
223*c13c0cc6SAntony Antony       ``new_sel`` field is ignored in that case. The notification
224*c13c0cc6SAntony Antony       always carries the real selector of the new SA.
225*c13c0cc6SAntony Antony   * - ``XFRMA_SA_DIR``
226*c13c0cc6SAntony Antony     - Present in the notification (set from the direction of the new
227*c13c0cc6SAntony Antony       SA) but **not accepted as input** — direction is immutable.
228*c13c0cc6SAntony Antony   * - ``flags``
229*c13c0cc6SAntony Antony     - Echoed back as-is. ``XFRM_MIGRATE_STATE_CLEAR_OFFLOAD`` and
230*c13c0cc6SAntony Antony       ``XFRM_MIGRATE_STATE_UPDATE_H2H_SEL`` describe the request that was
231*c13c0cc6SAntony Antony       made, not a property of the resulting SA.
232*c13c0cc6SAntony Antony
233*c13c0cc6SAntony AntonyAttributes in the notification
234*c13c0cc6SAntony Antony-------------------------------
235*c13c0cc6SAntony Antony
236*c13c0cc6SAntony Antony.. list-table::
237*c13c0cc6SAntony Antony   :widths: 30 70
238*c13c0cc6SAntony Antony   :header-rows: 1
239*c13c0cc6SAntony Antony
240*c13c0cc6SAntony Antony   * - Attribute
241*c13c0cc6SAntony Antony     - Description
242*c13c0cc6SAntony Antony   * - ``XFRMA_ENCAP``
243*c13c0cc6SAntony Antony     - UDP encapsulation template, if configured on the new SA.
244*c13c0cc6SAntony Antony   * - ``XFRMA_OFFLOAD_DEV``
245*c13c0cc6SAntony Antony     - Hardware offload configuration, if active on the new SA.
246*c13c0cc6SAntony Antony   * - ``XFRMA_MARK``
247*c13c0cc6SAntony Antony     - Mark on the new SA, if set.
248*c13c0cc6SAntony Antony   * - ``XFRMA_SET_MARK``
249*c13c0cc6SAntony Antony     - Output mark on the new SA, if set.
250*c13c0cc6SAntony Antony   * - ``XFRMA_SET_MARK_MASK``
251*c13c0cc6SAntony Antony     - Output mark mask, present together with ``XFRMA_SET_MARK``.
252*c13c0cc6SAntony Antony   * - ``XFRMA_MTIMER_THRESH``
253*c13c0cc6SAntony Antony     - Mapping maxage threshold, if non-zero.
254*c13c0cc6SAntony Antony   * - ``XFRMA_NAT_KEEPALIVE_INTERVAL``
255*c13c0cc6SAntony Antony     - NAT keepalive interval, if non-zero.
256*c13c0cc6SAntony Antony   * - ``XFRMA_SA_DIR``
257*c13c0cc6SAntony Antony     - Direction of the new SA.
258*c13c0cc6SAntony Antony
259*c13c0cc6SAntony AntonyError Handling
260*c13c0cc6SAntony Antony==============
261*c13c0cc6SAntony Antony
262*c13c0cc6SAntony AntonyIf the target SA tuple (new daddr, SPI, proto, new family) is already
263*c13c0cc6SAntony Antonyoccupied, the operation returns ``-EEXIST`` before the migration begins.
264*c13c0cc6SAntony AntonyThe old SA remains intact and the operation is safe to retry after
265*c13c0cc6SAntony Antonyresolving the conflict.
266*c13c0cc6SAntony Antony
267*c13c0cc6SAntony AntonyIf the target SA is deleted before the migration completes, the operation
268*c13c0cc6SAntony Antonyreturns ``-ESRCH``. No new SA is installed. Userspace should verify the
269*c13c0cc6SAntony Antonycurrent SA state before retrying.
270*c13c0cc6SAntony Antony
271*c13c0cc6SAntony AntonyIf the multicast notification (``XFRMNLGRP_MIGRATE``) fails to send,
272*c13c0cc6SAntony Antonythe migration itself has already completed successfully and the new SA
273*c13c0cc6SAntony Antonyis installed. The operation returns success, 0, with an extack warning,
274*c13c0cc6SAntony Antonybut listeners will not receive the migration event.
275