blob: 2abd100b83954fc3eaa1a1314d6ae79bd96f53e3 [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
51static struct l2tp_session *l2tp_nl_session_find(struct genl_info *info)
52{
53 u32 tunnel_id;
54 u32 session_id;
55 char *ifname;
56 struct l2tp_tunnel *tunnel;
57 struct l2tp_session *session = NULL;
58 struct net *net = genl_info_net(info);
59
60 if (info->attrs[L2TP_ATTR_IFNAME]) {
61 ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
62 session = l2tp_session_find_by_ifname(net, ifname);
63 } else if ((info->attrs[L2TP_ATTR_SESSION_ID]) &&
64 (info->attrs[L2TP_ATTR_CONN_ID])) {
65 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
66 session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
67 tunnel = l2tp_tunnel_find(net, tunnel_id);
68 if (tunnel)
69 session = l2tp_session_find(net, tunnel, session_id);
70 }
71
72 return session;
73}
74
75static int l2tp_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
76{
77 struct sk_buff *msg;
78 void *hdr;
79 int ret = -ENOBUFS;
80
Thomas Graf58050fc2012-06-28 03:57:45 +000081 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
James Chapman309795f2010-04-02 06:19:10 +000082 if (!msg) {
83 ret = -ENOMEM;
84 goto out;
85 }
86
Eric W. Biederman15e47302012-09-07 20:12:54 +000087 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
James Chapman309795f2010-04-02 06:19:10 +000088 &l2tp_nl_family, 0, L2TP_CMD_NOOP);
Wei Yongjun7f8436a2012-09-24 18:29:01 +000089 if (!hdr) {
90 ret = -EMSGSIZE;
James Chapman309795f2010-04-02 06:19:10 +000091 goto err_out;
92 }
93
94 genlmsg_end(msg, hdr);
95
Eric W. Biederman15e47302012-09-07 20:12:54 +000096 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
James Chapman309795f2010-04-02 06:19:10 +000097
98err_out:
99 nlmsg_free(msg);
100
101out:
102 return ret;
103}
104
Bill Hong33f72e62014-12-27 10:12:39 -0800105static int l2tp_tunnel_notify(struct genl_family *family,
106 struct genl_info *info,
107 struct l2tp_tunnel *tunnel,
108 u8 cmd)
109{
110 struct sk_buff *msg;
111 int ret;
112
113 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
114 if (!msg)
115 return -ENOMEM;
116
117 ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
118 NLM_F_ACK, tunnel, cmd);
119
Mark Tomlinson853effc2016-02-15 16:24:44 +1300120 if (ret >= 0) {
121 ret = genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC);
122 /* We don't care if no one is listening */
123 if (ret == -ESRCH)
124 ret = 0;
125 return ret;
126 }
Bill Hong33f72e62014-12-27 10:12:39 -0800127
128 nlmsg_free(msg);
129
130 return ret;
131}
132
133static int l2tp_session_notify(struct genl_family *family,
134 struct genl_info *info,
135 struct l2tp_session *session,
136 u8 cmd)
137{
138 struct sk_buff *msg;
139 int ret;
140
141 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
142 if (!msg)
143 return -ENOMEM;
144
145 ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
146 NLM_F_ACK, session, cmd);
147
Mark Tomlinson853effc2016-02-15 16:24:44 +1300148 if (ret >= 0) {
149 ret = genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC);
150 /* We don't care if no one is listening */
151 if (ret == -ESRCH)
152 ret = 0;
153 return ret;
154 }
Bill Hong33f72e62014-12-27 10:12:39 -0800155
156 nlmsg_free(msg);
157
158 return ret;
159}
160
James Chapman309795f2010-04-02 06:19:10 +0000161static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info)
162{
163 u32 tunnel_id;
164 u32 peer_tunnel_id;
165 int proto_version;
166 int fd;
167 int ret = 0;
168 struct l2tp_tunnel_cfg cfg = { 0, };
169 struct l2tp_tunnel *tunnel;
170 struct net *net = genl_info_net(info);
171
172 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
173 ret = -EINVAL;
174 goto out;
175 }
176 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
177
178 if (!info->attrs[L2TP_ATTR_PEER_CONN_ID]) {
179 ret = -EINVAL;
180 goto out;
181 }
182 peer_tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_CONN_ID]);
183
184 if (!info->attrs[L2TP_ATTR_PROTO_VERSION]) {
185 ret = -EINVAL;
186 goto out;
187 }
188 proto_version = nla_get_u8(info->attrs[L2TP_ATTR_PROTO_VERSION]);
189
190 if (!info->attrs[L2TP_ATTR_ENCAP_TYPE]) {
191 ret = -EINVAL;
192 goto out;
193 }
194 cfg.encap = nla_get_u16(info->attrs[L2TP_ATTR_ENCAP_TYPE]);
195
James Chapman789a4a22010-04-02 06:19:40 +0000196 fd = -1;
197 if (info->attrs[L2TP_ATTR_FD]) {
198 fd = nla_get_u32(info->attrs[L2TP_ATTR_FD]);
199 } else {
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000200#if IS_ENABLED(CONFIG_IPV6)
201 if (info->attrs[L2TP_ATTR_IP6_SADDR] &&
202 info->attrs[L2TP_ATTR_IP6_DADDR]) {
203 cfg.local_ip6 = nla_data(
204 info->attrs[L2TP_ATTR_IP6_SADDR]);
205 cfg.peer_ip6 = nla_data(
206 info->attrs[L2TP_ATTR_IP6_DADDR]);
207 } else
208#endif
209 if (info->attrs[L2TP_ATTR_IP_SADDR] &&
210 info->attrs[L2TP_ATTR_IP_DADDR]) {
Jiri Benc67b61f62015-03-29 16:59:26 +0200211 cfg.local_ip.s_addr = nla_get_in_addr(
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000212 info->attrs[L2TP_ATTR_IP_SADDR]);
Jiri Benc67b61f62015-03-29 16:59:26 +0200213 cfg.peer_ip.s_addr = nla_get_in_addr(
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000214 info->attrs[L2TP_ATTR_IP_DADDR]);
215 } else {
216 ret = -EINVAL;
217 goto out;
218 }
James Chapman789a4a22010-04-02 06:19:40 +0000219 if (info->attrs[L2TP_ATTR_UDP_SPORT])
220 cfg.local_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_SPORT]);
221 if (info->attrs[L2TP_ATTR_UDP_DPORT])
222 cfg.peer_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_DPORT]);
223 if (info->attrs[L2TP_ATTR_UDP_CSUM])
224 cfg.use_udp_checksums = nla_get_flag(info->attrs[L2TP_ATTR_UDP_CSUM]);
Tom Herbert6b649fea2014-05-23 08:47:40 -0700225
226#if IS_ENABLED(CONFIG_IPV6)
227 if (info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_TX])
228 cfg.udp6_zero_tx_checksums = nla_get_flag(info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_TX]);
229 if (info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_RX])
230 cfg.udp6_zero_rx_checksums = nla_get_flag(info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_RX]);
231#endif
James Chapman309795f2010-04-02 06:19:10 +0000232 }
James Chapman309795f2010-04-02 06:19:10 +0000233
234 if (info->attrs[L2TP_ATTR_DEBUG])
235 cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
236
237 tunnel = l2tp_tunnel_find(net, tunnel_id);
238 if (tunnel != NULL) {
239 ret = -EEXIST;
240 goto out;
241 }
242
243 ret = -EINVAL;
244 switch (cfg.encap) {
245 case L2TP_ENCAPTYPE_UDP:
246 case L2TP_ENCAPTYPE_IP:
247 ret = l2tp_tunnel_create(net, fd, proto_version, tunnel_id,
248 peer_tunnel_id, &cfg, &tunnel);
249 break;
250 }
251
Bill Hong33f72e62014-12-27 10:12:39 -0800252 if (ret >= 0)
253 ret = l2tp_tunnel_notify(&l2tp_nl_family, info,
254 tunnel, L2TP_CMD_TUNNEL_CREATE);
James Chapman309795f2010-04-02 06:19:10 +0000255out:
256 return ret;
257}
258
259static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info)
260{
261 struct l2tp_tunnel *tunnel;
262 u32 tunnel_id;
263 int ret = 0;
264 struct net *net = genl_info_net(info);
265
266 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
267 ret = -EINVAL;
268 goto out;
269 }
270 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
271
272 tunnel = l2tp_tunnel_find(net, tunnel_id);
273 if (tunnel == NULL) {
274 ret = -ENODEV;
275 goto out;
276 }
277
Bill Hong33f72e62014-12-27 10:12:39 -0800278 l2tp_tunnel_notify(&l2tp_nl_family, info,
279 tunnel, L2TP_CMD_TUNNEL_DELETE);
280
James Chapman309795f2010-04-02 06:19:10 +0000281 (void) l2tp_tunnel_delete(tunnel);
282
283out:
284 return ret;
285}
286
287static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info)
288{
289 struct l2tp_tunnel *tunnel;
290 u32 tunnel_id;
291 int ret = 0;
292 struct net *net = genl_info_net(info);
293
294 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
295 ret = -EINVAL;
296 goto out;
297 }
298 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
299
300 tunnel = l2tp_tunnel_find(net, tunnel_id);
301 if (tunnel == NULL) {
302 ret = -ENODEV;
303 goto out;
304 }
305
306 if (info->attrs[L2TP_ATTR_DEBUG])
307 tunnel->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
308
Bill Hong33f72e62014-12-27 10:12:39 -0800309 ret = l2tp_tunnel_notify(&l2tp_nl_family, info,
310 tunnel, L2TP_CMD_TUNNEL_MODIFY);
311
James Chapman309795f2010-04-02 06:19:10 +0000312out:
313 return ret;
314}
315
Eric W. Biederman15e47302012-09-07 20:12:54 +0000316static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
Bill Hong33f72e62014-12-27 10:12:39 -0800317 struct l2tp_tunnel *tunnel, u8 cmd)
James Chapman309795f2010-04-02 06:19:10 +0000318{
319 void *hdr;
320 struct nlattr *nest;
321 struct sock *sk = NULL;
322 struct inet_sock *inet;
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000323#if IS_ENABLED(CONFIG_IPV6)
324 struct ipv6_pinfo *np = NULL;
325#endif
James Chapman309795f2010-04-02 06:19:10 +0000326
Bill Hong33f72e62014-12-27 10:12:39 -0800327 hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
Wei Yongjun7f8436a2012-09-24 18:29:01 +0000328 if (!hdr)
329 return -EMSGSIZE;
James Chapman309795f2010-04-02 06:19:10 +0000330
David S. Miller60aed2a2012-04-01 19:59:31 -0400331 if (nla_put_u8(skb, L2TP_ATTR_PROTO_VERSION, tunnel->version) ||
332 nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
333 nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
334 nla_put_u32(skb, L2TP_ATTR_DEBUG, tunnel->debug) ||
335 nla_put_u16(skb, L2TP_ATTR_ENCAP_TYPE, tunnel->encap))
336 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000337
338 nest = nla_nest_start(skb, L2TP_ATTR_STATS);
339 if (nest == NULL)
340 goto nla_put_failure;
341
Nicolas Dichtel1c714a92016-04-25 10:25:19 +0200342 if (nla_put_u64_64bit(skb, L2TP_ATTR_TX_PACKETS,
343 atomic_long_read(&tunnel->stats.tx_packets),
344 L2TP_ATTR_STATS_PAD) ||
345 nla_put_u64_64bit(skb, L2TP_ATTR_TX_BYTES,
346 atomic_long_read(&tunnel->stats.tx_bytes),
347 L2TP_ATTR_STATS_PAD) ||
348 nla_put_u64_64bit(skb, L2TP_ATTR_TX_ERRORS,
349 atomic_long_read(&tunnel->stats.tx_errors),
350 L2TP_ATTR_STATS_PAD) ||
351 nla_put_u64_64bit(skb, L2TP_ATTR_RX_PACKETS,
352 atomic_long_read(&tunnel->stats.rx_packets),
353 L2TP_ATTR_STATS_PAD) ||
354 nla_put_u64_64bit(skb, L2TP_ATTR_RX_BYTES,
355 atomic_long_read(&tunnel->stats.rx_bytes),
356 L2TP_ATTR_STATS_PAD) ||
357 nla_put_u64_64bit(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
358 atomic_long_read(&tunnel->stats.rx_seq_discards),
359 L2TP_ATTR_STATS_PAD) ||
360 nla_put_u64_64bit(skb, L2TP_ATTR_RX_OOS_PACKETS,
361 atomic_long_read(&tunnel->stats.rx_oos_packets),
362 L2TP_ATTR_STATS_PAD) ||
363 nla_put_u64_64bit(skb, L2TP_ATTR_RX_ERRORS,
364 atomic_long_read(&tunnel->stats.rx_errors),
365 L2TP_ATTR_STATS_PAD))
David S. Miller60aed2a2012-04-01 19:59:31 -0400366 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000367 nla_nest_end(skb, nest);
368
369 sk = tunnel->sock;
370 if (!sk)
371 goto out;
372
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000373#if IS_ENABLED(CONFIG_IPV6)
374 if (sk->sk_family == AF_INET6)
375 np = inet6_sk(sk);
376#endif
377
James Chapman309795f2010-04-02 06:19:10 +0000378 inet = inet_sk(sk);
379
380 switch (tunnel->encap) {
381 case L2TP_ENCAPTYPE_UDP:
Asbjørn Sloth Tønnesen7ff516f2016-11-07 20:39:25 +0000382 switch (sk->sk_family) {
383 case AF_INET:
384 if (nla_put_u8(skb, L2TP_ATTR_UDP_CSUM, !sk->sk_no_check_tx))
385 goto nla_put_failure;
386 break;
387 }
David S. Miller60aed2a2012-04-01 19:59:31 -0400388 if (nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
Asbjørn Sloth Tønnesen7ff516f2016-11-07 20:39:25 +0000389 nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)))
David S. Miller60aed2a2012-04-01 19:59:31 -0400390 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000391 /* NOBREAK */
392 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000393#if IS_ENABLED(CONFIG_IPV6)
394 if (np) {
Jiri Benc930345e2015-03-29 16:59:25 +0200395 if (nla_put_in6_addr(skb, L2TP_ATTR_IP6_SADDR,
396 &np->saddr) ||
397 nla_put_in6_addr(skb, L2TP_ATTR_IP6_DADDR,
398 &sk->sk_v6_daddr))
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000399 goto nla_put_failure;
400 } else
401#endif
Jiri Benc930345e2015-03-29 16:59:25 +0200402 if (nla_put_in_addr(skb, L2TP_ATTR_IP_SADDR,
403 inet->inet_saddr) ||
404 nla_put_in_addr(skb, L2TP_ATTR_IP_DADDR,
405 inet->inet_daddr))
David S. Miller60aed2a2012-04-01 19:59:31 -0400406 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000407 break;
408 }
409
410out:
Johannes Berg053c0952015-01-16 22:09:00 +0100411 genlmsg_end(skb, hdr);
412 return 0;
James Chapman309795f2010-04-02 06:19:10 +0000413
414nla_put_failure:
415 genlmsg_cancel(skb, hdr);
416 return -1;
417}
418
419static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
420{
421 struct l2tp_tunnel *tunnel;
422 struct sk_buff *msg;
423 u32 tunnel_id;
424 int ret = -ENOBUFS;
425 struct net *net = genl_info_net(info);
426
427 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
428 ret = -EINVAL;
429 goto out;
430 }
431
432 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
433
434 tunnel = l2tp_tunnel_find(net, tunnel_id);
435 if (tunnel == NULL) {
436 ret = -ENODEV;
437 goto out;
438 }
439
Thomas Graf58050fc2012-06-28 03:57:45 +0000440 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
James Chapman309795f2010-04-02 06:19:10 +0000441 if (!msg) {
442 ret = -ENOMEM;
443 goto out;
444 }
445
Eric W. Biederman15e47302012-09-07 20:12:54 +0000446 ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
Bill Hong33f72e62014-12-27 10:12:39 -0800447 NLM_F_ACK, tunnel, L2TP_CMD_TUNNEL_GET);
James Chapman309795f2010-04-02 06:19:10 +0000448 if (ret < 0)
449 goto err_out;
450
Eric W. Biederman15e47302012-09-07 20:12:54 +0000451 return genlmsg_unicast(net, msg, info->snd_portid);
James Chapman309795f2010-04-02 06:19:10 +0000452
453err_out:
454 nlmsg_free(msg);
455
456out:
457 return ret;
458}
459
460static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
461{
462 int ti = cb->args[0];
463 struct l2tp_tunnel *tunnel;
464 struct net *net = sock_net(skb->sk);
465
466 for (;;) {
467 tunnel = l2tp_tunnel_find_nth(net, ti);
468 if (tunnel == NULL)
469 goto out;
470
Eric W. Biederman15e47302012-09-07 20:12:54 +0000471 if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).portid,
James Chapman309795f2010-04-02 06:19:10 +0000472 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg053c0952015-01-16 22:09:00 +0100473 tunnel, L2TP_CMD_TUNNEL_GET) < 0)
James Chapman309795f2010-04-02 06:19:10 +0000474 goto out;
475
476 ti++;
477 }
478
479out:
480 cb->args[0] = ti;
481
482 return skb->len;
483}
484
485static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
486{
487 u32 tunnel_id = 0;
488 u32 session_id;
489 u32 peer_session_id;
490 int ret = 0;
491 struct l2tp_tunnel *tunnel;
492 struct l2tp_session *session;
493 struct l2tp_session_cfg cfg = { 0, };
494 struct net *net = genl_info_net(info);
495
496 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
497 ret = -EINVAL;
498 goto out;
499 }
500 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
501 tunnel = l2tp_tunnel_find(net, tunnel_id);
502 if (!tunnel) {
503 ret = -ENODEV;
504 goto out;
505 }
506
507 if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
508 ret = -EINVAL;
509 goto out;
510 }
511 session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
512 session = l2tp_session_find(net, tunnel, session_id);
513 if (session) {
514 ret = -EEXIST;
515 goto out;
516 }
517
518 if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
519 ret = -EINVAL;
520 goto out;
521 }
522 peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
523
524 if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
525 ret = -EINVAL;
526 goto out;
527 }
528 cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
529 if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
530 ret = -EINVAL;
531 goto out;
532 }
533
534 if (tunnel->version > 2) {
535 if (info->attrs[L2TP_ATTR_OFFSET])
536 cfg.offset = nla_get_u16(info->attrs[L2TP_ATTR_OFFSET]);
537
538 if (info->attrs[L2TP_ATTR_DATA_SEQ])
539 cfg.data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
540
541 cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
542 if (info->attrs[L2TP_ATTR_L2SPEC_TYPE])
543 cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
544
545 cfg.l2specific_len = 4;
546 if (info->attrs[L2TP_ATTR_L2SPEC_LEN])
547 cfg.l2specific_len = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_LEN]);
548
549 if (info->attrs[L2TP_ATTR_COOKIE]) {
550 u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
551 if (len > 8) {
552 ret = -EINVAL;
553 goto out;
554 }
555 cfg.cookie_len = len;
556 memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
557 }
558 if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
559 u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
560 if (len > 8) {
561 ret = -EINVAL;
562 goto out;
563 }
564 cfg.peer_cookie_len = len;
565 memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
566 }
567 if (info->attrs[L2TP_ATTR_IFNAME])
568 cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
569
570 if (info->attrs[L2TP_ATTR_VLAN_ID])
571 cfg.vlan_id = nla_get_u16(info->attrs[L2TP_ATTR_VLAN_ID]);
572 }
573
574 if (info->attrs[L2TP_ATTR_DEBUG])
575 cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
576
577 if (info->attrs[L2TP_ATTR_RECV_SEQ])
578 cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
579
580 if (info->attrs[L2TP_ATTR_SEND_SEQ])
581 cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
582
583 if (info->attrs[L2TP_ATTR_LNS_MODE])
584 cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
585
586 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
587 cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
588
589 if (info->attrs[L2TP_ATTR_MTU])
590 cfg.mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
591
592 if (info->attrs[L2TP_ATTR_MRU])
593 cfg.mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
594
stephen hemmingerf1f39f92015-09-23 21:33:34 -0700595#ifdef CONFIG_MODULES
596 if (l2tp_nl_cmd_ops[cfg.pw_type] == NULL) {
597 genl_unlock();
598 request_module("net-l2tp-type-%u", cfg.pw_type);
599 genl_lock();
600 }
601#endif
James Chapman309795f2010-04-02 06:19:10 +0000602 if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) ||
603 (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) {
604 ret = -EPROTONOSUPPORT;
605 goto out;
606 }
607
608 /* Check that pseudowire-specific params are present */
609 switch (cfg.pw_type) {
610 case L2TP_PWTYPE_NONE:
611 break;
612 case L2TP_PWTYPE_ETH_VLAN:
613 if (!info->attrs[L2TP_ATTR_VLAN_ID]) {
614 ret = -EINVAL;
615 goto out;
616 }
617 break;
618 case L2TP_PWTYPE_ETH:
619 break;
620 case L2TP_PWTYPE_PPP:
621 case L2TP_PWTYPE_PPP_AC:
622 break;
623 case L2TP_PWTYPE_IP:
624 default:
625 ret = -EPROTONOSUPPORT;
626 break;
627 }
628
629 ret = -EPROTONOSUPPORT;
630 if (l2tp_nl_cmd_ops[cfg.pw_type]->session_create)
631 ret = (*l2tp_nl_cmd_ops[cfg.pw_type]->session_create)(net, tunnel_id,
632 session_id, peer_session_id, &cfg);
633
Bill Hong33f72e62014-12-27 10:12:39 -0800634 if (ret >= 0) {
635 session = l2tp_session_find(net, tunnel, session_id);
636 if (session)
637 ret = l2tp_session_notify(&l2tp_nl_family, info, session,
638 L2TP_CMD_SESSION_CREATE);
639 }
640
James Chapman309795f2010-04-02 06:19:10 +0000641out:
642 return ret;
643}
644
645static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
646{
647 int ret = 0;
648 struct l2tp_session *session;
649 u16 pw_type;
650
651 session = l2tp_nl_session_find(info);
652 if (session == NULL) {
653 ret = -ENODEV;
654 goto out;
655 }
656
Bill Hong33f72e62014-12-27 10:12:39 -0800657 l2tp_session_notify(&l2tp_nl_family, info,
658 session, L2TP_CMD_SESSION_DELETE);
659
James Chapman309795f2010-04-02 06:19:10 +0000660 pw_type = session->pwtype;
661 if (pw_type < __L2TP_PWTYPE_MAX)
662 if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
663 ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
664
665out:
666 return ret;
667}
668
669static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
670{
671 int ret = 0;
672 struct l2tp_session *session;
673
674 session = l2tp_nl_session_find(info);
675 if (session == NULL) {
676 ret = -ENODEV;
677 goto out;
678 }
679
680 if (info->attrs[L2TP_ATTR_DEBUG])
681 session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
682
683 if (info->attrs[L2TP_ATTR_DATA_SEQ])
684 session->data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
685
686 if (info->attrs[L2TP_ATTR_RECV_SEQ])
687 session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
688
Guillaume Naultbb5016e2014-03-06 11:14:30 +0100689 if (info->attrs[L2TP_ATTR_SEND_SEQ]) {
James Chapman309795f2010-04-02 06:19:10 +0000690 session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
Guillaume Naultbb5016e2014-03-06 11:14:30 +0100691 l2tp_session_set_header_len(session, session->tunnel->version);
692 }
James Chapman309795f2010-04-02 06:19:10 +0000693
694 if (info->attrs[L2TP_ATTR_LNS_MODE])
695 session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
696
697 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
698 session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
699
700 if (info->attrs[L2TP_ATTR_MTU])
701 session->mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
702
703 if (info->attrs[L2TP_ATTR_MRU])
704 session->mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
705
Bill Hong33f72e62014-12-27 10:12:39 -0800706 ret = l2tp_session_notify(&l2tp_nl_family, info,
707 session, L2TP_CMD_SESSION_MODIFY);
708
James Chapman309795f2010-04-02 06:19:10 +0000709out:
710 return ret;
711}
712
Eric W. Biederman15e47302012-09-07 20:12:54 +0000713static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
Bill Hong33f72e62014-12-27 10:12:39 -0800714 struct l2tp_session *session, u8 cmd)
James Chapman309795f2010-04-02 06:19:10 +0000715{
716 void *hdr;
717 struct nlattr *nest;
718 struct l2tp_tunnel *tunnel = session->tunnel;
719 struct sock *sk = NULL;
720
721 sk = tunnel->sock;
722
Bill Hong33f72e62014-12-27 10:12:39 -0800723 hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
Wei Yongjun7f8436a2012-09-24 18:29:01 +0000724 if (!hdr)
725 return -EMSGSIZE;
James Chapman309795f2010-04-02 06:19:10 +0000726
David S. Miller60aed2a2012-04-01 19:59:31 -0400727 if (nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
728 nla_put_u32(skb, L2TP_ATTR_SESSION_ID, session->session_id) ||
729 nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
730 nla_put_u32(skb, L2TP_ATTR_PEER_SESSION_ID,
731 session->peer_session_id) ||
732 nla_put_u32(skb, L2TP_ATTR_DEBUG, session->debug) ||
733 nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype) ||
734 nla_put_u16(skb, L2TP_ATTR_MTU, session->mtu) ||
735 (session->mru &&
736 nla_put_u16(skb, L2TP_ATTR_MRU, session->mru)))
737 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000738
Alan Coxe269ed22012-10-25 05:22:01 +0000739 if ((session->ifname[0] &&
David S. Miller60aed2a2012-04-01 19:59:31 -0400740 nla_put_string(skb, L2TP_ATTR_IFNAME, session->ifname)) ||
741 (session->cookie_len &&
742 nla_put(skb, L2TP_ATTR_COOKIE, session->cookie_len,
743 &session->cookie[0])) ||
744 (session->peer_cookie_len &&
745 nla_put(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len,
746 &session->peer_cookie[0])) ||
747 nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
748 nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
749 nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
James Chapman309795f2010-04-02 06:19:10 +0000750#ifdef CONFIG_XFRM
David S. Miller60aed2a2012-04-01 19:59:31 -0400751 (((sk) && (sk->sk_policy[0] || sk->sk_policy[1])) &&
752 nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
James Chapman309795f2010-04-02 06:19:10 +0000753#endif
David S. Miller60aed2a2012-04-01 19:59:31 -0400754 (session->reorder_timeout &&
Nicolas Dichtel2175d872016-04-22 17:31:21 +0200755 nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT,
756 session->reorder_timeout, L2TP_ATTR_PAD)))
David S. Miller60aed2a2012-04-01 19:59:31 -0400757 goto nla_put_failure;
James Chapman5de7aee2012-04-29 21:48:46 +0000758
James Chapman309795f2010-04-02 06:19:10 +0000759 nest = nla_nest_start(skb, L2TP_ATTR_STATS);
760 if (nest == NULL)
761 goto nla_put_failure;
James Chapman5de7aee2012-04-29 21:48:46 +0000762
Nicolas Dichtel1c714a92016-04-25 10:25:19 +0200763 if (nla_put_u64_64bit(skb, L2TP_ATTR_TX_PACKETS,
764 atomic_long_read(&session->stats.tx_packets),
765 L2TP_ATTR_STATS_PAD) ||
766 nla_put_u64_64bit(skb, L2TP_ATTR_TX_BYTES,
767 atomic_long_read(&session->stats.tx_bytes),
768 L2TP_ATTR_STATS_PAD) ||
769 nla_put_u64_64bit(skb, L2TP_ATTR_TX_ERRORS,
770 atomic_long_read(&session->stats.tx_errors),
771 L2TP_ATTR_STATS_PAD) ||
772 nla_put_u64_64bit(skb, L2TP_ATTR_RX_PACKETS,
773 atomic_long_read(&session->stats.rx_packets),
774 L2TP_ATTR_STATS_PAD) ||
775 nla_put_u64_64bit(skb, L2TP_ATTR_RX_BYTES,
776 atomic_long_read(&session->stats.rx_bytes),
777 L2TP_ATTR_STATS_PAD) ||
778 nla_put_u64_64bit(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
779 atomic_long_read(&session->stats.rx_seq_discards),
780 L2TP_ATTR_STATS_PAD) ||
781 nla_put_u64_64bit(skb, L2TP_ATTR_RX_OOS_PACKETS,
782 atomic_long_read(&session->stats.rx_oos_packets),
783 L2TP_ATTR_STATS_PAD) ||
784 nla_put_u64_64bit(skb, L2TP_ATTR_RX_ERRORS,
785 atomic_long_read(&session->stats.rx_errors),
786 L2TP_ATTR_STATS_PAD))
David S. Miller60aed2a2012-04-01 19:59:31 -0400787 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000788 nla_nest_end(skb, nest);
789
Johannes Berg053c0952015-01-16 22:09:00 +0100790 genlmsg_end(skb, hdr);
791 return 0;
James Chapman309795f2010-04-02 06:19:10 +0000792
793 nla_put_failure:
794 genlmsg_cancel(skb, hdr);
795 return -1;
796}
797
798static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
799{
800 struct l2tp_session *session;
801 struct sk_buff *msg;
802 int ret;
803
804 session = l2tp_nl_session_find(info);
805 if (session == NULL) {
806 ret = -ENODEV;
807 goto out;
808 }
809
Thomas Graf58050fc2012-06-28 03:57:45 +0000810 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
James Chapman309795f2010-04-02 06:19:10 +0000811 if (!msg) {
812 ret = -ENOMEM;
813 goto out;
814 }
815
Eric W. Biederman15e47302012-09-07 20:12:54 +0000816 ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
Bill Hong33f72e62014-12-27 10:12:39 -0800817 0, session, L2TP_CMD_SESSION_GET);
James Chapman309795f2010-04-02 06:19:10 +0000818 if (ret < 0)
819 goto err_out;
820
Eric W. Biederman15e47302012-09-07 20:12:54 +0000821 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
James Chapman309795f2010-04-02 06:19:10 +0000822
823err_out:
824 nlmsg_free(msg);
825
826out:
827 return ret;
828}
829
830static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
831{
832 struct net *net = sock_net(skb->sk);
833 struct l2tp_session *session;
834 struct l2tp_tunnel *tunnel = NULL;
835 int ti = cb->args[0];
836 int si = cb->args[1];
837
838 for (;;) {
839 if (tunnel == NULL) {
840 tunnel = l2tp_tunnel_find_nth(net, ti);
841 if (tunnel == NULL)
842 goto out;
843 }
844
845 session = l2tp_session_find_nth(tunnel, si);
846 if (session == NULL) {
847 ti++;
848 tunnel = NULL;
849 si = 0;
850 continue;
851 }
852
Eric W. Biederman15e47302012-09-07 20:12:54 +0000853 if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).portid,
James Chapman309795f2010-04-02 06:19:10 +0000854 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg053c0952015-01-16 22:09:00 +0100855 session, L2TP_CMD_SESSION_GET) < 0)
James Chapman309795f2010-04-02 06:19:10 +0000856 break;
857
858 si++;
859 }
860
861out:
862 cb->args[0] = ti;
863 cb->args[1] = si;
864
865 return skb->len;
866}
867
stephen hemmingerf5bb3412016-08-31 23:24:41 -0700868static const struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
James Chapman309795f2010-04-02 06:19:10 +0000869 [L2TP_ATTR_NONE] = { .type = NLA_UNSPEC, },
870 [L2TP_ATTR_PW_TYPE] = { .type = NLA_U16, },
871 [L2TP_ATTR_ENCAP_TYPE] = { .type = NLA_U16, },
872 [L2TP_ATTR_OFFSET] = { .type = NLA_U16, },
873 [L2TP_ATTR_DATA_SEQ] = { .type = NLA_U8, },
874 [L2TP_ATTR_L2SPEC_TYPE] = { .type = NLA_U8, },
875 [L2TP_ATTR_L2SPEC_LEN] = { .type = NLA_U8, },
876 [L2TP_ATTR_PROTO_VERSION] = { .type = NLA_U8, },
877 [L2TP_ATTR_CONN_ID] = { .type = NLA_U32, },
878 [L2TP_ATTR_PEER_CONN_ID] = { .type = NLA_U32, },
879 [L2TP_ATTR_SESSION_ID] = { .type = NLA_U32, },
880 [L2TP_ATTR_PEER_SESSION_ID] = { .type = NLA_U32, },
881 [L2TP_ATTR_UDP_CSUM] = { .type = NLA_U8, },
882 [L2TP_ATTR_VLAN_ID] = { .type = NLA_U16, },
883 [L2TP_ATTR_DEBUG] = { .type = NLA_U32, },
884 [L2TP_ATTR_RECV_SEQ] = { .type = NLA_U8, },
885 [L2TP_ATTR_SEND_SEQ] = { .type = NLA_U8, },
886 [L2TP_ATTR_LNS_MODE] = { .type = NLA_U8, },
887 [L2TP_ATTR_USING_IPSEC] = { .type = NLA_U8, },
888 [L2TP_ATTR_RECV_TIMEOUT] = { .type = NLA_MSECS, },
889 [L2TP_ATTR_FD] = { .type = NLA_U32, },
890 [L2TP_ATTR_IP_SADDR] = { .type = NLA_U32, },
891 [L2TP_ATTR_IP_DADDR] = { .type = NLA_U32, },
892 [L2TP_ATTR_UDP_SPORT] = { .type = NLA_U16, },
893 [L2TP_ATTR_UDP_DPORT] = { .type = NLA_U16, },
894 [L2TP_ATTR_MTU] = { .type = NLA_U16, },
895 [L2TP_ATTR_MRU] = { .type = NLA_U16, },
896 [L2TP_ATTR_STATS] = { .type = NLA_NESTED, },
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000897 [L2TP_ATTR_IP6_SADDR] = {
898 .type = NLA_BINARY,
899 .len = sizeof(struct in6_addr),
900 },
901 [L2TP_ATTR_IP6_DADDR] = {
902 .type = NLA_BINARY,
903 .len = sizeof(struct in6_addr),
904 },
James Chapman309795f2010-04-02 06:19:10 +0000905 [L2TP_ATTR_IFNAME] = {
906 .type = NLA_NUL_STRING,
907 .len = IFNAMSIZ - 1,
908 },
909 [L2TP_ATTR_COOKIE] = {
910 .type = NLA_BINARY,
911 .len = 8,
912 },
913 [L2TP_ATTR_PEER_COOKIE] = {
914 .type = NLA_BINARY,
915 .len = 8,
916 },
917};
918
Johannes Berg4534de82013-11-14 17:14:46 +0100919static const struct genl_ops l2tp_nl_ops[] = {
James Chapman309795f2010-04-02 06:19:10 +0000920 {
921 .cmd = L2TP_CMD_NOOP,
922 .doit = l2tp_nl_cmd_noop,
923 .policy = l2tp_nl_policy,
924 /* can be retrieved by unprivileged users */
925 },
926 {
927 .cmd = L2TP_CMD_TUNNEL_CREATE,
928 .doit = l2tp_nl_cmd_tunnel_create,
929 .policy = l2tp_nl_policy,
930 .flags = GENL_ADMIN_PERM,
931 },
932 {
933 .cmd = L2TP_CMD_TUNNEL_DELETE,
934 .doit = l2tp_nl_cmd_tunnel_delete,
935 .policy = l2tp_nl_policy,
936 .flags = GENL_ADMIN_PERM,
937 },
938 {
939 .cmd = L2TP_CMD_TUNNEL_MODIFY,
940 .doit = l2tp_nl_cmd_tunnel_modify,
941 .policy = l2tp_nl_policy,
942 .flags = GENL_ADMIN_PERM,
943 },
944 {
945 .cmd = L2TP_CMD_TUNNEL_GET,
946 .doit = l2tp_nl_cmd_tunnel_get,
947 .dumpit = l2tp_nl_cmd_tunnel_dump,
948 .policy = l2tp_nl_policy,
949 .flags = GENL_ADMIN_PERM,
950 },
951 {
952 .cmd = L2TP_CMD_SESSION_CREATE,
953 .doit = l2tp_nl_cmd_session_create,
954 .policy = l2tp_nl_policy,
955 .flags = GENL_ADMIN_PERM,
956 },
957 {
958 .cmd = L2TP_CMD_SESSION_DELETE,
959 .doit = l2tp_nl_cmd_session_delete,
960 .policy = l2tp_nl_policy,
961 .flags = GENL_ADMIN_PERM,
962 },
963 {
964 .cmd = L2TP_CMD_SESSION_MODIFY,
965 .doit = l2tp_nl_cmd_session_modify,
966 .policy = l2tp_nl_policy,
967 .flags = GENL_ADMIN_PERM,
968 },
969 {
970 .cmd = L2TP_CMD_SESSION_GET,
971 .doit = l2tp_nl_cmd_session_get,
972 .dumpit = l2tp_nl_cmd_session_dump,
973 .policy = l2tp_nl_policy,
974 .flags = GENL_ADMIN_PERM,
975 },
976};
977
Johannes Berg56989f62016-10-24 14:40:05 +0200978static struct genl_family l2tp_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200979 .name = L2TP_GENL_NAME,
980 .version = L2TP_GENL_VERSION,
981 .hdrsize = 0,
982 .maxattr = L2TP_ATTR_MAX,
983 .netnsok = true,
984 .module = THIS_MODULE,
985 .ops = l2tp_nl_ops,
986 .n_ops = ARRAY_SIZE(l2tp_nl_ops),
987 .mcgrps = l2tp_multicast_group,
988 .n_mcgrps = ARRAY_SIZE(l2tp_multicast_group),
989};
990
James Chapman309795f2010-04-02 06:19:10 +0000991int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
992{
993 int ret;
994
995 ret = -EINVAL;
996 if (pw_type >= __L2TP_PWTYPE_MAX)
997 goto err;
998
999 genl_lock();
1000 ret = -EBUSY;
1001 if (l2tp_nl_cmd_ops[pw_type])
1002 goto out;
1003
1004 l2tp_nl_cmd_ops[pw_type] = ops;
David S. Miller8cb49012011-04-17 17:01:05 -07001005 ret = 0;
James Chapman309795f2010-04-02 06:19:10 +00001006
1007out:
1008 genl_unlock();
1009err:
David S. Miller8cb49012011-04-17 17:01:05 -07001010 return ret;
James Chapman309795f2010-04-02 06:19:10 +00001011}
1012EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
1013
1014void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
1015{
1016 if (pw_type < __L2TP_PWTYPE_MAX) {
1017 genl_lock();
1018 l2tp_nl_cmd_ops[pw_type] = NULL;
1019 genl_unlock();
1020 }
1021}
1022EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
1023
Johannes Berg56989f62016-10-24 14:40:05 +02001024static int __init l2tp_nl_init(void)
James Chapman309795f2010-04-02 06:19:10 +00001025{
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001026 pr_info("L2TP netlink interface\n");
Johannes Berg489111e2016-10-24 14:40:03 +02001027 return genl_register_family(&l2tp_nl_family);
James Chapman309795f2010-04-02 06:19:10 +00001028}
1029
1030static void l2tp_nl_cleanup(void)
1031{
1032 genl_unregister_family(&l2tp_nl_family);
1033}
1034
1035module_init(l2tp_nl_init);
1036module_exit(l2tp_nl_cleanup);
1037
1038MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1039MODULE_DESCRIPTION("L2TP netlink");
1040MODULE_LICENSE("GPL");
1041MODULE_VERSION("1.0");
Neil Hormane9412c32012-05-29 09:30:41 +00001042MODULE_ALIAS_GENL_FAMILY("l2tp");