l2tp_eth.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 "l2tp_core.h"
  32. /* Default device name. May be overridden by name specified by user */
  33. #define L2TP_ETH_DEV_NAME "l2tpeth%d"
  34. /* via netdev_priv() */
  35. struct l2tp_eth {
  36. struct net_device *dev;
  37. struct sock *tunnel_sock;
  38. struct l2tp_session *session;
  39. struct list_head list;
  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 *dev;
  50. };
  51. /* per-net private data for this module */
  52. static unsigned int l2tp_eth_net_id;
  53. struct l2tp_eth_net {
  54. struct list_head l2tp_eth_dev_list;
  55. spinlock_t l2tp_eth_lock;
  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. static int l2tp_eth_dev_init(struct net_device *dev)
  62. {
  63. struct l2tp_eth *priv = netdev_priv(dev);
  64. priv->dev = dev;
  65. eth_hw_addr_random(dev);
  66. eth_broadcast_addr(dev->broadcast);
  67. netdev_lockdep_set_classes(dev);
  68. return 0;
  69. }
  70. static void l2tp_eth_dev_uninit(struct net_device *dev)
  71. {
  72. struct l2tp_eth *priv = netdev_priv(dev);
  73. struct l2tp_eth_net *pn = l2tp_eth_pernet(dev_net(dev));
  74. spin_lock(&pn->l2tp_eth_lock);
  75. list_del_init(&priv->list);
  76. spin_unlock(&pn->l2tp_eth_lock);
  77. dev_put(dev);
  78. }
  79. static int l2tp_eth_dev_xmit(struct sk_buff *skb, struct net_device *dev)
  80. {
  81. struct l2tp_eth *priv = netdev_priv(dev);
  82. struct l2tp_session *session = priv->session;
  83. unsigned int len = skb->len;
  84. int ret = l2tp_xmit_skb(session, skb, session->hdr_len);
  85. if (likely(ret == NET_XMIT_SUCCESS)) {
  86. atomic_long_add(len, &priv->tx_bytes);
  87. atomic_long_inc(&priv->tx_packets);
  88. } else {
  89. atomic_long_inc(&priv->tx_dropped);
  90. }
  91. return NETDEV_TX_OK;
  92. }
  93. static struct rtnl_link_stats64 *l2tp_eth_get_stats64(struct net_device *dev,
  94. struct rtnl_link_stats64 *stats)
  95. {
  96. struct l2tp_eth *priv = netdev_priv(dev);
  97. stats->tx_bytes = atomic_long_read(&priv->tx_bytes);
  98. stats->tx_packets = atomic_long_read(&priv->tx_packets);
  99. stats->tx_dropped = atomic_long_read(&priv->tx_dropped);
  100. stats->rx_bytes = atomic_long_read(&priv->rx_bytes);
  101. stats->rx_packets = atomic_long_read(&priv->rx_packets);
  102. stats->rx_errors = atomic_long_read(&priv->rx_errors);
  103. return stats;
  104. }
  105. static const struct net_device_ops l2tp_eth_netdev_ops = {
  106. .ndo_init = l2tp_eth_dev_init,
  107. .ndo_uninit = l2tp_eth_dev_uninit,
  108. .ndo_start_xmit = l2tp_eth_dev_xmit,
  109. .ndo_get_stats64 = l2tp_eth_get_stats64,
  110. .ndo_set_mac_address = eth_mac_addr,
  111. };
  112. static void l2tp_eth_dev_setup(struct net_device *dev)
  113. {
  114. ether_setup(dev);
  115. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  116. dev->features |= NETIF_F_LLTX;
  117. dev->netdev_ops = &l2tp_eth_netdev_ops;
  118. dev->destructor = free_netdev;
  119. }
  120. static void l2tp_eth_dev_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
  121. {
  122. struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
  123. struct net_device *dev = spriv->dev;
  124. struct l2tp_eth *priv = netdev_priv(dev);
  125. if (session->debug & L2TP_MSG_DATA) {
  126. unsigned int length;
  127. length = min(32u, skb->len);
  128. if (!pskb_may_pull(skb, length))
  129. goto error;
  130. pr_debug("%s: eth recv\n", session->name);
  131. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
  132. }
  133. if (!pskb_may_pull(skb, ETH_HLEN))
  134. goto error;
  135. secpath_reset(skb);
  136. /* checksums verified by L2TP */
  137. skb->ip_summed = CHECKSUM_NONE;
  138. skb_dst_drop(skb);
  139. nf_reset(skb);
  140. if (dev_forward_skb(dev, skb) == NET_RX_SUCCESS) {
  141. atomic_long_inc(&priv->rx_packets);
  142. atomic_long_add(data_len, &priv->rx_bytes);
  143. } else {
  144. atomic_long_inc(&priv->rx_errors);
  145. }
  146. return;
  147. error:
  148. atomic_long_inc(&priv->rx_errors);
  149. kfree_skb(skb);
  150. }
  151. static void l2tp_eth_delete(struct l2tp_session *session)
  152. {
  153. struct l2tp_eth_sess *spriv;
  154. struct net_device *dev;
  155. if (session) {
  156. spriv = l2tp_session_priv(session);
  157. dev = spriv->dev;
  158. if (dev) {
  159. unregister_netdev(dev);
  160. spriv->dev = NULL;
  161. module_put(THIS_MODULE);
  162. }
  163. }
  164. }
  165. #if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
  166. static void l2tp_eth_show(struct seq_file *m, void *arg)
  167. {
  168. struct l2tp_session *session = arg;
  169. struct l2tp_eth_sess *spriv = l2tp_session_priv(session);
  170. struct net_device *dev = spriv->dev;
  171. seq_printf(m, " interface %s\n", dev->name);
  172. }
  173. #endif
  174. static int l2tp_eth_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
  175. {
  176. struct net_device *dev;
  177. char name[IFNAMSIZ];
  178. struct l2tp_tunnel *tunnel;
  179. struct l2tp_session *session;
  180. struct l2tp_eth *priv;
  181. struct l2tp_eth_sess *spriv;
  182. int rc;
  183. struct l2tp_eth_net *pn;
  184. tunnel = l2tp_tunnel_find(net, tunnel_id);
  185. if (!tunnel) {
  186. rc = -ENODEV;
  187. goto out;
  188. }
  189. if (cfg->ifname) {
  190. dev = dev_get_by_name(net, cfg->ifname);
  191. if (dev) {
  192. dev_put(dev);
  193. rc = -EEXIST;
  194. goto out;
  195. }
  196. strlcpy(name, cfg->ifname, IFNAMSIZ);
  197. } else
  198. strcpy(name, L2TP_ETH_DEV_NAME);
  199. session = l2tp_session_create(sizeof(*spriv), tunnel, session_id,
  200. peer_session_id, cfg);
  201. if (IS_ERR(session)) {
  202. rc = PTR_ERR(session);
  203. goto out;
  204. }
  205. dev = alloc_netdev(sizeof(*priv), name, NET_NAME_UNKNOWN,
  206. l2tp_eth_dev_setup);
  207. if (!dev) {
  208. rc = -ENOMEM;
  209. goto out_del_session;
  210. }
  211. dev_net_set(dev, net);
  212. if (session->mtu == 0)
  213. session->mtu = dev->mtu - session->hdr_len;
  214. dev->mtu = session->mtu;
  215. dev->needed_headroom += session->hdr_len;
  216. priv = netdev_priv(dev);
  217. priv->dev = dev;
  218. priv->session = session;
  219. INIT_LIST_HEAD(&priv->list);
  220. priv->tunnel_sock = tunnel->sock;
  221. session->recv_skb = l2tp_eth_dev_recv;
  222. session->session_close = l2tp_eth_delete;
  223. #if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
  224. session->show = l2tp_eth_show;
  225. #endif
  226. spriv = l2tp_session_priv(session);
  227. spriv->dev = dev;
  228. rc = register_netdev(dev);
  229. if (rc < 0)
  230. goto out_del_dev;
  231. __module_get(THIS_MODULE);
  232. /* Must be done after register_netdev() */
  233. strlcpy(session->ifname, dev->name, IFNAMSIZ);
  234. dev_hold(dev);
  235. pn = l2tp_eth_pernet(dev_net(dev));
  236. spin_lock(&pn->l2tp_eth_lock);
  237. list_add(&priv->list, &pn->l2tp_eth_dev_list);
  238. spin_unlock(&pn->l2tp_eth_lock);
  239. return 0;
  240. out_del_dev:
  241. free_netdev(dev);
  242. spriv->dev = NULL;
  243. out_del_session:
  244. l2tp_session_delete(session);
  245. out:
  246. return rc;
  247. }
  248. static __net_init int l2tp_eth_init_net(struct net *net)
  249. {
  250. struct l2tp_eth_net *pn = net_generic(net, l2tp_eth_net_id);
  251. INIT_LIST_HEAD(&pn->l2tp_eth_dev_list);
  252. spin_lock_init(&pn->l2tp_eth_lock);
  253. return 0;
  254. }
  255. static struct pernet_operations l2tp_eth_net_ops = {
  256. .init = l2tp_eth_init_net,
  257. .id = &l2tp_eth_net_id,
  258. .size = sizeof(struct l2tp_eth_net),
  259. };
  260. static const struct l2tp_nl_cmd_ops l2tp_eth_nl_cmd_ops = {
  261. .session_create = l2tp_eth_create,
  262. .session_delete = l2tp_session_delete,
  263. };
  264. static int __init l2tp_eth_init(void)
  265. {
  266. int err = 0;
  267. err = l2tp_nl_register_ops(L2TP_PWTYPE_ETH, &l2tp_eth_nl_cmd_ops);
  268. if (err)
  269. goto out;
  270. err = register_pernet_device(&l2tp_eth_net_ops);
  271. if (err)
  272. goto out_unreg;
  273. pr_info("L2TP ethernet pseudowire support (L2TPv3)\n");
  274. return 0;
  275. out_unreg:
  276. l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
  277. out:
  278. return err;
  279. }
  280. static void __exit l2tp_eth_exit(void)
  281. {
  282. unregister_pernet_device(&l2tp_eth_net_ops);
  283. l2tp_nl_unregister_ops(L2TP_PWTYPE_ETH);
  284. }
  285. module_init(l2tp_eth_init);
  286. module_exit(l2tp_eth_exit);
  287. MODULE_LICENSE("GPL");
  288. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  289. MODULE_DESCRIPTION("L2TP ethernet pseudowire driver");
  290. MODULE_VERSION("1.0");
  291. MODULE_ALIAS_L2TP_PWTYPE(5);