xref: /csrg-svn/sys/netinet/ip_output.c (revision 6412)
1*6412Ssam /*	ip_output.c	1.31	82/03/31	*/
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"
136339Ssam #include "../net/route.h"
144496Swnj 
156339Ssam ip_output(m, opt, ro, allowbroadcast)
164924Swnj 	struct mbuf *m;
175085Swnj 	struct mbuf *opt;
186339Ssam 	struct route *ro;
196211Swnj 	int allowbroadcast;
204496Swnj {
214924Swnj 	register struct ip *ip = mtod(m, struct ip *);
225085Swnj 	register struct ifnet *ifp;
236368Ssam 	int len, hlen = sizeof (struct ip), off;
246339Ssam 	struct route iproute;
256351Ssam 	struct sockaddr *dst;
264496Swnj 
274496Swnj COUNT(IP_OUTPUT);
285219Swnj 	if (opt)				/* XXX */
295242Sroot 		(void) m_free(opt);		/* XXX */
304924Swnj 	/*
314924Swnj 	 * Fill in IP header.
324924Swnj 	 */
334924Swnj 	ip->ip_v = IPVERSION;
344924Swnj 	ip->ip_hl = hlen >> 2;
354924Swnj 	ip->ip_off &= IP_DF;
365085Swnj 	ip->ip_id = htons(ip_id++);
374496Swnj 
384545Swnj 	/*
396351Ssam 	 * Find interface for this packet in the routing
406351Ssam 	 * table.  Note each interface has placed itself
416377Ssam 	 * in there at boot time, so calls to rtalloc
426377Ssam 	 * degenerate to if_ifonnetof(ip->ip_dst.s_net).
435085Swnj 	 */
446339Ssam 	if (ro == 0) {
456339Ssam 		ro = &iproute;
466339Ssam 		bzero((caddr_t)ro, sizeof (*ro));
475085Swnj 	}
486339Ssam 	if (ro->ro_rt == 0) {
496368Ssam 		ro->ro_dst.sa_family = AF_INET;
506368Ssam 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = ip->ip_dst;
516368Ssam 		rtalloc(ro);
526339Ssam 	}
536368Ssam 	if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) {
54*6412Ssam 		printf("no route to %x (from %x, len %d)\n",
55*6412Ssam 		    ip->ip_dst.s_addr, ip->ip_src.s_addr, ip->ip_len);
566211Swnj 		goto bad;
57*6412Ssam 	}
586368Ssam 	dst = ro->ro_rt->rt_flags&RTF_DIRECT ?
596368Ssam 	    (struct sockaddr *)&ro->ro_dst : &ro->ro_rt->rt_gateway;
60*6412Ssam 	if (ro == &iproute)
61*6412Ssam 		RTFREE(ro->ro_rt);
626339Ssam 	if (!allowbroadcast && (ifp->if_flags & IFF_BROADCAST)) {
636339Ssam 		struct sockaddr_in *sin;
646339Ssam 
656339Ssam 		sin = (struct sockaddr_in *)&ifp->if_broadaddr;
666339Ssam 		if (sin->sin_addr.s_addr == ip->ip_dst.s_addr)
676339Ssam 			goto bad;
686339Ssam 	}
696339Ssam 
705085Swnj 	/*
714924Swnj 	 * If small enough for interface, can just send directly.
724545Swnj 	 */
735085Swnj 	if (ip->ip_len <= ifp->if_mtu) {
745219Swnj #if vax
755085Swnj 		ip->ip_len = htons((u_short)ip->ip_len);
765085Swnj 		ip->ip_off = htons((u_short)ip->ip_off);
775219Swnj #endif
785085Swnj 		ip->ip_sum = 0;
795085Swnj 		ip->ip_sum = in_cksum(m, hlen);
806377Ssam 		ro->ro_rt->rt_use++;
816351Ssam 		return ((*ifp->if_output)(ifp, m, dst));
824908Swnj 	}
834924Swnj 
844924Swnj 	/*
854924Swnj 	 * Too large for interface; fragment if possible.
864924Swnj 	 * Must be able to put at least 8 bytes per fragment.
874924Swnj 	 */
884924Swnj 	if (ip->ip_off & IP_DF)
894924Swnj 		goto bad;
905085Swnj 	len = (ifp->if_mtu - hlen) &~ 7;
914924Swnj 	if (len < 8)
924924Swnj 		goto bad;
934924Swnj 
944924Swnj 	/*
954924Swnj 	 * Discard IP header from logical mbuf for m_copy's sake.
964924Swnj 	 * Loop through length of segment, make a copy of each
974924Swnj 	 * part and output.
984924Swnj 	 */
994924Swnj 	m->m_len -= sizeof (struct ip);
1004924Swnj 	m->m_off += sizeof (struct ip);
1015892Sroot 	for (off = 0; off < ip->ip_len-hlen; off += len) {
1025584Sroot 		struct mbuf *mh = m_get(M_DONTWAIT);
1034924Swnj 		struct ip *mhip;
1044924Swnj 
1054924Swnj 		if (mh == 0)
1064924Swnj 			goto bad;
1074924Swnj 		mh->m_off = MMAXOFF - hlen;
1084924Swnj 		mhip = mtod(mh, struct ip *);
1094924Swnj 		*mhip = *ip;
1104952Swnj 		if (hlen > sizeof (struct ip)) {
1114924Swnj 			int olen = ip_optcopy(ip, mhip, off);
1124924Swnj 			mh->m_len = sizeof (struct ip) + olen;
1134924Swnj 		} else
1144924Swnj 			mh->m_len = sizeof (struct ip);
1155770Swnj 		mhip->ip_off = off >> 3;
1165892Sroot 		if (off + len >= ip->ip_len-hlen)
1175892Sroot 			len = mhip->ip_len = ip->ip_len - hlen - off;
1184924Swnj 		else {
1194924Swnj 			mhip->ip_len = len;
1204924Swnj 			mhip->ip_off |= IP_MF;
1214496Swnj 		}
1225770Swnj 		mhip->ip_len += sizeof (struct ip);
1235770Swnj #if vax
1245770Swnj 		mhip->ip_len = htons((u_short)mhip->ip_len);
1255770Swnj #endif
1264924Swnj 		mh->m_next = m_copy(m, off, len);
1274924Swnj 		if (mh->m_next == 0) {
1284967Swnj 			(void) m_free(mh);
1294924Swnj 			goto bad;
1304674Swnj 		}
1315770Swnj #if vax
1325892Sroot 		mhip->ip_off = htons((u_short)mhip->ip_off);
1335770Swnj #endif
1345892Sroot 		mhip->ip_sum = 0;
1355892Sroot 		mhip->ip_sum = in_cksum(mh, hlen);
1366377Ssam 		ro->ro_rt->rt_use++;
1376351Ssam 		if ((*ifp->if_output)(ifp, mh, dst) == 0)
1385085Swnj 			goto bad;
1394924Swnj 	}
1405085Swnj 	m_freem(m);
1415085Swnj 	return (1);
1424924Swnj bad:
1434924Swnj 	m_freem(m);
1445085Swnj 	return (0);
1454924Swnj }
1464924Swnj 
1474924Swnj /*
1484924Swnj  * Copy options from ip to jp.
1494952Swnj  * If off is 0 all options are copied
1504924Swnj  * otherwise copy selectively.
1514924Swnj  */
1524924Swnj ip_optcopy(ip, jp, off)
1534924Swnj 	struct ip *ip, *jp;
1544924Swnj 	int off;
1554924Swnj {
1564924Swnj 	register u_char *cp, *dp;
1574924Swnj 	int opt, optlen, cnt;
1584924Swnj 
1594952Swnj COUNT(IP_OPTCOPY);
1604924Swnj 	cp = (u_char *)(ip + 1);
1614924Swnj 	dp = (u_char *)(jp + 1);
1624924Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1634924Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1644924Swnj 		opt = cp[0];
1654924Swnj 		if (opt == IPOPT_EOL)
1664924Swnj 			break;
1674924Swnj 		if (opt == IPOPT_NOP)
1684924Swnj 			optlen = 1;
1694924Swnj 		else
1704924Swnj 			optlen = cp[1];
1714924Swnj 		if (optlen > cnt)			/* XXX */
1724924Swnj 			optlen = cnt;			/* XXX */
1734924Swnj 		if (off == 0 || IPOPT_COPIED(opt)) {
1744952Swnj 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
1754924Swnj 			dp += optlen;
1764674Swnj 		}
1774545Swnj 	}
1784924Swnj 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1794924Swnj 		*dp++ = IPOPT_EOL;
1804924Swnj 	return (optlen);
1814496Swnj }
182