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 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/skbuff.h> |
| 14 | #include <linux/socket.h> |
| 15 | #include <linux/hash.h> |
| 16 | #include <linux/l2tp.h> |
| 17 | #include <linux/in.h> |
| 18 | #include <linux/etherdevice.h> |
| 19 | #include <linux/spinlock.h> |
| 20 | #include <net/sock.h> |
| 21 | #include <net/ip.h> |
| 22 | #include <net/icmp.h> |
| 23 | #include <net/udp.h> |
| 24 | #include <net/inet_common.h> |
| 25 | #include <net/inet_hashtables.h> |
| 26 | #include <net/tcp_states.h> |
| 27 | #include <net/protocol.h> |
| 28 | #include <net/xfrm.h> |
| 29 | #include <net/net_namespace.h> |
| 30 | #include <net/netns/generic.h> |
| 31 | |
| 32 | #include "l2tp_core.h" |
| 33 | |
| 34 | /* Default device name. May be overridden by name specified by user */ |
| 35 | #define L2TP_ETH_DEV_NAME "l2tpeth%d" |
| 36 | |
| 37 | /* via netdev_priv() */ |
| 38 | struct l2tp_eth { |
| 39 | struct net_device *dev; |
| 40 | struct sock *tunnel_sock; |
| 41 | struct l2tp_session *session; |
| 42 | struct list_head list; |
| 43 | }; |
| 44 | |
| 45 | /* via l2tp_session_priv() */ |
| 46 | struct l2tp_eth_sess { |
| 47 | struct net_device *dev; |
| 48 | }; |
| 49 | |
| 50 | /* per-net private data for this module */ |
| 51 | static unsigned int l2tp_eth_net_id; |
| 52 | struct l2tp_eth_net { |
| 53 | struct list_head l2tp_eth_dev_list; |
| 54 | spinlock_t l2tp_eth_lock; |
| 55 | }; |
| 56 | |
| 57 | static inline struct l2tp_eth_net *l2tp_eth_pernet(struct net *net) |
| 58 | { |
| 59 | return net_generic(net, l2tp_eth_net_id); |
| 60 | } |
| 61 | |
| 62 | static int l2tp_eth_dev_init(struct net_device *dev) |
| 63 | { |
| 64 | struct l2tp_eth *priv = netdev_priv(dev); |
| 65 | |
| 66 | priv->dev = dev; |
| 67 | random_ether_addr(dev->dev_addr); |
| 68 | memset(&dev->broadcast[0], 0xff, 6); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static void l2tp_eth_dev_uninit(struct net_device *dev) |
| 74 | { |
| 75 | struct l2tp_eth *priv = netdev_priv(dev); |
| 76 | struct l2tp_eth_net *pn = l2tp_eth_pernet(dev_net(dev)); |
| 77 | |
| 78 | spin_lock(&pn->l2tp_eth_lock); |
| 79 | list_del_init(&priv->list); |
| 80 | spin_unlock(&pn->l2tp_eth_lock); |
| 81 | dev_put(dev); |
| 82 | } |
| 83 | |
| 84 | static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev) |
| 85 | { |
| 86 | struct l2tp_eth *priv = netdev_priv(dev); |
| 87 | struct l2tp_session *session = priv->session; |
| 88 | |
| 89 | l2tp_xmit_skb(session, skb, session->hdr_len); |
| 90 | |
| 91 | dev->stats.tx_bytes += skb->len; |
| 92 | dev->stats.tx_packets++; |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static struct net_device_ops l2tp_eth_netdev_ops = { |
| 98 | .ndo_init = l2tp_eth_dev_init, |
| 99 | .ndo_uninit = l2tp_eth_dev_uninit, |
| 100 | .ndo_start_xmit = l2tp_eth_dev_xmit, |
| 101 | }; |
| 102 | |
| 103 | static void l2tp_eth_dev_setup(struct net_device *dev) |
| 104 | { |
| 105 | ether_setup(dev); |
| 106 | |
| 107 | dev->netdev_ops = &l2tp_eth_netdev_ops; |
| 108 | dev->destructor = free_netdev; |
| 109 | } |
| 110 | |
| 111 | static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len) |
| 112 | { |
| 113 | struct l2tp_eth_sess *spriv = l2tp_session_priv(session); |
| 114 | struct net_device *dev = spriv->dev; |
| 115 | |
| 116 | if (session->debug & L2TP_MSG_DATA) { |
| 117 | unsigned int length; |
| 118 | int offset; |
| 119 | u8 *ptr = skb->data; |
| 120 | |
| 121 | length = min(32u, skb->len); |
| 122 | if (!pskb_may_pull(skb, length)) |
| 123 | goto error; |
| 124 | |
| 125 | printk(KERN_DEBUG "%s: eth recv: ", session->name); |
| 126 | |
| 127 | offset = 0; |
| 128 | do { |
| 129 | printk(" %02X", ptr[offset]); |
| 130 | } while (++offset < length); |
| 131 | |
| 132 | printk("\n"); |
| 133 | } |
| 134 | |
Eric Dumazet | bfc960a | 2010-08-25 23:44:35 +0000 | [diff] [blame] | 135 | if (!pskb_may_pull(skb, sizeof(ETH_HLEN))) |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 136 | goto error; |
| 137 | |
| 138 | secpath_reset(skb); |
| 139 | |
| 140 | /* checksums verified by L2TP */ |
| 141 | skb->ip_summed = CHECKSUM_NONE; |
| 142 | |
| 143 | skb_dst_drop(skb); |
| 144 | nf_reset(skb); |
| 145 | |
| 146 | if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) { |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 147 | dev->stats.rx_packets++; |
| 148 | dev->stats.rx_bytes += data_len; |
| 149 | } else |
| 150 | dev->stats.rx_errors++; |
| 151 | |
| 152 | return; |
| 153 | |
| 154 | error: |
| 155 | dev->stats.rx_errors++; |
| 156 | kfree_skb(skb); |
| 157 | } |
| 158 | |
| 159 | static void l2tp_eth_delete(struct l2tp_session *session) |
| 160 | { |
| 161 | struct l2tp_eth_sess *spriv; |
| 162 | struct net_device *dev; |
| 163 | |
| 164 | if (session) { |
| 165 | spriv = l2tp_session_priv(session); |
| 166 | dev = spriv->dev; |
| 167 | if (dev) { |
| 168 | unregister_netdev(dev); |
| 169 | spriv->dev = NULL; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
David S. Miller | f66ef2d | 2010-04-03 15:01:37 -0700 | [diff] [blame] | 174 | #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE) |
James Chapman | 0ad6614 | 2010-04-02 06:19:33 +0000 | [diff] [blame] | 175 | static void l2tp_eth_show(struct seq_file *m, void *arg) |
| 176 | { |
| 177 | struct l2tp_session *session = arg; |
| 178 | struct l2tp_eth_sess *spriv = l2tp_session_priv(session); |
| 179 | struct net_device *dev = spriv->dev; |
| 180 | |
| 181 | seq_printf(m, " interface %s\n", dev->name); |
| 182 | } |
| 183 | #endif |
| 184 | |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 185 | static int l2tp_eth_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg) |
| 186 | { |
| 187 | struct net_device *dev; |
| 188 | char name[IFNAMSIZ]; |
| 189 | struct l2tp_tunnel *tunnel; |
| 190 | struct l2tp_session *session; |
| 191 | struct l2tp_eth *priv; |
| 192 | struct l2tp_eth_sess *spriv; |
| 193 | int rc; |
| 194 | struct l2tp_eth_net *pn; |
| 195 | |
| 196 | tunnel = l2tp_tunnel_find(net, tunnel_id); |
| 197 | if (!tunnel) { |
| 198 | rc = -ENODEV; |
| 199 | goto out; |
| 200 | } |
| 201 | |
| 202 | session = l2tp_session_find(net, tunnel, session_id); |
| 203 | if (session) { |
| 204 | rc = -EEXIST; |
| 205 | goto out; |
| 206 | } |
| 207 | |
| 208 | if (cfg->ifname) { |
| 209 | dev = dev_get_by_name(net, cfg->ifname); |
| 210 | if (dev) { |
| 211 | dev_put(dev); |
| 212 | rc = -EEXIST; |
| 213 | goto out; |
| 214 | } |
| 215 | strlcpy(name, cfg->ifname, IFNAMSIZ); |
| 216 | } else |
| 217 | strcpy(name, L2TP_ETH_DEV_NAME); |
| 218 | |
| 219 | session = l2tp_session_create(sizeof(*spriv), tunnel, session_id, |
| 220 | peer_session_id, cfg); |
| 221 | if (!session) { |
| 222 | rc = -ENOMEM; |
| 223 | goto out; |
| 224 | } |
| 225 | |
| 226 | dev = alloc_netdev(sizeof(*priv), name, l2tp_eth_dev_setup); |
| 227 | if (!dev) { |
| 228 | rc = -ENOMEM; |
| 229 | goto out_del_session; |
| 230 | } |
| 231 | |
| 232 | dev_net_set(dev, net); |
| 233 | if (session->mtu == 0) |
| 234 | session->mtu = dev->mtu - session->hdr_len; |
| 235 | dev->mtu = session->mtu; |
| 236 | dev->needed_headroom += session->hdr_len; |
| 237 | |
| 238 | priv = netdev_priv(dev); |
| 239 | priv->dev = dev; |
| 240 | priv->session = session; |
| 241 | INIT_LIST_HEAD(&priv->list); |
| 242 | |
| 243 | priv->tunnel_sock = tunnel->sock; |
| 244 | session->recv_skb = l2tp_eth_dev_recv; |
| 245 | session->session_close = l2tp_eth_delete; |
David S. Miller | f66ef2d | 2010-04-03 15:01:37 -0700 | [diff] [blame] | 246 | #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE) |
James Chapman | 0ad6614 | 2010-04-02 06:19:33 +0000 | [diff] [blame] | 247 | session->show = l2tp_eth_show; |
| 248 | #endif |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 249 | |
| 250 | spriv = l2tp_session_priv(session); |
| 251 | spriv->dev = dev; |
| 252 | |
| 253 | rc = register_netdev(dev); |
| 254 | if (rc < 0) |
| 255 | goto out_del_dev; |
| 256 | |
| 257 | /* Must be done after register_netdev() */ |
| 258 | strlcpy(session->ifname, dev->name, IFNAMSIZ); |
| 259 | |
| 260 | dev_hold(dev); |
| 261 | pn = l2tp_eth_pernet(dev_net(dev)); |
| 262 | spin_lock(&pn->l2tp_eth_lock); |
| 263 | list_add(&priv->list, &pn->l2tp_eth_dev_list); |
| 264 | spin_unlock(&pn->l2tp_eth_lock); |
| 265 | |
| 266 | return 0; |
| 267 | |
| 268 | out_del_dev: |
| 269 | free_netdev(dev); |
| 270 | out_del_session: |
| 271 | l2tp_session_delete(session); |
| 272 | out: |
| 273 | return rc; |
| 274 | } |
| 275 | |
| 276 | static __net_init int l2tp_eth_init_net(struct net *net) |
| 277 | { |
Jiri Pirko | 3a73702 | 2010-04-23 01:01:52 +0000 | [diff] [blame] | 278 | struct l2tp_eth_net *pn = net_generic(net, l2tp_eth_net_id); |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 279 | |
| 280 | INIT_LIST_HEAD(&pn->l2tp_eth_dev_list); |
| 281 | spin_lock_init(&pn->l2tp_eth_lock); |
| 282 | |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 283 | return 0; |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static __net_initdata struct pernet_operations l2tp_eth_net_ops = { |
| 287 | .init = l2tp_eth_init_net, |
James Chapman | d9e31d1 | 2010-04-02 06:19:26 +0000 | [diff] [blame] | 288 | .id = &l2tp_eth_net_id, |
| 289 | .size = sizeof(struct l2tp_eth_net), |
| 290 | }; |
| 291 | |
| 292 | |
| 293 | static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = { |
| 294 | .session_create = l2tp_eth_create, |
| 295 | .session_delete = l2tp_session_delete, |
| 296 | }; |
| 297 | |
| 298 | |
| 299 | static int __init l2tp_eth_init(void) |
| 300 | { |
| 301 | int err = 0; |
| 302 | |
| 303 | err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops); |
| 304 | if (err) |
| 305 | goto out; |
| 306 | |
| 307 | err = register_pernet_device(&l2tp_eth_net_ops); |
| 308 | if (err) |
| 309 | goto out_unreg; |
| 310 | |
| 311 | printk(KERN_INFO "L2TP ethernet pseudowire support (L2TPv3)\n"); |
| 312 | |
| 313 | return 0; |
| 314 | |
| 315 | out_unreg: |
| 316 | l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH); |
| 317 | out: |
| 318 | return err; |
| 319 | } |
| 320 | |
| 321 | static void __exit l2tp_eth_exit(void) |
| 322 | { |
| 323 | unregister_pernet_device(&l2tp_eth_net_ops); |
| 324 | l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH); |
| 325 | } |
| 326 | |
| 327 | module_init(l2tp_eth_init); |
| 328 | module_exit(l2tp_eth_exit); |
| 329 | |
| 330 | MODULE_LICENSE("GPL"); |
| 331 | MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); |
| 332 | MODULE_DESCRIPTION("L2TP ethernet pseudowire driver"); |
| 333 | MODULE_VERSION("1.0"); |