l2tp_core.c 42 KB

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