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