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