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