l2tp_core.c 51 KB

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