xref: /csrg-svn/sys/netinet/ip_output.c (revision 15276)
1*15276Ssam /*	ip_output.c	6.2	83/10/22	*/
24571Swnj 
34496Swnj #include "../h/param.h"
44662Swnj #include "../h/mbuf.h"
510893Ssam #include "../h/errno.h"
64662Swnj #include "../h/socket.h"
74924Swnj #include "../h/socketvar.h"
810893Ssam 
910893Ssam #include "../net/if.h"
1010893Ssam #include "../net/route.h"
1110893Ssam 
128400Swnj #include "../netinet/in.h"
138400Swnj #include "../netinet/in_systm.h"
148400Swnj #include "../netinet/ip.h"
158400Swnj #include "../netinet/ip_var.h"
164496Swnj 
1712460Ssam #ifdef vax
1812460Ssam #include "../vax/mtpr.h"
1912460Ssam #endif
2010893Ssam 
2112417Ssam ip_output(m, opt, ro, flags)
224924Swnj 	struct mbuf *m;
235085Swnj 	struct mbuf *opt;
246339Ssam 	struct route *ro;
2512417Ssam 	int flags;
264496Swnj {
274924Swnj 	register struct ip *ip = mtod(m, struct ip *);
285085Swnj 	register struct ifnet *ifp;
296505Ssam 	int len, hlen = sizeof (struct ip), off, error = 0;
306339Ssam 	struct route iproute;
316351Ssam 	struct sockaddr *dst;
324496Swnj 
335219Swnj 	if (opt)				/* XXX */
345242Sroot 		(void) m_free(opt);		/* XXX */
354924Swnj 	/*
364924Swnj 	 * Fill in IP header.
374924Swnj 	 */
384924Swnj 	ip->ip_hl = hlen >> 2;
3912417Ssam 	if ((flags & IP_FORWARDING) == 0) {
4012417Ssam 		ip->ip_v = IPVERSION;
4112417Ssam 		ip->ip_off &= IP_DF;
4212417Ssam 		ip->ip_id = htons(ip_id++);
4312417Ssam 	}
444496Swnj 
454545Swnj 	/*
467155Swnj 	 * Route packet.
475085Swnj 	 */
486339Ssam 	if (ro == 0) {
496339Ssam 		ro = &iproute;
506339Ssam 		bzero((caddr_t)ro, sizeof (*ro));
515085Swnj 	}
527155Swnj 	dst = &ro->ro_dst;
536339Ssam 	if (ro->ro_rt == 0) {
546368Ssam 		ro->ro_dst.sa_family = AF_INET;
556368Ssam 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = ip->ip_dst;
567155Swnj 		/*
5712417Ssam 		 * If routing to interface only,
5812417Ssam 		 * short circuit routing lookup.
597155Swnj 		 */
6012417Ssam 		if (flags & IP_ROUTETOIF) {
618638Sroot 			ifp = if_ifonnetof(in_netof(ip->ip_dst));
6212417Ssam 			if (ifp == 0) {
6312417Ssam 				error = ENETUNREACH;
6412417Ssam 				goto bad;
6512417Ssam 			}
667155Swnj 			goto gotif;
677155Swnj 		}
686368Ssam 		rtalloc(ro);
696339Ssam 	}
7012417Ssam 	if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) {
7112417Ssam 		error = ENETUNREACH;
7212417Ssam 		goto bad;
7312417Ssam 	}
747155Swnj 	ro->ro_rt->rt_use++;
75*15276Ssam 	if (ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST))
767155Swnj 		dst = &ro->ro_rt->rt_gateway;
777155Swnj gotif:
7810402Ssam #ifndef notdef
797155Swnj 	/*
807155Swnj 	 * If source address not specified yet, use address
817155Swnj 	 * of outgoing interface.
827155Swnj 	 */
8310402Ssam 	if (in_lnaof(ip->ip_src) == INADDR_ANY)
847155Swnj 		ip->ip_src.s_addr =
857155Swnj 		    ((struct sockaddr_in *)&ifp->if_addr)->sin_addr.s_addr;
8610402Ssam #endif
876505Ssam 
887155Swnj 	/*
8910402Ssam 	 * Look for broadcast address and
9010402Ssam 	 * and verify user is allowed to send
9110146Ssam 	 * such a packet.
927155Swnj 	 */
9312762Ssam 	if (in_lnaof(((struct sockaddr_in *)dst)->sin_addr) == INADDR_ANY) {
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);
1177155Swnj 		error = (*ifp->if_output)(ifp, m, 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;
1595892Sroot 		if (off + len >= ip->ip_len-hlen)
1605892Sroot 			len = mhip->ip_len = ip->ip_len - hlen - off;
1614924Swnj 		else {
1624924Swnj 			mhip->ip_len = len;
1634924Swnj 			mhip->ip_off |= IP_MF;
1644496Swnj 		}
1655770Swnj 		mhip->ip_len += sizeof (struct ip);
1665770Swnj 		mhip->ip_len = htons((u_short)mhip->ip_len);
1674924Swnj 		mh->m_next = m_copy(m, off, len);
1684924Swnj 		if (mh->m_next == 0) {
1694967Swnj 			(void) m_free(mh);
1706505Ssam 			error = ENOBUFS;	/* ??? */
1714924Swnj 			goto bad;
1724674Swnj 		}
1735892Sroot 		mhip->ip_off = htons((u_short)mhip->ip_off);
1745892Sroot 		mhip->ip_sum = 0;
1755892Sroot 		mhip->ip_sum = in_cksum(mh, hlen);
1766505Ssam 		if (error = (*ifp->if_output)(ifp, mh, dst))
1776505Ssam 			break;
1784924Swnj 	}
1797155Swnj 	m_freem(m);
1807155Swnj 	goto done;
1817155Swnj 
1824924Swnj bad:
1834924Swnj 	m_freem(m);
1847155Swnj done:
18512417Ssam 	if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt)
1867155Swnj 		RTFREE(ro->ro_rt);
1876505Ssam 	return (error);
1884924Swnj }
1894924Swnj 
1904924Swnj /*
1914924Swnj  * Copy options from ip to jp.
1924952Swnj  * If off is 0 all options are copied
1934924Swnj  * otherwise copy selectively.
1944924Swnj  */
1954924Swnj ip_optcopy(ip, jp, off)
1964924Swnj 	struct ip *ip, *jp;
1974924Swnj 	int off;
1984924Swnj {
1994924Swnj 	register u_char *cp, *dp;
2004924Swnj 	int opt, optlen, cnt;
2014924Swnj 
2024924Swnj 	cp = (u_char *)(ip + 1);
2034924Swnj 	dp = (u_char *)(jp + 1);
2044924Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
2054924Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2064924Swnj 		opt = cp[0];
2074924Swnj 		if (opt == IPOPT_EOL)
2084924Swnj 			break;
2094924Swnj 		if (opt == IPOPT_NOP)
2104924Swnj 			optlen = 1;
2114924Swnj 		else
2124924Swnj 			optlen = cp[1];
2134924Swnj 		if (optlen > cnt)			/* XXX */
2144924Swnj 			optlen = cnt;			/* XXX */
2154924Swnj 		if (off == 0 || IPOPT_COPIED(opt)) {
2164952Swnj 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
2174924Swnj 			dp += optlen;
2184674Swnj 		}
2194545Swnj 	}
2204924Swnj 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
2214924Swnj 		*dp++ = IPOPT_EOL;
2224924Swnj 	return (optlen);
2234496Swnj }
224