28 #if defined(ENABLE_DCO) && defined(TARGET_LINUX)
47 #include <netlink/socket.h>
48 #include <netlink/netlink.h>
49 #include <netlink/genl/genl.h>
50 #include <netlink/genl/family.h>
51 #include <netlink/genl/ctrl.h>
58 #define nla_nest_start(_msg, _type) \
59 nla_nest_start(_msg, (_type) | NLA_F_NESTED)
65 typedef int (*ovpn_nl_cb)(
struct nl_msg *
msg,
void *arg);
79 resolve_ovpn_netlink_id(
int msglevel)
82 struct nl_sock *nl_sock = nl_socket_alloc();
86 msg(msglevel,
"Allocating net link socket failed");
90 ret = genl_connect(nl_sock);
93 msg(msglevel,
"Cannot connect to generic netlink: %s",
102 msg(msglevel,
"Cannot find ovpn_dco netlink component: %s",
107 nl_socket_free(nl_sock);
111 static struct nl_msg *
114 struct nl_msg *nl_msg = nlmsg_alloc();
117 msg(
M_ERR,
"cannot allocate netlink message");
121 genlmsg_put(nl_msg, 0, 0, dco->ovpn_dco_id, 0, 0, cmd, 0);
127 msg(
M_INFO,
"cannot put into netlink message");
134 int ret = nl_recvmsgs(dco->nl_sock, dco->nl_cb);
139 msg(
M_WARN,
"%s: netlink received interrupt due to signal - ignoring", prefix);
143 msg(
M_ERR,
"%s: netlink out of memory error", prefix);
147 msg(
M_WARN,
"%s: netlink reports blocking read - aborting wait", prefix);
151 msg(
M_ERR,
"%s: netlink reports device not found:", prefix);
154 case -NLE_OBJ_NOTFOUND:
155 msg(
M_INFO,
"%s: netlink reports object not found, ovpn-dco unloaded?", prefix);
161 msg(
M_NONFATAL,
"%s: netlink reports error (%d): %s", prefix, ret, nl_geterror(-ret));
181 ovpn_nl_msg_send(
dco_context_t *dco,
struct nl_msg *nl_msg, ovpn_nl_cb cb,
182 void *cb_arg,
const char *prefix)
186 nl_cb_set(dco->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, cb, cb_arg);
187 nl_send_auto(dco->nl_sock, nl_msg);
189 while (dco->status == 1)
191 ovpn_nl_recvmsgs(dco, prefix);
196 msg(
M_INFO,
"%s: failed to send netlink message: %s (%d)",
197 prefix, strerror(-dco->status), dco->status);
204 mapped_v4_to_v6(
struct sockaddr *sock,
struct gc_arena *gc)
206 struct sockaddr_in6 *sock6 = (
struct sockaddr_in6 *)sock;
207 if (sock->sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sock6->sin6_addr))
210 struct sockaddr_in *sock4;
212 memcpy(&sock4->sin_addr, sock6->sin6_addr.s6_addr + 12, 4);
213 sock4->sin_port = sock6->sin6_port;
214 sock4->sin_family = AF_INET;
215 return (
struct sockaddr *)sock4;
222 struct sockaddr *localaddr,
struct sockaddr *remoteaddr,
223 struct in_addr *remote_in4,
struct in6_addr *remote_in6)
226 const char *remotestr =
"[undefined]";
231 msg(
D_DCO_DEBUG,
"%s: peer-id %d, fd %d, remote addr: %s", __func__,
232 peerid, sd, remotestr);
244 remoteaddr = mapped_v4_to_v6(remoteaddr, &gc);
252 localaddr = mapped_v4_to_v6(localaddr, &gc);
253 if (localaddr->sa_family == AF_INET)
256 &((
struct sockaddr_in *)localaddr)->sin_addr);
258 else if (localaddr->sa_family == AF_INET6)
261 &((
struct sockaddr_in6 *)localaddr)->sin6_addr);
275 nla_nest_end(nl_msg, attr);
277 ret = ovpn_nl_msg_send(dco, nl_msg, NULL, NULL, __func__);
303 ovpn_nl_cb_error(
struct sockaddr_nl (*nla)
__attribute__ ((unused)),
304 struct nlmsgerr *err,
void *arg)
306 struct nlmsghdr *nlh = (
struct nlmsghdr *)err - 1;
307 struct nlattr *tb_msg[NLMSGERR_ATTR_MAX + 1];
308 int len = nlh->nlmsg_len;
309 struct nlattr *attrs;
311 int ack_len =
sizeof(*nlh) +
sizeof(int) +
sizeof(*nlh);
315 if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
320 if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
322 ack_len += err->msg.nlmsg_len -
sizeof(*nlh);
330 attrs = (
void *)((
unsigned char *)nlh + ack_len);
333 nla_parse(tb_msg, NLMSGERR_ATTR_MAX, attrs, len, NULL);
334 if (tb_msg[NLMSGERR_ATTR_MSG])
336 len = strnlen((
char *)nla_data(tb_msg[NLMSGERR_ATTR_MSG]),
337 nla_len(tb_msg[NLMSGERR_ATTR_MSG]));
339 (
char *)nla_data(tb_msg[NLMSGERR_ATTR_MSG]));
348 dco->ovpn_dco_id = resolve_ovpn_netlink_id(
M_ERR);
350 dco->nl_sock = nl_socket_alloc();
354 msg(
M_ERR,
"Cannot create netlink socket");
357 int ret = genl_connect(dco->nl_sock);
360 msg(
M_ERR,
"Cannot connect to generic netlink: %s",
368 dco->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
371 msg(
M_ERR,
"failed to allocate netlink callback");
374 nl_socket_set_cb(dco->nl_sock, dco->nl_cb);
376 nl_cb_err(dco->nl_cb, NL_CB_CUSTOM, ovpn_nl_cb_error, &dco->status);
377 nl_cb_set(dco->nl_cb, NL_CB_FINISH, NL_CB_CUSTOM, ovpn_nl_cb_finish,
379 nl_cb_set(dco->nl_cb, NL_CB_ACK, NL_CB_CUSTOM, ovpn_nl_cb_finish,
385 nl_socket_disable_seq_check(dco->nl_sock);
390 ASSERT(!nl_socket_set_buffer_size(dco->nl_sock, 1024*1024, 1024*1024));
410 ovpn_dco_init_netlink(dco);
417 nl_socket_free(dco->nl_sock);
421 nl_cb_put(dco->nl_cb);
430 ovpn_get_mcast_id(dco);
432 if (dco->ovpn_dco_mcast_id < 0)
434 msg(
M_ERR,
"cannot get mcast group: %s", nl_geterror(dco->ovpn_dco_mcast_id));
440 int ret = nl_socket_add_membership(dco->nl_sock, dco->ovpn_dco_mcast_id);
443 msg(
M_ERR,
"%s: failed to join groups: %d", __func__, ret);
453 int ret = net_iface_new(ctx, dev,
"ovpn-dco", &tt->
dco);
460 tt->
dco.ifindex = if_nametoindex(dev);
461 if (!tt->
dco.ifindex)
463 msg(
M_FATAL,
"DCO: cannot retrieve ifindex for interface %s", dev);
466 tt->
dco.dco_message_peer_id = -1;
468 ovpn_dco_register(&tt->
dco);
479 ovpn_dco_uninit_netlink(&tt->
dco);
496 nla_nest_end(nl_msg, attr);
498 ret = ovpn_nl_msg_send(dco, nl_msg, NULL, NULL, __func__);
520 nla_nest_end(nl_msg, attr);
522 ret = ovpn_nl_msg_send(dco, nl_msg, NULL, NULL, __func__);
534 msg(
D_DCO_DEBUG,
"%s: peer-id %d, slot %d", __func__, peerid, slot);
546 nla_nest_end(nl_msg, attr);
548 ret = ovpn_nl_msg_send(dco, nl_msg, NULL, NULL, __func__);
558 const uint8_t *encrypt_key,
const uint8_t *encrypt_iv,
559 const uint8_t *decrypt_key,
const uint8_t *decrypt_iv,
560 const char *ciphername)
563 __func__, slot, keyid, peerid, ciphername);
566 const int nonce_tail_len = 8;
574 dco_cipher_t dco_cipher = dco_get_cipher(ciphername);
583 struct nlattr *key_enc = nla_nest_start(nl_msg,
591 nla_nest_end(nl_msg, key_enc);
593 struct nlattr *key_dec = nla_nest_start(nl_msg,
601 nla_nest_end(nl_msg, key_dec);
603 nla_nest_end(nl_msg, attr);
605 ret = ovpn_nl_msg_send(dco, nl_msg, NULL, NULL, __func__);
614 int keepalive_interval,
int keepalive_timeout,
int mss)
616 msg(
D_DCO_DEBUG,
"%s: peer-id %d, keepalive %d/%d, mss %d", __func__,
617 peerid, keepalive_interval, keepalive_timeout, mss);
632 nla_nest_end(nl_msg, attr);
634 ret = ovpn_nl_msg_send(dco, nl_msg, NULL, NULL, __func__);
649 mcast_family_handler(
struct nl_msg *
msg,
void *arg)
652 struct nlattr *tb[CTRL_ATTR_MAX + 1];
653 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(
msg));
655 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
656 genlmsg_attrlen(gnlh, 0), NULL);
658 if (!tb[CTRL_ATTR_MCAST_GROUPS])
663 struct nlattr *mcgrp;
665 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], rem_mcgrp)
667 struct nlattr *tb_mcgrp[CTRL_ATTR_MCAST_GRP_MAX + 1];
669 nla_parse(tb_mcgrp, CTRL_ATTR_MCAST_GRP_MAX,
670 nla_data(mcgrp), nla_len(mcgrp), NULL);
672 if (!tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME]
673 || !tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID])
678 if (strncmp(nla_data(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME]),
680 nla_len(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
684 dco->ovpn_dco_mcast_id = nla_get_u32(tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID]);
698 dco->ovpn_dco_mcast_id = -ENOENT;
702 int ctrlid = genl_ctrl_resolve(dco->nl_sock,
"nlctrl");
704 struct nl_msg *nl_msg = nlmsg_alloc();
710 genlmsg_put(nl_msg, 0, 0, ctrlid, 0, 0, CTRL_CMD_GETFAMILY, 0);
713 NLA_PUT_STRING(nl_msg, CTRL_ATTR_FAMILY_NAME,
OVPN_NL_NAME);
715 ret = ovpn_nl_msg_send(dco, nl_msg, mcast_family_handler, dco, __func__);
724 ovpn_handle_msg(
struct nl_msg *
msg,
void *arg)
728 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(
msg));
730 struct nlmsghdr *nlh = nlmsg_hdr(
msg);
732 if (!genlmsg_valid_hdr(nlh, 0))
734 msg(
D_DCO,
"ovpn-dco: invalid header");
738 if (nla_parse(attrs,
OVPN_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
739 genlmsg_attrlen(gnlh, 0), NULL))
741 msg(
D_DCO,
"received bogus data from ovpn-dco");
750 msg(
D_DCO,
"ovpn-dco: Received message without ifindex");
755 if (ifindex != dco->ifindex)
758 "ovpn-dco: ignoring message (type=%d) for foreign ifindex %d",
776 msg(
D_DCO,
"ovpn-dco: no attributes in OVPN_DEL_PEER message");
784 msg(
D_DCO,
"received bogus del peer packet data from ovpn-dco");
790 msg(
D_DCO,
"ovpn-dco: no reason in DEL_PEER message");
795 msg(
D_DCO,
"ovpn-dco: no peer-id in DEL_PEER message");
801 msg(
D_DCO_DEBUG,
"ovpn-dco: received CMD_DEL_PEER, ifindex: %d, peer-id %d, reason: %d",
802 ifindex, peerid, reason);
803 dco->dco_message_peer_id = peerid;
804 dco->dco_del_peer_reason = reason;
811 msg(
D_DCO,
"ovpn-dco: received unknown command: %d", gnlh->cmd);
812 dco->dco_message_type = 0;
823 nl_cb_set(dco->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, ovpn_handle_msg, dco);
825 return ovpn_nl_recvmsgs(dco, __func__);
829 dco_update_peer_stat(
struct context_2 *c2,
struct nlattr *tb[], uint32_t
id)
839 msg(
M_WARN,
"%s: no link RX bytes provided in reply for peer %u",
851 msg(
M_WARN,
"%s: no link TX bytes provided in reply for peer %u",
863 msg(
M_WARN,
"%s: no VPN RX bytes provided in reply for peer %u",
875 msg(
M_WARN,
"%s: no VPN TX bytes provided in reply for peer %u",
881 dco_parse_peer_multi(
struct nl_msg *
msg,
void *arg)
884 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(
msg));
889 genlmsg_attrlen(gnlh, 0), NULL);
904 msg(
M_WARN,
"%s: no peer-id provided in reply", __func__);
913 msg(
M_WARN,
"%s: cannot store DCO stats for peer %u", __func__,
930 nlmsg_hdr(nl_msg)->nlmsg_flags |= NLM_F_DUMP;
932 int ret = ovpn_nl_msg_send(dco, nl_msg, dco_parse_peer_multi, m, __func__);
939 dco_parse_peer(
struct nl_msg *
msg,
void *arg)
943 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(
msg));
948 genlmsg_attrlen(gnlh, 0), NULL);
964 msg(
M_WARN,
"%s: no peer-id provided in reply", __func__);
974 dco_update_peer_stat(&c->
c2, tb_peer, peer_id);
996 nla_nest_end(nl_msg, attr);
998 ret = ovpn_nl_msg_send(dco, nl_msg, dco_parse_peer, c, __func__);
1011 "Note: Kernel support for ovpn-dco missing, disabling data channel offload.");
1022 FILE *fp = fopen(
"/sys/module/ovpn_dco_v2/version",
"r");
1028 if (!fgets(
BSTR(&out),
BCAP(&out), fp))
1035 char *str =
BSTR(&out);
1036 char *nl = strchr(str,
'\n');
1049 if (dco && dco->nl_sock)
1058 return "AES-128-GCM:AES-256-GCM:AES-192-GCM:CHACHA20-POLY1305";