0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0014
0015 #include <net/sock.h>
0016 #include <net/genetlink.h>
0017 #include <net/udp.h>
0018 #include <linux/in.h>
0019 #include <linux/udp.h>
0020 #include <linux/socket.h>
0021 #include <linux/module.h>
0022 #include <linux/list.h>
0023 #include <net/net_namespace.h>
0024
0025 #include <linux/l2tp.h>
0026
0027 #include "l2tp_core.h"
0028
0029 static struct genl_family l2tp_nl_family;
0030
0031 static const struct genl_multicast_group l2tp_multicast_group[] = {
0032 {
0033 .name = L2TP_GENL_MCGROUP,
0034 },
0035 };
0036
0037 static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq,
0038 int flags, struct l2tp_tunnel *tunnel, u8 cmd);
0039 static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq,
0040 int flags, struct l2tp_session *session,
0041 u8 cmd);
0042
0043
0044 static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX];
0045
0046 static struct l2tp_session *l2tp_nl_session_get(struct genl_info *info)
0047 {
0048 u32 tunnel_id;
0049 u32 session_id;
0050 char *ifname;
0051 struct l2tp_tunnel *tunnel;
0052 struct l2tp_session *session = NULL;
0053 struct net *net = genl_info_net(info);
0054
0055 if (info->attrs[L2TP_ATTR_IFNAME]) {
0056 ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
0057 session = l2tp_session_get_by_ifname(net, ifname);
0058 } else if ((info->attrs[L2TP_ATTR_SESSION_ID]) &&
0059 (info->attrs[L2TP_ATTR_CONN_ID])) {
0060 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
0061 session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
0062 tunnel = l2tp_tunnel_get(net, tunnel_id);
0063 if (tunnel) {
0064 session = l2tp_tunnel_get_session(tunnel, session_id);
0065 l2tp_tunnel_dec_refcount(tunnel);
0066 }
0067 }
0068
0069 return session;
0070 }
0071
0072 static int l2tp_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
0073 {
0074 struct sk_buff *msg;
0075 void *hdr;
0076 int ret = -ENOBUFS;
0077
0078 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0079 if (!msg) {
0080 ret = -ENOMEM;
0081 goto out;
0082 }
0083
0084 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
0085 &l2tp_nl_family, 0, L2TP_CMD_NOOP);
0086 if (!hdr) {
0087 ret = -EMSGSIZE;
0088 goto err_out;
0089 }
0090
0091 genlmsg_end(msg, hdr);
0092
0093 return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
0094
0095 err_out:
0096 nlmsg_free(msg);
0097
0098 out:
0099 return ret;
0100 }
0101
0102 static int l2tp_tunnel_notify(struct genl_family *family,
0103 struct genl_info *info,
0104 struct l2tp_tunnel *tunnel,
0105 u8 cmd)
0106 {
0107 struct sk_buff *msg;
0108 int ret;
0109
0110 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0111 if (!msg)
0112 return -ENOMEM;
0113
0114 ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
0115 NLM_F_ACK, tunnel, cmd);
0116
0117 if (ret >= 0) {
0118 ret = genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC);
0119
0120 if (ret == -ESRCH)
0121 ret = 0;
0122 return ret;
0123 }
0124
0125 nlmsg_free(msg);
0126
0127 return ret;
0128 }
0129
0130 static int l2tp_session_notify(struct genl_family *family,
0131 struct genl_info *info,
0132 struct l2tp_session *session,
0133 u8 cmd)
0134 {
0135 struct sk_buff *msg;
0136 int ret;
0137
0138 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0139 if (!msg)
0140 return -ENOMEM;
0141
0142 ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
0143 NLM_F_ACK, session, cmd);
0144
0145 if (ret >= 0) {
0146 ret = genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC);
0147
0148 if (ret == -ESRCH)
0149 ret = 0;
0150 return ret;
0151 }
0152
0153 nlmsg_free(msg);
0154
0155 return ret;
0156 }
0157
0158 static int l2tp_nl_cmd_tunnel_create_get_addr(struct nlattr **attrs, struct l2tp_tunnel_cfg *cfg)
0159 {
0160 if (attrs[L2TP_ATTR_UDP_SPORT])
0161 cfg->local_udp_port = nla_get_u16(attrs[L2TP_ATTR_UDP_SPORT]);
0162 if (attrs[L2TP_ATTR_UDP_DPORT])
0163 cfg->peer_udp_port = nla_get_u16(attrs[L2TP_ATTR_UDP_DPORT]);
0164 cfg->use_udp_checksums = nla_get_flag(attrs[L2TP_ATTR_UDP_CSUM]);
0165
0166
0167 #if IS_ENABLED(CONFIG_IPV6)
0168 if (attrs[L2TP_ATTR_IP6_SADDR] && attrs[L2TP_ATTR_IP6_DADDR]) {
0169 cfg->local_ip6 = nla_data(attrs[L2TP_ATTR_IP6_SADDR]);
0170 cfg->peer_ip6 = nla_data(attrs[L2TP_ATTR_IP6_DADDR]);
0171 cfg->udp6_zero_tx_checksums = nla_get_flag(attrs[L2TP_ATTR_UDP_ZERO_CSUM6_TX]);
0172 cfg->udp6_zero_rx_checksums = nla_get_flag(attrs[L2TP_ATTR_UDP_ZERO_CSUM6_RX]);
0173 return 0;
0174 }
0175 #endif
0176 if (attrs[L2TP_ATTR_IP_SADDR] && attrs[L2TP_ATTR_IP_DADDR]) {
0177 cfg->local_ip.s_addr = nla_get_in_addr(attrs[L2TP_ATTR_IP_SADDR]);
0178 cfg->peer_ip.s_addr = nla_get_in_addr(attrs[L2TP_ATTR_IP_DADDR]);
0179 return 0;
0180 }
0181 return -EINVAL;
0182 }
0183
0184 static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info)
0185 {
0186 u32 tunnel_id;
0187 u32 peer_tunnel_id;
0188 int proto_version;
0189 int fd = -1;
0190 int ret = 0;
0191 struct l2tp_tunnel_cfg cfg = { 0, };
0192 struct l2tp_tunnel *tunnel;
0193 struct net *net = genl_info_net(info);
0194 struct nlattr **attrs = info->attrs;
0195
0196 if (!attrs[L2TP_ATTR_CONN_ID]) {
0197 ret = -EINVAL;
0198 goto out;
0199 }
0200 tunnel_id = nla_get_u32(attrs[L2TP_ATTR_CONN_ID]);
0201
0202 if (!attrs[L2TP_ATTR_PEER_CONN_ID]) {
0203 ret = -EINVAL;
0204 goto out;
0205 }
0206 peer_tunnel_id = nla_get_u32(attrs[L2TP_ATTR_PEER_CONN_ID]);
0207
0208 if (!attrs[L2TP_ATTR_PROTO_VERSION]) {
0209 ret = -EINVAL;
0210 goto out;
0211 }
0212 proto_version = nla_get_u8(attrs[L2TP_ATTR_PROTO_VERSION]);
0213
0214 if (!attrs[L2TP_ATTR_ENCAP_TYPE]) {
0215 ret = -EINVAL;
0216 goto out;
0217 }
0218 cfg.encap = nla_get_u16(attrs[L2TP_ATTR_ENCAP_TYPE]);
0219
0220
0221
0222
0223
0224 if (attrs[L2TP_ATTR_FD]) {
0225 fd = nla_get_u32(attrs[L2TP_ATTR_FD]);
0226 } else {
0227 ret = l2tp_nl_cmd_tunnel_create_get_addr(attrs, &cfg);
0228 if (ret < 0)
0229 goto out;
0230 }
0231
0232 ret = -EINVAL;
0233 switch (cfg.encap) {
0234 case L2TP_ENCAPTYPE_UDP:
0235 case L2TP_ENCAPTYPE_IP:
0236 ret = l2tp_tunnel_create(fd, proto_version, tunnel_id,
0237 peer_tunnel_id, &cfg, &tunnel);
0238 break;
0239 }
0240
0241 if (ret < 0)
0242 goto out;
0243
0244 l2tp_tunnel_inc_refcount(tunnel);
0245 ret = l2tp_tunnel_register(tunnel, net, &cfg);
0246 if (ret < 0) {
0247 kfree(tunnel);
0248 goto out;
0249 }
0250 ret = l2tp_tunnel_notify(&l2tp_nl_family, info, tunnel,
0251 L2TP_CMD_TUNNEL_CREATE);
0252 l2tp_tunnel_dec_refcount(tunnel);
0253
0254 out:
0255 return ret;
0256 }
0257
0258 static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info)
0259 {
0260 struct l2tp_tunnel *tunnel;
0261 u32 tunnel_id;
0262 int ret = 0;
0263 struct net *net = genl_info_net(info);
0264
0265 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
0266 ret = -EINVAL;
0267 goto out;
0268 }
0269 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
0270
0271 tunnel = l2tp_tunnel_get(net, tunnel_id);
0272 if (!tunnel) {
0273 ret = -ENODEV;
0274 goto out;
0275 }
0276
0277 l2tp_tunnel_notify(&l2tp_nl_family, info,
0278 tunnel, L2TP_CMD_TUNNEL_DELETE);
0279
0280 l2tp_tunnel_delete(tunnel);
0281
0282 l2tp_tunnel_dec_refcount(tunnel);
0283
0284 out:
0285 return ret;
0286 }
0287
0288 static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info)
0289 {
0290 struct l2tp_tunnel *tunnel;
0291 u32 tunnel_id;
0292 int ret = 0;
0293 struct net *net = genl_info_net(info);
0294
0295 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
0296 ret = -EINVAL;
0297 goto out;
0298 }
0299 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
0300
0301 tunnel = l2tp_tunnel_get(net, tunnel_id);
0302 if (!tunnel) {
0303 ret = -ENODEV;
0304 goto out;
0305 }
0306
0307 ret = l2tp_tunnel_notify(&l2tp_nl_family, info,
0308 tunnel, L2TP_CMD_TUNNEL_MODIFY);
0309
0310 l2tp_tunnel_dec_refcount(tunnel);
0311
0312 out:
0313 return ret;
0314 }
0315
0316 #if IS_ENABLED(CONFIG_IPV6)
0317 static int l2tp_nl_tunnel_send_addr6(struct sk_buff *skb, struct sock *sk,
0318 enum l2tp_encap_type encap)
0319 {
0320 struct inet_sock *inet = inet_sk(sk);
0321 struct ipv6_pinfo *np = inet6_sk(sk);
0322
0323 switch (encap) {
0324 case L2TP_ENCAPTYPE_UDP:
0325 if (udp_get_no_check6_tx(sk) &&
0326 nla_put_flag(skb, L2TP_ATTR_UDP_ZERO_CSUM6_TX))
0327 return -1;
0328 if (udp_get_no_check6_rx(sk) &&
0329 nla_put_flag(skb, L2TP_ATTR_UDP_ZERO_CSUM6_RX))
0330 return -1;
0331 if (nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
0332 nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)))
0333 return -1;
0334 fallthrough;
0335 case L2TP_ENCAPTYPE_IP:
0336 if (nla_put_in6_addr(skb, L2TP_ATTR_IP6_SADDR, &np->saddr) ||
0337 nla_put_in6_addr(skb, L2TP_ATTR_IP6_DADDR, &sk->sk_v6_daddr))
0338 return -1;
0339 break;
0340 }
0341 return 0;
0342 }
0343 #endif
0344
0345 static int l2tp_nl_tunnel_send_addr4(struct sk_buff *skb, struct sock *sk,
0346 enum l2tp_encap_type encap)
0347 {
0348 struct inet_sock *inet = inet_sk(sk);
0349
0350 switch (encap) {
0351 case L2TP_ENCAPTYPE_UDP:
0352 if (nla_put_u8(skb, L2TP_ATTR_UDP_CSUM, !sk->sk_no_check_tx) ||
0353 nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
0354 nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)))
0355 return -1;
0356 fallthrough;
0357 case L2TP_ENCAPTYPE_IP:
0358 if (nla_put_in_addr(skb, L2TP_ATTR_IP_SADDR, inet->inet_saddr) ||
0359 nla_put_in_addr(skb, L2TP_ATTR_IP_DADDR, inet->inet_daddr))
0360 return -1;
0361 break;
0362 }
0363
0364 return 0;
0365 }
0366
0367
0368
0369
0370 static int l2tp_nl_tunnel_send_addr(struct sk_buff *skb, struct l2tp_tunnel *tunnel)
0371 {
0372 struct sock *sk = tunnel->sock;
0373
0374 if (!sk)
0375 return 0;
0376
0377 #if IS_ENABLED(CONFIG_IPV6)
0378 if (sk->sk_family == AF_INET6)
0379 return l2tp_nl_tunnel_send_addr6(skb, sk, tunnel->encap);
0380 #endif
0381 return l2tp_nl_tunnel_send_addr4(skb, sk, tunnel->encap);
0382 }
0383
0384 static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
0385 struct l2tp_tunnel *tunnel, u8 cmd)
0386 {
0387 void *hdr;
0388 struct nlattr *nest;
0389
0390 hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
0391 if (!hdr)
0392 return -EMSGSIZE;
0393
0394 if (nla_put_u8(skb, L2TP_ATTR_PROTO_VERSION, tunnel->version) ||
0395 nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
0396 nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
0397 nla_put_u32(skb, L2TP_ATTR_DEBUG, 0) ||
0398 nla_put_u16(skb, L2TP_ATTR_ENCAP_TYPE, tunnel->encap))
0399 goto nla_put_failure;
0400
0401 nest = nla_nest_start_noflag(skb, L2TP_ATTR_STATS);
0402 if (!nest)
0403 goto nla_put_failure;
0404
0405 if (nla_put_u64_64bit(skb, L2TP_ATTR_TX_PACKETS,
0406 atomic_long_read(&tunnel->stats.tx_packets),
0407 L2TP_ATTR_STATS_PAD) ||
0408 nla_put_u64_64bit(skb, L2TP_ATTR_TX_BYTES,
0409 atomic_long_read(&tunnel->stats.tx_bytes),
0410 L2TP_ATTR_STATS_PAD) ||
0411 nla_put_u64_64bit(skb, L2TP_ATTR_TX_ERRORS,
0412 atomic_long_read(&tunnel->stats.tx_errors),
0413 L2TP_ATTR_STATS_PAD) ||
0414 nla_put_u64_64bit(skb, L2TP_ATTR_RX_PACKETS,
0415 atomic_long_read(&tunnel->stats.rx_packets),
0416 L2TP_ATTR_STATS_PAD) ||
0417 nla_put_u64_64bit(skb, L2TP_ATTR_RX_BYTES,
0418 atomic_long_read(&tunnel->stats.rx_bytes),
0419 L2TP_ATTR_STATS_PAD) ||
0420 nla_put_u64_64bit(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
0421 atomic_long_read(&tunnel->stats.rx_seq_discards),
0422 L2TP_ATTR_STATS_PAD) ||
0423 nla_put_u64_64bit(skb, L2TP_ATTR_RX_COOKIE_DISCARDS,
0424 atomic_long_read(&tunnel->stats.rx_cookie_discards),
0425 L2TP_ATTR_STATS_PAD) ||
0426 nla_put_u64_64bit(skb, L2TP_ATTR_RX_OOS_PACKETS,
0427 atomic_long_read(&tunnel->stats.rx_oos_packets),
0428 L2TP_ATTR_STATS_PAD) ||
0429 nla_put_u64_64bit(skb, L2TP_ATTR_RX_ERRORS,
0430 atomic_long_read(&tunnel->stats.rx_errors),
0431 L2TP_ATTR_STATS_PAD) ||
0432 nla_put_u64_64bit(skb, L2TP_ATTR_RX_INVALID,
0433 atomic_long_read(&tunnel->stats.rx_invalid),
0434 L2TP_ATTR_STATS_PAD))
0435 goto nla_put_failure;
0436 nla_nest_end(skb, nest);
0437
0438 if (l2tp_nl_tunnel_send_addr(skb, tunnel))
0439 goto nla_put_failure;
0440
0441 genlmsg_end(skb, hdr);
0442 return 0;
0443
0444 nla_put_failure:
0445 genlmsg_cancel(skb, hdr);
0446 return -1;
0447 }
0448
0449 static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
0450 {
0451 struct l2tp_tunnel *tunnel;
0452 struct sk_buff *msg;
0453 u32 tunnel_id;
0454 int ret = -ENOBUFS;
0455 struct net *net = genl_info_net(info);
0456
0457 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
0458 ret = -EINVAL;
0459 goto err;
0460 }
0461
0462 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
0463
0464 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0465 if (!msg) {
0466 ret = -ENOMEM;
0467 goto err;
0468 }
0469
0470 tunnel = l2tp_tunnel_get(net, tunnel_id);
0471 if (!tunnel) {
0472 ret = -ENODEV;
0473 goto err_nlmsg;
0474 }
0475
0476 ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
0477 NLM_F_ACK, tunnel, L2TP_CMD_TUNNEL_GET);
0478 if (ret < 0)
0479 goto err_nlmsg_tunnel;
0480
0481 l2tp_tunnel_dec_refcount(tunnel);
0482
0483 return genlmsg_unicast(net, msg, info->snd_portid);
0484
0485 err_nlmsg_tunnel:
0486 l2tp_tunnel_dec_refcount(tunnel);
0487 err_nlmsg:
0488 nlmsg_free(msg);
0489 err:
0490 return ret;
0491 }
0492
0493 static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
0494 {
0495 int ti = cb->args[0];
0496 struct l2tp_tunnel *tunnel;
0497 struct net *net = sock_net(skb->sk);
0498
0499 for (;;) {
0500 tunnel = l2tp_tunnel_get_nth(net, ti);
0501 if (!tunnel)
0502 goto out;
0503
0504 if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).portid,
0505 cb->nlh->nlmsg_seq, NLM_F_MULTI,
0506 tunnel, L2TP_CMD_TUNNEL_GET) < 0) {
0507 l2tp_tunnel_dec_refcount(tunnel);
0508 goto out;
0509 }
0510 l2tp_tunnel_dec_refcount(tunnel);
0511
0512 ti++;
0513 }
0514
0515 out:
0516 cb->args[0] = ti;
0517
0518 return skb->len;
0519 }
0520
0521 static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
0522 {
0523 u32 tunnel_id = 0;
0524 u32 session_id;
0525 u32 peer_session_id;
0526 int ret = 0;
0527 struct l2tp_tunnel *tunnel;
0528 struct l2tp_session *session;
0529 struct l2tp_session_cfg cfg = { 0, };
0530 struct net *net = genl_info_net(info);
0531
0532 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
0533 ret = -EINVAL;
0534 goto out;
0535 }
0536
0537 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
0538 tunnel = l2tp_tunnel_get(net, tunnel_id);
0539 if (!tunnel) {
0540 ret = -ENODEV;
0541 goto out;
0542 }
0543
0544 if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
0545 ret = -EINVAL;
0546 goto out_tunnel;
0547 }
0548 session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
0549
0550 if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
0551 ret = -EINVAL;
0552 goto out_tunnel;
0553 }
0554 peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
0555
0556 if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
0557 ret = -EINVAL;
0558 goto out_tunnel;
0559 }
0560 cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
0561 if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
0562 ret = -EINVAL;
0563 goto out_tunnel;
0564 }
0565
0566
0567 if (tunnel->version == 2 && cfg.pw_type != L2TP_PWTYPE_PPP) {
0568 ret = -EPROTONOSUPPORT;
0569 goto out_tunnel;
0570 }
0571
0572 if (tunnel->version > 2) {
0573 if (info->attrs[L2TP_ATTR_L2SPEC_TYPE]) {
0574 cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
0575 if (cfg.l2specific_type != L2TP_L2SPECTYPE_DEFAULT &&
0576 cfg.l2specific_type != L2TP_L2SPECTYPE_NONE) {
0577 ret = -EINVAL;
0578 goto out_tunnel;
0579 }
0580 } else {
0581 cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
0582 }
0583
0584 if (info->attrs[L2TP_ATTR_COOKIE]) {
0585 u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
0586
0587 if (len > 8) {
0588 ret = -EINVAL;
0589 goto out_tunnel;
0590 }
0591 cfg.cookie_len = len;
0592 memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
0593 }
0594 if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
0595 u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
0596
0597 if (len > 8) {
0598 ret = -EINVAL;
0599 goto out_tunnel;
0600 }
0601 cfg.peer_cookie_len = len;
0602 memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
0603 }
0604 if (info->attrs[L2TP_ATTR_IFNAME])
0605 cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
0606 }
0607
0608 if (info->attrs[L2TP_ATTR_RECV_SEQ])
0609 cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
0610
0611 if (info->attrs[L2TP_ATTR_SEND_SEQ])
0612 cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
0613
0614 if (info->attrs[L2TP_ATTR_LNS_MODE])
0615 cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
0616
0617 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
0618 cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
0619
0620 #ifdef CONFIG_MODULES
0621 if (!l2tp_nl_cmd_ops[cfg.pw_type]) {
0622 genl_unlock();
0623 request_module("net-l2tp-type-%u", cfg.pw_type);
0624 genl_lock();
0625 }
0626 #endif
0627 if (!l2tp_nl_cmd_ops[cfg.pw_type] || !l2tp_nl_cmd_ops[cfg.pw_type]->session_create) {
0628 ret = -EPROTONOSUPPORT;
0629 goto out_tunnel;
0630 }
0631
0632 ret = l2tp_nl_cmd_ops[cfg.pw_type]->session_create(net, tunnel,
0633 session_id,
0634 peer_session_id,
0635 &cfg);
0636
0637 if (ret >= 0) {
0638 session = l2tp_tunnel_get_session(tunnel, session_id);
0639 if (session) {
0640 ret = l2tp_session_notify(&l2tp_nl_family, info, session,
0641 L2TP_CMD_SESSION_CREATE);
0642 l2tp_session_dec_refcount(session);
0643 }
0644 }
0645
0646 out_tunnel:
0647 l2tp_tunnel_dec_refcount(tunnel);
0648 out:
0649 return ret;
0650 }
0651
0652 static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
0653 {
0654 int ret = 0;
0655 struct l2tp_session *session;
0656 u16 pw_type;
0657
0658 session = l2tp_nl_session_get(info);
0659 if (!session) {
0660 ret = -ENODEV;
0661 goto out;
0662 }
0663
0664 l2tp_session_notify(&l2tp_nl_family, info,
0665 session, L2TP_CMD_SESSION_DELETE);
0666
0667 pw_type = session->pwtype;
0668 if (pw_type < __L2TP_PWTYPE_MAX)
0669 if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
0670 l2tp_nl_cmd_ops[pw_type]->session_delete(session);
0671
0672 l2tp_session_dec_refcount(session);
0673
0674 out:
0675 return ret;
0676 }
0677
0678 static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
0679 {
0680 int ret = 0;
0681 struct l2tp_session *session;
0682
0683 session = l2tp_nl_session_get(info);
0684 if (!session) {
0685 ret = -ENODEV;
0686 goto out;
0687 }
0688
0689 if (info->attrs[L2TP_ATTR_RECV_SEQ])
0690 session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
0691
0692 if (info->attrs[L2TP_ATTR_SEND_SEQ]) {
0693 session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
0694 l2tp_session_set_header_len(session, session->tunnel->version);
0695 }
0696
0697 if (info->attrs[L2TP_ATTR_LNS_MODE])
0698 session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
0699
0700 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
0701 session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
0702
0703 ret = l2tp_session_notify(&l2tp_nl_family, info,
0704 session, L2TP_CMD_SESSION_MODIFY);
0705
0706 l2tp_session_dec_refcount(session);
0707
0708 out:
0709 return ret;
0710 }
0711
0712 static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
0713 struct l2tp_session *session, u8 cmd)
0714 {
0715 void *hdr;
0716 struct nlattr *nest;
0717 struct l2tp_tunnel *tunnel = session->tunnel;
0718
0719 hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
0720 if (!hdr)
0721 return -EMSGSIZE;
0722
0723 if (nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
0724 nla_put_u32(skb, L2TP_ATTR_SESSION_ID, session->session_id) ||
0725 nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
0726 nla_put_u32(skb, L2TP_ATTR_PEER_SESSION_ID, session->peer_session_id) ||
0727 nla_put_u32(skb, L2TP_ATTR_DEBUG, 0) ||
0728 nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype))
0729 goto nla_put_failure;
0730
0731 if ((session->ifname[0] &&
0732 nla_put_string(skb, L2TP_ATTR_IFNAME, session->ifname)) ||
0733 (session->cookie_len &&
0734 nla_put(skb, L2TP_ATTR_COOKIE, session->cookie_len, session->cookie)) ||
0735 (session->peer_cookie_len &&
0736 nla_put(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len, session->peer_cookie)) ||
0737 nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
0738 nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
0739 nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
0740 (l2tp_tunnel_uses_xfrm(tunnel) &&
0741 nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
0742 (session->reorder_timeout &&
0743 nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT,
0744 session->reorder_timeout, L2TP_ATTR_PAD)))
0745 goto nla_put_failure;
0746
0747 nest = nla_nest_start_noflag(skb, L2TP_ATTR_STATS);
0748 if (!nest)
0749 goto nla_put_failure;
0750
0751 if (nla_put_u64_64bit(skb, L2TP_ATTR_TX_PACKETS,
0752 atomic_long_read(&session->stats.tx_packets),
0753 L2TP_ATTR_STATS_PAD) ||
0754 nla_put_u64_64bit(skb, L2TP_ATTR_TX_BYTES,
0755 atomic_long_read(&session->stats.tx_bytes),
0756 L2TP_ATTR_STATS_PAD) ||
0757 nla_put_u64_64bit(skb, L2TP_ATTR_TX_ERRORS,
0758 atomic_long_read(&session->stats.tx_errors),
0759 L2TP_ATTR_STATS_PAD) ||
0760 nla_put_u64_64bit(skb, L2TP_ATTR_RX_PACKETS,
0761 atomic_long_read(&session->stats.rx_packets),
0762 L2TP_ATTR_STATS_PAD) ||
0763 nla_put_u64_64bit(skb, L2TP_ATTR_RX_BYTES,
0764 atomic_long_read(&session->stats.rx_bytes),
0765 L2TP_ATTR_STATS_PAD) ||
0766 nla_put_u64_64bit(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
0767 atomic_long_read(&session->stats.rx_seq_discards),
0768 L2TP_ATTR_STATS_PAD) ||
0769 nla_put_u64_64bit(skb, L2TP_ATTR_RX_COOKIE_DISCARDS,
0770 atomic_long_read(&session->stats.rx_cookie_discards),
0771 L2TP_ATTR_STATS_PAD) ||
0772 nla_put_u64_64bit(skb, L2TP_ATTR_RX_OOS_PACKETS,
0773 atomic_long_read(&session->stats.rx_oos_packets),
0774 L2TP_ATTR_STATS_PAD) ||
0775 nla_put_u64_64bit(skb, L2TP_ATTR_RX_ERRORS,
0776 atomic_long_read(&session->stats.rx_errors),
0777 L2TP_ATTR_STATS_PAD) ||
0778 nla_put_u64_64bit(skb, L2TP_ATTR_RX_INVALID,
0779 atomic_long_read(&session->stats.rx_invalid),
0780 L2TP_ATTR_STATS_PAD))
0781 goto nla_put_failure;
0782 nla_nest_end(skb, nest);
0783
0784 genlmsg_end(skb, hdr);
0785 return 0;
0786
0787 nla_put_failure:
0788 genlmsg_cancel(skb, hdr);
0789 return -1;
0790 }
0791
0792 static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
0793 {
0794 struct l2tp_session *session;
0795 struct sk_buff *msg;
0796 int ret;
0797
0798 session = l2tp_nl_session_get(info);
0799 if (!session) {
0800 ret = -ENODEV;
0801 goto err;
0802 }
0803
0804 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
0805 if (!msg) {
0806 ret = -ENOMEM;
0807 goto err_ref;
0808 }
0809
0810 ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
0811 0, session, L2TP_CMD_SESSION_GET);
0812 if (ret < 0)
0813 goto err_ref_msg;
0814
0815 ret = genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
0816
0817 l2tp_session_dec_refcount(session);
0818
0819 return ret;
0820
0821 err_ref_msg:
0822 nlmsg_free(msg);
0823 err_ref:
0824 l2tp_session_dec_refcount(session);
0825 err:
0826 return ret;
0827 }
0828
0829 static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
0830 {
0831 struct net *net = sock_net(skb->sk);
0832 struct l2tp_session *session;
0833 struct l2tp_tunnel *tunnel = NULL;
0834 int ti = cb->args[0];
0835 int si = cb->args[1];
0836
0837 for (;;) {
0838 if (!tunnel) {
0839 tunnel = l2tp_tunnel_get_nth(net, ti);
0840 if (!tunnel)
0841 goto out;
0842 }
0843
0844 session = l2tp_session_get_nth(tunnel, si);
0845 if (!session) {
0846 ti++;
0847 l2tp_tunnel_dec_refcount(tunnel);
0848 tunnel = NULL;
0849 si = 0;
0850 continue;
0851 }
0852
0853 if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).portid,
0854 cb->nlh->nlmsg_seq, NLM_F_MULTI,
0855 session, L2TP_CMD_SESSION_GET) < 0) {
0856 l2tp_session_dec_refcount(session);
0857 l2tp_tunnel_dec_refcount(tunnel);
0858 break;
0859 }
0860 l2tp_session_dec_refcount(session);
0861
0862 si++;
0863 }
0864
0865 out:
0866 cb->args[0] = ti;
0867 cb->args[1] = si;
0868
0869 return skb->len;
0870 }
0871
0872 static const struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
0873 [L2TP_ATTR_NONE] = { .type = NLA_UNSPEC, },
0874 [L2TP_ATTR_PW_TYPE] = { .type = NLA_U16, },
0875 [L2TP_ATTR_ENCAP_TYPE] = { .type = NLA_U16, },
0876 [L2TP_ATTR_OFFSET] = { .type = NLA_U16, },
0877 [L2TP_ATTR_DATA_SEQ] = { .type = NLA_U8, },
0878 [L2TP_ATTR_L2SPEC_TYPE] = { .type = NLA_U8, },
0879 [L2TP_ATTR_L2SPEC_LEN] = { .type = NLA_U8, },
0880 [L2TP_ATTR_PROTO_VERSION] = { .type = NLA_U8, },
0881 [L2TP_ATTR_CONN_ID] = { .type = NLA_U32, },
0882 [L2TP_ATTR_PEER_CONN_ID] = { .type = NLA_U32, },
0883 [L2TP_ATTR_SESSION_ID] = { .type = NLA_U32, },
0884 [L2TP_ATTR_PEER_SESSION_ID] = { .type = NLA_U32, },
0885 [L2TP_ATTR_UDP_CSUM] = { .type = NLA_U8, },
0886 [L2TP_ATTR_VLAN_ID] = { .type = NLA_U16, },
0887 [L2TP_ATTR_DEBUG] = { .type = NLA_U32, },
0888 [L2TP_ATTR_RECV_SEQ] = { .type = NLA_U8, },
0889 [L2TP_ATTR_SEND_SEQ] = { .type = NLA_U8, },
0890 [L2TP_ATTR_LNS_MODE] = { .type = NLA_U8, },
0891 [L2TP_ATTR_USING_IPSEC] = { .type = NLA_U8, },
0892 [L2TP_ATTR_RECV_TIMEOUT] = { .type = NLA_MSECS, },
0893 [L2TP_ATTR_FD] = { .type = NLA_U32, },
0894 [L2TP_ATTR_IP_SADDR] = { .type = NLA_U32, },
0895 [L2TP_ATTR_IP_DADDR] = { .type = NLA_U32, },
0896 [L2TP_ATTR_UDP_SPORT] = { .type = NLA_U16, },
0897 [L2TP_ATTR_UDP_DPORT] = { .type = NLA_U16, },
0898 [L2TP_ATTR_MTU] = { .type = NLA_U16, },
0899 [L2TP_ATTR_MRU] = { .type = NLA_U16, },
0900 [L2TP_ATTR_STATS] = { .type = NLA_NESTED, },
0901 [L2TP_ATTR_IP6_SADDR] = {
0902 .type = NLA_BINARY,
0903 .len = sizeof(struct in6_addr),
0904 },
0905 [L2TP_ATTR_IP6_DADDR] = {
0906 .type = NLA_BINARY,
0907 .len = sizeof(struct in6_addr),
0908 },
0909 [L2TP_ATTR_IFNAME] = {
0910 .type = NLA_NUL_STRING,
0911 .len = IFNAMSIZ - 1,
0912 },
0913 [L2TP_ATTR_COOKIE] = {
0914 .type = NLA_BINARY,
0915 .len = 8,
0916 },
0917 [L2TP_ATTR_PEER_COOKIE] = {
0918 .type = NLA_BINARY,
0919 .len = 8,
0920 },
0921 };
0922
0923 static const struct genl_small_ops l2tp_nl_ops[] = {
0924 {
0925 .cmd = L2TP_CMD_NOOP,
0926 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
0927 .doit = l2tp_nl_cmd_noop,
0928
0929 },
0930 {
0931 .cmd = L2TP_CMD_TUNNEL_CREATE,
0932 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
0933 .doit = l2tp_nl_cmd_tunnel_create,
0934 .flags = GENL_UNS_ADMIN_PERM,
0935 },
0936 {
0937 .cmd = L2TP_CMD_TUNNEL_DELETE,
0938 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
0939 .doit = l2tp_nl_cmd_tunnel_delete,
0940 .flags = GENL_UNS_ADMIN_PERM,
0941 },
0942 {
0943 .cmd = L2TP_CMD_TUNNEL_MODIFY,
0944 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
0945 .doit = l2tp_nl_cmd_tunnel_modify,
0946 .flags = GENL_UNS_ADMIN_PERM,
0947 },
0948 {
0949 .cmd = L2TP_CMD_TUNNEL_GET,
0950 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
0951 .doit = l2tp_nl_cmd_tunnel_get,
0952 .dumpit = l2tp_nl_cmd_tunnel_dump,
0953 .flags = GENL_UNS_ADMIN_PERM,
0954 },
0955 {
0956 .cmd = L2TP_CMD_SESSION_CREATE,
0957 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
0958 .doit = l2tp_nl_cmd_session_create,
0959 .flags = GENL_UNS_ADMIN_PERM,
0960 },
0961 {
0962 .cmd = L2TP_CMD_SESSION_DELETE,
0963 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
0964 .doit = l2tp_nl_cmd_session_delete,
0965 .flags = GENL_UNS_ADMIN_PERM,
0966 },
0967 {
0968 .cmd = L2TP_CMD_SESSION_MODIFY,
0969 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
0970 .doit = l2tp_nl_cmd_session_modify,
0971 .flags = GENL_UNS_ADMIN_PERM,
0972 },
0973 {
0974 .cmd = L2TP_CMD_SESSION_GET,
0975 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
0976 .doit = l2tp_nl_cmd_session_get,
0977 .dumpit = l2tp_nl_cmd_session_dump,
0978 .flags = GENL_UNS_ADMIN_PERM,
0979 },
0980 };
0981
0982 static struct genl_family l2tp_nl_family __ro_after_init = {
0983 .name = L2TP_GENL_NAME,
0984 .version = L2TP_GENL_VERSION,
0985 .hdrsize = 0,
0986 .maxattr = L2TP_ATTR_MAX,
0987 .policy = l2tp_nl_policy,
0988 .netnsok = true,
0989 .module = THIS_MODULE,
0990 .small_ops = l2tp_nl_ops,
0991 .n_small_ops = ARRAY_SIZE(l2tp_nl_ops),
0992 .mcgrps = l2tp_multicast_group,
0993 .n_mcgrps = ARRAY_SIZE(l2tp_multicast_group),
0994 };
0995
0996 int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
0997 {
0998 int ret;
0999
1000 ret = -EINVAL;
1001 if (pw_type >= __L2TP_PWTYPE_MAX)
1002 goto err;
1003
1004 genl_lock();
1005 ret = -EBUSY;
1006 if (l2tp_nl_cmd_ops[pw_type])
1007 goto out;
1008
1009 l2tp_nl_cmd_ops[pw_type] = ops;
1010 ret = 0;
1011
1012 out:
1013 genl_unlock();
1014 err:
1015 return ret;
1016 }
1017 EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
1018
1019 void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
1020 {
1021 if (pw_type < __L2TP_PWTYPE_MAX) {
1022 genl_lock();
1023 l2tp_nl_cmd_ops[pw_type] = NULL;
1024 genl_unlock();
1025 }
1026 }
1027 EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
1028
1029 static int __init l2tp_nl_init(void)
1030 {
1031 pr_info("L2TP netlink interface\n");
1032 return genl_register_family(&l2tp_nl_family);
1033 }
1034
1035 static void l2tp_nl_cleanup(void)
1036 {
1037 genl_unregister_family(&l2tp_nl_family);
1038 }
1039
1040 module_init(l2tp_nl_init);
1041 module_exit(l2tp_nl_cleanup);
1042
1043 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
1044 MODULE_DESCRIPTION("L2TP netlink");
1045 MODULE_LICENSE("GPL");
1046 MODULE_VERSION("1.0");
1047 MODULE_ALIAS_GENL_FAMILY("l2tp");