l2tp_core.c 55 KB

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