/** initialize the basic android values */ void android_init_values(void) { const char *text; text = get_android_manufacturer(); if(text) { write_to_file("/sys/class/android_usb/android0/iManufacturer", text); g_free((char *)text); } text = get_android_vendor_id(); if(text) { write_to_file("/sys/class/android_usb/android0/idVendor", text); g_free((char *)text); } text = get_android_product(); if(text) { write_to_file("/sys/class/android_usb/android0/iProduct", text); g_free((char *)text); } text = get_android_product_id(); if(text) { write_to_file("/sys/class/android_usb/android0/idProduct", text); g_free((char *)text); } text = read_mac(); if(text) { write_to_file("/sys/class/android_usb/f_rndis/ethaddr", text); g_free((char *)text); } }
static int read_staticlease(const char *const_line, void *arg) { char *line; char *mac_string; char *ip_string; uint8_t *mac_bytes; uint32_t *ip; /* Allocate memory for addresses */ mac_bytes = xmalloc(sizeof(unsigned char) * 8); ip = xmalloc(sizeof(uint32_t)); /* Read mac */ line = (char *) const_line; mac_string = strtok(line, " \t"); read_mac(mac_string, mac_bytes); /* Read ip */ ip_string = strtok(NULL, " \t"); read_ip(ip_string, ip); addStaticLease(arg, mac_bytes, ip); #ifdef UDHCP_DEBUG printStaticLeases(arg); #endif return 1; }
int load_lease(struct dhcpOfferedAddr *lease, FILE *fp) { char buffer[256]; char *ip, *mac; if (fgets(buffer, sizeof(buffer), fp)==NULL) return 0; mac = buffer; ip = strsep(&mac, ","); read_ip(ip, &lease->yiaddr); read_mac(mac, &lease->chaddr); lease->expires = 0xffffffff; return 1; }
/** @brief Open and configure the TAP device for packet read/write. * * This routine creates the interface via the tuntap driver then uses ifconfig * to configure address/mask and MTU. * * @param device - [inout] a device info holder object * @param dev - user-defined name for the new iface, * if NULL system will assign a name * @param device_ip - address of iface * @param device_mask - netmask for device_ip * @param mtu - MTU for device_ip * * @return - negative value on error * - non-negative file-descriptor on success */ int tuntap_open(tuntap_dev *device, char *dev, /* user-definable interface name, eg. edge0 */ char *device_ip, char *device_mask, const char * device_mac, int mtu) { char *tuntap_device = "/dev/net/tun"; #define N2N_LINUX_SYSTEMCMD_SIZE 128 char buf[N2N_LINUX_SYSTEMCMD_SIZE]; struct ifreq ifr; int rc; device->fd = open(tuntap_device, O_RDWR); if(device->fd < 0) { printf("ERROR: ioctl() [%s][%d]\n", strerror(errno), errno); return -1; } memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = IFF_TAP|IFF_NO_PI; /* Want a TAP device for layer 2 frames. */ strncpy(ifr.ifr_name, dev, IFNAMSIZ); rc = ioctl(device->fd, TUNSETIFF, (void *)&ifr); if(rc < 0) { traceEvent(TRACE_ERROR, "ioctl() [%s][%d]\n", strerror(errno), rc); close(device->fd); return -1; } if ( device_mac ) { /* Set the hw address before bringing the if up. */ snprintf(buf, sizeof(buf), "/sbin/ifconfig %s hw ether %s", ifr.ifr_name, device_mac ); system(buf); traceEvent(TRACE_INFO, "Setting MAC: %s", buf); } snprintf(buf, sizeof(buf), "/sbin/ifconfig %s %s netmask %s mtu %d up", ifr.ifr_name, device_ip, device_mask, mtu); system(buf); traceEvent(TRACE_INFO, "Bringing up: %s", buf); device->ip_addr = inet_addr(device_ip); device->device_mask = inet_addr(device_mask); read_mac(dev, (char*)device->mac_addr); return(device->fd); }
static void check_wifi_module (void) { const char path[] = "/sys/wifi_properties/wifi_module"; char wifi_module[20]; int fd; if (0xff == is_wifi_module_4330) { // read the mac just once xiaomi_wifi_mac = read_mac(); fd = open(path, O_RDONLY); if (fd < 0) { ALOGE("unable to open %s: %s", path, strerror(errno)); return ; } if (read(fd, wifi_module, sizeof(wifi_module)) < 0) { ALOGE("read %s failed: %s", path, strerror(errno)); close(fd); return ; } if (0 == strncmp(wifi_module, "wifi_module=4330", 16)) { is_wifi_module_4330 = 1; strcpy(DRIVER_MODULE_NAME, DRIVER_MODULE_NAME_DHD); strcpy(DRIVER_MODULE_TAG, DRIVER_MODULE_TAG_DHD); strcpy(DRIVER_MODULE_PATH, DRIVER_MODULE_PATH_DHD); } else { is_wifi_module_4330 = 0; strcpy(DRIVER_MODULE_ARG, DRIVER_MODULE_ARG_DHD); } ALOGW("is_wifi_module_4330: %d", is_wifi_module_4330); close(fd); } }
/** @brief Open and configure the TAP device for packet read/write. * * This routine creates the interface via the tuntap driver then uses ifconfig * to configure address/mask and MTU. * * @param device - [inout] a device info holder object * @param dev - user-defined name for the new iface, * if NULL system will assign a name * @param device_ip - address of iface * @param device_mask - netmask for device_ip * @param mtu - MTU for device_ip * * @return - negative value on error * - non-negative file-descriptor on success */ int tuntap_open(tuntap_dev_t *device, ip_mode_t ip_mode) //char *dev, /* user-definable interface name, eg. edge0 */ //const char *address_mode, /* static or dhcp */ //char *device_ip, //char *device_mask, //const char *device_mac, //int mtu) { char *tuntap_device = "/dev/net/tun"; #define N2N_LINUX_SYSTEMCMD_SIZE 128 char buf[N2N_LINUX_SYSTEMCMD_SIZE]; struct ifreq ifr; int rc; //TODO ipstr_t ipstr; device->fd = open(tuntap_device, O_RDWR); if (device->fd < 0) { traceEvent(TRACE_ERROR, "Unable to open TUN/TAP device: ioctl() [%s][%d]\n", strerror(errno), errno); return -1; } traceEvent(TRACE_NORMAL, "Succesfully open %s\n", tuntap_device); memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = IFF_TAP | IFF_NO_PI; /* Want a TAP device for layer 2 frames. */ strncpy(ifr.ifr_name, device->dev_name, IFNAMSIZ); rc = ioctl(device->fd, TUNSETIFF, (void *) &ifr); if (rc < 0) { traceError("ioctl() [%s][%d]\n", strerror(errno), rc); close(device->fd); return -1; } /* Store the device name for later reuse */ strncpy(device->dev_name, ifr.ifr_name, MIN(IFNAMSIZ, N2N_IFNAMSIZ)); if ( !is_empty_mac(device->mac_addr) ) { /* Set the hw address before bringing the if up. */ macstr_t macstr; snprintf(buf, sizeof(buf), "/sbin/ifconfig %s hw ether %s", ifr.ifr_name, mac2str(macstr, device->mac_addr)); system(buf); traceInfo("Setting MAC: %s", buf); } ipv4_to_str(ipstr, sizeof(ipstr_t), (const uint8_t *) &device->ip_addr);//TODO make array if (ip_mode == N2N_IPM_DHCP) { snprintf(buf, sizeof(buf), "/sbin/ifconfig %s %s mtu %d up", ifr.ifr_name, ipstr, device->mtu); } else { ipstr_t maskstr; strcpy((char *) maskstr, inet_ntoa( *( (struct in_addr *) &device->device_mask) ));//TODO //intoa(device->device_mask, maskstr, sizeof(maskstr)); snprintf(buf, sizeof(buf), "/sbin/ifconfig %s %s netmask %s mtu %d up", ifr.ifr_name, ipstr, maskstr, device->mtu); } traceInfo("Bringing up: %s", buf); system(buf); //device->ip_addr = inet_addr(device_ip); //device->device_mask = inet_addr(device_mask); read_mac(device->dev_name, device->mac_addr); return (device->fd); }
int create_raw_eth_socket(char *arg, int is_output) { int sk; if ((sk = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) { perror("raw socket"); exit(1); } char src_mac[6], dst_mac[6], proto; short type, dst_port; int has_src_mac = 0, has_dst_mac = 0, has_type = 0, has_proto = 0; int has_dst_port = 0; if (strtok(arg, ",") != 0) { do { if (strncmp("src=", arg, 4) == 0) { read_mac(arg + 4, src_mac); has_src_mac = 1; } else if (strncmp("dst=", arg, 4) == 0) { read_mac(arg + 4, dst_mac); has_dst_mac = 1; } else if (strncmp("type=", arg, 5) == 0) { type = atoi(arg + 5); has_type = 1; } else if (strncmp("proto=", arg, 6) == 0) { proto = atoi(arg + 6); has_proto = 1; } else if (strncmp("dst_port=", arg, 9) == 0) { dst_port = atoi(arg + 9); has_dst_port = 1; } else { fprintf(stderr, "unknown option \"%s\"", arg); exit(1); } } while ((arg = strtok(0, ",")) != 0); #define MAX_FILTER_LENGTH 16 struct sock_filter mac_filter_insn[MAX_FILTER_LENGTH], *prog = mac_filter_insn + MAX_FILTER_LENGTH; unsigned jump_offset = 1; // FILTER IS CONSTRUCTED IN REVERSED ORDER! // * -- prog = (struct sock_filter) BPF_STMT(BPF_RET + BPF_K, 0); // wrong packet; ret 0 * -- prog = (struct sock_filter) BPF_STMT(BPF_RET + BPF_K, 65535); // right packet; ret everything if (has_proto) { * -- prog = (struct sock_filter) BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, proto, 0 , jump_offset); * -- prog = (struct sock_filter) BPF_STMT(BPF_LD + BPF_B + BPF_ABS , 14 + 9); jump_offset += 2; } if (has_type) { * -- prog = (struct sock_filter) BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, htons(type), 0 , jump_offset); * -- prog = (struct sock_filter) BPF_STMT(BPF_LD + BPF_H + BPF_ABS , 12); jump_offset += 2; } if (has_dst_port) { * -- prog = (struct sock_filter) BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, htons(dst_port), 0 , jump_offset); * -- prog = (struct sock_filter) BPF_STMT(BPF_LD + BPF_H + BPF_ABS , 14 + 20 + 2); jump_offset += 2; } if (has_src_mac) { * -- prog = (struct sock_filter) BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, htonl(* (int *) (src_mac + 2)), 0 , jump_offset); * -- prog = (struct sock_filter) BPF_STMT(BPF_LD + BPF_W + BPF_ABS , 8); jump_offset += 2; * -- prog = (struct sock_filter) BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, htons(* (short *) src_mac), 0 , jump_offset); * -- prog = (struct sock_filter) BPF_STMT(BPF_LD + BPF_H + BPF_ABS , 6); jump_offset += 2; } if (has_dst_mac) { * -- prog = (struct sock_filter) BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, htons(* (short *) (dst_mac + 4)), 0 , jump_offset); * -- prog = (struct sock_filter) BPF_STMT(BPF_LD + BPF_H + BPF_ABS , 4); jump_offset += 2; * -- prog = (struct sock_filter) BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, htonl(* (int *) dst_mac), 0 , jump_offset); * -- prog = (struct sock_filter) BPF_STMT(BPF_LD + BPF_W + BPF_ABS , 0); jump_offset += 2; } struct sock_fprog filter; memset(&filter, 0, sizeof(struct sock_fprog)); filter.filter = prog; filter.len = mac_filter_insn + MAX_FILTER_LENGTH - prog; if (setsockopt(sk, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)) < 0) { perror("error creating filter"); exit(1); } } #if 1 struct tpacket_req req; req.tp_block_size = 131072; req.tp_block_nr = 64; req.tp_frame_size = 8192; req.tp_frame_nr = req.tp_block_size / req.tp_frame_size * req.tp_block_nr; if (setsockopt(sk, SOL_PACKET, PACKET_RX_RING, &req, sizeof req) < 0) { perror("ring buffer setsockopt"); exit(1); } if ((ring_buffer = mmap(0, req.tp_block_size * req.tp_block_nr, PROT_READ|PROT_WRITE, MAP_SHARED, sk, 0)) == MAP_FAILED) { perror("ring buffer mmap"); exit(1); } #endif return sk; }
/** @brief Open and configure the TAP device for packet read/write. * * This routine creates the interface via the tuntap driver then uses ifconfig * to configure address/mask and MTU. * * @param device - [inout] a device info holder object * @param dev - user-defined name for the new iface, * if NULL system will assign a name * @param device_ip - address of iface * @param device_mask - netmask for device_ip * @param mtu - MTU for device_ip * * @return - negative value on error * - non-negative file-descriptor on success */ int tuntap_open(tuntap_dev *device, char *dev, /* user-definable interface name, eg. edge0 */ const char *address_mode, /* static or dhcp */ char *device_ip, char *device_mask, const char * device_mac, int mtu) { char *tuntap_device = "/dev/net/tun"; #define N2N_LINUX_SYSTEMCMD_SIZE 128 char buf[N2N_LINUX_SYSTEMCMD_SIZE]; struct ifreq ifr; int rc; device->fd = open(tuntap_device, O_RDWR); if(device->fd < 0) { traceEvent(TRACE_ERROR, "Unable to open TUN/TAP device: ioctl() [%s][%d]\n", strerror(errno), errno); return -1; } traceEvent(TRACE_NORMAL, "Succesfully open %s\n", tuntap_device); memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = IFF_TAP|IFF_NO_PI; /* Want a TAP device for layer 2 frames. */ strncpy(ifr.ifr_name, dev, IFNAMSIZ); rc = ioctl(device->fd, TUNSETIFF, (void *)&ifr); if(rc < 0) { traceEvent(TRACE_ERROR, "ioctl() [%s][%d]\n", strerror(errno), rc); close(device->fd); return -1; } /* Store the device name for later reuse */ strncpy(device->dev_name, ifr.ifr_name, MIN(IFNAMSIZ, N2N_IFNAMSIZ) ); if ( device_mac && device_mac[0] != '\0' ) { /* Set the hw address before bringing the if up. */ snprintf(buf, sizeof(buf), "/sbin/ifconfig %s hw ether %s", ifr.ifr_name, device_mac ); system(buf); traceEvent(TRACE_INFO, "Setting MAC: %s", buf); } if ( 0 == strncmp( "dhcp", address_mode, 5 ) ) { snprintf(buf, sizeof(buf), "/sbin/ifconfig %s %s mtu %d up", ifr.ifr_name, device_ip, mtu); } else { snprintf(buf, sizeof(buf), "/sbin/ifconfig %s %s netmask %s mtu %d up", ifr.ifr_name, device_ip, device_mask, mtu); } system(buf); traceEvent(TRACE_INFO, "Bringing up: %s", buf); device->ip_addr = inet_addr(device_ip); device->device_mask = inet_addr(device_mask); read_mac(dev, device->mac_addr); return(device->fd); }