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
44 struct hmac_storage {
45 local_lock_t bh_lock;
46 char hmac_ring[SEG6_HMAC_RING_SIZE];
47 };
48
49 static DEFINE_PER_CPU(struct hmac_storage, hmac_storage) = {
50 .bh_lock = INIT_LOCAL_LOCK(bh_lock),
51 };
52
seg6_hmac_cmpfn(struct rhashtable_compare_arg * arg,const void * obj)53 static 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
seg6_hinfo_release(struct seg6_hmac_info * hinfo)60 static inline void seg6_hinfo_release(struct seg6_hmac_info *hinfo)
61 {
62 kfree_rcu(hinfo, rcu);
63 }
64
seg6_free_hi(void * ptr,void * arg)65 static 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
73 static 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
seg6_get_tlv_hmac(struct ipv6_sr_hdr * srh)81 static 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
seg6_hmac_compute(struct seg6_hmac_info * hinfo,struct ipv6_sr_hdr * hdr,struct in6_addr * saddr,u8 * output)100 int 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 }
169 EXPORT_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 */
seg6_hmac_validate_skb(struct sk_buff * skb)176 bool 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 if (!idev)
188 return false;
189
190 srh = (struct ipv6_sr_hdr *)skb_transport_header(skb);
191
192 tlv = seg6_get_tlv_hmac(srh);
193
194 require_hmac = READ_ONCE(idev->cnf.seg6_require_hmac);
195 /* mandatory check but no tlv */
196 if (require_hmac > 0 && !tlv)
197 return false;
198
199 /* no check */
200 if (require_hmac < 0)
201 return true;
202
203 /* check only if present */
204 if (require_hmac == 0 && !tlv)
205 return true;
206
207 /* now, seg6_require_hmac >= 0 && tlv */
208
209 hinfo = seg6_hmac_info_lookup(net, be32_to_cpu(tlv->hmackeyid));
210 if (!hinfo)
211 return false;
212
213 if (seg6_hmac_compute(hinfo, srh, &ipv6_hdr(skb)->saddr, hmac_output))
214 return false;
215
216 if (crypto_memneq(hmac_output, tlv->hmac, SEG6_HMAC_FIELD_LEN))
217 return false;
218
219 return true;
220 }
221 EXPORT_SYMBOL(seg6_hmac_validate_skb);
222
223 /* called with rcu_read_lock() */
seg6_hmac_info_lookup(struct net * net,u32 key)224 struct seg6_hmac_info *seg6_hmac_info_lookup(struct net *net, u32 key)
225 {
226 struct seg6_pernet_data *sdata = seg6_pernet(net);
227 struct seg6_hmac_info *hinfo;
228
229 hinfo = rhashtable_lookup_fast(&sdata->hmac_infos, &key, rht_params);
230
231 return hinfo;
232 }
233 EXPORT_SYMBOL(seg6_hmac_info_lookup);
234
seg6_hmac_info_add(struct net * net,u32 key,struct seg6_hmac_info * hinfo)235 int seg6_hmac_info_add(struct net *net, u32 key, struct seg6_hmac_info *hinfo)
236 {
237 struct seg6_pernet_data *sdata = seg6_pernet(net);
238 int err;
239
240 switch (hinfo->alg_id) {
241 case SEG6_HMAC_ALGO_SHA1:
242 hmac_sha1_preparekey(&hinfo->key.sha1,
243 hinfo->secret, hinfo->slen);
244 break;
245 case SEG6_HMAC_ALGO_SHA256:
246 hmac_sha256_preparekey(&hinfo->key.sha256,
247 hinfo->secret, hinfo->slen);
248 break;
249 default:
250 return -EINVAL;
251 }
252
253 err = rhashtable_lookup_insert_fast(&sdata->hmac_infos, &hinfo->node,
254 rht_params);
255
256 return err;
257 }
258 EXPORT_SYMBOL(seg6_hmac_info_add);
259
seg6_hmac_info_del(struct net * net,u32 key)260 int seg6_hmac_info_del(struct net *net, u32 key)
261 {
262 struct seg6_pernet_data *sdata = seg6_pernet(net);
263 struct seg6_hmac_info *hinfo;
264 int err = -ENOENT;
265
266 hinfo = rhashtable_lookup_fast(&sdata->hmac_infos, &key, rht_params);
267 if (!hinfo)
268 goto out;
269
270 err = rhashtable_remove_fast(&sdata->hmac_infos, &hinfo->node,
271 rht_params);
272 if (err)
273 goto out;
274
275 seg6_hinfo_release(hinfo);
276
277 out:
278 return err;
279 }
280 EXPORT_SYMBOL(seg6_hmac_info_del);
281
seg6_push_hmac(struct net * net,struct in6_addr * saddr,struct ipv6_sr_hdr * srh)282 int seg6_push_hmac(struct net *net, struct in6_addr *saddr,
283 struct ipv6_sr_hdr *srh)
284 {
285 struct seg6_hmac_info *hinfo;
286 struct sr6_tlv_hmac *tlv;
287 int err = -ENOENT;
288
289 tlv = seg6_get_tlv_hmac(srh);
290 if (!tlv)
291 return -EINVAL;
292
293 rcu_read_lock();
294
295 hinfo = seg6_hmac_info_lookup(net, be32_to_cpu(tlv->hmackeyid));
296 if (!hinfo)
297 goto out;
298
299 memset(tlv->hmac, 0, SEG6_HMAC_FIELD_LEN);
300 err = seg6_hmac_compute(hinfo, srh, saddr, tlv->hmac);
301
302 out:
303 rcu_read_unlock();
304 return err;
305 }
306 EXPORT_SYMBOL(seg6_push_hmac);
307
seg6_hmac_net_init(struct net * net)308 int __net_init seg6_hmac_net_init(struct net *net)
309 {
310 struct seg6_pernet_data *sdata = seg6_pernet(net);
311
312 return rhashtable_init(&sdata->hmac_infos, &rht_params);
313 }
314
seg6_hmac_net_exit(struct net * net)315 void __net_exit seg6_hmac_net_exit(struct net *net)
316 {
317 struct seg6_pernet_data *sdata = seg6_pernet(net);
318
319 rhashtable_free_and_destroy(&sdata->hmac_infos, seg6_free_hi, NULL);
320 }
321 EXPORT_SYMBOL(seg6_hmac_net_exit);
322