l2tp.txt 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. This brief document describes how to use the kernel's PPPoL2TP driver
  2. to provide L2TP functionality. L2TP is a protocol that tunnels one or
  3. more PPP sessions over a UDP tunnel. It is commonly used for VPNs
  4. (L2TP/IPSec) and by ISPs to tunnel subscriber PPP sessions over an IP
  5. network infrastructure.
  6. Design
  7. ======
  8. The PPPoL2TP driver, drivers/net/pppol2tp.c, provides a mechanism by
  9. which PPP frames carried through an L2TP session are passed through
  10. the kernel's PPP subsystem. The standard PPP daemon, pppd, handles all
  11. PPP interaction with the peer. PPP network interfaces are created for
  12. each local PPP endpoint.
  13. The L2TP protocol http://www.faqs.org/rfcs/rfc2661.html defines L2TP
  14. control and data frames. L2TP control frames carry messages between
  15. L2TP clients/servers and are used to setup / teardown tunnels and
  16. sessions. An L2TP client or server is implemented in userspace and
  17. will use a regular UDP socket per tunnel. L2TP data frames carry PPP
  18. frames, which may be PPP control or PPP data. The kernel's PPP
  19. subsystem arranges for PPP control frames to be delivered to pppd,
  20. while data frames are forwarded as usual.
  21. Each tunnel and session within a tunnel is assigned a unique tunnel_id
  22. and session_id. These ids are carried in the L2TP header of every
  23. control and data packet. The pppol2tp driver uses them to lookup
  24. internal tunnel and/or session contexts. Zero tunnel / session ids are
  25. treated specially - zero ids are never assigned to tunnels or sessions
  26. in the network. In the driver, the tunnel context keeps a pointer to
  27. the tunnel UDP socket. The session context keeps a pointer to the
  28. PPPoL2TP socket, as well as other data that lets the driver interface
  29. to the kernel PPP subsystem.
  30. Note that the pppol2tp kernel driver handles only L2TP data frames;
  31. L2TP control frames are simply passed up to userspace in the UDP
  32. tunnel socket. The kernel handles all datapath aspects of the
  33. protocol, including data packet resequencing (if enabled).
  34. There are a number of requirements on the userspace L2TP daemon in
  35. order to use the pppol2tp driver.
  36. 1. Use a UDP socket per tunnel.
  37. 2. Create a single PPPoL2TP socket per tunnel bound to a special null
  38. session id. This is used only for communicating with the driver but
  39. must remain open while the tunnel is active. Opening this tunnel
  40. management socket causes the driver to mark the tunnel socket as an
  41. L2TP UDP encapsulation socket and flags it for use by the
  42. referenced tunnel id. This hooks up the UDP receive path via
  43. udp_encap_rcv() in net/ipv4/udp.c. PPP data frames are never passed
  44. in this special PPPoX socket.
  45. 3. Create a PPPoL2TP socket per L2TP session. This is typically done
  46. by starting pppd with the pppol2tp plugin and appropriate
  47. arguments. A PPPoL2TP tunnel management socket (Step 2) must be
  48. created before the first PPPoL2TP session socket is created.
  49. When creating PPPoL2TP sockets, the application provides information
  50. to the driver about the socket in a socket connect() call. Source and
  51. destination tunnel and session ids are provided, as well as the file
  52. descriptor of a UDP socket. See struct pppol2tp_addr in
  53. include/linux/if_ppp.h. Note that zero tunnel / session ids are
  54. treated specially. When creating the per-tunnel PPPoL2TP management
  55. socket in Step 2 above, zero source and destination session ids are
  56. specified, which tells the driver to prepare the supplied UDP file
  57. descriptor for use as an L2TP tunnel socket.
  58. Userspace may control behavior of the tunnel or session using
  59. setsockopt and ioctl on the PPPoX socket. The following socket
  60. options are supported:-
  61. DEBUG - bitmask of debug message categories. See below.
  62. SENDSEQ - 0 => don't send packets with sequence numbers
  63. 1 => send packets with sequence numbers
  64. RECVSEQ - 0 => receive packet sequence numbers are optional
  65. 1 => drop receive packets without sequence numbers
  66. LNSMODE - 0 => act as LAC.
  67. 1 => act as LNS.
  68. REORDERTO - reorder timeout (in millisecs). If 0, don't try to reorder.
  69. Only the DEBUG option is supported by the special tunnel management
  70. PPPoX socket.
  71. In addition to the standard PPP ioctls, a PPPIOCGL2TPSTATS is provided
  72. to retrieve tunnel and session statistics from the kernel using the
  73. PPPoX socket of the appropriate tunnel or session.
  74. Debugging
  75. =========
  76. The driver supports a flexible debug scheme where kernel trace
  77. messages may be optionally enabled per tunnel and per session. Care is
  78. needed when debugging a live system since the messages are not
  79. rate-limited and a busy system could be swamped. Userspace uses
  80. setsockopt on the PPPoX socket to set a debug mask.
  81. The following debug mask bits are available:
  82. PPPOL2TP_MSG_DEBUG verbose debug (if compiled in)
  83. PPPOL2TP_MSG_CONTROL userspace - kernel interface
  84. PPPOL2TP_MSG_SEQ sequence numbers handling
  85. PPPOL2TP_MSG_DATA data packets
  86. Sample Userspace Code
  87. =====================
  88. 1. Create tunnel management PPPoX socket
  89. kernel_fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
  90. if (kernel_fd >= 0) {
  91. struct sockaddr_pppol2tp sax;
  92. struct sockaddr_in const *peer_addr;
  93. peer_addr = l2tp_tunnel_get_peer_addr(tunnel);
  94. memset(&sax, 0, sizeof(sax));
  95. sax.sa_family = AF_PPPOX;
  96. sax.sa_protocol = PX_PROTO_OL2TP;
  97. sax.pppol2tp.fd = udp_fd; /* fd of tunnel UDP socket */
  98. sax.pppol2tp.addr.sin_addr.s_addr = peer_addr->sin_addr.s_addr;
  99. sax.pppol2tp.addr.sin_port = peer_addr->sin_port;
  100. sax.pppol2tp.addr.sin_family = AF_INET;
  101. sax.pppol2tp.s_tunnel = tunnel_id;
  102. sax.pppol2tp.s_session = 0; /* special case: mgmt socket */
  103. sax.pppol2tp.d_tunnel = 0;
  104. sax.pppol2tp.d_session = 0; /* special case: mgmt socket */
  105. if(connect(kernel_fd, (struct sockaddr *)&sax, sizeof(sax) ) < 0 ) {
  106. perror("connect failed");
  107. result = -errno;
  108. goto err;
  109. }
  110. }
  111. 2. Create session PPPoX data socket
  112. struct sockaddr_pppol2tp sax;
  113. int fd;
  114. /* Note, the target socket must be bound already, else it will not be ready */
  115. sax.sa_family = AF_PPPOX;
  116. sax.sa_protocol = PX_PROTO_OL2TP;
  117. sax.pppol2tp.fd = tunnel_fd;
  118. sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr;
  119. sax.pppol2tp.addr.sin_port = addr->sin_port;
  120. sax.pppol2tp.addr.sin_family = AF_INET;
  121. sax.pppol2tp.s_tunnel = tunnel_id;
  122. sax.pppol2tp.s_session = session_id;
  123. sax.pppol2tp.d_tunnel = peer_tunnel_id;
  124. sax.pppol2tp.d_session = peer_session_id;
  125. /* session_fd is the fd of the session's PPPoL2TP socket.
  126. * tunnel_fd is the fd of the tunnel UDP socket.
  127. */
  128. fd = connect(session_fd, (struct sockaddr *)&sax, sizeof(sax));
  129. if (fd < 0 ) {
  130. return -errno;
  131. }
  132. return 0;
  133. Miscellanous
  134. ============
  135. The PPPoL2TP driver was developed as part of the OpenL2TP project by
  136. Katalix Systems Ltd. OpenL2TP is a full-featured L2TP client / server,
  137. designed from the ground up to have the L2TP datapath in the
  138. kernel. The project also implemented the pppol2tp plugin for pppd
  139. which allows pppd to use the kernel driver. Details can be found at
  140. http://openl2tp.sourceforge.net.