-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegrity_option3.patch
More file actions
354 lines (334 loc) · 10.4 KB
/
integrity_option3.patch
File metadata and controls
354 lines (334 loc) · 10.4 KB
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
diff --git a/include/net/ioam6.h b/include/net/ioam6.h
index 2cbbee6e806a..5399dca99b56 100644
--- a/include/net/ioam6.h
+++ b/include/net/ioam6.h
@@ -43,6 +43,7 @@ struct ioam6_pernet_data {
struct mutex lock;
struct rhashtable namespaces;
struct rhashtable schemas;
+ struct crypto_aead *tfm;
};
static inline struct ioam6_pernet_data *ioam6_pernet(struct net *net)
@@ -55,11 +56,18 @@ static inline struct ioam6_pernet_data *ioam6_pernet(struct net *net)
}
struct ioam6_namespace *ioam6_namespace(struct net *net, __be16 id);
+struct crypto_aead *ioam6_crypto(struct net *net);
+
void ioam6_fill_trace_data(struct sk_buff *skb,
struct ioam6_namespace *ns,
struct ioam6_trace_hdr *trace,
bool is_input);
+bool ioam6_aes_gmac(struct crypto_aead *tfm, u8 *iv,
+ u8 *tag, u8 *aad, size_t aad_len);
+bool ioam6_valid_aes_gmac(struct crypto_aead *tfm, u8 *iv,
+ u8 *tag, u8 *aad, size_t aad_len);
+
int ioam6_init(void);
void ioam6_exit(void);
diff --git a/include/uapi/linux/ioam6.h b/include/uapi/linux/ioam6.h
index 8f72b24fefb3..129c111c261d 100644
--- a/include/uapi/linux/ioam6.h
+++ b/include/uapi/linux/ioam6.h
@@ -29,9 +29,24 @@ struct ioam6_hdr {
__u8 opt_len;
__u8 :8; /* reserved */
#define IOAM6_TYPE_PREALLOC 0
+#define IOAM6_TYPE_INT_PREALLOC 64
__u8 type;
} __attribute__((packed));
+/*
+ * IOAM Integrity 1 Protected Trace Header
+ */
+#define IOAM6_INTEGRITY_1 1
+struct ioam6_integrity_1_hdr {
+ __u8 suite_id;
+ __u8 nonce_len;
+ __be16 __reserved;
+#define IOAM6_GMAC_NONCE_SIZE 12
+ __u8 nonce[IOAM6_GMAC_NONCE_SIZE];
+#define IOAM6_GMAC_TAG_SIZE 16
+ __u8 icv[IOAM6_GMAC_TAG_SIZE];
+} __attribute__((packed));
+
/*
* IOAM Trace Header
*/
@@ -126,7 +141,7 @@ struct ioam6_trace_hdr {
#error "Please fix <asm/byteorder.h>"
#endif
-#define IOAM6_TRACE_DATA_SIZE_MAX 244
+#define IOAM6_TRACE_DATA_SIZE_MAX 212
__u8 data[];
} __attribute__((packed));
diff --git a/net/ipv6/exthdrs.c b/net/ipv6/exthdrs.c
index 26f1e4a5ade0..7e2fba6236ad 100644
--- a/net/ipv6/exthdrs.c
+++ b/net/ipv6/exthdrs.c
@@ -961,6 +961,87 @@ static bool ipv6_hop_ioam(struct sk_buff *skb, int optoff)
ioam6_event(IOAM6_EVENT_TRACE, dev_net(skb->dev),
GFP_ATOMIC, (void *)trace, hdr->opt_len - 2);
break;
+ case IOAM6_TYPE_INT_PREALLOC:
+ if (hdr->opt_len < 3)
+ goto drop;
+
+ u8 *nh = skb_network_header(skb) + optoff + sizeof(*hdr);
+ struct ioam6_integrity_1_hdr *i1hdr;
+
+ switch (nh[0]) {
+ case IOAM6_INTEGRITY_1:
+ if (hdr->opt_len < 2 + sizeof(*i1hdr) + sizeof(*trace))
+ goto drop;
+
+ i1hdr = (struct ioam6_integrity_1_hdr *)nh;
+ trace = (struct ioam6_trace_hdr *)(nh + sizeof(*i1hdr));
+
+ /* TODO
+ * Right now, we just print if it succeeds or fails.
+ *
+ * Instead, when it fails, we'll report it (or not?) as
+ * an ioam6 event, e.g., IOAM6_EVENT_TRACE_FALSIFIED.
+ * In that case, a drop is not what we want: we just
+ * skip and don't process the IOAM option. If it
+ * succeeds, we'll report the trace as the ioam6 event
+ * IOAM6_EVENT_TRACE *after* the processing.
+ */
+ if (!ioam6_valid_aes_gmac(
+ ioam6_crypto(dev_net(skb->dev)),
+ i1hdr->nonce, i1hdr->icv, (u8 *)trace,
+ hdr->opt_len - 2 - sizeof(*i1hdr))) {
+ printk(KERN_EMERG "IOAM trace integrity check failed\n");
+ goto ignore;
+ }
+
+ if (hdr->opt_len < 2 + sizeof(*i1hdr)
+ + sizeof(*trace) + trace->remlen * 4)
+ goto drop;
+
+ ns = ioam6_namespace(dev_net(skb->dev),
+ trace->namespace_id);
+ /* Don't process if Namespace-ID is unknown */
+ if (!ns)
+ goto ignore;
+
+ if (!skb_valid_dst(skb))
+ ip6_route_input(skb);
+
+ if (skb_ensure_writable(skb, optoff + 2 + hdr->opt_len))
+ goto drop;
+
+ nh = skb_network_header(skb) + optoff + sizeof(*hdr);
+ i1hdr = (struct ioam6_integrity_1_hdr *)nh;
+ trace = (struct ioam6_trace_hdr *)(nh + sizeof(*i1hdr));
+
+ ioam6_fill_trace_data(skb, ns, trace, true);
+
+ /* TODO
+ * For this PoC, we use the node_id (=777) as the
+ * decapsulating node (i.e., which must not perform the
+ * GMAC operation).
+ *
+ * We can't just check if we're the destination,
+ * in case of inline insertions for example. We'll also
+ * need a (bool) per interface sysctl disabled by
+ * default (e.g., ioam6_integrity_decap).
+ */
+ /* GMAC the whole trace for next neighbor, unless the
+ * decap role is enabled
+ */
+ if (dev_net(skb->dev)->ipv6.sysctl.ioam6_id != 777
+ && !ioam6_aes_gmac(
+ ioam6_crypto(dev_net(skb->dev)),
+ i1hdr->nonce, i1hdr->icv, (u8 *)trace,
+ hdr->opt_len - 2 - sizeof(*i1hdr)))
+ goto drop;
+
+ break;
+ default:
+ break;
+ }
+
+ break;
default:
break;
}
diff --git a/net/ipv6/ioam6.c b/net/ipv6/ioam6.c
index 08c929513065..73164177df4a 100644
--- a/net/ipv6/ioam6.c
+++ b/net/ipv6/ioam6.c
@@ -6,6 +6,9 @@
* Justin Iurman <justin.iurman@uliege.be>
*/
+#include <crypto/aead.h>
+
+#include <linux/err.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/kernel.h>
@@ -694,6 +697,53 @@ struct ioam6_namespace *ioam6_namespace(struct net *net, __be16 id)
return rhashtable_lookup_fast(&nsdata->namespaces, &id, rht_ns_params);
}
+struct crypto_aead *ioam6_crypto(struct net *net)
+{
+ struct ioam6_pernet_data *nsdata = ioam6_pernet(net);
+ return nsdata->tfm;
+}
+
+bool ioam6_aes_gmac(struct crypto_aead *tfm, u8 *iv,
+ u8 *tag, u8 *aad, size_t aad_len)
+{
+ struct aead_request *aead_req;
+ struct scatterlist sg[2];
+
+ if (!aad_len)
+ return false;
+
+ aead_req = aead_request_alloc(tfm, GFP_ATOMIC);
+ if (!aead_req)
+ return false;
+
+ sg_init_table(sg, ARRAY_SIZE(sg));
+ sg_set_buf(&sg[0], aad, aad_len);
+ sg_set_buf(&sg[1], tag, IOAM6_GMAC_TAG_SIZE);
+
+ aead_request_set_tfm(aead_req, tfm);
+ aead_request_set_crypt(aead_req, sg, sg, 0, iv);
+ aead_request_set_ad(aead_req, aad_len);
+
+ if (crypto_aead_encrypt(aead_req)) {
+ aead_request_free(aead_req);
+ return false;
+ }
+
+ aead_request_free(aead_req);
+ return true;
+}
+
+bool ioam6_valid_aes_gmac(struct crypto_aead *tfm, u8 *iv,
+ u8 *tag, u8 *aad, size_t aad_len)
+{
+ u8 icv[IOAM6_GMAC_TAG_SIZE];
+
+ if (!ioam6_aes_gmac(tfm, iv, icv, aad, aad_len))
+ return false;
+
+ return memcmp(icv, tag, IOAM6_GMAC_TAG_SIZE) == 0;
+}
+
static void __ioam6_fill_trace_data(struct sk_buff *skb,
struct ioam6_namespace *ns,
struct ioam6_trace_hdr *trace,
@@ -979,8 +1029,35 @@ static int __net_init ioam6_net_init(struct net *net)
if (err)
goto free_rht_ns;
+ nsdata->tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
+ if (IS_ERR(nsdata->tfm)) {
+ err = 1;
+ goto free_rht_sch;
+ }
+
+ /* TODO
+ * Right now, for this PoC, we use a static key.
+ * We'll need to configure it via Netlink (e.g., with iproute2) and
+ * attach it to the crypto_aead struct "later" than now/here.
+ */
+ u8 key[] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa,
+ 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14,
+ 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
+ 0x1e, 0x1f }; //32 bytes -> AES-256
+
+ err = crypto_aead_setkey(nsdata->tfm, key, sizeof(key));
+ if (err)
+ goto free_crypto_aead;
+
+ err = crypto_aead_setauthsize(nsdata->tfm, IOAM6_GMAC_TAG_SIZE);
+ if (err)
+ goto free_crypto_aead;
out:
return err;
+free_crypto_aead:
+ crypto_free_aead(nsdata->tfm);
+free_rht_sch:
+ rhashtable_destroy(&nsdata->schemas);
free_rht_ns:
rhashtable_destroy(&nsdata->namespaces);
free_nsdata:
@@ -993,6 +1070,8 @@ static void __net_exit ioam6_net_exit(struct net *net)
{
struct ioam6_pernet_data *nsdata = ioam6_pernet(net);
+ crypto_free_aead(nsdata->tfm);
+
rhashtable_free_and_destroy(&nsdata->namespaces, ioam6_free_ns, NULL);
rhashtable_free_and_destroy(&nsdata->schemas, ioam6_free_sc, NULL);
diff --git a/net/ipv6/ioam6_iptunnel.c b/net/ipv6/ioam6_iptunnel.c
index 7563f8c6aa87..e5a02d798156 100644
--- a/net/ipv6/ioam6_iptunnel.c
+++ b/net/ipv6/ioam6_iptunnel.c
@@ -29,6 +29,7 @@ struct ioam6_lwt_encap {
struct ipv6_hopopt_hdr eh;
u8 pad[2]; /* 2-octet padding for 4n-alignment */
struct ioam6_hdr ioamh;
+ struct ioam6_integrity_1_hdr inth;
struct ioam6_trace_hdr traceh;
} __packed;
@@ -183,13 +184,34 @@ static int ioam6_build_state(struct net *net, struct nlattr *nla,
tuninfo = ioam6_lwt_info(lwt);
tuninfo->eh.hdrlen = ((sizeof(*tuninfo) + len_aligned) >> 3) - 1;
tuninfo->pad[0] = IPV6_TLV_PADN;
- tuninfo->ioamh.type = IOAM6_TYPE_PREALLOC;
+ tuninfo->ioamh.type = IOAM6_TYPE_INT_PREALLOC;
tuninfo->ioamh.opt_type = IPV6_TLV_IOAM;
- tuninfo->ioamh.opt_len = sizeof(tuninfo->ioamh) - 2 + sizeof(*trace)
- + trace->remlen * 4;
+ tuninfo->ioamh.opt_len = sizeof(tuninfo->ioamh) - 2
+ + sizeof(tuninfo->inth)
+ + sizeof(*trace) + trace->remlen * 4;
memcpy(&tuninfo->traceh, trace, sizeof(*trace));
+ tuninfo->inth.suite_id = IOAM6_INTEGRITY_1;
+ tuninfo->inth.nonce_len = IOAM6_GMAC_NONCE_SIZE;
+
+ /* TODO
+ * Right now, for this PoC, we use a static nonce.
+ * We'll need to generate one instead.
+ */
+ tuninfo->inth.nonce[0] = 0x02;
+ tuninfo->inth.nonce[1] = 0x04;
+ tuninfo->inth.nonce[2] = 0x06;
+ tuninfo->inth.nonce[3] = 0x08;
+ tuninfo->inth.nonce[4] = 0x0a;
+ tuninfo->inth.nonce[5] = 0x0c;
+ tuninfo->inth.nonce[6] = 0x0e;
+ tuninfo->inth.nonce[7] = 0x01;
+ tuninfo->inth.nonce[8] = 0x03;
+ tuninfo->inth.nonce[9] = 0x07;
+ tuninfo->inth.nonce[10] = 0x0b;
+ tuninfo->inth.nonce[11] = 0x0f;
+
if (len_aligned - trace->remlen * 4) {
tuninfo->traceh.data[trace->remlen * 4] = IPV6_TLV_PADN;
tuninfo->traceh.data[trace->remlen * 4 + 1] = 2;
@@ -205,17 +227,26 @@ static int ioam6_build_state(struct net *net, struct nlattr *nla,
static int ioam6_do_fill(struct net *net, struct sk_buff *skb)
{
+ struct ioam6_integrity_1_hdr *inth;
struct ioam6_trace_hdr *trace;
struct ioam6_namespace *ns;
+ u8 remlen;
+
+ inth = (struct ioam6_integrity_1_hdr *)(skb_transport_header(skb)
+ + sizeof(struct ipv6_hopopt_hdr)
+ + 2 + sizeof(struct ioam6_hdr));
- trace = (struct ioam6_trace_hdr *)(skb_transport_header(skb)
- + sizeof(struct ipv6_hopopt_hdr) + 2
- + sizeof(struct ioam6_hdr));
+ trace = (struct ioam6_trace_hdr *)((u8 *)inth + sizeof(*inth));
+ remlen = trace->remlen;
ns = ioam6_namespace(net, trace->namespace_id);
if (ns)
ioam6_fill_trace_data(skb, ns, trace, false);
+ if (!ioam6_aes_gmac(ioam6_crypto(net), inth->nonce, inth->icv,
+ (u8 *)trace, sizeof(*trace) + remlen * 4))
+ return 1;
+
return 0;
}