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