X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/c67afe913011138a2504ec4d3d423b48e73b12f3..cbddb98484ea8ec1deece351abd56e063d775b38:/print-ip.c diff --git a/print-ip.c b/print-ip.c index f178fef3..ccf61a3f 100644 --- a/print-ip.c +++ b/print-ip.c @@ -19,18 +19,19 @@ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ -#define NETDISSECT_REWORKED +/* \summary: IP printer */ + #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include +#include #include -#include "interface.h" +#include "netdissect.h" #include "addrtoname.h" -#include "extract.h" /* must come after interface.h */ +#include "extract.h" #include "ip.h" #include "ipproto.h" @@ -123,7 +124,7 @@ ip_finddst(netdissect_options *ndo, } } trunc: - UNALIGNED_MEMCPY(&retval, &ip->ip_dst.s_addr, sizeof(uint32_t)); + UNALIGNED_MEMCPY(&retval, &ip->ip_dst, sizeof(uint32_t)); return retval; } @@ -148,9 +149,9 @@ nextproto4_cksum(netdissect_options *ndo, ph.len = htons((uint16_t)len); ph.mbz = 0; ph.proto = next_proto; - UNALIGNED_MEMCPY(&ph.src, &ip->ip_src.s_addr, sizeof(uint32_t)); + UNALIGNED_MEMCPY(&ph.src, &ip->ip_src, sizeof(uint32_t)); if (IP_HL(ip) == 5) - UNALIGNED_MEMCPY(&ph.dst, &ip->ip_dst.s_addr, sizeof(uint32_t)); + UNALIGNED_MEMCPY(&ph.dst, &ip->ip_dst, sizeof(uint32_t)); else ph.dst = ip_finddst(ndo, ip); @@ -323,13 +324,16 @@ static void ip_print_demux(netdissect_options *ndo, struct ip_print_demux_state *ipds) { - struct protoent *proto; - struct cksum_vec vec[1]; + const char *p_name; again: switch (ipds->nh) { case IPPROTO_AH: + if (!ND_TTEST(*ipds->cp)) { + ND_PRINT((ndo, "[|AH]")); + break; + } ipds->nh = *ipds->cp; ipds->advance = ah_print(ndo, ipds->cp); if (ipds->advance <= 0) @@ -354,14 +358,14 @@ again: case IPPROTO_IPCOMP: { - int enh; - ipds->advance = ipcomp_print(ndo, ipds->cp, &enh); - if (ipds->advance <= 0) - break; - ipds->cp += ipds->advance; - ipds->len -= ipds->advance; - ipds->nh = enh & 0xff; - goto again; + ipcomp_print(ndo, ipds->cp); + /* + * Either this has decompressed the payload and + * printed it, in which case there's nothing more + * to do, or it hasn't, in which case there's + * nothing more to do. + */ + break; } case IPPROTO_SCTP: @@ -455,9 +459,7 @@ again: break; case IPPROTO_PIM: - vec[0].ptr = ipds->cp; - vec[0].len = ipds->len; - pim_print(ndo, ipds->cp, ipds->len, in_cksum(vec, 1)); + pim_print(ndo, ipds->cp, ipds->len, (const u_char *)ipds->ip); break; case IPPROTO_VRRP: @@ -482,8 +484,8 @@ again: break; default: - if (ndo->ndo_nflag==0 && (proto = getprotobynumber(ipds->nh)) != NULL) - ND_PRINT((ndo, " %s", proto->p_name)); + if (ndo->ndo_nflag==0 && (p_name = netdb_protoname(ipds->nh)) != NULL) + ND_PRINT((ndo, " %s", p_name)); else ND_PRINT((ndo, " ip-proto-%d", ipds->nh)); ND_PRINT((ndo, " %d", ipds->len)); @@ -524,16 +526,18 @@ ip_print(netdissect_options *ndo, u_int hlen; struct cksum_vec vec[1]; uint16_t sum, ip_sum; - struct protoent *proto; + const char *p_name; ipds->ip = (const struct ip *)bp; ND_TCHECK(ipds->ip->ip_vhl); - if (IP_V(ipds->ip) != 4) { /* print version if != 4 */ - ND_PRINT((ndo, "IP%u ", IP_V(ipds->ip))); + if (IP_V(ipds->ip) != 4) { /* print version and fail if != 4 */ if (IP_V(ipds->ip) == 6) - ND_PRINT((ndo, ", wrong link-layer encapsulation")); + ND_PRINT((ndo, "IP6, wrong link-layer encapsulation ")); + else + ND_PRINT((ndo, "IP%u ", IP_V(ipds->ip))); + return; } - else if (!ndo->ndo_eflag) + if (!ndo->ndo_eflag) ND_PRINT((ndo, "IP ")); ND_TCHECK(*ipds->ip); @@ -581,17 +585,22 @@ ip_print(netdissect_options *ndo, if (ndo->ndo_vflag) { ND_PRINT((ndo, "(tos 0x%x", (int)ipds->ip->ip_tos)); /* ECN bits */ - if (ipds->ip->ip_tos & 0x03) { - switch (ipds->ip->ip_tos & 0x03) { - case 1: - ND_PRINT((ndo, ",ECT(1)")); - break; - case 2: - ND_PRINT((ndo, ",ECT(0)")); - break; - case 3: - ND_PRINT((ndo, ",CE")); - } + switch (ipds->ip->ip_tos & 0x03) { + + case 0: + break; + + case 1: + ND_PRINT((ndo, ",ECT(1)")); + break; + + case 2: + ND_PRINT((ndo, ",ECT(0)")); + break; + + case 3: + ND_PRINT((ndo, ",CE")); + break; } if (ipds->ip->ip_ttl >= 1) @@ -614,12 +623,12 @@ ip_print(netdissect_options *ndo, if ((hlen - sizeof(struct ip)) > 0) { ND_PRINT((ndo, ", options (")); - ip_optprint(ndo, (u_char *)(ipds->ip + 1), hlen - sizeof(struct ip)); + ip_optprint(ndo, (const u_char *)(ipds->ip + 1), hlen - sizeof(struct ip)); ND_PRINT((ndo, ")")); } - if (!ndo->ndo_Kflag && (u_char *)ipds->ip + hlen <= ndo->ndo_snapend) { - vec[0].ptr = (const uint8_t *)(void *)ipds->ip; + if (!ndo->ndo_Kflag && (const u_char *)ipds->ip + hlen <= ndo->ndo_snapend) { + vec[0].ptr = (const uint8_t *)(const void *)ipds->ip; vec[0].len = hlen; sum = in_cksum(vec, 1); if (sum != 0) { @@ -648,22 +657,24 @@ ip_print(netdissect_options *ndo, } ip_print_demux(ndo, ipds); } else { - /* Ultra quiet now means that all this stuff should be suppressed */ - if (ndo->ndo_qflag > 1) return; + /* + * Ultra quiet now means that all this stuff should be + * suppressed. + */ + if (ndo->ndo_qflag > 1) + return; - /* - * if this isn't the first frag, we're missing the - * next level protocol header. print the ip addr - * and the protocol. - */ - if (ipds->off & 0x1fff) { - ND_PRINT((ndo, "%s > %s:", ipaddr_string(ndo, &ipds->ip->ip_src), - ipaddr_string(ndo, &ipds->ip->ip_dst))); - if (!ndo->ndo_nflag && (proto = getprotobynumber(ipds->ip->ip_p)) != NULL) - ND_PRINT((ndo, " %s", proto->p_name)); - else - ND_PRINT((ndo, " ip-proto-%d", ipds->ip->ip_p)); - } + /* + * This isn't the first frag, so we're missing the + * next level protocol header. print the ip addr + * and the protocol. + */ + ND_PRINT((ndo, "%s > %s:", ipaddr_string(ndo, &ipds->ip->ip_src), + ipaddr_string(ndo, &ipds->ip->ip_dst))); + if (!ndo->ndo_nflag && (p_name = netdb_protoname(ipds->ip->ip_p)) != NULL) + ND_PRINT((ndo, " %s", p_name)); + else + ND_PRINT((ndo, " ip-proto-%d", ipds->ip->ip_p)); } return; @@ -675,24 +686,28 @@ trunc: void ipN_print(netdissect_options *ndo, register const u_char *bp, register u_int length) { - struct ip hdr; - - if (length < 4) { + if (length < 1) { ND_PRINT((ndo, "truncated-ip %d", length)); return; } - memcpy (&hdr, bp, 4); - switch (IP_V(&hdr)) { - case 4: + + ND_TCHECK(*bp); + switch (*bp & 0xF0) { + case 0x40: ip_print (ndo, bp, length); - return; - case 6: + break; + case 0x60: ip6_print (ndo, bp, length); - return; + break; default: - ND_PRINT((ndo, "unknown ip %d", IP_V(&hdr))); - return; + ND_PRINT((ndo, "unknown ip %d", (*bp & 0xF0) >> 4)); + break; } + return; + +trunc: + ND_PRINT((ndo, "%s", tstr)); + return; } /*