James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * L2TPv3 ethernet pseudowire driver |
| 3 | * |
| 4 | * Copyright (c) 2008,2009,2010 Katalix Systems Ltd |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/skbuff.h> |
| 16 | #include <linux/socket.h> |
| 17 | #include <linux/hash.h> |
| 18 | #include <linux/l2tp.h> |
| 19 | #include <linux/in.h> |
| 20 | #include <linux/etherdevice.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | #include <net/sock.h> |
| 23 | #include <net/ip.h> |
| 24 | #include <net/icmp.h> |
| 25 | #include <net/udp.h> |
| 26 | #include <net/inet_common.h> |
| 27 | #include <net/inet_hashtables.h> |
| 28 | #include <net/tcp_states.h> |
| 29 | #include <net/protocol.h> |
| 30 | #include <net/xfrm.h> |
| 31 | #include <net/net_namespace.h> |
| 32 | #include <net/netns/generic.h> |
| 33 | |
| 34 | #include "l2tp_core.h" |
| 35 | |
| 36 | /* Default device name. May be overridden by name specified by user */ |
| 37 | #define L2TP_ETH_DEV_NAME "l2tpeth%d" |
| 38 | |
| 39 | /* via netdev_priv() */ |
| 40 | struct l2tp_eth { |
| 41 | struct net_device *dev; |
| 42 | struct sock *tunnel_sock; |
| 43 | struct l2tp_session *session; |
| 44 | struct list_head list; |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 45 | atomic_long_t tx_bytes; |
| 46 | atomic_long_t tx_packets; |
Eric Dumazet | b8c8430 | 2012-06-28 20:15:13 +0000 | [diff] [blame] | 47 | atomic_long_t tx_dropped; |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 48 | atomic_long_t rx_bytes; |
| 49 | atomic_long_t rx_packets; |
| 50 | atomic_long_t rx_errors; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | /* via l2tp_session_priv() */ |
| 54 | struct l2tp_eth_sess { |
| 55 | struct net_device *dev; |
| 56 | }; |
| 57 | |
| 58 | /* per-net private data for this module */ |
| 59 | static unsigned int l2tp_eth_net_id; |
| 60 | struct l2tp_eth_net { |
| 61 | struct list_head l2tp_eth_dev_list; |
| 62 | spinlock_t l2tp_eth_lock; |
| 63 | }; |
| 64 | |
| 65 | static inline struct l2tp_eth_net *l2tp_eth_pernet(struct net *net) |
| 66 | { |
| 67 | return net_generic(net, l2tp_eth_net_id); |
| 68 | } |
| 69 | |
| 70 | static int l2tp_eth_dev_init(struct net_device *dev) |
| 71 | { |
| 72 | struct l2tp_eth *priv = netdev_priv(dev); |
| 73 | |
| 74 | priv->dev = dev; |
Danny Kukawka | f2cedb6 | 2012-02-15 06:45:39 +0000 | [diff] [blame] | 75 | eth_hw_addr_random(dev); |
Joe Perches | 1cea7e2 | 2015-03-02 19:54:59 -0800 | [diff] [blame] | 76 | eth_broadcast_addr(dev->broadcast); |
Eric Dumazet | d3fff6c | 2016-06-09 07:45:12 -0700 | [diff] [blame] | 77 | netdev_lockdep_set_classes(dev); |
Eric Dumazet | f9eb8ae | 2016-06-06 09:37:15 -0700 | [diff] [blame] | 78 | |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static void l2tp_eth_dev_uninit(struct net_device *dev) |
| 83 | { |
| 84 | struct l2tp_eth *priv = netdev_priv(dev); |
| 85 | struct l2tp_eth_net *pn = l2tp_eth_pernet(dev_net(dev)); |
| 86 | |
| 87 | spin_lock(&pn->l2tp_eth_lock); |
| 88 | list_del_init(&priv->list); |
| 89 | spin_unlock(&pn->l2tp_eth_lock); |
| 90 | dev_put(dev); |
| 91 | } |
| 92 | |
| 93 | static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev) |
| 94 | { |
| 95 | struct l2tp_eth *priv = netdev_priv(dev); |
| 96 | struct l2tp_session *session = priv->session; |
Eric Dumazet | b8c8430 | 2012-06-28 20:15:13 +0000 | [diff] [blame] | 97 | unsigned int len = skb->len; |
| 98 | int ret = l2tp_xmit_skb(session, skb, session->hdr_len); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 99 | |
WANG Cong | a4cd027 | 2016-11-21 23:24:43 -0800 | [diff] [blame] | 100 | if (likely(ret == NET_XMIT_SUCCESS)) { |
Eric Dumazet | b8c8430 | 2012-06-28 20:15:13 +0000 | [diff] [blame] | 101 | atomic_long_add(len, &priv->tx_bytes); |
| 102 | atomic_long_inc(&priv->tx_packets); |
| 103 | } else { |
| 104 | atomic_long_inc(&priv->tx_dropped); |
| 105 | } |
Eric Dumazet | aa214de0 | 2012-06-25 00:45:14 +0000 | [diff] [blame] | 106 | return NETDEV_TX_OK; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 109 | static struct rtnl_link_stats64 *l2tp_eth_get_stats64(struct net_device *dev, |
| 110 | struct rtnl_link_stats64 *stats) |
| 111 | { |
| 112 | struct l2tp_eth *priv = netdev_priv(dev); |
| 113 | |
| 114 | stats->tx_bytes = atomic_long_read(&priv->tx_bytes); |
| 115 | stats->tx_packets = atomic_long_read(&priv->tx_packets); |
Eric Dumazet | b8c8430 | 2012-06-28 20:15:13 +0000 | [diff] [blame] | 116 | stats->tx_dropped = atomic_long_read(&priv->tx_dropped); |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 117 | stats->rx_bytes = atomic_long_read(&priv->rx_bytes); |
| 118 | stats->rx_packets = atomic_long_read(&priv->rx_packets); |
| 119 | stats->rx_errors = atomic_long_read(&priv->rx_errors); |
| 120 | return stats; |
| 121 | } |
| 122 | |
| 123 | |
Julia Lawall | eb94737 | 2016-09-15 22:23:26 +0200 | [diff] [blame] | 124 | static const struct net_device_ops l2tp_eth_netdev_ops = { |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 125 | .ndo_init = l2tp_eth_dev_init, |
| 126 | .ndo_uninit = l2tp_eth_dev_uninit, |
| 127 | .ndo_start_xmit = l2tp_eth_dev_xmit, |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 128 | .ndo_get_stats64 = l2tp_eth_get_stats64, |
Alexander Couzens | fe15912 | 2014-11-19 13:24:39 +0100 | [diff] [blame] | 129 | .ndo_set_mac_address = eth_mac_addr, |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | static void l2tp_eth_dev_setup(struct net_device *dev) |
| 133 | { |
| 134 | ether_setup(dev); |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 135 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
| 136 | dev->features |= NETIF_F_LLTX; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 137 | dev->netdev_ops = &l2tp_eth_netdev_ops; |
| 138 | dev->destructor = free_netdev; |
| 139 | } |
| 140 | |
| 141 | static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len) |
| 142 | { |
| 143 | struct l2tp_eth_sess *spriv = l2tp_session_priv(session); |
| 144 | struct net_device *dev = spriv->dev; |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 145 | struct l2tp_eth *priv = netdev_priv(dev); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 146 | |
| 147 | if (session->debug & L2TP_MSG_DATA) { |
| 148 | unsigned int length; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 149 | |
| 150 | length = min(32u, skb->len); |
| 151 | if (!pskb_may_pull(skb, length)) |
| 152 | goto error; |
| 153 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 154 | pr_debug("%s: eth recv\n", session->name); |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 155 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Eric Dumazet | c0cc88a | 2012-09-04 15:54:55 -0400 | [diff] [blame] | 158 | if (!pskb_may_pull(skb, ETH_HLEN)) |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 159 | goto error; |
| 160 | |
| 161 | secpath_reset(skb); |
| 162 | |
| 163 | /* checksums verified by L2TP */ |
| 164 | skb->ip_summed = CHECKSUM_NONE; |
| 165 | |
| 166 | skb_dst_drop(skb); |
| 167 | nf_reset(skb); |
| 168 | |
| 169 | if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) { |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 170 | atomic_long_inc(&priv->rx_packets); |
| 171 | atomic_long_add(data_len, &priv->rx_bytes); |
| 172 | } else { |
| 173 | atomic_long_inc(&priv->rx_errors); |
| 174 | } |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 175 | return; |
| 176 | |
| 177 | error: |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 178 | atomic_long_inc(&priv->rx_errors); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 179 | kfree_skb(skb); |
| 180 | } |
| 181 | |
| 182 | static void l2tp_eth_delete(struct l2tp_session *session) |
| 183 | { |
| 184 | struct l2tp_eth_sess *spriv; |
| 185 | struct net_device *dev; |
| 186 | |
| 187 | if (session) { |
| 188 | spriv = l2tp_session_priv(session); |
| 189 | dev = spriv->dev; |
| 190 | if (dev) { |
| 191 | unregister_netdev(dev); |
| 192 | spriv->dev = NULL; |
Eric Dumazet | a06998b | 2012-06-07 00:07:20 +0000 | [diff] [blame] | 193 | module_put(THIS_MODULE); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
Javier Martinez Canillas | 9dd7994 | 2016-09-09 08:43:17 -0400 | [diff] [blame] | 198 | #if IS_ENABLED(CONFIG_L2TP_DEBUGFS) |
James Chapman | 0ad6614 | 2010-04-02 06:19:33 +0000 | [diff] [blame] | 199 | static void l2tp_eth_show(struct seq_file *m, void *arg) |
| 200 | { |
| 201 | struct l2tp_session *session = arg; |
| 202 | struct l2tp_eth_sess *spriv = l2tp_session_priv(session); |
| 203 | struct net_device *dev = spriv->dev; |
| 204 | |
| 205 | seq_printf(m, " interface %s\n", dev->name); |
| 206 | } |
| 207 | #endif |
| 208 | |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 209 | static int l2tp_eth_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg) |
| 210 | { |
| 211 | struct net_device *dev; |
| 212 | char name[IFNAMSIZ]; |
| 213 | struct l2tp_tunnel *tunnel; |
| 214 | struct l2tp_session *session; |
| 215 | struct l2tp_eth *priv; |
| 216 | struct l2tp_eth_sess *spriv; |
| 217 | int rc; |
| 218 | struct l2tp_eth_net *pn; |
| 219 | |
| 220 | tunnel = l2tp_tunnel_find(net, tunnel_id); |
| 221 | if (!tunnel) { |
| 222 | rc = -ENODEV; |
| 223 | goto out; |
| 224 | } |
| 225 | |
| 226 | session = l2tp_session_find(net, tunnel, session_id); |
| 227 | if (session) { |
| 228 | rc = -EEXIST; |
| 229 | goto out; |
| 230 | } |
| 231 | |
| 232 | if (cfg->ifname) { |
| 233 | dev = dev_get_by_name(net, cfg->ifname); |
| 234 | if (dev) { |
| 235 | dev_put(dev); |
| 236 | rc = -EEXIST; |
| 237 | goto out; |
| 238 | } |
| 239 | strlcpy(name, cfg->ifname, IFNAMSIZ); |
| 240 | } else |
| 241 | strcpy(name, L2TP_ETH_DEV_NAME); |
| 242 | |
| 243 | session = l2tp_session_create(sizeof(*spriv), tunnel, session_id, |
| 244 | peer_session_id, cfg); |
| 245 | if (!session) { |
| 246 | rc = -ENOMEM; |
| 247 | goto out; |
| 248 | } |
| 249 | |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 250 | dev = alloc_netdev(sizeof(*priv), name, NET_NAME_UNKNOWN, |
| 251 | l2tp_eth_dev_setup); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 252 | if (!dev) { |
| 253 | rc = -ENOMEM; |
| 254 | goto out_del_session; |
| 255 | } |
| 256 | |
| 257 | dev_net_set(dev, net); |
| 258 | if (session->mtu == 0) |
| 259 | session->mtu = dev->mtu - session->hdr_len; |
| 260 | dev->mtu = session->mtu; |
| 261 | dev->needed_headroom += session->hdr_len; |
Jarod Wilson | 8b1efc0 | 2016-10-20 23:25:27 -0400 | [diff] [blame] | 262 | dev->min_mtu = 0; |
| 263 | dev->max_mtu = ETH_MAX_MTU; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 264 | |
| 265 | priv = netdev_priv(dev); |
| 266 | priv->dev = dev; |
| 267 | priv->session = session; |
| 268 | INIT_LIST_HEAD(&priv->list); |
| 269 | |
| 270 | priv->tunnel_sock = tunnel->sock; |
| 271 | session->recv_skb = l2tp_eth_dev_recv; |
| 272 | session->session_close = l2tp_eth_delete; |
Javier Martinez Canillas | 9dd7994 | 2016-09-09 08:43:17 -0400 | [diff] [blame] | 273 | #if IS_ENABLED(CONFIG_L2TP_DEBUGFS) |
James Chapman | 0ad6614 | 2010-04-02 06:19:33 +0000 | [diff] [blame] | 274 | session->show = l2tp_eth_show; |
| 275 | #endif |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 276 | |
| 277 | spriv = l2tp_session_priv(session); |
| 278 | spriv->dev = dev; |
| 279 | |
| 280 | rc = register_netdev(dev); |
| 281 | if (rc < 0) |
| 282 | goto out_del_dev; |
| 283 | |
Eric Dumazet | a06998b | 2012-06-07 00:07:20 +0000 | [diff] [blame] | 284 | __module_get(THIS_MODULE); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 285 | /* Must be done after register_netdev() */ |
| 286 | strlcpy(session->ifname, dev->name, IFNAMSIZ); |
| 287 | |
| 288 | dev_hold(dev); |
| 289 | pn = l2tp_eth_pernet(dev_net(dev)); |
| 290 | spin_lock(&pn->l2tp_eth_lock); |
| 291 | list_add(&priv->list, &pn->l2tp_eth_dev_list); |
| 292 | spin_unlock(&pn->l2tp_eth_lock); |
| 293 | |
| 294 | return 0; |
| 295 | |
| 296 | out_del_dev: |
| 297 | free_netdev(dev); |
Tom Parkin | 7893363 | 2012-10-29 23:41:48 +0000 | [diff] [blame] | 298 | spriv->dev = NULL; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 299 | out_del_session: |
| 300 | l2tp_session_delete(session); |
| 301 | out: |
| 302 | return rc; |
| 303 | } |
| 304 | |
| 305 | static __net_init int l2tp_eth_init_net(struct net *net) |
| 306 | { |
Jiri Pirko | 3a73702 | 2010-04-23 01:01:52 +0000 | [diff] [blame] | 307 | struct l2tp_eth_net *pn = net_generic(net, l2tp_eth_net_id); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 308 | |
| 309 | INIT_LIST_HEAD(&pn->l2tp_eth_dev_list); |
| 310 | spin_lock_init(&pn->l2tp_eth_lock); |
| 311 | |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 312 | return 0; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 313 | } |
| 314 | |
James Chapman | 8aa525a | 2011-03-21 18:10:25 -0700 | [diff] [blame] | 315 | static struct pernet_operations l2tp_eth_net_ops = { |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 316 | .init = l2tp_eth_init_net, |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 317 | .id = &l2tp_eth_net_id, |
| 318 | .size = sizeof(struct l2tp_eth_net), |
| 319 | }; |
| 320 | |
| 321 | |
| 322 | static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = { |
| 323 | .session_create = l2tp_eth_create, |
| 324 | .session_delete = l2tp_session_delete, |
| 325 | }; |
| 326 | |
| 327 | |
| 328 | static int __init l2tp_eth_init(void) |
| 329 | { |
| 330 | int err = 0; |
| 331 | |
| 332 | err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops); |
| 333 | if (err) |
| 334 | goto out; |
| 335 | |
| 336 | err = register_pernet_device(&l2tp_eth_net_ops); |
| 337 | if (err) |
| 338 | goto out_unreg; |
| 339 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 340 | pr_info("L2TP ethernet pseudowire support (L2TPv3)\n"); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 341 | |
| 342 | return 0; |
| 343 | |
| 344 | out_unreg: |
| 345 | l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH); |
| 346 | out: |
| 347 | return err; |
| 348 | } |
| 349 | |
| 350 | static void __exit l2tp_eth_exit(void) |
| 351 | { |
| 352 | unregister_pernet_device(&l2tp_eth_net_ops); |
| 353 | l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH); |
| 354 | } |
| 355 | |
| 356 | module_init(l2tp_eth_init); |
| 357 | module_exit(l2tp_eth_exit); |
| 358 | |
| 359 | MODULE_LICENSE("GPL"); |
| 360 | MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); |
| 361 | MODULE_DESCRIPTION("L2TP ethernet pseudowire driver"); |
| 362 | MODULE_VERSION("1.0"); |
stephen hemminger | f1f39f9 | 2015-09-23 21:33:34 -0700 | [diff] [blame] | 363 | MODULE_ALIAS_L2TP_PWTYPE(5); |