l2tp_core.c 54 KB

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