1 /***********************************************************************
5 * Declaration of various PPPoE constants
7 * Copyright (C) 2000 Roaring Penguin Software Inc.
9 * This program may be distributed according to the terms of the GNU
10 * General Public License, version 2 or (at your option) any later version.
12 * $Id: pppoe.h,v 1.4 2008/06/15 04:35:50 paulus Exp $
14 ***********************************************************************/
18 #if defined(HAVE_NETPACKET_PACKET_H) || defined(HAVE_LINUX_IF_PACKET_H)
19 #define _POSIX_SOURCE 1 /* For sigaction defines */
22 #include <stdio.h> /* For FILE */
23 #include <sys/types.h> /* For pid_t */
25 /* How do we access raw Ethernet devices? */
26 #undef USE_LINUX_PACKET
29 #if defined(HAVE_NETPACKET_PACKET_H) || defined(HAVE_LINUX_IF_PACKET_H)
30 #define USE_LINUX_PACKET 1
31 #elif defined(HAVE_SYS_DLPI_H)
33 #elif defined(HAVE_NET_BPF_H)
38 #if !defined(USE_BPF) && !defined(USE_LINUX_PACKET) && !defined(USE_DLPI)
39 #error Unknown method for accessing raw Ethernet frames
42 #ifdef HAVE_SYS_CDEFS_H
43 #include <sys/cdefs.h>
46 #ifdef HAVE_SYS_SOCKET_H
47 #include <sys/socket.h>
50 /* Ugly header files on some Linux boxes... */
51 #if defined(HAVE_LINUX_IF_H)
53 #elif defined(HAVE_NET_IF_H)
57 #ifdef HAVE_NET_IF_TYPES_H
58 #include <net/if_types.h>
61 #define BPF_BUFFER_IS_EMPTY 1
62 #define BPF_BUFFER_HAS_DATA 0
64 /* Define various integer types -- assumes a char is 8 bits */
65 #if SIZEOF_UNSIGNED_SHORT == 2
66 typedef unsigned short UINT16_t;
67 #elif SIZEOF_UNSIGNED_INT == 2
68 typedef unsigned int UINT16_t;
70 #error Could not find a 16-bit integer type
73 #if SIZEOF_UNSIGNED_SHORT == 4
74 typedef unsigned short UINT32_t;
75 #elif SIZEOF_UNSIGNED_INT == 4
76 typedef unsigned int UINT32_t;
77 #elif SIZEOF_UNSIGNED_LONG == 4
78 typedef unsigned long UINT32_t;
80 #error Could not find a 32-bit integer type
83 #ifdef HAVE_LINUX_IF_ETHER_H
84 #include <linux/if_ether.h>
87 #include <netinet/in.h>
89 #ifdef HAVE_NETINET_IF_ETHER_H
90 #include <sys/types.h>
92 #ifdef HAVE_SYS_SOCKET_H
93 #include <sys/socket.h>
95 #ifndef HAVE_SYS_DLPI_H
96 #include <netinet/if_ether.h>
102 /* Ethernet frame types according to RFC 2516 */
103 #define ETH_PPPOE_DISCOVERY 0x8863
104 #define ETH_PPPOE_SESSION 0x8864
106 /* But some brain-dead peers disobey the RFC, so frame types are variables */
107 extern UINT16_t Eth_PPPOE_Discovery;
108 extern UINT16_t Eth_PPPOE_Session;
111 #define CODE_PADI 0x09
112 #define CODE_PADO 0x07
113 #define CODE_PADR 0x19
114 #define CODE_PADS 0x65
115 #define CODE_PADT 0xA7
117 /* Extensions from draft-carrel-info-pppoe-ext-00 */
118 /* I do NOT like PADM or PADN, but they are here for completeness */
119 #define CODE_PADM 0xD3
120 #define CODE_PADN 0xD4
122 #define CODE_SESS 0x00
125 #define TAG_END_OF_LIST 0x0000
126 #define TAG_SERVICE_NAME 0x0101
127 #define TAG_AC_NAME 0x0102
128 #define TAG_HOST_UNIQ 0x0103
129 #define TAG_AC_COOKIE 0x0104
130 #define TAG_VENDOR_SPECIFIC 0x0105
131 #define TAG_RELAY_SESSION_ID 0x0110
132 #define TAG_SERVICE_NAME_ERROR 0x0201
133 #define TAG_AC_SYSTEM_ERROR 0x0202
134 #define TAG_GENERIC_ERROR 0x0203
136 /* Extensions from draft-carrel-info-pppoe-ext-00 */
137 /* I do NOT like these tags one little bit */
138 #define TAG_HURL 0x111
139 #define TAG_MOTM 0x112
140 #define TAG_IP_ROUTE_ADD 0x121
142 /* Discovery phase states */
143 #define STATE_SENT_PADI 0
144 #define STATE_RECEIVED_PADO 1
145 #define STATE_SENT_PADR 2
146 #define STATE_SESSION 3
147 #define STATE_TERMINATED 4
149 /* How many PADI/PADS attempts? */
150 #define MAX_PADI_ATTEMPTS 3
152 /* Initial timeout for PADO/PADS */
153 #define PADI_TIMEOUT 5
155 /* States for scanning PPP frames */
156 #define STATE_WAITFOR_FRAME_ADDR 0
157 #define STATE_DROP_PROTO 1
158 #define STATE_BUILDING_PACKET 2
160 /* Special PPP frame characters */
161 #define FRAME_ESC 0x7D
162 #define FRAME_FLAG 0x7E
163 #define FRAME_ADDR 0xFF
164 #define FRAME_CTRL 0x03
165 #define FRAME_ENC 0x20
170 /* A PPPoE Packet, including Ethernet headers */
171 typedef struct PPPoEPacketStruct {
172 struct ethhdr ethHdr; /* Ethernet header */
173 unsigned int vertype:8; /* PPPoE Version and Type (must both be 1) */
174 unsigned int code:8; /* PPPoE code */
175 unsigned int session:16; /* PPPoE session */
176 unsigned int length:16; /* Payload length */
177 unsigned char payload[ETH_DATA_LEN]; /* A bit of room to spare */
180 #define PPPOE_VER(vt) ((vt) >> 4)
181 #define PPPOE_TYPE(vt) ((vt) & 0xf)
182 #define PPPOE_VER_TYPE(v, t) (((v) << 4) | (t))
184 /* Header size of a PPPoE packet */
185 #define PPPOE_OVERHEAD 6 /* type, code, session, length */
186 #define HDR_SIZE (sizeof(struct ethhdr) + PPPOE_OVERHEAD)
187 #define MAX_PPPOE_PAYLOAD (ETH_DATA_LEN - PPPOE_OVERHEAD)
188 #define MAX_PPPOE_MTU (MAX_PPPOE_PAYLOAD - 2)
192 typedef struct PPPoETagStruct {
193 unsigned int type:16; /* tag type */
194 unsigned int length:16; /* Length of payload */
195 unsigned char payload[ETH_DATA_LEN]; /* A LOT of room to spare */
197 /* Header size of a PPPoE tag */
198 #define TAG_HDR_SIZE 4
200 /* Chunk to read from stdin */
201 #define READ_CHUNK 4096
203 /* Function passed to parsePacket */
204 typedef void ParseFunc(UINT16_t type,
209 #define PPPINITFCS16 0xffff /* Initial FCS value */
211 /* Keep track of the state of a connection -- collect everything in
214 typedef struct PPPoEConnectionStruct {
215 int discoveryState; /* Where we are in discovery */
216 int discoverySocket; /* Raw socket for discovery frames */
217 int sessionSocket; /* Raw socket for session frames */
218 unsigned char myEth[ETH_ALEN]; /* My MAC address */
219 unsigned char peerEth[ETH_ALEN]; /* Peer's MAC address */
220 unsigned char req_peer_mac[ETH_ALEN]; /* required peer MAC address */
221 unsigned char req_peer; /* require mac addr to match req_peer_mac */
222 UINT16_t session; /* Session ID */
223 char *ifName; /* Interface name */
224 char *serviceName; /* Desired service name, if any */
225 char *acName; /* Desired AC name, if any */
226 int synchronous; /* Use synchronous PPP */
227 int useHostUniq; /* Use Host-Uniq tag */
228 int printACNames; /* Just print AC names */
229 FILE *debugFile; /* Debug file for dumping packets */
230 int numPADOs; /* Number of PADO packets received */
231 PPPoETag cookie; /* We have to send this if we get it */
232 PPPoETag relayId; /* Ditto */
233 int error; /* Error packet received */
234 int debug; /* Set to log packets sent and received */
235 int discoveryTimeout; /* Timeout for discovery packets */
238 /* Structure used to determine acceptable PADO or PADS packet */
239 struct PacketCriteria {
240 PPPoEConnection *conn;
247 /* Function Prototypes */
248 UINT16_t etherType(PPPoEPacket *packet);
249 int openInterface(char const *ifname, UINT16_t type, unsigned char *hwaddr);
250 int sendPacket(PPPoEConnection *conn, int sock, PPPoEPacket *pkt, int size);
251 int receivePacket(int sock, PPPoEPacket *pkt, int *size);
252 void fatalSys(char const *str);
253 void rp_fatal(char const *str);
254 void printErr(char const *str);
255 void sysErr(char const *str);
256 void dumpPacket(FILE *fp, PPPoEPacket *packet, char const *dir);
257 void dumpHex(FILE *fp, unsigned char const *buf, int len);
258 int parsePacket(PPPoEPacket *packet, ParseFunc *func, void *extra);
259 void parseLogErrs(UINT16_t typ, UINT16_t len, unsigned char *data, void *xtra);
260 void syncReadFromPPP(PPPoEConnection *conn, PPPoEPacket *packet);
261 void asyncReadFromPPP(PPPoEConnection *conn, PPPoEPacket *packet);
262 void asyncReadFromEth(PPPoEConnection *conn, int sock, int clampMss);
263 void syncReadFromEth(PPPoEConnection *conn, int sock, int clampMss);
264 char *strDup(char const *str);
265 void sendPADT(PPPoEConnection *conn, char const *msg);
266 void sendSessionPacket(PPPoEConnection *conn,
267 PPPoEPacket *packet, int len);
269 void clampMSS(PPPoEPacket *packet, char const *dir, int clampMss);
270 UINT16_t computeTCPChecksum(unsigned char *ipHdr, unsigned char *tcpHdr);
271 UINT16_t pppFCS16(UINT16_t fcs, unsigned char *cp, int len);
272 void discovery(PPPoEConnection *conn);
273 unsigned char *findTag(PPPoEPacket *packet, UINT16_t tagType,
276 void pppoe_printpkt(PPPoEPacket *packet,
277 void (*printer)(void *, char *, ...), void *arg);
278 void pppoe_log_packet(const char *prefix, PPPoEPacket *packet);
280 #define SET_STRING(var, val) do { if (var) free(var); var = strDup(val); } while(0);
282 #define CHECK_ROOM(cursor, start, len) \
284 if (((cursor)-(start))+(len) > MAX_PPPOE_PAYLOAD) { \
285 error("Would create too-long packet"); \
290 /* True if Ethernet address is broadcast or multicast */
291 #define NOT_UNICAST(e) ((e[0] & 0x01) != 0)
292 #define BROADCAST(e) ((e[0] & e[1] & e[2] & e[3] & e[4] & e[5]) == 0xFF)
293 #define NOT_BROADCAST(e) ((e[0] & e[1] & e[2] & e[3] & e[4] & e[5]) != 0xFF)