l2tp_core.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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/ip.h>
  37. #include <linux/udp.h>
  38. #include <linux/hash.h>
  39. #include <linux/sort.h>
  40. #include <linux/file.h>
  41. #include <linux/nsproxy.h>
  42. #include <net/net_namespace.h>
  43. #include <net/netns/generic.h>
  44. #include <net/dst.h>
  45. #include <net/ip.h>
  46. #include <net/udp.h>
  47. #include <net/xfrm.h>
  48. #include <asm/byteorder.h>
  49. #include <asm/atomic.h>
  50. #include "l2tp_core.h"
  51. #define L2TP_DRV_VERSION "V2.0"
  52. /* L2TP header constants */
  53. #define L2TP_HDRFLAG_T 0x8000
  54. #define L2TP_HDRFLAG_L 0x4000
  55. #define L2TP_HDRFLAG_S 0x0800
  56. #define L2TP_HDRFLAG_O 0x0200
  57. #define L2TP_HDRFLAG_P 0x0100
  58. #define L2TP_HDR_VER_MASK 0x000F
  59. #define L2TP_HDR_VER_2 0x0002
  60. /* L2TPv3 default L2-specific sublayer */
  61. #define L2TP_SLFLAG_S 0x40000000
  62. #define L2TP_SL_SEQ_MASK 0x00ffffff
  63. #define L2TP_HDR_SIZE_SEQ 10
  64. #define L2TP_HDR_SIZE_NOSEQ 6
  65. /* Default trace flags */
  66. #define L2TP_DEFAULT_DEBUG_FLAGS 0
  67. #define PRINTK(_mask, _type, _lvl, _fmt, args...) \
  68. do { \
  69. if ((_mask) & (_type)) \
  70. printk(_lvl "L2TP: " _fmt, ##args); \
  71. } while (0)
  72. /* Private data stored for received packets in the skb.
  73. */
  74. struct l2tp_skb_cb {
  75. u16 ns;
  76. u16 has_seq;
  77. u16 length;
  78. unsigned long expires;
  79. };
  80. #define L2TP_SKB_CB(skb) ((struct l2tp_skb_cb *) &skb->cb[sizeof(struct inet_skb_parm)])
  81. static atomic_t l2tp_tunnel_count;
  82. static atomic_t l2tp_session_count;
  83. /* per-net private data for this module */
  84. static unsigned int l2tp_net_id;
  85. struct l2tp_net {
  86. struct list_head l2tp_tunnel_list;
  87. rwlock_t l2tp_tunnel_list_lock;
  88. };
  89. static inline struct l2tp_net *l2tp_pernet(struct net *net)
  90. {
  91. BUG_ON(!net);
  92. return net_generic(net, l2tp_net_id);
  93. }
  94. /* Session hash list.
  95. * The session_id SHOULD be random according to RFC2661, but several
  96. * L2TP implementations (Cisco and Microsoft) use incrementing
  97. * session_ids. So we do a real hash on the session_id, rather than a
  98. * simple bitmask.
  99. */
  100. static inline struct hlist_head *
  101. l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
  102. {
  103. return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
  104. }
  105. /* Lookup a session by id
  106. */
  107. struct l2tp_session *l2tp_session_find(struct l2tp_tunnel *tunnel, u32 session_id)
  108. {
  109. struct hlist_head *session_list =
  110. l2tp_session_id_hash(tunnel, session_id);
  111. struct l2tp_session *session;
  112. struct hlist_node *walk;
  113. read_lock_bh(&tunnel->hlist_lock);
  114. hlist_for_each_entry(session, walk, session_list, hlist) {
  115. if (session->session_id == session_id) {
  116. read_unlock_bh(&tunnel->hlist_lock);
  117. return session;
  118. }
  119. }
  120. read_unlock_bh(&tunnel->hlist_lock);
  121. return NULL;
  122. }
  123. EXPORT_SYMBOL_GPL(l2tp_session_find);
  124. struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
  125. {
  126. int hash;
  127. struct hlist_node *walk;
  128. struct l2tp_session *session;
  129. int count = 0;
  130. read_lock_bh(&tunnel->hlist_lock);
  131. for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
  132. hlist_for_each_entry(session, walk, &tunnel->session_hlist[hash], hlist) {
  133. if (++count > nth) {
  134. read_unlock_bh(&tunnel->hlist_lock);
  135. return session;
  136. }
  137. }
  138. }
  139. read_unlock_bh(&tunnel->hlist_lock);
  140. return NULL;
  141. }
  142. EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
  143. /* Lookup a tunnel by id
  144. */
  145. struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id)
  146. {
  147. struct l2tp_tunnel *tunnel;
  148. struct l2tp_net *pn = l2tp_pernet(net);
  149. read_lock_bh(&pn->l2tp_tunnel_list_lock);
  150. list_for_each_entry(tunnel, &pn->l2tp_tunnel_list, list) {
  151. if (tunnel->tunnel_id == tunnel_id) {
  152. read_unlock_bh(&pn->l2tp_tunnel_list_lock);
  153. return tunnel;
  154. }
  155. }
  156. read_unlock_bh(&pn->l2tp_tunnel_list_lock);
  157. return NULL;
  158. }
  159. EXPORT_SYMBOL_GPL(l2tp_tunnel_find);
  160. struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth)
  161. {
  162. struct l2tp_net *pn = l2tp_pernet(net);
  163. struct l2tp_tunnel *tunnel;
  164. int count = 0;
  165. read_lock_bh(&pn->l2tp_tunnel_list_lock);
  166. list_for_each_entry(tunnel, &pn->l2tp_tunnel_list, list) {
  167. if (++count > nth) {
  168. read_unlock_bh(&pn->l2tp_tunnel_list_lock);
  169. return tunnel;
  170. }
  171. }
  172. read_unlock_bh(&pn->l2tp_tunnel_list_lock);
  173. return NULL;
  174. }
  175. EXPORT_SYMBOL_GPL(l2tp_tunnel_find_nth);
  176. /*****************************************************************************
  177. * Receive data handling
  178. *****************************************************************************/
  179. /* Queue a skb in order. We come here only if the skb has an L2TP sequence
  180. * number.
  181. */
  182. static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
  183. {
  184. struct sk_buff *skbp;
  185. struct sk_buff *tmp;
  186. u16 ns = L2TP_SKB_CB(skb)->ns;
  187. spin_lock_bh(&session->reorder_q.lock);
  188. skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
  189. if (L2TP_SKB_CB(skbp)->ns > ns) {
  190. __skb_queue_before(&session->reorder_q, skbp, skb);
  191. PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
  192. "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
  193. session->name, ns, L2TP_SKB_CB(skbp)->ns,
  194. skb_queue_len(&session->reorder_q));
  195. session->stats.rx_oos_packets++;
  196. goto out;
  197. }
  198. }
  199. __skb_queue_tail(&session->reorder_q, skb);
  200. out:
  201. spin_unlock_bh(&session->reorder_q.lock);
  202. }
  203. /* Dequeue a single skb.
  204. */
  205. static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
  206. {
  207. struct l2tp_tunnel *tunnel = session->tunnel;
  208. int length = L2TP_SKB_CB(skb)->length;
  209. /* We're about to requeue the skb, so return resources
  210. * to its current owner (a socket receive buffer).
  211. */
  212. skb_orphan(skb);
  213. tunnel->stats.rx_packets++;
  214. tunnel->stats.rx_bytes += length;
  215. session->stats.rx_packets++;
  216. session->stats.rx_bytes += length;
  217. if (L2TP_SKB_CB(skb)->has_seq) {
  218. /* Bump our Nr */
  219. session->nr++;
  220. PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
  221. "%s: updated nr to %hu\n", session->name, session->nr);
  222. }
  223. /* call private receive handler */
  224. if (session->recv_skb != NULL)
  225. (*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
  226. else
  227. kfree_skb(skb);
  228. if (session->deref)
  229. (*session->deref)(session);
  230. }
  231. /* Dequeue skbs from the session's reorder_q, subject to packet order.
  232. * Skbs that have been in the queue for too long are simply discarded.
  233. */
  234. static void l2tp_recv_dequeue(struct l2tp_session *session)
  235. {
  236. struct sk_buff *skb;
  237. struct sk_buff *tmp;
  238. /* If the pkt at the head of the queue has the nr that we
  239. * expect to send up next, dequeue it and any other
  240. * in-sequence packets behind it.
  241. */
  242. spin_lock_bh(&session->reorder_q.lock);
  243. skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
  244. if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
  245. session->stats.rx_seq_discards++;
  246. session->stats.rx_errors++;
  247. PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
  248. "%s: oos pkt %hu len %d discarded (too old), "
  249. "waiting for %hu, reorder_q_len=%d\n",
  250. session->name, L2TP_SKB_CB(skb)->ns,
  251. L2TP_SKB_CB(skb)->length, session->nr,
  252. skb_queue_len(&session->reorder_q));
  253. __skb_unlink(skb, &session->reorder_q);
  254. kfree_skb(skb);
  255. if (session->deref)
  256. (*session->deref)(session);
  257. continue;
  258. }
  259. if (L2TP_SKB_CB(skb)->has_seq) {
  260. if (L2TP_SKB_CB(skb)->ns != session->nr) {
  261. PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
  262. "%s: holding oos pkt %hu len %d, "
  263. "waiting for %hu, reorder_q_len=%d\n",
  264. session->name, L2TP_SKB_CB(skb)->ns,
  265. L2TP_SKB_CB(skb)->length, session->nr,
  266. skb_queue_len(&session->reorder_q));
  267. goto out;
  268. }
  269. }
  270. __skb_unlink(skb, &session->reorder_q);
  271. /* Process the skb. We release the queue lock while we
  272. * do so to let other contexts process the queue.
  273. */
  274. spin_unlock_bh(&session->reorder_q.lock);
  275. l2tp_recv_dequeue_skb(session, skb);
  276. spin_lock_bh(&session->reorder_q.lock);
  277. }
  278. out:
  279. spin_unlock_bh(&session->reorder_q.lock);
  280. }
  281. static inline int l2tp_verify_udp_checksum(struct sock *sk,
  282. struct sk_buff *skb)
  283. {
  284. struct udphdr *uh = udp_hdr(skb);
  285. u16 ulen = ntohs(uh->len);
  286. struct inet_sock *inet;
  287. __wsum psum;
  288. if (sk->sk_no_check || skb_csum_unnecessary(skb) || !uh->check)
  289. return 0;
  290. inet = inet_sk(sk);
  291. psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr, ulen,
  292. IPPROTO_UDP, 0);
  293. if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
  294. !csum_fold(csum_add(psum, skb->csum)))
  295. return 0;
  296. skb->csum = psum;
  297. return __skb_checksum_complete(skb);
  298. }
  299. /* Internal UDP receive frame. Do the real work of receiving an L2TP data frame
  300. * here. The skb is not on a list when we get here.
  301. * Returns 0 if the packet was a data packet and was successfully passed on.
  302. * Returns 1 if the packet was not a good data packet and could not be
  303. * forwarded. All such packets are passed up to userspace to deal with.
  304. */
  305. int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
  306. int (*payload_hook)(struct sk_buff *skb))
  307. {
  308. struct l2tp_session *session = NULL;
  309. unsigned char *ptr, *optr;
  310. u16 hdrflags;
  311. u32 tunnel_id, session_id;
  312. int length;
  313. int offset;
  314. u16 version;
  315. u16 ns, nr;
  316. if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
  317. goto discard_bad_csum;
  318. /* UDP always verifies the packet length. */
  319. __skb_pull(skb, sizeof(struct udphdr));
  320. /* Short packet? */
  321. if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
  322. PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
  323. "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
  324. goto error;
  325. }
  326. /* Point to L2TP header */
  327. optr = ptr = skb->data;
  328. /* Trace packet contents, if enabled */
  329. if (tunnel->debug & L2TP_MSG_DATA) {
  330. length = min(32u, skb->len);
  331. if (!pskb_may_pull(skb, length))
  332. goto error;
  333. printk(KERN_DEBUG "%s: recv: ", tunnel->name);
  334. offset = 0;
  335. do {
  336. printk(" %02X", ptr[offset]);
  337. } while (++offset < length);
  338. printk("\n");
  339. }
  340. /* Get L2TP header flags */
  341. hdrflags = ntohs(*(__be16 *)ptr);
  342. /* Check protocol version */
  343. version = hdrflags & L2TP_HDR_VER_MASK;
  344. if (version != tunnel->version) {
  345. PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
  346. "%s: recv protocol version mismatch: got %d expected %d\n",
  347. tunnel->name, version, tunnel->version);
  348. goto error;
  349. }
  350. /* Get length of L2TP packet */
  351. length = skb->len;
  352. /* If type is control packet, it is handled by userspace. */
  353. if (hdrflags & L2TP_HDRFLAG_T) {
  354. PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
  355. "%s: recv control packet, len=%d\n", tunnel->name, length);
  356. goto error;
  357. }
  358. /* Skip flags */
  359. ptr += 2;
  360. /* If length is present, skip it */
  361. if (hdrflags & L2TP_HDRFLAG_L)
  362. ptr += 2;
  363. /* Extract tunnel and session ID */
  364. tunnel_id = ntohs(*(__be16 *) ptr);
  365. ptr += 2;
  366. session_id = ntohs(*(__be16 *) ptr);
  367. ptr += 2;
  368. /* Find the session context */
  369. session = l2tp_session_find(tunnel, session_id);
  370. if (!session) {
  371. /* Not found? Pass to userspace to deal with */
  372. PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
  373. "%s: no session found (%hu/%hu). Passing up.\n",
  374. tunnel->name, tunnel_id, session_id);
  375. goto error;
  376. }
  377. /* The ref count is increased since we now hold a pointer to
  378. * the session. Take care to decrement the refcnt when exiting
  379. * this function from now on...
  380. */
  381. l2tp_session_inc_refcount(session);
  382. if (session->ref)
  383. (*session->ref)(session);
  384. /* Handle the optional sequence numbers. Sequence numbers are
  385. * in different places for L2TPv2 and L2TPv3.
  386. *
  387. * If we are the LAC, enable/disable sequence numbers under
  388. * the control of the LNS. If no sequence numbers present but
  389. * we were expecting them, discard frame.
  390. */
  391. ns = nr = 0;
  392. L2TP_SKB_CB(skb)->has_seq = 0;
  393. if (hdrflags & L2TP_HDRFLAG_S) {
  394. ns = (u16) ntohs(*(__be16 *) ptr);
  395. ptr += 2;
  396. nr = ntohs(*(__be16 *) ptr);
  397. ptr += 2;
  398. /* Store L2TP info in the skb */
  399. L2TP_SKB_CB(skb)->ns = ns;
  400. L2TP_SKB_CB(skb)->has_seq = 1;
  401. PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
  402. "%s: recv data ns=%hu, nr=%hu, session nr=%hu\n",
  403. session->name, ns, nr, session->nr);
  404. }
  405. if (L2TP_SKB_CB(skb)->has_seq) {
  406. /* Received a packet with sequence numbers. If we're the LNS,
  407. * check if we sre sending sequence numbers and if not,
  408. * configure it so.
  409. */
  410. if ((!session->lns_mode) && (!session->send_seq)) {
  411. PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
  412. "%s: requested to enable seq numbers by LNS\n",
  413. session->name);
  414. session->send_seq = -1;
  415. }
  416. } else {
  417. /* No sequence numbers.
  418. * If user has configured mandatory sequence numbers, discard.
  419. */
  420. if (session->recv_seq) {
  421. PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
  422. "%s: recv data has no seq numbers when required. "
  423. "Discarding\n", session->name);
  424. session->stats.rx_seq_discards++;
  425. goto discard;
  426. }
  427. /* If we're the LAC and we're sending sequence numbers, the
  428. * LNS has requested that we no longer send sequence numbers.
  429. * If we're the LNS and we're sending sequence numbers, the
  430. * LAC is broken. Discard the frame.
  431. */
  432. if ((!session->lns_mode) && (session->send_seq)) {
  433. PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
  434. "%s: requested to disable seq numbers by LNS\n",
  435. session->name);
  436. session->send_seq = 0;
  437. } else if (session->send_seq) {
  438. PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
  439. "%s: recv data has no seq numbers when required. "
  440. "Discarding\n", session->name);
  441. session->stats.rx_seq_discards++;
  442. goto discard;
  443. }
  444. }
  445. /* If offset bit set, skip it. */
  446. if (hdrflags & L2TP_HDRFLAG_O) {
  447. offset = ntohs(*(__be16 *)ptr);
  448. ptr += 2 + offset;
  449. }
  450. offset = ptr - optr;
  451. if (!pskb_may_pull(skb, offset))
  452. goto discard;
  453. __skb_pull(skb, offset);
  454. /* If caller wants to process the payload before we queue the
  455. * packet, do so now.
  456. */
  457. if (payload_hook)
  458. if ((*payload_hook)(skb))
  459. goto discard;
  460. /* Prepare skb for adding to the session's reorder_q. Hold
  461. * packets for max reorder_timeout or 1 second if not
  462. * reordering.
  463. */
  464. L2TP_SKB_CB(skb)->length = length;
  465. L2TP_SKB_CB(skb)->expires = jiffies +
  466. (session->reorder_timeout ? session->reorder_timeout : HZ);
  467. /* Add packet to the session's receive queue. Reordering is done here, if
  468. * enabled. Saved L2TP protocol info is stored in skb->sb[].
  469. */
  470. if (L2TP_SKB_CB(skb)->has_seq) {
  471. if (session->reorder_timeout != 0) {
  472. /* Packet reordering enabled. Add skb to session's
  473. * reorder queue, in order of ns.
  474. */
  475. l2tp_recv_queue_skb(session, skb);
  476. } else {
  477. /* Packet reordering disabled. Discard out-of-sequence
  478. * packets
  479. */
  480. if (L2TP_SKB_CB(skb)->ns != session->nr) {
  481. session->stats.rx_seq_discards++;
  482. PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
  483. "%s: oos pkt %hu len %d discarded, "
  484. "waiting for %hu, reorder_q_len=%d\n",
  485. session->name, L2TP_SKB_CB(skb)->ns,
  486. L2TP_SKB_CB(skb)->length, session->nr,
  487. skb_queue_len(&session->reorder_q));
  488. goto discard;
  489. }
  490. skb_queue_tail(&session->reorder_q, skb);
  491. }
  492. } else {
  493. /* No sequence numbers. Add the skb to the tail of the
  494. * reorder queue. This ensures that it will be
  495. * delivered after all previous sequenced skbs.
  496. */
  497. skb_queue_tail(&session->reorder_q, skb);
  498. }
  499. /* Try to dequeue as many skbs from reorder_q as we can. */
  500. l2tp_recv_dequeue(session);
  501. l2tp_session_dec_refcount(session);
  502. return 0;
  503. discard:
  504. session->stats.rx_errors++;
  505. kfree_skb(skb);
  506. if (session->deref)
  507. (*session->deref)(session);
  508. l2tp_session_dec_refcount(session);
  509. return 0;
  510. discard_bad_csum:
  511. LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
  512. UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
  513. tunnel->stats.rx_errors++;
  514. kfree_skb(skb);
  515. return 0;
  516. error:
  517. /* Put UDP header back */
  518. __skb_push(skb, sizeof(struct udphdr));
  519. return 1;
  520. }
  521. EXPORT_SYMBOL_GPL(l2tp_udp_recv_core);
  522. /* UDP encapsulation receive handler. See net/ipv4/udp.c.
  523. * Return codes:
  524. * 0 : success.
  525. * <0: error
  526. * >0: skb should be passed up to userspace as UDP.
  527. */
  528. int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  529. {
  530. struct l2tp_tunnel *tunnel;
  531. tunnel = l2tp_sock_to_tunnel(sk);
  532. if (tunnel == NULL)
  533. goto pass_up;
  534. PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
  535. "%s: received %d bytes\n", tunnel->name, skb->len);
  536. if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
  537. goto pass_up_put;
  538. sock_put(sk);
  539. return 0;
  540. pass_up_put:
  541. sock_put(sk);
  542. pass_up:
  543. return 1;
  544. }
  545. EXPORT_SYMBOL_GPL(l2tp_udp_encap_recv);
  546. /************************************************************************
  547. * Transmit handling
  548. ***********************************************************************/
  549. /* Build an L2TP header for the session into the buffer provided.
  550. */
  551. static void l2tp_build_l2tpv2_header(struct l2tp_tunnel *tunnel,
  552. struct l2tp_session *session,
  553. void *buf)
  554. {
  555. __be16 *bufp = buf;
  556. u16 flags = L2TP_HDR_VER_2;
  557. u32 tunnel_id = tunnel->peer_tunnel_id;
  558. u32 session_id = session->peer_session_id;
  559. if (session->send_seq)
  560. flags |= L2TP_HDRFLAG_S;
  561. /* Setup L2TP header. */
  562. *bufp++ = htons(flags);
  563. *bufp++ = htons(tunnel_id);
  564. *bufp++ = htons(session_id);
  565. if (session->send_seq) {
  566. *bufp++ = htons(session->ns);
  567. *bufp++ = 0;
  568. session->ns++;
  569. PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
  570. "%s: updated ns to %hu\n", session->name, session->ns);
  571. }
  572. }
  573. void l2tp_build_l2tp_header(struct l2tp_session *session, void *buf)
  574. {
  575. struct l2tp_tunnel *tunnel = session->tunnel;
  576. BUG_ON(tunnel->version != L2TP_HDR_VER_2);
  577. l2tp_build_l2tpv2_header(tunnel, session, buf);
  578. }
  579. EXPORT_SYMBOL_GPL(l2tp_build_l2tp_header);
  580. int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, size_t data_len)
  581. {
  582. struct l2tp_tunnel *tunnel = session->tunnel;
  583. unsigned int len = skb->len;
  584. int error;
  585. /* Debug */
  586. if (session->send_seq)
  587. PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
  588. "%s: send %Zd bytes, ns=%hu\n", session->name,
  589. data_len, session->ns - 1);
  590. else
  591. PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
  592. "%s: send %Zd bytes\n", session->name, data_len);
  593. if (session->debug & L2TP_MSG_DATA) {
  594. int i;
  595. unsigned char *datap = skb->data + sizeof(struct udphdr);
  596. printk(KERN_DEBUG "%s: xmit:", session->name);
  597. for (i = 0; i < (len - sizeof(struct udphdr)); i++) {
  598. printk(" %02X", *datap++);
  599. if (i == 31) {
  600. printk(" ...");
  601. break;
  602. }
  603. }
  604. printk("\n");
  605. }
  606. /* Queue the packet to IP for output */
  607. error = ip_queue_xmit(skb, 1);
  608. /* Update stats */
  609. if (error >= 0) {
  610. tunnel->stats.tx_packets++;
  611. tunnel->stats.tx_bytes += len;
  612. session->stats.tx_packets++;
  613. session->stats.tx_bytes += len;
  614. } else {
  615. tunnel->stats.tx_errors++;
  616. session->stats.tx_errors++;
  617. }
  618. return 0;
  619. }
  620. EXPORT_SYMBOL_GPL(l2tp_xmit_core);
  621. /* Automatically called when the skb is freed.
  622. */
  623. static void l2tp_sock_wfree(struct sk_buff *skb)
  624. {
  625. sock_put(skb->sk);
  626. }
  627. /* For data skbs that we transmit, we associate with the tunnel socket
  628. * but don't do accounting.
  629. */
  630. static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
  631. {
  632. sock_hold(sk);
  633. skb->sk = sk;
  634. skb->destructor = l2tp_sock_wfree;
  635. }
  636. /* If caller requires the skb to have a ppp header, the header must be
  637. * inserted in the skb data before calling this function.
  638. */
  639. int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len)
  640. {
  641. int data_len = skb->len;
  642. struct sock *sk = session->tunnel->sock;
  643. struct udphdr *uh;
  644. unsigned int udp_len;
  645. struct inet_sock *inet;
  646. __wsum csum;
  647. int old_headroom;
  648. int new_headroom;
  649. int headroom;
  650. /* Check that there's enough headroom in the skb to insert IP,
  651. * UDP and L2TP headers. If not enough, expand it to
  652. * make room. Adjust truesize.
  653. */
  654. headroom = NET_SKB_PAD + sizeof(struct iphdr) +
  655. sizeof(struct udphdr) + hdr_len;
  656. old_headroom = skb_headroom(skb);
  657. if (skb_cow_head(skb, headroom))
  658. goto abort;
  659. new_headroom = skb_headroom(skb);
  660. skb_orphan(skb);
  661. skb->truesize += new_headroom - old_headroom;
  662. /* Setup L2TP header */
  663. l2tp_build_l2tp_header(session, __skb_push(skb, hdr_len));
  664. udp_len = sizeof(struct udphdr) + hdr_len + data_len;
  665. /* Setup UDP header */
  666. inet = inet_sk(sk);
  667. __skb_push(skb, sizeof(*uh));
  668. skb_reset_transport_header(skb);
  669. uh = udp_hdr(skb);
  670. uh->source = inet->inet_sport;
  671. uh->dest = inet->inet_dport;
  672. uh->len = htons(udp_len);
  673. uh->check = 0;
  674. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  675. IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
  676. IPSKB_REROUTED);
  677. nf_reset(skb);
  678. /* Get routing info from the tunnel socket */
  679. skb_dst_drop(skb);
  680. skb_dst_set(skb, dst_clone(__sk_dst_get(sk)));
  681. l2tp_skb_set_owner_w(skb, sk);
  682. /* Calculate UDP checksum if configured to do so */
  683. if (sk->sk_no_check == UDP_CSUM_NOXMIT)
  684. skb->ip_summed = CHECKSUM_NONE;
  685. else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
  686. (!(skb_dst(skb)->dev->features & NETIF_F_V4_CSUM))) {
  687. skb->ip_summed = CHECKSUM_COMPLETE;
  688. csum = skb_checksum(skb, 0, udp_len, 0);
  689. uh->check = csum_tcpudp_magic(inet->inet_saddr,
  690. inet->inet_daddr,
  691. udp_len, IPPROTO_UDP, csum);
  692. if (uh->check == 0)
  693. uh->check = CSUM_MANGLED_0;
  694. } else {
  695. skb->ip_summed = CHECKSUM_PARTIAL;
  696. skb->csum_start = skb_transport_header(skb) - skb->head;
  697. skb->csum_offset = offsetof(struct udphdr, check);
  698. uh->check = ~csum_tcpudp_magic(inet->inet_saddr,
  699. inet->inet_daddr,
  700. udp_len, IPPROTO_UDP, 0);
  701. }
  702. l2tp_xmit_core(session, skb, data_len);
  703. abort:
  704. return 0;
  705. }
  706. EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
  707. /*****************************************************************************
  708. * Tinnel and session create/destroy.
  709. *****************************************************************************/
  710. /* Tunnel socket destruct hook.
  711. * The tunnel context is deleted only when all session sockets have been
  712. * closed.
  713. */
  714. void l2tp_tunnel_destruct(struct sock *sk)
  715. {
  716. struct l2tp_tunnel *tunnel;
  717. tunnel = sk->sk_user_data;
  718. if (tunnel == NULL)
  719. goto end;
  720. PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
  721. "%s: closing...\n", tunnel->name);
  722. /* Close all sessions */
  723. l2tp_tunnel_closeall(tunnel);
  724. /* No longer an encapsulation socket. See net/ipv4/udp.c */
  725. (udp_sk(sk))->encap_type = 0;
  726. (udp_sk(sk))->encap_rcv = NULL;
  727. /* Remove hooks into tunnel socket */
  728. tunnel->sock = NULL;
  729. sk->sk_destruct = tunnel->old_sk_destruct;
  730. sk->sk_user_data = NULL;
  731. /* Call the original destructor */
  732. if (sk->sk_destruct)
  733. (*sk->sk_destruct)(sk);
  734. /* We're finished with the socket */
  735. l2tp_tunnel_dec_refcount(tunnel);
  736. end:
  737. return;
  738. }
  739. EXPORT_SYMBOL(l2tp_tunnel_destruct);
  740. /* When the tunnel is closed, all the attached sessions need to go too.
  741. */
  742. void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
  743. {
  744. int hash;
  745. struct hlist_node *walk;
  746. struct hlist_node *tmp;
  747. struct l2tp_session *session;
  748. BUG_ON(tunnel == NULL);
  749. PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
  750. "%s: closing all sessions...\n", tunnel->name);
  751. write_lock_bh(&tunnel->hlist_lock);
  752. for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
  753. again:
  754. hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
  755. session = hlist_entry(walk, struct l2tp_session, hlist);
  756. PRINTK(session->debug, L2TP_MSG_CONTROL, KERN_INFO,
  757. "%s: closing session\n", session->name);
  758. hlist_del_init(&session->hlist);
  759. /* Since we should hold the sock lock while
  760. * doing any unbinding, we need to release the
  761. * lock we're holding before taking that lock.
  762. * Hold a reference to the sock so it doesn't
  763. * disappear as we're jumping between locks.
  764. */
  765. if (session->ref != NULL)
  766. (*session->ref)(session);
  767. write_unlock_bh(&tunnel->hlist_lock);
  768. if (session->session_close != NULL)
  769. (*session->session_close)(session);
  770. if (session->deref != NULL)
  771. (*session->deref)(session);
  772. write_lock_bh(&tunnel->hlist_lock);
  773. /* Now restart from the beginning of this hash
  774. * chain. We always remove a session from the
  775. * list so we are guaranteed to make forward
  776. * progress.
  777. */
  778. goto again;
  779. }
  780. }
  781. write_unlock_bh(&tunnel->hlist_lock);
  782. }
  783. EXPORT_SYMBOL_GPL(l2tp_tunnel_closeall);
  784. /* Really kill the tunnel.
  785. * Come here only when all sessions have been cleared from the tunnel.
  786. */
  787. void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
  788. {
  789. struct l2tp_net *pn = l2tp_pernet(tunnel->l2tp_net);
  790. BUG_ON(atomic_read(&tunnel->ref_count) != 0);
  791. BUG_ON(tunnel->sock != NULL);
  792. PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
  793. "%s: free...\n", tunnel->name);
  794. /* Remove from tunnel list */
  795. write_lock_bh(&pn->l2tp_tunnel_list_lock);
  796. list_del_init(&tunnel->list);
  797. write_unlock_bh(&pn->l2tp_tunnel_list_lock);
  798. atomic_dec(&l2tp_tunnel_count);
  799. kfree(tunnel);
  800. }
  801. EXPORT_SYMBOL_GPL(l2tp_tunnel_free);
  802. 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)
  803. {
  804. struct l2tp_tunnel *tunnel = NULL;
  805. int err;
  806. struct socket *sock = NULL;
  807. struct sock *sk = NULL;
  808. struct l2tp_net *pn;
  809. /* Get the tunnel socket from the fd, which was opened by
  810. * the userspace L2TP daemon.
  811. */
  812. err = -EBADF;
  813. sock = sockfd_lookup(fd, &err);
  814. if (!sock) {
  815. printk(KERN_ERR "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
  816. tunnel_id, fd, err);
  817. goto err;
  818. }
  819. sk = sock->sk;
  820. /* Quick sanity checks */
  821. err = -EPROTONOSUPPORT;
  822. if (sk->sk_protocol != IPPROTO_UDP) {
  823. printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
  824. tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
  825. goto err;
  826. }
  827. err = -EAFNOSUPPORT;
  828. if (sock->ops->family != AF_INET) {
  829. printk(KERN_ERR "tunl %hu: fd %d wrong family, got %d, expected %d\n",
  830. tunnel_id, fd, sock->ops->family, AF_INET);
  831. goto err;
  832. }
  833. /* Check if this socket has already been prepped */
  834. tunnel = (struct l2tp_tunnel *)sk->sk_user_data;
  835. if (tunnel != NULL) {
  836. /* This socket has already been prepped */
  837. err = -EBUSY;
  838. goto err;
  839. }
  840. if (version != L2TP_HDR_VER_2)
  841. goto err;
  842. tunnel = kzalloc(sizeof(struct l2tp_tunnel), GFP_KERNEL);
  843. if (tunnel == NULL) {
  844. err = -ENOMEM;
  845. goto err;
  846. }
  847. tunnel->version = version;
  848. tunnel->tunnel_id = tunnel_id;
  849. tunnel->peer_tunnel_id = peer_tunnel_id;
  850. tunnel->debug = L2TP_DEFAULT_DEBUG_FLAGS;
  851. tunnel->magic = L2TP_TUNNEL_MAGIC;
  852. sprintf(&tunnel->name[0], "tunl %u", tunnel_id);
  853. rwlock_init(&tunnel->hlist_lock);
  854. /* The net we belong to */
  855. tunnel->l2tp_net = net;
  856. pn = l2tp_pernet(net);
  857. if (cfg)
  858. tunnel->debug = cfg->debug;
  859. /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
  860. udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
  861. udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
  862. sk->sk_user_data = tunnel;
  863. /* Hook on the tunnel socket destructor so that we can cleanup
  864. * if the tunnel socket goes away.
  865. */
  866. tunnel->old_sk_destruct = sk->sk_destruct;
  867. sk->sk_destruct = &l2tp_tunnel_destruct;
  868. tunnel->sock = sk;
  869. sk->sk_allocation = GFP_ATOMIC;
  870. /* Add tunnel to our list */
  871. INIT_LIST_HEAD(&tunnel->list);
  872. write_lock_bh(&pn->l2tp_tunnel_list_lock);
  873. list_add(&tunnel->list, &pn->l2tp_tunnel_list);
  874. write_unlock_bh(&pn->l2tp_tunnel_list_lock);
  875. atomic_inc(&l2tp_tunnel_count);
  876. /* Bump the reference count. The tunnel context is deleted
  877. * only when this drops to zero.
  878. */
  879. l2tp_tunnel_inc_refcount(tunnel);
  880. err = 0;
  881. err:
  882. if (tunnelp)
  883. *tunnelp = tunnel;
  884. if (sock)
  885. sockfd_put(sock);
  886. return err;
  887. }
  888. EXPORT_SYMBOL_GPL(l2tp_tunnel_create);
  889. /* Really kill the session.
  890. */
  891. void l2tp_session_free(struct l2tp_session *session)
  892. {
  893. struct l2tp_tunnel *tunnel;
  894. BUG_ON(atomic_read(&session->ref_count) != 0);
  895. tunnel = session->tunnel;
  896. if (tunnel != NULL) {
  897. BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
  898. /* Delete the session from the hash */
  899. write_lock_bh(&tunnel->hlist_lock);
  900. hlist_del_init(&session->hlist);
  901. write_unlock_bh(&tunnel->hlist_lock);
  902. if (session->session_id != 0)
  903. atomic_dec(&l2tp_session_count);
  904. sock_put(tunnel->sock);
  905. /* This will delete the tunnel context if this
  906. * is the last session on the tunnel.
  907. */
  908. session->tunnel = NULL;
  909. l2tp_tunnel_dec_refcount(tunnel);
  910. }
  911. kfree(session);
  912. return;
  913. }
  914. EXPORT_SYMBOL_GPL(l2tp_session_free);
  915. 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)
  916. {
  917. struct l2tp_session *session;
  918. session = kzalloc(sizeof(struct l2tp_session) + priv_size, GFP_KERNEL);
  919. if (session != NULL) {
  920. session->magic = L2TP_SESSION_MAGIC;
  921. session->tunnel = tunnel;
  922. session->session_id = session_id;
  923. session->peer_session_id = peer_session_id;
  924. sprintf(&session->name[0], "sess %u/%u",
  925. tunnel->tunnel_id, session->session_id);
  926. skb_queue_head_init(&session->reorder_q);
  927. INIT_HLIST_NODE(&session->hlist);
  928. /* Inherit debug options from tunnel */
  929. session->debug = tunnel->debug;
  930. if (cfg) {
  931. session->debug = cfg->debug;
  932. session->hdr_len = cfg->hdr_len;
  933. session->mtu = cfg->mtu;
  934. session->mru = cfg->mru;
  935. session->send_seq = cfg->send_seq;
  936. session->recv_seq = cfg->recv_seq;
  937. session->lns_mode = cfg->lns_mode;
  938. }
  939. /* Bump the reference count. The session context is deleted
  940. * only when this drops to zero.
  941. */
  942. l2tp_session_inc_refcount(session);
  943. l2tp_tunnel_inc_refcount(tunnel);
  944. /* Ensure tunnel socket isn't deleted */
  945. sock_hold(tunnel->sock);
  946. /* Add session to the tunnel's hash list */
  947. write_lock_bh(&tunnel->hlist_lock);
  948. hlist_add_head(&session->hlist,
  949. l2tp_session_id_hash(tunnel, session_id));
  950. write_unlock_bh(&tunnel->hlist_lock);
  951. /* Ignore management session in session count value */
  952. if (session->session_id != 0)
  953. atomic_inc(&l2tp_session_count);
  954. }
  955. return session;
  956. }
  957. EXPORT_SYMBOL_GPL(l2tp_session_create);
  958. /*****************************************************************************
  959. * Init and cleanup
  960. *****************************************************************************/
  961. static __net_init int l2tp_init_net(struct net *net)
  962. {
  963. struct l2tp_net *pn;
  964. int err;
  965. pn = kzalloc(sizeof(*pn), GFP_KERNEL);
  966. if (!pn)
  967. return -ENOMEM;
  968. INIT_LIST_HEAD(&pn->l2tp_tunnel_list);
  969. rwlock_init(&pn->l2tp_tunnel_list_lock);
  970. err = net_assign_generic(net, l2tp_net_id, pn);
  971. if (err)
  972. goto out;
  973. return 0;
  974. out:
  975. kfree(pn);
  976. return err;
  977. }
  978. static __net_exit void l2tp_exit_net(struct net *net)
  979. {
  980. struct l2tp_net *pn;
  981. pn = net_generic(net, l2tp_net_id);
  982. /*
  983. * if someone has cached our net then
  984. * further net_generic call will return NULL
  985. */
  986. net_assign_generic(net, l2tp_net_id, NULL);
  987. kfree(pn);
  988. }
  989. static struct pernet_operations l2tp_net_ops = {
  990. .init = l2tp_init_net,
  991. .exit = l2tp_exit_net,
  992. .id = &l2tp_net_id,
  993. .size = sizeof(struct l2tp_net),
  994. };
  995. static int __init l2tp_init(void)
  996. {
  997. int rc = 0;
  998. rc = register_pernet_device(&l2tp_net_ops);
  999. if (rc)
  1000. goto out;
  1001. printk(KERN_INFO "L2TP core driver, %s\n", L2TP_DRV_VERSION);
  1002. out:
  1003. return rc;
  1004. }
  1005. static void __exit l2tp_exit(void)
  1006. {
  1007. unregister_pernet_device(&l2tp_net_ops);
  1008. }
  1009. module_init(l2tp_init);
  1010. module_exit(l2tp_exit);
  1011. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  1012. MODULE_DESCRIPTION("L2TP core");
  1013. MODULE_LICENSE("GPL");
  1014. MODULE_VERSION(L2TP_DRV_VERSION);