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