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