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
,
42 /* Accessed under genl lock */
43 static const struct l2tp_nl_cmd_ops
*l2tp_nl_cmd_ops
[__L2TP_PWTYPE_MAX
];
45 static struct l2tp_session
*l2tp_nl_session_find(struct genl_info
*info
)
50 struct l2tp_tunnel
*tunnel
;
51 struct l2tp_session
*session
= NULL
;
52 struct net
*net
= genl_info_net(info
);
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
);
63 session
= l2tp_session_find(net
, tunnel
, session_id
);
69 static int l2tp_nl_cmd_noop(struct sk_buff
*skb
, struct genl_info
*info
)
75 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
81 hdr
= genlmsg_put(msg
, info
->snd_pid
, info
->snd_seq
,
82 &l2tp_nl_family
, 0, L2TP_CMD_NOOP
);
88 genlmsg_end(msg
, hdr
);
90 return genlmsg_unicast(genl_info_net(info
), msg
, info
->snd_pid
);
99 static int l2tp_nl_cmd_tunnel_create(struct sk_buff
*skb
, struct genl_info
*info
)
106 struct l2tp_tunnel_cfg cfg
= { 0, };
107 struct l2tp_tunnel
*tunnel
;
108 struct net
*net
= genl_info_net(info
);
110 if (!info
->attrs
[L2TP_ATTR_CONN_ID
]) {
114 tunnel_id
= nla_get_u32(info
->attrs
[L2TP_ATTR_CONN_ID
]);
116 if (!info
->attrs
[L2TP_ATTR_PEER_CONN_ID
]) {
120 peer_tunnel_id
= nla_get_u32(info
->attrs
[L2TP_ATTR_PEER_CONN_ID
]);
122 if (!info
->attrs
[L2TP_ATTR_PROTO_VERSION
]) {
126 proto_version
= nla_get_u8(info
->attrs
[L2TP_ATTR_PROTO_VERSION
]);
128 if (!info
->attrs
[L2TP_ATTR_ENCAP_TYPE
]) {
132 cfg
.encap
= nla_get_u16(info
->attrs
[L2TP_ATTR_ENCAP_TYPE
]);
135 if (info
->attrs
[L2TP_ATTR_FD
]) {
136 fd
= nla_get_u32(info
->attrs
[L2TP_ATTR_FD
]);
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
]);
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
]);
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
]);
165 if (info
->attrs
[L2TP_ATTR_DEBUG
])
166 cfg
.debug
= nla_get_u32(info
->attrs
[L2TP_ATTR_DEBUG
]);
168 tunnel
= l2tp_tunnel_find(net
, tunnel_id
);
169 if (tunnel
!= NULL
) {
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
);
187 static int l2tp_nl_cmd_tunnel_delete(struct sk_buff
*skb
, struct genl_info
*info
)
189 struct l2tp_tunnel
*tunnel
;
192 struct net
*net
= genl_info_net(info
);
194 if (!info
->attrs
[L2TP_ATTR_CONN_ID
]) {
198 tunnel_id
= nla_get_u32(info
->attrs
[L2TP_ATTR_CONN_ID
]);
200 tunnel
= l2tp_tunnel_find(net
, tunnel_id
);
201 if (tunnel
== NULL
) {
206 (void) l2tp_tunnel_delete(tunnel
);
212 static int l2tp_nl_cmd_tunnel_modify(struct sk_buff
*skb
, struct genl_info
*info
)
214 struct l2tp_tunnel
*tunnel
;
217 struct net
*net
= genl_info_net(info
);
219 if (!info
->attrs
[L2TP_ATTR_CONN_ID
]) {
223 tunnel_id
= nla_get_u32(info
->attrs
[L2TP_ATTR_CONN_ID
]);
225 tunnel
= l2tp_tunnel_find(net
, tunnel_id
);
226 if (tunnel
== NULL
) {
231 if (info
->attrs
[L2TP_ATTR_DEBUG
])
232 tunnel
->debug
= nla_get_u32(info
->attrs
[L2TP_ATTR_DEBUG
]);
238 static int l2tp_nl_tunnel_send(struct sk_buff
*skb
, u32 pid
, u32 seq
, int flags
,
239 struct l2tp_tunnel
*tunnel
)
243 struct sock
*sk
= NULL
;
244 struct inet_sock
*inet
;
245 #if IS_ENABLED(CONFIG_IPV6)
246 struct ipv6_pinfo
*np
= NULL
;
248 struct l2tp_stats stats
;
251 hdr
= genlmsg_put(skb
, pid
, seq
, &l2tp_nl_family
, flags
,
252 L2TP_CMD_TUNNEL_GET
);
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
;
263 nest
= nla_nest_start(skb
, L2TP_ATTR_STATS
);
265 goto nla_put_failure
;
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
));
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
);
296 #if IS_ENABLED(CONFIG_IPV6)
297 if (sk
->sk_family
== AF_INET6
)
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
;
311 case L2TP_ENCAPTYPE_IP
:
312 #if IS_ENABLED(CONFIG_IPV6)
314 if (nla_put(skb
, L2TP_ATTR_IP6_SADDR
, sizeof(np
->saddr
),
316 nla_put(skb
, L2TP_ATTR_IP6_DADDR
, sizeof(np
->daddr
),
318 goto nla_put_failure
;
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
;
328 return genlmsg_end(skb
, hdr
);
331 genlmsg_cancel(skb
, hdr
);
335 static int l2tp_nl_cmd_tunnel_get(struct sk_buff
*skb
, struct genl_info
*info
)
337 struct l2tp_tunnel
*tunnel
;
341 struct net
*net
= genl_info_net(info
);
343 if (!info
->attrs
[L2TP_ATTR_CONN_ID
]) {
348 tunnel_id
= nla_get_u32(info
->attrs
[L2TP_ATTR_CONN_ID
]);
350 tunnel
= l2tp_tunnel_find(net
, tunnel_id
);
351 if (tunnel
== NULL
) {
356 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
362 ret
= l2tp_nl_tunnel_send(msg
, info
->snd_pid
, info
->snd_seq
,
367 return genlmsg_unicast(net
, msg
, info
->snd_pid
);
376 static int l2tp_nl_cmd_tunnel_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
378 int ti
= cb
->args
[0];
379 struct l2tp_tunnel
*tunnel
;
380 struct net
*net
= sock_net(skb
->sk
);
383 tunnel
= l2tp_tunnel_find_nth(net
, ti
);
387 if (l2tp_nl_tunnel_send(skb
, NETLINK_CB(cb
->skb
).pid
,
388 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
401 static int l2tp_nl_cmd_session_create(struct sk_buff
*skb
, struct genl_info
*info
)
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
);
412 if (!info
->attrs
[L2TP_ATTR_CONN_ID
]) {
416 tunnel_id
= nla_get_u32(info
->attrs
[L2TP_ATTR_CONN_ID
]);
417 tunnel
= l2tp_tunnel_find(net
, tunnel_id
);
423 if (!info
->attrs
[L2TP_ATTR_SESSION_ID
]) {
427 session_id
= nla_get_u32(info
->attrs
[L2TP_ATTR_SESSION_ID
]);
428 session
= l2tp_session_find(net
, tunnel
, session_id
);
434 if (!info
->attrs
[L2TP_ATTR_PEER_SESSION_ID
]) {
438 peer_session_id
= nla_get_u32(info
->attrs
[L2TP_ATTR_PEER_SESSION_ID
]);
440 if (!info
->attrs
[L2TP_ATTR_PW_TYPE
]) {
444 cfg
.pw_type
= nla_get_u16(info
->attrs
[L2TP_ATTR_PW_TYPE
]);
445 if (cfg
.pw_type
>= __L2TP_PWTYPE_MAX
) {
450 if (tunnel
->version
> 2) {
451 if (info
->attrs
[L2TP_ATTR_OFFSET
])
452 cfg
.offset
= nla_get_u16(info
->attrs
[L2TP_ATTR_OFFSET
]);
454 if (info
->attrs
[L2TP_ATTR_DATA_SEQ
])
455 cfg
.data_seq
= nla_get_u8(info
->attrs
[L2TP_ATTR_DATA_SEQ
]);
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
]);
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
]);
465 if (info
->attrs
[L2TP_ATTR_COOKIE
]) {
466 u16 len
= nla_len(info
->attrs
[L2TP_ATTR_COOKIE
]);
471 cfg
.cookie_len
= len
;
472 memcpy(&cfg
.cookie
[0], nla_data(info
->attrs
[L2TP_ATTR_COOKIE
]), len
);
474 if (info
->attrs
[L2TP_ATTR_PEER_COOKIE
]) {
475 u16 len
= nla_len(info
->attrs
[L2TP_ATTR_PEER_COOKIE
]);
480 cfg
.peer_cookie_len
= len
;
481 memcpy(&cfg
.peer_cookie
[0], nla_data(info
->attrs
[L2TP_ATTR_PEER_COOKIE
]), len
);
483 if (info
->attrs
[L2TP_ATTR_IFNAME
])
484 cfg
.ifname
= nla_data(info
->attrs
[L2TP_ATTR_IFNAME
]);
486 if (info
->attrs
[L2TP_ATTR_VLAN_ID
])
487 cfg
.vlan_id
= nla_get_u16(info
->attrs
[L2TP_ATTR_VLAN_ID
]);
490 if (info
->attrs
[L2TP_ATTR_DEBUG
])
491 cfg
.debug
= nla_get_u32(info
->attrs
[L2TP_ATTR_DEBUG
]);
493 if (info
->attrs
[L2TP_ATTR_RECV_SEQ
])
494 cfg
.recv_seq
= nla_get_u8(info
->attrs
[L2TP_ATTR_RECV_SEQ
]);
496 if (info
->attrs
[L2TP_ATTR_SEND_SEQ
])
497 cfg
.send_seq
= nla_get_u8(info
->attrs
[L2TP_ATTR_SEND_SEQ
]);
499 if (info
->attrs
[L2TP_ATTR_LNS_MODE
])
500 cfg
.lns_mode
= nla_get_u8(info
->attrs
[L2TP_ATTR_LNS_MODE
]);
502 if (info
->attrs
[L2TP_ATTR_RECV_TIMEOUT
])
503 cfg
.reorder_timeout
= nla_get_msecs(info
->attrs
[L2TP_ATTR_RECV_TIMEOUT
]);
505 if (info
->attrs
[L2TP_ATTR_MTU
])
506 cfg
.mtu
= nla_get_u16(info
->attrs
[L2TP_ATTR_MTU
]);
508 if (info
->attrs
[L2TP_ATTR_MRU
])
509 cfg
.mru
= nla_get_u16(info
->attrs
[L2TP_ATTR_MRU
]);
511 if ((l2tp_nl_cmd_ops
[cfg
.pw_type
] == NULL
) ||
512 (l2tp_nl_cmd_ops
[cfg
.pw_type
]->session_create
== NULL
)) {
513 ret
= -EPROTONOSUPPORT
;
517 /* Check that pseudowire-specific params are present */
518 switch (cfg
.pw_type
) {
519 case L2TP_PWTYPE_NONE
:
521 case L2TP_PWTYPE_ETH_VLAN
:
522 if (!info
->attrs
[L2TP_ATTR_VLAN_ID
]) {
527 case L2TP_PWTYPE_ETH
:
529 case L2TP_PWTYPE_PPP
:
530 case L2TP_PWTYPE_PPP_AC
:
534 ret
= -EPROTONOSUPPORT
;
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
);
547 static int l2tp_nl_cmd_session_delete(struct sk_buff
*skb
, struct genl_info
*info
)
550 struct l2tp_session
*session
;
553 session
= l2tp_nl_session_find(info
);
554 if (session
== NULL
) {
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
);
568 static int l2tp_nl_cmd_session_modify(struct sk_buff
*skb
, struct genl_info
*info
)
571 struct l2tp_session
*session
;
573 session
= l2tp_nl_session_find(info
);
574 if (session
== NULL
) {
579 if (info
->attrs
[L2TP_ATTR_DEBUG
])
580 session
->debug
= nla_get_u32(info
->attrs
[L2TP_ATTR_DEBUG
]);
582 if (info
->attrs
[L2TP_ATTR_DATA_SEQ
])
583 session
->data_seq
= nla_get_u8(info
->attrs
[L2TP_ATTR_DATA_SEQ
]);
585 if (info
->attrs
[L2TP_ATTR_RECV_SEQ
])
586 session
->recv_seq
= nla_get_u8(info
->attrs
[L2TP_ATTR_RECV_SEQ
]);
588 if (info
->attrs
[L2TP_ATTR_SEND_SEQ
])
589 session
->send_seq
= nla_get_u8(info
->attrs
[L2TP_ATTR_SEND_SEQ
]);
591 if (info
->attrs
[L2TP_ATTR_LNS_MODE
])
592 session
->lns_mode
= nla_get_u8(info
->attrs
[L2TP_ATTR_LNS_MODE
]);
594 if (info
->attrs
[L2TP_ATTR_RECV_TIMEOUT
])
595 session
->reorder_timeout
= nla_get_msecs(info
->attrs
[L2TP_ATTR_RECV_TIMEOUT
]);
597 if (info
->attrs
[L2TP_ATTR_MTU
])
598 session
->mtu
= nla_get_u16(info
->attrs
[L2TP_ATTR_MTU
]);
600 if (info
->attrs
[L2TP_ATTR_MRU
])
601 session
->mru
= nla_get_u16(info
->attrs
[L2TP_ATTR_MRU
]);
607 static int l2tp_nl_session_send(struct sk_buff
*skb
, u32 pid
, u32 seq
, int flags
,
608 struct l2tp_session
*session
)
612 struct l2tp_tunnel
*tunnel
= session
->tunnel
;
613 struct sock
*sk
= NULL
;
614 struct l2tp_stats stats
;
619 hdr
= genlmsg_put(skb
, pid
, seq
, &l2tp_nl_family
, flags
, L2TP_CMD_SESSION_GET
);
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
) ||
632 nla_put_u16(skb
, L2TP_ATTR_MRU
, session
->mru
)))
633 goto nla_put_failure
;
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
) ||
647 (((sk
) && (sk
->sk_policy
[0] || sk
->sk_policy
[1])) &&
648 nla_put_u8(skb
, L2TP_ATTR_USING_IPSEC
, 1)) ||
650 (session
->reorder_timeout
&&
651 nla_put_msecs(skb
, L2TP_ATTR_RECV_TIMEOUT
, session
->reorder_timeout
)))
652 goto nla_put_failure
;
654 nest
= nla_nest_start(skb
, L2TP_ATTR_STATS
);
656 goto nla_put_failure
;
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
));
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
);
683 return genlmsg_end(skb
, hdr
);
686 genlmsg_cancel(skb
, hdr
);
690 static int l2tp_nl_cmd_session_get(struct sk_buff
*skb
, struct genl_info
*info
)
692 struct l2tp_session
*session
;
696 session
= l2tp_nl_session_find(info
);
697 if (session
== NULL
) {
702 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
708 ret
= l2tp_nl_session_send(msg
, info
->snd_pid
, info
->snd_seq
,
713 return genlmsg_unicast(genl_info_net(info
), msg
, info
->snd_pid
);
722 static int l2tp_nl_cmd_session_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
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];
731 if (tunnel
== NULL
) {
732 tunnel
= l2tp_tunnel_find_nth(net
, ti
);
737 session
= l2tp_session_find_nth(tunnel
, si
);
738 if (session
== NULL
) {
745 if (l2tp_nl_session_send(skb
, NETLINK_CB(cb
->skb
).pid
,
746 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
760 static 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
] = {
791 .len
= sizeof(struct in6_addr
),
793 [L2TP_ATTR_IP6_DADDR
] = {
795 .len
= sizeof(struct in6_addr
),
797 [L2TP_ATTR_IFNAME
] = {
798 .type
= NLA_NUL_STRING
,
801 [L2TP_ATTR_COOKIE
] = {
805 [L2TP_ATTR_PEER_COOKIE
] = {
811 static struct genl_ops l2tp_nl_ops
[] = {
813 .cmd
= L2TP_CMD_NOOP
,
814 .doit
= l2tp_nl_cmd_noop
,
815 .policy
= l2tp_nl_policy
,
816 /* can be retrieved by unprivileged users */
819 .cmd
= L2TP_CMD_TUNNEL_CREATE
,
820 .doit
= l2tp_nl_cmd_tunnel_create
,
821 .policy
= l2tp_nl_policy
,
822 .flags
= GENL_ADMIN_PERM
,
825 .cmd
= L2TP_CMD_TUNNEL_DELETE
,
826 .doit
= l2tp_nl_cmd_tunnel_delete
,
827 .policy
= l2tp_nl_policy
,
828 .flags
= GENL_ADMIN_PERM
,
831 .cmd
= L2TP_CMD_TUNNEL_MODIFY
,
832 .doit
= l2tp_nl_cmd_tunnel_modify
,
833 .policy
= l2tp_nl_policy
,
834 .flags
= GENL_ADMIN_PERM
,
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
,
844 .cmd
= L2TP_CMD_SESSION_CREATE
,
845 .doit
= l2tp_nl_cmd_session_create
,
846 .policy
= l2tp_nl_policy
,
847 .flags
= GENL_ADMIN_PERM
,
850 .cmd
= L2TP_CMD_SESSION_DELETE
,
851 .doit
= l2tp_nl_cmd_session_delete
,
852 .policy
= l2tp_nl_policy
,
853 .flags
= GENL_ADMIN_PERM
,
856 .cmd
= L2TP_CMD_SESSION_MODIFY
,
857 .doit
= l2tp_nl_cmd_session_modify
,
858 .policy
= l2tp_nl_policy
,
859 .flags
= GENL_ADMIN_PERM
,
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
,
870 int l2tp_nl_register_ops(enum l2tp_pwtype pw_type
, const struct l2tp_nl_cmd_ops
*ops
)
875 if (pw_type
>= __L2TP_PWTYPE_MAX
)
880 if (l2tp_nl_cmd_ops
[pw_type
])
883 l2tp_nl_cmd_ops
[pw_type
] = ops
;
891 EXPORT_SYMBOL_GPL(l2tp_nl_register_ops
);
893 void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type
)
895 if (pw_type
< __L2TP_PWTYPE_MAX
) {
897 l2tp_nl_cmd_ops
[pw_type
] = NULL
;
901 EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops
);
903 static int l2tp_nl_init(void)
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
));
914 static void l2tp_nl_cleanup(void)
916 genl_unregister_family(&l2tp_nl_family
);
919 module_init(l2tp_nl_init
);
920 module_exit(l2tp_nl_cleanup
);
922 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
923 MODULE_DESCRIPTION("L2TP netlink");
924 MODULE_LICENSE("GPL");
925 MODULE_VERSION("1.0");
926 MODULE_ALIAS_GENL_FAMILY("l2tp");