l2tp_core.c 43 KB

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