Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 2 | /* |
| 3 | * L2TP netlink layer, for management |
| 4 | * |
| 5 | * Copyright (c) 2008,2009,2010 Katalix Systems Ltd |
| 6 | * |
| 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> |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 16 | #include <net/sock.h> |
| 17 | #include <net/genetlink.h> |
| 18 | #include <net/udp.h> |
| 19 | #include <linux/in.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> |
| 25 | |
| 26 | #include <linux/l2tp.h> |
| 27 | |
| 28 | #include "l2tp_core.h" |
| 29 | |
| 30 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 31 | static struct genl_family l2tp_nl_family; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 32 | |
Bill Hong | 33f72e6 | 2014-12-27 10:12:39 -0800 | [diff] [blame] | 33 | static const struct genl_multicast_group l2tp_multicast_group[] = { |
| 34 | { |
| 35 | .name = L2TP_GENL_MCGROUP, |
| 36 | }, |
| 37 | }; |
| 38 | |
| 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, |
| 43 | u8 cmd); |
| 44 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 45 | /* Accessed under genl lock */ |
| 46 | static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX]; |
| 47 | |
Guillaume Nault | a434621 | 2017-10-31 17:36:42 +0100 | [diff] [blame] | 48 | static struct l2tp_session *l2tp_nl_session_get(struct genl_info *info) |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 49 | { |
| 50 | u32 tunnel_id; |
| 51 | u32 session_id; |
| 52 | char *ifname; |
| 53 | struct l2tp_tunnel *tunnel; |
| 54 | struct l2tp_session *session = NULL; |
| 55 | struct net *net = genl_info_net(info); |
| 56 | |
| 57 | if (info->attrs[L2TP_ATTR_IFNAME]) { |
| 58 | ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]); |
Guillaume Nault | a434621 | 2017-10-31 17:36:42 +0100 | [diff] [blame] | 59 | session = l2tp_session_get_by_ifname(net, ifname); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 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]); |
Guillaume Nault | 54652eb | 2017-08-25 16:51:40 +0200 | [diff] [blame] | 64 | tunnel = l2tp_tunnel_get(net, tunnel_id); |
| 65 | if (tunnel) { |
Guillaume Nault | 01e28b9 | 2018-08-10 13:21:57 +0200 | [diff] [blame] | 66 | session = l2tp_tunnel_get_session(tunnel, session_id); |
Guillaume Nault | 54652eb | 2017-08-25 16:51:40 +0200 | [diff] [blame] | 67 | l2tp_tunnel_dec_refcount(tunnel); |
| 68 | } |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | return session; |
| 72 | } |
| 73 | |
| 74 | static int l2tp_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info) |
| 75 | { |
| 76 | struct sk_buff *msg; |
| 77 | void *hdr; |
| 78 | int ret = -ENOBUFS; |
| 79 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 80 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 81 | if (!msg) { |
| 82 | ret = -ENOMEM; |
| 83 | goto out; |
| 84 | } |
| 85 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 86 | hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 87 | &l2tp_nl_family, 0, L2TP_CMD_NOOP); |
Wei Yongjun | 7f8436a | 2012-09-24 18:29:01 +0000 | [diff] [blame] | 88 | if (!hdr) { |
| 89 | ret = -EMSGSIZE; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 90 | goto err_out; |
| 91 | } |
| 92 | |
| 93 | genlmsg_end(msg, hdr); |
| 94 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 95 | return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 96 | |
| 97 | err_out: |
| 98 | nlmsg_free(msg); |
| 99 | |
| 100 | out: |
| 101 | return ret; |
| 102 | } |
| 103 | |
Bill Hong | 33f72e6 | 2014-12-27 10:12:39 -0800 | [diff] [blame] | 104 | static int l2tp_tunnel_notify(struct genl_family *family, |
| 105 | struct genl_info *info, |
| 106 | struct l2tp_tunnel *tunnel, |
| 107 | u8 cmd) |
| 108 | { |
| 109 | struct sk_buff *msg; |
| 110 | int ret; |
| 111 | |
| 112 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 113 | if (!msg) |
| 114 | return -ENOMEM; |
| 115 | |
| 116 | ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq, |
| 117 | NLM_F_ACK, tunnel, cmd); |
| 118 | |
Mark Tomlinson | 853effc | 2016-02-15 16:24:44 +1300 | [diff] [blame] | 119 | if (ret >= 0) { |
| 120 | ret = genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC); |
| 121 | /* We don't care if no one is listening */ |
| 122 | if (ret == -ESRCH) |
| 123 | ret = 0; |
| 124 | return ret; |
| 125 | } |
Bill Hong | 33f72e6 | 2014-12-27 10:12:39 -0800 | [diff] [blame] | 126 | |
| 127 | nlmsg_free(msg); |
| 128 | |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | static int l2tp_session_notify(struct genl_family *family, |
| 133 | struct genl_info *info, |
| 134 | struct l2tp_session *session, |
| 135 | u8 cmd) |
| 136 | { |
| 137 | struct sk_buff *msg; |
| 138 | int ret; |
| 139 | |
| 140 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 141 | if (!msg) |
| 142 | return -ENOMEM; |
| 143 | |
| 144 | ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq, |
| 145 | NLM_F_ACK, session, cmd); |
| 146 | |
Mark Tomlinson | 853effc | 2016-02-15 16:24:44 +1300 | [diff] [blame] | 147 | if (ret >= 0) { |
| 148 | ret = genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC); |
| 149 | /* We don't care if no one is listening */ |
| 150 | if (ret == -ESRCH) |
| 151 | ret = 0; |
| 152 | return ret; |
| 153 | } |
Bill Hong | 33f72e6 | 2014-12-27 10:12:39 -0800 | [diff] [blame] | 154 | |
| 155 | nlmsg_free(msg); |
| 156 | |
| 157 | return ret; |
| 158 | } |
| 159 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 160 | static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info) |
| 161 | { |
| 162 | u32 tunnel_id; |
| 163 | u32 peer_tunnel_id; |
| 164 | int proto_version; |
| 165 | int fd; |
| 166 | int ret = 0; |
| 167 | struct l2tp_tunnel_cfg cfg = { 0, }; |
| 168 | struct l2tp_tunnel *tunnel; |
| 169 | struct net *net = genl_info_net(info); |
| 170 | |
| 171 | if (!info->attrs[L2TP_ATTR_CONN_ID]) { |
| 172 | ret = -EINVAL; |
| 173 | goto out; |
| 174 | } |
| 175 | tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]); |
| 176 | |
| 177 | if (!info->attrs[L2TP_ATTR_PEER_CONN_ID]) { |
| 178 | ret = -EINVAL; |
| 179 | goto out; |
| 180 | } |
| 181 | peer_tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_CONN_ID]); |
| 182 | |
| 183 | if (!info->attrs[L2TP_ATTR_PROTO_VERSION]) { |
| 184 | ret = -EINVAL; |
| 185 | goto out; |
| 186 | } |
| 187 | proto_version = nla_get_u8(info->attrs[L2TP_ATTR_PROTO_VERSION]); |
| 188 | |
| 189 | if (!info->attrs[L2TP_ATTR_ENCAP_TYPE]) { |
| 190 | ret = -EINVAL; |
| 191 | goto out; |
| 192 | } |
| 193 | cfg.encap = nla_get_u16(info->attrs[L2TP_ATTR_ENCAP_TYPE]); |
| 194 | |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 195 | fd = -1; |
| 196 | if (info->attrs[L2TP_ATTR_FD]) { |
| 197 | fd = nla_get_u32(info->attrs[L2TP_ATTR_FD]); |
| 198 | } else { |
Chris Elston | f9bac8d | 2012-04-29 21:48:52 +0000 | [diff] [blame] | 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]); |
| 206 | } else |
| 207 | #endif |
| 208 | if (info->attrs[L2TP_ATTR_IP_SADDR] && |
| 209 | info->attrs[L2TP_ATTR_IP_DADDR]) { |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 210 | cfg.local_ip.s_addr = nla_get_in_addr( |
Chris Elston | f9bac8d | 2012-04-29 21:48:52 +0000 | [diff] [blame] | 211 | info->attrs[L2TP_ATTR_IP_SADDR]); |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 212 | cfg.peer_ip.s_addr = nla_get_in_addr( |
Chris Elston | f9bac8d | 2012-04-29 21:48:52 +0000 | [diff] [blame] | 213 | info->attrs[L2TP_ATTR_IP_DADDR]); |
| 214 | } else { |
| 215 | ret = -EINVAL; |
| 216 | goto out; |
| 217 | } |
James Chapman | 789a4a2 | 2010-04-02 06:19:40 +0000 | [diff] [blame] | 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]); |
Asbjørn Sloth Tønnesen | 57ceb86 | 2016-11-07 20:39:27 +0000 | [diff] [blame] | 222 | cfg.use_udp_checksums = nla_get_flag( |
| 223 | info->attrs[L2TP_ATTR_UDP_CSUM]); |
Tom Herbert | 6b649fea | 2014-05-23 08:47:40 -0700 | [diff] [blame] | 224 | |
| 225 | #if IS_ENABLED(CONFIG_IPV6) |
Asbjørn Sloth Tønnesen | 57ceb86 | 2016-11-07 20:39:27 +0000 | [diff] [blame] | 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]); |
Tom Herbert | 6b649fea | 2014-05-23 08:47:40 -0700 | [diff] [blame] | 230 | #endif |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 231 | } |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 232 | |
| 233 | if (info->attrs[L2TP_ATTR_DEBUG]) |
| 234 | cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]); |
| 235 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 236 | ret = -EINVAL; |
| 237 | switch (cfg.encap) { |
| 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); |
| 242 | break; |
| 243 | } |
| 244 | |
Guillaume Nault | 6b9f342 | 2018-04-10 21:01:12 +0200 | [diff] [blame] | 245 | if (ret < 0) |
| 246 | goto out; |
| 247 | |
| 248 | l2tp_tunnel_inc_refcount(tunnel); |
| 249 | ret = l2tp_tunnel_register(tunnel, net, &cfg); |
| 250 | if (ret < 0) { |
| 251 | kfree(tunnel); |
| 252 | goto out; |
| 253 | } |
| 254 | ret = l2tp_tunnel_notify(&l2tp_nl_family, info, tunnel, |
| 255 | L2TP_CMD_TUNNEL_CREATE); |
| 256 | l2tp_tunnel_dec_refcount(tunnel); |
| 257 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 258 | out: |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info) |
| 263 | { |
| 264 | struct l2tp_tunnel *tunnel; |
| 265 | u32 tunnel_id; |
| 266 | int ret = 0; |
| 267 | struct net *net = genl_info_net(info); |
| 268 | |
| 269 | if (!info->attrs[L2TP_ATTR_CONN_ID]) { |
| 270 | ret = -EINVAL; |
| 271 | goto out; |
| 272 | } |
| 273 | tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]); |
| 274 | |
Guillaume Nault | bb0a32c | 2017-08-25 16:51:42 +0200 | [diff] [blame] | 275 | tunnel = l2tp_tunnel_get(net, tunnel_id); |
| 276 | if (!tunnel) { |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 277 | ret = -ENODEV; |
| 278 | goto out; |
| 279 | } |
| 280 | |
Bill Hong | 33f72e6 | 2014-12-27 10:12:39 -0800 | [diff] [blame] | 281 | l2tp_tunnel_notify(&l2tp_nl_family, info, |
| 282 | tunnel, L2TP_CMD_TUNNEL_DELETE); |
| 283 | |
Jiri Slaby | 4dc12ff | 2017-10-25 15:57:55 +0200 | [diff] [blame] | 284 | l2tp_tunnel_delete(tunnel); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 285 | |
Guillaume Nault | bb0a32c | 2017-08-25 16:51:42 +0200 | [diff] [blame] | 286 | l2tp_tunnel_dec_refcount(tunnel); |
| 287 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 288 | out: |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info) |
| 293 | { |
| 294 | struct l2tp_tunnel *tunnel; |
| 295 | u32 tunnel_id; |
| 296 | int ret = 0; |
| 297 | struct net *net = genl_info_net(info); |
| 298 | |
| 299 | if (!info->attrs[L2TP_ATTR_CONN_ID]) { |
| 300 | ret = -EINVAL; |
| 301 | goto out; |
| 302 | } |
| 303 | tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]); |
| 304 | |
Guillaume Nault | 8c0e421 | 2017-08-25 16:51:42 +0200 | [diff] [blame] | 305 | tunnel = l2tp_tunnel_get(net, tunnel_id); |
| 306 | if (!tunnel) { |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 307 | ret = -ENODEV; |
| 308 | goto out; |
| 309 | } |
| 310 | |
| 311 | if (info->attrs[L2TP_ATTR_DEBUG]) |
| 312 | tunnel->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]); |
| 313 | |
Bill Hong | 33f72e6 | 2014-12-27 10:12:39 -0800 | [diff] [blame] | 314 | ret = l2tp_tunnel_notify(&l2tp_nl_family, info, |
| 315 | tunnel, L2TP_CMD_TUNNEL_MODIFY); |
| 316 | |
Guillaume Nault | 8c0e421 | 2017-08-25 16:51:42 +0200 | [diff] [blame] | 317 | l2tp_tunnel_dec_refcount(tunnel); |
| 318 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 319 | out: |
| 320 | return ret; |
| 321 | } |
| 322 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 323 | static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq, int flags, |
Bill Hong | 33f72e6 | 2014-12-27 10:12:39 -0800 | [diff] [blame] | 324 | struct l2tp_tunnel *tunnel, u8 cmd) |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 325 | { |
| 326 | void *hdr; |
| 327 | struct nlattr *nest; |
| 328 | struct sock *sk = NULL; |
| 329 | struct inet_sock *inet; |
Chris Elston | f9bac8d | 2012-04-29 21:48:52 +0000 | [diff] [blame] | 330 | #if IS_ENABLED(CONFIG_IPV6) |
| 331 | struct ipv6_pinfo *np = NULL; |
| 332 | #endif |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 333 | |
Bill Hong | 33f72e6 | 2014-12-27 10:12:39 -0800 | [diff] [blame] | 334 | hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd); |
Wei Yongjun | 7f8436a | 2012-09-24 18:29:01 +0000 | [diff] [blame] | 335 | if (!hdr) |
| 336 | return -EMSGSIZE; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 337 | |
David S. Miller | 60aed2a | 2012-04-01 19:59:31 -0400 | [diff] [blame] | 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; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 344 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 345 | nest = nla_nest_start_noflag(skb, L2TP_ATTR_STATS); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 346 | if (nest == NULL) |
| 347 | goto nla_put_failure; |
| 348 | |
Nicolas Dichtel | 1c714a9 | 2016-04-25 10:25:19 +0200 | [diff] [blame] | 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)) |
David S. Miller | 60aed2a | 2012-04-01 19:59:31 -0400 | [diff] [blame] | 373 | goto nla_put_failure; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 374 | nla_nest_end(skb, nest); |
| 375 | |
| 376 | sk = tunnel->sock; |
| 377 | if (!sk) |
| 378 | goto out; |
| 379 | |
Chris Elston | f9bac8d | 2012-04-29 21:48:52 +0000 | [diff] [blame] | 380 | #if IS_ENABLED(CONFIG_IPV6) |
| 381 | if (sk->sk_family == AF_INET6) |
| 382 | np = inet6_sk(sk); |
| 383 | #endif |
| 384 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 385 | inet = inet_sk(sk); |
| 386 | |
| 387 | switch (tunnel->encap) { |
| 388 | case L2TP_ENCAPTYPE_UDP: |
Asbjørn Sloth Tønnesen | 7ff516f | 2016-11-07 20:39:25 +0000 | [diff] [blame] | 389 | switch (sk->sk_family) { |
| 390 | case AF_INET: |
| 391 | if (nla_put_u8(skb, L2TP_ATTR_UDP_CSUM, !sk->sk_no_check_tx)) |
| 392 | goto nla_put_failure; |
| 393 | break; |
Asbjørn Sloth Tønnesen | 97b7af0 | 2016-11-07 20:39:26 +0000 | [diff] [blame] | 394 | #if IS_ENABLED(CONFIG_IPV6) |
| 395 | case AF_INET6: |
| 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; |
| 402 | break; |
| 403 | #endif |
Asbjørn Sloth Tønnesen | 7ff516f | 2016-11-07 20:39:25 +0000 | [diff] [blame] | 404 | } |
David S. Miller | 60aed2a | 2012-04-01 19:59:31 -0400 | [diff] [blame] | 405 | if (nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) || |
Asbjørn Sloth Tønnesen | 7ff516f | 2016-11-07 20:39:25 +0000 | [diff] [blame] | 406 | nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport))) |
David S. Miller | 60aed2a | 2012-04-01 19:59:31 -0400 | [diff] [blame] | 407 | goto nla_put_failure; |
Gustavo A. R. Silva | be070c7 | 2017-10-17 17:42:53 -0500 | [diff] [blame] | 408 | /* fall through */ |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 409 | case L2TP_ENCAPTYPE_IP: |
Chris Elston | f9bac8d | 2012-04-29 21:48:52 +0000 | [diff] [blame] | 410 | #if IS_ENABLED(CONFIG_IPV6) |
| 411 | if (np) { |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 412 | if (nla_put_in6_addr(skb, L2TP_ATTR_IP6_SADDR, |
| 413 | &np->saddr) || |
| 414 | nla_put_in6_addr(skb, L2TP_ATTR_IP6_DADDR, |
| 415 | &sk->sk_v6_daddr)) |
Chris Elston | f9bac8d | 2012-04-29 21:48:52 +0000 | [diff] [blame] | 416 | goto nla_put_failure; |
| 417 | } else |
| 418 | #endif |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 419 | if (nla_put_in_addr(skb, L2TP_ATTR_IP_SADDR, |
| 420 | inet->inet_saddr) || |
| 421 | nla_put_in_addr(skb, L2TP_ATTR_IP_DADDR, |
| 422 | inet->inet_daddr)) |
David S. Miller | 60aed2a | 2012-04-01 19:59:31 -0400 | [diff] [blame] | 423 | goto nla_put_failure; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 424 | break; |
| 425 | } |
| 426 | |
| 427 | out: |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 428 | genlmsg_end(skb, hdr); |
| 429 | return 0; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 430 | |
| 431 | nla_put_failure: |
| 432 | genlmsg_cancel(skb, hdr); |
| 433 | return -1; |
| 434 | } |
| 435 | |
| 436 | static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info) |
| 437 | { |
| 438 | struct l2tp_tunnel *tunnel; |
| 439 | struct sk_buff *msg; |
| 440 | u32 tunnel_id; |
| 441 | int ret = -ENOBUFS; |
| 442 | struct net *net = genl_info_net(info); |
| 443 | |
| 444 | if (!info->attrs[L2TP_ATTR_CONN_ID]) { |
| 445 | ret = -EINVAL; |
Guillaume Nault | 4e4b21d | 2017-08-25 16:51:43 +0200 | [diff] [blame] | 446 | goto err; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]); |
| 450 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 451 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 452 | if (!msg) { |
| 453 | ret = -ENOMEM; |
Guillaume Nault | 4e4b21d | 2017-08-25 16:51:43 +0200 | [diff] [blame] | 454 | goto err; |
| 455 | } |
| 456 | |
| 457 | tunnel = l2tp_tunnel_get(net, tunnel_id); |
| 458 | if (!tunnel) { |
| 459 | ret = -ENODEV; |
| 460 | goto err_nlmsg; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 463 | ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq, |
Bill Hong | 33f72e6 | 2014-12-27 10:12:39 -0800 | [diff] [blame] | 464 | NLM_F_ACK, tunnel, L2TP_CMD_TUNNEL_GET); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 465 | if (ret < 0) |
Guillaume Nault | 4e4b21d | 2017-08-25 16:51:43 +0200 | [diff] [blame] | 466 | goto err_nlmsg_tunnel; |
| 467 | |
| 468 | l2tp_tunnel_dec_refcount(tunnel); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 469 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 470 | return genlmsg_unicast(net, msg, info->snd_portid); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 471 | |
Guillaume Nault | 4e4b21d | 2017-08-25 16:51:43 +0200 | [diff] [blame] | 472 | err_nlmsg_tunnel: |
| 473 | l2tp_tunnel_dec_refcount(tunnel); |
| 474 | err_nlmsg: |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 475 | nlmsg_free(msg); |
Guillaume Nault | 4e4b21d | 2017-08-25 16:51:43 +0200 | [diff] [blame] | 476 | err: |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 481 | { |
| 482 | int ti = cb->args[0]; |
| 483 | struct l2tp_tunnel *tunnel; |
| 484 | struct net *net = sock_net(skb->sk); |
| 485 | |
| 486 | for (;;) { |
Guillaume Nault | 5846c13 | 2018-04-12 20:50:33 +0200 | [diff] [blame] | 487 | tunnel = l2tp_tunnel_get_nth(net, ti); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 488 | if (tunnel == NULL) |
| 489 | goto out; |
| 490 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 491 | if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).portid, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 492 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
Guillaume Nault | 5846c13 | 2018-04-12 20:50:33 +0200 | [diff] [blame] | 493 | tunnel, L2TP_CMD_TUNNEL_GET) < 0) { |
| 494 | l2tp_tunnel_dec_refcount(tunnel); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 495 | goto out; |
Guillaume Nault | 5846c13 | 2018-04-12 20:50:33 +0200 | [diff] [blame] | 496 | } |
| 497 | l2tp_tunnel_dec_refcount(tunnel); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 498 | |
| 499 | ti++; |
| 500 | } |
| 501 | |
| 502 | out: |
| 503 | cb->args[0] = ti; |
| 504 | |
| 505 | return skb->len; |
| 506 | } |
| 507 | |
| 508 | static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info) |
| 509 | { |
| 510 | u32 tunnel_id = 0; |
| 511 | u32 session_id; |
| 512 | u32 peer_session_id; |
| 513 | int ret = 0; |
| 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); |
| 518 | |
| 519 | if (!info->attrs[L2TP_ATTR_CONN_ID]) { |
| 520 | ret = -EINVAL; |
| 521 | goto out; |
| 522 | } |
Guillaume Nault | e702c12 | 2017-08-25 16:51:46 +0200 | [diff] [blame] | 523 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 524 | tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]); |
Guillaume Nault | e702c12 | 2017-08-25 16:51:46 +0200 | [diff] [blame] | 525 | tunnel = l2tp_tunnel_get(net, tunnel_id); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 526 | if (!tunnel) { |
| 527 | ret = -ENODEV; |
| 528 | goto out; |
| 529 | } |
| 530 | |
| 531 | if (!info->attrs[L2TP_ATTR_SESSION_ID]) { |
| 532 | ret = -EINVAL; |
Guillaume Nault | e702c12 | 2017-08-25 16:51:46 +0200 | [diff] [blame] | 533 | goto out_tunnel; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 534 | } |
| 535 | session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 536 | |
| 537 | if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) { |
| 538 | ret = -EINVAL; |
Guillaume Nault | e702c12 | 2017-08-25 16:51:46 +0200 | [diff] [blame] | 539 | goto out_tunnel; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 540 | } |
| 541 | peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]); |
| 542 | |
| 543 | if (!info->attrs[L2TP_ATTR_PW_TYPE]) { |
| 544 | ret = -EINVAL; |
Guillaume Nault | e702c12 | 2017-08-25 16:51:46 +0200 | [diff] [blame] | 545 | goto out_tunnel; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 546 | } |
| 547 | cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]); |
| 548 | if (cfg.pw_type >= __L2TP_PWTYPE_MAX) { |
| 549 | ret = -EINVAL; |
Guillaume Nault | e702c12 | 2017-08-25 16:51:46 +0200 | [diff] [blame] | 550 | goto out_tunnel; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Guillaume Nault | de9bada | 2018-06-15 15:39:17 +0200 | [diff] [blame] | 553 | /* L2TPv2 only accepts PPP pseudo-wires */ |
| 554 | if (tunnel->version == 2 && cfg.pw_type != L2TP_PWTYPE_PPP) { |
| 555 | ret = -EPROTONOSUPPORT; |
| 556 | goto out_tunnel; |
| 557 | } |
| 558 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 559 | if (tunnel->version > 2) { |
Lorenzo Bianconi | dfffc97 | 2018-01-16 23:01:54 +0100 | [diff] [blame] | 560 | if (info->attrs[L2TP_ATTR_L2SPEC_TYPE]) { |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 561 | cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]); |
Lorenzo Bianconi | dfffc97 | 2018-01-16 23:01:54 +0100 | [diff] [blame] | 562 | if (cfg.l2specific_type != L2TP_L2SPECTYPE_DEFAULT && |
| 563 | cfg.l2specific_type != L2TP_L2SPECTYPE_NONE) { |
| 564 | ret = -EINVAL; |
| 565 | goto out_tunnel; |
| 566 | } |
| 567 | } else { |
| 568 | cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT; |
| 569 | } |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 570 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 571 | if (info->attrs[L2TP_ATTR_COOKIE]) { |
| 572 | u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]); |
| 573 | if (len > 8) { |
| 574 | ret = -EINVAL; |
Guillaume Nault | e702c12 | 2017-08-25 16:51:46 +0200 | [diff] [blame] | 575 | goto out_tunnel; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 576 | } |
| 577 | cfg.cookie_len = len; |
| 578 | memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len); |
| 579 | } |
| 580 | if (info->attrs[L2TP_ATTR_PEER_COOKIE]) { |
| 581 | u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]); |
| 582 | if (len > 8) { |
| 583 | ret = -EINVAL; |
Guillaume Nault | e702c12 | 2017-08-25 16:51:46 +0200 | [diff] [blame] | 584 | goto out_tunnel; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 585 | } |
| 586 | cfg.peer_cookie_len = len; |
| 587 | memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len); |
| 588 | } |
| 589 | if (info->attrs[L2TP_ATTR_IFNAME]) |
| 590 | cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | if (info->attrs[L2TP_ATTR_DEBUG]) |
| 594 | cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]); |
| 595 | |
| 596 | if (info->attrs[L2TP_ATTR_RECV_SEQ]) |
| 597 | cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]); |
| 598 | |
| 599 | if (info->attrs[L2TP_ATTR_SEND_SEQ]) |
| 600 | cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]); |
| 601 | |
| 602 | if (info->attrs[L2TP_ATTR_LNS_MODE]) |
| 603 | cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]); |
| 604 | |
| 605 | if (info->attrs[L2TP_ATTR_RECV_TIMEOUT]) |
| 606 | cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]); |
| 607 | |
stephen hemminger | f1f39f9 | 2015-09-23 21:33:34 -0700 | [diff] [blame] | 608 | #ifdef CONFIG_MODULES |
| 609 | if (l2tp_nl_cmd_ops[cfg.pw_type] == NULL) { |
| 610 | genl_unlock(); |
| 611 | request_module("net-l2tp-type-%u", cfg.pw_type); |
| 612 | genl_lock(); |
| 613 | } |
| 614 | #endif |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 615 | if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) || |
| 616 | (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) { |
| 617 | ret = -EPROTONOSUPPORT; |
Guillaume Nault | e702c12 | 2017-08-25 16:51:46 +0200 | [diff] [blame] | 618 | goto out_tunnel; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Guillaume Nault | f026bc2 | 2017-09-01 17:58:51 +0200 | [diff] [blame] | 621 | ret = l2tp_nl_cmd_ops[cfg.pw_type]->session_create(net, tunnel, |
| 622 | session_id, |
| 623 | peer_session_id, |
| 624 | &cfg); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 625 | |
Bill Hong | 33f72e6 | 2014-12-27 10:12:39 -0800 | [diff] [blame] | 626 | if (ret >= 0) { |
Guillaume Nault | 01e28b9 | 2018-08-10 13:21:57 +0200 | [diff] [blame] | 627 | session = l2tp_tunnel_get_session(tunnel, session_id); |
Guillaume Nault | 5e6a9e5 | 2017-03-31 13:02:29 +0200 | [diff] [blame] | 628 | if (session) { |
Bill Hong | 33f72e6 | 2014-12-27 10:12:39 -0800 | [diff] [blame] | 629 | ret = l2tp_session_notify(&l2tp_nl_family, info, session, |
| 630 | L2TP_CMD_SESSION_CREATE); |
Guillaume Nault | 5e6a9e5 | 2017-03-31 13:02:29 +0200 | [diff] [blame] | 631 | l2tp_session_dec_refcount(session); |
| 632 | } |
Bill Hong | 33f72e6 | 2014-12-27 10:12:39 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Guillaume Nault | e702c12 | 2017-08-25 16:51:46 +0200 | [diff] [blame] | 635 | out_tunnel: |
| 636 | l2tp_tunnel_dec_refcount(tunnel); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 637 | out: |
| 638 | return ret; |
| 639 | } |
| 640 | |
| 641 | static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info) |
| 642 | { |
| 643 | int ret = 0; |
| 644 | struct l2tp_session *session; |
| 645 | u16 pw_type; |
| 646 | |
Guillaume Nault | a434621 | 2017-10-31 17:36:42 +0100 | [diff] [blame] | 647 | session = l2tp_nl_session_get(info); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 648 | if (session == NULL) { |
| 649 | ret = -ENODEV; |
| 650 | goto out; |
| 651 | } |
| 652 | |
Bill Hong | 33f72e6 | 2014-12-27 10:12:39 -0800 | [diff] [blame] | 653 | l2tp_session_notify(&l2tp_nl_family, info, |
| 654 | session, L2TP_CMD_SESSION_DELETE); |
| 655 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 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); |
| 660 | |
Guillaume Nault | 2777e2a | 2017-03-31 13:02:30 +0200 | [diff] [blame] | 661 | l2tp_session_dec_refcount(session); |
| 662 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 663 | out: |
| 664 | return ret; |
| 665 | } |
| 666 | |
| 667 | static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info) |
| 668 | { |
| 669 | int ret = 0; |
| 670 | struct l2tp_session *session; |
| 671 | |
Guillaume Nault | a434621 | 2017-10-31 17:36:42 +0100 | [diff] [blame] | 672 | session = l2tp_nl_session_get(info); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 673 | if (session == NULL) { |
| 674 | ret = -ENODEV; |
| 675 | goto out; |
| 676 | } |
| 677 | |
| 678 | if (info->attrs[L2TP_ATTR_DEBUG]) |
| 679 | session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]); |
| 680 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 681 | if (info->attrs[L2TP_ATTR_RECV_SEQ]) |
| 682 | session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]); |
| 683 | |
Guillaume Nault | bb5016e | 2014-03-06 11:14:30 +0100 | [diff] [blame] | 684 | if (info->attrs[L2TP_ATTR_SEND_SEQ]) { |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 685 | session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]); |
Guillaume Nault | bb5016e | 2014-03-06 11:14:30 +0100 | [diff] [blame] | 686 | l2tp_session_set_header_len(session, session->tunnel->version); |
| 687 | } |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 688 | |
| 689 | if (info->attrs[L2TP_ATTR_LNS_MODE]) |
| 690 | session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]); |
| 691 | |
| 692 | if (info->attrs[L2TP_ATTR_RECV_TIMEOUT]) |
| 693 | session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]); |
| 694 | |
Bill Hong | 33f72e6 | 2014-12-27 10:12:39 -0800 | [diff] [blame] | 695 | ret = l2tp_session_notify(&l2tp_nl_family, info, |
| 696 | session, L2TP_CMD_SESSION_MODIFY); |
| 697 | |
Guillaume Nault | 2777e2a | 2017-03-31 13:02:30 +0200 | [diff] [blame] | 698 | l2tp_session_dec_refcount(session); |
| 699 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 700 | out: |
| 701 | return ret; |
| 702 | } |
| 703 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 704 | static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int flags, |
Bill Hong | 33f72e6 | 2014-12-27 10:12:39 -0800 | [diff] [blame] | 705 | struct l2tp_session *session, u8 cmd) |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 706 | { |
| 707 | void *hdr; |
| 708 | struct nlattr *nest; |
| 709 | struct l2tp_tunnel *tunnel = session->tunnel; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 710 | |
Bill Hong | 33f72e6 | 2014-12-27 10:12:39 -0800 | [diff] [blame] | 711 | hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd); |
Wei Yongjun | 7f8436a | 2012-09-24 18:29:01 +0000 | [diff] [blame] | 712 | if (!hdr) |
| 713 | return -EMSGSIZE; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 714 | |
David S. Miller | 60aed2a | 2012-04-01 19:59:31 -0400 | [diff] [blame] | 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) || |
Guillaume Nault | e9697e2 | 2018-08-03 12:38:39 +0200 | [diff] [blame] | 721 | nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype)) |
David S. Miller | 60aed2a | 2012-04-01 19:59:31 -0400 | [diff] [blame] | 722 | goto nla_put_failure; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 723 | |
Alan Cox | e269ed2 | 2012-10-25 05:22:01 +0000 | [diff] [blame] | 724 | if ((session->ifname[0] && |
David S. Miller | 60aed2a | 2012-04-01 19:59:31 -0400 | [diff] [blame] | 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) || |
Guillaume Nault | d6a61ec | 2018-08-10 13:21:55 +0200 | [diff] [blame] | 735 | (l2tp_tunnel_uses_xfrm(tunnel) && |
David S. Miller | 60aed2a | 2012-04-01 19:59:31 -0400 | [diff] [blame] | 736 | nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) || |
David S. Miller | 60aed2a | 2012-04-01 19:59:31 -0400 | [diff] [blame] | 737 | (session->reorder_timeout && |
Nicolas Dichtel | 2175d87 | 2016-04-22 17:31:21 +0200 | [diff] [blame] | 738 | nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT, |
| 739 | session->reorder_timeout, L2TP_ATTR_PAD))) |
David S. Miller | 60aed2a | 2012-04-01 19:59:31 -0400 | [diff] [blame] | 740 | goto nla_put_failure; |
James Chapman | 5de7aee | 2012-04-29 21:48:46 +0000 | [diff] [blame] | 741 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 742 | nest = nla_nest_start_noflag(skb, L2TP_ATTR_STATS); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 743 | if (nest == NULL) |
| 744 | goto nla_put_failure; |
James Chapman | 5de7aee | 2012-04-29 21:48:46 +0000 | [diff] [blame] | 745 | |
Nicolas Dichtel | 1c714a9 | 2016-04-25 10:25:19 +0200 | [diff] [blame] | 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)) |
David S. Miller | 60aed2a | 2012-04-01 19:59:31 -0400 | [diff] [blame] | 770 | goto nla_put_failure; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 771 | nla_nest_end(skb, nest); |
| 772 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 773 | genlmsg_end(skb, hdr); |
| 774 | return 0; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 775 | |
| 776 | nla_put_failure: |
| 777 | genlmsg_cancel(skb, hdr); |
| 778 | return -1; |
| 779 | } |
| 780 | |
| 781 | static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info) |
| 782 | { |
| 783 | struct l2tp_session *session; |
| 784 | struct sk_buff *msg; |
| 785 | int ret; |
| 786 | |
Guillaume Nault | a434621 | 2017-10-31 17:36:42 +0100 | [diff] [blame] | 787 | session = l2tp_nl_session_get(info); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 788 | if (session == NULL) { |
| 789 | ret = -ENODEV; |
Guillaume Nault | 2777e2a | 2017-03-31 13:02:30 +0200 | [diff] [blame] | 790 | goto err; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 793 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 794 | if (!msg) { |
| 795 | ret = -ENOMEM; |
Guillaume Nault | 2777e2a | 2017-03-31 13:02:30 +0200 | [diff] [blame] | 796 | goto err_ref; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 799 | ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq, |
Bill Hong | 33f72e6 | 2014-12-27 10:12:39 -0800 | [diff] [blame] | 800 | 0, session, L2TP_CMD_SESSION_GET); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 801 | if (ret < 0) |
Guillaume Nault | 2777e2a | 2017-03-31 13:02:30 +0200 | [diff] [blame] | 802 | goto err_ref_msg; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 803 | |
Guillaume Nault | 2777e2a | 2017-03-31 13:02:30 +0200 | [diff] [blame] | 804 | ret = genlmsg_unicast(genl_info_net(info), msg, info->snd_portid); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 805 | |
Guillaume Nault | 2777e2a | 2017-03-31 13:02:30 +0200 | [diff] [blame] | 806 | l2tp_session_dec_refcount(session); |
| 807 | |
| 808 | return ret; |
| 809 | |
| 810 | err_ref_msg: |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 811 | nlmsg_free(msg); |
Guillaume Nault | 2777e2a | 2017-03-31 13:02:30 +0200 | [diff] [blame] | 812 | err_ref: |
| 813 | l2tp_session_dec_refcount(session); |
| 814 | err: |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 815 | return ret; |
| 816 | } |
| 817 | |
| 818 | static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 819 | { |
| 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]; |
| 825 | |
| 826 | for (;;) { |
| 827 | if (tunnel == NULL) { |
Guillaume Nault | 5846c13 | 2018-04-12 20:50:33 +0200 | [diff] [blame] | 828 | tunnel = l2tp_tunnel_get_nth(net, ti); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 829 | if (tunnel == NULL) |
| 830 | goto out; |
| 831 | } |
| 832 | |
Guillaume Nault | a434621 | 2017-10-31 17:36:42 +0100 | [diff] [blame] | 833 | session = l2tp_session_get_nth(tunnel, si); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 834 | if (session == NULL) { |
| 835 | ti++; |
Guillaume Nault | 5846c13 | 2018-04-12 20:50:33 +0200 | [diff] [blame] | 836 | l2tp_tunnel_dec_refcount(tunnel); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 837 | tunnel = NULL; |
| 838 | si = 0; |
| 839 | continue; |
| 840 | } |
| 841 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 842 | if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).portid, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 843 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
Guillaume Nault | e08293a | 2017-04-03 12:03:13 +0200 | [diff] [blame] | 844 | session, L2TP_CMD_SESSION_GET) < 0) { |
| 845 | l2tp_session_dec_refcount(session); |
Guillaume Nault | 5846c13 | 2018-04-12 20:50:33 +0200 | [diff] [blame] | 846 | l2tp_tunnel_dec_refcount(tunnel); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 847 | break; |
Guillaume Nault | e08293a | 2017-04-03 12:03:13 +0200 | [diff] [blame] | 848 | } |
| 849 | l2tp_session_dec_refcount(session); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 850 | |
| 851 | si++; |
| 852 | } |
| 853 | |
| 854 | out: |
| 855 | cb->args[0] = ti; |
| 856 | cb->args[1] = si; |
| 857 | |
| 858 | return skb->len; |
| 859 | } |
| 860 | |
stephen hemminger | f5bb341 | 2016-08-31 23:24:41 -0700 | [diff] [blame] | 861 | static const struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = { |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 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, }, |
Chris Elston | f9bac8d | 2012-04-29 21:48:52 +0000 | [diff] [blame] | 890 | [L2TP_ATTR_IP6_SADDR] = { |
| 891 | .type = NLA_BINARY, |
| 892 | .len = sizeof(struct in6_addr), |
| 893 | }, |
| 894 | [L2TP_ATTR_IP6_DADDR] = { |
| 895 | .type = NLA_BINARY, |
| 896 | .len = sizeof(struct in6_addr), |
| 897 | }, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 898 | [L2TP_ATTR_IFNAME] = { |
| 899 | .type = NLA_NUL_STRING, |
| 900 | .len = IFNAMSIZ - 1, |
| 901 | }, |
| 902 | [L2TP_ATTR_COOKIE] = { |
| 903 | .type = NLA_BINARY, |
| 904 | .len = 8, |
| 905 | }, |
| 906 | [L2TP_ATTR_PEER_COOKIE] = { |
| 907 | .type = NLA_BINARY, |
| 908 | .len = 8, |
| 909 | }, |
| 910 | }; |
| 911 | |
Johannes Berg | 4534de8 | 2013-11-14 17:14:46 +0100 | [diff] [blame] | 912 | static const struct genl_ops l2tp_nl_ops[] = { |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 913 | { |
| 914 | .cmd = L2TP_CMD_NOOP, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 915 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 916 | .doit = l2tp_nl_cmd_noop, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 917 | /* can be retrieved by unprivileged users */ |
| 918 | }, |
| 919 | { |
| 920 | .cmd = L2TP_CMD_TUNNEL_CREATE, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 921 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 922 | .doit = l2tp_nl_cmd_tunnel_create, |
Michael Weiß | 016e353 | 2020-04-07 13:11:48 +0200 | [diff] [blame] | 923 | .flags = GENL_UNS_ADMIN_PERM, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 924 | }, |
| 925 | { |
| 926 | .cmd = L2TP_CMD_TUNNEL_DELETE, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 927 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 928 | .doit = l2tp_nl_cmd_tunnel_delete, |
Michael Weiß | 016e353 | 2020-04-07 13:11:48 +0200 | [diff] [blame] | 929 | .flags = GENL_UNS_ADMIN_PERM, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 930 | }, |
| 931 | { |
| 932 | .cmd = L2TP_CMD_TUNNEL_MODIFY, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 933 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 934 | .doit = l2tp_nl_cmd_tunnel_modify, |
Michael Weiß | 016e353 | 2020-04-07 13:11:48 +0200 | [diff] [blame] | 935 | .flags = GENL_UNS_ADMIN_PERM, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 936 | }, |
| 937 | { |
| 938 | .cmd = L2TP_CMD_TUNNEL_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 939 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 940 | .doit = l2tp_nl_cmd_tunnel_get, |
| 941 | .dumpit = l2tp_nl_cmd_tunnel_dump, |
Michael Weiß | 016e353 | 2020-04-07 13:11:48 +0200 | [diff] [blame] | 942 | .flags = GENL_UNS_ADMIN_PERM, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 943 | }, |
| 944 | { |
| 945 | .cmd = L2TP_CMD_SESSION_CREATE, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 946 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 947 | .doit = l2tp_nl_cmd_session_create, |
Michael Weiß | 016e353 | 2020-04-07 13:11:48 +0200 | [diff] [blame] | 948 | .flags = GENL_UNS_ADMIN_PERM, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 949 | }, |
| 950 | { |
| 951 | .cmd = L2TP_CMD_SESSION_DELETE, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 952 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 953 | .doit = l2tp_nl_cmd_session_delete, |
Michael Weiß | 016e353 | 2020-04-07 13:11:48 +0200 | [diff] [blame] | 954 | .flags = GENL_UNS_ADMIN_PERM, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 955 | }, |
| 956 | { |
| 957 | .cmd = L2TP_CMD_SESSION_MODIFY, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 958 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 959 | .doit = l2tp_nl_cmd_session_modify, |
Michael Weiß | 016e353 | 2020-04-07 13:11:48 +0200 | [diff] [blame] | 960 | .flags = GENL_UNS_ADMIN_PERM, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 961 | }, |
| 962 | { |
| 963 | .cmd = L2TP_CMD_SESSION_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 964 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 965 | .doit = l2tp_nl_cmd_session_get, |
| 966 | .dumpit = l2tp_nl_cmd_session_dump, |
Michael Weiß | 016e353 | 2020-04-07 13:11:48 +0200 | [diff] [blame] | 967 | .flags = GENL_UNS_ADMIN_PERM, |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 968 | }, |
| 969 | }; |
| 970 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 971 | static struct genl_family l2tp_nl_family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 972 | .name = L2TP_GENL_NAME, |
| 973 | .version = L2TP_GENL_VERSION, |
| 974 | .hdrsize = 0, |
| 975 | .maxattr = L2TP_ATTR_MAX, |
Johannes Berg | 3b0f31f | 2019-03-21 22:51:02 +0100 | [diff] [blame] | 976 | .policy = l2tp_nl_policy, |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 977 | .netnsok = true, |
| 978 | .module = THIS_MODULE, |
| 979 | .ops = l2tp_nl_ops, |
| 980 | .n_ops = ARRAY_SIZE(l2tp_nl_ops), |
| 981 | .mcgrps = l2tp_multicast_group, |
| 982 | .n_mcgrps = ARRAY_SIZE(l2tp_multicast_group), |
| 983 | }; |
| 984 | |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 985 | int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops) |
| 986 | { |
| 987 | int ret; |
| 988 | |
| 989 | ret = -EINVAL; |
| 990 | if (pw_type >= __L2TP_PWTYPE_MAX) |
| 991 | goto err; |
| 992 | |
| 993 | genl_lock(); |
| 994 | ret = -EBUSY; |
| 995 | if (l2tp_nl_cmd_ops[pw_type]) |
| 996 | goto out; |
| 997 | |
| 998 | l2tp_nl_cmd_ops[pw_type] = ops; |
David S. Miller | 8cb4901 | 2011-04-17 17:01:05 -0700 | [diff] [blame] | 999 | ret = 0; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 1000 | |
| 1001 | out: |
| 1002 | genl_unlock(); |
| 1003 | err: |
David S. Miller | 8cb4901 | 2011-04-17 17:01:05 -0700 | [diff] [blame] | 1004 | return ret; |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 1005 | } |
| 1006 | EXPORT_SYMBOL_GPL(l2tp_nl_register_ops); |
| 1007 | |
| 1008 | void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type) |
| 1009 | { |
| 1010 | if (pw_type < __L2TP_PWTYPE_MAX) { |
| 1011 | genl_lock(); |
| 1012 | l2tp_nl_cmd_ops[pw_type] = NULL; |
| 1013 | genl_unlock(); |
| 1014 | } |
| 1015 | } |
| 1016 | EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops); |
| 1017 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 1018 | static int __init l2tp_nl_init(void) |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 1019 | { |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 1020 | pr_info("L2TP netlink interface\n"); |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1021 | return genl_register_family(&l2tp_nl_family); |
James Chapman | 309795f | 2010-04-02 06:19:10 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | static void l2tp_nl_cleanup(void) |
| 1025 | { |
| 1026 | genl_unregister_family(&l2tp_nl_family); |
| 1027 | } |
| 1028 | |
| 1029 | module_init(l2tp_nl_init); |
| 1030 | module_exit(l2tp_nl_cleanup); |
| 1031 | |
| 1032 | MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); |
| 1033 | MODULE_DESCRIPTION("L2TP netlink"); |
| 1034 | MODULE_LICENSE("GPL"); |
| 1035 | MODULE_VERSION("1.0"); |
Neil Horman | e9412c3 | 2012-05-29 09:30:41 +0000 | [diff] [blame] | 1036 | MODULE_ALIAS_GENL_FAMILY("l2tp"); |