l2tp_ppp.c 41 KB

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