Debian Codegraph @

drivers/net/ovpn/netlink.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /* OpenVPN data channel offload
3 *
4 * Copyright (C) 2020-2025 OpenVPN, Inc.
5 *
6 * Author: Antonio Quartulli <[email protected]>
7 */
8
9 #include <linux/netdevice.h>
10 #include <linux/types.h>
11 #include <net/genetlink.h>
12
13 #include <uapi/linux/ovpn.h>
14
15 #include "ovpnpriv.h"
16 #include "main.h"
17 #include "netlink.h"
18 #include "netlink-gen.h"
19 #include "bind.h"
20 #include "crypto.h"
21 #include "peer.h"
22 #include "socket.h"
23
24 MODULE_ALIAS_GENL_FAMILY(OVPN_FAMILY_NAME);
25
26 /**
27 * ovpn_get_dev_from_attrs - retrieve the ovpn private data from the netdevice
28 * a netlink message is targeting
29 * @net: network namespace where to look for the interface
30 * @info: generic netlink info from the user request
31 * @tracker: tracker object to be used for the netdev reference acquisition
32 *
33 * Return: the ovpn private data, if found, or an error otherwise
34 */
35 static struct ovpn_priv *
36 ovpn_get_dev_from_attrs(struct net *net, const struct genl_info *info,
37 netdevice_tracker *tracker)
38 {
39 struct ovpn_priv *ovpn;
40 struct net_device *dev;
41 int ifindex;
42
43 if (GENL_REQ_ATTR_CHECK(info, OVPN_A_IFINDEX))
44 return ERR_PTR(-EINVAL);
45
46 ifindex = nla_get_u32(info->attrs[OVPN_A_IFINDEX]);
47
48 rcu_read_lock();
49 dev = dev_get_by_index_rcu(net, ifindex);
50 if (!dev) {
51 rcu_read_unlock();
52 NL_SET_ERR_MSG_MOD(info->extack,
53 "ifindex does not match any interface");
54 return ERR_PTR(-ENODEV);
55 }
56
57 if (!ovpn_dev_is_valid(dev)) {
58 rcu_read_unlock();
59 NL_SET_ERR_MSG_MOD(info->extack,
60 "specified interface is not ovpn");
61 NL_SET_BAD_ATTR(info->extack, info->attrs[OVPN_A_IFINDEX]);
62 return ERR_PTR(-EINVAL);
63 }
64
65 ovpn = netdev_priv(dev);
66 netdev_hold(dev, tracker, GFP_ATOMIC);
67 rcu_read_unlock();
68
69 return ovpn;
70 }
71
72 int ovpn_nl_pre_doit(const struct genl_split_ops *ops, struct sk_buff *skb,
73 struct genl_info *info)
74 {
75 netdevice_tracker *tracker = (netdevice_tracker *)&info->user_ptr[1];
76 struct ovpn_priv *ovpn = ovpn_get_dev_from_attrs(genl_info_net(info),
77 info, tracker);
78
79 if (IS_ERR(ovpn))
80 return PTR_ERR(ovpn);
81
82 info->user_ptr[0] = ovpn;
83
84 return 0;
85 }
86
87 void ovpn_nl_post_doit(const struct genl_split_ops *ops, struct sk_buff *skb,
88 struct genl_info *info)
89 {
90 netdevice_tracker *tracker = (netdevice_tracker *)&info->user_ptr[1];
91 struct ovpn_priv *ovpn = info->user_ptr[0];
92
93 if (ovpn)
94 netdev_put(ovpn->dev, tracker);
95 }
96
97 static bool ovpn_nl_attr_sockaddr_remote(struct nlattr **attrs,
98 struct sockaddr_storage *ss)
99 {
100 struct sockaddr_in6 *sin6;
101 struct sockaddr_in *sin;
102 struct in6_addr *in6;
103 __be16 port = 0;
104 __be32 *in;
105
106 ss->ss_family = AF_UNSPEC;
107
108 if (attrs[OVPN_A_PEER_REMOTE_PORT])
109 port = nla_get_be16(attrs[OVPN_A_PEER_REMOTE_PORT]);
110
111 if (attrs[OVPN_A_PEER_REMOTE_IPV4]) {
112 ss->ss_family = AF_INET;
113 in = nla_data(attrs[OVPN_A_PEER_REMOTE_IPV4]);
114 } else if (attrs[OVPN_A_PEER_REMOTE_IPV6]) {
115 ss->ss_family = AF_INET6;
116 in6 = nla_data(attrs[OVPN_A_PEER_REMOTE_IPV6]);
117 } else {
118 return false;
119 }
120
121 switch (ss->ss_family) {
122 case AF_INET6:
123 /* If this is a regular IPv6 just break and move on,
124 * otherwise switch to AF_INET and extract the IPv4 accordingly
125 */
126 if (!ipv6_addr_v4mapped(in6)) {
127 sin6 = (struct sockaddr_in6 *)ss;
128 sin6->sin6_port = port;
129 memcpy(&sin6->sin6_addr, in6, sizeof(*in6));
130 break;
131 }
132
133 /* v4-mapped-v6 address */
134 ss->ss_family = AF_INET;
135 in = &in6->s6_addr32[3];
136 fallthrough;
137 case AF_INET:
138 sin = (struct sockaddr_in *)ss;
139 sin->sin_port = port;
140 sin->sin_addr.s_addr = *in;
141 break;
142 }
143
144 return true;
145 }
146
147 static u8 *ovpn_nl_attr_local_ip(struct nlattr **attrs)
148 {
149 u8 *addr6;
150
151 if (!attrs[OVPN_A_PEER_LOCAL_IPV4] && !attrs[OVPN_A_PEER_LOCAL_IPV6])
152 return NULL;
153
154 if (attrs[OVPN_A_PEER_LOCAL_IPV4])
155 return nla_data(attrs[OVPN_A_PEER_LOCAL_IPV4]);
156
157 addr6 = nla_data(attrs[OVPN_A_PEER_LOCAL_IPV6]);
158 /* this is an IPv4-mapped IPv6 address, therefore extract the actual
159 * v4 address from the last 4 bytes
160 */
161 if (ipv6_addr_v4mapped((struct in6_addr *)addr6))
162 return addr6 + 12;
163
164 return addr6;
165 }
166
167 static sa_family_t ovpn_nl_family_get(struct nlattr *addr4,
168 struct nlattr *addr6)
169 {
170 if (addr4)
171 return AF_INET;
172
173 if (addr6) {
174 if (ipv6_addr_v4mapped((struct in6_addr *)nla_data(addr6)))
175 return AF_INET;
176 return AF_INET6;
177 }
178
179 return AF_UNSPEC;
180 }
181
182 static int ovpn_nl_peer_precheck(struct ovpn_priv *ovpn,
183 struct genl_info *info,
184 struct nlattr **attrs)
185 {
186 sa_family_t local_fam, remote_fam;
187
188 if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_PEER], attrs,
189 OVPN_A_PEER_ID))
190 return -EINVAL;
191
192 if (attrs[OVPN_A_PEER_REMOTE_IPV4] && attrs[OVPN_A_PEER_REMOTE_IPV6]) {
193 NL_SET_ERR_MSG_MOD(info->extack,
194 "cannot specify both remote IPv4 or IPv6 address");
195 return -EINVAL;
196 }
197
198 if (!attrs[OVPN_A_PEER_REMOTE_IPV4] &&
199 !attrs[OVPN_A_PEER_REMOTE_IPV6] && attrs[OVPN_A_PEER_REMOTE_PORT]) {
200 NL_SET_ERR_MSG_MOD(info->extack,
201 "cannot specify remote port without IP address");
202 return -EINVAL;
203 }
204
205 if ((attrs[OVPN_A_PEER_REMOTE_IPV4] ||
206 attrs[OVPN_A_PEER_REMOTE_IPV6]) &&
207 !attrs[OVPN_A_PEER_REMOTE_PORT]) {
208 NL_SET_ERR_MSG_MOD(info->extack,
209 "cannot specify remote IP address without port");
210 return -EINVAL;
211 }
212
213 if (!attrs[OVPN_A_PEER_REMOTE_IPV4] &&
214 attrs[OVPN_A_PEER_LOCAL_IPV4]) {
215 NL_SET_ERR_MSG_MOD(info->extack,
216 "cannot specify local IPv4 address without remote");
217 return -EINVAL;
218 }
219
220 if (!attrs[OVPN_A_PEER_REMOTE_IPV6] &&
221 attrs[OVPN_A_PEER_LOCAL_IPV6]) {
222 NL_SET_ERR_MSG_MOD(info->extack,
223 "cannot specify local IPV6 address without remote");
224 return -EINVAL;
225 }
226
227 /* check that local and remote address families are the same even
228 * after parsing v4mapped IPv6 addresses.
229 * (if addresses are not provided, family will be AF_UNSPEC and
230 * the check is skipped)
231 */
232 local_fam = ovpn_nl_family_get(attrs[OVPN_A_PEER_LOCAL_IPV4],
233 attrs[OVPN_A_PEER_LOCAL_IPV6]);
234 remote_fam = ovpn_nl_family_get(attrs[OVPN_A_PEER_REMOTE_IPV4],
235 attrs[OVPN_A_PEER_REMOTE_IPV6]);
236 if (local_fam != AF_UNSPEC && remote_fam != AF_UNSPEC &&
237 local_fam != remote_fam) {
238 NL_SET_ERR_MSG_MOD(info->extack,
239 "mismatching local and remote address families");
240 return -EINVAL;
241 }
242
243 if (remote_fam != AF_INET6 && attrs[OVPN_A_PEER_REMOTE_IPV6_SCOPE_ID]) {
244 NL_SET_ERR_MSG_MOD(info->extack,
245 "cannot specify scope id without remote IPv6 address");
246 return -EINVAL;
247 }
248
249 /* VPN IPs are needed only in MP mode for selecting the right peer */
250 if (ovpn->mode == OVPN_MODE_P2P && (attrs[OVPN_A_PEER_VPN_IPV4] ||
251 attrs[OVPN_A_PEER_VPN_IPV6])) {
252 NL_SET_ERR_MSG_FMT_MOD(info->extack,
253 "unexpected VPN IP in P2P mode");
254 return -EINVAL;
255 }
256
257 if ((attrs[OVPN_A_PEER_KEEPALIVE_INTERVAL] &&
258 !attrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT]) ||
259 (!attrs[OVPN_A_PEER_KEEPALIVE_INTERVAL] &&
260 attrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT])) {
261 NL_SET_ERR_MSG_FMT_MOD(info->extack,
262 "keepalive interval and timeout are required together");
263 return -EINVAL;
264 }
265
266 return 0;
267 }
268
269 /**
270 * ovpn_nl_peer_modify - modify the peer attributes according to the incoming msg
271 * @peer: the peer to modify
272 * @info: generic netlink info from the user request
273 * @attrs: the attributes from the user request
274 *
275 * Return: a negative error code in case of failure, 0 on success or 1 on
276 * success and the VPN IPs have been modified (requires rehashing in MP
277 * mode)
278 */
279 static int ovpn_nl_peer_modify(struct ovpn_peer *peer, struct genl_info *info,
280 struct nlattr **attrs)
281 {
282 struct sockaddr_storage ss = {};
283 void *local_ip = NULL;
284 u32 interv, timeout;
285 bool rehash = false;
286 int ret;
287
288 spin_lock_bh(&peer->lock);
289
290 if (ovpn_nl_attr_sockaddr_remote(attrs, &ss)) {
291 /* we carry the local IP in a generic container.
292 * ovpn_peer_reset_sockaddr() will properly interpret it
293 * based on ss.ss_family
294 */
295 local_ip = ovpn_nl_attr_local_ip(attrs);
296
297 /* set peer sockaddr */
298 ret = ovpn_peer_reset_sockaddr(peer, &ss, local_ip);
299 if (ret < 0) {
300 NL_SET_ERR_MSG_FMT_MOD(info->extack,
301 "cannot set peer sockaddr: %d",
302 ret);
303 goto err_unlock;
304 }
305 dst_cache_reset(&peer->dst_cache);
306 }
307
308 if (attrs[OVPN_A_PEER_VPN_IPV4]) {
309 rehash = true;
310 peer->vpn_addrs.ipv4.s_addr =
311 nla_get_in_addr(attrs[OVPN_A_PEER_VPN_IPV4]);
312 }
313
314 if (attrs[OVPN_A_PEER_VPN_IPV6]) {
315 rehash = true;
316 peer->vpn_addrs.ipv6 =
317 nla_get_in6_addr(attrs[OVPN_A_PEER_VPN_IPV6]);
318 }
319
320 /* when setting the keepalive, both parameters have to be configured */
321 if (attrs[OVPN_A_PEER_KEEPALIVE_INTERVAL] &&
322 attrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT]) {
323 interv = nla_get_u32(attrs[OVPN_A_PEER_KEEPALIVE_INTERVAL]);
324 timeout = nla_get_u32(attrs[OVPN_A_PEER_KEEPALIVE_TIMEOUT]);
325 ovpn_peer_keepalive_set(peer, interv, timeout);
326 }
327
328 netdev_dbg(peer->ovpn->dev,
329 "modify peer id=%u endpoint=%pIScp VPN-IPv4=%pI4 VPN-IPv6=%pI6c\n",
330 peer->id, &ss,
331 &peer->vpn_addrs.ipv4.s_addr, &peer->vpn_addrs.ipv6);
332
333 spin_unlock_bh(&peer->lock);
334
335 return rehash ? 1 : 0;
336 err_unlock:
337 spin_unlock_bh(&peer->lock);
338 return ret;
339 }
340
341 int ovpn_nl_peer_new_doit(struct sk_buff *skb, struct genl_info *info)
342 {
343 struct nlattr *attrs[OVPN_A_PEER_MAX + 1];
344 struct ovpn_priv *ovpn = info->user_ptr[0];
345 struct ovpn_socket *ovpn_sock;
346 struct socket *sock = NULL;
347 struct ovpn_peer *peer;
348 u32 sockfd, peer_id;
349 int ret;
350
351 if (GENL_REQ_ATTR_CHECK(info, OVPN_A_PEER))
352 return -EINVAL;
353
354 ret = nla_parse_nested(attrs, OVPN_A_PEER_MAX, info->attrs[OVPN_A_PEER],
355 ovpn_peer_new_input_nl_policy, info->extack);
356 if (ret)
357 return ret;
358
359 ret = ovpn_nl_peer_precheck(ovpn, info, attrs);
360 if (ret < 0)
361 return ret;
362
363 if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_PEER], attrs,
364 OVPN_A_PEER_SOCKET))
365 return -EINVAL;
366
367 /* in MP mode VPN IPs are required for selecting the right peer */
368 if (ovpn->mode == OVPN_MODE_MP && !attrs[OVPN_A_PEER_VPN_IPV4] &&
369 !attrs[OVPN_A_PEER_VPN_IPV6]) {
370 NL_SET_ERR_MSG_FMT_MOD(info->extack,
371 "VPN IP must be provided in MP mode");
372 return -EINVAL;
373 }
374
375 peer_id = nla_get_u32(attrs[OVPN_A_PEER_ID]);
376 peer = ovpn_peer_new(ovpn, peer_id);
377 if (IS_ERR(peer)) {
378 NL_SET_ERR_MSG_FMT_MOD(info->extack,
379 "cannot create new peer object for peer %u: %ld",
380 peer_id, PTR_ERR(peer));
381 return PTR_ERR(peer);
382 }
383
384 /* lookup the fd in the kernel table and extract the socket object */
385 sockfd = nla_get_u32(attrs[OVPN_A_PEER_SOCKET]);
386 /* sockfd_lookup() increases sock's refcounter */
387 sock = sockfd_lookup(sockfd, &ret);
388 if (!sock) {
389 NL_SET_ERR_MSG_FMT_MOD(info->extack,
390 "cannot lookup peer socket (fd=%u): %d",
391 sockfd, ret);
392 ret = -ENOTSOCK;
393 goto peer_release;
394 }
395
396 /* Only when using UDP as transport protocol the remote endpoint
397 * can be configured so that ovpn knows where to send packets to.
398 */
399 if (sock->sk->sk_protocol == IPPROTO_UDP &&
400 !attrs[OVPN_A_PEER_REMOTE_IPV4] &&
401 !attrs[OVPN_A_PEER_REMOTE_IPV6]) {
402 NL_SET_ERR_MSG_FMT_MOD(info->extack,
403 "missing remote IP address for UDP socket");
404 sockfd_put(sock);
405 ret = -EINVAL;
406 goto peer_release;
407 }
408
409 /* In case of TCP, the socket is connected to the peer and ovpn
410 * will just send bytes over it, without the need to specify a
411 * destination.
412 */
413 if (sock->sk->sk_protocol == IPPROTO_TCP &&
414 (attrs[OVPN_A_PEER_REMOTE_IPV4] ||
415 attrs[OVPN_A_PEER_REMOTE_IPV6])) {
416 NL_SET_ERR_MSG_FMT_MOD(info->extack,
417 "unexpected remote IP address with TCP socket");
418 sockfd_put(sock);
419 ret = -EINVAL;
420 goto peer_release;
421 }
422
423 ovpn_sock = ovpn_socket_new(sock, peer);
424 /* at this point we unconditionally drop the reference to the socket:
425 * - in case of error, the socket has to be dropped
426 * - if case of success, the socket is configured and let
427 * userspace own the reference, so that the latter can
428 * trigger the final close()
429 */
430 sockfd_put(sock);
431 if (IS_ERR(ovpn_sock)) {
432 NL_SET_ERR_MSG_FMT_MOD(info->extack,
433 "cannot encapsulate socket: %ld",
434 PTR_ERR(ovpn_sock));
435 ret = -ENOTSOCK;
436 goto peer_release;
437 }
438
439 rcu_assign_pointer(peer->sock, ovpn_sock);
440
441 ret = ovpn_nl_peer_modify(peer, info, attrs);
442 if (ret < 0)
443 goto sock_release;
444
445 ret = ovpn_peer_add(ovpn, peer);
446 if (ret < 0) {
447 NL_SET_ERR_MSG_FMT_MOD(info->extack,
448 "cannot add new peer (id=%u) to hashtable: %d",
449 peer->id, ret);
450 goto sock_release;
451 }
452
453 return 0;
454
455 sock_release:
456 ovpn_socket_release(peer);
457 peer_release:
458 /* For UDP, the peer is unreachable until added to the hashtables, so
459 * dropping the initial reference is enough. For TCP, the peer may be
460 * concurrently reachable via sk_user_data->peer until
461 * ovpn_socket_release() detaches; rely on the refcount.
462 */
463 ovpn_peer_put(peer);
464
465 return ret;
466 }
467
468 int ovpn_nl_peer_set_doit(struct sk_buff *skb, struct genl_info *info)
469 {
470 struct nlattr *attrs[OVPN_A_PEER_MAX + 1];
471 struct ovpn_priv *ovpn = info->user_ptr[0];
472 struct ovpn_socket *sock;
473 struct ovpn_peer *peer;
474 u32 peer_id;
475 int ret;
476
477 if (GENL_REQ_ATTR_CHECK(info, OVPN_A_PEER))
478 return -EINVAL;
479
480 ret = nla_parse_nested(attrs, OVPN_A_PEER_MAX, info->attrs[OVPN_A_PEER],
481 ovpn_peer_set_input_nl_policy, info->extack);
482 if (ret)
483 return ret;
484
485 ret = ovpn_nl_peer_precheck(ovpn, info, attrs);
486 if (ret < 0)
487 return ret;
488
489 if (attrs[OVPN_A_PEER_SOCKET]) {
490 NL_SET_ERR_MSG_FMT_MOD(info->extack,
491 "socket cannot be modified");
492 return -EINVAL;
493 }
494
495 peer_id = nla_get_u32(attrs[OVPN_A_PEER_ID]);
496 peer = ovpn_peer_get_by_id(ovpn, peer_id);
497 if (!peer) {
498 NL_SET_ERR_MSG_FMT_MOD(info->extack,
499 "cannot find peer with id %u", peer_id);
500 return -ENOENT;
501 }
502
503 /* when using a TCP socket the remote IP is not expected */
504 rcu_read_lock();
505 sock = rcu_dereference(peer->sock);
506 if (sock && sock->sk->sk_protocol == IPPROTO_TCP &&
507 (attrs[OVPN_A_PEER_REMOTE_IPV4] ||
508 attrs[OVPN_A_PEER_REMOTE_IPV6])) {
509 rcu_read_unlock();
510 NL_SET_ERR_MSG_FMT_MOD(info->extack,
511 "unexpected remote IP address with TCP socket");
512 ovpn_peer_put(peer);
513 return -EINVAL;
514 }
515 rcu_read_unlock();
516
517 spin_lock_bh(&ovpn->lock);
518 ret = ovpn_nl_peer_modify(peer, info, attrs);
519 if (ret < 0) {
520 spin_unlock_bh(&ovpn->lock);
521 ovpn_peer_put(peer);
522 return ret;
523 }
524
525 /* ret == 1 means that VPN IPv4/6 has been modified and rehashing
526 * is required
527 */
528 if (ret > 0)
529 ovpn_peer_hash_vpn_ip(peer);
530 spin_unlock_bh(&ovpn->lock);
531 ovpn_peer_put(peer);
532
533 return 0;
534 }
535
536 static int ovpn_nl_send_peer(struct sk_buff *skb, const struct genl_info *info,
537 const struct ovpn_peer *peer, u32 portid, u32 seq,
538 int flags)
539 {
540 const struct ovpn_bind *bind;
541 struct ovpn_socket *sock;
542 int ret = -EMSGSIZE;
543 struct nlattr *attr;
544 __be16 local_port;
545 void *hdr;
546 int id;
547
548 hdr = genlmsg_put(skb, portid, seq, &ovpn_nl_family, flags,
549 OVPN_CMD_PEER_GET);
550 if (!hdr)
551 return -ENOBUFS;
552
553 attr = nla_nest_start(skb, OVPN_A_PEER);
554 if (!attr)
555 goto err;
556
557 rcu_read_lock();
558 sock = rcu_dereference(peer->sock);
559 if (!sock) {
560 ret = -EINVAL;
561 goto err_unlock;
562 }
563
564 if (!net_eq(genl_info_net(info), sock_net(sock->sk))) {
565 id = peernet2id_alloc(genl_info_net(info),
566 sock_net(sock->sk),
567 GFP_ATOMIC);
568 if (nla_put_s32(skb, OVPN_A_PEER_SOCKET_NETNSID, id))
569 goto err_unlock;
570 }
571 local_port = inet_sk(sock->sk)->inet_sport;
572 rcu_read_unlock();
573
574 if (nla_put_u32(skb, OVPN_A_PEER_ID, peer->id))
575 goto err;
576
577 if (peer->vpn_addrs.ipv4.s_addr != htonl(INADDR_ANY))
578 if (nla_put_in_addr(skb, OVPN_A_PEER_VPN_IPV4,
579 peer->vpn_addrs.ipv4.s_addr))
580 goto err;
581
582 if (!ipv6_addr_equal(&peer->vpn_addrs.ipv6, &in6addr_any))
583 if (nla_put_in6_addr(skb, OVPN_A_PEER_VPN_IPV6,
584 &peer->vpn_addrs.ipv6))
585 goto err;
586
587 if (nla_put_u32(skb, OVPN_A_PEER_KEEPALIVE_INTERVAL,
588 peer->keepalive_interval) ||
589 nla_put_u32(skb, OVPN_A_PEER_KEEPALIVE_TIMEOUT,
590 peer->keepalive_timeout))
591 goto err;
592
593 rcu_read_lock();
594 bind = rcu_dereference(peer->bind);
595 if (bind) {
596 if (bind->remote.in4.sin_family == AF_INET) {
597 if (nla_put_in_addr(skb, OVPN_A_PEER_REMOTE_IPV4,
598 bind->remote.in4.sin_addr.s_addr) ||
599 nla_put_net16(skb, OVPN_A_PEER_REMOTE_PORT,
600 bind->remote.in4.sin_port) ||
601 nla_put_in_addr(skb, OVPN_A_PEER_LOCAL_IPV4,
602 bind->local.ipv4.s_addr))
603 goto err_unlock;
604 } else if (bind->remote.in4.sin_family == AF_INET6) {
605 if (nla_put_in6_addr(skb, OVPN_A_PEER_REMOTE_IPV6,
606 &bind->remote.in6.sin6_addr) ||
607 nla_put_u32(skb, OVPN_A_PEER_REMOTE_IPV6_SCOPE_ID,
608 bind->remote.in6.sin6_scope_id) ||
609 nla_put_net16(skb, OVPN_A_PEER_REMOTE_PORT,
610 bind->remote.in6.sin6_port) ||
611 nla_put_in6_addr(skb, OVPN_A_PEER_LOCAL_IPV6,
612 &bind->local.ipv6))
613 goto err_unlock;
614 }
615 }
616 rcu_read_unlock();
617
618 if (nla_put_net16(skb, OVPN_A_PEER_LOCAL_PORT, local_port) ||
619 /* VPN RX stats */
620 nla_put_uint(skb, OVPN_A_PEER_VPN_RX_BYTES,
621 atomic64_read(&peer->vpn_stats.rx.bytes)) ||
622 nla_put_uint(skb, OVPN_A_PEER_VPN_RX_PACKETS,
623 atomic64_read(&peer->vpn_stats.rx.packets)) ||
624 /* VPN TX stats */
625 nla_put_uint(skb, OVPN_A_PEER_VPN_TX_BYTES,
626 atomic64_read(&peer->vpn_stats.tx.bytes)) ||
627 nla_put_uint(skb, OVPN_A_PEER_VPN_TX_PACKETS,
628 atomic64_read(&peer->vpn_stats.tx.packets)) ||
629 /* link RX stats */
630 nla_put_uint(skb, OVPN_A_PEER_LINK_RX_BYTES,
631 atomic64_read(&peer->link_stats.rx.bytes)) ||
632 nla_put_uint(skb, OVPN_A_PEER_LINK_RX_PACKETS,
633 atomic64_read(&peer->link_stats.rx.packets)) ||
634 /* link TX stats */
635 nla_put_uint(skb, OVPN_A_PEER_LINK_TX_BYTES,
636 atomic64_read(&peer->link_stats.tx.bytes)) ||
637 nla_put_uint(skb, OVPN_A_PEER_LINK_TX_PACKETS,
638 atomic64_read(&peer->link_stats.tx.packets)))
639 goto err;
640
641 nla_nest_end(skb, attr);
642 genlmsg_end(skb, hdr);
643
644 return 0;
645 err_unlock:
646 rcu_read_unlock();
647 err:
648 genlmsg_cancel(skb, hdr);
649 return ret;
650 }
651
652 int ovpn_nl_peer_get_doit(struct sk_buff *skb, struct genl_info *info)
653 {
654 struct nlattr *attrs[OVPN_A_PEER_MAX + 1];
655 struct ovpn_priv *ovpn = info->user_ptr[0];
656 struct ovpn_peer *peer;
657 struct sk_buff *msg;
658 u32 peer_id;
659 int ret, i;
660
661 if (GENL_REQ_ATTR_CHECK(info, OVPN_A_PEER))
662 return -EINVAL;
663
664 ret = nla_parse_nested(attrs, OVPN_A_PEER_MAX, info->attrs[OVPN_A_PEER],
665 ovpn_peer_nl_policy, info->extack);
666 if (ret)
667 return ret;
668
669 if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_PEER], attrs,
670 OVPN_A_PEER_ID))
671 return -EINVAL;
672
673 /* OVPN_CMD_PEER_GET expects only the PEER_ID, therefore
674 * ensure that the user hasn't specified any other attribute.
675 *
676 * Unfortunately this check cannot be performed via netlink
677 * spec/policy and must be open-coded.
678 */
679 for (i = 0; i < OVPN_A_PEER_MAX + 1; i++) {
680 if (i == OVPN_A_PEER_ID)
681 continue;
682
683 if (attrs[i]) {
684 NL_SET_ERR_MSG_FMT_MOD(info->extack,
685 "unexpected attribute %u", i);
686 return -EINVAL;
687 }
688 }
689
690 peer_id = nla_get_u32(attrs[OVPN_A_PEER_ID]);
691 peer = ovpn_peer_get_by_id(ovpn, peer_id);
692 if (!peer) {
693 NL_SET_ERR_MSG_FMT_MOD(info->extack,
694 "cannot find peer with id %u", peer_id);
695 return -ENOENT;
696 }
697
698 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
699 if (!msg) {
700 ret = -ENOMEM;
701 goto err;
702 }
703
704 ret = ovpn_nl_send_peer(msg, info, peer, info->snd_portid,
705 info->snd_seq, 0);
706 if (ret < 0) {
707 nlmsg_free(msg);
708 goto err;
709 }
710
711 ret = genlmsg_reply(msg, info);
712 err:
713 ovpn_peer_put(peer);
714 return ret;
715 }
716
717 int ovpn_nl_peer_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
718 {
719 const struct genl_info *info = genl_info_dump(cb);
720 int bkt, last_idx = cb->args[1], dumped = 0;
721 netdevice_tracker tracker;
722 struct ovpn_priv *ovpn;
723 struct ovpn_peer *peer;
724
725 ovpn = ovpn_get_dev_from_attrs(sock_net(cb->skb->sk), info, &tracker);
726 if (IS_ERR(ovpn))
727 return PTR_ERR(ovpn);
728
729 if (ovpn->mode == OVPN_MODE_P2P) {
730 /* if we already dumped a peer it means we are done */
731 if (last_idx)
732 goto out;
733
734 rcu_read_lock();
735 peer = rcu_dereference(ovpn->peer);
736 if (peer) {
737 if (ovpn_nl_send_peer(skb, info, peer,
738 NETLINK_CB(cb->skb).portid,
739 cb->nlh->nlmsg_seq,
740 NLM_F_MULTI) == 0)
741 dumped++;
742 }
743 rcu_read_unlock();
744 } else {
745 rcu_read_lock();
746 hash_for_each_rcu(ovpn->peers->by_id, bkt, peer,
747 hash_entry_id) {
748 /* skip already dumped peers that were dumped by
749 * previous invocations
750 */
751 if (last_idx > 0) {
752 last_idx--;
753 continue;
754 }
755
756 if (ovpn_nl_send_peer(skb, info, peer,
757 NETLINK_CB(cb->skb).portid,
758 cb->nlh->nlmsg_seq,
759 NLM_F_MULTI) < 0)
760 break;
761
762 /* count peers being dumped during this invocation */
763 dumped++;
764 }
765 rcu_read_unlock();
766 }
767
768 out:
769 netdev_put(ovpn->dev, &tracker);
770
771 /* sum up peers dumped in this message, so that at the next invocation
772 * we can continue from where we left
773 */
774 cb->args[1] += dumped;
775 return skb->len;
776 }
777
778 int ovpn_nl_peer_del_doit(struct sk_buff *skb, struct genl_info *info)
779 {
780 struct nlattr *attrs[OVPN_A_PEER_MAX + 1];
781 struct ovpn_priv *ovpn = info->user_ptr[0];
782 struct ovpn_peer *peer;
783 u32 peer_id;
784 int ret;
785
786 if (GENL_REQ_ATTR_CHECK(info, OVPN_A_PEER))
787 return -EINVAL;
788
789 ret = nla_parse_nested(attrs, OVPN_A_PEER_MAX, info->attrs[OVPN_A_PEER],
790 ovpn_peer_del_input_nl_policy, info->extack);
791 if (ret)
792 return ret;
793
794 if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_PEER], attrs,
795 OVPN_A_PEER_ID))
796 return -EINVAL;
797
798 peer_id = nla_get_u32(attrs[OVPN_A_PEER_ID]);
799 peer = ovpn_peer_get_by_id(ovpn, peer_id);
800 if (!peer) {
801 NL_SET_ERR_MSG_FMT_MOD(info->extack,
802 "cannot find peer with id %u", peer_id);
803 return -ENOENT;
804 }
805
806 netdev_dbg(ovpn->dev, "del peer %u\n", peer->id);
807 ret = ovpn_peer_del(peer, OVPN_DEL_PEER_REASON_USERSPACE);
808 ovpn_peer_put(peer);
809
810 return ret;
811 }
812
813 static int ovpn_nl_get_key_dir(struct genl_info *info, struct nlattr *key,
814 enum ovpn_cipher_alg cipher,
815 struct ovpn_key_direction *dir)
816 {
817 struct nlattr *attrs[OVPN_A_KEYDIR_MAX + 1];
818 int ret;
819
820 ret = nla_parse_nested(attrs, OVPN_A_KEYDIR_MAX, key,
821 ovpn_keydir_nl_policy, info->extack);
822 if (ret)
823 return ret;
824
825 switch (cipher) {
826 case OVPN_CIPHER_ALG_AES_GCM:
827 case OVPN_CIPHER_ALG_CHACHA20_POLY1305:
828 if (NL_REQ_ATTR_CHECK(info->extack, key, attrs,
829 OVPN_A_KEYDIR_CIPHER_KEY) ||
830 NL_REQ_ATTR_CHECK(info->extack, key, attrs,
831 OVPN_A_KEYDIR_NONCE_TAIL))
832 return -EINVAL;
833
834 dir->cipher_key = nla_data(attrs[OVPN_A_KEYDIR_CIPHER_KEY]);
835 dir->cipher_key_size = nla_len(attrs[OVPN_A_KEYDIR_CIPHER_KEY]);
836
837 /* These algorithms require a 96bit nonce,
838 * Construct it by combining 4-bytes packet id and
839 * 8-bytes nonce-tail from userspace
840 */
841 dir->nonce_tail = nla_data(attrs[OVPN_A_KEYDIR_NONCE_TAIL]);
842 dir->nonce_tail_size = nla_len(attrs[OVPN_A_KEYDIR_NONCE_TAIL]);
843 break;
844 default:
845 NL_SET_ERR_MSG_MOD(info->extack, "unsupported cipher");
846 return -EINVAL;
847 }
848
849 return 0;
850 }
851
852 /**
853 * ovpn_nl_key_new_doit - configure a new key for the specified peer
854 * @skb: incoming netlink message
855 * @info: genetlink metadata
856 *
857 * This function allows the user to install a new key in the peer crypto
858 * state.
859 * Each peer has two 'slots', namely 'primary' and 'secondary', where
860 * keys can be installed. The key in the 'primary' slot is used for
861 * encryption, while both keys can be used for decryption by matching the
862 * key ID carried in the incoming packet.
863 *
864 * The user is responsible for rotating keys when necessary. The user
865 * may fetch peer traffic statistics via netlink in order to better
866 * identify the right time to rotate keys.
867 * The renegotiation follows these steps:
868 * 1. a new key is computed by the user and is installed in the 'secondary'
869 * slot
870 * 2. at user discretion (usually after a predetermined time) 'primary' and
871 * 'secondary' contents are swapped and the new key starts being used for
872 * encryption, while the old key is kept around for decryption of late
873 * packets.
874 *
875 * Return: 0 on success or a negative error code otherwise.
876 */
877 int ovpn_nl_key_new_doit(struct sk_buff *skb, struct genl_info *info)
878 {
879 struct nlattr *attrs[OVPN_A_KEYCONF_MAX + 1];
880 struct ovpn_priv *ovpn = info->user_ptr[0];
881 struct ovpn_peer_key_reset pkr;
882 struct ovpn_peer *peer;
883 u32 peer_id;
884 int ret;
885
886 if (GENL_REQ_ATTR_CHECK(info, OVPN_A_KEYCONF))
887 return -EINVAL;
888
889 ret = nla_parse_nested(attrs, OVPN_A_KEYCONF_MAX,
890 info->attrs[OVPN_A_KEYCONF],
891 ovpn_keyconf_nl_policy, info->extack);
892 if (ret)
893 return ret;
894
895 if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs,
896 OVPN_A_KEYCONF_PEER_ID))
897 return -EINVAL;
898
899 if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs,
900 OVPN_A_KEYCONF_SLOT) ||
901 NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs,
902 OVPN_A_KEYCONF_KEY_ID) ||
903 NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs,
904 OVPN_A_KEYCONF_CIPHER_ALG) ||
905 NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs,
906 OVPN_A_KEYCONF_ENCRYPT_DIR) ||
907 NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs,
908 OVPN_A_KEYCONF_DECRYPT_DIR))
909 return -EINVAL;
910
911 pkr.slot = nla_get_u32(attrs[OVPN_A_KEYCONF_SLOT]);
912 pkr.key.key_id = nla_get_u32(attrs[OVPN_A_KEYCONF_KEY_ID]);
913 pkr.key.cipher_alg = nla_get_u32(attrs[OVPN_A_KEYCONF_CIPHER_ALG]);
914
915 ret = ovpn_nl_get_key_dir(info, attrs[OVPN_A_KEYCONF_ENCRYPT_DIR],
916 pkr.key.cipher_alg, &pkr.key.encrypt);
917 if (ret < 0)
918 return ret;
919
920 ret = ovpn_nl_get_key_dir(info, attrs[OVPN_A_KEYCONF_DECRYPT_DIR],
921 pkr.key.cipher_alg, &pkr.key.decrypt);
922 if (ret < 0)
923 return ret;
924
925 peer_id = nla_get_u32(attrs[OVPN_A_KEYCONF_PEER_ID]);
926 peer = ovpn_peer_get_by_id(ovpn, peer_id);
927 if (!peer) {
928 NL_SET_ERR_MSG_FMT_MOD(info->extack,
929 "no peer with id %u to set key for",
930 peer_id);
931 return -ENOENT;
932 }
933
934 ret = ovpn_crypto_state_reset(&peer->crypto, &pkr);
935 if (ret < 0) {
936 NL_SET_ERR_MSG_FMT_MOD(info->extack,
937 "cannot install new key for peer %u",
938 peer_id);
939 goto out;
940 }
941
942 netdev_dbg(ovpn->dev, "new key installed (id=%u) for peer %u\n",
943 pkr.key.key_id, peer_id);
944 out:
945 ovpn_peer_put(peer);
946 return ret;
947 }
948
949 static int ovpn_nl_send_key(struct sk_buff *skb, const struct genl_info *info,
950 u32 peer_id, enum ovpn_key_slot slot,
951 const struct ovpn_key_config *keyconf)
952 {
953 struct nlattr *attr;
954 void *hdr;
955
956 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq, &ovpn_nl_family,
957 0, OVPN_CMD_KEY_GET);
958 if (!hdr)
959 return -ENOBUFS;
960
961 attr = nla_nest_start(skb, OVPN_A_KEYCONF);
962 if (!attr)
963 goto err;
964
965 if (nla_put_u32(skb, OVPN_A_KEYCONF_PEER_ID, peer_id))
966 goto err;
967
968 if (nla_put_u32(skb, OVPN_A_KEYCONF_SLOT, slot) ||
969 nla_put_u32(skb, OVPN_A_KEYCONF_KEY_ID, keyconf->key_id) ||
970 nla_put_u32(skb, OVPN_A_KEYCONF_CIPHER_ALG, keyconf->cipher_alg))
971 goto err;
972
973 nla_nest_end(skb, attr);
974 genlmsg_end(skb, hdr);
975
976 return 0;
977 err:
978 genlmsg_cancel(skb, hdr);
979 return -EMSGSIZE;
980 }
981
982 int ovpn_nl_key_get_doit(struct sk_buff *skb, struct genl_info *info)
983 {
984 struct nlattr *attrs[OVPN_A_KEYCONF_MAX + 1];
985 struct ovpn_priv *ovpn = info->user_ptr[0];
986 struct ovpn_key_config keyconf = { 0 };
987 enum ovpn_key_slot slot;
988 struct ovpn_peer *peer;
989 struct sk_buff *msg;
990 u32 peer_id;
991 int ret, i;
992
993 if (GENL_REQ_ATTR_CHECK(info, OVPN_A_KEYCONF))
994 return -EINVAL;
995
996 ret = nla_parse_nested(attrs, OVPN_A_KEYCONF_MAX,
997 info->attrs[OVPN_A_KEYCONF],
998 ovpn_keyconf_get_nl_policy, info->extack);
999 if (ret)
1000 return ret;
1001
1002 if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs,
1003 OVPN_A_KEYCONF_PEER_ID))
1004 return -EINVAL;
1005
1006 if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs,
1007 OVPN_A_KEYCONF_SLOT))
1008 return -EINVAL;
1009
1010 /* OVPN_CMD_KEY_GET expects only the PEER_ID and the SLOT, therefore
1011 * ensure that the user hasn't specified any other attribute.
1012 *
1013 * Unfortunately this check cannot be performed via netlink
1014 * spec/policy and must be open-coded.
1015 */
1016 for (i = 0; i < OVPN_A_KEYCONF_MAX + 1; i++) {
1017 if (i == OVPN_A_KEYCONF_PEER_ID ||
1018 i == OVPN_A_KEYCONF_SLOT)
1019 continue;
1020
1021 if (attrs[i]) {
1022 NL_SET_ERR_MSG_FMT_MOD(info->extack,
1023 "unexpected attribute %u", i);
1024 return -EINVAL;
1025 }
1026 }
1027
1028 peer_id = nla_get_u32(attrs[OVPN_A_KEYCONF_PEER_ID]);
1029 peer = ovpn_peer_get_by_id(ovpn, peer_id);
1030 if (!peer) {
1031 NL_SET_ERR_MSG_FMT_MOD(info->extack,
1032 "cannot find peer with id %u", peer_id);
1033 return -ENOENT;
1034 }
1035
1036 slot = nla_get_u32(attrs[OVPN_A_KEYCONF_SLOT]);
1037
1038 ret = ovpn_crypto_config_get(&peer->crypto, slot, &keyconf);
1039 if (ret < 0) {
1040 NL_SET_ERR_MSG_FMT_MOD(info->extack,
1041 "cannot extract key from slot %u for peer %u",
1042 slot, peer_id);
1043 goto err;
1044 }
1045
1046 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1047 if (!msg) {
1048 ret = -ENOMEM;
1049 goto err;
1050 }
1051
1052 ret = ovpn_nl_send_key(msg, info, peer->id, slot, &keyconf);
1053 if (ret < 0) {
1054 nlmsg_free(msg);
1055 goto err;
1056 }
1057
1058 ret = genlmsg_reply(msg, info);
1059 err:
1060 ovpn_peer_put(peer);
1061 return ret;
1062 }
1063
1064 int ovpn_nl_key_swap_doit(struct sk_buff *skb, struct genl_info *info)
1065 {
1066 struct ovpn_priv *ovpn = info->user_ptr[0];
1067 struct nlattr *attrs[OVPN_A_PEER_MAX + 1];
1068 struct ovpn_peer *peer;
1069 u32 peer_id;
1070 int ret;
1071
1072 if (GENL_REQ_ATTR_CHECK(info, OVPN_A_KEYCONF))
1073 return -EINVAL;
1074
1075 ret = nla_parse_nested(attrs, OVPN_A_KEYCONF_MAX,
1076 info->attrs[OVPN_A_KEYCONF],
1077 ovpn_keyconf_swap_input_nl_policy, info->extack);
1078 if (ret)
1079 return ret;
1080
1081 if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs,
1082 OVPN_A_KEYCONF_PEER_ID))
1083 return -EINVAL;
1084
1085 peer_id = nla_get_u32(attrs[OVPN_A_KEYCONF_PEER_ID]);
1086 peer = ovpn_peer_get_by_id(ovpn, peer_id);
1087 if (!peer) {
1088 NL_SET_ERR_MSG_FMT_MOD(info->extack,
1089 "no peer with id %u to swap keys for",
1090 peer_id);
1091 return -ENOENT;
1092 }
1093
1094 ovpn_crypto_key_slots_swap(&peer->crypto);
1095 ovpn_peer_put(peer);
1096
1097 return 0;
1098 }
1099
1100 int ovpn_nl_key_del_doit(struct sk_buff *skb, struct genl_info *info)
1101 {
1102 struct nlattr *attrs[OVPN_A_KEYCONF_MAX + 1];
1103 struct ovpn_priv *ovpn = info->user_ptr[0];
1104 enum ovpn_key_slot slot;
1105 struct ovpn_peer *peer;
1106 u32 peer_id;
1107 int ret;
1108
1109 if (GENL_REQ_ATTR_CHECK(info, OVPN_A_KEYCONF))
1110 return -EINVAL;
1111
1112 ret = nla_parse_nested(attrs, OVPN_A_KEYCONF_MAX,
1113 info->attrs[OVPN_A_KEYCONF],
1114 ovpn_keyconf_del_input_nl_policy, info->extack);
1115 if (ret)
1116 return ret;
1117
1118 if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs,
1119 OVPN_A_KEYCONF_PEER_ID))
1120 return -EINVAL;
1121
1122 if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_KEYCONF], attrs,
1123 OVPN_A_KEYCONF_SLOT))
1124 return -EINVAL;
1125
1126 peer_id = nla_get_u32(attrs[OVPN_A_KEYCONF_PEER_ID]);
1127 slot = nla_get_u32(attrs[OVPN_A_KEYCONF_SLOT]);
1128
1129 peer = ovpn_peer_get_by_id(ovpn, peer_id);
1130 if (!peer) {
1131 NL_SET_ERR_MSG_FMT_MOD(info->extack,
1132 "no peer with id %u to delete key for",
1133 peer_id);
1134 return -ENOENT;
1135 }
1136
1137 ovpn_crypto_key_slot_delete(&peer->crypto, slot);
1138 ovpn_peer_put(peer);
1139
1140 return 0;
1141 }
1142
1143 /**
1144 * ovpn_nl_peer_del_notify - notify userspace about peer being deleted
1145 * @peer: the peer being deleted
1146 *
1147 * Return: 0 on success or a negative error code otherwise
1148 */
1149 int ovpn_nl_peer_del_notify(struct ovpn_peer *peer)
1150 {
1151 struct ovpn_socket *sock;
1152 struct sk_buff *msg;
1153 struct nlattr *attr;
1154 int ret = -EMSGSIZE;
1155 void *hdr;
1156
1157 netdev_info(peer->ovpn->dev, "deleting peer with id %u, reason %d\n",
1158 peer->id, peer->delete_reason);
1159
1160 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1161 if (!msg)
1162 return -ENOMEM;
1163
1164 hdr = genlmsg_put(msg, 0, 0, &ovpn_nl_family, 0, OVPN_CMD_PEER_DEL_NTF);
1165 if (!hdr) {
1166 ret = -ENOBUFS;
1167 goto err_free_msg;
1168 }
1169
1170 if (nla_put_u32(msg, OVPN_A_IFINDEX, peer->ovpn->dev->ifindex))
1171 goto err_cancel_msg;
1172
1173 attr = nla_nest_start(msg, OVPN_A_PEER);
1174 if (!attr)
1175 goto err_cancel_msg;
1176
1177 if (nla_put_u32(msg, OVPN_A_PEER_DEL_REASON, peer->delete_reason))
1178 goto err_cancel_msg;
1179
1180 if (nla_put_u32(msg, OVPN_A_PEER_ID, peer->id))
1181 goto err_cancel_msg;
1182
1183 nla_nest_end(msg, attr);
1184
1185 genlmsg_end(msg, hdr);
1186
1187 rcu_read_lock();
1188 sock = rcu_dereference(peer->sock);
1189 if (!sock) {
1190 ret = -EINVAL;
1191 goto err_unlock;
1192 }
1193 genlmsg_multicast_netns(&ovpn_nl_family, sock_net(sock->sk), msg, 0,
1194 OVPN_NLGRP_PEERS, GFP_ATOMIC);
1195 rcu_read_unlock();
1196
1197 return 0;
1198
1199 err_unlock:
1200 rcu_read_unlock();
1201 err_cancel_msg:
1202 genlmsg_cancel(msg, hdr);
1203 err_free_msg:
1204 nlmsg_free(msg);
1205 return ret;
1206 }
1207
1208 /**
1209 * ovpn_nl_key_swap_notify - notify userspace peer's key must be renewed
1210 * @peer: the peer whose key needs to be renewed
1211 * @key_id: the ID of the key that needs to be renewed
1212 *
1213 * Return: 0 on success or a negative error code otherwise
1214 */
1215 int ovpn_nl_key_swap_notify(struct ovpn_peer *peer, u8 key_id)
1216 {
1217 struct ovpn_socket *sock;
1218 struct nlattr *k_attr;
1219 struct sk_buff *msg;
1220 int ret = -EMSGSIZE;
1221 void *hdr;
1222
1223 netdev_info(peer->ovpn->dev, "peer with id %u must rekey - primary key unusable.\n",
1224 peer->id);
1225
1226 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1227 if (!msg)
1228 return -ENOMEM;
1229
1230 hdr = genlmsg_put(msg, 0, 0, &ovpn_nl_family, 0, OVPN_CMD_KEY_SWAP_NTF);
1231 if (!hdr) {
1232 ret = -ENOBUFS;
1233 goto err_free_msg;
1234 }
1235
1236 if (nla_put_u32(msg, OVPN_A_IFINDEX, peer->ovpn->dev->ifindex))
1237 goto err_cancel_msg;
1238
1239 k_attr = nla_nest_start(msg, OVPN_A_KEYCONF);
1240 if (!k_attr)
1241 goto err_cancel_msg;
1242
1243 if (nla_put_u32(msg, OVPN_A_KEYCONF_PEER_ID, peer->id))
1244 goto err_cancel_msg;
1245
1246 if (nla_put_u16(msg, OVPN_A_KEYCONF_KEY_ID, key_id))
1247 goto err_cancel_msg;
1248
1249 nla_nest_end(msg, k_attr);
1250 genlmsg_end(msg, hdr);
1251
1252 rcu_read_lock();
1253 sock = rcu_dereference(peer->sock);
1254 if (!sock) {
1255 ret = -EINVAL;
1256 goto err_unlock;
1257 }
1258 genlmsg_multicast_netns(&ovpn_nl_family, sock_net(sock->sk), msg, 0,
1259 OVPN_NLGRP_PEERS, GFP_ATOMIC);
1260 rcu_read_unlock();
1261
1262 return 0;
1263 err_unlock:
1264 rcu_read_unlock();
1265 err_cancel_msg:
1266 genlmsg_cancel(msg, hdr);
1267 err_free_msg:
1268 nlmsg_free(msg);
1269 return ret;
1270 }
1271
1272 /**
1273 * ovpn_nl_register - perform any needed registration in the NL subsustem
1274 *
1275 * Return: 0 on success, a negative error code otherwise
1276 */
1277 int __init ovpn_nl_register(void)
1278 {
1279 int ret = genl_register_family(&ovpn_nl_family);
1280
1281 if (ret) {
1282 pr_err("ovpn: genl_register_family failed: %d\n", ret);
1283 return ret;
1284 }
1285
1286 return 0;
1287 }
1288
1289 /**
1290 * ovpn_nl_unregister - undo any module wide netlink registration
1291 */
1292 void ovpn_nl_unregister(void)
1293 {
1294 genl_unregister_family(&ovpn_nl_family);
1295 }