l2tp_core.c 53 KB

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