l2tp_core.c 53 KB

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