l2tp_core.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  1. /*
  2. * L2TP core.
  3. *
  4. * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
  5. *
  6. * This file contains some code of the original L2TPv2 pppol2tp
  7. * driver, which has the following copyright:
  8. *
  9. * Authors: Martijn van Oosterhout <kleptog@svana.org>
  10. * James Chapman (jchapman@katalix.com)
  11. * Contributors:
  12. * Michal Ostrowski <mostrows@speakeasy.net>
  13. * Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
  14. * David S. Miller (davem@redhat.com)
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License version 2 as
  18. * published by the Free Software Foundation.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/module.h>
  22. #include <linux/string.h>
  23. #include <linux/list.h>
  24. #include <linux/rculist.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/kernel.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/kthread.h>
  29. #include <linux/sched.h>
  30. #include <linux/slab.h>
  31. #include <linux/errno.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/net.h>
  35. #include <linux/inetdevice.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/init.h>
  38. #include <linux/in.h>
  39. #include <linux/ip.h>
  40. #include <linux/udp.h>
  41. #include <linux/l2tp.h>
  42. #include <linux/hash.h>
  43. #include <linux/sort.h>
  44. #include <linux/file.h>
  45. #include <linux/nsproxy.h>
  46. #include <net/net_namespace.h>
  47. #include <net/netns/generic.h>
  48. #include <net/dst.h>
  49. #include <net/ip.h>
  50. #include <net/udp.h>
  51. #include <net/udp_tunnel.h>
  52. #include <net/inet_common.h>
  53. #include <net/xfrm.h>
  54. #include <net/protocol.h>
  55. #include <net/inet6_connection_sock.h>
  56. #include <net/inet_ecn.h>
  57. #include <net/ip6_route.h>
  58. #include <net/ip6_checksum.h>
  59. #include <asm/byteorder.h>
  60. #include <linux/atomic.h>
  61. #include "l2tp_core.h"
  62. #define L2TP_DRV_VERSION "V2.0"
  63. /* L2TP header constants */
  64. #define L2TP_HDRFLAG_T 0x8000
  65. #define L2TP_HDRFLAG_L 0x4000
  66. #define L2TP_HDRFLAG_S 0x0800
  67. #define L2TP_HDRFLAG_O 0x0200
  68. #define L2TP_HDRFLAG_P 0x0100
  69. #define L2TP_HDR_VER_MASK 0x000F
  70. #define L2TP_HDR_VER_2 0x0002
  71. #define L2TP_HDR_VER_3 0x0003
  72. /* L2TPv3 default L2-specific sublayer */
  73. #define L2TP_SLFLAG_S 0x40000000
  74. #define L2TP_SL_SEQ_MASK 0x00ffffff
  75. #define L2TP_HDR_SIZE_SEQ 10
  76. #define L2TP_HDR_SIZE_NOSEQ 6
  77. /* Default trace flags */
  78. #define L2TP_DEFAULT_DEBUG_FLAGS 0
  79. /* Private data stored for received packets in the skb.
  80. */
  81. struct l2tp_skb_cb {
  82. u32 ns;
  83. u16 has_seq;
  84. u16 length;
  85. unsigned long expires;
  86. };
  87. #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
  88. static struct workqueue_struct *l2tp_wq;
  89. /* per-net private data for this module */
  90. static unsigned int l2tp_net_id;
  91. struct l2tp_net {
  92. struct list_head l2tp_tunnel_list;
  93. spinlock_t l2tp_tunnel_list_lock;
  94. struct hlist_head l2tp_session_hlist[L2TP_HASH_SIZE_2];
  95. spinlock_t l2tp_session_hlist_lock;
  96. };
  97. #if IS_ENABLED(CONFIG_IPV6)
  98. static bool l2tp_sk_is_v6(struct sock *sk)
  99. {
  100. return sk->sk_family == PF_INET6 &&
  101. !ipv6_addr_v4mapped(&sk->sk_v6_daddr);
  102. }
  103. #endif
  104. static inline struct l2tp_tunnel *l2tp_tunnel(struct sock *sk)
  105. {
  106. return sk->sk_user_data;
  107. }
  108. static inline struct l2tp_net *l2tp_pernet(const struct net *net)
  109. {
  110. BUG_ON(!net);
  111. return net_generic(net, l2tp_net_id);
  112. }
  113. /* Session hash global list for L2TPv3.
  114. * The session_id SHOULD be random according to RFC3931, but several
  115. * L2TP implementations use incrementing session_ids. So we do a real
  116. * hash on the session_id, rather than a simple bitmask.
  117. */
  118. static inline struct hlist_head *
  119. l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
  120. {
  121. return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
  122. }
  123. /* Session hash list.
  124. * The session_id SHOULD be random according to RFC2661, but several
  125. * L2TP implementations (Cisco and Microsoft) use incrementing
  126. * session_ids. So we do a real hash on the session_id, rather than a
  127. * simple bitmask.
  128. */
  129. static inline struct hlist_head *
  130. l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
  131. {
  132. return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
  133. }
  134. void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
  135. {
  136. sock_put(tunnel->sock);
  137. /* the tunnel is freed in the socket destructor */
  138. }
  139. EXPORT_SYMBOL(l2tp_tunnel_free);
  140. /* Lookup a tunnel. A new reference is held on the returned tunnel. */
  141. struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id)
  142. {
  143. const struct l2tp_net *pn = l2tp_pernet(net);
  144. struct l2tp_tunnel *tunnel;
  145. rcu_read_lock_bh();
  146. list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
  147. if (tunnel->tunnel_id == tunnel_id) {
  148. l2tp_tunnel_inc_refcount(tunnel);
  149. rcu_read_unlock_bh();
  150. return tunnel;
  151. }
  152. }
  153. rcu_read_unlock_bh();
  154. return NULL;
  155. }
  156. EXPORT_SYMBOL_GPL(l2tp_tunnel_get);
  157. struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth)
  158. {
  159. const struct l2tp_net *pn = l2tp_pernet(net);
  160. struct l2tp_tunnel *tunnel;
  161. int count = 0;
  162. rcu_read_lock_bh();
  163. list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
  164. if (++count > nth) {
  165. l2tp_tunnel_inc_refcount(tunnel);
  166. rcu_read_unlock_bh();
  167. return tunnel;
  168. }
  169. }
  170. rcu_read_unlock_bh();
  171. return NULL;
  172. }
  173. EXPORT_SYMBOL_GPL(l2tp_tunnel_get_nth);
  174. struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
  175. u32 session_id)
  176. {
  177. struct hlist_head *session_list;
  178. struct l2tp_session *session;
  179. session_list = l2tp_session_id_hash(tunnel, session_id);
  180. read_lock_bh(&tunnel->hlist_lock);
  181. hlist_for_each_entry(session, session_list, hlist)
  182. if (session->session_id == session_id) {
  183. l2tp_session_inc_refcount(session);
  184. read_unlock_bh(&tunnel->hlist_lock);
  185. return session;
  186. }
  187. read_unlock_bh(&tunnel->hlist_lock);
  188. return NULL;
  189. }
  190. EXPORT_SYMBOL_GPL(l2tp_tunnel_get_session);
  191. struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id)
  192. {
  193. struct hlist_head *session_list;
  194. struct l2tp_session *session;
  195. session_list = l2tp_session_id_hash_2(l2tp_pernet(net), session_id);
  196. rcu_read_lock_bh();
  197. hlist_for_each_entry_rcu(session, session_list, global_hlist)
  198. if (session->session_id == session_id) {
  199. l2tp_session_inc_refcount(session);
  200. rcu_read_unlock_bh();
  201. return session;
  202. }
  203. rcu_read_unlock_bh();
  204. return NULL;
  205. }
  206. EXPORT_SYMBOL_GPL(l2tp_session_get);
  207. struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth)
  208. {
  209. int hash;
  210. struct l2tp_session *session;
  211. int count = 0;
  212. read_lock_bh(&tunnel->hlist_lock);
  213. for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
  214. hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
  215. if (++count > nth) {
  216. l2tp_session_inc_refcount(session);
  217. read_unlock_bh(&tunnel->hlist_lock);
  218. return session;
  219. }
  220. }
  221. }
  222. read_unlock_bh(&tunnel->hlist_lock);
  223. return NULL;
  224. }
  225. EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
  226. /* Lookup a session by interface name.
  227. * This is very inefficient but is only used by management interfaces.
  228. */
  229. struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
  230. const char *ifname)
  231. {
  232. struct l2tp_net *pn = l2tp_pernet(net);
  233. int hash;
  234. struct l2tp_session *session;
  235. rcu_read_lock_bh();
  236. for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
  237. hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
  238. if (!strcmp(session->ifname, ifname)) {
  239. l2tp_session_inc_refcount(session);
  240. rcu_read_unlock_bh();
  241. return session;
  242. }
  243. }
  244. }
  245. rcu_read_unlock_bh();
  246. return NULL;
  247. }
  248. EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
  249. int l2tp_session_register(struct l2tp_session *session,
  250. struct l2tp_tunnel *tunnel)
  251. {
  252. struct l2tp_session *session_walk;
  253. struct hlist_head *g_head;
  254. struct hlist_head *head;
  255. struct l2tp_net *pn;
  256. int err;
  257. head = l2tp_session_id_hash(tunnel, session->session_id);
  258. write_lock_bh(&tunnel->hlist_lock);
  259. if (!tunnel->acpt_newsess) {
  260. err = -ENODEV;
  261. goto err_tlock;
  262. }
  263. hlist_for_each_entry(session_walk, head, hlist)
  264. if (session_walk->session_id == session->session_id) {
  265. err = -EEXIST;
  266. goto err_tlock;
  267. }
  268. if (tunnel->version == L2TP_HDR_VER_3) {
  269. pn = l2tp_pernet(tunnel->l2tp_net);
  270. g_head = l2tp_session_id_hash_2(pn, session->session_id);
  271. spin_lock_bh(&pn->l2tp_session_hlist_lock);
  272. hlist_for_each_entry(session_walk, g_head, global_hlist)
  273. if (session_walk->session_id == session->session_id) {
  274. err = -EEXIST;
  275. goto err_tlock_pnlock;
  276. }
  277. l2tp_tunnel_inc_refcount(tunnel);
  278. hlist_add_head_rcu(&session->global_hlist, g_head);
  279. spin_unlock_bh(&pn->l2tp_session_hlist_lock);
  280. } else {
  281. l2tp_tunnel_inc_refcount(tunnel);
  282. }
  283. hlist_add_head(&session->hlist, head);
  284. write_unlock_bh(&tunnel->hlist_lock);
  285. return 0;
  286. err_tlock_pnlock:
  287. spin_unlock_bh(&pn->l2tp_session_hlist_lock);
  288. err_tlock:
  289. write_unlock_bh(&tunnel->hlist_lock);
  290. return err;
  291. }
  292. EXPORT_SYMBOL_GPL(l2tp_session_register);
  293. /*****************************************************************************
  294. * Receive data handling
  295. *****************************************************************************/
  296. /* Queue a skb in order. We come here only if the skb has an L2TP sequence
  297. * number.
  298. */
  299. static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
  300. {
  301. struct sk_buff *skbp;
  302. struct sk_buff *tmp;
  303. u32 ns = L2TP_SKB_CB(skb)->ns;
  304. spin_lock_bh(&session->reorder_q.lock);
  305. skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
  306. if (L2TP_SKB_CB(skbp)->ns > ns) {
  307. __skb_queue_before(&session->reorder_q, skbp, skb);
  308. l2tp_dbg(session, L2TP_MSG_SEQ,
  309. "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
  310. session->name, ns, L2TP_SKB_CB(skbp)->ns,
  311. skb_queue_len(&session->reorder_q));
  312. atomic_long_inc(&session->stats.rx_oos_packets);
  313. goto out;
  314. }
  315. }
  316. __skb_queue_tail(&session->reorder_q, skb);
  317. out:
  318. spin_unlock_bh(&session->reorder_q.lock);
  319. }
  320. /* Dequeue a single skb.
  321. */
  322. static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
  323. {
  324. struct l2tp_tunnel *tunnel = session->tunnel;
  325. int length = L2TP_SKB_CB(skb)->length;
  326. /* We're about to requeue the skb, so return resources
  327. * to its current owner (a socket receive buffer).
  328. */
  329. skb_orphan(skb);
  330. atomic_long_inc(&tunnel->stats.rx_packets);
  331. atomic_long_add(length, &tunnel->stats.rx_bytes);
  332. atomic_long_inc(&session->stats.rx_packets);
  333. atomic_long_add(length, &session->stats.rx_bytes);
  334. if (L2TP_SKB_CB(skb)->has_seq) {
  335. /* Bump our Nr */
  336. session->nr++;
  337. session->nr &= session->nr_max;
  338. l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
  339. session->name, session->nr);
  340. }
  341. /* call private receive handler */
  342. if (session->recv_skb != NULL)
  343. (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
  344. else
  345. kfree_skb(skb);
  346. }
  347. /* Dequeue skbs from the session's reorder_q, subject to packet order.
  348. * Skbs that have been in the queue for too long are simply discarded.
  349. */
  350. static void l2tp_recv_dequeue(struct l2tp_session *session)
  351. {
  352. struct sk_buff *skb;
  353. struct sk_buff *tmp;
  354. /* If the pkt at the head of the queue has the nr that we
  355. * expect to send up next, dequeue it and any other
  356. * in-sequence packets behind it.
  357. */
  358. start:
  359. spin_lock_bh(&session->reorder_q.lock);
  360. skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
  361. if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
  362. atomic_long_inc(&session->stats.rx_seq_discards);
  363. atomic_long_inc(&session->stats.rx_errors);
  364. l2tp_dbg(session, L2TP_MSG_SEQ,
  365. "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
  366. session->name, L2TP_SKB_CB(skb)->ns,
  367. L2TP_SKB_CB(skb)->length, session->nr,
  368. skb_queue_len(&session->reorder_q));
  369. session->reorder_skip = 1;
  370. __skb_unlink(skb, &session->reorder_q);
  371. kfree_skb(skb);
  372. continue;
  373. }
  374. if (L2TP_SKB_CB(skb)->has_seq) {
  375. if (session->reorder_skip) {
  376. l2tp_dbg(session, L2TP_MSG_SEQ,
  377. "%s: advancing nr to next pkt: %u -> %u",
  378. session->name, session->nr,
  379. L2TP_SKB_CB(skb)->ns);
  380. session->reorder_skip = 0;
  381. session->nr = L2TP_SKB_CB(skb)->ns;
  382. }
  383. if (L2TP_SKB_CB(skb)->ns != session->nr) {
  384. l2tp_dbg(session, L2TP_MSG_SEQ,
  385. "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
  386. session->name, L2TP_SKB_CB(skb)->ns,
  387. L2TP_SKB_CB(skb)->length, session->nr,
  388. skb_queue_len(&session->reorder_q));
  389. goto out;
  390. }
  391. }
  392. __skb_unlink(skb, &session->reorder_q);
  393. /* Process the skb. We release the queue lock while we
  394. * do so to let other contexts process the queue.
  395. */
  396. spin_unlock_bh(&session->reorder_q.lock);
  397. l2tp_recv_dequeue_skb(session, skb);
  398. goto start;
  399. }
  400. out:
  401. spin_unlock_bh(&session->reorder_q.lock);
  402. }
  403. static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
  404. {
  405. u32 nws;
  406. if (nr >= session->nr)
  407. nws = nr - session->nr;
  408. else
  409. nws = (session->nr_max + 1) - (session->nr - nr);
  410. return nws < session->nr_window_size;
  411. }
  412. /* If packet has sequence numbers, queue it if acceptable. Returns 0 if
  413. * acceptable, else non-zero.
  414. */
  415. static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
  416. {
  417. if (!l2tp_seq_check_rx_window(session, L2TP_SKB_CB(skb)->ns)) {
  418. /* Packet sequence number is outside allowed window.
  419. * Discard it.
  420. */
  421. l2tp_dbg(session, L2TP_MSG_SEQ,
  422. "%s: pkt %u len %d discarded, outside window, nr=%u\n",
  423. session->name, L2TP_SKB_CB(skb)->ns,
  424. L2TP_SKB_CB(skb)->length, session->nr);
  425. goto discard;
  426. }
  427. if (session->reorder_timeout != 0) {
  428. /* Packet reordering enabled. Add skb to session's
  429. * reorder queue, in order of ns.
  430. */
  431. l2tp_recv_queue_skb(session, skb);
  432. goto out;
  433. }
  434. /* Packet reordering disabled. Discard out-of-sequence packets, while
  435. * tracking the number if in-sequence packets after the first OOS packet
  436. * is seen. After nr_oos_count_max in-sequence packets, reset the
  437. * sequence number to re-enable packet reception.
  438. */
  439. if (L2TP_SKB_CB(skb)->ns == session->nr) {
  440. skb_queue_tail(&session->reorder_q, skb);
  441. } else {
  442. u32 nr_oos = L2TP_SKB_CB(skb)->ns;
  443. u32 nr_next = (session->nr_oos + 1) & session->nr_max;
  444. if (nr_oos == nr_next)
  445. session->nr_oos_count++;
  446. else
  447. session->nr_oos_count = 0;
  448. session->nr_oos = nr_oos;
  449. if (session->nr_oos_count > session->nr_oos_count_max) {
  450. session->reorder_skip = 1;
  451. l2tp_dbg(session, L2TP_MSG_SEQ,
  452. "%s: %d oos packets received. Resetting sequence numbers\n",
  453. session->name, session->nr_oos_count);
  454. }
  455. if (!session->reorder_skip) {
  456. atomic_long_inc(&session->stats.rx_seq_discards);
  457. l2tp_dbg(session, L2TP_MSG_SEQ,
  458. "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
  459. session->name, L2TP_SKB_CB(skb)->ns,
  460. L2TP_SKB_CB(skb)->length, session->nr,
  461. skb_queue_len(&session->reorder_q));
  462. goto discard;
  463. }
  464. skb_queue_tail(&session->reorder_q, skb);
  465. }
  466. out:
  467. return 0;
  468. discard:
  469. return 1;
  470. }
  471. /* Do receive processing of L2TP data frames. We handle both L2TPv2
  472. * and L2TPv3 data frames here.
  473. *
  474. * L2TPv2 Data Message Header
  475. *
  476. * 0 1 2 3
  477. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  478. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  479. * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
  480. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  481. * | Tunnel ID | Session ID |
  482. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  483. * | Ns (opt) | Nr (opt) |
  484. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  485. * | Offset Size (opt) | Offset pad... (opt)
  486. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  487. *
  488. * Data frames are marked by T=0. All other fields are the same as
  489. * those in L2TP control frames.
  490. *
  491. * L2TPv3 Data Message Header
  492. *
  493. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  494. * | L2TP Session Header |
  495. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  496. * | L2-Specific Sublayer |
  497. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  498. * | Tunnel Payload ...
  499. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  500. *
  501. * L2TPv3 Session Header Over IP
  502. *
  503. * 0 1 2 3
  504. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  505. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  506. * | Session ID |
  507. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  508. * | Cookie (optional, maximum 64 bits)...
  509. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  510. * |
  511. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  512. *
  513. * L2TPv3 L2-Specific Sublayer Format
  514. *
  515. * 0 1 2 3
  516. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  517. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  518. * |x|S|x|x|x|x|x|x| Sequence Number |
  519. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  520. *
  521. * Cookie value and sublayer format are negotiated with the peer when
  522. * the session is set up. Unlike L2TPv2, we do not need to parse the
  523. * packet header to determine if optional fields are present.
  524. *
  525. * Caller must already have parsed the frame and determined that it is
  526. * a data (not control) frame before coming here. Fields up to the
  527. * session-id have already been parsed and ptr points to the data
  528. * after the session-id.
  529. */
  530. void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
  531. unsigned char *ptr, unsigned char *optr, u16 hdrflags,
  532. int length)
  533. {
  534. struct l2tp_tunnel *tunnel = session->tunnel;
  535. int offset;
  536. u32 ns, nr;
  537. /* Parse and check optional cookie */
  538. if (session->peer_cookie_len > 0) {
  539. if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
  540. l2tp_info(tunnel, L2TP_MSG_DATA,
  541. "%s: cookie mismatch (%u/%u). Discarding.\n",
  542. tunnel->name, tunnel->tunnel_id,
  543. session->session_id);
  544. atomic_long_inc(&session->stats.rx_cookie_discards);
  545. goto discard;
  546. }
  547. ptr += session->peer_cookie_len;
  548. }
  549. /* Handle the optional sequence numbers. Sequence numbers are
  550. * in different places for L2TPv2 and L2TPv3.
  551. *
  552. * If we are the LAC, enable/disable sequence numbers under
  553. * the control of the LNS. If no sequence numbers present but
  554. * we were expecting them, discard frame.
  555. */
  556. ns = nr = 0;
  557. L2TP_SKB_CB(skb)->has_seq = 0;
  558. if (tunnel->version == L2TP_HDR_VER_2) {
  559. if (hdrflags & L2TP_HDRFLAG_S) {
  560. ns = ntohs(*(__be16 *) ptr);
  561. ptr += 2;
  562. nr = ntohs(*(__be16 *) ptr);
  563. ptr += 2;
  564. /* Store L2TP info in the skb */
  565. L2TP_SKB_CB(skb)->ns = ns;
  566. L2TP_SKB_CB(skb)->has_seq = 1;
  567. l2tp_dbg(session, L2TP_MSG_SEQ,
  568. "%s: recv data ns=%u, nr=%u, session nr=%u\n",
  569. session->name, ns, nr, session->nr);
  570. }
  571. } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
  572. u32 l2h = ntohl(*(__be32 *) ptr);
  573. if (l2h & 0x40000000) {
  574. ns = l2h & 0x00ffffff;
  575. /* Store L2TP info in the skb */
  576. L2TP_SKB_CB(skb)->ns = ns;
  577. L2TP_SKB_CB(skb)->has_seq = 1;
  578. l2tp_dbg(session, L2TP_MSG_SEQ,
  579. "%s: recv data ns=%u, session nr=%u\n",
  580. session->name, ns, session->nr);
  581. }
  582. ptr += 4;
  583. }
  584. if (L2TP_SKB_CB(skb)->has_seq) {
  585. /* Received a packet with sequence numbers. If we're the LNS,
  586. * check if we sre sending sequence numbers and if not,
  587. * configure it so.
  588. */
  589. if ((!session->lns_mode) && (!session->send_seq)) {
  590. l2tp_info(session, L2TP_MSG_SEQ,
  591. "%s: requested to enable seq numbers by LNS\n",
  592. session->name);
  593. session->send_seq = 1;
  594. l2tp_session_set_header_len(session, tunnel->version);
  595. }
  596. } else {
  597. /* No sequence numbers.
  598. * If user has configured mandatory sequence numbers, discard.
  599. */
  600. if (session->recv_seq) {
  601. l2tp_warn(session, L2TP_MSG_SEQ,
  602. "%s: recv data has no seq numbers when required. Discarding.\n",
  603. session->name);
  604. atomic_long_inc(&session->stats.rx_seq_discards);
  605. goto discard;
  606. }
  607. /* If we're the LAC and we're sending sequence numbers, the
  608. * LNS has requested that we no longer send sequence numbers.
  609. * If we're the LNS and we're sending sequence numbers, the
  610. * LAC is broken. Discard the frame.
  611. */
  612. if ((!session->lns_mode) && (session->send_seq)) {
  613. l2tp_info(session, L2TP_MSG_SEQ,
  614. "%s: requested to disable seq numbers by LNS\n",
  615. session->name);
  616. session->send_seq = 0;
  617. l2tp_session_set_header_len(session, tunnel->version);
  618. } else if (session->send_seq) {
  619. l2tp_warn(session, L2TP_MSG_SEQ,
  620. "%s: recv data has no seq numbers when required. Discarding.\n",
  621. session->name);
  622. atomic_long_inc(&session->stats.rx_seq_discards);
  623. goto discard;
  624. }
  625. }
  626. /* Session data offset is defined only for L2TPv2 and is
  627. * indicated by an optional 16-bit value in the header.
  628. */
  629. if (tunnel->version == L2TP_HDR_VER_2) {
  630. /* If offset bit set, skip it. */
  631. if (hdrflags & L2TP_HDRFLAG_O) {
  632. offset = ntohs(*(__be16 *)ptr);
  633. ptr += 2 + offset;
  634. }
  635. }
  636. offset = ptr - optr;
  637. if (!pskb_may_pull(skb, offset))
  638. goto discard;
  639. __skb_pull(skb, offset);
  640. /* Prepare skb for adding to the session's reorder_q. Hold
  641. * packets for max reorder_timeout or 1 second if not
  642. * reordering.
  643. */
  644. L2TP_SKB_CB(skb)->length = length;
  645. L2TP_SKB_CB(skb)->expires = jiffies +
  646. (session->reorder_timeout ? session->reorder_timeout : HZ);
  647. /* Add packet to the session's receive queue. Reordering is done here, if
  648. * enabled. Saved L2TP protocol info is stored in skb->sb[].
  649. */
  650. if (L2TP_SKB_CB(skb)->has_seq) {
  651. if (l2tp_recv_data_seq(session, skb))
  652. goto discard;
  653. } else {
  654. /* No sequence numbers. Add the skb to the tail of the
  655. * reorder queue. This ensures that it will be
  656. * delivered after all previous sequenced skbs.
  657. */
  658. skb_queue_tail(&session->reorder_q, skb);
  659. }
  660. /* Try to dequeue as many skbs from reorder_q as we can. */
  661. l2tp_recv_dequeue(session);
  662. return;
  663. discard:
  664. atomic_long_inc(&session->stats.rx_errors);
  665. kfree_skb(skb);
  666. }
  667. EXPORT_SYMBOL(l2tp_recv_common);
  668. /* Drop skbs from the session's reorder_q
  669. */
  670. static int l2tp_session_queue_purge(struct l2tp_session *session)
  671. {
  672. struct sk_buff *skb = NULL;
  673. BUG_ON(!session);
  674. BUG_ON(session->magic != L2TP_SESSION_MAGIC);
  675. while ((skb = skb_dequeue(&session->reorder_q))) {
  676. atomic_long_inc(&session->stats.rx_errors);
  677. kfree_skb(skb);
  678. }
  679. return 0;
  680. }
  681. /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
  682. * here. The skb is not on a list when we get here.
  683. * Returns 0 if the packet was a data packet and was successfully passed on.
  684. * Returns 1 if the packet was not a good data packet and could not be
  685. * forwarded. All such packets are passed up to userspace to deal with.
  686. */
  687. static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb)
  688. {
  689. struct l2tp_session *session = NULL;
  690. unsigned char *ptr, *optr;
  691. u16 hdrflags;
  692. u32 tunnel_id, session_id;
  693. u16 version;
  694. int length;
  695. /* UDP has verifed checksum */
  696. /* UDP always verifies the packet length. */
  697. __skb_pull(skb, sizeof(struct udphdr));
  698. /* Short packet? */
  699. if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
  700. l2tp_info(tunnel, L2TP_MSG_DATA,
  701. "%s: recv short packet (len=%d)\n",
  702. tunnel->name, skb->len);
  703. goto error;
  704. }
  705. /* Trace packet contents, if enabled */
  706. if (tunnel->debug & L2TP_MSG_DATA) {
  707. length = min(32u, skb->len);
  708. if (!pskb_may_pull(skb, length))
  709. goto error;
  710. pr_debug("%s: recv\n", tunnel->name);
  711. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
  712. }
  713. /* Point to L2TP header */
  714. optr = ptr = skb->data;
  715. /* Get L2TP header flags */
  716. hdrflags = ntohs(*(__be16 *) ptr);
  717. /* Check protocol version */
  718. version = hdrflags & L2TP_HDR_VER_MASK;
  719. if (version != tunnel->version) {
  720. l2tp_info(tunnel, L2TP_MSG_DATA,
  721. "%s: recv protocol version mismatch: got %d expected %d\n",
  722. tunnel->name, version, tunnel->version);
  723. goto error;
  724. }
  725. /* Get length of L2TP packet */
  726. length = skb->len;
  727. /* If type is control packet, it is handled by userspace. */
  728. if (hdrflags & L2TP_HDRFLAG_T) {
  729. l2tp_dbg(tunnel, L2TP_MSG_DATA,
  730. "%s: recv control packet, len=%d\n",
  731. tunnel->name, length);
  732. goto error;
  733. }
  734. /* Skip flags */
  735. ptr += 2;
  736. if (tunnel->version == L2TP_HDR_VER_2) {
  737. /* If length is present, skip it */
  738. if (hdrflags & L2TP_HDRFLAG_L)
  739. ptr += 2;
  740. /* Extract tunnel and session ID */
  741. tunnel_id = ntohs(*(__be16 *) ptr);
  742. ptr += 2;
  743. session_id = ntohs(*(__be16 *) ptr);
  744. ptr += 2;
  745. } else {
  746. ptr += 2; /* skip reserved bits */
  747. tunnel_id = tunnel->tunnel_id;
  748. session_id = ntohl(*(__be32 *) ptr);
  749. ptr += 4;
  750. }
  751. /* Find the session context */
  752. session = l2tp_tunnel_get_session(tunnel, session_id);
  753. if (!session || !session->recv_skb) {
  754. if (session)
  755. l2tp_session_dec_refcount(session);
  756. /* Not found? Pass to userspace to deal with */
  757. l2tp_info(tunnel, L2TP_MSG_DATA,
  758. "%s: no session found (%u/%u). Passing up.\n",
  759. tunnel->name, tunnel_id, session_id);
  760. goto error;
  761. }
  762. l2tp_recv_common(session, skb, ptr, optr, hdrflags, length);
  763. l2tp_session_dec_refcount(session);
  764. return 0;
  765. error:
  766. /* Put UDP header back */
  767. __skb_push(skb, sizeof(struct udphdr));
  768. return 1;
  769. }
  770. /* UDP encapsulation receive handler. See net/ipv4/udp.c.
  771. * Return codes:
  772. * 0 : success.
  773. * <0: error
  774. * >0: skb should be passed up to userspace as UDP.
  775. */
  776. int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  777. {
  778. struct l2tp_tunnel *tunnel;
  779. tunnel = l2tp_tunnel(sk);
  780. if (tunnel == NULL)
  781. goto pass_up;
  782. l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
  783. tunnel->name, skb->len);
  784. if (l2tp_udp_recv_core(tunnel, skb))
  785. goto pass_up;
  786. return 0;
  787. pass_up:
  788. return 1;
  789. }
  790. EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
  791. /************************************************************************
  792. * Transmit handling
  793. ***********************************************************************/
  794. /* Build an L2TP header for the session into the buffer provided.
  795. */
  796. static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
  797. {
  798. struct l2tp_tunnel *tunnel = session->tunnel;
  799. __be16 *bufp = buf;
  800. __be16 *optr = buf;
  801. u16 flags = L2TP_HDR_VER_2;
  802. u32 tunnel_id = tunnel->peer_tunnel_id;
  803. u32 session_id = session->peer_session_id;
  804. if (session->send_seq)
  805. flags |= L2TP_HDRFLAG_S;
  806. /* Setup L2TP header. */
  807. *bufp++ = htons(flags);
  808. *bufp++ = htons(tunnel_id);
  809. *bufp++ = htons(session_id);
  810. if (session->send_seq) {
  811. *bufp++ = htons(session->ns);
  812. *bufp++ = 0;
  813. session->ns++;
  814. session->ns &= 0xffff;
  815. l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
  816. session->name, session->ns);
  817. }
  818. return bufp - optr;
  819. }
  820. static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
  821. {
  822. struct l2tp_tunnel *tunnel = session->tunnel;
  823. char *bufp = buf;
  824. char *optr = bufp;
  825. /* Setup L2TP header. The header differs slightly for UDP and
  826. * IP encapsulations. For UDP, there is 4 bytes of flags.
  827. */
  828. if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
  829. u16 flags = L2TP_HDR_VER_3;
  830. *((__be16 *) bufp) = htons(flags);
  831. bufp += 2;
  832. *((__be16 *) bufp) = 0;
  833. bufp += 2;
  834. }
  835. *((__be32 *) bufp) = htonl(session->peer_session_id);
  836. bufp += 4;
  837. if (session->cookie_len) {
  838. memcpy(bufp, &session->cookie[0], session->cookie_len);
  839. bufp += session->cookie_len;
  840. }
  841. if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
  842. u32 l2h = 0;
  843. if (session->send_seq) {
  844. l2h = 0x40000000 | session->ns;
  845. session->ns++;
  846. session->ns &= 0xffffff;
  847. l2tp_dbg(session, L2TP_MSG_SEQ,
  848. "%s: updated ns to %u\n",
  849. session->name, session->ns);
  850. }
  851. *((__be32 *)bufp) = htonl(l2h);
  852. bufp += 4;
  853. }
  854. return bufp - optr;
  855. }
  856. static void l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
  857. struct flowi *fl, size_t data_len)
  858. {
  859. struct l2tp_tunnel *tunnel = session->tunnel;
  860. unsigned int len = skb->len;
  861. int error;
  862. /* Debug */
  863. if (session->send_seq)
  864. l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes, ns=%u\n",
  865. session->name, data_len, session->ns - 1);
  866. else
  867. l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %zd bytes\n",
  868. session->name, data_len);
  869. if (session->debug & L2TP_MSG_DATA) {
  870. int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
  871. unsigned char *datap = skb->data + uhlen;
  872. pr_debug("%s: xmit\n", session->name);
  873. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
  874. datap, min_t(size_t, 32, len - uhlen));
  875. }
  876. /* Queue the packet to IP for output */
  877. skb->ignore_df = 1;
  878. #if IS_ENABLED(CONFIG_IPV6)
  879. if (l2tp_sk_is_v6(tunnel->sock))
  880. error = inet6_csk_xmit(tunnel->sock, skb, NULL);
  881. else
  882. #endif
  883. error = ip_queue_xmit(tunnel->sock, skb, fl);
  884. /* Update stats */
  885. if (error >= 0) {
  886. atomic_long_inc(&tunnel->stats.tx_packets);
  887. atomic_long_add(len, &tunnel->stats.tx_bytes);
  888. atomic_long_inc(&session->stats.tx_packets);
  889. atomic_long_add(len, &session->stats.tx_bytes);
  890. } else {
  891. atomic_long_inc(&tunnel->stats.tx_errors);
  892. atomic_long_inc(&session->stats.tx_errors);
  893. }
  894. }
  895. /* If caller requires the skb to have a ppp header, the header must be
  896. * inserted in the skb data before calling this function.
  897. */
  898. int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
  899. {
  900. int data_len = skb->len;
  901. struct l2tp_tunnel *tunnel = session->tunnel;
  902. struct sock *sk = tunnel->sock;
  903. struct flowi *fl;
  904. struct udphdr *uh;
  905. struct inet_sock *inet;
  906. int headroom;
  907. int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
  908. int udp_len;
  909. int ret = NET_XMIT_SUCCESS;
  910. /* Check that there's enough headroom in the skb to insert IP,
  911. * UDP and L2TP headers. If not enough, expand it to
  912. * make room. Adjust truesize.
  913. */
  914. headroom = NET_SKB_PAD + sizeof(struct iphdr) +
  915. uhlen + hdr_len;
  916. if (skb_cow_head(skb, headroom)) {
  917. kfree_skb(skb);
  918. return NET_XMIT_DROP;
  919. }
  920. /* Setup L2TP header */
  921. session->build_header(session, __skb_push(skb, hdr_len));
  922. /* Reset skb netfilter state */
  923. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  924. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  925. IPSKB_REROUTED);
  926. nf_reset(skb);
  927. bh_lock_sock(sk);
  928. if (sock_owned_by_user(sk)) {
  929. kfree_skb(skb);
  930. ret = NET_XMIT_DROP;
  931. goto out_unlock;
  932. }
  933. /* The user-space may change the connection status for the user-space
  934. * provided socket at run time: we must check it under the socket lock
  935. */
  936. if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) {
  937. kfree_skb(skb);
  938. ret = NET_XMIT_DROP;
  939. goto out_unlock;
  940. }
  941. /* Get routing info from the tunnel socket */
  942. skb_dst_drop(skb);
  943. skb_dst_set(skb, sk_dst_check(sk, 0));
  944. inet = inet_sk(sk);
  945. fl = &inet->cork.fl;
  946. switch (tunnel->encap) {
  947. case L2TP_ENCAPTYPE_UDP:
  948. /* Setup UDP header */
  949. __skb_push(skb, sizeof(*uh));
  950. skb_reset_transport_header(skb);
  951. uh = udp_hdr(skb);
  952. uh->source = inet->inet_sport;
  953. uh->dest = inet->inet_dport;
  954. udp_len = uhlen + hdr_len + data_len;
  955. uh->len = htons(udp_len);
  956. /* Calculate UDP checksum if configured to do so */
  957. #if IS_ENABLED(CONFIG_IPV6)
  958. if (l2tp_sk_is_v6(sk))
  959. udp6_set_csum(udp_get_no_check6_tx(sk),
  960. skb, &inet6_sk(sk)->saddr,
  961. &sk->sk_v6_daddr, udp_len);
  962. else
  963. #endif
  964. udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
  965. inet->inet_daddr, udp_len);
  966. break;
  967. case L2TP_ENCAPTYPE_IP:
  968. break;
  969. }
  970. l2tp_xmit_core(session, skb, fl, data_len);
  971. out_unlock:
  972. bh_unlock_sock(sk);
  973. return ret;
  974. }
  975. EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
  976. /*****************************************************************************
  977. * Tinnel and session create/destroy.
  978. *****************************************************************************/
  979. /* Tunnel socket destruct hook.
  980. * The tunnel context is deleted only when all session sockets have been
  981. * closed.
  982. */
  983. static void l2tp_tunnel_destruct(struct sock *sk)
  984. {
  985. struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
  986. if (tunnel == NULL)
  987. goto end;
  988. l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
  989. /* Disable udp encapsulation */
  990. switch (tunnel->encap) {
  991. case L2TP_ENCAPTYPE_UDP:
  992. /* No longer an encapsulation socket. See net/ipv4/udp.c */
  993. (udp_sk(sk))->encap_type = 0;
  994. (udp_sk(sk))->encap_rcv = NULL;
  995. (udp_sk(sk))->encap_destroy = NULL;
  996. break;
  997. case L2TP_ENCAPTYPE_IP:
  998. break;
  999. }
  1000. /* Remove hooks into tunnel socket */
  1001. sk->sk_destruct = tunnel->old_sk_destruct;
  1002. sk->sk_user_data = NULL;
  1003. /* Call the original destructor */
  1004. if (sk->sk_destruct)
  1005. (*sk->sk_destruct)(sk);
  1006. kfree_rcu(tunnel, rcu);
  1007. end:
  1008. return;
  1009. }
  1010. /* When the tunnel is closed, all the attached sessions need to go too.
  1011. */
  1012. static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
  1013. {
  1014. int hash;
  1015. struct hlist_node *walk;
  1016. struct hlist_node *tmp;
  1017. struct l2tp_session *session;
  1018. BUG_ON(tunnel == NULL);
  1019. l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
  1020. tunnel->name);
  1021. write_lock_bh(&tunnel->hlist_lock);
  1022. tunnel->acpt_newsess = false;
  1023. for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
  1024. again:
  1025. hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
  1026. session = hlist_entry(walk, struct l2tp_session, hlist);
  1027. l2tp_info(session, L2TP_MSG_CONTROL,
  1028. "%s: closing session\n", session->name);
  1029. hlist_del_init(&session->hlist);
  1030. if (test_and_set_bit(0, &session->dead))
  1031. goto again;
  1032. write_unlock_bh(&tunnel->hlist_lock);
  1033. __l2tp_session_unhash(session);
  1034. l2tp_session_queue_purge(session);
  1035. if (session->session_close != NULL)
  1036. (*session->session_close)(session);
  1037. l2tp_session_dec_refcount(session);
  1038. write_lock_bh(&tunnel->hlist_lock);
  1039. /* Now restart from the beginning of this hash
  1040. * chain. We always remove a session from the
  1041. * list so we are guaranteed to make forward
  1042. * progress.
  1043. */
  1044. goto again;
  1045. }
  1046. }
  1047. write_unlock_bh(&tunnel->hlist_lock);
  1048. }
  1049. /* Tunnel socket destroy hook for UDP encapsulation */
  1050. static void l2tp_udp_encap_destroy(struct sock *sk)
  1051. {
  1052. struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
  1053. if (tunnel)
  1054. l2tp_tunnel_delete(tunnel);
  1055. }
  1056. /* Workqueue tunnel deletion function */
  1057. static void l2tp_tunnel_del_work(struct work_struct *work)
  1058. {
  1059. struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel,
  1060. del_work);
  1061. struct sock *sk = tunnel->sock;
  1062. struct socket *sock = sk->sk_socket;
  1063. struct l2tp_net *pn;
  1064. l2tp_tunnel_closeall(tunnel);
  1065. /* If the tunnel socket was created within the kernel, use
  1066. * the sk API to release it here.
  1067. */
  1068. if (tunnel->fd < 0) {
  1069. if (sock) {
  1070. kernel_sock_shutdown(sock, SHUT_RDWR);
  1071. sock_release(sock);
  1072. }
  1073. }
  1074. /* Remove the tunnel struct from the tunnel list */
  1075. pn = l2tp_pernet(tunnel->l2tp_net);
  1076. spin_lock_bh(&pn->l2tp_tunnel_list_lock);
  1077. list_del_rcu(&tunnel->list);
  1078. spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
  1079. /* drop initial ref */
  1080. l2tp_tunnel_dec_refcount(tunnel);
  1081. /* drop workqueue ref */
  1082. l2tp_tunnel_dec_refcount(tunnel);
  1083. }
  1084. /* Create a socket for the tunnel, if one isn't set up by
  1085. * userspace. This is used for static tunnels where there is no
  1086. * managing L2TP daemon.
  1087. *
  1088. * Since we don't want these sockets to keep a namespace alive by
  1089. * themselves, we drop the socket's namespace refcount after creation.
  1090. * These sockets are freed when the namespace exits using the pernet
  1091. * exit hook.
  1092. */
  1093. static int l2tp_tunnel_sock_create(struct net *net,
  1094. u32 tunnel_id,
  1095. u32 peer_tunnel_id,
  1096. struct l2tp_tunnel_cfg *cfg,
  1097. struct socket **sockp)
  1098. {
  1099. int err = -EINVAL;
  1100. struct socket *sock = NULL;
  1101. struct udp_port_cfg udp_conf;
  1102. switch (cfg->encap) {
  1103. case L2TP_ENCAPTYPE_UDP:
  1104. memset(&udp_conf, 0, sizeof(udp_conf));
  1105. #if IS_ENABLED(CONFIG_IPV6)
  1106. if (cfg->local_ip6 && cfg->peer_ip6) {
  1107. udp_conf.family = AF_INET6;
  1108. memcpy(&udp_conf.local_ip6, cfg->local_ip6,
  1109. sizeof(udp_conf.local_ip6));
  1110. memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
  1111. sizeof(udp_conf.peer_ip6));
  1112. udp_conf.use_udp6_tx_checksums =
  1113. ! cfg->udp6_zero_tx_checksums;
  1114. udp_conf.use_udp6_rx_checksums =
  1115. ! cfg->udp6_zero_rx_checksums;
  1116. } else
  1117. #endif
  1118. {
  1119. udp_conf.family = AF_INET;
  1120. udp_conf.local_ip = cfg->local_ip;
  1121. udp_conf.peer_ip = cfg->peer_ip;
  1122. udp_conf.use_udp_checksums = cfg->use_udp_checksums;
  1123. }
  1124. udp_conf.local_udp_port = htons(cfg->local_udp_port);
  1125. udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
  1126. err = udp_sock_create(net, &udp_conf, &sock);
  1127. if (err < 0)
  1128. goto out;
  1129. break;
  1130. case L2TP_ENCAPTYPE_IP:
  1131. #if IS_ENABLED(CONFIG_IPV6)
  1132. if (cfg->local_ip6 && cfg->peer_ip6) {
  1133. struct sockaddr_l2tpip6 ip6_addr = {0};
  1134. err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
  1135. IPPROTO_L2TP, &sock);
  1136. if (err < 0)
  1137. goto out;
  1138. ip6_addr.l2tp_family = AF_INET6;
  1139. memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
  1140. sizeof(ip6_addr.l2tp_addr));
  1141. ip6_addr.l2tp_conn_id = tunnel_id;
  1142. err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
  1143. sizeof(ip6_addr));
  1144. if (err < 0)
  1145. goto out;
  1146. ip6_addr.l2tp_family = AF_INET6;
  1147. memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
  1148. sizeof(ip6_addr.l2tp_addr));
  1149. ip6_addr.l2tp_conn_id = peer_tunnel_id;
  1150. err = kernel_connect(sock,
  1151. (struct sockaddr *) &ip6_addr,
  1152. sizeof(ip6_addr), 0);
  1153. if (err < 0)
  1154. goto out;
  1155. } else
  1156. #endif
  1157. {
  1158. struct sockaddr_l2tpip ip_addr = {0};
  1159. err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
  1160. IPPROTO_L2TP, &sock);
  1161. if (err < 0)
  1162. goto out;
  1163. ip_addr.l2tp_family = AF_INET;
  1164. ip_addr.l2tp_addr = cfg->local_ip;
  1165. ip_addr.l2tp_conn_id = tunnel_id;
  1166. err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
  1167. sizeof(ip_addr));
  1168. if (err < 0)
  1169. goto out;
  1170. ip_addr.l2tp_family = AF_INET;
  1171. ip_addr.l2tp_addr = cfg->peer_ip;
  1172. ip_addr.l2tp_conn_id = peer_tunnel_id;
  1173. err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
  1174. sizeof(ip_addr), 0);
  1175. if (err < 0)
  1176. goto out;
  1177. }
  1178. break;
  1179. default:
  1180. goto out;
  1181. }
  1182. out:
  1183. *sockp = sock;
  1184. if ((err < 0) && sock) {
  1185. kernel_sock_shutdown(sock, SHUT_RDWR);
  1186. sock_release(sock);
  1187. *sockp = NULL;
  1188. }
  1189. return err;
  1190. }
  1191. static struct lock_class_key l2tp_socket_class;
  1192. int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp)
  1193. {
  1194. struct l2tp_tunnel *tunnel = NULL;
  1195. int err;
  1196. enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
  1197. if (cfg != NULL)
  1198. encap = cfg->encap;
  1199. tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
  1200. if (tunnel == NULL) {
  1201. err = -ENOMEM;
  1202. goto err;
  1203. }
  1204. tunnel->version = version;
  1205. tunnel->tunnel_id = tunnel_id;
  1206. tunnel->peer_tunnel_id = peer_tunnel_id;
  1207. tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
  1208. tunnel->magic = L2TP_TUNNEL_MAGIC;
  1209. sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
  1210. rwlock_init(&tunnel->hlist_lock);
  1211. tunnel->acpt_newsess = true;
  1212. if (cfg != NULL)
  1213. tunnel->debug = cfg->debug;
  1214. tunnel->encap = encap;
  1215. refcount_set(&tunnel->ref_count, 1);
  1216. tunnel->fd = fd;
  1217. /* Init delete workqueue struct */
  1218. INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
  1219. INIT_LIST_HEAD(&tunnel->list);
  1220. err = 0;
  1221. err:
  1222. if (tunnelp)
  1223. *tunnelp = tunnel;
  1224. return err;
  1225. }
  1226. EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
  1227. static int l2tp_validate_socket(const struct sock *sk, const struct net *net,
  1228. enum l2tp_encap_type encap)
  1229. {
  1230. if (!net_eq(sock_net(sk), net))
  1231. return -EINVAL;
  1232. if (sk->sk_type != SOCK_DGRAM)
  1233. return -EPROTONOSUPPORT;
  1234. if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) ||
  1235. (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP))
  1236. return -EPROTONOSUPPORT;
  1237. if (sk->sk_user_data)
  1238. return -EBUSY;
  1239. return 0;
  1240. }
  1241. int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
  1242. struct l2tp_tunnel_cfg *cfg)
  1243. {
  1244. struct l2tp_tunnel *tunnel_walk;
  1245. struct l2tp_net *pn;
  1246. struct socket *sock;
  1247. struct sock *sk;
  1248. int ret;
  1249. if (tunnel->fd < 0) {
  1250. ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id,
  1251. tunnel->peer_tunnel_id, cfg,
  1252. &sock);
  1253. if (ret < 0)
  1254. goto err;
  1255. } else {
  1256. sock = sockfd_lookup(tunnel->fd, &ret);
  1257. if (!sock)
  1258. goto err;
  1259. ret = l2tp_validate_socket(sock->sk, net, tunnel->encap);
  1260. if (ret < 0)
  1261. goto err_sock;
  1262. }
  1263. tunnel->l2tp_net = net;
  1264. pn = l2tp_pernet(net);
  1265. spin_lock_bh(&pn->l2tp_tunnel_list_lock);
  1266. list_for_each_entry(tunnel_walk, &pn->l2tp_tunnel_list, list) {
  1267. if (tunnel_walk->tunnel_id == tunnel->tunnel_id) {
  1268. spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
  1269. ret = -EEXIST;
  1270. goto err_sock;
  1271. }
  1272. }
  1273. list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
  1274. spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
  1275. sk = sock->sk;
  1276. sock_hold(sk);
  1277. tunnel->sock = sk;
  1278. if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
  1279. struct udp_tunnel_sock_cfg udp_cfg = {
  1280. .sk_user_data = tunnel,
  1281. .encap_type = UDP_ENCAP_L2TPINUDP,
  1282. .encap_rcv = l2tp_udp_encap_recv,
  1283. .encap_destroy = l2tp_udp_encap_destroy,
  1284. };
  1285. setup_udp_tunnel_sock(net, sock, &udp_cfg);
  1286. } else {
  1287. sk->sk_user_data = tunnel;
  1288. }
  1289. tunnel->old_sk_destruct = sk->sk_destruct;
  1290. sk->sk_destruct = &l2tp_tunnel_destruct;
  1291. lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class,
  1292. "l2tp_sock");
  1293. sk->sk_allocation = GFP_ATOMIC;
  1294. if (tunnel->fd >= 0)
  1295. sockfd_put(sock);
  1296. return 0;
  1297. err_sock:
  1298. if (tunnel->fd < 0)
  1299. sock_release(sock);
  1300. else
  1301. sockfd_put(sock);
  1302. err:
  1303. return ret;
  1304. }
  1305. EXPORT_SYMBOL_GPL(l2tp_tunnel_register);
  1306. /* This function is used by the netlink TUNNEL_DELETE command.
  1307. */
  1308. void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
  1309. {
  1310. if (!test_and_set_bit(0, &tunnel->dead)) {
  1311. l2tp_tunnel_inc_refcount(tunnel);
  1312. queue_work(l2tp_wq, &tunnel->del_work);
  1313. }
  1314. }
  1315. EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
  1316. /* Really kill the session.
  1317. */
  1318. void l2tp_session_free(struct l2tp_session *session)
  1319. {
  1320. struct l2tp_tunnel *tunnel = session->tunnel;
  1321. BUG_ON(refcount_read(&session->ref_count) != 0);
  1322. if (tunnel) {
  1323. BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
  1324. l2tp_tunnel_dec_refcount(tunnel);
  1325. }
  1326. kfree(session);
  1327. }
  1328. EXPORT_SYMBOL_GPL(l2tp_session_free);
  1329. /* Remove an l2tp session from l2tp_core's hash lists.
  1330. * Provides a tidyup interface for pseudowire code which can't just route all
  1331. * shutdown via. l2tp_session_delete and a pseudowire-specific session_close
  1332. * callback.
  1333. */
  1334. void __l2tp_session_unhash(struct l2tp_session *session)
  1335. {
  1336. struct l2tp_tunnel *tunnel = session->tunnel;
  1337. /* Remove the session from core hashes */
  1338. if (tunnel) {
  1339. /* Remove from the per-tunnel hash */
  1340. write_lock_bh(&tunnel->hlist_lock);
  1341. hlist_del_init(&session->hlist);
  1342. write_unlock_bh(&tunnel->hlist_lock);
  1343. /* For L2TPv3 we have a per-net hash: remove from there, too */
  1344. if (tunnel->version != L2TP_HDR_VER_2) {
  1345. struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
  1346. spin_lock_bh(&pn->l2tp_session_hlist_lock);
  1347. hlist_del_init_rcu(&session->global_hlist);
  1348. spin_unlock_bh(&pn->l2tp_session_hlist_lock);
  1349. synchronize_rcu();
  1350. }
  1351. }
  1352. }
  1353. EXPORT_SYMBOL_GPL(__l2tp_session_unhash);
  1354. /* This function is used by the netlink SESSION_DELETE command and by
  1355. pseudowire modules.
  1356. */
  1357. int l2tp_session_delete(struct l2tp_session *session)
  1358. {
  1359. if (test_and_set_bit(0, &session->dead))
  1360. return 0;
  1361. __l2tp_session_unhash(session);
  1362. l2tp_session_queue_purge(session);
  1363. if (session->session_close != NULL)
  1364. (*session->session_close)(session);
  1365. l2tp_session_dec_refcount(session);
  1366. return 0;
  1367. }
  1368. EXPORT_SYMBOL_GPL(l2tp_session_delete);
  1369. /* We come here whenever a session's send_seq, cookie_len or
  1370. * l2specific_type parameters are set.
  1371. */
  1372. void l2tp_session_set_header_len(struct l2tp_session *session, int version)
  1373. {
  1374. if (version == L2TP_HDR_VER_2) {
  1375. session->hdr_len = 6;
  1376. if (session->send_seq)
  1377. session->hdr_len += 4;
  1378. } else {
  1379. session->hdr_len = 4 + session->cookie_len;
  1380. session->hdr_len += l2tp_get_l2specific_len(session);
  1381. if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
  1382. session->hdr_len += 4;
  1383. }
  1384. }
  1385. EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
  1386. struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
  1387. {
  1388. struct l2tp_session *session;
  1389. session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
  1390. if (session != NULL) {
  1391. session->magic = L2TP_SESSION_MAGIC;
  1392. session->tunnel = tunnel;
  1393. session->session_id = session_id;
  1394. session->peer_session_id = peer_session_id;
  1395. session->nr = 0;
  1396. if (tunnel->version == L2TP_HDR_VER_2)
  1397. session->nr_max = 0xffff;
  1398. else
  1399. session->nr_max = 0xffffff;
  1400. session->nr_window_size = session->nr_max / 2;
  1401. session->nr_oos_count_max = 4;
  1402. /* Use NR of first received packet */
  1403. session->reorder_skip = 1;
  1404. sprintf(&session->name[0], "sess %u/%u",
  1405. tunnel->tunnel_id, session->session_id);
  1406. skb_queue_head_init(&session->reorder_q);
  1407. INIT_HLIST_NODE(&session->hlist);
  1408. INIT_HLIST_NODE(&session->global_hlist);
  1409. /* Inherit debug options from tunnel */
  1410. session->debug = tunnel->debug;
  1411. if (cfg) {
  1412. session->pwtype = cfg->pw_type;
  1413. session->debug = cfg->debug;
  1414. session->send_seq = cfg->send_seq;
  1415. session->recv_seq = cfg->recv_seq;
  1416. session->lns_mode = cfg->lns_mode;
  1417. session->reorder_timeout = cfg->reorder_timeout;
  1418. session->l2specific_type = cfg->l2specific_type;
  1419. session->cookie_len = cfg->cookie_len;
  1420. memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
  1421. session->peer_cookie_len = cfg->peer_cookie_len;
  1422. memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
  1423. }
  1424. if (tunnel->version == L2TP_HDR_VER_2)
  1425. session->build_header = l2tp_build_l2tpv2_header;
  1426. else
  1427. session->build_header = l2tp_build_l2tpv3_header;
  1428. l2tp_session_set_header_len(session, tunnel->version);
  1429. refcount_set(&session->ref_count, 1);
  1430. return session;
  1431. }
  1432. return ERR_PTR(-ENOMEM);
  1433. }
  1434. EXPORT_SYMBOL_GPL(l2tp_session_create);
  1435. /*****************************************************************************
  1436. * Init and cleanup
  1437. *****************************************************************************/
  1438. static __net_init int l2tp_init_net(struct net *net)
  1439. {
  1440. struct l2tp_net *pn = net_generic(net, l2tp_net_id);
  1441. int hash;
  1442. INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
  1443. spin_lock_init(&pn->l2tp_tunnel_list_lock);
  1444. for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
  1445. INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
  1446. spin_lock_init(&pn->l2tp_session_hlist_lock);
  1447. return 0;
  1448. }
  1449. static __net_exit void l2tp_exit_net(struct net *net)
  1450. {
  1451. struct l2tp_net *pn = l2tp_pernet(net);
  1452. struct l2tp_tunnel *tunnel = NULL;
  1453. int hash;
  1454. rcu_read_lock_bh();
  1455. list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
  1456. l2tp_tunnel_delete(tunnel);
  1457. }
  1458. rcu_read_unlock_bh();
  1459. flush_workqueue(l2tp_wq);
  1460. rcu_barrier();
  1461. for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
  1462. WARN_ON_ONCE(!hlist_empty(&pn->l2tp_session_hlist[hash]));
  1463. }
  1464. static struct pernet_operations l2tp_net_ops = {
  1465. .init = l2tp_init_net,
  1466. .exit = l2tp_exit_net,
  1467. .id = &l2tp_net_id,
  1468. .size = sizeof(struct l2tp_net),
  1469. };
  1470. static int __init l2tp_init(void)
  1471. {
  1472. int rc = 0;
  1473. rc = register_pernet_device(&l2tp_net_ops);
  1474. if (rc)
  1475. goto out;
  1476. l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
  1477. if (!l2tp_wq) {
  1478. pr_err("alloc_workqueue failed\n");
  1479. unregister_pernet_device(&l2tp_net_ops);
  1480. rc = -ENOMEM;
  1481. goto out;
  1482. }
  1483. pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
  1484. out:
  1485. return rc;
  1486. }
  1487. static void __exit l2tp_exit(void)
  1488. {
  1489. unregister_pernet_device(&l2tp_net_ops);
  1490. if (l2tp_wq) {
  1491. destroy_workqueue(l2tp_wq);
  1492. l2tp_wq = NULL;
  1493. }
  1494. }
  1495. module_init(l2tp_init);
  1496. module_exit(l2tp_exit);
  1497. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  1498. MODULE_DESCRIPTION("L2TP core");
  1499. MODULE_LICENSE("GPL");
  1500. MODULE_VERSION(L2TP_DRV_VERSION);