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