xref: /csrg-svn/sys/netinet/ip_output.c (revision 15716)
1*15716Skarels /*	ip_output.c	6.3	83/12/15	*/
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);
69*15716Skarels 	} else if (ro->ro_rt->rt_flags & RTF_UP == 0) {
70*15716Skarels 		/*
71*15716Skarels 		 * The old route has gone away; try for a new one.
72*15716Skarels 		 */
73*15716Skarels 		rtfree(ro->ro_rt);
74*15716Skarels 		rtalloc(ro);
756339Ssam 	}
7612417Ssam 	if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) {
7712417Ssam 		error = ENETUNREACH;
7812417Ssam 		goto bad;
7912417Ssam 	}
807155Swnj 	ro->ro_rt->rt_use++;
8115276Ssam 	if (ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST))
827155Swnj 		dst = &ro->ro_rt->rt_gateway;
837155Swnj gotif:
8410402Ssam #ifndef notdef
857155Swnj 	/*
867155Swnj 	 * If source address not specified yet, use address
877155Swnj 	 * of outgoing interface.
887155Swnj 	 */
8910402Ssam 	if (in_lnaof(ip->ip_src) == INADDR_ANY)
907155Swnj 		ip->ip_src.s_addr =
917155Swnj 		    ((struct sockaddr_in *)&ifp->if_addr)->sin_addr.s_addr;
9210402Ssam #endif
936505Ssam 
947155Swnj 	/*
9510402Ssam 	 * Look for broadcast address and
9610402Ssam 	 * and verify user is allowed to send
9710146Ssam 	 * such a packet.
987155Swnj 	 */
9912762Ssam 	if (in_lnaof(((struct sockaddr_in *)dst)->sin_addr) == INADDR_ANY) {
10010146Ssam 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
10110146Ssam 			error = EADDRNOTAVAIL;
10210146Ssam 			goto bad;
10310146Ssam 		}
10412417Ssam 		if ((flags & IP_ALLOWBROADCAST) == 0) {
1057155Swnj 			error = EACCES;
1066339Ssam 			goto bad;
1076505Ssam 		}
10810146Ssam 		/* don't allow broadcast messages to be fragmented */
10910146Ssam 		if (ip->ip_len > ifp->if_mtu) {
11010146Ssam 			error = EMSGSIZE;
11110146Ssam 			goto bad;
11210146Ssam 		}
1136339Ssam 	}
1146339Ssam 
1155085Swnj 	/*
1164924Swnj 	 * If small enough for interface, can just send directly.
1174545Swnj 	 */
1185085Swnj 	if (ip->ip_len <= ifp->if_mtu) {
1195085Swnj 		ip->ip_len = htons((u_short)ip->ip_len);
1205085Swnj 		ip->ip_off = htons((u_short)ip->ip_off);
1215085Swnj 		ip->ip_sum = 0;
1225085Swnj 		ip->ip_sum = in_cksum(m, hlen);
1237155Swnj 		error = (*ifp->if_output)(ifp, m, dst);
1247155Swnj 		goto done;
1254908Swnj 	}
1264924Swnj 
1274924Swnj 	/*
1284924Swnj 	 * Too large for interface; fragment if possible.
1294924Swnj 	 * Must be able to put at least 8 bytes per fragment.
1304924Swnj 	 */
1316505Ssam 	if (ip->ip_off & IP_DF) {
1326505Ssam 		error = EMSGSIZE;
1334924Swnj 		goto bad;
1346505Ssam 	}
1355085Swnj 	len = (ifp->if_mtu - hlen) &~ 7;
1366505Ssam 	if (len < 8) {
1376505Ssam 		error = EMSGSIZE;
1384924Swnj 		goto bad;
1396505Ssam 	}
1404924Swnj 
1414924Swnj 	/*
1424924Swnj 	 * Discard IP header from logical mbuf for m_copy's sake.
1434924Swnj 	 * Loop through length of segment, make a copy of each
1444924Swnj 	 * part and output.
1454924Swnj 	 */
1464924Swnj 	m->m_len -= sizeof (struct ip);
1474924Swnj 	m->m_off += sizeof (struct ip);
1485892Sroot 	for (off = 0; off < ip->ip_len-hlen; off += len) {
14910012Ssam 		struct mbuf *mh = m_get(M_DONTWAIT, MT_HEADER);
1504924Swnj 		struct ip *mhip;
1514924Swnj 
1526505Ssam 		if (mh == 0) {
1536505Ssam 			error = ENOBUFS;
1544924Swnj 			goto bad;
1556505Ssam 		}
1564924Swnj 		mh->m_off = MMAXOFF - hlen;
1574924Swnj 		mhip = mtod(mh, struct ip *);
1584924Swnj 		*mhip = *ip;
1594952Swnj 		if (hlen > sizeof (struct ip)) {
1604924Swnj 			int olen = ip_optcopy(ip, mhip, off);
1614924Swnj 			mh->m_len = sizeof (struct ip) + olen;
1624924Swnj 		} else
1634924Swnj 			mh->m_len = sizeof (struct ip);
1645770Swnj 		mhip->ip_off = off >> 3;
1655892Sroot 		if (off + len >= ip->ip_len-hlen)
1665892Sroot 			len = mhip->ip_len = ip->ip_len - hlen - off;
1674924Swnj 		else {
1684924Swnj 			mhip->ip_len = len;
1694924Swnj 			mhip->ip_off |= IP_MF;
1704496Swnj 		}
1715770Swnj 		mhip->ip_len += sizeof (struct ip);
1725770Swnj 		mhip->ip_len = htons((u_short)mhip->ip_len);
1734924Swnj 		mh->m_next = m_copy(m, off, len);
1744924Swnj 		if (mh->m_next == 0) {
1754967Swnj 			(void) m_free(mh);
1766505Ssam 			error = ENOBUFS;	/* ??? */
1774924Swnj 			goto bad;
1784674Swnj 		}
1795892Sroot 		mhip->ip_off = htons((u_short)mhip->ip_off);
1805892Sroot 		mhip->ip_sum = 0;
1815892Sroot 		mhip->ip_sum = in_cksum(mh, hlen);
1826505Ssam 		if (error = (*ifp->if_output)(ifp, mh, dst))
1836505Ssam 			break;
1844924Swnj 	}
1857155Swnj 	m_freem(m);
1867155Swnj 	goto done;
1877155Swnj 
1884924Swnj bad:
1894924Swnj 	m_freem(m);
1907155Swnj done:
19112417Ssam 	if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt)
1927155Swnj 		RTFREE(ro->ro_rt);
1936505Ssam 	return (error);
1944924Swnj }
1954924Swnj 
1964924Swnj /*
1974924Swnj  * Copy options from ip to jp.
1984952Swnj  * If off is 0 all options are copied
1994924Swnj  * otherwise copy selectively.
2004924Swnj  */
2014924Swnj ip_optcopy(ip, jp, off)
2024924Swnj 	struct ip *ip, *jp;
2034924Swnj 	int off;
2044924Swnj {
2054924Swnj 	register u_char *cp, *dp;
2064924Swnj 	int opt, optlen, cnt;
2074924Swnj 
2084924Swnj 	cp = (u_char *)(ip + 1);
2094924Swnj 	dp = (u_char *)(jp + 1);
2104924Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
2114924Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2124924Swnj 		opt = cp[0];
2134924Swnj 		if (opt == IPOPT_EOL)
2144924Swnj 			break;
2154924Swnj 		if (opt == IPOPT_NOP)
2164924Swnj 			optlen = 1;
2174924Swnj 		else
2184924Swnj 			optlen = cp[1];
2194924Swnj 		if (optlen > cnt)			/* XXX */
2204924Swnj 			optlen = cnt;			/* XXX */
2214924Swnj 		if (off == 0 || IPOPT_COPIED(opt)) {
2224952Swnj 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
2234924Swnj 			dp += optlen;
2244674Swnj 		}
2254545Swnj 	}
2264924Swnj 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
2274924Swnj 		*dp++ = IPOPT_EOL;
2284924Swnj 	return (optlen);
2294496Swnj }
230