| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include "main.h" |
| 11 |
|
| 12 |
#include "ovpn.h" |
| 13 |
#include "ovpnstruct.h" |
| 14 |
#include "netlink.h" |
| 15 |
#include "tcp.h" |
| 16 |
|
| 17 |
#include <linux/ethtool.h> |
| 18 |
#include <linux/genetlink.h> |
| 19 |
#include <linux/inetdevice.h> |
| 20 |
#include <linux/module.h> |
| 21 |
#include <linux/moduleparam.h> |
| 22 |
#include <linux/types.h> |
| 23 |
#include <linux/lockdep.h> |
| 24 |
#include <linux/rcupdate.h> |
| 25 |
#include <linux/net.h> |
| 26 |
#include <linux/netdevice.h> |
| 27 |
#include <linux/skbuff.h> |
| 28 |
#include <linux/version.h> |
| 29 |
|
| 30 |
#include <net/genetlink.h> |
| 31 |
#include <net/ip_tunnels.h> |
| 32 |
|
| 33 |
|
| 34 |
#define DRV_NAME "ovpn-dco" |
| 35 |
#define DRV_VERSION OVPN_DCO_VERSION |
| 36 |
#define DRV_DESCRIPTION "OpenVPN data channel offload (ovpn-dco)" |
| 37 |
#define DRV_COPYRIGHT "(C) 2020- OpenVPN, Inc." |
| 38 |
|
| 39 |
static void ovpn_struct_free(struct net_device *net) |
| 40 |
{ |
| 41 |
struct ovpn_struct *ovpn = netdev_priv(net); |
| 42 |
|
| 43 |
security_tun_dev_free_security(ovpn->security); |
| 44 |
free_percpu(net->tstats); |
| 45 |
flush_workqueue(ovpn->crypto_wq); |
| 46 |
flush_workqueue(ovpn->events_wq); |
| 47 |
destroy_workqueue(ovpn->crypto_wq); |
| 48 |
destroy_workqueue(ovpn->events_wq); |
| 49 |
rcu_barrier(); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
static int ovpn_net_open(struct net_device *dev) |
| 54 |
{ |
| 55 |
struct in_device *dev_v4 = __in_dev_get_rtnl(dev); |
| 56 |
|
| 57 |
if (dev_v4) { |
| 58 |
|
| 59 |
IN_DEV_CONF_SET(dev_v4, SEND_REDIRECTS, false); |
| 60 |
IPV4_DEVCONF_ALL(dev_net(dev), SEND_REDIRECTS) = false; |
| 61 |
} |
| 62 |
|
| 63 |
netif_tx_start_all_queues(dev); |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
static int ovpn_net_stop(struct net_device *dev) |
| 69 |
{ |
| 70 |
netif_tx_stop_all_queues(dev); |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
static int ovpn_get_link_ksettings(struct net_device *dev, |
| 79 |
struct ethtool_link_ksettings *cmd) |
| 80 |
{ |
| 81 |
ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported, 0); |
| 82 |
ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising, 0); |
| 83 |
cmd->base.speed = SPEED_1000; |
| 84 |
cmd->base.duplex = DUPLEX_FULL; |
| 85 |
cmd->base.port = PORT_TP; |
| 86 |
cmd->base.phy_address = 0; |
| 87 |
cmd->base.transceiver = XCVR_INTERNAL; |
| 88 |
cmd->base.autoneg = AUTONEG_DISABLE; |
| 89 |
|
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
static void ovpn_get_drvinfo(struct net_device *dev, |
| 94 |
struct ethtool_drvinfo *info) |
| 95 |
{ |
| 96 |
strscpy(info->driver, DRV_NAME, sizeof(info->driver)); |
| 97 |
strscpy(info->version, DRV_VERSION, sizeof(info->version)); |
| 98 |
strscpy(info->bus_info, "ovpn", sizeof(info->bus_info)); |
| 99 |
} |
| 100 |
|
| 101 |
bool ovpn_dev_is_valid(const struct net_device *dev) |
| 102 |
{ |
| 103 |
return dev->netdev_ops->ndo_start_xmit == ovpn_net_xmit; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
static const struct net_device_ops ovpn_netdev_ops = { |
| 111 |
.ndo_open = ovpn_net_open, |
| 112 |
.ndo_stop = ovpn_net_stop, |
| 113 |
.ndo_start_xmit = ovpn_net_xmit, |
| 114 |
.ndo_get_stats64 = dev_get_tstats64, |
| 115 |
}; |
| 116 |
|
| 117 |
static const struct ethtool_ops ovpn_ethtool_ops = { |
| 118 |
.get_link_ksettings = ovpn_get_link_ksettings, |
| 119 |
.get_drvinfo = ovpn_get_drvinfo, |
| 120 |
.get_link = ethtool_op_get_link, |
| 121 |
.get_ts_info = ethtool_op_get_ts_info, |
| 122 |
}; |
| 123 |
|
| 124 |
static void ovpn_setup(struct net_device *dev) |
| 125 |
{ |
| 126 |
|
| 127 |
const int overhead = sizeof(u32) + NONCE_WIRE_SIZE + 16 + sizeof(struct udphdr) + |
| 128 |
max(sizeof(struct ipv6hdr), sizeof(struct iphdr)); |
| 129 |
|
| 130 |
netdev_features_t feat = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM | |
| 131 |
NETIF_F_GSO | NETIF_F_GSO_SOFTWARE | |
| 132 |
NETIF_F_HIGHDMA; |
| 133 |
|
| 134 |
dev->ethtool_ops = &ovpn_ethtool_ops; |
| 135 |
dev->needs_free_netdev = true; |
| 136 |
|
| 137 |
dev->netdev_ops = &ovpn_netdev_ops; |
| 138 |
|
| 139 |
dev->priv_destructor = ovpn_struct_free; |
| 140 |
|
| 141 |
|
| 142 |
dev->hard_header_len = 0; |
| 143 |
dev->addr_len = 0; |
| 144 |
dev->mtu = ETH_DATA_LEN - overhead; |
| 145 |
dev->min_mtu = IPV4_MIN_MTU; |
| 146 |
dev->max_mtu = IP_MAX_MTU - overhead; |
| 147 |
|
| 148 |
|
| 149 |
dev->type = ARPHRD_NONE; |
| 150 |
dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; |
| 151 |
dev->lltx = true; |
| 152 |
|
| 153 |
dev->features |= feat; |
| 154 |
dev->hw_features |= feat; |
| 155 |
dev->hw_enc_features |= feat; |
| 156 |
|
| 157 |
dev->needed_headroom = OVPN_HEAD_ROOM; |
| 158 |
dev->needed_tailroom = OVPN_MAX_PADDING; |
| 159 |
} |
| 160 |
|
| 161 |
static const struct nla_policy ovpn_policy[IFLA_OVPN_MAX + 1] = { |
| 162 |
[IFLA_OVPN_MODE] = NLA_POLICY_RANGE(NLA_U8, __OVPN_MODE_FIRST, |
| 163 |
__OVPN_MODE_AFTER_LAST - 1), |
| 164 |
}; |
| 165 |
|
| 166 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 15, 0) |
| 167 |
static int ovpn_newlink(struct net_device *dev, |
| 168 |
struct rtnl_newlink_params *params, |
| 169 |
struct netlink_ext_ack *extack) |
| 170 |
{ |
| 171 |
struct nlattr **data = params->data; |
| 172 |
#else |
| 173 |
static int ovpn_newlink(struct net *src_net, struct net_device *dev, |
| 174 |
struct nlattr *tb[], struct nlattr *data[], |
| 175 |
struct netlink_ext_ack *extack) |
| 176 |
{ |
| 177 |
#endif |
| 178 |
struct ovpn_struct *ovpn = netdev_priv(dev); |
| 179 |
int ret; |
| 180 |
|
| 181 |
ret = security_tun_dev_create(); |
| 182 |
if (ret < 0) |
| 183 |
return ret; |
| 184 |
|
| 185 |
ret = ovpn_struct_init(dev); |
| 186 |
if (ret < 0) |
| 187 |
return ret; |
| 188 |
|
| 189 |
ovpn->mode = OVPN_MODE_P2P; |
| 190 |
if (data && data[IFLA_OVPN_MODE]) { |
| 191 |
ovpn->mode = nla_get_u8(data[IFLA_OVPN_MODE]); |
| 192 |
netdev_dbg(dev, "%s: setting device (%s) mode: %u\n", __func__, dev->name, |
| 193 |
ovpn->mode); |
| 194 |
} |
| 195 |
|
| 196 |
return register_netdevice(dev); |
| 197 |
} |
| 198 |
|
| 199 |
static void ovpn_dellink(struct net_device *dev, struct list_head *head) |
| 200 |
{ |
| 201 |
struct ovpn_struct *ovpn = netdev_priv(dev); |
| 202 |
|
| 203 |
switch (ovpn->mode) { |
| 204 |
case OVPN_MODE_P2P: |
| 205 |
ovpn_peer_release_p2p(ovpn); |
| 206 |
break; |
| 207 |
default: |
| 208 |
ovpn_peers_free(ovpn); |
| 209 |
break; |
| 210 |
} |
| 211 |
|
| 212 |
unregister_netdevice_queue(dev, head); |
| 213 |
} |
| 214 |
|
| 215 |
static struct rtnl_link_ops ovpn_link_ops __read_mostly = { |
| 216 |
.kind = DRV_NAME, |
| 217 |
.priv_size = sizeof(struct ovpn_struct), |
| 218 |
.setup = ovpn_setup, |
| 219 |
.policy = ovpn_policy, |
| 220 |
.maxtype = IFLA_OVPN_MAX, |
| 221 |
.newlink = ovpn_newlink, |
| 222 |
.dellink = ovpn_dellink, |
| 223 |
}; |
| 224 |
|
| 225 |
static int __init ovpn_init(void) |
| 226 |
{ |
| 227 |
int err = 0; |
| 228 |
|
| 229 |
pr_info("%s %s -- %s\n", DRV_DESCRIPTION, DRV_VERSION, DRV_COPYRIGHT); |
| 230 |
|
| 231 |
err = ovpn_tcp_init(); |
| 232 |
if (err) { |
| 233 |
pr_err("ovpn: can't initialize TCP subsystem\n"); |
| 234 |
goto err; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
err = rtnl_link_register(&ovpn_link_ops); |
| 239 |
if (err) { |
| 240 |
pr_err("ovpn: can't register RTNL link ops\n"); |
| 241 |
goto err; |
| 242 |
} |
| 243 |
|
| 244 |
err = ovpn_netlink_register(); |
| 245 |
if (err) { |
| 246 |
pr_err("ovpn: can't register netlink family\n"); |
| 247 |
goto err_rtnl_unregister; |
| 248 |
} |
| 249 |
|
| 250 |
return 0; |
| 251 |
|
| 252 |
err_rtnl_unregister: |
| 253 |
rtnl_link_unregister(&ovpn_link_ops); |
| 254 |
err: |
| 255 |
pr_err("ovpn: initialization failed, error status=%d\n", err); |
| 256 |
return err; |
| 257 |
} |
| 258 |
|
| 259 |
static __exit void ovpn_cleanup(void) |
| 260 |
{ |
| 261 |
rtnl_link_unregister(&ovpn_link_ops); |
| 262 |
ovpn_netlink_unregister(); |
| 263 |
rcu_barrier(); |
| 264 |
} |
| 265 |
|
| 266 |
module_init(ovpn_init); |
| 267 |
module_exit(ovpn_cleanup); |
| 268 |
|
| 269 |
MODULE_DESCRIPTION(DRV_DESCRIPTION); |
| 270 |
MODULE_AUTHOR(DRV_COPYRIGHT); |
| 271 |
MODULE_LICENSE("GPL"); |
| 272 |
MODULE_VERSION(DRV_VERSION); |
| 273 |
MODULE_ALIAS_RTNL_LINK(DRV_NAME); |
| 274 |
MODULE_ALIAS_GENL_FAMILY(OVPN_NL_NAME); |