l2tp_core.c 51 KB

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