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 = {
35 .id = GENL_ID_GENERATE,
36 .name = L2TP_GENL_NAME,
37 .version = L2TP_GENL_VERSION,
39 .maxattr = L2TP_ATTR_MAX,
43 static const struct genl_multicast_group l2tp_multicast_group[] = {
45 .name = L2TP_GENL_MCGROUP,
49 static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq,
50 int flags, struct l2tp_tunnel *tunnel, u8 cmd);
51 static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq,
52 int flags, struct l2tp_session *session,
55 /* Accessed under genl lock */
56 static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX];
58 static struct l2tp_session *l2tp_nl_session_find(struct genl_info *info)
63 struct l2tp_tunnel *tunnel;
64 struct l2tp_session *session = NULL;
65 struct net *net = genl_info_net(info);
67 if (info->attrs[L2TP_ATTR_IFNAME]) {
68 ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
69 session = l2tp_session_find_by_ifname(net, ifname);
70 } else if ((info->attrs[L2TP_ATTR_SESSION_ID]) &&
71 (info->attrs[L2TP_ATTR_CONN_ID])) {
72 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
73 session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
74 tunnel = l2tp_tunnel_find(net, tunnel_id);
76 session = l2tp_session_find(net, tunnel, session_id);
82 static int l2tp_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
88 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
94 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
95 &l2tp_nl_family, 0, L2TP_CMD_NOOP);
101 genlmsg_end(msg, hdr);
103 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
112 static int l2tp_tunnel_notify(struct genl_family *family,
113 struct genl_info *info,
114 struct l2tp_tunnel *tunnel,
120 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
124 ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
125 NLM_F_ACK, tunnel, cmd);
128 return genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC);
135 static int l2tp_session_notify(struct genl_family *family,
136 struct genl_info *info,
137 struct l2tp_session *session,
143 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
147 ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
148 NLM_F_ACK, session, cmd);
151 return genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC);
158 static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info)
165 struct l2tp_tunnel_cfg cfg = { 0, };
166 struct l2tp_tunnel *tunnel;
167 struct net *net = genl_info_net(info);
169 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
173 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
175 if (!info->attrs[L2TP_ATTR_PEER_CONN_ID]) {
179 peer_tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_CONN_ID]);
181 if (!info->attrs[L2TP_ATTR_PROTO_VERSION]) {
185 proto_version = nla_get_u8(info->attrs[L2TP_ATTR_PROTO_VERSION]);
187 if (!info->attrs[L2TP_ATTR_ENCAP_TYPE]) {
191 cfg.encap = nla_get_u16(info->attrs[L2TP_ATTR_ENCAP_TYPE]);
194 if (info->attrs[L2TP_ATTR_FD]) {
195 fd = nla_get_u32(info->attrs[L2TP_ATTR_FD]);
197 #if IS_ENABLED(CONFIG_IPV6)
198 if (info->attrs[L2TP_ATTR_IP6_SADDR] &&
199 info->attrs[L2TP_ATTR_IP6_DADDR]) {
200 cfg.local_ip6 = nla_data(
201 info->attrs[L2TP_ATTR_IP6_SADDR]);
202 cfg.peer_ip6 = nla_data(
203 info->attrs[L2TP_ATTR_IP6_DADDR]);
206 if (info->attrs[L2TP_ATTR_IP_SADDR] &&
207 info->attrs[L2TP_ATTR_IP_DADDR]) {
208 cfg.local_ip.s_addr = nla_get_be32(
209 info->attrs[L2TP_ATTR_IP_SADDR]);
210 cfg.peer_ip.s_addr = nla_get_be32(
211 info->attrs[L2TP_ATTR_IP_DADDR]);
216 if (info->attrs[L2TP_ATTR_UDP_SPORT])
217 cfg.local_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_SPORT]);
218 if (info->attrs[L2TP_ATTR_UDP_DPORT])
219 cfg.peer_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_DPORT]);
220 if (info->attrs[L2TP_ATTR_UDP_CSUM])
221 cfg.use_udp_checksums = nla_get_flag(info->attrs[L2TP_ATTR_UDP_CSUM]);
223 #if IS_ENABLED(CONFIG_IPV6)
224 if (info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_TX])
225 cfg.udp6_zero_tx_checksums = nla_get_flag(info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_TX]);
226 if (info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_RX])
227 cfg.udp6_zero_rx_checksums = nla_get_flag(info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_RX]);
231 if (info->attrs[L2TP_ATTR_DEBUG])
232 cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
234 tunnel = l2tp_tunnel_find(net, tunnel_id);
235 if (tunnel != NULL) {
242 case L2TP_ENCAPTYPE_UDP:
243 case L2TP_ENCAPTYPE_IP:
244 ret = l2tp_tunnel_create(net, fd, proto_version, tunnel_id,
245 peer_tunnel_id, &cfg, &tunnel);
250 ret = l2tp_tunnel_notify(&l2tp_nl_family, info,
251 tunnel, L2TP_CMD_TUNNEL_CREATE);
256 static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info)
258 struct l2tp_tunnel *tunnel;
261 struct net *net = genl_info_net(info);
263 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
267 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
269 tunnel = l2tp_tunnel_find(net, tunnel_id);
270 if (tunnel == NULL) {
275 l2tp_tunnel_notify(&l2tp_nl_family, info,
276 tunnel, L2TP_CMD_TUNNEL_DELETE);
278 (void) l2tp_tunnel_delete(tunnel);
284 static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info)
286 struct l2tp_tunnel *tunnel;
289 struct net *net = genl_info_net(info);
291 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
295 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
297 tunnel = l2tp_tunnel_find(net, tunnel_id);
298 if (tunnel == NULL) {
303 if (info->attrs[L2TP_ATTR_DEBUG])
304 tunnel->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
306 ret = l2tp_tunnel_notify(&l2tp_nl_family, info,
307 tunnel, L2TP_CMD_TUNNEL_MODIFY);
313 static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
314 struct l2tp_tunnel *tunnel, u8 cmd)
318 struct sock *sk = NULL;
319 struct inet_sock *inet;
320 #if IS_ENABLED(CONFIG_IPV6)
321 struct ipv6_pinfo *np = NULL;
324 hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
328 if (nla_put_u8(skb, L2TP_ATTR_PROTO_VERSION, tunnel->version) ||
329 nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
330 nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
331 nla_put_u32(skb, L2TP_ATTR_DEBUG, tunnel->debug) ||
332 nla_put_u16(skb, L2TP_ATTR_ENCAP_TYPE, tunnel->encap))
333 goto nla_put_failure;
335 nest = nla_nest_start(skb, L2TP_ATTR_STATS);
337 goto nla_put_failure;
339 if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS,
340 atomic_long_read(&tunnel->stats.tx_packets)) ||
341 nla_put_u64(skb, L2TP_ATTR_TX_BYTES,
342 atomic_long_read(&tunnel->stats.tx_bytes)) ||
343 nla_put_u64(skb, L2TP_ATTR_TX_ERRORS,
344 atomic_long_read(&tunnel->stats.tx_errors)) ||
345 nla_put_u64(skb, L2TP_ATTR_RX_PACKETS,
346 atomic_long_read(&tunnel->stats.rx_packets)) ||
347 nla_put_u64(skb, L2TP_ATTR_RX_BYTES,
348 atomic_long_read(&tunnel->stats.rx_bytes)) ||
349 nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
350 atomic_long_read(&tunnel->stats.rx_seq_discards)) ||
351 nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
352 atomic_long_read(&tunnel->stats.rx_oos_packets)) ||
353 nla_put_u64(skb, L2TP_ATTR_RX_ERRORS,
354 atomic_long_read(&tunnel->stats.rx_errors)))
355 goto nla_put_failure;
356 nla_nest_end(skb, nest);
362 #if IS_ENABLED(CONFIG_IPV6)
363 if (sk->sk_family == AF_INET6)
369 switch (tunnel->encap) {
370 case L2TP_ENCAPTYPE_UDP:
371 if (nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
372 nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)) ||
373 nla_put_u8(skb, L2TP_ATTR_UDP_CSUM, !sk->sk_no_check_tx))
374 goto nla_put_failure;
376 case L2TP_ENCAPTYPE_IP:
377 #if IS_ENABLED(CONFIG_IPV6)
379 if (nla_put(skb, L2TP_ATTR_IP6_SADDR, sizeof(np->saddr),
381 nla_put(skb, L2TP_ATTR_IP6_DADDR, sizeof(sk->sk_v6_daddr),
383 goto nla_put_failure;
386 if (nla_put_be32(skb, L2TP_ATTR_IP_SADDR, inet->inet_saddr) ||
387 nla_put_be32(skb, L2TP_ATTR_IP_DADDR, inet->inet_daddr))
388 goto nla_put_failure;
393 genlmsg_end(skb, hdr);
397 genlmsg_cancel(skb, hdr);
401 static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
403 struct l2tp_tunnel *tunnel;
407 struct net *net = genl_info_net(info);
409 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
414 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
416 tunnel = l2tp_tunnel_find(net, tunnel_id);
417 if (tunnel == NULL) {
422 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
428 ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
429 NLM_F_ACK, tunnel, L2TP_CMD_TUNNEL_GET);
433 return genlmsg_unicast(net, msg, info->snd_portid);
442 static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
444 int ti = cb->args[0];
445 struct l2tp_tunnel *tunnel;
446 struct net *net = sock_net(skb->sk);
449 tunnel = l2tp_tunnel_find_nth(net, ti);
453 if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).portid,
454 cb->nlh->nlmsg_seq, NLM_F_MULTI,
455 tunnel, L2TP_CMD_TUNNEL_GET) < 0)
467 static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
473 struct l2tp_tunnel *tunnel;
474 struct l2tp_session *session;
475 struct l2tp_session_cfg cfg = { 0, };
476 struct net *net = genl_info_net(info);
478 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
482 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
483 tunnel = l2tp_tunnel_find(net, tunnel_id);
489 if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
493 session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
494 session = l2tp_session_find(net, tunnel, session_id);
500 if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
504 peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
506 if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
510 cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
511 if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
516 if (tunnel->version > 2) {
517 if (info->attrs[L2TP_ATTR_OFFSET])
518 cfg.offset = nla_get_u16(info->attrs[L2TP_ATTR_OFFSET]);
520 if (info->attrs[L2TP_ATTR_DATA_SEQ])
521 cfg.data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
523 cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
524 if (info->attrs[L2TP_ATTR_L2SPEC_TYPE])
525 cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
527 cfg.l2specific_len = 4;
528 if (info->attrs[L2TP_ATTR_L2SPEC_LEN])
529 cfg.l2specific_len = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_LEN]);
531 if (info->attrs[L2TP_ATTR_COOKIE]) {
532 u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
537 cfg.cookie_len = len;
538 memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
540 if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
541 u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
546 cfg.peer_cookie_len = len;
547 memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
549 if (info->attrs[L2TP_ATTR_IFNAME])
550 cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
552 if (info->attrs[L2TP_ATTR_VLAN_ID])
553 cfg.vlan_id = nla_get_u16(info->attrs[L2TP_ATTR_VLAN_ID]);
556 if (info->attrs[L2TP_ATTR_DEBUG])
557 cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
559 if (info->attrs[L2TP_ATTR_RECV_SEQ])
560 cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
562 if (info->attrs[L2TP_ATTR_SEND_SEQ])
563 cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
565 if (info->attrs[L2TP_ATTR_LNS_MODE])
566 cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
568 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
569 cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
571 if (info->attrs[L2TP_ATTR_MTU])
572 cfg.mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
574 if (info->attrs[L2TP_ATTR_MRU])
575 cfg.mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
577 if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) ||
578 (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) {
579 ret = -EPROTONOSUPPORT;
583 /* Check that pseudowire-specific params are present */
584 switch (cfg.pw_type) {
585 case L2TP_PWTYPE_NONE:
587 case L2TP_PWTYPE_ETH_VLAN:
588 if (!info->attrs[L2TP_ATTR_VLAN_ID]) {
593 case L2TP_PWTYPE_ETH:
595 case L2TP_PWTYPE_PPP:
596 case L2TP_PWTYPE_PPP_AC:
600 ret = -EPROTONOSUPPORT;
604 ret = -EPROTONOSUPPORT;
605 if (l2tp_nl_cmd_ops[cfg.pw_type]->session_create)
606 ret = (*l2tp_nl_cmd_ops[cfg.pw_type]->session_create)(net, tunnel_id,
607 session_id, peer_session_id, &cfg);
610 session = l2tp_session_find(net, tunnel, session_id);
612 ret = l2tp_session_notify(&l2tp_nl_family, info, session,
613 L2TP_CMD_SESSION_CREATE);
620 static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
623 struct l2tp_session *session;
626 session = l2tp_nl_session_find(info);
627 if (session == NULL) {
632 l2tp_session_notify(&l2tp_nl_family, info,
633 session, L2TP_CMD_SESSION_DELETE);
635 pw_type = session->pwtype;
636 if (pw_type < __L2TP_PWTYPE_MAX)
637 if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
638 ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
644 static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
647 struct l2tp_session *session;
649 session = l2tp_nl_session_find(info);
650 if (session == NULL) {
655 if (info->attrs[L2TP_ATTR_DEBUG])
656 session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
658 if (info->attrs[L2TP_ATTR_DATA_SEQ])
659 session->data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
661 if (info->attrs[L2TP_ATTR_RECV_SEQ])
662 session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
664 if (info->attrs[L2TP_ATTR_SEND_SEQ]) {
665 session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
666 l2tp_session_set_header_len(session, session->tunnel->version);
669 if (info->attrs[L2TP_ATTR_LNS_MODE])
670 session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
672 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
673 session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
675 if (info->attrs[L2TP_ATTR_MTU])
676 session->mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
678 if (info->attrs[L2TP_ATTR_MRU])
679 session->mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
681 ret = l2tp_session_notify(&l2tp_nl_family, info,
682 session, L2TP_CMD_SESSION_MODIFY);
688 static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
689 struct l2tp_session *session, u8 cmd)
693 struct l2tp_tunnel *tunnel = session->tunnel;
694 struct sock *sk = NULL;
698 hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
702 if (nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
703 nla_put_u32(skb, L2TP_ATTR_SESSION_ID, session->session_id) ||
704 nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
705 nla_put_u32(skb, L2TP_ATTR_PEER_SESSION_ID,
706 session->peer_session_id) ||
707 nla_put_u32(skb, L2TP_ATTR_DEBUG, session->debug) ||
708 nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype) ||
709 nla_put_u16(skb, L2TP_ATTR_MTU, session->mtu) ||
711 nla_put_u16(skb, L2TP_ATTR_MRU, session->mru)))
712 goto nla_put_failure;
714 if ((session->ifname[0] &&
715 nla_put_string(skb, L2TP_ATTR_IFNAME, session->ifname)) ||
716 (session->cookie_len &&
717 nla_put(skb, L2TP_ATTR_COOKIE, session->cookie_len,
718 &session->cookie[0])) ||
719 (session->peer_cookie_len &&
720 nla_put(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len,
721 &session->peer_cookie[0])) ||
722 nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
723 nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
724 nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
726 (((sk) && (sk->sk_policy[0] || sk->sk_policy[1])) &&
727 nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
729 (session->reorder_timeout &&
730 nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT, session->reorder_timeout)))
731 goto nla_put_failure;
733 nest = nla_nest_start(skb, L2TP_ATTR_STATS);
735 goto nla_put_failure;
737 if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS,
738 atomic_long_read(&session->stats.tx_packets)) ||
739 nla_put_u64(skb, L2TP_ATTR_TX_BYTES,
740 atomic_long_read(&session->stats.tx_bytes)) ||
741 nla_put_u64(skb, L2TP_ATTR_TX_ERRORS,
742 atomic_long_read(&session->stats.tx_errors)) ||
743 nla_put_u64(skb, L2TP_ATTR_RX_PACKETS,
744 atomic_long_read(&session->stats.rx_packets)) ||
745 nla_put_u64(skb, L2TP_ATTR_RX_BYTES,
746 atomic_long_read(&session->stats.rx_bytes)) ||
747 nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
748 atomic_long_read(&session->stats.rx_seq_discards)) ||
749 nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
750 atomic_long_read(&session->stats.rx_oos_packets)) ||
751 nla_put_u64(skb, L2TP_ATTR_RX_ERRORS,
752 atomic_long_read(&session->stats.rx_errors)))
753 goto nla_put_failure;
754 nla_nest_end(skb, nest);
756 genlmsg_end(skb, hdr);
760 genlmsg_cancel(skb, hdr);
764 static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
766 struct l2tp_session *session;
770 session = l2tp_nl_session_find(info);
771 if (session == NULL) {
776 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
782 ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
783 0, session, L2TP_CMD_SESSION_GET);
787 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
796 static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
798 struct net *net = sock_net(skb->sk);
799 struct l2tp_session *session;
800 struct l2tp_tunnel *tunnel = NULL;
801 int ti = cb->args[0];
802 int si = cb->args[1];
805 if (tunnel == NULL) {
806 tunnel = l2tp_tunnel_find_nth(net, ti);
811 session = l2tp_session_find_nth(tunnel, si);
812 if (session == NULL) {
819 if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).portid,
820 cb->nlh->nlmsg_seq, NLM_F_MULTI,
821 session, L2TP_CMD_SESSION_GET) < 0)
834 static struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
835 [L2TP_ATTR_NONE] = { .type = NLA_UNSPEC, },
836 [L2TP_ATTR_PW_TYPE] = { .type = NLA_U16, },
837 [L2TP_ATTR_ENCAP_TYPE] = { .type = NLA_U16, },
838 [L2TP_ATTR_OFFSET] = { .type = NLA_U16, },
839 [L2TP_ATTR_DATA_SEQ] = { .type = NLA_U8, },
840 [L2TP_ATTR_L2SPEC_TYPE] = { .type = NLA_U8, },
841 [L2TP_ATTR_L2SPEC_LEN] = { .type = NLA_U8, },
842 [L2TP_ATTR_PROTO_VERSION] = { .type = NLA_U8, },
843 [L2TP_ATTR_CONN_ID] = { .type = NLA_U32, },
844 [L2TP_ATTR_PEER_CONN_ID] = { .type = NLA_U32, },
845 [L2TP_ATTR_SESSION_ID] = { .type = NLA_U32, },
846 [L2TP_ATTR_PEER_SESSION_ID] = { .type = NLA_U32, },
847 [L2TP_ATTR_UDP_CSUM] = { .type = NLA_U8, },
848 [L2TP_ATTR_VLAN_ID] = { .type = NLA_U16, },
849 [L2TP_ATTR_DEBUG] = { .type = NLA_U32, },
850 [L2TP_ATTR_RECV_SEQ] = { .type = NLA_U8, },
851 [L2TP_ATTR_SEND_SEQ] = { .type = NLA_U8, },
852 [L2TP_ATTR_LNS_MODE] = { .type = NLA_U8, },
853 [L2TP_ATTR_USING_IPSEC] = { .type = NLA_U8, },
854 [L2TP_ATTR_RECV_TIMEOUT] = { .type = NLA_MSECS, },
855 [L2TP_ATTR_FD] = { .type = NLA_U32, },
856 [L2TP_ATTR_IP_SADDR] = { .type = NLA_U32, },
857 [L2TP_ATTR_IP_DADDR] = { .type = NLA_U32, },
858 [L2TP_ATTR_UDP_SPORT] = { .type = NLA_U16, },
859 [L2TP_ATTR_UDP_DPORT] = { .type = NLA_U16, },
860 [L2TP_ATTR_MTU] = { .type = NLA_U16, },
861 [L2TP_ATTR_MRU] = { .type = NLA_U16, },
862 [L2TP_ATTR_STATS] = { .type = NLA_NESTED, },
863 [L2TP_ATTR_IP6_SADDR] = {
865 .len = sizeof(struct in6_addr),
867 [L2TP_ATTR_IP6_DADDR] = {
869 .len = sizeof(struct in6_addr),
871 [L2TP_ATTR_IFNAME] = {
872 .type = NLA_NUL_STRING,
875 [L2TP_ATTR_COOKIE] = {
879 [L2TP_ATTR_PEER_COOKIE] = {
885 static const struct genl_ops l2tp_nl_ops[] = {
887 .cmd = L2TP_CMD_NOOP,
888 .doit = l2tp_nl_cmd_noop,
889 .policy = l2tp_nl_policy,
890 /* can be retrieved by unprivileged users */
893 .cmd = L2TP_CMD_TUNNEL_CREATE,
894 .doit = l2tp_nl_cmd_tunnel_create,
895 .policy = l2tp_nl_policy,
896 .flags = GENL_ADMIN_PERM,
899 .cmd = L2TP_CMD_TUNNEL_DELETE,
900 .doit = l2tp_nl_cmd_tunnel_delete,
901 .policy = l2tp_nl_policy,
902 .flags = GENL_ADMIN_PERM,
905 .cmd = L2TP_CMD_TUNNEL_MODIFY,
906 .doit = l2tp_nl_cmd_tunnel_modify,
907 .policy = l2tp_nl_policy,
908 .flags = GENL_ADMIN_PERM,
911 .cmd = L2TP_CMD_TUNNEL_GET,
912 .doit = l2tp_nl_cmd_tunnel_get,
913 .dumpit = l2tp_nl_cmd_tunnel_dump,
914 .policy = l2tp_nl_policy,
915 .flags = GENL_ADMIN_PERM,
918 .cmd = L2TP_CMD_SESSION_CREATE,
919 .doit = l2tp_nl_cmd_session_create,
920 .policy = l2tp_nl_policy,
921 .flags = GENL_ADMIN_PERM,
924 .cmd = L2TP_CMD_SESSION_DELETE,
925 .doit = l2tp_nl_cmd_session_delete,
926 .policy = l2tp_nl_policy,
927 .flags = GENL_ADMIN_PERM,
930 .cmd = L2TP_CMD_SESSION_MODIFY,
931 .doit = l2tp_nl_cmd_session_modify,
932 .policy = l2tp_nl_policy,
933 .flags = GENL_ADMIN_PERM,
936 .cmd = L2TP_CMD_SESSION_GET,
937 .doit = l2tp_nl_cmd_session_get,
938 .dumpit = l2tp_nl_cmd_session_dump,
939 .policy = l2tp_nl_policy,
940 .flags = GENL_ADMIN_PERM,
944 int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
949 if (pw_type >= __L2TP_PWTYPE_MAX)
954 if (l2tp_nl_cmd_ops[pw_type])
957 l2tp_nl_cmd_ops[pw_type] = ops;
965 EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
967 void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
969 if (pw_type < __L2TP_PWTYPE_MAX) {
971 l2tp_nl_cmd_ops[pw_type] = NULL;
975 EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
977 static int l2tp_nl_init(void)
979 pr_info("L2TP netlink interface\n");
980 return genl_register_family_with_ops_groups(&l2tp_nl_family,
982 l2tp_multicast_group);
985 static void l2tp_nl_cleanup(void)
987 genl_unregister_family(&l2tp_nl_family);
990 module_init(l2tp_nl_init);
991 module_exit(l2tp_nl_cleanup);
993 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
994 MODULE_DESCRIPTION("L2TP netlink");
995 MODULE_LICENSE("GPL");
996 MODULE_VERSION("1.0");
997 MODULE_ALIAS_GENL_FAMILY("l2tp");