blob: 59aa2d204e4aa0ceda4d10068e73cb1ef1498af0 [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:
David S. Miller60aed2a2012-04-01 19:59:31 -0400382 if (nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
383 nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)) ||
Tom Herbert28448b82014-05-23 08:47:19 -0700384 nla_put_u8(skb, L2TP_ATTR_UDP_CSUM, !sk->sk_no_check_tx))
David S. Miller60aed2a2012-04-01 19:59:31 -0400385 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000386 /* NOBREAK */
387 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000388#if IS_ENABLED(CONFIG_IPV6)
389 if (np) {
Jiri Benc930345e2015-03-29 16:59:25 +0200390 if (nla_put_in6_addr(skb, L2TP_ATTR_IP6_SADDR,
391 &np->saddr) ||
392 nla_put_in6_addr(skb, L2TP_ATTR_IP6_DADDR,
393 &sk->sk_v6_daddr))
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000394 goto nla_put_failure;
395 } else
396#endif
Jiri Benc930345e2015-03-29 16:59:25 +0200397 if (nla_put_in_addr(skb, L2TP_ATTR_IP_SADDR,
398 inet->inet_saddr) ||
399 nla_put_in_addr(skb, L2TP_ATTR_IP_DADDR,
400 inet->inet_daddr))
David S. Miller60aed2a2012-04-01 19:59:31 -0400401 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000402 break;
403 }
404
405out:
Johannes Berg053c0952015-01-16 22:09:00 +0100406 genlmsg_end(skb, hdr);
407 return 0;
James Chapman309795f2010-04-02 06:19:10 +0000408
409nla_put_failure:
410 genlmsg_cancel(skb, hdr);
411 return -1;
412}
413
414static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
415{
416 struct l2tp_tunnel *tunnel;
417 struct sk_buff *msg;
418 u32 tunnel_id;
419 int ret = -ENOBUFS;
420 struct net *net = genl_info_net(info);
421
422 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
423 ret = -EINVAL;
424 goto out;
425 }
426
427 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
428
429 tunnel = l2tp_tunnel_find(net, tunnel_id);
430 if (tunnel == NULL) {
431 ret = -ENODEV;
432 goto out;
433 }
434
Thomas Graf58050fc2012-06-28 03:57:45 +0000435 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
James Chapman309795f2010-04-02 06:19:10 +0000436 if (!msg) {
437 ret = -ENOMEM;
438 goto out;
439 }
440
Eric W. Biederman15e47302012-09-07 20:12:54 +0000441 ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
Bill Hong33f72e62014-12-27 10:12:39 -0800442 NLM_F_ACK, tunnel, L2TP_CMD_TUNNEL_GET);
James Chapman309795f2010-04-02 06:19:10 +0000443 if (ret < 0)
444 goto err_out;
445
Eric W. Biederman15e47302012-09-07 20:12:54 +0000446 return genlmsg_unicast(net, msg, info->snd_portid);
James Chapman309795f2010-04-02 06:19:10 +0000447
448err_out:
449 nlmsg_free(msg);
450
451out:
452 return ret;
453}
454
455static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
456{
457 int ti = cb->args[0];
458 struct l2tp_tunnel *tunnel;
459 struct net *net = sock_net(skb->sk);
460
461 for (;;) {
462 tunnel = l2tp_tunnel_find_nth(net, ti);
463 if (tunnel == NULL)
464 goto out;
465
Eric W. Biederman15e47302012-09-07 20:12:54 +0000466 if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).portid,
James Chapman309795f2010-04-02 06:19:10 +0000467 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg053c0952015-01-16 22:09:00 +0100468 tunnel, L2TP_CMD_TUNNEL_GET) < 0)
James Chapman309795f2010-04-02 06:19:10 +0000469 goto out;
470
471 ti++;
472 }
473
474out:
475 cb->args[0] = ti;
476
477 return skb->len;
478}
479
480static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
481{
482 u32 tunnel_id = 0;
483 u32 session_id;
484 u32 peer_session_id;
485 int ret = 0;
486 struct l2tp_tunnel *tunnel;
487 struct l2tp_session *session;
488 struct l2tp_session_cfg cfg = { 0, };
489 struct net *net = genl_info_net(info);
490
491 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
492 ret = -EINVAL;
493 goto out;
494 }
495 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
496 tunnel = l2tp_tunnel_find(net, tunnel_id);
497 if (!tunnel) {
498 ret = -ENODEV;
499 goto out;
500 }
501
502 if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
503 ret = -EINVAL;
504 goto out;
505 }
506 session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
507 session = l2tp_session_find(net, tunnel, session_id);
508 if (session) {
509 ret = -EEXIST;
510 goto out;
511 }
512
513 if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
514 ret = -EINVAL;
515 goto out;
516 }
517 peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
518
519 if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
520 ret = -EINVAL;
521 goto out;
522 }
523 cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
524 if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
525 ret = -EINVAL;
526 goto out;
527 }
528
529 if (tunnel->version > 2) {
530 if (info->attrs[L2TP_ATTR_OFFSET])
531 cfg.offset = nla_get_u16(info->attrs[L2TP_ATTR_OFFSET]);
532
533 if (info->attrs[L2TP_ATTR_DATA_SEQ])
534 cfg.data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
535
536 cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
537 if (info->attrs[L2TP_ATTR_L2SPEC_TYPE])
538 cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
539
540 cfg.l2specific_len = 4;
541 if (info->attrs[L2TP_ATTR_L2SPEC_LEN])
542 cfg.l2specific_len = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_LEN]);
543
544 if (info->attrs[L2TP_ATTR_COOKIE]) {
545 u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
546 if (len > 8) {
547 ret = -EINVAL;
548 goto out;
549 }
550 cfg.cookie_len = len;
551 memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
552 }
553 if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
554 u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
555 if (len > 8) {
556 ret = -EINVAL;
557 goto out;
558 }
559 cfg.peer_cookie_len = len;
560 memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
561 }
562 if (info->attrs[L2TP_ATTR_IFNAME])
563 cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
564
565 if (info->attrs[L2TP_ATTR_VLAN_ID])
566 cfg.vlan_id = nla_get_u16(info->attrs[L2TP_ATTR_VLAN_ID]);
567 }
568
569 if (info->attrs[L2TP_ATTR_DEBUG])
570 cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
571
572 if (info->attrs[L2TP_ATTR_RECV_SEQ])
573 cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
574
575 if (info->attrs[L2TP_ATTR_SEND_SEQ])
576 cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
577
578 if (info->attrs[L2TP_ATTR_LNS_MODE])
579 cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
580
581 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
582 cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
583
584 if (info->attrs[L2TP_ATTR_MTU])
585 cfg.mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
586
587 if (info->attrs[L2TP_ATTR_MRU])
588 cfg.mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
589
stephen hemmingerf1f39f92015-09-23 21:33:34 -0700590#ifdef CONFIG_MODULES
591 if (l2tp_nl_cmd_ops[cfg.pw_type] == NULL) {
592 genl_unlock();
593 request_module("net-l2tp-type-%u", cfg.pw_type);
594 genl_lock();
595 }
596#endif
James Chapman309795f2010-04-02 06:19:10 +0000597 if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) ||
598 (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) {
599 ret = -EPROTONOSUPPORT;
600 goto out;
601 }
602
603 /* Check that pseudowire-specific params are present */
604 switch (cfg.pw_type) {
605 case L2TP_PWTYPE_NONE:
606 break;
607 case L2TP_PWTYPE_ETH_VLAN:
608 if (!info->attrs[L2TP_ATTR_VLAN_ID]) {
609 ret = -EINVAL;
610 goto out;
611 }
612 break;
613 case L2TP_PWTYPE_ETH:
614 break;
615 case L2TP_PWTYPE_PPP:
616 case L2TP_PWTYPE_PPP_AC:
617 break;
618 case L2TP_PWTYPE_IP:
619 default:
620 ret = -EPROTONOSUPPORT;
621 break;
622 }
623
624 ret = -EPROTONOSUPPORT;
625 if (l2tp_nl_cmd_ops[cfg.pw_type]->session_create)
626 ret = (*l2tp_nl_cmd_ops[cfg.pw_type]->session_create)(net, tunnel_id,
627 session_id, peer_session_id, &cfg);
628
Bill Hong33f72e62014-12-27 10:12:39 -0800629 if (ret >= 0) {
630 session = l2tp_session_find(net, tunnel, session_id);
631 if (session)
632 ret = l2tp_session_notify(&l2tp_nl_family, info, session,
633 L2TP_CMD_SESSION_CREATE);
634 }
635
James Chapman309795f2010-04-02 06:19:10 +0000636out:
637 return ret;
638}
639
640static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
641{
642 int ret = 0;
643 struct l2tp_session *session;
644 u16 pw_type;
645
646 session = l2tp_nl_session_find(info);
647 if (session == NULL) {
648 ret = -ENODEV;
649 goto out;
650 }
651
Bill Hong33f72e62014-12-27 10:12:39 -0800652 l2tp_session_notify(&l2tp_nl_family, info,
653 session, L2TP_CMD_SESSION_DELETE);
654
James Chapman309795f2010-04-02 06:19:10 +0000655 pw_type = session->pwtype;
656 if (pw_type < __L2TP_PWTYPE_MAX)
657 if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
658 ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
659
660out:
661 return ret;
662}
663
664static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
665{
666 int ret = 0;
667 struct l2tp_session *session;
668
669 session = l2tp_nl_session_find(info);
670 if (session == NULL) {
671 ret = -ENODEV;
672 goto out;
673 }
674
675 if (info->attrs[L2TP_ATTR_DEBUG])
676 session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
677
678 if (info->attrs[L2TP_ATTR_DATA_SEQ])
679 session->data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
680
681 if (info->attrs[L2TP_ATTR_RECV_SEQ])
682 session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
683
Guillaume Naultbb5016e2014-03-06 11:14:30 +0100684 if (info->attrs[L2TP_ATTR_SEND_SEQ]) {
James Chapman309795f2010-04-02 06:19:10 +0000685 session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
Guillaume Naultbb5016e2014-03-06 11:14:30 +0100686 l2tp_session_set_header_len(session, session->tunnel->version);
687 }
James Chapman309795f2010-04-02 06:19:10 +0000688
689 if (info->attrs[L2TP_ATTR_LNS_MODE])
690 session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
691
692 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
693 session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
694
695 if (info->attrs[L2TP_ATTR_MTU])
696 session->mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
697
698 if (info->attrs[L2TP_ATTR_MRU])
699 session->mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
700
Bill Hong33f72e62014-12-27 10:12:39 -0800701 ret = l2tp_session_notify(&l2tp_nl_family, info,
702 session, L2TP_CMD_SESSION_MODIFY);
703
James Chapman309795f2010-04-02 06:19:10 +0000704out:
705 return ret;
706}
707
Eric W. Biederman15e47302012-09-07 20:12:54 +0000708static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
Bill Hong33f72e62014-12-27 10:12:39 -0800709 struct l2tp_session *session, u8 cmd)
James Chapman309795f2010-04-02 06:19:10 +0000710{
711 void *hdr;
712 struct nlattr *nest;
713 struct l2tp_tunnel *tunnel = session->tunnel;
714 struct sock *sk = NULL;
715
716 sk = tunnel->sock;
717
Bill Hong33f72e62014-12-27 10:12:39 -0800718 hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
Wei Yongjun7f8436a2012-09-24 18:29:01 +0000719 if (!hdr)
720 return -EMSGSIZE;
James Chapman309795f2010-04-02 06:19:10 +0000721
David S. Miller60aed2a2012-04-01 19:59:31 -0400722 if (nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
723 nla_put_u32(skb, L2TP_ATTR_SESSION_ID, session->session_id) ||
724 nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
725 nla_put_u32(skb, L2TP_ATTR_PEER_SESSION_ID,
726 session->peer_session_id) ||
727 nla_put_u32(skb, L2TP_ATTR_DEBUG, session->debug) ||
728 nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype) ||
729 nla_put_u16(skb, L2TP_ATTR_MTU, session->mtu) ||
730 (session->mru &&
731 nla_put_u16(skb, L2TP_ATTR_MRU, session->mru)))
732 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000733
Alan Coxe269ed22012-10-25 05:22:01 +0000734 if ((session->ifname[0] &&
David S. Miller60aed2a2012-04-01 19:59:31 -0400735 nla_put_string(skb, L2TP_ATTR_IFNAME, session->ifname)) ||
736 (session->cookie_len &&
737 nla_put(skb, L2TP_ATTR_COOKIE, session->cookie_len,
738 &session->cookie[0])) ||
739 (session->peer_cookie_len &&
740 nla_put(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len,
741 &session->peer_cookie[0])) ||
742 nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
743 nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
744 nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
James Chapman309795f2010-04-02 06:19:10 +0000745#ifdef CONFIG_XFRM
David S. Miller60aed2a2012-04-01 19:59:31 -0400746 (((sk) && (sk->sk_policy[0] || sk->sk_policy[1])) &&
747 nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
James Chapman309795f2010-04-02 06:19:10 +0000748#endif
David S. Miller60aed2a2012-04-01 19:59:31 -0400749 (session->reorder_timeout &&
Nicolas Dichtel2175d872016-04-22 17:31:21 +0200750 nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT,
751 session->reorder_timeout, L2TP_ATTR_PAD)))
David S. Miller60aed2a2012-04-01 19:59:31 -0400752 goto nla_put_failure;
James Chapman5de7aee2012-04-29 21:48:46 +0000753
James Chapman309795f2010-04-02 06:19:10 +0000754 nest = nla_nest_start(skb, L2TP_ATTR_STATS);
755 if (nest == NULL)
756 goto nla_put_failure;
James Chapman5de7aee2012-04-29 21:48:46 +0000757
Nicolas Dichtel1c714a92016-04-25 10:25:19 +0200758 if (nla_put_u64_64bit(skb, L2TP_ATTR_TX_PACKETS,
759 atomic_long_read(&session->stats.tx_packets),
760 L2TP_ATTR_STATS_PAD) ||
761 nla_put_u64_64bit(skb, L2TP_ATTR_TX_BYTES,
762 atomic_long_read(&session->stats.tx_bytes),
763 L2TP_ATTR_STATS_PAD) ||
764 nla_put_u64_64bit(skb, L2TP_ATTR_TX_ERRORS,
765 atomic_long_read(&session->stats.tx_errors),
766 L2TP_ATTR_STATS_PAD) ||
767 nla_put_u64_64bit(skb, L2TP_ATTR_RX_PACKETS,
768 atomic_long_read(&session->stats.rx_packets),
769 L2TP_ATTR_STATS_PAD) ||
770 nla_put_u64_64bit(skb, L2TP_ATTR_RX_BYTES,
771 atomic_long_read(&session->stats.rx_bytes),
772 L2TP_ATTR_STATS_PAD) ||
773 nla_put_u64_64bit(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
774 atomic_long_read(&session->stats.rx_seq_discards),
775 L2TP_ATTR_STATS_PAD) ||
776 nla_put_u64_64bit(skb, L2TP_ATTR_RX_OOS_PACKETS,
777 atomic_long_read(&session->stats.rx_oos_packets),
778 L2TP_ATTR_STATS_PAD) ||
779 nla_put_u64_64bit(skb, L2TP_ATTR_RX_ERRORS,
780 atomic_long_read(&session->stats.rx_errors),
781 L2TP_ATTR_STATS_PAD))
David S. Miller60aed2a2012-04-01 19:59:31 -0400782 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000783 nla_nest_end(skb, nest);
784
Johannes Berg053c0952015-01-16 22:09:00 +0100785 genlmsg_end(skb, hdr);
786 return 0;
James Chapman309795f2010-04-02 06:19:10 +0000787
788 nla_put_failure:
789 genlmsg_cancel(skb, hdr);
790 return -1;
791}
792
793static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
794{
795 struct l2tp_session *session;
796 struct sk_buff *msg;
797 int ret;
798
799 session = l2tp_nl_session_find(info);
800 if (session == NULL) {
801 ret = -ENODEV;
802 goto out;
803 }
804
Thomas Graf58050fc2012-06-28 03:57:45 +0000805 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
James Chapman309795f2010-04-02 06:19:10 +0000806 if (!msg) {
807 ret = -ENOMEM;
808 goto out;
809 }
810
Eric W. Biederman15e47302012-09-07 20:12:54 +0000811 ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
Bill Hong33f72e62014-12-27 10:12:39 -0800812 0, session, L2TP_CMD_SESSION_GET);
James Chapman309795f2010-04-02 06:19:10 +0000813 if (ret < 0)
814 goto err_out;
815
Eric W. Biederman15e47302012-09-07 20:12:54 +0000816 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
James Chapman309795f2010-04-02 06:19:10 +0000817
818err_out:
819 nlmsg_free(msg);
820
821out:
822 return ret;
823}
824
825static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
826{
827 struct net *net = sock_net(skb->sk);
828 struct l2tp_session *session;
829 struct l2tp_tunnel *tunnel = NULL;
830 int ti = cb->args[0];
831 int si = cb->args[1];
832
833 for (;;) {
834 if (tunnel == NULL) {
835 tunnel = l2tp_tunnel_find_nth(net, ti);
836 if (tunnel == NULL)
837 goto out;
838 }
839
840 session = l2tp_session_find_nth(tunnel, si);
841 if (session == NULL) {
842 ti++;
843 tunnel = NULL;
844 si = 0;
845 continue;
846 }
847
Eric W. Biederman15e47302012-09-07 20:12:54 +0000848 if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).portid,
James Chapman309795f2010-04-02 06:19:10 +0000849 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg053c0952015-01-16 22:09:00 +0100850 session, L2TP_CMD_SESSION_GET) < 0)
James Chapman309795f2010-04-02 06:19:10 +0000851 break;
852
853 si++;
854 }
855
856out:
857 cb->args[0] = ti;
858 cb->args[1] = si;
859
860 return skb->len;
861}
862
stephen hemmingerf5bb3412016-08-31 23:24:41 -0700863static const struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
James Chapman309795f2010-04-02 06:19:10 +0000864 [L2TP_ATTR_NONE] = { .type = NLA_UNSPEC, },
865 [L2TP_ATTR_PW_TYPE] = { .type = NLA_U16, },
866 [L2TP_ATTR_ENCAP_TYPE] = { .type = NLA_U16, },
867 [L2TP_ATTR_OFFSET] = { .type = NLA_U16, },
868 [L2TP_ATTR_DATA_SEQ] = { .type = NLA_U8, },
869 [L2TP_ATTR_L2SPEC_TYPE] = { .type = NLA_U8, },
870 [L2TP_ATTR_L2SPEC_LEN] = { .type = NLA_U8, },
871 [L2TP_ATTR_PROTO_VERSION] = { .type = NLA_U8, },
872 [L2TP_ATTR_CONN_ID] = { .type = NLA_U32, },
873 [L2TP_ATTR_PEER_CONN_ID] = { .type = NLA_U32, },
874 [L2TP_ATTR_SESSION_ID] = { .type = NLA_U32, },
875 [L2TP_ATTR_PEER_SESSION_ID] = { .type = NLA_U32, },
876 [L2TP_ATTR_UDP_CSUM] = { .type = NLA_U8, },
877 [L2TP_ATTR_VLAN_ID] = { .type = NLA_U16, },
878 [L2TP_ATTR_DEBUG] = { .type = NLA_U32, },
879 [L2TP_ATTR_RECV_SEQ] = { .type = NLA_U8, },
880 [L2TP_ATTR_SEND_SEQ] = { .type = NLA_U8, },
881 [L2TP_ATTR_LNS_MODE] = { .type = NLA_U8, },
882 [L2TP_ATTR_USING_IPSEC] = { .type = NLA_U8, },
883 [L2TP_ATTR_RECV_TIMEOUT] = { .type = NLA_MSECS, },
884 [L2TP_ATTR_FD] = { .type = NLA_U32, },
885 [L2TP_ATTR_IP_SADDR] = { .type = NLA_U32, },
886 [L2TP_ATTR_IP_DADDR] = { .type = NLA_U32, },
887 [L2TP_ATTR_UDP_SPORT] = { .type = NLA_U16, },
888 [L2TP_ATTR_UDP_DPORT] = { .type = NLA_U16, },
889 [L2TP_ATTR_MTU] = { .type = NLA_U16, },
890 [L2TP_ATTR_MRU] = { .type = NLA_U16, },
891 [L2TP_ATTR_STATS] = { .type = NLA_NESTED, },
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000892 [L2TP_ATTR_IP6_SADDR] = {
893 .type = NLA_BINARY,
894 .len = sizeof(struct in6_addr),
895 },
896 [L2TP_ATTR_IP6_DADDR] = {
897 .type = NLA_BINARY,
898 .len = sizeof(struct in6_addr),
899 },
James Chapman309795f2010-04-02 06:19:10 +0000900 [L2TP_ATTR_IFNAME] = {
901 .type = NLA_NUL_STRING,
902 .len = IFNAMSIZ - 1,
903 },
904 [L2TP_ATTR_COOKIE] = {
905 .type = NLA_BINARY,
906 .len = 8,
907 },
908 [L2TP_ATTR_PEER_COOKIE] = {
909 .type = NLA_BINARY,
910 .len = 8,
911 },
912};
913
Johannes Berg4534de82013-11-14 17:14:46 +0100914static const struct genl_ops l2tp_nl_ops[] = {
James Chapman309795f2010-04-02 06:19:10 +0000915 {
916 .cmd = L2TP_CMD_NOOP,
917 .doit = l2tp_nl_cmd_noop,
918 .policy = l2tp_nl_policy,
919 /* can be retrieved by unprivileged users */
920 },
921 {
922 .cmd = L2TP_CMD_TUNNEL_CREATE,
923 .doit = l2tp_nl_cmd_tunnel_create,
924 .policy = l2tp_nl_policy,
925 .flags = GENL_ADMIN_PERM,
926 },
927 {
928 .cmd = L2TP_CMD_TUNNEL_DELETE,
929 .doit = l2tp_nl_cmd_tunnel_delete,
930 .policy = l2tp_nl_policy,
931 .flags = GENL_ADMIN_PERM,
932 },
933 {
934 .cmd = L2TP_CMD_TUNNEL_MODIFY,
935 .doit = l2tp_nl_cmd_tunnel_modify,
936 .policy = l2tp_nl_policy,
937 .flags = GENL_ADMIN_PERM,
938 },
939 {
940 .cmd = L2TP_CMD_TUNNEL_GET,
941 .doit = l2tp_nl_cmd_tunnel_get,
942 .dumpit = l2tp_nl_cmd_tunnel_dump,
943 .policy = l2tp_nl_policy,
944 .flags = GENL_ADMIN_PERM,
945 },
946 {
947 .cmd = L2TP_CMD_SESSION_CREATE,
948 .doit = l2tp_nl_cmd_session_create,
949 .policy = l2tp_nl_policy,
950 .flags = GENL_ADMIN_PERM,
951 },
952 {
953 .cmd = L2TP_CMD_SESSION_DELETE,
954 .doit = l2tp_nl_cmd_session_delete,
955 .policy = l2tp_nl_policy,
956 .flags = GENL_ADMIN_PERM,
957 },
958 {
959 .cmd = L2TP_CMD_SESSION_MODIFY,
960 .doit = l2tp_nl_cmd_session_modify,
961 .policy = l2tp_nl_policy,
962 .flags = GENL_ADMIN_PERM,
963 },
964 {
965 .cmd = L2TP_CMD_SESSION_GET,
966 .doit = l2tp_nl_cmd_session_get,
967 .dumpit = l2tp_nl_cmd_session_dump,
968 .policy = l2tp_nl_policy,
969 .flags = GENL_ADMIN_PERM,
970 },
971};
972
Johannes Berg56989f62016-10-24 14:40:05 +0200973static struct genl_family l2tp_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200974 .name = L2TP_GENL_NAME,
975 .version = L2TP_GENL_VERSION,
976 .hdrsize = 0,
977 .maxattr = L2TP_ATTR_MAX,
978 .netnsok = true,
979 .module = THIS_MODULE,
980 .ops = l2tp_nl_ops,
981 .n_ops = ARRAY_SIZE(l2tp_nl_ops),
982 .mcgrps = l2tp_multicast_group,
983 .n_mcgrps = ARRAY_SIZE(l2tp_multicast_group),
984};
985
James Chapman309795f2010-04-02 06:19:10 +0000986int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
987{
988 int ret;
989
990 ret = -EINVAL;
991 if (pw_type >= __L2TP_PWTYPE_MAX)
992 goto err;
993
994 genl_lock();
995 ret = -EBUSY;
996 if (l2tp_nl_cmd_ops[pw_type])
997 goto out;
998
999 l2tp_nl_cmd_ops[pw_type] = ops;
David S. Miller8cb49012011-04-17 17:01:05 -07001000 ret = 0;
James Chapman309795f2010-04-02 06:19:10 +00001001
1002out:
1003 genl_unlock();
1004err:
David S. Miller8cb49012011-04-17 17:01:05 -07001005 return ret;
James Chapman309795f2010-04-02 06:19:10 +00001006}
1007EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
1008
1009void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
1010{
1011 if (pw_type < __L2TP_PWTYPE_MAX) {
1012 genl_lock();
1013 l2tp_nl_cmd_ops[pw_type] = NULL;
1014 genl_unlock();
1015 }
1016}
1017EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
1018
Johannes Berg56989f62016-10-24 14:40:05 +02001019static int __init l2tp_nl_init(void)
James Chapman309795f2010-04-02 06:19:10 +00001020{
Joe Perchesa4ca44f2012-05-16 09:55:56 +00001021 pr_info("L2TP netlink interface\n");
Johannes Berg489111e2016-10-24 14:40:03 +02001022 return genl_register_family(&l2tp_nl_family);
James Chapman309795f2010-04-02 06:19:10 +00001023}
1024
1025static void l2tp_nl_cleanup(void)
1026{
1027 genl_unregister_family(&l2tp_nl_family);
1028}
1029
1030module_init(l2tp_nl_init);
1031module_exit(l2tp_nl_cleanup);
1032
1033MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1034MODULE_DESCRIPTION("L2TP netlink");
1035MODULE_LICENSE("GPL");
1036MODULE_VERSION("1.0");
Neil Hormane9412c32012-05-29 09:30:41 +00001037MODULE_ALIAS_GENL_FAMILY("l2tp");