l2tp_ppp.c 43 KB

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