l2tp_core.c 52 KB

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