seg6_hmac.c 9.8 KB

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