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