xref: /csrg-svn/sys/netinet/ip_output.c (revision 10402)
1*10402Ssam /*	ip_output.c	1.45	83/01/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??? */
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:
69*10402Ssam #ifndef notdef
707155Swnj 	/*
717155Swnj 	 * If source address not specified yet, use address
727155Swnj 	 * of outgoing interface.
737155Swnj 	 */
74*10402Ssam 	if (in_lnaof(ip->ip_src) == INADDR_ANY)
757155Swnj 		ip->ip_src.s_addr =
767155Swnj 		    ((struct sockaddr_in *)&ifp->if_addr)->sin_addr.s_addr;
77*10402Ssam #endif
786505Ssam 
797155Swnj 	/*
80*10402Ssam 	 * Look for broadcast address and
81*10402Ssam 	 * and verify user is allowed to send
8210146Ssam 	 * such a packet.
837155Swnj 	 */
8410146Ssam 	if (in_lnaof(dst) == INADDR_ANY) {
8510146Ssam 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
8610146Ssam 			error = EADDRNOTAVAIL;
8710146Ssam 			goto bad;
8810146Ssam 		}
8910146Ssam 		if (!allowbroadcast) {
907155Swnj 			error = EACCES;
916339Ssam 			goto bad;
926505Ssam 		}
9310146Ssam 		/* don't allow broadcast messages to be fragmented */
9410146Ssam 		if (ip->ip_len > ifp->if_mtu) {
9510146Ssam 			error = EMSGSIZE;
9610146Ssam 			goto bad;
9710146Ssam 		}
986339Ssam 	}
996339Ssam 
1005085Swnj 	/*
1014924Swnj 	 * If small enough for interface, can just send directly.
1024545Swnj 	 */
1035085Swnj 	if (ip->ip_len <= ifp->if_mtu) {
1045085Swnj 		ip->ip_len = htons((u_short)ip->ip_len);
1055085Swnj 		ip->ip_off = htons((u_short)ip->ip_off);
1065085Swnj 		ip->ip_sum = 0;
1075085Swnj 		ip->ip_sum = in_cksum(m, hlen);
1087155Swnj 		error = (*ifp->if_output)(ifp, m, dst);
1097155Swnj 		goto done;
1104908Swnj 	}
1114924Swnj 
1124924Swnj 	/*
1134924Swnj 	 * Too large for interface; fragment if possible.
1144924Swnj 	 * Must be able to put at least 8 bytes per fragment.
1154924Swnj 	 */
1166505Ssam 	if (ip->ip_off & IP_DF) {
1176505Ssam 		error = EMSGSIZE;
1184924Swnj 		goto bad;
1196505Ssam 	}
1205085Swnj 	len = (ifp->if_mtu - hlen) &~ 7;
1216505Ssam 	if (len < 8) {
1226505Ssam 		error = EMSGSIZE;
1234924Swnj 		goto bad;
1246505Ssam 	}
1254924Swnj 
1264924Swnj 	/*
1274924Swnj 	 * Discard IP header from logical mbuf for m_copy's sake.
1284924Swnj 	 * Loop through length of segment, make a copy of each
1294924Swnj 	 * part and output.
1304924Swnj 	 */
1314924Swnj 	m->m_len -= sizeof (struct ip);
1324924Swnj 	m->m_off += sizeof (struct ip);
1335892Sroot 	for (off = 0; off < ip->ip_len-hlen; off += len) {
13410012Ssam 		struct mbuf *mh = m_get(M_DONTWAIT, MT_HEADER);
1354924Swnj 		struct ip *mhip;
1364924Swnj 
1376505Ssam 		if (mh == 0) {
1386505Ssam 			error = ENOBUFS;
1394924Swnj 			goto bad;
1406505Ssam 		}
1414924Swnj 		mh->m_off = MMAXOFF - hlen;
1424924Swnj 		mhip = mtod(mh, struct ip *);
1434924Swnj 		*mhip = *ip;
1444952Swnj 		if (hlen > sizeof (struct ip)) {
1454924Swnj 			int olen = ip_optcopy(ip, mhip, off);
1464924Swnj 			mh->m_len = sizeof (struct ip) + olen;
1474924Swnj 		} else
1484924Swnj 			mh->m_len = sizeof (struct ip);
1495770Swnj 		mhip->ip_off = off >> 3;
1505892Sroot 		if (off + len >= ip->ip_len-hlen)
1515892Sroot 			len = mhip->ip_len = ip->ip_len - hlen - off;
1524924Swnj 		else {
1534924Swnj 			mhip->ip_len = len;
1544924Swnj 			mhip->ip_off |= IP_MF;
1554496Swnj 		}
1565770Swnj 		mhip->ip_len += sizeof (struct ip);
1575770Swnj 		mhip->ip_len = htons((u_short)mhip->ip_len);
1584924Swnj 		mh->m_next = m_copy(m, off, len);
1594924Swnj 		if (mh->m_next == 0) {
1604967Swnj 			(void) m_free(mh);
1616505Ssam 			error = ENOBUFS;	/* ??? */
1624924Swnj 			goto bad;
1634674Swnj 		}
1645892Sroot 		mhip->ip_off = htons((u_short)mhip->ip_off);
1655892Sroot 		mhip->ip_sum = 0;
1665892Sroot 		mhip->ip_sum = in_cksum(mh, hlen);
1676505Ssam 		if (error = (*ifp->if_output)(ifp, mh, dst))
1686505Ssam 			break;
1694924Swnj 	}
1707155Swnj 	m_freem(m);
1717155Swnj 	goto done;
1727155Swnj 
1737155Swnj unreachable:
1747155Swnj 	if (ipnorouteprint)
1757155Swnj 		printf("no route to %x (from %x, len %d)\n",
1767155Swnj 		    ip->ip_dst.s_addr, ip->ip_src.s_addr, ip->ip_len);
1777155Swnj 	error = ENETUNREACH;
1784924Swnj bad:
1794924Swnj 	m_freem(m);
1807155Swnj done:
1817155Swnj 	if (ro == &iproute && ro->ro_rt)
1827155Swnj 		RTFREE(ro->ro_rt);
1836505Ssam 	return (error);
1844924Swnj }
1854924Swnj 
1864924Swnj /*
1874924Swnj  * Copy options from ip to jp.
1884952Swnj  * If off is 0 all options are copied
1894924Swnj  * otherwise copy selectively.
1904924Swnj  */
1914924Swnj ip_optcopy(ip, jp, off)
1924924Swnj 	struct ip *ip, *jp;
1934924Swnj 	int off;
1944924Swnj {
1954924Swnj 	register u_char *cp, *dp;
1964924Swnj 	int opt, optlen, cnt;
1974924Swnj 
1984924Swnj 	cp = (u_char *)(ip + 1);
1994924Swnj 	dp = (u_char *)(jp + 1);
2004924Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
2014924Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2024924Swnj 		opt = cp[0];
2034924Swnj 		if (opt == IPOPT_EOL)
2044924Swnj 			break;
2054924Swnj 		if (opt == IPOPT_NOP)
2064924Swnj 			optlen = 1;
2074924Swnj 		else
2084924Swnj 			optlen = cp[1];
2094924Swnj 		if (optlen > cnt)			/* XXX */
2104924Swnj 			optlen = cnt;			/* XXX */
2114924Swnj 		if (off == 0 || IPOPT_COPIED(opt)) {
2124952Swnj 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
2134924Swnj 			dp += optlen;
2144674Swnj 		}
2154545Swnj 	}
2164924Swnj 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
2174924Swnj 		*dp++ = IPOPT_EOL;
2184924Swnj 	return (optlen);
2194496Swnj }
220