xref: /csrg-svn/sys/netinet/ip_output.c (revision 5242)
1*5242Sroot /*	ip_output.c	1.22	81/12/11	*/
24571Swnj 
34496Swnj #include "../h/param.h"
44662Swnj #include "../h/mbuf.h"
54724Swnj #include "../h/mtpr.h"
64662Swnj #include "../h/socket.h"
74924Swnj #include "../h/socketvar.h"
85085Swnj #include "../net/in.h"
95085Swnj #include "../net/in_systm.h"
105085Swnj #include "../net/if.h"
114802Swnj #include "../net/ip.h"
124899Swnj #include "../net/ip_var.h"
134496Swnj 
145085Swnj ip_output(m, opt)
154924Swnj 	struct mbuf *m;
165085Swnj 	struct mbuf *opt;
174496Swnj {
184924Swnj 	register struct ip *ip = mtod(m, struct ip *);
195085Swnj 	register struct ifnet *ifp;
205219Swnj 	int len, hlen = sizeof (struct ip), off;
214496Swnj 
224496Swnj COUNT(IP_OUTPUT);
235219Swnj 	if (opt)				/* XXX */
24*5242Sroot 		(void) m_free(opt);		/* XXX */
254924Swnj 	/*
264924Swnj 	 * Fill in IP header.
274924Swnj 	 */
284924Swnj 	ip->ip_v = IPVERSION;
294924Swnj 	ip->ip_hl = hlen >> 2;
304924Swnj 	ip->ip_off &= IP_DF;
315085Swnj 	ip->ip_id = htons(ip_id++);
324496Swnj 
334545Swnj 	/*
345085Swnj 	 * Find interface for this packet.
355085Swnj 	 */
365085Swnj 	ifp = if_ifonnetof(ip->ip_dst);
375085Swnj 	if (ifp == 0) {
385085Swnj 		ifp = if_gatewayfor(ip->ip_dst);
395085Swnj 		if (ifp == 0)
405085Swnj 			goto bad;
415085Swnj 	}
425085Swnj 
435085Swnj 	/*
444924Swnj 	 * If small enough for interface, can just send directly.
454545Swnj 	 */
465085Swnj 	if (ip->ip_len <= ifp->if_mtu) {
475219Swnj #if vax
485085Swnj 		ip->ip_len = htons((u_short)ip->ip_len);
495085Swnj 		ip->ip_off = htons((u_short)ip->ip_off);
505219Swnj #endif
515085Swnj 		ip->ip_sum = 0;
525085Swnj 		ip->ip_sum = in_cksum(m, hlen);
535085Swnj 		return ((*ifp->if_output)(ifp, m, PF_INET));
544908Swnj 	}
554924Swnj 
564924Swnj 	/*
574924Swnj 	 * Too large for interface; fragment if possible.
584924Swnj 	 * Must be able to put at least 8 bytes per fragment.
594924Swnj 	 */
604924Swnj 	if (ip->ip_off & IP_DF)
614924Swnj 		goto bad;
625085Swnj 	len = (ifp->if_mtu - hlen) &~ 7;
634924Swnj 	if (len < 8)
644924Swnj 		goto bad;
654924Swnj 
664924Swnj 	/*
674924Swnj 	 * Discard IP header from logical mbuf for m_copy's sake.
684924Swnj 	 * Loop through length of segment, make a copy of each
694924Swnj 	 * part and output.
704924Swnj 	 */
714924Swnj 	m->m_len -= sizeof (struct ip);
724924Swnj 	m->m_off += sizeof (struct ip);
734924Swnj 	for (off = 0; off < ip->ip_len; off += len) {
744924Swnj 		struct mbuf *mh = m_get(0);
754924Swnj 		struct ip *mhip;
764924Swnj 
774924Swnj 		if (mh == 0)
784924Swnj 			goto bad;
794924Swnj 		mh->m_off = MMAXOFF - hlen;
804924Swnj 		mhip = mtod(mh, struct ip *);
814924Swnj 		*mhip = *ip;
824952Swnj 		if (hlen > sizeof (struct ip)) {
834924Swnj 			int olen = ip_optcopy(ip, mhip, off);
844924Swnj 			mh->m_len = sizeof (struct ip) + olen;
854924Swnj 		} else
864924Swnj 			mh->m_len = sizeof (struct ip);
874924Swnj 		mhip->ip_off = off;
884924Swnj 		if (off + len >= ip->ip_len)
894924Swnj 			mhip->ip_len = ip->ip_len - off;
904924Swnj 		else {
914924Swnj 			mhip->ip_len = len;
924924Swnj 			mhip->ip_off |= IP_MF;
934496Swnj 		}
945085Swnj 		mhip->ip_len = htons((u_short)(mhip->ip_len + sizeof (struct ip)));
954924Swnj 		mh->m_next = m_copy(m, off, len);
964924Swnj 		if (mh->m_next == 0) {
974967Swnj 			(void) m_free(mh);
984924Swnj 			goto bad;
994674Swnj 		}
1005085Swnj 		ip->ip_off = htons((u_short)ip->ip_off);
1015085Swnj 		ip->ip_sum = 0;
1025085Swnj 		ip->ip_sum = in_cksum(m, hlen);
1035085Swnj 		if ((*ifp->if_output)(ifp, mh, PF_INET) == 0)
1045085Swnj 			goto bad;
1054924Swnj 	}
1065085Swnj 	m_freem(m);
1075085Swnj 	return (1);
1084924Swnj bad:
1094924Swnj 	m_freem(m);
1105085Swnj 	return (0);
1114924Swnj }
1124924Swnj 
1134924Swnj /*
1144924Swnj  * Copy options from ip to jp.
1154952Swnj  * If off is 0 all options are copied
1164924Swnj  * otherwise copy selectively.
1174924Swnj  */
1184924Swnj ip_optcopy(ip, jp, off)
1194924Swnj 	struct ip *ip, *jp;
1204924Swnj 	int off;
1214924Swnj {
1224924Swnj 	register u_char *cp, *dp;
1234924Swnj 	int opt, optlen, cnt;
1244924Swnj 
1254952Swnj COUNT(IP_OPTCOPY);
1264924Swnj 	cp = (u_char *)(ip + 1);
1274924Swnj 	dp = (u_char *)(jp + 1);
1284924Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1294924Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1304924Swnj 		opt = cp[0];
1314924Swnj 		if (opt == IPOPT_EOL)
1324924Swnj 			break;
1334924Swnj 		if (opt == IPOPT_NOP)
1344924Swnj 			optlen = 1;
1354924Swnj 		else
1364924Swnj 			optlen = cp[1];
1374924Swnj 		if (optlen > cnt)			/* XXX */
1384924Swnj 			optlen = cnt;			/* XXX */
1394924Swnj 		if (off == 0 || IPOPT_COPIED(opt)) {
1404952Swnj 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
1414924Swnj 			dp += optlen;
1424674Swnj 		}
1434545Swnj 	}
1444924Swnj 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1454924Swnj 		*dp++ = IPOPT_EOL;
1464924Swnj 	return (optlen);
1474496Swnj }
148