l2tp_core.c 47 KB

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