• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src/router/accel-pptp/src/pppd/plugins/rp-pppoe/
1/***********************************************************************
2*
3* pppoe.h
4*
5* Declaration of various PPPoE constants
6*
7* Copyright (C) 2000-2012 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* LIC: GPL
13*
14* $Id$
15*
16***********************************************************************/
17
18#include "config.h"
19
20extern int IsSetID;
21
22#if defined(HAVE_NETPACKET_PACKET_H) || defined(HAVE_LINUX_IF_PACKET_H)
23#define _POSIX_SOURCE 1 /* For sigaction defines */
24#endif
25
26#include <stdio.h>		/* For FILE */
27#include <sys/types.h>		/* For pid_t */
28
29/* How do we access raw Ethernet devices? */
30#undef USE_LINUX_PACKET
31#undef USE_BPF
32
33#if defined(HAVE_NETPACKET_PACKET_H) || defined(HAVE_LINUX_IF_PACKET_H)
34#define USE_LINUX_PACKET 1
35#elif defined(HAVE_SYS_DLPI_H)
36#define USE_DLPI
37#elif defined(HAVE_NET_BPF_H)
38#define USE_BPF 1
39#endif
40
41/* Sanity check */
42#if !defined(USE_BPF) && !defined(USE_LINUX_PACKET) && !defined(USE_DLPI)
43#error Unknown method for accessing raw Ethernet frames
44#endif
45
46#ifdef HAVE_SYS_CDEFS_H
47#include <sys/cdefs.h>
48#endif
49
50#ifdef HAVE_SYS_SOCKET_H
51#include <sys/socket.h>
52#endif
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#ifdef HAVE_NET_IF_DL_H
66#include <net/if_dl.h>
67#endif
68
69/* I'm not sure why this is needed... I do not have OpenBSD */
70#if defined(__OpenBSD__)
71#include <net/ppp_defs.h>
72#include <net/if_ppp.h>
73#endif
74
75#ifdef USE_BPF
76extern int bpfSize;
77struct PPPoEPacketStruct;
78void sessionDiscoveryPacket(struct PPPoEPacketStruct *packet);
79#define BPF_BUFFER_IS_EMPTY (bpfSize <= 0)
80#define BPF_BUFFER_HAS_DATA (bpfSize > 0)
81#define ethhdr ether_header
82#define h_dest ether_dhost
83#define h_source ether_shost
84#define h_proto ether_type
85#define	ETH_DATA_LEN ETHERMTU
86#define	ETH_ALEN ETHER_ADDR_LEN
87#else
88#undef USE_BPF
89#define BPF_BUFFER_IS_EMPTY 1
90#define BPF_BUFFER_HAS_DATA 0
91#endif
92
93#ifdef USE_DLPI
94#include <sys/ethernet.h>
95#define ethhdr ether_header
96#define	ETH_DATA_LEN ETHERMTU
97#define	ETH_ALEN ETHERADDRL
98#define h_dest ether_dhost.ether_addr_octet
99#define h_source ether_shost.ether_addr_octet
100#define h_proto ether_type
101
102/* cloned from dltest.h */
103#define         MAXDLBUF        8192
104#define         MAXDLADDR       1024
105#define         MAXWAIT         15
106#define         OFFADDR(s, n)   (u_char*)((char*)(s) + (int)(n))
107#define         CASERET(s)      case s:  return ("s")
108
109#endif
110
111/* Define various integer types -- assumes a char is 8 bits */
112#if SIZEOF_UNSIGNED_SHORT == 2
113typedef unsigned short UINT16_t;
114#elif SIZEOF_UNSIGNED_INT == 2
115typedef unsigned int UINT16_t;
116#else
117#error Could not find a 16-bit integer type
118#endif
119
120#if SIZEOF_UNSIGNED_SHORT == 4
121typedef unsigned short UINT32_t;
122#elif SIZEOF_UNSIGNED_INT == 4
123typedef unsigned int UINT32_t;
124#elif SIZEOF_UNSIGNED_LONG == 4
125typedef unsigned long UINT32_t;
126#else
127#error Could not find a 32-bit integer type
128#endif
129
130#ifdef HAVE_LINUX_IF_ETHER_H
131#include <linux/if_ether.h>
132#endif
133
134#include <netinet/in.h>
135
136#ifdef HAVE_NETINET_IF_ETHER_H
137#include <sys/types.h>
138
139#ifdef HAVE_SYS_SOCKET_H
140#include <sys/socket.h>
141#endif
142#ifndef HAVE_SYS_DLPI_H
143#include <netinet/if_ether.h>
144#endif
145#endif
146
147
148
149/* Ethernet frame types according to RFC 2516 */
150#define ETH_PPPOE_DISCOVERY 0x8863
151#define ETH_PPPOE_SESSION   0x8864
152
153/* But some brain-dead peers disobey the RFC, so frame types are variables */
154extern UINT16_t Eth_PPPOE_Discovery;
155extern UINT16_t Eth_PPPOE_Session;
156
157extern void switchToRealID(void);
158extern void switchToEffectiveID(void);
159extern void dropPrivs(void);
160
161/* PPPoE codes */
162#define CODE_PADI           0x09
163#define CODE_PADO           0x07
164#define CODE_PADR           0x19
165#define CODE_PADS           0x65
166#define CODE_PADT           0xA7
167
168/* Extensions from draft-carrel-info-pppoe-ext-00 */
169/* I do NOT like PADM or PADN, but they are here for completeness */
170#define CODE_PADM           0xD3
171#define CODE_PADN           0xD4
172
173#define CODE_SESS           0x00
174
175/* PPPoE Tags */
176#define TAG_END_OF_LIST        0x0000
177#define TAG_SERVICE_NAME       0x0101
178#define TAG_AC_NAME            0x0102
179#define TAG_HOST_UNIQ          0x0103
180#define TAG_AC_COOKIE          0x0104
181#define TAG_VENDOR_SPECIFIC    0x0105
182#define TAG_RELAY_SESSION_ID   0x0110
183#define TAG_PPP_MAX_PAYLOAD    0x0120
184#define TAG_SERVICE_NAME_ERROR 0x0201
185#define TAG_AC_SYSTEM_ERROR    0x0202
186#define TAG_GENERIC_ERROR      0x0203
187
188/* Extensions from draft-carrel-info-pppoe-ext-00 */
189/* I do NOT like these tags one little bit */
190#define TAG_HURL               0x111
191#define TAG_MOTM               0x112
192#define TAG_IP_ROUTE_ADD       0x121
193
194/* Discovery phase states */
195#define STATE_SENT_PADI     0
196#define STATE_RECEIVED_PADO 1
197#define STATE_SENT_PADR     2
198#define STATE_SESSION       3
199#define STATE_TERMINATED    4
200
201/* How many PADI/PADS attempts? */
202#define MAX_PADI_ATTEMPTS 3
203
204/* Initial timeout for PADO/PADS */
205#define PADI_TIMEOUT 5
206
207/* States for scanning PPP frames */
208#define STATE_WAITFOR_FRAME_ADDR 0
209#define STATE_DROP_PROTO         1
210#define STATE_BUILDING_PACKET    2
211
212/* Special PPP frame characters */
213#define FRAME_ESC    0x7D
214#define FRAME_FLAG   0x7E
215#define FRAME_ADDR   0xFF
216#define FRAME_CTRL   0x03
217#define FRAME_ENC    0x20
218
219#define IPV4ALEN     4
220#define SMALLBUF   256
221
222/* Allow for 1500-byte PPPoE data which makes the
223   Ethernet packet size bigger by 8 bytes */
224#define ETH_JUMBO_LEN (ETH_DATA_LEN+8)
225
226/* A PPPoE Packet, including Ethernet headers */
227typedef struct PPPoEPacketStruct {
228    struct ethhdr ethHdr;	/* Ethernet header */
229#ifdef PACK_BITFIELDS_REVERSED
230    unsigned int type:4;	/* PPPoE Type (must be 1) */
231    unsigned int ver:4;		/* PPPoE Version (must be 1) */
232#else
233    unsigned int ver:4;		/* PPPoE Version (must be 1) */
234    unsigned int type:4;	/* PPPoE Type (must be 1) */
235#endif
236    unsigned int code:8;	/* PPPoE code */
237    unsigned int session:16;	/* PPPoE session */
238    unsigned int length:16;	/* Payload length */
239    unsigned char payload[ETH_JUMBO_LEN]; /* A bit of room to spare */
240} PPPoEPacket;
241
242/* Header size of a PPPoE packet */
243#define PPPOE_OVERHEAD 6  /* type, code, session, length */
244#define HDR_SIZE (sizeof(struct ethhdr) + PPPOE_OVERHEAD)
245#define MAX_PPPOE_PAYLOAD (ETH_JUMBO_LEN - PPPOE_OVERHEAD)
246#define PPP_OVERHEAD 2
247#define MAX_PPPOE_MTU (MAX_PPPOE_PAYLOAD - PPP_OVERHEAD)
248#define TOTAL_OVERHEAD (PPPOE_OVERHEAD + PPP_OVERHEAD)
249
250/* Normal PPPoE MTU without jumbo frames */
251#define ETH_PPPOE_MTU (ETH_DATA_LEN - TOTAL_OVERHEAD)
252
253/* PPPoE Tag */
254
255typedef struct PPPoETagStruct {
256    unsigned int type:16;	/* tag type */
257    unsigned int length:16;	/* Length of payload */
258    unsigned char payload[ETH_JUMBO_LEN]; /* A LOT of room to spare */
259} PPPoETag;
260/* Header size of a PPPoE tag */
261#define TAG_HDR_SIZE 4
262
263/* Chunk to read from stdin */
264#define READ_CHUNK 4096
265
266/* Function passed to parsePacket */
267typedef void ParseFunc(UINT16_t type,
268		       UINT16_t len,
269		       unsigned char *data,
270		       void *extra);
271
272#define PPPINITFCS16    0xffff  /* Initial FCS value */
273
274/* Keep track of the state of a connection -- collect everything in
275   one spot */
276
277typedef struct PPPoEConnectionStruct {
278    int discoveryState;		/* Where we are in discovery */
279    int discoverySocket;	/* Raw socket for discovery frames */
280    int sessionSocket;		/* Raw socket for session frames */
281    unsigned char myEth[ETH_ALEN]; /* My MAC address */
282    unsigned char peerEth[ETH_ALEN]; /* Peer's MAC address */
283#ifdef PLUGIN
284    unsigned char req_peer_mac[ETH_ALEN]; /* required peer MAC address */
285    unsigned char req_peer;     /* require mac addr to match req_peer_mac */
286#endif
287
288    UINT16_t session;		/* Session ID */
289    char *ifName;		/* Interface name */
290    char *serviceName;		/* Desired service name, if any */
291    char *acName;		/* Desired AC name, if any */
292    int synchronous;		/* Use synchronous PPP */
293    int useHostUniq;		/* Use Host-Uniq tag */
294    int printACNames;		/* Just print AC names */
295    int skipDiscovery;		/* Skip discovery */
296    int noDiscoverySocket;	/* Don't even open discovery socket */
297    int killSession;		/* Kill session and exit */
298    FILE *debugFile;		/* Debug file for dumping packets */
299    int numPADOs;		/* Number of PADO packets received */
300    PPPoETag cookie;		/* We have to send this if we get it */
301    PPPoETag relayId;		/* Ditto */
302    int PADSHadError;           /* If PADS had an error tag */
303    int discoveryTimeout;       /* Timeout for discovery packets */
304#ifdef PLUGIN
305    int seenMaxPayload;
306    int mtu;
307    int mru;
308#endif
309} PPPoEConnection;
310
311/* Structure used to determine acceptable PADO or PADS packet */
312struct PacketCriteria {
313    PPPoEConnection *conn;
314    int acNameOK;
315    int serviceNameOK;
316    int seenACName;
317    int seenServiceName;
318    int gotError;
319};
320
321/* Function Prototypes */
322UINT16_t etherType(PPPoEPacket *packet);
323int openInterface(char const *ifname, UINT16_t type, unsigned char *hwaddr, UINT16_t *mtu);
324int sendPacket(PPPoEConnection *conn, int sock, PPPoEPacket *pkt, int size);
325int receivePacket(int sock, PPPoEPacket *pkt, int *size);
326void fatalSys(char const *str);
327void rp_fatal(char const *str);
328void printErr(char const *str);
329void sysErr(char const *str);
330#ifdef DEBUGGING_ENABLED
331void dumpPacket(FILE *fp, PPPoEPacket *packet, char const *dir);
332void dumpHex(FILE *fp, unsigned char const *buf, int len);
333#endif
334int parsePacket(PPPoEPacket *packet, ParseFunc *func, void *extra);
335void parseLogErrs(UINT16_t typ, UINT16_t len, unsigned char *data, void *xtra);
336void pktLogErrs(char const *pkt, UINT16_t typ, UINT16_t len, unsigned char *data, void *xtra);
337void syncReadFromPPP(PPPoEConnection *conn, PPPoEPacket *packet);
338void asyncReadFromPPP(PPPoEConnection *conn, PPPoEPacket *packet);
339void asyncReadFromEth(PPPoEConnection *conn, int sock, int clampMss);
340void syncReadFromEth(PPPoEConnection *conn, int sock, int clampMss);
341char *strDup(char const *str);
342void sendPADT(PPPoEConnection *conn, char const *msg);
343void sendPADTf(PPPoEConnection *conn, char const *fmt, ...);
344
345void sendSessionPacket(PPPoEConnection *conn,
346		       PPPoEPacket *packet, int len);
347void initPPP(void);
348void clampMSS(PPPoEPacket *packet, char const *dir, int clampMss);
349UINT16_t computeTCPChecksum(unsigned char *ipHdr, unsigned char *tcpHdr);
350UINT16_t pppFCS16(UINT16_t fcs, unsigned char *cp, int len);
351void discovery(PPPoEConnection *conn);
352unsigned char *findTag(PPPoEPacket *packet, UINT16_t tagType,
353		       PPPoETag *tag);
354
355#define SET_STRING(var, val) do { if (var) free(var); var = strDup(val); } while(0);
356
357#define CHECK_ROOM(cursor, start, len) \
358do {\
359    if (((cursor)-(start))+(len) > MAX_PPPOE_PAYLOAD) { \
360        syslog(LOG_ERR, "Would create too-long packet"); \
361        return; \
362    } \
363} while(0)
364
365/* True if Ethernet address is broadcast or multicast */
366#define NOT_UNICAST(e) ((e[0] & 0x01) != 0)
367#define BROADCAST(e) ((e[0] & e[1] & e[2] & e[3] & e[4] & e[5]) == 0xFF)
368#define NOT_BROADCAST(e) ((e[0] & e[1] & e[2] & e[3] & e[4] & e[5]) != 0xFF)
369