l2tp_eth.c 9.4 KB

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