Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 2 | /* |
| 3 | * L2TPv3 ethernet pseudowire driver |
| 4 | * |
| 5 | * Copyright (c) 2008,2009,2010 Katalix Systems Ltd |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/skbuff.h> |
| 12 | #include <linux/socket.h> |
| 13 | #include <linux/hash.h> |
| 14 | #include <linux/l2tp.h> |
| 15 | #include <linux/in.h> |
| 16 | #include <linux/etherdevice.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <net/sock.h> |
| 19 | #include <net/ip.h> |
| 20 | #include <net/icmp.h> |
| 21 | #include <net/udp.h> |
| 22 | #include <net/inet_common.h> |
| 23 | #include <net/inet_hashtables.h> |
| 24 | #include <net/tcp_states.h> |
| 25 | #include <net/protocol.h> |
| 26 | #include <net/xfrm.h> |
| 27 | #include <net/net_namespace.h> |
| 28 | #include <net/netns/generic.h> |
R. Parameswaran | b784e7e | 2017-04-05 17:00:07 -0700 | [diff] [blame] | 29 | #include <linux/ip.h> |
| 30 | #include <linux/ipv6.h> |
| 31 | #include <linux/udp.h> |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 32 | |
| 33 | #include "l2tp_core.h" |
| 34 | |
| 35 | /* Default device name. May be overridden by name specified by user */ |
| 36 | #define L2TP_ETH_DEV_NAME "l2tpeth%d" |
| 37 | |
| 38 | /* via netdev_priv() */ |
| 39 | struct l2tp_eth { |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 40 | struct l2tp_session *session; |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 41 | atomic_long_t tx_bytes; |
| 42 | atomic_long_t tx_packets; |
Eric Dumazet | b8c8430 | 2012-06-28 20:15:13 +0000 | [diff] [blame] | 43 | atomic_long_t tx_dropped; |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 44 | atomic_long_t rx_bytes; |
| 45 | atomic_long_t rx_packets; |
| 46 | atomic_long_t rx_errors; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | /* via l2tp_session_priv() */ |
| 50 | struct l2tp_eth_sess { |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 51 | struct net_device __rcu *dev; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 54 | |
| 55 | static int l2tp_eth_dev_init(struct net_device *dev) |
| 56 | { |
Danny Kukawka | f2cedb6 | 2012-02-15 06:45:39 +0000 | [diff] [blame] | 57 | eth_hw_addr_random(dev); |
Joe Perches | 1cea7e2 | 2015-03-02 19:54:59 -0800 | [diff] [blame] | 58 | eth_broadcast_addr(dev->broadcast); |
Eric Dumazet | d3fff6c | 2016-06-09 07:45:12 -0700 | [diff] [blame] | 59 | netdev_lockdep_set_classes(dev); |
Eric Dumazet | f9eb8ae | 2016-06-06 09:37:15 -0700 | [diff] [blame] | 60 | |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static void l2tp_eth_dev_uninit(struct net_device *dev) |
| 65 | { |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 66 | struct l2tp_eth *priv = netdev_priv(dev); |
| 67 | struct l2tp_eth_sess *spriv; |
| 68 | |
| 69 | spriv = l2tp_session_priv(priv->session); |
| 70 | RCU_INIT_POINTER(spriv->dev, NULL); |
| 71 | /* No need for synchronize_net() here. We're called by |
| 72 | * unregister_netdev*(), which does the synchronisation for us. |
| 73 | */ |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev) |
| 77 | { |
| 78 | struct l2tp_eth *priv = netdev_priv(dev); |
| 79 | struct l2tp_session *session = priv->session; |
Eric Dumazet | b8c8430 | 2012-06-28 20:15:13 +0000 | [diff] [blame] | 80 | unsigned int len = skb->len; |
| 81 | int ret = l2tp_xmit_skb(session, skb, session->hdr_len); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 82 | |
WANG Cong | a4cd027 | 2016-11-21 23:24:43 -0800 | [diff] [blame] | 83 | if (likely(ret == NET_XMIT_SUCCESS)) { |
Eric Dumazet | b8c8430 | 2012-06-28 20:15:13 +0000 | [diff] [blame] | 84 | atomic_long_add(len, &priv->tx_bytes); |
| 85 | atomic_long_inc(&priv->tx_packets); |
| 86 | } else { |
| 87 | atomic_long_inc(&priv->tx_dropped); |
| 88 | } |
Eric Dumazet | aa214de0 | 2012-06-25 00:45:14 +0000 | [diff] [blame] | 89 | return NETDEV_TX_OK; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 90 | } |
| 91 | |
stephen hemminger | bc1f447 | 2017-01-06 19:12:52 -0800 | [diff] [blame] | 92 | static void l2tp_eth_get_stats64(struct net_device *dev, |
| 93 | struct rtnl_link_stats64 *stats) |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 94 | { |
| 95 | struct l2tp_eth *priv = netdev_priv(dev); |
| 96 | |
Dominik Heidler | 9b3dc0a | 2017-06-09 16:29:47 +0200 | [diff] [blame] | 97 | stats->tx_bytes = (unsigned long) atomic_long_read(&priv->tx_bytes); |
| 98 | stats->tx_packets = (unsigned long) atomic_long_read(&priv->tx_packets); |
| 99 | stats->tx_dropped = (unsigned long) atomic_long_read(&priv->tx_dropped); |
| 100 | stats->rx_bytes = (unsigned long) atomic_long_read(&priv->rx_bytes); |
| 101 | stats->rx_packets = (unsigned long) atomic_long_read(&priv->rx_packets); |
| 102 | stats->rx_errors = (unsigned long) atomic_long_read(&priv->rx_errors); |
| 103 | |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Julia Lawall | eb94737 | 2016-09-15 22:23:26 +0200 | [diff] [blame] | 106 | static const struct net_device_ops l2tp_eth_netdev_ops = { |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 107 | .ndo_init = l2tp_eth_dev_init, |
| 108 | .ndo_uninit = l2tp_eth_dev_uninit, |
| 109 | .ndo_start_xmit = l2tp_eth_dev_xmit, |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 110 | .ndo_get_stats64 = l2tp_eth_get_stats64, |
Alexander Couzens | fe15912 | 2014-11-19 13:24:39 +0100 | [diff] [blame] | 111 | .ndo_set_mac_address = eth_mac_addr, |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
Guillaume Nault | a485c2b | 2017-04-24 14:16:07 +0200 | [diff] [blame] | 114 | static struct device_type l2tpeth_type = { |
| 115 | .name = "l2tpeth", |
| 116 | }; |
| 117 | |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 118 | static void l2tp_eth_dev_setup(struct net_device *dev) |
| 119 | { |
Guillaume Nault | a485c2b | 2017-04-24 14:16:07 +0200 | [diff] [blame] | 120 | SET_NETDEV_DEVTYPE(dev, &l2tpeth_type); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 121 | ether_setup(dev); |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 122 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
| 123 | dev->features |= NETIF_F_LLTX; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 124 | dev->netdev_ops = &l2tp_eth_netdev_ops; |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 125 | dev->needs_free_netdev = true; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len) |
| 129 | { |
| 130 | struct l2tp_eth_sess *spriv = l2tp_session_priv(session); |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 131 | struct net_device *dev; |
| 132 | struct l2tp_eth *priv; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 133 | |
| 134 | if (session->debug & L2TP_MSG_DATA) { |
| 135 | unsigned int length; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 136 | |
| 137 | length = min(32u, skb->len); |
| 138 | if (!pskb_may_pull(skb, length)) |
| 139 | goto error; |
| 140 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 141 | pr_debug("%s: eth recv\n", session->name); |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 142 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Eric Dumazet | c0cc88a | 2012-09-04 15:54:55 -0400 | [diff] [blame] | 145 | if (!pskb_may_pull(skb, ETH_HLEN)) |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 146 | goto error; |
| 147 | |
| 148 | secpath_reset(skb); |
| 149 | |
| 150 | /* checksums verified by L2TP */ |
| 151 | skb->ip_summed = CHECKSUM_NONE; |
| 152 | |
| 153 | skb_dst_drop(skb); |
| 154 | nf_reset(skb); |
| 155 | |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 156 | rcu_read_lock(); |
| 157 | dev = rcu_dereference(spriv->dev); |
| 158 | if (!dev) |
| 159 | goto error_rcu; |
| 160 | |
| 161 | priv = netdev_priv(dev); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 162 | if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) { |
Eric Dumazet | a2842a1 | 2012-06-25 05:35:45 +0000 | [diff] [blame] | 163 | atomic_long_inc(&priv->rx_packets); |
| 164 | atomic_long_add(data_len, &priv->rx_bytes); |
| 165 | } else { |
| 166 | atomic_long_inc(&priv->rx_errors); |
| 167 | } |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 168 | rcu_read_unlock(); |
| 169 | |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 170 | return; |
| 171 | |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 172 | error_rcu: |
| 173 | rcu_read_unlock(); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 174 | error: |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 175 | kfree_skb(skb); |
| 176 | } |
| 177 | |
| 178 | static void l2tp_eth_delete(struct l2tp_session *session) |
| 179 | { |
| 180 | struct l2tp_eth_sess *spriv; |
| 181 | struct net_device *dev; |
| 182 | |
| 183 | if (session) { |
| 184 | spriv = l2tp_session_priv(session); |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 185 | |
| 186 | rtnl_lock(); |
| 187 | dev = rtnl_dereference(spriv->dev); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 188 | if (dev) { |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 189 | unregister_netdevice(dev); |
| 190 | rtnl_unlock(); |
Eric Dumazet | a06998b | 2012-06-07 00:07:20 +0000 | [diff] [blame] | 191 | module_put(THIS_MODULE); |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 192 | } else { |
| 193 | rtnl_unlock(); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
James Chapman | 0ad6614 | 2010-04-02 06:19:33 +0000 | [diff] [blame] | 198 | static void l2tp_eth_show(struct seq_file *m, void *arg) |
| 199 | { |
| 200 | struct l2tp_session *session = arg; |
| 201 | struct l2tp_eth_sess *spriv = l2tp_session_priv(session); |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 202 | struct net_device *dev; |
| 203 | |
| 204 | rcu_read_lock(); |
| 205 | dev = rcu_dereference(spriv->dev); |
| 206 | if (!dev) { |
| 207 | rcu_read_unlock(); |
| 208 | return; |
| 209 | } |
| 210 | dev_hold(dev); |
| 211 | rcu_read_unlock(); |
James Chapman | 0ad6614 | 2010-04-02 06:19:33 +0000 | [diff] [blame] | 212 | |
| 213 | seq_printf(m, " interface %s\n", dev->name); |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 214 | |
| 215 | dev_put(dev); |
James Chapman | 0ad6614 | 2010-04-02 06:19:33 +0000 | [diff] [blame] | 216 | } |
James Chapman | 0ad6614 | 2010-04-02 06:19:33 +0000 | [diff] [blame] | 217 | |
R. Parameswaran | b784e7e | 2017-04-05 17:00:07 -0700 | [diff] [blame] | 218 | static void l2tp_eth_adjust_mtu(struct l2tp_tunnel *tunnel, |
| 219 | struct l2tp_session *session, |
| 220 | struct net_device *dev) |
| 221 | { |
| 222 | unsigned int overhead = 0; |
R. Parameswaran | b784e7e | 2017-04-05 17:00:07 -0700 | [diff] [blame] | 223 | u32 l3_overhead = 0; |
Guillaume Nault | 1f5cd2a | 2018-08-03 12:38:34 +0200 | [diff] [blame] | 224 | u32 mtu; |
R. Parameswaran | b784e7e | 2017-04-05 17:00:07 -0700 | [diff] [blame] | 225 | |
| 226 | /* if the encap is UDP, account for UDP header size */ |
| 227 | if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { |
| 228 | overhead += sizeof(struct udphdr); |
| 229 | dev->needed_headroom += sizeof(struct udphdr); |
| 230 | } |
Guillaume Nault | e9697e2 | 2018-08-03 12:38:39 +0200 | [diff] [blame] | 231 | |
R. Parameswaran | 57240d0 | 2017-04-12 18:31:04 -0700 | [diff] [blame] | 232 | lock_sock(tunnel->sock); |
R. Parameswaran | b784e7e | 2017-04-05 17:00:07 -0700 | [diff] [blame] | 233 | l3_overhead = kernel_sock_ip_overhead(tunnel->sock); |
R. Parameswaran | 57240d0 | 2017-04-12 18:31:04 -0700 | [diff] [blame] | 234 | release_sock(tunnel->sock); |
Guillaume Nault | e9697e2 | 2018-08-03 12:38:39 +0200 | [diff] [blame] | 235 | |
R. Parameswaran | b784e7e | 2017-04-05 17:00:07 -0700 | [diff] [blame] | 236 | if (l3_overhead == 0) { |
| 237 | /* L3 Overhead couldn't be identified, this could be |
| 238 | * because tunnel->sock was NULL or the socket's |
| 239 | * address family was not IPv4 or IPv6, |
| 240 | * dev mtu stays at 1500. |
| 241 | */ |
| 242 | return; |
| 243 | } |
| 244 | /* Adjust MTU, factor overhead - underlay L3, overlay L2 hdr |
| 245 | * UDP overhead, if any, was already factored in above. |
| 246 | */ |
| 247 | overhead += session->hdr_len + ETH_HLEN + l3_overhead; |
| 248 | |
Guillaume Nault | e9697e2 | 2018-08-03 12:38:39 +0200 | [diff] [blame] | 249 | mtu = l2tp_tunnel_dst_mtu(tunnel) - overhead; |
| 250 | if (mtu < dev->min_mtu || mtu > dev->max_mtu) |
| 251 | dev->mtu = ETH_DATA_LEN - overhead; |
| 252 | else |
Guillaume Nault | 1f5cd2a | 2018-08-03 12:38:34 +0200 | [diff] [blame] | 253 | dev->mtu = mtu; |
Guillaume Nault | e9697e2 | 2018-08-03 12:38:39 +0200 | [diff] [blame] | 254 | |
R. Parameswaran | b784e7e | 2017-04-05 17:00:07 -0700 | [diff] [blame] | 255 | dev->needed_headroom += session->hdr_len; |
| 256 | } |
| 257 | |
Guillaume Nault | f026bc2 | 2017-09-01 17:58:51 +0200 | [diff] [blame] | 258 | static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel, |
| 259 | u32 session_id, u32 peer_session_id, |
| 260 | struct l2tp_session_cfg *cfg) |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 261 | { |
Guillaume Nault | c39855f | 2017-04-24 14:16:06 +0200 | [diff] [blame] | 262 | unsigned char name_assign_type; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 263 | struct net_device *dev; |
| 264 | char name[IFNAMSIZ]; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 265 | struct l2tp_session *session; |
| 266 | struct l2tp_eth *priv; |
| 267 | struct l2tp_eth_sess *spriv; |
| 268 | int rc; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 269 | |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 270 | if (cfg->ifname) { |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 271 | strlcpy(name, cfg->ifname, IFNAMSIZ); |
Guillaume Nault | c39855f | 2017-04-24 14:16:06 +0200 | [diff] [blame] | 272 | name_assign_type = NET_NAME_USER; |
| 273 | } else { |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 274 | strcpy(name, L2TP_ETH_DEV_NAME); |
Guillaume Nault | c39855f | 2017-04-24 14:16:06 +0200 | [diff] [blame] | 275 | name_assign_type = NET_NAME_ENUM; |
| 276 | } |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 277 | |
| 278 | session = l2tp_session_create(sizeof(*spriv), tunnel, session_id, |
| 279 | peer_session_id, cfg); |
Guillaume Nault | dbdbc73 | 2017-03-31 13:02:27 +0200 | [diff] [blame] | 280 | if (IS_ERR(session)) { |
| 281 | rc = PTR_ERR(session); |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 282 | goto err; |
Guillaume Nault | 3953ae7 | 2017-10-27 16:51:50 +0200 | [diff] [blame] | 283 | } |
| 284 | |
Guillaume Nault | c39855f | 2017-04-24 14:16:06 +0200 | [diff] [blame] | 285 | dev = alloc_netdev(sizeof(*priv), name, name_assign_type, |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 286 | l2tp_eth_dev_setup); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 287 | if (!dev) { |
| 288 | rc = -ENOMEM; |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 289 | goto err_sess; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | dev_net_set(dev, net); |
Jarod Wilson | 8b1efc0 | 2016-10-20 23:25:27 -0400 | [diff] [blame] | 293 | dev->min_mtu = 0; |
| 294 | dev->max_mtu = ETH_MAX_MTU; |
R. Parameswaran | b784e7e | 2017-04-05 17:00:07 -0700 | [diff] [blame] | 295 | l2tp_eth_adjust_mtu(tunnel, session, dev); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 296 | |
| 297 | priv = netdev_priv(dev); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 298 | priv->session = session; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 299 | |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 300 | session->recv_skb = l2tp_eth_dev_recv; |
| 301 | session->session_close = l2tp_eth_delete; |
Arnd Bergmann | c2ebc25 | 2018-08-13 23:43:05 +0200 | [diff] [blame] | 302 | if (IS_ENABLED(CONFIG_L2TP_DEBUGFS)) |
| 303 | session->show = l2tp_eth_show; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 304 | |
| 305 | spriv = l2tp_session_priv(session); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 306 | |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 307 | l2tp_session_inc_refcount(session); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 308 | |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 309 | rtnl_lock(); |
| 310 | |
| 311 | /* Register both device and session while holding the rtnl lock. This |
| 312 | * ensures that l2tp_eth_delete() will see that there's a device to |
| 313 | * unregister, even if it happened to run before we assign spriv->dev. |
| 314 | */ |
| 315 | rc = l2tp_session_register(session, tunnel); |
| 316 | if (rc < 0) { |
| 317 | rtnl_unlock(); |
| 318 | goto err_sess_dev; |
| 319 | } |
| 320 | |
| 321 | rc = register_netdevice(dev); |
| 322 | if (rc < 0) { |
| 323 | rtnl_unlock(); |
| 324 | l2tp_session_delete(session); |
| 325 | l2tp_session_dec_refcount(session); |
| 326 | free_netdev(dev); |
| 327 | |
| 328 | return rc; |
| 329 | } |
| 330 | |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 331 | strlcpy(session->ifname, dev->name, IFNAMSIZ); |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 332 | rcu_assign_pointer(spriv->dev, dev); |
| 333 | |
| 334 | rtnl_unlock(); |
| 335 | |
Guillaume Nault | 3953ae7 | 2017-10-27 16:51:50 +0200 | [diff] [blame] | 336 | l2tp_session_dec_refcount(session); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 337 | |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 338 | __module_get(THIS_MODULE); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 339 | |
| 340 | return 0; |
| 341 | |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 342 | err_sess_dev: |
Guillaume Nault | 3953ae7 | 2017-10-27 16:51:50 +0200 | [diff] [blame] | 343 | l2tp_session_dec_refcount(session); |
Guillaume Nault | ee28de6 | 2017-10-27 16:51:51 +0200 | [diff] [blame] | 344 | free_netdev(dev); |
| 345 | err_sess: |
| 346 | kfree(session); |
| 347 | err: |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 348 | return rc; |
| 349 | } |
| 350 | |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 351 | |
| 352 | static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = { |
| 353 | .session_create = l2tp_eth_create, |
| 354 | .session_delete = l2tp_session_delete, |
| 355 | }; |
| 356 | |
| 357 | |
| 358 | static int __init l2tp_eth_init(void) |
| 359 | { |
| 360 | int err = 0; |
| 361 | |
| 362 | err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops); |
| 363 | if (err) |
Guillaume Nault | 9f775ea | 2017-09-28 15:44:38 +0200 | [diff] [blame] | 364 | goto err; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 365 | |
Joe Perches | a4ca44f | 2012-05-16 09:55:56 +0000 | [diff] [blame] | 366 | pr_info("L2TP ethernet pseudowire support (L2TPv3)\n"); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 367 | |
| 368 | return 0; |
| 369 | |
Guillaume Nault | 9f775ea | 2017-09-28 15:44:38 +0200 | [diff] [blame] | 370 | err: |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 371 | return err; |
| 372 | } |
| 373 | |
| 374 | static void __exit l2tp_eth_exit(void) |
| 375 | { |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 376 | l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH); |
| 377 | } |
| 378 | |
| 379 | module_init(l2tp_eth_init); |
| 380 | module_exit(l2tp_eth_exit); |
| 381 | |
| 382 | MODULE_LICENSE("GPL"); |
| 383 | MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); |
| 384 | MODULE_DESCRIPTION("L2TP ethernet pseudowire driver"); |
| 385 | MODULE_VERSION("1.0"); |
stephen hemminger | f1f39f9 | 2015-09-23 21:33:34 -0700 | [diff] [blame] | 386 | MODULE_ALIAS_L2TP_PWTYPE(5); |