xref: /illumos-gate/usr/src/uts/common/inet/ipf/misc.c (revision 2d6eb4a5)
17c478bd9Sstevel@tonic-gate /*
2ab25eeb5Syz  * Copyright (C) 2003 by Darren Reed.
3ab25eeb5Syz  *
4ab25eeb5Syz  * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate  */
6381a2a9aSdr /*
7381a2a9aSdr  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
8381a2a9aSdr  * Use is subject to license terms.
9381a2a9aSdr  */
10*2d6eb4a5SToomas Soome #ifdef __hpux
117c478bd9Sstevel@tonic-gate struct uio;
127c478bd9Sstevel@tonic-gate #endif
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #include <sys/systm.h>
157c478bd9Sstevel@tonic-gate #include <sys/types.h>
167c478bd9Sstevel@tonic-gate #include <sys/stream.h>
17381a2a9aSdr #include <sys/ddi.h>
187c478bd9Sstevel@tonic-gate 
197c478bd9Sstevel@tonic-gate #ifdef	__hpux
207c478bd9Sstevel@tonic-gate # define	BCOPY(a,b,c)	bcopy((caddr_t)a, (caddr_t)b, c)
217c478bd9Sstevel@tonic-gate #endif
227c478bd9Sstevel@tonic-gate #ifdef	sun
237c478bd9Sstevel@tonic-gate # define	BCOPY(a,b,c)	bcopy((char *)a, (char *)b, c)
247c478bd9Sstevel@tonic-gate #endif
257c478bd9Sstevel@tonic-gate 
mb_copydata(min,off,len,buf)267c478bd9Sstevel@tonic-gate void mb_copydata(min, off, len, buf)
277c478bd9Sstevel@tonic-gate mblk_t *min;
287c478bd9Sstevel@tonic-gate size_t off, len;
297c478bd9Sstevel@tonic-gate char *buf;
307c478bd9Sstevel@tonic-gate {
317c478bd9Sstevel@tonic-gate 	u_char *s, *bp = (u_char *)buf;
327c478bd9Sstevel@tonic-gate 	size_t mlen, olen, clen;
337c478bd9Sstevel@tonic-gate 	mblk_t *m;
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate 	for (m = min; (m != NULL) && (len > 0); m = m->b_cont) {
36381a2a9aSdr 		if (m->b_datap->db_type != M_DATA)
377c478bd9Sstevel@tonic-gate 			continue;
387c478bd9Sstevel@tonic-gate 		s = m->b_rptr;
397c478bd9Sstevel@tonic-gate 		mlen = m->b_wptr - s;
40381a2a9aSdr 		olen = min(off, mlen);
417c478bd9Sstevel@tonic-gate 		if ((olen == mlen) || (olen < off)) {
427c478bd9Sstevel@tonic-gate 			off -= olen;
437c478bd9Sstevel@tonic-gate 			continue;
447c478bd9Sstevel@tonic-gate 		} else if (olen) {
457c478bd9Sstevel@tonic-gate 			off -= olen;
467c478bd9Sstevel@tonic-gate 			s += olen;
477c478bd9Sstevel@tonic-gate 			mlen -= olen;
487c478bd9Sstevel@tonic-gate 		}
49381a2a9aSdr 		clen = min(mlen, len);
507c478bd9Sstevel@tonic-gate 		BCOPY(s, bp, clen);
517c478bd9Sstevel@tonic-gate 		len -= clen;
527c478bd9Sstevel@tonic-gate 		bp += clen;
537c478bd9Sstevel@tonic-gate 	}
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 
mb_copyback(min,off,len,buf)577c478bd9Sstevel@tonic-gate void mb_copyback(min, off, len, buf)
587c478bd9Sstevel@tonic-gate mblk_t *min;
597c478bd9Sstevel@tonic-gate size_t off, len;
607c478bd9Sstevel@tonic-gate char *buf;
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	u_char *s, *bp = (u_char *)buf;
637c478bd9Sstevel@tonic-gate 	size_t mlen, olen, clen;
647c478bd9Sstevel@tonic-gate 	mblk_t *m, *mp;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	for (m = min, mp = NULL; (m != NULL) && (len > 0); m = m->b_cont) {
677c478bd9Sstevel@tonic-gate 		mp = m;
68381a2a9aSdr 		if (m->b_datap->db_type != M_DATA)
697c478bd9Sstevel@tonic-gate 			continue;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 		s = m->b_rptr;
727c478bd9Sstevel@tonic-gate 		mlen = m->b_wptr - s;
73381a2a9aSdr 		olen = min(off, mlen);
747c478bd9Sstevel@tonic-gate 		if ((olen == mlen) || (olen < off)) {
757c478bd9Sstevel@tonic-gate 			off -= olen;
767c478bd9Sstevel@tonic-gate 			continue;
777c478bd9Sstevel@tonic-gate 		} else if (olen) {
787c478bd9Sstevel@tonic-gate 			off -= olen;
797c478bd9Sstevel@tonic-gate 			s += olen;
807c478bd9Sstevel@tonic-gate 			mlen -= olen;
817c478bd9Sstevel@tonic-gate 		}
82381a2a9aSdr 		clen = min(mlen, len);
837c478bd9Sstevel@tonic-gate 		BCOPY(bp, s, clen);
847c478bd9Sstevel@tonic-gate 		len -= clen;
857c478bd9Sstevel@tonic-gate 		bp += clen;
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if ((m == NULL) && (mp != NULL)) {
897c478bd9Sstevel@tonic-gate 		if (len > 0) {
907c478bd9Sstevel@tonic-gate 			mlen = mp->b_datap->db_lim - mp->b_wptr;
917c478bd9Sstevel@tonic-gate 			if (mlen > 0) {
927c478bd9Sstevel@tonic-gate 				if (mlen > len)
937c478bd9Sstevel@tonic-gate 					mlen = len;
94381a2a9aSdr 				BCOPY(bp, mp->b_wptr, mlen);
957c478bd9Sstevel@tonic-gate 				bp += mlen;
967c478bd9Sstevel@tonic-gate 				len -= mlen;
977c478bd9Sstevel@tonic-gate 				mp->b_wptr += mlen;
987c478bd9Sstevel@tonic-gate #ifdef  STRUIO_IP
997c478bd9Sstevel@tonic-gate # if SOLARIS2 < 10
1007c478bd9Sstevel@tonic-gate 				mp->b_datap->db_struiolim = mp->b_wptr;
1017c478bd9Sstevel@tonic-gate # endif
1027c478bd9Sstevel@tonic-gate 				mp->b_datap->db_struioflag &= ~STRUIO_IP;
1037c478bd9Sstevel@tonic-gate #endif
1047c478bd9Sstevel@tonic-gate 			}
1057c478bd9Sstevel@tonic-gate 		}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 		if (len > 0) {
1087c478bd9Sstevel@tonic-gate 			m = allocb(len, BPRI_MED);
1097c478bd9Sstevel@tonic-gate 			if (m != NULL) {
110381a2a9aSdr 				BCOPY(bp, m->b_wptr, len);
1117c478bd9Sstevel@tonic-gate 				m->b_band = mp->b_band;
1127c478bd9Sstevel@tonic-gate 				m->b_wptr += len;
1137c478bd9Sstevel@tonic-gate 				linkb(mp, m);
1147c478bd9Sstevel@tonic-gate 			}
1157c478bd9Sstevel@tonic-gate 		}
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate }
118