l2tp_core.c 52 KB

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