l2tp_core.c 47 KB

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