l2tp_core.c 47 KB

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