l2tp_core.c 54 KB

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