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