2 * L2TP netlink layer, for management
4 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
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>
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.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <net/genetlink.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>
29 #include <linux/l2tp.h>
31 #include "l2tp_core.h"
34 static struct genl_family l2tp_nl_family;
36 static const struct genl_multicast_group l2tp_multicast_group[] = {
38 .name = L2TP_GENL_MCGROUP,
42 static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq,
43 int flags, struct l2tp_tunnel *tunnel, u8 cmd);
44 static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq,
45 int flags, struct l2tp_session *session,
48 /* Accessed under genl lock */
49 static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX];
51 static struct l2tp_session *l2tp_nl_session_get(struct genl_info *info,
57 struct l2tp_tunnel *tunnel;
58 struct l2tp_session *session = NULL;
59 struct net *net = genl_info_net(info);
61 if (info->attrs[L2TP_ATTR_IFNAME]) {
62 ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
63 session = l2tp_session_get_by_ifname(net, ifname, do_ref);
64 } else if ((info->attrs[L2TP_ATTR_SESSION_ID]) &&
65 (info->attrs[L2TP_ATTR_CONN_ID])) {
66 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
67 session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
68 tunnel = l2tp_tunnel_get(net, tunnel_id);
70 session = l2tp_session_get(net, tunnel, session_id,
72 l2tp_tunnel_dec_refcount(tunnel);
79 static int l2tp_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
85 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
91 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
92 &l2tp_nl_family, 0, L2TP_CMD_NOOP);
98 genlmsg_end(msg, hdr);
100 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
109 static int l2tp_tunnel_notify(struct genl_family *family,
110 struct genl_info *info,
111 struct l2tp_tunnel *tunnel,
117 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
121 ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
122 NLM_F_ACK, tunnel, cmd);
125 ret = genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC);
126 /* We don't care if no one is listening */
137 static int l2tp_session_notify(struct genl_family *family,
138 struct genl_info *info,
139 struct l2tp_session *session,
145 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
149 ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
150 NLM_F_ACK, session, cmd);
153 ret = genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC);
154 /* We don't care if no one is listening */
165 static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info)
172 struct l2tp_tunnel_cfg cfg = { 0, };
173 struct l2tp_tunnel *tunnel;
174 struct net *net = genl_info_net(info);
176 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
180 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
182 if (!info->attrs[L2TP_ATTR_PEER_CONN_ID]) {
186 peer_tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_CONN_ID]);
188 if (!info->attrs[L2TP_ATTR_PROTO_VERSION]) {
192 proto_version = nla_get_u8(info->attrs[L2TP_ATTR_PROTO_VERSION]);
194 if (!info->attrs[L2TP_ATTR_ENCAP_TYPE]) {
198 cfg.encap = nla_get_u16(info->attrs[L2TP_ATTR_ENCAP_TYPE]);
201 if (info->attrs[L2TP_ATTR_FD]) {
202 fd = nla_get_u32(info->attrs[L2TP_ATTR_FD]);
204 #if IS_ENABLED(CONFIG_IPV6)
205 if (info->attrs[L2TP_ATTR_IP6_SADDR] &&
206 info->attrs[L2TP_ATTR_IP6_DADDR]) {
207 cfg.local_ip6 = nla_data(
208 info->attrs[L2TP_ATTR_IP6_SADDR]);
209 cfg.peer_ip6 = nla_data(
210 info->attrs[L2TP_ATTR_IP6_DADDR]);
213 if (info->attrs[L2TP_ATTR_IP_SADDR] &&
214 info->attrs[L2TP_ATTR_IP_DADDR]) {
215 cfg.local_ip.s_addr = nla_get_in_addr(
216 info->attrs[L2TP_ATTR_IP_SADDR]);
217 cfg.peer_ip.s_addr = nla_get_in_addr(
218 info->attrs[L2TP_ATTR_IP_DADDR]);
223 if (info->attrs[L2TP_ATTR_UDP_SPORT])
224 cfg.local_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_SPORT]);
225 if (info->attrs[L2TP_ATTR_UDP_DPORT])
226 cfg.peer_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_DPORT]);
227 cfg.use_udp_checksums = nla_get_flag(
228 info->attrs[L2TP_ATTR_UDP_CSUM]);
230 #if IS_ENABLED(CONFIG_IPV6)
231 cfg.udp6_zero_tx_checksums = nla_get_flag(
232 info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_TX]);
233 cfg.udp6_zero_rx_checksums = nla_get_flag(
234 info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_RX]);
238 if (info->attrs[L2TP_ATTR_DEBUG])
239 cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
241 tunnel = l2tp_tunnel_find(net, tunnel_id);
242 if (tunnel != NULL) {
249 case L2TP_ENCAPTYPE_UDP:
250 case L2TP_ENCAPTYPE_IP:
251 ret = l2tp_tunnel_create(net, fd, proto_version, tunnel_id,
252 peer_tunnel_id, &cfg, &tunnel);
257 ret = l2tp_tunnel_notify(&l2tp_nl_family, info,
258 tunnel, L2TP_CMD_TUNNEL_CREATE);
263 static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info)
265 struct l2tp_tunnel *tunnel;
268 struct net *net = genl_info_net(info);
270 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
274 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
276 tunnel = l2tp_tunnel_get(net, tunnel_id);
282 l2tp_tunnel_notify(&l2tp_nl_family, info,
283 tunnel, L2TP_CMD_TUNNEL_DELETE);
285 l2tp_tunnel_delete(tunnel);
287 l2tp_tunnel_dec_refcount(tunnel);
293 static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info)
295 struct l2tp_tunnel *tunnel;
298 struct net *net = genl_info_net(info);
300 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
304 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
306 tunnel = l2tp_tunnel_get(net, tunnel_id);
312 if (info->attrs[L2TP_ATTR_DEBUG])
313 tunnel->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
315 ret = l2tp_tunnel_notify(&l2tp_nl_family, info,
316 tunnel, L2TP_CMD_TUNNEL_MODIFY);
318 l2tp_tunnel_dec_refcount(tunnel);
324 static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
325 struct l2tp_tunnel *tunnel, u8 cmd)
329 struct sock *sk = NULL;
330 struct inet_sock *inet;
331 #if IS_ENABLED(CONFIG_IPV6)
332 struct ipv6_pinfo *np = NULL;
335 hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
339 if (nla_put_u8(skb, L2TP_ATTR_PROTO_VERSION, tunnel->version) ||
340 nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
341 nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
342 nla_put_u32(skb, L2TP_ATTR_DEBUG, tunnel->debug) ||
343 nla_put_u16(skb, L2TP_ATTR_ENCAP_TYPE, tunnel->encap))
344 goto nla_put_failure;
346 nest = nla_nest_start(skb, L2TP_ATTR_STATS);
348 goto nla_put_failure;
350 if (nla_put_u64_64bit(skb, L2TP_ATTR_TX_PACKETS,
351 atomic_long_read(&tunnel->stats.tx_packets),
352 L2TP_ATTR_STATS_PAD) ||
353 nla_put_u64_64bit(skb, L2TP_ATTR_TX_BYTES,
354 atomic_long_read(&tunnel->stats.tx_bytes),
355 L2TP_ATTR_STATS_PAD) ||
356 nla_put_u64_64bit(skb, L2TP_ATTR_TX_ERRORS,
357 atomic_long_read(&tunnel->stats.tx_errors),
358 L2TP_ATTR_STATS_PAD) ||
359 nla_put_u64_64bit(skb, L2TP_ATTR_RX_PACKETS,
360 atomic_long_read(&tunnel->stats.rx_packets),
361 L2TP_ATTR_STATS_PAD) ||
362 nla_put_u64_64bit(skb, L2TP_ATTR_RX_BYTES,
363 atomic_long_read(&tunnel->stats.rx_bytes),
364 L2TP_ATTR_STATS_PAD) ||
365 nla_put_u64_64bit(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
366 atomic_long_read(&tunnel->stats.rx_seq_discards),
367 L2TP_ATTR_STATS_PAD) ||
368 nla_put_u64_64bit(skb, L2TP_ATTR_RX_OOS_PACKETS,
369 atomic_long_read(&tunnel->stats.rx_oos_packets),
370 L2TP_ATTR_STATS_PAD) ||
371 nla_put_u64_64bit(skb, L2TP_ATTR_RX_ERRORS,
372 atomic_long_read(&tunnel->stats.rx_errors),
373 L2TP_ATTR_STATS_PAD))
374 goto nla_put_failure;
375 nla_nest_end(skb, nest);
381 #if IS_ENABLED(CONFIG_IPV6)
382 if (sk->sk_family == AF_INET6)
388 switch (tunnel->encap) {
389 case L2TP_ENCAPTYPE_UDP:
390 switch (sk->sk_family) {
392 if (nla_put_u8(skb, L2TP_ATTR_UDP_CSUM, !sk->sk_no_check_tx))
393 goto nla_put_failure;
395 #if IS_ENABLED(CONFIG_IPV6)
397 if (udp_get_no_check6_tx(sk) &&
398 nla_put_flag(skb, L2TP_ATTR_UDP_ZERO_CSUM6_TX))
399 goto nla_put_failure;
400 if (udp_get_no_check6_rx(sk) &&
401 nla_put_flag(skb, L2TP_ATTR_UDP_ZERO_CSUM6_RX))
402 goto nla_put_failure;
406 if (nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
407 nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)))
408 goto nla_put_failure;
410 case L2TP_ENCAPTYPE_IP:
411 #if IS_ENABLED(CONFIG_IPV6)
413 if (nla_put_in6_addr(skb, L2TP_ATTR_IP6_SADDR,
415 nla_put_in6_addr(skb, L2TP_ATTR_IP6_DADDR,
417 goto nla_put_failure;
420 if (nla_put_in_addr(skb, L2TP_ATTR_IP_SADDR,
422 nla_put_in_addr(skb, L2TP_ATTR_IP_DADDR,
424 goto nla_put_failure;
429 genlmsg_end(skb, hdr);
433 genlmsg_cancel(skb, hdr);
437 static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
439 struct l2tp_tunnel *tunnel;
443 struct net *net = genl_info_net(info);
445 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
450 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
452 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
458 tunnel = l2tp_tunnel_get(net, tunnel_id);
464 ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
465 NLM_F_ACK, tunnel, L2TP_CMD_TUNNEL_GET);
467 goto err_nlmsg_tunnel;
469 l2tp_tunnel_dec_refcount(tunnel);
471 return genlmsg_unicast(net, msg, info->snd_portid);
474 l2tp_tunnel_dec_refcount(tunnel);
481 static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
483 int ti = cb->args[0];
484 struct l2tp_tunnel *tunnel;
485 struct net *net = sock_net(skb->sk);
488 tunnel = l2tp_tunnel_find_nth(net, ti);
492 if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).portid,
493 cb->nlh->nlmsg_seq, NLM_F_MULTI,
494 tunnel, L2TP_CMD_TUNNEL_GET) < 0)
506 static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
512 struct l2tp_tunnel *tunnel;
513 struct l2tp_session *session;
514 struct l2tp_session_cfg cfg = { 0, };
515 struct net *net = genl_info_net(info);
517 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
522 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
523 tunnel = l2tp_tunnel_get(net, tunnel_id);
529 if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
533 session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
535 if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
539 peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
541 if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
545 cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
546 if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
551 if (tunnel->version > 2) {
552 if (info->attrs[L2TP_ATTR_DATA_SEQ])
553 cfg.data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
555 cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
556 if (info->attrs[L2TP_ATTR_L2SPEC_TYPE])
557 cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
559 cfg.l2specific_len = 4;
560 if (info->attrs[L2TP_ATTR_L2SPEC_LEN])
561 cfg.l2specific_len = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_LEN]);
563 if (info->attrs[L2TP_ATTR_COOKIE]) {
564 u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
569 cfg.cookie_len = len;
570 memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
572 if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
573 u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
578 cfg.peer_cookie_len = len;
579 memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
581 if (info->attrs[L2TP_ATTR_IFNAME])
582 cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
584 if (info->attrs[L2TP_ATTR_VLAN_ID])
585 cfg.vlan_id = nla_get_u16(info->attrs[L2TP_ATTR_VLAN_ID]);
588 if (info->attrs[L2TP_ATTR_DEBUG])
589 cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
591 if (info->attrs[L2TP_ATTR_RECV_SEQ])
592 cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
594 if (info->attrs[L2TP_ATTR_SEND_SEQ])
595 cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
597 if (info->attrs[L2TP_ATTR_LNS_MODE])
598 cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
600 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
601 cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
603 if (info->attrs[L2TP_ATTR_MTU])
604 cfg.mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
606 if (info->attrs[L2TP_ATTR_MRU])
607 cfg.mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
609 #ifdef CONFIG_MODULES
610 if (l2tp_nl_cmd_ops[cfg.pw_type] == NULL) {
612 request_module("net-l2tp-type-%u", cfg.pw_type);
616 if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) ||
617 (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) {
618 ret = -EPROTONOSUPPORT;
622 /* Check that pseudowire-specific params are present */
623 switch (cfg.pw_type) {
624 case L2TP_PWTYPE_NONE:
626 case L2TP_PWTYPE_ETH_VLAN:
627 if (!info->attrs[L2TP_ATTR_VLAN_ID]) {
632 case L2TP_PWTYPE_ETH:
634 case L2TP_PWTYPE_PPP:
635 case L2TP_PWTYPE_PPP_AC:
639 ret = -EPROTONOSUPPORT;
643 ret = l2tp_nl_cmd_ops[cfg.pw_type]->session_create(net, tunnel,
649 session = l2tp_session_get(net, tunnel, session_id, false);
651 ret = l2tp_session_notify(&l2tp_nl_family, info, session,
652 L2TP_CMD_SESSION_CREATE);
653 l2tp_session_dec_refcount(session);
658 l2tp_tunnel_dec_refcount(tunnel);
663 static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
666 struct l2tp_session *session;
669 session = l2tp_nl_session_get(info, true);
670 if (session == NULL) {
675 l2tp_session_notify(&l2tp_nl_family, info,
676 session, L2TP_CMD_SESSION_DELETE);
678 pw_type = session->pwtype;
679 if (pw_type < __L2TP_PWTYPE_MAX)
680 if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
681 ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
684 session->deref(session);
685 l2tp_session_dec_refcount(session);
691 static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
694 struct l2tp_session *session;
696 session = l2tp_nl_session_get(info, false);
697 if (session == NULL) {
702 if (info->attrs[L2TP_ATTR_DEBUG])
703 session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
705 if (info->attrs[L2TP_ATTR_DATA_SEQ])
706 session->data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
708 if (info->attrs[L2TP_ATTR_RECV_SEQ])
709 session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
711 if (info->attrs[L2TP_ATTR_SEND_SEQ]) {
712 session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
713 l2tp_session_set_header_len(session, session->tunnel->version);
716 if (info->attrs[L2TP_ATTR_LNS_MODE])
717 session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
719 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
720 session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
722 if (info->attrs[L2TP_ATTR_MTU])
723 session->mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
725 if (info->attrs[L2TP_ATTR_MRU])
726 session->mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
728 ret = l2tp_session_notify(&l2tp_nl_family, info,
729 session, L2TP_CMD_SESSION_MODIFY);
731 l2tp_session_dec_refcount(session);
737 static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
738 struct l2tp_session *session, u8 cmd)
742 struct l2tp_tunnel *tunnel = session->tunnel;
743 struct sock *sk = NULL;
747 hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
751 if (nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
752 nla_put_u32(skb, L2TP_ATTR_SESSION_ID, session->session_id) ||
753 nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
754 nla_put_u32(skb, L2TP_ATTR_PEER_SESSION_ID,
755 session->peer_session_id) ||
756 nla_put_u32(skb, L2TP_ATTR_DEBUG, session->debug) ||
757 nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype) ||
758 nla_put_u16(skb, L2TP_ATTR_MTU, session->mtu) ||
760 nla_put_u16(skb, L2TP_ATTR_MRU, session->mru)))
761 goto nla_put_failure;
763 if ((session->ifname[0] &&
764 nla_put_string(skb, L2TP_ATTR_IFNAME, session->ifname)) ||
765 (session->cookie_len &&
766 nla_put(skb, L2TP_ATTR_COOKIE, session->cookie_len,
767 &session->cookie[0])) ||
768 (session->peer_cookie_len &&
769 nla_put(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len,
770 &session->peer_cookie[0])) ||
771 nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
772 nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
773 nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
775 (((sk) && (sk->sk_policy[0] || sk->sk_policy[1])) &&
776 nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
778 (session->reorder_timeout &&
779 nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT,
780 session->reorder_timeout, L2TP_ATTR_PAD)))
781 goto nla_put_failure;
783 nest = nla_nest_start(skb, L2TP_ATTR_STATS);
785 goto nla_put_failure;
787 if (nla_put_u64_64bit(skb, L2TP_ATTR_TX_PACKETS,
788 atomic_long_read(&session->stats.tx_packets),
789 L2TP_ATTR_STATS_PAD) ||
790 nla_put_u64_64bit(skb, L2TP_ATTR_TX_BYTES,
791 atomic_long_read(&session->stats.tx_bytes),
792 L2TP_ATTR_STATS_PAD) ||
793 nla_put_u64_64bit(skb, L2TP_ATTR_TX_ERRORS,
794 atomic_long_read(&session->stats.tx_errors),
795 L2TP_ATTR_STATS_PAD) ||
796 nla_put_u64_64bit(skb, L2TP_ATTR_RX_PACKETS,
797 atomic_long_read(&session->stats.rx_packets),
798 L2TP_ATTR_STATS_PAD) ||
799 nla_put_u64_64bit(skb, L2TP_ATTR_RX_BYTES,
800 atomic_long_read(&session->stats.rx_bytes),
801 L2TP_ATTR_STATS_PAD) ||
802 nla_put_u64_64bit(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
803 atomic_long_read(&session->stats.rx_seq_discards),
804 L2TP_ATTR_STATS_PAD) ||
805 nla_put_u64_64bit(skb, L2TP_ATTR_RX_OOS_PACKETS,
806 atomic_long_read(&session->stats.rx_oos_packets),
807 L2TP_ATTR_STATS_PAD) ||
808 nla_put_u64_64bit(skb, L2TP_ATTR_RX_ERRORS,
809 atomic_long_read(&session->stats.rx_errors),
810 L2TP_ATTR_STATS_PAD))
811 goto nla_put_failure;
812 nla_nest_end(skb, nest);
814 genlmsg_end(skb, hdr);
818 genlmsg_cancel(skb, hdr);
822 static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
824 struct l2tp_session *session;
828 session = l2tp_nl_session_get(info, false);
829 if (session == NULL) {
834 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
840 ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
841 0, session, L2TP_CMD_SESSION_GET);
845 ret = genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
847 l2tp_session_dec_refcount(session);
854 l2tp_session_dec_refcount(session);
859 static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
861 struct net *net = sock_net(skb->sk);
862 struct l2tp_session *session;
863 struct l2tp_tunnel *tunnel = NULL;
864 int ti = cb->args[0];
865 int si = cb->args[1];
868 if (tunnel == NULL) {
869 tunnel = l2tp_tunnel_find_nth(net, ti);
874 session = l2tp_session_get_nth(tunnel, si, false);
875 if (session == NULL) {
882 if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).portid,
883 cb->nlh->nlmsg_seq, NLM_F_MULTI,
884 session, L2TP_CMD_SESSION_GET) < 0) {
885 l2tp_session_dec_refcount(session);
888 l2tp_session_dec_refcount(session);
900 static const struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
901 [L2TP_ATTR_NONE] = { .type = NLA_UNSPEC, },
902 [L2TP_ATTR_PW_TYPE] = { .type = NLA_U16, },
903 [L2TP_ATTR_ENCAP_TYPE] = { .type = NLA_U16, },
904 [L2TP_ATTR_OFFSET] = { .type = NLA_U16, },
905 [L2TP_ATTR_DATA_SEQ] = { .type = NLA_U8, },
906 [L2TP_ATTR_L2SPEC_TYPE] = { .type = NLA_U8, },
907 [L2TP_ATTR_L2SPEC_LEN] = { .type = NLA_U8, },
908 [L2TP_ATTR_PROTO_VERSION] = { .type = NLA_U8, },
909 [L2TP_ATTR_CONN_ID] = { .type = NLA_U32, },
910 [L2TP_ATTR_PEER_CONN_ID] = { .type = NLA_U32, },
911 [L2TP_ATTR_SESSION_ID] = { .type = NLA_U32, },
912 [L2TP_ATTR_PEER_SESSION_ID] = { .type = NLA_U32, },
913 [L2TP_ATTR_UDP_CSUM] = { .type = NLA_U8, },
914 [L2TP_ATTR_VLAN_ID] = { .type = NLA_U16, },
915 [L2TP_ATTR_DEBUG] = { .type = NLA_U32, },
916 [L2TP_ATTR_RECV_SEQ] = { .type = NLA_U8, },
917 [L2TP_ATTR_SEND_SEQ] = { .type = NLA_U8, },
918 [L2TP_ATTR_LNS_MODE] = { .type = NLA_U8, },
919 [L2TP_ATTR_USING_IPSEC] = { .type = NLA_U8, },
920 [L2TP_ATTR_RECV_TIMEOUT] = { .type = NLA_MSECS, },
921 [L2TP_ATTR_FD] = { .type = NLA_U32, },
922 [L2TP_ATTR_IP_SADDR] = { .type = NLA_U32, },
923 [L2TP_ATTR_IP_DADDR] = { .type = NLA_U32, },
924 [L2TP_ATTR_UDP_SPORT] = { .type = NLA_U16, },
925 [L2TP_ATTR_UDP_DPORT] = { .type = NLA_U16, },
926 [L2TP_ATTR_MTU] = { .type = NLA_U16, },
927 [L2TP_ATTR_MRU] = { .type = NLA_U16, },
928 [L2TP_ATTR_STATS] = { .type = NLA_NESTED, },
929 [L2TP_ATTR_IP6_SADDR] = {
931 .len = sizeof(struct in6_addr),
933 [L2TP_ATTR_IP6_DADDR] = {
935 .len = sizeof(struct in6_addr),
937 [L2TP_ATTR_IFNAME] = {
938 .type = NLA_NUL_STRING,
941 [L2TP_ATTR_COOKIE] = {
945 [L2TP_ATTR_PEER_COOKIE] = {
951 static const struct genl_ops l2tp_nl_ops[] = {
953 .cmd = L2TP_CMD_NOOP,
954 .doit = l2tp_nl_cmd_noop,
955 .policy = l2tp_nl_policy,
956 /* can be retrieved by unprivileged users */
959 .cmd = L2TP_CMD_TUNNEL_CREATE,
960 .doit = l2tp_nl_cmd_tunnel_create,
961 .policy = l2tp_nl_policy,
962 .flags = GENL_ADMIN_PERM,
965 .cmd = L2TP_CMD_TUNNEL_DELETE,
966 .doit = l2tp_nl_cmd_tunnel_delete,
967 .policy = l2tp_nl_policy,
968 .flags = GENL_ADMIN_PERM,
971 .cmd = L2TP_CMD_TUNNEL_MODIFY,
972 .doit = l2tp_nl_cmd_tunnel_modify,
973 .policy = l2tp_nl_policy,
974 .flags = GENL_ADMIN_PERM,
977 .cmd = L2TP_CMD_TUNNEL_GET,
978 .doit = l2tp_nl_cmd_tunnel_get,
979 .dumpit = l2tp_nl_cmd_tunnel_dump,
980 .policy = l2tp_nl_policy,
981 .flags = GENL_ADMIN_PERM,
984 .cmd = L2TP_CMD_SESSION_CREATE,
985 .doit = l2tp_nl_cmd_session_create,
986 .policy = l2tp_nl_policy,
987 .flags = GENL_ADMIN_PERM,
990 .cmd = L2TP_CMD_SESSION_DELETE,
991 .doit = l2tp_nl_cmd_session_delete,
992 .policy = l2tp_nl_policy,
993 .flags = GENL_ADMIN_PERM,
996 .cmd = L2TP_CMD_SESSION_MODIFY,
997 .doit = l2tp_nl_cmd_session_modify,
998 .policy = l2tp_nl_policy,
999 .flags = GENL_ADMIN_PERM,
1002 .cmd = L2TP_CMD_SESSION_GET,
1003 .doit = l2tp_nl_cmd_session_get,
1004 .dumpit = l2tp_nl_cmd_session_dump,
1005 .policy = l2tp_nl_policy,
1006 .flags = GENL_ADMIN_PERM,
1010 static struct genl_family l2tp_nl_family __ro_after_init = {
1011 .name = L2TP_GENL_NAME,
1012 .version = L2TP_GENL_VERSION,
1014 .maxattr = L2TP_ATTR_MAX,
1016 .module = THIS_MODULE,
1018 .n_ops = ARRAY_SIZE(l2tp_nl_ops),
1019 .mcgrps = l2tp_multicast_group,
1020 .n_mcgrps = ARRAY_SIZE(l2tp_multicast_group),
1023 int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
1028 if (pw_type >= __L2TP_PWTYPE_MAX)
1033 if (l2tp_nl_cmd_ops[pw_type])
1036 l2tp_nl_cmd_ops[pw_type] = ops;
1044 EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
1046 void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
1048 if (pw_type < __L2TP_PWTYPE_MAX) {
1050 l2tp_nl_cmd_ops[pw_type] = NULL;
1054 EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
1056 static int __init l2tp_nl_init(void)
1058 pr_info("L2TP netlink interface\n");
1059 return genl_register_family(&l2tp_nl_family);
1062 static void l2tp_nl_cleanup(void)
1064 genl_unregister_family(&l2tp_nl_family);
1067 module_init(l2tp_nl_init);
1068 module_exit(l2tp_nl_cleanup);
1070 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1071 MODULE_DESCRIPTION("L2TP netlink");
1072 MODULE_LICENSE("GPL");
1073 MODULE_VERSION("1.0");
1074 MODULE_ALIAS_GENL_FAMILY("l2tp");