l2tp_core.c 55 KB

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