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 void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
  101. static inline struct l2tp_net *l2tp_pernet(struct net *net)
  102. {
  103. BUG_ON(!net);
  104. return net_generic(net, l2tp_net_id);
  105. }
  106. /* Tunnel reference counts. Incremented per session that is added to
  107. * the tunnel.
  108. */
  109. static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
  110. {
  111. atomic_inc(&tunnel->ref_count);
  112. }
  113. static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
  114. {
  115. if (atomic_dec_and_test(&tunnel->ref_count))
  116. l2tp_tunnel_free(tunnel);
  117. }
  118. #ifdef L2TP_REFCNT_DEBUG
  119. #define l2tp_tunnel_inc_refcount(_t) \
  120. do { \
  121. pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \
  122. __func__, __LINE__, (_t)->name, \
  123. atomic_read(&_t->ref_count)); \
  124. l2tp_tunnel_inc_refcount_1(_t); \
  125. } while (0)
  126. #define l2tp_tunnel_dec_refcount(_t)
  127. do { \
  128. pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
  129. __func__, __LINE__, (_t)->name, \
  130. atomic_read(&_t->ref_count)); \
  131. l2tp_tunnel_dec_refcount_1(_t); \
  132. } while (0)
  133. #else
  134. #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
  135. #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
  136. #endif
  137. /* Session hash global list for L2TPv3.
  138. * The session_id SHOULD be random according to RFC3931, but several
  139. * L2TP implementations use incrementing session_ids. So we do a real
  140. * hash on the session_id, rather than a simple bitmask.
  141. */
  142. static inline struct hlist_head *
  143. l2tp_session_id_hash_2(struct l2tp_net *pn, u32 session_id)
  144. {
  145. return &pn->l2tp_session_hlist[hash_32(session_id, L2TP_HASH_BITS_2)];
  146. }
  147. /* Lookup the tunnel socket, possibly involving the fs code if the socket is
  148. * owned by userspace. A struct sock returned from this function must be
  149. * released using l2tp_tunnel_sock_put once you're done with it.
  150. */
  151. struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel)
  152. {
  153. int err = 0;
  154. struct socket *sock = NULL;
  155. struct sock *sk = NULL;
  156. if (!tunnel)
  157. goto out;
  158. if (tunnel->fd >= 0) {
  159. /* Socket is owned by userspace, who might be in the process
  160. * of closing it. Look the socket up using the fd to ensure
  161. * consistency.
  162. */
  163. sock = sockfd_lookup(tunnel->fd, &err);
  164. if (sock)
  165. sk = sock->sk;
  166. } else {
  167. /* Socket is owned by kernelspace */
  168. sk = tunnel->sock;
  169. }
  170. out:
  171. return sk;
  172. }
  173. EXPORT_SYMBOL_GPL(l2tp_tunnel_sock_lookup);
  174. /* Drop a reference to a tunnel socket obtained via. l2tp_tunnel_sock_put */
  175. void l2tp_tunnel_sock_put(struct sock *sk)
  176. {
  177. struct l2tp_tunnel *tunnel = l2tp_sock_to_tunnel(sk);
  178. if (tunnel) {
  179. if (tunnel->fd >= 0) {
  180. /* Socket is owned by userspace */
  181. sockfd_put(sk->sk_socket);
  182. }
  183. sock_put(sk);
  184. }
  185. }
  186. EXPORT_SYMBOL_GPL(l2tp_tunnel_sock_put);
  187. /* Lookup a session by id in the global session list
  188. */
  189. static struct l2tp_session *l2tp_session_find_2(struct net *net, u32 session_id)
  190. {
  191. struct l2tp_net *pn = l2tp_pernet(net);
  192. struct hlist_head *session_list =
  193. l2tp_session_id_hash_2(pn, session_id);
  194. struct l2tp_session *session;
  195. rcu_read_lock_bh();
  196. hlist_for_each_entry_rcu(session, session_list, global_hlist) {
  197. if (session->session_id == session_id) {
  198. rcu_read_unlock_bh();
  199. return session;
  200. }
  201. }
  202. rcu_read_unlock_bh();
  203. return NULL;
  204. }
  205. /* Session hash list.
  206. * The session_id SHOULD be random according to RFC2661, but several
  207. * L2TP implementations (Cisco and Microsoft) use incrementing
  208. * session_ids. So we do a real hash on the session_id, rather than a
  209. * simple bitmask.
  210. */
  211. static inline struct hlist_head *
  212. l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
  213. {
  214. return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
  215. }
  216. /* Lookup a session by id
  217. */
  218. struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id)
  219. {
  220. struct hlist_head *session_list;
  221. struct l2tp_session *session;
  222. /* In L2TPv3, session_ids are unique over all tunnels and we
  223. * sometimes need to look them up before we know the
  224. * tunnel.
  225. */
  226. if (tunnel == NULL)
  227. return l2tp_session_find_2(net, session_id);
  228. session_list = l2tp_session_id_hash(tunnel, session_id);
  229. read_lock_bh(&tunnel->hlist_lock);
  230. hlist_for_each_entry(session, session_list, hlist) {
  231. if (session->session_id == session_id) {
  232. read_unlock_bh(&tunnel->hlist_lock);
  233. return session;
  234. }
  235. }
  236. read_unlock_bh(&tunnel->hlist_lock);
  237. return NULL;
  238. }
  239. EXPORT_SYMBOL_GPL(l2tp_session_find);
  240. struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
  241. {
  242. int hash;
  243. struct l2tp_session *session;
  244. int count = 0;
  245. read_lock_bh(&tunnel->hlist_lock);
  246. for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
  247. hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
  248. if (++count > nth) {
  249. read_unlock_bh(&tunnel->hlist_lock);
  250. return session;
  251. }
  252. }
  253. }
  254. read_unlock_bh(&tunnel->hlist_lock);
  255. return NULL;
  256. }
  257. EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
  258. /* Lookup a session by interface name.
  259. * This is very inefficient but is only used by management interfaces.
  260. */
  261. struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname)
  262. {
  263. struct l2tp_net *pn = l2tp_pernet(net);
  264. int hash;
  265. struct l2tp_session *session;
  266. rcu_read_lock_bh();
  267. for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
  268. hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
  269. if (!strcmp(session->ifname, ifname)) {
  270. rcu_read_unlock_bh();
  271. return session;
  272. }
  273. }
  274. }
  275. rcu_read_unlock_bh();
  276. return NULL;
  277. }
  278. EXPORT_SYMBOL_GPL(l2tp_session_find_by_ifname);
  279. /* Lookup a tunnel by id
  280. */
  281. struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
  282. {
  283. struct l2tp_tunnel *tunnel;
  284. struct l2tp_net *pn = l2tp_pernet(net);
  285. rcu_read_lock_bh();
  286. list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
  287. if (tunnel->tunnel_id == tunnel_id) {
  288. rcu_read_unlock_bh();
  289. return tunnel;
  290. }
  291. }
  292. rcu_read_unlock_bh();
  293. return NULL;
  294. }
  295. EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
  296. struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
  297. {
  298. struct l2tp_net *pn = l2tp_pernet(net);
  299. struct l2tp_tunnel *tunnel;
  300. int count = 0;
  301. rcu_read_lock_bh();
  302. list_for_each_entry_rcu(tunnel, &pn->l2tp_tunnel_list, list) {
  303. if (++count > nth) {
  304. rcu_read_unlock_bh();
  305. return tunnel;
  306. }
  307. }
  308. rcu_read_unlock_bh();
  309. return NULL;
  310. }
  311. EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
  312. /*****************************************************************************
  313. * Receive data handling
  314. *****************************************************************************/
  315. /* Queue a skb in order. We come here only if the skb has an L2TP sequence
  316. * number.
  317. */
  318. static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
  319. {
  320. struct sk_buff *skbp;
  321. struct sk_buff *tmp;
  322. u32 ns = L2TP_SKB_CB(skb)->ns;
  323. struct l2tp_stats *sstats;
  324. spin_lock_bh(&session->reorder_q.lock);
  325. sstats = &session->stats;
  326. skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
  327. if (L2TP_SKB_CB(skbp)->ns > ns) {
  328. __skb_queue_before(&session->reorder_q, skbp, skb);
  329. l2tp_dbg(session, L2TP_MSG_SEQ,
  330. "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
  331. session->name, ns, L2TP_SKB_CB(skbp)->ns,
  332. skb_queue_len(&session->reorder_q));
  333. u64_stats_update_begin(&sstats->syncp);
  334. sstats->rx_oos_packets++;
  335. u64_stats_update_end(&sstats->syncp);
  336. goto out;
  337. }
  338. }
  339. __skb_queue_tail(&session->reorder_q, skb);
  340. out:
  341. spin_unlock_bh(&session->reorder_q.lock);
  342. }
  343. /* Dequeue a single skb.
  344. */
  345. static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
  346. {
  347. struct l2tp_tunnel *tunnel = session->tunnel;
  348. int length = L2TP_SKB_CB(skb)->length;
  349. struct l2tp_stats *tstats, *sstats;
  350. /* We're about to requeue the skb, so return resources
  351. * to its current owner (a socket receive buffer).
  352. */
  353. skb_orphan(skb);
  354. tstats = &tunnel->stats;
  355. u64_stats_update_begin(&tstats->syncp);
  356. sstats = &session->stats;
  357. u64_stats_update_begin(&sstats->syncp);
  358. tstats->rx_packets++;
  359. tstats->rx_bytes += length;
  360. sstats->rx_packets++;
  361. sstats->rx_bytes += length;
  362. u64_stats_update_end(&tstats->syncp);
  363. u64_stats_update_end(&sstats->syncp);
  364. if (L2TP_SKB_CB(skb)->has_seq) {
  365. /* Bump our Nr */
  366. session->nr++;
  367. if (tunnel->version == L2TP_HDR_VER_2)
  368. session->nr &= 0xffff;
  369. else
  370. session->nr &= 0xffffff;
  371. l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
  372. session->name, session->nr);
  373. }
  374. /* call private receive handler */
  375. if (session->recv_skb != NULL)
  376. (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
  377. else
  378. kfree_skb(skb);
  379. if (session->deref)
  380. (*session->deref)(session);
  381. }
  382. /* Dequeue skbs from the session's reorder_q, subject to packet order.
  383. * Skbs that have been in the queue for too long are simply discarded.
  384. */
  385. static void l2tp_recv_dequeue(struct l2tp_session *session)
  386. {
  387. struct sk_buff *skb;
  388. struct sk_buff *tmp;
  389. struct l2tp_stats *sstats;
  390. /* If the pkt at the head of the queue has the nr that we
  391. * expect to send up next, dequeue it and any other
  392. * in-sequence packets behind it.
  393. */
  394. start:
  395. spin_lock_bh(&session->reorder_q.lock);
  396. sstats = &session->stats;
  397. skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
  398. if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
  399. u64_stats_update_begin(&sstats->syncp);
  400. sstats->rx_seq_discards++;
  401. sstats->rx_errors++;
  402. u64_stats_update_end(&sstats->syncp);
  403. l2tp_dbg(session, L2TP_MSG_SEQ,
  404. "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
  405. session->name, L2TP_SKB_CB(skb)->ns,
  406. L2TP_SKB_CB(skb)->length, session->nr,
  407. skb_queue_len(&session->reorder_q));
  408. session->reorder_skip = 1;
  409. __skb_unlink(skb, &session->reorder_q);
  410. kfree_skb(skb);
  411. if (session->deref)
  412. (*session->deref)(session);
  413. continue;
  414. }
  415. if (L2TP_SKB_CB(skb)->has_seq) {
  416. if (session->reorder_skip) {
  417. l2tp_dbg(session, L2TP_MSG_SEQ,
  418. "%s: advancing nr to next pkt: %u -> %u",
  419. session->name, session->nr,
  420. L2TP_SKB_CB(skb)->ns);
  421. session->reorder_skip = 0;
  422. session->nr = L2TP_SKB_CB(skb)->ns;
  423. }
  424. if (L2TP_SKB_CB(skb)->ns != session->nr) {
  425. l2tp_dbg(session, L2TP_MSG_SEQ,
  426. "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
  427. session->name, L2TP_SKB_CB(skb)->ns,
  428. L2TP_SKB_CB(skb)->length, session->nr,
  429. skb_queue_len(&session->reorder_q));
  430. goto out;
  431. }
  432. }
  433. __skb_unlink(skb, &session->reorder_q);
  434. /* Process the skb. We release the queue lock while we
  435. * do so to let other contexts process the queue.
  436. */
  437. spin_unlock_bh(&session->reorder_q.lock);
  438. l2tp_recv_dequeue_skb(session, skb);
  439. goto start;
  440. }
  441. out:
  442. spin_unlock_bh(&session->reorder_q.lock);
  443. }
  444. static inline int l2tp_verify_udp_checksum(struct sock *sk,
  445. struct sk_buff *skb)
  446. {
  447. struct udphdr *uh = udp_hdr(skb);
  448. u16 ulen = ntohs(uh->len);
  449. __wsum psum;
  450. if (sk->sk_no_check || skb_csum_unnecessary(skb))
  451. return 0;
  452. #if IS_ENABLED(CONFIG_IPV6)
  453. if (sk->sk_family == PF_INET6) {
  454. if (!uh->check) {
  455. LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
  456. return 1;
  457. }
  458. if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
  459. !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  460. &ipv6_hdr(skb)->daddr, ulen,
  461. IPPROTO_UDP, skb->csum)) {
  462. skb->ip_summed = CHECKSUM_UNNECESSARY;
  463. return 0;
  464. }
  465. skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  466. &ipv6_hdr(skb)->daddr,
  467. skb->len, IPPROTO_UDP,
  468. 0));
  469. } else
  470. #endif
  471. {
  472. struct inet_sock *inet;
  473. if (!uh->check)
  474. return 0;
  475. inet = inet_sk(sk);
  476. psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
  477. ulen, IPPROTO_UDP, 0);
  478. if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
  479. !csum_fold(csum_add(psum, skb->csum)))
  480. return 0;
  481. skb->csum = psum;
  482. }
  483. return __skb_checksum_complete(skb);
  484. }
  485. /* Do receive processing of L2TP data frames. We handle both L2TPv2
  486. * and L2TPv3 data frames here.
  487. *
  488. * L2TPv2 Data Message Header
  489. *
  490. * 0 1 2 3
  491. * 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
  492. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  493. * |T|L|x|x|S|x|O|P|x|x|x|x| Ver | Length (opt) |
  494. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  495. * | Tunnel ID | Session ID |
  496. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  497. * | Ns (opt) | Nr (opt) |
  498. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  499. * | Offset Size (opt) | Offset pad... (opt)
  500. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  501. *
  502. * Data frames are marked by T=0. All other fields are the same as
  503. * those in L2TP control frames.
  504. *
  505. * L2TPv3 Data Message Header
  506. *
  507. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  508. * | L2TP Session Header |
  509. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  510. * | L2-Specific Sublayer |
  511. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  512. * | Tunnel Payload ...
  513. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  514. *
  515. * L2TPv3 Session Header Over IP
  516. *
  517. * 0 1 2 3
  518. * 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
  519. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  520. * | Session ID |
  521. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  522. * | Cookie (optional, maximum 64 bits)...
  523. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  524. * |
  525. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  526. *
  527. * L2TPv3 L2-Specific Sublayer Format
  528. *
  529. * 0 1 2 3
  530. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  531. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  532. * |x|S|x|x|x|x|x|x| Sequence Number |
  533. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  534. *
  535. * Cookie value, sublayer format and offset (pad) are negotiated with
  536. * the peer when the session is set up. Unlike L2TPv2, we do not need
  537. * to parse the packet header to determine if optional fields are
  538. * present.
  539. *
  540. * Caller must already have parsed the frame and determined that it is
  541. * a data (not control) frame before coming here. Fields up to the
  542. * session-id have already been parsed and ptr points to the data
  543. * after the session-id.
  544. */
  545. void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
  546. unsigned char *ptr, unsigned char *optr, u16 hdrflags,
  547. int length, int (*payload_hook)(struct sk_buff *skb))
  548. {
  549. struct l2tp_tunnel *tunnel = session->tunnel;
  550. int offset;
  551. u32 ns, nr;
  552. struct l2tp_stats *sstats = &session->stats;
  553. /* The ref count is increased since we now hold a pointer to
  554. * the session. Take care to decrement the refcnt when exiting
  555. * this function from now on...
  556. */
  557. l2tp_session_inc_refcount(session);
  558. if (session->ref)
  559. (*session->ref)(session);
  560. /* Parse and check optional cookie */
  561. if (session->peer_cookie_len > 0) {
  562. if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
  563. l2tp_info(tunnel, L2TP_MSG_DATA,
  564. "%s: cookie mismatch (%u/%u). Discarding.\n",
  565. tunnel->name, tunnel->tunnel_id,
  566. session->session_id);
  567. u64_stats_update_begin(&sstats->syncp);
  568. sstats->rx_cookie_discards++;
  569. u64_stats_update_end(&sstats->syncp);
  570. goto discard;
  571. }
  572. ptr += session->peer_cookie_len;
  573. }
  574. /* Handle the optional sequence numbers. Sequence numbers are
  575. * in different places for L2TPv2 and L2TPv3.
  576. *
  577. * If we are the LAC, enable/disable sequence numbers under
  578. * the control of the LNS. If no sequence numbers present but
  579. * we were expecting them, discard frame.
  580. */
  581. ns = nr = 0;
  582. L2TP_SKB_CB(skb)->has_seq = 0;
  583. if (tunnel->version == L2TP_HDR_VER_2) {
  584. if (hdrflags & L2TP_HDRFLAG_S) {
  585. ns = ntohs(*(__be16 *) ptr);
  586. ptr += 2;
  587. nr = ntohs(*(__be16 *) ptr);
  588. ptr += 2;
  589. /* Store L2TP info in the skb */
  590. L2TP_SKB_CB(skb)->ns = ns;
  591. L2TP_SKB_CB(skb)->has_seq = 1;
  592. l2tp_dbg(session, L2TP_MSG_SEQ,
  593. "%s: recv data ns=%u, nr=%u, session nr=%u\n",
  594. session->name, ns, nr, session->nr);
  595. }
  596. } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
  597. u32 l2h = ntohl(*(__be32 *) ptr);
  598. if (l2h & 0x40000000) {
  599. ns = l2h & 0x00ffffff;
  600. /* Store L2TP info in the skb */
  601. L2TP_SKB_CB(skb)->ns = ns;
  602. L2TP_SKB_CB(skb)->has_seq = 1;
  603. l2tp_dbg(session, L2TP_MSG_SEQ,
  604. "%s: recv data ns=%u, session nr=%u\n",
  605. session->name, ns, session->nr);
  606. }
  607. }
  608. /* Advance past L2-specific header, if present */
  609. ptr += session->l2specific_len;
  610. if (L2TP_SKB_CB(skb)->has_seq) {
  611. /* Received a packet with sequence numbers. If we're the LNS,
  612. * check if we sre sending sequence numbers and if not,
  613. * configure it so.
  614. */
  615. if ((!session->lns_mode) && (!session->send_seq)) {
  616. l2tp_info(session, L2TP_MSG_SEQ,
  617. "%s: requested to enable seq numbers by LNS\n",
  618. session->name);
  619. session->send_seq = -1;
  620. l2tp_session_set_header_len(session, tunnel->version);
  621. }
  622. } else {
  623. /* No sequence numbers.
  624. * If user has configured mandatory sequence numbers, discard.
  625. */
  626. if (session->recv_seq) {
  627. l2tp_warn(session, L2TP_MSG_SEQ,
  628. "%s: recv data has no seq numbers when required. Discarding.\n",
  629. session->name);
  630. u64_stats_update_begin(&sstats->syncp);
  631. sstats->rx_seq_discards++;
  632. u64_stats_update_end(&sstats->syncp);
  633. goto discard;
  634. }
  635. /* If we're the LAC and we're sending sequence numbers, the
  636. * LNS has requested that we no longer send sequence numbers.
  637. * If we're the LNS and we're sending sequence numbers, the
  638. * LAC is broken. Discard the frame.
  639. */
  640. if ((!session->lns_mode) && (session->send_seq)) {
  641. l2tp_info(session, L2TP_MSG_SEQ,
  642. "%s: requested to disable seq numbers by LNS\n",
  643. session->name);
  644. session->send_seq = 0;
  645. l2tp_session_set_header_len(session, tunnel->version);
  646. } else if (session->send_seq) {
  647. l2tp_warn(session, L2TP_MSG_SEQ,
  648. "%s: recv data has no seq numbers when required. Discarding.\n",
  649. session->name);
  650. u64_stats_update_begin(&sstats->syncp);
  651. sstats->rx_seq_discards++;
  652. u64_stats_update_end(&sstats->syncp);
  653. goto discard;
  654. }
  655. }
  656. /* Session data offset is handled differently for L2TPv2 and
  657. * L2TPv3. For L2TPv2, there is an optional 16-bit value in
  658. * the header. For L2TPv3, the offset is negotiated using AVPs
  659. * in the session setup control protocol.
  660. */
  661. if (tunnel->version == L2TP_HDR_VER_2) {
  662. /* If offset bit set, skip it. */
  663. if (hdrflags & L2TP_HDRFLAG_O) {
  664. offset = ntohs(*(__be16 *)ptr);
  665. ptr += 2 + offset;
  666. }
  667. } else
  668. ptr += session->offset;
  669. offset = ptr - optr;
  670. if (!pskb_may_pull(skb, offset))
  671. goto discard;
  672. __skb_pull(skb, offset);
  673. /* If caller wants to process the payload before we queue the
  674. * packet, do so now.
  675. */
  676. if (payload_hook)
  677. if ((*payload_hook)(skb))
  678. goto discard;
  679. /* Prepare skb for adding to the session's reorder_q. Hold
  680. * packets for max reorder_timeout or 1 second if not
  681. * reordering.
  682. */
  683. L2TP_SKB_CB(skb)->length = length;
  684. L2TP_SKB_CB(skb)->expires = jiffies +
  685. (session->reorder_timeout ? session->reorder_timeout : HZ);
  686. /* Add packet to the session's receive queue. Reordering is done here, if
  687. * enabled. Saved L2TP protocol info is stored in skb->sb[].
  688. */
  689. if (L2TP_SKB_CB(skb)->has_seq) {
  690. if (session->reorder_timeout != 0) {
  691. /* Packet reordering enabled. Add skb to session's
  692. * reorder queue, in order of ns.
  693. */
  694. l2tp_recv_queue_skb(session, skb);
  695. } else {
  696. /* Packet reordering disabled. Discard out-of-sequence
  697. * packets
  698. */
  699. if (L2TP_SKB_CB(skb)->ns != session->nr) {
  700. u64_stats_update_begin(&sstats->syncp);
  701. sstats->rx_seq_discards++;
  702. u64_stats_update_end(&sstats->syncp);
  703. l2tp_dbg(session, L2TP_MSG_SEQ,
  704. "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
  705. session->name, L2TP_SKB_CB(skb)->ns,
  706. L2TP_SKB_CB(skb)->length, session->nr,
  707. skb_queue_len(&session->reorder_q));
  708. goto discard;
  709. }
  710. skb_queue_tail(&session->reorder_q, skb);
  711. }
  712. } else {
  713. /* No sequence numbers. Add the skb to the tail of the
  714. * reorder queue. This ensures that it will be
  715. * delivered after all previous sequenced skbs.
  716. */
  717. skb_queue_tail(&session->reorder_q, skb);
  718. }
  719. /* Try to dequeue as many skbs from reorder_q as we can. */
  720. l2tp_recv_dequeue(session);
  721. l2tp_session_dec_refcount(session);
  722. return;
  723. discard:
  724. u64_stats_update_begin(&sstats->syncp);
  725. sstats->rx_errors++;
  726. u64_stats_update_end(&sstats->syncp);
  727. kfree_skb(skb);
  728. if (session->deref)
  729. (*session->deref)(session);
  730. l2tp_session_dec_refcount(session);
  731. }
  732. EXPORT_SYMBOL(l2tp_recv_common);
  733. /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
  734. * here. The skb is not on a list when we get here.
  735. * Returns 0 if the packet was a data packet and was successfully passed on.
  736. * Returns 1 if the packet was not a good data packet and could not be
  737. * forwarded. All such packets are passed up to userspace to deal with.
  738. */
  739. static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
  740. int (*payload_hook)(struct sk_buff *skb))
  741. {
  742. struct l2tp_session *session = NULL;
  743. unsigned char *ptr, *optr;
  744. u16 hdrflags;
  745. u32 tunnel_id, session_id;
  746. u16 version;
  747. int length;
  748. struct l2tp_stats *tstats;
  749. if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
  750. goto discard_bad_csum;
  751. /* UDP always verifies the packet length. */
  752. __skb_pull(skb, sizeof(struct udphdr));
  753. /* Short packet? */
  754. if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
  755. l2tp_info(tunnel, L2TP_MSG_DATA,
  756. "%s: recv short packet (len=%d)\n",
  757. tunnel->name, skb->len);
  758. goto error;
  759. }
  760. /* Trace packet contents, if enabled */
  761. if (tunnel->debug & L2TP_MSG_DATA) {
  762. length = min(32u, skb->len);
  763. if (!pskb_may_pull(skb, length))
  764. goto error;
  765. pr_debug("%s: recv\n", tunnel->name);
  766. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
  767. }
  768. /* Point to L2TP header */
  769. optr = ptr = skb->data;
  770. /* Get L2TP header flags */
  771. hdrflags = ntohs(*(__be16 *) ptr);
  772. /* Check protocol version */
  773. version = hdrflags & L2TP_HDR_VER_MASK;
  774. if (version != tunnel->version) {
  775. l2tp_info(tunnel, L2TP_MSG_DATA,
  776. "%s: recv protocol version mismatch: got %d expected %d\n",
  777. tunnel->name, version, tunnel->version);
  778. goto error;
  779. }
  780. /* Get length of L2TP packet */
  781. length = skb->len;
  782. /* If type is control packet, it is handled by userspace. */
  783. if (hdrflags & L2TP_HDRFLAG_T) {
  784. l2tp_dbg(tunnel, L2TP_MSG_DATA,
  785. "%s: recv control packet, len=%d\n",
  786. tunnel->name, length);
  787. goto error;
  788. }
  789. /* Skip flags */
  790. ptr += 2;
  791. if (tunnel->version == L2TP_HDR_VER_2) {
  792. /* If length is present, skip it */
  793. if (hdrflags & L2TP_HDRFLAG_L)
  794. ptr += 2;
  795. /* Extract tunnel and session ID */
  796. tunnel_id = ntohs(*(__be16 *) ptr);
  797. ptr += 2;
  798. session_id = ntohs(*(__be16 *) ptr);
  799. ptr += 2;
  800. } else {
  801. ptr += 2; /* skip reserved bits */
  802. tunnel_id = tunnel->tunnel_id;
  803. session_id = ntohl(*(__be32 *) ptr);
  804. ptr += 4;
  805. }
  806. /* Find the session context */
  807. session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
  808. if (!session || !session->recv_skb) {
  809. /* Not found? Pass to userspace to deal with */
  810. l2tp_info(tunnel, L2TP_MSG_DATA,
  811. "%s: no session found (%u/%u). Passing up.\n",
  812. tunnel->name, tunnel_id, session_id);
  813. goto error;
  814. }
  815. l2tp_recv_common(session, skb, ptr, optr, hdrflags, length, payload_hook);
  816. return 0;
  817. discard_bad_csum:
  818. LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
  819. UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
  820. tstats = &tunnel->stats;
  821. u64_stats_update_begin(&tstats->syncp);
  822. tstats->rx_errors++;
  823. u64_stats_update_end(&tstats->syncp);
  824. kfree_skb(skb);
  825. return 0;
  826. error:
  827. /* Put UDP header back */
  828. __skb_push(skb, sizeof(struct udphdr));
  829. return 1;
  830. }
  831. /* UDP encapsulation receive handler. See net/ipv4/udp.c.
  832. * Return codes:
  833. * 0 : success.
  834. * <0: error
  835. * >0: skb should be passed up to userspace as UDP.
  836. */
  837. int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  838. {
  839. struct l2tp_tunnel *tunnel;
  840. tunnel = l2tp_sock_to_tunnel(sk);
  841. if (tunnel == NULL)
  842. goto pass_up;
  843. l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
  844. tunnel->name, skb->len);
  845. if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
  846. goto pass_up_put;
  847. sock_put(sk);
  848. return 0;
  849. pass_up_put:
  850. sock_put(sk);
  851. pass_up:
  852. return 1;
  853. }
  854. EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
  855. /************************************************************************
  856. * Transmit handling
  857. ***********************************************************************/
  858. /* Build an L2TP header for the session into the buffer provided.
  859. */
  860. static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
  861. {
  862. struct l2tp_tunnel *tunnel = session->tunnel;
  863. __be16 *bufp = buf;
  864. __be16 *optr = buf;
  865. u16 flags = L2TP_HDR_VER_2;
  866. u32 tunnel_id = tunnel->peer_tunnel_id;
  867. u32 session_id = session->peer_session_id;
  868. if (session->send_seq)
  869. flags |= L2TP_HDRFLAG_S;
  870. /* Setup L2TP header. */
  871. *bufp++ = htons(flags);
  872. *bufp++ = htons(tunnel_id);
  873. *bufp++ = htons(session_id);
  874. if (session->send_seq) {
  875. *bufp++ = htons(session->ns);
  876. *bufp++ = 0;
  877. session->ns++;
  878. session->ns &= 0xffff;
  879. l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
  880. session->name, session->ns);
  881. }
  882. return bufp - optr;
  883. }
  884. static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
  885. {
  886. struct l2tp_tunnel *tunnel = session->tunnel;
  887. char *bufp = buf;
  888. char *optr = bufp;
  889. /* Setup L2TP header. The header differs slightly for UDP and
  890. * IP encapsulations. For UDP, there is 4 bytes of flags.
  891. */
  892. if (tunnel->encap == L2TP_ENCAPTYPE_UDP) {
  893. u16 flags = L2TP_HDR_VER_3;
  894. *((__be16 *) bufp) = htons(flags);
  895. bufp += 2;
  896. *((__be16 *) bufp) = 0;
  897. bufp += 2;
  898. }
  899. *((__be32 *) bufp) = htonl(session->peer_session_id);
  900. bufp += 4;
  901. if (session->cookie_len) {
  902. memcpy(bufp, &session->cookie[0], session->cookie_len);
  903. bufp += session->cookie_len;
  904. }
  905. if (session->l2specific_len) {
  906. if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
  907. u32 l2h = 0;
  908. if (session->send_seq) {
  909. l2h = 0x40000000 | session->ns;
  910. session->ns++;
  911. session->ns &= 0xffffff;
  912. l2tp_dbg(session, L2TP_MSG_SEQ,
  913. "%s: updated ns to %u\n",
  914. session->name, session->ns);
  915. }
  916. *((__be32 *) bufp) = htonl(l2h);
  917. }
  918. bufp += session->l2specific_len;
  919. }
  920. if (session->offset)
  921. bufp += session->offset;
  922. return bufp - optr;
  923. }
  924. static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
  925. struct flowi *fl, size_t data_len)
  926. {
  927. struct l2tp_tunnel *tunnel = session->tunnel;
  928. unsigned int len = skb->len;
  929. int error;
  930. struct l2tp_stats *tstats, *sstats;
  931. /* Debug */
  932. if (session->send_seq)
  933. l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
  934. session->name, data_len, session->ns - 1);
  935. else
  936. l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
  937. session->name, data_len);
  938. if (session->debug & L2TP_MSG_DATA) {
  939. int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
  940. unsigned char *datap = skb->data + uhlen;
  941. pr_debug("%s: xmit\n", session->name);
  942. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
  943. datap, min_t(size_t, 32, len - uhlen));
  944. }
  945. /* Queue the packet to IP for output */
  946. skb->local_df = 1;
  947. #if IS_ENABLED(CONFIG_IPV6)
  948. if (skb->sk->sk_family == PF_INET6)
  949. error = inet6_csk_xmit(skb, NULL);
  950. else
  951. #endif
  952. error = ip_queue_xmit(skb, fl);
  953. /* Update stats */
  954. tstats = &tunnel->stats;
  955. u64_stats_update_begin(&tstats->syncp);
  956. sstats = &session->stats;
  957. u64_stats_update_begin(&sstats->syncp);
  958. if (error >= 0) {
  959. tstats->tx_packets++;
  960. tstats->tx_bytes += len;
  961. sstats->tx_packets++;
  962. sstats->tx_bytes += len;
  963. } else {
  964. tstats->tx_errors++;
  965. sstats->tx_errors++;
  966. }
  967. u64_stats_update_end(&tstats->syncp);
  968. u64_stats_update_end(&sstats->syncp);
  969. return 0;
  970. }
  971. /* Automatically called when the skb is freed.
  972. */
  973. static void l2tp_sock_wfree(struct sk_buff *skb)
  974. {
  975. sock_put(skb->sk);
  976. }
  977. /* For data skbs that we transmit, we associate with the tunnel socket
  978. * but don't do accounting.
  979. */
  980. static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
  981. {
  982. sock_hold(sk);
  983. skb->sk = sk;
  984. skb->destructor = l2tp_sock_wfree;
  985. }
  986. #if IS_ENABLED(CONFIG_IPV6)
  987. static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
  988. int udp_len)
  989. {
  990. struct ipv6_pinfo *np = inet6_sk(sk);
  991. struct udphdr *uh = udp_hdr(skb);
  992. if (!skb_dst(skb) || !skb_dst(skb)->dev ||
  993. !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
  994. __wsum csum = skb_checksum(skb, 0, udp_len, 0);
  995. skb->ip_summed = CHECKSUM_UNNECESSARY;
  996. uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
  997. IPPROTO_UDP, csum);
  998. if (uh->check == 0)
  999. uh->check = CSUM_MANGLED_0;
  1000. } else {
  1001. skb->ip_summed = CHECKSUM_PARTIAL;
  1002. skb->csum_start = skb_transport_header(skb) - skb->head;
  1003. skb->csum_offset = offsetof(struct udphdr, check);
  1004. uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
  1005. udp_len, IPPROTO_UDP, 0);
  1006. }
  1007. }
  1008. #endif
  1009. /* If caller requires the skb to have a ppp header, the header must be
  1010. * inserted in the skb data before calling this function.
  1011. */
  1012. int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
  1013. {
  1014. int data_len = skb->len;
  1015. struct l2tp_tunnel *tunnel = session->tunnel;
  1016. struct sock *sk = tunnel->sock;
  1017. struct flowi *fl;
  1018. struct udphdr *uh;
  1019. struct inet_sock *inet;
  1020. __wsum csum;
  1021. int headroom;
  1022. int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
  1023. int udp_len;
  1024. int ret = NET_XMIT_SUCCESS;
  1025. /* Check that there's enough headroom in the skb to insert IP,
  1026. * UDP and L2TP headers. If not enough, expand it to
  1027. * make room. Adjust truesize.
  1028. */
  1029. headroom = NET_SKB_PAD + sizeof(struct iphdr) +
  1030. uhlen + hdr_len;
  1031. if (skb_cow_head(skb, headroom)) {
  1032. kfree_skb(skb);
  1033. return NET_XMIT_DROP;
  1034. }
  1035. skb_orphan(skb);
  1036. /* Setup L2TP header */
  1037. session->build_header(session, __skb_push(skb, hdr_len));
  1038. /* Reset skb netfilter state */
  1039. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  1040. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  1041. IPSKB_REROUTED);
  1042. nf_reset(skb);
  1043. bh_lock_sock(sk);
  1044. if (sock_owned_by_user(sk)) {
  1045. kfree_skb(skb);
  1046. ret = NET_XMIT_DROP;
  1047. goto out_unlock;
  1048. }
  1049. /* Get routing info from the tunnel socket */
  1050. skb_dst_drop(skb);
  1051. skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0)));
  1052. inet = inet_sk(sk);
  1053. fl = &inet->cork.fl;
  1054. switch (tunnel->encap) {
  1055. case L2TP_ENCAPTYPE_UDP:
  1056. /* Setup UDP header */
  1057. __skb_push(skb, sizeof(*uh));
  1058. skb_reset_transport_header(skb);
  1059. uh = udp_hdr(skb);
  1060. uh->source = inet->inet_sport;
  1061. uh->dest = inet->inet_dport;
  1062. udp_len = uhlen + hdr_len + data_len;
  1063. uh->len = htons(udp_len);
  1064. uh->check = 0;
  1065. /* Calculate UDP checksum if configured to do so */
  1066. #if IS_ENABLED(CONFIG_IPV6)
  1067. if (sk->sk_family == PF_INET6)
  1068. l2tp_xmit_ipv6_csum(sk, skb, udp_len);
  1069. else
  1070. #endif
  1071. if (sk->sk_no_check == UDP_CSUM_NOXMIT)
  1072. skb->ip_summed = CHECKSUM_NONE;
  1073. else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
  1074. (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
  1075. skb->ip_summed = CHECKSUM_COMPLETE;
  1076. csum = skb_checksum(skb, 0, udp_len, 0);
  1077. uh->check = csum_tcpudp_magic(inet->inet_saddr,
  1078. inet->inet_daddr,
  1079. udp_len, IPPROTO_UDP, csum);
  1080. if (uh->check == 0)
  1081. uh->check = CSUM_MANGLED_0;
  1082. } else {
  1083. skb->ip_summed = CHECKSUM_PARTIAL;
  1084. skb->csum_start = skb_transport_header(skb) - skb->head;
  1085. skb->csum_offset = offsetof(struct udphdr, check);
  1086. uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
  1087. inet->inet_daddr,
  1088. udp_len, IPPROTO_UDP, 0);
  1089. }
  1090. break;
  1091. case L2TP_ENCAPTYPE_IP:
  1092. break;
  1093. }
  1094. l2tp_skb_set_owner_w(skb, sk);
  1095. l2tp_xmit_core(session, skb, fl, data_len);
  1096. out_unlock:
  1097. bh_unlock_sock(sk);
  1098. return ret;
  1099. }
  1100. EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
  1101. /*****************************************************************************
  1102. * Tinnel and session create/destroy.
  1103. *****************************************************************************/
  1104. /* Tunnel socket destruct hook.
  1105. * The tunnel context is deleted only when all session sockets have been
  1106. * closed.
  1107. */
  1108. static void l2tp_tunnel_destruct(struct sock *sk)
  1109. {
  1110. struct l2tp_tunnel *tunnel;
  1111. struct l2tp_net *pn;
  1112. tunnel = sk->sk_user_data;
  1113. if (tunnel == NULL)
  1114. goto end;
  1115. l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
  1116. /* Disable udp encapsulation */
  1117. switch (tunnel->encap) {
  1118. case L2TP_ENCAPTYPE_UDP:
  1119. /* No longer an encapsulation socket. See net/ipv4/udp.c */
  1120. (udp_sk(sk))->encap_type = 0;
  1121. (udp_sk(sk))->encap_rcv = NULL;
  1122. (udp_sk(sk))->encap_destroy = NULL;
  1123. break;
  1124. case L2TP_ENCAPTYPE_IP:
  1125. break;
  1126. }
  1127. /* Remove hooks into tunnel socket */
  1128. sk->sk_destruct = tunnel->old_sk_destruct;
  1129. sk->sk_user_data = NULL;
  1130. tunnel->sock = NULL;
  1131. /* Remove the tunnel struct from the tunnel list */
  1132. pn = l2tp_pernet(tunnel->l2tp_net);
  1133. spin_lock_bh(&pn->l2tp_tunnel_list_lock);
  1134. list_del_rcu(&tunnel->list);
  1135. spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
  1136. atomic_dec(&l2tp_tunnel_count);
  1137. l2tp_tunnel_closeall(tunnel);
  1138. l2tp_tunnel_dec_refcount(tunnel);
  1139. /* Call the original destructor */
  1140. if (sk->sk_destruct)
  1141. (*sk->sk_destruct)(sk);
  1142. end:
  1143. return;
  1144. }
  1145. /* When the tunnel is closed, all the attached sessions need to go too.
  1146. */
  1147. static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
  1148. {
  1149. int hash;
  1150. struct hlist_node *walk;
  1151. struct hlist_node *tmp;
  1152. struct l2tp_session *session;
  1153. BUG_ON(tunnel == NULL);
  1154. l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
  1155. tunnel->name);
  1156. write_lock_bh(&tunnel->hlist_lock);
  1157. for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
  1158. again:
  1159. hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
  1160. session = hlist_entry(walk, struct l2tp_session, hlist);
  1161. l2tp_info(session, L2TP_MSG_CONTROL,
  1162. "%s: closing session\n", session->name);
  1163. hlist_del_init(&session->hlist);
  1164. /* Since we should hold the sock lock while
  1165. * doing any unbinding, we need to release the
  1166. * lock we're holding before taking that lock.
  1167. * Hold a reference to the sock so it doesn't
  1168. * disappear as we're jumping between locks.
  1169. */
  1170. if (session->ref != NULL)
  1171. (*session->ref)(session);
  1172. write_unlock_bh(&tunnel->hlist_lock);
  1173. if (tunnel->version != L2TP_HDR_VER_2) {
  1174. struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
  1175. spin_lock_bh(&pn->l2tp_session_hlist_lock);
  1176. hlist_del_init_rcu(&session->global_hlist);
  1177. spin_unlock_bh(&pn->l2tp_session_hlist_lock);
  1178. synchronize_rcu();
  1179. }
  1180. if (session->session_close != NULL)
  1181. (*session->session_close)(session);
  1182. if (session->deref != NULL)
  1183. (*session->deref)(session);
  1184. l2tp_session_dec_refcount(session);
  1185. write_lock_bh(&tunnel->hlist_lock);
  1186. /* Now restart from the beginning of this hash
  1187. * chain. We always remove a session from the
  1188. * list so we are guaranteed to make forward
  1189. * progress.
  1190. */
  1191. goto again;
  1192. }
  1193. }
  1194. write_unlock_bh(&tunnel->hlist_lock);
  1195. }
  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);