l2tp_ppp.c 42 KB

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