xref: /openbmc/linux/net/l2tp/l2tp_netlink.c (revision 0760aad038b5a032c31ea124feed63d88627d2f1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* L2TP netlink layer, for management
3  *
4  * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
5  *
6  * Partly based on the IrDA nelink implementation
7  * (see net/irda/irnetlink.c) which is:
8  * Copyright (c) 2007 Samuel Ortiz <samuel@sortiz.org>
9  * which is in turn partly based on the wireless netlink code:
10  * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
11  */
12 
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 
15 #include <net/sock.h>
16 #include <net/genetlink.h>
17 #include <net/udp.h>
18 #include <linux/in.h>
19 #include <linux/udp.h>
20 #include <linux/socket.h>
21 #include <linux/module.h>
22 #include <linux/list.h>
23 #include <net/net_namespace.h>
24 
25 #include <linux/l2tp.h>
26 
27 #include "l2tp_core.h"
28 
29 static struct genl_family l2tp_nl_family;
30 
31 static const struct genl_multicast_group l2tp_multicast_group[] = {
32 	{
33 		.name = L2TP_GENL_MCGROUP,
34 	},
35 };
36 
37 static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq,
38 			       int flags, struct l2tp_tunnel *tunnel, u8 cmd);
39 static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq,
40 				int flags, struct l2tp_session *session,
41 				u8 cmd);
42 
43 /* Accessed under genl lock */
44 static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX];
45 
46 static struct l2tp_session *l2tp_nl_session_get(struct genl_info *info)
47 {
48 	u32 tunnel_id;
49 	u32 session_id;
50 	char *ifname;
51 	struct l2tp_tunnel *tunnel;
52 	struct l2tp_session *session = NULL;
53 	struct net *net = genl_info_net(info);
54 
55 	if (info->attrs[L2TP_ATTR_IFNAME]) {
56 		ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
57 		session = l2tp_session_get_by_ifname(net, ifname);
58 	} else if ((info->attrs[L2TP_ATTR_SESSION_ID]) &&
59 		   (info->attrs[L2TP_ATTR_CONN_ID])) {
60 		tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
61 		session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
62 		tunnel = l2tp_tunnel_get(net, tunnel_id);
63 		if (tunnel) {
64 			session = l2tp_tunnel_get_session(tunnel, session_id);
65 			l2tp_tunnel_dec_refcount(tunnel);
66 		}
67 	}
68 
69 	return session;
70 }
71 
72 static int l2tp_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
73 {
74 	struct sk_buff *msg;
75 	void *hdr;
76 	int ret = -ENOBUFS;
77 
78 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
79 	if (!msg) {
80 		ret = -ENOMEM;
81 		goto out;
82 	}
83 
84 	hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
85 			  &l2tp_nl_family, 0, L2TP_CMD_NOOP);
86 	if (!hdr) {
87 		ret = -EMSGSIZE;
88 		goto err_out;
89 	}
90 
91 	genlmsg_end(msg, hdr);
92 
93 	return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
94 
95 err_out:
96 	nlmsg_free(msg);
97 
98 out:
99 	return ret;
100 }
101 
102 static int l2tp_tunnel_notify(struct genl_family *family,
103 			      struct genl_info *info,
104 			      struct l2tp_tunnel *tunnel,
105 			      u8 cmd)
106 {
107 	struct sk_buff *msg;
108 	int ret;
109 
110 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
111 	if (!msg)
112 		return -ENOMEM;
113 
114 	ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
115 				  NLM_F_ACK, tunnel, cmd);
116 
117 	if (ret >= 0) {
118 		ret = genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC);
119 		/* We don't care if no one is listening */
120 		if (ret == -ESRCH)
121 			ret = 0;
122 		return ret;
123 	}
124 
125 	nlmsg_free(msg);
126 
127 	return ret;
128 }
129 
130 static int l2tp_session_notify(struct genl_family *family,
131 			       struct genl_info *info,
132 			       struct l2tp_session *session,
133 			       u8 cmd)
134 {
135 	struct sk_buff *msg;
136 	int ret;
137 
138 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
139 	if (!msg)
140 		return -ENOMEM;
141 
142 	ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
143 				   NLM_F_ACK, session, cmd);
144 
145 	if (ret >= 0) {
146 		ret = genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC);
147 		/* We don't care if no one is listening */
148 		if (ret == -ESRCH)
149 			ret = 0;
150 		return ret;
151 	}
152 
153 	nlmsg_free(msg);
154 
155 	return ret;
156 }
157 
158 static int l2tp_nl_cmd_tunnel_create_get_addr(struct nlattr **attrs, struct l2tp_tunnel_cfg *cfg)
159 {
160 	if (attrs[L2TP_ATTR_UDP_SPORT])
161 		cfg->local_udp_port = nla_get_u16(attrs[L2TP_ATTR_UDP_SPORT]);
162 	if (attrs[L2TP_ATTR_UDP_DPORT])
163 		cfg->peer_udp_port = nla_get_u16(attrs[L2TP_ATTR_UDP_DPORT]);
164 	cfg->use_udp_checksums = nla_get_flag(attrs[L2TP_ATTR_UDP_CSUM]);
165 
166 	/* Must have either AF_INET or AF_INET6 address for source and destination */
167 #if IS_ENABLED(CONFIG_IPV6)
168 	if (attrs[L2TP_ATTR_IP6_SADDR] && attrs[L2TP_ATTR_IP6_DADDR]) {
169 		cfg->local_ip6 = nla_data(attrs[L2TP_ATTR_IP6_SADDR]);
170 		cfg->peer_ip6 = nla_data(attrs[L2TP_ATTR_IP6_DADDR]);
171 		cfg->udp6_zero_tx_checksums = nla_get_flag(attrs[L2TP_ATTR_UDP_ZERO_CSUM6_TX]);
172 		cfg->udp6_zero_rx_checksums = nla_get_flag(attrs[L2TP_ATTR_UDP_ZERO_CSUM6_RX]);
173 		return 0;
174 	}
175 #endif
176 	if (attrs[L2TP_ATTR_IP_SADDR] && attrs[L2TP_ATTR_IP_DADDR]) {
177 		cfg->local_ip.s_addr = nla_get_in_addr(attrs[L2TP_ATTR_IP_SADDR]);
178 		cfg->peer_ip.s_addr = nla_get_in_addr(attrs[L2TP_ATTR_IP_DADDR]);
179 		return 0;
180 	}
181 	return -EINVAL;
182 }
183 
184 static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info)
185 {
186 	u32 tunnel_id;
187 	u32 peer_tunnel_id;
188 	int proto_version;
189 	int fd = -1;
190 	int ret = 0;
191 	struct l2tp_tunnel_cfg cfg = { 0, };
192 	struct l2tp_tunnel *tunnel;
193 	struct net *net = genl_info_net(info);
194 	struct nlattr **attrs = info->attrs;
195 
196 	if (!attrs[L2TP_ATTR_CONN_ID]) {
197 		ret = -EINVAL;
198 		goto out;
199 	}
200 	tunnel_id = nla_get_u32(attrs[L2TP_ATTR_CONN_ID]);
201 
202 	if (!attrs[L2TP_ATTR_PEER_CONN_ID]) {
203 		ret = -EINVAL;
204 		goto out;
205 	}
206 	peer_tunnel_id = nla_get_u32(attrs[L2TP_ATTR_PEER_CONN_ID]);
207 
208 	if (!attrs[L2TP_ATTR_PROTO_VERSION]) {
209 		ret = -EINVAL;
210 		goto out;
211 	}
212 	proto_version = nla_get_u8(attrs[L2TP_ATTR_PROTO_VERSION]);
213 
214 	if (!attrs[L2TP_ATTR_ENCAP_TYPE]) {
215 		ret = -EINVAL;
216 		goto out;
217 	}
218 	cfg.encap = nla_get_u16(attrs[L2TP_ATTR_ENCAP_TYPE]);
219 
220 	/* Managed tunnels take the tunnel socket from userspace.
221 	 * Unmanaged tunnels must call out the source and destination addresses
222 	 * for the kernel to create the tunnel socket itself.
223 	 */
224 	if (attrs[L2TP_ATTR_FD]) {
225 		fd = nla_get_u32(attrs[L2TP_ATTR_FD]);
226 	} else {
227 		ret = l2tp_nl_cmd_tunnel_create_get_addr(attrs, &cfg);
228 		if (ret < 0)
229 			goto out;
230 	}
231 
232 	ret = -EINVAL;
233 	switch (cfg.encap) {
234 	case L2TP_ENCAPTYPE_UDP:
235 	case L2TP_ENCAPTYPE_IP:
236 		ret = l2tp_tunnel_create(fd, proto_version, tunnel_id,
237 					 peer_tunnel_id, &cfg, &tunnel);
238 		break;
239 	}
240 
241 	if (ret < 0)
242 		goto out;
243 
244 	l2tp_tunnel_inc_refcount(tunnel);
245 	ret = l2tp_tunnel_register(tunnel, net, &cfg);
246 	if (ret < 0) {
247 		kfree(tunnel);
248 		goto out;
249 	}
250 	ret = l2tp_tunnel_notify(&l2tp_nl_family, info, tunnel,
251 				 L2TP_CMD_TUNNEL_CREATE);
252 	l2tp_tunnel_dec_refcount(tunnel);
253 
254 out:
255 	return ret;
256 }
257 
258 static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info)
259 {
260 	struct l2tp_tunnel *tunnel;
261 	u32 tunnel_id;
262 	int ret = 0;
263 	struct net *net = genl_info_net(info);
264 
265 	if (!info->attrs[L2TP_ATTR_CONN_ID]) {
266 		ret = -EINVAL;
267 		goto out;
268 	}
269 	tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
270 
271 	tunnel = l2tp_tunnel_get(net, tunnel_id);
272 	if (!tunnel) {
273 		ret = -ENODEV;
274 		goto out;
275 	}
276 
277 	l2tp_tunnel_notify(&l2tp_nl_family, info,
278 			   tunnel, L2TP_CMD_TUNNEL_DELETE);
279 
280 	l2tp_tunnel_delete(tunnel);
281 
282 	l2tp_tunnel_dec_refcount(tunnel);
283 
284 out:
285 	return ret;
286 }
287 
288 static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info)
289 {
290 	struct l2tp_tunnel *tunnel;
291 	u32 tunnel_id;
292 	int ret = 0;
293 	struct net *net = genl_info_net(info);
294 
295 	if (!info->attrs[L2TP_ATTR_CONN_ID]) {
296 		ret = -EINVAL;
297 		goto out;
298 	}
299 	tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
300 
301 	tunnel = l2tp_tunnel_get(net, tunnel_id);
302 	if (!tunnel) {
303 		ret = -ENODEV;
304 		goto out;
305 	}
306 
307 	ret = l2tp_tunnel_notify(&l2tp_nl_family, info,
308 				 tunnel, L2TP_CMD_TUNNEL_MODIFY);
309 
310 	l2tp_tunnel_dec_refcount(tunnel);
311 
312 out:
313 	return ret;
314 }
315 
316 #if IS_ENABLED(CONFIG_IPV6)
317 static int l2tp_nl_tunnel_send_addr6(struct sk_buff *skb, struct sock *sk,
318 				     enum l2tp_encap_type encap)
319 {
320 	struct inet_sock *inet = inet_sk(sk);
321 	struct ipv6_pinfo *np = inet6_sk(sk);
322 
323 	switch (encap) {
324 	case L2TP_ENCAPTYPE_UDP:
325 		if (udp_get_no_check6_tx(sk) &&
326 		    nla_put_flag(skb, L2TP_ATTR_UDP_ZERO_CSUM6_TX))
327 			return -1;
328 		if (udp_get_no_check6_rx(sk) &&
329 		    nla_put_flag(skb, L2TP_ATTR_UDP_ZERO_CSUM6_RX))
330 			return -1;
331 		if (nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
332 		    nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)))
333 			return -1;
334 		fallthrough;
335 	case L2TP_ENCAPTYPE_IP:
336 		if (nla_put_in6_addr(skb, L2TP_ATTR_IP6_SADDR, &np->saddr) ||
337 		    nla_put_in6_addr(skb, L2TP_ATTR_IP6_DADDR, &sk->sk_v6_daddr))
338 			return -1;
339 		break;
340 	}
341 	return 0;
342 }
343 #endif
344 
345 static int l2tp_nl_tunnel_send_addr4(struct sk_buff *skb, struct sock *sk,
346 				     enum l2tp_encap_type encap)
347 {
348 	struct inet_sock *inet = inet_sk(sk);
349 
350 	switch (encap) {
351 	case L2TP_ENCAPTYPE_UDP:
352 		if (nla_put_u8(skb, L2TP_ATTR_UDP_CSUM, !sk->sk_no_check_tx) ||
353 		    nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
354 		    nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)))
355 			return -1;
356 		fallthrough;
357 	case L2TP_ENCAPTYPE_IP:
358 		if (nla_put_in_addr(skb, L2TP_ATTR_IP_SADDR, inet->inet_saddr) ||
359 		    nla_put_in_addr(skb, L2TP_ATTR_IP_DADDR, inet->inet_daddr))
360 			return -1;
361 		break;
362 	}
363 
364 	return 0;
365 }
366 
367 /* Append attributes for the tunnel address, handling the different attribute types
368  * used for different tunnel encapsulation and AF_INET v.s. AF_INET6.
369  */
370 static int l2tp_nl_tunnel_send_addr(struct sk_buff *skb, struct l2tp_tunnel *tunnel)
371 {
372 	struct sock *sk = tunnel->sock;
373 
374 	if (!sk)
375 		return 0;
376 
377 #if IS_ENABLED(CONFIG_IPV6)
378 	if (sk->sk_family == AF_INET6)
379 		return l2tp_nl_tunnel_send_addr6(skb, sk, tunnel->encap);
380 #endif
381 	return l2tp_nl_tunnel_send_addr4(skb, sk, tunnel->encap);
382 }
383 
384 static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
385 			       struct l2tp_tunnel *tunnel, u8 cmd)
386 {
387 	void *hdr;
388 	struct nlattr *nest;
389 
390 	hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
391 	if (!hdr)
392 		return -EMSGSIZE;
393 
394 	if (nla_put_u8(skb, L2TP_ATTR_PROTO_VERSION, tunnel->version) ||
395 	    nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
396 	    nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
397 	    nla_put_u32(skb, L2TP_ATTR_DEBUG, 0) ||
398 	    nla_put_u16(skb, L2TP_ATTR_ENCAP_TYPE, tunnel->encap))
399 		goto nla_put_failure;
400 
401 	nest = nla_nest_start_noflag(skb, L2TP_ATTR_STATS);
402 	if (!nest)
403 		goto nla_put_failure;
404 
405 	if (nla_put_u64_64bit(skb, L2TP_ATTR_TX_PACKETS,
406 			      atomic_long_read(&tunnel->stats.tx_packets),
407 			      L2TP_ATTR_STATS_PAD) ||
408 	    nla_put_u64_64bit(skb, L2TP_ATTR_TX_BYTES,
409 			      atomic_long_read(&tunnel->stats.tx_bytes),
410 			      L2TP_ATTR_STATS_PAD) ||
411 	    nla_put_u64_64bit(skb, L2TP_ATTR_TX_ERRORS,
412 			      atomic_long_read(&tunnel->stats.tx_errors),
413 			      L2TP_ATTR_STATS_PAD) ||
414 	    nla_put_u64_64bit(skb, L2TP_ATTR_RX_PACKETS,
415 			      atomic_long_read(&tunnel->stats.rx_packets),
416 			      L2TP_ATTR_STATS_PAD) ||
417 	    nla_put_u64_64bit(skb, L2TP_ATTR_RX_BYTES,
418 			      atomic_long_read(&tunnel->stats.rx_bytes),
419 			      L2TP_ATTR_STATS_PAD) ||
420 	    nla_put_u64_64bit(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
421 			      atomic_long_read(&tunnel->stats.rx_seq_discards),
422 			      L2TP_ATTR_STATS_PAD) ||
423 	    nla_put_u64_64bit(skb, L2TP_ATTR_RX_OOS_PACKETS,
424 			      atomic_long_read(&tunnel->stats.rx_oos_packets),
425 			      L2TP_ATTR_STATS_PAD) ||
426 	    nla_put_u64_64bit(skb, L2TP_ATTR_RX_ERRORS,
427 			      atomic_long_read(&tunnel->stats.rx_errors),
428 			      L2TP_ATTR_STATS_PAD))
429 		goto nla_put_failure;
430 	nla_nest_end(skb, nest);
431 
432 	if (l2tp_nl_tunnel_send_addr(skb, tunnel))
433 		goto nla_put_failure;
434 
435 	genlmsg_end(skb, hdr);
436 	return 0;
437 
438 nla_put_failure:
439 	genlmsg_cancel(skb, hdr);
440 	return -1;
441 }
442 
443 static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
444 {
445 	struct l2tp_tunnel *tunnel;
446 	struct sk_buff *msg;
447 	u32 tunnel_id;
448 	int ret = -ENOBUFS;
449 	struct net *net = genl_info_net(info);
450 
451 	if (!info->attrs[L2TP_ATTR_CONN_ID]) {
452 		ret = -EINVAL;
453 		goto err;
454 	}
455 
456 	tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
457 
458 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
459 	if (!msg) {
460 		ret = -ENOMEM;
461 		goto err;
462 	}
463 
464 	tunnel = l2tp_tunnel_get(net, tunnel_id);
465 	if (!tunnel) {
466 		ret = -ENODEV;
467 		goto err_nlmsg;
468 	}
469 
470 	ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
471 				  NLM_F_ACK, tunnel, L2TP_CMD_TUNNEL_GET);
472 	if (ret < 0)
473 		goto err_nlmsg_tunnel;
474 
475 	l2tp_tunnel_dec_refcount(tunnel);
476 
477 	return genlmsg_unicast(net, msg, info->snd_portid);
478 
479 err_nlmsg_tunnel:
480 	l2tp_tunnel_dec_refcount(tunnel);
481 err_nlmsg:
482 	nlmsg_free(msg);
483 err:
484 	return ret;
485 }
486 
487 static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
488 {
489 	int ti = cb->args[0];
490 	struct l2tp_tunnel *tunnel;
491 	struct net *net = sock_net(skb->sk);
492 
493 	for (;;) {
494 		tunnel = l2tp_tunnel_get_nth(net, ti);
495 		if (!tunnel)
496 			goto out;
497 
498 		if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).portid,
499 					cb->nlh->nlmsg_seq, NLM_F_MULTI,
500 					tunnel, L2TP_CMD_TUNNEL_GET) < 0) {
501 			l2tp_tunnel_dec_refcount(tunnel);
502 			goto out;
503 		}
504 		l2tp_tunnel_dec_refcount(tunnel);
505 
506 		ti++;
507 	}
508 
509 out:
510 	cb->args[0] = ti;
511 
512 	return skb->len;
513 }
514 
515 static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
516 {
517 	u32 tunnel_id = 0;
518 	u32 session_id;
519 	u32 peer_session_id;
520 	int ret = 0;
521 	struct l2tp_tunnel *tunnel;
522 	struct l2tp_session *session;
523 	struct l2tp_session_cfg cfg = { 0, };
524 	struct net *net = genl_info_net(info);
525 
526 	if (!info->attrs[L2TP_ATTR_CONN_ID]) {
527 		ret = -EINVAL;
528 		goto out;
529 	}
530 
531 	tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
532 	tunnel = l2tp_tunnel_get(net, tunnel_id);
533 	if (!tunnel) {
534 		ret = -ENODEV;
535 		goto out;
536 	}
537 
538 	if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
539 		ret = -EINVAL;
540 		goto out_tunnel;
541 	}
542 	session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
543 
544 	if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
545 		ret = -EINVAL;
546 		goto out_tunnel;
547 	}
548 	peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
549 
550 	if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
551 		ret = -EINVAL;
552 		goto out_tunnel;
553 	}
554 	cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
555 	if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
556 		ret = -EINVAL;
557 		goto out_tunnel;
558 	}
559 
560 	/* L2TPv2 only accepts PPP pseudo-wires */
561 	if (tunnel->version == 2 && cfg.pw_type != L2TP_PWTYPE_PPP) {
562 		ret = -EPROTONOSUPPORT;
563 		goto out_tunnel;
564 	}
565 
566 	if (tunnel->version > 2) {
567 		if (info->attrs[L2TP_ATTR_L2SPEC_TYPE]) {
568 			cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
569 			if (cfg.l2specific_type != L2TP_L2SPECTYPE_DEFAULT &&
570 			    cfg.l2specific_type != L2TP_L2SPECTYPE_NONE) {
571 				ret = -EINVAL;
572 				goto out_tunnel;
573 			}
574 		} else {
575 			cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
576 		}
577 
578 		if (info->attrs[L2TP_ATTR_COOKIE]) {
579 			u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
580 
581 			if (len > 8) {
582 				ret = -EINVAL;
583 				goto out_tunnel;
584 			}
585 			cfg.cookie_len = len;
586 			memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
587 		}
588 		if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
589 			u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
590 
591 			if (len > 8) {
592 				ret = -EINVAL;
593 				goto out_tunnel;
594 			}
595 			cfg.peer_cookie_len = len;
596 			memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
597 		}
598 		if (info->attrs[L2TP_ATTR_IFNAME])
599 			cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
600 	}
601 
602 	if (info->attrs[L2TP_ATTR_RECV_SEQ])
603 		cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
604 
605 	if (info->attrs[L2TP_ATTR_SEND_SEQ])
606 		cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
607 
608 	if (info->attrs[L2TP_ATTR_LNS_MODE])
609 		cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
610 
611 	if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
612 		cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
613 
614 #ifdef CONFIG_MODULES
615 	if (!l2tp_nl_cmd_ops[cfg.pw_type]) {
616 		genl_unlock();
617 		request_module("net-l2tp-type-%u", cfg.pw_type);
618 		genl_lock();
619 	}
620 #endif
621 	if (!l2tp_nl_cmd_ops[cfg.pw_type] || !l2tp_nl_cmd_ops[cfg.pw_type]->session_create) {
622 		ret = -EPROTONOSUPPORT;
623 		goto out_tunnel;
624 	}
625 
626 	ret = l2tp_nl_cmd_ops[cfg.pw_type]->session_create(net, tunnel,
627 							   session_id,
628 							   peer_session_id,
629 							   &cfg);
630 
631 	if (ret >= 0) {
632 		session = l2tp_tunnel_get_session(tunnel, session_id);
633 		if (session) {
634 			ret = l2tp_session_notify(&l2tp_nl_family, info, session,
635 						  L2TP_CMD_SESSION_CREATE);
636 			l2tp_session_dec_refcount(session);
637 		}
638 	}
639 
640 out_tunnel:
641 	l2tp_tunnel_dec_refcount(tunnel);
642 out:
643 	return ret;
644 }
645 
646 static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
647 {
648 	int ret = 0;
649 	struct l2tp_session *session;
650 	u16 pw_type;
651 
652 	session = l2tp_nl_session_get(info);
653 	if (!session) {
654 		ret = -ENODEV;
655 		goto out;
656 	}
657 
658 	l2tp_session_notify(&l2tp_nl_family, info,
659 			    session, L2TP_CMD_SESSION_DELETE);
660 
661 	pw_type = session->pwtype;
662 	if (pw_type < __L2TP_PWTYPE_MAX)
663 		if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
664 			l2tp_nl_cmd_ops[pw_type]->session_delete(session);
665 
666 	l2tp_session_dec_refcount(session);
667 
668 out:
669 	return ret;
670 }
671 
672 static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
673 {
674 	int ret = 0;
675 	struct l2tp_session *session;
676 
677 	session = l2tp_nl_session_get(info);
678 	if (!session) {
679 		ret = -ENODEV;
680 		goto out;
681 	}
682 
683 	if (info->attrs[L2TP_ATTR_RECV_SEQ])
684 		session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
685 
686 	if (info->attrs[L2TP_ATTR_SEND_SEQ]) {
687 		session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
688 		l2tp_session_set_header_len(session, session->tunnel->version);
689 	}
690 
691 	if (info->attrs[L2TP_ATTR_LNS_MODE])
692 		session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
693 
694 	if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
695 		session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
696 
697 	ret = l2tp_session_notify(&l2tp_nl_family, info,
698 				  session, L2TP_CMD_SESSION_MODIFY);
699 
700 	l2tp_session_dec_refcount(session);
701 
702 out:
703 	return ret;
704 }
705 
706 static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
707 				struct l2tp_session *session, u8 cmd)
708 {
709 	void *hdr;
710 	struct nlattr *nest;
711 	struct l2tp_tunnel *tunnel = session->tunnel;
712 
713 	hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
714 	if (!hdr)
715 		return -EMSGSIZE;
716 
717 	if (nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
718 	    nla_put_u32(skb, L2TP_ATTR_SESSION_ID, session->session_id) ||
719 	    nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
720 	    nla_put_u32(skb, L2TP_ATTR_PEER_SESSION_ID, session->peer_session_id) ||
721 	    nla_put_u32(skb, L2TP_ATTR_DEBUG, 0) ||
722 	    nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype))
723 		goto nla_put_failure;
724 
725 	if ((session->ifname[0] &&
726 	     nla_put_string(skb, L2TP_ATTR_IFNAME, session->ifname)) ||
727 	    (session->cookie_len &&
728 	     nla_put(skb, L2TP_ATTR_COOKIE, session->cookie_len, session->cookie)) ||
729 	    (session->peer_cookie_len &&
730 	     nla_put(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len, session->peer_cookie)) ||
731 	    nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
732 	    nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
733 	    nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
734 	    (l2tp_tunnel_uses_xfrm(tunnel) &&
735 	     nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
736 	    (session->reorder_timeout &&
737 	     nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT,
738 			   session->reorder_timeout, L2TP_ATTR_PAD)))
739 		goto nla_put_failure;
740 
741 	nest = nla_nest_start_noflag(skb, L2TP_ATTR_STATS);
742 	if (!nest)
743 		goto nla_put_failure;
744 
745 	if (nla_put_u64_64bit(skb, L2TP_ATTR_TX_PACKETS,
746 			      atomic_long_read(&session->stats.tx_packets),
747 			      L2TP_ATTR_STATS_PAD) ||
748 	    nla_put_u64_64bit(skb, L2TP_ATTR_TX_BYTES,
749 			      atomic_long_read(&session->stats.tx_bytes),
750 			      L2TP_ATTR_STATS_PAD) ||
751 	    nla_put_u64_64bit(skb, L2TP_ATTR_TX_ERRORS,
752 			      atomic_long_read(&session->stats.tx_errors),
753 			      L2TP_ATTR_STATS_PAD) ||
754 	    nla_put_u64_64bit(skb, L2TP_ATTR_RX_PACKETS,
755 			      atomic_long_read(&session->stats.rx_packets),
756 			      L2TP_ATTR_STATS_PAD) ||
757 	    nla_put_u64_64bit(skb, L2TP_ATTR_RX_BYTES,
758 			      atomic_long_read(&session->stats.rx_bytes),
759 			      L2TP_ATTR_STATS_PAD) ||
760 	    nla_put_u64_64bit(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
761 			      atomic_long_read(&session->stats.rx_seq_discards),
762 			      L2TP_ATTR_STATS_PAD) ||
763 	    nla_put_u64_64bit(skb, L2TP_ATTR_RX_OOS_PACKETS,
764 			      atomic_long_read(&session->stats.rx_oos_packets),
765 			      L2TP_ATTR_STATS_PAD) ||
766 	    nla_put_u64_64bit(skb, L2TP_ATTR_RX_ERRORS,
767 			      atomic_long_read(&session->stats.rx_errors),
768 			      L2TP_ATTR_STATS_PAD))
769 		goto nla_put_failure;
770 	nla_nest_end(skb, nest);
771 
772 	genlmsg_end(skb, hdr);
773 	return 0;
774 
775  nla_put_failure:
776 	genlmsg_cancel(skb, hdr);
777 	return -1;
778 }
779 
780 static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
781 {
782 	struct l2tp_session *session;
783 	struct sk_buff *msg;
784 	int ret;
785 
786 	session = l2tp_nl_session_get(info);
787 	if (!session) {
788 		ret = -ENODEV;
789 		goto err;
790 	}
791 
792 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
793 	if (!msg) {
794 		ret = -ENOMEM;
795 		goto err_ref;
796 	}
797 
798 	ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
799 				   0, session, L2TP_CMD_SESSION_GET);
800 	if (ret < 0)
801 		goto err_ref_msg;
802 
803 	ret = genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
804 
805 	l2tp_session_dec_refcount(session);
806 
807 	return ret;
808 
809 err_ref_msg:
810 	nlmsg_free(msg);
811 err_ref:
812 	l2tp_session_dec_refcount(session);
813 err:
814 	return ret;
815 }
816 
817 static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
818 {
819 	struct net *net = sock_net(skb->sk);
820 	struct l2tp_session *session;
821 	struct l2tp_tunnel *tunnel = NULL;
822 	int ti = cb->args[0];
823 	int si = cb->args[1];
824 
825 	for (;;) {
826 		if (!tunnel) {
827 			tunnel = l2tp_tunnel_get_nth(net, ti);
828 			if (!tunnel)
829 				goto out;
830 		}
831 
832 		session = l2tp_session_get_nth(tunnel, si);
833 		if (!session) {
834 			ti++;
835 			l2tp_tunnel_dec_refcount(tunnel);
836 			tunnel = NULL;
837 			si = 0;
838 			continue;
839 		}
840 
841 		if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).portid,
842 					 cb->nlh->nlmsg_seq, NLM_F_MULTI,
843 					 session, L2TP_CMD_SESSION_GET) < 0) {
844 			l2tp_session_dec_refcount(session);
845 			l2tp_tunnel_dec_refcount(tunnel);
846 			break;
847 		}
848 		l2tp_session_dec_refcount(session);
849 
850 		si++;
851 	}
852 
853 out:
854 	cb->args[0] = ti;
855 	cb->args[1] = si;
856 
857 	return skb->len;
858 }
859 
860 static const struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
861 	[L2TP_ATTR_NONE]		= { .type = NLA_UNSPEC, },
862 	[L2TP_ATTR_PW_TYPE]		= { .type = NLA_U16, },
863 	[L2TP_ATTR_ENCAP_TYPE]		= { .type = NLA_U16, },
864 	[L2TP_ATTR_OFFSET]		= { .type = NLA_U16, },
865 	[L2TP_ATTR_DATA_SEQ]		= { .type = NLA_U8, },
866 	[L2TP_ATTR_L2SPEC_TYPE]		= { .type = NLA_U8, },
867 	[L2TP_ATTR_L2SPEC_LEN]		= { .type = NLA_U8, },
868 	[L2TP_ATTR_PROTO_VERSION]	= { .type = NLA_U8, },
869 	[L2TP_ATTR_CONN_ID]		= { .type = NLA_U32, },
870 	[L2TP_ATTR_PEER_CONN_ID]	= { .type = NLA_U32, },
871 	[L2TP_ATTR_SESSION_ID]		= { .type = NLA_U32, },
872 	[L2TP_ATTR_PEER_SESSION_ID]	= { .type = NLA_U32, },
873 	[L2TP_ATTR_UDP_CSUM]		= { .type = NLA_U8, },
874 	[L2TP_ATTR_VLAN_ID]		= { .type = NLA_U16, },
875 	[L2TP_ATTR_DEBUG]		= { .type = NLA_U32, },
876 	[L2TP_ATTR_RECV_SEQ]		= { .type = NLA_U8, },
877 	[L2TP_ATTR_SEND_SEQ]		= { .type = NLA_U8, },
878 	[L2TP_ATTR_LNS_MODE]		= { .type = NLA_U8, },
879 	[L2TP_ATTR_USING_IPSEC]		= { .type = NLA_U8, },
880 	[L2TP_ATTR_RECV_TIMEOUT]	= { .type = NLA_MSECS, },
881 	[L2TP_ATTR_FD]			= { .type = NLA_U32, },
882 	[L2TP_ATTR_IP_SADDR]		= { .type = NLA_U32, },
883 	[L2TP_ATTR_IP_DADDR]		= { .type = NLA_U32, },
884 	[L2TP_ATTR_UDP_SPORT]		= { .type = NLA_U16, },
885 	[L2TP_ATTR_UDP_DPORT]		= { .type = NLA_U16, },
886 	[L2TP_ATTR_MTU]			= { .type = NLA_U16, },
887 	[L2TP_ATTR_MRU]			= { .type = NLA_U16, },
888 	[L2TP_ATTR_STATS]		= { .type = NLA_NESTED, },
889 	[L2TP_ATTR_IP6_SADDR] = {
890 		.type = NLA_BINARY,
891 		.len = sizeof(struct in6_addr),
892 	},
893 	[L2TP_ATTR_IP6_DADDR] = {
894 		.type = NLA_BINARY,
895 		.len = sizeof(struct in6_addr),
896 	},
897 	[L2TP_ATTR_IFNAME] = {
898 		.type = NLA_NUL_STRING,
899 		.len = IFNAMSIZ - 1,
900 	},
901 	[L2TP_ATTR_COOKIE] = {
902 		.type = NLA_BINARY,
903 		.len = 8,
904 	},
905 	[L2TP_ATTR_PEER_COOKIE] = {
906 		.type = NLA_BINARY,
907 		.len = 8,
908 	},
909 };
910 
911 static const struct genl_ops l2tp_nl_ops[] = {
912 	{
913 		.cmd = L2TP_CMD_NOOP,
914 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
915 		.doit = l2tp_nl_cmd_noop,
916 		/* can be retrieved by unprivileged users */
917 	},
918 	{
919 		.cmd = L2TP_CMD_TUNNEL_CREATE,
920 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
921 		.doit = l2tp_nl_cmd_tunnel_create,
922 		.flags = GENL_UNS_ADMIN_PERM,
923 	},
924 	{
925 		.cmd = L2TP_CMD_TUNNEL_DELETE,
926 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
927 		.doit = l2tp_nl_cmd_tunnel_delete,
928 		.flags = GENL_UNS_ADMIN_PERM,
929 	},
930 	{
931 		.cmd = L2TP_CMD_TUNNEL_MODIFY,
932 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
933 		.doit = l2tp_nl_cmd_tunnel_modify,
934 		.flags = GENL_UNS_ADMIN_PERM,
935 	},
936 	{
937 		.cmd = L2TP_CMD_TUNNEL_GET,
938 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
939 		.doit = l2tp_nl_cmd_tunnel_get,
940 		.dumpit = l2tp_nl_cmd_tunnel_dump,
941 		.flags = GENL_UNS_ADMIN_PERM,
942 	},
943 	{
944 		.cmd = L2TP_CMD_SESSION_CREATE,
945 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
946 		.doit = l2tp_nl_cmd_session_create,
947 		.flags = GENL_UNS_ADMIN_PERM,
948 	},
949 	{
950 		.cmd = L2TP_CMD_SESSION_DELETE,
951 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
952 		.doit = l2tp_nl_cmd_session_delete,
953 		.flags = GENL_UNS_ADMIN_PERM,
954 	},
955 	{
956 		.cmd = L2TP_CMD_SESSION_MODIFY,
957 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
958 		.doit = l2tp_nl_cmd_session_modify,
959 		.flags = GENL_UNS_ADMIN_PERM,
960 	},
961 	{
962 		.cmd = L2TP_CMD_SESSION_GET,
963 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
964 		.doit = l2tp_nl_cmd_session_get,
965 		.dumpit = l2tp_nl_cmd_session_dump,
966 		.flags = GENL_UNS_ADMIN_PERM,
967 	},
968 };
969 
970 static struct genl_family l2tp_nl_family __ro_after_init = {
971 	.name		= L2TP_GENL_NAME,
972 	.version	= L2TP_GENL_VERSION,
973 	.hdrsize	= 0,
974 	.maxattr	= L2TP_ATTR_MAX,
975 	.policy = l2tp_nl_policy,
976 	.netnsok	= true,
977 	.module		= THIS_MODULE,
978 	.ops		= l2tp_nl_ops,
979 	.n_ops		= ARRAY_SIZE(l2tp_nl_ops),
980 	.mcgrps		= l2tp_multicast_group,
981 	.n_mcgrps	= ARRAY_SIZE(l2tp_multicast_group),
982 };
983 
984 int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
985 {
986 	int ret;
987 
988 	ret = -EINVAL;
989 	if (pw_type >= __L2TP_PWTYPE_MAX)
990 		goto err;
991 
992 	genl_lock();
993 	ret = -EBUSY;
994 	if (l2tp_nl_cmd_ops[pw_type])
995 		goto out;
996 
997 	l2tp_nl_cmd_ops[pw_type] = ops;
998 	ret = 0;
999 
1000 out:
1001 	genl_unlock();
1002 err:
1003 	return ret;
1004 }
1005 EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
1006 
1007 void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
1008 {
1009 	if (pw_type < __L2TP_PWTYPE_MAX) {
1010 		genl_lock();
1011 		l2tp_nl_cmd_ops[pw_type] = NULL;
1012 		genl_unlock();
1013 	}
1014 }
1015 EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
1016 
1017 static int __init l2tp_nl_init(void)
1018 {
1019 	pr_info("L2TP netlink interface\n");
1020 	return genl_register_family(&l2tp_nl_family);
1021 }
1022 
1023 static void l2tp_nl_cleanup(void)
1024 {
1025 	genl_unregister_family(&l2tp_nl_family);
1026 }
1027 
1028 module_init(l2tp_nl_init);
1029 module_exit(l2tp_nl_cleanup);
1030 
1031 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1032 MODULE_DESCRIPTION("L2TP netlink");
1033 MODULE_LICENSE("GPL");
1034 MODULE_VERSION("1.0");
1035 MODULE_ALIAS_GENL_FAMILY("l2tp");
1036