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