l2tp_core.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946
  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(sizeof(*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. goto invalid;
  930. if (version == L2TP_HDR_VER_3 &&
  931. l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr)) {
  932. l2tp_session_put(session);
  933. goto invalid;
  934. }
  935. l2tp_recv_common(session, skb, ptr, optr, hdrflags, length);
  936. l2tp_session_put(session);
  937. return 0;
  938. invalid:
  939. atomic_long_inc(&tunnel->stats.rx_invalid);
  940. pass:
  941. /* Put UDP header back */
  942. __skb_push(skb, sizeof(struct udphdr));
  943. return 1;
  944. }
  945. EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
  946. /* UDP encapsulation receive error handler. See net/ipv4/udp.c for details. */
  947. static void l2tp_udp_encap_err_recv(struct sock *sk, struct sk_buff *skb, int err,
  948. __be16 port, u32 info, u8 *payload)
  949. {
  950. sk->sk_err = err;
  951. sk_error_report(sk);
  952. if (ip_hdr(skb)->version == IPVERSION) {
  953. if (inet_test_bit(RECVERR, sk))
  954. return ip_icmp_error(sk, skb, err, port, info, payload);
  955. #if IS_ENABLED(CONFIG_IPV6)
  956. } else {
  957. if (inet6_test_bit(RECVERR6, sk))
  958. return ipv6_icmp_error(sk, skb, err, port, info, payload);
  959. #endif
  960. }
  961. }
  962. /************************************************************************
  963. * Transmit handling
  964. ***********************************************************************/
  965. /* Build an L2TP header for the session into the buffer provided.
  966. */
  967. static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
  968. {
  969. struct l2tp_tunnel *tunnel = session->tunnel;
  970. __be16 *bufp = buf;
  971. __be16 *optr = buf;
  972. u16 flags = L2TP_HDR_VER_2;
  973. u32 tunnel_id = tunnel->peer_tunnel_id;
  974. u32 session_id = session->peer_session_id;
  975. if (session->send_seq)
  976. flags |= L2TP_HDRFLAG_S;
  977. /* Setup L2TP header. */
  978. *bufp++ = htons(flags);
  979. *bufp++ = htons(tunnel_id);
  980. *bufp++ = htons(session_id);
  981. if (session->send_seq) {
  982. *bufp++ = htons(session->ns);
  983. *bufp++ = 0;
  984. session->ns++;
  985. session->ns &= 0xffff;
  986. trace_session_seqnum_update(session);
  987. }
  988. return bufp - optr;
  989. }
  990. static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
  991. {
  992. struct l2tp_tunnel *tunnel = session->tunnel;
  993. char *bufp = buf;
  994. char *optr = bufp;
  995. /* Setup L2TP header. The header differs slightly for UDP and
  996. * IP encapsulations. For UDP, there is 4 bytes of flags.
  997. */
  998. if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
  999. u16 flags = L2TP_HDR_VER_3;
  1000. *((__be16 *)bufp) = htons(flags);
  1001. bufp += 2;
  1002. *((__be16 *)bufp) = 0;
  1003. bufp += 2;
  1004. }
  1005. *((__be32 *)bufp) = htonl(session->peer_session_id);
  1006. bufp += 4;
  1007. if (session->cookie_len) {
  1008. memcpy(bufp, &session->cookie[0], session->cookie_len);
  1009. bufp += session->cookie_len;
  1010. }
  1011. if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
  1012. u32 l2h = 0;
  1013. if (session->send_seq) {
  1014. l2h = 0x40000000 | session->ns;
  1015. session->ns++;
  1016. session->ns &= 0xffffff;
  1017. trace_session_seqnum_update(session);
  1018. }
  1019. *((__be32 *)bufp) = htonl(l2h);
  1020. bufp += 4;
  1021. }
  1022. return bufp - optr;
  1023. }
  1024. /* Queue the packet to IP for output: tunnel socket lock must be held */
  1025. static int l2tp_xmit_queue(struct l2tp_tunnel *tunnel, struct sk_buff *skb, struct flowi *fl)
  1026. {
  1027. int err;
  1028. skb->ignore_df = 1;
  1029. skb_dst_drop(skb);
  1030. #if IS_ENABLED(CONFIG_IPV6)
  1031. if (l2tp_sk_is_v6(tunnel->sock))
  1032. err = inet6_csk_xmit(tunnel->sock, skb, NULL);
  1033. else
  1034. #endif
  1035. err = ip_queue_xmit(tunnel->sock, skb, fl);
  1036. return err >= 0 ? NET_XMIT_SUCCESS : NET_XMIT_DROP;
  1037. }
  1038. static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, unsigned int *len)
  1039. {
  1040. struct l2tp_tunnel *tunnel = session->tunnel;
  1041. unsigned int data_len = skb->len;
  1042. struct sock *sk = tunnel->sock;
  1043. int headroom, uhlen, udp_len;
  1044. int ret = NET_XMIT_SUCCESS;
  1045. struct inet_sock *inet;
  1046. struct udphdr *uh;
  1047. /* Check that there's enough headroom in the skb to insert IP,
  1048. * UDP and L2TP headers. If not enough, expand it to
  1049. * make room. Adjust truesize.
  1050. */
  1051. uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(*uh) : 0;
  1052. headroom = NET_SKB_PAD + sizeof(struct iphdr) + uhlen + session->hdr_len;
  1053. if (skb_cow_head(skb, headroom)) {
  1054. kfree_skb(skb);
  1055. return NET_XMIT_DROP;
  1056. }
  1057. /* Setup L2TP header */
  1058. if (tunnel->version == L2TP_HDR_VER_2)
  1059. l2tp_build_l2tpv2_header(session, __skb_push(skb, session->hdr_len));
  1060. else
  1061. l2tp_build_l2tpv3_header(session, __skb_push(skb, session->hdr_len));
  1062. /* Reset skb netfilter state */
  1063. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  1064. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | IPSKB_REROUTED);
  1065. nf_reset_ct(skb);
  1066. /* L2TP uses its own lockdep subclass to avoid lockdep splats caused by
  1067. * nested socket calls on the same lockdep socket class. This can
  1068. * happen when data from a user socket is routed over l2tp, which uses
  1069. * another userspace socket.
  1070. */
  1071. spin_lock_nested(&sk->sk_lock.slock, L2TP_DEPTH_NESTING);
  1072. if (sock_owned_by_user(sk)) {
  1073. kfree_skb(skb);
  1074. ret = NET_XMIT_DROP;
  1075. goto out_unlock;
  1076. }
  1077. /* The user-space may change the connection status for the user-space
  1078. * provided socket at run time: we must check it under the socket lock
  1079. */
  1080. if (tunnel->fd >= 0 && sk->sk_state != TCP_ESTABLISHED) {
  1081. kfree_skb(skb);
  1082. ret = NET_XMIT_DROP;
  1083. goto out_unlock;
  1084. }
  1085. /* Report transmitted length before we add encap header, which keeps
  1086. * statistics consistent for both UDP and IP encap tx/rx paths.
  1087. */
  1088. *len = skb->len;
  1089. inet = inet_sk(sk);
  1090. switch (tunnel->encap) {
  1091. case L2TP_ENCAPTYPE_UDP:
  1092. /* Setup UDP header */
  1093. __skb_push(skb, sizeof(*uh));
  1094. skb_reset_transport_header(skb);
  1095. uh = udp_hdr(skb);
  1096. uh->source = inet->inet_sport;
  1097. uh->dest = inet->inet_dport;
  1098. udp_len = uhlen + session->hdr_len + data_len;
  1099. uh->len = htons(udp_len);
  1100. /* Calculate UDP checksum if configured to do so */
  1101. #if IS_ENABLED(CONFIG_IPV6)
  1102. if (l2tp_sk_is_v6(sk))
  1103. udp6_set_csum(udp_get_no_check6_tx(sk),
  1104. skb, &inet6_sk(sk)->saddr,
  1105. &sk->sk_v6_daddr, udp_len);
  1106. else
  1107. #endif
  1108. udp_set_csum(sk->sk_no_check_tx, skb, inet->inet_saddr,
  1109. inet->inet_daddr, udp_len);
  1110. break;
  1111. case L2TP_ENCAPTYPE_IP:
  1112. break;
  1113. }
  1114. ret = l2tp_xmit_queue(tunnel, skb, &inet->cork.fl);
  1115. out_unlock:
  1116. spin_unlock(&sk->sk_lock.slock);
  1117. return ret;
  1118. }
  1119. /* If caller requires the skb to have a ppp header, the header must be
  1120. * inserted in the skb data before calling this function.
  1121. */
  1122. int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb)
  1123. {
  1124. unsigned int len = 0;
  1125. int ret;
  1126. ret = l2tp_xmit_core(session, skb, &len);
  1127. if (ret == NET_XMIT_SUCCESS) {
  1128. atomic_long_inc(&session->tunnel->stats.tx_packets);
  1129. atomic_long_add(len, &session->tunnel->stats.tx_bytes);
  1130. atomic_long_inc(&session->stats.tx_packets);
  1131. atomic_long_add(len, &session->stats.tx_bytes);
  1132. } else {
  1133. atomic_long_inc(&session->tunnel->stats.tx_errors);
  1134. atomic_long_inc(&session->stats.tx_errors);
  1135. }
  1136. return ret;
  1137. }
  1138. EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
  1139. /*****************************************************************************
  1140. * Tinnel and session create/destroy.
  1141. *****************************************************************************/
  1142. /* Remove an l2tp session from l2tp_core's lists. */
  1143. static void l2tp_session_unhash(struct l2tp_session *session)
  1144. {
  1145. struct l2tp_tunnel *tunnel = session->tunnel;
  1146. if (tunnel) {
  1147. struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
  1148. struct l2tp_session *removed = session;
  1149. spin_lock_bh(&tunnel->list_lock);
  1150. spin_lock_bh(&pn->l2tp_session_idr_lock);
  1151. /* Remove from the per-tunnel list */
  1152. list_del_init(&session->list);
  1153. /* Remove from per-net IDR */
  1154. if (tunnel->version == L2TP_HDR_VER_3) {
  1155. if (hash_hashed(&session->hlist))
  1156. l2tp_session_collision_del(pn, session);
  1157. else
  1158. removed = idr_remove(&pn->l2tp_v3_session_idr,
  1159. session->session_id);
  1160. } else {
  1161. u32 session_key = l2tp_v2_session_key(tunnel->tunnel_id,
  1162. session->session_id);
  1163. removed = idr_remove(&pn->l2tp_v2_session_idr,
  1164. session_key);
  1165. }
  1166. WARN_ON_ONCE(removed && removed != session);
  1167. spin_unlock_bh(&pn->l2tp_session_idr_lock);
  1168. spin_unlock_bh(&tunnel->list_lock);
  1169. }
  1170. }
  1171. /* When the tunnel is closed, all the attached sessions need to go too.
  1172. */
  1173. static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
  1174. {
  1175. struct l2tp_session *session;
  1176. spin_lock_bh(&tunnel->list_lock);
  1177. tunnel->acpt_newsess = false;
  1178. list_for_each_entry(session, &tunnel->session_list, list)
  1179. l2tp_session_delete(session);
  1180. spin_unlock_bh(&tunnel->list_lock);
  1181. }
  1182. /* Tunnel socket destroy hook for UDP encapsulation */
  1183. static void l2tp_udp_encap_destroy(struct sock *sk)
  1184. {
  1185. struct l2tp_tunnel *tunnel;
  1186. tunnel = l2tp_sk_to_tunnel(sk);
  1187. if (tunnel) {
  1188. l2tp_tunnel_delete(tunnel);
  1189. l2tp_tunnel_put(tunnel);
  1190. }
  1191. }
  1192. static void l2tp_tunnel_remove(struct net *net, struct l2tp_tunnel *tunnel)
  1193. {
  1194. struct l2tp_net *pn = l2tp_pernet(net);
  1195. spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
  1196. idr_remove(&pn->l2tp_tunnel_idr, tunnel->tunnel_id);
  1197. spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
  1198. }
  1199. /* Workqueue tunnel deletion function */
  1200. static void l2tp_tunnel_del_work(struct work_struct *work)
  1201. {
  1202. struct l2tp_tunnel *tunnel = container_of(work, struct l2tp_tunnel,
  1203. del_work);
  1204. struct sock *sk = tunnel->sock;
  1205. struct socket *sock = sk->sk_socket;
  1206. l2tp_tunnel_closeall(tunnel);
  1207. /* If the tunnel socket was created within the kernel, use
  1208. * the sk API to release it here.
  1209. */
  1210. if (tunnel->fd < 0) {
  1211. if (sock) {
  1212. kernel_sock_shutdown(sock, SHUT_RDWR);
  1213. sock_release(sock);
  1214. }
  1215. }
  1216. l2tp_tunnel_remove(tunnel->l2tp_net, tunnel);
  1217. /* drop initial ref */
  1218. l2tp_tunnel_put(tunnel);
  1219. /* drop workqueue ref */
  1220. l2tp_tunnel_put(tunnel);
  1221. }
  1222. /* Create a socket for the tunnel, if one isn't set up by
  1223. * userspace. This is used for static tunnels where there is no
  1224. * managing L2TP daemon.
  1225. *
  1226. * Since we don't want these sockets to keep a namespace alive by
  1227. * themselves, we drop the socket's namespace refcount after creation.
  1228. * These sockets are freed when the namespace exits using the pernet
  1229. * exit hook.
  1230. */
  1231. static int l2tp_tunnel_sock_create(struct net *net,
  1232. u32 tunnel_id,
  1233. u32 peer_tunnel_id,
  1234. struct l2tp_tunnel_cfg *cfg,
  1235. struct socket **sockp)
  1236. {
  1237. int err = -EINVAL;
  1238. struct socket *sock = NULL;
  1239. struct udp_port_cfg udp_conf;
  1240. switch (cfg->encap) {
  1241. case L2TP_ENCAPTYPE_UDP:
  1242. memset(&udp_conf, 0, sizeof(udp_conf));
  1243. #if IS_ENABLED(CONFIG_IPV6)
  1244. if (cfg->local_ip6 && cfg->peer_ip6) {
  1245. udp_conf.family = AF_INET6;
  1246. memcpy(&udp_conf.local_ip6, cfg->local_ip6,
  1247. sizeof(udp_conf.local_ip6));
  1248. memcpy(&udp_conf.peer_ip6, cfg->peer_ip6,
  1249. sizeof(udp_conf.peer_ip6));
  1250. udp_conf.use_udp6_tx_checksums =
  1251. !cfg->udp6_zero_tx_checksums;
  1252. udp_conf.use_udp6_rx_checksums =
  1253. !cfg->udp6_zero_rx_checksums;
  1254. } else
  1255. #endif
  1256. {
  1257. udp_conf.family = AF_INET;
  1258. udp_conf.local_ip = cfg->local_ip;
  1259. udp_conf.peer_ip = cfg->peer_ip;
  1260. udp_conf.use_udp_checksums = cfg->use_udp_checksums;
  1261. }
  1262. udp_conf.local_udp_port = htons(cfg->local_udp_port);
  1263. udp_conf.peer_udp_port = htons(cfg->peer_udp_port);
  1264. err = udp_sock_create(net, &udp_conf, &sock);
  1265. if (err < 0)
  1266. goto out;
  1267. break;
  1268. case L2TP_ENCAPTYPE_IP:
  1269. #if IS_ENABLED(CONFIG_IPV6)
  1270. if (cfg->local_ip6 && cfg->peer_ip6) {
  1271. struct sockaddr_l2tpip6 ip6_addr = {0};
  1272. err = sock_create_kern(net, AF_INET6, SOCK_DGRAM,
  1273. IPPROTO_L2TP, &sock);
  1274. if (err < 0)
  1275. goto out;
  1276. ip6_addr.l2tp_family = AF_INET6;
  1277. memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
  1278. sizeof(ip6_addr.l2tp_addr));
  1279. ip6_addr.l2tp_conn_id = tunnel_id;
  1280. err = kernel_bind(sock, (struct sockaddr *)&ip6_addr,
  1281. sizeof(ip6_addr));
  1282. if (err < 0)
  1283. goto out;
  1284. ip6_addr.l2tp_family = AF_INET6;
  1285. memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
  1286. sizeof(ip6_addr.l2tp_addr));
  1287. ip6_addr.l2tp_conn_id = peer_tunnel_id;
  1288. err = kernel_connect(sock,
  1289. (struct sockaddr *)&ip6_addr,
  1290. sizeof(ip6_addr), 0);
  1291. if (err < 0)
  1292. goto out;
  1293. } else
  1294. #endif
  1295. {
  1296. struct sockaddr_l2tpip ip_addr = {0};
  1297. err = sock_create_kern(net, AF_INET, SOCK_DGRAM,
  1298. IPPROTO_L2TP, &sock);
  1299. if (err < 0)
  1300. goto out;
  1301. ip_addr.l2tp_family = AF_INET;
  1302. ip_addr.l2tp_addr = cfg->local_ip;
  1303. ip_addr.l2tp_conn_id = tunnel_id;
  1304. err = kernel_bind(sock, (struct sockaddr *)&ip_addr,
  1305. sizeof(ip_addr));
  1306. if (err < 0)
  1307. goto out;
  1308. ip_addr.l2tp_family = AF_INET;
  1309. ip_addr.l2tp_addr = cfg->peer_ip;
  1310. ip_addr.l2tp_conn_id = peer_tunnel_id;
  1311. err = kernel_connect(sock, (struct sockaddr *)&ip_addr,
  1312. sizeof(ip_addr), 0);
  1313. if (err < 0)
  1314. goto out;
  1315. }
  1316. break;
  1317. default:
  1318. goto out;
  1319. }
  1320. out:
  1321. *sockp = sock;
  1322. if (err < 0 && sock) {
  1323. kernel_sock_shutdown(sock, SHUT_RDWR);
  1324. sock_release(sock);
  1325. *sockp = NULL;
  1326. }
  1327. return err;
  1328. }
  1329. int l2tp_tunnel_create(int fd, int version, u32 tunnel_id, u32 peer_tunnel_id,
  1330. struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp)
  1331. {
  1332. struct l2tp_tunnel *tunnel = NULL;
  1333. int err;
  1334. enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
  1335. if (cfg)
  1336. encap = cfg->encap;
  1337. tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
  1338. if (!tunnel) {
  1339. err = -ENOMEM;
  1340. goto err;
  1341. }
  1342. tunnel->version = version;
  1343. tunnel->tunnel_id = tunnel_id;
  1344. tunnel->peer_tunnel_id = peer_tunnel_id;
  1345. sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
  1346. spin_lock_init(&tunnel->list_lock);
  1347. tunnel->acpt_newsess = true;
  1348. INIT_LIST_HEAD(&tunnel->session_list);
  1349. tunnel->encap = encap;
  1350. refcount_set(&tunnel->ref_count, 1);
  1351. tunnel->fd = fd;
  1352. /* Init delete workqueue struct */
  1353. INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
  1354. err = 0;
  1355. err:
  1356. if (tunnelp)
  1357. *tunnelp = tunnel;
  1358. return err;
  1359. }
  1360. EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
  1361. static int l2tp_validate_socket(const struct sock *sk, const struct net *net,
  1362. enum l2tp_encap_type encap)
  1363. {
  1364. struct l2tp_tunnel *tunnel;
  1365. if (!net_eq(sock_net(sk), net))
  1366. return -EINVAL;
  1367. if (sk->sk_type != SOCK_DGRAM)
  1368. return -EPROTONOSUPPORT;
  1369. if (sk->sk_family != PF_INET && sk->sk_family != PF_INET6)
  1370. return -EPROTONOSUPPORT;
  1371. if ((encap == L2TP_ENCAPTYPE_UDP && sk->sk_protocol != IPPROTO_UDP) ||
  1372. (encap == L2TP_ENCAPTYPE_IP && sk->sk_protocol != IPPROTO_L2TP))
  1373. return -EPROTONOSUPPORT;
  1374. if (encap == L2TP_ENCAPTYPE_UDP && sk->sk_user_data)
  1375. return -EBUSY;
  1376. tunnel = l2tp_sk_to_tunnel(sk);
  1377. if (tunnel) {
  1378. l2tp_tunnel_put(tunnel);
  1379. return -EBUSY;
  1380. }
  1381. return 0;
  1382. }
  1383. int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
  1384. struct l2tp_tunnel_cfg *cfg)
  1385. {
  1386. struct l2tp_net *pn = l2tp_pernet(net);
  1387. u32 tunnel_id = tunnel->tunnel_id;
  1388. struct socket *sock;
  1389. struct sock *sk;
  1390. int ret;
  1391. spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
  1392. ret = idr_alloc_u32(&pn->l2tp_tunnel_idr, NULL, &tunnel_id, tunnel_id,
  1393. GFP_ATOMIC);
  1394. spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
  1395. if (ret)
  1396. return ret == -ENOSPC ? -EEXIST : ret;
  1397. if (tunnel->fd < 0) {
  1398. ret = l2tp_tunnel_sock_create(net, tunnel->tunnel_id,
  1399. tunnel->peer_tunnel_id, cfg,
  1400. &sock);
  1401. if (ret < 0)
  1402. goto err;
  1403. } else {
  1404. sock = sockfd_lookup(tunnel->fd, &ret);
  1405. if (!sock)
  1406. goto err;
  1407. }
  1408. sk = sock->sk;
  1409. lock_sock(sk);
  1410. write_lock_bh(&sk->sk_callback_lock);
  1411. ret = l2tp_validate_socket(sk, net, tunnel->encap);
  1412. if (ret < 0)
  1413. goto err_inval_sock;
  1414. write_unlock_bh(&sk->sk_callback_lock);
  1415. if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
  1416. struct udp_tunnel_sock_cfg udp_cfg = {
  1417. .encap_type = UDP_ENCAP_L2TPINUDP,
  1418. .encap_rcv = l2tp_udp_encap_recv,
  1419. .encap_err_rcv = l2tp_udp_encap_err_recv,
  1420. .encap_destroy = l2tp_udp_encap_destroy,
  1421. };
  1422. setup_udp_tunnel_sock(net, sock, &udp_cfg);
  1423. }
  1424. sk->sk_allocation = GFP_ATOMIC;
  1425. release_sock(sk);
  1426. sock_hold(sk);
  1427. tunnel->sock = sk;
  1428. tunnel->l2tp_net = net;
  1429. spin_lock_bh(&pn->l2tp_tunnel_idr_lock);
  1430. idr_replace(&pn->l2tp_tunnel_idr, tunnel, tunnel->tunnel_id);
  1431. spin_unlock_bh(&pn->l2tp_tunnel_idr_lock);
  1432. trace_register_tunnel(tunnel);
  1433. if (tunnel->fd >= 0)
  1434. sockfd_put(sock);
  1435. return 0;
  1436. err_inval_sock:
  1437. write_unlock_bh(&sk->sk_callback_lock);
  1438. release_sock(sk);
  1439. if (tunnel->fd < 0)
  1440. sock_release(sock);
  1441. else
  1442. sockfd_put(sock);
  1443. err:
  1444. l2tp_tunnel_remove(net, tunnel);
  1445. return ret;
  1446. }
  1447. EXPORT_SYMBOL_GPL(l2tp_tunnel_register);
  1448. /* This function is used by the netlink TUNNEL_DELETE command.
  1449. */
  1450. void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
  1451. {
  1452. if (!test_and_set_bit(0, &tunnel->dead)) {
  1453. trace_delete_tunnel(tunnel);
  1454. refcount_inc(&tunnel->ref_count);
  1455. queue_work(l2tp_wq, &tunnel->del_work);
  1456. }
  1457. }
  1458. EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
  1459. void l2tp_session_delete(struct l2tp_session *session)
  1460. {
  1461. if (!test_and_set_bit(0, &session->dead)) {
  1462. trace_delete_session(session);
  1463. refcount_inc(&session->ref_count);
  1464. queue_work(l2tp_wq, &session->del_work);
  1465. }
  1466. }
  1467. EXPORT_SYMBOL_GPL(l2tp_session_delete);
  1468. /* Workqueue session deletion function */
  1469. static void l2tp_session_del_work(struct work_struct *work)
  1470. {
  1471. struct l2tp_session *session = container_of(work, struct l2tp_session,
  1472. del_work);
  1473. l2tp_session_unhash(session);
  1474. l2tp_session_queue_purge(session);
  1475. if (session->session_close)
  1476. (*session->session_close)(session);
  1477. /* drop initial ref */
  1478. l2tp_session_put(session);
  1479. /* drop workqueue ref */
  1480. l2tp_session_put(session);
  1481. }
  1482. /* We come here whenever a session's send_seq, cookie_len or
  1483. * l2specific_type parameters are set.
  1484. */
  1485. void l2tp_session_set_header_len(struct l2tp_session *session, int version,
  1486. enum l2tp_encap_type encap)
  1487. {
  1488. if (version == L2TP_HDR_VER_2) {
  1489. session->hdr_len = 6;
  1490. if (session->send_seq)
  1491. session->hdr_len += 4;
  1492. } else {
  1493. session->hdr_len = 4 + session->cookie_len;
  1494. session->hdr_len += l2tp_get_l2specific_len(session);
  1495. if (encap == L2TP_ENCAPTYPE_UDP)
  1496. session->hdr_len += 4;
  1497. }
  1498. }
  1499. EXPORT_SYMBOL_GPL(l2tp_session_set_header_len);
  1500. struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id,
  1501. u32 peer_session_id, struct l2tp_session_cfg *cfg)
  1502. {
  1503. struct l2tp_session *session;
  1504. session = kzalloc(sizeof(*session) + priv_size, GFP_KERNEL);
  1505. if (session) {
  1506. session->magic = L2TP_SESSION_MAGIC;
  1507. session->session_id = session_id;
  1508. session->peer_session_id = peer_session_id;
  1509. session->nr = 0;
  1510. if (tunnel->version == L2TP_HDR_VER_2)
  1511. session->nr_max = 0xffff;
  1512. else
  1513. session->nr_max = 0xffffff;
  1514. session->nr_window_size = session->nr_max / 2;
  1515. session->nr_oos_count_max = 4;
  1516. /* Use NR of first received packet */
  1517. session->reorder_skip = 1;
  1518. sprintf(&session->name[0], "sess %u/%u",
  1519. tunnel->tunnel_id, session->session_id);
  1520. skb_queue_head_init(&session->reorder_q);
  1521. session->hlist_key = l2tp_v3_session_hashkey(tunnel->sock, session->session_id);
  1522. INIT_HLIST_NODE(&session->hlist);
  1523. INIT_LIST_HEAD(&session->clist);
  1524. INIT_LIST_HEAD(&session->list);
  1525. INIT_WORK(&session->del_work, l2tp_session_del_work);
  1526. if (cfg) {
  1527. session->pwtype = cfg->pw_type;
  1528. session->send_seq = cfg->send_seq;
  1529. session->recv_seq = cfg->recv_seq;
  1530. session->lns_mode = cfg->lns_mode;
  1531. session->reorder_timeout = cfg->reorder_timeout;
  1532. session->l2specific_type = cfg->l2specific_type;
  1533. session->cookie_len = cfg->cookie_len;
  1534. memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
  1535. session->peer_cookie_len = cfg->peer_cookie_len;
  1536. memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
  1537. }
  1538. l2tp_session_set_header_len(session, tunnel->version, tunnel->encap);
  1539. refcount_set(&session->ref_count, 1);
  1540. return session;
  1541. }
  1542. return ERR_PTR(-ENOMEM);
  1543. }
  1544. EXPORT_SYMBOL_GPL(l2tp_session_create);
  1545. /*****************************************************************************
  1546. * Init and cleanup
  1547. *****************************************************************************/
  1548. static __net_init int l2tp_init_net(struct net *net)
  1549. {
  1550. struct l2tp_net *pn = net_generic(net, l2tp_net_id);
  1551. idr_init(&pn->l2tp_tunnel_idr);
  1552. spin_lock_init(&pn->l2tp_tunnel_idr_lock);
  1553. idr_init(&pn->l2tp_v2_session_idr);
  1554. idr_init(&pn->l2tp_v3_session_idr);
  1555. spin_lock_init(&pn->l2tp_session_idr_lock);
  1556. return 0;
  1557. }
  1558. static __net_exit void l2tp_pre_exit_net(struct net *net)
  1559. {
  1560. struct l2tp_net *pn = l2tp_pernet(net);
  1561. struct l2tp_tunnel *tunnel = NULL;
  1562. unsigned long tunnel_id, tmp;
  1563. rcu_read_lock_bh();
  1564. idr_for_each_entry_ul(&pn->l2tp_tunnel_idr, tunnel, tmp, tunnel_id) {
  1565. if (tunnel)
  1566. l2tp_tunnel_delete(tunnel);
  1567. }
  1568. rcu_read_unlock_bh();
  1569. if (l2tp_wq) {
  1570. /* Run all TUNNEL_DELETE work items just queued. */
  1571. __flush_workqueue(l2tp_wq);
  1572. /* Each TUNNEL_DELETE work item will queue a SESSION_DELETE
  1573. * work item for each session in the tunnel. Flush the
  1574. * workqueue again to process these.
  1575. */
  1576. __flush_workqueue(l2tp_wq);
  1577. }
  1578. }
  1579. static int l2tp_idr_item_unexpected(int id, void *p, void *data)
  1580. {
  1581. const char *idr_name = data;
  1582. pr_err("l2tp: %s IDR not empty at net %d exit\n", idr_name, id);
  1583. WARN_ON_ONCE(1);
  1584. return 1;
  1585. }
  1586. static __net_exit void l2tp_exit_net(struct net *net)
  1587. {
  1588. struct l2tp_net *pn = l2tp_pernet(net);
  1589. /* Our per-net IDRs should be empty. Check that is so, to
  1590. * help catch cleanup races or refcnt leaks.
  1591. */
  1592. idr_for_each(&pn->l2tp_v2_session_idr, l2tp_idr_item_unexpected,
  1593. "v2_session");
  1594. idr_for_each(&pn->l2tp_v3_session_idr, l2tp_idr_item_unexpected,
  1595. "v3_session");
  1596. idr_for_each(&pn->l2tp_tunnel_idr, l2tp_idr_item_unexpected,
  1597. "tunnel");
  1598. idr_destroy(&pn->l2tp_v2_session_idr);
  1599. idr_destroy(&pn->l2tp_v3_session_idr);
  1600. idr_destroy(&pn->l2tp_tunnel_idr);
  1601. }
  1602. static struct pernet_operations l2tp_net_ops = {
  1603. .init = l2tp_init_net,
  1604. .exit = l2tp_exit_net,
  1605. .pre_exit = l2tp_pre_exit_net,
  1606. .id = &l2tp_net_id,
  1607. .size = sizeof(struct l2tp_net),
  1608. };
  1609. static int __init l2tp_init(void)
  1610. {
  1611. int rc = 0;
  1612. rc = register_pernet_device(&l2tp_net_ops);
  1613. if (rc)
  1614. goto out;
  1615. l2tp_wq = alloc_workqueue("l2tp", WQ_UNBOUND, 0);
  1616. if (!l2tp_wq) {
  1617. pr_err("alloc_workqueue failed\n");
  1618. unregister_pernet_device(&l2tp_net_ops);
  1619. rc = -ENOMEM;
  1620. goto out;
  1621. }
  1622. pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
  1623. out:
  1624. return rc;
  1625. }
  1626. static void __exit l2tp_exit(void)
  1627. {
  1628. unregister_pernet_device(&l2tp_net_ops);
  1629. if (l2tp_wq) {
  1630. destroy_workqueue(l2tp_wq);
  1631. l2tp_wq = NULL;
  1632. }
  1633. }
  1634. module_init(l2tp_init);
  1635. module_exit(l2tp_exit);
  1636. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  1637. MODULE_DESCRIPTION("L2TP core");
  1638. MODULE_LICENSE("GPL");
  1639. MODULE_VERSION(L2TP_DRV_VERSION);