l2tp_core.c 46 KB

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