l2tp_core.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  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. #if IS_ENABLED(CONFIG_IPV6)
  886. if (l2tp_sk_is_v6(tunnel->sock))
  887. error = inet6_csk_xmit(tunnel->sock, skb, NULL);
  888. else
  889. #endif
  890. error = ip_queue_xmit(tunnel->sock, skb, fl);
  891. /* Update stats */
  892. if (error >= 0) {
  893. atomic_long_inc(&tunnel->stats.tx_packets);
  894. atomic_long_add(len, &tunnel->stats.tx_bytes);
  895. atomic_long_inc(&session->stats.tx_packets);
  896. atomic_long_add(len, &session->stats.tx_bytes);
  897. } else {
  898. atomic_long_inc(&tunnel->stats.tx_errors);
  899. atomic_long_inc(&session->stats.tx_errors);
  900. }
  901. }
  902. /* If caller requires the skb to have a ppp header, the header must be
  903. * inserted in the skb data before calling this function.
  904. */
  905. int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
  906. {
  907. int data_len = skb->len;
  908. struct l2tp_tunnel *tunnel = session->tunnel;
  909. struct sock *sk = tunnel->sock;
  910. struct flowi *fl;
  911. struct udphdr *uh;
  912. struct inet_sock *inet;
  913. int headroom;
  914. int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
  915. int udp_len;
  916. int ret = NET_XMIT_SUCCESS;
  917. /* Check that there's enough headroom in the skb to insert IP,
  918. * UDP and L2TP headers. If not enough, expand it to
  919. * make room. Adjust truesize.
  920. */
  921. headroom = NET_SKB_PAD + sizeof(struct iphdr) +
  922. uhlen + hdr_len;
  923. if (skb_cow_head(skb, headroom)) {
  924. kfree_skb(skb);
  925. return NET_XMIT_DROP;
  926. }
  927. /* Setup L2TP header */
  928. session->build_header(session, __skb_push(skb, hdr_len));
  929. /* Reset skb netfilter state */
  930. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  931. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  932. IPSKB_REROUTED);
  933. nf_reset(skb);
  934. bh_lock_sock(sk);
  935. if (sock_owned_by_user(sk)) {
  936. kfree_skb(skb);
  937. ret = NET_XMIT_DROP;
  938. goto out_unlock;
  939. }
  940. /* The user-space may change the connection status for the user-space
  941. * provided socket at run time: we must check it under the socket lock
  942. */
  943. if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) {
  944. kfree_skb(skb);
  945. ret = NET_XMIT_DROP;
  946. goto out_unlock;
  947. }
  948. /* Get routing info from the tunnel socket */
  949. skb_dst_drop(skb);
  950. skb_dst_set(skb, sk_dst_check(sk, 0));
  951. inet = inet_sk(sk);
  952. fl = &inet->cork.fl;
  953. switch (tunnel->encap) {
  954. case L2TP_ENCAPTYPE_UDP:
  955. /* Setup UDP header */
  956. __skb_push(skb, sizeof(*uh));
  957. skb_reset_transport_header(skb);
  958. uh = udp_hdr(skb);
  959. uh->source = inet->inet_sport;
  960. uh->dest = inet->inet_dport;
  961. udp_len = uhlen + hdr_len + data_len;
  962. uh->len = htons(udp_len);
  963. /* Calculate UDP checksum if configured to do so */
  964. #if IS_ENABLED(CONFIG_IPV6)
  965. if (l2tp_sk_is_v6(sk))
  966. udp6_set_csum(udp_get_no_check6_tx(sk),
  967. skb, &inet6_sk(sk)->saddr,
  968. &sk->sk_v6_daddr, udp_len);
  969. else
  970. #endif
  971. udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
  972. inet->inet_daddr, udp_len);
  973. break;
  974. case L2TP_ENCAPTYPE_IP:
  975. break;
  976. }
  977. l2tp_xmit_core(session, skb, fl, data_len);
  978. out_unlock:
  979. bh_unlock_sock(sk);
  980. return ret;
  981. }
  982. EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
  983. /*****************************************************************************
  984. * Tinnel and session create/destroy.
  985. *****************************************************************************/
  986. /* Tunnel socket destruct hook.
  987. * The tunnel context is deleted only when all session sockets have been
  988. * closed.
  989. */
  990. static void l2tp_tunnel_destruct(struct sock *sk)
  991. {
  992. struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
  993. if (tunnel == NULL)
  994. goto end;
  995. l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
  996. /* Disable udp encapsulation */
  997. switch (tunnel->encap) {
  998. case L2TP_ENCAPTYPE_UDP:
  999. /* No longer an encapsulation socket. See net/ipv4/udp.c */
  1000. (udp_sk(sk))->encap_type = 0;
  1001. (udp_sk(sk))->encap_rcv = NULL;
  1002. (udp_sk(sk))->encap_destroy = NULL;
  1003. break;
  1004. case L2TP_ENCAPTYPE_IP:
  1005. break;
  1006. }
  1007. /* Remove hooks into tunnel socket */
  1008. sk->sk_destruct = tunnel->old_sk_destruct;
  1009. sk->sk_user_data = NULL;
  1010. /* Call the original destructor */
  1011. if (sk->sk_destruct)
  1012. (*sk->sk_destruct)(sk);
  1013. kfree_rcu(tunnel, rcu);
  1014. end:
  1015. return;
  1016. }
  1017. /* When the tunnel is closed, all the attached sessions need to go too.
  1018. */
  1019. static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
  1020. {
  1021. int hash;
  1022. struct hlist_node *walk;
  1023. struct hlist_node *tmp;
  1024. struct l2tp_session *session;
  1025. BUG_ON(tunnel == NULL);
  1026. l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
  1027. tunnel->name);
  1028. write_lock_bh(&tunnel->hlist_lock);
  1029. tunnel->acpt_newsess = false;
  1030. for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
  1031. again:
  1032. hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
  1033. session = hlist_entry(walk, struct l2tp_session, hlist);
  1034. l2tp_info(session, L2TP_MSG_CONTROL,
  1035. "%s: closing session\n", session->name);
  1036. hlist_del_init(&session->hlist);
  1037. if (test_and_set_bit(0, &session->dead))
  1038. goto again;
  1039. write_unlock_bh(&tunnel->hlist_lock);
  1040. __l2tp_session_unhash(session);
  1041. l2tp_session_queue_purge(session);
  1042. if (session->session_close != NULL)
  1043. (*session->session_close)(session);
  1044. l2tp_session_dec_refcount(session);
  1045. write_lock_bh(&tunnel->hlist_lock);
  1046. /* Now restart from the beginning of this hash
  1047. * chain. We always remove a session from the
  1048. * list so we are guaranteed to make forward
  1049. * progress.
  1050. */
  1051. goto again;
  1052. }
  1053. }
  1054. write_unlock_bh(&tunnel->hlist_lock);
  1055. }
  1056. /* Tunnel socket destroy hook for UDP encapsulation */
  1057. static void l2tp_udp_encap_destroy(struct sock *sk)
  1058. {
  1059. struct l2tp_tunnel *tunnel = l2tp_tunnel(sk);
  1060. if (tunnel)
  1061. l2tp_tunnel_delete(tunnel);
  1062. }
  1063. /* Workqueue tunnel deletion function */
  1064. static void l2tp_tunnel_del_work(struct work_struct *work)
  1065. {
  1066. struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel,
  1067. del_work);
  1068. struct sock *sk = tunnel->sock;
  1069. struct socket *sock = sk->sk_socket;
  1070. struct l2tp_net *pn;
  1071. l2tp_tunnel_closeall(tunnel);
  1072. /* If the tunnel socket was created within the kernel, use
  1073. * the sk API to release it here.
  1074. */
  1075. if (tunnel->fd < 0) {
  1076. if (sock) {
  1077. kernel_sock_shutdown(sock, SHUT_RDWR);
  1078. sock_release(sock);
  1079. }
  1080. }
  1081. /* Remove the tunnel struct from the tunnel list */
  1082. pn = l2tp_pernet(tunnel->l2tp_net);
  1083. spin_lock_bh(&pn->l2tp_tunnel_list_lock);
  1084. list_del_rcu(&tunnel->list);
  1085. spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
  1086. /* drop initial ref */
  1087. l2tp_tunnel_dec_refcount(tunnel);
  1088. /* drop workqueue ref */
  1089. l2tp_tunnel_dec_refcount(tunnel);
  1090. }
  1091. /* Create a socket for the tunnel, if one isn't set up by
  1092. * userspace. This is used for static tunnels where there is no
  1093. * managing L2TP daemon.
  1094. *
  1095. * Since we don't want these sockets to keep a namespace alive by
  1096. * themselves, we drop the socket's namespace refcount after creation.
  1097. * These sockets are freed when the namespace exits using the pernet
  1098. * exit hook.
  1099. */
  1100. static int l2tp_tunnel_sock_create(struct net *net,
  1101. u32 tunnel_id,
  1102. u32 peer_tunnel_id,
  1103. struct l2tp_tunnel_cfg *cfg,
  1104. struct socket **sockp)
  1105. {
  1106. int err = -EINVAL;
  1107. struct socket *sock = NULL;
  1108. struct udp_port_cfg udp_conf;
  1109. switch (cfg->encap) {
  1110. case L2TP_ENCAPTYPE_UDP:
  1111. memset(&udp_conf, 0, sizeof(udp_conf));
  1112. #if IS_ENABLED(CONFIG_IPV6)
  1113. if (cfg->local_ip6 && cfg->peer_ip6) {
  1114. udp_conf.family = AF_INET6;
  1115. memcpy(&udp_conf.local_ip6, cfg->local_ip6,
  1116. sizeof(udp_conf.local_ip6));
  1117. memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
  1118. sizeof(udp_conf.peer_ip6));
  1119. udp_conf.use_udp6_tx_checksums =
  1120. ! cfg->udp6_zero_tx_checksums;
  1121. udp_conf.use_udp6_rx_checksums =
  1122. ! cfg->udp6_zero_rx_checksums;
  1123. } else
  1124. #endif
  1125. {
  1126. udp_conf.family = AF_INET;
  1127. udp_conf.local_ip = cfg->local_ip;
  1128. udp_conf.peer_ip = cfg->peer_ip;
  1129. udp_conf.use_udp_checksums = cfg->use_udp_checksums;
  1130. }
  1131. udp_conf.local_udp_port = htons(cfg->local_udp_port);
  1132. udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
  1133. err = udp_sock_create(net, &udp_conf, &sock);
  1134. if (err < 0)
  1135. goto out;
  1136. break;
  1137. case L2TP_ENCAPTYPE_IP:
  1138. #if IS_ENABLED(CONFIG_IPV6)
  1139. if (cfg->local_ip6 && cfg->peer_ip6) {
  1140. struct sockaddr_l2tpip6 ip6_addr = {0};
  1141. err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
  1142. IPPROTO_L2TP, &sock);
  1143. if (err < 0)
  1144. goto out;
  1145. ip6_addr.l2tp_family = AF_INET6;
  1146. memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
  1147. sizeof(ip6_addr.l2tp_addr));
  1148. ip6_addr.l2tp_conn_id = tunnel_id;
  1149. err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
  1150. sizeof(ip6_addr));
  1151. if (err < 0)
  1152. goto out;
  1153. ip6_addr.l2tp_family = AF_INET6;
  1154. memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
  1155. sizeof(ip6_addr.l2tp_addr));
  1156. ip6_addr.l2tp_conn_id = peer_tunnel_id;
  1157. err = kernel_connect(sock,
  1158. (struct sockaddr *) &ip6_addr,
  1159. sizeof(ip6_addr), 0);
  1160. if (err < 0)
  1161. goto out;
  1162. } else
  1163. #endif
  1164. {
  1165. struct sockaddr_l2tpip ip_addr = {0};
  1166. err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
  1167. IPPROTO_L2TP, &sock);
  1168. if (err < 0)
  1169. goto out;
  1170. ip_addr.l2tp_family = AF_INET;
  1171. ip_addr.l2tp_addr = cfg->local_ip;
  1172. ip_addr.l2tp_conn_id = tunnel_id;
  1173. err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
  1174. sizeof(ip_addr));
  1175. if (err < 0)
  1176. goto out;
  1177. ip_addr.l2tp_family = AF_INET;
  1178. ip_addr.l2tp_addr = cfg->peer_ip;
  1179. ip_addr.l2tp_conn_id = peer_tunnel_id;
  1180. err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
  1181. sizeof(ip_addr), 0);
  1182. if (err < 0)
  1183. goto out;
  1184. }
  1185. break;
  1186. default:
  1187. goto out;
  1188. }
  1189. out:
  1190. *sockp = sock;
  1191. if ((err < 0) && sock) {
  1192. kernel_sock_shutdown(sock, SHUT_RDWR);
  1193. sock_release(sock);
  1194. *sockp = NULL;
  1195. }
  1196. return err;
  1197. }
  1198. static struct lock_class_key l2tp_socket_class;
  1199. 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)
  1200. {
  1201. struct l2tp_tunnel *tunnel = NULL;
  1202. int err;
  1203. enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
  1204. if (cfg != NULL)
  1205. encap = cfg->encap;
  1206. tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
  1207. if (tunnel == NULL) {
  1208. err = -ENOMEM;
  1209. goto err;
  1210. }
  1211. tunnel->version = version;
  1212. tunnel->tunnel_id = tunnel_id;
  1213. tunnel->peer_tunnel_id = peer_tunnel_id;
  1214. tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
  1215. tunnel->magic = L2TP_TUNNEL_MAGIC;
  1216. sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
  1217. rwlock_init(&tunnel->hlist_lock);
  1218. tunnel->acpt_newsess = true;
  1219. if (cfg != NULL)
  1220. tunnel->debug = cfg->debug;
  1221. tunnel->encap = encap;
  1222. refcount_set(&tunnel->ref_count, 1);
  1223. tunnel->fd = fd;
  1224. /* Init delete workqueue struct */
  1225. INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
  1226. INIT_LIST_HEAD(&tunnel->list);
  1227. err = 0;
  1228. err:
  1229. if (tunnelp)
  1230. *tunnelp = tunnel;
  1231. return err;
  1232. }
  1233. EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
  1234. static int l2tp_validate_socket(const struct sock *sk, const struct net *net,
  1235. enum l2tp_encap_type encap)
  1236. {
  1237. if (!net_eq(sock_net(sk), net))
  1238. return -EINVAL;
  1239. if (sk->sk_type != SOCK_DGRAM)
  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);