l2tp_ppp.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881
  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. skb_queue_purge(&sk->sk_receive_queue);
  401. skb_queue_purge(&sk->sk_write_queue);
  402. if (session) {
  403. sk->sk_user_data = NULL;
  404. BUG_ON(session->magic != L2TP_SESSION_MAGIC);
  405. l2tp_session_dec_refcount(session);
  406. }
  407. }
  408. /* Called when the PPPoX socket (session) is closed.
  409. */
  410. static int pppol2tp_release(struct socket *sock)
  411. {
  412. struct sock *sk = sock->sk;
  413. struct l2tp_session *session;
  414. int error;
  415. if (!sk)
  416. return 0;
  417. error = -EBADF;
  418. lock_sock(sk);
  419. if (sock_flag(sk, SOCK_DEAD) != 0)
  420. goto error;
  421. pppox_unbind_sock(sk);
  422. /* Signal the death of the socket. */
  423. sk->sk_state = PPPOX_DEAD;
  424. sock_orphan(sk);
  425. sock->sk = NULL;
  426. session = pppol2tp_sock_to_session(sk);
  427. /* Purge any queued data */
  428. if (session != NULL) {
  429. __l2tp_session_unhash(session);
  430. l2tp_session_queue_purge(session);
  431. sock_put(sk);
  432. }
  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 (sockaddr_len != sizeof(struct sockaddr_pppol2tp) &&
  512. sockaddr_len != sizeof(struct sockaddr_pppol2tpv3) &&
  513. sockaddr_len != sizeof(struct sockaddr_pppol2tpin6) &&
  514. sockaddr_len != sizeof(struct sockaddr_pppol2tpv3in6))
  515. goto end;
  516. if (sp->sa_protocol != PX_PROTO_OL2TP)
  517. goto end;
  518. /* Check for already bound sockets */
  519. error = -EBUSY;
  520. if (sk->sk_state & PPPOX_CONNECTED)
  521. goto end;
  522. /* We don't supporting rebinding anyway */
  523. error = -EALREADY;
  524. if (sk->sk_user_data)
  525. goto end; /* socket is already attached */
  526. /* Get params from socket address. Handle L2TPv2 and L2TPv3.
  527. * This is nasty because there are different sockaddr_pppol2tp
  528. * structs for L2TPv2, L2TPv3, over IPv4 and IPv6. We use
  529. * the sockaddr size to determine which structure the caller
  530. * is using.
  531. */
  532. peer_tunnel_id = 0;
  533. if (sockaddr_len == sizeof(struct sockaddr_pppol2tp)) {
  534. fd = sp->pppol2tp.fd;
  535. tunnel_id = sp->pppol2tp.s_tunnel;
  536. peer_tunnel_id = sp->pppol2tp.d_tunnel;
  537. session_id = sp->pppol2tp.s_session;
  538. peer_session_id = sp->pppol2tp.d_session;
  539. } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3)) {
  540. struct sockaddr_pppol2tpv3 *sp3 =
  541. (struct sockaddr_pppol2tpv3 *) sp;
  542. ver = 3;
  543. fd = sp3->pppol2tp.fd;
  544. tunnel_id = sp3->pppol2tp.s_tunnel;
  545. peer_tunnel_id = sp3->pppol2tp.d_tunnel;
  546. session_id = sp3->pppol2tp.s_session;
  547. peer_session_id = sp3->pppol2tp.d_session;
  548. } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpin6)) {
  549. struct sockaddr_pppol2tpin6 *sp6 =
  550. (struct sockaddr_pppol2tpin6 *) sp;
  551. fd = sp6->pppol2tp.fd;
  552. tunnel_id = sp6->pppol2tp.s_tunnel;
  553. peer_tunnel_id = sp6->pppol2tp.d_tunnel;
  554. session_id = sp6->pppol2tp.s_session;
  555. peer_session_id = sp6->pppol2tp.d_session;
  556. } else if (sockaddr_len == sizeof(struct sockaddr_pppol2tpv3in6)) {
  557. struct sockaddr_pppol2tpv3in6 *sp6 =
  558. (struct sockaddr_pppol2tpv3in6 *) sp;
  559. ver = 3;
  560. fd = sp6->pppol2tp.fd;
  561. tunnel_id = sp6->pppol2tp.s_tunnel;
  562. peer_tunnel_id = sp6->pppol2tp.d_tunnel;
  563. session_id = sp6->pppol2tp.s_session;
  564. peer_session_id = sp6->pppol2tp.d_session;
  565. } else {
  566. error = -EINVAL;
  567. goto end; /* bad socket address */
  568. }
  569. /* Don't bind if tunnel_id is 0 */
  570. error = -EINVAL;
  571. if (tunnel_id == 0)
  572. goto end;
  573. tunnel = l2tp_tunnel_find(sock_net(sk), tunnel_id);
  574. /* Special case: create tunnel context if session_id and
  575. * peer_session_id is 0. Otherwise look up tunnel using supplied
  576. * tunnel id.
  577. */
  578. if ((session_id == 0) && (peer_session_id == 0)) {
  579. if (tunnel == NULL) {
  580. struct l2tp_tunnel_cfg tcfg = {
  581. .encap = L2TP_ENCAPTYPE_UDP,
  582. .debug = 0,
  583. };
  584. error = l2tp_tunnel_create(sock_net(sk), fd, ver, tunnel_id, peer_tunnel_id, &tcfg, &tunnel);
  585. if (error < 0)
  586. goto end;
  587. }
  588. } else {
  589. /* Error if we can't find the tunnel */
  590. error = -ENOENT;
  591. if (tunnel == NULL)
  592. goto end;
  593. /* Error if socket is not prepped */
  594. if (tunnel->sock == NULL)
  595. goto end;
  596. }
  597. if (tunnel->recv_payload_hook == NULL)
  598. tunnel->recv_payload_hook = pppol2tp_recv_payload_hook;
  599. if (tunnel->peer_tunnel_id == 0)
  600. tunnel->peer_tunnel_id = peer_tunnel_id;
  601. /* Create session if it doesn't already exist. We handle the
  602. * case where a session was previously created by the netlink
  603. * interface by checking that the session doesn't already have
  604. * a socket and its tunnel socket are what we expect. If any
  605. * of those checks fail, return EEXIST to the caller.
  606. */
  607. session = l2tp_session_find(sock_net(sk), tunnel, session_id);
  608. if (session == NULL) {
  609. /* Default MTU must allow space for UDP/L2TP/PPP
  610. * headers.
  611. */
  612. cfg.mtu = cfg.mru = 1500 - PPPOL2TP_HEADER_OVERHEAD;
  613. /* Allocate and initialize a new session context. */
  614. session = l2tp_session_create(sizeof(struct pppol2tp_session),
  615. tunnel, session_id,
  616. peer_session_id, &cfg);
  617. if (session == NULL) {
  618. error = -ENOMEM;
  619. goto end;
  620. }
  621. } else {
  622. ps = l2tp_session_priv(session);
  623. error = -EEXIST;
  624. if (ps->sock != NULL)
  625. goto end;
  626. /* consistency checks */
  627. if (ps->tunnel_sock != tunnel->sock)
  628. goto end;
  629. }
  630. /* Associate session with its PPPoL2TP socket */
  631. ps = l2tp_session_priv(session);
  632. ps->owner = current->pid;
  633. ps->sock = sk;
  634. ps->tunnel_sock = tunnel->sock;
  635. session->recv_skb = pppol2tp_recv;
  636. session->session_close = pppol2tp_session_close;
  637. #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
  638. session->show = pppol2tp_show;
  639. #endif
  640. /* We need to know each time a skb is dropped from the reorder
  641. * queue.
  642. */
  643. session->ref = pppol2tp_session_sock_hold;
  644. session->deref = pppol2tp_session_sock_put;
  645. /* If PMTU discovery was enabled, use the MTU that was discovered */
  646. dst = sk_dst_get(tunnel->sock);
  647. if (dst != NULL) {
  648. u32 pmtu = dst_mtu(dst);
  649. if (pmtu != 0)
  650. session->mtu = session->mru = pmtu -
  651. PPPOL2TP_HEADER_OVERHEAD;
  652. dst_release(dst);
  653. }
  654. /* Special case: if source & dest session_id == 0x0000, this
  655. * socket is being created to manage the tunnel. Just set up
  656. * the internal context for use by ioctl() and sockopt()
  657. * handlers.
  658. */
  659. if ((session->session_id == 0) &&
  660. (session->peer_session_id == 0)) {
  661. error = 0;
  662. goto out_no_ppp;
  663. }
  664. /* The only header we need to worry about is the L2TP
  665. * header. This size is different depending on whether
  666. * sequence numbers are enabled for the data channel.
  667. */
  668. po->chan.hdrlen = PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
  669. po->chan.private = sk;
  670. po->chan.ops = &pppol2tp_chan_ops;
  671. po->chan.mtu = session->mtu;
  672. error = ppp_register_net_channel(sock_net(sk), &po->chan);
  673. if (error)
  674. goto end;
  675. out_no_ppp:
  676. /* This is how we get the session context from the socket. */
  677. sk->sk_user_data = session;
  678. sk->sk_state = PPPOX_CONNECTED;
  679. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: created\n",
  680. session->name);
  681. end:
  682. release_sock(sk);
  683. return error;
  684. }
  685. #ifdef CONFIG_L2TP_V3
  686. /* Called when creating sessions via the netlink interface.
  687. */
  688. static int pppol2tp_session_create(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg)
  689. {
  690. int error;
  691. struct l2tp_tunnel *tunnel;
  692. struct l2tp_session *session;
  693. struct pppol2tp_session *ps;
  694. tunnel = l2tp_tunnel_find(net, tunnel_id);
  695. /* Error if we can't find the tunnel */
  696. error = -ENOENT;
  697. if (tunnel == NULL)
  698. goto out;
  699. /* Error if tunnel socket is not prepped */
  700. if (tunnel->sock == NULL)
  701. goto out;
  702. /* Check that this session doesn't already exist */
  703. error = -EEXIST;
  704. session = l2tp_session_find(net, tunnel, session_id);
  705. if (session != NULL)
  706. goto out;
  707. /* Default MTU values. */
  708. if (cfg->mtu == 0)
  709. cfg->mtu = 1500 - PPPOL2TP_HEADER_OVERHEAD;
  710. if (cfg->mru == 0)
  711. cfg->mru = cfg->mtu;
  712. /* Allocate and initialize a new session context. */
  713. error = -ENOMEM;
  714. session = l2tp_session_create(sizeof(struct pppol2tp_session),
  715. tunnel, session_id,
  716. peer_session_id, cfg);
  717. if (session == NULL)
  718. goto out;
  719. ps = l2tp_session_priv(session);
  720. ps->tunnel_sock = tunnel->sock;
  721. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: created\n",
  722. session->name);
  723. error = 0;
  724. out:
  725. return error;
  726. }
  727. #endif /* CONFIG_L2TP_V3 */
  728. /* getname() support.
  729. */
  730. static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
  731. int *usockaddr_len, int peer)
  732. {
  733. int len = 0;
  734. int error = 0;
  735. struct l2tp_session *session;
  736. struct l2tp_tunnel *tunnel;
  737. struct sock *sk = sock->sk;
  738. struct inet_sock *inet;
  739. struct pppol2tp_session *pls;
  740. error = -ENOTCONN;
  741. if (sk == NULL)
  742. goto end;
  743. if (sk->sk_state != PPPOX_CONNECTED)
  744. goto end;
  745. error = -EBADF;
  746. session = pppol2tp_sock_to_session(sk);
  747. if (session == NULL)
  748. goto end;
  749. pls = l2tp_session_priv(session);
  750. tunnel = l2tp_sock_to_tunnel(pls->tunnel_sock);
  751. if (tunnel == NULL) {
  752. error = -EBADF;
  753. goto end_put_sess;
  754. }
  755. inet = inet_sk(tunnel->sock);
  756. if ((tunnel->version == 2) && (tunnel->sock->sk_family == AF_INET)) {
  757. struct sockaddr_pppol2tp sp;
  758. len = sizeof(sp);
  759. memset(&sp, 0, len);
  760. sp.sa_family = AF_PPPOX;
  761. sp.sa_protocol = PX_PROTO_OL2TP;
  762. sp.pppol2tp.fd = tunnel->fd;
  763. sp.pppol2tp.pid = pls->owner;
  764. sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
  765. sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
  766. sp.pppol2tp.s_session = session->session_id;
  767. sp.pppol2tp.d_session = session->peer_session_id;
  768. sp.pppol2tp.addr.sin_family = AF_INET;
  769. sp.pppol2tp.addr.sin_port = inet->inet_dport;
  770. sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
  771. memcpy(uaddr, &sp, len);
  772. #if IS_ENABLED(CONFIG_IPV6)
  773. } else if ((tunnel->version == 2) &&
  774. (tunnel->sock->sk_family == AF_INET6)) {
  775. struct sockaddr_pppol2tpin6 sp;
  776. len = sizeof(sp);
  777. memset(&sp, 0, len);
  778. sp.sa_family = AF_PPPOX;
  779. sp.sa_protocol = PX_PROTO_OL2TP;
  780. sp.pppol2tp.fd = tunnel->fd;
  781. sp.pppol2tp.pid = pls->owner;
  782. sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
  783. sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
  784. sp.pppol2tp.s_session = session->session_id;
  785. sp.pppol2tp.d_session = session->peer_session_id;
  786. sp.pppol2tp.addr.sin6_family = AF_INET6;
  787. sp.pppol2tp.addr.sin6_port = inet->inet_dport;
  788. memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
  789. sizeof(tunnel->sock->sk_v6_daddr));
  790. memcpy(uaddr, &sp, len);
  791. } else if ((tunnel->version == 3) &&
  792. (tunnel->sock->sk_family == AF_INET6)) {
  793. struct sockaddr_pppol2tpv3in6 sp;
  794. len = sizeof(sp);
  795. memset(&sp, 0, len);
  796. sp.sa_family = AF_PPPOX;
  797. sp.sa_protocol = PX_PROTO_OL2TP;
  798. sp.pppol2tp.fd = tunnel->fd;
  799. sp.pppol2tp.pid = pls->owner;
  800. sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
  801. sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
  802. sp.pppol2tp.s_session = session->session_id;
  803. sp.pppol2tp.d_session = session->peer_session_id;
  804. sp.pppol2tp.addr.sin6_family = AF_INET6;
  805. sp.pppol2tp.addr.sin6_port = inet->inet_dport;
  806. memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
  807. sizeof(tunnel->sock->sk_v6_daddr));
  808. memcpy(uaddr, &sp, len);
  809. #endif
  810. } else if (tunnel->version == 3) {
  811. struct sockaddr_pppol2tpv3 sp;
  812. len = sizeof(sp);
  813. memset(&sp, 0, len);
  814. sp.sa_family = AF_PPPOX;
  815. sp.sa_protocol = PX_PROTO_OL2TP;
  816. sp.pppol2tp.fd = tunnel->fd;
  817. sp.pppol2tp.pid = pls->owner;
  818. sp.pppol2tp.s_tunnel = tunnel->tunnel_id;
  819. sp.pppol2tp.d_tunnel = tunnel->peer_tunnel_id;
  820. sp.pppol2tp.s_session = session->session_id;
  821. sp.pppol2tp.d_session = session->peer_session_id;
  822. sp.pppol2tp.addr.sin_family = AF_INET;
  823. sp.pppol2tp.addr.sin_port = inet->inet_dport;
  824. sp.pppol2tp.addr.sin_addr.s_addr = inet->inet_daddr;
  825. memcpy(uaddr, &sp, len);
  826. }
  827. *usockaddr_len = len;
  828. sock_put(pls->tunnel_sock);
  829. end_put_sess:
  830. sock_put(sk);
  831. error = 0;
  832. end:
  833. return error;
  834. }
  835. /****************************************************************************
  836. * ioctl() handlers.
  837. *
  838. * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
  839. * sockets. However, in order to control kernel tunnel features, we allow
  840. * userspace to create a special "tunnel" PPPoX socket which is used for
  841. * control only. Tunnel PPPoX sockets have session_id == 0 and simply allow
  842. * the user application to issue L2TP setsockopt(), getsockopt() and ioctl()
  843. * calls.
  844. ****************************************************************************/
  845. static void pppol2tp_copy_stats(struct pppol2tp_ioc_stats *dest,
  846. struct l2tp_stats *stats)
  847. {
  848. dest->tx_packets = atomic_long_read(&stats->tx_packets);
  849. dest->tx_bytes = atomic_long_read(&stats->tx_bytes);
  850. dest->tx_errors = atomic_long_read(&stats->tx_errors);
  851. dest->rx_packets = atomic_long_read(&stats->rx_packets);
  852. dest->rx_bytes = atomic_long_read(&stats->rx_bytes);
  853. dest->rx_seq_discards = atomic_long_read(&stats->rx_seq_discards);
  854. dest->rx_oos_packets = atomic_long_read(&stats->rx_oos_packets);
  855. dest->rx_errors = atomic_long_read(&stats->rx_errors);
  856. }
  857. /* Session ioctl helper.
  858. */
  859. static int pppol2tp_session_ioctl(struct l2tp_session *session,
  860. unsigned int cmd, unsigned long arg)
  861. {
  862. struct ifreq ifr;
  863. int err = 0;
  864. struct sock *sk;
  865. int val = (int) arg;
  866. struct pppol2tp_session *ps = l2tp_session_priv(session);
  867. struct l2tp_tunnel *tunnel = session->tunnel;
  868. struct pppol2tp_ioc_stats stats;
  869. l2tp_dbg(session, PPPOL2TP_MSG_CONTROL,
  870. "%s: pppol2tp_session_ioctl(cmd=%#x, arg=%#lx)\n",
  871. session->name, cmd, arg);
  872. sk = ps->sock;
  873. if (!sk)
  874. return -EBADR;
  875. sock_hold(sk);
  876. switch (cmd) {
  877. case SIOCGIFMTU:
  878. err = -ENXIO;
  879. if (!(sk->sk_state & PPPOX_CONNECTED))
  880. break;
  881. err = -EFAULT;
  882. if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
  883. break;
  884. ifr.ifr_mtu = session->mtu;
  885. if (copy_to_user((void __user *) arg, &ifr, sizeof(struct ifreq)))
  886. break;
  887. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get mtu=%d\n",
  888. session->name, session->mtu);
  889. err = 0;
  890. break;
  891. case SIOCSIFMTU:
  892. err = -ENXIO;
  893. if (!(sk->sk_state & PPPOX_CONNECTED))
  894. break;
  895. err = -EFAULT;
  896. if (copy_from_user(&ifr, (void __user *) arg, sizeof(struct ifreq)))
  897. break;
  898. session->mtu = ifr.ifr_mtu;
  899. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set mtu=%d\n",
  900. session->name, session->mtu);
  901. err = 0;
  902. break;
  903. case PPPIOCGMRU:
  904. err = -ENXIO;
  905. if (!(sk->sk_state & PPPOX_CONNECTED))
  906. break;
  907. err = -EFAULT;
  908. if (put_user(session->mru, (int __user *) arg))
  909. break;
  910. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get mru=%d\n",
  911. session->name, session->mru);
  912. err = 0;
  913. break;
  914. case PPPIOCSMRU:
  915. err = -ENXIO;
  916. if (!(sk->sk_state & PPPOX_CONNECTED))
  917. break;
  918. err = -EFAULT;
  919. if (get_user(val, (int __user *) arg))
  920. break;
  921. session->mru = val;
  922. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set mru=%d\n",
  923. session->name, session->mru);
  924. err = 0;
  925. break;
  926. case PPPIOCGFLAGS:
  927. err = -EFAULT;
  928. if (put_user(ps->flags, (int __user *) arg))
  929. break;
  930. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get flags=%d\n",
  931. session->name, ps->flags);
  932. err = 0;
  933. break;
  934. case PPPIOCSFLAGS:
  935. err = -EFAULT;
  936. if (get_user(val, (int __user *) arg))
  937. break;
  938. ps->flags = val;
  939. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set flags=%d\n",
  940. session->name, ps->flags);
  941. err = 0;
  942. break;
  943. case PPPIOCGL2TPSTATS:
  944. err = -ENXIO;
  945. if (!(sk->sk_state & PPPOX_CONNECTED))
  946. break;
  947. memset(&stats, 0, sizeof(stats));
  948. stats.tunnel_id = tunnel->tunnel_id;
  949. stats.session_id = session->session_id;
  950. pppol2tp_copy_stats(&stats, &session->stats);
  951. if (copy_to_user((void __user *) arg, &stats,
  952. sizeof(stats)))
  953. break;
  954. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get L2TP stats\n",
  955. session->name);
  956. err = 0;
  957. break;
  958. default:
  959. err = -ENOSYS;
  960. break;
  961. }
  962. sock_put(sk);
  963. return err;
  964. }
  965. /* Tunnel ioctl helper.
  966. *
  967. * Note the special handling for PPPIOCGL2TPSTATS below. If the ioctl data
  968. * specifies a session_id, the session ioctl handler is called. This allows an
  969. * application to retrieve session stats via a tunnel socket.
  970. */
  971. static int pppol2tp_tunnel_ioctl(struct l2tp_tunnel *tunnel,
  972. unsigned int cmd, unsigned long arg)
  973. {
  974. int err = 0;
  975. struct sock *sk;
  976. struct pppol2tp_ioc_stats stats;
  977. l2tp_dbg(tunnel, PPPOL2TP_MSG_CONTROL,
  978. "%s: pppol2tp_tunnel_ioctl(cmd=%#x, arg=%#lx)\n",
  979. tunnel->name, cmd, arg);
  980. sk = tunnel->sock;
  981. sock_hold(sk);
  982. switch (cmd) {
  983. case PPPIOCGL2TPSTATS:
  984. err = -ENXIO;
  985. if (!(sk->sk_state & PPPOX_CONNECTED))
  986. break;
  987. if (copy_from_user(&stats, (void __user *) arg,
  988. sizeof(stats))) {
  989. err = -EFAULT;
  990. break;
  991. }
  992. if (stats.session_id != 0) {
  993. /* resend to session ioctl handler */
  994. struct l2tp_session *session =
  995. l2tp_session_find(sock_net(sk), tunnel, stats.session_id);
  996. if (session != NULL)
  997. err = pppol2tp_session_ioctl(session, cmd, arg);
  998. else
  999. err = -EBADR;
  1000. break;
  1001. }
  1002. #ifdef CONFIG_XFRM
  1003. stats.using_ipsec = (sk->sk_policy[0] || sk->sk_policy[1]) ? 1 : 0;
  1004. #endif
  1005. pppol2tp_copy_stats(&stats, &tunnel->stats);
  1006. if (copy_to_user((void __user *) arg, &stats, sizeof(stats))) {
  1007. err = -EFAULT;
  1008. break;
  1009. }
  1010. l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: get L2TP stats\n",
  1011. tunnel->name);
  1012. err = 0;
  1013. break;
  1014. default:
  1015. err = -ENOSYS;
  1016. break;
  1017. }
  1018. sock_put(sk);
  1019. return err;
  1020. }
  1021. /* Main ioctl() handler.
  1022. * Dispatch to tunnel or session helpers depending on the socket.
  1023. */
  1024. static int pppol2tp_ioctl(struct socket *sock, unsigned int cmd,
  1025. unsigned long arg)
  1026. {
  1027. struct sock *sk = sock->sk;
  1028. struct l2tp_session *session;
  1029. struct l2tp_tunnel *tunnel;
  1030. struct pppol2tp_session *ps;
  1031. int err;
  1032. if (!sk)
  1033. return 0;
  1034. err = -EBADF;
  1035. if (sock_flag(sk, SOCK_DEAD) != 0)
  1036. goto end;
  1037. err = -ENOTCONN;
  1038. if ((sk->sk_user_data == NULL) ||
  1039. (!(sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND))))
  1040. goto end;
  1041. /* Get session context from the socket */
  1042. err = -EBADF;
  1043. session = pppol2tp_sock_to_session(sk);
  1044. if (session == NULL)
  1045. goto end;
  1046. /* Special case: if session's session_id is zero, treat ioctl as a
  1047. * tunnel ioctl
  1048. */
  1049. ps = l2tp_session_priv(session);
  1050. if ((session->session_id == 0) &&
  1051. (session->peer_session_id == 0)) {
  1052. err = -EBADF;
  1053. tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
  1054. if (tunnel == NULL)
  1055. goto end_put_sess;
  1056. err = pppol2tp_tunnel_ioctl(tunnel, cmd, arg);
  1057. sock_put(ps->tunnel_sock);
  1058. goto end_put_sess;
  1059. }
  1060. err = pppol2tp_session_ioctl(session, cmd, arg);
  1061. end_put_sess:
  1062. sock_put(sk);
  1063. end:
  1064. return err;
  1065. }
  1066. /*****************************************************************************
  1067. * setsockopt() / getsockopt() support.
  1068. *
  1069. * The PPPoX socket is created for L2TP sessions: tunnels have their own UDP
  1070. * sockets. In order to control kernel tunnel features, we allow userspace to
  1071. * create a special "tunnel" PPPoX socket which is used for control only.
  1072. * Tunnel PPPoX sockets have session_id == 0 and simply allow the user
  1073. * application to issue L2TP setsockopt(), getsockopt() and ioctl() calls.
  1074. *****************************************************************************/
  1075. /* Tunnel setsockopt() helper.
  1076. */
  1077. static int pppol2tp_tunnel_setsockopt(struct sock *sk,
  1078. struct l2tp_tunnel *tunnel,
  1079. int optname, int val)
  1080. {
  1081. int err = 0;
  1082. switch (optname) {
  1083. case PPPOL2TP_SO_DEBUG:
  1084. tunnel->debug = val;
  1085. l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: set debug=%x\n",
  1086. tunnel->name, tunnel->debug);
  1087. break;
  1088. default:
  1089. err = -ENOPROTOOPT;
  1090. break;
  1091. }
  1092. return err;
  1093. }
  1094. /* Session setsockopt helper.
  1095. */
  1096. static int pppol2tp_session_setsockopt(struct sock *sk,
  1097. struct l2tp_session *session,
  1098. int optname, int val)
  1099. {
  1100. int err = 0;
  1101. struct pppol2tp_session *ps = l2tp_session_priv(session);
  1102. switch (optname) {
  1103. case PPPOL2TP_SO_RECVSEQ:
  1104. if ((val != 0) && (val != 1)) {
  1105. err = -EINVAL;
  1106. break;
  1107. }
  1108. session->recv_seq = val ? -1 : 0;
  1109. l2tp_info(session, PPPOL2TP_MSG_CONTROL,
  1110. "%s: set recv_seq=%d\n",
  1111. session->name, session->recv_seq);
  1112. break;
  1113. case PPPOL2TP_SO_SENDSEQ:
  1114. if ((val != 0) && (val != 1)) {
  1115. err = -EINVAL;
  1116. break;
  1117. }
  1118. session->send_seq = val ? -1 : 0;
  1119. {
  1120. struct sock *ssk = ps->sock;
  1121. struct pppox_sock *po = pppox_sk(ssk);
  1122. po->chan.hdrlen = val ? PPPOL2TP_L2TP_HDR_SIZE_SEQ :
  1123. PPPOL2TP_L2TP_HDR_SIZE_NOSEQ;
  1124. }
  1125. l2tp_session_set_header_len(session, session->tunnel->version);
  1126. l2tp_info(session, PPPOL2TP_MSG_CONTROL,
  1127. "%s: set send_seq=%d\n",
  1128. session->name, session->send_seq);
  1129. break;
  1130. case PPPOL2TP_SO_LNSMODE:
  1131. if ((val != 0) && (val != 1)) {
  1132. err = -EINVAL;
  1133. break;
  1134. }
  1135. session->lns_mode = val ? -1 : 0;
  1136. l2tp_info(session, PPPOL2TP_MSG_CONTROL,
  1137. "%s: set lns_mode=%d\n",
  1138. session->name, session->lns_mode);
  1139. break;
  1140. case PPPOL2TP_SO_DEBUG:
  1141. session->debug = val;
  1142. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: set debug=%x\n",
  1143. session->name, session->debug);
  1144. break;
  1145. case PPPOL2TP_SO_REORDERTO:
  1146. session->reorder_timeout = msecs_to_jiffies(val);
  1147. l2tp_info(session, PPPOL2TP_MSG_CONTROL,
  1148. "%s: set reorder_timeout=%d\n",
  1149. session->name, session->reorder_timeout);
  1150. break;
  1151. default:
  1152. err = -ENOPROTOOPT;
  1153. break;
  1154. }
  1155. return err;
  1156. }
  1157. /* Main setsockopt() entry point.
  1158. * Does API checks, then calls either the tunnel or session setsockopt
  1159. * handler, according to whether the PPPoL2TP socket is a for a regular
  1160. * session or the special tunnel type.
  1161. */
  1162. static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
  1163. char __user *optval, unsigned int optlen)
  1164. {
  1165. struct sock *sk = sock->sk;
  1166. struct l2tp_session *session;
  1167. struct l2tp_tunnel *tunnel;
  1168. struct pppol2tp_session *ps;
  1169. int val;
  1170. int err;
  1171. if (level != SOL_PPPOL2TP)
  1172. return -EINVAL;
  1173. if (optlen < sizeof(int))
  1174. return -EINVAL;
  1175. if (get_user(val, (int __user *)optval))
  1176. return -EFAULT;
  1177. err = -ENOTCONN;
  1178. if (sk->sk_user_data == NULL)
  1179. goto end;
  1180. /* Get session context from the socket */
  1181. err = -EBADF;
  1182. session = pppol2tp_sock_to_session(sk);
  1183. if (session == NULL)
  1184. goto end;
  1185. /* Special case: if session_id == 0x0000, treat as operation on tunnel
  1186. */
  1187. ps = l2tp_session_priv(session);
  1188. if ((session->session_id == 0) &&
  1189. (session->peer_session_id == 0)) {
  1190. err = -EBADF;
  1191. tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
  1192. if (tunnel == NULL)
  1193. goto end_put_sess;
  1194. err = pppol2tp_tunnel_setsockopt(sk, tunnel, optname, val);
  1195. sock_put(ps->tunnel_sock);
  1196. } else
  1197. err = pppol2tp_session_setsockopt(sk, session, optname, val);
  1198. err = 0;
  1199. end_put_sess:
  1200. sock_put(sk);
  1201. end:
  1202. return err;
  1203. }
  1204. /* Tunnel getsockopt helper. Called with sock locked.
  1205. */
  1206. static int pppol2tp_tunnel_getsockopt(struct sock *sk,
  1207. struct l2tp_tunnel *tunnel,
  1208. int optname, int *val)
  1209. {
  1210. int err = 0;
  1211. switch (optname) {
  1212. case PPPOL2TP_SO_DEBUG:
  1213. *val = tunnel->debug;
  1214. l2tp_info(tunnel, PPPOL2TP_MSG_CONTROL, "%s: get debug=%x\n",
  1215. tunnel->name, tunnel->debug);
  1216. break;
  1217. default:
  1218. err = -ENOPROTOOPT;
  1219. break;
  1220. }
  1221. return err;
  1222. }
  1223. /* Session getsockopt helper. Called with sock locked.
  1224. */
  1225. static int pppol2tp_session_getsockopt(struct sock *sk,
  1226. struct l2tp_session *session,
  1227. int optname, int *val)
  1228. {
  1229. int err = 0;
  1230. switch (optname) {
  1231. case PPPOL2TP_SO_RECVSEQ:
  1232. *val = session->recv_seq;
  1233. l2tp_info(session, PPPOL2TP_MSG_CONTROL,
  1234. "%s: get recv_seq=%d\n", session->name, *val);
  1235. break;
  1236. case PPPOL2TP_SO_SENDSEQ:
  1237. *val = session->send_seq;
  1238. l2tp_info(session, PPPOL2TP_MSG_CONTROL,
  1239. "%s: get send_seq=%d\n", session->name, *val);
  1240. break;
  1241. case PPPOL2TP_SO_LNSMODE:
  1242. *val = session->lns_mode;
  1243. l2tp_info(session, PPPOL2TP_MSG_CONTROL,
  1244. "%s: get lns_mode=%d\n", session->name, *val);
  1245. break;
  1246. case PPPOL2TP_SO_DEBUG:
  1247. *val = session->debug;
  1248. l2tp_info(session, PPPOL2TP_MSG_CONTROL, "%s: get debug=%d\n",
  1249. session->name, *val);
  1250. break;
  1251. case PPPOL2TP_SO_REORDERTO:
  1252. *val = (int) jiffies_to_msecs(session->reorder_timeout);
  1253. l2tp_info(session, PPPOL2TP_MSG_CONTROL,
  1254. "%s: get reorder_timeout=%d\n", session->name, *val);
  1255. break;
  1256. default:
  1257. err = -ENOPROTOOPT;
  1258. }
  1259. return err;
  1260. }
  1261. /* Main getsockopt() entry point.
  1262. * Does API checks, then calls either the tunnel or session getsockopt
  1263. * handler, according to whether the PPPoX socket is a for a regular session
  1264. * or the special tunnel type.
  1265. */
  1266. static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
  1267. char __user *optval, int __user *optlen)
  1268. {
  1269. struct sock *sk = sock->sk;
  1270. struct l2tp_session *session;
  1271. struct l2tp_tunnel *tunnel;
  1272. int val, len;
  1273. int err;
  1274. struct pppol2tp_session *ps;
  1275. if (level != SOL_PPPOL2TP)
  1276. return -EINVAL;
  1277. if (get_user(len, optlen))
  1278. return -EFAULT;
  1279. len = min_t(unsigned int, len, sizeof(int));
  1280. if (len < 0)
  1281. return -EINVAL;
  1282. err = -ENOTCONN;
  1283. if (sk->sk_user_data == NULL)
  1284. goto end;
  1285. /* Get the session context */
  1286. err = -EBADF;
  1287. session = pppol2tp_sock_to_session(sk);
  1288. if (session == NULL)
  1289. goto end;
  1290. /* Special case: if session_id == 0x0000, treat as operation on tunnel */
  1291. ps = l2tp_session_priv(session);
  1292. if ((session->session_id == 0) &&
  1293. (session->peer_session_id == 0)) {
  1294. err = -EBADF;
  1295. tunnel = l2tp_sock_to_tunnel(ps->tunnel_sock);
  1296. if (tunnel == NULL)
  1297. goto end_put_sess;
  1298. err = pppol2tp_tunnel_getsockopt(sk, tunnel, optname, &val);
  1299. sock_put(ps->tunnel_sock);
  1300. } else
  1301. err = pppol2tp_session_getsockopt(sk, session, optname, &val);
  1302. err = -EFAULT;
  1303. if (put_user(len, optlen))
  1304. goto end_put_sess;
  1305. if (copy_to_user((void __user *) optval, &val, len))
  1306. goto end_put_sess;
  1307. err = 0;
  1308. end_put_sess:
  1309. sock_put(sk);
  1310. end:
  1311. return err;
  1312. }
  1313. /*****************************************************************************
  1314. * /proc filesystem for debug
  1315. * Since the original pppol2tp driver provided /proc/net/pppol2tp for
  1316. * L2TPv2, we dump only L2TPv2 tunnels and sessions here.
  1317. *****************************************************************************/
  1318. static unsigned int pppol2tp_net_id;
  1319. #ifdef CONFIG_PROC_FS
  1320. struct pppol2tp_seq_data {
  1321. struct seq_net_private p;
  1322. int tunnel_idx; /* current tunnel */
  1323. int session_idx; /* index of session within current tunnel */
  1324. struct l2tp_tunnel *tunnel;
  1325. struct l2tp_session *session; /* NULL means get next tunnel */
  1326. };
  1327. static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
  1328. {
  1329. for (;;) {
  1330. pd->tunnel = l2tp_tunnel_find_nth(net, pd->tunnel_idx);
  1331. pd->tunnel_idx++;
  1332. if (pd->tunnel == NULL)
  1333. break;
  1334. /* Ignore L2TPv3 tunnels */
  1335. if (pd->tunnel->version < 3)
  1336. break;
  1337. }
  1338. }
  1339. static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
  1340. {
  1341. pd->session = l2tp_session_get_nth(pd->tunnel, pd->session_idx, true);
  1342. pd->session_idx++;
  1343. if (pd->session == NULL) {
  1344. pd->session_idx = 0;
  1345. pppol2tp_next_tunnel(net, pd);
  1346. }
  1347. }
  1348. static void *pppol2tp_seq_start(struct seq_file *m, loff_t *offs)
  1349. {
  1350. struct pppol2tp_seq_data *pd = SEQ_START_TOKEN;
  1351. loff_t pos = *offs;
  1352. struct net *net;
  1353. if (!pos)
  1354. goto out;
  1355. BUG_ON(m->private == NULL);
  1356. pd = m->private;
  1357. net = seq_file_net(m);
  1358. if (pd->tunnel == NULL)
  1359. pppol2tp_next_tunnel(net, pd);
  1360. else
  1361. pppol2tp_next_session(net, pd);
  1362. /* NULL tunnel and session indicates end of list */
  1363. if ((pd->tunnel == NULL) && (pd->session == NULL))
  1364. pd = NULL;
  1365. out:
  1366. return pd;
  1367. }
  1368. static void *pppol2tp_seq_next(struct seq_file *m, void *v, loff_t *pos)
  1369. {
  1370. (*pos)++;
  1371. return NULL;
  1372. }
  1373. static void pppol2tp_seq_stop(struct seq_file *p, void *v)
  1374. {
  1375. /* nothing to do */
  1376. }
  1377. static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
  1378. {
  1379. struct l2tp_tunnel *tunnel = v;
  1380. seq_printf(m, "\nTUNNEL '%s', %c %d\n",
  1381. tunnel->name,
  1382. (tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
  1383. atomic_read(&tunnel->ref_count) - 1);
  1384. seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n",
  1385. tunnel->debug,
  1386. atomic_long_read(&tunnel->stats.tx_packets),
  1387. atomic_long_read(&tunnel->stats.tx_bytes),
  1388. atomic_long_read(&tunnel->stats.tx_errors),
  1389. atomic_long_read(&tunnel->stats.rx_packets),
  1390. atomic_long_read(&tunnel->stats.rx_bytes),
  1391. atomic_long_read(&tunnel->stats.rx_errors));
  1392. }
  1393. static void pppol2tp_seq_session_show(struct seq_file *m, void *v)
  1394. {
  1395. struct l2tp_session *session = v;
  1396. struct l2tp_tunnel *tunnel = session->tunnel;
  1397. struct pppol2tp_session *ps = l2tp_session_priv(session);
  1398. struct pppox_sock *po = pppox_sk(ps->sock);
  1399. u32 ip = 0;
  1400. u16 port = 0;
  1401. if (tunnel->sock) {
  1402. struct inet_sock *inet = inet_sk(tunnel->sock);
  1403. ip = ntohl(inet->inet_saddr);
  1404. port = ntohs(inet->inet_sport);
  1405. }
  1406. seq_printf(m, " SESSION '%s' %08X/%d %04X/%04X -> "
  1407. "%04X/%04X %d %c\n",
  1408. session->name, ip, port,
  1409. tunnel->tunnel_id,
  1410. session->session_id,
  1411. tunnel->peer_tunnel_id,
  1412. session->peer_session_id,
  1413. ps->sock->sk_state,
  1414. (session == ps->sock->sk_user_data) ?
  1415. 'Y' : 'N');
  1416. seq_printf(m, " %d/%d/%c/%c/%s %08x %u\n",
  1417. session->mtu, session->mru,
  1418. session->recv_seq ? 'R' : '-',
  1419. session->send_seq ? 'S' : '-',
  1420. session->lns_mode ? "LNS" : "LAC",
  1421. session->debug,
  1422. jiffies_to_msecs(session->reorder_timeout));
  1423. seq_printf(m, " %hu/%hu %ld/%ld/%ld %ld/%ld/%ld\n",
  1424. session->nr, session->ns,
  1425. atomic_long_read(&session->stats.tx_packets),
  1426. atomic_long_read(&session->stats.tx_bytes),
  1427. atomic_long_read(&session->stats.tx_errors),
  1428. atomic_long_read(&session->stats.rx_packets),
  1429. atomic_long_read(&session->stats.rx_bytes),
  1430. atomic_long_read(&session->stats.rx_errors));
  1431. if (po)
  1432. seq_printf(m, " interface %s\n", ppp_dev_name(&po->chan));
  1433. }
  1434. static int pppol2tp_seq_show(struct seq_file *m, void *v)
  1435. {
  1436. struct pppol2tp_seq_data *pd = v;
  1437. /* display header on line 1 */
  1438. if (v == SEQ_START_TOKEN) {
  1439. seq_puts(m, "PPPoL2TP driver info, " PPPOL2TP_DRV_VERSION "\n");
  1440. seq_puts(m, "TUNNEL name, user-data-ok session-count\n");
  1441. seq_puts(m, " debug tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
  1442. seq_puts(m, " SESSION name, addr/port src-tid/sid "
  1443. "dest-tid/sid state user-data-ok\n");
  1444. seq_puts(m, " mtu/mru/rcvseq/sendseq/lns debug reorderto\n");
  1445. seq_puts(m, " nr/ns tx-pkts/bytes/errs rx-pkts/bytes/errs\n");
  1446. goto out;
  1447. }
  1448. /* Show the tunnel or session context.
  1449. */
  1450. if (!pd->session) {
  1451. pppol2tp_seq_tunnel_show(m, pd->tunnel);
  1452. } else {
  1453. pppol2tp_seq_session_show(m, pd->session);
  1454. if (pd->session->deref)
  1455. pd->session->deref(pd->session);
  1456. l2tp_session_dec_refcount(pd->session);
  1457. }
  1458. out:
  1459. return 0;
  1460. }
  1461. static const struct seq_operations pppol2tp_seq_ops = {
  1462. .start = pppol2tp_seq_start,
  1463. .next = pppol2tp_seq_next,
  1464. .stop = pppol2tp_seq_stop,
  1465. .show = pppol2tp_seq_show,
  1466. };
  1467. /* Called when our /proc file is opened. We allocate data for use when
  1468. * iterating our tunnel / session contexts and store it in the private
  1469. * data of the seq_file.
  1470. */
  1471. static int pppol2tp_proc_open(struct inode *inode, struct file *file)
  1472. {
  1473. return seq_open_net(inode, file, &pppol2tp_seq_ops,
  1474. sizeof(struct pppol2tp_seq_data));
  1475. }
  1476. static const struct file_operations pppol2tp_proc_fops = {
  1477. .owner = THIS_MODULE,
  1478. .open = pppol2tp_proc_open,
  1479. .read = seq_read,
  1480. .llseek = seq_lseek,
  1481. .release = seq_release_net,
  1482. };
  1483. #endif /* CONFIG_PROC_FS */
  1484. /*****************************************************************************
  1485. * Network namespace
  1486. *****************************************************************************/
  1487. static __net_init int pppol2tp_init_net(struct net *net)
  1488. {
  1489. struct proc_dir_entry *pde;
  1490. int err = 0;
  1491. pde = proc_create("pppol2tp", S_IRUGO, net->proc_net,
  1492. &pppol2tp_proc_fops);
  1493. if (!pde) {
  1494. err = -ENOMEM;
  1495. goto out;
  1496. }
  1497. out:
  1498. return err;
  1499. }
  1500. static __net_exit void pppol2tp_exit_net(struct net *net)
  1501. {
  1502. remove_proc_entry("pppol2tp", net->proc_net);
  1503. }
  1504. static struct pernet_operations pppol2tp_net_ops = {
  1505. .init = pppol2tp_init_net,
  1506. .exit = pppol2tp_exit_net,
  1507. .id = &pppol2tp_net_id,
  1508. };
  1509. /*****************************************************************************
  1510. * Init and cleanup
  1511. *****************************************************************************/
  1512. static const struct proto_ops pppol2tp_ops = {
  1513. .family = AF_PPPOX,
  1514. .owner = THIS_MODULE,
  1515. .release = pppol2tp_release,
  1516. .bind = sock_no_bind,
  1517. .connect = pppol2tp_connect,
  1518. .socketpair = sock_no_socketpair,
  1519. .accept = sock_no_accept,
  1520. .getname = pppol2tp_getname,
  1521. .poll = datagram_poll,
  1522. .listen = sock_no_listen,
  1523. .shutdown = sock_no_shutdown,
  1524. .setsockopt = pppol2tp_setsockopt,
  1525. .getsockopt = pppol2tp_getsockopt,
  1526. .sendmsg = pppol2tp_sendmsg,
  1527. .recvmsg = pppol2tp_recvmsg,
  1528. .mmap = sock_no_mmap,
  1529. .ioctl = pppox_ioctl,
  1530. };
  1531. static const struct pppox_proto pppol2tp_proto = {
  1532. .create = pppol2tp_create,
  1533. .ioctl = pppol2tp_ioctl,
  1534. .owner = THIS_MODULE,
  1535. };
  1536. #ifdef CONFIG_L2TP_V3
  1537. static const struct l2tp_nl_cmd_ops pppol2tp_nl_cmd_ops = {
  1538. .session_create = pppol2tp_session_create,
  1539. .session_delete = l2tp_session_delete,
  1540. };
  1541. #endif /* CONFIG_L2TP_V3 */
  1542. static int __init pppol2tp_init(void)
  1543. {
  1544. int err;
  1545. err = register_pernet_device(&pppol2tp_net_ops);
  1546. if (err)
  1547. goto out;
  1548. err = proto_register(&pppol2tp_sk_proto, 0);
  1549. if (err)
  1550. goto out_unregister_pppol2tp_pernet;
  1551. err = register_pppox_proto(PX_PROTO_OL2TP, &pppol2tp_proto);
  1552. if (err)
  1553. goto out_unregister_pppol2tp_proto;
  1554. #ifdef CONFIG_L2TP_V3
  1555. err = l2tp_nl_register_ops(L2TP_PWTYPE_PPP, &pppol2tp_nl_cmd_ops);
  1556. if (err)
  1557. goto out_unregister_pppox;
  1558. #endif
  1559. pr_info("PPPoL2TP kernel driver, %s\n", PPPOL2TP_DRV_VERSION);
  1560. out:
  1561. return err;
  1562. #ifdef CONFIG_L2TP_V3
  1563. out_unregister_pppox:
  1564. unregister_pppox_proto(PX_PROTO_OL2TP);
  1565. #endif
  1566. out_unregister_pppol2tp_proto:
  1567. proto_unregister(&pppol2tp_sk_proto);
  1568. out_unregister_pppol2tp_pernet:
  1569. unregister_pernet_device(&pppol2tp_net_ops);
  1570. goto out;
  1571. }
  1572. static void __exit pppol2tp_exit(void)
  1573. {
  1574. #ifdef CONFIG_L2TP_V3
  1575. l2tp_nl_unregister_ops(L2TP_PWTYPE_PPP);
  1576. #endif
  1577. unregister_pppox_proto(PX_PROTO_OL2TP);
  1578. proto_unregister(&pppol2tp_sk_proto);
  1579. unregister_pernet_device(&pppol2tp_net_ops);
  1580. }
  1581. module_init(pppol2tp_init);
  1582. module_exit(pppol2tp_exit);
  1583. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  1584. MODULE_DESCRIPTION("PPP over L2TP over UDP");
  1585. MODULE_LICENSE("GPL");
  1586. MODULE_VERSION(PPPOL2TP_DRV_VERSION);
  1587. MODULE_ALIAS("pppox-proto-" __stringify(PX_PROTO_OL2TP));
  1588. MODULE_ALIAS_L2TP_PWTYPE(7);