📄 pppoe.c
字号:
struct ifnet_head ifhead; struct ifnet intf; unsigned long v; char ifn[IFNAMSIZ+1]; struct arpcom arp; k = kvm_open(NULL,NULL,NULL,O_RDONLY,"pppoe"); if (k == NULL) { fprintf(stderr, "pppoe: failed to open kvm\n"); return -1; } n[0].n_name = "_ifnet"; n[1].n_name = NULL; if (kvm_nlist(k,n) != 0) { fprintf(stderr, "pppoe: could not find interface list\n"); kvm_close(k); return -1; } if (kvm_read(k,n[0].n_value,(void *)&ifhead,sizeof(ifhead)) != sizeof(ifhead)) { fprintf(stderr, "pppoe: could not read ifnet_head structure\n"); kvm_close(k); return -1; } v = (unsigned long)(ifhead.tqh_first); while(v != 0) { if (kvm_read(k,v,(void *)&intf,sizeof(intf)) != sizeof(intf)) { fprintf(stderr, "pppoe: could not read ifnet structure\n"); kvm_close(k); return -1; } strncpy(ifn,intf.if_xname,IFNAMSIZ); ifn[IFNAMSIZ] = '\0'; if (strcmp(ifn,if_name) == 0) /* found our interface */ break; else /* walk the chain */ v = (unsigned long)(intf.if_list.tqe_next); } if (v == 0) { fprintf(stderr, "pppoe: cannot find interface %s in kernel\n",if_name); kvm_close(k); return -1; } /* since we have the right interface, and we determined previously that it is an ethernet interface, reread from the same address into a "struct arpcom" structure (which begins with a struct ifnet). The ethernet address is located past the end of the ifnet structure */ if (kvm_read(k,v,(void *)&arp,sizeof(arp)) != sizeof(arp)) { fprintf(stderr, "could not read arpcom structure\n"); kvm_close(k); return -1; } /* whew! */ /* save a copy of this for ourselves */ memcpy(local_ether,arp.ac_enaddr,ETH_ALEN); if (hw_addr) memcpy(hw_addr,arp.ac_enaddr,ETH_ALEN); /* also copy if requested */ kvm_close(k); } /* setup BPF filter */ { union { unsigned int i; unsigned char b[4]; } x; union { unsigned short i; unsigned char b[2]; } y; filt[1].k = type; /* set type of packet we are looking for */#ifndef SIMPLE_BPF /* now setup our source address so it gets filtered out */ for(i = 0; i < 4; i++) x.b[i] = local_ether[i]; for(i = 0; i < 2; i++) y.b[i] = local_ether[i+4]; filt[3].k = x.i; filt[5].k = y.i;#endif /* SIMPLE_BPF */ } prog.bf_insns = filt; prog.bf_len = sizeof(filt)/sizeof(struct bpf_insn); if (ioctl(fd, BIOCSETF, &prog) < 0) { perror("pppoe: bpf(BIOCSETF)"); return -1; } return fd;#else /* do regular linux stuff */ int optval = 1, rv; struct ifreq ifr; if ((rv = socket(PF_INET, SOCK_PACKET, htons(type))) < 0) { perror("pppoe: socket"); return -1; } if (setsockopt(rv, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) < 0) { perror("pppoe: setsockopt"); return -1; } if (hw_addr != NULL) { strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name)); if (ioctl(rv, SIOCGIFHWADDR, &ifr) < 0) { perror("pppoe: ioctl(SIOCGIFHWADDR)"); return -1; } if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) { fprintf(error_file, "pppoe: interface %s is not Ethernet!\n", if_name); return -1; } memcpy(hw_addr, ifr.ifr_hwaddr.sa_data, sizeof(ifr.ifr_hwaddr.sa_data)); } return rv;#endif /* USE_BPF / linux */}int create_padi(struct pppoe_packet *packet, const char *src, const char *name){ int size; if (packet == NULL) return 0; size = sizeof(struct pppoe_packet) + sizeof(struct pppoe_tag); if (name != NULL) size += strlen(name);#ifdef __linux__ memcpy(packet->ethhdr.h_dest, MAC_BCAST_ADDR, 6); memcpy(packet->ethhdr.h_source, src, 6); packet->ethhdr.h_proto = htons(ETH_P_PPPOE_DISC);#else memcpy(packet->ethhdr.ether_dhost, MAC_BCAST_ADDR, 6); memcpy(packet->ethhdr.ether_shost, src, 6); packet->ethhdr.ether_type = htons(ETH_P_PPPOE_DISC);#endif packet->ver = 1; packet->type = 1; packet->code = CODE_PADI; packet->session = 0; packet->length = htons(size - sizeof(struct pppoe_packet)); /* fill out a blank service-name tag */ (*(struct pppoe_tag *)(packet+1)).type = htons(TAG_SERVICE_NAME); (*(struct pppoe_tag *)(packet+1)).length = name ? htons(strlen(name)) : 0; if (name != NULL) memcpy((char *)(packet + 1) + sizeof(struct pppoe_tag), name, strlen(name)); return size;}int create_padr(struct pppoe_packet *packet, const char *src, const char *dst, char *name){ int size; if (packet == NULL) return 0; size = sizeof(struct pppoe_packet) + sizeof(struct pppoe_tag); if (name != NULL) size += strlen(name);#ifdef __linux__ memcpy(packet->ethhdr.h_dest, dst, 6); memcpy(packet->ethhdr.h_source, src, 6); packet->ethhdr.h_proto = htons(ETH_P_PPPOE_DISC);#else memcpy(packet->ethhdr.ether_dhost, dst, 6); memcpy(packet->ethhdr.ether_shost, src, 6); packet->ethhdr.ether_type = htons(ETH_P_PPPOE_DISC);#endif packet->ver = 1; packet->type = 1; packet->code = CODE_PADR; packet->session = 0; packet->length = htons(size - sizeof(struct pppoe_packet)); /* fill out a blank service-name tag */ (*(struct pppoe_tag *)(packet+1)).type = htons(TAG_SERVICE_NAME); (*(struct pppoe_tag *)(packet+1)).length = name ? htons(strlen(name)) : 0; if (name != NULL) memcpy((char *)(packet + 1) + sizeof(struct pppoe_tag), name, strlen(name)); memset(((char *)packet) + size, 0, 14); return size;}unsigned short fcstab[256] = { 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78};#define PPPINITFCS16 0xffff /* Initial FCS value */#define PPPGOODFCS16 0xf0b8 /* Good final FCS value *//* * Calculate a new fcs given the current fcs and the new data. */unsigned short pppfcs16(register unsigned short fcs, register unsigned char * cp, register int len){/* assert(sizeof (unsigned short) == 2); assert(((unsigned short) -1) > 0); */ while (len--) fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff]; return (fcs);}#define FRAME_ESC 0x7d#define FRAME_FLAG 0x7e#define FRAME_ADDR 0xff#define FRAME_CTL 0x03#define FRAME_ENC 0x20#define ADD_OUT(c) { *out++ = (c); n++; if (opt_verbose) fprintf(log_file, "%x ", (c)); }void encode_ppp(int fd, unsigned char *buf, int len){ static int first = 0; unsigned char out_buf[PACKETBUF]; unsigned char *out = out_buf; unsigned char header[2], tail[2]; int i,n; unsigned short fcs; time_t tm; header[0] = FRAME_ADDR; header[1] = FRAME_CTL; fcs = pppfcs16(PPPINITFCS16, header, 2); fcs = pppfcs16(fcs, buf, len) ^ 0xffff; tail[0] = fcs & 0x00ff; tail[1] = (fcs >> 8) & 0x00ff; if (opt_verbose) { time(&tm); fprintf(log_file, "%sWriting to pppd: \n", ctime(&tm)); } n = 0; if (!first) { ADD_OUT(FRAME_FLAG); first = 1; } ADD_OUT(FRAME_ADDR); /* the header - which is constant */ ADD_OUT(FRAME_ESC); ADD_OUT(FRAME_CTL ^ FRAME_ENC); for (i = 0; i < len; i++) if (buf[i] == FRAME_FLAG || buf[i] == FRAME_ESC || buf[i] < 0x20) { ADD_OUT(FRAME_ESC); ADD_OUT(buf[i] ^ FRAME_ENC); } else ADD_OUT(buf[i]); for (i = 0; i < 2; i++) { if (tail[i] == FRAME_FLAG || tail[i] == FRAME_ESC || tail[i] < 0x20) { ADD_OUT(FRAME_ESC); ADD_OUT(tail[i] ^ FRAME_ENC); } else ADD_OUT(tail[i]); } ADD_OUT(FRAME_FLAG); write(fd, out_buf, n); if (opt_verbose) fprintf(log_file, "\n");}int create_sess(struct pppoe_packet *packet, const char *src, const char *dst, unsigned char *buf, int bufsize, int sess){ int size; int i, o = 0; if (opt_fwd || !((buf[0] == FRAME_FLAG) || (buf[0] == FRAME_ADDR))) { if (opt_fwd_search) /* search for a valid packet */ { while (*buf++ != FRAME_FLAG && bufsize != 0) bufsize--; if (bufsize == 0) return 0; } else { fprintf(error_file, "create_sess: invalid data\n"); return 0; } } for (i = (buf[0] == FRAME_FLAG ? 4 : 3); i < bufsize - 1; i++) if (buf[i] == FRAME_ESC) buf[o++] = buf[++i] ^ FRAME_ENC; else buf[o++] = buf[i]; bufsize = o - 2; /* ignore fcs */ if (packet == NULL) return 0; size = sizeof(struct pppoe_packet) + bufsize;#ifdef __linux__ memcpy(packet->ethhdr.h_dest, dst, 6); memcpy(packet->ethhdr.h_source, src, 6); packet->ethhdr.h_proto = htons(ETH_P_PPPOE_SESS);#else memcpy(packet->ethhdr.ether_dhost, dst, 6); memcpy(packet->ethhdr.ether_shost, src, 6); packet->ethhdr.ether_type = htons(ETH_P_PPPOE_SESS);#endif packet->ver = 1; packet->type = 1; packet->code = CODE_SESS; packet->session = sess; packet->length = htons(size - sizeof(struct pppoe_packet)); /* fill out payload */ memcpy(packet + 1, buf, bufsize); return size;}int send_packet(int sock, struct pppoe_packet *packet, int len, const char *ifn){#ifdef USE_BPF int c; if ((c = write(sock,packet,len)) != len) perror("pppoe: write (send_packet)"); return c;#else /* regular linux stuff */ struct sockaddr addr; int c; time_t tm; memset(&addr, 0, sizeof(addr)); strcpy(addr.sa_data, ifn); if (opt_verbose == 1) { time(&tm); fprintf(log_file, "%sSending ", ctime(&tm)); print_packet(packet); fputc('\n', log_file); } if ((c = sendto(sock, packet, len, 0, &addr, sizeof(addr))) < 0) perror("pppoe: sendto (send_packet)"); return c;#endif /* USE_BPF */}#ifdef USE_BPF/* return: -1 == error, 0 == okay, 1 == ignore this packet */int read_bpf_packet(int fd, struct pppoe_packet *packet) { /* Nastiness - BPF may return multiple packets in one fell swoop */ /* This makes select() difficult to use - you need to be ready to clear out packets as they arrive */ static char *buf = NULL; static int lastdrop = 0; static int n = 0, off = 0; struct bpf_hdr *h; if (buf == NULL) { if ((buf = malloc(bpf_buf_size)) == NULL) { perror("pppoe:malloc"); return -1; } } if (off < n) { /* read out of previously grabbed buffer */ if (n-off < sizeof(struct bpf_hdr)) { fprintf(stderr, "BPF: not enough left for header: %d\n", n-off); off = n = 0; /* force reread from BPF next time */ return 1; /* try again */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -