l2tp_core.c 53 KB

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