int main(){ struct in6_addr ip6; getifipv6addr(&ip6, IF_NUM); printf("%s - IPv6 address is %s.\n", IF_NUM, ip6_ntoa(ip6)); server(ip6_ntoa(ip6)); return 0; }
int check_ns(struct pkt *p) { struct nd_neighbor_solicit *ns; struct ip6_hdr *ip6; char ip6_addr[INET6_ADDRSTRLEN]; char ip6_addr2[INET6_ADDRSTRLEN]; char *pkt_dump; int rc; ns = p->ns; ip6 = p->ip6; rc = 0; if (ip6->ip6_hlim != 255) { pkt_dump = base64_encode_packet(p); log_msg(LOG_WARNING, "%s: Malformed ICMPv6 NS packet. IPv6 Hop Limit is not 255. Packet dump: %s", p->ifc->name, pkt_dump); rc = -1; } if (p->icmp6->icmp6_code != 0) { pkt_dump = base64_encode_packet(p); log_msg(LOG_WARNING, "%s: Malformed ICMPv6 NS packet. ICMPv6 Code is not 0. Packet dump: %s", p->ifc->name, pkt_dump); rc = -1; } if (IN6_IS_ADDR_MULTICAST(&ns->nd_ns_target)) { pkt_dump = base64_encode_packet(p); ip6_ntoa((uint8_t *) &ns->nd_ns_target, ip6_addr); log_msg(LOG_WARNING, "%s: Malformed ICMPv6 NS packet. Target address is multicast (%s). Packet dump: %s", p->ifc->name, ip6_addr, pkt_dump); rc = -1; } if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { if (!IN6_IS_ADDR_SN_MULTICAST(&ip6->ip6_dst, &ns->nd_ns_target)) { ip6_ntoa((uint8_t *) &ip6->ip6_dst, ip6_addr); ip6_ntoa((uint8_t *) &ns->nd_ns_target, ip6_addr2); pkt_dump = base64_encode_packet(p); log_msg(LOG_WARNING, "%s: Malformed ICMPv6 NS packet. Src IP is unspecified and dst IP is not solicited-note multicast address (%s, %s). Packet dump: %s", p->ifc->name, ip6_addr, ip6_addr2, pkt_dump); rc = -1; } if (p->opt_slla) { pkt_dump = base64_encode_packet(p); log_msg(LOG_WARNING, "%s: Malformed ICMPv6 NS packet. Src IP is unspecified and source link-layer address option is present. Packet dump: %s", p->ifc->name, pkt_dump); rc = -1; } } return rc; }
int printIP6Header2(struct ip6_hdr *ip6, FILE *fp){ fprintf(fp, "ip6--------------------\n"); fprintf(fp, "ip6_flow = %x,", ip6->ip6_flow); fprintf(fp, "ip6_plen = %d,", ntohs(ip6->ip6_plen)); fprintf(fp, "ip6_nxt = %u,", ip6->ip6_nxt); fprintf(fp, "ip6_hlim = %d\n", ip6->ip6_hlim); fprintf(fp, "ip6_src = %s\n", ip6_ntoa(ip6->ip6_src)); fprintf(fp, "ip6_dst = %s\n", ip6_ntoa(ip6->ip6_dst)); return 0; }
static void _print_icmp6(u_char *p, int length) { struct ip6_hdr *ip6; struct icmp_hdr *icmp; ip6 = (struct ip6_hdr *)p; icmp = (struct icmp_hdr *)(p + IP6_HDR_LEN); /* XXX - truncation? */ printf("%s > %s:", ip6_ntoa(&ip6->ip6_src), ip6_ntoa(&ip6->ip6_dst)); printf(" icmp: type %hhu code %hhu", icmp->icmp_type, icmp->icmp_code); }
static void _print_frag6(u_char *p, int length) { struct ip6_hdr *ip6; struct ip6_ext_hdr *ext; int off; ip6 = (struct ip6_hdr *)p; ext = (struct ip6_ext_hdr *)(p + IP6_HDR_LEN); off = htons(ext->ext_data.fragment.offlg & IP6_OFF_MASK); printf("%s > %s:", ip6_ntoa(&ip6->ip6_src), ip6_ntoa(&ip6->ip6_dst)); printf(" fragment: next %hhu offset %d%s ident 0x%08x", ext->ext_nxt, off, (ext->ext_data.fragment.offlg & IP6_MORE_FRAG) ? " MF" : "", htonl(ext->ext_data.fragment.ident)); }
static void _print_ip6(u_char *p, int length) { struct ip6_hdr *ip6; int plen; ip6 = (struct ip6_hdr *)p; if (length < IP6_HDR_LEN) { printf("truncated-ip6 %d", length); return; } plen = htons(ip6->ip6_plen); switch (ip6->ip6_nxt) { case IP_PROTO_TCP: _print_tcp(AF_INET6, p, plen); break; case IP_PROTO_UDP: _print_udp(AF_INET6, p, plen); break; case IP_PROTO_ICMPV6: _print_icmp6(p, plen); break; case IP_PROTO_FRAGMENT: _print_frag6(p, plen); break; default: printf("%s > %s:", ip6_ntoa(&ip6->ip6_src), ip6_ntoa(&ip6->ip6_dst)); printf(" ip-proto-%hhu ttl %hhu payload len %hu", ip6->ip6_nxt, ip6->ip6_hlim, plen); break; } if (ip6->ip6_hlim <= 1) printf(" [ttl %d]", ip6->ip6_hlim); }
int check_na(struct pkt *p) { struct nd_neighbor_advert *na; struct ip6_hdr *ip6; char ip6_addr[INET6_ADDRSTRLEN]; char *pkt_dump; int rc; na = p->na; ip6 = p->ip6; rc = 0; if (ip6->ip6_hlim != 255) { pkt_dump = base64_encode_packet(p); log_msg(LOG_WARNING, "%s: Malformed ICMPv6 NA packet. IPv6 Hop Limit is not 255. Packet dump: %s", p->ifc->name, pkt_dump); rc = -1; } if (p->icmp6->icmp6_code != 0) { pkt_dump = base64_encode_packet(p); log_msg(LOG_WARNING, "%s: Malformed ICMPv6 NA packet. ICMPv6 Code is not 0. Packet dump: %s", p->ifc->name, pkt_dump); rc = -1; } if (IN6_IS_ADDR_MULTICAST(&na->nd_na_target)) { ip6_ntoa((uint8_t *) &na->nd_na_target, ip6_addr); pkt_dump = base64_encode_packet(p); log_msg(LOG_WARNING, "%s: Malformed ICMPv6 NA packet. Target address is multicast (%s). Packet dump: %s", p->ifc->name, ip6_addr, pkt_dump); rc = -1; } if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && na->nd_na_flags_reserved & ND_NA_FLAG_SOLICITED) { pkt_dump = base64_encode_packet(p); log_msg(LOG_WARNING, "%s: Malformed ICMPv6 NA packet. Dst IP is multicast address, but Solicited flag is set. Packet dump: %s", p->ifc->name, pkt_dump); rc = -1; } return rc; }
void printIP6Header(u_char *buf) { struct ip6_hdr *ptr; ptr = (struct ip6_hdr *)buf; printf("src addr=%s\n", ip6_ntoa(ptr->ip6_src)); printf("dst addr=%s\n", ip6_ntoa(ptr->ip6_dst)); }