l2tp_core.c 48 KB

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