l2tp_core.c 53 KB

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