int border_add_addr(const char *ip_addr) { struct in6_addr parsed_addr; if (inet_pton(AF_INET6, ip_addr, &parsed_addr) != 1) { return -1; } tun_add_addr(ip_addr); multiplex_send_addr_over_tty(&parsed_addr); return 0; }
int border_initialize(char *if_name, const char *ip_addr, const char *tty_dev) { int res, i; char command[21 + IPV6_ADDR_LEN + IF_NAME_LEN]; char ip_addr_cpy[IPV6_ADDR_LEN]; struct in6_addr parsed_addr; strncpy(ip_addr_cpy, ip_addr, IPV6_ADDR_LEN); strtok(ip_addr_cpy, "/"); if ((res = inet_pton(AF_INET6, ip_addr_cpy, &parsed_addr)) != 1) { return res; } if ((res = init_multiplex(tty_dev)) != 0) { return res; } tun_fd = open_tun(if_name, IFF_TUN); printf("INFO: ip link set %s up\n", if_name); sprintf(command, "ip link set %s up", if_name); strncpy(tun_if_name, if_name, IF_NAME_LEN); if ((res = system(command)) != 0) { return res; } if ((res = tun_add_addr(ip_addr)) != 0) { return res; } // initialize context cache as empty. for (i = 0; i < MAXIMUM_CONTEXTS; i++) { context_cache[i].cid = 0xFF; } pthread_create(&serial_reader, NULL, serial_reader_f, NULL); hard_reset_to_user_code(); flowcontrol_init(&parsed_addr); pthread_create(&tun_reader, NULL, tun_reader_f, NULL); return 0; }
void demultiplex(const border_packet_t *packet, int len) { switch(packet->type) { case (BORDER_PACKET_RAW_TYPE): { printf("\033[00;33m[via serial interface] %s\033[00m\n", ((unsigned char *)packet) + sizeof(border_packet_t) ); break; } case (BORDER_PACKET_L3_TYPE): { border_l3_header_t *l3_header_buf = (border_l3_header_t *)packet; switch(l3_header_buf->ethertype) { case (ETHERTYPE_IPV6): { printf("INFO: IPv6-Packet %d received\n", l3_header_buf->seq_num); struct ip6_hdr *ip6_buf = (struct ip6_hdr *)(((unsigned char *)packet) + sizeof(border_l3_header_t)); border_send_ipv6_over_tun(get_tun_fd(), ip6_buf); break; } default: printf("INFO: Unknown ethertype %04x for packet %d\n", l3_header_buf->ethertype, l3_header_buf->seq_num); break; } break; } case (BORDER_PACKET_CONF_TYPE): { border_conf_header_t *conf_header_buf = (border_conf_header_t *)packet; switch(conf_header_buf->conftype) { case (BORDER_CONF_SYNACK): { printf("INFO: SYNACK-Packet %d received\n", conf_header_buf->seq_num); signal_connection_established(); break; } case (BORDER_CONF_CONTEXT): { printf("INFO: Context packet (%d) received, " "but nothing is implemented yet for this case.\n", conf_header_buf->seq_num); break; } case (BORDER_CONF_IPADDR): { char str_addr[IPV6_ADDR_LEN]; border_addr_packet_t *addr_packet = (border_addr_packet_t *)packet; printf("INFO: Address packet (%d) received.\n", conf_header_buf->seq_num); inet_ntop(AF_INET6, &addr_packet->addr, str_addr, IPV6_ADDR_LEN); tun_add_addr(str_addr); } default: printf("INFO: Unknown conftype %02x for packet %d\n", conf_header_buf->conftype, conf_header_buf->seq_num); break; } break; } default: printf("INFO: Unknown border packet type %02x for packet %d\n", packet->type, packet->seq_num); //print_packet_hex((unsigned char *)packet,len); break; } }