xfrmnetlink 0.3.0

Manipulate Linux IPsec tunnels via netlink
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// SPDX-License-Identifier: MIT

use futures::stream::StreamExt;
use std::ffi::CString;
use std::net::IpAddr;

use crate::{try_nl, Error, Handle};
use netlink_packet_core::{NetlinkMessage, NLM_F_ACK, NLM_F_REQUEST};
use netlink_packet_xfrm::{
    constants::*, state::ModifyMessage, Address, Alg, AlgAead, AlgAuth, EncapTmpl, Mark, Replay,
    ReplayEsn, SecurityCtx, UserOffloadDev, XfrmAttrs, XfrmMessage, XFRM_ALG_AEAD_NAME_LEN,
    XFRM_ALG_AUTH_NAME_LEN, XFRM_ALG_NAME_LEN,
};

/// A request to add or update xfrm state. This is equivalent to the `ip xfrm state add|update` commands.
#[non_exhaustive]
pub struct StateModifyRequest {
    handle: Handle,
    message: ModifyMessage,
    update: bool,
}

impl StateModifyRequest {
    pub(crate) fn new(handle: Handle, update: bool, src_addr: IpAddr, dst_addr: IpAddr) -> Self {
        let mut message = ModifyMessage::default();

        message.user_sa_info.source(&src_addr);
        message.user_sa_info.destination(&dst_addr);

        StateModifyRequest {
            handle,
            message,
            update,
        }
    }

    pub fn protocol(mut self, protocol: u8) -> Self {
        self.message.user_sa_info.id.proto = protocol;
        self
    }
    pub fn spi(mut self, spi: u32) -> Self {
        self.message.user_sa_info.id.spi = spi;
        self
    }

    pub fn authentication(mut self, alg_name: &str, key: &Vec<u8>) -> Result<Self, Error> {
        let mut auth_name: [u8; XFRM_ALG_NAME_LEN] = [0; XFRM_ALG_NAME_LEN];
        let mut c_auth_name = CString::new(alg_name)
            .map_err(|_| Error::AlgName(alg_name.to_string()))?
            .into_bytes_with_nul();

        if c_auth_name.len() > XFRM_ALG_NAME_LEN {
            c_auth_name.truncate(XFRM_ALG_NAME_LEN);
            c_auth_name[XFRM_ALG_NAME_LEN - 1] = 0;
        }
        auth_name[0..c_auth_name.len()].copy_from_slice(c_auth_name.as_slice());

        let alg_auth = Alg {
            alg_name: auth_name,
            alg_key_len: (key.len() * 8) as u32,
            alg_key: key.clone(),
        };

        self.message
            .nlas
            .push(XfrmAttrs::AuthenticationAlg(alg_auth));
        Ok(self)
    }

    pub fn authentication_trunc(
        mut self,
        alg_name: &str,
        key: &Vec<u8>,
        trunc_len: u32,
    ) -> Result<Self, Error> {
        let mut auth_name: [u8; XFRM_ALG_AUTH_NAME_LEN] = [0; XFRM_ALG_AUTH_NAME_LEN];
        let mut c_auth_name = CString::new(alg_name)
            .map_err(|_| Error::AlgName(alg_name.to_string()))?
            .into_bytes_with_nul();

        if c_auth_name.len() > XFRM_ALG_AUTH_NAME_LEN {
            c_auth_name.truncate(XFRM_ALG_AUTH_NAME_LEN);
            c_auth_name[XFRM_ALG_AUTH_NAME_LEN - 1] = 0;
        }
        auth_name[0..c_auth_name.len()].copy_from_slice(c_auth_name.as_slice());

        let alg_auth = AlgAuth {
            alg_name: auth_name,
            alg_key_len: (key.len() * 8) as u32,
            alg_trunc_len: trunc_len,
            alg_key: key.clone(),
        };

        self.message
            .nlas
            .push(XfrmAttrs::AuthenticationAlgTrunc(alg_auth));
        Ok(self)
    }

    pub fn compression(mut self, alg_name: &str) -> Result<Self, Error> {
        let mut comp_name: [u8; XFRM_ALG_NAME_LEN] = [0; XFRM_ALG_NAME_LEN];
        let mut c_comp_name = CString::new(alg_name)
            .map_err(|_| Error::AlgName(alg_name.to_string()))?
            .into_bytes_with_nul();

        if c_comp_name.len() > XFRM_ALG_NAME_LEN {
            c_comp_name.truncate(XFRM_ALG_NAME_LEN);
            c_comp_name[XFRM_ALG_NAME_LEN - 1] = 0;
        }
        comp_name[0..c_comp_name.len()].copy_from_slice(c_comp_name.as_slice());

        let alg_comp = Alg {
            alg_name: comp_name,
            alg_key_len: 0,
            alg_key: Default::default(),
        };

        self.message.nlas.push(XfrmAttrs::CompressionAlg(alg_comp));
        Ok(self)
    }

    pub fn encryption(mut self, alg_name: &str, key: &Vec<u8>) -> Result<Self, Error> {
        let mut enc_name: [u8; XFRM_ALG_NAME_LEN] = [0; XFRM_ALG_NAME_LEN];
        let mut c_enc_name = CString::new(alg_name)
            .map_err(|_| Error::AlgName(alg_name.to_string()))?
            .into_bytes_with_nul();

        if c_enc_name.len() > XFRM_ALG_NAME_LEN {
            c_enc_name.truncate(XFRM_ALG_NAME_LEN);
            c_enc_name[XFRM_ALG_NAME_LEN - 1] = 0;
        }
        enc_name[0..c_enc_name.len()].copy_from_slice(c_enc_name.as_slice());

        let alg_enc = Alg {
            alg_name: enc_name,
            alg_key_len: (key.len() * 8) as u32,
            alg_key: key.clone(),
        };

        self.message.nlas.push(XfrmAttrs::EncryptionAlg(alg_enc));
        Ok(self)
    }

    // icv_len should be in bits
    pub fn encryption_aead(
        mut self,
        alg_name: &str,
        key: &Vec<u8>,
        icv_len: u32,
    ) -> Result<Self, Error> {
        let mut enc_name: [u8; XFRM_ALG_AEAD_NAME_LEN] = [0; XFRM_ALG_AEAD_NAME_LEN];
        let mut c_enc_name = CString::new(alg_name)
            .map_err(|_| Error::AlgName(alg_name.to_string()))?
            .into_bytes_with_nul();

        if c_enc_name.len() > XFRM_ALG_AEAD_NAME_LEN {
            c_enc_name.truncate(XFRM_ALG_AEAD_NAME_LEN);
            c_enc_name[XFRM_ALG_AEAD_NAME_LEN - 1] = 0;
        }
        enc_name[0..c_enc_name.len()].copy_from_slice(c_enc_name.as_slice());

        let alg_enc = AlgAead {
            alg_name: enc_name,
            alg_key_len: (key.len() * 8) as u32,
            alg_icv_len: icv_len,
            alg_key: key.clone(),
        };

        self.message
            .nlas
            .push(XfrmAttrs::EncryptionAlgAead(alg_enc));
        Ok(self)
    }

    pub fn mode(mut self, mode: u8) -> Self {
        self.message.user_sa_info.mode = mode;
        self
    }
    pub fn reqid(mut self, reqid: u32) -> Self {
        self.message.user_sa_info.reqid = reqid;
        self
    }
    // iproute2 may be handling this incorrectly, either by
    // treating it as big-endian or by displaying it as hex instead of dec,
    // or some combination of the two. More investigation needed.
    pub fn seq(mut self, seq: u32) -> Self {
        self.message.user_sa_info.seq = seq;
        self
    }
    pub fn flags(mut self, flags: u8) -> Self {
        self.message.user_sa_info.flags = flags;
        self
    }
    pub fn extra_flags(mut self, flags: u32) -> Self {
        self.message.nlas.push(XfrmAttrs::ExtraFlags(flags));
        self
    }
    pub fn security_context(mut self, secctx: &[u8]) -> Self {
        let mut sc = SecurityCtx::default();

        sc.context(secctx);
        self.message.nlas.push(XfrmAttrs::SecurityContext(sc));
        self
    }
    pub fn ifid(mut self, ifid: u32) -> Self {
        self.message.nlas.push(XfrmAttrs::IfId(ifid));
        self
    }
    pub fn mark(mut self, mark: u32, mask: u32) -> Self {
        self.message
            .nlas
            .push(XfrmAttrs::Mark(Mark { value: mark, mask }));
        self
    }
    pub fn output_mark(mut self, mark: u32, mask: u32) -> Self {
        if mark > 0 {
            self.message.nlas.push(XfrmAttrs::MarkVal(mark));
        }
        if mask > 0 {
            self.message.nlas.push(XfrmAttrs::MarkMask(mask));
        }
        self
    }
    // Traffic Flow Confidentiality padding
    pub fn tfc_pad_length(mut self, len: u32) -> Self {
        self.message.nlas.push(XfrmAttrs::TfcPadding(len));
        self
    }

    pub fn replay_window(
        mut self,
        size: u32,
        seq: u32,
        seq_hi: u32,
        offload_seq: u32,
        offload_seq_hi: u32,
    ) -> Self {
        if (size > u32::BITS)
            || (self.message.user_sa_info.flags & XFRM_STATE_ESN) == XFRM_STATE_ESN
        {
            let bmp_len: u32 = (size + u32::BITS - 1) / u32::BITS;
            let replay_esn = ReplayEsn {
                bmp_len,
                oseq: offload_seq,
                seq,
                oseq_hi: offload_seq_hi,
                seq_hi,
                replay_window: size,
                bmp: Vec::default(),
            };
            self.message
                .nlas
                .push(XfrmAttrs::ReplayStateEsn(replay_esn));
        } else {
            if seq != 0 || offload_seq != 0 {
                let replay = Replay {
                    seq,
                    oseq: offload_seq,
                    bitmap: 0,
                };
                self.message.nlas.push(XfrmAttrs::ReplayState(replay));
            }
            self.message.user_sa_info.replay_window = size as u8;
        }
        self
    }

    pub fn time_limit(mut self, soft: u64, hard: u64) -> Self {
        self.message
            .user_sa_info
            .lifetime_cfg
            .soft_add_expires_seconds = soft;
        self.message
            .user_sa_info
            .lifetime_cfg
            .hard_add_expires_seconds = hard;
        self
    }
    pub fn time_use_limit(mut self, soft: u64, hard: u64) -> Self {
        self.message
            .user_sa_info
            .lifetime_cfg
            .soft_use_expires_seconds = soft;
        self.message
            .user_sa_info
            .lifetime_cfg
            .hard_use_expires_seconds = hard;
        self
    }
    pub fn byte_limit(mut self, soft: u64, hard: u64) -> Self {
        self.message.user_sa_info.lifetime_cfg.soft_byte_limit = soft;
        self.message.user_sa_info.lifetime_cfg.hard_byte_limit = hard;
        self
    }
    pub fn packet_limit(mut self, soft: u64, hard: u64) -> Self {
        self.message.user_sa_info.lifetime_cfg.soft_packet_limit = soft;
        self.message.user_sa_info.lifetime_cfg.hard_packet_limit = hard;
        self
    }
    pub fn selector_addresses(
        mut self,
        src_addr: IpAddr,
        src_prefix_len: u8,
        dst_addr: IpAddr,
        dst_prefix_len: u8,
    ) -> Self {
        self.message
            .user_sa_info
            .selector
            .source_prefix(&src_addr, src_prefix_len);
        self.message
            .user_sa_info
            .selector
            .destination_prefix(&dst_addr, dst_prefix_len);
        self
    }
    pub fn selector_protocol(mut self, proto: u8) -> Self {
        self.message.user_sa_info.selector.proto = proto;
        self
    }
    pub fn selector_protocol_src_port(mut self, port: u16) -> Self {
        self.message.user_sa_info.selector.sport = port;
        self.message.user_sa_info.selector.sport_mask = u16::MAX;
        self
    }
    pub fn selector_protocol_dst_port(mut self, port: u16) -> Self {
        self.message.user_sa_info.selector.dport = port;
        self.message.user_sa_info.selector.dport_mask = u16::MAX;
        self
    }
    pub fn selector_protocol_type(mut self, proto_type: u8) -> Self {
        self.message.user_sa_info.selector.sport = proto_type as u16;
        self.message.user_sa_info.selector.sport_mask = u16::MAX;
        self
    }
    pub fn selector_protocol_code(mut self, proto_code: u8) -> Self {
        self.message.user_sa_info.selector.dport = proto_code as u16;
        self.message.user_sa_info.selector.dport_mask = u16::MAX;
        self
    }
    pub fn selector_protocol_gre_key(mut self, gre_key: u32) -> Self {
        self.message.user_sa_info.selector.sport = (gre_key >> 16) as u16;
        self.message.user_sa_info.selector.sport_mask = u16::MAX;
        self.message.user_sa_info.selector.dport = (gre_key & 0xffff) as u16;
        self.message.user_sa_info.selector.dport_mask = u16::MAX;
        self
    }
    pub fn selector_dev_id(mut self, id: u32) -> Self {
        self.message.user_sa_info.selector.ifindex = id as i32;
        self
    }

    pub fn encapsulation(
        mut self,
        encap_type: u16,
        src_port: u16,
        dst_port: u16,
        outside_addr: IpAddr,
    ) -> Self {
        let encap_tmpl = EncapTmpl {
            encap_type,
            encap_sport: src_port,
            encap_dport: dst_port,
            encap_oa: Address::from_ip(&outside_addr),
        };

        self.message
            .nlas
            .push(XfrmAttrs::EncapsulationTemplate(encap_tmpl));
        self
    }

    // only used for routing protocols (xfrm proto route2 & hao)
    pub fn care_of_address(mut self, co_addr: IpAddr) -> Self {
        self.message
            .nlas
            .push(XfrmAttrs::CareOfAddr(Address::from_ip(&co_addr)));
        self
    }

    // flags can be XFRM_OFFLOAD_IPV6 or XFRM_OFFLOAD_INBOUND
    pub fn offload_device(mut self, id: u32, flags: u8) -> Self {
        self.message
            .nlas
            .push(XfrmAttrs::OffloadDevice(UserOffloadDev {
                ifindex: id as i32,
                flags,
            }));
        self
    }

    /// Execute the request.
    pub async fn execute(self) -> Result<(), Error> {
        let StateModifyRequest {
            mut handle,
            message,
            update,
        } = self;

        let mut req = if update {
            NetlinkMessage::from(XfrmMessage::UpdateSa(message))
        } else {
            NetlinkMessage::from(XfrmMessage::AddSa(message))
        };
        req.header.flags = NLM_F_REQUEST | NLM_F_ACK;

        let mut response = handle.request(req)?;

        while let Some(message) = response.next().await {
            try_nl!(message);
        }
        Ok(())
    }

    /// Execute the request without waiting for an ACK response.
    pub fn execute_noack(self) -> Result<(), Error> {
        let StateModifyRequest {
            mut handle,
            message,
            update,
        } = self;

        let mut req = if update {
            NetlinkMessage::from(XfrmMessage::UpdateSa(message))
        } else {
            NetlinkMessage::from(XfrmMessage::AddSa(message))
        };
        req.header.flags = NLM_F_REQUEST;

        let mut _response = handle.request(req)?;

        Ok(())
    }

    /// Return a mutable reference to the request message.
    pub fn message_mut(&mut self) -> &mut ModifyMessage {
        &mut self.message
    }
}