xref: /illumos-gate/usr/src/uts/common/inet/ipf/misc.c (revision 381a2a9a)
17c478bd9Sstevel@tonic-gate /*
2ab25eeb5Syz  * Copyright (C) 2003 by Darren Reed.
3ab25eeb5Syz  *
4ab25eeb5Syz  * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate  */
6*381a2a9aSdr /*
7*381a2a9aSdr  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
8*381a2a9aSdr  * Use is subject to license terms.
9*381a2a9aSdr  */
107c478bd9Sstevel@tonic-gate #ifndef __hpux
11ab25eeb5Syz #pragma ident "@(#)$Id: misc.c,v 1.12 2003/11/29 07:11:03 darrenr Exp $"
127c478bd9Sstevel@tonic-gate #else
137c478bd9Sstevel@tonic-gate struct uio;
147c478bd9Sstevel@tonic-gate #endif
157c478bd9Sstevel@tonic-gate 
16*381a2a9aSdr #pragma ident	"%Z%%M%	%I%	%E% SMI"
17*381a2a9aSdr 
187c478bd9Sstevel@tonic-gate #include <sys/systm.h>
197c478bd9Sstevel@tonic-gate #include <sys/types.h>
207c478bd9Sstevel@tonic-gate #include <sys/stream.h>
21*381a2a9aSdr #include <sys/ddi.h>
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate #ifdef	__hpux
247c478bd9Sstevel@tonic-gate # define	BCOPY(a,b,c)	bcopy((caddr_t)a, (caddr_t)b, c)
257c478bd9Sstevel@tonic-gate #endif
267c478bd9Sstevel@tonic-gate #ifdef	sun
277c478bd9Sstevel@tonic-gate # define	BCOPY(a,b,c)	bcopy((char *)a, (char *)b, c)
287c478bd9Sstevel@tonic-gate #endif
297c478bd9Sstevel@tonic-gate 
mb_copydata(min,off,len,buf)307c478bd9Sstevel@tonic-gate void mb_copydata(min, off, len, buf)
317c478bd9Sstevel@tonic-gate mblk_t *min;
327c478bd9Sstevel@tonic-gate size_t off, len;
337c478bd9Sstevel@tonic-gate char *buf;
347c478bd9Sstevel@tonic-gate {
357c478bd9Sstevel@tonic-gate 	u_char *s, *bp = (u_char *)buf;
367c478bd9Sstevel@tonic-gate 	size_t mlen, olen, clen;
377c478bd9Sstevel@tonic-gate 	mblk_t *m;
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 	for (m = min; (m != NULL) && (len > 0); m = m->b_cont) {
40*381a2a9aSdr 		if (m->b_datap->db_type != M_DATA)
417c478bd9Sstevel@tonic-gate 			continue;
427c478bd9Sstevel@tonic-gate 		s = m->b_rptr;
437c478bd9Sstevel@tonic-gate 		mlen = m->b_wptr - s;
44*381a2a9aSdr 		olen = min(off, mlen);
457c478bd9Sstevel@tonic-gate 		if ((olen == mlen) || (olen < off)) {
467c478bd9Sstevel@tonic-gate 			off -= olen;
477c478bd9Sstevel@tonic-gate 			continue;
487c478bd9Sstevel@tonic-gate 		} else if (olen) {
497c478bd9Sstevel@tonic-gate 			off -= olen;
507c478bd9Sstevel@tonic-gate 			s += olen;
517c478bd9Sstevel@tonic-gate 			mlen -= olen;
527c478bd9Sstevel@tonic-gate 		}
53*381a2a9aSdr 		clen = min(mlen, len);
547c478bd9Sstevel@tonic-gate 		BCOPY(s, bp, clen);
557c478bd9Sstevel@tonic-gate 		len -= clen;
567c478bd9Sstevel@tonic-gate 		bp += clen;
577c478bd9Sstevel@tonic-gate 	}
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 
mb_copyback(min,off,len,buf)617c478bd9Sstevel@tonic-gate void mb_copyback(min, off, len, buf)
627c478bd9Sstevel@tonic-gate mblk_t *min;
637c478bd9Sstevel@tonic-gate size_t off, len;
647c478bd9Sstevel@tonic-gate char *buf;
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	u_char *s, *bp = (u_char *)buf;
677c478bd9Sstevel@tonic-gate 	size_t mlen, olen, clen;
687c478bd9Sstevel@tonic-gate 	mblk_t *m, *mp;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	for (m = min, mp = NULL; (m != NULL) && (len > 0); m = m->b_cont) {
717c478bd9Sstevel@tonic-gate 		mp = m;
72*381a2a9aSdr 		if (m->b_datap->db_type != M_DATA)
737c478bd9Sstevel@tonic-gate 			continue;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 		s = m->b_rptr;
767c478bd9Sstevel@tonic-gate 		mlen = m->b_wptr - s;
77*381a2a9aSdr 		olen = min(off, mlen);
787c478bd9Sstevel@tonic-gate 		if ((olen == mlen) || (olen < off)) {
797c478bd9Sstevel@tonic-gate 			off -= olen;
807c478bd9Sstevel@tonic-gate 			continue;
817c478bd9Sstevel@tonic-gate 		} else if (olen) {
827c478bd9Sstevel@tonic-gate 			off -= olen;
837c478bd9Sstevel@tonic-gate 			s += olen;
847c478bd9Sstevel@tonic-gate 			mlen -= olen;
857c478bd9Sstevel@tonic-gate 		}
86*381a2a9aSdr 		clen = min(mlen, len);
877c478bd9Sstevel@tonic-gate 		BCOPY(bp, s, clen);
887c478bd9Sstevel@tonic-gate 		len -= clen;
897c478bd9Sstevel@tonic-gate 		bp += clen;
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if ((m == NULL) && (mp != NULL)) {
937c478bd9Sstevel@tonic-gate 		if (len > 0) {
947c478bd9Sstevel@tonic-gate 			mlen = mp->b_datap->db_lim - mp->b_wptr;
957c478bd9Sstevel@tonic-gate 			if (mlen > 0) {
967c478bd9Sstevel@tonic-gate 				if (mlen > len)
977c478bd9Sstevel@tonic-gate 					mlen = len;
98*381a2a9aSdr 				BCOPY(bp, mp->b_wptr, mlen);
997c478bd9Sstevel@tonic-gate 				bp += mlen;
1007c478bd9Sstevel@tonic-gate 				len -= mlen;
1017c478bd9Sstevel@tonic-gate 				mp->b_wptr += mlen;
1027c478bd9Sstevel@tonic-gate #ifdef  STRUIO_IP
1037c478bd9Sstevel@tonic-gate # if SOLARIS2 < 10
1047c478bd9Sstevel@tonic-gate 				mp->b_datap->db_struiolim = mp->b_wptr;
1057c478bd9Sstevel@tonic-gate # endif
1067c478bd9Sstevel@tonic-gate 				mp->b_datap->db_struioflag &= ~STRUIO_IP;
1077c478bd9Sstevel@tonic-gate #endif
1087c478bd9Sstevel@tonic-gate 			}
1097c478bd9Sstevel@tonic-gate 		}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 		if (len > 0) {
1127c478bd9Sstevel@tonic-gate 			m = allocb(len, BPRI_MED);
1137c478bd9Sstevel@tonic-gate 			if (m != NULL) {
114*381a2a9aSdr 				BCOPY(bp, m->b_wptr, len);
1157c478bd9Sstevel@tonic-gate 				m->b_band = mp->b_band;
1167c478bd9Sstevel@tonic-gate 				m->b_wptr += len;
1177c478bd9Sstevel@tonic-gate 				linkb(mp, m);
1187c478bd9Sstevel@tonic-gate 			}
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate }
122