l2tp_ppp.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  1. /*****************************************************************************
  2. * Linux PPP over L2TP (PPPoX/PPPoL2TP) Sockets
  3. *
  4. * PPPoX --- Generic PPP encapsulation socket family
  5. * PPPoL2TP --- PPP over L2TP (RFC 2661)
  6. *
  7. * Version: 2.0.0
  8. *
  9. * Authors: James Chapman (jchapman@katalix.com)
  10. *
  11. * Based on original work by Martijn van Oosterhout <kleptog@svana.org>
  12. *
  13. * License:
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. */
  20. /* This driver handles only L2TP data frames; control frames are handled by a
  21. * userspace application.
  22. *
  23. * To send data in an L2TP session, userspace opens a PPPoL2TP socket and
  24. * attaches it to a bound UDP socket with local tunnel_id / session_id and
  25. * peer tunnel_id / session_id set. Data can then be sent or received using
  26. * regular socket sendmsg() / recvmsg() calls. Kernel parameters of the socket
  27. * can be read or modified using ioctl() or [gs]etsockopt() calls.
  28. *
  29. * When a PPPoL2TP socket is connected with local and peer session_id values
  30. * zero, the socket is treated as a special tunnel management socket.
  31. *
  32. * Here's example userspace code to create a socket for sending/receiving data
  33. * over an L2TP session:-
  34. *
  35. * struct sockaddr_pppol2tp sax;
  36. * int fd;
  37. * int session_fd;
  38. *
  39. * fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
  40. *
  41. * sax.sa_family = AF_PPPOX;
  42. * sax.sa_protocol = PX_PROTO_OL2TP;
  43. * sax.pppol2tp.fd = tunnel_fd; // bound UDP socket
  44. * sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr;
  45. * sax.pppol2tp.addr.sin_port = addr->sin_port;
  46. * sax.pppol2tp.addr.sin_family = AF_INET;
  47. * sax.pppol2tp.s_tunnel = tunnel_id;
  48. * sax.pppol2tp.s_session = session_id;
  49. * sax.pppol2tp.d_tunnel = peer_tunnel_id;
  50. * sax.pppol2tp.d_session = peer_session_id;
  51. *
  52. * session_fd = connect(fd, (struct sockaddr *)&sax, sizeof(sax));
  53. *
  54. * A pppd plugin that allows PPP traffic to be carried over L2TP using
  55. * this driver is available from the OpenL2TP project at
  56. * http://openl2tp.sourceforge.net.
  57. */
  58. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  59. #include <linux/module.h>
  60. #include <linux/string.h>
  61. #include <linux/list.h>
  62. #include <linux/uaccess.h>
  63. #include <linux/kernel.h>
  64. #include <linux/spinlock.h>
  65. #include <linux/kthread.h>
  66. #include <linux/sched.h>
  67. #include <linux/slab.h>
  68. #include <linux/errno.h>
  69. #include <linux/jiffies.h>
  70. #include <linux/netdevice.h>
  71. #include <linux/net.h>
  72. #include <linux/inetdevice.h>
  73. #include <linux/skbuff.h>
  74. #include <linux/init.h>
  75. #include <linux/ip.h>
  76. #include <linux/udp.h>
  77. #include <linux/if_pppox.h>
  78. #include <linux/if_pppol2tp.h>
  79. #include <net/sock.h>
  80. #include <linux/ppp_channel.h>
  81. #include <linux/ppp_defs.h>
  82. #include <linux/ppp-ioctl.h>
  83. #include <linux/file.h>
  84. #include <linux/hash.h>
  85. #include <linux/sort.h>
  86. #include <linux/proc_fs.h>
  87. #include <linux/l2tp.h>
  88. #include <linux/nsproxy.h>
  89. #include <net/net_namespace.h>
  90. #include <net/netns/generic.h>
  91. #include <net/dst.h>
  92. #include <net/ip.h>
  93. #include <net/udp.h>
  94. #include <net/xfrm.h>
  95. #include <net/inet_common.h>
  96. #include <asm/byteorder.h>
  97. #include <linux/atomic.h>
  98. #include "l2tp_core.h"
  99. #define PPPOL2TP_DRV_VERSION "V2.0"
  100. /* Space for UDP, L2TP and PPP headers */
  101. #define PPPOL2TP_HEADER_OVERHEAD 40
  102. /* Number of bytes to build transmit L2TP headers.
  103. * Unfortunately the size is different depending on whether sequence numbers
  104. * are enabled.
  105. */
  106. #define PPPOL2TP_L2TP_HDR_SIZE_SEQ 10
  107. #define PPPOL2TP_L2TP_HDR_SIZE_NOSEQ 6
  108. /* Private data of each session. This data lives at the end of struct
  109. * l2tp_session, referenced via session->priv[].
  110. */
  111. struct pppol2tp_session {
  112. int owner; /* pid that opened the socket */
  113. struct sock *sock; /* Pointer to the session
  114. * PPPoX socket */
  115. struct sock *tunnel_sock; /* Pointer to the tunnel UDP
  116. * socket */
  117. int flags; /* accessed by PPPIOCGFLAGS.
  118. * Unused. */
  119. };
  120. static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb);
  121. static const struct ppp_channel_ops pppol2tp_chan_ops = {
  122. .start_xmit = pppol2tp_xmit,
  123. };
  124. static const struct proto_ops pppol2tp_ops;
  125. /* Helpers to obtain tunnel/session contexts from sockets.
  126. */
  127. static inline struct l2tp_session *pppol2tp_sock_to_session(struct sock *sk)
  128. {
  129. struct l2tp_session *session;
  130. if (sk == NULL)
  131. return NULL;
  132. sock_hold(sk);
  133. session = (struct l2tp_session *)(sk->sk_user_data);
  134. if (session == NULL) {
  135. sock_put(sk);
  136. goto out;
  137. }
  138. BUG_ON(session->magic != L2TP_SESSION_MAGIC);
  139. out:
  140. return session;
  141. }
  142. /*****************************************************************************
  143. * Receive data handling
  144. *****************************************************************************/
  145. static int pppol2tp_recv_payload_hook(struct sk_buff *skb)
  146. {
  147. /* Skip PPP header, if present. In testing, Microsoft L2TP clients
  148. * don't send the PPP header (PPP header compression enabled), but
  149. * other clients can include the header. So we cope with both cases
  150. * here. The PPP header is always FF03 when using L2TP.
  151. *
  152. * Note that skb->data[] isn't dereferenced from a u16 ptr here since
  153. * the field may be unaligned.
  154. */
  155. if (!pskb_may_pull(skb, 2))
  156. return 1;
  157. if ((skb->data[0] == 0xff) && (skb->data[1] == 0x03))
  158. skb_pull(skb, 2);
  159. return 0;
  160. }
  161. /* Receive message. This is the recvmsg for the PPPoL2TP socket.
  162. */
  163. static int pppol2tp_recvmsg(struct socket *sock, struct msghdr *msg,
  164. size_t len, int flags)
  165. {
  166. int err;
  167. struct sk_buff *skb;
  168. struct sock *sk = sock->sk;
  169. err = -EIO;
  170. if (sk->sk_state & PPPOX_BOUND)
  171. goto end;
  172. err = 0;
  173. skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
  174. flags & MSG_DONTWAIT, &err);
  175. if (!skb)
  176. goto end;
  177. if (len > skb->len)
  178. len = skb->len;
  179. else if (len < skb->len)
  180. msg->msg_flags |= MSG_TRUNC;
  181. err = skb_copy_datagram_msg(skb, 0, msg, len);
  182. if (likely(err == 0))
  183. err = len;
  184. kfree_skb(skb);
  185. end:
  186. return err;
  187. }
  188. static void pppol2tp_recv(struct l2tp_session *session, struct sk_buff *skb, int data_len)
  189. {
  190. struct pppol2tp_session *ps = l2tp_session_priv(session);
  191. struct sock *sk = NULL;
  192. /* If the socket is bound, send it in to PPP's input queue. Otherwise
  193. * queue it on the session socket.
  194. */
  195. sk = ps->sock;
  196. if (sk == NULL)
  197. goto no_sock;
  198. if (sk->sk_state & PPPOX_BOUND) {
  199. struct pppox_sock *po;
  200. l2tp_dbg(session, PPPOL2TP_MSG_DATA,
  201. "%s: recv %d byte data frame, passing to ppp\n",
  202. session->name, data_len);
  203. /* We need to forget all info related to the L2TP packet
  204. * gathered in the skb as we are going to reuse the same
  205. * skb for the inner packet.
  206. * Namely we need to:
  207. * - reset xfrm (IPSec) information as it applies to
  208. * the outer L2TP packet and not to the inner one
  209. * - release the dst to force a route lookup on the inner
  210. * IP packet since skb->dst currently points to the dst
  211. * of the UDP tunnel
  212. * - reset netfilter information as it doesn't apply
  213. * to the inner packet either
  214. */
  215. secpath_reset(skb);
  216. skb_dst_drop(skb);
  217. nf_reset(skb);
  218. po = pppox_sk(sk);
  219. ppp_input(&po->chan, skb);
  220. } else {
  221. l2tp_dbg(session, PPPOL2TP_MSG_DATA,
  222. "%s: recv %d byte data frame, passing to L2TP socket\n",
  223. session->name, data_len);
  224. if (sock_queue_rcv_skb(sk, skb) < 0) {
  225. atomic_long_inc(&session->stats.rx_errors);
  226. kfree_skb(skb);
  227. }
  228. }
  229. return;
  230. no_sock:
  231. l2tp_info(session, PPPOL2TP_MSG_DATA, "%s: no socket\n", session->name);
  232. kfree_skb(skb);
  233. }
  234. static void pppol2tp_session_sock_hold(struct l2tp_session *session)
  235. {
  236. struct pppol2tp_session *ps = l2tp_session_priv(session);
  237. if (ps->sock)
  238. sock_hold(ps->sock);
  239. }
  240. static void pppol2tp_session_sock_put(struct l2tp_session *session)
  241. {
  242. struct pppol2tp_session *ps = l2tp_session_priv(session);
  243. if (ps->sock)
  244. sock_put(ps->sock);
  245. }
  246. /************************************************************************
  247. * Transmit handling
  248. ***********************************************************************/
  249. /* This is the sendmsg for the PPPoL2TP pppol2tp_session socket. We come here
  250. * when a user application does a sendmsg() on the session socket. L2TP and
  251. * PPP headers must be inserted into the user's data.
  252. */
  253. static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m,
  254. size_t total_len)
  255. {
  256. static const unsigned char ppph[2] = { 0xff, 0x03 };
  257. struct sock *sk = sock->sk;
  258. struct sk_buff *skb;
  259. int error;
  260. struct l2tp_session *session;
  261. struct l2tp_tunnel *tunnel;
  262. struct pppol2tp_session *ps;
  263. int uhlen;
  264. error = -ENOTCONN;
  265. if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
  266. goto error;
  267. /* Get session and tunnel contexts */
  268. error = -EBADF;
  269. session = pppol2tp_sock_to_session(sk);
  270. if (session == NULL)
  271. goto error;
  272. ps = l2tp_session_priv(session);
  273. tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
  274. if (tunnel == NULL)
  275. goto error_put_sess;
  276. uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
  277. /* Allocate a socket buffer */
  278. error = -ENOMEM;
  279. skb = sock_wmalloc(sk, NET_SKB_PAD + sizeof(struct iphdr) +
  280. uhlen + session->hdr_len +
  281. sizeof(ppph) + total_len,
  282. 0, GFP_KERNEL);
  283. if (!skb)
  284. goto error_put_sess_tun;
  285. /* Reserve space for headers. */
  286. skb_reserve(skb, NET_SKB_PAD);
  287. skb_reset_network_header(skb);
  288. skb_reserve(skb, sizeof(struct iphdr));
  289. skb_reset_transport_header(skb);
  290. skb_reserve(skb, uhlen);
  291. /* Add PPP header */
  292. skb->data[0] = ppph[0];
  293. skb->data[1] = ppph[1];
  294. skb_put(skb, 2);
  295. /* Copy user data into skb */
  296. error = memcpy_from_msg(skb_put(skb, total_len), m, total_len);
  297. if (error < 0) {
  298. kfree_skb(skb);
  299. goto error_put_sess_tun;
  300. }
  301. local_bh_disable();
  302. l2tp_xmit_skb(session, skb, session->hdr_len);
  303. local_bh_enable();
  304. sock_put(ps->tunnel_sock);
  305. sock_put(sk);
  306. return total_len;
  307. error_put_sess_tun:
  308. sock_put(ps->tunnel_sock);
  309. error_put_sess:
  310. sock_put(sk);
  311. error:
  312. return error;
  313. }
  314. /* Transmit function called by generic PPP driver. Sends PPP frame
  315. * over PPPoL2TP socket.
  316. *
  317. * This is almost the same as pppol2tp_sendmsg(), but rather than
  318. * being called with a msghdr from userspace, it is called with a skb
  319. * from the kernel.
  320. *
  321. * The supplied skb from ppp doesn't have enough headroom for the
  322. * insertion of L2TP, UDP and IP headers so we need to allocate more
  323. * headroom in the skb. This will create a cloned skb. But we must be
  324. * careful in the error case because the caller will expect to free
  325. * the skb it supplied, not our cloned skb. So we take care to always
  326. * leave the original skb unfreed if we return an error.
  327. */
  328. static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
  329. {
  330. static const u8 ppph[2] = { 0xff, 0x03 };
  331. struct sock *sk = (struct sock *) chan->private;
  332. struct sock *sk_tun;
  333. struct l2tp_session *session;
  334. struct l2tp_tunnel *tunnel;
  335. struct pppol2tp_session *ps;
  336. int uhlen, headroom;
  337. if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
  338. goto abort;
  339. /* Get session and tunnel contexts from the socket */
  340. session = pppol2tp_sock_to_session(sk);
  341. if (session == NULL)
  342. goto abort;
  343. ps = l2tp_session_priv(session);
  344. sk_tun = ps->tunnel_sock;
  345. if (sk_tun == NULL)
  346. goto abort_put_sess;
  347. tunnel = l2tp_sock_to_tunnel(sk_tun);
  348. if (tunnel == NULL)
  349. goto abort_put_sess;
  350. uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
  351. headroom = NET_SKB_PAD +
  352. sizeof(struct iphdr) + /* IP header */
  353. uhlen + /* UDP header (if L2TP_ENCAPTYPE_UDP) */
  354. session->hdr_len + /* L2TP header */
  355. sizeof(ppph); /* PPP header */
  356. if (skb_cow_head(skb, headroom))
  357. goto abort_put_sess_tun;
  358. /* Setup PPP header */
  359. __skb_push(skb, sizeof(ppph));
  360. skb->data[0] = ppph[0];
  361. skb->data[1] = ppph[1];
  362. local_bh_disable();
  363. l2tp_xmit_skb(session, skb, session->hdr_len);
  364. local_bh_enable();
  365. sock_put(sk_tun);
  366. sock_put(sk);
  367. return 1;
  368. abort_put_sess_tun:
  369. sock_put(sk_tun);
  370. abort_put_sess:
  371. sock_put(sk);
  372. abort:
  373. /* Free the original skb */
  374. kfree_skb(skb);
  375. return 1;
  376. }
  377. /*****************************************************************************
  378. * Session (and tunnel control) socket create/destroy.
  379. *****************************************************************************/
  380. /* Called by l2tp_core when a session socket is being closed.
  381. */
  382. static void pppol2tp_session_close(struct l2tp_session *session)
  383. {
  384. struct pppol2tp_session *ps = l2tp_session_priv(session);
  385. struct sock *sk = ps->sock;
  386. struct socket *sock = sk->sk_socket;
  387. BUG_ON(session->magic != L2TP_SESSION_MAGIC);
  388. if (sock) {
  389. inet_shutdown(sock, 2);
  390. /* Don't let the session go away before our socket does */
  391. l2tp_session_inc_refcount(session);
  392. }
  393. }
  394. /* Really kill the session socket. (Called from sock_put() if
  395. * refcnt == 0.)
  396. */
  397. static void pppol2tp_session_destruct(struct sock *sk)
  398. {
  399. struct l2tp_session *session = sk->sk_user_data;
  400. if (session) {
  401. sk->sk_user_data = NULL;
  402. BUG_ON(session->magic != L2TP_SESSION_MAGIC);
  403. l2tp_session_dec_refcount(session);
  404. }
  405. }
  406. /* Called when the PPPoX socket (session) is closed.
  407. */
  408. static int pppol2tp_release(struct socket *sock)
  409. {
  410. struct sock *sk = sock->sk;
  411. struct l2tp_session *session;
  412. int error;
  413. if (!sk)
  414. return 0;
  415. error = -EBADF;
  416. lock_sock(sk);
  417. if (sock_flag(sk, SOCK_DEAD) != 0)
  418. goto error;
  419. pppox_unbind_sock(sk);
  420. /* Signal the death of the socket. */
  421. sk->sk_state = PPPOX_DEAD;
  422. sock_orphan(sk);
  423. sock->sk = NULL;
  424. session = pppol2tp_sock_to_session(sk);
  425. /* Purge any queued data */
  426. if (session != NULL) {
  427. __l2tp_session_unhash(session);
  428. l2tp_session_queue_purge(session);
  429. sock_put(sk);
  430. }
  431. skb_queue_purge(&sk->sk_receive_queue);
  432. skb_queue_purge(&sk->sk_write_queue);
  433. release_sock(sk);
  434. /* This will delete the session context via
  435. * pppol2tp_session_destruct() if the socket's refcnt drops to
  436. * zero.
  437. */
  438. sock_put(sk);
  439. return 0;
  440. error:
  441. release_sock(sk);
  442. return error;
  443. }
  444. static struct proto pppol2tp_sk_proto = {
  445. .name = "PPPOL2TP",
  446. .owner = THIS_MODULE,
  447. .obj_size = sizeof(struct pppox_sock),
  448. };
  449. static int pppol2tp_backlog_recv(struct sock *sk, struct sk_buff *skb)
  450. {
  451. int rc;
  452. rc = l2tp_udp_encap_recv(sk, skb);
  453. if (rc)
  454. kfree_skb(skb);
  455. return NET_RX_SUCCESS;
  456. }
  457. /* socket() handler. Initialize a new struct sock.
  458. */
  459. static int pppol2tp_create(struct net *net, struct socket *sock, int kern)
  460. {
  461. int error = -ENOMEM;
  462. struct sock *sk;
  463. sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppol2tp_sk_proto, kern);
  464. if (!sk)
  465. goto out;
  466. sock_init_data(sock, sk);
  467. sock->state = SS_UNCONNECTED;
  468. sock->ops = &pppol2tp_ops;
  469. sk->sk_backlog_rcv = pppol2tp_backlog_recv;
  470. sk->sk_protocol = PX_PROTO_OL2TP;
  471. sk->sk_family = PF_PPPOX;
  472. sk->sk_state = PPPOX_NONE;
  473. sk->sk_type = SOCK_STREAM;
  474. sk->sk_destruct = pppol2tp_session_destruct;
  475. error = 0;
  476. out:
  477. return error;
  478. }
  479. #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
  480. static void pppol2tp_show(struct seq_file *m, void *arg)
  481. {
  482. struct l2tp_session *session = arg;
  483. struct pppol2tp_session *ps = l2tp_session_priv(session);
  484. if (ps) {
  485. struct pppox_sock *po = pppox_sk(ps->sock);
  486. if (po)
  487. seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
  488. }
  489. }
  490. #endif
  491. /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
  492. */
  493. static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
  494. int sockaddr_len, int flags)
  495. {
  496. struct sock *sk = sock->sk;
  497. struct sockaddr_pppol2tp *sp = (struct sockaddr_pppol2tp *) uservaddr;
  498. struct pppox_sock *po = pppox_sk(sk);
  499. struct l2tp_session *session = NULL;
  500. struct l2tp_tunnel *tunnel;
  501. struct pppol2tp_session *ps;
  502. struct dst_entry *dst;
  503. struct l2tp_session_cfg cfg = { 0, };
  504. int error = 0;
  505. u32 tunnel_id, peer_tunnel_id;
  506. u32 session_id, peer_session_id;
  507. int ver = 2;
  508. int fd;
  509. lock_sock(sk);
  510. error = -EINVAL;
  511. if (sp->sa_protocol != PX_PROTO_OL2TP)
  512. goto end;
  513. /* Check for already bound sockets */
  514. error = -EBUSY;
  515. if (sk->sk_state & PPPOX_CONNECTED)
  516. goto end;
  517. /* We don't supporting rebinding anyway */
  518. error = -EALREADY;
  519. if (sk->sk_user_data)
  520. goto end; /* socket is already attached */
  521. /* Get params from socket address. Handle L2TPv2 and L2TPv3.
  522. * This is nasty because there are different sockaddr_pppol2tp
  523. * structs for L2TPv2, L2TPv3, over IPv4 and IPv6. We use
  524. * the sockaddr size to determine which structure the caller
  525. * is using.
  526. */
  527. peer_tunnel_id = 0;
  528. if (sockaddr_len == sizeof(struct sockaddr_pppol2tp)) {
  529. fd = sp->pppol2tp.fd;
  530. tunnel_id = sp->pppol2tp.s_tunnel;
  531. peer_tunnel_id = sp->pppol2tp.d_tunnel;
  532. session_id = sp->pppol2tp.s_session;
  533. peer_session_id = sp->pppol2tp.d_session;
  534. } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3)) {
  535. struct sockaddr_pppol2tpv3 *sp3 =
  536. (struct sockaddr_pppol2tpv3 *) sp;
  537. ver = 3;
  538. fd = sp3->pppol2tp.fd;
  539. tunnel_id = sp3->pppol2tp.s_tunnel;
  540. peer_tunnel_id = sp3->pppol2tp.d_tunnel;
  541. session_id = sp3->pppol2tp.s_session;
  542. peer_session_id = sp3->pppol2tp.d_session;
  543. } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpin6)) {
  544. struct sockaddr_pppol2tpin6 *sp6 =
  545. (struct sockaddr_pppol2tpin6 *) sp;
  546. fd = sp6->pppol2tp.fd;
  547. tunnel_id = sp6->pppol2tp.s_tunnel;
  548. peer_tunnel_id = sp6->pppol2tp.d_tunnel;
  549. session_id = sp6->pppol2tp.s_session;
  550. peer_session_id = sp6->pppol2tp.d_session;
  551. } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3in6)) {
  552. struct sockaddr_pppol2tpv3in6 *sp6 =
  553. (struct sockaddr_pppol2tpv3in6 *) sp;
  554. ver = 3;
  555. fd = sp6->pppol2tp.fd;
  556. tunnel_id = sp6->pppol2tp.s_tunnel;
  557. peer_tunnel_id = sp6->pppol2tp.d_tunnel;
  558. session_id = sp6->pppol2tp.s_session;
  559. peer_session_id = sp6->pppol2tp.d_session;
  560. } else {
  561. error = -EINVAL;
  562. goto end; /* bad socket address */
  563. }
  564. /* Don't bind if tunnel_id is 0 */
  565. error = -EINVAL;
  566. if (tunnel_id == 0)
  567. goto end;
  568. tunnel = l2tp_tunnel_find(sock_net(sk), tunnel_id);
  569. /* Special case: create tunnel context if session_id and
  570. * peer_session_id is 0. Otherwise look up tunnel using supplied
  571. * tunnel id.
  572. */
  573. if ((session_id == 0) && (peer_session_id == 0)) {
  574. if (tunnel == NULL) {
  575. struct l2tp_tunnel_cfg tcfg = {
  576. .encap = L2TP_ENCAPTYPE_UDP,
  577. .debug = 0,
  578. };
  579. error = l2tp_tunnel_create(sock_net(sk), fd, ver, tunnel_id, peer_tunnel_id, &tcfg, &tunnel);
  580. if (error < 0)
  581. goto end;
  582. }
  583. } else {
  584. /* Error if we can't find the tunnel */
  585. error = -ENOENT;
  586. if (tunnel == NULL)
  587. goto end;
  588. /* Error if socket is not prepped */
  589. if (tunnel->sock == NULL)
  590. goto end;
  591. }
  592. if (tunnel->recv_payload_hook == NULL)
  593. tunnel->recv_payload_hook = pppol2tp_recv_payload_hook;
  594. if (tunnel->peer_tunnel_id == 0)
  595. tunnel->peer_tunnel_id = peer_tunnel_id;
  596. /* Create session if it doesn't already exist. We handle the
  597. * case where a session was previously created by the netlink
  598. * interface by checking that the session doesn't already have
  599. * a socket and its tunnel socket are what we expect. If any
  600. * of those checks fail, return EEXIST to the caller.
  601. */
  602. session = l2tp_session_find(sock_net(sk), tunnel, session_id);
  603. if (session == NULL) {
  604. /* Default MTU must allow space for UDP/L2TP/PPP
  605. * headers.
  606. */
  607. cfg.mtu = cfg.mru = 1500 - PPPOL2TP_HEADER_OVERHEAD;
  608. /* Allocate and initialize a new session context. */
  609. session = l2tp_session_create(sizeof(struct pppol2tp_session),
  610. tunnel, session_id,
  611. peer_session_id, &cfg);
  612. if (session == NULL) {
  613. error = -ENOMEM;
  614. goto end;
  615. }
  616. } else {
  617. ps = l2tp_session_priv(session);
  618. error = -EEXIST;
  619. if (ps->sock != NULL)
  620. goto end;
  621. /* consistency checks */
  622. if (ps->tunnel_sock != tunnel->sock)
  623. goto end;
  624. }
  625. /* Associate session with its PPPoL2TP socket */
  626. ps = l2tp_session_priv(session);
  627. ps->owner = current->pid;
  628. ps->sock = sk;
  629. ps->tunnel_sock = tunnel->sock;
  630. session->recv_skb = pppol2tp_recv;
  631. session->session_close = pppol2tp_session_close;
  632. #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
  633. session->show = pppol2tp_show;
  634. #endif
  635. /* We need to know each time a skb is dropped from the reorder
  636. * queue.
  637. */
  638. session->ref = pppol2tp_session_sock_hold;
  639. session->deref = pppol2tp_session_sock_put;
  640. /* If PMTU discovery was enabled, use the MTU that was discovered */
  641. dst = sk_dst_get(tunnel->sock);
  642. if (dst != NULL) {
  643. u32 pmtu = dst_mtu(dst);
  644. if (pmtu != 0)
  645. session->mtu = session->mru = pmtu -
  646. PPPOL2TP_HEADER_OVERHEAD;
  647. dst_release(dst);
  648. }
  649. /* Special case: if source & dest session_id == 0x0000, this
  650. * socket is being created to manage the tunnel. Just set up
  651. * the internal context for use by ioctl() and sockopt()
  652. * handlers.
  653. */
  654. if ((session->session_id == 0) &&
  655. (session->peer_session_id == 0)) {
  656. error = 0;
  657. goto out_no_ppp;
  658. }
  659. /* The only header we need to worry about is the L2TP
  660. * header. This size is different depending on whether
  661. * sequence numbers are enabled for the data channel.
  662. */
  663. po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
  664. po->chan.private = sk;
  665. po->chan.ops = &pppol2tp_chan_ops;
  666. po->chan.mtu = session->mtu;
  667. error = ppp_register_net_channel(sock_net(sk), &po->chan);
  668. if (error)
  669. goto end;
  670. out_no_ppp:
  671. /* This is how we get the session context from the socket. */
  672. sk->sk_user_data = session;
  673. sk->sk_state = PPPOX_CONNECTED;
  674. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: created\n",
  675. session->name);
  676. end:
  677. release_sock(sk);
  678. return error;
  679. }
  680. #ifdef CONFIG_L2TP_V3
  681. /* Called when creating sessions via the netlink interface.
  682. */
  683. static int pppol2tp_session_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
  684. {
  685. int error;
  686. struct l2tp_tunnel *tunnel;
  687. struct l2tp_session *session;
  688. struct pppol2tp_session *ps;
  689. tunnel = l2tp_tunnel_find(net, tunnel_id);
  690. /* Error if we can't find the tunnel */
  691. error = -ENOENT;
  692. if (tunnel == NULL)
  693. goto out;
  694. /* Error if tunnel socket is not prepped */
  695. if (tunnel->sock == NULL)
  696. goto out;
  697. /* Check that this session doesn't already exist */
  698. error = -EEXIST;
  699. session = l2tp_session_find(net, tunnel, session_id);
  700. if (session != NULL)
  701. goto out;
  702. /* Default MTU values. */
  703. if (cfg->mtu == 0)
  704. cfg->mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
  705. if (cfg->mru == 0)
  706. cfg->mru = cfg->mtu;
  707. /* Allocate and initialize a new session context. */
  708. error = -ENOMEM;
  709. session = l2tp_session_create(sizeof(struct pppol2tp_session),
  710. tunnel, session_id,
  711. peer_session_id, cfg);
  712. if (session == NULL)
  713. goto out;
  714. ps = l2tp_session_priv(session);
  715. ps->tunnel_sock = tunnel->sock;
  716. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: created\n",
  717. session->name);
  718. error = 0;
  719. out:
  720. return error;
  721. }
  722. #endif /* CONFIG_L2TP_V3 */
  723. /* getname() support.
  724. */
  725. static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
  726. int *usockaddr_len, int peer)
  727. {
  728. int len = 0;
  729. int error = 0;
  730. struct l2tp_session *session;
  731. struct l2tp_tunnel *tunnel;
  732. struct sock *sk = sock->sk;
  733. struct inet_sock *inet;
  734. struct pppol2tp_session *pls;
  735. error = -ENOTCONN;
  736. if (sk == NULL)
  737. goto end;
  738. if (sk->sk_state != PPPOX_CONNECTED)
  739. goto end;
  740. error = -EBADF;
  741. session = pppol2tp_sock_to_session(sk);
  742. if (session == NULL)
  743. goto end;
  744. pls = l2tp_session_priv(session);
  745. tunnel = l2tp_sock_to_tunnel(pls->tunnel_sock);
  746. if (tunnel == NULL) {
  747. error = -EBADF;
  748. goto end_put_sess;
  749. }
  750. inet = inet_sk(tunnel->sock);
  751. if ((tunnel->version == 2) && (tunnel->sock->sk_family == AF_INET)) {
  752. struct sockaddr_pppol2tp sp;
  753. len = sizeof(sp);
  754. memset(&sp, 0, len);
  755. sp.sa_family = AF_PPPOX;
  756. sp.sa_protocol = PX_PROTO_OL2TP;
  757. sp.pppol2tp.fd = tunnel->fd;
  758. sp.pppol2tp.pid = pls->owner;
  759. sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
  760. sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
  761. sp.pppol2tp.s_session = session->session_id;
  762. sp.pppol2tp.d_session = session->peer_session_id;
  763. sp.pppol2tp.addr.sin_family = AF_INET;
  764. sp.pppol2tp.addr.sin_port = inet->inet_dport;
  765. sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
  766. memcpy(uaddr, &sp, len);
  767. #if IS_ENABLED(CONFIG_IPV6)
  768. } else if ((tunnel->version == 2) &&
  769. (tunnel->sock->sk_family == AF_INET6)) {
  770. struct sockaddr_pppol2tpin6 sp;
  771. len = sizeof(sp);
  772. memset(&sp, 0, len);
  773. sp.sa_family = AF_PPPOX;
  774. sp.sa_protocol = PX_PROTO_OL2TP;
  775. sp.pppol2tp.fd = tunnel->fd;
  776. sp.pppol2tp.pid = pls->owner;
  777. sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
  778. sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
  779. sp.pppol2tp.s_session = session->session_id;
  780. sp.pppol2tp.d_session = session->peer_session_id;
  781. sp.pppol2tp.addr.sin6_family = AF_INET6;
  782. sp.pppol2tp.addr.sin6_port = inet->inet_dport;
  783. memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
  784. sizeof(tunnel->sock->sk_v6_daddr));
  785. memcpy(uaddr, &sp, len);
  786. } else if ((tunnel->version == 3) &&
  787. (tunnel->sock->sk_family == AF_INET6)) {
  788. struct sockaddr_pppol2tpv3in6 sp;
  789. len = sizeof(sp);
  790. memset(&sp, 0, len);
  791. sp.sa_family = AF_PPPOX;
  792. sp.sa_protocol = PX_PROTO_OL2TP;
  793. sp.pppol2tp.fd = tunnel->fd;
  794. sp.pppol2tp.pid = pls->owner;
  795. sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
  796. sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
  797. sp.pppol2tp.s_session = session->session_id;
  798. sp.pppol2tp.d_session = session->peer_session_id;
  799. sp.pppol2tp.addr.sin6_family = AF_INET6;
  800. sp.pppol2tp.addr.sin6_port = inet->inet_dport;
  801. memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
  802. sizeof(tunnel->sock->sk_v6_daddr));
  803. memcpy(uaddr, &sp, len);
  804. #endif
  805. } else if (tunnel->version == 3) {
  806. struct sockaddr_pppol2tpv3 sp;
  807. len = sizeof(sp);
  808. memset(&sp, 0, len);
  809. sp.sa_family = AF_PPPOX;
  810. sp.sa_protocol = PX_PROTO_OL2TP;
  811. sp.pppol2tp.fd = tunnel->fd;
  812. sp.pppol2tp.pid = pls->owner;
  813. sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
  814. sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
  815. sp.pppol2tp.s_session = session->session_id;
  816. sp.pppol2tp.d_session = session->peer_session_id;
  817. sp.pppol2tp.addr.sin_family = AF_INET;
  818. sp.pppol2tp.addr.sin_port = inet->inet_dport;
  819. sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
  820. memcpy(uaddr, &sp, len);
  821. }
  822. *usockaddr_len = len;
  823. sock_put(pls->tunnel_sock);
  824. end_put_sess:
  825. sock_put(sk);
  826. error = 0;
  827. end:
  828. return error;
  829. }
  830. /****************************************************************************
  831. * ioctl() handlers.
  832. *
  833. * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
  834. * sockets. However, in order to control kernel tunnel features, we allow
  835. * userspace to create a special "tunnel" PPPoX socket which is used for
  836. * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
  837. * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
  838. * calls.
  839. ****************************************************************************/
  840. static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
  841. struct l2tp_stats *stats)
  842. {
  843. dest->tx_packets = atomic_long_read(&stats->tx_packets);
  844. dest->tx_bytes = atomic_long_read(&stats->tx_bytes);
  845. dest->tx_errors = atomic_long_read(&stats->tx_errors);
  846. dest->rx_packets = atomic_long_read(&stats->rx_packets);
  847. dest->rx_bytes = atomic_long_read(&stats->rx_bytes);
  848. dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards);
  849. dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets);
  850. dest->rx_errors = atomic_long_read(&stats->rx_errors);
  851. }
  852. /* Session ioctl helper.
  853. */
  854. static int pppol2tp_session_ioctl(struct l2tp_session *session,
  855. unsigned int cmd, unsigned long arg)
  856. {
  857. struct ifreq ifr;
  858. int err = 0;
  859. struct sock *sk;
  860. int val = (int) arg;
  861. struct pppol2tp_session *ps = l2tp_session_priv(session);
  862. struct l2tp_tunnel *tunnel = session->tunnel;
  863. struct pppol2tp_ioc_stats stats;
  864. l2tp_dbg(session, PPPOL2TP_MSG_CONTROL,
  865. "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
  866. session->name, cmd, arg);
  867. sk = ps->sock;
  868. sock_hold(sk);
  869. switch (cmd) {
  870. case SIOCGIFMTU:
  871. err = -ENXIO;
  872. if (!(sk->sk_state & PPPOX_CONNECTED))
  873. break;
  874. err = -EFAULT;
  875. if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
  876. break;
  877. ifr.ifr_mtu = session->mtu;
  878. if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
  879. break;
  880. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get mtu=%d\n",
  881. session->name, session->mtu);
  882. err = 0;
  883. break;
  884. case SIOCSIFMTU:
  885. err = -ENXIO;
  886. if (!(sk->sk_state & PPPOX_CONNECTED))
  887. break;
  888. err = -EFAULT;
  889. if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
  890. break;
  891. session->mtu = ifr.ifr_mtu;
  892. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set mtu=%d\n",
  893. session->name, session->mtu);
  894. err = 0;
  895. break;
  896. case PPPIOCGMRU:
  897. err = -ENXIO;
  898. if (!(sk->sk_state & PPPOX_CONNECTED))
  899. break;
  900. err = -EFAULT;
  901. if (put_user(session->mru, (int __user *) arg))
  902. break;
  903. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get mru=%d\n",
  904. session->name, session->mru);
  905. err = 0;
  906. break;
  907. case PPPIOCSMRU:
  908. err = -ENXIO;
  909. if (!(sk->sk_state & PPPOX_CONNECTED))
  910. break;
  911. err = -EFAULT;
  912. if (get_user(val, (int __user *) arg))
  913. break;
  914. session->mru = val;
  915. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set mru=%d\n",
  916. session->name, session->mru);
  917. err = 0;
  918. break;
  919. case PPPIOCGFLAGS:
  920. err = -EFAULT;
  921. if (put_user(ps->flags, (int __user *) arg))
  922. break;
  923. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get flags=%d\n",
  924. session->name, ps->flags);
  925. err = 0;
  926. break;
  927. case PPPIOCSFLAGS:
  928. err = -EFAULT;
  929. if (get_user(val, (int __user *) arg))
  930. break;
  931. ps->flags = val;
  932. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set flags=%d\n",
  933. session->name, ps->flags);
  934. err = 0;
  935. break;
  936. case PPPIOCGL2TPSTATS:
  937. err = -ENXIO;
  938. if (!(sk->sk_state & PPPOX_CONNECTED))
  939. break;
  940. memset(&stats, 0, sizeof(stats));
  941. stats.tunnel_id = tunnel->tunnel_id;
  942. stats.session_id = session->session_id;
  943. pppol2tp_copy_stats(&stats, &session->stats);
  944. if (copy_to_user((void __user *) arg, &stats,
  945. sizeof(stats)))
  946. break;
  947. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get L2TP stats\n",
  948. session->name);
  949. err = 0;
  950. break;
  951. default:
  952. err = -ENOSYS;
  953. break;
  954. }
  955. sock_put(sk);
  956. return err;
  957. }
  958. /* Tunnel ioctl helper.
  959. *
  960. * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
  961. * specifies a session_id, the session ioctl handler is called. This allows an
  962. * application to retrieve session stats via a tunnel socket.
  963. */
  964. static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
  965. unsigned int cmd, unsigned long arg)
  966. {
  967. int err = 0;
  968. struct sock *sk;
  969. struct pppol2tp_ioc_stats stats;
  970. l2tp_dbg(tunnel, PPPOL2TP_MSG_CONTROL,
  971. "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
  972. tunnel->name, cmd, arg);
  973. sk = tunnel->sock;
  974. sock_hold(sk);
  975. switch (cmd) {
  976. case PPPIOCGL2TPSTATS:
  977. err = -ENXIO;
  978. if (!(sk->sk_state & PPPOX_CONNECTED))
  979. break;
  980. if (copy_from_user(&stats, (void __user *) arg,
  981. sizeof(stats))) {
  982. err = -EFAULT;
  983. break;
  984. }
  985. if (stats.session_id != 0) {
  986. /* resend to session ioctl handler */
  987. struct l2tp_session *session =
  988. l2tp_session_find(sock_net(sk), tunnel, stats.session_id);
  989. if (session != NULL)
  990. err = pppol2tp_session_ioctl(session, cmd, arg);
  991. else
  992. err = -EBADR;
  993. break;
  994. }
  995. #ifdef CONFIG_XFRM
  996. stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
  997. #endif
  998. pppol2tp_copy_stats(&stats, &tunnel->stats);
  999. if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
  1000. err = -EFAULT;
  1001. break;
  1002. }
  1003. l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: get L2TP stats\n",
  1004. tunnel->name);
  1005. err = 0;
  1006. break;
  1007. default:
  1008. err = -ENOSYS;
  1009. break;
  1010. }
  1011. sock_put(sk);
  1012. return err;
  1013. }
  1014. /* Main ioctl() handler.
  1015. * Dispatch to tunnel or session helpers depending on the socket.
  1016. */
  1017. static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
  1018. unsigned long arg)
  1019. {
  1020. struct sock *sk = sock->sk;
  1021. struct l2tp_session *session;
  1022. struct l2tp_tunnel *tunnel;
  1023. struct pppol2tp_session *ps;
  1024. int err;
  1025. if (!sk)
  1026. return 0;
  1027. err = -EBADF;
  1028. if (sock_flag(sk, SOCK_DEAD) != 0)
  1029. goto end;
  1030. err = -ENOTCONN;
  1031. if ((sk->sk_user_data == NULL) ||
  1032. (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
  1033. goto end;
  1034. /* Get session context from the socket */
  1035. err = -EBADF;
  1036. session = pppol2tp_sock_to_session(sk);
  1037. if (session == NULL)
  1038. goto end;
  1039. /* Special case: if session's session_id is zero, treat ioctl as a
  1040. * tunnel ioctl
  1041. */
  1042. ps = l2tp_session_priv(session);
  1043. if ((session->session_id == 0) &&
  1044. (session->peer_session_id == 0)) {
  1045. err = -EBADF;
  1046. tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
  1047. if (tunnel == NULL)
  1048. goto end_put_sess;
  1049. err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
  1050. sock_put(ps->tunnel_sock);
  1051. goto end_put_sess;
  1052. }
  1053. err = pppol2tp_session_ioctl(session, cmd, arg);
  1054. end_put_sess:
  1055. sock_put(sk);
  1056. end:
  1057. return err;
  1058. }
  1059. /*****************************************************************************
  1060. * setsockopt() / getsockopt() support.
  1061. *
  1062. * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
  1063. * sockets. In order to control kernel tunnel features, we allow userspace to
  1064. * create a special "tunnel" PPPoX socket which is used for control only.
  1065. * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
  1066. * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
  1067. *****************************************************************************/
  1068. /* Tunnel setsockopt() helper.
  1069. */
  1070. static int pppol2tp_tunnel_setsockopt(struct sock *sk,
  1071. struct l2tp_tunnel *tunnel,
  1072. int optname, int val)
  1073. {
  1074. int err = 0;
  1075. switch (optname) {
  1076. case PPPOL2TP_SO_DEBUG:
  1077. tunnel->debug = val;
  1078. l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: set debug=%x\n",
  1079. tunnel->name, tunnel->debug);
  1080. break;
  1081. default:
  1082. err = -ENOPROTOOPT;
  1083. break;
  1084. }
  1085. return err;
  1086. }
  1087. /* Session setsockopt helper.
  1088. */
  1089. static int pppol2tp_session_setsockopt(struct sock *sk,
  1090. struct l2tp_session *session,
  1091. int optname, int val)
  1092. {
  1093. int err = 0;
  1094. struct pppol2tp_session *ps = l2tp_session_priv(session);
  1095. switch (optname) {
  1096. case PPPOL2TP_SO_RECVSEQ:
  1097. if ((val != 0) && (val != 1)) {
  1098. err = -EINVAL;
  1099. break;
  1100. }
  1101. session->recv_seq = val ? -1 : 0;
  1102. l2tp_info(session, PPPOL2TP_MSG_CONTROL,
  1103. "%s: set recv_seq=%d\n",
  1104. session->name, session->recv_seq);
  1105. break;
  1106. case PPPOL2TP_SO_SENDSEQ:
  1107. if ((val != 0) && (val != 1)) {
  1108. err = -EINVAL;
  1109. break;
  1110. }
  1111. session->send_seq = val ? -1 : 0;
  1112. {
  1113. struct sock *ssk = ps->sock;
  1114. struct pppox_sock *po = pppox_sk(ssk);
  1115. po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
  1116. PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
  1117. }
  1118. l2tp_session_set_header_len(session, session->tunnel->version);
  1119. l2tp_info(session, PPPOL2TP_MSG_CONTROL,
  1120. "%s: set send_seq=%d\n",
  1121. session->name, session->send_seq);
  1122. break;
  1123. case PPPOL2TP_SO_LNSMODE:
  1124. if ((val != 0) && (val != 1)) {
  1125. err = -EINVAL;
  1126. break;
  1127. }
  1128. session->lns_mode = val ? -1 : 0;
  1129. l2tp_info(session, PPPOL2TP_MSG_CONTROL,
  1130. "%s: set lns_mode=%d\n",
  1131. session->name, session->lns_mode);
  1132. break;
  1133. case PPPOL2TP_SO_DEBUG:
  1134. session->debug = val;
  1135. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set debug=%x\n",
  1136. session->name, session->debug);
  1137. break;
  1138. case PPPOL2TP_SO_REORDERTO:
  1139. session->reorder_timeout = msecs_to_jiffies(val);
  1140. l2tp_info(session, PPPOL2TP_MSG_CONTROL,
  1141. "%s: set reorder_timeout=%d\n",
  1142. session->name, session->reorder_timeout);
  1143. break;
  1144. default:
  1145. err = -ENOPROTOOPT;
  1146. break;
  1147. }
  1148. return err;
  1149. }
  1150. /* Main setsockopt() entry point.
  1151. * Does API checks, then calls either the tunnel or session setsockopt
  1152. * handler, according to whether the PPPoL2TP socket is a for a regular
  1153. * session or the special tunnel type.
  1154. */
  1155. static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
  1156. char __user *optval, unsigned int optlen)
  1157. {
  1158. struct sock *sk = sock->sk;
  1159. struct l2tp_session *session;
  1160. struct l2tp_tunnel *tunnel;
  1161. struct pppol2tp_session *ps;
  1162. int val;
  1163. int err;
  1164. if (level != SOL_PPPOL2TP)
  1165. return -EINVAL;
  1166. if (optlen < sizeof(int))
  1167. return -EINVAL;
  1168. if (get_user(val, (int __user *)optval))
  1169. return -EFAULT;
  1170. err = -ENOTCONN;
  1171. if (sk->sk_user_data == NULL)
  1172. goto end;
  1173. /* Get session context from the socket */
  1174. err = -EBADF;
  1175. session = pppol2tp_sock_to_session(sk);
  1176. if (session == NULL)
  1177. goto end;
  1178. /* Special case: if session_id == 0x0000, treat as operation on tunnel
  1179. */
  1180. ps = l2tp_session_priv(session);
  1181. if ((session->session_id == 0) &&
  1182. (session->peer_session_id == 0)) {
  1183. err = -EBADF;
  1184. tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
  1185. if (tunnel == NULL)
  1186. goto end_put_sess;
  1187. err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
  1188. sock_put(ps->tunnel_sock);
  1189. } else
  1190. err = pppol2tp_session_setsockopt(sk, session, optname, val);
  1191. err = 0;
  1192. end_put_sess:
  1193. sock_put(sk);
  1194. end:
  1195. return err;
  1196. }
  1197. /* Tunnel getsockopt helper. Called with sock locked.
  1198. */
  1199. static int pppol2tp_tunnel_getsockopt(struct sock *sk,
  1200. struct l2tp_tunnel *tunnel,
  1201. int optname, int *val)
  1202. {
  1203. int err = 0;
  1204. switch (optname) {
  1205. case PPPOL2TP_SO_DEBUG:
  1206. *val = tunnel->debug;
  1207. l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: get debug=%x\n",
  1208. tunnel->name, tunnel->debug);
  1209. break;
  1210. default:
  1211. err = -ENOPROTOOPT;
  1212. break;
  1213. }
  1214. return err;
  1215. }
  1216. /* Session getsockopt helper. Called with sock locked.
  1217. */
  1218. static int pppol2tp_session_getsockopt(struct sock *sk,
  1219. struct l2tp_session *session,
  1220. int optname, int *val)
  1221. {
  1222. int err = 0;
  1223. switch (optname) {
  1224. case PPPOL2TP_SO_RECVSEQ:
  1225. *val = session->recv_seq;
  1226. l2tp_info(session, PPPOL2TP_MSG_CONTROL,
  1227. "%s: get recv_seq=%d\n", session->name, *val);
  1228. break;
  1229. case PPPOL2TP_SO_SENDSEQ:
  1230. *val = session->send_seq;
  1231. l2tp_info(session, PPPOL2TP_MSG_CONTROL,
  1232. "%s: get send_seq=%d\n", session->name, *val);
  1233. break;
  1234. case PPPOL2TP_SO_LNSMODE:
  1235. *val = session->lns_mode;
  1236. l2tp_info(session, PPPOL2TP_MSG_CONTROL,
  1237. "%s: get lns_mode=%d\n", session->name, *val);
  1238. break;
  1239. case PPPOL2TP_SO_DEBUG:
  1240. *val = session->debug;
  1241. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get debug=%d\n",
  1242. session->name, *val);
  1243. break;
  1244. case PPPOL2TP_SO_REORDERTO:
  1245. *val = (int) jiffies_to_msecs(session->reorder_timeout);
  1246. l2tp_info(session, PPPOL2TP_MSG_CONTROL,
  1247. "%s: get reorder_timeout=%d\n", session->name, *val);
  1248. break;
  1249. default:
  1250. err = -ENOPROTOOPT;
  1251. }
  1252. return err;
  1253. }
  1254. /* Main getsockopt() entry point.
  1255. * Does API checks, then calls either the tunnel or session getsockopt
  1256. * handler, according to whether the PPPoX socket is a for a regular session
  1257. * or the special tunnel type.
  1258. */
  1259. static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
  1260. char __user *optval, int __user *optlen)
  1261. {
  1262. struct sock *sk = sock->sk;
  1263. struct l2tp_session *session;
  1264. struct l2tp_tunnel *tunnel;
  1265. int val, len;
  1266. int err;
  1267. struct pppol2tp_session *ps;
  1268. if (level != SOL_PPPOL2TP)
  1269. return -EINVAL;
  1270. if (get_user(len, optlen))
  1271. return -EFAULT;
  1272. len = min_t(unsigned int, len, sizeof(int));
  1273. if (len < 0)
  1274. return -EINVAL;
  1275. err = -ENOTCONN;
  1276. if (sk->sk_user_data == NULL)
  1277. goto end;
  1278. /* Get the session context */
  1279. err = -EBADF;
  1280. session = pppol2tp_sock_to_session(sk);
  1281. if (session == NULL)
  1282. goto end;
  1283. /* Special case: if session_id == 0x0000, treat as operation on tunnel */
  1284. ps = l2tp_session_priv(session);
  1285. if ((session->session_id == 0) &&
  1286. (session->peer_session_id == 0)) {
  1287. err = -EBADF;
  1288. tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
  1289. if (tunnel == NULL)
  1290. goto end_put_sess;
  1291. err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
  1292. sock_put(ps->tunnel_sock);
  1293. } else
  1294. err = pppol2tp_session_getsockopt(sk, session, optname, &val);
  1295. err = -EFAULT;
  1296. if (put_user(len, optlen))
  1297. goto end_put_sess;
  1298. if (copy_to_user((void __user *) optval, &val, len))
  1299. goto end_put_sess;
  1300. err = 0;
  1301. end_put_sess:
  1302. sock_put(sk);
  1303. end:
  1304. return err;
  1305. }
  1306. /*****************************************************************************
  1307. * /proc filesystem for debug
  1308. * Since the original pppol2tp driver provided /proc/net/pppol2tp for
  1309. * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
  1310. *****************************************************************************/
  1311. static unsigned int pppol2tp_net_id;
  1312. #ifdef CONFIG_PROC_FS
  1313. struct pppol2tp_seq_data {
  1314. struct seq_net_private p;
  1315. int tunnel_idx; /* current tunnel */
  1316. int session_idx; /* index of session within current tunnel */
  1317. struct l2tp_tunnel *tunnel;
  1318. struct l2tp_session *session; /* NULL means get next tunnel */
  1319. };
  1320. static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
  1321. {
  1322. for (;;) {
  1323. pd->tunnel = l2tp_tunnel_find_nth(net, pd->tunnel_idx);
  1324. pd->tunnel_idx++;
  1325. if (pd->tunnel == NULL)
  1326. break;
  1327. /* Ignore L2TPv3 tunnels */
  1328. if (pd->tunnel->version < 3)
  1329. break;
  1330. }
  1331. }
  1332. static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
  1333. {
  1334. pd->session = l2tp_session_find_nth(pd->tunnel, pd->session_idx);
  1335. pd->session_idx++;
  1336. if (pd->session == NULL) {
  1337. pd->session_idx = 0;
  1338. pppol2tp_next_tunnel(net, pd);
  1339. }
  1340. }
  1341. static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
  1342. {
  1343. struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
  1344. loff_t pos = *offs;
  1345. struct net *net;
  1346. if (!pos)
  1347. goto out;
  1348. BUG_ON(m->private == NULL);
  1349. pd = m->private;
  1350. net = seq_file_net(m);
  1351. if (pd->tunnel == NULL)
  1352. pppol2tp_next_tunnel(net, pd);
  1353. else
  1354. pppol2tp_next_session(net, pd);
  1355. /* NULL tunnel and session indicates end of list */
  1356. if ((pd->tunnel == NULL) && (pd->session == NULL))
  1357. pd = NULL;
  1358. out:
  1359. return pd;
  1360. }
  1361. static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
  1362. {
  1363. (*pos)++;
  1364. return NULL;
  1365. }
  1366. static void pppol2tp_seq_stop(struct seq_file *p, void *v)
  1367. {
  1368. /* nothing to do */
  1369. }
  1370. static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
  1371. {
  1372. struct l2tp_tunnel *tunnel = v;
  1373. seq_printf(m, "\nTUNNEL '%s', %c %d\n",
  1374. tunnel->name,
  1375. (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
  1376. atomic_read(&tunnel->ref_count) - 1);
  1377. seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n",
  1378. tunnel->debug,
  1379. atomic_long_read(&tunnel->stats.tx_packets),
  1380. atomic_long_read(&tunnel->stats.tx_bytes),
  1381. atomic_long_read(&tunnel->stats.tx_errors),
  1382. atomic_long_read(&tunnel->stats.rx_packets),
  1383. atomic_long_read(&tunnel->stats.rx_bytes),
  1384. atomic_long_read(&tunnel->stats.rx_errors));
  1385. }
  1386. static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
  1387. {
  1388. struct l2tp_session *session = v;
  1389. struct l2tp_tunnel *tunnel = session->tunnel;
  1390. struct pppol2tp_session *ps = l2tp_session_priv(session);
  1391. struct pppox_sock *po = pppox_sk(ps->sock);
  1392. u32 ip = 0;
  1393. u16 port = 0;
  1394. if (tunnel->sock) {
  1395. struct inet_sock *inet = inet_sk(tunnel->sock);
  1396. ip = ntohl(inet->inet_saddr);
  1397. port = ntohs(inet->inet_sport);
  1398. }
  1399. seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
  1400. "%04X/%04X %d %c\n",
  1401. session->name, ip, port,
  1402. tunnel->tunnel_id,
  1403. session->session_id,
  1404. tunnel->peer_tunnel_id,
  1405. session->peer_session_id,
  1406. ps->sock->sk_state,
  1407. (session == ps->sock->sk_user_data) ?
  1408. 'Y' : 'N');
  1409. seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
  1410. session->mtu, session->mru,
  1411. session->recv_seq ? 'R' : '-',
  1412. session->send_seq ? 'S' : '-',
  1413. session->lns_mode ? "LNS" : "LAC",
  1414. session->debug,
  1415. jiffies_to_msecs(session->reorder_timeout));
  1416. seq_printf(m, " %hu/%hu %ld/%ld/%ld %ld/%ld/%ld\n",
  1417. session->nr, session->ns,
  1418. atomic_long_read(&session->stats.tx_packets),
  1419. atomic_long_read(&session->stats.tx_bytes),
  1420. atomic_long_read(&session->stats.tx_errors),
  1421. atomic_long_read(&session->stats.rx_packets),
  1422. atomic_long_read(&session->stats.rx_bytes),
  1423. atomic_long_read(&session->stats.rx_errors));
  1424. if (po)
  1425. seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
  1426. }
  1427. static int pppol2tp_seq_show(struct seq_file *m, void *v)
  1428. {
  1429. struct pppol2tp_seq_data *pd = v;
  1430. /* display header on line 1 */
  1431. if (v == SEQ_START_TOKEN) {
  1432. seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
  1433. seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
  1434. seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
  1435. seq_puts(m, " SESSION name, addr/port src-tid/sid "
  1436. "dest-tid/sid state user-data-ok\n");
  1437. seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
  1438. seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
  1439. goto out;
  1440. }
  1441. /* Show the tunnel or session context.
  1442. */
  1443. if (pd->session == NULL)
  1444. pppol2tp_seq_tunnel_show(m, pd->tunnel);
  1445. else
  1446. pppol2tp_seq_session_show(m, pd->session);
  1447. out:
  1448. return 0;
  1449. }
  1450. static const struct seq_operations pppol2tp_seq_ops = {
  1451. .start = pppol2tp_seq_start,
  1452. .next = pppol2tp_seq_next,
  1453. .stop = pppol2tp_seq_stop,
  1454. .show = pppol2tp_seq_show,
  1455. };
  1456. /* Called when our /proc file is opened. We allocate data for use when
  1457. * iterating our tunnel / session contexts and store it in the private
  1458. * data of the seq_file.
  1459. */
  1460. static int pppol2tp_proc_open(struct inode *inode, struct file *file)
  1461. {
  1462. return seq_open_net(inode, file, &pppol2tp_seq_ops,
  1463. sizeof(struct pppol2tp_seq_data));
  1464. }
  1465. static const struct file_operations pppol2tp_proc_fops = {
  1466. .owner = THIS_MODULE,
  1467. .open = pppol2tp_proc_open,
  1468. .read = seq_read,
  1469. .llseek = seq_lseek,
  1470. .release = seq_release_net,
  1471. };
  1472. #endif /* CONFIG_PROC_FS */
  1473. /*****************************************************************************
  1474. * Network namespace
  1475. *****************************************************************************/
  1476. static __net_init int pppol2tp_init_net(struct net *net)
  1477. {
  1478. struct proc_dir_entry *pde;
  1479. int err = 0;
  1480. pde = proc_create("pppol2tp", S_IRUGO, net->proc_net,
  1481. &pppol2tp_proc_fops);
  1482. if (!pde) {
  1483. err = -ENOMEM;
  1484. goto out;
  1485. }
  1486. out:
  1487. return err;
  1488. }
  1489. static __net_exit void pppol2tp_exit_net(struct net *net)
  1490. {
  1491. remove_proc_entry("pppol2tp", net->proc_net);
  1492. }
  1493. static struct pernet_operations pppol2tp_net_ops = {
  1494. .init = pppol2tp_init_net,
  1495. .exit = pppol2tp_exit_net,
  1496. .id = &pppol2tp_net_id,
  1497. };
  1498. /*****************************************************************************
  1499. * Init and cleanup
  1500. *****************************************************************************/
  1501. static const struct proto_ops pppol2tp_ops = {
  1502. .family = AF_PPPOX,
  1503. .owner = THIS_MODULE,
  1504. .release = pppol2tp_release,
  1505. .bind = sock_no_bind,
  1506. .connect = pppol2tp_connect,
  1507. .socketpair = sock_no_socketpair,
  1508. .accept = sock_no_accept,
  1509. .getname = pppol2tp_getname,
  1510. .poll = datagram_poll,
  1511. .listen = sock_no_listen,
  1512. .shutdown = sock_no_shutdown,
  1513. .setsockopt = pppol2tp_setsockopt,
  1514. .getsockopt = pppol2tp_getsockopt,
  1515. .sendmsg = pppol2tp_sendmsg,
  1516. .recvmsg = pppol2tp_recvmsg,
  1517. .mmap = sock_no_mmap,
  1518. .ioctl = pppox_ioctl,
  1519. };
  1520. static const struct pppox_proto pppol2tp_proto = {
  1521. .create = pppol2tp_create,
  1522. .ioctl = pppol2tp_ioctl,
  1523. .owner = THIS_MODULE,
  1524. };
  1525. #ifdef CONFIG_L2TP_V3
  1526. static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
  1527. .session_create = pppol2tp_session_create,
  1528. .session_delete = l2tp_session_delete,
  1529. };
  1530. #endif /* CONFIG_L2TP_V3 */
  1531. static int __init pppol2tp_init(void)
  1532. {
  1533. int err;
  1534. err = register_pernet_device(&pppol2tp_net_ops);
  1535. if (err)
  1536. goto out;
  1537. err = proto_register(&pppol2tp_sk_proto, 0);
  1538. if (err)
  1539. goto out_unregister_pppol2tp_pernet;
  1540. err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
  1541. if (err)
  1542. goto out_unregister_pppol2tp_proto;
  1543. #ifdef CONFIG_L2TP_V3
  1544. err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
  1545. if (err)
  1546. goto out_unregister_pppox;
  1547. #endif
  1548. pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
  1549. out:
  1550. return err;
  1551. #ifdef CONFIG_L2TP_V3
  1552. out_unregister_pppox:
  1553. unregister_pppox_proto(PX_PROTO_OL2TP);
  1554. #endif
  1555. out_unregister_pppol2tp_proto:
  1556. proto_unregister(&pppol2tp_sk_proto);
  1557. out_unregister_pppol2tp_pernet:
  1558. unregister_pernet_device(&pppol2tp_net_ops);
  1559. goto out;
  1560. }
  1561. static void __exit pppol2tp_exit(void)
  1562. {
  1563. #ifdef CONFIG_L2TP_V3
  1564. l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
  1565. #endif
  1566. unregister_pppox_proto(PX_PROTO_OL2TP);
  1567. proto_unregister(&pppol2tp_sk_proto);
  1568. unregister_pernet_device(&pppol2tp_net_ops);
  1569. }
  1570. module_init(pppol2tp_init);
  1571. module_exit(pppol2tp_exit);
  1572. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  1573. MODULE_DESCRIPTION("PPP over L2TP over UDP");
  1574. MODULE_LICENSE("GPL");
  1575. MODULE_VERSION(PPPOL2TP_DRV_VERSION);
  1576. MODULE_ALIAS("pppox-proto-" __stringify(PX_PROTO_OL2TP));