Blame view
net/l2tp/l2tp_eth.c
8.3 KB
d9e31d17c
|
1 2 3 4 5 6 7 8 9 10 |
/* * 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. */ |
a4ca44fa5
|
11 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
d9e31d17c
|
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#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 "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; struct list_head list; |
a2842a1e6
|
43 44 |
atomic_long_t tx_bytes; atomic_long_t tx_packets; |
b8c843072
|
45 |
atomic_long_t tx_dropped; |
a2842a1e6
|
46 47 48 |
atomic_long_t rx_bytes; atomic_long_t rx_packets; atomic_long_t rx_errors; |
d9e31d17c
|
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
}; /* via l2tp_session_priv() */ struct l2tp_eth_sess { struct net_device *dev; }; /* per-net private data for this module */ static unsigned int l2tp_eth_net_id; struct l2tp_eth_net { struct list_head l2tp_eth_dev_list; spinlock_t l2tp_eth_lock; }; static inline struct l2tp_eth_net *l2tp_eth_pernet(struct net *net) { return net_generic(net, l2tp_eth_net_id); } |
23d3b8bfb
|
67 |
static struct lock_class_key l2tp_eth_tx_busylock; |
d9e31d17c
|
68 69 70 71 72 |
static int l2tp_eth_dev_init(struct net_device *dev) { struct l2tp_eth *priv = netdev_priv(dev); priv->dev = dev; |
f2cedb63d
|
73 |
eth_hw_addr_random(dev); |
d9e31d17c
|
74 |
memset(&dev->broadcast[0], 0xff, 6); |
23d3b8bfb
|
75 |
dev->qdisc_tx_busylock = &l2tp_eth_tx_busylock; |
d9e31d17c
|
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
return 0; } static void l2tp_eth_dev_uninit(struct net_device *dev) { struct l2tp_eth *priv = netdev_priv(dev); struct l2tp_eth_net *pn = l2tp_eth_pernet(dev_net(dev)); spin_lock(&pn->l2tp_eth_lock); list_del_init(&priv->list); spin_unlock(&pn->l2tp_eth_lock); dev_put(dev); } 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; |
b8c843072
|
94 95 96 97 98 99 100 101 102 |
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); } |
aa214de05
|
103 |
return NETDEV_TX_OK; |
d9e31d17c
|
104 |
} |
a2842a1e6
|
105 106 107 108 109 110 111 |
static struct rtnl_link_stats64 *l2tp_eth_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) { struct l2tp_eth *priv = netdev_priv(dev); stats->tx_bytes = atomic_long_read(&priv->tx_bytes); stats->tx_packets = atomic_long_read(&priv->tx_packets); |
b8c843072
|
112 |
stats->tx_dropped = atomic_long_read(&priv->tx_dropped); |
a2842a1e6
|
113 114 115 116 117 |
stats->rx_bytes = atomic_long_read(&priv->rx_bytes); stats->rx_packets = atomic_long_read(&priv->rx_packets); stats->rx_errors = atomic_long_read(&priv->rx_errors); return stats; } |
d9e31d17c
|
118 119 120 121 |
static 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, |
a2842a1e6
|
122 |
.ndo_get_stats64 = l2tp_eth_get_stats64, |
d9e31d17c
|
123 124 125 126 127 |
}; static void l2tp_eth_dev_setup(struct net_device *dev) { ether_setup(dev); |
a2842a1e6
|
128 129 |
dev->priv_flags &= ~IFF_TX_SKB_SHARING; dev->features |= NETIF_F_LLTX; |
d9e31d17c
|
130 131 132 133 134 135 136 137 |
dev->netdev_ops = &l2tp_eth_netdev_ops; dev->destructor = free_netdev; } 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; |
a2842a1e6
|
138 |
struct l2tp_eth *priv = netdev_priv(dev); |
d9e31d17c
|
139 140 141 |
if (session->debug & L2TP_MSG_DATA) { unsigned int length; |
d9e31d17c
|
142 143 144 145 |
length = min(32u, skb->len); if (!pskb_may_pull(skb, length)) goto error; |
a4ca44fa5
|
146 147 |
pr_debug("%s: eth recv ", session->name); |
a2842a1e6
|
148 |
print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length); |
d9e31d17c
|
149 |
} |
c0cc88a76
|
150 |
if (!pskb_may_pull(skb, ETH_HLEN)) |
d9e31d17c
|
151 152 153 154 155 156 157 158 159 160 161 |
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) { |
a2842a1e6
|
162 163 164 165 166 |
atomic_long_inc(&priv->rx_packets); atomic_long_add(data_len, &priv->rx_bytes); } else { atomic_long_inc(&priv->rx_errors); } |
d9e31d17c
|
167 168 169 |
return; error: |
a2842a1e6
|
170 |
atomic_long_inc(&priv->rx_errors); |
d9e31d17c
|
171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
kfree_skb(skb); } 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; |
a06998b88
|
185 |
module_put(THIS_MODULE); |
d9e31d17c
|
186 187 188 |
} } } |
f66ef2d06
|
189 |
#if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE) |
0ad661404
|
190 191 192 193 194 195 196 197 198 199 |
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 ", dev->name); } #endif |
d9e31d17c
|
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
static int l2tp_eth_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg) { struct net_device *dev; char name[IFNAMSIZ]; struct l2tp_tunnel *tunnel; struct l2tp_session *session; struct l2tp_eth *priv; struct l2tp_eth_sess *spriv; int rc; struct l2tp_eth_net *pn; tunnel = l2tp_tunnel_find(net, tunnel_id); if (!tunnel) { rc = -ENODEV; goto out; } session = l2tp_session_find(net, tunnel, session_id); if (session) { rc = -EEXIST; goto out; } if (cfg->ifname) { dev = dev_get_by_name(net, cfg->ifname); if (dev) { dev_put(dev); rc = -EEXIST; goto out; } strlcpy(name, cfg->ifname, IFNAMSIZ); } else strcpy(name, L2TP_ETH_DEV_NAME); session = l2tp_session_create(sizeof(*spriv), tunnel, session_id, peer_session_id, cfg); if (!session) { rc = -ENOMEM; goto out; } dev = alloc_netdev(sizeof(*priv), name, l2tp_eth_dev_setup); if (!dev) { rc = -ENOMEM; goto out_del_session; } dev_net_set(dev, net); if (session->mtu == 0) session->mtu = dev->mtu - session->hdr_len; dev->mtu = session->mtu; dev->needed_headroom += session->hdr_len; priv = netdev_priv(dev); priv->dev = dev; priv->session = session; INIT_LIST_HEAD(&priv->list); priv->tunnel_sock = tunnel->sock; session->recv_skb = l2tp_eth_dev_recv; session->session_close = l2tp_eth_delete; |
f66ef2d06
|
261 |
#if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE) |
0ad661404
|
262 263 |
session->show = l2tp_eth_show; #endif |
d9e31d17c
|
264 265 266 267 268 269 270 |
spriv = l2tp_session_priv(session); spriv->dev = dev; rc = register_netdev(dev); if (rc < 0) goto out_del_dev; |
a06998b88
|
271 |
__module_get(THIS_MODULE); |
d9e31d17c
|
272 273 274 275 276 277 278 279 280 281 282 283 284 |
/* Must be done after register_netdev() */ strlcpy(session->ifname, dev->name, IFNAMSIZ); dev_hold(dev); pn = l2tp_eth_pernet(dev_net(dev)); spin_lock(&pn->l2tp_eth_lock); list_add(&priv->list, &pn->l2tp_eth_dev_list); spin_unlock(&pn->l2tp_eth_lock); return 0; out_del_dev: free_netdev(dev); |
789336360
|
285 |
spriv->dev = NULL; |
d9e31d17c
|
286 287 288 289 290 291 292 293 |
out_del_session: l2tp_session_delete(session); out: return rc; } static __net_init int l2tp_eth_init_net(struct net *net) { |
3a7370286
|
294 |
struct l2tp_eth_net *pn = net_generic(net, l2tp_eth_net_id); |
d9e31d17c
|
295 296 297 |
INIT_LIST_HEAD(&pn->l2tp_eth_dev_list); spin_lock_init(&pn->l2tp_eth_lock); |
d9e31d17c
|
298 |
return 0; |
d9e31d17c
|
299 |
} |
8aa525a93
|
300 |
static struct pernet_operations l2tp_eth_net_ops = { |
d9e31d17c
|
301 |
.init = l2tp_eth_init_net, |
d9e31d17c
|
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
.id = &l2tp_eth_net_id, .size = sizeof(struct l2tp_eth_net), }; 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 out; err = register_pernet_device(&l2tp_eth_net_ops); if (err) goto out_unreg; |
a4ca44fa5
|
324 325 |
pr_info("L2TP ethernet pseudowire support (L2TPv3) "); |
d9e31d17c
|
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
return 0; out_unreg: l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH); out: return err; } static void __exit l2tp_eth_exit(void) { unregister_pernet_device(&l2tp_eth_net_ops); l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH); } 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"); |