l2tp_core.c 51 KB

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