Skip to content

Commit b97d738

Browse files
committed
ICMP: Modernize packet parsing
Remove the icmp_print() unused parameter 'bp2'. Enable ND_LONGJMP_FROM_TCHECK and remove a 'trunc' label. Remove some redundant ND_TCHECK_*(). Reduce the scope of some variables. Fix some indentations. Remove some extra blank lines.
1 parent 5fefba1 commit b97d738

File tree

4 files changed

+35
-33
lines changed

4 files changed

+35
-33
lines changed

CHANGES

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ DayOfTheWeek, Month DD, YYYY / The Tcpdump Group
2626
Remove unused missing/snprintf.c.
2727
Remove unused missing/strdup.c.
2828
(FIXME: somebody please wrap the line below just before the release)
29-
AODV, AppleTalk, BOOTP, CHDLC, DCCP, EAP, EGP, EIGRP, ForCES, Geneve, GRE, Juniper, L2TP, mobile, NetFlow, NTP, OLSR, PGM, RADIUS, RIP, RSVP, SCTP, SNMP, TCP, UDP, vsock: Modernize packet parsing style
29+
AODV, AppleTalk, BOOTP, CHDLC, DCCP, EAP, EGP, EIGRP, ForCES, Geneve, GRE, ICMP, Juniper, L2TP, mobile, NetFlow, NTP, OLSR, PGM, RADIUS, RIP, RSVP, SCTP, SNMP, TCP, UDP, vsock: Modernize packet parsing style
3030
DCCP, EGP: Replace custom code with tok2str()
3131
UDP: Clean up address and port printing.
3232
AppleTalk: Declutter appletalk.h.

netdissect.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ extern void hncp_print(netdissect_options *, const u_char *, u_int);
657657
extern void hsrp_print(netdissect_options *, const u_char *, u_int);
658658
extern void http_print(netdissect_options *, const u_char *, u_int);
659659
extern void icmp6_print(netdissect_options *, const u_char *, u_int, const u_char *, int);
660-
extern void icmp_print(netdissect_options *, const u_char *, u_int, const u_char *, int);
660+
extern void icmp_print(netdissect_options *, const u_char *, u_int, int);
661661
extern u_int ieee802_11_radio_print(netdissect_options *, const u_char *, u_int, u_int);
662662
extern u_int ieee802_15_4_print(netdissect_options *, const u_char *, u_int);
663663
extern void igmp_print(netdissect_options *, const u_char *, u_int);

print-icmp.c

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <stdio.h>
3131
#include <string.h>
3232

33+
#define ND_LONGJMP_FROM_TCHECK
3334
#include "netdissect.h"
3435
#include "addrtoname.h"
3536
#include "extract.h"
@@ -333,7 +334,6 @@ struct icmp_interface_identification_ifname_subobject_t {
333334
nd_byte if_name[63];
334335
};
335336

336-
337337
/* prototypes */
338338
const char *icmp_tstamp_print(u_int);
339339

@@ -354,34 +354,21 @@ icmp_tstamp_print(u_int tstamp)
354354
}
355355

356356
void
357-
icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *bp2,
357+
icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen,
358358
int fragmented)
359359
{
360-
char *cp;
361360
const struct icmp *dp;
362361
uint8_t icmp_type, icmp_code;
363-
const struct icmp_ext_t *ext_dp;
364-
const struct ip *ip;
362+
const struct icmp_ext_t *ext_dp;
365363
const char *str;
366-
const struct ip *oip;
367-
uint8_t ip_proto;
368-
const struct udphdr *ouh;
369-
const uint8_t *obj_tptr;
370-
uint32_t raw_label;
371-
const struct icmp_multipart_ext_object_header_t *icmp_multipart_ext_object_header;
372-
u_int interface_role, if_index_flag, ipaddr_flag, name_flag, mtu_flag;
373-
const uint8_t *offset;
374-
const struct icmp_interface_identification_ipaddr_subobject_t *ipaddr_subobj;
375-
const struct icmp_interface_identification_ifname_subobject_t *ifname_subobj;
376-
u_int hlen, mtu, obj_tlen, obj_class_num, obj_ctype;
377-
uint16_t dport;
364+
const uint8_t *obj_tptr;
365+
u_int hlen;
378366
char buf[MAXHOSTNAMELEN + 100];
379367
struct cksum_vec vec[1];
380368

381369
ndo->ndo_protocol = "icmp";
382370
dp = (const struct icmp *)bp;
383-
ext_dp = (const struct icmp_ext_t *)bp;
384-
ip = (const struct ip *)bp2;
371+
ext_dp = (const struct icmp_ext_t *)bp;
385372
str = buf;
386373

387374
icmp_type = GET_U_1(dp->icmp_type);
@@ -420,7 +407,12 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
420407
break;
421408

422409
case ICMP_UNREACH_PORT:
423-
ND_TCHECK_1(dp->icmp_ip.ip_p);
410+
{
411+
const struct ip *oip;
412+
const struct udphdr *ouh;
413+
uint8_t ip_proto;
414+
uint16_t dport;
415+
424416
oip = &dp->icmp_ip;
425417
hlen = IP_HL(oip) * 4;
426418
ouh = (const struct udphdr *)(((const u_char *)oip) + hlen);
@@ -450,10 +442,13 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
450442
break;
451443
}
452444
break;
445+
}
453446

454447
case ICMP_UNREACH_NEEDFRAG:
455448
{
456449
const struct mtu_discovery *mp;
450+
u_int mtu;
451+
457452
mp = (const struct mtu_discovery *)(const u_char *)&dp->icmp_void;
458453
mtu = GET_BE_U_2(mp->nexthopmtu);
459454
if (mtu) {
@@ -585,6 +580,7 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
585580

586581
case ICMP_ROUTERADVERT:
587582
{
583+
char *cp;
588584
const struct ih_rdiscovery *ihp;
589585
const struct id_rdiscovery *idp;
590586
u_int lifetime, num, size;
@@ -593,7 +589,6 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
593589
cp = buf + strlen(buf);
594590

595591
ihp = (const struct ih_rdiscovery *)&dp->icmp_void;
596-
ND_TCHECK_SIZE(ihp);
597592
(void)strncpy(cp, " lifetime ", sizeof(buf) - (cp - buf));
598593
cp = buf + strlen(buf);
599594
lifetime = GET_BE_U_2(ihp->ird_lifetime);
@@ -624,13 +619,12 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
624619
}
625620
idp = (const struct id_rdiscovery *)&dp->icmp_data;
626621
while (num > 0) {
627-
ND_TCHECK_SIZE(idp);
628622
(void)snprintf(cp, sizeof(buf) - (cp - buf), " {%s %u}",
629623
GET_IPADDR_STRING(idp->ird_addr),
630624
GET_BE_U_4(idp->ird_pref));
631625
cp = buf + strlen(buf);
632626
++idp;
633-
num--;
627+
num--;
634628
}
635629
}
636630
break;
@@ -678,7 +672,6 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
678672
break;
679673

680674
case ICMP_TSTAMPREPLY:
681-
ND_TCHECK_4(dp->icmp_ttime);
682675
(void)snprintf(buf, sizeof(buf),
683676
"time stamp reply id %u seq %u: org %s",
684677
GET_BE_U_2(dp->icmp_id),
@@ -717,6 +710,7 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
717710
* save the snaplength as this may get overridden in the IP printer.
718711
*/
719712
if (ndo->ndo_vflag >= 1 && ICMP_ERRTYPE(icmp_type)) {
713+
const struct ip *ip;
720714
const u_char *snapend_save;
721715

722716
bp += 8;
@@ -745,7 +739,6 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
745739
* Attempt to decode multi-part message extensions (rfc4884) only for some ICMP types.
746740
*/
747741
if (ndo->ndo_vflag >= 1 && plen > ICMP_EXTD_MINLEN && ICMP_MULTIPART_EXT_TYPE(icmp_type)) {
748-
749742
ND_TCHECK_SIZE(ext_dp);
750743

751744
/*
@@ -789,9 +782,10 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
789782
obj_tptr = (const uint8_t *)ext_dp->icmp_ext_data;
790783

791784
while (hlen > sizeof(struct icmp_multipart_ext_object_header_t)) {
785+
u_int obj_tlen, obj_class_num, obj_ctype;
786+
const struct icmp_multipart_ext_object_header_t *icmp_multipart_ext_object_header;
792787

793788
icmp_multipart_ext_object_header = (const struct icmp_multipart_ext_object_header_t *)obj_tptr;
794-
ND_TCHECK_SIZE(icmp_multipart_ext_object_header);
795789
obj_tlen = GET_BE_U_2(icmp_multipart_ext_object_header->length);
796790
obj_class_num = GET_U_1(icmp_multipart_ext_object_header->class_num);
797791
obj_ctype = GET_U_1(icmp_multipart_ext_object_header->ctype);
@@ -816,18 +810,23 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
816810
case MPLS_STACK_ENTRY_OBJECT_CLASS:
817811
switch(obj_ctype) {
818812
case 1:
813+
{
814+
uint32_t raw_label;
815+
819816
raw_label = GET_BE_U_4(obj_tptr);
820817
ND_PRINT("\n\t label %u, tc %u", MPLS_LABEL(raw_label), MPLS_TC(raw_label));
821818
if (MPLS_STACK(raw_label))
822819
ND_PRINT(", [S]");
823820
ND_PRINT(", ttl %u", MPLS_TTL(raw_label));
824821
break;
822+
}
825823
default:
826824
print_unknown_data(ndo, obj_tptr, "\n\t ", obj_tlen);
827825
}
828826
break;
829827

830828
case INTERFACE_INFORMATION_OBJECT_CLASS:
829+
{
831830
/*
832831
Ctype in a INTERFACE_INFORMATION_OBJECT_CLASS object:
833832
@@ -836,6 +835,9 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
836835
| Interface Role| Rsvd1 | Rsvd2 |ifIndex| IPAddr| name | MTU |
837836
+-------+-------+-------+-------+-------+-------+-------+-------+
838837
*/
838+
const uint8_t *offset;
839+
u_int interface_role, if_index_flag, ipaddr_flag, name_flag, mtu_flag;
840+
839841
interface_role = (obj_ctype & 0xc0) >> 6;
840842
if_index_flag = (obj_ctype & 0x8) >> 3;
841843
ipaddr_flag = (obj_ctype & 0x4) >> 2;
@@ -853,6 +855,8 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
853855
offset += 4;
854856
}
855857
if (ipaddr_flag) {
858+
const struct icmp_interface_identification_ipaddr_subobject_t *ipaddr_subobj;
859+
856860
ND_PRINT("\n\t\t IP Address sub-object: ");
857861
ipaddr_subobj = (const struct icmp_interface_identification_ipaddr_subobject_t *) offset;
858862
switch (GET_BE_U_2(ipaddr_subobj->afi)) {
@@ -872,6 +876,7 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
872876
}
873877
if (name_flag) {
874878
uint8_t inft_name_length_field;
879+
const struct icmp_interface_identification_ifname_subobject_t *ifname_subobj;
875880

876881
ifname_subobj = (const struct icmp_interface_identification_ifname_subobject_t *) offset;
877882
inft_name_length_field = GET_U_1(ifname_subobj->length);
@@ -903,6 +908,7 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
903908
offset += 4;
904909
}
905910
break;
911+
}
906912

907913
default:
908914
print_unknown_data(ndo, obj_tptr, "\n\t ", obj_tlen);
@@ -914,8 +920,4 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
914920
obj_tptr += obj_tlen;
915921
}
916922
}
917-
918-
return;
919-
trunc:
920-
nd_print_trunc(ndo);
921923
}

print-ip-demux.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ ip_demux_print(netdissect_options *ndo,
104104

105105
case IPPROTO_ICMP:
106106
if (ver == 4)
107-
icmp_print(ndo, bp, length, iph, fragmented);
107+
icmp_print(ndo, bp, length, fragmented);
108108
else {
109109
ND_PRINT("[%s requires IPv4]",
110110
tok2str(ipproto_values,"unknown",nh));

0 commit comments

Comments
 (0)