ppp_generic.txt 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. PPP Generic Driver and Channel Interface
  2. ----------------------------------------
  3. Paul Mackerras
  4. paulus@samba.org
  5. 7 Feb 2002
  6. The generic PPP driver in linux-2.4 provides an implementation of the
  7. functionality which is of use in any PPP implementation, including:
  8. * the network interface unit (ppp0 etc.)
  9. * the interface to the networking code
  10. * PPP multilink: splitting datagrams between multiple links, and
  11. ordering and combining received fragments
  12. * the interface to pppd, via a /dev/ppp character device
  13. * packet compression and decompression
  14. * TCP/IP header compression and decompression
  15. * detecting network traffic for demand dialling and for idle timeouts
  16. * simple packet filtering
  17. For sending and receiving PPP frames, the generic PPP driver calls on
  18. the services of PPP `channels'. A PPP channel encapsulates a
  19. mechanism for transporting PPP frames from one machine to another. A
  20. PPP channel implementation can be arbitrarily complex internally but
  21. has a very simple interface with the generic PPP code: it merely has
  22. to be able to send PPP frames, receive PPP frames, and optionally
  23. handle ioctl requests. Currently there are PPP channel
  24. implementations for asynchronous serial ports, synchronous serial
  25. ports, and for PPP over ethernet.
  26. This architecture makes it possible to implement PPP multilink in a
  27. natural and straightforward way, by allowing more than one channel to
  28. be linked to each ppp network interface unit. The generic layer is
  29. responsible for splitting datagrams on transmit and recombining them
  30. on receive.
  31. PPP channel API
  32. ---------------
  33. See include/linux/ppp_channel.h for the declaration of the types and
  34. functions used to communicate between the generic PPP layer and PPP
  35. channels.
  36. Each channel has to provide two functions to the generic PPP layer,
  37. via the ppp_channel.ops pointer:
  38. * start_xmit() is called by the generic layer when it has a frame to
  39. send. The channel has the option of rejecting the frame for
  40. flow-control reasons. In this case, start_xmit() should return 0
  41. and the channel should call the ppp_output_wakeup() function at a
  42. later time when it can accept frames again, and the generic layer
  43. will then attempt to retransmit the rejected frame(s). If the frame
  44. is accepted, the start_xmit() function should return 1.
  45. * ioctl() provides an interface which can be used by a user-space
  46. program to control aspects of the channel's behaviour. This
  47. procedure will be called when a user-space program does an ioctl
  48. system call on an instance of /dev/ppp which is bound to the
  49. channel. (Usually it would only be pppd which would do this.)
  50. The generic PPP layer provides seven functions to channels:
  51. * ppp_register_channel() is called when a channel has been created, to
  52. notify the PPP generic layer of its presence. For example, setting
  53. a serial port to the PPPDISC line discipline causes the ppp_async
  54. channel code to call this function.
  55. * ppp_unregister_channel() is called when a channel is to be
  56. destroyed. For example, the ppp_async channel code calls this when
  57. a hangup is detected on the serial port.
  58. * ppp_output_wakeup() is called by a channel when it has previously
  59. rejected a call to its start_xmit function, and can now accept more
  60. packets.
  61. * ppp_input() is called by a channel when it has received a complete
  62. PPP frame.
  63. * ppp_input_error() is called by a channel when it has detected that a
  64. frame has been lost or dropped (for example, because of a FCS (frame
  65. check sequence) error).
  66. * ppp_channel_index() returns the channel index assigned by the PPP
  67. generic layer to this channel. The channel should provide some way
  68. (e.g. an ioctl) to transmit this back to user-space, as user-space
  69. will need it to attach an instance of /dev/ppp to this channel.
  70. * ppp_unit_number() returns the unit number of the ppp network
  71. interface to which this channel is connected, or -1 if the channel
  72. is not connected.
  73. Connecting a channel to the ppp generic layer is initiated from the
  74. channel code, rather than from the generic layer. The channel is
  75. expected to have some way for a user-level process to control it
  76. independently of the ppp generic layer. For example, with the
  77. ppp_async channel, this is provided by the file descriptor to the
  78. serial port.
  79. Generally a user-level process will initialize the underlying
  80. communications medium and prepare it to do PPP. For example, with an
  81. async tty, this can involve setting the tty speed and modes, issuing
  82. modem commands, and then going through some sort of dialog with the
  83. remote system to invoke PPP service there. We refer to this process
  84. as `discovery'. Then the user-level process tells the medium to
  85. become a PPP channel and register itself with the generic PPP layer.
  86. The channel then has to report the channel number assigned to it back
  87. to the user-level process. From that point, the PPP negotiation code
  88. in the PPP daemon (pppd) can take over and perform the PPP
  89. negotiation, accessing the channel through the /dev/ppp interface.
  90. At the interface to the PPP generic layer, PPP frames are stored in
  91. skbuff structures and start with the two-byte PPP protocol number.
  92. The frame does *not* include the 0xff `address' byte or the 0x03
  93. `control' byte that are optionally used in async PPP. Nor is there
  94. any escaping of control characters, nor are there any FCS or framing
  95. characters included. That is all the responsibility of the channel
  96. code, if it is needed for the particular medium. That is, the skbuffs
  97. presented to the start_xmit() function contain only the 2-byte
  98. protocol number and the data, and the skbuffs presented to ppp_input()
  99. must be in the same format.
  100. The channel must provide an instance of a ppp_channel struct to
  101. represent the channel. The channel is free to use the `private' field
  102. however it wishes. The channel should initialize the `mtu' and
  103. `hdrlen' fields before calling ppp_register_channel() and not change
  104. them until after ppp_unregister_channel() returns. The `mtu' field
  105. represents the maximum size of the data part of the PPP frames, that
  106. is, it does not include the 2-byte protocol number.
  107. If the channel needs some headroom in the skbuffs presented to it for
  108. transmission (i.e., some space free in the skbuff data area before the
  109. start of the PPP frame), it should set the `hdrlen' field of the
  110. ppp_channel struct to the amount of headroom required. The generic
  111. PPP layer will attempt to provide that much headroom but the channel
  112. should still check if there is sufficient headroom and copy the skbuff
  113. if there isn't.
  114. On the input side, channels should ideally provide at least 2 bytes of
  115. headroom in the skbuffs presented to ppp_input(). The generic PPP
  116. code does not require this but will be more efficient if this is done.
  117. Buffering and flow control
  118. --------------------------
  119. The generic PPP layer has been designed to minimize the amount of data
  120. that it buffers in the transmit direction. It maintains a queue of
  121. transmit packets for the PPP unit (network interface device) plus a
  122. queue of transmit packets for each attached channel. Normally the
  123. transmit queue for the unit will contain at most one packet; the
  124. exceptions are when pppd sends packets by writing to /dev/ppp, and
  125. when the core networking code calls the generic layer's start_xmit()
  126. function with the queue stopped, i.e. when the generic layer has
  127. called netif_stop_queue(), which only happens on a transmit timeout.
  128. The start_xmit function always accepts and queues the packet which it
  129. is asked to transmit.
  130. Transmit packets are dequeued from the PPP unit transmit queue and
  131. then subjected to TCP/IP header compression and packet compression
  132. (Deflate or BSD-Compress compression), as appropriate. After this
  133. point the packets can no longer be reordered, as the decompression
  134. algorithms rely on receiving compressed packets in the same order that
  135. they were generated.
  136. If multilink is not in use, this packet is then passed to the attached
  137. channel's start_xmit() function. If the channel refuses to take
  138. the packet, the generic layer saves it for later transmission. The
  139. generic layer will call the channel's start_xmit() function again
  140. when the channel calls ppp_output_wakeup() or when the core
  141. networking code calls the generic layer's start_xmit() function
  142. again. The generic layer contains no timeout and retransmission
  143. logic; it relies on the core networking code for that.
  144. If multilink is in use, the generic layer divides the packet into one
  145. or more fragments and puts a multilink header on each fragment. It
  146. decides how many fragments to use based on the length of the packet
  147. and the number of channels which are potentially able to accept a
  148. fragment at the moment. A channel is potentially able to accept a
  149. fragment if it doesn't have any fragments currently queued up for it
  150. to transmit. The channel may still refuse a fragment; in this case
  151. the fragment is queued up for the channel to transmit later. This
  152. scheme has the effect that more fragments are given to higher-
  153. bandwidth channels. It also means that under light load, the generic
  154. layer will tend to fragment large packets across all the channels,
  155. thus reducing latency, while under heavy load, packets will tend to be
  156. transmitted as single fragments, thus reducing the overhead of
  157. fragmentation.
  158. SMP safety
  159. ----------
  160. The PPP generic layer has been designed to be SMP-safe. Locks are
  161. used around accesses to the internal data structures where necessary
  162. to ensure their integrity. As part of this, the generic layer
  163. requires that the channels adhere to certain requirements and in turn
  164. provides certain guarantees to the channels. Essentially the channels
  165. are required to provide the appropriate locking on the ppp_channel
  166. structures that form the basis of the communication between the
  167. channel and the generic layer. This is because the channel provides
  168. the storage for the ppp_channel structure, and so the channel is
  169. required to provide the guarantee that this storage exists and is
  170. valid at the appropriate times.
  171. The generic layer requires these guarantees from the channel:
  172. * The ppp_channel object must exist from the time that
  173. ppp_register_channel() is called until after the call to
  174. ppp_unregister_channel() returns.
  175. * No thread may be in a call to any of ppp_input(), ppp_input_error(),
  176. ppp_output_wakeup(), ppp_channel_index() or ppp_unit_number() for a
  177. channel at the time that ppp_unregister_channel() is called for that
  178. channel.
  179. * ppp_register_channel() and ppp_unregister_channel() must be called
  180. from process context, not interrupt or softirq/BH context.
  181. * The remaining generic layer functions may be called at softirq/BH
  182. level but must not be called from a hardware interrupt handler.
  183. * The generic layer may call the channel start_xmit() function at
  184. softirq/BH level but will not call it at interrupt level. Thus the
  185. start_xmit() function may not block.
  186. * The generic layer will only call the channel ioctl() function in
  187. process context.
  188. The generic layer provides these guarantees to the channels:
  189. * The generic layer will not call the start_xmit() function for a
  190. channel while any thread is already executing in that function for
  191. that channel.
  192. * The generic layer will not call the ioctl() function for a channel
  193. while any thread is already executing in that function for that
  194. channel.
  195. * By the time a call to ppp_unregister_channel() returns, no thread
  196. will be executing in a call from the generic layer to that channel's
  197. start_xmit() or ioctl() function, and the generic layer will not
  198. call either of those functions subsequently.
  199. Interface to pppd
  200. -----------------
  201. The PPP generic layer exports a character device interface called
  202. /dev/ppp. This is used by pppd to control PPP interface units and
  203. channels. Although there is only one /dev/ppp, each open instance of
  204. /dev/ppp acts independently and can be attached either to a PPP unit
  205. or a PPP channel. This is achieved using the file->private_data field
  206. to point to a separate object for each open instance of /dev/ppp. In
  207. this way an effect similar to Solaris' clone open is obtained,
  208. allowing us to control an arbitrary number of PPP interfaces and
  209. channels without having to fill up /dev with hundreds of device names.
  210. When /dev/ppp is opened, a new instance is created which is initially
  211. unattached. Using an ioctl call, it can then be attached to an
  212. existing unit, attached to a newly-created unit, or attached to an
  213. existing channel. An instance attached to a unit can be used to send
  214. and receive PPP control frames, using the read() and write() system
  215. calls, along with poll() if necessary. Similarly, an instance
  216. attached to a channel can be used to send and receive PPP frames on
  217. that channel.
  218. In multilink terms, the unit represents the bundle, while the channels
  219. represent the individual physical links. Thus, a PPP frame sent by a
  220. write to the unit (i.e., to an instance of /dev/ppp attached to the
  221. unit) will be subject to bundle-level compression and to fragmentation
  222. across the individual links (if multilink is in use). In contrast, a
  223. PPP frame sent by a write to the channel will be sent as-is on that
  224. channel, without any multilink header.
  225. A channel is not initially attached to any unit. In this state it can
  226. be used for PPP negotiation but not for the transfer of data packets.
  227. It can then be connected to a PPP unit with an ioctl call, which
  228. makes it available to send and receive data packets for that unit.
  229. The ioctl calls which are available on an instance of /dev/ppp depend
  230. on whether it is unattached, attached to a PPP interface, or attached
  231. to a PPP channel. The ioctl calls which are available on an
  232. unattached instance are:
  233. * PPPIOCNEWUNIT creates a new PPP interface and makes this /dev/ppp
  234. instance the "owner" of the interface. The argument should point to
  235. an int which is the desired unit number if >= 0, or -1 to assign the
  236. lowest unused unit number. Being the owner of the interface means
  237. that the interface will be shut down if this instance of /dev/ppp is
  238. closed.
  239. * PPPIOCATTACH attaches this instance to an existing PPP interface.
  240. The argument should point to an int containing the unit number.
  241. This does not make this instance the owner of the PPP interface.
  242. * PPPIOCATTCHAN attaches this instance to an existing PPP channel.
  243. The argument should point to an int containing the channel number.
  244. The ioctl calls available on an instance of /dev/ppp attached to a
  245. channel are:
  246. * PPPIOCDETACH detaches the instance from the channel. This ioctl is
  247. deprecated since the same effect can be achieved by closing the
  248. instance. In order to prevent possible races this ioctl will fail
  249. with an EINVAL error if more than one file descriptor refers to this
  250. instance (i.e. as a result of dup(), dup2() or fork()).
  251. * PPPIOCCONNECT connects this channel to a PPP interface. The
  252. argument should point to an int containing the interface unit
  253. number. It will return an EINVAL error if the channel is already
  254. connected to an interface, or ENXIO if the requested interface does
  255. not exist.
  256. * PPPIOCDISCONN disconnects this channel from the PPP interface that
  257. it is connected to. It will return an EINVAL error if the channel
  258. is not connected to an interface.
  259. * All other ioctl commands are passed to the channel ioctl() function.
  260. The ioctl calls that are available on an instance that is attached to
  261. an interface unit are:
  262. * PPPIOCSMRU sets the MRU (maximum receive unit) for the interface.
  263. The argument should point to an int containing the new MRU value.
  264. * PPPIOCSFLAGS sets flags which control the operation of the
  265. interface. The argument should be a pointer to an int containing
  266. the new flags value. The bits in the flags value that can be set
  267. are:
  268. SC_COMP_TCP enable transmit TCP header compression
  269. SC_NO_TCP_CCID disable connection-id compression for
  270. TCP header compression
  271. SC_REJ_COMP_TCP disable receive TCP header decompression
  272. SC_CCP_OPEN Compression Control Protocol (CCP) is
  273. open, so inspect CCP packets
  274. SC_CCP_UP CCP is up, may (de)compress packets
  275. SC_LOOP_TRAFFIC send IP traffic to pppd
  276. SC_MULTILINK enable PPP multilink fragmentation on
  277. transmitted packets
  278. SC_MP_SHORTSEQ expect short multilink sequence
  279. numbers on received multilink fragments
  280. SC_MP_XSHORTSEQ transmit short multilink sequence nos.
  281. The values of these flags are defined in <linux/ppp-ioctl.h>. Note
  282. that the values of the SC_MULTILINK, SC_MP_SHORTSEQ and
  283. SC_MP_XSHORTSEQ bits are ignored if the CONFIG_PPP_MULTILINK option
  284. is not selected.
  285. * PPPIOCGFLAGS returns the value of the status/control flags for the
  286. interface unit. The argument should point to an int where the ioctl
  287. will store the flags value. As well as the values listed above for
  288. PPPIOCSFLAGS, the following bits may be set in the returned value:
  289. SC_COMP_RUN CCP compressor is running
  290. SC_DECOMP_RUN CCP decompressor is running
  291. SC_DC_ERROR CCP decompressor detected non-fatal error
  292. SC_DC_FERROR CCP decompressor detected fatal error
  293. * PPPIOCSCOMPRESS sets the parameters for packet compression or
  294. decompression. The argument should point to a ppp_option_data
  295. structure (defined in <linux/ppp-ioctl.h>), which contains a
  296. pointer/length pair which should describe a block of memory
  297. containing a CCP option specifying a compression method and its
  298. parameters. The ppp_option_data struct also contains a `transmit'
  299. field. If this is 0, the ioctl will affect the receive path,
  300. otherwise the transmit path.
  301. * PPPIOCGUNIT returns, in the int pointed to by the argument, the unit
  302. number of this interface unit.
  303. * PPPIOCSDEBUG sets the debug flags for the interface to the value in
  304. the int pointed to by the argument. Only the least significant bit
  305. is used; if this is 1 the generic layer will print some debug
  306. messages during its operation. This is only intended for debugging
  307. the generic PPP layer code; it is generally not helpful for working
  308. out why a PPP connection is failing.
  309. * PPPIOCGDEBUG returns the debug flags for the interface in the int
  310. pointed to by the argument.
  311. * PPPIOCGIDLE returns the time, in seconds, since the last data
  312. packets were sent and received. The argument should point to a
  313. ppp_idle structure (defined in <linux/ppp_defs.h>). If the
  314. CONFIG_PPP_FILTER option is enabled, the set of packets which reset
  315. the transmit and receive idle timers is restricted to those which
  316. pass the `active' packet filter.
  317. * PPPIOCSMAXCID sets the maximum connection-ID parameter (and thus the
  318. number of connection slots) for the TCP header compressor and
  319. decompressor. The lower 16 bits of the int pointed to by the
  320. argument specify the maximum connection-ID for the compressor. If
  321. the upper 16 bits of that int are non-zero, they specify the maximum
  322. connection-ID for the decompressor, otherwise the decompressor's
  323. maximum connection-ID is set to 15.
  324. * PPPIOCSNPMODE sets the network-protocol mode for a given network
  325. protocol. The argument should point to an npioctl struct (defined
  326. in <linux/ppp-ioctl.h>). The `protocol' field gives the PPP protocol
  327. number for the protocol to be affected, and the `mode' field
  328. specifies what to do with packets for that protocol:
  329. NPMODE_PASS normal operation, transmit and receive packets
  330. NPMODE_DROP silently drop packets for this protocol
  331. NPMODE_ERROR drop packets and return an error on transmit
  332. NPMODE_QUEUE queue up packets for transmit, drop received
  333. packets
  334. At present NPMODE_ERROR and NPMODE_QUEUE have the same effect as
  335. NPMODE_DROP.
  336. * PPPIOCGNPMODE returns the network-protocol mode for a given
  337. protocol. The argument should point to an npioctl struct with the
  338. `protocol' field set to the PPP protocol number for the protocol of
  339. interest. On return the `mode' field will be set to the network-
  340. protocol mode for that protocol.
  341. * PPPIOCSPASS and PPPIOCSACTIVE set the `pass' and `active' packet
  342. filters. These ioctls are only available if the CONFIG_PPP_FILTER
  343. option is selected. The argument should point to a sock_fprog
  344. structure (defined in <linux/filter.h>) containing the compiled BPF
  345. instructions for the filter. Packets are dropped if they fail the
  346. `pass' filter; otherwise, if they fail the `active' filter they are
  347. passed but they do not reset the transmit or receive idle timer.
  348. * PPPIOCSMRRU enables or disables multilink processing for received
  349. packets and sets the multilink MRRU (maximum reconstructed receive
  350. unit). The argument should point to an int containing the new MRRU
  351. value. If the MRRU value is 0, processing of received multilink
  352. fragments is disabled. This ioctl is only available if the
  353. CONFIG_PPP_MULTILINK option is selected.
  354. Last modified: 7-feb-2002