seg6_hmac.c 9.8 KB

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