1 // SPDX-License-Identifier: GPL-2.0-only
3 * L2TP netlink layer, for management
5 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
7 * Partly based on the IrDA nelink implementation
8 * (see net/irda/irnetlink.c) which is:
9 * Copyright (c) 2007 Samuel Ortiz <samuel@sortiz.org>
10 * which is in turn partly based on the wireless netlink code:
11 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <net/genetlink.h>
20 #include <linux/udp.h>
21 #include <linux/socket.h>
22 #include <linux/module.h>
23 #include <linux/list.h>
24 #include <net/net_namespace.h>
26 #include <linux/l2tp.h>
28 #include "l2tp_core.h"
31 static struct genl_family l2tp_nl_family;
33 static const struct genl_multicast_group l2tp_multicast_group[] = {
35 .name = L2TP_GENL_MCGROUP,
39 static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq,
40 int flags, struct l2tp_tunnel *tunnel, u8 cmd);
41 static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq,
42 int flags, struct l2tp_session *session,
45 /* Accessed under genl lock */
46 static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX];
48 static struct l2tp_session *l2tp_nl_session_get(struct genl_info *info)
53 struct l2tp_tunnel *tunnel;
54 struct l2tp_session *session = NULL;
55 struct net *net = genl_info_net(info);
57 if (info->attrs[L2TP_ATTR_IFNAME]) {
58 ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
59 session = l2tp_session_get_by_ifname(net, ifname);
60 } else if ((info->attrs[L2TP_ATTR_SESSION_ID]) &&
61 (info->attrs[L2TP_ATTR_CONN_ID])) {
62 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
63 session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
64 tunnel = l2tp_tunnel_get(net, tunnel_id);
66 session = l2tp_tunnel_get_session(tunnel, session_id);
67 l2tp_tunnel_dec_refcount(tunnel);
74 static int l2tp_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
80 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
86 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
87 &l2tp_nl_family, 0, L2TP_CMD_NOOP);
93 genlmsg_end(msg, hdr);
95 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
104 static int l2tp_tunnel_notify(struct genl_family *family,
105 struct genl_info *info,
106 struct l2tp_tunnel *tunnel,
112 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
116 ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
117 NLM_F_ACK, tunnel, cmd);
120 ret = genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC);
121 /* We don't care if no one is listening */
132 static int l2tp_session_notify(struct genl_family *family,
133 struct genl_info *info,
134 struct l2tp_session *session,
140 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
144 ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
145 NLM_F_ACK, session, cmd);
148 ret = genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC);
149 /* We don't care if no one is listening */
160 static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info)
167 struct l2tp_tunnel_cfg cfg = { 0, };
168 struct l2tp_tunnel *tunnel;
169 struct net *net = genl_info_net(info);
171 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
175 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
177 if (!info->attrs[L2TP_ATTR_PEER_CONN_ID]) {
181 peer_tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_CONN_ID]);
183 if (!info->attrs[L2TP_ATTR_PROTO_VERSION]) {
187 proto_version = nla_get_u8(info->attrs[L2TP_ATTR_PROTO_VERSION]);
189 if (!info->attrs[L2TP_ATTR_ENCAP_TYPE]) {
193 cfg.encap = nla_get_u16(info->attrs[L2TP_ATTR_ENCAP_TYPE]);
196 if (info->attrs[L2TP_ATTR_FD]) {
197 fd = nla_get_u32(info->attrs[L2TP_ATTR_FD]);
199 #if IS_ENABLED(CONFIG_IPV6)
200 if (info->attrs[L2TP_ATTR_IP6_SADDR] &&
201 info->attrs[L2TP_ATTR_IP6_DADDR]) {
202 cfg.local_ip6 = nla_data(
203 info->attrs[L2TP_ATTR_IP6_SADDR]);
204 cfg.peer_ip6 = nla_data(
205 info->attrs[L2TP_ATTR_IP6_DADDR]);
208 if (info->attrs[L2TP_ATTR_IP_SADDR] &&
209 info->attrs[L2TP_ATTR_IP_DADDR]) {
210 cfg.local_ip.s_addr = nla_get_in_addr(
211 info->attrs[L2TP_ATTR_IP_SADDR]);
212 cfg.peer_ip.s_addr = nla_get_in_addr(
213 info->attrs[L2TP_ATTR_IP_DADDR]);
218 if (info->attrs[L2TP_ATTR_UDP_SPORT])
219 cfg.local_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_SPORT]);
220 if (info->attrs[L2TP_ATTR_UDP_DPORT])
221 cfg.peer_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_DPORT]);
222 cfg.use_udp_checksums = nla_get_flag(
223 info->attrs[L2TP_ATTR_UDP_CSUM]);
225 #if IS_ENABLED(CONFIG_IPV6)
226 cfg.udp6_zero_tx_checksums = nla_get_flag(
227 info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_TX]);
228 cfg.udp6_zero_rx_checksums = nla_get_flag(
229 info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_RX]);
233 if (info->attrs[L2TP_ATTR_DEBUG])
234 cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
238 case L2TP_ENCAPTYPE_UDP:
239 case L2TP_ENCAPTYPE_IP:
240 ret = l2tp_tunnel_create(net, fd, proto_version, tunnel_id,
241 peer_tunnel_id, &cfg, &tunnel);
248 l2tp_tunnel_inc_refcount(tunnel);
249 ret = l2tp_tunnel_register(tunnel, net, &cfg);
254 ret = l2tp_tunnel_notify(&l2tp_nl_family, info, tunnel,
255 L2TP_CMD_TUNNEL_CREATE);
256 l2tp_tunnel_dec_refcount(tunnel);
262 static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info)
264 struct l2tp_tunnel *tunnel;
267 struct net *net = genl_info_net(info);
269 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
273 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
275 tunnel = l2tp_tunnel_get(net, tunnel_id);
281 l2tp_tunnel_notify(&l2tp_nl_family, info,
282 tunnel, L2TP_CMD_TUNNEL_DELETE);
284 l2tp_tunnel_delete(tunnel);
286 l2tp_tunnel_dec_refcount(tunnel);
292 static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info)
294 struct l2tp_tunnel *tunnel;
297 struct net *net = genl_info_net(info);
299 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
303 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
305 tunnel = l2tp_tunnel_get(net, tunnel_id);
311 if (info->attrs[L2TP_ATTR_DEBUG])
312 tunnel->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
314 ret = l2tp_tunnel_notify(&l2tp_nl_family, info,
315 tunnel, L2TP_CMD_TUNNEL_MODIFY);
317 l2tp_tunnel_dec_refcount(tunnel);
323 static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
324 struct l2tp_tunnel *tunnel, u8 cmd)
328 struct sock *sk = NULL;
329 struct inet_sock *inet;
330 #if IS_ENABLED(CONFIG_IPV6)
331 struct ipv6_pinfo *np = NULL;
334 hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
338 if (nla_put_u8(skb, L2TP_ATTR_PROTO_VERSION, tunnel->version) ||
339 nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
340 nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
341 nla_put_u32(skb, L2TP_ATTR_DEBUG, tunnel->debug) ||
342 nla_put_u16(skb, L2TP_ATTR_ENCAP_TYPE, tunnel->encap))
343 goto nla_put_failure;
345 nest = nla_nest_start_noflag(skb, L2TP_ATTR_STATS);
347 goto nla_put_failure;
349 if (nla_put_u64_64bit(skb, L2TP_ATTR_TX_PACKETS,
350 atomic_long_read(&tunnel->stats.tx_packets),
351 L2TP_ATTR_STATS_PAD) ||
352 nla_put_u64_64bit(skb, L2TP_ATTR_TX_BYTES,
353 atomic_long_read(&tunnel->stats.tx_bytes),
354 L2TP_ATTR_STATS_PAD) ||
355 nla_put_u64_64bit(skb, L2TP_ATTR_TX_ERRORS,
356 atomic_long_read(&tunnel->stats.tx_errors),
357 L2TP_ATTR_STATS_PAD) ||
358 nla_put_u64_64bit(skb, L2TP_ATTR_RX_PACKETS,
359 atomic_long_read(&tunnel->stats.rx_packets),
360 L2TP_ATTR_STATS_PAD) ||
361 nla_put_u64_64bit(skb, L2TP_ATTR_RX_BYTES,
362 atomic_long_read(&tunnel->stats.rx_bytes),
363 L2TP_ATTR_STATS_PAD) ||
364 nla_put_u64_64bit(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
365 atomic_long_read(&tunnel->stats.rx_seq_discards),
366 L2TP_ATTR_STATS_PAD) ||
367 nla_put_u64_64bit(skb, L2TP_ATTR_RX_OOS_PACKETS,
368 atomic_long_read(&tunnel->stats.rx_oos_packets),
369 L2TP_ATTR_STATS_PAD) ||
370 nla_put_u64_64bit(skb, L2TP_ATTR_RX_ERRORS,
371 atomic_long_read(&tunnel->stats.rx_errors),
372 L2TP_ATTR_STATS_PAD))
373 goto nla_put_failure;
374 nla_nest_end(skb, nest);
380 #if IS_ENABLED(CONFIG_IPV6)
381 if (sk->sk_family == AF_INET6)
387 switch (tunnel->encap) {
388 case L2TP_ENCAPTYPE_UDP:
389 switch (sk->sk_family) {
391 if (nla_put_u8(skb, L2TP_ATTR_UDP_CSUM, !sk->sk_no_check_tx))
392 goto nla_put_failure;
394 #if IS_ENABLED(CONFIG_IPV6)
396 if (udp_get_no_check6_tx(sk) &&
397 nla_put_flag(skb, L2TP_ATTR_UDP_ZERO_CSUM6_TX))
398 goto nla_put_failure;
399 if (udp_get_no_check6_rx(sk) &&
400 nla_put_flag(skb, L2TP_ATTR_UDP_ZERO_CSUM6_RX))
401 goto nla_put_failure;
405 if (nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
406 nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)))
407 goto nla_put_failure;
409 case L2TP_ENCAPTYPE_IP:
410 #if IS_ENABLED(CONFIG_IPV6)
412 if (nla_put_in6_addr(skb, L2TP_ATTR_IP6_SADDR,
414 nla_put_in6_addr(skb, L2TP_ATTR_IP6_DADDR,
416 goto nla_put_failure;
419 if (nla_put_in_addr(skb, L2TP_ATTR_IP_SADDR,
421 nla_put_in_addr(skb, L2TP_ATTR_IP_DADDR,
423 goto nla_put_failure;
428 genlmsg_end(skb, hdr);
432 genlmsg_cancel(skb, hdr);
436 static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
438 struct l2tp_tunnel *tunnel;
442 struct net *net = genl_info_net(info);
444 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
449 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
451 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
457 tunnel = l2tp_tunnel_get(net, tunnel_id);
463 ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
464 NLM_F_ACK, tunnel, L2TP_CMD_TUNNEL_GET);
466 goto err_nlmsg_tunnel;
468 l2tp_tunnel_dec_refcount(tunnel);
470 return genlmsg_unicast(net, msg, info->snd_portid);
473 l2tp_tunnel_dec_refcount(tunnel);
480 static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
482 int ti = cb->args[0];
483 struct l2tp_tunnel *tunnel;
484 struct net *net = sock_net(skb->sk);
487 tunnel = l2tp_tunnel_get_nth(net, ti);
491 if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).portid,
492 cb->nlh->nlmsg_seq, NLM_F_MULTI,
493 tunnel, L2TP_CMD_TUNNEL_GET) < 0) {
494 l2tp_tunnel_dec_refcount(tunnel);
497 l2tp_tunnel_dec_refcount(tunnel);
508 static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
514 struct l2tp_tunnel *tunnel;
515 struct l2tp_session *session;
516 struct l2tp_session_cfg cfg = { 0, };
517 struct net *net = genl_info_net(info);
519 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
524 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
525 tunnel = l2tp_tunnel_get(net, tunnel_id);
531 if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
535 session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
537 if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
541 peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
543 if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
547 cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
548 if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
553 /* L2TPv2 only accepts PPP pseudo-wires */
554 if (tunnel->version == 2 && cfg.pw_type != L2TP_PWTYPE_PPP) {
555 ret = -EPROTONOSUPPORT;
559 if (tunnel->version > 2) {
560 if (info->attrs[L2TP_ATTR_L2SPEC_TYPE]) {
561 cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
562 if (cfg.l2specific_type != L2TP_L2SPECTYPE_DEFAULT &&
563 cfg.l2specific_type != L2TP_L2SPECTYPE_NONE) {
568 cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
571 if (info->attrs[L2TP_ATTR_COOKIE]) {
572 u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
577 cfg.cookie_len = len;
578 memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
580 if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
581 u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
586 cfg.peer_cookie_len = len;
587 memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
589 if (info->attrs[L2TP_ATTR_IFNAME])
590 cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
593 if (info->attrs[L2TP_ATTR_DEBUG])
594 cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
596 if (info->attrs[L2TP_ATTR_RECV_SEQ])
597 cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
599 if (info->attrs[L2TP_ATTR_SEND_SEQ])
600 cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
602 if (info->attrs[L2TP_ATTR_LNS_MODE])
603 cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
605 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
606 cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
608 #ifdef CONFIG_MODULES
609 if (l2tp_nl_cmd_ops[cfg.pw_type] == NULL) {
611 request_module("net-l2tp-type-%u", cfg.pw_type);
615 if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) ||
616 (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) {
617 ret = -EPROTONOSUPPORT;
621 ret = l2tp_nl_cmd_ops[cfg.pw_type]->session_create(net, tunnel,
627 session = l2tp_tunnel_get_session(tunnel, session_id);
629 ret = l2tp_session_notify(&l2tp_nl_family, info, session,
630 L2TP_CMD_SESSION_CREATE);
631 l2tp_session_dec_refcount(session);
636 l2tp_tunnel_dec_refcount(tunnel);
641 static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
644 struct l2tp_session *session;
647 session = l2tp_nl_session_get(info);
648 if (session == NULL) {
653 l2tp_session_notify(&l2tp_nl_family, info,
654 session, L2TP_CMD_SESSION_DELETE);
656 pw_type = session->pwtype;
657 if (pw_type < __L2TP_PWTYPE_MAX)
658 if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
659 ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
661 l2tp_session_dec_refcount(session);
667 static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
670 struct l2tp_session *session;
672 session = l2tp_nl_session_get(info);
673 if (session == NULL) {
678 if (info->attrs[L2TP_ATTR_DEBUG])
679 session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
681 if (info->attrs[L2TP_ATTR_RECV_SEQ])
682 session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
684 if (info->attrs[L2TP_ATTR_SEND_SEQ]) {
685 session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
686 l2tp_session_set_header_len(session, session->tunnel->version);
689 if (info->attrs[L2TP_ATTR_LNS_MODE])
690 session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
692 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
693 session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
695 ret = l2tp_session_notify(&l2tp_nl_family, info,
696 session, L2TP_CMD_SESSION_MODIFY);
698 l2tp_session_dec_refcount(session);
704 static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
705 struct l2tp_session *session, u8 cmd)
709 struct l2tp_tunnel *tunnel = session->tunnel;
711 hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
715 if (nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
716 nla_put_u32(skb, L2TP_ATTR_SESSION_ID, session->session_id) ||
717 nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
718 nla_put_u32(skb, L2TP_ATTR_PEER_SESSION_ID,
719 session->peer_session_id) ||
720 nla_put_u32(skb, L2TP_ATTR_DEBUG, session->debug) ||
721 nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype))
722 goto nla_put_failure;
724 if ((session->ifname[0] &&
725 nla_put_string(skb, L2TP_ATTR_IFNAME, session->ifname)) ||
726 (session->cookie_len &&
727 nla_put(skb, L2TP_ATTR_COOKIE, session->cookie_len,
728 &session->cookie[0])) ||
729 (session->peer_cookie_len &&
730 nla_put(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len,
731 &session->peer_cookie[0])) ||
732 nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
733 nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
734 nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
735 (l2tp_tunnel_uses_xfrm(tunnel) &&
736 nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
737 (session->reorder_timeout &&
738 nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT,
739 session->reorder_timeout, L2TP_ATTR_PAD)))
740 goto nla_put_failure;
742 nest = nla_nest_start_noflag(skb, L2TP_ATTR_STATS);
744 goto nla_put_failure;
746 if (nla_put_u64_64bit(skb, L2TP_ATTR_TX_PACKETS,
747 atomic_long_read(&session->stats.tx_packets),
748 L2TP_ATTR_STATS_PAD) ||
749 nla_put_u64_64bit(skb, L2TP_ATTR_TX_BYTES,
750 atomic_long_read(&session->stats.tx_bytes),
751 L2TP_ATTR_STATS_PAD) ||
752 nla_put_u64_64bit(skb, L2TP_ATTR_TX_ERRORS,
753 atomic_long_read(&session->stats.tx_errors),
754 L2TP_ATTR_STATS_PAD) ||
755 nla_put_u64_64bit(skb, L2TP_ATTR_RX_PACKETS,
756 atomic_long_read(&session->stats.rx_packets),
757 L2TP_ATTR_STATS_PAD) ||
758 nla_put_u64_64bit(skb, L2TP_ATTR_RX_BYTES,
759 atomic_long_read(&session->stats.rx_bytes),
760 L2TP_ATTR_STATS_PAD) ||
761 nla_put_u64_64bit(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
762 atomic_long_read(&session->stats.rx_seq_discards),
763 L2TP_ATTR_STATS_PAD) ||
764 nla_put_u64_64bit(skb, L2TP_ATTR_RX_OOS_PACKETS,
765 atomic_long_read(&session->stats.rx_oos_packets),
766 L2TP_ATTR_STATS_PAD) ||
767 nla_put_u64_64bit(skb, L2TP_ATTR_RX_ERRORS,
768 atomic_long_read(&session->stats.rx_errors),
769 L2TP_ATTR_STATS_PAD))
770 goto nla_put_failure;
771 nla_nest_end(skb, nest);
773 genlmsg_end(skb, hdr);
777 genlmsg_cancel(skb, hdr);
781 static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
783 struct l2tp_session *session;
787 session = l2tp_nl_session_get(info);
788 if (session == NULL) {
793 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
799 ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
800 0, session, L2TP_CMD_SESSION_GET);
804 ret = genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
806 l2tp_session_dec_refcount(session);
813 l2tp_session_dec_refcount(session);
818 static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
820 struct net *net = sock_net(skb->sk);
821 struct l2tp_session *session;
822 struct l2tp_tunnel *tunnel = NULL;
823 int ti = cb->args[0];
824 int si = cb->args[1];
827 if (tunnel == NULL) {
828 tunnel = l2tp_tunnel_get_nth(net, ti);
833 session = l2tp_session_get_nth(tunnel, si);
834 if (session == NULL) {
836 l2tp_tunnel_dec_refcount(tunnel);
842 if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).portid,
843 cb->nlh->nlmsg_seq, NLM_F_MULTI,
844 session, L2TP_CMD_SESSION_GET) < 0) {
845 l2tp_session_dec_refcount(session);
846 l2tp_tunnel_dec_refcount(tunnel);
849 l2tp_session_dec_refcount(session);
861 static const struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
862 [L2TP_ATTR_NONE] = { .type = NLA_UNSPEC, },
863 [L2TP_ATTR_PW_TYPE] = { .type = NLA_U16, },
864 [L2TP_ATTR_ENCAP_TYPE] = { .type = NLA_U16, },
865 [L2TP_ATTR_OFFSET] = { .type = NLA_U16, },
866 [L2TP_ATTR_DATA_SEQ] = { .type = NLA_U8, },
867 [L2TP_ATTR_L2SPEC_TYPE] = { .type = NLA_U8, },
868 [L2TP_ATTR_L2SPEC_LEN] = { .type = NLA_U8, },
869 [L2TP_ATTR_PROTO_VERSION] = { .type = NLA_U8, },
870 [L2TP_ATTR_CONN_ID] = { .type = NLA_U32, },
871 [L2TP_ATTR_PEER_CONN_ID] = { .type = NLA_U32, },
872 [L2TP_ATTR_SESSION_ID] = { .type = NLA_U32, },
873 [L2TP_ATTR_PEER_SESSION_ID] = { .type = NLA_U32, },
874 [L2TP_ATTR_UDP_CSUM] = { .type = NLA_U8, },
875 [L2TP_ATTR_VLAN_ID] = { .type = NLA_U16, },
876 [L2TP_ATTR_DEBUG] = { .type = NLA_U32, },
877 [L2TP_ATTR_RECV_SEQ] = { .type = NLA_U8, },
878 [L2TP_ATTR_SEND_SEQ] = { .type = NLA_U8, },
879 [L2TP_ATTR_LNS_MODE] = { .type = NLA_U8, },
880 [L2TP_ATTR_USING_IPSEC] = { .type = NLA_U8, },
881 [L2TP_ATTR_RECV_TIMEOUT] = { .type = NLA_MSECS, },
882 [L2TP_ATTR_FD] = { .type = NLA_U32, },
883 [L2TP_ATTR_IP_SADDR] = { .type = NLA_U32, },
884 [L2TP_ATTR_IP_DADDR] = { .type = NLA_U32, },
885 [L2TP_ATTR_UDP_SPORT] = { .type = NLA_U16, },
886 [L2TP_ATTR_UDP_DPORT] = { .type = NLA_U16, },
887 [L2TP_ATTR_MTU] = { .type = NLA_U16, },
888 [L2TP_ATTR_MRU] = { .type = NLA_U16, },
889 [L2TP_ATTR_STATS] = { .type = NLA_NESTED, },
890 [L2TP_ATTR_IP6_SADDR] = {
892 .len = sizeof(struct in6_addr),
894 [L2TP_ATTR_IP6_DADDR] = {
896 .len = sizeof(struct in6_addr),
898 [L2TP_ATTR_IFNAME] = {
899 .type = NLA_NUL_STRING,
902 [L2TP_ATTR_COOKIE] = {
906 [L2TP_ATTR_PEER_COOKIE] = {
912 static const struct genl_ops l2tp_nl_ops[] = {
914 .cmd = L2TP_CMD_NOOP,
915 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
916 .doit = l2tp_nl_cmd_noop,
917 /* can be retrieved by unprivileged users */
920 .cmd = L2TP_CMD_TUNNEL_CREATE,
921 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
922 .doit = l2tp_nl_cmd_tunnel_create,
923 .flags = GENL_ADMIN_PERM,
926 .cmd = L2TP_CMD_TUNNEL_DELETE,
927 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
928 .doit = l2tp_nl_cmd_tunnel_delete,
929 .flags = GENL_ADMIN_PERM,
932 .cmd = L2TP_CMD_TUNNEL_MODIFY,
933 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
934 .doit = l2tp_nl_cmd_tunnel_modify,
935 .flags = GENL_ADMIN_PERM,
938 .cmd = L2TP_CMD_TUNNEL_GET,
939 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
940 .doit = l2tp_nl_cmd_tunnel_get,
941 .dumpit = l2tp_nl_cmd_tunnel_dump,
942 .flags = GENL_ADMIN_PERM,
945 .cmd = L2TP_CMD_SESSION_CREATE,
946 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
947 .doit = l2tp_nl_cmd_session_create,
948 .flags = GENL_ADMIN_PERM,
951 .cmd = L2TP_CMD_SESSION_DELETE,
952 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
953 .doit = l2tp_nl_cmd_session_delete,
954 .flags = GENL_ADMIN_PERM,
957 .cmd = L2TP_CMD_SESSION_MODIFY,
958 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
959 .doit = l2tp_nl_cmd_session_modify,
960 .flags = GENL_ADMIN_PERM,
963 .cmd = L2TP_CMD_SESSION_GET,
964 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
965 .doit = l2tp_nl_cmd_session_get,
966 .dumpit = l2tp_nl_cmd_session_dump,
967 .flags = GENL_ADMIN_PERM,
971 static struct genl_family l2tp_nl_family __ro_after_init = {
972 .name = L2TP_GENL_NAME,
973 .version = L2TP_GENL_VERSION,
975 .maxattr = L2TP_ATTR_MAX,
976 .policy = l2tp_nl_policy,
978 .module = THIS_MODULE,
980 .n_ops = ARRAY_SIZE(l2tp_nl_ops),
981 .mcgrps = l2tp_multicast_group,
982 .n_mcgrps = ARRAY_SIZE(l2tp_multicast_group),
985 int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
990 if (pw_type >= __L2TP_PWTYPE_MAX)
995 if (l2tp_nl_cmd_ops[pw_type])
998 l2tp_nl_cmd_ops[pw_type] = ops;
1006 EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
1008 void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
1010 if (pw_type < __L2TP_PWTYPE_MAX) {
1012 l2tp_nl_cmd_ops[pw_type] = NULL;
1016 EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
1018 static int __init l2tp_nl_init(void)
1020 pr_info("L2TP netlink interface\n");
1021 return genl_register_family(&l2tp_nl_family);
1024 static void l2tp_nl_cleanup(void)
1026 genl_unregister_family(&l2tp_nl_family);
1029 module_init(l2tp_nl_init);
1030 module_exit(l2tp_nl_cleanup);
1032 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1033 MODULE_DESCRIPTION("L2TP netlink");
1034 MODULE_LICENSE("GPL");
1035 MODULE_VERSION("1.0");
1036 MODULE_ALIAS_GENL_FAMILY("l2tp");