l2tp_core.c 46 KB

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