123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866 |
- /*
- * L2TP netlink layer, for management
- *
- * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
- *
- * Partly based on the IrDA nelink implementation
- * (see net/irda/irnetlink.c) which is:
- * Copyright (c) 2007 Samuel Ortiz <samuel@sortiz.org>
- * which is in turn partly based on the wireless netlink code:
- * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- */
- #include <net/sock.h>
- #include <net/genetlink.h>
- #include <net/udp.h>
- #include <linux/in.h>
- #include <linux/udp.h>
- #include <linux/socket.h>
- #include <linux/module.h>
- #include <linux/list.h>
- #include <net/net_namespace.h>
- #include <linux/l2tp.h>
- #include "l2tp_core.h"
- static struct genl_family l2tp_nl_family = {
- .id = GENL_ID_GENERATE,
- .name = L2TP_GENL_NAME,
- .version = L2TP_GENL_VERSION,
- .hdrsize = 0,
- .maxattr = L2TP_ATTR_MAX,
- };
- /* Accessed under genl lock */
- static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX];
- static struct l2tp_session *l2tp_nl_session_get(struct genl_info *info,
- bool do_ref)
- {
- u32 tunnel_id;
- u32 session_id;
- char *ifname;
- struct l2tp_tunnel *tunnel;
- struct l2tp_session *session = NULL;
- struct net *net = genl_info_net(info);
- if (info->attrs[L2TP_ATTR_IFNAME]) {
- ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
- session = l2tp_session_get_by_ifname(net, ifname, do_ref);
- } else if ((info->attrs[L2TP_ATTR_SESSION_ID]) &&
- (info->attrs[L2TP_ATTR_CONN_ID])) {
- tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
- session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
- tunnel = l2tp_tunnel_get(net, tunnel_id);
- if (tunnel) {
- session = l2tp_session_get(net, tunnel, session_id,
- do_ref);
- l2tp_tunnel_dec_refcount(tunnel);
- }
- }
- return session;
- }
- static int l2tp_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
- {
- struct sk_buff *msg;
- void *hdr;
- int ret = -ENOBUFS;
- msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
- if (!msg) {
- ret = -ENOMEM;
- goto out;
- }
- hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
- &l2tp_nl_family, 0, L2TP_CMD_NOOP);
- if (IS_ERR(hdr)) {
- ret = PTR_ERR(hdr);
- goto err_out;
- }
- genlmsg_end(msg, hdr);
- return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
- err_out:
- nlmsg_free(msg);
- out:
- return ret;
- }
- static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info)
- {
- u32 tunnel_id;
- u32 peer_tunnel_id;
- int proto_version;
- int fd;
- int ret = 0;
- struct l2tp_tunnel_cfg cfg = { 0, };
- struct l2tp_tunnel *tunnel;
- struct net *net = genl_info_net(info);
- if (!info->attrs[L2TP_ATTR_CONN_ID]) {
- ret = -EINVAL;
- goto out;
- }
- tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
- if (!info->attrs[L2TP_ATTR_PEER_CONN_ID]) {
- ret = -EINVAL;
- goto out;
- }
- peer_tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_CONN_ID]);
- if (!info->attrs[L2TP_ATTR_PROTO_VERSION]) {
- ret = -EINVAL;
- goto out;
- }
- proto_version = nla_get_u8(info->attrs[L2TP_ATTR_PROTO_VERSION]);
- if (!info->attrs[L2TP_ATTR_ENCAP_TYPE]) {
- ret = -EINVAL;
- goto out;
- }
- cfg.encap = nla_get_u16(info->attrs[L2TP_ATTR_ENCAP_TYPE]);
- fd = -1;
- if (info->attrs[L2TP_ATTR_FD]) {
- fd = nla_get_u32(info->attrs[L2TP_ATTR_FD]);
- } else {
- if (info->attrs[L2TP_ATTR_IP_SADDR])
- cfg.local_ip.s_addr = nla_get_be32(info->attrs[L2TP_ATTR_IP_SADDR]);
- if (info->attrs[L2TP_ATTR_IP_DADDR])
- cfg.peer_ip.s_addr = nla_get_be32(info->attrs[L2TP_ATTR_IP_DADDR]);
- if (info->attrs[L2TP_ATTR_UDP_SPORT])
- cfg.local_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_SPORT]);
- if (info->attrs[L2TP_ATTR_UDP_DPORT])
- cfg.peer_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_DPORT]);
- if (info->attrs[L2TP_ATTR_UDP_CSUM])
- cfg.use_udp_checksums = nla_get_flag(info->attrs[L2TP_ATTR_UDP_CSUM]);
- }
- if (info->attrs[L2TP_ATTR_DEBUG])
- cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
- tunnel = l2tp_tunnel_find(net, tunnel_id);
- if (tunnel != NULL) {
- ret = -EEXIST;
- goto out;
- }
- ret = -EINVAL;
- switch (cfg.encap) {
- case L2TP_ENCAPTYPE_UDP:
- case L2TP_ENCAPTYPE_IP:
- ret = l2tp_tunnel_create(net, fd, proto_version, tunnel_id,
- peer_tunnel_id, &cfg, &tunnel);
- break;
- }
- out:
- return ret;
- }
- static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info)
- {
- struct l2tp_tunnel *tunnel;
- u32 tunnel_id;
- int ret = 0;
- struct net *net = genl_info_net(info);
- if (!info->attrs[L2TP_ATTR_CONN_ID]) {
- ret = -EINVAL;
- goto out;
- }
- tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
- tunnel = l2tp_tunnel_get(net, tunnel_id);
- if (!tunnel) {
- ret = -ENODEV;
- goto out;
- }
- (void) l2tp_tunnel_delete(tunnel);
- l2tp_tunnel_dec_refcount(tunnel);
- out:
- return ret;
- }
- static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info)
- {
- struct l2tp_tunnel *tunnel;
- u32 tunnel_id;
- int ret = 0;
- struct net *net = genl_info_net(info);
- if (!info->attrs[L2TP_ATTR_CONN_ID]) {
- ret = -EINVAL;
- goto out;
- }
- tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
- tunnel = l2tp_tunnel_get(net, tunnel_id);
- if (!tunnel) {
- ret = -ENODEV;
- goto out;
- }
- if (info->attrs[L2TP_ATTR_DEBUG])
- tunnel->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
- l2tp_tunnel_dec_refcount(tunnel);
- out:
- return ret;
- }
- static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 pid, u32 seq, int flags,
- struct l2tp_tunnel *tunnel)
- {
- void *hdr;
- struct nlattr *nest;
- struct sock *sk = NULL;
- struct inet_sock *inet;
- hdr = genlmsg_put(skb, pid, seq, &l2tp_nl_family, flags,
- L2TP_CMD_TUNNEL_GET);
- if (IS_ERR(hdr))
- return PTR_ERR(hdr);
- NLA_PUT_U8(skb, L2TP_ATTR_PROTO_VERSION, tunnel->version);
- NLA_PUT_U32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id);
- NLA_PUT_U32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id);
- NLA_PUT_U32(skb, L2TP_ATTR_DEBUG, tunnel->debug);
- NLA_PUT_U16(skb, L2TP_ATTR_ENCAP_TYPE, tunnel->encap);
- nest = nla_nest_start(skb, L2TP_ATTR_STATS);
- if (nest == NULL)
- goto nla_put_failure;
- NLA_PUT_U64(skb, L2TP_ATTR_TX_PACKETS, tunnel->stats.tx_packets);
- NLA_PUT_U64(skb, L2TP_ATTR_TX_BYTES, tunnel->stats.tx_bytes);
- NLA_PUT_U64(skb, L2TP_ATTR_TX_ERRORS, tunnel->stats.tx_errors);
- NLA_PUT_U64(skb, L2TP_ATTR_RX_PACKETS, tunnel->stats.rx_packets);
- NLA_PUT_U64(skb, L2TP_ATTR_RX_BYTES, tunnel->stats.rx_bytes);
- NLA_PUT_U64(skb, L2TP_ATTR_RX_SEQ_DISCARDS, tunnel->stats.rx_seq_discards);
- NLA_PUT_U64(skb, L2TP_ATTR_RX_OOS_PACKETS, tunnel->stats.rx_oos_packets);
- NLA_PUT_U64(skb, L2TP_ATTR_RX_ERRORS, tunnel->stats.rx_errors);
- nla_nest_end(skb, nest);
- sk = tunnel->sock;
- if (!sk)
- goto out;
- inet = inet_sk(sk);
- switch (tunnel->encap) {
- case L2TP_ENCAPTYPE_UDP:
- NLA_PUT_U16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport));
- NLA_PUT_U16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport));
- NLA_PUT_U8(skb, L2TP_ATTR_UDP_CSUM, (sk->sk_no_check != UDP_CSUM_NOXMIT));
- /* NOBREAK */
- case L2TP_ENCAPTYPE_IP:
- NLA_PUT_BE32(skb, L2TP_ATTR_IP_SADDR, inet->inet_saddr);
- NLA_PUT_BE32(skb, L2TP_ATTR_IP_DADDR, inet->inet_daddr);
- break;
- }
- out:
- return genlmsg_end(skb, hdr);
- nla_put_failure:
- genlmsg_cancel(skb, hdr);
- return -1;
- }
- static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
- {
- struct l2tp_tunnel *tunnel;
- struct sk_buff *msg;
- u32 tunnel_id;
- int ret = -ENOBUFS;
- struct net *net = genl_info_net(info);
- if (!info->attrs[L2TP_ATTR_CONN_ID]) {
- ret = -EINVAL;
- goto err;
- }
- tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
- msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
- if (!msg) {
- ret = -ENOMEM;
- goto err;
- }
- tunnel = l2tp_tunnel_get(net, tunnel_id);
- if (!tunnel) {
- ret = -ENODEV;
- goto err_nlmsg;
- }
- ret = l2tp_nl_tunnel_send(msg, info->snd_pid, info->snd_seq,
- NLM_F_ACK, tunnel);
- if (ret < 0)
- goto err_nlmsg_tunnel;
- l2tp_tunnel_dec_refcount(tunnel);
- return genlmsg_unicast(net, msg, info->snd_pid);
- err_nlmsg_tunnel:
- l2tp_tunnel_dec_refcount(tunnel);
- err_nlmsg:
- nlmsg_free(msg);
- err:
- return ret;
- }
- static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
- {
- int ti = cb->args[0];
- struct l2tp_tunnel *tunnel;
- struct net *net = sock_net(skb->sk);
- for (;;) {
- tunnel = l2tp_tunnel_find_nth(net, ti);
- if (tunnel == NULL)
- goto out;
- if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).pid,
- cb->nlh->nlmsg_seq, NLM_F_MULTI,
- tunnel) <= 0)
- goto out;
- ti++;
- }
- out:
- cb->args[0] = ti;
- return skb->len;
- }
- static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
- {
- u32 tunnel_id = 0;
- u32 session_id;
- u32 peer_session_id;
- int ret = 0;
- struct l2tp_tunnel *tunnel;
- struct l2tp_session_cfg cfg = { 0, };
- struct net *net = genl_info_net(info);
- if (!info->attrs[L2TP_ATTR_CONN_ID]) {
- ret = -EINVAL;
- goto out;
- }
- tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
- tunnel = l2tp_tunnel_get(net, tunnel_id);
- if (!tunnel) {
- ret = -ENODEV;
- goto out;
- }
- if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
- ret = -EINVAL;
- goto out_tunnel;
- }
- session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
- if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
- ret = -EINVAL;
- goto out_tunnel;
- }
- peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
- if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
- ret = -EINVAL;
- goto out_tunnel;
- }
- cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
- if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
- ret = -EINVAL;
- goto out_tunnel;
- }
- if (tunnel->version > 2) {
- if (info->attrs[L2TP_ATTR_OFFSET])
- cfg.offset = nla_get_u16(info->attrs[L2TP_ATTR_OFFSET]);
- if (info->attrs[L2TP_ATTR_DATA_SEQ])
- cfg.data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
- cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
- if (info->attrs[L2TP_ATTR_L2SPEC_TYPE])
- cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
- cfg.l2specific_len = 4;
- if (info->attrs[L2TP_ATTR_L2SPEC_LEN])
- cfg.l2specific_len = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_LEN]);
- if (info->attrs[L2TP_ATTR_COOKIE]) {
- u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
- if (len > 8) {
- ret = -EINVAL;
- goto out_tunnel;
- }
- cfg.cookie_len = len;
- memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
- }
- if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
- u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
- if (len > 8) {
- ret = -EINVAL;
- goto out_tunnel;
- }
- cfg.peer_cookie_len = len;
- memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
- }
- if (info->attrs[L2TP_ATTR_IFNAME])
- cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
- if (info->attrs[L2TP_ATTR_VLAN_ID])
- cfg.vlan_id = nla_get_u16(info->attrs[L2TP_ATTR_VLAN_ID]);
- }
- if (info->attrs[L2TP_ATTR_DEBUG])
- cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
- if (info->attrs[L2TP_ATTR_RECV_SEQ])
- cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
- if (info->attrs[L2TP_ATTR_SEND_SEQ])
- cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
- if (info->attrs[L2TP_ATTR_LNS_MODE])
- cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
- if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
- cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
- if (info->attrs[L2TP_ATTR_MTU])
- cfg.mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
- if (info->attrs[L2TP_ATTR_MRU])
- cfg.mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
- if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) ||
- (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) {
- ret = -EPROTONOSUPPORT;
- goto out_tunnel;
- }
- /* Check that pseudowire-specific params are present */
- switch (cfg.pw_type) {
- case L2TP_PWTYPE_NONE:
- break;
- case L2TP_PWTYPE_ETH_VLAN:
- if (!info->attrs[L2TP_ATTR_VLAN_ID]) {
- ret = -EINVAL;
- goto out_tunnel;
- }
- break;
- case L2TP_PWTYPE_ETH:
- break;
- case L2TP_PWTYPE_PPP:
- case L2TP_PWTYPE_PPP_AC:
- break;
- case L2TP_PWTYPE_IP:
- default:
- ret = -EPROTONOSUPPORT;
- break;
- }
- ret = l2tp_nl_cmd_ops[cfg.pw_type]->session_create(net, tunnel,
- session_id,
- peer_session_id,
- &cfg);
- out_tunnel:
- l2tp_tunnel_dec_refcount(tunnel);
- out:
- return ret;
- }
- static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
- {
- int ret = 0;
- struct l2tp_session *session;
- u16 pw_type;
- session = l2tp_nl_session_get(info, true);
- if (session == NULL) {
- ret = -ENODEV;
- goto out;
- }
- pw_type = session->pwtype;
- if (pw_type < __L2TP_PWTYPE_MAX)
- if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
- ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
- if (session->deref)
- session->deref(session);
- l2tp_session_dec_refcount(session);
- out:
- return ret;
- }
- static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
- {
- int ret = 0;
- struct l2tp_session *session;
- session = l2tp_nl_session_get(info, false);
- if (session == NULL) {
- ret = -ENODEV;
- goto out;
- }
- if (info->attrs[L2TP_ATTR_DEBUG])
- session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
- if (info->attrs[L2TP_ATTR_DATA_SEQ])
- session->data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
- if (info->attrs[L2TP_ATTR_RECV_SEQ])
- session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
- if (info->attrs[L2TP_ATTR_SEND_SEQ])
- session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
- if (info->attrs[L2TP_ATTR_LNS_MODE])
- session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
- if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
- session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
- if (info->attrs[L2TP_ATTR_MTU])
- session->mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
- if (info->attrs[L2TP_ATTR_MRU])
- session->mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
- l2tp_session_dec_refcount(session);
- out:
- return ret;
- }
- static int l2tp_nl_session_send(struct sk_buff *skb, u32 pid, u32 seq, int flags,
- struct l2tp_session *session)
- {
- void *hdr;
- struct nlattr *nest;
- struct l2tp_tunnel *tunnel = session->tunnel;
- struct sock *sk = NULL;
- sk = tunnel->sock;
- hdr = genlmsg_put(skb, pid, seq, &l2tp_nl_family, flags, L2TP_CMD_SESSION_GET);
- if (IS_ERR(hdr))
- return PTR_ERR(hdr);
- NLA_PUT_U32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id);
- NLA_PUT_U32(skb, L2TP_ATTR_SESSION_ID, session->session_id);
- NLA_PUT_U32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id);
- NLA_PUT_U32(skb, L2TP_ATTR_PEER_SESSION_ID, session->peer_session_id);
- NLA_PUT_U32(skb, L2TP_ATTR_DEBUG, session->debug);
- NLA_PUT_U16(skb, L2TP_ATTR_PW_TYPE, session->pwtype);
- NLA_PUT_U16(skb, L2TP_ATTR_MTU, session->mtu);
- if (session->mru)
- NLA_PUT_U16(skb, L2TP_ATTR_MRU, session->mru);
- if (session->ifname && session->ifname[0])
- NLA_PUT_STRING(skb, L2TP_ATTR_IFNAME, session->ifname);
- if (session->offset)
- NLA_PUT_U16(skb, L2TP_ATTR_OFFSET, session->offset);
- if (session->cookie_len)
- NLA_PUT(skb, L2TP_ATTR_COOKIE, session->cookie_len, &session->cookie[0]);
- if (session->peer_cookie_len)
- NLA_PUT(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len, &session->peer_cookie[0]);
- NLA_PUT_U8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq);
- NLA_PUT_U8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq);
- NLA_PUT_U8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode);
- #ifdef CONFIG_XFRM
- if ((sk) && (sk->sk_policy[0] || sk->sk_policy[1]))
- NLA_PUT_U8(skb, L2TP_ATTR_USING_IPSEC, 1);
- #endif
- if (session->reorder_timeout)
- NLA_PUT_MSECS(skb, L2TP_ATTR_RECV_TIMEOUT, session->reorder_timeout);
- nest = nla_nest_start(skb, L2TP_ATTR_STATS);
- if (nest == NULL)
- goto nla_put_failure;
- NLA_PUT_U64(skb, L2TP_ATTR_TX_PACKETS, session->stats.tx_packets);
- NLA_PUT_U64(skb, L2TP_ATTR_TX_BYTES, session->stats.tx_bytes);
- NLA_PUT_U64(skb, L2TP_ATTR_TX_ERRORS, session->stats.tx_errors);
- NLA_PUT_U64(skb, L2TP_ATTR_RX_PACKETS, session->stats.rx_packets);
- NLA_PUT_U64(skb, L2TP_ATTR_RX_BYTES, session->stats.rx_bytes);
- NLA_PUT_U64(skb, L2TP_ATTR_RX_SEQ_DISCARDS, session->stats.rx_seq_discards);
- NLA_PUT_U64(skb, L2TP_ATTR_RX_OOS_PACKETS, session->stats.rx_oos_packets);
- NLA_PUT_U64(skb, L2TP_ATTR_RX_ERRORS, session->stats.rx_errors);
- nla_nest_end(skb, nest);
- return genlmsg_end(skb, hdr);
- nla_put_failure:
- genlmsg_cancel(skb, hdr);
- return -1;
- }
- static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
- {
- struct l2tp_session *session;
- struct sk_buff *msg;
- int ret;
- session = l2tp_nl_session_get(info, false);
- if (session == NULL) {
- ret = -ENODEV;
- goto err;
- }
- msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
- if (!msg) {
- ret = -ENOMEM;
- goto err_ref;
- }
- ret = l2tp_nl_session_send(msg, info->snd_pid, info->snd_seq,
- 0, session);
- if (ret < 0)
- goto err_ref_msg;
- ret = genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
- l2tp_session_dec_refcount(session);
- return ret;
- err_ref_msg:
- nlmsg_free(msg);
- err_ref:
- l2tp_session_dec_refcount(session);
- err:
- return ret;
- }
- static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
- {
- struct net *net = sock_net(skb->sk);
- struct l2tp_session *session;
- struct l2tp_tunnel *tunnel = NULL;
- int ti = cb->args[0];
- int si = cb->args[1];
- for (;;) {
- if (tunnel == NULL) {
- tunnel = l2tp_tunnel_find_nth(net, ti);
- if (tunnel == NULL)
- goto out;
- }
- session = l2tp_session_get_nth(tunnel, si, false);
- if (session == NULL) {
- ti++;
- tunnel = NULL;
- si = 0;
- continue;
- }
- if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).pid,
- cb->nlh->nlmsg_seq, NLM_F_MULTI,
- session) <= 0) {
- l2tp_session_dec_refcount(session);
- break;
- }
- l2tp_session_dec_refcount(session);
- si++;
- }
- out:
- cb->args[0] = ti;
- cb->args[1] = si;
- return skb->len;
- }
- static struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
- [L2TP_ATTR_NONE] = { .type = NLA_UNSPEC, },
- [L2TP_ATTR_PW_TYPE] = { .type = NLA_U16, },
- [L2TP_ATTR_ENCAP_TYPE] = { .type = NLA_U16, },
- [L2TP_ATTR_OFFSET] = { .type = NLA_U16, },
- [L2TP_ATTR_DATA_SEQ] = { .type = NLA_U8, },
- [L2TP_ATTR_L2SPEC_TYPE] = { .type = NLA_U8, },
- [L2TP_ATTR_L2SPEC_LEN] = { .type = NLA_U8, },
- [L2TP_ATTR_PROTO_VERSION] = { .type = NLA_U8, },
- [L2TP_ATTR_CONN_ID] = { .type = NLA_U32, },
- [L2TP_ATTR_PEER_CONN_ID] = { .type = NLA_U32, },
- [L2TP_ATTR_SESSION_ID] = { .type = NLA_U32, },
- [L2TP_ATTR_PEER_SESSION_ID] = { .type = NLA_U32, },
- [L2TP_ATTR_UDP_CSUM] = { .type = NLA_U8, },
- [L2TP_ATTR_VLAN_ID] = { .type = NLA_U16, },
- [L2TP_ATTR_DEBUG] = { .type = NLA_U32, },
- [L2TP_ATTR_RECV_SEQ] = { .type = NLA_U8, },
- [L2TP_ATTR_SEND_SEQ] = { .type = NLA_U8, },
- [L2TP_ATTR_LNS_MODE] = { .type = NLA_U8, },
- [L2TP_ATTR_USING_IPSEC] = { .type = NLA_U8, },
- [L2TP_ATTR_RECV_TIMEOUT] = { .type = NLA_MSECS, },
- [L2TP_ATTR_FD] = { .type = NLA_U32, },
- [L2TP_ATTR_IP_SADDR] = { .type = NLA_U32, },
- [L2TP_ATTR_IP_DADDR] = { .type = NLA_U32, },
- [L2TP_ATTR_UDP_SPORT] = { .type = NLA_U16, },
- [L2TP_ATTR_UDP_DPORT] = { .type = NLA_U16, },
- [L2TP_ATTR_MTU] = { .type = NLA_U16, },
- [L2TP_ATTR_MRU] = { .type = NLA_U16, },
- [L2TP_ATTR_STATS] = { .type = NLA_NESTED, },
- [L2TP_ATTR_IFNAME] = {
- .type = NLA_NUL_STRING,
- .len = IFNAMSIZ - 1,
- },
- [L2TP_ATTR_COOKIE] = {
- .type = NLA_BINARY,
- .len = 8,
- },
- [L2TP_ATTR_PEER_COOKIE] = {
- .type = NLA_BINARY,
- .len = 8,
- },
- };
- static struct genl_ops l2tp_nl_ops[] = {
- {
- .cmd = L2TP_CMD_NOOP,
- .doit = l2tp_nl_cmd_noop,
- .policy = l2tp_nl_policy,
- /* can be retrieved by unprivileged users */
- },
- {
- .cmd = L2TP_CMD_TUNNEL_CREATE,
- .doit = l2tp_nl_cmd_tunnel_create,
- .policy = l2tp_nl_policy,
- .flags = GENL_ADMIN_PERM,
- },
- {
- .cmd = L2TP_CMD_TUNNEL_DELETE,
- .doit = l2tp_nl_cmd_tunnel_delete,
- .policy = l2tp_nl_policy,
- .flags = GENL_ADMIN_PERM,
- },
- {
- .cmd = L2TP_CMD_TUNNEL_MODIFY,
- .doit = l2tp_nl_cmd_tunnel_modify,
- .policy = l2tp_nl_policy,
- .flags = GENL_ADMIN_PERM,
- },
- {
- .cmd = L2TP_CMD_TUNNEL_GET,
- .doit = l2tp_nl_cmd_tunnel_get,
- .dumpit = l2tp_nl_cmd_tunnel_dump,
- .policy = l2tp_nl_policy,
- .flags = GENL_ADMIN_PERM,
- },
- {
- .cmd = L2TP_CMD_SESSION_CREATE,
- .doit = l2tp_nl_cmd_session_create,
- .policy = l2tp_nl_policy,
- .flags = GENL_ADMIN_PERM,
- },
- {
- .cmd = L2TP_CMD_SESSION_DELETE,
- .doit = l2tp_nl_cmd_session_delete,
- .policy = l2tp_nl_policy,
- .flags = GENL_ADMIN_PERM,
- },
- {
- .cmd = L2TP_CMD_SESSION_MODIFY,
- .doit = l2tp_nl_cmd_session_modify,
- .policy = l2tp_nl_policy,
- .flags = GENL_ADMIN_PERM,
- },
- {
- .cmd = L2TP_CMD_SESSION_GET,
- .doit = l2tp_nl_cmd_session_get,
- .dumpit = l2tp_nl_cmd_session_dump,
- .policy = l2tp_nl_policy,
- .flags = GENL_ADMIN_PERM,
- },
- };
- int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
- {
- int ret;
- ret = -EINVAL;
- if (pw_type >= __L2TP_PWTYPE_MAX)
- goto err;
- genl_lock();
- ret = -EBUSY;
- if (l2tp_nl_cmd_ops[pw_type])
- goto out;
- l2tp_nl_cmd_ops[pw_type] = ops;
- ret = 0;
- out:
- genl_unlock();
- err:
- return ret;
- }
- EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
- void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
- {
- if (pw_type < __L2TP_PWTYPE_MAX) {
- genl_lock();
- l2tp_nl_cmd_ops[pw_type] = NULL;
- genl_unlock();
- }
- }
- EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
- static int l2tp_nl_init(void)
- {
- int err;
- printk(KERN_INFO "L2TP netlink interface\n");
- err = genl_register_family_with_ops(&l2tp_nl_family, l2tp_nl_ops,
- ARRAY_SIZE(l2tp_nl_ops));
- return err;
- }
- static void l2tp_nl_cleanup(void)
- {
- genl_unregister_family(&l2tp_nl_family);
- }
- module_init(l2tp_nl_init);
- module_exit(l2tp_nl_cleanup);
- MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
- MODULE_DESCRIPTION("L2TP netlink");
- MODULE_LICENSE("GPL");
- MODULE_VERSION("1.0");
- MODULE_ALIAS("net-pf-" __stringify(PF_NETLINK) "-proto-" \
- __stringify(NETLINK_GENERIC) "-type-" "l2tp");
|