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