xref: /csrg-svn/sys/netinet/ip_output.c (revision 10012)
1*10012Ssam /*	ip_output.c	1.42	82/12/30	*/
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 	 */
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) {
955085Swnj 		ip->ip_len = htons((u_short)ip->ip_len);
965085Swnj 		ip->ip_off = htons((u_short)ip->ip_off);
975085Swnj 		ip->ip_sum = 0;
985085Swnj 		ip->ip_sum = in_cksum(m, hlen);
997155Swnj 		error = (*ifp->if_output)(ifp, m, dst);
1007155Swnj 		goto done;
1014908Swnj 	}
1024924Swnj 
1034924Swnj 	/*
1044924Swnj 	 * Too large for interface; fragment if possible.
1054924Swnj 	 * Must be able to put at least 8 bytes per fragment.
1064924Swnj 	 */
1076505Ssam 	if (ip->ip_off & IP_DF) {
1086505Ssam 		error = EMSGSIZE;
1094924Swnj 		goto bad;
1106505Ssam 	}
1115085Swnj 	len = (ifp->if_mtu - hlen) &~ 7;
1126505Ssam 	if (len < 8) {
1136505Ssam 		error = EMSGSIZE;
1144924Swnj 		goto bad;
1156505Ssam 	}
1164924Swnj 
1174924Swnj 	/*
1184924Swnj 	 * Discard IP header from logical mbuf for m_copy's sake.
1194924Swnj 	 * Loop through length of segment, make a copy of each
1204924Swnj 	 * part and output.
1214924Swnj 	 */
1224924Swnj 	m->m_len -= sizeof (struct ip);
1234924Swnj 	m->m_off += sizeof (struct ip);
1245892Sroot 	for (off = 0; off < ip->ip_len-hlen; off += len) {
125*10012Ssam 		struct mbuf *mh = m_get(M_DONTWAIT, MT_HEADER);
1264924Swnj 		struct ip *mhip;
1274924Swnj 
1286505Ssam 		if (mh == 0) {
1296505Ssam 			error = ENOBUFS;
1304924Swnj 			goto bad;
1316505Ssam 		}
1324924Swnj 		mh->m_off = MMAXOFF - hlen;
1334924Swnj 		mhip = mtod(mh, struct ip *);
1344924Swnj 		*mhip = *ip;
1354952Swnj 		if (hlen > sizeof (struct ip)) {
1364924Swnj 			int olen = ip_optcopy(ip, mhip, off);
1374924Swnj 			mh->m_len = sizeof (struct ip) + olen;
1384924Swnj 		} else
1394924Swnj 			mh->m_len = sizeof (struct ip);
1405770Swnj 		mhip->ip_off = off >> 3;
1415892Sroot 		if (off + len >= ip->ip_len-hlen)
1425892Sroot 			len = mhip->ip_len = ip->ip_len - hlen - off;
1434924Swnj 		else {
1444924Swnj 			mhip->ip_len = len;
1454924Swnj 			mhip->ip_off |= IP_MF;
1464496Swnj 		}
1475770Swnj 		mhip->ip_len += sizeof (struct ip);
1485770Swnj 		mhip->ip_len = htons((u_short)mhip->ip_len);
1494924Swnj 		mh->m_next = m_copy(m, off, len);
1504924Swnj 		if (mh->m_next == 0) {
1514967Swnj 			(void) m_free(mh);
1526505Ssam 			error = ENOBUFS;	/* ??? */
1534924Swnj 			goto bad;
1544674Swnj 		}
1555892Sroot 		mhip->ip_off = htons((u_short)mhip->ip_off);
1565892Sroot 		mhip->ip_sum = 0;
1575892Sroot 		mhip->ip_sum = in_cksum(mh, hlen);
1586505Ssam 		if (error = (*ifp->if_output)(ifp, mh, dst))
1596505Ssam 			break;
1604924Swnj 	}
1617155Swnj 	m_freem(m);
1627155Swnj 	goto done;
1637155Swnj 
1647155Swnj unreachable:
1657155Swnj 	if (ipnorouteprint)
1667155Swnj 		printf("no route to %x (from %x, len %d)\n",
1677155Swnj 		    ip->ip_dst.s_addr, ip->ip_src.s_addr, ip->ip_len);
1687155Swnj 	error = ENETUNREACH;
1694924Swnj bad:
1704924Swnj 	m_freem(m);
1717155Swnj done:
1727155Swnj 	if (ro == &iproute && ro->ro_rt)
1737155Swnj 		RTFREE(ro->ro_rt);
1746505Ssam 	return (error);
1754924Swnj }
1764924Swnj 
1774924Swnj /*
1784924Swnj  * Copy options from ip to jp.
1794952Swnj  * If off is 0 all options are copied
1804924Swnj  * otherwise copy selectively.
1814924Swnj  */
1824924Swnj ip_optcopy(ip, jp, off)
1834924Swnj 	struct ip *ip, *jp;
1844924Swnj 	int off;
1854924Swnj {
1864924Swnj 	register u_char *cp, *dp;
1874924Swnj 	int opt, optlen, cnt;
1884924Swnj 
1894924Swnj 	cp = (u_char *)(ip + 1);
1904924Swnj 	dp = (u_char *)(jp + 1);
1914924Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1924924Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1934924Swnj 		opt = cp[0];
1944924Swnj 		if (opt == IPOPT_EOL)
1954924Swnj 			break;
1964924Swnj 		if (opt == IPOPT_NOP)
1974924Swnj 			optlen = 1;
1984924Swnj 		else
1994924Swnj 			optlen = cp[1];
2004924Swnj 		if (optlen > cnt)			/* XXX */
2014924Swnj 			optlen = cnt;			/* XXX */
2024924Swnj 		if (off == 0 || IPOPT_COPIED(opt)) {
2034952Swnj 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
2044924Swnj 			dp += optlen;
2054674Swnj 		}
2064545Swnj 	}
2074924Swnj 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
2084924Swnj 		*dp++ = IPOPT_EOL;
2094924Swnj 	return (optlen);
2104496Swnj }
211