l2tp_core.c 52 KB

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