xref: /csrg-svn/sys/netinet/ip_output.c (revision 6368)
1*6368Ssam /*	ip_output.c	1.29	82/03/30	*/
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;
23*6368Ssam 	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
416351Ssam 	 * in there at boot time, so call on route degenerates
426351Ssam 	 * 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) {
49*6368Ssam 		ro->ro_dst.sa_family = AF_INET;
50*6368Ssam 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = ip->ip_dst;
51*6368Ssam 		rtalloc(ro);
526339Ssam 	}
53*6368Ssam 	if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) {
54*6368Ssam printf("no route to %x\n", ip->ip_dst.s_addr);
556211Swnj 		goto bad;
56*6368Ssam }
57*6368Ssam 	dst = ro->ro_rt->rt_flags&RTF_DIRECT ?
58*6368Ssam 	    (struct sockaddr *)&ro->ro_dst : &ro->ro_rt->rt_gateway;
596339Ssam 	if (!allowbroadcast && (ifp->if_flags & IFF_BROADCAST)) {
606339Ssam 		struct sockaddr_in *sin;
616339Ssam 
626339Ssam 		sin = (struct sockaddr_in *)&ifp->if_broadaddr;
636339Ssam 		if (sin->sin_addr.s_addr == ip->ip_dst.s_addr)
646339Ssam 			goto bad;
656339Ssam 	}
666339Ssam 
675085Swnj 	/*
684924Swnj 	 * If small enough for interface, can just send directly.
694545Swnj 	 */
705085Swnj 	if (ip->ip_len <= ifp->if_mtu) {
715219Swnj #if vax
725085Swnj 		ip->ip_len = htons((u_short)ip->ip_len);
735085Swnj 		ip->ip_off = htons((u_short)ip->ip_off);
745219Swnj #endif
755085Swnj 		ip->ip_sum = 0;
765085Swnj 		ip->ip_sum = in_cksum(m, hlen);
776351Ssam 		return ((*ifp->if_output)(ifp, m, dst));
784908Swnj 	}
794924Swnj 
804924Swnj 	/*
814924Swnj 	 * Too large for interface; fragment if possible.
824924Swnj 	 * Must be able to put at least 8 bytes per fragment.
834924Swnj 	 */
844924Swnj 	if (ip->ip_off & IP_DF)
854924Swnj 		goto bad;
865085Swnj 	len = (ifp->if_mtu - hlen) &~ 7;
874924Swnj 	if (len < 8)
884924Swnj 		goto bad;
894924Swnj 
904924Swnj 	/*
914924Swnj 	 * Discard IP header from logical mbuf for m_copy's sake.
924924Swnj 	 * Loop through length of segment, make a copy of each
934924Swnj 	 * part and output.
944924Swnj 	 */
954924Swnj 	m->m_len -= sizeof (struct ip);
964924Swnj 	m->m_off += sizeof (struct ip);
975892Sroot 	for (off = 0; off < ip->ip_len-hlen; off += len) {
985584Sroot 		struct mbuf *mh = m_get(M_DONTWAIT);
994924Swnj 		struct ip *mhip;
1004924Swnj 
1014924Swnj 		if (mh == 0)
1024924Swnj 			goto bad;
1034924Swnj 		mh->m_off = MMAXOFF - hlen;
1044924Swnj 		mhip = mtod(mh, struct ip *);
1054924Swnj 		*mhip = *ip;
1064952Swnj 		if (hlen > sizeof (struct ip)) {
1074924Swnj 			int olen = ip_optcopy(ip, mhip, off);
1084924Swnj 			mh->m_len = sizeof (struct ip) + olen;
1094924Swnj 		} else
1104924Swnj 			mh->m_len = sizeof (struct ip);
1115770Swnj 		mhip->ip_off = off >> 3;
1125892Sroot 		if (off + len >= ip->ip_len-hlen)
1135892Sroot 			len = mhip->ip_len = ip->ip_len - hlen - off;
1144924Swnj 		else {
1154924Swnj 			mhip->ip_len = len;
1164924Swnj 			mhip->ip_off |= IP_MF;
1174496Swnj 		}
1185770Swnj 		mhip->ip_len += sizeof (struct ip);
1195770Swnj #if vax
1205770Swnj 		mhip->ip_len = htons((u_short)mhip->ip_len);
1215770Swnj #endif
1224924Swnj 		mh->m_next = m_copy(m, off, len);
1234924Swnj 		if (mh->m_next == 0) {
1244967Swnj 			(void) m_free(mh);
1254924Swnj 			goto bad;
1264674Swnj 		}
1275770Swnj #if vax
1285892Sroot 		mhip->ip_off = htons((u_short)mhip->ip_off);
1295770Swnj #endif
1305892Sroot 		mhip->ip_sum = 0;
1315892Sroot 		mhip->ip_sum = in_cksum(mh, hlen);
1326351Ssam 		if ((*ifp->if_output)(ifp, mh, dst) == 0)
1335085Swnj 			goto bad;
1344924Swnj 	}
1355085Swnj 	m_freem(m);
1365085Swnj 	return (1);
1374924Swnj bad:
1384924Swnj 	m_freem(m);
1395085Swnj 	return (0);
1404924Swnj }
1414924Swnj 
1424924Swnj /*
1434924Swnj  * Copy options from ip to jp.
1444952Swnj  * If off is 0 all options are copied
1454924Swnj  * otherwise copy selectively.
1464924Swnj  */
1474924Swnj ip_optcopy(ip, jp, off)
1484924Swnj 	struct ip *ip, *jp;
1494924Swnj 	int off;
1504924Swnj {
1514924Swnj 	register u_char *cp, *dp;
1524924Swnj 	int opt, optlen, cnt;
1534924Swnj 
1544952Swnj COUNT(IP_OPTCOPY);
1554924Swnj 	cp = (u_char *)(ip + 1);
1564924Swnj 	dp = (u_char *)(jp + 1);
1574924Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1584924Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1594924Swnj 		opt = cp[0];
1604924Swnj 		if (opt == IPOPT_EOL)
1614924Swnj 			break;
1624924Swnj 		if (opt == IPOPT_NOP)
1634924Swnj 			optlen = 1;
1644924Swnj 		else
1654924Swnj 			optlen = cp[1];
1664924Swnj 		if (optlen > cnt)			/* XXX */
1674924Swnj 			optlen = cnt;			/* XXX */
1684924Swnj 		if (off == 0 || IPOPT_COPIED(opt)) {
1694952Swnj 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
1704924Swnj 			dp += optlen;
1714674Swnj 		}
1724545Swnj 	}
1734924Swnj 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1744924Swnj 		*dp++ = IPOPT_EOL;
1754924Swnj 	return (optlen);
1764496Swnj }
177