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 #include <stdio.h> /* For FILE */
19 #include <sys/types.h> /* For pid_t */
23 #include "pppd/pppd.h" /* For error */
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 /* This has to be included before Linux 4.8's linux/in.h
52 #include <netinet/in.h>
54 /* Ugly header files on some Linux boxes... */
55 #if defined(HAVE_LINUX_IF_H)
57 #elif defined(HAVE_NET_IF_H)
61 #ifdef HAVE_NET_IF_TYPES_H
62 #include <net/if_types.h>
65 #define BPF_BUFFER_IS_EMPTY 1
66 #define BPF_BUFFER_HAS_DATA 0
68 /* Define various integer types -- assumes a char is 8 bits */
69 #if SIZEOF_UNSIGNED_SHORT == 2
70 typedef unsigned short UINT16_t;
71 #elif SIZEOF_UNSIGNED_INT == 2
72 typedef unsigned int UINT16_t;
74 #error Could not find a 16-bit integer type
77 #if SIZEOF_UNSIGNED_SHORT == 4
78 typedef unsigned short UINT32_t;
79 #elif SIZEOF_UNSIGNED_INT == 4
80 typedef unsigned int UINT32_t;
81 #elif SIZEOF_UNSIGNED_LONG == 4
82 typedef unsigned long UINT32_t;
84 #error Could not find a 32-bit integer type
87 #ifdef HAVE_LINUX_IF_ETHER_H
88 #include <linux/if_ether.h>
91 #ifdef HAVE_NETINET_IF_ETHER_H
92 #include <sys/types.h>
94 #ifdef HAVE_SYS_SOCKET_H
95 #include <sys/socket.h>
97 #ifndef HAVE_SYS_DLPI_H
98 #include <netinet/if_ether.h>
103 /* Ethernet frame types according to RFC 2516 */
104 #define ETH_PPPOE_DISCOVERY 0x8863
105 #define ETH_PPPOE_SESSION 0x8864
107 /* But some brain-dead peers disobey the RFC, so frame types are variables */
108 extern UINT16_t Eth_PPPOE_Discovery;
109 extern UINT16_t Eth_PPPOE_Session;
112 #define CODE_PADI 0x09
113 #define CODE_PADO 0x07
114 #define CODE_PADR 0x19
115 #define CODE_PADS 0x65
116 #define CODE_PADT 0xA7
118 /* Extensions from draft-carrel-info-pppoe-ext-00 */
119 /* I do NOT like PADM or PADN, but they are here for completeness */
120 #define CODE_PADM 0xD3
121 #define CODE_PADN 0xD4
123 #define CODE_SESS 0x00
126 #define TAG_END_OF_LIST 0x0000
127 #define TAG_SERVICE_NAME 0x0101
128 #define TAG_AC_NAME 0x0102
129 #define TAG_HOST_UNIQ 0x0103
130 #define TAG_AC_COOKIE 0x0104
131 #define TAG_VENDOR_SPECIFIC 0x0105
132 #define TAG_RELAY_SESSION_ID 0x0110
133 #define TAG_PPP_MAX_PAYLOAD 0x0120
134 #define TAG_SERVICE_NAME_ERROR 0x0201
135 #define TAG_AC_SYSTEM_ERROR 0x0202
136 #define TAG_GENERIC_ERROR 0x0203
138 /* Extensions from draft-carrel-info-pppoe-ext-00 */
139 /* I do NOT like these tags one little bit */
140 #define TAG_HURL 0x111
141 #define TAG_MOTM 0x112
142 #define TAG_IP_ROUTE_ADD 0x121
144 /* Discovery phase states */
145 #define STATE_SENT_PADI 0
146 #define STATE_RECEIVED_PADO 1
147 #define STATE_SENT_PADR 2
148 #define STATE_SESSION 3
149 #define STATE_TERMINATED 4
151 /* How many PADI/PADS attempts? */
152 #define MAX_PADI_ATTEMPTS 3
154 /* Initial timeout for PADO/PADS */
155 #define PADI_TIMEOUT 5
157 /* States for scanning PPP frames */
158 #define STATE_WAITFOR_FRAME_ADDR 0
159 #define STATE_DROP_PROTO 1
160 #define STATE_BUILDING_PACKET 2
162 /* Special PPP frame characters */
163 #define FRAME_ESC 0x7D
164 #define FRAME_FLAG 0x7E
165 #define FRAME_ADDR 0xFF
166 #define FRAME_CTRL 0x03
167 #define FRAME_ENC 0x20
172 /* There are other fixed-size buffers preventing
173 this from being increased to 16110. The buffer
174 sizes would need to be properly de-coupled from
175 the default MRU. For now, getting up to 1500 is
177 #define ETH_JUMBO_LEN 1508
179 /* A PPPoE Packet, including Ethernet headers */
180 typedef struct PPPoEPacketStruct {
181 struct ethhdr ethHdr; /* Ethernet header */
182 unsigned int vertype:8; /* PPPoE Version and Type (must both be 1) */
183 unsigned int code:8; /* PPPoE code */
184 unsigned int session:16; /* PPPoE session */
185 unsigned int length:16; /* Payload length */
186 unsigned char payload[ETH_JUMBO_LEN]; /* A bit of room to spare */
189 #define PPPOE_VER(vt) ((vt) >> 4)
190 #define PPPOE_TYPE(vt) ((vt) & 0xf)
191 #define PPPOE_VER_TYPE(v, t) (((v) << 4) | (t))
193 /* Header size of a PPPoE packet */
194 #define PPPOE_OVERHEAD 6 /* type, code, session, length */
195 #define HDR_SIZE (sizeof(struct ethhdr) + PPPOE_OVERHEAD)
196 #define MAX_PPPOE_PAYLOAD (ETH_JUMBO_LEN - PPPOE_OVERHEAD)
197 #define PPP_OVERHEAD 2 /* protocol */
198 #define MAX_PPPOE_MTU (MAX_PPPOE_PAYLOAD - PPP_OVERHEAD)
199 #define TOTAL_OVERHEAD (PPPOE_OVERHEAD + PPP_OVERHEAD)
200 #define ETH_PPPOE_MTU (ETH_DATA_LEN - TOTAL_OVERHEAD)
204 typedef struct PPPoETagStruct {
205 unsigned int type:16; /* tag type */
206 unsigned int length:16; /* Length of payload */
207 unsigned char payload[ETH_JUMBO_LEN]; /* A LOT of room to spare */
209 /* Header size of a PPPoE tag */
210 #define TAG_HDR_SIZE 4
212 /* Chunk to read from stdin */
213 #define READ_CHUNK 4096
215 /* Function passed to parsePacket */
216 typedef void ParseFunc(UINT16_t type,
221 #define PPPINITFCS16 0xffff /* Initial FCS value */
223 /* Keep track of the state of a connection -- collect everything in
226 typedef struct PPPoEConnectionStruct {
227 int discoveryState; /* Where we are in discovery */
228 int discoverySocket; /* Raw socket for discovery frames */
229 int sessionSocket; /* Raw socket for session frames */
230 unsigned char myEth[ETH_ALEN]; /* My MAC address */
231 unsigned char peerEth[ETH_ALEN]; /* Peer's MAC address */
232 unsigned char req_peer_mac[ETH_ALEN]; /* required peer MAC address */
233 unsigned char req_peer; /* require mac addr to match req_peer_mac */
234 UINT16_t session; /* Session ID */
235 char *ifName; /* Interface name */
236 char *serviceName; /* Desired service name, if any */
237 char *acName; /* Desired AC name, if any */
238 int synchronous; /* Use synchronous PPP */
239 PPPoETag hostUniq; /* Use Host-Uniq tag */
240 int printACNames; /* Just print AC names */
241 FILE *debugFile; /* Debug file for dumping packets */
242 int numPADOs; /* Number of PADO packets received */
243 PPPoETag cookie; /* We have to send this if we get it */
244 PPPoETag relayId; /* Ditto */
245 int error; /* Error packet received */
246 int debug; /* Set to log packets sent and received */
247 int discoveryTimeout; /* Timeout for discovery packets */
248 int discoveryAttempts; /* Number of discovery attempts */
250 int mtu; /* Stored MTU */
251 int mru; /* Stored MRU */
254 /* Structure used to determine acceptable PADO or PADS packet */
255 struct PacketCriteria {
256 PPPoEConnection *conn;
263 /* Function Prototypes */
264 UINT16_t etherType(PPPoEPacket *packet);
265 int openInterface(char const *ifname, UINT16_t type, unsigned char *hwaddr);
266 int sendPacket(PPPoEConnection *conn, int sock, PPPoEPacket *pkt, int size);
267 int receivePacket(int sock, PPPoEPacket *pkt, int *size);
268 void fatalSys(char const *str);
269 void rp_fatal(char const *str);
270 void printErr(char const *str);
271 void sysErr(char const *str);
272 void dumpPacket(FILE *fp, PPPoEPacket *packet, char const *dir);
273 void dumpHex(FILE *fp, unsigned char const *buf, int len);
274 int parsePacket(PPPoEPacket *packet, ParseFunc *func, void *extra);
275 void parseLogErrs(UINT16_t typ, UINT16_t len, unsigned char *data, void *xtra);
276 void syncReadFromPPP(PPPoEConnection *conn, PPPoEPacket *packet);
277 void asyncReadFromPPP(PPPoEConnection *conn, PPPoEPacket *packet);
278 void asyncReadFromEth(PPPoEConnection *conn, int sock, int clampMss);
279 void syncReadFromEth(PPPoEConnection *conn, int sock, int clampMss);
280 char *strDup(char const *str);
281 void sendPADT(PPPoEConnection *conn, char const *msg);
282 void sendSessionPacket(PPPoEConnection *conn,
283 PPPoEPacket *packet, int len);
285 void clampMSS(PPPoEPacket *packet, char const *dir, int clampMss);
286 UINT16_t computeTCPChecksum(unsigned char *ipHdr, unsigned char *tcpHdr);
287 UINT16_t pppFCS16(UINT16_t fcs, unsigned char *cp, int len);
288 void discovery(PPPoEConnection *conn);
289 unsigned char *findTag(PPPoEPacket *packet, UINT16_t tagType,
292 void pppoe_printpkt(PPPoEPacket *packet,
293 void (*printer)(void *, char *, ...), void *arg);
294 void pppoe_log_packet(const char *prefix, PPPoEPacket *packet);
296 static inline int parseHostUniq(const char *uniq, PPPoETag *tag)
298 unsigned i, len = strlen(uniq);
301 (((x) <= '9') ? ((x) - '0') : \
302 (((x) <= 'F') ? ((x) - 'A' + 10) : \
305 if (!len || len % 2 || len / 2 > sizeof(tag->payload))
308 for (i = 0; i < len; i += 2) {
309 if (!isxdigit(uniq[i]) || !isxdigit(uniq[i+1]))
312 tag->payload[i / 2] = (char)(hex(uniq[i]) << 4 | hex(uniq[i+1]));
317 tag->type = htons(TAG_HOST_UNIQ);
318 tag->length = htons(len / 2);
322 #define SET_STRING(var, val) do { if (var) free(var); var = strDup(val); } while(0);
324 #define CHECK_ROOM(cursor, start, len) \
326 if (((cursor)-(start))+(len) > MAX_PPPOE_PAYLOAD) { \
327 error("Would create too-long packet"); \
332 /* True if Ethernet address is broadcast or multicast */
333 #define NOT_UNICAST(e) ((e[0] & 0x01) != 0)
334 #define BROADCAST(e) ((e[0] & e[1] & e[2] & e[3] & e[4] & e[5]) == 0xFF)
335 #define NOT_BROADCAST(e) ((e[0] & e[1] & e[2] & e[3] & e[4] & e[5]) != 0xFF)