l2tp_core.c 45 KB

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