seg6_hmac.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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/rhashtable.h>
  19. #include <linux/netfilter.h>
  20. #include <linux/netfilter_ipv6.h>
  21. #include <net/sock.h>
  22. #include <net/snmp.h>
  23. #include <net/ipv6.h>
  24. #include <net/protocol.h>
  25. #include <net/transp_v6.h>
  26. #include <net/rawv6.h>
  27. #include <net/ndisc.h>
  28. #include <net/ip6_route.h>
  29. #include <net/addrconf.h>
  30. #include <net/xfrm.h>
  31. #include <crypto/sha1.h>
  32. #include <crypto/sha2.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. struct hmac_storage {
  39. local_lock_t bh_lock;
  40. char hmac_ring[SEG6_HMAC_RING_SIZE];
  41. };
  42. static DEFINE_PER_CPU(struct hmac_storage, hmac_storage) = {
  43. .bh_lock = INIT_LOCAL_LOCK(bh_lock),
  44. };
  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 sr6_tlv_hmac *seg6_get_tlv_hmac(struct ipv6_sr_hdr *srh)
  68. {
  69. struct sr6_tlv_hmac *tlv;
  70. if (srh->hdrlen < (srh->first_segment + 1) * 2 + 5)
  71. return NULL;
  72. if (!sr_has_hmac(srh))
  73. return NULL;
  74. tlv = (struct sr6_tlv_hmac *)
  75. ((char *)srh + ((srh->hdrlen + 1) << 3) - 40);
  76. if (tlv->tlvhdr.type != SR6_TLV_HMAC || tlv->tlvhdr.len != 38)
  77. return NULL;
  78. return tlv;
  79. }
  80. int seg6_hmac_compute(struct seg6_hmac_info *hinfo, struct ipv6_sr_hdr *hdr,
  81. struct in6_addr *saddr, u8 *output)
  82. {
  83. __be32 hmackeyid = cpu_to_be32(hinfo->hmackeyid);
  84. int plen, i, ret = 0;
  85. char *ring, *off;
  86. /* saddr(16) + first_seg(1) + flags(1) + keyid(4) + seglist(16n) */
  87. plen = 16 + 1 + 1 + 4 + (hdr->first_segment + 1) * 16;
  88. /* this limit allows for 14 segments */
  89. if (plen >= SEG6_HMAC_RING_SIZE)
  90. return -EMSGSIZE;
  91. /* Let's build the HMAC text on the ring buffer. The text is composed
  92. * as follows, in order:
  93. *
  94. * 1. Source IPv6 address (128 bits)
  95. * 2. first_segment value (8 bits)
  96. * 3. Flags (8 bits)
  97. * 4. HMAC Key ID (32 bits)
  98. * 5. All segments in the segments list (n * 128 bits)
  99. */
  100. local_bh_disable();
  101. local_lock_nested_bh(&hmac_storage.bh_lock);
  102. ring = this_cpu_ptr(hmac_storage.hmac_ring);
  103. off = ring;
  104. /* source address */
  105. memcpy(off, saddr, 16);
  106. off += 16;
  107. /* first_segment value */
  108. *off++ = hdr->first_segment;
  109. /* flags */
  110. *off++ = hdr->flags;
  111. /* HMAC Key ID */
  112. memcpy(off, &hmackeyid, 4);
  113. off += 4;
  114. /* all segments in the list */
  115. for (i = 0; i < hdr->first_segment + 1; i++) {
  116. memcpy(off, hdr->segments + i, 16);
  117. off += 16;
  118. }
  119. switch (hinfo->alg_id) {
  120. case SEG6_HMAC_ALGO_SHA1:
  121. hmac_sha1(&hinfo->key.sha1, ring, plen, output);
  122. static_assert(SEG6_HMAC_FIELD_LEN > SHA1_DIGEST_SIZE);
  123. memset(&output[SHA1_DIGEST_SIZE], 0,
  124. SEG6_HMAC_FIELD_LEN - SHA1_DIGEST_SIZE);
  125. break;
  126. case SEG6_HMAC_ALGO_SHA256:
  127. hmac_sha256(&hinfo->key.sha256, ring, plen, output);
  128. static_assert(SEG6_HMAC_FIELD_LEN == SHA256_DIGEST_SIZE);
  129. break;
  130. default:
  131. WARN_ON_ONCE(1);
  132. ret = -EINVAL;
  133. break;
  134. }
  135. local_unlock_nested_bh(&hmac_storage.bh_lock);
  136. local_bh_enable();
  137. return ret;
  138. }
  139. EXPORT_SYMBOL(seg6_hmac_compute);
  140. /* checks if an incoming SR-enabled packet's HMAC status matches
  141. * the incoming policy.
  142. *
  143. * called with rcu_read_lock()
  144. */
  145. bool seg6_hmac_validate_skb(struct sk_buff *skb)
  146. {
  147. u8 hmac_output[SEG6_HMAC_FIELD_LEN];
  148. struct net *net = dev_net(skb->dev);
  149. struct seg6_hmac_info *hinfo;
  150. struct sr6_tlv_hmac *tlv;
  151. struct ipv6_sr_hdr *srh;
  152. struct inet6_dev *idev;
  153. int require_hmac;
  154. idev = __in6_dev_get(skb->dev);
  155. if (!idev)
  156. return false;
  157. srh = (struct ipv6_sr_hdr *)skb_transport_header(skb);
  158. tlv = seg6_get_tlv_hmac(srh);
  159. require_hmac = READ_ONCE(idev->cnf.seg6_require_hmac);
  160. /* mandatory check but no tlv */
  161. if (require_hmac > 0 && !tlv)
  162. return false;
  163. /* no check */
  164. if (require_hmac < 0)
  165. return true;
  166. /* check only if present */
  167. if (require_hmac == 0 && !tlv)
  168. return true;
  169. /* now, seg6_require_hmac >= 0 && tlv */
  170. hinfo = seg6_hmac_info_lookup(net, be32_to_cpu(tlv->hmackeyid));
  171. if (!hinfo)
  172. return false;
  173. if (seg6_hmac_compute(hinfo, srh, &ipv6_hdr(skb)->saddr, hmac_output))
  174. return false;
  175. if (crypto_memneq(hmac_output, tlv->hmac, SEG6_HMAC_FIELD_LEN))
  176. return false;
  177. return true;
  178. }
  179. EXPORT_SYMBOL(seg6_hmac_validate_skb);
  180. /* called with rcu_read_lock() */
  181. struct seg6_hmac_info *seg6_hmac_info_lookup(struct net *net, u32 key)
  182. {
  183. struct seg6_pernet_data *sdata = seg6_pernet(net);
  184. struct seg6_hmac_info *hinfo;
  185. hinfo = rhashtable_lookup_fast(&sdata->hmac_infos, &key, rht_params);
  186. return hinfo;
  187. }
  188. EXPORT_SYMBOL(seg6_hmac_info_lookup);
  189. int seg6_hmac_info_add(struct net *net, u32 key, struct seg6_hmac_info *hinfo)
  190. {
  191. struct seg6_pernet_data *sdata = seg6_pernet(net);
  192. int err;
  193. switch (hinfo->alg_id) {
  194. case SEG6_HMAC_ALGO_SHA1:
  195. hmac_sha1_preparekey(&hinfo->key.sha1,
  196. hinfo->secret, hinfo->slen);
  197. break;
  198. case SEG6_HMAC_ALGO_SHA256:
  199. hmac_sha256_preparekey(&hinfo->key.sha256,
  200. hinfo->secret, hinfo->slen);
  201. break;
  202. default:
  203. return -EINVAL;
  204. }
  205. err = rhashtable_lookup_insert_fast(&sdata->hmac_infos, &hinfo->node,
  206. rht_params);
  207. return err;
  208. }
  209. EXPORT_SYMBOL(seg6_hmac_info_add);
  210. int seg6_hmac_info_del(struct net *net, u32 key)
  211. {
  212. struct seg6_pernet_data *sdata = seg6_pernet(net);
  213. struct seg6_hmac_info *hinfo;
  214. int err = -ENOENT;
  215. hinfo = rhashtable_lookup_fast(&sdata->hmac_infos, &key, rht_params);
  216. if (!hinfo)
  217. goto out;
  218. err = rhashtable_remove_fast(&sdata->hmac_infos, &hinfo->node,
  219. rht_params);
  220. if (err)
  221. goto out;
  222. seg6_hinfo_release(hinfo);
  223. out:
  224. return err;
  225. }
  226. EXPORT_SYMBOL(seg6_hmac_info_del);
  227. int seg6_push_hmac(struct net *net, struct in6_addr *saddr,
  228. struct ipv6_sr_hdr *srh)
  229. {
  230. struct seg6_hmac_info *hinfo;
  231. struct sr6_tlv_hmac *tlv;
  232. int err = -ENOENT;
  233. tlv = seg6_get_tlv_hmac(srh);
  234. if (!tlv)
  235. return -EINVAL;
  236. rcu_read_lock();
  237. hinfo = seg6_hmac_info_lookup(net, be32_to_cpu(tlv->hmackeyid));
  238. if (!hinfo)
  239. goto out;
  240. memset(tlv->hmac, 0, SEG6_HMAC_FIELD_LEN);
  241. err = seg6_hmac_compute(hinfo, srh, saddr, tlv->hmac);
  242. out:
  243. rcu_read_unlock();
  244. return err;
  245. }
  246. EXPORT_SYMBOL(seg6_push_hmac);
  247. int __net_init seg6_hmac_net_init(struct net *net)
  248. {
  249. struct seg6_pernet_data *sdata = seg6_pernet(net);
  250. return rhashtable_init(&sdata->hmac_infos, &rht_params);
  251. }
  252. void __net_exit seg6_hmac_net_exit(struct net *net)
  253. {
  254. struct seg6_pernet_data *sdata = seg6_pernet(net);
  255. rhashtable_free_and_destroy(&sdata->hmac_infos, seg6_free_hi, NULL);
  256. }
  257. EXPORT_SYMBOL(seg6_hmac_net_exit);