l2tp_core.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  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. /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
  735. * here. The skb is not on a list when we get here.
  736. * Returns 0 if the packet was a data packet and was successfully passed on.
  737. * Returns 1 if the packet was not a good data packet and could not be
  738. * forwarded. All such packets are passed up to userspace to deal with.
  739. */
  740. static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
  741. int (*payload_hook)(struct sk_buff *skb))
  742. {
  743. struct l2tp_session *session = NULL;
  744. unsigned char *ptr, *optr;
  745. u16 hdrflags;
  746. u32 tunnel_id, session_id;
  747. u16 version;
  748. int length;
  749. struct l2tp_stats *tstats;
  750. if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
  751. goto discard_bad_csum;
  752. /* UDP always verifies the packet length. */
  753. __skb_pull(skb, sizeof(struct udphdr));
  754. /* Short packet? */
  755. if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
  756. l2tp_info(tunnel, L2TP_MSG_DATA,
  757. "%s: recv short packet (len=%d)\n",
  758. tunnel->name, skb->len);
  759. goto error;
  760. }
  761. /* Trace packet contents, if enabled */
  762. if (tunnel->debug & L2TP_MSG_DATA) {
  763. length = min(32u, skb->len);
  764. if (!pskb_may_pull(skb, length))
  765. goto error;
  766. pr_debug("%s: recv\n", tunnel->name);
  767. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
  768. }
  769. /* Point to L2TP header */
  770. optr = ptr = skb->data;
  771. /* Get L2TP header flags */
  772. hdrflags = ntohs(*(__be16 *) ptr);
  773. /* Check protocol version */
  774. version = hdrflags & L2TP_HDR_VER_MASK;
  775. if (version != tunnel->version) {
  776. l2tp_info(tunnel, L2TP_MSG_DATA,
  777. "%s: recv protocol version mismatch: got %d expected %d\n",
  778. tunnel->name, version, tunnel->version);
  779. goto error;
  780. }
  781. /* Get length of L2TP packet */
  782. length = skb->len;
  783. /* If type is control packet, it is handled by userspace. */
  784. if (hdrflags & L2TP_HDRFLAG_T) {
  785. l2tp_dbg(tunnel, L2TP_MSG_DATA,
  786. "%s: recv control packet, len=%d\n",
  787. tunnel->name, length);
  788. goto error;
  789. }
  790. /* Skip flags */
  791. ptr += 2;
  792. if (tunnel->version == L2TP_HDR_VER_2) {
  793. /* If length is present, skip it */
  794. if (hdrflags & L2TP_HDRFLAG_L)
  795. ptr += 2;
  796. /* Extract tunnel and session ID */
  797. tunnel_id = ntohs(*(__be16 *) ptr);
  798. ptr += 2;
  799. session_id = ntohs(*(__be16 *) ptr);
  800. ptr += 2;
  801. } else {
  802. ptr += 2; /* skip reserved bits */
  803. tunnel_id = tunnel->tunnel_id;
  804. session_id = ntohl(*(__be32 *) ptr);
  805. ptr += 4;
  806. }
  807. /* Find the session context */
  808. session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
  809. if (!session || !session->recv_skb) {
  810. /* Not found? Pass to userspace to deal with */
  811. l2tp_info(tunnel, L2TP_MSG_DATA,
  812. "%s: no session found (%u/%u). Passing up.\n",
  813. tunnel->name, tunnel_id, session_id);
  814. goto error;
  815. }
  816. l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
  817. return 0;
  818. discard_bad_csum:
  819. LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
  820. UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
  821. tstats = &tunnel->stats;
  822. u64_stats_update_begin(&tstats->syncp);
  823. tstats->rx_errors++;
  824. u64_stats_update_end(&tstats->syncp);
  825. kfree_skb(skb);
  826. return 0;
  827. error:
  828. /* Put UDP header back */
  829. __skb_push(skb, sizeof(struct udphdr));
  830. return 1;
  831. }
  832. /* UDP encapsulation receive handler. See net/ipv4/udp.c.
  833. * Return codes:
  834. * 0 : success.
  835. * <0: error
  836. * >0: skb should be passed up to userspace as UDP.
  837. */
  838. int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  839. {
  840. struct l2tp_tunnel *tunnel;
  841. tunnel = l2tp_sock_to_tunnel(sk);
  842. if (tunnel == NULL)
  843. goto pass_up;
  844. l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
  845. tunnel->name, skb->len);
  846. if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
  847. goto pass_up_put;
  848. sock_put(sk);
  849. return 0;
  850. pass_up_put:
  851. sock_put(sk);
  852. pass_up:
  853. return 1;
  854. }
  855. EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
  856. /************************************************************************
  857. * Transmit handling
  858. ***********************************************************************/
  859. /* Build an L2TP header for the session into the buffer provided.
  860. */
  861. static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
  862. {
  863. struct l2tp_tunnel *tunnel = session->tunnel;
  864. __be16 *bufp = buf;
  865. __be16 *optr = buf;
  866. u16 flags = L2TP_HDR_VER_2;
  867. u32 tunnel_id = tunnel->peer_tunnel_id;
  868. u32 session_id = session->peer_session_id;
  869. if (session->send_seq)
  870. flags |= L2TP_HDRFLAG_S;
  871. /* Setup L2TP header. */
  872. *bufp++ = htons(flags);
  873. *bufp++ = htons(tunnel_id);
  874. *bufp++ = htons(session_id);
  875. if (session->send_seq) {
  876. *bufp++ = htons(session->ns);
  877. *bufp++ = 0;
  878. session->ns++;
  879. session->ns &= 0xffff;
  880. l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
  881. session->name, session->ns);
  882. }
  883. return bufp - optr;
  884. }
  885. static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
  886. {
  887. struct l2tp_tunnel *tunnel = session->tunnel;
  888. char *bufp = buf;
  889. char *optr = bufp;
  890. /* Setup L2TP header. The header differs slightly for UDP and
  891. * IP encapsulations. For UDP, there is 4 bytes of flags.
  892. */
  893. if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
  894. u16 flags = L2TP_HDR_VER_3;
  895. *((__be16 *) bufp) = htons(flags);
  896. bufp += 2;
  897. *((__be16 *) bufp) = 0;
  898. bufp += 2;
  899. }
  900. *((__be32 *) bufp) = htonl(session->peer_session_id);
  901. bufp += 4;
  902. if (session->cookie_len) {
  903. memcpy(bufp, &session->cookie[0], session->cookie_len);
  904. bufp += session->cookie_len;
  905. }
  906. if (session->l2specific_len) {
  907. if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
  908. u32 l2h = 0;
  909. if (session->send_seq) {
  910. l2h = 0x40000000 | session->ns;
  911. session->ns++;
  912. session->ns &= 0xffffff;
  913. l2tp_dbg(session, L2TP_MSG_SEQ,
  914. "%s: updated ns to %u\n",
  915. session->name, session->ns);
  916. }
  917. *((__be32 *) bufp) = htonl(l2h);
  918. }
  919. bufp += session->l2specific_len;
  920. }
  921. if (session->offset)
  922. bufp += session->offset;
  923. return bufp - optr;
  924. }
  925. static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
  926. struct flowi *fl, size_t data_len)
  927. {
  928. struct l2tp_tunnel *tunnel = session->tunnel;
  929. unsigned int len = skb->len;
  930. int error;
  931. struct l2tp_stats *tstats, *sstats;
  932. /* Debug */
  933. if (session->send_seq)
  934. l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
  935. session->name, data_len, session->ns - 1);
  936. else
  937. l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
  938. session->name, data_len);
  939. if (session->debug & L2TP_MSG_DATA) {
  940. int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
  941. unsigned char *datap = skb->data + uhlen;
  942. pr_debug("%s: xmit\n", session->name);
  943. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
  944. datap, min_t(size_t, 32, len - uhlen));
  945. }
  946. /* Queue the packet to IP for output */
  947. skb->local_df = 1;
  948. #if IS_ENABLED(CONFIG_IPV6)
  949. if (skb->sk->sk_family == PF_INET6)
  950. error = inet6_csk_xmit(skb, NULL);
  951. else
  952. #endif
  953. error = ip_queue_xmit(skb, fl);
  954. /* Update stats */
  955. tstats = &tunnel->stats;
  956. u64_stats_update_begin(&tstats->syncp);
  957. sstats = &session->stats;
  958. u64_stats_update_begin(&sstats->syncp);
  959. if (error >= 0) {
  960. tstats->tx_packets++;
  961. tstats->tx_bytes += len;
  962. sstats->tx_packets++;
  963. sstats->tx_bytes += len;
  964. } else {
  965. tstats->tx_errors++;
  966. sstats->tx_errors++;
  967. }
  968. u64_stats_update_end(&tstats->syncp);
  969. u64_stats_update_end(&sstats->syncp);
  970. return 0;
  971. }
  972. /* Automatically called when the skb is freed.
  973. */
  974. static void l2tp_sock_wfree(struct sk_buff *skb)
  975. {
  976. sock_put(skb->sk);
  977. }
  978. /* For data skbs that we transmit, we associate with the tunnel socket
  979. * but don't do accounting.
  980. */
  981. static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
  982. {
  983. sock_hold(sk);
  984. skb->sk = sk;
  985. skb->destructor = l2tp_sock_wfree;
  986. }
  987. #if IS_ENABLED(CONFIG_IPV6)
  988. static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
  989. int udp_len)
  990. {
  991. struct ipv6_pinfo *np = inet6_sk(sk);
  992. struct udphdr *uh = udp_hdr(skb);
  993. if (!skb_dst(skb) || !skb_dst(skb)->dev ||
  994. !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
  995. __wsum csum = skb_checksum(skb, 0, udp_len, 0);
  996. skb->ip_summed = CHECKSUM_UNNECESSARY;
  997. uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
  998. IPPROTO_UDP, csum);
  999. if (uh->check == 0)
  1000. uh->check = CSUM_MANGLED_0;
  1001. } else {
  1002. skb->ip_summed = CHECKSUM_PARTIAL;
  1003. skb->csum_start = skb_transport_header(skb) - skb->head;
  1004. skb->csum_offset = offsetof(struct udphdr, check);
  1005. uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
  1006. udp_len, IPPROTO_UDP, 0);
  1007. }
  1008. }
  1009. #endif
  1010. /* If caller requires the skb to have a ppp header, the header must be
  1011. * inserted in the skb data before calling this function.
  1012. */
  1013. int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
  1014. {
  1015. int data_len = skb->len;
  1016. struct l2tp_tunnel *tunnel = session->tunnel;
  1017. struct sock *sk = tunnel->sock;
  1018. struct flowi *fl;
  1019. struct udphdr *uh;
  1020. struct inet_sock *inet;
  1021. __wsum csum;
  1022. int headroom;
  1023. int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
  1024. int udp_len;
  1025. int ret = NET_XMIT_SUCCESS;
  1026. /* Check that there's enough headroom in the skb to insert IP,
  1027. * UDP and L2TP headers. If not enough, expand it to
  1028. * make room. Adjust truesize.
  1029. */
  1030. headroom = NET_SKB_PAD + sizeof(struct iphdr) +
  1031. uhlen + hdr_len;
  1032. if (skb_cow_head(skb, headroom)) {
  1033. kfree_skb(skb);
  1034. return NET_XMIT_DROP;
  1035. }
  1036. skb_orphan(skb);
  1037. /* Setup L2TP header */
  1038. session->build_header(session, __skb_push(skb, hdr_len));
  1039. /* Reset skb netfilter state */
  1040. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  1041. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  1042. IPSKB_REROUTED);
  1043. nf_reset(skb);
  1044. bh_lock_sock(sk);
  1045. if (sock_owned_by_user(sk)) {
  1046. kfree_skb(skb);
  1047. ret = NET_XMIT_DROP;
  1048. goto out_unlock;
  1049. }
  1050. /* Get routing info from the tunnel socket */
  1051. skb_dst_drop(skb);
  1052. skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
  1053. inet = inet_sk(sk);
  1054. fl = &inet->cork.fl;
  1055. switch (tunnel->encap) {
  1056. case L2TP_ENCAPTYPE_UDP:
  1057. /* Setup UDP header */
  1058. __skb_push(skb, sizeof(*uh));
  1059. skb_reset_transport_header(skb);
  1060. uh = udp_hdr(skb);
  1061. uh->source = inet->inet_sport;
  1062. uh->dest = inet->inet_dport;
  1063. udp_len = uhlen + hdr_len + data_len;
  1064. uh->len = htons(udp_len);
  1065. uh->check = 0;
  1066. /* Calculate UDP checksum if configured to do so */
  1067. #if IS_ENABLED(CONFIG_IPV6)
  1068. if (sk->sk_family == PF_INET6)
  1069. l2tp_xmit_ipv6_csum(sk, skb, udp_len);
  1070. else
  1071. #endif
  1072. if (sk->sk_no_check == UDP_CSUM_NOXMIT)
  1073. skb->ip_summed = CHECKSUM_NONE;
  1074. else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
  1075. (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
  1076. skb->ip_summed = CHECKSUM_COMPLETE;
  1077. csum = skb_checksum(skb, 0, udp_len, 0);
  1078. uh->check = csum_tcpudp_magic(inet->inet_saddr,
  1079. inet->inet_daddr,
  1080. udp_len, IPPROTO_UDP, csum);
  1081. if (uh->check == 0)
  1082. uh->check = CSUM_MANGLED_0;
  1083. } else {
  1084. skb->ip_summed = CHECKSUM_PARTIAL;
  1085. skb->csum_start = skb_transport_header(skb) - skb->head;
  1086. skb->csum_offset = offsetof(struct udphdr, check);
  1087. uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
  1088. inet->inet_daddr,
  1089. udp_len, IPPROTO_UDP, 0);
  1090. }
  1091. break;
  1092. case L2TP_ENCAPTYPE_IP:
  1093. break;
  1094. }
  1095. l2tp_skb_set_owner_w(skb, sk);
  1096. l2tp_xmit_core(session, skb, fl, data_len);
  1097. out_unlock:
  1098. bh_unlock_sock(sk);
  1099. return ret;
  1100. }
  1101. EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
  1102. /*****************************************************************************
  1103. * Tinnel and session create/destroy.
  1104. *****************************************************************************/
  1105. /* Tunnel socket destruct hook.
  1106. * The tunnel context is deleted only when all session sockets have been
  1107. * closed.
  1108. */
  1109. static void l2tp_tunnel_destruct(struct sock *sk)
  1110. {
  1111. struct l2tp_tunnel *tunnel;
  1112. struct l2tp_net *pn;
  1113. tunnel = sk->sk_user_data;
  1114. if (tunnel == NULL)
  1115. goto end;
  1116. l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
  1117. /* Disable udp encapsulation */
  1118. switch (tunnel->encap) {
  1119. case L2TP_ENCAPTYPE_UDP:
  1120. /* No longer an encapsulation socket. See net/ipv4/udp.c */
  1121. (udp_sk(sk))->encap_type = 0;
  1122. (udp_sk(sk))->encap_rcv = NULL;
  1123. (udp_sk(sk))->encap_destroy = NULL;
  1124. break;
  1125. case L2TP_ENCAPTYPE_IP:
  1126. break;
  1127. }
  1128. /* Remove hooks into tunnel socket */
  1129. sk->sk_destruct = tunnel->old_sk_destruct;
  1130. sk->sk_user_data = NULL;
  1131. tunnel->sock = NULL;
  1132. /* Remove the tunnel struct from the tunnel list */
  1133. pn = l2tp_pernet(tunnel->l2tp_net);
  1134. spin_lock_bh(&pn->l2tp_tunnel_list_lock);
  1135. list_del_rcu(&tunnel->list);
  1136. spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
  1137. atomic_dec(&l2tp_tunnel_count);
  1138. l2tp_tunnel_closeall(tunnel);
  1139. l2tp_tunnel_dec_refcount(tunnel);
  1140. /* Call the original destructor */
  1141. if (sk->sk_destruct)
  1142. (*sk->sk_destruct)(sk);
  1143. end:
  1144. return;
  1145. }
  1146. /* When the tunnel is closed, all the attached sessions need to go too.
  1147. */
  1148. void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
  1149. {
  1150. int hash;
  1151. struct hlist_node *walk;
  1152. struct hlist_node *tmp;
  1153. struct l2tp_session *session;
  1154. BUG_ON(tunnel == NULL);
  1155. l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
  1156. tunnel->name);
  1157. write_lock_bh(&tunnel->hlist_lock);
  1158. for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
  1159. again:
  1160. hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
  1161. session = hlist_entry(walk, struct l2tp_session, hlist);
  1162. l2tp_info(session, L2TP_MSG_CONTROL,
  1163. "%s: closing session\n", session->name);
  1164. hlist_del_init(&session->hlist);
  1165. /* Since we should hold the sock lock while
  1166. * doing any unbinding, we need to release the
  1167. * lock we're holding before taking that lock.
  1168. * Hold a reference to the sock so it doesn't
  1169. * disappear as we're jumping between locks.
  1170. */
  1171. if (session->ref != NULL)
  1172. (*session->ref)(session);
  1173. write_unlock_bh(&tunnel->hlist_lock);
  1174. if (tunnel->version != L2TP_HDR_VER_2) {
  1175. struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
  1176. spin_lock_bh(&pn->l2tp_session_hlist_lock);
  1177. hlist_del_init_rcu(&session->global_hlist);
  1178. spin_unlock_bh(&pn->l2tp_session_hlist_lock);
  1179. synchronize_rcu();
  1180. }
  1181. if (session->session_close != NULL)
  1182. (*session->session_close)(session);
  1183. if (session->deref != NULL)
  1184. (*session->deref)(session);
  1185. l2tp_session_dec_refcount(session);
  1186. write_lock_bh(&tunnel->hlist_lock);
  1187. /* Now restart from the beginning of this hash
  1188. * chain. We always remove a session from the
  1189. * list so we are guaranteed to make forward
  1190. * progress.
  1191. */
  1192. goto again;
  1193. }
  1194. }
  1195. write_unlock_bh(&tunnel->hlist_lock);
  1196. }
  1197. EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
  1198. /* Tunnel socket destroy hook for UDP encapsulation */
  1199. static void l2tp_udp_encap_destroy(struct sock *sk)
  1200. {
  1201. struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
  1202. if (tunnel) {
  1203. l2tp_tunnel_closeall(tunnel);
  1204. sock_put(sk);
  1205. }
  1206. }
  1207. /* Really kill the tunnel.
  1208. * Come here only when all sessions have been cleared from the tunnel.
  1209. */
  1210. static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
  1211. {
  1212. BUG_ON(atomic_read(&tunnel->ref_count) != 0);
  1213. BUG_ON(tunnel->sock != NULL);
  1214. l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
  1215. kfree_rcu(tunnel, rcu);
  1216. }
  1217. /* Workqueue tunnel deletion function */
  1218. static void l2tp_tunnel_del_work(struct work_struct *work)
  1219. {
  1220. struct l2tp_tunnel *tunnel = NULL;
  1221. struct socket *sock = NULL;
  1222. struct sock *sk = NULL;
  1223. tunnel = container_of(work, struct l2tp_tunnel, del_work);
  1224. sk = l2tp_tunnel_sock_lookup(tunnel);
  1225. if (!sk)
  1226. return;
  1227. sock = sk->sk_socket;
  1228. /* If the tunnel socket was created by userspace, then go through the
  1229. * inet layer to shut the socket down, and let userspace close it.
  1230. * Otherwise, if we created the socket directly within the kernel, use
  1231. * the sk API to release it here.
  1232. * In either case the tunnel resources are freed in the socket
  1233. * destructor when the tunnel socket goes away.
  1234. */
  1235. if (tunnel->fd >= 0) {
  1236. if (sock)
  1237. inet_shutdown(sock, 2);
  1238. } else {
  1239. if (sock)
  1240. kernel_sock_shutdown(sock, SHUT_RDWR);
  1241. sk_release_kernel(sk);
  1242. }
  1243. l2tp_tunnel_sock_put(sk);
  1244. }
  1245. /* Create a socket for the tunnel, if one isn't set up by
  1246. * userspace. This is used for static tunnels where there is no
  1247. * managing L2TP daemon.
  1248. *
  1249. * Since we don't want these sockets to keep a namespace alive by
  1250. * themselves, we drop the socket's namespace refcount after creation.
  1251. * These sockets are freed when the namespace exits using the pernet
  1252. * exit hook.
  1253. */
  1254. static int l2tp_tunnel_sock_create(struct net *net,
  1255. u32 tunnel_id,
  1256. u32 peer_tunnel_id,
  1257. struct l2tp_tunnel_cfg *cfg,
  1258. struct socket **sockp)
  1259. {
  1260. int err = -EINVAL;
  1261. struct socket *sock = NULL;
  1262. struct sockaddr_in udp_addr = {0};
  1263. struct sockaddr_l2tpip ip_addr = {0};
  1264. #if IS_ENABLED(CONFIG_IPV6)
  1265. struct sockaddr_in6 udp6_addr = {0};
  1266. struct sockaddr_l2tpip6 ip6_addr = {0};
  1267. #endif
  1268. switch (cfg->encap) {
  1269. case L2TP_ENCAPTYPE_UDP:
  1270. #if IS_ENABLED(CONFIG_IPV6)
  1271. if (cfg->local_ip6 && cfg->peer_ip6) {
  1272. err = sock_create_kern(AF_INET6, SOCK_DGRAM, 0, &sock);
  1273. if (err < 0)
  1274. goto out;
  1275. sk_change_net(sock->sk, net);
  1276. udp6_addr.sin6_family = AF_INET6;
  1277. memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
  1278. sizeof(udp6_addr.sin6_addr));
  1279. udp6_addr.sin6_port = htons(cfg->local_udp_port);
  1280. err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
  1281. sizeof(udp6_addr));
  1282. if (err < 0)
  1283. goto out;
  1284. udp6_addr.sin6_family = AF_INET6;
  1285. memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
  1286. sizeof(udp6_addr.sin6_addr));
  1287. udp6_addr.sin6_port = htons(cfg->peer_udp_port);
  1288. err = kernel_connect(sock,
  1289. (struct sockaddr *) &udp6_addr,
  1290. sizeof(udp6_addr), 0);
  1291. if (err < 0)
  1292. goto out;
  1293. } else
  1294. #endif
  1295. {
  1296. err = sock_create_kern(AF_INET, SOCK_DGRAM, 0, &sock);
  1297. if (err < 0)
  1298. goto out;
  1299. sk_change_net(sock->sk, net);
  1300. udp_addr.sin_family = AF_INET;
  1301. udp_addr.sin_addr = cfg->local_ip;
  1302. udp_addr.sin_port = htons(cfg->local_udp_port);
  1303. err = kernel_bind(sock, (struct sockaddr *) &udp_addr,
  1304. sizeof(udp_addr));
  1305. if (err < 0)
  1306. goto out;
  1307. udp_addr.sin_family = AF_INET;
  1308. udp_addr.sin_addr = cfg->peer_ip;
  1309. udp_addr.sin_port = htons(cfg->peer_udp_port);
  1310. err = kernel_connect(sock,
  1311. (struct sockaddr *) &udp_addr,
  1312. sizeof(udp_addr), 0);
  1313. if (err < 0)
  1314. goto out;
  1315. }
  1316. if (!cfg->use_udp_checksums)
  1317. sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
  1318. break;
  1319. case L2TP_ENCAPTYPE_IP:
  1320. #if IS_ENABLED(CONFIG_IPV6)
  1321. if (cfg->local_ip6 && cfg->peer_ip6) {
  1322. err = sock_create_kern(AF_INET6, SOCK_DGRAM,
  1323. IPPROTO_L2TP, &sock);
  1324. if (err < 0)
  1325. goto out;
  1326. sk_change_net(sock->sk, net);
  1327. ip6_addr.l2tp_family = AF_INET6;
  1328. memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
  1329. sizeof(ip6_addr.l2tp_addr));
  1330. ip6_addr.l2tp_conn_id = tunnel_id;
  1331. err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
  1332. sizeof(ip6_addr));
  1333. if (err < 0)
  1334. goto out;
  1335. ip6_addr.l2tp_family = AF_INET6;
  1336. memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
  1337. sizeof(ip6_addr.l2tp_addr));
  1338. ip6_addr.l2tp_conn_id = peer_tunnel_id;
  1339. err = kernel_connect(sock,
  1340. (struct sockaddr *) &ip6_addr,
  1341. sizeof(ip6_addr), 0);
  1342. if (err < 0)
  1343. goto out;
  1344. } else
  1345. #endif
  1346. {
  1347. err = sock_create_kern(AF_INET, SOCK_DGRAM,
  1348. IPPROTO_L2TP, &sock);
  1349. if (err < 0)
  1350. goto out;
  1351. sk_change_net(sock->sk, net);
  1352. ip_addr.l2tp_family = AF_INET;
  1353. ip_addr.l2tp_addr = cfg->local_ip;
  1354. ip_addr.l2tp_conn_id = tunnel_id;
  1355. err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
  1356. sizeof(ip_addr));
  1357. if (err < 0)
  1358. goto out;
  1359. ip_addr.l2tp_family = AF_INET;
  1360. ip_addr.l2tp_addr = cfg->peer_ip;
  1361. ip_addr.l2tp_conn_id = peer_tunnel_id;
  1362. err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
  1363. sizeof(ip_addr), 0);
  1364. if (err < 0)
  1365. goto out;
  1366. }
  1367. break;
  1368. default:
  1369. goto out;
  1370. }
  1371. out:
  1372. *sockp = sock;
  1373. if ((err < 0) && sock) {
  1374. kernel_sock_shutdown(sock, SHUT_RDWR);
  1375. sk_release_kernel(sock->sk);
  1376. *sockp = NULL;
  1377. }
  1378. return err;
  1379. }
  1380. static struct lock_class_key l2tp_socket_class;
  1381. 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)
  1382. {
  1383. struct l2tp_tunnel *tunnel = NULL;
  1384. int err;
  1385. struct socket *sock = NULL;
  1386. struct sock *sk = NULL;
  1387. struct l2tp_net *pn;
  1388. enum l2tp_encap_type encap = L2TP_ENCAPTYPE_UDP;
  1389. /* Get the tunnel socket from the fd, which was opened by
  1390. * the userspace L2TP daemon. If not specified, create a
  1391. * kernel socket.
  1392. */
  1393. if (fd < 0) {
  1394. err = l2tp_tunnel_sock_create(net, tunnel_id, peer_tunnel_id,
  1395. cfg, &sock);
  1396. if (err < 0)
  1397. goto err;
  1398. } else {
  1399. sock = sockfd_lookup(fd, &err);
  1400. if (!sock) {
  1401. pr_err("tunl %u: sockfd_lookup(fd=%d) returned %d\n",
  1402. tunnel_id, fd, err);
  1403. err = -EBADF;
  1404. goto err;
  1405. }
  1406. /* Reject namespace mismatches */
  1407. if (!net_eq(sock_net(sock->sk), net)) {
  1408. pr_err("tunl %u: netns mismatch\n", tunnel_id);
  1409. err = -EINVAL;
  1410. goto err;
  1411. }
  1412. }
  1413. sk = sock->sk;
  1414. if (cfg != NULL)
  1415. encap = cfg->encap;
  1416. /* Quick sanity checks */
  1417. switch (encap) {
  1418. case L2TP_ENCAPTYPE_UDP:
  1419. err = -EPROTONOSUPPORT;
  1420. if (sk->sk_protocol != IPPROTO_UDP) {
  1421. pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
  1422. tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
  1423. goto err;
  1424. }
  1425. break;
  1426. case L2TP_ENCAPTYPE_IP:
  1427. err = -EPROTONOSUPPORT;
  1428. if (sk->sk_protocol != IPPROTO_L2TP) {
  1429. pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
  1430. tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
  1431. goto err;
  1432. }
  1433. break;
  1434. }
  1435. /* Check if this socket has already been prepped */
  1436. tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
  1437. if (tunnel != NULL) {
  1438. /* This socket has already been prepped */
  1439. err = -EBUSY;
  1440. goto err;
  1441. }
  1442. tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
  1443. if (tunnel == NULL) {
  1444. err = -ENOMEM;
  1445. goto err;
  1446. }
  1447. tunnel->version = version;
  1448. tunnel->tunnel_id = tunnel_id;
  1449. tunnel->peer_tunnel_id = peer_tunnel_id;
  1450. tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
  1451. tunnel->magic = L2TP_TUNNEL_MAGIC;
  1452. sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
  1453. rwlock_init(&tunnel->hlist_lock);
  1454. /* The net we belong to */
  1455. tunnel->l2tp_net = net;
  1456. pn = l2tp_pernet(net);
  1457. if (cfg != NULL)
  1458. tunnel->debug = cfg->debug;
  1459. /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
  1460. tunnel->encap = encap;
  1461. if (encap == L2TP_ENCAPTYPE_UDP) {
  1462. /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
  1463. udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
  1464. udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
  1465. udp_sk(sk)->encap_destroy = l2tp_udp_encap_destroy;
  1466. #if IS_ENABLED(CONFIG_IPV6)
  1467. if (sk->sk_family == PF_INET6)
  1468. udpv6_encap_enable();
  1469. else
  1470. #endif
  1471. udp_encap_enable();
  1472. }
  1473. sk->sk_user_data = tunnel;
  1474. /* Hook on the tunnel socket destructor so that we can cleanup
  1475. * if the tunnel socket goes away.
  1476. */
  1477. tunnel->old_sk_destruct = sk->sk_destruct;
  1478. sk->sk_destruct = &l2tp_tunnel_destruct;
  1479. tunnel->sock = sk;
  1480. tunnel->fd = fd;
  1481. lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
  1482. sk->sk_allocation = GFP_ATOMIC;
  1483. /* Init delete workqueue struct */
  1484. INIT_WORK(&tunnel->del_work, l2tp_tunnel_del_work);
  1485. /* Add tunnel to our list */
  1486. INIT_LIST_HEAD(&tunnel->list);
  1487. atomic_inc(&l2tp_tunnel_count);
  1488. /* Bump the reference count. The tunnel context is deleted
  1489. * only when this drops to zero. Must be done before list insertion
  1490. */
  1491. l2tp_tunnel_inc_refcount(tunnel);
  1492. spin_lock_bh(&pn->l2tp_tunnel_list_lock);
  1493. list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
  1494. spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
  1495. err = 0;
  1496. err:
  1497. if (tunnelp)
  1498. *tunnelp = tunnel;
  1499. /* If tunnel's socket was created by the kernel, it doesn't
  1500. * have a file.
  1501. */
  1502. if (sock && sock->file)
  1503. sockfd_put(sock);
  1504. return err;
  1505. }
  1506. EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
  1507. /* This function is used by the netlink TUNNEL_DELETE command.
  1508. */
  1509. int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
  1510. {
  1511. l2tp_tunnel_closeall(tunnel);
  1512. return (false == queue_work(l2tp_wq, &tunnel->del_work));
  1513. }
  1514. EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
  1515. /* Really kill the session.
  1516. */
  1517. void l2tp_session_free(struct l2tp_session *session)
  1518. {
  1519. struct l2tp_tunnel *tunnel;
  1520. BUG_ON(atomic_read(&session->ref_count) != 0);
  1521. tunnel = session->tunnel;
  1522. if (tunnel != NULL) {
  1523. BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
  1524. /* Delete the session from the hash */
  1525. write_lock_bh(&tunnel->hlist_lock);
  1526. hlist_del_init(&session->hlist);
  1527. write_unlock_bh(&tunnel->hlist_lock);
  1528. /* Unlink from the global hash if not L2TPv2 */
  1529. if (tunnel->version != L2TP_HDR_VER_2) {
  1530. struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
  1531. spin_lock_bh(&pn->l2tp_session_hlist_lock);
  1532. hlist_del_init_rcu(&session->global_hlist);
  1533. spin_unlock_bh(&pn->l2tp_session_hlist_lock);
  1534. synchronize_rcu();
  1535. }
  1536. if (session->session_id != 0)
  1537. atomic_dec(&l2tp_session_count);
  1538. sock_put(tunnel->sock);
  1539. /* This will delete the tunnel context if this
  1540. * is the last session on the tunnel.
  1541. */
  1542. session->tunnel = NULL;
  1543. l2tp_tunnel_dec_refcount(tunnel);
  1544. }
  1545. kfree(session);
  1546. return;
  1547. }
  1548. EXPORT_SYMBOL_GPL(l2tp_session_free);
  1549. /* This function is used by the netlink SESSION_DELETE command and by
  1550. pseudowire modules.
  1551. */
  1552. int l2tp_session_delete(struct l2tp_session *session)
  1553. {
  1554. if (session->session_close != NULL)
  1555. (*session->session_close)(session);
  1556. l2tp_session_dec_refcount(session);
  1557. return 0;
  1558. }
  1559. EXPORT_SYMBOL_GPL(l2tp_session_delete);
  1560. /* We come here whenever a session's send_seq, cookie_len or
  1561. * l2specific_len parameters are set.
  1562. */
  1563. static void l2tp_session_set_header_len(struct l2tp_session *session, int version)
  1564. {
  1565. if (version == L2TP_HDR_VER_2) {
  1566. session->hdr_len = 6;
  1567. if (session->send_seq)
  1568. session->hdr_len += 4;
  1569. } else {
  1570. session->hdr_len = 4 + session->cookie_len + session->l2specific_len + session->offset;
  1571. if (session->tunnel->encap == L2TP_ENCAPTYPE_UDP)
  1572. session->hdr_len += 4;
  1573. }
  1574. }
  1575. 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)
  1576. {
  1577. struct l2tp_session *session;
  1578. session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
  1579. if (session != NULL) {
  1580. session->magic = L2TP_SESSION_MAGIC;
  1581. session->tunnel = tunnel;
  1582. session->session_id = session_id;
  1583. session->peer_session_id = peer_session_id;
  1584. session->nr = 0;
  1585. sprintf(&session->name[0], "sess %u/%u",
  1586. tunnel->tunnel_id, session->session_id);
  1587. skb_queue_head_init(&session->reorder_q);
  1588. INIT_HLIST_NODE(&session->hlist);
  1589. INIT_HLIST_NODE(&session->global_hlist);
  1590. /* Inherit debug options from tunnel */
  1591. session->debug = tunnel->debug;
  1592. if (cfg) {
  1593. session->pwtype = cfg->pw_type;
  1594. session->debug = cfg->debug;
  1595. session->mtu = cfg->mtu;
  1596. session->mru = cfg->mru;
  1597. session->send_seq = cfg->send_seq;
  1598. session->recv_seq = cfg->recv_seq;
  1599. session->lns_mode = cfg->lns_mode;
  1600. session->reorder_timeout = cfg->reorder_timeout;
  1601. session->offset = cfg->offset;
  1602. session->l2specific_type = cfg->l2specific_type;
  1603. session->l2specific_len = cfg->l2specific_len;
  1604. session->cookie_len = cfg->cookie_len;
  1605. memcpy(&session->cookie[0], &cfg->cookie[0], cfg->cookie_len);
  1606. session->peer_cookie_len = cfg->peer_cookie_len;
  1607. memcpy(&session->peer_cookie[0], &cfg->peer_cookie[0], cfg->peer_cookie_len);
  1608. }
  1609. if (tunnel->version == L2TP_HDR_VER_2)
  1610. session->build_header = l2tp_build_l2tpv2_header;
  1611. else
  1612. session->build_header = l2tp_build_l2tpv3_header;
  1613. l2tp_session_set_header_len(session, tunnel->version);
  1614. /* Bump the reference count. The session context is deleted
  1615. * only when this drops to zero.
  1616. */
  1617. l2tp_session_inc_refcount(session);
  1618. l2tp_tunnel_inc_refcount(tunnel);
  1619. /* Ensure tunnel socket isn't deleted */
  1620. sock_hold(tunnel->sock);
  1621. /* Add session to the tunnel's hash list */
  1622. write_lock_bh(&tunnel->hlist_lock);
  1623. hlist_add_head(&session->hlist,
  1624. l2tp_session_id_hash(tunnel, session_id));
  1625. write_unlock_bh(&tunnel->hlist_lock);
  1626. /* And to the global session list if L2TPv3 */
  1627. if (tunnel->version != L2TP_HDR_VER_2) {
  1628. struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
  1629. spin_lock_bh(&pn->l2tp_session_hlist_lock);
  1630. hlist_add_head_rcu(&session->global_hlist,
  1631. l2tp_session_id_hash_2(pn, session_id));
  1632. spin_unlock_bh(&pn->l2tp_session_hlist_lock);
  1633. }
  1634. /* Ignore management session in session count value */
  1635. if (session->session_id != 0)
  1636. atomic_inc(&l2tp_session_count);
  1637. }
  1638. return session;
  1639. }
  1640. EXPORT_SYMBOL_GPL(l2tp_session_create);
  1641. /*****************************************************************************
  1642. * Init and cleanup
  1643. *****************************************************************************/
  1644. static __net_init int l2tp_init_net(struct net *net)
  1645. {
  1646. struct l2tp_net *pn = net_generic(net, l2tp_net_id);
  1647. int hash;
  1648. INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
  1649. spin_lock_init(&pn->l2tp_tunnel_list_lock);
  1650. for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++)
  1651. INIT_HLIST_HEAD(&pn->l2tp_session_hlist[hash]);
  1652. spin_lock_init(&pn->l2tp_session_hlist_lock);
  1653. return 0;
  1654. }
  1655. static __net_exit void l2tp_exit_net(struct net *net)
  1656. {
  1657. struct l2tp_net *pn = l2tp_pernet(net);
  1658. struct l2tp_tunnel *tunnel = NULL;
  1659. rcu_read_lock_bh();
  1660. list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
  1661. (void)l2tp_tunnel_delete(tunnel);
  1662. }
  1663. rcu_read_unlock_bh();
  1664. }
  1665. static struct pernet_operations l2tp_net_ops = {
  1666. .init = l2tp_init_net,
  1667. .exit = l2tp_exit_net,
  1668. .id = &l2tp_net_id,
  1669. .size = sizeof(struct l2tp_net),
  1670. };
  1671. static int __init l2tp_init(void)
  1672. {
  1673. int rc = 0;
  1674. rc = register_pernet_device(&l2tp_net_ops);
  1675. if (rc)
  1676. goto out;
  1677. l2tp_wq = alloc_workqueue("l2tp", WQ_NON_REENTRANT | WQ_UNBOUND, 0);
  1678. if (!l2tp_wq) {
  1679. pr_err("alloc_workqueue failed\n");
  1680. rc = -ENOMEM;
  1681. goto out;
  1682. }
  1683. pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
  1684. out:
  1685. return rc;
  1686. }
  1687. static void __exit l2tp_exit(void)
  1688. {
  1689. unregister_pernet_device(&l2tp_net_ops);
  1690. if (l2tp_wq) {
  1691. destroy_workqueue(l2tp_wq);
  1692. l2tp_wq = NULL;
  1693. }
  1694. }
  1695. module_init(l2tp_init);
  1696. module_exit(l2tp_exit);
  1697. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  1698. MODULE_DESCRIPTION("L2TP core");
  1699. MODULE_LICENSE("GPL");
  1700. MODULE_VERSION(L2TP_DRV_VERSION);