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