xref: /csrg-svn/sys/netinet/ip_output.c (revision 18375)
1*18375Skarels /*	ip_output.c	6.7	85/03/18	*/
24571Swnj 
317061Sbloom #include "param.h"
417061Sbloom #include "mbuf.h"
517061Sbloom #include "errno.h"
617061Sbloom #include "socket.h"
717061Sbloom #include "socketvar.h"
810893Ssam 
910893Ssam #include "../net/if.h"
1010893Ssam #include "../net/route.h"
1110893Ssam 
1217061Sbloom #include "in.h"
1317061Sbloom #include "in_systm.h"
14*18375Skarels #include "in_var.h"
1517061Sbloom #include "ip.h"
1617061Sbloom #include "ip_var.h"
174496Swnj 
1812460Ssam #ifdef vax
1912460Ssam #include "../vax/mtpr.h"
2012460Ssam #endif
2110893Ssam 
2212417Ssam ip_output(m, opt, ro, flags)
234924Swnj 	struct mbuf *m;
245085Swnj 	struct mbuf *opt;
256339Ssam 	struct route *ro;
2612417Ssam 	int flags;
274496Swnj {
284924Swnj 	register struct ip *ip = mtod(m, struct ip *);
295085Swnj 	register struct ifnet *ifp;
306505Ssam 	int len, hlen = sizeof (struct ip), off, error = 0;
316339Ssam 	struct route iproute;
3216602Ssam 	struct sockaddr_in *dst;
334496Swnj 
345219Swnj 	if (opt)				/* XXX */
355242Sroot 		(void) m_free(opt);		/* XXX */
364924Swnj 	/*
374924Swnj 	 * Fill in IP header.
384924Swnj 	 */
3912417Ssam 	if ((flags & IP_FORWARDING) == 0) {
4012417Ssam 		ip->ip_v = IPVERSION;
4112417Ssam 		ip->ip_off &= IP_DF;
4212417Ssam 		ip->ip_id = htons(ip_id++);
4316545Skarels 		ip->ip_hl = hlen >> 2;
44*18375Skarels 	}
454496Swnj 
464545Swnj 	/*
477155Swnj 	 * Route packet.
485085Swnj 	 */
496339Ssam 	if (ro == 0) {
506339Ssam 		ro = &iproute;
516339Ssam 		bzero((caddr_t)ro, sizeof (*ro));
525085Swnj 	}
5316602Ssam 	dst = (struct sockaddr_in *)&ro->ro_dst;
546339Ssam 	if (ro->ro_rt == 0) {
5516602Ssam 		dst->sin_family = AF_INET;
5616602Ssam 		dst->sin_addr = ip->ip_dst;
577155Swnj 		/*
5812417Ssam 		 * If routing to interface only,
5912417Ssam 		 * short circuit routing lookup.
607155Swnj 		 */
6112417Ssam 		if (flags & IP_ROUTETOIF) {
62*18375Skarels 			struct in_ifaddr *ia;
63*18375Skarels 			ia = in_iaonnetof(in_netof(ip->ip_dst));
64*18375Skarels 			if (ia == 0) {
6512417Ssam 				error = ENETUNREACH;
6612417Ssam 				goto bad;
6712417Ssam 			}
68*18375Skarels 			ifp = ia->ia_ifp;
697155Swnj 			goto gotif;
707155Swnj 		}
716368Ssam 		rtalloc(ro);
7216545Skarels 	} else if ((ro->ro_rt->rt_flags & RTF_UP) == 0) {
7315716Skarels 		/*
7415716Skarels 		 * The old route has gone away; try for a new one.
7515716Skarels 		 */
7615716Skarels 		rtfree(ro->ro_rt);
77*18375Skarels 		ro->ro_rt = NULL;
7815716Skarels 		rtalloc(ro);
796339Ssam 	}
8012417Ssam 	if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) {
8112417Ssam 		error = ENETUNREACH;
8212417Ssam 		goto bad;
8312417Ssam 	}
847155Swnj 	ro->ro_rt->rt_use++;
8515276Ssam 	if (ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST))
8616602Ssam 		dst = (struct sockaddr_in *)&ro->ro_rt->rt_gateway;
877155Swnj gotif:
887155Swnj 	/*
8910402Ssam 	 * Look for broadcast address and
9010402Ssam 	 * and verify user is allowed to send
9110146Ssam 	 * such a packet.
927155Swnj 	 */
93*18375Skarels 	if (in_broadcast(dst->sin_addr)) {
9410146Ssam 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
9510146Ssam 			error = EADDRNOTAVAIL;
9610146Ssam 			goto bad;
9710146Ssam 		}
9812417Ssam 		if ((flags & IP_ALLOWBROADCAST) == 0) {
997155Swnj 			error = EACCES;
1006339Ssam 			goto bad;
1016505Ssam 		}
10210146Ssam 		/* don't allow broadcast messages to be fragmented */
10310146Ssam 		if (ip->ip_len > ifp->if_mtu) {
10410146Ssam 			error = EMSGSIZE;
10510146Ssam 			goto bad;
10610146Ssam 		}
1076339Ssam 	}
1086339Ssam 
1095085Swnj 	/*
1104924Swnj 	 * If small enough for interface, can just send directly.
1114545Swnj 	 */
1125085Swnj 	if (ip->ip_len <= ifp->if_mtu) {
1135085Swnj 		ip->ip_len = htons((u_short)ip->ip_len);
1145085Swnj 		ip->ip_off = htons((u_short)ip->ip_off);
1155085Swnj 		ip->ip_sum = 0;
1165085Swnj 		ip->ip_sum = in_cksum(m, hlen);
11716602Ssam 		error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst);
1187155Swnj 		goto done;
1194908Swnj 	}
1204924Swnj 
1214924Swnj 	/*
1224924Swnj 	 * Too large for interface; fragment if possible.
1234924Swnj 	 * Must be able to put at least 8 bytes per fragment.
1244924Swnj 	 */
1256505Ssam 	if (ip->ip_off & IP_DF) {
1266505Ssam 		error = EMSGSIZE;
1274924Swnj 		goto bad;
1286505Ssam 	}
1295085Swnj 	len = (ifp->if_mtu - hlen) &~ 7;
1306505Ssam 	if (len < 8) {
1316505Ssam 		error = EMSGSIZE;
1324924Swnj 		goto bad;
1336505Ssam 	}
1344924Swnj 
1354924Swnj 	/*
1364924Swnj 	 * Discard IP header from logical mbuf for m_copy's sake.
1374924Swnj 	 * Loop through length of segment, make a copy of each
1384924Swnj 	 * part and output.
1394924Swnj 	 */
1404924Swnj 	m->m_len -= sizeof (struct ip);
1414924Swnj 	m->m_off += sizeof (struct ip);
1425892Sroot 	for (off = 0; off < ip->ip_len-hlen; off += len) {
14310012Ssam 		struct mbuf *mh = m_get(M_DONTWAIT, MT_HEADER);
1444924Swnj 		struct ip *mhip;
1454924Swnj 
1466505Ssam 		if (mh == 0) {
1476505Ssam 			error = ENOBUFS;
1484924Swnj 			goto bad;
1496505Ssam 		}
1504924Swnj 		mh->m_off = MMAXOFF - hlen;
1514924Swnj 		mhip = mtod(mh, struct ip *);
1524924Swnj 		*mhip = *ip;
1534952Swnj 		if (hlen > sizeof (struct ip)) {
1544924Swnj 			int olen = ip_optcopy(ip, mhip, off);
1554924Swnj 			mh->m_len = sizeof (struct ip) + olen;
1564924Swnj 		} else
1574924Swnj 			mh->m_len = sizeof (struct ip);
1585770Swnj 		mhip->ip_off = off >> 3;
15916545Skarels 		if (ip->ip_off & IP_MF)
16016545Skarels 			mhip->ip_off |= IP_MF;
1615892Sroot 		if (off + len >= ip->ip_len-hlen)
1625892Sroot 			len = mhip->ip_len = ip->ip_len - hlen - off;
1634924Swnj 		else {
1644924Swnj 			mhip->ip_len = len;
1654924Swnj 			mhip->ip_off |= IP_MF;
1664496Swnj 		}
1675770Swnj 		mhip->ip_len += sizeof (struct ip);
1685770Swnj 		mhip->ip_len = htons((u_short)mhip->ip_len);
1694924Swnj 		mh->m_next = m_copy(m, off, len);
1704924Swnj 		if (mh->m_next == 0) {
1714967Swnj 			(void) m_free(mh);
1726505Ssam 			error = ENOBUFS;	/* ??? */
1734924Swnj 			goto bad;
1744674Swnj 		}
1755892Sroot 		mhip->ip_off = htons((u_short)mhip->ip_off);
1765892Sroot 		mhip->ip_sum = 0;
1775892Sroot 		mhip->ip_sum = in_cksum(mh, hlen);
17816602Ssam 		if (error = (*ifp->if_output)(ifp, mh, (struct sockaddr *)dst))
1796505Ssam 			break;
1804924Swnj 	}
1814924Swnj bad:
1824924Swnj 	m_freem(m);
1837155Swnj done:
18412417Ssam 	if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt)
1857155Swnj 		RTFREE(ro->ro_rt);
1866505Ssam 	return (error);
1874924Swnj }
1884924Swnj 
1894924Swnj /*
1904924Swnj  * Copy options from ip to jp.
1914952Swnj  * If off is 0 all options are copied
1924924Swnj  * otherwise copy selectively.
1934924Swnj  */
1944924Swnj ip_optcopy(ip, jp, off)
1954924Swnj 	struct ip *ip, *jp;
1964924Swnj 	int off;
1974924Swnj {
1984924Swnj 	register u_char *cp, *dp;
1994924Swnj 	int opt, optlen, cnt;
2004924Swnj 
2014924Swnj 	cp = (u_char *)(ip + 1);
2024924Swnj 	dp = (u_char *)(jp + 1);
2034924Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
2044924Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2054924Swnj 		opt = cp[0];
2064924Swnj 		if (opt == IPOPT_EOL)
2074924Swnj 			break;
2084924Swnj 		if (opt == IPOPT_NOP)
2094924Swnj 			optlen = 1;
2104924Swnj 		else
2114924Swnj 			optlen = cp[1];
2124924Swnj 		if (optlen > cnt)			/* XXX */
2134924Swnj 			optlen = cnt;			/* XXX */
2144924Swnj 		if (off == 0 || IPOPT_COPIED(opt)) {
2154952Swnj 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
2164924Swnj 			dp += optlen;
2174674Swnj 		}
2184545Swnj 	}
2194924Swnj 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
2204924Swnj 		*dp++ = IPOPT_EOL;
2214924Swnj 	return (optlen);
2224496Swnj }
223