xref: /csrg-svn/sys/netinet/ip_output.c (revision 10146)
1*10146Ssam /*	ip_output.c	1.43	83/01/04	*/
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??? */
568638Sroot 			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 	 */
73*10146Ssam 	if (ip->ip_src.s_addr == INADDR_ANY)
747155Swnj 		ip->ip_src.s_addr =
757155Swnj 		    ((struct sockaddr_in *)&ifp->if_addr)->sin_addr.s_addr;
766505Ssam 
777155Swnj 	/*
78*10146Ssam 	 * Map broadcast address to hardware's broadcast
79*10146Ssam 	 * address and verify user is allowed to send
80*10146Ssam 	 * such a packet.
817155Swnj 	 */
82*10146Ssam 	if (in_lnaof(dst) == INADDR_ANY) {
836339Ssam 		struct sockaddr_in *sin;
846339Ssam 
85*10146Ssam 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
86*10146Ssam 			error = EADDRNOTAVAIL;
87*10146Ssam 			goto bad;
88*10146Ssam 		}
89*10146Ssam 		if (!allowbroadcast) {
907155Swnj 			error = EACCES;
916339Ssam 			goto bad;
926505Ssam 		}
93*10146Ssam 		/* don't allow broadcast messages to be fragmented */
94*10146Ssam 		if (ip->ip_len > ifp->if_mtu) {
95*10146Ssam 			error = EMSGSIZE;
96*10146Ssam 			goto bad;
97*10146Ssam 		}
98*10146Ssam 		sin = (struct sockaddr_in *)&ifp->if_broadaddr;
99*10146Ssam 		dst.sin_addr = sin->sin_addr;
1006339Ssam 	}
1016339Ssam 
1025085Swnj 	/*
1034924Swnj 	 * If small enough for interface, can just send directly.
1044545Swnj 	 */
1055085Swnj 	if (ip->ip_len <= ifp->if_mtu) {
1065085Swnj 		ip->ip_len = htons((u_short)ip->ip_len);
1075085Swnj 		ip->ip_off = htons((u_short)ip->ip_off);
1085085Swnj 		ip->ip_sum = 0;
1095085Swnj 		ip->ip_sum = in_cksum(m, hlen);
1107155Swnj 		error = (*ifp->if_output)(ifp, m, dst);
1117155Swnj 		goto done;
1124908Swnj 	}
1134924Swnj 
1144924Swnj 	/*
1154924Swnj 	 * Too large for interface; fragment if possible.
1164924Swnj 	 * Must be able to put at least 8 bytes per fragment.
1174924Swnj 	 */
1186505Ssam 	if (ip->ip_off & IP_DF) {
1196505Ssam 		error = EMSGSIZE;
1204924Swnj 		goto bad;
1216505Ssam 	}
1225085Swnj 	len = (ifp->if_mtu - hlen) &~ 7;
1236505Ssam 	if (len < 8) {
1246505Ssam 		error = EMSGSIZE;
1254924Swnj 		goto bad;
1266505Ssam 	}
1274924Swnj 
1284924Swnj 	/*
1294924Swnj 	 * Discard IP header from logical mbuf for m_copy's sake.
1304924Swnj 	 * Loop through length of segment, make a copy of each
1314924Swnj 	 * part and output.
1324924Swnj 	 */
1334924Swnj 	m->m_len -= sizeof (struct ip);
1344924Swnj 	m->m_off += sizeof (struct ip);
1355892Sroot 	for (off = 0; off < ip->ip_len-hlen; off += len) {
13610012Ssam 		struct mbuf *mh = m_get(M_DONTWAIT, MT_HEADER);
1374924Swnj 		struct ip *mhip;
1384924Swnj 
1396505Ssam 		if (mh == 0) {
1406505Ssam 			error = ENOBUFS;
1414924Swnj 			goto bad;
1426505Ssam 		}
1434924Swnj 		mh->m_off = MMAXOFF - hlen;
1444924Swnj 		mhip = mtod(mh, struct ip *);
1454924Swnj 		*mhip = *ip;
1464952Swnj 		if (hlen > sizeof (struct ip)) {
1474924Swnj 			int olen = ip_optcopy(ip, mhip, off);
1484924Swnj 			mh->m_len = sizeof (struct ip) + olen;
1494924Swnj 		} else
1504924Swnj 			mh->m_len = sizeof (struct ip);
1515770Swnj 		mhip->ip_off = off >> 3;
1525892Sroot 		if (off + len >= ip->ip_len-hlen)
1535892Sroot 			len = mhip->ip_len = ip->ip_len - hlen - off;
1544924Swnj 		else {
1554924Swnj 			mhip->ip_len = len;
1564924Swnj 			mhip->ip_off |= IP_MF;
1574496Swnj 		}
1585770Swnj 		mhip->ip_len += sizeof (struct ip);
1595770Swnj 		mhip->ip_len = htons((u_short)mhip->ip_len);
1604924Swnj 		mh->m_next = m_copy(m, off, len);
1614924Swnj 		if (mh->m_next == 0) {
1624967Swnj 			(void) m_free(mh);
1636505Ssam 			error = ENOBUFS;	/* ??? */
1644924Swnj 			goto bad;
1654674Swnj 		}
1665892Sroot 		mhip->ip_off = htons((u_short)mhip->ip_off);
1675892Sroot 		mhip->ip_sum = 0;
1685892Sroot 		mhip->ip_sum = in_cksum(mh, hlen);
1696505Ssam 		if (error = (*ifp->if_output)(ifp, mh, dst))
1706505Ssam 			break;
1714924Swnj 	}
1727155Swnj 	m_freem(m);
1737155Swnj 	goto done;
1747155Swnj 
1757155Swnj unreachable:
1767155Swnj 	if (ipnorouteprint)
1777155Swnj 		printf("no route to %x (from %x, len %d)\n",
1787155Swnj 		    ip->ip_dst.s_addr, ip->ip_src.s_addr, ip->ip_len);
1797155Swnj 	error = ENETUNREACH;
1804924Swnj bad:
1814924Swnj 	m_freem(m);
1827155Swnj done:
1837155Swnj 	if (ro == &iproute && ro->ro_rt)
1847155Swnj 		RTFREE(ro->ro_rt);
1856505Ssam 	return (error);
1864924Swnj }
1874924Swnj 
1884924Swnj /*
1894924Swnj  * Copy options from ip to jp.
1904952Swnj  * If off is 0 all options are copied
1914924Swnj  * otherwise copy selectively.
1924924Swnj  */
1934924Swnj ip_optcopy(ip, jp, off)
1944924Swnj 	struct ip *ip, *jp;
1954924Swnj 	int off;
1964924Swnj {
1974924Swnj 	register u_char *cp, *dp;
1984924Swnj 	int opt, optlen, cnt;
1994924Swnj 
2004924Swnj 	cp = (u_char *)(ip + 1);
2014924Swnj 	dp = (u_char *)(jp + 1);
2024924Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
2034924Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2044924Swnj 		opt = cp[0];
2054924Swnj 		if (opt == IPOPT_EOL)
2064924Swnj 			break;
2074924Swnj 		if (opt == IPOPT_NOP)
2084924Swnj 			optlen = 1;
2094924Swnj 		else
2104924Swnj 			optlen = cp[1];
2114924Swnj 		if (optlen > cnt)			/* XXX */
2124924Swnj 			optlen = cnt;			/* XXX */
2134924Swnj 		if (off == 0 || IPOPT_COPIED(opt)) {
2144952Swnj 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
2154924Swnj 			dp += optlen;
2164674Swnj 		}
2174545Swnj 	}
2184924Swnj 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
2194924Swnj 		*dp++ = IPOPT_EOL;
2204924Swnj 	return (optlen);
2214496Swnj }
222