seg6_hmac.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. #include <linux/errno.h>
  9. #include <linux/kernel.h>
  10. #include <linux/types.h>
  11. #include <linux/socket.h>
  12. #include <linux/sockios.h>
  13. #include <linux/net.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/in6.h>
  16. #include <linux/icmpv6.h>
  17. #include <linux/mroute6.h>
  18. #include <linux/slab.h>
  19. #include <linux/rhashtable.h>
  20. #include <linux/netfilter.h>
  21. #include <linux/netfilter_ipv6.h>
  22. #include <net/sock.h>
  23. #include <net/snmp.h>
  24. #include <net/ipv6.h>
  25. #include <net/protocol.h>
  26. #include <net/transp_v6.h>
  27. #include <net/rawv6.h>
  28. #include <net/ndisc.h>
  29. #include <net/ip6_route.h>
  30. #include <net/addrconf.h>
  31. #include <net/xfrm.h>
  32. #include <crypto/hash.h>
  33. #include <crypto/utils.h>
  34. #include <net/seg6.h>
  35. #include <net/genetlink.h>
  36. #include <net/seg6_hmac.h>
  37. #include <linux/random.h>
  38. static DEFINE_PER_CPU(char [SEG6_HMAC_RING_SIZE], hmac_ring);
  39. static int seg6_hmac_cmpfn(struct rhashtable_compare_arg *arg, const void *obj)
  40. {
  41. const struct seg6_hmac_info *hinfo = obj;
  42. return (hinfo->hmackeyid != *(__u32 *)arg->key);
  43. }
  44. static inline void seg6_hinfo_release(struct seg6_hmac_info *hinfo)
  45. {
  46. kfree_rcu(hinfo, rcu);
  47. }
  48. static void seg6_free_hi(void *ptr, void *arg)
  49. {
  50. struct seg6_hmac_info *hinfo = (struct seg6_hmac_info *)ptr;
  51. if (hinfo)
  52. seg6_hinfo_release(hinfo);
  53. }
  54. static const struct rhashtable_params rht_params = {
  55. .head_offset = offsetof(struct seg6_hmac_info, node),
  56. .key_offset = offsetof(struct seg6_hmac_info, hmackeyid),
  57. .key_len = sizeof(u32),
  58. .automatic_shrinking = true,
  59. .obj_cmpfn = seg6_hmac_cmpfn,
  60. };
  61. static struct seg6_hmac_algo hmac_algos[] = {
  62. {
  63. .alg_id = SEG6_HMAC_ALGO_SHA1,
  64. .name = "hmac(sha1)",
  65. },
  66. {
  67. .alg_id = SEG6_HMAC_ALGO_SHA256,
  68. .name = "hmac(sha256)",
  69. },
  70. };
  71. static struct sr6_tlv_hmac *seg6_get_tlv_hmac(struct ipv6_sr_hdr *srh)
  72. {
  73. struct sr6_tlv_hmac *tlv;
  74. if (srh->hdrlen < (srh->first_segment + 1) * 2 + 5)
  75. return NULL;
  76. if (!sr_has_hmac(srh))
  77. return NULL;
  78. tlv = (struct sr6_tlv_hmac *)
  79. ((char *)srh + ((srh->hdrlen + 1) << 3) - 40);
  80. if (tlv->tlvhdr.type != SR6_TLV_HMAC || tlv->tlvhdr.len != 38)
  81. return NULL;
  82. return tlv;
  83. }
  84. static struct seg6_hmac_algo *__hmac_get_algo(u8 alg_id)
  85. {
  86. struct seg6_hmac_algo *algo;
  87. int i, alg_count;
  88. alg_count = ARRAY_SIZE(hmac_algos);
  89. for (i = 0; i < alg_count; i++) {
  90. algo = &hmac_algos[i];
  91. if (algo->alg_id == alg_id)
  92. return algo;
  93. }
  94. return NULL;
  95. }
  96. static int __do_hmac(struct seg6_hmac_info *hinfo, const char *text, u8 psize,
  97. u8 *output, int outlen)
  98. {
  99. struct seg6_hmac_algo *algo;
  100. struct crypto_shash *tfm;
  101. struct shash_desc *shash;
  102. int ret, dgsize;
  103. algo = __hmac_get_algo(hinfo->alg_id);
  104. if (!algo)
  105. return -ENOENT;
  106. tfm = *this_cpu_ptr(algo->tfms);
  107. dgsize = crypto_shash_digestsize(tfm);
  108. if (dgsize > outlen) {
  109. pr_debug("sr-ipv6: __do_hmac: digest size too big (%d / %d)\n",
  110. dgsize, outlen);
  111. return -ENOMEM;
  112. }
  113. ret = crypto_shash_setkey(tfm, hinfo->secret, hinfo->slen);
  114. if (ret < 0) {
  115. pr_debug("sr-ipv6: crypto_shash_setkey failed: err %d\n", ret);
  116. goto failed;
  117. }
  118. shash = *this_cpu_ptr(algo->shashs);
  119. shash->tfm = tfm;
  120. ret = crypto_shash_digest(shash, text, psize, output);
  121. if (ret < 0) {
  122. pr_debug("sr-ipv6: crypto_shash_digest failed: err %d\n", ret);
  123. goto failed;
  124. }
  125. return dgsize;
  126. failed:
  127. return ret;
  128. }
  129. int seg6_hmac_compute(struct seg6_hmac_info *hinfo, struct ipv6_sr_hdr *hdr,
  130. struct in6_addr *saddr, u8 *output)
  131. {
  132. __be32 hmackeyid = cpu_to_be32(hinfo->hmackeyid);
  133. u8 tmp_out[SEG6_HMAC_MAX_DIGESTSIZE];
  134. int plen, i, dgsize, wrsize;
  135. char *ring, *off;
  136. /* a 160-byte buffer for digest output allows to store highest known
  137. * hash function (RadioGatun) with up to 1216 bits
  138. */
  139. /* saddr(16) + first_seg(1) + flags(1) + keyid(4) + seglist(16n) */
  140. plen = 16 + 1 + 1 + 4 + (hdr->first_segment + 1) * 16;
  141. /* this limit allows for 14 segments */
  142. if (plen >= SEG6_HMAC_RING_SIZE)
  143. return -EMSGSIZE;
  144. /* Let's build the HMAC text on the ring buffer. The text is composed
  145. * as follows, in order:
  146. *
  147. * 1. Source IPv6 address (128 bits)
  148. * 2. first_segment value (8 bits)
  149. * 3. Flags (8 bits)
  150. * 4. HMAC Key ID (32 bits)
  151. * 5. All segments in the segments list (n * 128 bits)
  152. */
  153. local_bh_disable();
  154. ring = this_cpu_ptr(hmac_ring);
  155. off = ring;
  156. /* source address */
  157. memcpy(off, saddr, 16);
  158. off += 16;
  159. /* first_segment value */
  160. *off++ = hdr->first_segment;
  161. /* flags */
  162. *off++ = hdr->flags;
  163. /* HMAC Key ID */
  164. memcpy(off, &hmackeyid, 4);
  165. off += 4;
  166. /* all segments in the list */
  167. for (i = 0; i < hdr->first_segment + 1; i++) {
  168. memcpy(off, hdr->segments + i, 16);
  169. off += 16;
  170. }
  171. dgsize = __do_hmac(hinfo, ring, plen, tmp_out,
  172. SEG6_HMAC_MAX_DIGESTSIZE);
  173. local_bh_enable();
  174. if (dgsize < 0)
  175. return dgsize;
  176. wrsize = SEG6_HMAC_FIELD_LEN;
  177. if (wrsize > dgsize)
  178. wrsize = dgsize;
  179. memset(output, 0, SEG6_HMAC_FIELD_LEN);
  180. memcpy(output, tmp_out, wrsize);
  181. return 0;
  182. }
  183. EXPORT_SYMBOL(seg6_hmac_compute);
  184. /* checks if an incoming SR-enabled packet's HMAC status matches
  185. * the incoming policy.
  186. *
  187. * called with rcu_read_lock()
  188. */
  189. bool seg6_hmac_validate_skb(struct sk_buff *skb)
  190. {
  191. u8 hmac_output[SEG6_HMAC_FIELD_LEN];
  192. struct net *net = dev_net(skb->dev);
  193. struct seg6_hmac_info *hinfo;
  194. struct sr6_tlv_hmac *tlv;
  195. struct ipv6_sr_hdr *srh;
  196. struct inet6_dev *idev;
  197. int require_hmac;
  198. idev = __in6_dev_get(skb->dev);
  199. srh = (struct ipv6_sr_hdr *)skb_transport_header(skb);
  200. tlv = seg6_get_tlv_hmac(srh);
  201. require_hmac = READ_ONCE(idev->cnf.seg6_require_hmac);
  202. /* mandatory check but no tlv */
  203. if (require_hmac > 0 && !tlv)
  204. return false;
  205. /* no check */
  206. if (require_hmac < 0)
  207. return true;
  208. /* check only if present */
  209. if (require_hmac == 0 && !tlv)
  210. return true;
  211. /* now, seg6_require_hmac >= 0 && tlv */
  212. hinfo = seg6_hmac_info_lookup(net, be32_to_cpu(tlv->hmackeyid));
  213. if (!hinfo)
  214. return false;
  215. if (seg6_hmac_compute(hinfo, srh, &ipv6_hdr(skb)->saddr, hmac_output))
  216. return false;
  217. if (crypto_memneq(hmac_output, tlv->hmac, SEG6_HMAC_FIELD_LEN))
  218. return false;
  219. return true;
  220. }
  221. EXPORT_SYMBOL(seg6_hmac_validate_skb);
  222. /* called with rcu_read_lock() */
  223. struct seg6_hmac_info *seg6_hmac_info_lookup(struct net *net, u32 key)
  224. {
  225. struct seg6_pernet_data *sdata = seg6_pernet(net);
  226. struct seg6_hmac_info *hinfo;
  227. hinfo = rhashtable_lookup_fast(&sdata->hmac_infos, &key, rht_params);
  228. return hinfo;
  229. }
  230. EXPORT_SYMBOL(seg6_hmac_info_lookup);
  231. int seg6_hmac_info_add(struct net *net, u32 key, struct seg6_hmac_info *hinfo)
  232. {
  233. struct seg6_pernet_data *sdata = seg6_pernet(net);
  234. int err;
  235. if (!__hmac_get_algo(hinfo->alg_id))
  236. return -EINVAL;
  237. err = rhashtable_lookup_insert_fast(&sdata->hmac_infos, &hinfo->node,
  238. rht_params);
  239. return err;
  240. }
  241. EXPORT_SYMBOL(seg6_hmac_info_add);
  242. int seg6_hmac_info_del(struct net *net, u32 key)
  243. {
  244. struct seg6_pernet_data *sdata = seg6_pernet(net);
  245. struct seg6_hmac_info *hinfo;
  246. int err = -ENOENT;
  247. hinfo = rhashtable_lookup_fast(&sdata->hmac_infos, &key, rht_params);
  248. if (!hinfo)
  249. goto out;
  250. err = rhashtable_remove_fast(&sdata->hmac_infos, &hinfo->node,
  251. rht_params);
  252. if (err)
  253. goto out;
  254. seg6_hinfo_release(hinfo);
  255. out:
  256. return err;
  257. }
  258. EXPORT_SYMBOL(seg6_hmac_info_del);
  259. int seg6_push_hmac(struct net *net, struct in6_addr *saddr,
  260. struct ipv6_sr_hdr *srh)
  261. {
  262. struct seg6_hmac_info *hinfo;
  263. struct sr6_tlv_hmac *tlv;
  264. int err = -ENOENT;
  265. tlv = seg6_get_tlv_hmac(srh);
  266. if (!tlv)
  267. return -EINVAL;
  268. rcu_read_lock();
  269. hinfo = seg6_hmac_info_lookup(net, be32_to_cpu(tlv->hmackeyid));
  270. if (!hinfo)
  271. goto out;
  272. memset(tlv->hmac, 0, SEG6_HMAC_FIELD_LEN);
  273. err = seg6_hmac_compute(hinfo, srh, saddr, tlv->hmac);
  274. out:
  275. rcu_read_unlock();
  276. return err;
  277. }
  278. EXPORT_SYMBOL(seg6_push_hmac);
  279. static int seg6_hmac_init_algo(void)
  280. {
  281. struct seg6_hmac_algo *algo;
  282. struct crypto_shash *tfm;
  283. struct shash_desc *shash;
  284. int i, alg_count, cpu;
  285. int ret = -ENOMEM;
  286. alg_count = ARRAY_SIZE(hmac_algos);
  287. for (i = 0; i < alg_count; i++) {
  288. struct crypto_shash **p_tfm;
  289. int shsize;
  290. algo = &hmac_algos[i];
  291. algo->tfms = alloc_percpu(struct crypto_shash *);
  292. if (!algo->tfms)
  293. goto error_out;
  294. for_each_possible_cpu(cpu) {
  295. tfm = crypto_alloc_shash(algo->name, 0, 0);
  296. if (IS_ERR(tfm)) {
  297. ret = PTR_ERR(tfm);
  298. goto error_out;
  299. }
  300. p_tfm = per_cpu_ptr(algo->tfms, cpu);
  301. *p_tfm = tfm;
  302. }
  303. p_tfm = raw_cpu_ptr(algo->tfms);
  304. tfm = *p_tfm;
  305. shsize = sizeof(*shash) + crypto_shash_descsize(tfm);
  306. algo->shashs = alloc_percpu(struct shash_desc *);
  307. if (!algo->shashs)
  308. goto error_out;
  309. for_each_possible_cpu(cpu) {
  310. shash = kzalloc_node(shsize, GFP_KERNEL,
  311. cpu_to_node(cpu));
  312. if (!shash)
  313. goto error_out;
  314. *per_cpu_ptr(algo->shashs, cpu) = shash;
  315. }
  316. }
  317. return 0;
  318. error_out:
  319. seg6_hmac_exit();
  320. return ret;
  321. }
  322. int __init seg6_hmac_init(void)
  323. {
  324. return seg6_hmac_init_algo();
  325. }
  326. int __net_init seg6_hmac_net_init(struct net *net)
  327. {
  328. struct seg6_pernet_data *sdata = seg6_pernet(net);
  329. return rhashtable_init(&sdata->hmac_infos, &rht_params);
  330. }
  331. void seg6_hmac_exit(void)
  332. {
  333. struct seg6_hmac_algo *algo = NULL;
  334. struct crypto_shash *tfm;
  335. struct shash_desc *shash;
  336. int i, alg_count, cpu;
  337. alg_count = ARRAY_SIZE(hmac_algos);
  338. for (i = 0; i < alg_count; i++) {
  339. algo = &hmac_algos[i];
  340. if (algo->shashs) {
  341. for_each_possible_cpu(cpu) {
  342. shash = *per_cpu_ptr(algo->shashs, cpu);
  343. kfree(shash);
  344. }
  345. free_percpu(algo->shashs);
  346. }
  347. if (algo->tfms) {
  348. for_each_possible_cpu(cpu) {
  349. tfm = *per_cpu_ptr(algo->tfms, cpu);
  350. crypto_free_shash(tfm);
  351. }
  352. free_percpu(algo->tfms);
  353. }
  354. }
  355. }
  356. EXPORT_SYMBOL(seg6_hmac_exit);
  357. void __net_exit seg6_hmac_net_exit(struct net *net)
  358. {
  359. struct seg6_pernet_data *sdata = seg6_pernet(net);
  360. rhashtable_free_and_destroy(&sdata->hmac_infos, seg6_free_hi, NULL);
  361. }
  362. EXPORT_SYMBOL(seg6_hmac_net_exit);