Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Apr 14-17, 2025
Register
Loading...
v3.1
  1/*
  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
 
 
 17#include <net/sock.h>
 18#include <net/genetlink.h>
 19#include <net/udp.h>
 20#include <linux/in.h>
 21#include <linux/udp.h>
 22#include <linux/socket.h>
 23#include <linux/module.h>
 24#include <linux/list.h>
 25#include <net/net_namespace.h>
 26
 27#include <linux/l2tp.h>
 28
 29#include "l2tp_core.h"
 30
 31
 32static struct genl_family l2tp_nl_family = {
 33	.id		= GENL_ID_GENERATE,
 34	.name		= L2TP_GENL_NAME,
 35	.version	= L2TP_GENL_VERSION,
 36	.hdrsize	= 0,
 37	.maxattr	= L2TP_ATTR_MAX,
 38};
 39
 40/* Accessed under genl lock */
 41static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX];
 42
 43static struct l2tp_session *l2tp_nl_session_find(struct genl_info *info)
 44{
 45	u32 tunnel_id;
 46	u32 session_id;
 47	char *ifname;
 48	struct l2tp_tunnel *tunnel;
 49	struct l2tp_session *session = NULL;
 50	struct net *net = genl_info_net(info);
 51
 52	if (info->attrs[L2TP_ATTR_IFNAME]) {
 53		ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
 54		session = l2tp_session_find_by_ifname(net, ifname);
 55	} else if ((info->attrs[L2TP_ATTR_SESSION_ID]) &&
 56		   (info->attrs[L2TP_ATTR_CONN_ID])) {
 57		tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
 58		session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
 59		tunnel = l2tp_tunnel_find(net, tunnel_id);
 60		if (tunnel)
 61			session = l2tp_session_find(net, tunnel, session_id);
 62	}
 63
 64	return session;
 65}
 66
 67static int l2tp_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
 68{
 69	struct sk_buff *msg;
 70	void *hdr;
 71	int ret = -ENOBUFS;
 72
 73	msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
 74	if (!msg) {
 75		ret = -ENOMEM;
 76		goto out;
 77	}
 78
 79	hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
 80			  &l2tp_nl_family, 0, L2TP_CMD_NOOP);
 81	if (IS_ERR(hdr)) {
 82		ret = PTR_ERR(hdr);
 83		goto err_out;
 84	}
 85
 86	genlmsg_end(msg, hdr);
 87
 88	return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
 89
 90err_out:
 91	nlmsg_free(msg);
 92
 93out:
 94	return ret;
 95}
 96
 97static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info)
 98{
 99	u32 tunnel_id;
100	u32 peer_tunnel_id;
101	int proto_version;
102	int fd;
103	int ret = 0;
104	struct l2tp_tunnel_cfg cfg = { 0, };
105	struct l2tp_tunnel *tunnel;
106	struct net *net = genl_info_net(info);
107
108	if (!info->attrs[L2TP_ATTR_CONN_ID]) {
109		ret = -EINVAL;
110		goto out;
111	}
112	tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
113
114	if (!info->attrs[L2TP_ATTR_PEER_CONN_ID]) {
115		ret = -EINVAL;
116		goto out;
117	}
118	peer_tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_CONN_ID]);
119
120	if (!info->attrs[L2TP_ATTR_PROTO_VERSION]) {
121		ret = -EINVAL;
122		goto out;
123	}
124	proto_version = nla_get_u8(info->attrs[L2TP_ATTR_PROTO_VERSION]);
125
126	if (!info->attrs[L2TP_ATTR_ENCAP_TYPE]) {
127		ret = -EINVAL;
128		goto out;
129	}
130	cfg.encap = nla_get_u16(info->attrs[L2TP_ATTR_ENCAP_TYPE]);
131
132	fd = -1;
133	if (info->attrs[L2TP_ATTR_FD]) {
134		fd = nla_get_u32(info->attrs[L2TP_ATTR_FD]);
135	} else {
136		if (info->attrs[L2TP_ATTR_IP_SADDR])
137			cfg.local_ip.s_addr = nla_get_be32(info->attrs[L2TP_ATTR_IP_SADDR]);
138		if (info->attrs[L2TP_ATTR_IP_DADDR])
139			cfg.peer_ip.s_addr = nla_get_be32(info->attrs[L2TP_ATTR_IP_DADDR]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140		if (info->attrs[L2TP_ATTR_UDP_SPORT])
141			cfg.local_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_SPORT]);
142		if (info->attrs[L2TP_ATTR_UDP_DPORT])
143			cfg.peer_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_DPORT]);
144		if (info->attrs[L2TP_ATTR_UDP_CSUM])
145			cfg.use_udp_checksums = nla_get_flag(info->attrs[L2TP_ATTR_UDP_CSUM]);
146	}
147
148	if (info->attrs[L2TP_ATTR_DEBUG])
149		cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
150
151	tunnel = l2tp_tunnel_find(net, tunnel_id);
152	if (tunnel != NULL) {
153		ret = -EEXIST;
154		goto out;
155	}
156
157	ret = -EINVAL;
158	switch (cfg.encap) {
159	case L2TP_ENCAPTYPE_UDP:
160	case L2TP_ENCAPTYPE_IP:
161		ret = l2tp_tunnel_create(net, fd, proto_version, tunnel_id,
162					 peer_tunnel_id, &cfg, &tunnel);
163		break;
164	}
165
166out:
167	return ret;
168}
169
170static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info)
171{
172	struct l2tp_tunnel *tunnel;
173	u32 tunnel_id;
174	int ret = 0;
175	struct net *net = genl_info_net(info);
176
177	if (!info->attrs[L2TP_ATTR_CONN_ID]) {
178		ret = -EINVAL;
179		goto out;
180	}
181	tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
182
183	tunnel = l2tp_tunnel_find(net, tunnel_id);
184	if (tunnel == NULL) {
185		ret = -ENODEV;
186		goto out;
187	}
188
189	(void) l2tp_tunnel_delete(tunnel);
190
191out:
192	return ret;
193}
194
195static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info)
196{
197	struct l2tp_tunnel *tunnel;
198	u32 tunnel_id;
199	int ret = 0;
200	struct net *net = genl_info_net(info);
201
202	if (!info->attrs[L2TP_ATTR_CONN_ID]) {
203		ret = -EINVAL;
204		goto out;
205	}
206	tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
207
208	tunnel = l2tp_tunnel_find(net, tunnel_id);
209	if (tunnel == NULL) {
210		ret = -ENODEV;
211		goto out;
212	}
213
214	if (info->attrs[L2TP_ATTR_DEBUG])
215		tunnel->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
216
217out:
218	return ret;
219}
220
221static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 pid, u32 seq, int flags,
222			       struct l2tp_tunnel *tunnel)
223{
224	void *hdr;
225	struct nlattr *nest;
226	struct sock *sk = NULL;
227	struct inet_sock *inet;
 
 
 
 
 
228
229	hdr = genlmsg_put(skb, pid, seq, &l2tp_nl_family, flags,
230			  L2TP_CMD_TUNNEL_GET);
231	if (IS_ERR(hdr))
232		return PTR_ERR(hdr);
233
234	NLA_PUT_U8(skb, L2TP_ATTR_PROTO_VERSION, tunnel->version);
235	NLA_PUT_U32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id);
236	NLA_PUT_U32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id);
237	NLA_PUT_U32(skb, L2TP_ATTR_DEBUG, tunnel->debug);
238	NLA_PUT_U16(skb, L2TP_ATTR_ENCAP_TYPE, tunnel->encap);
 
239
240	nest = nla_nest_start(skb, L2TP_ATTR_STATS);
241	if (nest == NULL)
242		goto nla_put_failure;
243
244	NLA_PUT_U64(skb, L2TP_ATTR_TX_PACKETS, tunnel->stats.tx_packets);
245	NLA_PUT_U64(skb, L2TP_ATTR_TX_BYTES, tunnel->stats.tx_bytes);
246	NLA_PUT_U64(skb, L2TP_ATTR_TX_ERRORS, tunnel->stats.tx_errors);
247	NLA_PUT_U64(skb, L2TP_ATTR_RX_PACKETS, tunnel->stats.rx_packets);
248	NLA_PUT_U64(skb, L2TP_ATTR_RX_BYTES, tunnel->stats.rx_bytes);
249	NLA_PUT_U64(skb, L2TP_ATTR_RX_SEQ_DISCARDS, tunnel->stats.rx_seq_discards);
250	NLA_PUT_U64(skb, L2TP_ATTR_RX_OOS_PACKETS, tunnel->stats.rx_oos_packets);
251	NLA_PUT_U64(skb, L2TP_ATTR_RX_ERRORS, tunnel->stats.rx_errors);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252	nla_nest_end(skb, nest);
253
254	sk = tunnel->sock;
255	if (!sk)
256		goto out;
257
 
 
 
 
 
258	inet = inet_sk(sk);
259
260	switch (tunnel->encap) {
261	case L2TP_ENCAPTYPE_UDP:
262		NLA_PUT_U16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport));
263		NLA_PUT_U16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport));
264		NLA_PUT_U8(skb, L2TP_ATTR_UDP_CSUM, (sk->sk_no_check != UDP_CSUM_NOXMIT));
 
 
265		/* NOBREAK */
266	case L2TP_ENCAPTYPE_IP:
267		NLA_PUT_BE32(skb, L2TP_ATTR_IP_SADDR, inet->inet_saddr);
268		NLA_PUT_BE32(skb, L2TP_ATTR_IP_DADDR, inet->inet_daddr);
 
 
 
 
 
 
 
 
 
 
269		break;
270	}
271
272out:
273	return genlmsg_end(skb, hdr);
274
275nla_put_failure:
276	genlmsg_cancel(skb, hdr);
277	return -1;
278}
279
280static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
281{
282	struct l2tp_tunnel *tunnel;
283	struct sk_buff *msg;
284	u32 tunnel_id;
285	int ret = -ENOBUFS;
286	struct net *net = genl_info_net(info);
287
288	if (!info->attrs[L2TP_ATTR_CONN_ID]) {
289		ret = -EINVAL;
290		goto out;
291	}
292
293	tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
294
295	tunnel = l2tp_tunnel_find(net, tunnel_id);
296	if (tunnel == NULL) {
297		ret = -ENODEV;
298		goto out;
299	}
300
301	msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
302	if (!msg) {
303		ret = -ENOMEM;
304		goto out;
305	}
306
307	ret = l2tp_nl_tunnel_send(msg, info->snd_pid, info->snd_seq,
308				  NLM_F_ACK, tunnel);
309	if (ret < 0)
310		goto err_out;
311
312	return genlmsg_unicast(net, msg, info->snd_pid);
313
314err_out:
315	nlmsg_free(msg);
316
317out:
318	return ret;
319}
320
321static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
322{
323	int ti = cb->args[0];
324	struct l2tp_tunnel *tunnel;
325	struct net *net = sock_net(skb->sk);
326
327	for (;;) {
328		tunnel = l2tp_tunnel_find_nth(net, ti);
329		if (tunnel == NULL)
330			goto out;
331
332		if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).pid,
333					cb->nlh->nlmsg_seq, NLM_F_MULTI,
334					tunnel) <= 0)
335			goto out;
336
337		ti++;
338	}
339
340out:
341	cb->args[0] = ti;
342
343	return skb->len;
344}
345
346static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
347{
348	u32 tunnel_id = 0;
349	u32 session_id;
350	u32 peer_session_id;
351	int ret = 0;
352	struct l2tp_tunnel *tunnel;
353	struct l2tp_session *session;
354	struct l2tp_session_cfg cfg = { 0, };
355	struct net *net = genl_info_net(info);
356
357	if (!info->attrs[L2TP_ATTR_CONN_ID]) {
358		ret = -EINVAL;
359		goto out;
360	}
361	tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
362	tunnel = l2tp_tunnel_find(net, tunnel_id);
363	if (!tunnel) {
364		ret = -ENODEV;
365		goto out;
366	}
367
368	if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
369		ret = -EINVAL;
370		goto out;
371	}
372	session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
373	session = l2tp_session_find(net, tunnel, session_id);
374	if (session) {
375		ret = -EEXIST;
376		goto out;
377	}
378
379	if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
380		ret = -EINVAL;
381		goto out;
382	}
383	peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
384
385	if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
386		ret = -EINVAL;
387		goto out;
388	}
389	cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
390	if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
391		ret = -EINVAL;
392		goto out;
393	}
394
395	if (tunnel->version > 2) {
396		if (info->attrs[L2TP_ATTR_OFFSET])
397			cfg.offset = nla_get_u16(info->attrs[L2TP_ATTR_OFFSET]);
398
399		if (info->attrs[L2TP_ATTR_DATA_SEQ])
400			cfg.data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
401
402		cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
403		if (info->attrs[L2TP_ATTR_L2SPEC_TYPE])
404			cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
405
406		cfg.l2specific_len = 4;
407		if (info->attrs[L2TP_ATTR_L2SPEC_LEN])
408			cfg.l2specific_len = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_LEN]);
409
410		if (info->attrs[L2TP_ATTR_COOKIE]) {
411			u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
412			if (len > 8) {
413				ret = -EINVAL;
414				goto out;
415			}
416			cfg.cookie_len = len;
417			memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
418		}
419		if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
420			u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
421			if (len > 8) {
422				ret = -EINVAL;
423				goto out;
424			}
425			cfg.peer_cookie_len = len;
426			memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
427		}
428		if (info->attrs[L2TP_ATTR_IFNAME])
429			cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
430
431		if (info->attrs[L2TP_ATTR_VLAN_ID])
432			cfg.vlan_id = nla_get_u16(info->attrs[L2TP_ATTR_VLAN_ID]);
433	}
434
435	if (info->attrs[L2TP_ATTR_DEBUG])
436		cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
437
438	if (info->attrs[L2TP_ATTR_RECV_SEQ])
439		cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
440
441	if (info->attrs[L2TP_ATTR_SEND_SEQ])
442		cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
443
444	if (info->attrs[L2TP_ATTR_LNS_MODE])
445		cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
446
447	if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
448		cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
449
450	if (info->attrs[L2TP_ATTR_MTU])
451		cfg.mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
452
453	if (info->attrs[L2TP_ATTR_MRU])
454		cfg.mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
455
456	if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) ||
457	    (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) {
458		ret = -EPROTONOSUPPORT;
459		goto out;
460	}
461
462	/* Check that pseudowire-specific params are present */
463	switch (cfg.pw_type) {
464	case L2TP_PWTYPE_NONE:
465		break;
466	case L2TP_PWTYPE_ETH_VLAN:
467		if (!info->attrs[L2TP_ATTR_VLAN_ID]) {
468			ret = -EINVAL;
469			goto out;
470		}
471		break;
472	case L2TP_PWTYPE_ETH:
473		break;
474	case L2TP_PWTYPE_PPP:
475	case L2TP_PWTYPE_PPP_AC:
476		break;
477	case L2TP_PWTYPE_IP:
478	default:
479		ret = -EPROTONOSUPPORT;
480		break;
481	}
482
483	ret = -EPROTONOSUPPORT;
484	if (l2tp_nl_cmd_ops[cfg.pw_type]->session_create)
485		ret = (*l2tp_nl_cmd_ops[cfg.pw_type]->session_create)(net, tunnel_id,
486			session_id, peer_session_id, &cfg);
487
488out:
489	return ret;
490}
491
492static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
493{
494	int ret = 0;
495	struct l2tp_session *session;
496	u16 pw_type;
497
498	session = l2tp_nl_session_find(info);
499	if (session == NULL) {
500		ret = -ENODEV;
501		goto out;
502	}
503
504	pw_type = session->pwtype;
505	if (pw_type < __L2TP_PWTYPE_MAX)
506		if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
507			ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
508
509out:
510	return ret;
511}
512
513static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
514{
515	int ret = 0;
516	struct l2tp_session *session;
517
518	session = l2tp_nl_session_find(info);
519	if (session == NULL) {
520		ret = -ENODEV;
521		goto out;
522	}
523
524	if (info->attrs[L2TP_ATTR_DEBUG])
525		session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
526
527	if (info->attrs[L2TP_ATTR_DATA_SEQ])
528		session->data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
529
530	if (info->attrs[L2TP_ATTR_RECV_SEQ])
531		session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
532
533	if (info->attrs[L2TP_ATTR_SEND_SEQ])
534		session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
535
536	if (info->attrs[L2TP_ATTR_LNS_MODE])
537		session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
538
539	if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
540		session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
541
542	if (info->attrs[L2TP_ATTR_MTU])
543		session->mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
544
545	if (info->attrs[L2TP_ATTR_MRU])
546		session->mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
547
548out:
549	return ret;
550}
551
552static int l2tp_nl_session_send(struct sk_buff *skb, u32 pid, u32 seq, int flags,
553				struct l2tp_session *session)
554{
555	void *hdr;
556	struct nlattr *nest;
557	struct l2tp_tunnel *tunnel = session->tunnel;
558	struct sock *sk = NULL;
 
 
559
560	sk = tunnel->sock;
561
562	hdr = genlmsg_put(skb, pid, seq, &l2tp_nl_family, flags, L2TP_CMD_SESSION_GET);
563	if (IS_ERR(hdr))
564		return PTR_ERR(hdr);
565
566	NLA_PUT_U32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id);
567	NLA_PUT_U32(skb, L2TP_ATTR_SESSION_ID, session->session_id);
568	NLA_PUT_U32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id);
569	NLA_PUT_U32(skb, L2TP_ATTR_PEER_SESSION_ID, session->peer_session_id);
570	NLA_PUT_U32(skb, L2TP_ATTR_DEBUG, session->debug);
571	NLA_PUT_U16(skb, L2TP_ATTR_PW_TYPE, session->pwtype);
572	NLA_PUT_U16(skb, L2TP_ATTR_MTU, session->mtu);
573	if (session->mru)
574		NLA_PUT_U16(skb, L2TP_ATTR_MRU, session->mru);
575
576	if (session->ifname && session->ifname[0])
577		NLA_PUT_STRING(skb, L2TP_ATTR_IFNAME, session->ifname);
578	if (session->cookie_len)
579		NLA_PUT(skb, L2TP_ATTR_COOKIE, session->cookie_len, &session->cookie[0]);
580	if (session->peer_cookie_len)
581		NLA_PUT(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len, &session->peer_cookie[0]);
582	NLA_PUT_U8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq);
583	NLA_PUT_U8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq);
584	NLA_PUT_U8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode);
 
 
 
 
585#ifdef CONFIG_XFRM
586	if ((sk) && (sk->sk_policy[0] || sk->sk_policy[1]))
587		NLA_PUT_U8(skb, L2TP_ATTR_USING_IPSEC, 1);
588#endif
589	if (session->reorder_timeout)
590		NLA_PUT_MSECS(skb, L2TP_ATTR_RECV_TIMEOUT, session->reorder_timeout);
 
591
592	nest = nla_nest_start(skb, L2TP_ATTR_STATS);
593	if (nest == NULL)
594		goto nla_put_failure;
595	NLA_PUT_U64(skb, L2TP_ATTR_TX_PACKETS, session->stats.tx_packets);
596	NLA_PUT_U64(skb, L2TP_ATTR_TX_BYTES, session->stats.tx_bytes);
597	NLA_PUT_U64(skb, L2TP_ATTR_TX_ERRORS, session->stats.tx_errors);
598	NLA_PUT_U64(skb, L2TP_ATTR_RX_PACKETS, session->stats.rx_packets);
599	NLA_PUT_U64(skb, L2TP_ATTR_RX_BYTES, session->stats.rx_bytes);
600	NLA_PUT_U64(skb, L2TP_ATTR_RX_SEQ_DISCARDS, session->stats.rx_seq_discards);
601	NLA_PUT_U64(skb, L2TP_ATTR_RX_OOS_PACKETS, session->stats.rx_oos_packets);
602	NLA_PUT_U64(skb, L2TP_ATTR_RX_ERRORS, session->stats.rx_errors);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
603	nla_nest_end(skb, nest);
604
605	return genlmsg_end(skb, hdr);
606
607 nla_put_failure:
608	genlmsg_cancel(skb, hdr);
609	return -1;
610}
611
612static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
613{
614	struct l2tp_session *session;
615	struct sk_buff *msg;
616	int ret;
617
618	session = l2tp_nl_session_find(info);
619	if (session == NULL) {
620		ret = -ENODEV;
621		goto out;
622	}
623
624	msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
625	if (!msg) {
626		ret = -ENOMEM;
627		goto out;
628	}
629
630	ret = l2tp_nl_session_send(msg, info->snd_pid, info->snd_seq,
631				   0, session);
632	if (ret < 0)
633		goto err_out;
634
635	return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
636
637err_out:
638	nlmsg_free(msg);
639
640out:
641	return ret;
642}
643
644static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
645{
646	struct net *net = sock_net(skb->sk);
647	struct l2tp_session *session;
648	struct l2tp_tunnel *tunnel = NULL;
649	int ti = cb->args[0];
650	int si = cb->args[1];
651
652	for (;;) {
653		if (tunnel == NULL) {
654			tunnel = l2tp_tunnel_find_nth(net, ti);
655			if (tunnel == NULL)
656				goto out;
657		}
658
659		session = l2tp_session_find_nth(tunnel, si);
660		if (session == NULL) {
661			ti++;
662			tunnel = NULL;
663			si = 0;
664			continue;
665		}
666
667		if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).pid,
668					 cb->nlh->nlmsg_seq, NLM_F_MULTI,
669					 session) <= 0)
670			break;
671
672		si++;
673	}
674
675out:
676	cb->args[0] = ti;
677	cb->args[1] = si;
678
679	return skb->len;
680}
681
682static struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
683	[L2TP_ATTR_NONE]		= { .type = NLA_UNSPEC, },
684	[L2TP_ATTR_PW_TYPE]		= { .type = NLA_U16, },
685	[L2TP_ATTR_ENCAP_TYPE]		= { .type = NLA_U16, },
686	[L2TP_ATTR_OFFSET]		= { .type = NLA_U16, },
687	[L2TP_ATTR_DATA_SEQ]		= { .type = NLA_U8, },
688	[L2TP_ATTR_L2SPEC_TYPE]		= { .type = NLA_U8, },
689	[L2TP_ATTR_L2SPEC_LEN]		= { .type = NLA_U8, },
690	[L2TP_ATTR_PROTO_VERSION]	= { .type = NLA_U8, },
691	[L2TP_ATTR_CONN_ID]		= { .type = NLA_U32, },
692	[L2TP_ATTR_PEER_CONN_ID]	= { .type = NLA_U32, },
693	[L2TP_ATTR_SESSION_ID]		= { .type = NLA_U32, },
694	[L2TP_ATTR_PEER_SESSION_ID]	= { .type = NLA_U32, },
695	[L2TP_ATTR_UDP_CSUM]		= { .type = NLA_U8, },
696	[L2TP_ATTR_VLAN_ID]		= { .type = NLA_U16, },
697	[L2TP_ATTR_DEBUG]		= { .type = NLA_U32, },
698	[L2TP_ATTR_RECV_SEQ]		= { .type = NLA_U8, },
699	[L2TP_ATTR_SEND_SEQ]		= { .type = NLA_U8, },
700	[L2TP_ATTR_LNS_MODE]		= { .type = NLA_U8, },
701	[L2TP_ATTR_USING_IPSEC]		= { .type = NLA_U8, },
702	[L2TP_ATTR_RECV_TIMEOUT]	= { .type = NLA_MSECS, },
703	[L2TP_ATTR_FD]			= { .type = NLA_U32, },
704	[L2TP_ATTR_IP_SADDR]		= { .type = NLA_U32, },
705	[L2TP_ATTR_IP_DADDR]		= { .type = NLA_U32, },
706	[L2TP_ATTR_UDP_SPORT]		= { .type = NLA_U16, },
707	[L2TP_ATTR_UDP_DPORT]		= { .type = NLA_U16, },
708	[L2TP_ATTR_MTU]			= { .type = NLA_U16, },
709	[L2TP_ATTR_MRU]			= { .type = NLA_U16, },
710	[L2TP_ATTR_STATS]		= { .type = NLA_NESTED, },
 
 
 
 
 
 
 
 
711	[L2TP_ATTR_IFNAME] = {
712		.type = NLA_NUL_STRING,
713		.len = IFNAMSIZ - 1,
714	},
715	[L2TP_ATTR_COOKIE] = {
716		.type = NLA_BINARY,
717		.len = 8,
718	},
719	[L2TP_ATTR_PEER_COOKIE] = {
720		.type = NLA_BINARY,
721		.len = 8,
722	},
723};
724
725static struct genl_ops l2tp_nl_ops[] = {
726	{
727		.cmd = L2TP_CMD_NOOP,
728		.doit = l2tp_nl_cmd_noop,
729		.policy = l2tp_nl_policy,
730		/* can be retrieved by unprivileged users */
731	},
732	{
733		.cmd = L2TP_CMD_TUNNEL_CREATE,
734		.doit = l2tp_nl_cmd_tunnel_create,
735		.policy = l2tp_nl_policy,
736		.flags = GENL_ADMIN_PERM,
737	},
738	{
739		.cmd = L2TP_CMD_TUNNEL_DELETE,
740		.doit = l2tp_nl_cmd_tunnel_delete,
741		.policy = l2tp_nl_policy,
742		.flags = GENL_ADMIN_PERM,
743	},
744	{
745		.cmd = L2TP_CMD_TUNNEL_MODIFY,
746		.doit = l2tp_nl_cmd_tunnel_modify,
747		.policy = l2tp_nl_policy,
748		.flags = GENL_ADMIN_PERM,
749	},
750	{
751		.cmd = L2TP_CMD_TUNNEL_GET,
752		.doit = l2tp_nl_cmd_tunnel_get,
753		.dumpit = l2tp_nl_cmd_tunnel_dump,
754		.policy = l2tp_nl_policy,
755		.flags = GENL_ADMIN_PERM,
756	},
757	{
758		.cmd = L2TP_CMD_SESSION_CREATE,
759		.doit = l2tp_nl_cmd_session_create,
760		.policy = l2tp_nl_policy,
761		.flags = GENL_ADMIN_PERM,
762	},
763	{
764		.cmd = L2TP_CMD_SESSION_DELETE,
765		.doit = l2tp_nl_cmd_session_delete,
766		.policy = l2tp_nl_policy,
767		.flags = GENL_ADMIN_PERM,
768	},
769	{
770		.cmd = L2TP_CMD_SESSION_MODIFY,
771		.doit = l2tp_nl_cmd_session_modify,
772		.policy = l2tp_nl_policy,
773		.flags = GENL_ADMIN_PERM,
774	},
775	{
776		.cmd = L2TP_CMD_SESSION_GET,
777		.doit = l2tp_nl_cmd_session_get,
778		.dumpit = l2tp_nl_cmd_session_dump,
779		.policy = l2tp_nl_policy,
780		.flags = GENL_ADMIN_PERM,
781	},
782};
783
784int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
785{
786	int ret;
787
788	ret = -EINVAL;
789	if (pw_type >= __L2TP_PWTYPE_MAX)
790		goto err;
791
792	genl_lock();
793	ret = -EBUSY;
794	if (l2tp_nl_cmd_ops[pw_type])
795		goto out;
796
797	l2tp_nl_cmd_ops[pw_type] = ops;
798	ret = 0;
799
800out:
801	genl_unlock();
802err:
803	return ret;
804}
805EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
806
807void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
808{
809	if (pw_type < __L2TP_PWTYPE_MAX) {
810		genl_lock();
811		l2tp_nl_cmd_ops[pw_type] = NULL;
812		genl_unlock();
813	}
814}
815EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
816
817static int l2tp_nl_init(void)
818{
819	int err;
820
821	printk(KERN_INFO "L2TP netlink interface\n");
822	err = genl_register_family_with_ops(&l2tp_nl_family, l2tp_nl_ops,
823					    ARRAY_SIZE(l2tp_nl_ops));
824
825	return err;
826}
827
828static void l2tp_nl_cleanup(void)
829{
830	genl_unregister_family(&l2tp_nl_family);
831}
832
833module_init(l2tp_nl_init);
834module_exit(l2tp_nl_cleanup);
835
836MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
837MODULE_DESCRIPTION("L2TP netlink");
838MODULE_LICENSE("GPL");
839MODULE_VERSION("1.0");
840MODULE_ALIAS("net-pf-" __stringify(PF_NETLINK) "-proto-" \
841	     __stringify(NETLINK_GENERIC) "-type-" "l2tp");
v3.5.6
  1/*
  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
 17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 18
 19#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
 34static struct genl_family l2tp_nl_family = {
 35	.id		= GENL_ID_GENERATE,
 36	.name		= L2TP_GENL_NAME,
 37	.version	= L2TP_GENL_VERSION,
 38	.hdrsize	= 0,
 39	.maxattr	= L2TP_ATTR_MAX,
 40};
 41
 42/* Accessed under genl lock */
 43static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX];
 44
 45static struct l2tp_session *l2tp_nl_session_find(struct genl_info *info)
 46{
 47	u32 tunnel_id;
 48	u32 session_id;
 49	char *ifname;
 50	struct l2tp_tunnel *tunnel;
 51	struct l2tp_session *session = NULL;
 52	struct net *net = genl_info_net(info);
 53
 54	if (info->attrs[L2TP_ATTR_IFNAME]) {
 55		ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
 56		session = l2tp_session_find_by_ifname(net, ifname);
 57	} else if ((info->attrs[L2TP_ATTR_SESSION_ID]) &&
 58		   (info->attrs[L2TP_ATTR_CONN_ID])) {
 59		tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
 60		session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
 61		tunnel = l2tp_tunnel_find(net, tunnel_id);
 62		if (tunnel)
 63			session = l2tp_session_find(net, tunnel, session_id);
 64	}
 65
 66	return session;
 67}
 68
 69static int l2tp_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
 70{
 71	struct sk_buff *msg;
 72	void *hdr;
 73	int ret = -ENOBUFS;
 74
 75	msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
 76	if (!msg) {
 77		ret = -ENOMEM;
 78		goto out;
 79	}
 80
 81	hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
 82			  &l2tp_nl_family, 0, L2TP_CMD_NOOP);
 83	if (IS_ERR(hdr)) {
 84		ret = PTR_ERR(hdr);
 85		goto err_out;
 86	}
 87
 88	genlmsg_end(msg, hdr);
 89
 90	return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
 91
 92err_out:
 93	nlmsg_free(msg);
 94
 95out:
 96	return ret;
 97}
 98
 99static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info)
100{
101	u32 tunnel_id;
102	u32 peer_tunnel_id;
103	int proto_version;
104	int fd;
105	int ret = 0;
106	struct l2tp_tunnel_cfg cfg = { 0, };
107	struct l2tp_tunnel *tunnel;
108	struct net *net = genl_info_net(info);
109
110	if (!info->attrs[L2TP_ATTR_CONN_ID]) {
111		ret = -EINVAL;
112		goto out;
113	}
114	tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
115
116	if (!info->attrs[L2TP_ATTR_PEER_CONN_ID]) {
117		ret = -EINVAL;
118		goto out;
119	}
120	peer_tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_CONN_ID]);
121
122	if (!info->attrs[L2TP_ATTR_PROTO_VERSION]) {
123		ret = -EINVAL;
124		goto out;
125	}
126	proto_version = nla_get_u8(info->attrs[L2TP_ATTR_PROTO_VERSION]);
127
128	if (!info->attrs[L2TP_ATTR_ENCAP_TYPE]) {
129		ret = -EINVAL;
130		goto out;
131	}
132	cfg.encap = nla_get_u16(info->attrs[L2TP_ATTR_ENCAP_TYPE]);
133
134	fd = -1;
135	if (info->attrs[L2TP_ATTR_FD]) {
136		fd = nla_get_u32(info->attrs[L2TP_ATTR_FD]);
137	} else {
138#if IS_ENABLED(CONFIG_IPV6)
139		if (info->attrs[L2TP_ATTR_IP6_SADDR] &&
140		    info->attrs[L2TP_ATTR_IP6_DADDR]) {
141			cfg.local_ip6 = nla_data(
142				info->attrs[L2TP_ATTR_IP6_SADDR]);
143			cfg.peer_ip6 = nla_data(
144				info->attrs[L2TP_ATTR_IP6_DADDR]);
145		} else
146#endif
147		if (info->attrs[L2TP_ATTR_IP_SADDR] &&
148		    info->attrs[L2TP_ATTR_IP_DADDR]) {
149			cfg.local_ip.s_addr = nla_get_be32(
150				info->attrs[L2TP_ATTR_IP_SADDR]);
151			cfg.peer_ip.s_addr = nla_get_be32(
152				info->attrs[L2TP_ATTR_IP_DADDR]);
153		} else {
154			ret = -EINVAL;
155			goto out;
156		}
157		if (info->attrs[L2TP_ATTR_UDP_SPORT])
158			cfg.local_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_SPORT]);
159		if (info->attrs[L2TP_ATTR_UDP_DPORT])
160			cfg.peer_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_DPORT]);
161		if (info->attrs[L2TP_ATTR_UDP_CSUM])
162			cfg.use_udp_checksums = nla_get_flag(info->attrs[L2TP_ATTR_UDP_CSUM]);
163	}
164
165	if (info->attrs[L2TP_ATTR_DEBUG])
166		cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
167
168	tunnel = l2tp_tunnel_find(net, tunnel_id);
169	if (tunnel != NULL) {
170		ret = -EEXIST;
171		goto out;
172	}
173
174	ret = -EINVAL;
175	switch (cfg.encap) {
176	case L2TP_ENCAPTYPE_UDP:
177	case L2TP_ENCAPTYPE_IP:
178		ret = l2tp_tunnel_create(net, fd, proto_version, tunnel_id,
179					 peer_tunnel_id, &cfg, &tunnel);
180		break;
181	}
182
183out:
184	return ret;
185}
186
187static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info)
188{
189	struct l2tp_tunnel *tunnel;
190	u32 tunnel_id;
191	int ret = 0;
192	struct net *net = genl_info_net(info);
193
194	if (!info->attrs[L2TP_ATTR_CONN_ID]) {
195		ret = -EINVAL;
196		goto out;
197	}
198	tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
199
200	tunnel = l2tp_tunnel_find(net, tunnel_id);
201	if (tunnel == NULL) {
202		ret = -ENODEV;
203		goto out;
204	}
205
206	(void) l2tp_tunnel_delete(tunnel);
207
208out:
209	return ret;
210}
211
212static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info)
213{
214	struct l2tp_tunnel *tunnel;
215	u32 tunnel_id;
216	int ret = 0;
217	struct net *net = genl_info_net(info);
218
219	if (!info->attrs[L2TP_ATTR_CONN_ID]) {
220		ret = -EINVAL;
221		goto out;
222	}
223	tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
224
225	tunnel = l2tp_tunnel_find(net, tunnel_id);
226	if (tunnel == NULL) {
227		ret = -ENODEV;
228		goto out;
229	}
230
231	if (info->attrs[L2TP_ATTR_DEBUG])
232		tunnel->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
233
234out:
235	return ret;
236}
237
238static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 pid, u32 seq, int flags,
239			       struct l2tp_tunnel *tunnel)
240{
241	void *hdr;
242	struct nlattr *nest;
243	struct sock *sk = NULL;
244	struct inet_sock *inet;
245#if IS_ENABLED(CONFIG_IPV6)
246	struct ipv6_pinfo *np = NULL;
247#endif
248	struct l2tp_stats stats;
249	unsigned int start;
250
251	hdr = genlmsg_put(skb, pid, seq, &l2tp_nl_family, flags,
252			  L2TP_CMD_TUNNEL_GET);
253	if (IS_ERR(hdr))
254		return PTR_ERR(hdr);
255
256	if (nla_put_u8(skb, L2TP_ATTR_PROTO_VERSION, tunnel->version) ||
257	    nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
258	    nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
259	    nla_put_u32(skb, L2TP_ATTR_DEBUG, tunnel->debug) ||
260	    nla_put_u16(skb, L2TP_ATTR_ENCAP_TYPE, tunnel->encap))
261		goto nla_put_failure;
262
263	nest = nla_nest_start(skb, L2TP_ATTR_STATS);
264	if (nest == NULL)
265		goto nla_put_failure;
266
267	do {
268		start = u64_stats_fetch_begin(&tunnel->stats.syncp);
269		stats.tx_packets = tunnel->stats.tx_packets;
270		stats.tx_bytes = tunnel->stats.tx_bytes;
271		stats.tx_errors = tunnel->stats.tx_errors;
272		stats.rx_packets = tunnel->stats.rx_packets;
273		stats.rx_bytes = tunnel->stats.rx_bytes;
274		stats.rx_errors = tunnel->stats.rx_errors;
275		stats.rx_seq_discards = tunnel->stats.rx_seq_discards;
276		stats.rx_oos_packets = tunnel->stats.rx_oos_packets;
277	} while (u64_stats_fetch_retry(&tunnel->stats.syncp, start));
278
279	if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS, stats.tx_packets) ||
280	    nla_put_u64(skb, L2TP_ATTR_TX_BYTES, stats.tx_bytes) ||
281	    nla_put_u64(skb, L2TP_ATTR_TX_ERRORS, stats.tx_errors) ||
282	    nla_put_u64(skb, L2TP_ATTR_RX_PACKETS, stats.rx_packets) ||
283	    nla_put_u64(skb, L2TP_ATTR_RX_BYTES, stats.rx_bytes) ||
284	    nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
285			stats.rx_seq_discards) ||
286	    nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
287			stats.rx_oos_packets) ||
288	    nla_put_u64(skb, L2TP_ATTR_RX_ERRORS, stats.rx_errors))
289		goto nla_put_failure;
290	nla_nest_end(skb, nest);
291
292	sk = tunnel->sock;
293	if (!sk)
294		goto out;
295
296#if IS_ENABLED(CONFIG_IPV6)
297	if (sk->sk_family == AF_INET6)
298		np = inet6_sk(sk);
299#endif
300
301	inet = inet_sk(sk);
302
303	switch (tunnel->encap) {
304	case L2TP_ENCAPTYPE_UDP:
305		if (nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
306		    nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)) ||
307		    nla_put_u8(skb, L2TP_ATTR_UDP_CSUM,
308			       (sk->sk_no_check != UDP_CSUM_NOXMIT)))
309			goto nla_put_failure;
310		/* NOBREAK */
311	case L2TP_ENCAPTYPE_IP:
312#if IS_ENABLED(CONFIG_IPV6)
313		if (np) {
314			if (nla_put(skb, L2TP_ATTR_IP6_SADDR, sizeof(np->saddr),
315				    &np->saddr) ||
316			    nla_put(skb, L2TP_ATTR_IP6_DADDR, sizeof(np->daddr),
317				    &np->daddr))
318				goto nla_put_failure;
319		} else
320#endif
321		if (nla_put_be32(skb, L2TP_ATTR_IP_SADDR, inet->inet_saddr) ||
322		    nla_put_be32(skb, L2TP_ATTR_IP_DADDR, inet->inet_daddr))
323			goto nla_put_failure;
324		break;
325	}
326
327out:
328	return genlmsg_end(skb, hdr);
329
330nla_put_failure:
331	genlmsg_cancel(skb, hdr);
332	return -1;
333}
334
335static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
336{
337	struct l2tp_tunnel *tunnel;
338	struct sk_buff *msg;
339	u32 tunnel_id;
340	int ret = -ENOBUFS;
341	struct net *net = genl_info_net(info);
342
343	if (!info->attrs[L2TP_ATTR_CONN_ID]) {
344		ret = -EINVAL;
345		goto out;
346	}
347
348	tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
349
350	tunnel = l2tp_tunnel_find(net, tunnel_id);
351	if (tunnel == NULL) {
352		ret = -ENODEV;
353		goto out;
354	}
355
356	msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
357	if (!msg) {
358		ret = -ENOMEM;
359		goto out;
360	}
361
362	ret = l2tp_nl_tunnel_send(msg, info->snd_pid, info->snd_seq,
363				  NLM_F_ACK, tunnel);
364	if (ret < 0)
365		goto err_out;
366
367	return genlmsg_unicast(net, msg, info->snd_pid);
368
369err_out:
370	nlmsg_free(msg);
371
372out:
373	return ret;
374}
375
376static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
377{
378	int ti = cb->args[0];
379	struct l2tp_tunnel *tunnel;
380	struct net *net = sock_net(skb->sk);
381
382	for (;;) {
383		tunnel = l2tp_tunnel_find_nth(net, ti);
384		if (tunnel == NULL)
385			goto out;
386
387		if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).pid,
388					cb->nlh->nlmsg_seq, NLM_F_MULTI,
389					tunnel) <= 0)
390			goto out;
391
392		ti++;
393	}
394
395out:
396	cb->args[0] = ti;
397
398	return skb->len;
399}
400
401static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
402{
403	u32 tunnel_id = 0;
404	u32 session_id;
405	u32 peer_session_id;
406	int ret = 0;
407	struct l2tp_tunnel *tunnel;
408	struct l2tp_session *session;
409	struct l2tp_session_cfg cfg = { 0, };
410	struct net *net = genl_info_net(info);
411
412	if (!info->attrs[L2TP_ATTR_CONN_ID]) {
413		ret = -EINVAL;
414		goto out;
415	}
416	tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
417	tunnel = l2tp_tunnel_find(net, tunnel_id);
418	if (!tunnel) {
419		ret = -ENODEV;
420		goto out;
421	}
422
423	if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
424		ret = -EINVAL;
425		goto out;
426	}
427	session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
428	session = l2tp_session_find(net, tunnel, session_id);
429	if (session) {
430		ret = -EEXIST;
431		goto out;
432	}
433
434	if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
435		ret = -EINVAL;
436		goto out;
437	}
438	peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
439
440	if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
441		ret = -EINVAL;
442		goto out;
443	}
444	cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
445	if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
446		ret = -EINVAL;
447		goto out;
448	}
449
450	if (tunnel->version > 2) {
451		if (info->attrs[L2TP_ATTR_OFFSET])
452			cfg.offset = nla_get_u16(info->attrs[L2TP_ATTR_OFFSET]);
453
454		if (info->attrs[L2TP_ATTR_DATA_SEQ])
455			cfg.data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
456
457		cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
458		if (info->attrs[L2TP_ATTR_L2SPEC_TYPE])
459			cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
460
461		cfg.l2specific_len = 4;
462		if (info->attrs[L2TP_ATTR_L2SPEC_LEN])
463			cfg.l2specific_len = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_LEN]);
464
465		if (info->attrs[L2TP_ATTR_COOKIE]) {
466			u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
467			if (len > 8) {
468				ret = -EINVAL;
469				goto out;
470			}
471			cfg.cookie_len = len;
472			memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
473		}
474		if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
475			u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
476			if (len > 8) {
477				ret = -EINVAL;
478				goto out;
479			}
480			cfg.peer_cookie_len = len;
481			memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
482		}
483		if (info->attrs[L2TP_ATTR_IFNAME])
484			cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
485
486		if (info->attrs[L2TP_ATTR_VLAN_ID])
487			cfg.vlan_id = nla_get_u16(info->attrs[L2TP_ATTR_VLAN_ID]);
488	}
489
490	if (info->attrs[L2TP_ATTR_DEBUG])
491		cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
492
493	if (info->attrs[L2TP_ATTR_RECV_SEQ])
494		cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
495
496	if (info->attrs[L2TP_ATTR_SEND_SEQ])
497		cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
498
499	if (info->attrs[L2TP_ATTR_LNS_MODE])
500		cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
501
502	if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
503		cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
504
505	if (info->attrs[L2TP_ATTR_MTU])
506		cfg.mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
507
508	if (info->attrs[L2TP_ATTR_MRU])
509		cfg.mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
510
511	if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) ||
512	    (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) {
513		ret = -EPROTONOSUPPORT;
514		goto out;
515	}
516
517	/* Check that pseudowire-specific params are present */
518	switch (cfg.pw_type) {
519	case L2TP_PWTYPE_NONE:
520		break;
521	case L2TP_PWTYPE_ETH_VLAN:
522		if (!info->attrs[L2TP_ATTR_VLAN_ID]) {
523			ret = -EINVAL;
524			goto out;
525		}
526		break;
527	case L2TP_PWTYPE_ETH:
528		break;
529	case L2TP_PWTYPE_PPP:
530	case L2TP_PWTYPE_PPP_AC:
531		break;
532	case L2TP_PWTYPE_IP:
533	default:
534		ret = -EPROTONOSUPPORT;
535		break;
536	}
537
538	ret = -EPROTONOSUPPORT;
539	if (l2tp_nl_cmd_ops[cfg.pw_type]->session_create)
540		ret = (*l2tp_nl_cmd_ops[cfg.pw_type]->session_create)(net, tunnel_id,
541			session_id, peer_session_id, &cfg);
542
543out:
544	return ret;
545}
546
547static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
548{
549	int ret = 0;
550	struct l2tp_session *session;
551	u16 pw_type;
552
553	session = l2tp_nl_session_find(info);
554	if (session == NULL) {
555		ret = -ENODEV;
556		goto out;
557	}
558
559	pw_type = session->pwtype;
560	if (pw_type < __L2TP_PWTYPE_MAX)
561		if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
562			ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
563
564out:
565	return ret;
566}
567
568static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
569{
570	int ret = 0;
571	struct l2tp_session *session;
572
573	session = l2tp_nl_session_find(info);
574	if (session == NULL) {
575		ret = -ENODEV;
576		goto out;
577	}
578
579	if (info->attrs[L2TP_ATTR_DEBUG])
580		session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
581
582	if (info->attrs[L2TP_ATTR_DATA_SEQ])
583		session->data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
584
585	if (info->attrs[L2TP_ATTR_RECV_SEQ])
586		session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
587
588	if (info->attrs[L2TP_ATTR_SEND_SEQ])
589		session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
590
591	if (info->attrs[L2TP_ATTR_LNS_MODE])
592		session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
593
594	if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
595		session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
596
597	if (info->attrs[L2TP_ATTR_MTU])
598		session->mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
599
600	if (info->attrs[L2TP_ATTR_MRU])
601		session->mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
602
603out:
604	return ret;
605}
606
607static int l2tp_nl_session_send(struct sk_buff *skb, u32 pid, u32 seq, int flags,
608				struct l2tp_session *session)
609{
610	void *hdr;
611	struct nlattr *nest;
612	struct l2tp_tunnel *tunnel = session->tunnel;
613	struct sock *sk = NULL;
614	struct l2tp_stats stats;
615	unsigned int start;
616
617	sk = tunnel->sock;
618
619	hdr = genlmsg_put(skb, pid, seq, &l2tp_nl_family, flags, L2TP_CMD_SESSION_GET);
620	if (IS_ERR(hdr))
621		return PTR_ERR(hdr);
622
623	if (nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
624	    nla_put_u32(skb, L2TP_ATTR_SESSION_ID, session->session_id) ||
625	    nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
626	    nla_put_u32(skb, L2TP_ATTR_PEER_SESSION_ID,
627			session->peer_session_id) ||
628	    nla_put_u32(skb, L2TP_ATTR_DEBUG, session->debug) ||
629	    nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype) ||
630	    nla_put_u16(skb, L2TP_ATTR_MTU, session->mtu) ||
631	    (session->mru &&
632	     nla_put_u16(skb, L2TP_ATTR_MRU, session->mru)))
633		goto nla_put_failure;
634
635	if ((session->ifname && session->ifname[0] &&
636	     nla_put_string(skb, L2TP_ATTR_IFNAME, session->ifname)) ||
637	    (session->cookie_len &&
638	     nla_put(skb, L2TP_ATTR_COOKIE, session->cookie_len,
639		     &session->cookie[0])) ||
640	    (session->peer_cookie_len &&
641	     nla_put(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len,
642		     &session->peer_cookie[0])) ||
643	    nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
644	    nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
645	    nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
646#ifdef CONFIG_XFRM
647	    (((sk) && (sk->sk_policy[0] || sk->sk_policy[1])) &&
648	     nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
649#endif
650	    (session->reorder_timeout &&
651	     nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT, session->reorder_timeout)))
652		goto nla_put_failure;
653
654	nest = nla_nest_start(skb, L2TP_ATTR_STATS);
655	if (nest == NULL)
656		goto nla_put_failure;
657
658	do {
659		start = u64_stats_fetch_begin(&session->stats.syncp);
660		stats.tx_packets = session->stats.tx_packets;
661		stats.tx_bytes = session->stats.tx_bytes;
662		stats.tx_errors = session->stats.tx_errors;
663		stats.rx_packets = session->stats.rx_packets;
664		stats.rx_bytes = session->stats.rx_bytes;
665		stats.rx_errors = session->stats.rx_errors;
666		stats.rx_seq_discards = session->stats.rx_seq_discards;
667		stats.rx_oos_packets = session->stats.rx_oos_packets;
668	} while (u64_stats_fetch_retry(&session->stats.syncp, start));
669
670	if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS, stats.tx_packets) ||
671	    nla_put_u64(skb, L2TP_ATTR_TX_BYTES, stats.tx_bytes) ||
672	    nla_put_u64(skb, L2TP_ATTR_TX_ERRORS, stats.tx_errors) ||
673	    nla_put_u64(skb, L2TP_ATTR_RX_PACKETS, stats.rx_packets) ||
674	    nla_put_u64(skb, L2TP_ATTR_RX_BYTES, stats.rx_bytes) ||
675	    nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
676			stats.rx_seq_discards) ||
677	    nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
678			stats.rx_oos_packets) ||
679	    nla_put_u64(skb, L2TP_ATTR_RX_ERRORS, stats.rx_errors))
680		goto nla_put_failure;
681	nla_nest_end(skb, nest);
682
683	return genlmsg_end(skb, hdr);
684
685 nla_put_failure:
686	genlmsg_cancel(skb, hdr);
687	return -1;
688}
689
690static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
691{
692	struct l2tp_session *session;
693	struct sk_buff *msg;
694	int ret;
695
696	session = l2tp_nl_session_find(info);
697	if (session == NULL) {
698		ret = -ENODEV;
699		goto out;
700	}
701
702	msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
703	if (!msg) {
704		ret = -ENOMEM;
705		goto out;
706	}
707
708	ret = l2tp_nl_session_send(msg, info->snd_pid, info->snd_seq,
709				   0, session);
710	if (ret < 0)
711		goto err_out;
712
713	return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
714
715err_out:
716	nlmsg_free(msg);
717
718out:
719	return ret;
720}
721
722static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
723{
724	struct net *net = sock_net(skb->sk);
725	struct l2tp_session *session;
726	struct l2tp_tunnel *tunnel = NULL;
727	int ti = cb->args[0];
728	int si = cb->args[1];
729
730	for (;;) {
731		if (tunnel == NULL) {
732			tunnel = l2tp_tunnel_find_nth(net, ti);
733			if (tunnel == NULL)
734				goto out;
735		}
736
737		session = l2tp_session_find_nth(tunnel, si);
738		if (session == NULL) {
739			ti++;
740			tunnel = NULL;
741			si = 0;
742			continue;
743		}
744
745		if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).pid,
746					 cb->nlh->nlmsg_seq, NLM_F_MULTI,
747					 session) <= 0)
748			break;
749
750		si++;
751	}
752
753out:
754	cb->args[0] = ti;
755	cb->args[1] = si;
756
757	return skb->len;
758}
759
760static struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
761	[L2TP_ATTR_NONE]		= { .type = NLA_UNSPEC, },
762	[L2TP_ATTR_PW_TYPE]		= { .type = NLA_U16, },
763	[L2TP_ATTR_ENCAP_TYPE]		= { .type = NLA_U16, },
764	[L2TP_ATTR_OFFSET]		= { .type = NLA_U16, },
765	[L2TP_ATTR_DATA_SEQ]		= { .type = NLA_U8, },
766	[L2TP_ATTR_L2SPEC_TYPE]		= { .type = NLA_U8, },
767	[L2TP_ATTR_L2SPEC_LEN]		= { .type = NLA_U8, },
768	[L2TP_ATTR_PROTO_VERSION]	= { .type = NLA_U8, },
769	[L2TP_ATTR_CONN_ID]		= { .type = NLA_U32, },
770	[L2TP_ATTR_PEER_CONN_ID]	= { .type = NLA_U32, },
771	[L2TP_ATTR_SESSION_ID]		= { .type = NLA_U32, },
772	[L2TP_ATTR_PEER_SESSION_ID]	= { .type = NLA_U32, },
773	[L2TP_ATTR_UDP_CSUM]		= { .type = NLA_U8, },
774	[L2TP_ATTR_VLAN_ID]		= { .type = NLA_U16, },
775	[L2TP_ATTR_DEBUG]		= { .type = NLA_U32, },
776	[L2TP_ATTR_RECV_SEQ]		= { .type = NLA_U8, },
777	[L2TP_ATTR_SEND_SEQ]		= { .type = NLA_U8, },
778	[L2TP_ATTR_LNS_MODE]		= { .type = NLA_U8, },
779	[L2TP_ATTR_USING_IPSEC]		= { .type = NLA_U8, },
780	[L2TP_ATTR_RECV_TIMEOUT]	= { .type = NLA_MSECS, },
781	[L2TP_ATTR_FD]			= { .type = NLA_U32, },
782	[L2TP_ATTR_IP_SADDR]		= { .type = NLA_U32, },
783	[L2TP_ATTR_IP_DADDR]		= { .type = NLA_U32, },
784	[L2TP_ATTR_UDP_SPORT]		= { .type = NLA_U16, },
785	[L2TP_ATTR_UDP_DPORT]		= { .type = NLA_U16, },
786	[L2TP_ATTR_MTU]			= { .type = NLA_U16, },
787	[L2TP_ATTR_MRU]			= { .type = NLA_U16, },
788	[L2TP_ATTR_STATS]		= { .type = NLA_NESTED, },
789	[L2TP_ATTR_IP6_SADDR] = {
790		.type = NLA_BINARY,
791		.len = sizeof(struct in6_addr),
792	},
793	[L2TP_ATTR_IP6_DADDR] = {
794		.type = NLA_BINARY,
795		.len = sizeof(struct in6_addr),
796	},
797	[L2TP_ATTR_IFNAME] = {
798		.type = NLA_NUL_STRING,
799		.len = IFNAMSIZ - 1,
800	},
801	[L2TP_ATTR_COOKIE] = {
802		.type = NLA_BINARY,
803		.len = 8,
804	},
805	[L2TP_ATTR_PEER_COOKIE] = {
806		.type = NLA_BINARY,
807		.len = 8,
808	},
809};
810
811static struct genl_ops l2tp_nl_ops[] = {
812	{
813		.cmd = L2TP_CMD_NOOP,
814		.doit = l2tp_nl_cmd_noop,
815		.policy = l2tp_nl_policy,
816		/* can be retrieved by unprivileged users */
817	},
818	{
819		.cmd = L2TP_CMD_TUNNEL_CREATE,
820		.doit = l2tp_nl_cmd_tunnel_create,
821		.policy = l2tp_nl_policy,
822		.flags = GENL_ADMIN_PERM,
823	},
824	{
825		.cmd = L2TP_CMD_TUNNEL_DELETE,
826		.doit = l2tp_nl_cmd_tunnel_delete,
827		.policy = l2tp_nl_policy,
828		.flags = GENL_ADMIN_PERM,
829	},
830	{
831		.cmd = L2TP_CMD_TUNNEL_MODIFY,
832		.doit = l2tp_nl_cmd_tunnel_modify,
833		.policy = l2tp_nl_policy,
834		.flags = GENL_ADMIN_PERM,
835	},
836	{
837		.cmd = L2TP_CMD_TUNNEL_GET,
838		.doit = l2tp_nl_cmd_tunnel_get,
839		.dumpit = l2tp_nl_cmd_tunnel_dump,
840		.policy = l2tp_nl_policy,
841		.flags = GENL_ADMIN_PERM,
842	},
843	{
844		.cmd = L2TP_CMD_SESSION_CREATE,
845		.doit = l2tp_nl_cmd_session_create,
846		.policy = l2tp_nl_policy,
847		.flags = GENL_ADMIN_PERM,
848	},
849	{
850		.cmd = L2TP_CMD_SESSION_DELETE,
851		.doit = l2tp_nl_cmd_session_delete,
852		.policy = l2tp_nl_policy,
853		.flags = GENL_ADMIN_PERM,
854	},
855	{
856		.cmd = L2TP_CMD_SESSION_MODIFY,
857		.doit = l2tp_nl_cmd_session_modify,
858		.policy = l2tp_nl_policy,
859		.flags = GENL_ADMIN_PERM,
860	},
861	{
862		.cmd = L2TP_CMD_SESSION_GET,
863		.doit = l2tp_nl_cmd_session_get,
864		.dumpit = l2tp_nl_cmd_session_dump,
865		.policy = l2tp_nl_policy,
866		.flags = GENL_ADMIN_PERM,
867	},
868};
869
870int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
871{
872	int ret;
873
874	ret = -EINVAL;
875	if (pw_type >= __L2TP_PWTYPE_MAX)
876		goto err;
877
878	genl_lock();
879	ret = -EBUSY;
880	if (l2tp_nl_cmd_ops[pw_type])
881		goto out;
882
883	l2tp_nl_cmd_ops[pw_type] = ops;
884	ret = 0;
885
886out:
887	genl_unlock();
888err:
889	return ret;
890}
891EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
892
893void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
894{
895	if (pw_type < __L2TP_PWTYPE_MAX) {
896		genl_lock();
897		l2tp_nl_cmd_ops[pw_type] = NULL;
898		genl_unlock();
899	}
900}
901EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
902
903static int l2tp_nl_init(void)
904{
905	int err;
906
907	pr_info("L2TP netlink interface\n");
908	err = genl_register_family_with_ops(&l2tp_nl_family, l2tp_nl_ops,
909					    ARRAY_SIZE(l2tp_nl_ops));
910
911	return err;
912}
913
914static void l2tp_nl_cleanup(void)
915{
916	genl_unregister_family(&l2tp_nl_family);
917}
918
919module_init(l2tp_nl_init);
920module_exit(l2tp_nl_cleanup);
921
922MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
923MODULE_DESCRIPTION("L2TP netlink");
924MODULE_LICENSE("GPL");
925MODULE_VERSION("1.0");
926MODULE_ALIAS_GENL_FAMILY("l2tp");