xref: /linux/Documentation/networking/xfrm/xfrm_sync.rst (revision cc4adab164b772a34b3340d644b7c4728498581e)
1*03e23b18SBagas Sanjaya.. SPDX-License-Identifier: GPL-2.0
2*03e23b18SBagas Sanjaya
3*03e23b18SBagas Sanjaya=========
4*03e23b18SBagas SanjayaXFRM sync
5*03e23b18SBagas Sanjaya=========
6*03e23b18SBagas Sanjaya
7*03e23b18SBagas SanjayaThe sync patches work is based on initial patches from
8*03e23b18SBagas SanjayaKrisztian <hidden@balabit.hu> and others and additional patches
9*03e23b18SBagas Sanjayafrom Jamal <hadi@cyberus.ca>.
10*03e23b18SBagas Sanjaya
11*03e23b18SBagas SanjayaThe end goal for syncing is to be able to insert attributes + generate
12*03e23b18SBagas Sanjayaevents so that the SA can be safely moved from one machine to another
13*03e23b18SBagas Sanjayafor HA purposes.
14*03e23b18SBagas SanjayaThe idea is to synchronize the SA so that the takeover machine can do
15*03e23b18SBagas Sanjayathe processing of the SA as accurate as possible if it has access to it.
16*03e23b18SBagas Sanjaya
17*03e23b18SBagas SanjayaWe already have the ability to generate SA add/del/upd events.
18*03e23b18SBagas SanjayaThese patches add ability to sync and have accurate lifetime byte (to
19*03e23b18SBagas Sanjayaensure proper decay of SAs) and replay counters to avoid replay attacks
20*03e23b18SBagas Sanjayawith as minimal loss at failover time.
21*03e23b18SBagas SanjayaThis way a backup stays as closely up-to-date as an active member.
22*03e23b18SBagas Sanjaya
23*03e23b18SBagas SanjayaBecause the above items change for every packet the SA receives,
24*03e23b18SBagas Sanjayait is possible for a lot of the events to be generated.
25*03e23b18SBagas SanjayaFor this reason, we also add a nagle-like algorithm to restrict
26*03e23b18SBagas Sanjayathe events. i.e we are going to set thresholds to say "let me
27*03e23b18SBagas Sanjayaknow if the replay sequence threshold is reached or 10 secs have passed"
28*03e23b18SBagas SanjayaThese thresholds are set system-wide via sysctls or can be updated
29*03e23b18SBagas Sanjayaper SA.
30*03e23b18SBagas Sanjaya
31*03e23b18SBagas SanjayaThe identified items that need to be synchronized are:
32*03e23b18SBagas Sanjaya- the lifetime byte counter
33*03e23b18SBagas Sanjayanote that: lifetime time limit is not important if you assume the failover
34*03e23b18SBagas Sanjayamachine is known ahead of time since the decay of the time countdown
35*03e23b18SBagas Sanjayais not driven by packet arrival.
36*03e23b18SBagas Sanjaya- the replay sequence for both inbound and outbound
37*03e23b18SBagas Sanjaya
38*03e23b18SBagas Sanjaya1) Message Structure
39*03e23b18SBagas Sanjaya--------------------
40*03e23b18SBagas Sanjaya
41*03e23b18SBagas Sanjayanlmsghdr:aevent_id:optional-TLVs.
42*03e23b18SBagas Sanjaya
43*03e23b18SBagas SanjayaThe netlink message types are:
44*03e23b18SBagas Sanjaya
45*03e23b18SBagas SanjayaXFRM_MSG_NEWAE and XFRM_MSG_GETAE.
46*03e23b18SBagas Sanjaya
47*03e23b18SBagas SanjayaA XFRM_MSG_GETAE does not have TLVs.
48*03e23b18SBagas Sanjaya
49*03e23b18SBagas SanjayaA XFRM_MSG_NEWAE will have at least two TLVs (as is
50*03e23b18SBagas Sanjayadiscussed further below).
51*03e23b18SBagas Sanjaya
52*03e23b18SBagas Sanjayaaevent_id structure looks like::
53*03e23b18SBagas Sanjaya
54*03e23b18SBagas Sanjaya   struct xfrm_aevent_id {
55*03e23b18SBagas Sanjaya	     struct xfrm_usersa_id           sa_id;
56*03e23b18SBagas Sanjaya	     xfrm_address_t                  saddr;
57*03e23b18SBagas Sanjaya	     __u32                           flags;
58*03e23b18SBagas Sanjaya	     __u32                           reqid;
59*03e23b18SBagas Sanjaya   };
60*03e23b18SBagas Sanjaya
61*03e23b18SBagas SanjayaThe unique SA is identified by the combination of xfrm_usersa_id,
62*03e23b18SBagas Sanjayareqid and saddr.
63*03e23b18SBagas Sanjaya
64*03e23b18SBagas Sanjayaflags are used to indicate different things. The possible
65*03e23b18SBagas Sanjayaflags are::
66*03e23b18SBagas Sanjaya
67*03e23b18SBagas Sanjaya	XFRM_AE_RTHR=1, /* replay threshold*/
68*03e23b18SBagas Sanjaya	XFRM_AE_RVAL=2, /* replay value */
69*03e23b18SBagas Sanjaya	XFRM_AE_LVAL=4, /* lifetime value */
70*03e23b18SBagas Sanjaya	XFRM_AE_ETHR=8, /* expiry timer threshold */
71*03e23b18SBagas Sanjaya	XFRM_AE_CR=16, /* Event cause is replay update */
72*03e23b18SBagas Sanjaya	XFRM_AE_CE=32, /* Event cause is timer expiry */
73*03e23b18SBagas Sanjaya	XFRM_AE_CU=64, /* Event cause is policy update */
74*03e23b18SBagas Sanjaya
75*03e23b18SBagas SanjayaHow these flags are used is dependent on the direction of the
76*03e23b18SBagas Sanjayamessage (kernel<->user) as well the cause (config, query or event).
77*03e23b18SBagas SanjayaThis is described below in the different messages.
78*03e23b18SBagas Sanjaya
79*03e23b18SBagas SanjayaThe pid will be set appropriately in netlink to recognize direction
80*03e23b18SBagas Sanjaya(0 to the kernel and pid = processid that created the event
81*03e23b18SBagas Sanjayawhen going from kernel to user space)
82*03e23b18SBagas Sanjaya
83*03e23b18SBagas SanjayaA program needs to subscribe to multicast group XFRMNLGRP_AEVENTS
84*03e23b18SBagas Sanjayato get notified of these events.
85*03e23b18SBagas Sanjaya
86*03e23b18SBagas Sanjaya2) TLVS reflect the different parameters
87*03e23b18SBagas Sanjaya----------------------------------------
88*03e23b18SBagas Sanjaya
89*03e23b18SBagas Sanjayaa) byte value (XFRMA_LTIME_VAL)
90*03e23b18SBagas Sanjaya
91*03e23b18SBagas Sanjaya   This TLV carries the running/current counter for byte lifetime since
92*03e23b18SBagas Sanjaya   last event.
93*03e23b18SBagas Sanjaya
94*03e23b18SBagas Sanjayab) replay value (XFRMA_REPLAY_VAL)
95*03e23b18SBagas Sanjaya
96*03e23b18SBagas Sanjaya   This TLV carries the running/current counter for replay sequence since
97*03e23b18SBagas Sanjaya   last event.
98*03e23b18SBagas Sanjaya
99*03e23b18SBagas Sanjayac) replay threshold (XFRMA_REPLAY_THRESH)
100*03e23b18SBagas Sanjaya
101*03e23b18SBagas Sanjaya   This TLV carries the threshold being used by the kernel to trigger events
102*03e23b18SBagas Sanjaya   when the replay sequence is exceeded.
103*03e23b18SBagas Sanjaya
104*03e23b18SBagas Sanjayad) expiry timer (XFRMA_ETIMER_THRESH)
105*03e23b18SBagas Sanjaya
106*03e23b18SBagas Sanjaya   This is a timer value in milliseconds which is used as the nagle
107*03e23b18SBagas Sanjaya   value to rate limit the events.
108*03e23b18SBagas Sanjaya
109*03e23b18SBagas Sanjaya3) Default configurations for the parameters
110*03e23b18SBagas Sanjaya--------------------------------------------
111*03e23b18SBagas Sanjaya
112*03e23b18SBagas SanjayaBy default these events should be turned off unless there is
113*03e23b18SBagas Sanjayaat least one listener registered to listen to the multicast
114*03e23b18SBagas Sanjayagroup XFRMNLGRP_AEVENTS.
115*03e23b18SBagas Sanjaya
116*03e23b18SBagas SanjayaPrograms installing SAs will need to specify the two thresholds, however,
117*03e23b18SBagas Sanjayain order to not change existing applications such as racoon
118*03e23b18SBagas Sanjayawe also provide default threshold values for these different parameters
119*03e23b18SBagas Sanjayain case they are not specified.
120*03e23b18SBagas Sanjaya
121*03e23b18SBagas Sanjayathe two sysctls/proc entries are:
122*03e23b18SBagas Sanjaya
123*03e23b18SBagas Sanjayaa) /proc/sys/net/core/sysctl_xfrm_aevent_etime
124*03e23b18SBagas Sanjaya
125*03e23b18SBagas Sanjaya   Used to provide default values for the XFRMA_ETIMER_THRESH in incremental
126*03e23b18SBagas Sanjaya   units of time of 100ms. The default is 10 (1 second)
127*03e23b18SBagas Sanjaya
128*03e23b18SBagas Sanjayab) /proc/sys/net/core/sysctl_xfrm_aevent_rseqth
129*03e23b18SBagas Sanjaya
130*03e23b18SBagas Sanjaya   Used to provide default values for XFRMA_REPLAY_THRESH parameter
131*03e23b18SBagas Sanjaya   in incremental packet count. The default is two packets.
132*03e23b18SBagas Sanjaya
133*03e23b18SBagas Sanjaya4) Message types
134*03e23b18SBagas Sanjaya----------------
135*03e23b18SBagas Sanjaya
136*03e23b18SBagas Sanjayaa) XFRM_MSG_GETAE issued by user-->kernel.
137*03e23b18SBagas Sanjaya   XFRM_MSG_GETAE does not carry any TLVs.
138*03e23b18SBagas Sanjaya
139*03e23b18SBagas Sanjaya   The response is a XFRM_MSG_NEWAE which is formatted based on what
140*03e23b18SBagas Sanjaya   XFRM_MSG_GETAE queried for.
141*03e23b18SBagas Sanjaya
142*03e23b18SBagas Sanjaya   The response will always have XFRMA_LTIME_VAL and XFRMA_REPLAY_VAL TLVs.
143*03e23b18SBagas Sanjaya
144*03e23b18SBagas Sanjaya     * if XFRM_AE_RTHR flag is set, then XFRMA_REPLAY_THRESH is also retrieved
145*03e23b18SBagas Sanjaya     * if XFRM_AE_ETHR flag is set, then XFRMA_ETIMER_THRESH is also retrieved
146*03e23b18SBagas Sanjaya
147*03e23b18SBagas Sanjayab) XFRM_MSG_NEWAE is issued by either user space to configure
148*03e23b18SBagas Sanjaya   or kernel to announce events or respond to a XFRM_MSG_GETAE.
149*03e23b18SBagas Sanjaya
150*03e23b18SBagas Sanjaya   i) user --> kernel to configure a specific SA.
151*03e23b18SBagas Sanjaya
152*03e23b18SBagas Sanjaya      any of the values or threshold parameters can be updated by passing the
153*03e23b18SBagas Sanjaya      appropriate TLV.
154*03e23b18SBagas Sanjaya
155*03e23b18SBagas Sanjaya      A response is issued back to the sender in user space to indicate success
156*03e23b18SBagas Sanjaya      or failure.
157*03e23b18SBagas Sanjaya
158*03e23b18SBagas Sanjaya      In the case of success, additionally an event with
159*03e23b18SBagas Sanjaya      XFRM_MSG_NEWAE is also issued to any listeners as described in iii).
160*03e23b18SBagas Sanjaya
161*03e23b18SBagas Sanjaya   ii) kernel->user direction as a response to XFRM_MSG_GETAE
162*03e23b18SBagas Sanjaya
163*03e23b18SBagas Sanjaya       The response will always have XFRMA_LTIME_VAL and XFRMA_REPLAY_VAL TLVs.
164*03e23b18SBagas Sanjaya
165*03e23b18SBagas Sanjaya       The threshold TLVs will be included if explicitly requested in
166*03e23b18SBagas Sanjaya       the XFRM_MSG_GETAE message.
167*03e23b18SBagas Sanjaya
168*03e23b18SBagas Sanjaya   iii) kernel->user to report as event if someone sets any values or
169*03e23b18SBagas Sanjaya        thresholds for an SA using XFRM_MSG_NEWAE (as described in #i above).
170*03e23b18SBagas Sanjaya        In such a case XFRM_AE_CU flag is set to inform the user that
171*03e23b18SBagas Sanjaya        the change happened as a result of an update.
172*03e23b18SBagas Sanjaya        The message will always have XFRMA_LTIME_VAL and XFRMA_REPLAY_VAL TLVs.
173*03e23b18SBagas Sanjaya
174*03e23b18SBagas Sanjaya   iv) kernel->user to report event when replay threshold or a timeout
175*03e23b18SBagas Sanjaya       is exceeded.
176*03e23b18SBagas Sanjaya
177*03e23b18SBagas SanjayaIn such a case either XFRM_AE_CR (replay exceeded) or XFRM_AE_CE (timeout
178*03e23b18SBagas Sanjayahappened) is set to inform the user what happened.
179*03e23b18SBagas SanjayaNote the two flags are mutually exclusive.
180*03e23b18SBagas SanjayaThe message will always have XFRMA_LTIME_VAL and XFRMA_REPLAY_VAL TLVs.
181*03e23b18SBagas Sanjaya
182*03e23b18SBagas Sanjaya5) Exceptions to threshold settings
183*03e23b18SBagas Sanjaya-----------------------------------
184*03e23b18SBagas Sanjaya
185*03e23b18SBagas SanjayaIf you have an SA that is getting hit by traffic in bursts such that
186*03e23b18SBagas Sanjayathere is a period where the timer threshold expires with no packets
187*03e23b18SBagas Sanjayaseen, then an odd behavior is seen as follows:
188*03e23b18SBagas SanjayaThe first packet arrival after a timer expiry will trigger a timeout
189*03e23b18SBagas Sanjayaevent; i.e we don't wait for a timeout period or a packet threshold
190*03e23b18SBagas Sanjayato be reached. This is done for simplicity and efficiency reasons.
191*03e23b18SBagas Sanjaya
192*03e23b18SBagas Sanjaya-JHS
193