l2tp_eth.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #include <linux/module.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/socket.h>
  14. #include <linux/hash.h>
  15. #include <linux/l2tp.h>
  16. #include <linux/in.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/spinlock.h>
  19. #include <net/sock.h>
  20. #include <net/ip.h>
  21. #include <net/icmp.h>
  22. #include <net/udp.h>
  23. #include <net/inet_common.h>
  24. #include <net/inet_hashtables.h>
  25. #include <net/tcp_states.h>
  26. #include <net/protocol.h>
  27. #include <net/xfrm.h>
  28. #include <net/net_namespace.h>
  29. #include <net/netns/generic.h>
  30. #include "l2tp_core.h"
  31. /* Default device name. May be overridden by name specified by user */
  32. #define L2TP_ETH_DEV_NAME "l2tpeth%d"
  33. /* via netdev_priv() */
  34. struct l2tp_eth {
  35. struct net_device *dev;
  36. struct sock *tunnel_sock;
  37. struct l2tp_session *session;
  38. };
  39. /* via l2tp_session_priv() */
  40. struct l2tp_eth_sess {
  41. struct net_device __rcu *dev;
  42. };
  43. static int l2tp_eth_dev_init(struct net_device *dev)
  44. {
  45. struct l2tp_eth *priv = netdev_priv(dev);
  46. priv->dev = dev;
  47. eth_hw_addr_random(dev);
  48. memset(&dev->broadcast[0], 0xff, 6);
  49. return 0;
  50. }
  51. static void l2tp_eth_dev_uninit(struct net_device *dev)
  52. {
  53. struct l2tp_eth *priv = netdev_priv(dev);
  54. struct l2tp_eth_sess *spriv;
  55. spriv = l2tp_session_priv(priv->session);
  56. RCU_INIT_POINTER(spriv->dev, NULL);
  57. /* No need for synchronize_net() here. We're called by
  58. * unregister_netdev*(), which does the synchronisation for us.
  59. */
  60. }
  61. static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev)
  62. {
  63. struct l2tp_eth *priv = netdev_priv(dev);
  64. struct l2tp_session *session = priv->session;
  65. l2tp_xmit_skb(session, skb, session->hdr_len);
  66. dev->stats.tx_bytes += skb->len;
  67. dev->stats.tx_packets++;
  68. return 0;
  69. }
  70. static struct net_device_ops l2tp_eth_netdev_ops = {
  71. .ndo_init = l2tp_eth_dev_init,
  72. .ndo_uninit = l2tp_eth_dev_uninit,
  73. .ndo_start_xmit = l2tp_eth_dev_xmit,
  74. };
  75. static void l2tp_eth_dev_setup(struct net_device *dev)
  76. {
  77. ether_setup(dev);
  78. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  79. dev->netdev_ops = &l2tp_eth_netdev_ops;
  80. dev->destructor = free_netdev;
  81. }
  82. static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
  83. {
  84. struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
  85. struct net_device *dev;
  86. if (session->debug & L2TP_MSG_DATA) {
  87. unsigned int length;
  88. int offset;
  89. u8 *ptr = skb->data;
  90. length = min(32u, skb->len);
  91. if (!pskb_may_pull(skb, length))
  92. goto error;
  93. printk(KERN_DEBUG "%s: eth recv: ", session->name);
  94. offset = 0;
  95. do {
  96. printk(" %02X", ptr[offset]);
  97. } while (++offset < length);
  98. printk("\n");
  99. }
  100. if (!pskb_may_pull(skb, ETH_HLEN))
  101. goto error;
  102. secpath_reset(skb);
  103. /* checksums verified by L2TP */
  104. skb->ip_summed = CHECKSUM_NONE;
  105. skb_dst_drop(skb);
  106. nf_reset(skb);
  107. rcu_read_lock();
  108. dev = rcu_dereference(spriv->dev);
  109. if (!dev)
  110. goto error_rcu;
  111. if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) {
  112. dev->stats.rx_packets++;
  113. dev->stats.rx_bytes += data_len;
  114. } else
  115. dev->stats.rx_errors++;
  116. rcu_read_unlock();
  117. return;
  118. error_rcu:
  119. rcu_read_unlock();
  120. error:
  121. dev->stats.rx_errors++;
  122. kfree_skb(skb);
  123. }
  124. static void l2tp_eth_delete(struct l2tp_session *session)
  125. {
  126. struct l2tp_eth_sess *spriv;
  127. struct net_device *dev;
  128. if (session) {
  129. spriv = l2tp_session_priv(session);
  130. rtnl_lock();
  131. dev = rtnl_dereference(spriv->dev);
  132. if (dev) {
  133. unregister_netdevice(dev);
  134. rtnl_unlock();
  135. module_put(THIS_MODULE);
  136. } else {
  137. rtnl_unlock();
  138. }
  139. }
  140. }
  141. #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
  142. static void l2tp_eth_show(struct seq_file *m, void *arg)
  143. {
  144. struct l2tp_session *session = arg;
  145. struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
  146. struct net_device *dev;
  147. rcu_read_lock();
  148. dev = rcu_dereference(spriv->dev);
  149. if (!dev) {
  150. rcu_read_unlock();
  151. return;
  152. }
  153. dev_hold(dev);
  154. rcu_read_unlock();
  155. seq_printf(m, " interface %s\n", dev->name);
  156. dev_put(dev);
  157. }
  158. #endif
  159. static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel,
  160. u32 session_id, u32 peer_session_id,
  161. struct l2tp_session_cfg *cfg)
  162. {
  163. struct net_device *dev;
  164. char name[IFNAMSIZ];
  165. struct l2tp_session *session;
  166. struct l2tp_eth *priv;
  167. struct l2tp_eth_sess *spriv;
  168. int rc;
  169. if (cfg->ifname) {
  170. dev = dev_get_by_name(net, cfg->ifname);
  171. if (dev) {
  172. dev_put(dev);
  173. rc = -EEXIST;
  174. goto err;
  175. }
  176. strlcpy(name, cfg->ifname, IFNAMSIZ);
  177. } else
  178. strcpy(name, L2TP_ETH_DEV_NAME);
  179. session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
  180. peer_session_id, cfg);
  181. if (IS_ERR(session)) {
  182. rc = PTR_ERR(session);
  183. goto err;
  184. }
  185. dev = alloc_netdev(sizeof(*priv), name, l2tp_eth_dev_setup);
  186. if (!dev) {
  187. rc = -ENOMEM;
  188. goto err_sess;
  189. }
  190. dev_net_set(dev, net);
  191. if (session->mtu == 0)
  192. session->mtu = dev->mtu - session->hdr_len;
  193. dev->mtu = session->mtu;
  194. dev->needed_headroom += session->hdr_len;
  195. priv = netdev_priv(dev);
  196. priv->dev = dev;
  197. priv->session = session;
  198. priv->tunnel_sock = tunnel->sock;
  199. session->recv_skb = l2tp_eth_dev_recv;
  200. session->session_close = l2tp_eth_delete;
  201. #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
  202. session->show = l2tp_eth_show;
  203. #endif
  204. spriv = l2tp_session_priv(session);
  205. l2tp_session_inc_refcount(session);
  206. rtnl_lock();
  207. /* Register both device and session while holding the rtnl lock. This
  208. * ensures that l2tp_eth_delete() will see that there's a device to
  209. * unregister, even if it happened to run before we assign spriv->dev.
  210. */
  211. rc = l2tp_session_register(session, tunnel);
  212. if (rc < 0) {
  213. rtnl_unlock();
  214. goto err_sess_dev;
  215. }
  216. rc = register_netdevice(dev);
  217. if (rc < 0) {
  218. rtnl_unlock();
  219. l2tp_session_delete(session);
  220. l2tp_session_dec_refcount(session);
  221. free_netdev(dev);
  222. return rc;
  223. }
  224. strlcpy(session->ifname, dev->name, IFNAMSIZ);
  225. rcu_assign_pointer(spriv->dev, dev);
  226. rtnl_unlock();
  227. l2tp_session_dec_refcount(session);
  228. __module_get(THIS_MODULE);
  229. return 0;
  230. err_sess_dev:
  231. l2tp_session_dec_refcount(session);
  232. free_netdev(dev);
  233. err_sess:
  234. kfree(session);
  235. err:
  236. return rc;
  237. }
  238. static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
  239. .session_create = l2tp_eth_create,
  240. .session_delete = l2tp_session_delete,
  241. };
  242. static int __init l2tp_eth_init(void)
  243. {
  244. int err = 0;
  245. err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
  246. if (err)
  247. goto err;
  248. printk(KERN_INFO "L2TP ethernet pseudowire support (L2TPv3)\n");
  249. return 0;
  250. err:
  251. return err;
  252. }
  253. static void __exit l2tp_eth_exit(void)
  254. {
  255. l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
  256. }
  257. module_init(l2tp_eth_init);
  258. module_exit(l2tp_eth_exit);
  259. MODULE_LICENSE("GPL");
  260. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  261. MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
  262. MODULE_VERSION("1.0");