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