l2tp_core.c 49 KB

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