l2tp.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. This document describes how to use the kernel's L2TP drivers to
  2. provide L2TP functionality. L2TP is a protocol that tunnels one or
  3. more sessions over an IP tunnel. It is commonly used for VPNs
  4. (L2TP/IPSec) and by ISPs to tunnel subscriber PPP sessions over an IP
  5. network infrastructure. With L2TPv3, it is also useful as a Layer-2
  6. tunneling infrastructure.
  7. Features
  8. ========
  9. L2TPv2 (PPP over L2TP (UDP tunnels)).
  10. L2TPv3 ethernet pseudowires.
  11. L2TPv3 PPP pseudowires.
  12. L2TPv3 IP encapsulation.
  13. Netlink sockets for L2TPv3 configuration management.
  14. History
  15. =======
  16. The original pppol2tp driver was introduced in 2.6.23 and provided
  17. L2TPv2 functionality (rfc2661). L2TPv2 is used to tunnel one or more PPP
  18. sessions over a UDP tunnel.
  19. L2TPv3 (rfc3931) changes the protocol to allow different frame types
  20. to be passed over an L2TP tunnel by moving the PPP-specific parts of
  21. the protocol out of the core L2TP packet headers. Each frame type is
  22. known as a pseudowire type. Ethernet, PPP, HDLC, Frame Relay and ATM
  23. pseudowires for L2TP are defined in separate RFC standards. Another
  24. change for L2TPv3 is that it can be carried directly over IP with no
  25. UDP header (UDP is optional). It is also possible to create static
  26. unmanaged L2TPv3 tunnels manually without a control protocol
  27. (userspace daemon) to manage them.
  28. To support L2TPv3, the original pppol2tp driver was split up to
  29. separate the L2TP and PPP functionality. Existing L2TPv2 userspace
  30. apps should be unaffected as the original pppol2tp sockets API is
  31. retained. L2TPv3, however, uses netlink to manage L2TPv3 tunnels and
  32. sessions.
  33. Design
  34. ======
  35. The L2TP protocol separates control and data frames. The L2TP kernel
  36. drivers handle only L2TP data frames; control frames are always
  37. handled by userspace. L2TP control frames carry messages between L2TP
  38. clients/servers and are used to setup / teardown tunnels and
  39. sessions. An L2TP client or server is implemented in userspace.
  40. Each L2TP tunnel is implemented using a UDP or L2TPIP socket; L2TPIP
  41. provides L2TPv3 IP encapsulation (no UDP) and is implemented using a
  42. new l2tpip socket family. The tunnel socket is typically created by
  43. userspace, though for unmanaged L2TPv3 tunnels, the socket can also be
  44. created by the kernel. Each L2TP session (pseudowire) gets a network
  45. interface instance. In the case of PPP, these interfaces are created
  46. indirectly by pppd using a pppol2tp socket. In the case of ethernet,
  47. the netdevice is created upon a netlink request to create an L2TPv3
  48. ethernet pseudowire.
  49. For PPP, the PPPoL2TP driver, net/l2tp/l2tp_ppp.c, provides a
  50. mechanism by which PPP frames carried through an L2TP session are
  51. passed through the kernel's PPP subsystem. The standard PPP daemon,
  52. pppd, handles all PPP interaction with the peer. PPP network
  53. interfaces are created for each local PPP endpoint. The kernel's PPP
  54. subsystem arranges for PPP control frames to be delivered to pppd,
  55. while data frames are forwarded as usual.
  56. For ethernet, the L2TPETH driver, net/l2tp/l2tp_eth.c, implements a
  57. netdevice driver, managing virtual ethernet devices, one per
  58. pseudowire. These interfaces can be managed using standard Linux tools
  59. such as "ip" and "ifconfig". If only IP frames are passed over the
  60. tunnel, the interface can be given an IP addresses of itself and its
  61. peer. If non-IP frames are to be passed over the tunnel, the interface
  62. can be added to a bridge using brctl. All L2TP datapath protocol
  63. functions are handled by the L2TP core driver.
  64. Each tunnel and session within a tunnel is assigned a unique tunnel_id
  65. and session_id. These ids are carried in the L2TP header of every
  66. control and data packet. (Actually, in L2TPv3, the tunnel_id isn't
  67. present in data frames - it is inferred from the IP connection on
  68. which the packet was received.) The L2TP driver uses the ids to lookup
  69. internal tunnel and/or session contexts to determine how to handle the
  70. packet. Zero tunnel / session ids are treated specially - zero ids are
  71. never assigned to tunnels or sessions in the network. In the driver,
  72. the tunnel context keeps a reference to the tunnel UDP or L2TPIP
  73. socket. The session context holds data that lets the driver interface
  74. to the kernel's network frame type subsystems, i.e. PPP, ethernet.
  75. Userspace Programming
  76. =====================
  77. For L2TPv2, there are a number of requirements on the userspace L2TP
  78. daemon in order to use the pppol2tp driver.
  79. 1. Use a UDP socket per tunnel.
  80. 2. Create a single PPPoL2TP socket per tunnel bound to a special null
  81. session id. This is used only for communicating with the driver but
  82. must remain open while the tunnel is active. Opening this tunnel
  83. management socket causes the driver to mark the tunnel socket as an
  84. L2TP UDP encapsulation socket and flags it for use by the
  85. referenced tunnel id. This hooks up the UDP receive path via
  86. udp_encap_rcv() in net/ipv4/udp.c. PPP data frames are never passed
  87. in this special PPPoX socket.
  88. 3. Create a PPPoL2TP socket per L2TP session. This is typically done
  89. by starting pppd with the pppol2tp plugin and appropriate
  90. arguments. A PPPoL2TP tunnel management socket (Step 2) must be
  91. created before the first PPPoL2TP session socket is created.
  92. When creating PPPoL2TP sockets, the application provides information
  93. to the driver about the socket in a socket connect() call. Source and
  94. destination tunnel and session ids are provided, as well as the file
  95. descriptor of a UDP socket. See struct pppol2tp_addr in
  96. include/linux/if_pppol2tp.h. Note that zero tunnel / session ids are
  97. treated specially. When creating the per-tunnel PPPoL2TP management
  98. socket in Step 2 above, zero source and destination session ids are
  99. specified, which tells the driver to prepare the supplied UDP file
  100. descriptor for use as an L2TP tunnel socket.
  101. Userspace may control behavior of the tunnel or session using
  102. setsockopt and ioctl on the PPPoX socket. The following socket
  103. options are supported:-
  104. DEBUG - bitmask of debug message categories. See below.
  105. SENDSEQ - 0 => don't send packets with sequence numbers
  106. 1 => send packets with sequence numbers
  107. RECVSEQ - 0 => receive packet sequence numbers are optional
  108. 1 => drop receive packets without sequence numbers
  109. LNSMODE - 0 => act as LAC.
  110. 1 => act as LNS.
  111. REORDERTO - reorder timeout (in millisecs). If 0, don't try to reorder.
  112. Only the DEBUG option is supported by the special tunnel management
  113. PPPoX socket.
  114. In addition to the standard PPP ioctls, a PPPIOCGL2TPSTATS is provided
  115. to retrieve tunnel and session statistics from the kernel using the
  116. PPPoX socket of the appropriate tunnel or session.
  117. For L2TPv3, userspace must use the netlink API defined in
  118. include/linux/l2tp.h to manage tunnel and session contexts. The
  119. general procedure to create a new L2TP tunnel with one session is:-
  120. 1. Open a GENL socket using L2TP_GENL_NAME for configuring the kernel
  121. using netlink.
  122. 2. Create a UDP or L2TPIP socket for the tunnel.
  123. 3. Create a new L2TP tunnel using a L2TP_CMD_TUNNEL_CREATE
  124. request. Set attributes according to desired tunnel parameters,
  125. referencing the UDP or L2TPIP socket created in the previous step.
  126. 4. Create a new L2TP session in the tunnel using a
  127. L2TP_CMD_SESSION_CREATE request.
  128. The tunnel and all of its sessions are closed when the tunnel socket
  129. is closed. The netlink API may also be used to delete sessions and
  130. tunnels. Configuration and status info may be set or read using netlink.
  131. The L2TP driver also supports static (unmanaged) L2TPv3 tunnels. These
  132. are where there is no L2TP control message exchange with the peer to
  133. setup the tunnel; the tunnel is configured manually at each end of the
  134. tunnel. There is no need for an L2TP userspace application in this
  135. case -- the tunnel socket is created by the kernel and configured
  136. using parameters sent in the L2TP_CMD_TUNNEL_CREATE netlink
  137. request. The "ip" utility of iproute2 has commands for managing static
  138. L2TPv3 tunnels; do "ip l2tp help" for more information.
  139. Debugging
  140. =========
  141. The driver supports a flexible debug scheme where kernel trace
  142. messages may be optionally enabled per tunnel and per session. Care is
  143. needed when debugging a live system since the messages are not
  144. rate-limited and a busy system could be swamped. Userspace uses
  145. setsockopt on the PPPoX socket to set a debug mask.
  146. The following debug mask bits are available:
  147. PPPOL2TP_MSG_DEBUG verbose debug (if compiled in)
  148. PPPOL2TP_MSG_CONTROL userspace - kernel interface
  149. PPPOL2TP_MSG_SEQ sequence numbers handling
  150. PPPOL2TP_MSG_DATA data packets
  151. If enabled, files under a l2tp debugfs directory can be used to dump
  152. kernel state about L2TP tunnels and sessions. To access it, the
  153. debugfs filesystem must first be mounted.
  154. # mount -t debugfs debugfs /debug
  155. Files under the l2tp directory can then be accessed.
  156. # cat /debug/l2tp/tunnels
  157. The debugfs files should not be used by applications to obtain L2TP
  158. state information because the file format is subject to change. It is
  159. implemented to provide extra debug information to help diagnose
  160. problems.) Users should use the netlink API.
  161. /proc/net/pppol2tp is also provided for backwards compatibility with
  162. the original pppol2tp driver. It lists information about L2TPv2
  163. tunnels and sessions only. Its use is discouraged.
  164. Unmanaged L2TPv3 Tunnels
  165. ========================
  166. Some commercial L2TP products support unmanaged L2TPv3 ethernet
  167. tunnels, where there is no L2TP control protocol; tunnels are
  168. configured at each side manually. New commands are available in
  169. iproute2's ip utility to support this.
  170. To create an L2TPv3 ethernet pseudowire between local host 192.168.1.1
  171. and peer 192.168.1.2, using IP addresses 10.5.1.1 and 10.5.1.2 for the
  172. tunnel endpoints:-
  173. # ip l2tp add tunnel tunnel_id 1 peer_tunnel_id 1 udp_sport 5000 \
  174. udp_dport 5000 encap udp local 192.168.1.1 remote 192.168.1.2
  175. # ip l2tp add session tunnel_id 1 session_id 1 peer_session_id 1
  176. # ip -s -d show dev l2tpeth0
  177. # ip addr add 10.5.1.2/32 peer 10.5.1.1/32 dev l2tpeth0
  178. # ip li set dev l2tpeth0 up
  179. Choose IP addresses to be the address of a local IP interface and that
  180. of the remote system. The IP addresses of the l2tpeth0 interface can be
  181. anything suitable.
  182. Repeat the above at the peer, with ports, tunnel/session ids and IP
  183. addresses reversed. The tunnel and session IDs can be any non-zero
  184. 32-bit number, but the values must be reversed at the peer.
  185. Host 1 Host2
  186. udp_sport=5000 udp_sport=5001
  187. udp_dport=5001 udp_dport=5000
  188. tunnel_id=42 tunnel_id=45
  189. peer_tunnel_id=45 peer_tunnel_id=42
  190. session_id=128 session_id=5196755
  191. peer_session_id=5196755 peer_session_id=128
  192. When done at both ends of the tunnel, it should be possible to send
  193. data over the network. e.g.
  194. # ping 10.5.1.1
  195. Sample Userspace Code
  196. =====================
  197. 1. Create tunnel management PPPoX socket
  198. kernel_fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
  199. if (kernel_fd >= 0) {
  200. struct sockaddr_pppol2tp sax;
  201. struct sockaddr_in const *peer_addr;
  202. peer_addr = l2tp_tunnel_get_peer_addr(tunnel);
  203. memset(&sax, 0, sizeof(sax));
  204. sax.sa_family = AF_PPPOX;
  205. sax.sa_protocol = PX_PROTO_OL2TP;
  206. sax.pppol2tp.fd = udp_fd; /* fd of tunnel UDP socket */
  207. sax.pppol2tp.addr.sin_addr.s_addr = peer_addr->sin_addr.s_addr;
  208. sax.pppol2tp.addr.sin_port = peer_addr->sin_port;
  209. sax.pppol2tp.addr.sin_family = AF_INET;
  210. sax.pppol2tp.s_tunnel = tunnel_id;
  211. sax.pppol2tp.s_session = 0; /* special case: mgmt socket */
  212. sax.pppol2tp.d_tunnel = 0;
  213. sax.pppol2tp.d_session = 0; /* special case: mgmt socket */
  214. if(connect(kernel_fd, (struct sockaddr *)&sax, sizeof(sax) ) < 0 ) {
  215. perror("connect failed");
  216. result = -errno;
  217. goto err;
  218. }
  219. }
  220. 2. Create session PPPoX data socket
  221. struct sockaddr_pppol2tp sax;
  222. int fd;
  223. /* Note, the target socket must be bound already, else it will not be ready */
  224. sax.sa_family = AF_PPPOX;
  225. sax.sa_protocol = PX_PROTO_OL2TP;
  226. sax.pppol2tp.fd = tunnel_fd;
  227. sax.pppol2tp.addr.sin_addr.s_addr = addr->sin_addr.s_addr;
  228. sax.pppol2tp.addr.sin_port = addr->sin_port;
  229. sax.pppol2tp.addr.sin_family = AF_INET;
  230. sax.pppol2tp.s_tunnel = tunnel_id;
  231. sax.pppol2tp.s_session = session_id;
  232. sax.pppol2tp.d_tunnel = peer_tunnel_id;
  233. sax.pppol2tp.d_session = peer_session_id;
  234. /* session_fd is the fd of the session's PPPoL2TP socket.
  235. * tunnel_fd is the fd of the tunnel UDP socket.
  236. */
  237. fd = connect(session_fd, (struct sockaddr *)&sax, sizeof(sax));
  238. if (fd < 0 ) {
  239. return -errno;
  240. }
  241. return 0;
  242. Internal Implementation
  243. =======================
  244. The driver keeps a struct l2tp_tunnel context per L2TP tunnel and a
  245. struct l2tp_session context for each session. The l2tp_tunnel is
  246. always associated with a UDP or L2TP/IP socket and keeps a list of
  247. sessions in the tunnel. The l2tp_session context keeps kernel state
  248. about the session. It has private data which is used for data specific
  249. to the session type. With L2TPv2, the session always carried PPP
  250. traffic. With L2TPv3, the session can also carry ethernet frames
  251. (ethernet pseudowire) or other data types such as ATM, HDLC or Frame
  252. Relay.
  253. When a tunnel is first opened, the reference count on the socket is
  254. increased using sock_hold(). This ensures that the kernel socket
  255. cannot be removed while L2TP's data structures reference it.
  256. Some L2TP sessions also have a socket (PPP pseudowires) while others
  257. do not (ethernet pseudowires). We can't use the socket reference count
  258. as the reference count for session contexts. The L2TP implementation
  259. therefore has its own internal reference counts on the session
  260. contexts.
  261. To Do
  262. =====
  263. Add L2TP tunnel switching support. This would route tunneled traffic
  264. from one L2TP tunnel into another. Specified in
  265. http://tools.ietf.org/html/draft-ietf-l2tpext-tunnel-switching-08
  266. Add L2TPv3 VLAN pseudowire support.
  267. Add L2TPv3 IP pseudowire support.
  268. Add L2TPv3 ATM pseudowire support.
  269. Miscellaneous
  270. =============
  271. The L2TP drivers were developed as part of the OpenL2TP project by
  272. Katalix Systems Ltd. OpenL2TP is a full-featured L2TP client / server,
  273. designed from the ground up to have the L2TP datapath in the
  274. kernel. The project also implemented the pppol2tp plugin for pppd
  275. which allows pppd to use the kernel driver. Details can be found at
  276. http://www.openl2tp.org.