l2tp_core.c 50 KB

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