l2tp_core.c 50 KB

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