l2tp_core.c 50 KB

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