l2tp_core.c 55 KB

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