cregit-Linux how code gets into the kernel

Release 4.14 net/l2tp/l2tp_eth.c

Directory: net/l2tp
/*
 * L2TPv3 ethernet pseudowire driver
 *
 * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 */


#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/socket.h>
#include <linux/hash.h>
#include <linux/l2tp.h>
#include <linux/in.h>
#include <linux/etherdevice.h>
#include <linux/spinlock.h>
#include <net/sock.h>
#include <net/ip.h>
#include <net/icmp.h>
#include <net/udp.h>
#include <net/inet_common.h>
#include <net/inet_hashtables.h>
#include <net/tcp_states.h>
#include <net/protocol.h>
#include <net/xfrm.h>
#include <net/net_namespace.h>
#include <net/netns/generic.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/udp.h>

#include "l2tp_core.h"

/* Default device name. May be overridden by name specified by user */

#define L2TP_ETH_DEV_NAME	"l2tpeth%d"

/* via netdev_priv() */

struct l2tp_eth {
	
struct net_device	*dev;
	
struct sock		*tunnel_sock;
	
struct l2tp_session	*session;
	
atomic_long_t		tx_bytes;
	
atomic_long_t		tx_packets;
	
atomic_long_t		tx_dropped;
	
atomic_long_t		rx_bytes;
	
atomic_long_t		rx_packets;
	
atomic_long_t		rx_errors;
};

/* via l2tp_session_priv() */

struct l2tp_eth_sess {
	
struct net_device	*dev;
};



static int l2tp_eth_dev_init(struct net_device *dev) { struct l2tp_eth *priv = netdev_priv(dev); priv->dev = dev; eth_hw_addr_random(dev); eth_broadcast_addr(dev->broadcast); netdev_lockdep_set_classes(dev); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
James Chapman4085.11%116.67%
Eric Dumazet510.64%350.00%
Danny Kukawka12.13%116.67%
Joe Perches12.13%116.67%
Total47100.00%6100.00%


static void l2tp_eth_dev_uninit(struct net_device *dev) { dev_put(dev); }

Contributors

PersonTokensPropCommitsCommitProp
James Chapman16100.00%1100.00%
Total16100.00%1100.00%


static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev) { struct l2tp_eth *priv = netdev_priv(dev); struct l2tp_session *session = priv->session; unsigned int len = skb->len; int ret = l2tp_xmit_skb(session, skb, session->hdr_len); if (likely(ret == NET_XMIT_SUCCESS)) { atomic_long_add(len, &priv->tx_bytes); atomic_long_inc(&priv->tx_packets); } else { atomic_long_inc(&priv->tx_dropped); } return NETDEV_TX_OK; }

Contributors

PersonTokensPropCommitsCommitProp
Eric Dumazet5656.00%375.00%
James Chapman4444.00%125.00%
Total100100.00%4100.00%


static void l2tp_eth_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) { struct l2tp_eth *priv = netdev_priv(dev); stats->tx_bytes = (unsigned long) atomic_long_read(&priv->tx_bytes); stats->tx_packets = (unsigned long) atomic_long_read(&priv->tx_packets); stats->tx_dropped = (unsigned long) atomic_long_read(&priv->tx_dropped); stats->rx_bytes = (unsigned long) atomic_long_read(&priv->rx_bytes); stats->rx_packets = (unsigned long) atomic_long_read(&priv->rx_packets); stats->rx_errors = (unsigned long) atomic_long_read(&priv->rx_errors); }

Contributors

PersonTokensPropCommitsCommitProp
Eric Dumazet9779.51%250.00%
Dominik Heidler2419.67%125.00%
Stephen Hemminger10.82%125.00%
Total122100.00%4100.00%

static const struct net_device_ops l2tp_eth_netdev_ops = { .ndo_init = l2tp_eth_dev_init, .ndo_uninit = l2tp_eth_dev_uninit, .ndo_start_xmit = l2tp_eth_dev_xmit, .ndo_get_stats64 = l2tp_eth_get_stats64, .ndo_set_mac_address = eth_mac_addr, }; static struct device_type l2tpeth_type = { .name = "l2tpeth", };
static void l2tp_eth_dev_setup(struct net_device *dev) { SET_NETDEV_DEVTYPE(dev, &l2tpeth_type); ether_setup(dev); dev->priv_flags &= ~IFF_TX_SKB_SHARING; dev->features |= NETIF_F_LLTX; dev->netdev_ops = &l2tp_eth_netdev_ops; dev->needs_free_netdev = true; }

Contributors

PersonTokensPropCommitsCommitProp
James Chapman2754.00%120.00%
Guillaume Nault816.00%120.00%
Neil Horman714.00%120.00%
Eric Dumazet612.00%120.00%
David S. Miller24.00%120.00%
Total50100.00%5100.00%


static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len) { struct l2tp_eth_sess *spriv = l2tp_session_priv(session); struct net_device *dev = spriv->dev; struct l2tp_eth *priv = netdev_priv(dev); if (session->debug & L2TP_MSG_DATA) { unsigned int length; length = min(32u, skb->len); if (!pskb_may_pull(skb, length)) goto error; pr_debug("%s: eth recv\n", session->name); print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length); } if (!pskb_may_pull(skb, ETH_HLEN)) goto error; secpath_reset(skb); /* checksums verified by L2TP */ skb->ip_summed = CHECKSUM_NONE; skb_dst_drop(skb); nf_reset(skb); if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) { atomic_long_inc(&priv->rx_packets); atomic_long_add(data_len, &priv->rx_bytes); } else { atomic_long_inc(&priv->rx_errors); } return; error: atomic_long_inc(&priv->rx_errors); kfree_skb(skb); }

Contributors

PersonTokensPropCommitsCommitProp
James Chapman15175.12%125.00%
Eric Dumazet4321.39%250.00%
Joe Perches73.48%125.00%
Total201100.00%4100.00%


static void l2tp_eth_delete(struct l2tp_session *session) { struct l2tp_eth_sess *spriv; struct net_device *dev; if (session) { spriv = l2tp_session_priv(session); dev = spriv->dev; if (dev) { unregister_netdev(dev); spriv->dev = NULL; module_put(THIS_MODULE); } } }

Contributors

PersonTokensPropCommitsCommitProp
James Chapman5791.94%150.00%
Eric Dumazet58.06%150.00%
Total62100.00%2100.00%

#if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
static void l2tp_eth_show(struct seq_file *m, void *arg) { struct l2tp_session *session = arg; struct l2tp_eth_sess *spriv = l2tp_session_priv(session); struct net_device *dev = spriv->dev; seq_printf(m, " interface %s\n", dev->name); }

Contributors

PersonTokensPropCommitsCommitProp
James Chapman52100.00%1100.00%
Total52100.00%1100.00%

#endif
static void l2tp_eth_adjust_mtu(struct l2tp_tunnel *tunnel, struct l2tp_session *session, struct net_device *dev) { unsigned int overhead = 0; struct dst_entry *dst; u32 l3_overhead = 0; /* if the encap is UDP, account for UDP header size */ if (tunnel->encap == L2TP_ENCAPTYPE_UDP) { overhead += sizeof(struct udphdr); dev->needed_headroom += sizeof(struct udphdr); } if (session->mtu != 0) { dev->mtu = session->mtu; dev->needed_headroom += session->hdr_len; return; } lock_sock(tunnel->sock); l3_overhead = kernel_sock_ip_overhead(tunnel->sock); release_sock(tunnel->sock); if (l3_overhead == 0) { /* L3 Overhead couldn't be identified, this could be * because tunnel->sock was NULL or the socket's * address family was not IPv4 or IPv6, * dev mtu stays at 1500. */ return; } /* Adjust MTU, factor overhead - underlay L3, overlay L2 hdr * UDP overhead, if any, was already factored in above. */ overhead += session->hdr_len + ETH_HLEN + l3_overhead; /* If PMTU discovery was enabled, use discovered MTU on L2TP device */ dst = sk_dst_get(tunnel->sock); if (dst) { /* dst_mtu will use PMTU if found, else fallback to intf MTU */ u32 pmtu = dst_mtu(dst); if (pmtu != 0) dev->mtu = pmtu; dst_release(dst); } session->mtu = dev->mtu - overhead; dev->mtu = session->mtu; dev->needed_headroom += session->hdr_len; }

Contributors

PersonTokensPropCommitsCommitProp
R. Parameswaran205100.00%2100.00%
Total205100.00%2100.00%


static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg) { unsigned char name_assign_type; struct net_device *dev; char name[IFNAMSIZ]; struct l2tp_session *session; struct l2tp_eth *priv; struct l2tp_eth_sess *spriv; int rc; if (cfg->ifname) { strlcpy(name, cfg->ifname, IFNAMSIZ); name_assign_type = NET_NAME_USER; } else { strcpy(name, L2TP_ETH_DEV_NAME); name_assign_type = NET_NAME_ENUM; } session = l2tp_session_create(sizeof(*spriv), tunnel, session_id, peer_session_id, cfg); if (IS_ERR(session)) { rc = PTR_ERR(session); goto out; } dev = alloc_netdev(sizeof(*priv), name, name_assign_type, l2tp_eth_dev_setup); if (!dev) { rc = -ENOMEM; goto out_del_session; } dev_net_set(dev, net); dev->min_mtu = 0; dev->max_mtu = ETH_MAX_MTU; l2tp_eth_adjust_mtu(tunnel, session, dev); priv = netdev_priv(dev); priv->dev = dev; priv->session = session; priv->tunnel_sock = tunnel->sock; session->recv_skb = l2tp_eth_dev_recv; session->session_close = l2tp_eth_delete; #if IS_ENABLED(CONFIG_L2TP_DEBUGFS) session->show = l2tp_eth_show; #endif spriv = l2tp_session_priv(session); spriv->dev = dev; rc = register_netdev(dev); if (rc < 0) goto out_del_dev; __module_get(THIS_MODULE); /* Must be done after register_netdev() */ strlcpy(session->ifname, dev->name, IFNAMSIZ); dev_hold(dev); return 0; out_del_dev: free_netdev(dev); spriv->dev = NULL; out_del_session: l2tp_session_delete(session); out: return rc; }

Contributors

PersonTokensPropCommitsCommitProp
James Chapman26580.55%216.67%
Guillaume Nault267.90%325.00%
Jarod Wilson123.65%18.33%
R. Parameswaran92.74%18.33%
Tom Parkin61.82%18.33%
Eric Dumazet51.52%18.33%
David S. Miller41.22%18.33%
Javier Martinez Canillas10.30%18.33%
Tom Gundersen10.30%18.33%
Total329100.00%12100.00%

static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = { .session_create = l2tp_eth_create, .session_delete = l2tp_session_delete, };
static int __init l2tp_eth_init(void) { int err = 0; err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops); if (err) goto err; pr_info("L2TP ethernet pseudowire support (L2TPv3)\n"); return 0; err: return err; }

Contributors

PersonTokensPropCommitsCommitProp
James Chapman4295.45%133.33%
Joe Perches12.27%133.33%
Guillaume Nault12.27%133.33%
Total44100.00%3100.00%


static void __exit l2tp_eth_exit(void) { l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH); }

Contributors

PersonTokensPropCommitsCommitProp
James Chapman14100.00%1100.00%
Total14100.00%1100.00%

module_init(l2tp_eth_init); module_exit(l2tp_eth_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); MODULE_DESCRIPTION("L2TP ethernet pseudowire driver"); MODULE_VERSION("1.0"); MODULE_ALIAS_L2TP_PWTYPE(5);

Overall Contributors

PersonTokensPropCommitsCommitProp
James Chapman87959.39%26.25%
Eric Dumazet24016.22%825.00%
R. Parameswaran22315.07%26.25%
Guillaume Nault473.18%515.62%
Dominik Heidler241.62%13.12%
Joe Perches161.08%26.25%
Jarod Wilson120.81%13.12%
David S. Miller100.68%26.25%
Neil Horman70.47%13.12%
Tom Parkin60.41%13.12%
Stephen Hemminger60.41%26.25%
Alexander Couzens50.34%13.12%
Javier Martinez Canillas20.14%13.12%
Julia Lawall10.07%13.12%
Tom Gundersen10.07%13.12%
Danny Kukawka10.07%13.12%
Total1480100.00%32100.00%
Directory: net/l2tp
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.