l2tp_core.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953
  1. // SPDX-License-Identifier: GPL-2.0-only
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/module.h>
  18. #include <linux/string.h>
  19. #include <linux/list.h>
  20. #include <linux/rculist.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/kernel.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/kthread.h>
  25. #include <linux/sched.h>
  26. #include <linux/slab.h>
  27. #include <linux/errno.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/net.h>
  31. #include <linux/inetdevice.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/init.h>
  34. #include <linux/in.h>
  35. #include <linux/ip.h>
  36. #include <linux/udp.h>
  37. #include <linux/l2tp.h>
  38. #include <linux/sort.h>
  39. #include <linux/file.h>
  40. #include <linux/nsproxy.h>
  41. #include <net/net_namespace.h>
  42. #include <net/netns/generic.h>
  43. #include <net/dst.h>
  44. #include <net/ip.h>
  45. #include <net/udp.h>
  46. #include <net/udp_tunnel.h>
  47. #include <net/inet_common.h>
  48. #include <net/xfrm.h>
  49. #include <net/protocol.h>
  50. #include <net/inet6_connection_sock.h>
  51. #include <net/inet_ecn.h>
  52. #include <net/ip6_route.h>
  53. #include <net/ip6_checksum.h>
  54. #include <asm/byteorder.h>
  55. #include <linux/atomic.h>
  56. #include "l2tp_core.h"
  57. #define CREATE_TRACE_POINTS
  58. #include "trace.h"
  59. #define L2TP_DRV_VERSION "V2.0"
  60. /* L2TP header constants */
  61. #define L2TP_HDRFLAG_T 0x8000
  62. #define L2TP_HDRFLAG_L 0x4000
  63. #define L2TP_HDRFLAG_S 0x0800
  64. #define L2TP_HDRFLAG_O 0x0200
  65. #define L2TP_HDRFLAG_P 0x0100
  66. #define L2TP_HDR_VER_MASK 0x000F
  67. #define L2TP_HDR_VER_2 0x0002
  68. #define L2TP_HDR_VER_3 0x0003
  69. /* L2TPv3 default L2-specific sublayer */
  70. #define L2TP_SLFLAG_S 0x40000000
  71. #define L2TP_SL_SEQ_MASK 0x00ffffff
  72. #define L2TP_HDR_SIZE_MAX 14
  73. /* Default trace flags */
  74. #define L2TP_DEFAULT_DEBUG_FLAGS 0
  75. #define L2TP_DEPTH_NESTING 2
  76. #if L2TP_DEPTH_NESTING == SINGLE_DEPTH_NESTING
  77. #error "L2TP requires its own lockdep subclass"
  78. #endif
  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. /* Lock for write access to l2tp_tunnel_idr */
  93. spinlock_t l2tp_tunnel_idr_lock;
  94. struct idr l2tp_tunnel_idr;
  95. /* Lock for write access to l2tp_v[23]_session_idr/htable */
  96. spinlock_t l2tp_session_idr_lock;
  97. struct idr l2tp_v2_session_idr;
  98. struct idr l2tp_v3_session_idr;
  99. struct hlist_head l2tp_v3_session_htable[16];
  100. };
  101. static u32 l2tp_v2_session_key(u16 tunnel_id, u16 session_id)
  102. {
  103. return ((u32)tunnel_id) << 16 | session_id;
  104. }
  105. static unsigned long l2tp_v3_session_hashkey(struct sock *sk, u32 session_id)
  106. {
  107. return ((unsigned long)sk) + session_id;
  108. }
  109. #if IS_ENABLED(CONFIG_IPV6)
  110. static bool l2tp_sk_is_v6(struct sock *sk)
  111. {
  112. return sk->sk_family == PF_INET6 &&
  113. !ipv6_addr_v4mapped(&sk->sk_v6_daddr);
  114. }
  115. #endif
  116. static struct l2tp_net *l2tp_pernet(const struct net *net)
  117. {
  118. return net_generic(net, l2tp_net_id);
  119. }
  120. static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
  121. {
  122. struct sock *sk = tunnel->sock;
  123. trace_free_tunnel(tunnel);
  124. if (sk) {
  125. /* Disable udp encapsulation */
  126. switch (tunnel->encap) {
  127. case L2TP_ENCAPTYPE_UDP:
  128. /* No longer an encapsulation socket. See net/ipv4/udp.c */
  129. WRITE_ONCE(udp_sk(sk)->encap_type, 0);
  130. udp_sk(sk)->encap_rcv = NULL;
  131. udp_sk(sk)->encap_destroy = NULL;
  132. break;
  133. case L2TP_ENCAPTYPE_IP:
  134. break;
  135. }
  136. tunnel->sock = NULL;
  137. sock_put(sk);
  138. }
  139. kfree_rcu(tunnel, rcu);
  140. }
  141. static void l2tp_session_free(struct l2tp_session *session)
  142. {
  143. trace_free_session(session);
  144. if (session->tunnel)
  145. l2tp_tunnel_put(session->tunnel);
  146. kfree_rcu(session, rcu);
  147. }
  148. struct l2tp_tunnel *l2tp_sk_to_tunnel(const struct sock *sk)
  149. {
  150. const struct net *net = sock_net(sk);
  151. unsigned long tunnel_id, tmp;
  152. struct l2tp_tunnel *tunnel;
  153. struct l2tp_net *pn;
  154. rcu_read_lock_bh();
  155. pn = l2tp_pernet(net);
  156. idr_for_each_entry_ul(&pn->l2tp_tunnel_idr, tunnel, tmp, tunnel_id) {
  157. if (tunnel &&
  158. tunnel->sock == sk &&
  159. refcount_inc_not_zero(&tunnel->ref_count)) {
  160. rcu_read_unlock_bh();
  161. return tunnel;
  162. }
  163. }
  164. rcu_read_unlock_bh();
  165. return NULL;
  166. }
  167. EXPORT_SYMBOL_GPL(l2tp_sk_to_tunnel);
  168. void l2tp_tunnel_put(struct l2tp_tunnel *tunnel)
  169. {
  170. if (refcount_dec_and_test(&tunnel->ref_count))
  171. l2tp_tunnel_free(tunnel);
  172. }
  173. EXPORT_SYMBOL_GPL(l2tp_tunnel_put);
  174. void l2tp_session_put(struct l2tp_session *session)
  175. {
  176. if (refcount_dec_and_test(&session->ref_count))
  177. l2tp_session_free(session);
  178. }
  179. EXPORT_SYMBOL_GPL(l2tp_session_put);
  180. /* Lookup a tunnel. A new reference is held on the returned tunnel. */
  181. struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id)
  182. {
  183. const struct l2tp_net *pn = l2tp_pernet(net);
  184. struct l2tp_tunnel *tunnel;
  185. rcu_read_lock_bh();
  186. tunnel = idr_find(&pn->l2tp_tunnel_idr, tunnel_id);
  187. if (tunnel && refcount_inc_not_zero(&tunnel->ref_count)) {
  188. rcu_read_unlock_bh();
  189. return tunnel;
  190. }
  191. rcu_read_unlock_bh();
  192. return NULL;
  193. }
  194. EXPORT_SYMBOL_GPL(l2tp_tunnel_get);
  195. struct l2tp_tunnel *l2tp_tunnel_get_next(const struct net *net, unsigned long *key)
  196. {
  197. struct l2tp_net *pn = l2tp_pernet(net);
  198. struct l2tp_tunnel *tunnel = NULL;
  199. rcu_read_lock_bh();
  200. again:
  201. tunnel = idr_get_next_ul(&pn->l2tp_tunnel_idr, key);
  202. if (tunnel) {
  203. if (refcount_inc_not_zero(&tunnel->ref_count)) {
  204. rcu_read_unlock_bh();
  205. return tunnel;
  206. }
  207. (*key)++;
  208. goto again;
  209. }
  210. rcu_read_unlock_bh();
  211. return NULL;
  212. }
  213. EXPORT_SYMBOL_GPL(l2tp_tunnel_get_next);
  214. struct l2tp_session *l2tp_v3_session_get(const struct net *net, struct sock *sk, u32 session_id)
  215. {
  216. const struct l2tp_net *pn = l2tp_pernet(net);
  217. struct l2tp_session *session;
  218. rcu_read_lock_bh();
  219. session = idr_find(&pn->l2tp_v3_session_idr, session_id);
  220. if (session && !hash_hashed(&session->hlist) &&
  221. refcount_inc_not_zero(&session->ref_count)) {
  222. rcu_read_unlock_bh();
  223. return session;
  224. }
  225. /* If we get here and session is non-NULL, the session_id
  226. * collides with one in another tunnel. If sk is non-NULL,
  227. * find the session matching sk.
  228. */
  229. if (session && sk) {
  230. unsigned long key = l2tp_v3_session_hashkey(sk, session->session_id);
  231. hash_for_each_possible_rcu(pn->l2tp_v3_session_htable, session,
  232. hlist, key) {
  233. /* session->tunnel may be NULL if another thread is in
  234. * l2tp_session_register and has added an item to
  235. * l2tp_v3_session_htable but hasn't yet added the
  236. * session to its tunnel's session_list.
  237. */
  238. struct l2tp_tunnel *tunnel = READ_ONCE(session->tunnel);
  239. if (session->session_id == session_id &&
  240. tunnel && tunnel->sock == sk &&
  241. refcount_inc_not_zero(&session->ref_count)) {
  242. rcu_read_unlock_bh();
  243. return session;
  244. }
  245. }
  246. }
  247. rcu_read_unlock_bh();
  248. return NULL;
  249. }
  250. EXPORT_SYMBOL_GPL(l2tp_v3_session_get);
  251. struct l2tp_session *l2tp_v2_session_get(const struct net *net, u16 tunnel_id, u16 session_id)
  252. {
  253. u32 session_key = l2tp_v2_session_key(tunnel_id, session_id);
  254. const struct l2tp_net *pn = l2tp_pernet(net);
  255. struct l2tp_session *session;
  256. rcu_read_lock_bh();
  257. session = idr_find(&pn->l2tp_v2_session_idr, session_key);
  258. if (session && refcount_inc_not_zero(&session->ref_count)) {
  259. rcu_read_unlock_bh();
  260. return session;
  261. }
  262. rcu_read_unlock_bh();
  263. return NULL;
  264. }
  265. EXPORT_SYMBOL_GPL(l2tp_v2_session_get);
  266. struct l2tp_session *l2tp_session_get(const struct net *net, struct sock *sk, int pver,
  267. u32 tunnel_id, u32 session_id)
  268. {
  269. if (pver == L2TP_HDR_VER_2)
  270. return l2tp_v2_session_get(net, tunnel_id, session_id);
  271. else
  272. return l2tp_v3_session_get(net, sk, session_id);
  273. }
  274. EXPORT_SYMBOL_GPL(l2tp_session_get);
  275. static struct l2tp_session *l2tp_v2_session_get_next(const struct net *net,
  276. u16 tid,
  277. unsigned long *key)
  278. {
  279. struct l2tp_net *pn = l2tp_pernet(net);
  280. struct l2tp_session *session = NULL;
  281. /* Start searching within the range of the tid */
  282. if (*key == 0)
  283. *key = l2tp_v2_session_key(tid, 0);
  284. rcu_read_lock_bh();
  285. again:
  286. session = idr_get_next_ul(&pn->l2tp_v2_session_idr, key);
  287. if (session) {
  288. struct l2tp_tunnel *tunnel = READ_ONCE(session->tunnel);
  289. /* ignore sessions with id 0 as they are internal for pppol2tp */
  290. if (session->session_id == 0) {
  291. (*key)++;
  292. goto again;
  293. }
  294. if (tunnel->tunnel_id == tid &&
  295. refcount_inc_not_zero(&session->ref_count)) {
  296. rcu_read_unlock_bh();
  297. return session;
  298. }
  299. (*key)++;
  300. if (tunnel->tunnel_id == tid)
  301. goto again;
  302. }
  303. rcu_read_unlock_bh();
  304. return NULL;
  305. }
  306. static struct l2tp_session *l2tp_v3_session_get_next(const struct net *net,
  307. u32 tid, struct sock *sk,
  308. unsigned long *key)
  309. {
  310. struct l2tp_net *pn = l2tp_pernet(net);
  311. struct l2tp_session *session = NULL;
  312. rcu_read_lock_bh();
  313. again:
  314. session = idr_get_next_ul(&pn->l2tp_v3_session_idr, key);
  315. if (session && !hash_hashed(&session->hlist)) {
  316. struct l2tp_tunnel *tunnel = READ_ONCE(session->tunnel);
  317. if (tunnel && tunnel->tunnel_id == tid &&
  318. refcount_inc_not_zero(&session->ref_count)) {
  319. rcu_read_unlock_bh();
  320. return session;
  321. }
  322. (*key)++;
  323. goto again;
  324. }
  325. /* If we get here and session is non-NULL, the IDR entry may be one
  326. * where the session_id collides with one in another tunnel. Check
  327. * session_htable for a match. There can only be one session of a given
  328. * ID per tunnel so we can return as soon as a match is found.
  329. */
  330. if (session && hash_hashed(&session->hlist)) {
  331. unsigned long hkey = l2tp_v3_session_hashkey(sk, session->session_id);
  332. u32 sid = session->session_id;
  333. hash_for_each_possible_rcu(pn->l2tp_v3_session_htable, session,
  334. hlist, hkey) {
  335. struct l2tp_tunnel *tunnel = READ_ONCE(session->tunnel);
  336. if (session->session_id == sid &&
  337. tunnel && tunnel->tunnel_id == tid &&
  338. refcount_inc_not_zero(&session->ref_count)) {
  339. rcu_read_unlock_bh();
  340. return session;
  341. }
  342. }
  343. /* If no match found, the colliding session ID isn't in our
  344. * tunnel so try the next session ID.
  345. */
  346. (*key)++;
  347. goto again;
  348. }
  349. rcu_read_unlock_bh();
  350. return NULL;
  351. }
  352. struct l2tp_session *l2tp_session_get_next(const struct net *net, struct sock *sk, int pver,
  353. u32 tunnel_id, unsigned long *key)
  354. {
  355. if (pver == L2TP_HDR_VER_2)
  356. return l2tp_v2_session_get_next(net, tunnel_id, key);
  357. else
  358. return l2tp_v3_session_get_next(net, tunnel_id, sk, key);
  359. }
  360. EXPORT_SYMBOL_GPL(l2tp_session_get_next);
  361. /* Lookup a session by interface name.
  362. * This is very inefficient but is only used by management interfaces.
  363. */
  364. struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
  365. const char *ifname)
  366. {
  367. struct l2tp_net *pn = l2tp_pernet(net);
  368. unsigned long tunnel_id, tmp;
  369. struct l2tp_session *session;
  370. struct l2tp_tunnel *tunnel;
  371. rcu_read_lock_bh();
  372. idr_for_each_entry_ul(&pn->l2tp_tunnel_idr, tunnel, tmp, tunnel_id) {
  373. if (tunnel) {
  374. list_for_each_entry_rcu(session, &tunnel->session_list, list) {
  375. if (!strcmp(session->ifname, ifname)) {
  376. refcount_inc(&session->ref_count);
  377. rcu_read_unlock_bh();
  378. return session;
  379. }
  380. }
  381. }
  382. }
  383. rcu_read_unlock_bh();
  384. return NULL;
  385. }
  386. EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
  387. static void l2tp_session_coll_list_add(struct l2tp_session_coll_list *clist,
  388. struct l2tp_session *session)
  389. {
  390. refcount_inc(&session->ref_count);
  391. WARN_ON_ONCE(session->coll_list);
  392. session->coll_list = clist;
  393. spin_lock(&clist->lock);
  394. list_add(&session->clist, &clist->list);
  395. spin_unlock(&clist->lock);
  396. }
  397. static int l2tp_session_collision_add(struct l2tp_net *pn,
  398. struct l2tp_session *session1,
  399. struct l2tp_session *session2)
  400. {
  401. struct l2tp_session_coll_list *clist;
  402. lockdep_assert_held(&pn->l2tp_session_idr_lock);
  403. if (!session2)
  404. return -EEXIST;
  405. /* If existing session is in IP-encap tunnel, refuse new session */
  406. if (session2->tunnel->encap == L2TP_ENCAPTYPE_IP)
  407. return -EEXIST;
  408. clist = session2->coll_list;
  409. if (!clist) {
  410. /* First collision. Allocate list to manage the collided sessions
  411. * and add the existing session to the list.
  412. */
  413. clist = kmalloc_obj(*clist, GFP_ATOMIC);
  414. if (!clist)
  415. return -ENOMEM;
  416. spin_lock_init(&clist->lock);
  417. INIT_LIST_HEAD(&clist->list);
  418. refcount_set(&clist->ref_count, 1);
  419. l2tp_session_coll_list_add(clist, session2);
  420. }
  421. /* If existing session isn't already in the session hlist, add it. */
  422. if (!hash_hashed(&session2->hlist))
  423. hash_add_rcu(pn->l2tp_v3_session_htable, &session2->hlist,
  424. session2->hlist_key);
  425. /* Add new session to the hlist and collision list */
  426. hash_add_rcu(pn->l2tp_v3_session_htable, &session1->hlist,
  427. session1->hlist_key);
  428. refcount_inc(&clist->ref_count);
  429. l2tp_session_coll_list_add(clist, session1);
  430. return 0;
  431. }
  432. static void l2tp_session_collision_del(struct l2tp_net *pn,
  433. struct l2tp_session *session)
  434. {
  435. struct l2tp_session_coll_list *clist = session->coll_list;
  436. unsigned long session_key = session->session_id;
  437. struct l2tp_session *session2;
  438. lockdep_assert_held(&pn->l2tp_session_idr_lock);
  439. hash_del_rcu(&session->hlist);
  440. if (clist) {
  441. /* Remove session from its collision list. If there
  442. * are other sessions with the same ID, replace this
  443. * session's IDR entry with that session, otherwise
  444. * remove the IDR entry. If this is the last session,
  445. * the collision list data is freed.
  446. */
  447. spin_lock(&clist->lock);
  448. list_del_init(&session->clist);
  449. session2 = list_first_entry_or_null(&clist->list, struct l2tp_session, clist);
  450. if (session2) {
  451. void *old = idr_replace(&pn->l2tp_v3_session_idr, session2, session_key);
  452. WARN_ON_ONCE(IS_ERR_VALUE(old));
  453. } else {
  454. void *removed = idr_remove(&pn->l2tp_v3_session_idr, session_key);
  455. WARN_ON_ONCE(removed != session);
  456. }
  457. session->coll_list = NULL;
  458. spin_unlock(&clist->lock);
  459. if (refcount_dec_and_test(&clist->ref_count))
  460. kfree(clist);
  461. l2tp_session_put(session);
  462. }
  463. }
  464. int l2tp_session_register(struct l2tp_session *session,
  465. struct l2tp_tunnel *tunnel)
  466. {
  467. struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
  468. struct l2tp_session *other_session = NULL;
  469. void *old = NULL;
  470. u32 session_key;
  471. int err;
  472. spin_lock_bh(&tunnel->list_lock);
  473. spin_lock_bh(&pn->l2tp_session_idr_lock);
  474. if (!tunnel->acpt_newsess) {
  475. err = -ENODEV;
  476. goto out;
  477. }
  478. if (tunnel->version == L2TP_HDR_VER_3) {
  479. session_key = session->session_id;
  480. err = idr_alloc_u32(&pn->l2tp_v3_session_idr, NULL,
  481. &session_key, session_key, GFP_ATOMIC);
  482. /* IP encap expects session IDs to be globally unique, while
  483. * UDP encap doesn't. This isn't per the RFC, which says that
  484. * sessions are identified only by the session ID, but is to
  485. * support existing userspace which depends on it.
  486. */
  487. if (err == -ENOSPC && tunnel->encap == L2TP_ENCAPTYPE_UDP) {
  488. other_session = idr_find(&pn->l2tp_v3_session_idr,
  489. session_key);
  490. err = l2tp_session_collision_add(pn, session,
  491. other_session);
  492. }
  493. } else {
  494. session_key = l2tp_v2_session_key(tunnel->tunnel_id,
  495. session->session_id);
  496. err = idr_alloc_u32(&pn->l2tp_v2_session_idr, NULL,
  497. &session_key, session_key, GFP_ATOMIC);
  498. }
  499. if (err) {
  500. if (err == -ENOSPC)
  501. err = -EEXIST;
  502. goto out;
  503. }
  504. refcount_inc(&tunnel->ref_count);
  505. WRITE_ONCE(session->tunnel, tunnel);
  506. list_add_rcu(&session->list, &tunnel->session_list);
  507. /* this makes session available to lockless getters */
  508. if (tunnel->version == L2TP_HDR_VER_3) {
  509. if (!other_session)
  510. old = idr_replace(&pn->l2tp_v3_session_idr, session, session_key);
  511. } else {
  512. old = idr_replace(&pn->l2tp_v2_session_idr, session, session_key);
  513. }
  514. /* old should be NULL, unless something removed or modified
  515. * the IDR entry after our idr_alloc_32 above (which shouldn't
  516. * happen).
  517. */
  518. WARN_ON_ONCE(old);
  519. out:
  520. spin_unlock_bh(&pn->l2tp_session_idr_lock);
  521. spin_unlock_bh(&tunnel->list_lock);
  522. if (!err)
  523. trace_register_session(session);
  524. return err;
  525. }
  526. EXPORT_SYMBOL_GPL(l2tp_session_register);
  527. /*****************************************************************************
  528. * Receive data handling
  529. *****************************************************************************/
  530. /* Queue a skb in order. We come here only if the skb has an L2TP sequence
  531. * number.
  532. */
  533. static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
  534. {
  535. struct sk_buff *skbp;
  536. struct sk_buff *tmp;
  537. u32 ns = L2TP_SKB_CB(skb)->ns;
  538. spin_lock_bh(&session->reorder_q.lock);
  539. skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
  540. if (L2TP_SKB_CB(skbp)->ns > ns) {
  541. __skb_queue_before(&session->reorder_q, skbp, skb);
  542. atomic_long_inc(&session->stats.rx_oos_packets);
  543. goto out;
  544. }
  545. }
  546. __skb_queue_tail(&session->reorder_q, skb);
  547. out:
  548. spin_unlock_bh(&session->reorder_q.lock);
  549. }
  550. /* Dequeue a single skb.
  551. */
  552. static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
  553. {
  554. struct l2tp_tunnel *tunnel = session->tunnel;
  555. int length = L2TP_SKB_CB(skb)->length;
  556. /* We're about to requeue the skb, so return resources
  557. * to its current owner (a socket receive buffer).
  558. */
  559. skb_orphan(skb);
  560. atomic_long_inc(&tunnel->stats.rx_packets);
  561. atomic_long_add(length, &tunnel->stats.rx_bytes);
  562. atomic_long_inc(&session->stats.rx_packets);
  563. atomic_long_add(length, &session->stats.rx_bytes);
  564. if (L2TP_SKB_CB(skb)->has_seq) {
  565. /* Bump our Nr */
  566. session->nr++;
  567. session->nr &= session->nr_max;
  568. trace_session_seqnum_update(session);
  569. }
  570. /* call private receive handler */
  571. if (session->recv_skb)
  572. (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
  573. else
  574. kfree_skb(skb);
  575. }
  576. /* Dequeue skbs from the session's reorder_q, subject to packet order.
  577. * Skbs that have been in the queue for too long are simply discarded.
  578. */
  579. static void l2tp_recv_dequeue(struct l2tp_session *session)
  580. {
  581. struct sk_buff *skb;
  582. struct sk_buff *tmp;
  583. /* If the pkt at the head of the queue has the nr that we
  584. * expect to send up next, dequeue it and any other
  585. * in-sequence packets behind it.
  586. */
  587. start:
  588. spin_lock_bh(&session->reorder_q.lock);
  589. skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
  590. struct l2tp_skb_cb *cb = L2TP_SKB_CB(skb);
  591. /* If the packet has been pending on the queue for too long, discard it */
  592. if (time_after(jiffies, cb->expires)) {
  593. atomic_long_inc(&session->stats.rx_seq_discards);
  594. atomic_long_inc(&session->stats.rx_errors);
  595. trace_session_pkt_expired(session, cb->ns);
  596. session->reorder_skip = 1;
  597. __skb_unlink(skb, &session->reorder_q);
  598. kfree_skb(skb);
  599. continue;
  600. }
  601. if (cb->has_seq) {
  602. if (session->reorder_skip) {
  603. session->reorder_skip = 0;
  604. session->nr = cb->ns;
  605. trace_session_seqnum_reset(session);
  606. }
  607. if (cb->ns != session->nr)
  608. goto out;
  609. }
  610. __skb_unlink(skb, &session->reorder_q);
  611. /* Process the skb. We release the queue lock while we
  612. * do so to let other contexts process the queue.
  613. */
  614. spin_unlock_bh(&session->reorder_q.lock);
  615. l2tp_recv_dequeue_skb(session, skb);
  616. goto start;
  617. }
  618. out:
  619. spin_unlock_bh(&session->reorder_q.lock);
  620. }
  621. static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
  622. {
  623. u32 nws;
  624. if (nr >= session->nr)
  625. nws = nr - session->nr;
  626. else
  627. nws = (session->nr_max + 1) - (session->nr - nr);
  628. return nws < session->nr_window_size;
  629. }
  630. /* If packet has sequence numbers, queue it if acceptable. Returns 0 if
  631. * acceptable, else non-zero.
  632. */
  633. static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
  634. {
  635. struct l2tp_skb_cb *cb = L2TP_SKB_CB(skb);
  636. if (!l2tp_seq_check_rx_window(session, cb->ns)) {
  637. /* Packet sequence number is outside allowed window.
  638. * Discard it.
  639. */
  640. trace_session_pkt_outside_rx_window(session, cb->ns);
  641. goto discard;
  642. }
  643. if (session->reorder_timeout != 0) {
  644. /* Packet reordering enabled. Add skb to session's
  645. * reorder queue, in order of ns.
  646. */
  647. l2tp_recv_queue_skb(session, skb);
  648. goto out;
  649. }
  650. /* Packet reordering disabled. Discard out-of-sequence packets, while
  651. * tracking the number if in-sequence packets after the first OOS packet
  652. * is seen. After nr_oos_count_max in-sequence packets, reset the
  653. * sequence number to re-enable packet reception.
  654. */
  655. if (cb->ns == session->nr) {
  656. skb_queue_tail(&session->reorder_q, skb);
  657. } else {
  658. u32 nr_oos = cb->ns;
  659. u32 nr_next = (session->nr_oos + 1) & session->nr_max;
  660. if (nr_oos == nr_next)
  661. session->nr_oos_count++;
  662. else
  663. session->nr_oos_count = 0;
  664. session->nr_oos = nr_oos;
  665. if (session->nr_oos_count > session->nr_oos_count_max) {
  666. session->reorder_skip = 1;
  667. }
  668. if (!session->reorder_skip) {
  669. atomic_long_inc(&session->stats.rx_seq_discards);
  670. trace_session_pkt_oos(session, cb->ns);
  671. goto discard;
  672. }
  673. skb_queue_tail(&session->reorder_q, skb);
  674. }
  675. out:
  676. return 0;
  677. discard:
  678. return 1;
  679. }
  680. /* Do receive processing of L2TP data frames. We handle both L2TPv2
  681. * and L2TPv3 data frames here.
  682. *
  683. * L2TPv2 Data Message Header
  684. *
  685. * 0 1 2 3
  686. * 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
  687. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  688. * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
  689. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  690. * | Tunnel ID | Session ID |
  691. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  692. * | Ns (opt) | Nr (opt) |
  693. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  694. * | Offset Size (opt) | Offset pad... (opt)
  695. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  696. *
  697. * Data frames are marked by T=0. All other fields are the same as
  698. * those in L2TP control frames.
  699. *
  700. * L2TPv3 Data Message Header
  701. *
  702. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  703. * | L2TP Session Header |
  704. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  705. * | L2-Specific Sublayer |
  706. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  707. * | Tunnel Payload ...
  708. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  709. *
  710. * L2TPv3 Session Header Over IP
  711. *
  712. * 0 1 2 3
  713. * 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
  714. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  715. * | Session ID |
  716. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  717. * | Cookie (optional, maximum 64 bits)...
  718. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  719. * |
  720. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  721. *
  722. * L2TPv3 L2-Specific Sublayer Format
  723. *
  724. * 0 1 2 3
  725. * 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
  726. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  727. * |x|S|x|x|x|x|x|x| Sequence Number |
  728. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  729. *
  730. * Cookie value and sublayer format are negotiated with the peer when
  731. * the session is set up. Unlike L2TPv2, we do not need to parse the
  732. * packet header to determine if optional fields are present.
  733. *
  734. * Caller must already have parsed the frame and determined that it is
  735. * a data (not control) frame before coming here. Fields up to the
  736. * session-id have already been parsed and ptr points to the data
  737. * after the session-id.
  738. */
  739. void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
  740. unsigned char *ptr, unsigned char *optr, u16 hdrflags,
  741. int length)
  742. {
  743. struct l2tp_tunnel *tunnel = session->tunnel;
  744. int offset;
  745. /* Parse and check optional cookie */
  746. if (session->peer_cookie_len > 0) {
  747. if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
  748. pr_debug_ratelimited("%s: cookie mismatch (%u/%u). Discarding.\n",
  749. tunnel->name, tunnel->tunnel_id,
  750. session->session_id);
  751. atomic_long_inc(&session->stats.rx_cookie_discards);
  752. goto discard;
  753. }
  754. ptr += session->peer_cookie_len;
  755. }
  756. /* Handle the optional sequence numbers. Sequence numbers are
  757. * in different places for L2TPv2 and L2TPv3.
  758. *
  759. * If we are the LAC, enable/disable sequence numbers under
  760. * the control of the LNS. If no sequence numbers present but
  761. * we were expecting them, discard frame.
  762. */
  763. L2TP_SKB_CB(skb)->has_seq = 0;
  764. if (tunnel->version == L2TP_HDR_VER_2) {
  765. if (hdrflags & L2TP_HDRFLAG_S) {
  766. /* Store L2TP info in the skb */
  767. L2TP_SKB_CB(skb)->ns = ntohs(*(__be16 *)ptr);
  768. L2TP_SKB_CB(skb)->has_seq = 1;
  769. ptr += 2;
  770. /* Skip past nr in the header */
  771. ptr += 2;
  772. }
  773. } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
  774. u32 l2h = ntohl(*(__be32 *)ptr);
  775. if (l2h & 0x40000000) {
  776. /* Store L2TP info in the skb */
  777. L2TP_SKB_CB(skb)->ns = l2h & 0x00ffffff;
  778. L2TP_SKB_CB(skb)->has_seq = 1;
  779. }
  780. ptr += 4;
  781. }
  782. if (L2TP_SKB_CB(skb)->has_seq) {
  783. /* Received a packet with sequence numbers. If we're the LAC,
  784. * check if we sre sending sequence numbers and if not,
  785. * configure it so.
  786. */
  787. if (!session->lns_mode && !session->send_seq) {
  788. trace_session_seqnum_lns_enable(session);
  789. session->send_seq = 1;
  790. l2tp_session_set_header_len(session, tunnel->version,
  791. tunnel->encap);
  792. }
  793. } else {
  794. /* No sequence numbers.
  795. * If user has configured mandatory sequence numbers, discard.
  796. */
  797. if (session->recv_seq) {
  798. pr_debug_ratelimited("%s: recv data has no seq numbers when required. Discarding.\n",
  799. session->name);
  800. atomic_long_inc(&session->stats.rx_seq_discards);
  801. goto discard;
  802. }
  803. /* If we're the LAC and we're sending sequence numbers, the
  804. * LNS has requested that we no longer send sequence numbers.
  805. * If we're the LNS and we're sending sequence numbers, the
  806. * LAC is broken. Discard the frame.
  807. */
  808. if (!session->lns_mode && session->send_seq) {
  809. trace_session_seqnum_lns_disable(session);
  810. session->send_seq = 0;
  811. l2tp_session_set_header_len(session, tunnel->version,
  812. tunnel->encap);
  813. } else if (session->send_seq) {
  814. pr_debug_ratelimited("%s: recv data has no seq numbers when required. Discarding.\n",
  815. session->name);
  816. atomic_long_inc(&session->stats.rx_seq_discards);
  817. goto discard;
  818. }
  819. }
  820. /* Session data offset is defined only for L2TPv2 and is
  821. * indicated by an optional 16-bit value in the header.
  822. */
  823. if (tunnel->version == L2TP_HDR_VER_2) {
  824. /* If offset bit set, skip it. */
  825. if (hdrflags & L2TP_HDRFLAG_O) {
  826. offset = ntohs(*(__be16 *)ptr);
  827. ptr += 2 + offset;
  828. }
  829. }
  830. offset = ptr - optr;
  831. if (!pskb_may_pull(skb, offset))
  832. goto discard;
  833. __skb_pull(skb, offset);
  834. /* Prepare skb for adding to the session's reorder_q. Hold
  835. * packets for max reorder_timeout or 1 second if not
  836. * reordering.
  837. */
  838. L2TP_SKB_CB(skb)->length = length;
  839. L2TP_SKB_CB(skb)->expires = jiffies +
  840. (session->reorder_timeout ? session->reorder_timeout : HZ);
  841. /* Add packet to the session's receive queue. Reordering is done here, if
  842. * enabled. Saved L2TP protocol info is stored in skb->sb[].
  843. */
  844. if (L2TP_SKB_CB(skb)->has_seq) {
  845. if (l2tp_recv_data_seq(session, skb))
  846. goto discard;
  847. } else {
  848. /* No sequence numbers. Add the skb to the tail of the
  849. * reorder queue. This ensures that it will be
  850. * delivered after all previous sequenced skbs.
  851. */
  852. skb_queue_tail(&session->reorder_q, skb);
  853. }
  854. /* Try to dequeue as many skbs from reorder_q as we can. */
  855. l2tp_recv_dequeue(session);
  856. return;
  857. discard:
  858. atomic_long_inc(&session->stats.rx_errors);
  859. kfree_skb(skb);
  860. }
  861. EXPORT_SYMBOL_GPL(l2tp_recv_common);
  862. /* Drop skbs from the session's reorder_q
  863. */
  864. static void l2tp_session_queue_purge(struct l2tp_session *session)
  865. {
  866. struct sk_buff *skb = NULL;
  867. while ((skb = skb_dequeue(&session->reorder_q))) {
  868. atomic_long_inc(&session->stats.rx_errors);
  869. kfree_skb(skb);
  870. }
  871. }
  872. /* UDP encapsulation receive handler. See net/ipv4/udp.c for details. */
  873. int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  874. {
  875. struct l2tp_session *session = NULL;
  876. struct l2tp_tunnel *tunnel = NULL;
  877. struct net *net = sock_net(sk);
  878. unsigned char *ptr, *optr;
  879. u16 hdrflags;
  880. u16 version;
  881. int length;
  882. /* UDP has verified checksum */
  883. /* UDP always verifies the packet length. */
  884. __skb_pull(skb, sizeof(struct udphdr));
  885. /* Short packet? */
  886. if (!pskb_may_pull(skb, L2TP_HDR_SIZE_MAX))
  887. goto pass;
  888. /* Point to L2TP header */
  889. optr = skb->data;
  890. ptr = skb->data;
  891. /* Get L2TP header flags */
  892. hdrflags = ntohs(*(__be16 *)ptr);
  893. /* Get protocol version */
  894. version = hdrflags & L2TP_HDR_VER_MASK;
  895. /* Get length of L2TP packet */
  896. length = skb->len;
  897. /* If type is control packet, it is handled by userspace. */
  898. if (hdrflags & L2TP_HDRFLAG_T)
  899. goto pass;
  900. /* Skip flags */
  901. ptr += 2;
  902. if (version == L2TP_HDR_VER_2) {
  903. u16 tunnel_id, session_id;
  904. /* If length is present, skip it */
  905. if (hdrflags & L2TP_HDRFLAG_L)
  906. ptr += 2;
  907. /* Extract tunnel and session ID */
  908. tunnel_id = ntohs(*(__be16 *)ptr);
  909. ptr += 2;
  910. session_id = ntohs(*(__be16 *)ptr);
  911. ptr += 2;
  912. session = l2tp_v2_session_get(net, tunnel_id, session_id);
  913. } else {
  914. u32 session_id;
  915. ptr += 2; /* skip reserved bits */
  916. session_id = ntohl(*(__be32 *)ptr);
  917. ptr += 4;
  918. session = l2tp_v3_session_get(net, sk, session_id);
  919. }
  920. if (!session || !session->recv_skb) {
  921. if (session)
  922. l2tp_session_put(session);
  923. /* Not found? Pass to userspace to deal with */
  924. goto pass;
  925. }
  926. tunnel = session->tunnel;
  927. /* Check protocol version */
  928. if (version != tunnel->version) {
  929. l2tp_session_put(session);
  930. goto invalid;
  931. }
  932. if (version == L2TP_HDR_VER_3 &&
  933. l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr)) {
  934. l2tp_session_put(session);
  935. goto invalid;
  936. }
  937. l2tp_recv_common(session, skb, ptr, optr, hdrflags, length);
  938. l2tp_session_put(session);
  939. return 0;
  940. invalid:
  941. atomic_long_inc(&tunnel->stats.rx_invalid);
  942. pass:
  943. /* Put UDP header back */
  944. __skb_push(skb, sizeof(struct udphdr));
  945. return 1;
  946. }
  947. EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
  948. /* UDP encapsulation receive error handler. See net/ipv4/udp.c for details. */
  949. static void l2tp_udp_encap_err_recv(struct sock *sk, struct sk_buff *skb, int err,
  950. __be16 port, u32 info, u8 *payload)
  951. {
  952. sk->sk_err = err;
  953. sk_error_report(sk);
  954. if (ip_hdr(skb)->version == IPVERSION) {
  955. if (inet_test_bit(RECVERR, sk))
  956. return ip_icmp_error(sk, skb, err, port, info, payload);
  957. #if IS_ENABLED(CONFIG_IPV6)
  958. } else {
  959. if (inet6_test_bit(RECVERR6, sk))
  960. return ipv6_icmp_error(sk, skb, err, port, info, payload);
  961. #endif
  962. }
  963. }
  964. /************************************************************************
  965. * Transmit handling
  966. ***********************************************************************/
  967. /* Build an L2TP header for the session into the buffer provided.
  968. */
  969. static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
  970. {
  971. struct l2tp_tunnel *tunnel = session->tunnel;
  972. __be16 *bufp = buf;
  973. __be16 *optr = buf;
  974. u16 flags = L2TP_HDR_VER_2;
  975. u32 tunnel_id = tunnel->peer_tunnel_id;
  976. u32 session_id = session->peer_session_id;
  977. if (session->send_seq)
  978. flags |= L2TP_HDRFLAG_S;
  979. /* Setup L2TP header. */
  980. *bufp++ = htons(flags);
  981. *bufp++ = htons(tunnel_id);
  982. *bufp++ = htons(session_id);
  983. if (session->send_seq) {
  984. *bufp++ = htons(session->ns);
  985. *bufp++ = 0;
  986. session->ns++;
  987. session->ns &= 0xffff;
  988. trace_session_seqnum_update(session);
  989. }
  990. return bufp - optr;
  991. }
  992. static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
  993. {
  994. struct l2tp_tunnel *tunnel = session->tunnel;
  995. char *bufp = buf;
  996. char *optr = bufp;
  997. /* Setup L2TP header. The header differs slightly for UDP and
  998. * IP encapsulations. For UDP, there is 4 bytes of flags.
  999. */
  1000. if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
  1001. u16 flags = L2TP_HDR_VER_3;
  1002. *((__be16 *)bufp) = htons(flags);
  1003. bufp += 2;
  1004. *((__be16 *)bufp) = 0;
  1005. bufp += 2;
  1006. }
  1007. *((__be32 *)bufp) = htonl(session->peer_session_id);
  1008. bufp += 4;
  1009. if (session->cookie_len) {
  1010. memcpy(bufp, &session->cookie[0], session->cookie_len);
  1011. bufp += session->cookie_len;
  1012. }
  1013. if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
  1014. u32 l2h = 0;
  1015. if (session->send_seq) {
  1016. l2h = 0x40000000 | session->ns;
  1017. session->ns++;
  1018. session->ns &= 0xffffff;
  1019. trace_session_seqnum_update(session);
  1020. }
  1021. *((__be32 *)bufp) = htonl(l2h);
  1022. bufp += 4;
  1023. }
  1024. return bufp - optr;
  1025. }
  1026. /* Queue the packet to IP for output: tunnel socket lock must be held */
  1027. static int l2tp_xmit_queue(struct l2tp_tunnel *tunnel, struct sk_buff *skb, struct flowi *fl)
  1028. {
  1029. int err;
  1030. skb->ignore_df = 1;
  1031. skb_dst_drop(skb);
  1032. #if IS_ENABLED(CONFIG_IPV6)
  1033. if (l2tp_sk_is_v6(tunnel->sock))
  1034. err = inet6_csk_xmit(tunnel->sock, skb, NULL);
  1035. else
  1036. #endif
  1037. err = ip_queue_xmit(tunnel->sock, skb, fl);
  1038. return err >= 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
  1039. }
  1040. static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, unsigned int *len)
  1041. {
  1042. struct l2tp_tunnel *tunnel = session->tunnel;
  1043. unsigned int data_len = skb->len;
  1044. struct sock *sk = tunnel->sock;
  1045. int headroom, uhlen, udp_len;
  1046. int ret = NET_XMIT_SUCCESS;
  1047. struct inet_sock *inet;
  1048. struct udphdr *uh;
  1049. /* Check that there's enough headroom in the skb to insert IP,
  1050. * UDP and L2TP headers. If not enough, expand it to
  1051. * make room. Adjust truesize.
  1052. */
  1053. uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(*uh) : 0;
  1054. headroom = NET_SKB_PAD + sizeof(struct iphdr) + uhlen + session->hdr_len;
  1055. if (skb_cow_head(skb, headroom)) {
  1056. kfree_skb(skb);
  1057. return NET_XMIT_DROP;
  1058. }
  1059. /* Setup L2TP header */
  1060. if (tunnel->version == L2TP_HDR_VER_2)
  1061. l2tp_build_l2tpv2_header(session, __skb_push(skb, session->hdr_len));
  1062. else
  1063. l2tp_build_l2tpv3_header(session, __skb_push(skb, session->hdr_len));
  1064. /* Reset control buffer */
  1065. memset(skb->cb, 0, sizeof(skb->cb));
  1066. nf_reset_ct(skb);
  1067. /* L2TP uses its own lockdep subclass to avoid lockdep splats caused by
  1068. * nested socket calls on the same lockdep socket class. This can
  1069. * happen when data from a user socket is routed over l2tp, which uses
  1070. * another userspace socket.
  1071. */
  1072. spin_lock_nested(&sk->sk_lock.slock, L2TP_DEPTH_NESTING);
  1073. if (sock_owned_by_user(sk)) {
  1074. kfree_skb(skb);
  1075. ret = NET_XMIT_DROP;
  1076. goto out_unlock;
  1077. }
  1078. /* The user-space may change the connection status for the user-space
  1079. * provided socket at run time: we must check it under the socket lock
  1080. */
  1081. if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) {
  1082. kfree_skb(skb);
  1083. ret = NET_XMIT_DROP;
  1084. goto out_unlock;
  1085. }
  1086. /* Report transmitted length before we add encap header, which keeps
  1087. * statistics consistent for both UDP and IP encap tx/rx paths.
  1088. */
  1089. *len = skb->len;
  1090. inet = inet_sk(sk);
  1091. switch (tunnel->encap) {
  1092. case L2TP_ENCAPTYPE_UDP:
  1093. /* Setup UDP header */
  1094. __skb_push(skb, sizeof(*uh));
  1095. skb_reset_transport_header(skb);
  1096. uh = udp_hdr(skb);
  1097. uh->source = inet->inet_sport;
  1098. uh->dest = inet->inet_dport;
  1099. udp_len = uhlen + session->hdr_len + data_len;
  1100. if (udp_len > U16_MAX) {
  1101. kfree_skb(skb);
  1102. ret = NET_XMIT_DROP;
  1103. goto out_unlock;
  1104. }
  1105. uh->len = htons(udp_len);
  1106. /* Calculate UDP checksum if configured to do so */
  1107. #if IS_ENABLED(CONFIG_IPV6)
  1108. if (l2tp_sk_is_v6(sk))
  1109. udp6_set_csum(udp_get_no_check6_tx(sk),
  1110. skb, &inet6_sk(sk)->saddr,
  1111. &sk->sk_v6_daddr, udp_len);
  1112. else
  1113. #endif
  1114. udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
  1115. inet->inet_daddr, udp_len);
  1116. break;
  1117. case L2TP_ENCAPTYPE_IP:
  1118. break;
  1119. }
  1120. ret = l2tp_xmit_queue(tunnel, skb, &inet->cork.fl);
  1121. out_unlock:
  1122. spin_unlock(&sk->sk_lock.slock);
  1123. return ret;
  1124. }
  1125. /* If caller requires the skb to have a ppp header, the header must be
  1126. * inserted in the skb data before calling this function.
  1127. */
  1128. int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb)
  1129. {
  1130. unsigned int len = 0;
  1131. int ret;
  1132. ret = l2tp_xmit_core(session, skb, &len);
  1133. if (ret == NET_XMIT_SUCCESS) {
  1134. atomic_long_inc(&session->tunnel->stats.tx_packets);
  1135. atomic_long_add(len, &session->tunnel->stats.tx_bytes);
  1136. atomic_long_inc(&session->stats.tx_packets);
  1137. atomic_long_add(len, &session->stats.tx_bytes);
  1138. } else {
  1139. atomic_long_inc(&session->tunnel->stats.tx_errors);
  1140. atomic_long_inc(&session->stats.tx_errors);
  1141. }
  1142. return ret;
  1143. }
  1144. EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
  1145. /*****************************************************************************
  1146. * Tinnel and session create/destroy.
  1147. *****************************************************************************/
  1148. /* Remove an l2tp session from l2tp_core's lists. */
  1149. static void l2tp_session_unhash(struct l2tp_session *session)
  1150. {
  1151. struct l2tp_tunnel *tunnel = session->tunnel;
  1152. if (tunnel) {
  1153. struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
  1154. struct l2tp_session *removed = session;
  1155. spin_lock_bh(&tunnel->list_lock);
  1156. spin_lock_bh(&pn->l2tp_session_idr_lock);
  1157. /* Remove from the per-tunnel list */
  1158. list_del_init(&session->list);
  1159. /* Remove from per-net IDR */
  1160. if (tunnel->version == L2TP_HDR_VER_3) {
  1161. if (hash_hashed(&session->hlist))
  1162. l2tp_session_collision_del(pn, session);
  1163. else
  1164. removed = idr_remove(&pn->l2tp_v3_session_idr,
  1165. session->session_id);
  1166. } else {
  1167. u32 session_key = l2tp_v2_session_key(tunnel->tunnel_id,
  1168. session->session_id);
  1169. removed = idr_remove(&pn->l2tp_v2_session_idr,
  1170. session_key);
  1171. }
  1172. WARN_ON_ONCE(removed && removed != session);
  1173. spin_unlock_bh(&pn->l2tp_session_idr_lock);
  1174. spin_unlock_bh(&tunnel->list_lock);
  1175. }
  1176. }
  1177. /* When the tunnel is closed, all the attached sessions need to go too.
  1178. */
  1179. static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
  1180. {
  1181. struct l2tp_session *session;
  1182. spin_lock_bh(&tunnel->list_lock);
  1183. tunnel->acpt_newsess = false;
  1184. list_for_each_entry(session, &tunnel->session_list, list)
  1185. l2tp_session_delete(session);
  1186. spin_unlock_bh(&tunnel->list_lock);
  1187. }
  1188. /* Tunnel socket destroy hook for UDP encapsulation */
  1189. static void l2tp_udp_encap_destroy(struct sock *sk)
  1190. {
  1191. struct l2tp_tunnel *tunnel;
  1192. tunnel = l2tp_sk_to_tunnel(sk);
  1193. if (tunnel) {
  1194. l2tp_tunnel_delete(tunnel);
  1195. l2tp_tunnel_put(tunnel);
  1196. }
  1197. }
  1198. static void l2tp_tunnel_remove(struct net *net, struct l2tp_tunnel *tunnel)
  1199. {
  1200. struct l2tp_net *pn = l2tp_pernet(net);
  1201. spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
  1202. idr_remove(&pn->l2tp_tunnel_idr, tunnel->tunnel_id);
  1203. spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
  1204. }
  1205. /* Workqueue tunnel deletion function */
  1206. static void l2tp_tunnel_del_work(struct work_struct *work)
  1207. {
  1208. struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel,
  1209. del_work);
  1210. l2tp_tunnel_closeall(tunnel);
  1211. /* If the tunnel socket was created within the kernel, use
  1212. * the sk API to release it here.
  1213. */
  1214. if (tunnel->fd < 0) {
  1215. struct socket *sock = tunnel->sock->sk_socket;
  1216. if (sock) {
  1217. kernel_sock_shutdown(sock, SHUT_RDWR);
  1218. sock_release(sock);
  1219. }
  1220. }
  1221. l2tp_tunnel_remove(tunnel->l2tp_net, tunnel);
  1222. /* drop initial ref */
  1223. l2tp_tunnel_put(tunnel);
  1224. /* drop workqueue ref */
  1225. l2tp_tunnel_put(tunnel);
  1226. }
  1227. /* Create a socket for the tunnel, if one isn't set up by
  1228. * userspace. This is used for static tunnels where there is no
  1229. * managing L2TP daemon.
  1230. *
  1231. * Since we don't want these sockets to keep a namespace alive by
  1232. * themselves, we drop the socket's namespace refcount after creation.
  1233. * These sockets are freed when the namespace exits using the pernet
  1234. * exit hook.
  1235. */
  1236. static int l2tp_tunnel_sock_create(struct net *net,
  1237. u32 tunnel_id,
  1238. u32 peer_tunnel_id,
  1239. struct l2tp_tunnel_cfg *cfg,
  1240. struct socket **sockp)
  1241. {
  1242. int err = -EINVAL;
  1243. struct socket *sock = NULL;
  1244. struct udp_port_cfg udp_conf;
  1245. switch (cfg->encap) {
  1246. case L2TP_ENCAPTYPE_UDP:
  1247. memset(&udp_conf, 0, sizeof(udp_conf));
  1248. #if IS_ENABLED(CONFIG_IPV6)
  1249. if (cfg->local_ip6 && cfg->peer_ip6) {
  1250. udp_conf.family = AF_INET6;
  1251. memcpy(&udp_conf.local_ip6, cfg->local_ip6,
  1252. sizeof(udp_conf.local_ip6));
  1253. memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
  1254. sizeof(udp_conf.peer_ip6));
  1255. udp_conf.use_udp6_tx_checksums =
  1256. !cfg->udp6_zero_tx_checksums;
  1257. udp_conf.use_udp6_rx_checksums =
  1258. !cfg->udp6_zero_rx_checksums;
  1259. } else
  1260. #endif
  1261. {
  1262. udp_conf.family = AF_INET;
  1263. udp_conf.local_ip = cfg->local_ip;
  1264. udp_conf.peer_ip = cfg->peer_ip;
  1265. udp_conf.use_udp_checksums = cfg->use_udp_checksums;
  1266. }
  1267. udp_conf.local_udp_port = htons(cfg->local_udp_port);
  1268. udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
  1269. err = udp_sock_create(net, &udp_conf, &sock);
  1270. if (err < 0)
  1271. goto out;
  1272. break;
  1273. case L2TP_ENCAPTYPE_IP:
  1274. #if IS_ENABLED(CONFIG_IPV6)
  1275. if (cfg->local_ip6 && cfg->peer_ip6) {
  1276. struct sockaddr_l2tpip6 ip6_addr = {0};
  1277. err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
  1278. IPPROTO_L2TP, &sock);
  1279. if (err < 0)
  1280. goto out;
  1281. ip6_addr.l2tp_family = AF_INET6;
  1282. memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
  1283. sizeof(ip6_addr.l2tp_addr));
  1284. ip6_addr.l2tp_conn_id = tunnel_id;
  1285. err = kernel_bind(sock, (struct sockaddr_unsized *)&ip6_addr,
  1286. sizeof(ip6_addr));
  1287. if (err < 0)
  1288. goto out;
  1289. ip6_addr.l2tp_family = AF_INET6;
  1290. memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
  1291. sizeof(ip6_addr.l2tp_addr));
  1292. ip6_addr.l2tp_conn_id = peer_tunnel_id;
  1293. err = kernel_connect(sock,
  1294. (struct sockaddr_unsized *)&ip6_addr,
  1295. sizeof(ip6_addr), 0);
  1296. if (err < 0)
  1297. goto out;
  1298. } else
  1299. #endif
  1300. {
  1301. struct sockaddr_l2tpip ip_addr = {0};
  1302. err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
  1303. IPPROTO_L2TP, &sock);
  1304. if (err < 0)
  1305. goto out;
  1306. ip_addr.l2tp_family = AF_INET;
  1307. ip_addr.l2tp_addr = cfg->local_ip;
  1308. ip_addr.l2tp_conn_id = tunnel_id;
  1309. err = kernel_bind(sock, (struct sockaddr_unsized *)&ip_addr,
  1310. sizeof(ip_addr));
  1311. if (err < 0)
  1312. goto out;
  1313. ip_addr.l2tp_family = AF_INET;
  1314. ip_addr.l2tp_addr = cfg->peer_ip;
  1315. ip_addr.l2tp_conn_id = peer_tunnel_id;
  1316. err = kernel_connect(sock, (struct sockaddr_unsized *)&ip_addr,
  1317. sizeof(ip_addr), 0);
  1318. if (err < 0)
  1319. goto out;
  1320. }
  1321. break;
  1322. default:
  1323. goto out;
  1324. }
  1325. out:
  1326. *sockp = sock;
  1327. if (err < 0 && sock) {
  1328. kernel_sock_shutdown(sock, SHUT_RDWR);
  1329. sock_release(sock);
  1330. *sockp = NULL;
  1331. }
  1332. return err;
  1333. }
  1334. int l2tp_tunnel_create(int fd, int version, u32 tunnel_id, u32 peer_tunnel_id,
  1335. struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp)
  1336. {
  1337. struct l2tp_tunnel *tunnel = NULL;
  1338. int err;
  1339. enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
  1340. if (cfg)
  1341. encap = cfg->encap;
  1342. tunnel = kzalloc_obj(*tunnel);
  1343. if (!tunnel) {
  1344. err = -ENOMEM;
  1345. goto err;
  1346. }
  1347. tunnel->version = version;
  1348. tunnel->tunnel_id = tunnel_id;
  1349. tunnel->peer_tunnel_id = peer_tunnel_id;
  1350. sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
  1351. spin_lock_init(&tunnel->list_lock);
  1352. tunnel->acpt_newsess = true;
  1353. INIT_LIST_HEAD(&tunnel->session_list);
  1354. tunnel->encap = encap;
  1355. refcount_set(&tunnel->ref_count, 1);
  1356. tunnel->fd = fd;
  1357. /* Init delete workqueue struct */
  1358. INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
  1359. err = 0;
  1360. err:
  1361. if (tunnelp)
  1362. *tunnelp = tunnel;
  1363. return err;
  1364. }
  1365. EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
  1366. static int l2tp_validate_socket(const struct sock *sk, const struct net *net,
  1367. enum l2tp_encap_type encap)
  1368. {
  1369. struct l2tp_tunnel *tunnel;
  1370. if (!net_eq(sock_net(sk), net))
  1371. return -EINVAL;
  1372. if (sk->sk_type != SOCK_DGRAM)
  1373. return -EPROTONOSUPPORT;
  1374. if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
  1375. return -EPROTONOSUPPORT;
  1376. if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) ||
  1377. (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP))
  1378. return -EPROTONOSUPPORT;
  1379. if (encap == L2TP_ENCAPTYPE_UDP && sk->sk_user_data)
  1380. return -EBUSY;
  1381. tunnel = l2tp_sk_to_tunnel(sk);
  1382. if (tunnel) {
  1383. l2tp_tunnel_put(tunnel);
  1384. return -EBUSY;
  1385. }
  1386. return 0;
  1387. }
  1388. int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
  1389. struct l2tp_tunnel_cfg *cfg)
  1390. {
  1391. struct l2tp_net *pn = l2tp_pernet(net);
  1392. u32 tunnel_id = tunnel->tunnel_id;
  1393. struct socket *sock;
  1394. struct sock *sk;
  1395. int ret;
  1396. spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
  1397. ret = idr_alloc_u32(&pn->l2tp_tunnel_idr, NULL, &tunnel_id, tunnel_id,
  1398. GFP_ATOMIC);
  1399. spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
  1400. if (ret)
  1401. return ret == -ENOSPC ? -EEXIST : ret;
  1402. if (tunnel->fd < 0) {
  1403. ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id,
  1404. tunnel->peer_tunnel_id, cfg,
  1405. &sock);
  1406. if (ret < 0)
  1407. goto err;
  1408. } else {
  1409. sock = sockfd_lookup(tunnel->fd, &ret);
  1410. if (!sock)
  1411. goto err;
  1412. }
  1413. sk = sock->sk;
  1414. lock_sock(sk);
  1415. write_lock_bh(&sk->sk_callback_lock);
  1416. ret = l2tp_validate_socket(sk, net, tunnel->encap);
  1417. if (ret < 0)
  1418. goto err_inval_sock;
  1419. write_unlock_bh(&sk->sk_callback_lock);
  1420. if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
  1421. struct udp_tunnel_sock_cfg udp_cfg = {
  1422. .encap_type = UDP_ENCAP_L2TPINUDP,
  1423. .encap_rcv = l2tp_udp_encap_recv,
  1424. .encap_err_rcv = l2tp_udp_encap_err_recv,
  1425. .encap_destroy = l2tp_udp_encap_destroy,
  1426. };
  1427. setup_udp_tunnel_sock(net, sock, &udp_cfg);
  1428. }
  1429. sk->sk_allocation = GFP_ATOMIC;
  1430. release_sock(sk);
  1431. sock_hold(sk);
  1432. tunnel->sock = sk;
  1433. tunnel->l2tp_net = net;
  1434. spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
  1435. idr_replace(&pn->l2tp_tunnel_idr, tunnel, tunnel->tunnel_id);
  1436. spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
  1437. trace_register_tunnel(tunnel);
  1438. if (tunnel->fd >= 0)
  1439. sockfd_put(sock);
  1440. return 0;
  1441. err_inval_sock:
  1442. write_unlock_bh(&sk->sk_callback_lock);
  1443. release_sock(sk);
  1444. if (tunnel->fd < 0)
  1445. sock_release(sock);
  1446. else
  1447. sockfd_put(sock);
  1448. err:
  1449. l2tp_tunnel_remove(net, tunnel);
  1450. return ret;
  1451. }
  1452. EXPORT_SYMBOL_GPL(l2tp_tunnel_register);
  1453. /* This function is used by the netlink TUNNEL_DELETE command.
  1454. */
  1455. void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
  1456. {
  1457. if (!test_and_set_bit(0, &tunnel->dead)) {
  1458. trace_delete_tunnel(tunnel);
  1459. refcount_inc(&tunnel->ref_count);
  1460. queue_work(l2tp_wq, &tunnel->del_work);
  1461. }
  1462. }
  1463. EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
  1464. void l2tp_session_delete(struct l2tp_session *session)
  1465. {
  1466. if (!test_and_set_bit(0, &session->dead)) {
  1467. trace_delete_session(session);
  1468. refcount_inc(&session->ref_count);
  1469. queue_work(l2tp_wq, &session->del_work);
  1470. }
  1471. }
  1472. EXPORT_SYMBOL_GPL(l2tp_session_delete);
  1473. /* Workqueue session deletion function */
  1474. static void l2tp_session_del_work(struct work_struct *work)
  1475. {
  1476. struct l2tp_session *session = container_of(work, struct l2tp_session,
  1477. del_work);
  1478. l2tp_session_unhash(session);
  1479. l2tp_session_queue_purge(session);
  1480. if (session->session_close)
  1481. (*session->session_close)(session);
  1482. /* drop initial ref */
  1483. l2tp_session_put(session);
  1484. /* drop workqueue ref */
  1485. l2tp_session_put(session);
  1486. }
  1487. /* We come here whenever a session's send_seq, cookie_len or
  1488. * l2specific_type parameters are set.
  1489. */
  1490. void l2tp_session_set_header_len(struct l2tp_session *session, int version,
  1491. enum l2tp_encap_type encap)
  1492. {
  1493. if (version == L2TP_HDR_VER_2) {
  1494. session->hdr_len = 6;
  1495. if (session->send_seq)
  1496. session->hdr_len += 4;
  1497. } else {
  1498. session->hdr_len = 4 + session->cookie_len;
  1499. session->hdr_len += l2tp_get_l2specific_len(session);
  1500. if (encap == L2TP_ENCAPTYPE_UDP)
  1501. session->hdr_len += 4;
  1502. }
  1503. }
  1504. EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
  1505. struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id,
  1506. u32 peer_session_id, struct l2tp_session_cfg *cfg)
  1507. {
  1508. struct l2tp_session *session;
  1509. session = kzalloc(sizeof(*session) + priv_size, GFP_KERNEL);
  1510. if (session) {
  1511. session->magic = L2TP_SESSION_MAGIC;
  1512. session->session_id = session_id;
  1513. session->peer_session_id = peer_session_id;
  1514. session->nr = 0;
  1515. if (tunnel->version == L2TP_HDR_VER_2)
  1516. session->nr_max = 0xffff;
  1517. else
  1518. session->nr_max = 0xffffff;
  1519. session->nr_window_size = session->nr_max / 2;
  1520. session->nr_oos_count_max = 4;
  1521. /* Use NR of first received packet */
  1522. session->reorder_skip = 1;
  1523. sprintf(&session->name[0], "sess %u/%u",
  1524. tunnel->tunnel_id, session->session_id);
  1525. skb_queue_head_init(&session->reorder_q);
  1526. session->hlist_key = l2tp_v3_session_hashkey(tunnel->sock, session->session_id);
  1527. INIT_HLIST_NODE(&session->hlist);
  1528. INIT_LIST_HEAD(&session->clist);
  1529. INIT_LIST_HEAD(&session->list);
  1530. INIT_WORK(&session->del_work, l2tp_session_del_work);
  1531. if (cfg) {
  1532. session->pwtype = cfg->pw_type;
  1533. session->send_seq = cfg->send_seq;
  1534. session->recv_seq = cfg->recv_seq;
  1535. session->lns_mode = cfg->lns_mode;
  1536. session->reorder_timeout = cfg->reorder_timeout;
  1537. session->l2specific_type = cfg->l2specific_type;
  1538. session->cookie_len = cfg->cookie_len;
  1539. memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
  1540. session->peer_cookie_len = cfg->peer_cookie_len;
  1541. memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
  1542. }
  1543. l2tp_session_set_header_len(session, tunnel->version, tunnel->encap);
  1544. refcount_set(&session->ref_count, 1);
  1545. return session;
  1546. }
  1547. return ERR_PTR(-ENOMEM);
  1548. }
  1549. EXPORT_SYMBOL_GPL(l2tp_session_create);
  1550. /*****************************************************************************
  1551. * Init and cleanup
  1552. *****************************************************************************/
  1553. static __net_init int l2tp_init_net(struct net *net)
  1554. {
  1555. struct l2tp_net *pn = net_generic(net, l2tp_net_id);
  1556. idr_init(&pn->l2tp_tunnel_idr);
  1557. spin_lock_init(&pn->l2tp_tunnel_idr_lock);
  1558. idr_init(&pn->l2tp_v2_session_idr);
  1559. idr_init(&pn->l2tp_v3_session_idr);
  1560. spin_lock_init(&pn->l2tp_session_idr_lock);
  1561. return 0;
  1562. }
  1563. static __net_exit void l2tp_pre_exit_net(struct net *net)
  1564. {
  1565. struct l2tp_net *pn = l2tp_pernet(net);
  1566. struct l2tp_tunnel *tunnel = NULL;
  1567. unsigned long tunnel_id, tmp;
  1568. rcu_read_lock_bh();
  1569. idr_for_each_entry_ul(&pn->l2tp_tunnel_idr, tunnel, tmp, tunnel_id) {
  1570. if (tunnel)
  1571. l2tp_tunnel_delete(tunnel);
  1572. }
  1573. rcu_read_unlock_bh();
  1574. if (l2tp_wq) {
  1575. /* Run all TUNNEL_DELETE work items just queued. */
  1576. __flush_workqueue(l2tp_wq);
  1577. /* Each TUNNEL_DELETE work item will queue a SESSION_DELETE
  1578. * work item for each session in the tunnel. Flush the
  1579. * workqueue again to process these.
  1580. */
  1581. __flush_workqueue(l2tp_wq);
  1582. }
  1583. }
  1584. static int l2tp_idr_item_unexpected(int id, void *p, void *data)
  1585. {
  1586. const char *idr_name = data;
  1587. pr_err("l2tp: %s IDR not empty at net %d exit\n", idr_name, id);
  1588. WARN_ON_ONCE(1);
  1589. return 1;
  1590. }
  1591. static __net_exit void l2tp_exit_net(struct net *net)
  1592. {
  1593. struct l2tp_net *pn = l2tp_pernet(net);
  1594. /* Our per-net IDRs should be empty. Check that is so, to
  1595. * help catch cleanup races or refcnt leaks.
  1596. */
  1597. idr_for_each(&pn->l2tp_v2_session_idr, l2tp_idr_item_unexpected,
  1598. "v2_session");
  1599. idr_for_each(&pn->l2tp_v3_session_idr, l2tp_idr_item_unexpected,
  1600. "v3_session");
  1601. idr_for_each(&pn->l2tp_tunnel_idr, l2tp_idr_item_unexpected,
  1602. "tunnel");
  1603. idr_destroy(&pn->l2tp_v2_session_idr);
  1604. idr_destroy(&pn->l2tp_v3_session_idr);
  1605. idr_destroy(&pn->l2tp_tunnel_idr);
  1606. }
  1607. static struct pernet_operations l2tp_net_ops = {
  1608. .init = l2tp_init_net,
  1609. .exit = l2tp_exit_net,
  1610. .pre_exit = l2tp_pre_exit_net,
  1611. .id = &l2tp_net_id,
  1612. .size = sizeof(struct l2tp_net),
  1613. };
  1614. static int __init l2tp_init(void)
  1615. {
  1616. int rc = 0;
  1617. rc = register_pernet_device(&l2tp_net_ops);
  1618. if (rc)
  1619. goto out;
  1620. l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
  1621. if (!l2tp_wq) {
  1622. pr_err("alloc_workqueue failed\n");
  1623. unregister_pernet_device(&l2tp_net_ops);
  1624. rc = -ENOMEM;
  1625. goto out;
  1626. }
  1627. pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
  1628. out:
  1629. return rc;
  1630. }
  1631. static void __exit l2tp_exit(void)
  1632. {
  1633. unregister_pernet_device(&l2tp_net_ops);
  1634. if (l2tp_wq) {
  1635. destroy_workqueue(l2tp_wq);
  1636. l2tp_wq = NULL;
  1637. }
  1638. }
  1639. module_init(l2tp_init);
  1640. module_exit(l2tp_exit);
  1641. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  1642. MODULE_DESCRIPTION("L2TP core");
  1643. MODULE_LICENSE("GPL");
  1644. MODULE_VERSION(L2TP_DRV_VERSION);