xref: /csrg-svn/sys/netinet/ip_output.c (revision 8638)
1*8638Sroot /*	ip_output.c	1.39	82/10/17	*/
24571Swnj 
34496Swnj #include "../h/param.h"
44662Swnj #include "../h/mbuf.h"
58532Sroot #include "../vax/mtpr.h"
64662Swnj #include "../h/socket.h"
74924Swnj #include "../h/socketvar.h"
88400Swnj #include "../netinet/in.h"
98400Swnj #include "../netinet/in_systm.h"
105085Swnj #include "../net/if.h"
118400Swnj #include "../netinet/ip.h"
128400Swnj #include "../netinet/ip_var.h"
136339Ssam #include "../net/route.h"
146505Ssam #include <errno.h>
154496Swnj 
167155Swnj int	ipnorouteprint = 0;
177155Swnj 
186339Ssam ip_output(m, opt, ro, allowbroadcast)
194924Swnj 	struct mbuf *m;
205085Swnj 	struct mbuf *opt;
216339Ssam 	struct route *ro;
226211Swnj 	int allowbroadcast;
234496Swnj {
244924Swnj 	register struct ip *ip = mtod(m, struct ip *);
255085Swnj 	register struct ifnet *ifp;
266505Ssam 	int len, hlen = sizeof (struct ip), off, error = 0;
276339Ssam 	struct route iproute;
286351Ssam 	struct sockaddr *dst;
294496Swnj 
305219Swnj 	if (opt)				/* XXX */
315242Sroot 		(void) m_free(opt);		/* XXX */
324924Swnj 	/*
334924Swnj 	 * Fill in IP header.
344924Swnj 	 */
354924Swnj 	ip->ip_v = IPVERSION;
364924Swnj 	ip->ip_hl = hlen >> 2;
374924Swnj 	ip->ip_off &= IP_DF;
385085Swnj 	ip->ip_id = htons(ip_id++);
394496Swnj 
404545Swnj 	/*
417155Swnj 	 * Route packet.
425085Swnj 	 */
436339Ssam 	if (ro == 0) {
446339Ssam 		ro = &iproute;
456339Ssam 		bzero((caddr_t)ro, sizeof (*ro));
465085Swnj 	}
477155Swnj 	dst = &ro->ro_dst;
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;
517155Swnj 		/*
527155Swnj 		 * If routing to interface only, short circuit routing lookup.
537155Swnj 		 */
547155Swnj 		if (ro == &routetoif) {
557155Swnj 			/* check ifp is AF_INET??? */
56*8638Sroot 			ifp = if_ifonnetof(in_netof(ip->ip_dst));
577155Swnj 			if (ifp == 0)
587155Swnj 				goto unreachable;
597155Swnj 			goto gotif;
607155Swnj 		}
616368Ssam 		rtalloc(ro);
626339Ssam 	}
637155Swnj 	if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0)
647155Swnj 		goto unreachable;
657155Swnj 	ro->ro_rt->rt_use++;
667155Swnj 	if (ro->ro_rt->rt_flags & RTF_GATEWAY)
677155Swnj 		dst = &ro->ro_rt->rt_gateway;
687155Swnj gotif:
697155Swnj 	/*
707155Swnj 	 * If source address not specified yet, use address
717155Swnj 	 * of outgoing interface.
727155Swnj 	 */
737155Swnj 	if (ip->ip_src.s_addr == 0)
747155Swnj 		ip->ip_src.s_addr =
757155Swnj 		    ((struct sockaddr_in *)&ifp->if_addr)->sin_addr.s_addr;
766505Ssam 
777155Swnj 	/*
787155Swnj 	 * Have interface for packet.  Allow
797155Swnj 	 * broadcasts only by authorized users.
807155Swnj 	 */
816339Ssam 	if (!allowbroadcast && (ifp->if_flags & IFF_BROADCAST)) {
826339Ssam 		struct sockaddr_in *sin;
836339Ssam 
846339Ssam 		sin = (struct sockaddr_in *)&ifp->if_broadaddr;
856505Ssam 		if (sin->sin_addr.s_addr == ip->ip_dst.s_addr) {
867155Swnj 			error = EACCES;
876339Ssam 			goto bad;
886505Ssam 		}
896339Ssam 	}
906339Ssam 
915085Swnj 	/*
924924Swnj 	 * If small enough for interface, can just send directly.
934545Swnj 	 */
945085Swnj 	if (ip->ip_len <= ifp->if_mtu) {
958598Sroot #if vax || pdp11 || ns16032
965085Swnj 		ip->ip_len = htons((u_short)ip->ip_len);
975085Swnj 		ip->ip_off = htons((u_short)ip->ip_off);
985219Swnj #endif
995085Swnj 		ip->ip_sum = 0;
1005085Swnj 		ip->ip_sum = in_cksum(m, hlen);
1017155Swnj 		error = (*ifp->if_output)(ifp, m, dst);
1027155Swnj 		goto done;
1034908Swnj 	}
1044924Swnj 
1054924Swnj 	/*
1064924Swnj 	 * Too large for interface; fragment if possible.
1074924Swnj 	 * Must be able to put at least 8 bytes per fragment.
1084924Swnj 	 */
1096505Ssam 	if (ip->ip_off & IP_DF) {
1106505Ssam 		error = EMSGSIZE;
1114924Swnj 		goto bad;
1126505Ssam 	}
1135085Swnj 	len = (ifp->if_mtu - hlen) &~ 7;
1146505Ssam 	if (len < 8) {
1156505Ssam 		error = EMSGSIZE;
1164924Swnj 		goto bad;
1176505Ssam 	}
1184924Swnj 
1194924Swnj 	/*
1204924Swnj 	 * Discard IP header from logical mbuf for m_copy's sake.
1214924Swnj 	 * Loop through length of segment, make a copy of each
1224924Swnj 	 * part and output.
1234924Swnj 	 */
1244924Swnj 	m->m_len -= sizeof (struct ip);
1254924Swnj 	m->m_off += sizeof (struct ip);
1265892Sroot 	for (off = 0; off < ip->ip_len-hlen; off += len) {
1275584Sroot 		struct mbuf *mh = m_get(M_DONTWAIT);
1284924Swnj 		struct ip *mhip;
1294924Swnj 
1306505Ssam 		if (mh == 0) {
1316505Ssam 			error = ENOBUFS;
1324924Swnj 			goto bad;
1336505Ssam 		}
1344924Swnj 		mh->m_off = MMAXOFF - hlen;
1354924Swnj 		mhip = mtod(mh, struct ip *);
1364924Swnj 		*mhip = *ip;
1374952Swnj 		if (hlen > sizeof (struct ip)) {
1384924Swnj 			int olen = ip_optcopy(ip, mhip, off);
1394924Swnj 			mh->m_len = sizeof (struct ip) + olen;
1404924Swnj 		} else
1414924Swnj 			mh->m_len = sizeof (struct ip);
1425770Swnj 		mhip->ip_off = off >> 3;
1435892Sroot 		if (off + len >= ip->ip_len-hlen)
1445892Sroot 			len = mhip->ip_len = ip->ip_len - hlen - off;
1454924Swnj 		else {
1464924Swnj 			mhip->ip_len = len;
1474924Swnj 			mhip->ip_off |= IP_MF;
1484496Swnj 		}
1495770Swnj 		mhip->ip_len += sizeof (struct ip);
1508598Sroot #if vax || pdp11 || ns16032
1515770Swnj 		mhip->ip_len = htons((u_short)mhip->ip_len);
1525770Swnj #endif
1534924Swnj 		mh->m_next = m_copy(m, off, len);
1544924Swnj 		if (mh->m_next == 0) {
1554967Swnj 			(void) m_free(mh);
1566505Ssam 			error = ENOBUFS;	/* ??? */
1574924Swnj 			goto bad;
1584674Swnj 		}
1598598Sroot #if vax || pdp11 || ns16032
1605892Sroot 		mhip->ip_off = htons((u_short)mhip->ip_off);
1615770Swnj #endif
1625892Sroot 		mhip->ip_sum = 0;
1635892Sroot 		mhip->ip_sum = in_cksum(mh, hlen);
1646505Ssam 		if (error = (*ifp->if_output)(ifp, mh, dst))
1656505Ssam 			break;
1664924Swnj 	}
1677155Swnj 	m_freem(m);
1687155Swnj 	goto done;
1697155Swnj 
1707155Swnj unreachable:
1717155Swnj 	if (ipnorouteprint)
1727155Swnj 		printf("no route to %x (from %x, len %d)\n",
1737155Swnj 		    ip->ip_dst.s_addr, ip->ip_src.s_addr, ip->ip_len);
1747155Swnj 	error = ENETUNREACH;
1754924Swnj bad:
1764924Swnj 	m_freem(m);
1777155Swnj done:
1787155Swnj 	if (ro == &iproute && ro->ro_rt)
1797155Swnj 		RTFREE(ro->ro_rt);
1806505Ssam 	return (error);
1814924Swnj }
1824924Swnj 
1834924Swnj /*
1844924Swnj  * Copy options from ip to jp.
1854952Swnj  * If off is 0 all options are copied
1864924Swnj  * otherwise copy selectively.
1874924Swnj  */
1884924Swnj ip_optcopy(ip, jp, off)
1894924Swnj 	struct ip *ip, *jp;
1904924Swnj 	int off;
1914924Swnj {
1924924Swnj 	register u_char *cp, *dp;
1934924Swnj 	int opt, optlen, cnt;
1944924Swnj 
1954924Swnj 	cp = (u_char *)(ip + 1);
1964924Swnj 	dp = (u_char *)(jp + 1);
1974924Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1984924Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1994924Swnj 		opt = cp[0];
2004924Swnj 		if (opt == IPOPT_EOL)
2014924Swnj 			break;
2024924Swnj 		if (opt == IPOPT_NOP)
2034924Swnj 			optlen = 1;
2044924Swnj 		else
2054924Swnj 			optlen = cp[1];
2064924Swnj 		if (optlen > cnt)			/* XXX */
2074924Swnj 			optlen = cnt;			/* XXX */
2084924Swnj 		if (off == 0 || IPOPT_COPIED(opt)) {
2094952Swnj 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
2104924Swnj 			dp += optlen;
2114674Swnj 		}
2124545Swnj 	}
2134924Swnj 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
2144924Swnj 		*dp++ = IPOPT_EOL;
2154924Swnj 	return (optlen);
2164496Swnj }
217