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