l2tp_core.c 45 KB

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