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