l2tp_core.c 48 KB

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