l2tp_core.c 51 KB

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