seg6_hmac.c 10 KB

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