l2tp_core.c 53 KB

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