xref: /openbsd/usr.sbin/npppd/pppoe/pppoe.h (revision f5c2ff87)
1 /*	$OpenBSD: pppoe.h,v 1.8 2021/03/29 03:54:39 yasuoka Exp $ */
2 
3 /*-
4  * Copyright (c) 2009 Internet Initiative Japan Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 #ifndef	PPPOE_H
29 #define	PPPOE_H 1
30 
31 /*
32  * Constant variables and types from PPPoE protocol (RFC 2516)
33  */
34 #define PPPOE_RFC2516_TYPE	0x01
35 #define PPPOE_RFC2516_VER	0x01
36 
37 /** The PPPoE Active Discovery Initiation (PADI) packet */
38 #define	PPPOE_CODE_PADI		0x09
39 
40 /** The PPPoE Active Discovery Offer (PADO) packet */
41 #define	PPPOE_CODE_PADO		0x07
42 
43 /** The PPPoE Active Discovery Request (PADR) packet */
44 #define	PPPOE_CODE_PADR		0x19
45 
46 /** The PPPoE Active Discovery Session-confirmation (PADS) packet */
47 #define	PPPOE_CODE_PADS		0x65
48 
49 /** The PPPoE Active Discovery Terminate (PADT) packet */
50 #define	PPPOE_CODE_PADT		0xa7
51 
52 #define	PPPOE_TAG_END_OF_LIST		0x0000
53 #define	PPPOE_TAG_SERVICE_NAME		0x0101
54 #define	PPPOE_TAG_AC_NAME		0x0102
55 #define	PPPOE_TAG_HOST_UNIQ		0x0103
56 #define	PPPOE_TAG_AC_COOKIE		0x0104
57 #define	PPPOE_TAG_VENDOR_SPECIFIC	0x0105
58 #define	PPPOE_TAG_RELAY_SESSION_ID	0x0110
59 #define	PPPOE_TAG_SERVICE_NAME_ERROR	0x0201
60 #define	PPPOE_TAG_AC_SYSTEM_ERROR	0x0202
61 #define	PPPOE_TAG_GENERIC_ERROR		0x0203
62 
63 /** PPPoE Protocol Header */
64 struct pppoe_header {
65 #if BYTE_ORDER == BIG_ENDIAN
66     uint8_t ver:4, type:4;
67 #else
68     uint8_t type:4, ver:4;
69 #endif
70     uint8_t code;
71     uint16_t session_id;
72     uint16_t length;
73 } __attribute__((__packed__));
74 
75 /** PPPoE TLV Header */
76 struct pppoe_tlv {
77 	uint16_t	type;
78 	uint16_t	length;
79 	uint8_t		value[0];
80 } __attribute__((__packed__));
81 
82 /*
83  * Constant variables and types for implementions
84  */
85 #include "pppoe_conf.h"
86 
87 /** Default data link layer  */
88 #define PPPOED_DEFAULT_LAYER2_LABEL	"PPPoE"
89 
90 #define	PPPOED_CONFIG_BUFSIZ		65535
91 #define	PPPOED_HOSTUNIQ_LEN		64
92 #define PPPOED_PHY_LABEL_SIZE		16
93 
94 /*
95  * pppoed status
96  */
97 #define	PPPOED_STATE_INIT 		0 /** Initial */
98 #define	PPPOED_STATE_RUNNING 		1 /** Running */
99 #define	PPPOED_STATE_STOPPED 		2 /** Stopped */
100 
101 #define pppoed_is_stopped(pppoed)	\
102 	(((pppoed)->state == PPPOED_STATE_STOPPED)? 1 : 0)
103 #define pppoed_is_running(pppoed)	\
104 	(((pppoed)->state == PPPOED_STATE_RUNNING)? 1 : 0)
105 
106 #define	PPPOED_LISTENER_INVALID_INDEX	UINT16_MAX
107 
108 /** PPPoE listener type */
109 typedef struct _pppoed_listener {
110 	/** Descriptor of bpf(4) */
111 	int bpf;
112 	/** Context of event(3) for the descriptor of bpf(4) */
113 	struct event ev_bpf;
114 	/** Pointer to base PPPoE daemon */
115 	struct _pppoed *self;
116 	/** Ethernet address */
117 	u_char	ether_addr[ETHER_ADDR_LEN];
118 	/** Listener index numbered  by the base PPPoE daemon */
119 	uint16_t	index;
120 	/** Listening interface name */
121 	char	listen_ifname[IF_NAMESIZE];
122 	/** Label of physcal layer */
123 	char	tun_name[PPPOED_PHY_LABEL_SIZE];
124 	/** Configuration */
125 	struct pppoe_conf *conf;
126 } pppoed_listener;
127 
128 /** PPPoE daemon type */
129 typedef struct _pppoed {
130 	/** PPPoE daemon Id */
131 	int id;
132 	/** List of {@link pppoed_listener} */
133 	slist listener;
134 	/** Status of this daemon */
135 	int state;
136 
137 	/** Hashmap that maps from session number to {@link pppoe_session} */
138 	hash_table	*session_hash;
139 	/** List of free session numbers */
140 	slist	session_free_list;
141 
142 	/** Hashmap that contains uniq cookie value */
143 	hash_table	*acookie_hash;
144 	/** Next cookie number */
145 	uint32_t	acookie_next;
146 
147 	/** Flags */
148 	uint32_t
149 	    listen_incomplete:1,
150 	    reserved:31;
151 } pppoed;
152 
153 /** PPPoE session type */
154 typedef struct _pppoe_session {
155 	/** State of this session */
156 	int 		state;
157 	/** Pointer to base {@link pppoed *} PPPoE daemon */
158 	pppoed		*pppoed;
159 	/** Pointer to the PPP context */
160 	void 		*ppp;
161 	/** Session id */
162 	uint16_t	session_id;
163 	/** Cookie number */
164 	int		acookie;
165 	/** Peer ethernet address */
166 	u_char 		ether_addr[ETHER_ADDR_LEN];
167 	/** Index of listener */
168 	uint16_t	listener_index;
169 	/** Cache for ethernet frame header */
170 	struct ether_header ehdr;
171 	int lcp_echo_interval;			/* XXX can remove */
172 	int lcp_echo_max_failure;		/* XXX can remove */
173 	/** Context of event(3) for disposing */
174 	struct event ev_disposing;
175 } pppoe_session;
176 
177 #define	PPPOE_SESSION_STATE_INIT		0 /** Initial */
178 #define	PPPOE_SESSION_STATE_RUNNING		1 /** Running */
179 #define	PPPOE_SESSION_STATE_DISPOSING		2 /** Disposing */
180 
181 #define	pppoed_need_polling(pppoed)	\
182     (((pppoed)->listen_incomplete != 0)? 1 : 0)
183 
184 #ifdef __cplusplus
185 extern "C" {
186 #endif
187 
188 int         pppoe_session_init (pppoe_session *, pppoed *, int, int, u_char *);
189 void        pppoe_session_fini (pppoe_session *);
190 void        pppoe_session_stop (pppoe_session *);
191 int         pppoe_session_recv_PADR (pppoe_session *, slist *);
192 int         pppoe_session_recv_PADT (pppoe_session *, slist *);
193 void        pppoe_session_input (pppoe_session *, u_char *, int);
194 void        pppoe_session_disconnect (pppoe_session *);
195 
196 int         pppoed_add_listener (pppoed *, int, const char *, const char *);
197 int         pppoed_reload_listeners(pppoed *);
198 
199 int         pppoed_init (pppoed *);
200 int         pppoed_start (pppoed *);
201 void        pppoed_stop (pppoed *);
202 void        pppoed_uninit (pppoed *);
203 void        pppoed_pppoe_session_close_notify(pppoed *, pppoe_session *);
204 const char *pppoed_tlv_value_string(struct pppoe_tlv *);
205 int         pppoed_reload(pppoed *, struct pppoe_confs *);
206 
207 #ifdef __cplusplus
208 }
209 #endif
210 #endif
211