Home | History | Annotate | Download | only in rp-pppoe
      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.2 2004/11/04 10:07:37 paulus Exp $
     13 *
     14 ***********************************************************************/
     15 
     16 #ifdef __sun__
     17 #define __EXTENSIONS__
     18 #endif
     19 
     20 #include "config.h"
     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
     76 extern int bpfSize;
     77 struct PPPoEPacketStruct;
     78 void 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
    113 typedef unsigned short UINT16_t;
    114 #elif SIZEOF_UNSIGNED_INT == 2
    115 typedef 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
    121 typedef unsigned short UINT32_t;
    122 #elif SIZEOF_UNSIGNED_INT == 4
    123 typedef unsigned int UINT32_t;
    124 #elif SIZEOF_UNSIGNED_LONG == 4
    125 typedef unsigned long UINT32_t;
    126 #else
    127 #error Could not find a 16-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 */
    154 extern UINT16_t Eth_PPPOE_Discovery;
    155 extern UINT16_t Eth_PPPOE_Session;
    156 
    157 /* PPPoE codes */
    158 #define CODE_PADI           0x09
    159 #define CODE_PADO           0x07
    160 #define CODE_PADR           0x19
    161 #define CODE_PADS           0x65
    162 #define CODE_PADT           0xA7
    163 #define CODE_SESS           0x00
    164 
    165 /* PPPoE Tags */
    166 #define TAG_END_OF_LIST        0x0000
    167 #define TAG_SERVICE_NAME       0x0101
    168 #define TAG_AC_NAME            0x0102
    169 #define TAG_HOST_UNIQ          0x0103
    170 #define TAG_AC_COOKIE          0x0104
    171 #define TAG_VENDOR_SPECIFIC    0x0105
    172 #define TAG_RELAY_SESSION_ID   0x0110
    173 #define TAG_SERVICE_NAME_ERROR 0x0201
    174 #define TAG_AC_SYSTEM_ERROR    0x0202
    175 #define TAG_GENERIC_ERROR      0x0203
    176 
    177 /* Discovery phase states */
    178 #define STATE_SENT_PADI     0
    179 #define STATE_RECEIVED_PADO 1
    180 #define STATE_SENT_PADR     2
    181 #define STATE_SESSION       3
    182 #define STATE_TERMINATED    4
    183 
    184 /* How many PADI/PADS attempts? */
    185 #define MAX_PADI_ATTEMPTS 3
    186 
    187 /* Initial timeout for PADO/PADS */
    188 #define PADI_TIMEOUT 5
    189 
    190 /* States for scanning PPP frames */
    191 #define STATE_WAITFOR_FRAME_ADDR 0
    192 #define STATE_DROP_PROTO         1
    193 #define STATE_BUILDING_PACKET    2
    194 
    195 /* Special PPP frame characters */
    196 #define FRAME_ESC    0x7D
    197 #define FRAME_FLAG   0x7E
    198 #define FRAME_ADDR   0xFF
    199 #define FRAME_CTRL   0x03
    200 #define FRAME_ENC    0x20
    201 
    202 #define IPV4ALEN     4
    203 #define SMALLBUF   256
    204 
    205 /* A PPPoE Packet, including Ethernet headers */
    206 typedef struct PPPoEPacketStruct {
    207     struct ethhdr ethHdr;	/* Ethernet header */
    208 #ifdef PACK_BITFIELDS_REVERSED
    209     unsigned int type:4;	/* PPPoE Type (must be 1) */
    210     unsigned int ver:4;		/* PPPoE Version (must be 1) */
    211 #else
    212     unsigned int ver:4;		/* PPPoE Version (must be 1) */
    213     unsigned int type:4;	/* PPPoE Type (must be 1) */
    214 #endif
    215     unsigned int code:8;	/* PPPoE code */
    216     unsigned int session:16;	/* PPPoE session */
    217     unsigned int length:16;	/* Payload length */
    218     unsigned char payload[ETH_DATA_LEN]; /* A bit of room to spare */
    219 } PPPoEPacket;
    220 
    221 /* Header size of a PPPoE packet */
    222 #define PPPOE_OVERHEAD 6  /* type, code, session, length */
    223 #define HDR_SIZE (sizeof(struct ethhdr) + PPPOE_OVERHEAD)
    224 #define MAX_PPPOE_PAYLOAD (ETH_DATA_LEN - PPPOE_OVERHEAD)
    225 #define MAX_PPPOE_MTU (MAX_PPPOE_PAYLOAD - 2)
    226 
    227 /* PPPoE Tag */
    228 
    229 typedef struct PPPoETagStruct {
    230     unsigned int type:16;	/* tag type */
    231     unsigned int length:16;	/* Length of payload */
    232     unsigned char payload[ETH_DATA_LEN]; /* A LOT of room to spare */
    233 } PPPoETag;
    234 /* Header size of a PPPoE tag */
    235 #define TAG_HDR_SIZE 4
    236 
    237 /* Chunk to read from stdin */
    238 #define READ_CHUNK 4096
    239 
    240 /* Function passed to parsePacket */
    241 typedef void ParseFunc(UINT16_t type,
    242 		       UINT16_t len,
    243 		       unsigned char *data,
    244 		       void *extra);
    245 
    246 #define PPPINITFCS16    0xffff  /* Initial FCS value */
    247 
    248 /* Keep track of the state of a connection -- collect everything in
    249    one spot */
    250 
    251 typedef struct PPPoEConnectionStruct {
    252     int discoveryState;		/* Where we are in discovery */
    253     int discoverySocket;	/* Raw socket for discovery frames */
    254     int sessionSocket;		/* Raw socket for session frames */
    255     unsigned char myEth[ETH_ALEN]; /* My MAC address */
    256     unsigned char peerEth[ETH_ALEN]; /* Peer's MAC address */
    257     UINT16_t session;		/* Session ID */
    258     char *ifName;		/* Interface name */
    259     char *serviceName;		/* Desired service name, if any */
    260     char *acName;		/* Desired AC name, if any */
    261     int synchronous;		/* Use synchronous PPP */
    262     int useHostUniq;		/* Use Host-Uniq tag */
    263     int printACNames;		/* Just print AC names */
    264     int skipDiscovery;		/* Skip discovery */
    265     int noDiscoverySocket;	/* Don't even open discovery socket */
    266     FILE *debugFile;		/* Debug file for dumping packets */
    267     int numPADOs;		/* Number of PADO packets received */
    268     PPPoETag cookie;		/* We have to send this if we get it */
    269     PPPoETag relayId;		/* Ditto */
    270 } PPPoEConnection;
    271 
    272 /* Structure used to determine acceptable PADO or PADS packet */
    273 struct PacketCriteria {
    274     PPPoEConnection *conn;
    275     int acNameOK;
    276     int serviceNameOK;
    277     int seenACName;
    278     int seenServiceName;
    279 };
    280 
    281 /* Function Prototypes */
    282 UINT16_t etherType(PPPoEPacket *packet);
    283 int openInterface(char const *ifname, UINT16_t type, unsigned char *hwaddr);
    284 int sendPacket(PPPoEConnection *conn, int sock, PPPoEPacket *pkt, int size);
    285 int receivePacket(int sock, PPPoEPacket *pkt, int *size);
    286 void fatalSys(char const *str);
    287 void rp_fatal(char const *str);
    288 void printErr(char const *str);
    289 void sysErr(char const *str);
    290 void dumpPacket(FILE *fp, PPPoEPacket *packet, char const *dir);
    291 void dumpHex(FILE *fp, unsigned char const *buf, int len);
    292 int parsePacket(PPPoEPacket *packet, ParseFunc *func, void *extra);
    293 void parseLogErrs(UINT16_t typ, UINT16_t len, unsigned char *data, void *xtra);
    294 void syncReadFromPPP(PPPoEConnection *conn, PPPoEPacket *packet);
    295 void asyncReadFromPPP(PPPoEConnection *conn, PPPoEPacket *packet);
    296 void asyncReadFromEth(PPPoEConnection *conn, int sock, int clampMss);
    297 void syncReadFromEth(PPPoEConnection *conn, int sock, int clampMss);
    298 char *strDup(char const *str);
    299 void sendPADT(PPPoEConnection *conn, char const *msg);
    300 void sendSessionPacket(PPPoEConnection *conn,
    301 		       PPPoEPacket *packet, int len);
    302 void initPPP(void);
    303 void clampMSS(PPPoEPacket *packet, char const *dir, int clampMss);
    304 UINT16_t computeTCPChecksum(unsigned char *ipHdr, unsigned char *tcpHdr);
    305 UINT16_t pppFCS16(UINT16_t fcs, unsigned char *cp, int len);
    306 void discovery(PPPoEConnection *conn);
    307 unsigned char *findTag(PPPoEPacket *packet, UINT16_t tagType,
    308 		       PPPoETag *tag);
    309 
    310 #define SET_STRING(var, val) do { if (var) free(var); var = strDup(val); } while(0);
    311 
    312 #define CHECK_ROOM(cursor, start, len) \
    313 do {\
    314     if (((cursor)-(start))+(len) > MAX_PPPOE_PAYLOAD) { \
    315         syslog(LOG_ERR, "Would create too-long packet"); \
    316         return; \
    317     } \
    318 } while(0)
    319 
    320 /* True if Ethernet address is broadcast or multicast */
    321 #define NOT_UNICAST(e) ((e[0] & 0x01) != 0)
    322 #define BROADCAST(e) ((e[0] & e[1] & e[2] & e[3] & e[4] & e[5]) == 0xFF)
    323 #define NOT_BROADCAST(e) ((e[0] & e[1] & e[2] & e[3] & e[4] & e[5]) != 0xFF)
    324