Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * SR-IPv6 implementation -- HMAC functions
4 *
5 * Author:
6 * David Lebrun <david.lebrun@uclouvain.be>
7 */
8
9#include <linux/errno.h>
10#include <linux/kernel.h>
11#include <linux/types.h>
12#include <linux/socket.h>
13#include <linux/sockios.h>
14#include <linux/net.h>
15#include <linux/netdevice.h>
16#include <linux/in6.h>
17#include <linux/icmpv6.h>
18#include <linux/mroute6.h>
19#include <linux/rhashtable.h>
20
21#include <linux/netfilter.h>
22#include <linux/netfilter_ipv6.h>
23
24#include <net/sock.h>
25#include <net/snmp.h>
26
27#include <net/ipv6.h>
28#include <net/protocol.h>
29#include <net/transp_v6.h>
30#include <net/rawv6.h>
31#include <net/ndisc.h>
32#include <net/ip6_route.h>
33#include <net/addrconf.h>
34#include <net/xfrm.h>
35
36#include <crypto/sha1.h>
37#include <crypto/sha2.h>
38#include <crypto/utils.h>
39#include <net/seg6.h>
40#include <net/genetlink.h>
41#include <net/seg6_hmac.h>
42#include <linux/random.h>
43
44struct hmac_storage {
45 local_lock_t bh_lock;
46 char hmac_ring[SEG6_HMAC_RING_SIZE];
47};
48
49static DEFINE_PER_CPU(struct hmac_storage, hmac_storage) = {
50 .bh_lock = INIT_LOCAL_LOCK(bh_lock),
51};
52
53static int seg6_hmac_cmpfn(struct rhashtable_compare_arg *arg, const void *obj)
54{
55 const struct seg6_hmac_info *hinfo = obj;
56
57 return (hinfo->hmackeyid != *(__u32 *)arg->key);
58}
59
60static inline void seg6_hinfo_release(struct seg6_hmac_info *hinfo)
61{
62 kfree_rcu(hinfo, rcu);
63}
64
65static void seg6_free_hi(void *ptr, void *arg)
66{
67 struct seg6_hmac_info *hinfo = (struct seg6_hmac_info *)ptr;
68
69 if (hinfo)
70 seg6_hinfo_release(hinfo);
71}
72
73static const struct rhashtable_params rht_params = {
74 .head_offset = offsetof(struct seg6_hmac_info, node),
75 .key_offset = offsetof(struct seg6_hmac_info, hmackeyid),
76 .key_len = sizeof(u32),
77 .automatic_shrinking = true,
78 .obj_cmpfn = seg6_hmac_cmpfn,
79};
80
81static struct sr6_tlv_hmac *seg6_get_tlv_hmac(struct ipv6_sr_hdr *srh)
82{
83 struct sr6_tlv_hmac *tlv;
84
85 if (srh->hdrlen < (srh->first_segment + 1) * 2 + 5)
86 return NULL;
87
88 if (!sr_has_hmac(srh))
89 return NULL;
90
91 tlv = (struct sr6_tlv_hmac *)
92 ((char *)srh + ((srh->hdrlen + 1) << 3) - 40);
93
94 if (tlv->tlvhdr.type != SR6_TLV_HMAC || tlv->tlvhdr.len != 38)
95 return NULL;
96
97 return tlv;
98}
99
100int seg6_hmac_compute(struct seg6_hmac_info *hinfo, struct ipv6_sr_hdr *hdr,
101 struct in6_addr *saddr, u8 *output)
102{
103 __be32 hmackeyid = cpu_to_be32(hinfo->hmackeyid);
104 int plen, i, ret = 0;
105 char *ring, *off;
106
107 /* saddr(16) + first_seg(1) + flags(1) + keyid(4) + seglist(16n) */
108 plen = 16 + 1 + 1 + 4 + (hdr->first_segment + 1) * 16;
109
110 /* this limit allows for 14 segments */
111 if (plen >= SEG6_HMAC_RING_SIZE)
112 return -EMSGSIZE;
113
114 /* Let's build the HMAC text on the ring buffer. The text is composed
115 * as follows, in order:
116 *
117 * 1. Source IPv6 address (128 bits)
118 * 2. first_segment value (8 bits)
119 * 3. Flags (8 bits)
120 * 4. HMAC Key ID (32 bits)
121 * 5. All segments in the segments list (n * 128 bits)
122 */
123
124 local_bh_disable();
125 local_lock_nested_bh(&hmac_storage.bh_lock);
126 ring = this_cpu_ptr(hmac_storage.hmac_ring);
127 off = ring;
128
129 /* source address */
130 memcpy(off, saddr, 16);
131 off += 16;
132
133 /* first_segment value */
134 *off++ = hdr->first_segment;
135
136 /* flags */
137 *off++ = hdr->flags;
138
139 /* HMAC Key ID */
140 memcpy(off, &hmackeyid, 4);
141 off += 4;
142
143 /* all segments in the list */
144 for (i = 0; i < hdr->first_segment + 1; i++) {
145 memcpy(off, hdr->segments + i, 16);
146 off += 16;
147 }
148
149 switch (hinfo->alg_id) {
150 case SEG6_HMAC_ALGO_SHA1:
151 hmac_sha1(&hinfo->key.sha1, ring, plen, output);
152 static_assert(SEG6_HMAC_FIELD_LEN > SHA1_DIGEST_SIZE);
153 memset(&output[SHA1_DIGEST_SIZE], 0,
154 SEG6_HMAC_FIELD_LEN - SHA1_DIGEST_SIZE);
155 break;
156 case SEG6_HMAC_ALGO_SHA256:
157 hmac_sha256(&hinfo->key.sha256, ring, plen, output);
158 static_assert(SEG6_HMAC_FIELD_LEN == SHA256_DIGEST_SIZE);
159 break;
160 default:
161 WARN_ON_ONCE(1);
162 ret = -EINVAL;
163 break;
164 }
165 local_unlock_nested_bh(&hmac_storage.bh_lock);
166 local_bh_enable();
167 return ret;
168}
169EXPORT_SYMBOL(seg6_hmac_compute);
170
171/* checks if an incoming SR-enabled packet's HMAC status matches
172 * the incoming policy.
173 *
174 * called with rcu_read_lock()
175 */
176bool seg6_hmac_validate_skb(struct sk_buff *skb)
177{
178 u8 hmac_output[SEG6_HMAC_FIELD_LEN];
179 struct net *net = dev_net(skb->dev);
180 struct seg6_hmac_info *hinfo;
181 struct sr6_tlv_hmac *tlv;
182 struct ipv6_sr_hdr *srh;
183 struct inet6_dev *idev;
184 int require_hmac;
185
186 idev = __in6_dev_get(skb->dev);
187
188 srh = (struct ipv6_sr_hdr *)skb_transport_header(skb);
189
190 tlv = seg6_get_tlv_hmac(srh);
191
192 require_hmac = READ_ONCE(idev->cnf.seg6_require_hmac);
193 /* mandatory check but no tlv */
194 if (require_hmac > 0 && !tlv)
195 return false;
196
197 /* no check */
198 if (require_hmac < 0)
199 return true;
200
201 /* check only if present */
202 if (require_hmac == 0 && !tlv)
203 return true;
204
205 /* now, seg6_require_hmac >= 0 && tlv */
206
207 hinfo = seg6_hmac_info_lookup(net, be32_to_cpu(tlv->hmackeyid));
208 if (!hinfo)
209 return false;
210
211 if (seg6_hmac_compute(hinfo, srh, &ipv6_hdr(skb)->saddr, hmac_output))
212 return false;
213
214 if (crypto_memneq(hmac_output, tlv->hmac, SEG6_HMAC_FIELD_LEN))
215 return false;
216
217 return true;
218}
219EXPORT_SYMBOL(seg6_hmac_validate_skb);
220
221/* called with rcu_read_lock() */
222struct seg6_hmac_info *seg6_hmac_info_lookup(struct net *net, u32 key)
223{
224 struct seg6_pernet_data *sdata = seg6_pernet(net);
225 struct seg6_hmac_info *hinfo;
226
227 hinfo = rhashtable_lookup_fast(&sdata->hmac_infos, &key, rht_params);
228
229 return hinfo;
230}
231EXPORT_SYMBOL(seg6_hmac_info_lookup);
232
233int seg6_hmac_info_add(struct net *net, u32 key, struct seg6_hmac_info *hinfo)
234{
235 struct seg6_pernet_data *sdata = seg6_pernet(net);
236 int err;
237
238 switch (hinfo->alg_id) {
239 case SEG6_HMAC_ALGO_SHA1:
240 hmac_sha1_preparekey(&hinfo->key.sha1,
241 hinfo->secret, hinfo->slen);
242 break;
243 case SEG6_HMAC_ALGO_SHA256:
244 hmac_sha256_preparekey(&hinfo->key.sha256,
245 hinfo->secret, hinfo->slen);
246 break;
247 default:
248 return -EINVAL;
249 }
250
251 err = rhashtable_lookup_insert_fast(&sdata->hmac_infos, &hinfo->node,
252 rht_params);
253
254 return err;
255}
256EXPORT_SYMBOL(seg6_hmac_info_add);
257
258int seg6_hmac_info_del(struct net *net, u32 key)
259{
260 struct seg6_pernet_data *sdata = seg6_pernet(net);
261 struct seg6_hmac_info *hinfo;
262 int err = -ENOENT;
263
264 hinfo = rhashtable_lookup_fast(&sdata->hmac_infos, &key, rht_params);
265 if (!hinfo)
266 goto out;
267
268 err = rhashtable_remove_fast(&sdata->hmac_infos, &hinfo->node,
269 rht_params);
270 if (err)
271 goto out;
272
273 seg6_hinfo_release(hinfo);
274
275out:
276 return err;
277}
278EXPORT_SYMBOL(seg6_hmac_info_del);
279
280int seg6_push_hmac(struct net *net, struct in6_addr *saddr,
281 struct ipv6_sr_hdr *srh)
282{
283 struct seg6_hmac_info *hinfo;
284 struct sr6_tlv_hmac *tlv;
285 int err = -ENOENT;
286
287 tlv = seg6_get_tlv_hmac(srh);
288 if (!tlv)
289 return -EINVAL;
290
291 rcu_read_lock();
292
293 hinfo = seg6_hmac_info_lookup(net, be32_to_cpu(tlv->hmackeyid));
294 if (!hinfo)
295 goto out;
296
297 memset(tlv->hmac, 0, SEG6_HMAC_FIELD_LEN);
298 err = seg6_hmac_compute(hinfo, srh, saddr, tlv->hmac);
299
300out:
301 rcu_read_unlock();
302 return err;
303}
304EXPORT_SYMBOL(seg6_push_hmac);
305
306int __net_init seg6_hmac_net_init(struct net *net)
307{
308 struct seg6_pernet_data *sdata = seg6_pernet(net);
309
310 return rhashtable_init(&sdata->hmac_infos, &rht_params);
311}
312
313void __net_exit seg6_hmac_net_exit(struct net *net)
314{
315 struct seg6_pernet_data *sdata = seg6_pernet(net);
316
317 rhashtable_free_and_destroy(&sdata->hmac_infos, seg6_free_hi, NULL);
318}
319EXPORT_SYMBOL(seg6_hmac_net_exit);