• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***********************************************************************
2 *
3 * pppoe.h
4 *
5 * Declaration of various PPPoE constants
6 *
7 * Copyright (C) 2000 Roaring Penguin Software Inc.
8 *
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.
11 *
12 * $Id: pppoe.h,v 1.4 2008/06/15 04:35:50 paulus Exp $
13 *
14 ***********************************************************************/
15 
16 #include "config.h"
17 
18 #if defined(HAVE_NETPACKET_PACKET_H) || defined(HAVE_LINUX_IF_PACKET_H)
19 #define _POSIX_SOURCE 1 /* For sigaction defines */
20 #endif
21 
22 #include <stdio.h>		/* For FILE */
23 #include <sys/types.h>		/* For pid_t */
24 
25 /* How do we access raw Ethernet devices? */
26 #undef USE_LINUX_PACKET
27 #undef USE_BPF
28 
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)
32 #define USE_DLPI
33 #elif defined(HAVE_NET_BPF_H)
34 #define USE_BPF 1
35 #endif
36 
37 /* Sanity check */
38 #if !defined(USE_BPF) && !defined(USE_LINUX_PACKET) && !defined(USE_DLPI)
39 #error Unknown method for accessing raw Ethernet frames
40 #endif
41 
42 #ifdef HAVE_SYS_CDEFS_H
43 #include <sys/cdefs.h>
44 #endif
45 
46 #ifdef HAVE_SYS_SOCKET_H
47 #include <sys/socket.h>
48 #endif
49 
50 /* Ugly header files on some Linux boxes... */
51 #if defined(HAVE_LINUX_IF_H)
52 #include <linux/if.h>
53 #elif defined(HAVE_NET_IF_H)
54 #include <net/if.h>
55 #endif
56 
57 #ifdef HAVE_NET_IF_TYPES_H
58 #include <net/if_types.h>
59 #endif
60 
61 #define BPF_BUFFER_IS_EMPTY 1
62 #define BPF_BUFFER_HAS_DATA 0
63 
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;
69 #else
70 #error Could not find a 16-bit integer type
71 #endif
72 
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;
79 #else
80 #error Could not find a 32-bit integer type
81 #endif
82 
83 #ifdef HAVE_LINUX_IF_ETHER_H
84 #include <linux/if_ether.h>
85 #endif
86 
87 #include <netinet/in.h>
88 
89 #ifdef HAVE_NETINET_IF_ETHER_H
90 #include <sys/types.h>
91 
92 #ifdef HAVE_SYS_SOCKET_H
93 #include <sys/socket.h>
94 #endif
95 #ifndef HAVE_SYS_DLPI_H
96 #include <netinet/if_ether.h>
97 #endif
98 #endif
99 
100 
101 
102 /* Ethernet frame types according to RFC 2516 */
103 #define ETH_PPPOE_DISCOVERY 0x8863
104 #define ETH_PPPOE_SESSION   0x8864
105 
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;
109 
110 /* PPPoE codes */
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
116 
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
121 
122 #define CODE_SESS           0x00
123 
124 /* PPPoE Tags */
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_PPP_MAX_PAYLOAD    0x0120
133 #define TAG_SERVICE_NAME_ERROR 0x0201
134 #define TAG_AC_SYSTEM_ERROR    0x0202
135 #define TAG_GENERIC_ERROR      0x0203
136 
137 /* Extensions from draft-carrel-info-pppoe-ext-00 */
138 /* I do NOT like these tags one little bit */
139 #define TAG_HURL               0x111
140 #define TAG_MOTM               0x112
141 #define TAG_IP_ROUTE_ADD       0x121
142 
143 /* Discovery phase states */
144 #define STATE_SENT_PADI     0
145 #define STATE_RECEIVED_PADO 1
146 #define STATE_SENT_PADR     2
147 #define STATE_SESSION       3
148 #define STATE_TERMINATED    4
149 
150 /* How many PADI/PADS attempts? */
151 #define MAX_PADI_ATTEMPTS 3
152 
153 /* Initial timeout for PADO/PADS */
154 #define PADI_TIMEOUT 5
155 
156 /* States for scanning PPP frames */
157 #define STATE_WAITFOR_FRAME_ADDR 0
158 #define STATE_DROP_PROTO         1
159 #define STATE_BUILDING_PACKET    2
160 
161 /* Special PPP frame characters */
162 #define FRAME_ESC    0x7D
163 #define FRAME_FLAG   0x7E
164 #define FRAME_ADDR   0xFF
165 #define FRAME_CTRL   0x03
166 #define FRAME_ENC    0x20
167 
168 #define IPV4ALEN     4
169 #define SMALLBUF   256
170 
171 /* There are other fixed-size buffers preventing
172    this from being increased to 16110. The buffer
173    sizes would need to be properly de-coupled from
174    the default MRU. For now, getting up to 1500 is
175    enough. */
176 #define ETH_JUMBO_LEN 1508
177 
178 /* A PPPoE Packet, including Ethernet headers */
179 typedef struct PPPoEPacketStruct {
180     struct ethhdr ethHdr;	/* Ethernet header */
181     unsigned int vertype:8;	/* PPPoE Version and Type (must both be 1) */
182     unsigned int code:8;	/* PPPoE code */
183     unsigned int session:16;	/* PPPoE session */
184     unsigned int length:16;	/* Payload length */
185     unsigned char payload[ETH_JUMBO_LEN]; /* A bit of room to spare */
186 } PPPoEPacket;
187 
188 #define PPPOE_VER(vt)		((vt) >> 4)
189 #define PPPOE_TYPE(vt)		((vt) & 0xf)
190 #define PPPOE_VER_TYPE(v, t)	(((v) << 4) | (t))
191 
192 /* Header size of a PPPoE packet */
193 #define PPPOE_OVERHEAD 6  /* type, code, session, length */
194 #define HDR_SIZE (sizeof(struct ethhdr) + PPPOE_OVERHEAD)
195 #define MAX_PPPOE_PAYLOAD (ETH_JUMBO_LEN - PPPOE_OVERHEAD)
196 #define PPP_OVERHEAD 2  /* protocol */
197 #define MAX_PPPOE_MTU (MAX_PPPOE_PAYLOAD - PPP_OVERHEAD)
198 #define TOTAL_OVERHEAD (PPPOE_OVERHEAD + PPP_OVERHEAD)
199 #define ETH_PPPOE_MTU (ETH_DATA_LEN - TOTAL_OVERHEAD)
200 
201 /* PPPoE Tag */
202 
203 typedef struct PPPoETagStruct {
204     unsigned int type:16;	/* tag type */
205     unsigned int length:16;	/* Length of payload */
206     unsigned char payload[ETH_JUMBO_LEN]; /* A LOT of room to spare */
207 } PPPoETag;
208 /* Header size of a PPPoE tag */
209 #define TAG_HDR_SIZE 4
210 
211 /* Chunk to read from stdin */
212 #define READ_CHUNK 4096
213 
214 /* Function passed to parsePacket */
215 typedef void ParseFunc(UINT16_t type,
216 		       UINT16_t len,
217 		       unsigned char *data,
218 		       void *extra);
219 
220 #define PPPINITFCS16    0xffff  /* Initial FCS value */
221 
222 /* Keep track of the state of a connection -- collect everything in
223    one spot */
224 
225 typedef struct PPPoEConnectionStruct {
226     int discoveryState;		/* Where we are in discovery */
227     int discoverySocket;	/* Raw socket for discovery frames */
228     int sessionSocket;		/* Raw socket for session frames */
229     unsigned char myEth[ETH_ALEN]; /* My MAC address */
230     unsigned char peerEth[ETH_ALEN]; /* Peer's MAC address */
231     unsigned char req_peer_mac[ETH_ALEN]; /* required peer MAC address */
232     unsigned char req_peer;	/* require mac addr to match req_peer_mac */
233     UINT16_t session;		/* Session ID */
234     char *ifName;		/* Interface name */
235     char *serviceName;		/* Desired service name, if any */
236     char *acName;		/* Desired AC name, if any */
237     int synchronous;		/* Use synchronous PPP */
238     int useHostUniq;		/* Use Host-Uniq tag */
239     int printACNames;		/* Just print AC names */
240     FILE *debugFile;		/* Debug file for dumping packets */
241     int numPADOs;		/* Number of PADO packets received */
242     PPPoETag cookie;		/* We have to send this if we get it */
243     PPPoETag relayId;		/* Ditto */
244     int error;			/* Error packet received */
245     int debug;			/* Set to log packets sent and received */
246     int discoveryTimeout;       /* Timeout for discovery packets */
247     int seenMaxPayload;
248     int mtu;			/* Stored MTU */
249     int mru;			/* Stored MRU */
250 } PPPoEConnection;
251 
252 /* Structure used to determine acceptable PADO or PADS packet */
253 struct PacketCriteria {
254     PPPoEConnection *conn;
255     int acNameOK;
256     int serviceNameOK;
257     int seenACName;
258     int seenServiceName;
259 };
260 
261 /* Function Prototypes */
262 UINT16_t etherType(PPPoEPacket *packet);
263 int openInterface(char const *ifname, UINT16_t type, unsigned char *hwaddr);
264 int sendPacket(PPPoEConnection *conn, int sock, PPPoEPacket *pkt, int size);
265 int receivePacket(int sock, PPPoEPacket *pkt, int *size);
266 void fatalSys(char const *str);
267 void rp_fatal(char const *str);
268 void printErr(char const *str);
269 void sysErr(char const *str);
270 void dumpPacket(FILE *fp, PPPoEPacket *packet, char const *dir);
271 void dumpHex(FILE *fp, unsigned char const *buf, int len);
272 int parsePacket(PPPoEPacket *packet, ParseFunc *func, void *extra);
273 void parseLogErrs(UINT16_t typ, UINT16_t len, unsigned char *data, void *xtra);
274 void syncReadFromPPP(PPPoEConnection *conn, PPPoEPacket *packet);
275 void asyncReadFromPPP(PPPoEConnection *conn, PPPoEPacket *packet);
276 void asyncReadFromEth(PPPoEConnection *conn, int sock, int clampMss);
277 void syncReadFromEth(PPPoEConnection *conn, int sock, int clampMss);
278 char *strDup(char const *str);
279 void sendPADT(PPPoEConnection *conn, char const *msg);
280 void sendSessionPacket(PPPoEConnection *conn,
281 		       PPPoEPacket *packet, int len);
282 void initPPP(void);
283 void clampMSS(PPPoEPacket *packet, char const *dir, int clampMss);
284 UINT16_t computeTCPChecksum(unsigned char *ipHdr, unsigned char *tcpHdr);
285 UINT16_t pppFCS16(UINT16_t fcs, unsigned char *cp, int len);
286 void discovery(PPPoEConnection *conn);
287 unsigned char *findTag(PPPoEPacket *packet, UINT16_t tagType,
288 		       PPPoETag *tag);
289 
290 void pppoe_printpkt(PPPoEPacket *packet,
291 		    void (*printer)(void *, char *, ...), void *arg);
292 void pppoe_log_packet(const char *prefix, PPPoEPacket *packet);
293 
294 #define SET_STRING(var, val) do { if (var) free(var); var = strDup(val); } while(0);
295 
296 #define CHECK_ROOM(cursor, start, len) \
297 do {\
298     if (((cursor)-(start))+(len) > MAX_PPPOE_PAYLOAD) { \
299 	error("Would create too-long packet");	\
300         return; \
301     } \
302 } while(0)
303 
304 /* True if Ethernet address is broadcast or multicast */
305 #define NOT_UNICAST(e) ((e[0] & 0x01) != 0)
306 #define BROADCAST(e) ((e[0] & e[1] & e[2] & e[3] & e[4] & e[5]) == 0xFF)
307 #define NOT_BROADCAST(e) ((e[0] & e[1] & e[2] & e[3] & e[4] & e[5]) != 0xFF)
308