psp.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ip.h>
  3. #include <linux/skbuff.h>
  4. #include <net/ip6_checksum.h>
  5. #include <net/psp.h>
  6. #include <net/sock.h>
  7. #include "netdevsim.h"
  8. void nsim_psp_handle_ext(struct sk_buff *skb, struct skb_ext *psp_ext)
  9. {
  10. if (psp_ext)
  11. __skb_ext_set(skb, SKB_EXT_PSP, psp_ext);
  12. }
  13. enum skb_drop_reason
  14. nsim_do_psp(struct sk_buff *skb, struct netdevsim *ns,
  15. struct netdevsim *peer_ns, struct skb_ext **psp_ext)
  16. {
  17. enum skb_drop_reason rc = 0;
  18. struct psp_assoc *pas;
  19. struct net *net;
  20. void **ptr;
  21. rcu_read_lock();
  22. pas = psp_skb_get_assoc_rcu(skb);
  23. if (!pas) {
  24. rc = SKB_NOT_DROPPED_YET;
  25. goto out_unlock;
  26. }
  27. if (!skb_transport_header_was_set(skb)) {
  28. rc = SKB_DROP_REASON_PSP_OUTPUT;
  29. goto out_unlock;
  30. }
  31. ptr = psp_assoc_drv_data(pas);
  32. if (*ptr != ns) {
  33. rc = SKB_DROP_REASON_PSP_OUTPUT;
  34. goto out_unlock;
  35. }
  36. net = sock_net(skb->sk);
  37. if (!psp_dev_encapsulate(net, skb, pas->tx.spi, pas->version, 0)) {
  38. rc = SKB_DROP_REASON_PSP_OUTPUT;
  39. goto out_unlock;
  40. }
  41. /* Now pretend we just received this frame */
  42. if (peer_ns->psp.dev->config.versions & (1 << pas->version)) {
  43. bool strip_icv = false;
  44. u8 generation;
  45. /* We cheat a bit and put the generation in the key.
  46. * In real life if generation was too old, then decryption would
  47. * fail. Here, we just make it so a bad key causes a bad
  48. * generation too, and psp_sk_rx_policy_check() will fail.
  49. */
  50. generation = pas->tx.key[0];
  51. skb_ext_reset(skb);
  52. skb->mac_len = ETH_HLEN;
  53. if (psp_dev_rcv(skb, peer_ns->psp.dev->id, generation,
  54. strip_icv)) {
  55. rc = SKB_DROP_REASON_PSP_OUTPUT;
  56. goto out_unlock;
  57. }
  58. *psp_ext = skb->extensions;
  59. refcount_inc(&(*psp_ext)->refcnt);
  60. skb->decrypted = 1;
  61. u64_stats_update_begin(&ns->psp.syncp);
  62. u64_stats_inc(&ns->psp.tx_packets);
  63. u64_stats_inc(&ns->psp.rx_packets);
  64. u64_stats_add(&ns->psp.tx_bytes,
  65. skb->len - skb_inner_transport_offset(skb));
  66. u64_stats_add(&ns->psp.rx_bytes,
  67. skb->len - skb_inner_transport_offset(skb));
  68. u64_stats_update_end(&ns->psp.syncp);
  69. } else {
  70. struct ipv6hdr *ip6h __maybe_unused;
  71. struct iphdr *iph;
  72. struct udphdr *uh;
  73. __wsum csum;
  74. /* Do not decapsulate. Receive the skb with the udp and psp
  75. * headers still there as if this is a normal udp packet.
  76. * psp_dev_encapsulate() sets udp checksum to 0, so we need to
  77. * provide a valid checksum here, so the skb isn't dropped.
  78. */
  79. uh = udp_hdr(skb);
  80. csum = skb_checksum(skb, skb_transport_offset(skb),
  81. ntohs(uh->len), 0);
  82. switch (skb->protocol) {
  83. case htons(ETH_P_IP):
  84. iph = ip_hdr(skb);
  85. uh->check = udp_v4_check(ntohs(uh->len), iph->saddr,
  86. iph->daddr, csum);
  87. break;
  88. #if IS_ENABLED(CONFIG_IPV6)
  89. case htons(ETH_P_IPV6):
  90. ip6h = ipv6_hdr(skb);
  91. uh->check = udp_v6_check(ntohs(uh->len), &ip6h->saddr,
  92. &ip6h->daddr, csum);
  93. break;
  94. #endif
  95. }
  96. uh->check = uh->check ?: CSUM_MANGLED_0;
  97. skb->ip_summed = CHECKSUM_NONE;
  98. }
  99. out_unlock:
  100. rcu_read_unlock();
  101. return rc;
  102. }
  103. static int
  104. nsim_psp_set_config(struct psp_dev *psd, struct psp_dev_config *conf,
  105. struct netlink_ext_ack *extack)
  106. {
  107. return 0;
  108. }
  109. static int
  110. nsim_rx_spi_alloc(struct psp_dev *psd, u32 version,
  111. struct psp_key_parsed *assoc,
  112. struct netlink_ext_ack *extack)
  113. {
  114. struct netdevsim *ns = psd->drv_priv;
  115. unsigned int new;
  116. int i;
  117. new = ++ns->psp.spi & PSP_SPI_KEY_ID;
  118. if (psd->generation & 1)
  119. new |= PSP_SPI_KEY_PHASE;
  120. assoc->spi = cpu_to_be32(new);
  121. assoc->key[0] = psd->generation;
  122. for (i = 1; i < PSP_MAX_KEY; i++)
  123. assoc->key[i] = ns->psp.spi + i;
  124. return 0;
  125. }
  126. static int nsim_assoc_add(struct psp_dev *psd, struct psp_assoc *pas,
  127. struct netlink_ext_ack *extack)
  128. {
  129. struct netdevsim *ns = psd->drv_priv;
  130. void **ptr = psp_assoc_drv_data(pas);
  131. /* Copy drv_priv from psd to assoc */
  132. *ptr = psd->drv_priv;
  133. ns->psp.assoc_cnt++;
  134. return 0;
  135. }
  136. static int nsim_key_rotate(struct psp_dev *psd, struct netlink_ext_ack *extack)
  137. {
  138. return 0;
  139. }
  140. static void nsim_assoc_del(struct psp_dev *psd, struct psp_assoc *pas)
  141. {
  142. struct netdevsim *ns = psd->drv_priv;
  143. void **ptr = psp_assoc_drv_data(pas);
  144. *ptr = NULL;
  145. ns->psp.assoc_cnt--;
  146. }
  147. static void nsim_get_stats(struct psp_dev *psd, struct psp_dev_stats *stats)
  148. {
  149. struct netdevsim *ns = psd->drv_priv;
  150. unsigned int start;
  151. /* WARNING: do *not* blindly zero stats in real drivers!
  152. * All required stats must be reported by the device!
  153. */
  154. memset(stats, 0, sizeof(struct psp_dev_stats));
  155. do {
  156. start = u64_stats_fetch_begin(&ns->psp.syncp);
  157. stats->rx_bytes = u64_stats_read(&ns->psp.rx_bytes);
  158. stats->rx_packets = u64_stats_read(&ns->psp.rx_packets);
  159. stats->tx_bytes = u64_stats_read(&ns->psp.tx_bytes);
  160. stats->tx_packets = u64_stats_read(&ns->psp.tx_packets);
  161. } while (u64_stats_fetch_retry(&ns->psp.syncp, start));
  162. }
  163. static struct psp_dev_ops nsim_psp_ops = {
  164. .set_config = nsim_psp_set_config,
  165. .rx_spi_alloc = nsim_rx_spi_alloc,
  166. .tx_key_add = nsim_assoc_add,
  167. .tx_key_del = nsim_assoc_del,
  168. .key_rotate = nsim_key_rotate,
  169. .get_stats = nsim_get_stats,
  170. };
  171. static struct psp_dev_caps nsim_psp_caps = {
  172. .versions = 1 << PSP_VERSION_HDR0_AES_GCM_128 |
  173. 1 << PSP_VERSION_HDR0_AES_GMAC_128 |
  174. 1 << PSP_VERSION_HDR0_AES_GCM_256 |
  175. 1 << PSP_VERSION_HDR0_AES_GMAC_256,
  176. .assoc_drv_spc = sizeof(void *),
  177. };
  178. void nsim_psp_uninit(struct netdevsim *ns)
  179. {
  180. if (!IS_ERR(ns->psp.dev))
  181. psp_dev_unregister(ns->psp.dev);
  182. WARN_ON(ns->psp.assoc_cnt);
  183. }
  184. static ssize_t
  185. nsim_psp_rereg_write(struct file *file, const char __user *data, size_t count,
  186. loff_t *ppos)
  187. {
  188. struct netdevsim *ns = file->private_data;
  189. int err;
  190. nsim_psp_uninit(ns);
  191. ns->psp.dev = psp_dev_create(ns->netdev, &nsim_psp_ops,
  192. &nsim_psp_caps, ns);
  193. err = PTR_ERR_OR_ZERO(ns->psp.dev);
  194. return err ?: count;
  195. }
  196. static const struct file_operations nsim_psp_rereg_fops = {
  197. .open = simple_open,
  198. .write = nsim_psp_rereg_write,
  199. .llseek = generic_file_llseek,
  200. .owner = THIS_MODULE,
  201. };
  202. int nsim_psp_init(struct netdevsim *ns)
  203. {
  204. struct dentry *ddir = ns->nsim_dev_port->ddir;
  205. int err;
  206. ns->psp.dev = psp_dev_create(ns->netdev, &nsim_psp_ops,
  207. &nsim_psp_caps, ns);
  208. err = PTR_ERR_OR_ZERO(ns->psp.dev);
  209. if (err)
  210. return err;
  211. debugfs_create_file("psp_rereg", 0200, ddir, ns, &nsim_psp_rereg_fops);
  212. return 0;
  213. }