blob: fc26ebad2f4fcedb168f45e059830ce9969e0866 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Tom Parkin20dcb112020-07-22 17:32:06 +01002/* L2TP netlink layer, for management
James Chapman309795f2010-04-02 06:19:10 +00003 *
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>
James Chapman309795f2010-04-02 06:19:10 +000011 */
12
Joe Perchesa4ca44f2012-05-16 09:55:56 +000013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
James Chapman309795f2010-04-02 06:19:10 +000015#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
Johannes Berg489111e2016-10-24 14:40:03 +020029static struct genl_family l2tp_nl_family;
James Chapman309795f2010-04-02 06:19:10 +000030
Bill Hong33f72e62014-12-27 10:12:39 -080031static const struct genl_multicast_group l2tp_multicast_group[] = {
32 {
33 .name = L2TP_GENL_MCGROUP,
34 },
35};
36
37static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq,
38 int flags, struct l2tp_tunnel *tunnel, u8 cmd);
39static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq,
40 int flags, struct l2tp_session *session,
41 u8 cmd);
42
James Chapman309795f2010-04-02 06:19:10 +000043/* Accessed under genl lock */
44static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX];
45
Guillaume Naulta4346212017-10-31 17:36:42 +010046static struct l2tp_session *l2tp_nl_session_get(struct genl_info *info)
James Chapman309795f2010-04-02 06:19:10 +000047{
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]);
Guillaume Naulta4346212017-10-31 17:36:42 +010057 session = l2tp_session_get_by_ifname(net, ifname);
James Chapman309795f2010-04-02 06:19:10 +000058 } 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]);
Guillaume Nault54652eb2017-08-25 16:51:40 +020062 tunnel = l2tp_tunnel_get(net, tunnel_id);
63 if (tunnel) {
Guillaume Nault01e28b92018-08-10 13:21:57 +020064 session = l2tp_tunnel_get_session(tunnel, session_id);
Guillaume Nault54652eb2017-08-25 16:51:40 +020065 l2tp_tunnel_dec_refcount(tunnel);
66 }
James Chapman309795f2010-04-02 06:19:10 +000067 }
68
69 return session;
70}
71
72static 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
Thomas Graf58050fc2012-06-28 03:57:45 +000078 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
James Chapman309795f2010-04-02 06:19:10 +000079 if (!msg) {
80 ret = -ENOMEM;
81 goto out;
82 }
83
Eric W. Biederman15e47302012-09-07 20:12:54 +000084 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
James Chapman309795f2010-04-02 06:19:10 +000085 &l2tp_nl_family, 0, L2TP_CMD_NOOP);
Wei Yongjun7f8436a2012-09-24 18:29:01 +000086 if (!hdr) {
87 ret = -EMSGSIZE;
James Chapman309795f2010-04-02 06:19:10 +000088 goto err_out;
89 }
90
91 genlmsg_end(msg, hdr);
92
Eric W. Biederman15e47302012-09-07 20:12:54 +000093 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
James Chapman309795f2010-04-02 06:19:10 +000094
95err_out:
96 nlmsg_free(msg);
97
98out:
99 return ret;
100}
101
Bill Hong33f72e62014-12-27 10:12:39 -0800102static 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
Mark Tomlinson853effc2016-02-15 16:24:44 +1300117 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 }
Bill Hong33f72e62014-12-27 10:12:39 -0800124
125 nlmsg_free(msg);
126
127 return ret;
128}
129
130static 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
Mark Tomlinson853effc2016-02-15 16:24:44 +1300145 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 }
Bill Hong33f72e62014-12-27 10:12:39 -0800152
153 nlmsg_free(msg);
154
155 return ret;
156}
157
James Chapman309795f2010-04-02 06:19:10 +0000158static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info)
159{
160 u32 tunnel_id;
161 u32 peer_tunnel_id;
162 int proto_version;
163 int fd;
164 int ret = 0;
165 struct l2tp_tunnel_cfg cfg = { 0, };
166 struct l2tp_tunnel *tunnel;
167 struct net *net = genl_info_net(info);
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100168 struct nlattr **attrs = info->attrs;
James Chapman309795f2010-04-02 06:19:10 +0000169
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100170 if (!attrs[L2TP_ATTR_CONN_ID]) {
James Chapman309795f2010-04-02 06:19:10 +0000171 ret = -EINVAL;
172 goto out;
173 }
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100174 tunnel_id = nla_get_u32(attrs[L2TP_ATTR_CONN_ID]);
James Chapman309795f2010-04-02 06:19:10 +0000175
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100176 if (!attrs[L2TP_ATTR_PEER_CONN_ID]) {
James Chapman309795f2010-04-02 06:19:10 +0000177 ret = -EINVAL;
178 goto out;
179 }
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100180 peer_tunnel_id = nla_get_u32(attrs[L2TP_ATTR_PEER_CONN_ID]);
James Chapman309795f2010-04-02 06:19:10 +0000181
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100182 if (!attrs[L2TP_ATTR_PROTO_VERSION]) {
James Chapman309795f2010-04-02 06:19:10 +0000183 ret = -EINVAL;
184 goto out;
185 }
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100186 proto_version = nla_get_u8(attrs[L2TP_ATTR_PROTO_VERSION]);
James Chapman309795f2010-04-02 06:19:10 +0000187
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100188 if (!attrs[L2TP_ATTR_ENCAP_TYPE]) {
James Chapman309795f2010-04-02 06:19:10 +0000189 ret = -EINVAL;
190 goto out;
191 }
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100192 cfg.encap = nla_get_u16(attrs[L2TP_ATTR_ENCAP_TYPE]);
James Chapman309795f2010-04-02 06:19:10 +0000193
James Chapman789a4a22010-04-02 06:19:40 +0000194 fd = -1;
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100195 if (attrs[L2TP_ATTR_FD]) {
196 fd = nla_get_u32(attrs[L2TP_ATTR_FD]);
James Chapman789a4a22010-04-02 06:19:40 +0000197 } else {
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000198#if IS_ENABLED(CONFIG_IPV6)
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100199 if (attrs[L2TP_ATTR_IP6_SADDR] && attrs[L2TP_ATTR_IP6_DADDR]) {
200 cfg.local_ip6 = nla_data(attrs[L2TP_ATTR_IP6_SADDR]);
201 cfg.peer_ip6 = nla_data(attrs[L2TP_ATTR_IP6_DADDR]);
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000202 } else
203#endif
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100204 if (attrs[L2TP_ATTR_IP_SADDR] && attrs[L2TP_ATTR_IP_DADDR]) {
205 cfg.local_ip.s_addr = nla_get_in_addr(attrs[L2TP_ATTR_IP_SADDR]);
206 cfg.peer_ip.s_addr = nla_get_in_addr(attrs[L2TP_ATTR_IP_DADDR]);
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000207 } else {
208 ret = -EINVAL;
209 goto out;
210 }
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100211 if (attrs[L2TP_ATTR_UDP_SPORT])
212 cfg.local_udp_port = nla_get_u16(attrs[L2TP_ATTR_UDP_SPORT]);
213 if (attrs[L2TP_ATTR_UDP_DPORT])
214 cfg.peer_udp_port = nla_get_u16(attrs[L2TP_ATTR_UDP_DPORT]);
215 cfg.use_udp_checksums = nla_get_flag(attrs[L2TP_ATTR_UDP_CSUM]);
Tom Herbert6b649fea2014-05-23 08:47:40 -0700216
217#if IS_ENABLED(CONFIG_IPV6)
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100218 cfg.udp6_zero_tx_checksums = nla_get_flag(attrs[L2TP_ATTR_UDP_ZERO_CSUM6_TX]);
219 cfg.udp6_zero_rx_checksums = nla_get_flag(attrs[L2TP_ATTR_UDP_ZERO_CSUM6_RX]);
Tom Herbert6b649fea2014-05-23 08:47:40 -0700220#endif
James Chapman309795f2010-04-02 06:19:10 +0000221 }
James Chapman309795f2010-04-02 06:19:10 +0000222
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100223 if (attrs[L2TP_ATTR_DEBUG])
224 cfg.debug = nla_get_u32(attrs[L2TP_ATTR_DEBUG]);
James Chapman309795f2010-04-02 06:19:10 +0000225
James Chapman309795f2010-04-02 06:19:10 +0000226 ret = -EINVAL;
227 switch (cfg.encap) {
228 case L2TP_ENCAPTYPE_UDP:
229 case L2TP_ENCAPTYPE_IP:
230 ret = l2tp_tunnel_create(net, fd, proto_version, tunnel_id,
231 peer_tunnel_id, &cfg, &tunnel);
232 break;
233 }
234
Guillaume Nault6b9f3422018-04-10 21:01:12 +0200235 if (ret < 0)
236 goto out;
237
238 l2tp_tunnel_inc_refcount(tunnel);
239 ret = l2tp_tunnel_register(tunnel, net, &cfg);
240 if (ret < 0) {
241 kfree(tunnel);
242 goto out;
243 }
244 ret = l2tp_tunnel_notify(&l2tp_nl_family, info, tunnel,
245 L2TP_CMD_TUNNEL_CREATE);
246 l2tp_tunnel_dec_refcount(tunnel);
247
James Chapman309795f2010-04-02 06:19:10 +0000248out:
249 return ret;
250}
251
252static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info)
253{
254 struct l2tp_tunnel *tunnel;
255 u32 tunnel_id;
256 int ret = 0;
257 struct net *net = genl_info_net(info);
258
259 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
260 ret = -EINVAL;
261 goto out;
262 }
263 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
264
Guillaume Naultbb0a32c2017-08-25 16:51:42 +0200265 tunnel = l2tp_tunnel_get(net, tunnel_id);
266 if (!tunnel) {
James Chapman309795f2010-04-02 06:19:10 +0000267 ret = -ENODEV;
268 goto out;
269 }
270
Bill Hong33f72e62014-12-27 10:12:39 -0800271 l2tp_tunnel_notify(&l2tp_nl_family, info,
272 tunnel, L2TP_CMD_TUNNEL_DELETE);
273
Jiri Slaby4dc12ff2017-10-25 15:57:55 +0200274 l2tp_tunnel_delete(tunnel);
James Chapman309795f2010-04-02 06:19:10 +0000275
Guillaume Naultbb0a32c2017-08-25 16:51:42 +0200276 l2tp_tunnel_dec_refcount(tunnel);
277
James Chapman309795f2010-04-02 06:19:10 +0000278out:
279 return ret;
280}
281
282static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info)
283{
284 struct l2tp_tunnel *tunnel;
285 u32 tunnel_id;
286 int ret = 0;
287 struct net *net = genl_info_net(info);
288
289 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
290 ret = -EINVAL;
291 goto out;
292 }
293 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
294
Guillaume Nault8c0e4212017-08-25 16:51:42 +0200295 tunnel = l2tp_tunnel_get(net, tunnel_id);
296 if (!tunnel) {
James Chapman309795f2010-04-02 06:19:10 +0000297 ret = -ENODEV;
298 goto out;
299 }
300
301 if (info->attrs[L2TP_ATTR_DEBUG])
302 tunnel->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
303
Bill Hong33f72e62014-12-27 10:12:39 -0800304 ret = l2tp_tunnel_notify(&l2tp_nl_family, info,
305 tunnel, L2TP_CMD_TUNNEL_MODIFY);
306
Guillaume Nault8c0e4212017-08-25 16:51:42 +0200307 l2tp_tunnel_dec_refcount(tunnel);
308
James Chapman309795f2010-04-02 06:19:10 +0000309out:
310 return ret;
311}
312
Eric W. Biederman15e47302012-09-07 20:12:54 +0000313static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
Bill Hong33f72e62014-12-27 10:12:39 -0800314 struct l2tp_tunnel *tunnel, u8 cmd)
James Chapman309795f2010-04-02 06:19:10 +0000315{
316 void *hdr;
317 struct nlattr *nest;
318 struct sock *sk = NULL;
319 struct inet_sock *inet;
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000320#if IS_ENABLED(CONFIG_IPV6)
321 struct ipv6_pinfo *np = NULL;
322#endif
James Chapman309795f2010-04-02 06:19:10 +0000323
Bill Hong33f72e62014-12-27 10:12:39 -0800324 hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
Wei Yongjun7f8436a2012-09-24 18:29:01 +0000325 if (!hdr)
326 return -EMSGSIZE;
James Chapman309795f2010-04-02 06:19:10 +0000327
David S. Miller60aed2a2012-04-01 19:59:31 -0400328 if (nla_put_u8(skb, L2TP_ATTR_PROTO_VERSION, tunnel->version) ||
329 nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
330 nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
331 nla_put_u32(skb, L2TP_ATTR_DEBUG, tunnel->debug) ||
332 nla_put_u16(skb, L2TP_ATTR_ENCAP_TYPE, tunnel->encap))
333 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000334
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200335 nest = nla_nest_start_noflag(skb, L2TP_ATTR_STATS);
Tom Parkin0febc7b2020-07-23 12:29:50 +0100336 if (!nest)
James Chapman309795f2010-04-02 06:19:10 +0000337 goto nla_put_failure;
338
Nicolas Dichtel1c714a92016-04-25 10:25:19 +0200339 if (nla_put_u64_64bit(skb, L2TP_ATTR_TX_PACKETS,
340 atomic_long_read(&tunnel->stats.tx_packets),
341 L2TP_ATTR_STATS_PAD) ||
342 nla_put_u64_64bit(skb, L2TP_ATTR_TX_BYTES,
343 atomic_long_read(&tunnel->stats.tx_bytes),
344 L2TP_ATTR_STATS_PAD) ||
345 nla_put_u64_64bit(skb, L2TP_ATTR_TX_ERRORS,
346 atomic_long_read(&tunnel->stats.tx_errors),
347 L2TP_ATTR_STATS_PAD) ||
348 nla_put_u64_64bit(skb, L2TP_ATTR_RX_PACKETS,
349 atomic_long_read(&tunnel->stats.rx_packets),
350 L2TP_ATTR_STATS_PAD) ||
351 nla_put_u64_64bit(skb, L2TP_ATTR_RX_BYTES,
352 atomic_long_read(&tunnel->stats.rx_bytes),
353 L2TP_ATTR_STATS_PAD) ||
354 nla_put_u64_64bit(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
355 atomic_long_read(&tunnel->stats.rx_seq_discards),
356 L2TP_ATTR_STATS_PAD) ||
357 nla_put_u64_64bit(skb, L2TP_ATTR_RX_OOS_PACKETS,
358 atomic_long_read(&tunnel->stats.rx_oos_packets),
359 L2TP_ATTR_STATS_PAD) ||
360 nla_put_u64_64bit(skb, L2TP_ATTR_RX_ERRORS,
361 atomic_long_read(&tunnel->stats.rx_errors),
362 L2TP_ATTR_STATS_PAD))
David S. Miller60aed2a2012-04-01 19:59:31 -0400363 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000364 nla_nest_end(skb, nest);
365
366 sk = tunnel->sock;
367 if (!sk)
368 goto out;
369
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000370#if IS_ENABLED(CONFIG_IPV6)
371 if (sk->sk_family == AF_INET6)
372 np = inet6_sk(sk);
373#endif
374
James Chapman309795f2010-04-02 06:19:10 +0000375 inet = inet_sk(sk);
376
377 switch (tunnel->encap) {
378 case L2TP_ENCAPTYPE_UDP:
Asbjørn Sloth Tønnesen7ff516f2016-11-07 20:39:25 +0000379 switch (sk->sk_family) {
380 case AF_INET:
381 if (nla_put_u8(skb, L2TP_ATTR_UDP_CSUM, !sk->sk_no_check_tx))
382 goto nla_put_failure;
383 break;
Asbjørn Sloth Tønnesen97b7af02016-11-07 20:39:26 +0000384#if IS_ENABLED(CONFIG_IPV6)
385 case AF_INET6:
386 if (udp_get_no_check6_tx(sk) &&
387 nla_put_flag(skb, L2TP_ATTR_UDP_ZERO_CSUM6_TX))
388 goto nla_put_failure;
389 if (udp_get_no_check6_rx(sk) &&
390 nla_put_flag(skb, L2TP_ATTR_UDP_ZERO_CSUM6_RX))
391 goto nla_put_failure;
392 break;
393#endif
Asbjørn Sloth Tønnesen7ff516f2016-11-07 20:39:25 +0000394 }
David S. Miller60aed2a2012-04-01 19:59:31 -0400395 if (nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
Asbjørn Sloth Tønnesen7ff516f2016-11-07 20:39:25 +0000396 nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)))
David S. Miller60aed2a2012-04-01 19:59:31 -0400397 goto nla_put_failure;
Gustavo A. R. Silvabe070c72017-10-17 17:42:53 -0500398 /* fall through */
James Chapman309795f2010-04-02 06:19:10 +0000399 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000400#if IS_ENABLED(CONFIG_IPV6)
401 if (np) {
Jiri Benc930345e2015-03-29 16:59:25 +0200402 if (nla_put_in6_addr(skb, L2TP_ATTR_IP6_SADDR,
403 &np->saddr) ||
404 nla_put_in6_addr(skb, L2TP_ATTR_IP6_DADDR,
405 &sk->sk_v6_daddr))
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000406 goto nla_put_failure;
407 } else
408#endif
Jiri Benc930345e2015-03-29 16:59:25 +0200409 if (nla_put_in_addr(skb, L2TP_ATTR_IP_SADDR,
410 inet->inet_saddr) ||
411 nla_put_in_addr(skb, L2TP_ATTR_IP_DADDR,
412 inet->inet_daddr))
David S. Miller60aed2a2012-04-01 19:59:31 -0400413 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000414 break;
415 }
416
417out:
Johannes Berg053c0952015-01-16 22:09:00 +0100418 genlmsg_end(skb, hdr);
419 return 0;
James Chapman309795f2010-04-02 06:19:10 +0000420
421nla_put_failure:
422 genlmsg_cancel(skb, hdr);
423 return -1;
424}
425
426static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
427{
428 struct l2tp_tunnel *tunnel;
429 struct sk_buff *msg;
430 u32 tunnel_id;
431 int ret = -ENOBUFS;
432 struct net *net = genl_info_net(info);
433
434 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
435 ret = -EINVAL;
Guillaume Nault4e4b21d2017-08-25 16:51:43 +0200436 goto err;
James Chapman309795f2010-04-02 06:19:10 +0000437 }
438
439 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
440
Thomas Graf58050fc2012-06-28 03:57:45 +0000441 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
James Chapman309795f2010-04-02 06:19:10 +0000442 if (!msg) {
443 ret = -ENOMEM;
Guillaume Nault4e4b21d2017-08-25 16:51:43 +0200444 goto err;
445 }
446
447 tunnel = l2tp_tunnel_get(net, tunnel_id);
448 if (!tunnel) {
449 ret = -ENODEV;
450 goto err_nlmsg;
James Chapman309795f2010-04-02 06:19:10 +0000451 }
452
Eric W. Biederman15e47302012-09-07 20:12:54 +0000453 ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
Bill Hong33f72e62014-12-27 10:12:39 -0800454 NLM_F_ACK, tunnel, L2TP_CMD_TUNNEL_GET);
James Chapman309795f2010-04-02 06:19:10 +0000455 if (ret < 0)
Guillaume Nault4e4b21d2017-08-25 16:51:43 +0200456 goto err_nlmsg_tunnel;
457
458 l2tp_tunnel_dec_refcount(tunnel);
James Chapman309795f2010-04-02 06:19:10 +0000459
Eric W. Biederman15e47302012-09-07 20:12:54 +0000460 return genlmsg_unicast(net, msg, info->snd_portid);
James Chapman309795f2010-04-02 06:19:10 +0000461
Guillaume Nault4e4b21d2017-08-25 16:51:43 +0200462err_nlmsg_tunnel:
463 l2tp_tunnel_dec_refcount(tunnel);
464err_nlmsg:
James Chapman309795f2010-04-02 06:19:10 +0000465 nlmsg_free(msg);
Guillaume Nault4e4b21d2017-08-25 16:51:43 +0200466err:
James Chapman309795f2010-04-02 06:19:10 +0000467 return ret;
468}
469
470static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
471{
472 int ti = cb->args[0];
473 struct l2tp_tunnel *tunnel;
474 struct net *net = sock_net(skb->sk);
475
476 for (;;) {
Guillaume Nault5846c132018-04-12 20:50:33 +0200477 tunnel = l2tp_tunnel_get_nth(net, ti);
Tom Parkin0febc7b2020-07-23 12:29:50 +0100478 if (!tunnel)
James Chapman309795f2010-04-02 06:19:10 +0000479 goto out;
480
Eric W. Biederman15e47302012-09-07 20:12:54 +0000481 if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).portid,
James Chapman309795f2010-04-02 06:19:10 +0000482 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Guillaume Nault5846c132018-04-12 20:50:33 +0200483 tunnel, L2TP_CMD_TUNNEL_GET) < 0) {
484 l2tp_tunnel_dec_refcount(tunnel);
James Chapman309795f2010-04-02 06:19:10 +0000485 goto out;
Guillaume Nault5846c132018-04-12 20:50:33 +0200486 }
487 l2tp_tunnel_dec_refcount(tunnel);
James Chapman309795f2010-04-02 06:19:10 +0000488
489 ti++;
490 }
491
492out:
493 cb->args[0] = ti;
494
495 return skb->len;
496}
497
498static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
499{
500 u32 tunnel_id = 0;
501 u32 session_id;
502 u32 peer_session_id;
503 int ret = 0;
504 struct l2tp_tunnel *tunnel;
505 struct l2tp_session *session;
506 struct l2tp_session_cfg cfg = { 0, };
507 struct net *net = genl_info_net(info);
508
509 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
510 ret = -EINVAL;
511 goto out;
512 }
Guillaume Naulte702c122017-08-25 16:51:46 +0200513
James Chapman309795f2010-04-02 06:19:10 +0000514 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
Guillaume Naulte702c122017-08-25 16:51:46 +0200515 tunnel = l2tp_tunnel_get(net, tunnel_id);
James Chapman309795f2010-04-02 06:19:10 +0000516 if (!tunnel) {
517 ret = -ENODEV;
518 goto out;
519 }
520
521 if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
522 ret = -EINVAL;
Guillaume Naulte702c122017-08-25 16:51:46 +0200523 goto out_tunnel;
James Chapman309795f2010-04-02 06:19:10 +0000524 }
525 session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
James Chapman309795f2010-04-02 06:19:10 +0000526
527 if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
528 ret = -EINVAL;
Guillaume Naulte702c122017-08-25 16:51:46 +0200529 goto out_tunnel;
James Chapman309795f2010-04-02 06:19:10 +0000530 }
531 peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
532
533 if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
534 ret = -EINVAL;
Guillaume Naulte702c122017-08-25 16:51:46 +0200535 goto out_tunnel;
James Chapman309795f2010-04-02 06:19:10 +0000536 }
537 cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
538 if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
539 ret = -EINVAL;
Guillaume Naulte702c122017-08-25 16:51:46 +0200540 goto out_tunnel;
James Chapman309795f2010-04-02 06:19:10 +0000541 }
542
Guillaume Naultde9bada2018-06-15 15:39:17 +0200543 /* L2TPv2 only accepts PPP pseudo-wires */
544 if (tunnel->version == 2 && cfg.pw_type != L2TP_PWTYPE_PPP) {
545 ret = -EPROTONOSUPPORT;
546 goto out_tunnel;
547 }
548
James Chapman309795f2010-04-02 06:19:10 +0000549 if (tunnel->version > 2) {
Lorenzo Bianconidfffc972018-01-16 23:01:54 +0100550 if (info->attrs[L2TP_ATTR_L2SPEC_TYPE]) {
James Chapman309795f2010-04-02 06:19:10 +0000551 cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
Lorenzo Bianconidfffc972018-01-16 23:01:54 +0100552 if (cfg.l2specific_type != L2TP_L2SPECTYPE_DEFAULT &&
553 cfg.l2specific_type != L2TP_L2SPECTYPE_NONE) {
554 ret = -EINVAL;
555 goto out_tunnel;
556 }
557 } else {
558 cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
559 }
James Chapman309795f2010-04-02 06:19:10 +0000560
James Chapman309795f2010-04-02 06:19:10 +0000561 if (info->attrs[L2TP_ATTR_COOKIE]) {
562 u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
Tom Parkinb71a61c2020-07-22 17:32:05 +0100563
James Chapman309795f2010-04-02 06:19:10 +0000564 if (len > 8) {
565 ret = -EINVAL;
Guillaume Naulte702c122017-08-25 16:51:46 +0200566 goto out_tunnel;
James Chapman309795f2010-04-02 06:19:10 +0000567 }
568 cfg.cookie_len = len;
569 memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
570 }
571 if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
572 u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
Tom Parkinb71a61c2020-07-22 17:32:05 +0100573
James Chapman309795f2010-04-02 06:19:10 +0000574 if (len > 8) {
575 ret = -EINVAL;
Guillaume Naulte702c122017-08-25 16:51:46 +0200576 goto out_tunnel;
James Chapman309795f2010-04-02 06:19:10 +0000577 }
578 cfg.peer_cookie_len = len;
579 memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
580 }
581 if (info->attrs[L2TP_ATTR_IFNAME])
582 cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
James Chapman309795f2010-04-02 06:19:10 +0000583 }
584
585 if (info->attrs[L2TP_ATTR_DEBUG])
586 cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
587
588 if (info->attrs[L2TP_ATTR_RECV_SEQ])
589 cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
590
591 if (info->attrs[L2TP_ATTR_SEND_SEQ])
592 cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
593
594 if (info->attrs[L2TP_ATTR_LNS_MODE])
595 cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
596
597 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
598 cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
599
stephen hemmingerf1f39f92015-09-23 21:33:34 -0700600#ifdef CONFIG_MODULES
Tom Parkin0febc7b2020-07-23 12:29:50 +0100601 if (!l2tp_nl_cmd_ops[cfg.pw_type]) {
stephen hemmingerf1f39f92015-09-23 21:33:34 -0700602 genl_unlock();
603 request_module("net-l2tp-type-%u", cfg.pw_type);
604 genl_lock();
605 }
606#endif
Tom Parkin0febc7b2020-07-23 12:29:50 +0100607 if (!l2tp_nl_cmd_ops[cfg.pw_type] || !l2tp_nl_cmd_ops[cfg.pw_type]->session_create) {
James Chapman309795f2010-04-02 06:19:10 +0000608 ret = -EPROTONOSUPPORT;
Guillaume Naulte702c122017-08-25 16:51:46 +0200609 goto out_tunnel;
James Chapman309795f2010-04-02 06:19:10 +0000610 }
611
Guillaume Naultf026bc22017-09-01 17:58:51 +0200612 ret = l2tp_nl_cmd_ops[cfg.pw_type]->session_create(net, tunnel,
613 session_id,
614 peer_session_id,
615 &cfg);
James Chapman309795f2010-04-02 06:19:10 +0000616
Bill Hong33f72e62014-12-27 10:12:39 -0800617 if (ret >= 0) {
Guillaume Nault01e28b92018-08-10 13:21:57 +0200618 session = l2tp_tunnel_get_session(tunnel, session_id);
Guillaume Nault5e6a9e52017-03-31 13:02:29 +0200619 if (session) {
Bill Hong33f72e62014-12-27 10:12:39 -0800620 ret = l2tp_session_notify(&l2tp_nl_family, info, session,
621 L2TP_CMD_SESSION_CREATE);
Guillaume Nault5e6a9e52017-03-31 13:02:29 +0200622 l2tp_session_dec_refcount(session);
623 }
Bill Hong33f72e62014-12-27 10:12:39 -0800624 }
625
Guillaume Naulte702c122017-08-25 16:51:46 +0200626out_tunnel:
627 l2tp_tunnel_dec_refcount(tunnel);
James Chapman309795f2010-04-02 06:19:10 +0000628out:
629 return ret;
630}
631
632static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
633{
634 int ret = 0;
635 struct l2tp_session *session;
636 u16 pw_type;
637
Guillaume Naulta4346212017-10-31 17:36:42 +0100638 session = l2tp_nl_session_get(info);
Tom Parkin0febc7b2020-07-23 12:29:50 +0100639 if (!session) {
James Chapman309795f2010-04-02 06:19:10 +0000640 ret = -ENODEV;
641 goto out;
642 }
643
Bill Hong33f72e62014-12-27 10:12:39 -0800644 l2tp_session_notify(&l2tp_nl_family, info,
645 session, L2TP_CMD_SESSION_DELETE);
646
James Chapman309795f2010-04-02 06:19:10 +0000647 pw_type = session->pwtype;
648 if (pw_type < __L2TP_PWTYPE_MAX)
649 if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
650 ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
651
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200652 l2tp_session_dec_refcount(session);
653
James Chapman309795f2010-04-02 06:19:10 +0000654out:
655 return ret;
656}
657
658static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
659{
660 int ret = 0;
661 struct l2tp_session *session;
662
Guillaume Naulta4346212017-10-31 17:36:42 +0100663 session = l2tp_nl_session_get(info);
Tom Parkin0febc7b2020-07-23 12:29:50 +0100664 if (!session) {
James Chapman309795f2010-04-02 06:19:10 +0000665 ret = -ENODEV;
666 goto out;
667 }
668
669 if (info->attrs[L2TP_ATTR_DEBUG])
670 session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
671
James Chapman309795f2010-04-02 06:19:10 +0000672 if (info->attrs[L2TP_ATTR_RECV_SEQ])
673 session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
674
Guillaume Naultbb5016e2014-03-06 11:14:30 +0100675 if (info->attrs[L2TP_ATTR_SEND_SEQ]) {
James Chapman309795f2010-04-02 06:19:10 +0000676 session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
Guillaume Naultbb5016e2014-03-06 11:14:30 +0100677 l2tp_session_set_header_len(session, session->tunnel->version);
678 }
James Chapman309795f2010-04-02 06:19:10 +0000679
680 if (info->attrs[L2TP_ATTR_LNS_MODE])
681 session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
682
683 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
684 session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
685
Bill Hong33f72e62014-12-27 10:12:39 -0800686 ret = l2tp_session_notify(&l2tp_nl_family, info,
687 session, L2TP_CMD_SESSION_MODIFY);
688
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200689 l2tp_session_dec_refcount(session);
690
James Chapman309795f2010-04-02 06:19:10 +0000691out:
692 return ret;
693}
694
Eric W. Biederman15e47302012-09-07 20:12:54 +0000695static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
Bill Hong33f72e62014-12-27 10:12:39 -0800696 struct l2tp_session *session, u8 cmd)
James Chapman309795f2010-04-02 06:19:10 +0000697{
698 void *hdr;
699 struct nlattr *nest;
700 struct l2tp_tunnel *tunnel = session->tunnel;
James Chapman309795f2010-04-02 06:19:10 +0000701
Bill Hong33f72e62014-12-27 10:12:39 -0800702 hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
Wei Yongjun7f8436a2012-09-24 18:29:01 +0000703 if (!hdr)
704 return -EMSGSIZE;
James Chapman309795f2010-04-02 06:19:10 +0000705
David S. Miller60aed2a2012-04-01 19:59:31 -0400706 if (nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
707 nla_put_u32(skb, L2TP_ATTR_SESSION_ID, session->session_id) ||
708 nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100709 nla_put_u32(skb, L2TP_ATTR_PEER_SESSION_ID, session->peer_session_id) ||
David S. Miller60aed2a2012-04-01 19:59:31 -0400710 nla_put_u32(skb, L2TP_ATTR_DEBUG, session->debug) ||
Guillaume Naulte9697e22018-08-03 12:38:39 +0200711 nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype))
David S. Miller60aed2a2012-04-01 19:59:31 -0400712 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000713
Alan Coxe269ed22012-10-25 05:22:01 +0000714 if ((session->ifname[0] &&
David S. Miller60aed2a2012-04-01 19:59:31 -0400715 nla_put_string(skb, L2TP_ATTR_IFNAME, session->ifname)) ||
716 (session->cookie_len &&
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100717 nla_put(skb, L2TP_ATTR_COOKIE, session->cookie_len, session->cookie)) ||
David S. Miller60aed2a2012-04-01 19:59:31 -0400718 (session->peer_cookie_len &&
Tom Parkin9f7da9a2020-07-22 17:32:07 +0100719 nla_put(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len, session->peer_cookie)) ||
David S. Miller60aed2a2012-04-01 19:59:31 -0400720 nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
721 nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
722 nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
Guillaume Naultd6a61ec2018-08-10 13:21:55 +0200723 (l2tp_tunnel_uses_xfrm(tunnel) &&
David S. Miller60aed2a2012-04-01 19:59:31 -0400724 nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
David S. Miller60aed2a2012-04-01 19:59:31 -0400725 (session->reorder_timeout &&
Nicolas Dichtel2175d872016-04-22 17:31:21 +0200726 nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT,
727 session->reorder_timeout, L2TP_ATTR_PAD)))
David S. Miller60aed2a2012-04-01 19:59:31 -0400728 goto nla_put_failure;
James Chapman5de7aee2012-04-29 21:48:46 +0000729
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200730 nest = nla_nest_start_noflag(skb, L2TP_ATTR_STATS);
Tom Parkin0febc7b2020-07-23 12:29:50 +0100731 if (!nest)
James Chapman309795f2010-04-02 06:19:10 +0000732 goto nla_put_failure;
James Chapman5de7aee2012-04-29 21:48:46 +0000733
Nicolas Dichtel1c714a92016-04-25 10:25:19 +0200734 if (nla_put_u64_64bit(skb, L2TP_ATTR_TX_PACKETS,
735 atomic_long_read(&session->stats.tx_packets),
736 L2TP_ATTR_STATS_PAD) ||
737 nla_put_u64_64bit(skb, L2TP_ATTR_TX_BYTES,
738 atomic_long_read(&session->stats.tx_bytes),
739 L2TP_ATTR_STATS_PAD) ||
740 nla_put_u64_64bit(skb, L2TP_ATTR_TX_ERRORS,
741 atomic_long_read(&session->stats.tx_errors),
742 L2TP_ATTR_STATS_PAD) ||
743 nla_put_u64_64bit(skb, L2TP_ATTR_RX_PACKETS,
744 atomic_long_read(&session->stats.rx_packets),
745 L2TP_ATTR_STATS_PAD) ||
746 nla_put_u64_64bit(skb, L2TP_ATTR_RX_BYTES,
747 atomic_long_read(&session->stats.rx_bytes),
748 L2TP_ATTR_STATS_PAD) ||
749 nla_put_u64_64bit(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
750 atomic_long_read(&session->stats.rx_seq_discards),
751 L2TP_ATTR_STATS_PAD) ||
752 nla_put_u64_64bit(skb, L2TP_ATTR_RX_OOS_PACKETS,
753 atomic_long_read(&session->stats.rx_oos_packets),
754 L2TP_ATTR_STATS_PAD) ||
755 nla_put_u64_64bit(skb, L2TP_ATTR_RX_ERRORS,
756 atomic_long_read(&session->stats.rx_errors),
757 L2TP_ATTR_STATS_PAD))
David S. Miller60aed2a2012-04-01 19:59:31 -0400758 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000759 nla_nest_end(skb, nest);
760
Johannes Berg053c0952015-01-16 22:09:00 +0100761 genlmsg_end(skb, hdr);
762 return 0;
James Chapman309795f2010-04-02 06:19:10 +0000763
764 nla_put_failure:
765 genlmsg_cancel(skb, hdr);
766 return -1;
767}
768
769static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
770{
771 struct l2tp_session *session;
772 struct sk_buff *msg;
773 int ret;
774
Guillaume Naulta4346212017-10-31 17:36:42 +0100775 session = l2tp_nl_session_get(info);
Tom Parkin0febc7b2020-07-23 12:29:50 +0100776 if (!session) {
James Chapman309795f2010-04-02 06:19:10 +0000777 ret = -ENODEV;
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200778 goto err;
James Chapman309795f2010-04-02 06:19:10 +0000779 }
780
Thomas Graf58050fc2012-06-28 03:57:45 +0000781 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
James Chapman309795f2010-04-02 06:19:10 +0000782 if (!msg) {
783 ret = -ENOMEM;
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200784 goto err_ref;
James Chapman309795f2010-04-02 06:19:10 +0000785 }
786
Eric W. Biederman15e47302012-09-07 20:12:54 +0000787 ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
Bill Hong33f72e62014-12-27 10:12:39 -0800788 0, session, L2TP_CMD_SESSION_GET);
James Chapman309795f2010-04-02 06:19:10 +0000789 if (ret < 0)
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200790 goto err_ref_msg;
James Chapman309795f2010-04-02 06:19:10 +0000791
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200792 ret = genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
James Chapman309795f2010-04-02 06:19:10 +0000793
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200794 l2tp_session_dec_refcount(session);
795
796 return ret;
797
798err_ref_msg:
James Chapman309795f2010-04-02 06:19:10 +0000799 nlmsg_free(msg);
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200800err_ref:
801 l2tp_session_dec_refcount(session);
802err:
James Chapman309795f2010-04-02 06:19:10 +0000803 return ret;
804}
805
806static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
807{
808 struct net *net = sock_net(skb->sk);
809 struct l2tp_session *session;
810 struct l2tp_tunnel *tunnel = NULL;
811 int ti = cb->args[0];
812 int si = cb->args[1];
813
814 for (;;) {
Tom Parkin0febc7b2020-07-23 12:29:50 +0100815 if (!tunnel) {
Guillaume Nault5846c132018-04-12 20:50:33 +0200816 tunnel = l2tp_tunnel_get_nth(net, ti);
Tom Parkin0febc7b2020-07-23 12:29:50 +0100817 if (!tunnel)
James Chapman309795f2010-04-02 06:19:10 +0000818 goto out;
819 }
820
Guillaume Naulta4346212017-10-31 17:36:42 +0100821 session = l2tp_session_get_nth(tunnel, si);
Tom Parkin0febc7b2020-07-23 12:29:50 +0100822 if (!session) {
James Chapman309795f2010-04-02 06:19:10 +0000823 ti++;
Guillaume Nault5846c132018-04-12 20:50:33 +0200824 l2tp_tunnel_dec_refcount(tunnel);
James Chapman309795f2010-04-02 06:19:10 +0000825 tunnel = NULL;
826 si = 0;
827 continue;
828 }
829
Eric W. Biederman15e47302012-09-07 20:12:54 +0000830 if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).portid,
James Chapman309795f2010-04-02 06:19:10 +0000831 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Guillaume Naulte08293a2017-04-03 12:03:13 +0200832 session, L2TP_CMD_SESSION_GET) < 0) {
833 l2tp_session_dec_refcount(session);
Guillaume Nault5846c132018-04-12 20:50:33 +0200834 l2tp_tunnel_dec_refcount(tunnel);
James Chapman309795f2010-04-02 06:19:10 +0000835 break;
Guillaume Naulte08293a2017-04-03 12:03:13 +0200836 }
837 l2tp_session_dec_refcount(session);
James Chapman309795f2010-04-02 06:19:10 +0000838
839 si++;
840 }
841
842out:
843 cb->args[0] = ti;
844 cb->args[1] = si;
845
846 return skb->len;
847}
848
stephen hemmingerf5bb3412016-08-31 23:24:41 -0700849static const struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
James Chapman309795f2010-04-02 06:19:10 +0000850 [L2TP_ATTR_NONE] = { .type = NLA_UNSPEC, },
851 [L2TP_ATTR_PW_TYPE] = { .type = NLA_U16, },
852 [L2TP_ATTR_ENCAP_TYPE] = { .type = NLA_U16, },
853 [L2TP_ATTR_OFFSET] = { .type = NLA_U16, },
854 [L2TP_ATTR_DATA_SEQ] = { .type = NLA_U8, },
855 [L2TP_ATTR_L2SPEC_TYPE] = { .type = NLA_U8, },
856 [L2TP_ATTR_L2SPEC_LEN] = { .type = NLA_U8, },
857 [L2TP_ATTR_PROTO_VERSION] = { .type = NLA_U8, },
858 [L2TP_ATTR_CONN_ID] = { .type = NLA_U32, },
859 [L2TP_ATTR_PEER_CONN_ID] = { .type = NLA_U32, },
860 [L2TP_ATTR_SESSION_ID] = { .type = NLA_U32, },
861 [L2TP_ATTR_PEER_SESSION_ID] = { .type = NLA_U32, },
862 [L2TP_ATTR_UDP_CSUM] = { .type = NLA_U8, },
863 [L2TP_ATTR_VLAN_ID] = { .type = NLA_U16, },
864 [L2TP_ATTR_DEBUG] = { .type = NLA_U32, },
865 [L2TP_ATTR_RECV_SEQ] = { .type = NLA_U8, },
866 [L2TP_ATTR_SEND_SEQ] = { .type = NLA_U8, },
867 [L2TP_ATTR_LNS_MODE] = { .type = NLA_U8, },
868 [L2TP_ATTR_USING_IPSEC] = { .type = NLA_U8, },
869 [L2TP_ATTR_RECV_TIMEOUT] = { .type = NLA_MSECS, },
870 [L2TP_ATTR_FD] = { .type = NLA_U32, },
871 [L2TP_ATTR_IP_SADDR] = { .type = NLA_U32, },
872 [L2TP_ATTR_IP_DADDR] = { .type = NLA_U32, },
873 [L2TP_ATTR_UDP_SPORT] = { .type = NLA_U16, },
874 [L2TP_ATTR_UDP_DPORT] = { .type = NLA_U16, },
875 [L2TP_ATTR_MTU] = { .type = NLA_U16, },
876 [L2TP_ATTR_MRU] = { .type = NLA_U16, },
877 [L2TP_ATTR_STATS] = { .type = NLA_NESTED, },
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000878 [L2TP_ATTR_IP6_SADDR] = {
879 .type = NLA_BINARY,
880 .len = sizeof(struct in6_addr),
881 },
882 [L2TP_ATTR_IP6_DADDR] = {
883 .type = NLA_BINARY,
884 .len = sizeof(struct in6_addr),
885 },
James Chapman309795f2010-04-02 06:19:10 +0000886 [L2TP_ATTR_IFNAME] = {
887 .type = NLA_NUL_STRING,
888 .len = IFNAMSIZ - 1,
889 },
890 [L2TP_ATTR_COOKIE] = {
891 .type = NLA_BINARY,
892 .len = 8,
893 },
894 [L2TP_ATTR_PEER_COOKIE] = {
895 .type = NLA_BINARY,
896 .len = 8,
897 },
898};
899
Johannes Berg4534de82013-11-14 17:14:46 +0100900static const struct genl_ops l2tp_nl_ops[] = {
James Chapman309795f2010-04-02 06:19:10 +0000901 {
902 .cmd = L2TP_CMD_NOOP,
Johannes Bergef6243a2019-04-26 14:07:31 +0200903 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
James Chapman309795f2010-04-02 06:19:10 +0000904 .doit = l2tp_nl_cmd_noop,
James Chapman309795f2010-04-02 06:19:10 +0000905 /* can be retrieved by unprivileged users */
906 },
907 {
908 .cmd = L2TP_CMD_TUNNEL_CREATE,
Johannes Bergef6243a2019-04-26 14:07:31 +0200909 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
James Chapman309795f2010-04-02 06:19:10 +0000910 .doit = l2tp_nl_cmd_tunnel_create,
Michael Weiß2abe0522020-04-07 13:11:48 +0200911 .flags = GENL_UNS_ADMIN_PERM,
James Chapman309795f2010-04-02 06:19:10 +0000912 },
913 {
914 .cmd = L2TP_CMD_TUNNEL_DELETE,
Johannes Bergef6243a2019-04-26 14:07:31 +0200915 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
James Chapman309795f2010-04-02 06:19:10 +0000916 .doit = l2tp_nl_cmd_tunnel_delete,
Michael Weiß2abe0522020-04-07 13:11:48 +0200917 .flags = GENL_UNS_ADMIN_PERM,
James Chapman309795f2010-04-02 06:19:10 +0000918 },
919 {
920 .cmd = L2TP_CMD_TUNNEL_MODIFY,
Johannes Bergef6243a2019-04-26 14:07:31 +0200921 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
James Chapman309795f2010-04-02 06:19:10 +0000922 .doit = l2tp_nl_cmd_tunnel_modify,
Michael Weiß2abe0522020-04-07 13:11:48 +0200923 .flags = GENL_UNS_ADMIN_PERM,
James Chapman309795f2010-04-02 06:19:10 +0000924 },
925 {
926 .cmd = L2TP_CMD_TUNNEL_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +0200927 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
James Chapman309795f2010-04-02 06:19:10 +0000928 .doit = l2tp_nl_cmd_tunnel_get,
929 .dumpit = l2tp_nl_cmd_tunnel_dump,
Michael Weiß2abe0522020-04-07 13:11:48 +0200930 .flags = GENL_UNS_ADMIN_PERM,
James Chapman309795f2010-04-02 06:19:10 +0000931 },
932 {
933 .cmd = L2TP_CMD_SESSION_CREATE,
Johannes Bergef6243a2019-04-26 14:07:31 +0200934 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
James Chapman309795f2010-04-02 06:19:10 +0000935 .doit = l2tp_nl_cmd_session_create,
Michael Weiß2abe0522020-04-07 13:11:48 +0200936 .flags = GENL_UNS_ADMIN_PERM,
James Chapman309795f2010-04-02 06:19:10 +0000937 },
938 {
939 .cmd = L2TP_CMD_SESSION_DELETE,
Johannes Bergef6243a2019-04-26 14:07:31 +0200940 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
James Chapman309795f2010-04-02 06:19:10 +0000941 .doit = l2tp_nl_cmd_session_delete,
Michael Weiß2abe0522020-04-07 13:11:48 +0200942 .flags = GENL_UNS_ADMIN_PERM,
James Chapman309795f2010-04-02 06:19:10 +0000943 },
944 {
945 .cmd = L2TP_CMD_SESSION_MODIFY,
Johannes Bergef6243a2019-04-26 14:07:31 +0200946 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
James Chapman309795f2010-04-02 06:19:10 +0000947 .doit = l2tp_nl_cmd_session_modify,
Michael Weiß2abe0522020-04-07 13:11:48 +0200948 .flags = GENL_UNS_ADMIN_PERM,
James Chapman309795f2010-04-02 06:19:10 +0000949 },
950 {
951 .cmd = L2TP_CMD_SESSION_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +0200952 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
James Chapman309795f2010-04-02 06:19:10 +0000953 .doit = l2tp_nl_cmd_session_get,
954 .dumpit = l2tp_nl_cmd_session_dump,
Michael Weiß2abe0522020-04-07 13:11:48 +0200955 .flags = GENL_UNS_ADMIN_PERM,
James Chapman309795f2010-04-02 06:19:10 +0000956 },
957};
958
Johannes Berg56989f62016-10-24 14:40:05 +0200959static struct genl_family l2tp_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200960 .name = L2TP_GENL_NAME,
961 .version = L2TP_GENL_VERSION,
962 .hdrsize = 0,
963 .maxattr = L2TP_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +0100964 .policy = l2tp_nl_policy,
Johannes Berg489111e2016-10-24 14:40:03 +0200965 .netnsok = true,
966 .module = THIS_MODULE,
967 .ops = l2tp_nl_ops,
968 .n_ops = ARRAY_SIZE(l2tp_nl_ops),
969 .mcgrps = l2tp_multicast_group,
970 .n_mcgrps = ARRAY_SIZE(l2tp_multicast_group),
971};
972
James Chapman309795f2010-04-02 06:19:10 +0000973int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
974{
975 int ret;
976
977 ret = -EINVAL;
978 if (pw_type >= __L2TP_PWTYPE_MAX)
979 goto err;
980
981 genl_lock();
982 ret = -EBUSY;
983 if (l2tp_nl_cmd_ops[pw_type])
984 goto out;
985
986 l2tp_nl_cmd_ops[pw_type] = ops;
David S. Miller8cb49012011-04-17 17:01:05 -0700987 ret = 0;
James Chapman309795f2010-04-02 06:19:10 +0000988
989out:
990 genl_unlock();
991err:
David S. Miller8cb49012011-04-17 17:01:05 -0700992 return ret;
James Chapman309795f2010-04-02 06:19:10 +0000993}
994EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
995
996void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
997{
998 if (pw_type < __L2TP_PWTYPE_MAX) {
999 genl_lock();
1000 l2tp_nl_cmd_ops[pw_type] = NULL;
1001 genl_unlock();
1002 }
1003}
1004EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
1005
Johannes Berg56989f62016-10-24 14:40:05 +02001006static int __init l2tp_nl_init(void)
James Chapman309795f2010-04-02 06:19:10 +00001007{
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001008 pr_info("L2TP netlink interface\n");
Johannes Berg489111e2016-10-24 14:40:03 +02001009 return genl_register_family(&l2tp_nl_family);
James Chapman309795f2010-04-02 06:19:10 +00001010}
1011
1012static void l2tp_nl_cleanup(void)
1013{
1014 genl_unregister_family(&l2tp_nl_family);
1015}
1016
1017module_init(l2tp_nl_init);
1018module_exit(l2tp_nl_cleanup);
1019
1020MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1021MODULE_DESCRIPTION("L2TP netlink");
1022MODULE_LICENSE("GPL");
1023MODULE_VERSION("1.0");
Neil Hormane9412c32012-05-29 09:30:41 +00001024MODULE_ALIAS_GENL_FAMILY("l2tp");