blob: ae5170e26281669dbe35eac5f226aeb5cfffc2fd [file] [log] [blame]
James Chapman309795f2010-04-02 06:19:10 +00001/*
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 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
Joe Perchesa4ca44f2012-05-16 09:55:56 +000017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
James Chapman309795f2010-04-02 06:19:10 +000019#include <net/sock.h>
20#include <net/genetlink.h>
21#include <net/udp.h>
22#include <linux/in.h>
23#include <linux/udp.h>
24#include <linux/socket.h>
25#include <linux/module.h>
26#include <linux/list.h>
27#include <net/net_namespace.h>
28
29#include <linux/l2tp.h>
30
31#include "l2tp_core.h"
32
33
Johannes Berg489111e2016-10-24 14:40:03 +020034static struct genl_family l2tp_nl_family;
James Chapman309795f2010-04-02 06:19:10 +000035
Bill Hong33f72e62014-12-27 10:12:39 -080036static const struct genl_multicast_group l2tp_multicast_group[] = {
37 {
38 .name = L2TP_GENL_MCGROUP,
39 },
40};
41
42static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq,
43 int flags, struct l2tp_tunnel *tunnel, u8 cmd);
44static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq,
45 int flags, struct l2tp_session *session,
46 u8 cmd);
47
James Chapman309795f2010-04-02 06:19:10 +000048/* Accessed under genl lock */
49static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX];
50
Guillaume Nault2777e2a2017-03-31 13:02:30 +020051static struct l2tp_session *l2tp_nl_session_get(struct genl_info *info,
52 bool do_ref)
James Chapman309795f2010-04-02 06:19:10 +000053{
54 u32 tunnel_id;
55 u32 session_id;
56 char *ifname;
57 struct l2tp_tunnel *tunnel;
58 struct l2tp_session *session = NULL;
59 struct net *net = genl_info_net(info);
60
61 if (info->attrs[L2TP_ATTR_IFNAME]) {
62 ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
Guillaume Nault2777e2a2017-03-31 13:02:30 +020063 session = l2tp_session_get_by_ifname(net, ifname, do_ref);
James Chapman309795f2010-04-02 06:19:10 +000064 } else if ((info->attrs[L2TP_ATTR_SESSION_ID]) &&
65 (info->attrs[L2TP_ATTR_CONN_ID])) {
66 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
67 session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
Guillaume Nault54652eb2017-08-25 16:51:40 +020068 tunnel = l2tp_tunnel_get(net, tunnel_id);
69 if (tunnel) {
Guillaume Nault2777e2a2017-03-31 13:02:30 +020070 session = l2tp_session_get(net, tunnel, session_id,
71 do_ref);
Guillaume Nault54652eb2017-08-25 16:51:40 +020072 l2tp_tunnel_dec_refcount(tunnel);
73 }
James Chapman309795f2010-04-02 06:19:10 +000074 }
75
76 return session;
77}
78
79static int l2tp_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
80{
81 struct sk_buff *msg;
82 void *hdr;
83 int ret = -ENOBUFS;
84
Thomas Graf58050fc2012-06-28 03:57:45 +000085 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
James Chapman309795f2010-04-02 06:19:10 +000086 if (!msg) {
87 ret = -ENOMEM;
88 goto out;
89 }
90
Eric W. Biederman15e47302012-09-07 20:12:54 +000091 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
James Chapman309795f2010-04-02 06:19:10 +000092 &l2tp_nl_family, 0, L2TP_CMD_NOOP);
Wei Yongjun7f8436a2012-09-24 18:29:01 +000093 if (!hdr) {
94 ret = -EMSGSIZE;
James Chapman309795f2010-04-02 06:19:10 +000095 goto err_out;
96 }
97
98 genlmsg_end(msg, hdr);
99
Eric W. Biederman15e47302012-09-07 20:12:54 +0000100 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
James Chapman309795f2010-04-02 06:19:10 +0000101
102err_out:
103 nlmsg_free(msg);
104
105out:
106 return ret;
107}
108
Bill Hong33f72e62014-12-27 10:12:39 -0800109static int l2tp_tunnel_notify(struct genl_family *family,
110 struct genl_info *info,
111 struct l2tp_tunnel *tunnel,
112 u8 cmd)
113{
114 struct sk_buff *msg;
115 int ret;
116
117 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
118 if (!msg)
119 return -ENOMEM;
120
121 ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
122 NLM_F_ACK, tunnel, cmd);
123
Mark Tomlinson853effc2016-02-15 16:24:44 +1300124 if (ret >= 0) {
125 ret = genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC);
126 /* We don't care if no one is listening */
127 if (ret == -ESRCH)
128 ret = 0;
129 return ret;
130 }
Bill Hong33f72e62014-12-27 10:12:39 -0800131
132 nlmsg_free(msg);
133
134 return ret;
135}
136
137static int l2tp_session_notify(struct genl_family *family,
138 struct genl_info *info,
139 struct l2tp_session *session,
140 u8 cmd)
141{
142 struct sk_buff *msg;
143 int ret;
144
145 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
146 if (!msg)
147 return -ENOMEM;
148
149 ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
150 NLM_F_ACK, session, cmd);
151
Mark Tomlinson853effc2016-02-15 16:24:44 +1300152 if (ret >= 0) {
153 ret = genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC);
154 /* We don't care if no one is listening */
155 if (ret == -ESRCH)
156 ret = 0;
157 return ret;
158 }
Bill Hong33f72e62014-12-27 10:12:39 -0800159
160 nlmsg_free(msg);
161
162 return ret;
163}
164
James Chapman309795f2010-04-02 06:19:10 +0000165static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info)
166{
167 u32 tunnel_id;
168 u32 peer_tunnel_id;
169 int proto_version;
170 int fd;
171 int ret = 0;
172 struct l2tp_tunnel_cfg cfg = { 0, };
173 struct l2tp_tunnel *tunnel;
174 struct net *net = genl_info_net(info);
175
176 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
177 ret = -EINVAL;
178 goto out;
179 }
180 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
181
182 if (!info->attrs[L2TP_ATTR_PEER_CONN_ID]) {
183 ret = -EINVAL;
184 goto out;
185 }
186 peer_tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_CONN_ID]);
187
188 if (!info->attrs[L2TP_ATTR_PROTO_VERSION]) {
189 ret = -EINVAL;
190 goto out;
191 }
192 proto_version = nla_get_u8(info->attrs[L2TP_ATTR_PROTO_VERSION]);
193
194 if (!info->attrs[L2TP_ATTR_ENCAP_TYPE]) {
195 ret = -EINVAL;
196 goto out;
197 }
198 cfg.encap = nla_get_u16(info->attrs[L2TP_ATTR_ENCAP_TYPE]);
199
James Chapman789a4a22010-04-02 06:19:40 +0000200 fd = -1;
201 if (info->attrs[L2TP_ATTR_FD]) {
202 fd = nla_get_u32(info->attrs[L2TP_ATTR_FD]);
203 } else {
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000204#if IS_ENABLED(CONFIG_IPV6)
205 if (info->attrs[L2TP_ATTR_IP6_SADDR] &&
206 info->attrs[L2TP_ATTR_IP6_DADDR]) {
207 cfg.local_ip6 = nla_data(
208 info->attrs[L2TP_ATTR_IP6_SADDR]);
209 cfg.peer_ip6 = nla_data(
210 info->attrs[L2TP_ATTR_IP6_DADDR]);
211 } else
212#endif
213 if (info->attrs[L2TP_ATTR_IP_SADDR] &&
214 info->attrs[L2TP_ATTR_IP_DADDR]) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200215 cfg.local_ip.s_addr = nla_get_in_addr(
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000216 info->attrs[L2TP_ATTR_IP_SADDR]);
Jiri Benc67b61f62015-03-29 16:59:26 +0200217 cfg.peer_ip.s_addr = nla_get_in_addr(
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000218 info->attrs[L2TP_ATTR_IP_DADDR]);
219 } else {
220 ret = -EINVAL;
221 goto out;
222 }
James Chapman789a4a22010-04-02 06:19:40 +0000223 if (info->attrs[L2TP_ATTR_UDP_SPORT])
224 cfg.local_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_SPORT]);
225 if (info->attrs[L2TP_ATTR_UDP_DPORT])
226 cfg.peer_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_DPORT]);
Asbjørn Sloth Tønnesen57ceb862016-11-07 20:39:27 +0000227 cfg.use_udp_checksums = nla_get_flag(
228 info->attrs[L2TP_ATTR_UDP_CSUM]);
Tom Herbert6b649fea2014-05-23 08:47:40 -0700229
230#if IS_ENABLED(CONFIG_IPV6)
Asbjørn Sloth Tønnesen57ceb862016-11-07 20:39:27 +0000231 cfg.udp6_zero_tx_checksums = nla_get_flag(
232 info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_TX]);
233 cfg.udp6_zero_rx_checksums = nla_get_flag(
234 info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_RX]);
Tom Herbert6b649fea2014-05-23 08:47:40 -0700235#endif
James Chapman309795f2010-04-02 06:19:10 +0000236 }
James Chapman309795f2010-04-02 06:19:10 +0000237
238 if (info->attrs[L2TP_ATTR_DEBUG])
239 cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
240
241 tunnel = l2tp_tunnel_find(net, tunnel_id);
242 if (tunnel != NULL) {
243 ret = -EEXIST;
244 goto out;
245 }
246
247 ret = -EINVAL;
248 switch (cfg.encap) {
249 case L2TP_ENCAPTYPE_UDP:
250 case L2TP_ENCAPTYPE_IP:
251 ret = l2tp_tunnel_create(net, fd, proto_version, tunnel_id,
252 peer_tunnel_id, &cfg, &tunnel);
253 break;
254 }
255
Bill Hong33f72e62014-12-27 10:12:39 -0800256 if (ret >= 0)
257 ret = l2tp_tunnel_notify(&l2tp_nl_family, info,
258 tunnel, L2TP_CMD_TUNNEL_CREATE);
James Chapman309795f2010-04-02 06:19:10 +0000259out:
260 return ret;
261}
262
263static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info)
264{
265 struct l2tp_tunnel *tunnel;
266 u32 tunnel_id;
267 int ret = 0;
268 struct net *net = genl_info_net(info);
269
270 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
271 ret = -EINVAL;
272 goto out;
273 }
274 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
275
Guillaume Naultbb0a32c2017-08-25 16:51:42 +0200276 tunnel = l2tp_tunnel_get(net, tunnel_id);
277 if (!tunnel) {
James Chapman309795f2010-04-02 06:19:10 +0000278 ret = -ENODEV;
279 goto out;
280 }
281
Bill Hong33f72e62014-12-27 10:12:39 -0800282 l2tp_tunnel_notify(&l2tp_nl_family, info,
283 tunnel, L2TP_CMD_TUNNEL_DELETE);
284
James Chapman309795f2010-04-02 06:19:10 +0000285 (void) l2tp_tunnel_delete(tunnel);
286
Guillaume Naultbb0a32c2017-08-25 16:51:42 +0200287 l2tp_tunnel_dec_refcount(tunnel);
288
James Chapman309795f2010-04-02 06:19:10 +0000289out:
290 return ret;
291}
292
293static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info)
294{
295 struct l2tp_tunnel *tunnel;
296 u32 tunnel_id;
297 int ret = 0;
298 struct net *net = genl_info_net(info);
299
300 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
301 ret = -EINVAL;
302 goto out;
303 }
304 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
305
Guillaume Nault8c0e4212017-08-25 16:51:42 +0200306 tunnel = l2tp_tunnel_get(net, tunnel_id);
307 if (!tunnel) {
James Chapman309795f2010-04-02 06:19:10 +0000308 ret = -ENODEV;
309 goto out;
310 }
311
312 if (info->attrs[L2TP_ATTR_DEBUG])
313 tunnel->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
314
Bill Hong33f72e62014-12-27 10:12:39 -0800315 ret = l2tp_tunnel_notify(&l2tp_nl_family, info,
316 tunnel, L2TP_CMD_TUNNEL_MODIFY);
317
Guillaume Nault8c0e4212017-08-25 16:51:42 +0200318 l2tp_tunnel_dec_refcount(tunnel);
319
James Chapman309795f2010-04-02 06:19:10 +0000320out:
321 return ret;
322}
323
Eric W. Biederman15e47302012-09-07 20:12:54 +0000324static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
Bill Hong33f72e62014-12-27 10:12:39 -0800325 struct l2tp_tunnel *tunnel, u8 cmd)
James Chapman309795f2010-04-02 06:19:10 +0000326{
327 void *hdr;
328 struct nlattr *nest;
329 struct sock *sk = NULL;
330 struct inet_sock *inet;
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000331#if IS_ENABLED(CONFIG_IPV6)
332 struct ipv6_pinfo *np = NULL;
333#endif
James Chapman309795f2010-04-02 06:19:10 +0000334
Bill Hong33f72e62014-12-27 10:12:39 -0800335 hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
Wei Yongjun7f8436a2012-09-24 18:29:01 +0000336 if (!hdr)
337 return -EMSGSIZE;
James Chapman309795f2010-04-02 06:19:10 +0000338
David S. Miller60aed2a2012-04-01 19:59:31 -0400339 if (nla_put_u8(skb, L2TP_ATTR_PROTO_VERSION, tunnel->version) ||
340 nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
341 nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
342 nla_put_u32(skb, L2TP_ATTR_DEBUG, tunnel->debug) ||
343 nla_put_u16(skb, L2TP_ATTR_ENCAP_TYPE, tunnel->encap))
344 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000345
346 nest = nla_nest_start(skb, L2TP_ATTR_STATS);
347 if (nest == NULL)
348 goto nla_put_failure;
349
Nicolas Dichtel1c714a92016-04-25 10:25:19 +0200350 if (nla_put_u64_64bit(skb, L2TP_ATTR_TX_PACKETS,
351 atomic_long_read(&tunnel->stats.tx_packets),
352 L2TP_ATTR_STATS_PAD) ||
353 nla_put_u64_64bit(skb, L2TP_ATTR_TX_BYTES,
354 atomic_long_read(&tunnel->stats.tx_bytes),
355 L2TP_ATTR_STATS_PAD) ||
356 nla_put_u64_64bit(skb, L2TP_ATTR_TX_ERRORS,
357 atomic_long_read(&tunnel->stats.tx_errors),
358 L2TP_ATTR_STATS_PAD) ||
359 nla_put_u64_64bit(skb, L2TP_ATTR_RX_PACKETS,
360 atomic_long_read(&tunnel->stats.rx_packets),
361 L2TP_ATTR_STATS_PAD) ||
362 nla_put_u64_64bit(skb, L2TP_ATTR_RX_BYTES,
363 atomic_long_read(&tunnel->stats.rx_bytes),
364 L2TP_ATTR_STATS_PAD) ||
365 nla_put_u64_64bit(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
366 atomic_long_read(&tunnel->stats.rx_seq_discards),
367 L2TP_ATTR_STATS_PAD) ||
368 nla_put_u64_64bit(skb, L2TP_ATTR_RX_OOS_PACKETS,
369 atomic_long_read(&tunnel->stats.rx_oos_packets),
370 L2TP_ATTR_STATS_PAD) ||
371 nla_put_u64_64bit(skb, L2TP_ATTR_RX_ERRORS,
372 atomic_long_read(&tunnel->stats.rx_errors),
373 L2TP_ATTR_STATS_PAD))
David S. Miller60aed2a2012-04-01 19:59:31 -0400374 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000375 nla_nest_end(skb, nest);
376
377 sk = tunnel->sock;
378 if (!sk)
379 goto out;
380
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000381#if IS_ENABLED(CONFIG_IPV6)
382 if (sk->sk_family == AF_INET6)
383 np = inet6_sk(sk);
384#endif
385
James Chapman309795f2010-04-02 06:19:10 +0000386 inet = inet_sk(sk);
387
388 switch (tunnel->encap) {
389 case L2TP_ENCAPTYPE_UDP:
Asbjørn Sloth Tønnesen7ff516f2016-11-07 20:39:25 +0000390 switch (sk->sk_family) {
391 case AF_INET:
392 if (nla_put_u8(skb, L2TP_ATTR_UDP_CSUM, !sk->sk_no_check_tx))
393 goto nla_put_failure;
394 break;
Asbjørn Sloth Tønnesen97b7af02016-11-07 20:39:26 +0000395#if IS_ENABLED(CONFIG_IPV6)
396 case AF_INET6:
397 if (udp_get_no_check6_tx(sk) &&
398 nla_put_flag(skb, L2TP_ATTR_UDP_ZERO_CSUM6_TX))
399 goto nla_put_failure;
400 if (udp_get_no_check6_rx(sk) &&
401 nla_put_flag(skb, L2TP_ATTR_UDP_ZERO_CSUM6_RX))
402 goto nla_put_failure;
403 break;
404#endif
Asbjørn Sloth Tønnesen7ff516f2016-11-07 20:39:25 +0000405 }
David S. Miller60aed2a2012-04-01 19:59:31 -0400406 if (nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
Asbjørn Sloth Tønnesen7ff516f2016-11-07 20:39:25 +0000407 nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)))
David S. Miller60aed2a2012-04-01 19:59:31 -0400408 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000409 /* NOBREAK */
410 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000411#if IS_ENABLED(CONFIG_IPV6)
412 if (np) {
Jiri Benc930345e2015-03-29 16:59:25 +0200413 if (nla_put_in6_addr(skb, L2TP_ATTR_IP6_SADDR,
414 &np->saddr) ||
415 nla_put_in6_addr(skb, L2TP_ATTR_IP6_DADDR,
416 &sk->sk_v6_daddr))
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000417 goto nla_put_failure;
418 } else
419#endif
Jiri Benc930345e2015-03-29 16:59:25 +0200420 if (nla_put_in_addr(skb, L2TP_ATTR_IP_SADDR,
421 inet->inet_saddr) ||
422 nla_put_in_addr(skb, L2TP_ATTR_IP_DADDR,
423 inet->inet_daddr))
David S. Miller60aed2a2012-04-01 19:59:31 -0400424 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000425 break;
426 }
427
428out:
Johannes Berg053c0952015-01-16 22:09:00 +0100429 genlmsg_end(skb, hdr);
430 return 0;
James Chapman309795f2010-04-02 06:19:10 +0000431
432nla_put_failure:
433 genlmsg_cancel(skb, hdr);
434 return -1;
435}
436
437static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
438{
439 struct l2tp_tunnel *tunnel;
440 struct sk_buff *msg;
441 u32 tunnel_id;
442 int ret = -ENOBUFS;
443 struct net *net = genl_info_net(info);
444
445 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
446 ret = -EINVAL;
Guillaume Nault4e4b21d2017-08-25 16:51:43 +0200447 goto err;
James Chapman309795f2010-04-02 06:19:10 +0000448 }
449
450 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
451
Thomas Graf58050fc2012-06-28 03:57:45 +0000452 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
James Chapman309795f2010-04-02 06:19:10 +0000453 if (!msg) {
454 ret = -ENOMEM;
Guillaume Nault4e4b21d2017-08-25 16:51:43 +0200455 goto err;
456 }
457
458 tunnel = l2tp_tunnel_get(net, tunnel_id);
459 if (!tunnel) {
460 ret = -ENODEV;
461 goto err_nlmsg;
James Chapman309795f2010-04-02 06:19:10 +0000462 }
463
Eric W. Biederman15e47302012-09-07 20:12:54 +0000464 ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
Bill Hong33f72e62014-12-27 10:12:39 -0800465 NLM_F_ACK, tunnel, L2TP_CMD_TUNNEL_GET);
James Chapman309795f2010-04-02 06:19:10 +0000466 if (ret < 0)
Guillaume Nault4e4b21d2017-08-25 16:51:43 +0200467 goto err_nlmsg_tunnel;
468
469 l2tp_tunnel_dec_refcount(tunnel);
James Chapman309795f2010-04-02 06:19:10 +0000470
Eric W. Biederman15e47302012-09-07 20:12:54 +0000471 return genlmsg_unicast(net, msg, info->snd_portid);
James Chapman309795f2010-04-02 06:19:10 +0000472
Guillaume Nault4e4b21d2017-08-25 16:51:43 +0200473err_nlmsg_tunnel:
474 l2tp_tunnel_dec_refcount(tunnel);
475err_nlmsg:
James Chapman309795f2010-04-02 06:19:10 +0000476 nlmsg_free(msg);
Guillaume Nault4e4b21d2017-08-25 16:51:43 +0200477err:
James Chapman309795f2010-04-02 06:19:10 +0000478 return ret;
479}
480
481static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
482{
483 int ti = cb->args[0];
484 struct l2tp_tunnel *tunnel;
485 struct net *net = sock_net(skb->sk);
486
487 for (;;) {
488 tunnel = l2tp_tunnel_find_nth(net, ti);
489 if (tunnel == NULL)
490 goto out;
491
Eric W. Biederman15e47302012-09-07 20:12:54 +0000492 if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).portid,
James Chapman309795f2010-04-02 06:19:10 +0000493 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg053c0952015-01-16 22:09:00 +0100494 tunnel, L2TP_CMD_TUNNEL_GET) < 0)
James Chapman309795f2010-04-02 06:19:10 +0000495 goto out;
496
497 ti++;
498 }
499
500out:
501 cb->args[0] = ti;
502
503 return skb->len;
504}
505
506static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
507{
508 u32 tunnel_id = 0;
509 u32 session_id;
510 u32 peer_session_id;
511 int ret = 0;
512 struct l2tp_tunnel *tunnel;
513 struct l2tp_session *session;
514 struct l2tp_session_cfg cfg = { 0, };
515 struct net *net = genl_info_net(info);
516
517 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
518 ret = -EINVAL;
519 goto out;
520 }
521 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
522 tunnel = l2tp_tunnel_find(net, tunnel_id);
523 if (!tunnel) {
524 ret = -ENODEV;
525 goto out;
526 }
527
528 if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
529 ret = -EINVAL;
530 goto out;
531 }
532 session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
James Chapman309795f2010-04-02 06:19:10 +0000533
534 if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
535 ret = -EINVAL;
536 goto out;
537 }
538 peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
539
540 if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
541 ret = -EINVAL;
542 goto out;
543 }
544 cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
545 if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
546 ret = -EINVAL;
547 goto out;
548 }
549
550 if (tunnel->version > 2) {
551 if (info->attrs[L2TP_ATTR_OFFSET])
552 cfg.offset = nla_get_u16(info->attrs[L2TP_ATTR_OFFSET]);
553
554 if (info->attrs[L2TP_ATTR_DATA_SEQ])
555 cfg.data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
556
557 cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
558 if (info->attrs[L2TP_ATTR_L2SPEC_TYPE])
559 cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
560
561 cfg.l2specific_len = 4;
562 if (info->attrs[L2TP_ATTR_L2SPEC_LEN])
563 cfg.l2specific_len = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_LEN]);
564
565 if (info->attrs[L2TP_ATTR_COOKIE]) {
566 u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
567 if (len > 8) {
568 ret = -EINVAL;
569 goto out;
570 }
571 cfg.cookie_len = len;
572 memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
573 }
574 if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
575 u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
576 if (len > 8) {
577 ret = -EINVAL;
578 goto out;
579 }
580 cfg.peer_cookie_len = len;
581 memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
582 }
583 if (info->attrs[L2TP_ATTR_IFNAME])
584 cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
585
586 if (info->attrs[L2TP_ATTR_VLAN_ID])
587 cfg.vlan_id = nla_get_u16(info->attrs[L2TP_ATTR_VLAN_ID]);
588 }
589
590 if (info->attrs[L2TP_ATTR_DEBUG])
591 cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
592
593 if (info->attrs[L2TP_ATTR_RECV_SEQ])
594 cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
595
596 if (info->attrs[L2TP_ATTR_SEND_SEQ])
597 cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
598
599 if (info->attrs[L2TP_ATTR_LNS_MODE])
600 cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
601
602 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
603 cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
604
605 if (info->attrs[L2TP_ATTR_MTU])
606 cfg.mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
607
608 if (info->attrs[L2TP_ATTR_MRU])
609 cfg.mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
610
stephen hemmingerf1f39f92015-09-23 21:33:34 -0700611#ifdef CONFIG_MODULES
612 if (l2tp_nl_cmd_ops[cfg.pw_type] == NULL) {
613 genl_unlock();
614 request_module("net-l2tp-type-%u", cfg.pw_type);
615 genl_lock();
616 }
617#endif
James Chapman309795f2010-04-02 06:19:10 +0000618 if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) ||
619 (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) {
620 ret = -EPROTONOSUPPORT;
621 goto out;
622 }
623
624 /* Check that pseudowire-specific params are present */
625 switch (cfg.pw_type) {
626 case L2TP_PWTYPE_NONE:
627 break;
628 case L2TP_PWTYPE_ETH_VLAN:
629 if (!info->attrs[L2TP_ATTR_VLAN_ID]) {
630 ret = -EINVAL;
631 goto out;
632 }
633 break;
634 case L2TP_PWTYPE_ETH:
635 break;
636 case L2TP_PWTYPE_PPP:
637 case L2TP_PWTYPE_PPP_AC:
638 break;
639 case L2TP_PWTYPE_IP:
640 default:
641 ret = -EPROTONOSUPPORT;
642 break;
643 }
644
645 ret = -EPROTONOSUPPORT;
646 if (l2tp_nl_cmd_ops[cfg.pw_type]->session_create)
647 ret = (*l2tp_nl_cmd_ops[cfg.pw_type]->session_create)(net, tunnel_id,
648 session_id, peer_session_id, &cfg);
649
Bill Hong33f72e62014-12-27 10:12:39 -0800650 if (ret >= 0) {
Guillaume Nault5e6a9e52017-03-31 13:02:29 +0200651 session = l2tp_session_get(net, tunnel, session_id, false);
652 if (session) {
Bill Hong33f72e62014-12-27 10:12:39 -0800653 ret = l2tp_session_notify(&l2tp_nl_family, info, session,
654 L2TP_CMD_SESSION_CREATE);
Guillaume Nault5e6a9e52017-03-31 13:02:29 +0200655 l2tp_session_dec_refcount(session);
656 }
Bill Hong33f72e62014-12-27 10:12:39 -0800657 }
658
James Chapman309795f2010-04-02 06:19:10 +0000659out:
660 return ret;
661}
662
663static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
664{
665 int ret = 0;
666 struct l2tp_session *session;
667 u16 pw_type;
668
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200669 session = l2tp_nl_session_get(info, true);
James Chapman309795f2010-04-02 06:19:10 +0000670 if (session == NULL) {
671 ret = -ENODEV;
672 goto out;
673 }
674
Bill Hong33f72e62014-12-27 10:12:39 -0800675 l2tp_session_notify(&l2tp_nl_family, info,
676 session, L2TP_CMD_SESSION_DELETE);
677
James Chapman309795f2010-04-02 06:19:10 +0000678 pw_type = session->pwtype;
679 if (pw_type < __L2TP_PWTYPE_MAX)
680 if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
681 ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
682
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200683 if (session->deref)
684 session->deref(session);
685 l2tp_session_dec_refcount(session);
686
James Chapman309795f2010-04-02 06:19:10 +0000687out:
688 return ret;
689}
690
691static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
692{
693 int ret = 0;
694 struct l2tp_session *session;
695
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200696 session = l2tp_nl_session_get(info, false);
James Chapman309795f2010-04-02 06:19:10 +0000697 if (session == NULL) {
698 ret = -ENODEV;
699 goto out;
700 }
701
702 if (info->attrs[L2TP_ATTR_DEBUG])
703 session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
704
705 if (info->attrs[L2TP_ATTR_DATA_SEQ])
706 session->data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
707
708 if (info->attrs[L2TP_ATTR_RECV_SEQ])
709 session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
710
Guillaume Naultbb5016e2014-03-06 11:14:30 +0100711 if (info->attrs[L2TP_ATTR_SEND_SEQ]) {
James Chapman309795f2010-04-02 06:19:10 +0000712 session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
Guillaume Naultbb5016e2014-03-06 11:14:30 +0100713 l2tp_session_set_header_len(session, session->tunnel->version);
714 }
James Chapman309795f2010-04-02 06:19:10 +0000715
716 if (info->attrs[L2TP_ATTR_LNS_MODE])
717 session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
718
719 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
720 session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
721
722 if (info->attrs[L2TP_ATTR_MTU])
723 session->mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
724
725 if (info->attrs[L2TP_ATTR_MRU])
726 session->mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
727
Bill Hong33f72e62014-12-27 10:12:39 -0800728 ret = l2tp_session_notify(&l2tp_nl_family, info,
729 session, L2TP_CMD_SESSION_MODIFY);
730
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200731 l2tp_session_dec_refcount(session);
732
James Chapman309795f2010-04-02 06:19:10 +0000733out:
734 return ret;
735}
736
Eric W. Biederman15e47302012-09-07 20:12:54 +0000737static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
Bill Hong33f72e62014-12-27 10:12:39 -0800738 struct l2tp_session *session, u8 cmd)
James Chapman309795f2010-04-02 06:19:10 +0000739{
740 void *hdr;
741 struct nlattr *nest;
742 struct l2tp_tunnel *tunnel = session->tunnel;
743 struct sock *sk = NULL;
744
745 sk = tunnel->sock;
746
Bill Hong33f72e62014-12-27 10:12:39 -0800747 hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
Wei Yongjun7f8436a2012-09-24 18:29:01 +0000748 if (!hdr)
749 return -EMSGSIZE;
James Chapman309795f2010-04-02 06:19:10 +0000750
David S. Miller60aed2a2012-04-01 19:59:31 -0400751 if (nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
752 nla_put_u32(skb, L2TP_ATTR_SESSION_ID, session->session_id) ||
753 nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
754 nla_put_u32(skb, L2TP_ATTR_PEER_SESSION_ID,
755 session->peer_session_id) ||
756 nla_put_u32(skb, L2TP_ATTR_DEBUG, session->debug) ||
757 nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype) ||
758 nla_put_u16(skb, L2TP_ATTR_MTU, session->mtu) ||
759 (session->mru &&
760 nla_put_u16(skb, L2TP_ATTR_MRU, session->mru)))
761 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000762
Alan Coxe269ed22012-10-25 05:22:01 +0000763 if ((session->ifname[0] &&
David S. Miller60aed2a2012-04-01 19:59:31 -0400764 nla_put_string(skb, L2TP_ATTR_IFNAME, session->ifname)) ||
765 (session->cookie_len &&
766 nla_put(skb, L2TP_ATTR_COOKIE, session->cookie_len,
767 &session->cookie[0])) ||
768 (session->peer_cookie_len &&
769 nla_put(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len,
770 &session->peer_cookie[0])) ||
771 nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
772 nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
773 nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
James Chapman309795f2010-04-02 06:19:10 +0000774#ifdef CONFIG_XFRM
David S. Miller60aed2a2012-04-01 19:59:31 -0400775 (((sk) && (sk->sk_policy[0] || sk->sk_policy[1])) &&
776 nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
James Chapman309795f2010-04-02 06:19:10 +0000777#endif
David S. Miller60aed2a2012-04-01 19:59:31 -0400778 (session->reorder_timeout &&
Nicolas Dichtel2175d872016-04-22 17:31:21 +0200779 nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT,
780 session->reorder_timeout, L2TP_ATTR_PAD)))
David S. Miller60aed2a2012-04-01 19:59:31 -0400781 goto nla_put_failure;
James Chapman5de7aee2012-04-29 21:48:46 +0000782
James Chapman309795f2010-04-02 06:19:10 +0000783 nest = nla_nest_start(skb, L2TP_ATTR_STATS);
784 if (nest == NULL)
785 goto nla_put_failure;
James Chapman5de7aee2012-04-29 21:48:46 +0000786
Nicolas Dichtel1c714a92016-04-25 10:25:19 +0200787 if (nla_put_u64_64bit(skb, L2TP_ATTR_TX_PACKETS,
788 atomic_long_read(&session->stats.tx_packets),
789 L2TP_ATTR_STATS_PAD) ||
790 nla_put_u64_64bit(skb, L2TP_ATTR_TX_BYTES,
791 atomic_long_read(&session->stats.tx_bytes),
792 L2TP_ATTR_STATS_PAD) ||
793 nla_put_u64_64bit(skb, L2TP_ATTR_TX_ERRORS,
794 atomic_long_read(&session->stats.tx_errors),
795 L2TP_ATTR_STATS_PAD) ||
796 nla_put_u64_64bit(skb, L2TP_ATTR_RX_PACKETS,
797 atomic_long_read(&session->stats.rx_packets),
798 L2TP_ATTR_STATS_PAD) ||
799 nla_put_u64_64bit(skb, L2TP_ATTR_RX_BYTES,
800 atomic_long_read(&session->stats.rx_bytes),
801 L2TP_ATTR_STATS_PAD) ||
802 nla_put_u64_64bit(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
803 atomic_long_read(&session->stats.rx_seq_discards),
804 L2TP_ATTR_STATS_PAD) ||
805 nla_put_u64_64bit(skb, L2TP_ATTR_RX_OOS_PACKETS,
806 atomic_long_read(&session->stats.rx_oos_packets),
807 L2TP_ATTR_STATS_PAD) ||
808 nla_put_u64_64bit(skb, L2TP_ATTR_RX_ERRORS,
809 atomic_long_read(&session->stats.rx_errors),
810 L2TP_ATTR_STATS_PAD))
David S. Miller60aed2a2012-04-01 19:59:31 -0400811 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000812 nla_nest_end(skb, nest);
813
Johannes Berg053c0952015-01-16 22:09:00 +0100814 genlmsg_end(skb, hdr);
815 return 0;
James Chapman309795f2010-04-02 06:19:10 +0000816
817 nla_put_failure:
818 genlmsg_cancel(skb, hdr);
819 return -1;
820}
821
822static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
823{
824 struct l2tp_session *session;
825 struct sk_buff *msg;
826 int ret;
827
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200828 session = l2tp_nl_session_get(info, false);
James Chapman309795f2010-04-02 06:19:10 +0000829 if (session == NULL) {
830 ret = -ENODEV;
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200831 goto err;
James Chapman309795f2010-04-02 06:19:10 +0000832 }
833
Thomas Graf58050fc2012-06-28 03:57:45 +0000834 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
James Chapman309795f2010-04-02 06:19:10 +0000835 if (!msg) {
836 ret = -ENOMEM;
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200837 goto err_ref;
James Chapman309795f2010-04-02 06:19:10 +0000838 }
839
Eric W. Biederman15e47302012-09-07 20:12:54 +0000840 ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
Bill Hong33f72e62014-12-27 10:12:39 -0800841 0, session, L2TP_CMD_SESSION_GET);
James Chapman309795f2010-04-02 06:19:10 +0000842 if (ret < 0)
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200843 goto err_ref_msg;
James Chapman309795f2010-04-02 06:19:10 +0000844
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200845 ret = genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
James Chapman309795f2010-04-02 06:19:10 +0000846
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200847 l2tp_session_dec_refcount(session);
848
849 return ret;
850
851err_ref_msg:
James Chapman309795f2010-04-02 06:19:10 +0000852 nlmsg_free(msg);
Guillaume Nault2777e2a2017-03-31 13:02:30 +0200853err_ref:
854 l2tp_session_dec_refcount(session);
855err:
James Chapman309795f2010-04-02 06:19:10 +0000856 return ret;
857}
858
859static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
860{
861 struct net *net = sock_net(skb->sk);
862 struct l2tp_session *session;
863 struct l2tp_tunnel *tunnel = NULL;
864 int ti = cb->args[0];
865 int si = cb->args[1];
866
867 for (;;) {
868 if (tunnel == NULL) {
869 tunnel = l2tp_tunnel_find_nth(net, ti);
870 if (tunnel == NULL)
871 goto out;
872 }
873
Guillaume Naulte08293a2017-04-03 12:03:13 +0200874 session = l2tp_session_get_nth(tunnel, si, false);
James Chapman309795f2010-04-02 06:19:10 +0000875 if (session == NULL) {
876 ti++;
877 tunnel = NULL;
878 si = 0;
879 continue;
880 }
881
Eric W. Biederman15e47302012-09-07 20:12:54 +0000882 if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).portid,
James Chapman309795f2010-04-02 06:19:10 +0000883 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Guillaume Naulte08293a2017-04-03 12:03:13 +0200884 session, L2TP_CMD_SESSION_GET) < 0) {
885 l2tp_session_dec_refcount(session);
James Chapman309795f2010-04-02 06:19:10 +0000886 break;
Guillaume Naulte08293a2017-04-03 12:03:13 +0200887 }
888 l2tp_session_dec_refcount(session);
James Chapman309795f2010-04-02 06:19:10 +0000889
890 si++;
891 }
892
893out:
894 cb->args[0] = ti;
895 cb->args[1] = si;
896
897 return skb->len;
898}
899
stephen hemmingerf5bb3412016-08-31 23:24:41 -0700900static const struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
James Chapman309795f2010-04-02 06:19:10 +0000901 [L2TP_ATTR_NONE] = { .type = NLA_UNSPEC, },
902 [L2TP_ATTR_PW_TYPE] = { .type = NLA_U16, },
903 [L2TP_ATTR_ENCAP_TYPE] = { .type = NLA_U16, },
904 [L2TP_ATTR_OFFSET] = { .type = NLA_U16, },
905 [L2TP_ATTR_DATA_SEQ] = { .type = NLA_U8, },
906 [L2TP_ATTR_L2SPEC_TYPE] = { .type = NLA_U8, },
907 [L2TP_ATTR_L2SPEC_LEN] = { .type = NLA_U8, },
908 [L2TP_ATTR_PROTO_VERSION] = { .type = NLA_U8, },
909 [L2TP_ATTR_CONN_ID] = { .type = NLA_U32, },
910 [L2TP_ATTR_PEER_CONN_ID] = { .type = NLA_U32, },
911 [L2TP_ATTR_SESSION_ID] = { .type = NLA_U32, },
912 [L2TP_ATTR_PEER_SESSION_ID] = { .type = NLA_U32, },
913 [L2TP_ATTR_UDP_CSUM] = { .type = NLA_U8, },
914 [L2TP_ATTR_VLAN_ID] = { .type = NLA_U16, },
915 [L2TP_ATTR_DEBUG] = { .type = NLA_U32, },
916 [L2TP_ATTR_RECV_SEQ] = { .type = NLA_U8, },
917 [L2TP_ATTR_SEND_SEQ] = { .type = NLA_U8, },
918 [L2TP_ATTR_LNS_MODE] = { .type = NLA_U8, },
919 [L2TP_ATTR_USING_IPSEC] = { .type = NLA_U8, },
920 [L2TP_ATTR_RECV_TIMEOUT] = { .type = NLA_MSECS, },
921 [L2TP_ATTR_FD] = { .type = NLA_U32, },
922 [L2TP_ATTR_IP_SADDR] = { .type = NLA_U32, },
923 [L2TP_ATTR_IP_DADDR] = { .type = NLA_U32, },
924 [L2TP_ATTR_UDP_SPORT] = { .type = NLA_U16, },
925 [L2TP_ATTR_UDP_DPORT] = { .type = NLA_U16, },
926 [L2TP_ATTR_MTU] = { .type = NLA_U16, },
927 [L2TP_ATTR_MRU] = { .type = NLA_U16, },
928 [L2TP_ATTR_STATS] = { .type = NLA_NESTED, },
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000929 [L2TP_ATTR_IP6_SADDR] = {
930 .type = NLA_BINARY,
931 .len = sizeof(struct in6_addr),
932 },
933 [L2TP_ATTR_IP6_DADDR] = {
934 .type = NLA_BINARY,
935 .len = sizeof(struct in6_addr),
936 },
James Chapman309795f2010-04-02 06:19:10 +0000937 [L2TP_ATTR_IFNAME] = {
938 .type = NLA_NUL_STRING,
939 .len = IFNAMSIZ - 1,
940 },
941 [L2TP_ATTR_COOKIE] = {
942 .type = NLA_BINARY,
943 .len = 8,
944 },
945 [L2TP_ATTR_PEER_COOKIE] = {
946 .type = NLA_BINARY,
947 .len = 8,
948 },
949};
950
Johannes Berg4534de82013-11-14 17:14:46 +0100951static const struct genl_ops l2tp_nl_ops[] = {
James Chapman309795f2010-04-02 06:19:10 +0000952 {
953 .cmd = L2TP_CMD_NOOP,
954 .doit = l2tp_nl_cmd_noop,
955 .policy = l2tp_nl_policy,
956 /* can be retrieved by unprivileged users */
957 },
958 {
959 .cmd = L2TP_CMD_TUNNEL_CREATE,
960 .doit = l2tp_nl_cmd_tunnel_create,
961 .policy = l2tp_nl_policy,
962 .flags = GENL_ADMIN_PERM,
963 },
964 {
965 .cmd = L2TP_CMD_TUNNEL_DELETE,
966 .doit = l2tp_nl_cmd_tunnel_delete,
967 .policy = l2tp_nl_policy,
968 .flags = GENL_ADMIN_PERM,
969 },
970 {
971 .cmd = L2TP_CMD_TUNNEL_MODIFY,
972 .doit = l2tp_nl_cmd_tunnel_modify,
973 .policy = l2tp_nl_policy,
974 .flags = GENL_ADMIN_PERM,
975 },
976 {
977 .cmd = L2TP_CMD_TUNNEL_GET,
978 .doit = l2tp_nl_cmd_tunnel_get,
979 .dumpit = l2tp_nl_cmd_tunnel_dump,
980 .policy = l2tp_nl_policy,
981 .flags = GENL_ADMIN_PERM,
982 },
983 {
984 .cmd = L2TP_CMD_SESSION_CREATE,
985 .doit = l2tp_nl_cmd_session_create,
986 .policy = l2tp_nl_policy,
987 .flags = GENL_ADMIN_PERM,
988 },
989 {
990 .cmd = L2TP_CMD_SESSION_DELETE,
991 .doit = l2tp_nl_cmd_session_delete,
992 .policy = l2tp_nl_policy,
993 .flags = GENL_ADMIN_PERM,
994 },
995 {
996 .cmd = L2TP_CMD_SESSION_MODIFY,
997 .doit = l2tp_nl_cmd_session_modify,
998 .policy = l2tp_nl_policy,
999 .flags = GENL_ADMIN_PERM,
1000 },
1001 {
1002 .cmd = L2TP_CMD_SESSION_GET,
1003 .doit = l2tp_nl_cmd_session_get,
1004 .dumpit = l2tp_nl_cmd_session_dump,
1005 .policy = l2tp_nl_policy,
1006 .flags = GENL_ADMIN_PERM,
1007 },
1008};
1009
Johannes Berg56989f62016-10-24 14:40:05 +02001010static struct genl_family l2tp_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001011 .name = L2TP_GENL_NAME,
1012 .version = L2TP_GENL_VERSION,
1013 .hdrsize = 0,
1014 .maxattr = L2TP_ATTR_MAX,
1015 .netnsok = true,
1016 .module = THIS_MODULE,
1017 .ops = l2tp_nl_ops,
1018 .n_ops = ARRAY_SIZE(l2tp_nl_ops),
1019 .mcgrps = l2tp_multicast_group,
1020 .n_mcgrps = ARRAY_SIZE(l2tp_multicast_group),
1021};
1022
James Chapman309795f2010-04-02 06:19:10 +00001023int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
1024{
1025 int ret;
1026
1027 ret = -EINVAL;
1028 if (pw_type >= __L2TP_PWTYPE_MAX)
1029 goto err;
1030
1031 genl_lock();
1032 ret = -EBUSY;
1033 if (l2tp_nl_cmd_ops[pw_type])
1034 goto out;
1035
1036 l2tp_nl_cmd_ops[pw_type] = ops;
David S. Miller8cb49012011-04-17 17:01:05 -07001037 ret = 0;
James Chapman309795f2010-04-02 06:19:10 +00001038
1039out:
1040 genl_unlock();
1041err:
David S. Miller8cb49012011-04-17 17:01:05 -07001042 return ret;
James Chapman309795f2010-04-02 06:19:10 +00001043}
1044EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
1045
1046void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
1047{
1048 if (pw_type < __L2TP_PWTYPE_MAX) {
1049 genl_lock();
1050 l2tp_nl_cmd_ops[pw_type] = NULL;
1051 genl_unlock();
1052 }
1053}
1054EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
1055
Johannes Berg56989f62016-10-24 14:40:05 +02001056static int __init l2tp_nl_init(void)
James Chapman309795f2010-04-02 06:19:10 +00001057{
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001058 pr_info("L2TP netlink interface\n");
Johannes Berg489111e2016-10-24 14:40:03 +02001059 return genl_register_family(&l2tp_nl_family);
James Chapman309795f2010-04-02 06:19:10 +00001060}
1061
1062static void l2tp_nl_cleanup(void)
1063{
1064 genl_unregister_family(&l2tp_nl_family);
1065}
1066
1067module_init(l2tp_nl_init);
1068module_exit(l2tp_nl_cleanup);
1069
1070MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1071MODULE_DESCRIPTION("L2TP netlink");
1072MODULE_LICENSE("GPL");
1073MODULE_VERSION("1.0");
Neil Hormane9412c32012-05-29 09:30:41 +00001074MODULE_ALIAS_GENL_FAMILY("l2tp");