l2tp_eth.c 8.9 KB

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