KallistiOS  ##version##
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
ppp.h
Go to the documentation of this file.
1 /* KallistiOS ##version##
2 
3  ppp/ppp.h
4  Copyright (C) 2014 Lawrence Sebald
5 */
6 
7 #ifndef __PPP_PPP_H
8 #define __PPP_PPP_H
9 
10 #include <sys/cdefs.h>
11 __BEGIN_DECLS
12 
13 #include <stdint.h>
14 #include <sys/types.h>
15 #include <sys/queue.h>
16 
17 /** \file ppp/ppp.h
18  \brief PPP interface for network communications.
19 
20  This file defines the API provided by libppp to interact with the PPP stack.
21  PPP is a network communication protocol used to establish a direct link
22  between two peers. It is most commonly used as the data link layer protocol
23  for dialup internet access, but can also be potentially used on broadband
24  connections (PPP over Ethernet or PPPoE) or on a direct serial line to
25  a computer.
26 
27  The API presented by this library is designed to be extensible to whatever
28  devices you might want to use it with, and was designed to integrate fairly
29  simply into the rest of KOS' network stack.
30 
31  \author Lawrence Sebald
32 */
33 
34 /** \brief PPP device structure.
35 
36  This structure defines a basic output device for PPP packets. This structure
37  is largely modeled after netif_t from the main network stack, with a bit of
38  functionality removed that is irrelevant for PPP.
39 
40  Note that we only allow one device and one connection in this library.
41 
42  \headerfile ppp/ppp.h
43 */
44 typedef struct ppp_device {
45  /** \brief Device name ("modem", "scif", etc). */
46  const char *name;
47 
48  /** \brief Long description of the device. */
49  const char *descr;
50 
51  /** \brief Unit index (starts at zero and counts upwards for multiple
52  network devices of the same type). */
53  int index;
54 
55  /** \brief Device flags.
56  The lowest 16 bits of this value are reserved for use by libppp. You are
57  free to use the other 16 bits as you see fit in your driver. */
58  uint32_t flags;
59 
60  /** \brief Private, device-specific data.
61  This can be used for whatever the driver deems fit. The PPP code won't
62  touch this data at all. Set to NULL if you don't need anything here. */
63  void *privdata;
64 
65  /** \brief Attempt to detect the device.
66  \param self The network device in question.
67  \return 0 on success, <0 on failure.
68  */
69  int (*detect)(struct ppp_device *self);
70 
71  /** \brief Initialize the device.
72  \param self The network device in question.
73  \return 0 on success, <0 on failure.
74  */
75  int (*init)(struct ppp_device *self);
76 
77  /** \brief Shutdown the device.
78  \param self The network device in question.
79  \return 0 on success, <0 on failure.
80  */
81  int (*shutdown)(struct ppp_device *self);
82 
83  /** \brief Transmit data on the device.
84 
85  This function will be called periodically to transmit data on the
86  underlying device. The data passed in may not necessarily be a whole
87  packet (check the flags to see what's being passed in).
88 
89  \param self The network device in question.
90  \param data The data to transmit.
91  \param len The length of the data to transmit in bytes.
92  \param flags Flags to describe what data is being sent in.
93  \return 0 on success, <0 on failure.
94  */
95  int (*tx)(struct ppp_device *self, const uint8_t *data, size_t len,
96  uint32_t flags);
97 
98  /** \brief Poll for queued receive data.
99 
100  This function will be called periodically by a thread to check the
101  device for any new incoming data.
102 
103  \param self The network device in question.
104  \return A pointer to the received data on success. NULL on
105  failure or if no data is waiting.
106  */
107  const uint8_t *(*rx)(struct ppp_device *self, ssize_t *out_len);
108 } ppp_device_t;
109 
110 /** \brief End of packet flag. */
111 #define PPP_TX_END_OF_PKT 0x00000001
112 
113 /** \brief PPP Protocol structure.
114 
115  Each protocol that the PPP library can handle must have one of these
116  registered. All protocols should be registered BEFORE attempting to actually
117  establish a PPP session to ensure that each protocol can be used in the
118  setup of the connection as needed.
119 
120  \headerfile ppp/ppp.h
121 */
122 typedef struct ppp_proto {
123  /** \brief Protocol list entry (not a function!). */
124  TAILQ_ENTRY(ppp_proto) entry;
125 
126  /** \brief Protocol name ("lcp", "pap", etc). */
127  const char *name;
128 
129  /** \brief Protocol code. */
130  uint16_t code;
131 
132  /** \brief Private data (if any). */
133  void *privdata;
134 
135  /** \brief Initialization function.
136 
137  \param self The protocol structure for this protocol.
138  \return 0 on success, <0 on failure.
139  \note Set to NULL if this is not needed in the protocol.
140  */
141  int (*init)(struct ppp_proto *self);
142 
143  /** \brief Shutdown function.
144 
145  This function should perform any protocol-specific shutdown actions and
146  unregister the protocol from the PPP protocol list.
147 
148  \param self The protocol structure for this protocol.
149  \return 0 on success, <0 on failure.
150  */
151  int (*shutdown)(struct ppp_proto *self);
152 
153  /** \brief Protocol packet input function.
154 
155  This function will be called for each packet delivered to the specified
156  protocol.
157 
158  \param self The protocol structure for this protocol.
159  \param pkt The packet being delivered.
160  \param len The length of the packet in bytes.
161  \return 0 on success, <0 on failure.
162  */
163  int (*input)(struct ppp_proto *self, const uint8_t *buf, size_t len);
164 
165  /** \brief Notify the protocol of a PPP phase change.
166 
167  This function will be called by the PPP automaton any time that a phase
168  change is initiated. This is often used for starting up a protocol when
169  appropriate to do so (for instance, LCP uses this to begin negotiating
170  configuration options with the peer when the establish phase is entered
171  by the automaton).
172 
173  \param self The protocol structure for this protocol.
174  \param oldp The old phase (the one the automaton is leaving).
175  \param newp The new phase.
176  \see ppp_phases
177  */
178  void (*enter_phase)(struct ppp_proto *self, int oldp, int newp);
179 
180  /** \brief Check timeouts for resending packets.
181 
182  This function will be called periodically to allow the protocol to check
183  any resend timers that it might have responsibility for.
184 
185  \param self The protocol structure for this protocol.
186  \param tm The current system time for checking timeouts
187  against (in milliseconds since system startup).
188  */
189  void (*check_timeouts)(struct ppp_proto *self, uint64_t tm);
191 
192 /** \brief Static initializer for protocol list entry. */
193 #define PPP_PROTO_ENTRY_INIT { NULL, NULL }
194 
195 /** \defgroup ppp_phases PPP automaton phases
196 
197  This list defines the phases of the PPP automaton, as described in Section
198  3.2 of RFC 1661.
199 
200  @{
201 */
202 #define PPP_PHASE_DEAD 0x01 /**< \brief Pre-connection. */
203 #define PPP_PHASE_ESTABLISH 0x02 /**< \brief Establishing connection. */
204 #define PPP_PHASE_AUTHENTICATE 0x03 /**< \brief Authentication to peer. */
205 #define PPP_PHASE_NETWORK 0x04 /**< \brief Established and working. */
206 #define PPP_PHASE_TERMINATE 0x05 /**< \brief Tearing down the link. */
207 /** @} */
208 
209 /** \brief Set the device used to do PPP communications.
210 
211  This function sets the device that further communications over a
212  point-to-point link will take place over. The device need not be ready to
213  communicate immediately upon calling this function.
214 
215  Unless you are adding support for a new device, you will probably never have
216  to call this function. For instance, if you want to use the Dreamcast serial
217  port to establish a link, the ppp_scif_init() function will call this for
218  you.
219 
220  \param dev The device to use for communication.
221  \return 0 on success, <0 on failure.
222 
223  \note Calling this function after establishing a PPP link will
224  fail.
225 */
226 int ppp_set_device(ppp_device_t *dev);
227 
228 /** \brief Set the login credentials used to authenticate to the peer.
229 
230  This function sets the login credentials that will be used to authenticate
231  to the peer, if the peer requests authentication. The specifics of how the
232  authentication takes place depends on what options are configured when
233  establishing the link.
234 
235  \param username The username to authenticate as.
236  \param password The password to use to authenticate.
237  \return 0 on success, <0 on failure.
238 
239  \note Calling this function after establishing a PPP link will
240  fail.
241 */
242 int ppp_set_login(const char *username, const char *password);
243 
244 /** \brief Send a packet on the PPP link.
245 
246  This function sends a single packet to the peer on the PPP link. Generally,
247  you should not use this function directly, but rather use the facilities
248  provided by KOS' network stack.
249 
250  \param data The packet to send.
251  \param len The length of the packet, in bytes.
252  \param proto The PPP protocol number for the packet.
253  \return 0 on success, <0 on failure.
254 */
255 int ppp_send(const uint8_t *data, size_t len, uint16_t proto);
256 
257 /** \brief Register a protocol with the PPP stack.
258 
259  This function adds a new protocol to the PPP stack, allowing the stack to
260  communicate packets for the given protocol across the link. Generally, you
261  should not have any reason to call this function, as the library includes
262  a set of protocols to do normal communications.
263 
264  \param hnd A protocol handler structure.
265  \return 0 on success, <0 on failure.
266 */
268 
269 /** \brief Unregister a protocol from the PPP stack.
270 
271  This function removes protocol from the PPP stack. This should be done at
272  shutdown time of any protocols added with ppp_add_protocol().
273 
274  \param hnd A protocol handler structure.
275  \return 0 on success, <0 on failure.
276 */
278 
279 /** \brief Send a Protocol Reject packet on the link.
280 
281  This function sends a LCP protocol reject packet on the link for the
282  specified packet. Generally, you should not have to call this function, as
283  the library will handle doing so internally.
284 
285  \param proto The PPP protocol number of the invalid packet.
286  \param pkt The packet itself.
287  \param len The length of the packet, in bytes.
288  \return 0 on success, <0 on failure.
289 */
290 int ppp_lcp_send_proto_reject(uint16_t proto, const uint8_t *pkt, size_t len);
291 
292 /** \defgroup ppp_flags PPP link configuration flags
293 
294  This list defines the flags we can negotiate during link establishment.
295 
296  @{
297 */
298 #define PPP_FLAG_AUTH_PAP 0x00000001 /**< \brief PAP authentication */
299 #define PPP_FLAG_AUTH_CHAP 0x00000002 /**< \brief CHAP authentication */
300 #define PPP_FLAG_PCOMP 0x00000004 /**< \brief Protocol compression */
301 #define PPP_FLAG_ACCOMP 0x00000008 /**< \brief Addr/ctrl compression */
302 #define PPP_FLAG_MAGIC_NUMBER 0x00000010 /**< \brief Use magic numbers */
303 #define PPP_FLAG_WANT_MRU 0x00000020 /**< \brief Specify MRU */
304 #define PPP_FLAG_NO_ACCM 0x00000040 /**< \brief No ctl character map */
305 /** @} */
306 
307 /** \brief Get the flags set for our side of the link.
308 
309  This function retrieves the connection flags set for our side of the PPP
310  link. Before link establishment, this indicates the flags we would like to
311  establish on the link and after establishment it represents the flags that
312  were actually negotiated during link establishment.
313 
314  \return Bitwise OR of \ref ppp_flags.
315 */
316 uint32_t ppp_get_flags(void);
317 
318 /** \brief Get the flags set for the peer's side of the link.
319 
320  This function retrieves the connection flags set for the other side of the
321  PPP link. This value is only valid after link establishment.
322 
323  \return Bitwise OR of \ref ppp_flags.
324 */
325 uint32_t ppp_get_peer_flags(void);
326 
327 /** \brief Get the flags set for our side of the link.
328 
329  This function retrieves the connection flags set for our side of the PPP
330  link. Before link establishment, this indicates the flags we would like to
331  establish on the link and after establishment it represents the flags that
332  were actually negotiated during link establishment.
333 
334  \return Bitwise OR of \ref ppp_flags.
335 */
336 void ppp_set_flags(uint32_t flags);
337 
338 /** \brief Establish a point-to-point link across a previously set-up device.
339 
340  This function establishes a point-to-point link to the peer across a device
341  that was previously set up with ppp_set_device(). Before calling this
342  function, the device must be ready to communicate with the peer. That is to
343  say, any handshaking needed to establish the underlying hardware link must
344  have already completed.
345 
346  \return 0 on success, <0 on failure.
347 
348  \note This function will block until the link is established.
349 */
350 int ppp_connect(void);
351 
352 /** \brief Initialize the Dreamcast serial port for a PPP link.
353 
354  This function sets up the Dreamcast serial port to act as a communications
355  link for a point-to-point connection. This can be used in conjunction with a
356  coder's cable (or similar) to connect the Dreamcast to a PC and the internet
357  if the target system is set up properly.
358 
359  \param bps The speed to initialize the serial port at.
360  \return 0 on success, <0 on failure.
361 */
362 int ppp_scif_init(int bps);
363 
364 /** \brief Initialize the Dreamcast modem for a PPP link.
365 
366  This function sets up the Dreamcast serial port to act as a communications
367  link for a point-to-point connection. This includes dialing the specified
368  phone number and establishing the low-level link.
369 
370  \param number The phone number to dial out.
371  \param blind Non-zero to blind dial (don't wait for a dial tone).
372  \param conn_rate Storage for the connection rate, in bits per second. Set
373  to NULL if you do not need this value back from the
374  function.
375  \retval 0 On success.
376  \retval -1 If modem initialization fails.
377  \retval -2 If not using blind dial and no dial tone is detected
378  within 5 seconds of opening the line.
379  \retval -3 If dialing the modem fails.
380  \retval -4 If a connection is not established in 60 seconds after
381  dialing.
382 */
383 int ppp_modem_init(const char *number, int blind, int *conn_rate);
384 
385 /** \brief Initialize the PPP library.
386 
387  This function initializes the PPP library, preparing internal structures for
388  use and initializing the PPP protocols needed for normal IP communications.
389 
390  \return 0 on success, <0 on failure.
391 */
392 int ppp_init(void);
393 
394 /** \brief Shut down the PPP library.
395 
396  This function cleans up the PPP library, shutting down any connections and
397  deinitializing any protocols that have bene registered previously.
398 
399  \return 0 on success, <0 on failure.
400 */
401 int ppp_shutdown(void);
402 
403 __END_DECLS
404 #endif /* !__PPP_PPP_H */
int ppp_send(const uint8_t *data, size_t len, uint16_t proto)
Send a packet on the PPP link.
int ppp_modem_init(const char *number, int blind, int *conn_rate)
Initialize the Dreamcast modem for a PPP link.
int ppp_shutdown(void)
Shut down the PPP library.
TAILQ_ENTRY(ppp_proto) entry
Protocol list entry (not a function!).
int(* input)(struct ppp_proto *self, const uint8_t *buf, size_t len)
Protocol packet input function.
Definition: ppp.h:163
const char * name
Device name ("modem", "scif", etc).
Definition: ppp.h:46
void ppp_set_flags(uint32_t flags)
Get the flags set for our side of the link.
PPP device structure.
Definition: ppp.h:44
int ppp_connect(void)
Establish a point-to-point link across a previously set-up device.
int ppp_init(void)
Initialize the PPP library.
void(* enter_phase)(struct ppp_proto *self, int oldp, int newp)
Notify the protocol of a PPP phase change.
Definition: ppp.h:178
int index
Unit index (starts at zero and counts upwards for multiple network devices of the same type)...
Definition: ppp.h:53
void * privdata
Private data (if any).
Definition: ppp.h:133
int(* tx)(struct ppp_device *self, const uint8_t *data, size_t len, uint32_t flags)
Transmit data on the device.
Definition: ppp.h:95
int(* shutdown)(struct ppp_proto *self)
Shutdown function.
Definition: ppp.h:151
int(* init)(struct ppp_proto *self)
Initialization function.
Definition: ppp.h:141
uint32_t ppp_get_flags(void)
Get the flags set for our side of the link.
int(* init)(struct ppp_device *self)
Initialize the device.
Definition: ppp.h:75
int ppp_add_protocol(ppp_protocol_t *hnd)
Register a protocol with the PPP stack.
void * privdata
Private, device-specific data. This can be used for whatever the driver deems fit. The PPP code won't touch this data at all. Set to NULL if you don't need anything here.
Definition: ppp.h:63
int ppp_lcp_send_proto_reject(uint16_t proto, const uint8_t *pkt, size_t len)
Send a Protocol Reject packet on the link.
struct ppp_device ppp_device_t
PPP device structure.
int ppp_scif_init(int bps)
Initialize the Dreamcast serial port for a PPP link.
PPP Protocol structure.
Definition: ppp.h:122
int ppp_del_protocol(ppp_protocol_t *hnd)
Unregister a protocol from the PPP stack.
struct ppp_proto ppp_protocol_t
PPP Protocol structure.
const char * descr
Long description of the device.
Definition: ppp.h:49
void(* check_timeouts)(struct ppp_proto *self, uint64_t tm)
Check timeouts for resending packets.
Definition: ppp.h:189
uint32_t flags
Device flags. The lowest 16 bits of this value are reserved for use by libppp. You are free to use th...
Definition: ppp.h:58
const char * name
Protocol name ("lcp", "pap", etc).
Definition: ppp.h:127
uint16_t code
Protocol code.
Definition: ppp.h:130
int(* detect)(struct ppp_device *self)
Attempt to detect the device.
Definition: ppp.h:69
uint32_t ppp_get_peer_flags(void)
Get the flags set for the peer's side of the link.
int ppp_set_login(const char *username, const char *password)
Set the login credentials used to authenticate to the peer.
int ppp_set_device(ppp_device_t *dev)
Set the device used to do PPP communications.
int(* shutdown)(struct ppp_device *self)
Shutdown the device.
Definition: ppp.h:81