l2tp_core.c 51 KB

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