xref: /csrg-svn/sys/netinet/ip_output.c (revision 31037)
123745Skarels /*
229144Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
323745Skarels  * All rights reserved.  The Berkeley software License Agreement
423745Skarels  * specifies the terms and conditions for redistribution.
523745Skarels  *
6*31037Skarels  *	@(#)ip_output.c	7.4 (Berkeley) 05/07/87
723745Skarels  */
84571Swnj 
917061Sbloom #include "param.h"
1017061Sbloom #include "mbuf.h"
1117061Sbloom #include "errno.h"
1224814Skarels #include "protosw.h"
1317061Sbloom #include "socket.h"
1417061Sbloom #include "socketvar.h"
1510893Ssam 
1610893Ssam #include "../net/if.h"
1710893Ssam #include "../net/route.h"
1810893Ssam 
1917061Sbloom #include "in.h"
2024814Skarels #include "in_pcb.h"
2117061Sbloom #include "in_systm.h"
2218375Skarels #include "in_var.h"
2317061Sbloom #include "ip.h"
2417061Sbloom #include "ip_var.h"
254496Swnj 
2612460Ssam #ifdef vax
2729923Skarels #include "../machine/mtpr.h"
2812460Ssam #endif
2910893Ssam 
3024814Skarels struct mbuf *ip_insertoptions();
3124814Skarels 
3224814Skarels /*
3324814Skarels  * IP output.  The packet in mbuf chain m contains a skeletal IP
34*31037Skarels  * header (with len, off, ttl, proto, tos, src, dst).
35*31037Skarels  * The mbuf chain containing the packet will be freed.
36*31037Skarels  * The mbuf opt, if present, will not be freed.
3724814Skarels  */
3812417Ssam ip_output(m, opt, ro, flags)
394924Swnj 	struct mbuf *m;
405085Swnj 	struct mbuf *opt;
416339Ssam 	struct route *ro;
4212417Ssam 	int flags;
434496Swnj {
4424814Skarels 	register struct ip *ip;
455085Swnj 	register struct ifnet *ifp;
466505Ssam 	int len, hlen = sizeof (struct ip), off, error = 0;
476339Ssam 	struct route iproute;
4816602Ssam 	struct sockaddr_in *dst;
494496Swnj 
5024814Skarels 	if (opt)
5124814Skarels 		m = ip_insertoptions(m, opt, &hlen);
5224814Skarels 	ip = mtod(m, struct ip *);
534924Swnj 	/*
544924Swnj 	 * Fill in IP header.
554924Swnj 	 */
5612417Ssam 	if ((flags & IP_FORWARDING) == 0) {
5712417Ssam 		ip->ip_v = IPVERSION;
5812417Ssam 		ip->ip_off &= IP_DF;
5912417Ssam 		ip->ip_id = htons(ip_id++);
6016545Skarels 		ip->ip_hl = hlen >> 2;
6124814Skarels 	} else
6224814Skarels 		hlen = ip->ip_hl << 2;
634496Swnj 
644545Swnj 	/*
657155Swnj 	 * Route packet.
665085Swnj 	 */
676339Ssam 	if (ro == 0) {
686339Ssam 		ro = &iproute;
696339Ssam 		bzero((caddr_t)ro, sizeof (*ro));
705085Swnj 	}
7116602Ssam 	dst = (struct sockaddr_in *)&ro->ro_dst;
7226156Skarels 	/*
7326156Skarels 	 * If there is a cached route,
7426156Skarels 	 * check that it is to the same destination
7526156Skarels 	 * and is still up.  If not, free it and try again.
7626156Skarels 	 */
7726156Skarels 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
7826156Skarels 	   dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
7926156Skarels 		RTFREE(ro->ro_rt);
8026156Skarels 		ro->ro_rt = (struct rtentry *)0;
8126156Skarels 	}
826339Ssam 	if (ro->ro_rt == 0) {
8316602Ssam 		dst->sin_family = AF_INET;
8416602Ssam 		dst->sin_addr = ip->ip_dst;
8526058Skarels 	}
8626058Skarels 	/*
8726058Skarels 	 * If routing to interface only,
8826058Skarels 	 * short circuit routing lookup.
8926058Skarels 	 */
9026058Skarels 	if (flags & IP_ROUTETOIF) {
9126058Skarels 		struct in_ifaddr *ia;
9227196Skarels 
9327973Skarels 		ia = (struct in_ifaddr *)ifa_ifwithdstaddr(dst);
9427196Skarels 		if (ia == 0)
9527196Skarels 			ia = in_iaonnetof(in_netof(ip->ip_dst));
9626058Skarels 		if (ia == 0) {
9726058Skarels 			error = ENETUNREACH;
9826058Skarels 			goto bad;
9926058Skarels 		}
10026058Skarels 		ifp = ia->ia_ifp;
10126058Skarels 	} else {
10226058Skarels 		if (ro->ro_rt == 0)
10326058Skarels 			rtalloc(ro);
10426058Skarels 		if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) {
10526058Skarels 			error = ENETUNREACH;
10626058Skarels 			goto bad;
10726058Skarels 		}
10826058Skarels 		ro->ro_rt->rt_use++;
10930761Skarels 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
11026058Skarels 			dst = (struct sockaddr_in *)&ro->ro_rt->rt_gateway;
1116339Ssam 	}
11223745Skarels #ifndef notdef
1137155Swnj 	/*
11423745Skarels 	 * If source address not specified yet, use address
11523745Skarels 	 * of outgoing interface.
11623745Skarels 	 */
11723745Skarels 	if (ip->ip_src.s_addr == INADDR_ANY) {
11823745Skarels 		register struct in_ifaddr *ia;
11923745Skarels 
12023745Skarels 		for (ia = in_ifaddr; ia; ia = ia->ia_next)
12123745Skarels 			if (ia->ia_ifp == ifp) {
12223745Skarels 				ip->ip_src = IA_SIN(ia)->sin_addr;
12323745Skarels 				break;
12423745Skarels 			}
12523745Skarels 	}
12623745Skarels #endif
12723745Skarels 	/*
12810402Ssam 	 * Look for broadcast address and
12910402Ssam 	 * and verify user is allowed to send
13010146Ssam 	 * such a packet.
1317155Swnj 	 */
13218375Skarels 	if (in_broadcast(dst->sin_addr)) {
13310146Ssam 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
13410146Ssam 			error = EADDRNOTAVAIL;
13510146Ssam 			goto bad;
13610146Ssam 		}
13712417Ssam 		if ((flags & IP_ALLOWBROADCAST) == 0) {
1387155Swnj 			error = EACCES;
1396339Ssam 			goto bad;
1406505Ssam 		}
14110146Ssam 		/* don't allow broadcast messages to be fragmented */
14210146Ssam 		if (ip->ip_len > ifp->if_mtu) {
14310146Ssam 			error = EMSGSIZE;
14410146Ssam 			goto bad;
14510146Ssam 		}
1466339Ssam 	}
1476339Ssam 
1485085Swnj 	/*
1494924Swnj 	 * If small enough for interface, can just send directly.
1504545Swnj 	 */
1515085Swnj 	if (ip->ip_len <= ifp->if_mtu) {
1525085Swnj 		ip->ip_len = htons((u_short)ip->ip_len);
1535085Swnj 		ip->ip_off = htons((u_short)ip->ip_off);
1545085Swnj 		ip->ip_sum = 0;
1555085Swnj 		ip->ip_sum = in_cksum(m, hlen);
15616602Ssam 		error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst);
1577155Swnj 		goto done;
1584908Swnj 	}
1594924Swnj 
1604924Swnj 	/*
1614924Swnj 	 * Too large for interface; fragment if possible.
1624924Swnj 	 * Must be able to put at least 8 bytes per fragment.
1634924Swnj 	 */
1646505Ssam 	if (ip->ip_off & IP_DF) {
1656505Ssam 		error = EMSGSIZE;
1664924Swnj 		goto bad;
1676505Ssam 	}
1685085Swnj 	len = (ifp->if_mtu - hlen) &~ 7;
1696505Ssam 	if (len < 8) {
1706505Ssam 		error = EMSGSIZE;
1714924Swnj 		goto bad;
1726505Ssam 	}
1734924Swnj 
1744924Swnj 	/*
1754924Swnj 	 * Discard IP header from logical mbuf for m_copy's sake.
1764924Swnj 	 * Loop through length of segment, make a copy of each
1774924Swnj 	 * part and output.
1784924Swnj 	 */
1794924Swnj 	m->m_len -= sizeof (struct ip);
1804924Swnj 	m->m_off += sizeof (struct ip);
1815892Sroot 	for (off = 0; off < ip->ip_len-hlen; off += len) {
18210012Ssam 		struct mbuf *mh = m_get(M_DONTWAIT, MT_HEADER);
1834924Swnj 		struct ip *mhip;
1844924Swnj 
1856505Ssam 		if (mh == 0) {
1866505Ssam 			error = ENOBUFS;
1874924Swnj 			goto bad;
1886505Ssam 		}
1894924Swnj 		mh->m_off = MMAXOFF - hlen;
1904924Swnj 		mhip = mtod(mh, struct ip *);
1914924Swnj 		*mhip = *ip;
1924952Swnj 		if (hlen > sizeof (struct ip)) {
1934924Swnj 			int olen = ip_optcopy(ip, mhip, off);
1944924Swnj 			mh->m_len = sizeof (struct ip) + olen;
1954924Swnj 		} else
1964924Swnj 			mh->m_len = sizeof (struct ip);
19727738Skarels 		mhip->ip_off = (off >> 3) + (ip->ip_off & ~IP_MF);
19816545Skarels 		if (ip->ip_off & IP_MF)
19916545Skarels 			mhip->ip_off |= IP_MF;
2005892Sroot 		if (off + len >= ip->ip_len-hlen)
2015892Sroot 			len = mhip->ip_len = ip->ip_len - hlen - off;
2024924Swnj 		else {
2034924Swnj 			mhip->ip_len = len;
2044924Swnj 			mhip->ip_off |= IP_MF;
2054496Swnj 		}
2065770Swnj 		mhip->ip_len += sizeof (struct ip);
2075770Swnj 		mhip->ip_len = htons((u_short)mhip->ip_len);
2084924Swnj 		mh->m_next = m_copy(m, off, len);
2094924Swnj 		if (mh->m_next == 0) {
2104967Swnj 			(void) m_free(mh);
2116505Ssam 			error = ENOBUFS;	/* ??? */
2124924Swnj 			goto bad;
2134674Swnj 		}
2145892Sroot 		mhip->ip_off = htons((u_short)mhip->ip_off);
2155892Sroot 		mhip->ip_sum = 0;
2165892Sroot 		mhip->ip_sum = in_cksum(mh, hlen);
21716602Ssam 		if (error = (*ifp->if_output)(ifp, mh, (struct sockaddr *)dst))
2186505Ssam 			break;
2194924Swnj 	}
2204924Swnj bad:
2214924Swnj 	m_freem(m);
2227155Swnj done:
22312417Ssam 	if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt)
2247155Swnj 		RTFREE(ro->ro_rt);
2256505Ssam 	return (error);
2264924Swnj }
2274924Swnj 
2284924Swnj /*
22924814Skarels  * Insert IP options into preformed packet.
23024814Skarels  * Adjust IP destination as required for IP source routing,
23124814Skarels  * as indicated by a non-zero in_addr at the start of the options.
23224814Skarels  */
23324814Skarels struct mbuf *
23424814Skarels ip_insertoptions(m, opt, phlen)
23524814Skarels 	register struct mbuf *m;
23624814Skarels 	struct mbuf *opt;
23724814Skarels 	int *phlen;
23824814Skarels {
23924814Skarels 	register struct ipoption *p = mtod(opt, struct ipoption *);
24024814Skarels 	struct mbuf *n;
24124814Skarels 	register struct ip *ip = mtod(m, struct ip *);
24226385Skarels 	unsigned optlen;
24324814Skarels 
24424814Skarels 	optlen = opt->m_len - sizeof(p->ipopt_dst);
24524814Skarels 	if (p->ipopt_dst.s_addr)
24624814Skarels 		ip->ip_dst = p->ipopt_dst;
24724814Skarels 	if (m->m_off >= MMAXOFF || MMINOFF + optlen > m->m_off) {
24824814Skarels 		MGET(n, M_DONTWAIT, MT_HEADER);
24924814Skarels 		if (n == 0)
25024814Skarels 			return (m);
25124814Skarels 		m->m_len -= sizeof(struct ip);
25224814Skarels 		m->m_off += sizeof(struct ip);
25324814Skarels 		n->m_next = m;
25424814Skarels 		m = n;
25524814Skarels 		m->m_off = MMAXOFF - sizeof(struct ip) - optlen;
25624814Skarels 		m->m_len = optlen + sizeof(struct ip);
25724814Skarels 		bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
25824814Skarels 	} else {
25924814Skarels 		m->m_off -= optlen;
26024814Skarels 		m->m_len += optlen;
26124814Skarels 		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
26224814Skarels 	}
26324814Skarels 	ip = mtod(m, struct ip *);
26426385Skarels 	bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
26524814Skarels 	*phlen = sizeof(struct ip) + optlen;
26624814Skarels 	ip->ip_len += optlen;
26724814Skarels 	return (m);
26824814Skarels }
26924814Skarels 
27024814Skarels /*
2714924Swnj  * Copy options from ip to jp.
2724952Swnj  * If off is 0 all options are copied
2734924Swnj  * otherwise copy selectively.
2744924Swnj  */
2754924Swnj ip_optcopy(ip, jp, off)
2764924Swnj 	struct ip *ip, *jp;
2774924Swnj 	int off;
2784924Swnj {
2794924Swnj 	register u_char *cp, *dp;
2804924Swnj 	int opt, optlen, cnt;
2814924Swnj 
2824924Swnj 	cp = (u_char *)(ip + 1);
2834924Swnj 	dp = (u_char *)(jp + 1);
2844924Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
2854924Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2864924Swnj 		opt = cp[0];
2874924Swnj 		if (opt == IPOPT_EOL)
2884924Swnj 			break;
2894924Swnj 		if (opt == IPOPT_NOP)
2904924Swnj 			optlen = 1;
2914924Swnj 		else
29224814Skarels 			optlen = cp[IPOPT_OLEN];
2934924Swnj 		if (optlen > cnt)			/* XXX */
2944924Swnj 			optlen = cnt;			/* XXX */
2954924Swnj 		if (off == 0 || IPOPT_COPIED(opt)) {
2964952Swnj 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
2974924Swnj 			dp += optlen;
2984674Swnj 		}
2994545Swnj 	}
3004924Swnj 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
3014924Swnj 		*dp++ = IPOPT_EOL;
3024924Swnj 	return (optlen);
3034496Swnj }
30424814Skarels 
30524814Skarels /*
30624814Skarels  * IP socket option processing.
30724814Skarels  */
30824814Skarels ip_ctloutput(op, so, level, optname, m)
30924814Skarels 	int op;
31024814Skarels 	struct socket *so;
31124814Skarels 	int level, optname;
31224814Skarels 	struct mbuf **m;
31324814Skarels {
31424814Skarels 	int error = 0;
31524814Skarels 	struct inpcb *inp = sotoinpcb(so);
31624814Skarels 
31724814Skarels 	if (level != IPPROTO_IP)
31824814Skarels 		error = EINVAL;
31924814Skarels 	else switch (op) {
32024814Skarels 
32124814Skarels 	case PRCO_SETOPT:
32224814Skarels 		switch (optname) {
32324814Skarels 		case IP_OPTIONS:
32426036Skarels 			return (ip_pcbopts(&inp->inp_options, *m));
32524814Skarels 
32624814Skarels 		default:
32724814Skarels 			error = EINVAL;
32824814Skarels 			break;
32924814Skarels 		}
33024814Skarels 		break;
33124814Skarels 
33224814Skarels 	case PRCO_GETOPT:
33324814Skarels 		switch (optname) {
33424814Skarels 		case IP_OPTIONS:
33524814Skarels 			*m = m_get(M_WAIT, MT_SOOPTS);
33624814Skarels 			if (inp->inp_options) {
33724814Skarels 				(*m)->m_off = inp->inp_options->m_off;
33824814Skarels 				(*m)->m_len = inp->inp_options->m_len;
33924814Skarels 				bcopy(mtod(inp->inp_options, caddr_t),
34026385Skarels 				    mtod(*m, caddr_t), (unsigned)(*m)->m_len);
34124814Skarels 			} else
34224814Skarels 				(*m)->m_len = 0;
34324814Skarels 			break;
34424814Skarels 		default:
34524814Skarels 			error = EINVAL;
34624814Skarels 			break;
34724814Skarels 		}
34824814Skarels 		break;
34924814Skarels 	}
35024814Skarels 	if (op == PRCO_SETOPT)
35126385Skarels 		(void)m_free(*m);
35224814Skarels 	return (error);
35324814Skarels }
35424814Skarels 
35524814Skarels /*
35626036Skarels  * Set up IP options in pcb for insertion in output packets.
35726036Skarels  * Store in mbuf with pointer in pcbopt, adding pseudo-option
35826036Skarels  * with destination address if source routed.
35924814Skarels  */
36026036Skarels ip_pcbopts(pcbopt, m)
36126036Skarels 	struct mbuf **pcbopt;
36226036Skarels 	register struct mbuf *m;
36324814Skarels {
36424814Skarels 	register cnt, optlen;
36524814Skarels 	register u_char *cp;
36624814Skarels 	u_char opt;
36724814Skarels 
36824814Skarels 	/* turn off any old options */
36926036Skarels 	if (*pcbopt)
37026385Skarels 		(void)m_free(*pcbopt);
37126036Skarels 	*pcbopt = 0;
37224814Skarels 	if (m == (struct mbuf *)0 || m->m_len == 0) {
37324814Skarels 		/*
37424814Skarels 		 * Only turning off any previous options.
37524814Skarels 		 */
37624814Skarels 		if (m)
37726385Skarels 			(void)m_free(m);
37824814Skarels 		return (0);
37924814Skarels 	}
38024814Skarels 
38124814Skarels #ifndef	vax
38224814Skarels 	if (m->m_len % sizeof(long))
38324814Skarels 		goto bad;
38424814Skarels #endif
38524814Skarels 	/*
38624814Skarels 	 * IP first-hop destination address will be stored before
38724814Skarels 	 * actual options; move other options back
38824814Skarels 	 * and clear it when none present.
38924814Skarels 	 */
39024814Skarels #if	MAX_IPOPTLEN >= MMAXOFF - MMINOFF
39124814Skarels 	if (m->m_off + m->m_len + sizeof(struct in_addr) > MAX_IPOPTLEN)
39224814Skarels 		goto bad;
39324814Skarels #else
39424814Skarels 	if (m->m_off + m->m_len + sizeof(struct in_addr) > MMAXOFF)
39524814Skarels 		goto bad;
39624814Skarels #endif
39724814Skarels 	cnt = m->m_len;
39824814Skarels 	m->m_len += sizeof(struct in_addr);
39924814Skarels 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
40026385Skarels 	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
40124814Skarels 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
40224814Skarels 
40324814Skarels 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
40424814Skarels 		opt = cp[IPOPT_OPTVAL];
40524814Skarels 		if (opt == IPOPT_EOL)
40624814Skarels 			break;
40724814Skarels 		if (opt == IPOPT_NOP)
40824814Skarels 			optlen = 1;
40924814Skarels 		else {
41024814Skarels 			optlen = cp[IPOPT_OLEN];
41124814Skarels 			if (optlen <= IPOPT_OLEN || optlen > cnt)
41224814Skarels 				goto bad;
41324814Skarels 		}
41424814Skarels 		switch (opt) {
41524814Skarels 
41624814Skarels 		default:
41724814Skarels 			break;
41824814Skarels 
41924814Skarels 		case IPOPT_LSRR:
42024814Skarels 		case IPOPT_SSRR:
42124814Skarels 			/*
42224814Skarels 			 * user process specifies route as:
42324814Skarels 			 *	->A->B->C->D
42424814Skarels 			 * D must be our final destination (but we can't
42524814Skarels 			 * check that since we may not have connected yet).
42624814Skarels 			 * A is first hop destination, which doesn't appear in
42724814Skarels 			 * actual IP option, but is stored before the options.
42824814Skarels 			 */
42924814Skarels 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
43024814Skarels 				goto bad;
43124814Skarels 			m->m_len -= sizeof(struct in_addr);
43224814Skarels 			cnt -= sizeof(struct in_addr);
43324814Skarels 			optlen -= sizeof(struct in_addr);
43424814Skarels 			cp[IPOPT_OLEN] = optlen;
43524814Skarels 			/*
43624814Skarels 			 * Move first hop before start of options.
43724814Skarels 			 */
43826385Skarels 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
43924814Skarels 			    sizeof(struct in_addr));
44024814Skarels 			/*
44124814Skarels 			 * Then copy rest of options back
44224814Skarels 			 * to close up the deleted entry.
44324814Skarels 			 */
44426385Skarels 			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
44526385Skarels 			    sizeof(struct in_addr)),
44626385Skarels 			    (caddr_t)&cp[IPOPT_OFFSET+1],
44726385Skarels 			    (unsigned)cnt + sizeof(struct in_addr));
44824814Skarels 			break;
44924814Skarels 		}
45024814Skarels 	}
45126036Skarels 	*pcbopt = m;
45224814Skarels 	return (0);
45324814Skarels 
45424814Skarels bad:
45526385Skarels 	(void)m_free(m);
45624814Skarels 	return (EINVAL);
45724814Skarels }
458