l2tp_core.c 51 KB

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