print-isakmp.c

来自「TCPDUMP的C语言源代码,是在数据链路层的应用」· C语言 代码 · 共 2,402 行 · 第 1/4 页

C
2,402
字号
	case IV2_NOTIFY_USE_TRANSPORT_MODE:		notify_name = "use_transport_mode";		showspi = 0;		break;	case IV2_NOTIFY_HTTP_CERT_LOOKUP_SUPPORTED:		notify_name = "http_cert_lookup_supported";		showspi = 0;		break;	case IV2_NOTIFY_REKEY_SA:		notify_name = "rekey_sa";		showspi = 1;		break;	case IV2_NOTIFY_ESP_TFC_PADDING_NOT_SUPPORTED:		notify_name = "tfc_padding_not_supported";		showspi = 0;		break;	case IV2_NOTIFY_NON_FIRST_FRAGMENTS_ALSO:		notify_name = "non_first_fragment_also";		showspi = 0;		break;	default:		if (type < 8192) {			notify_name="error";		} else if(type < 16384) {			notify_name="private-error";		} else if(type < 40960) {			notify_name="status";		} else {			notify_name="private-status";		}	}	if(notify_name) {		ND_PRINT((ndo," type=%u(%s)", type, notify_name));	}			if (showspi && n.spi_size) {		ND_PRINT((ndo," spi="));		if (!rawprint(ndo, (caddr_t)(p + 1), n.spi_size))			goto trunc;	}	cp = (u_char *)(p + 1) + n.spi_size;	ep2 = (u_char *)p + item_len;	if(3 < ndo->ndo_vflag) {		showdata = 1;	}	if ((showdata || (showsomedata && ep-cp < 30)) && cp < ep) {		ND_PRINT((ndo," data=("));		if (!rawprint(ndo, (caddr_t)(cp), ep - cp))			goto trunc;		ND_PRINT((ndo,")"));	} else if(showsomedata && cp < ep) {		if(!ike_show_somedata(ndo, cp, ep)) goto trunc;	}			return (u_char *)ext + item_len;trunc:	ND_PRINT((ndo," [|%s]", NPSTR(ISAKMP_NPTYPE_N)));	return NULL;}static const u_char *ikev2_d_print(netdissect_options *ndo, u_char tpay, 		const struct isakmp_gen *ext,		u_int item_len _U_, const u_char *ep _U_,		u_int32_t phase _U_, u_int32_t doi _U_,		u_int32_t proto _U_, int depth _U_){	return ikev2_gen_print(ndo, tpay, ext);}static const u_char *ikev2_vid_print(netdissect_options *ndo, u_char tpay, 		const struct isakmp_gen *ext,		u_int item_len _U_, const u_char *ep _U_,		u_int32_t phase _U_, u_int32_t doi _U_,		u_int32_t proto _U_, int depth _U_){	struct isakmp_gen e;	const u_char *vid;	int i, len;	ND_TCHECK(*ext);	safememcpy(&e, ext, sizeof(e));	ikev2_pay_print(ndo, NPSTR(tpay), e.critical);	ND_PRINT((ndo," len=%d vid=", ntohs(e.len) - 4));		vid = (const u_char *)(ext+1);	len = ntohs(e.len) - 4;	ND_TCHECK2(*vid, len);	for(i=0; i<len; i++) {		if(isprint(vid[i])) ND_PRINT((ndo, "%c", vid[i]));		else ND_PRINT((ndo, ".", vid[i]));	}	if (2 < ndo->ndo_vflag && 4 < len) {		ND_PRINT((ndo," "));		if (!rawprint(ndo, (caddr_t)(ext + 1), ntohs(e.len) - 4))			goto trunc;	}	return (u_char *)ext + ntohs(e.len);trunc:	ND_PRINT((ndo," [|%s]", NPSTR(tpay)));	return NULL;}static const u_char *ikev2_TS_print(netdissect_options *ndo, u_char tpay, 		const struct isakmp_gen *ext,		u_int item_len _U_, const u_char *ep _U_,		u_int32_t phase _U_, u_int32_t doi _U_,		u_int32_t proto _U_, int depth _U_){	return ikev2_gen_print(ndo, tpay, ext);}static const u_char *ikev2_e_print(netdissect_options *ndo, u_char tpay, 		const struct isakmp_gen *ext,		u_int item_len _U_, const u_char *ep _U_,		u_int32_t phase _U_, u_int32_t doi _U_,		u_int32_t proto _U_, int depth _U_){	return ikev2_gen_print(ndo, tpay, ext);}static const u_char *ikev2_cp_print(netdissect_options *ndo, u_char tpay, 		const struct isakmp_gen *ext,		u_int item_len _U_, const u_char *ep _U_,		u_int32_t phase _U_, u_int32_t doi _U_,		u_int32_t proto _U_, int depth _U_){	return ikev2_gen_print(ndo, tpay, ext);}static const u_char *ikev2_eap_print(netdissect_options *ndo, u_char tpay, 		const struct isakmp_gen *ext,		u_int item_len _U_, const u_char *ep _U_,		u_int32_t phase _U_, u_int32_t doi _U_,		u_int32_t proto _U_, int depth _U_){	return ikev2_gen_print(ndo, tpay, ext);}static const u_char *ike_sub0_print(netdissect_options *ndo,		 u_char np, const struct isakmp_gen *ext, const u_char *ep,		 u_int32_t phase, u_int32_t doi, u_int32_t proto, int depth){	const u_char *cp;	struct isakmp_gen e;	u_int item_len;	cp = (u_char *)ext;	ND_TCHECK(*ext);	safememcpy(&e, ext, sizeof(e));	/*	 * Since we can't have a payload length of less than 4 bytes,	 * we need to bail out here if the generic header is nonsensical	 * or truncated, otherwise we could loop forever processing	 * zero-length items or otherwise misdissect the packet.	 */	item_len = ntohs(e.len);	if (item_len <= 4)		return NULL;	if (NPFUNC(np)) {		/*		 * XXX - what if item_len is too short, or too long,		 * for this payload type?		 */		cp = (*npfunc[np])(ndo, np, ext, item_len, ep, phase, doi, proto, depth);	} else {		ND_PRINT((ndo,"%s", NPSTR(np)));		cp += item_len;	}	return cp;trunc:	ND_PRINT((ndo," [|isakmp]"));	return NULL;}static const u_char *ikev1_sub_print(netdissect_options *ndo,		u_char np, const struct isakmp_gen *ext, const u_char *ep,		u_int32_t phase, u_int32_t doi, u_int32_t proto, int depth){	const u_char *cp;	int i;	struct isakmp_gen e;	cp = (const u_char *)ext;	while (np) {		ND_TCHECK(*ext);				safememcpy(&e, ext, sizeof(e));		ND_TCHECK2(*ext, ntohs(e.len));		depth++;		ND_PRINT((ndo,"\n"));		for (i = 0; i < depth; i++)			ND_PRINT((ndo,"    "));		ND_PRINT((ndo,"("));		cp = ike_sub0_print(ndo, np, ext, ep, phase, doi, proto, depth);		ND_PRINT((ndo,")"));		depth--;		if (cp == NULL) {			/* Zero-length subitem */			return NULL;		}		np = e.np;		ext = (struct isakmp_gen *)cp;	}	return cp;trunc:	ND_PRINT((ndo," [|%s]", NPSTR(np)));	return NULL;}static char *numstr(int x){	static char buf[20];	snprintf(buf, sizeof(buf), "#%d", x);	return buf;}/* * some compiler tries to optimize memcpy(), using the alignment constraint * on the argument pointer type.  by using this function, we try to avoid the * optimization. */static voidsafememcpy(void *p, const void *q, size_t l){	memcpy(p, q, l);}voidikev1_print(netdissect_options *ndo,	    const u_char *bp,  u_int length,	    const u_char *bp2, struct isakmp *base){	const struct isakmp *p;	const u_char *ep;	u_char np;	int i;	int phase;		p = (const struct isakmp *)bp;	ep = ndo->ndo_snapend;		phase = (*(u_int32_t *)base->msgid == 0) ? 1 : 2;	if (phase == 1)		ND_PRINT((ndo," phase %d", phase));	else		ND_PRINT((ndo," phase %d/others", phase));		i = cookie_find(&base->i_ck);	if (i < 0) {		if (iszero((u_char *)&base->r_ck, sizeof(base->r_ck))) {			/* the first packet */			ND_PRINT((ndo," I"));			if (bp2)				cookie_record(&base->i_ck, bp2);		} else			ND_PRINT((ndo," ?"));	} else {		if (bp2 && cookie_isinitiator(i, bp2))			ND_PRINT((ndo," I"));		else if (bp2 && cookie_isresponder(i, bp2))			ND_PRINT((ndo," R"));		else			ND_PRINT((ndo," ?"));	}		ND_PRINT((ndo," %s", ETYPESTR(base->etype)));	if (base->flags) {		ND_PRINT((ndo,"[%s%s]", base->flags & ISAKMP_FLAG_E ? "E" : "",			  base->flags & ISAKMP_FLAG_C ? "C" : ""));	}		if (ndo->ndo_vflag) {		const struct isakmp_gen *ext;		int nparen;				ND_PRINT((ndo,":"));				/* regardless of phase... */		if (base->flags & ISAKMP_FLAG_E) {			/*			 * encrypted, nothing we can do right now.			 * we hope to decrypt the packet in the future...			 */			ND_PRINT((ndo," [encrypted %s]", NPSTR(base->np)));			goto done;		}				nparen = 0;		CHECKLEN(p + 1, base->np);		np = base->np;		ext = (struct isakmp_gen *)(p + 1);		ikev1_sub_print(ndo, np, ext, ep, phase, 0, 0, 0);	}	done:	if (ndo->ndo_vflag) {		if (ntohl(base->len) != length) {			ND_PRINT((ndo," (len mismatch: isakmp %u/ip %u)",				  (u_int32_t)ntohl(base->len), length));		}	}}static const u_char *ikev2_sub0_print(netdissect_options *ndo, u_char np, int pcount,		 const struct isakmp_gen *ext, const u_char *ep,		 u_int32_t phase, u_int32_t doi, u_int32_t proto, int depth){	const u_char *cp;	struct isakmp_gen e;	u_int item_len;	cp = (u_char *)ext;	ND_TCHECK(*ext);	safememcpy(&e, ext, sizeof(e));	/*	 * Since we can't have a payload length of less than 4 bytes,	 * we need to bail out here if the generic header is nonsensical	 * or truncated, otherwise we could loop forever processing	 * zero-length items or otherwise misdissect the packet.	 */	item_len = ntohs(e.len);	if (item_len <= 4)		return NULL;	if(np == ISAKMP_NPTYPE_P) {		cp = ikev2_p_print(ndo, np, pcount, ext, item_len,				   ep, phase, doi, proto, depth);	} else if(np == ISAKMP_NPTYPE_T) {		cp = ikev2_t_print(ndo, np, pcount, ext, item_len,				   ep, phase, doi, proto, depth);	} else if (NPFUNC(np)) {		/*		 * XXX - what if item_len is too short, or too long,		 * for this payload type?		 */		cp = (*npfunc[np])(ndo, np, /*pcount,*/ ext, item_len,				   ep, phase, doi, proto, depth);	} else {		ND_PRINT((ndo,"%s", NPSTR(np)));		cp += item_len;	}	return cp;trunc:	ND_PRINT((ndo," [|isakmp]"));	return NULL;}static const u_char *ikev2_sub_print(netdissect_options *ndo,		u_char np, const struct isakmp_gen *ext, const u_char *ep,		u_int32_t phase, u_int32_t doi, u_int32_t proto, int depth){	const u_char *cp;	int i;	int pcount;	struct isakmp_gen e;	cp = (const u_char *)ext;	pcount = 0;							while (np) {		pcount++;		ND_TCHECK(*ext);				safememcpy(&e, ext, sizeof(e));		ND_TCHECK2(*ext, ntohs(e.len));		depth++;		ND_PRINT((ndo,"\n"));		for (i = 0; i < depth; i++)			ND_PRINT((ndo,"    "));		ND_PRINT((ndo,"("));		cp = ikev2_sub0_print(ndo, np, pcount,				      ext, ep, phase, doi, proto, depth);		ND_PRINT((ndo,")"));		depth--;		if (cp == NULL) {			/* Zero-length subitem */			return NULL;		}		np = e.np;		ext = (struct isakmp_gen *)cp;	}	return cp;trunc:	ND_PRINT((ndo," [|%s]", NPSTR(np)));	return NULL;}static voidikev2_print(netdissect_options *ndo,	    const u_char *bp,  u_int length,	    const u_char *bp2 _U_, struct isakmp *base){	const struct isakmp *p;	const u_char *ep;	u_char np;	int phase;	p = (const struct isakmp *)bp;	ep = ndo->ndo_snapend;	phase = (*(u_int32_t *)base->msgid == 0) ? 1 : 2;	if (phase == 1)		ND_PRINT((ndo, " parent_sa"));	else		ND_PRINT((ndo, " child_sa "));	ND_PRINT((ndo, " %s", ETYPESTR(base->etype)));	if (base->flags) {		ND_PRINT((ndo, "[%s%s]",			  base->flags & ISAKMP_FLAG_I ? "I" : "",			  base->flags & ISAKMP_FLAG_V ? "V" : "",			  base->flags & ISAKMP_FLAG_R ? "R" : ""));	}	if (ndo->ndo_vflag) {		const struct isakmp_gen *ext;		int nparen;		ND_PRINT((ndo, ":"));		/* regardless of phase... */		if (base->flags & ISAKMP_FLAG_E) {			/*			 * encrypted, nothing we can do right now.			 * we hope to decrypt the packet in the future...			 */			ND_PRINT((ndo, " [encrypted %s]", NPSTR(base->np)));			goto done;		}		nparen = 0;		CHECKLEN(p + 1, base->np)		np = base->np;		ext = (struct isakmp_gen *)(p + 1);		ikev2_sub_print(ndo, np, ext, ep, phase, 0, 0, 0);	}done:	if (ndo->ndo_vflag) {		if (ntohl(base->len) != length) {			ND_PRINT((ndo, " (len mismatch: isakmp %u/ip %u)",				  (u_int32_t)ntohl(base->len), length));		}	}}voidisakmp_print(netdissect_options *ndo,	     const u_char *bp, u_int length,	     const u_char *bp2){	const struct isakmp *p;	struct isakmp base;	const u_char *ep;	int major, minor;	p = (const struct isakmp *)bp;	ep = ndo->ndo_snapend;	if ((struct isakmp *)ep < p + 1) {		ND_PRINT((ndo,"[|isakmp]"));		return;	}	safememcpy(&base, p, sizeof(base));	ND_PRINT((ndo,"isakmp"));	major = (base.vers & ISAKMP_VERS_MAJOR)		>> ISAKMP_VERS_MAJOR_SHIFT;	minor = (base.vers & ISAKMP_VERS_MINOR)		>> ISAKMP_VERS_MINOR_SHIFT;	if (ndo->ndo_vflag) {		ND_PRINT((ndo," %d.%d", major, minor));	}	if (ndo->ndo_vflag) {		ND_PRINT((ndo," msgid "));		rawprint(ndo, (caddr_t)&base.msgid, sizeof(base.msgid));	}	if (1 < ndo->ndo_vflag) {		ND_PRINT((ndo," cookie "));		rawprint(ndo, (caddr_t)&base.i_ck, sizeof(base.i_ck));		ND_PRINT((ndo,"->"));		rawprint(ndo, (caddr_t)&base.r_ck, sizeof(base.r_ck));	}	ND_PRINT((ndo,":"));	switch(major) {	case IKEv1_MAJOR_VERSION:		ikev1_print(ndo, bp, length, bp2, &base);		break;	case IKEv2_MAJOR_VERSION:		ikev2_print(ndo, bp, length, bp2, &base);		break;	}}voidisakmp_rfc3948_print(netdissect_options *ndo,		     const u_char *bp, u_int length,		     const u_char *bp2){	const u_char *ep;	ep = ndo->ndo_snapend;	if(length == 1 && bp[0]==0xff) {		ND_PRINT((ndo, "isakmp-nat-keep-alive"));		return;	}	if(length < 4) {		goto trunc;	}		/*	 * see if this is an IKE packet	 */	if(bp[0]==0 && bp[1]==0 && bp[2]==0 && bp[3]==0) {		ND_PRINT((ndo, "NONESP-encap: "));		isakmp_print(ndo, bp+4, length-4, bp2);		return;	}	/* must be an ESP packet */	{		int nh, enh, padlen;		int advance;		ND_PRINT((ndo, "UDP-encap: "));		advance = esp_print(ndo, bp, length, bp2, &enh, &padlen);		if(advance <= 0)			return;		bp += advance;		length -= advance + padlen;		nh = enh & 0xff;	     		ip_print_inner(ndo, bp, length, nh, bp2);		return;	}trunc:	ND_PRINT((ndo,"[|isakmp]"));	return;}/* * Local Variables: * c-style: whitesmith * c-basic-offset: 8 * End: */  

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?