]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/rp-pppoe/pppoe.h
c4aaa6e68856e8506643a6c30c86177070af1fd7
[ppp.git] / pppd / plugins / rp-pppoe / pppoe.h
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 /* This has to be included before Linux 4.8's linux/in.h
51  * gets dragged in. */
52 #include <netinet/in.h>
53
54 /* Ugly header files on some Linux boxes... */
55 #if defined(HAVE_LINUX_IF_H)
56 #include <linux/if.h>
57 #elif defined(HAVE_NET_IF_H)
58 #include <net/if.h>
59 #endif
60
61 #ifdef HAVE_NET_IF_TYPES_H
62 #include <net/if_types.h>
63 #endif
64
65 #define BPF_BUFFER_IS_EMPTY 1
66 #define BPF_BUFFER_HAS_DATA 0
67
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;
73 #else
74 #error Could not find a 16-bit integer type
75 #endif
76
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;
83 #else
84 #error Could not find a 32-bit integer type
85 #endif
86
87 #ifdef HAVE_LINUX_IF_ETHER_H
88 #include <linux/if_ether.h>
89 #endif
90
91 #ifdef HAVE_NETINET_IF_ETHER_H
92 #include <sys/types.h>
93
94 #ifdef HAVE_SYS_SOCKET_H
95 #include <sys/socket.h>
96 #endif
97 #ifndef HAVE_SYS_DLPI_H
98 #include <netinet/if_ether.h>
99 #endif
100 #endif
101
102
103 /* Ethernet frame types according to RFC 2516 */
104 #define ETH_PPPOE_DISCOVERY 0x8863
105 #define ETH_PPPOE_SESSION   0x8864
106
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;
110
111 /* PPPoE codes */
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
117
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
122
123 #define CODE_SESS           0x00
124
125 /* PPPoE Tags */
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
137
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
143
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
150
151 /* How many PADI/PADS attempts? */
152 #define MAX_PADI_ATTEMPTS 3
153
154 /* Initial timeout for PADO/PADS */
155 #define PADI_TIMEOUT 5
156
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
161
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
168
169 #define IPV4ALEN     4
170 #define SMALLBUF   256
171
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
176    enough. */
177 #define ETH_JUMBO_LEN 1508
178
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 */
187 } PPPoEPacket;
188
189 #define PPPOE_VER(vt)           ((vt) >> 4)
190 #define PPPOE_TYPE(vt)          ((vt) & 0xf)
191 #define PPPOE_VER_TYPE(v, t)    (((v) << 4) | (t))
192
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)
201
202 /* PPPoE Tag */
203
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 */
208 } PPPoETag;
209 /* Header size of a PPPoE tag */
210 #define TAG_HDR_SIZE 4
211
212 /* Chunk to read from stdin */
213 #define READ_CHUNK 4096
214
215 /* Function passed to parsePacket */
216 typedef void ParseFunc(UINT16_t type,
217                        UINT16_t len,
218                        unsigned char *data,
219                        void *extra);
220
221 #define PPPINITFCS16    0xffff  /* Initial FCS value */
222
223 /* Keep track of the state of a connection -- collect everything in
224    one spot */
225
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     int useHostUniq;            /* 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 seenMaxPayload;
249     int mtu;                    /* Stored MTU */
250     int mru;                    /* Stored MRU */
251 } PPPoEConnection;
252
253 /* Structure used to determine acceptable PADO or PADS packet */
254 struct PacketCriteria {
255     PPPoEConnection *conn;
256     int acNameOK;
257     int serviceNameOK;
258     int seenACName;
259     int seenServiceName;
260 };
261
262 /* Function Prototypes */
263 UINT16_t etherType(PPPoEPacket *packet);
264 int openInterface(char const *ifname, UINT16_t type, unsigned char *hwaddr);
265 int sendPacket(PPPoEConnection *conn, int sock, PPPoEPacket *pkt, int size);
266 int receivePacket(int sock, PPPoEPacket *pkt, int *size);
267 void fatalSys(char const *str);
268 void rp_fatal(char const *str);
269 void printErr(char const *str);
270 void sysErr(char const *str);
271 void dumpPacket(FILE *fp, PPPoEPacket *packet, char const *dir);
272 void dumpHex(FILE *fp, unsigned char const *buf, int len);
273 int parsePacket(PPPoEPacket *packet, ParseFunc *func, void *extra);
274 void parseLogErrs(UINT16_t typ, UINT16_t len, unsigned char *data, void *xtra);
275 void syncReadFromPPP(PPPoEConnection *conn, PPPoEPacket *packet);
276 void asyncReadFromPPP(PPPoEConnection *conn, PPPoEPacket *packet);
277 void asyncReadFromEth(PPPoEConnection *conn, int sock, int clampMss);
278 void syncReadFromEth(PPPoEConnection *conn, int sock, int clampMss);
279 char *strDup(char const *str);
280 void sendPADT(PPPoEConnection *conn, char const *msg);
281 void sendSessionPacket(PPPoEConnection *conn,
282                        PPPoEPacket *packet, int len);
283 void initPPP(void);
284 void clampMSS(PPPoEPacket *packet, char const *dir, int clampMss);
285 UINT16_t computeTCPChecksum(unsigned char *ipHdr, unsigned char *tcpHdr);
286 UINT16_t pppFCS16(UINT16_t fcs, unsigned char *cp, int len);
287 void discovery(PPPoEConnection *conn);
288 unsigned char *findTag(PPPoEPacket *packet, UINT16_t tagType,
289                        PPPoETag *tag);
290
291 void pppoe_printpkt(PPPoEPacket *packet,
292                     void (*printer)(void *, char *, ...), void *arg);
293 void pppoe_log_packet(const char *prefix, PPPoEPacket *packet);
294
295 #define SET_STRING(var, val) do { if (var) free(var); var = strDup(val); } while(0);
296
297 #define CHECK_ROOM(cursor, start, len) \
298 do {\
299     if (((cursor)-(start))+(len) > MAX_PPPOE_PAYLOAD) { \
300         error("Would create too-long packet");  \
301         return; \
302     } \
303 } while(0)
304
305 /* True if Ethernet address is broadcast or multicast */
306 #define NOT_UNICAST(e) ((e[0] & 0x01) != 0)
307 #define BROADCAST(e) ((e[0] & e[1] & e[2] & e[3] & e[4] & e[5]) == 0xFF)
308 #define NOT_BROADCAST(e) ((e[0] & e[1] & e[2] & e[3] & e[4] & e[5]) != 0xFF)