xref: /csrg-svn/sys/netinet/ip_output.c (revision 27196)
123745Skarels /*
223745Skarels  * Copyright (c) 1982 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*27196Skarels  *	@(#)ip_output.c	6.14 (Berkeley) 04/19/86
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
2712460Ssam #include "../vax/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
3424814Skarels  * header (as ipovly).  The mbuf chain containing the packet will
3524814Skarels  * be freed.  The mbuf opt, if present, will not be freed.
3624814Skarels  */
3712417Ssam ip_output(m, opt, ro, flags)
384924Swnj 	struct mbuf *m;
395085Swnj 	struct mbuf *opt;
406339Ssam 	struct route *ro;
4112417Ssam 	int flags;
424496Swnj {
4324814Skarels 	register struct ip *ip;
445085Swnj 	register struct ifnet *ifp;
456505Ssam 	int len, hlen = sizeof (struct ip), off, error = 0;
466339Ssam 	struct route iproute;
4716602Ssam 	struct sockaddr_in *dst;
484496Swnj 
4924814Skarels 	if (opt)
5024814Skarels 		m = ip_insertoptions(m, opt, &hlen);
5124814Skarels 	ip = mtod(m, struct ip *);
524924Swnj 	/*
534924Swnj 	 * Fill in IP header.
544924Swnj 	 */
5512417Ssam 	if ((flags & IP_FORWARDING) == 0) {
5612417Ssam 		ip->ip_v = IPVERSION;
5712417Ssam 		ip->ip_off &= IP_DF;
5812417Ssam 		ip->ip_id = htons(ip_id++);
5916545Skarels 		ip->ip_hl = hlen >> 2;
6024814Skarels 	} else
6124814Skarels 		hlen = ip->ip_hl << 2;
624496Swnj 
634545Swnj 	/*
647155Swnj 	 * Route packet.
655085Swnj 	 */
666339Ssam 	if (ro == 0) {
676339Ssam 		ro = &iproute;
686339Ssam 		bzero((caddr_t)ro, sizeof (*ro));
695085Swnj 	}
7016602Ssam 	dst = (struct sockaddr_in *)&ro->ro_dst;
7126156Skarels 	/*
7226156Skarels 	 * If there is a cached route,
7326156Skarels 	 * check that it is to the same destination
7426156Skarels 	 * and is still up.  If not, free it and try again.
7526156Skarels 	 */
7626156Skarels 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
7726156Skarels 	   dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
7826156Skarels 		RTFREE(ro->ro_rt);
7926156Skarels 		ro->ro_rt = (struct rtentry *)0;
8026156Skarels 	}
816339Ssam 	if (ro->ro_rt == 0) {
8216602Ssam 		dst->sin_family = AF_INET;
8316602Ssam 		dst->sin_addr = ip->ip_dst;
8426058Skarels 	}
8526058Skarels 	/*
8626058Skarels 	 * If routing to interface only,
8726058Skarels 	 * short circuit routing lookup.
8826058Skarels 	 */
8926058Skarels 	if (flags & IP_ROUTETOIF) {
9026058Skarels 		struct in_ifaddr *ia;
91*27196Skarels 
92*27196Skarels 		ia = (struct in_ifadddr *)ifa_ifwithdstaddr(dst);
93*27196Skarels 		if (ia == 0)
94*27196Skarels 			ia = in_iaonnetof(in_netof(ip->ip_dst));
9526058Skarels 		if (ia == 0) {
9626058Skarels 			error = ENETUNREACH;
9726058Skarels 			goto bad;
9826058Skarels 		}
9926058Skarels 		ifp = ia->ia_ifp;
10026058Skarels 	} else {
10126058Skarels 		if (ro->ro_rt == 0)
10226058Skarels 			rtalloc(ro);
10326058Skarels 		if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) {
10426058Skarels 			error = ENETUNREACH;
10526058Skarels 			goto bad;
10626058Skarels 		}
10726058Skarels 		ro->ro_rt->rt_use++;
10826058Skarels 		if (ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST))
10926058Skarels 			dst = (struct sockaddr_in *)&ro->ro_rt->rt_gateway;
1106339Ssam 	}
11123745Skarels #ifndef notdef
1127155Swnj 	/*
11323745Skarels 	 * If source address not specified yet, use address
11423745Skarels 	 * of outgoing interface.
11523745Skarels 	 */
11623745Skarels 	if (ip->ip_src.s_addr == INADDR_ANY) {
11723745Skarels 		register struct in_ifaddr *ia;
11823745Skarels 
11923745Skarels 		for (ia = in_ifaddr; ia; ia = ia->ia_next)
12023745Skarels 			if (ia->ia_ifp == ifp) {
12123745Skarels 				ip->ip_src = IA_SIN(ia)->sin_addr;
12223745Skarels 				break;
12323745Skarels 			}
12423745Skarels 	}
12523745Skarels #endif
12623745Skarels 	/*
12710402Ssam 	 * Look for broadcast address and
12810402Ssam 	 * and verify user is allowed to send
12910146Ssam 	 * such a packet.
1307155Swnj 	 */
13118375Skarels 	if (in_broadcast(dst->sin_addr)) {
13210146Ssam 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
13310146Ssam 			error = EADDRNOTAVAIL;
13410146Ssam 			goto bad;
13510146Ssam 		}
13612417Ssam 		if ((flags & IP_ALLOWBROADCAST) == 0) {
1377155Swnj 			error = EACCES;
1386339Ssam 			goto bad;
1396505Ssam 		}
14010146Ssam 		/* don't allow broadcast messages to be fragmented */
14110146Ssam 		if (ip->ip_len > ifp->if_mtu) {
14210146Ssam 			error = EMSGSIZE;
14310146Ssam 			goto bad;
14410146Ssam 		}
1456339Ssam 	}
1466339Ssam 
1475085Swnj 	/*
1484924Swnj 	 * If small enough for interface, can just send directly.
1494545Swnj 	 */
1505085Swnj 	if (ip->ip_len <= ifp->if_mtu) {
1515085Swnj 		ip->ip_len = htons((u_short)ip->ip_len);
1525085Swnj 		ip->ip_off = htons((u_short)ip->ip_off);
1535085Swnj 		ip->ip_sum = 0;
1545085Swnj 		ip->ip_sum = in_cksum(m, hlen);
15516602Ssam 		error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst);
1567155Swnj 		goto done;
1574908Swnj 	}
1584924Swnj 
1594924Swnj 	/*
1604924Swnj 	 * Too large for interface; fragment if possible.
1614924Swnj 	 * Must be able to put at least 8 bytes per fragment.
1624924Swnj 	 */
1636505Ssam 	if (ip->ip_off & IP_DF) {
1646505Ssam 		error = EMSGSIZE;
1654924Swnj 		goto bad;
1666505Ssam 	}
1675085Swnj 	len = (ifp->if_mtu - hlen) &~ 7;
1686505Ssam 	if (len < 8) {
1696505Ssam 		error = EMSGSIZE;
1704924Swnj 		goto bad;
1716505Ssam 	}
1724924Swnj 
1734924Swnj 	/*
1744924Swnj 	 * Discard IP header from logical mbuf for m_copy's sake.
1754924Swnj 	 * Loop through length of segment, make a copy of each
1764924Swnj 	 * part and output.
1774924Swnj 	 */
1784924Swnj 	m->m_len -= sizeof (struct ip);
1794924Swnj 	m->m_off += sizeof (struct ip);
1805892Sroot 	for (off = 0; off < ip->ip_len-hlen; off += len) {
18110012Ssam 		struct mbuf *mh = m_get(M_DONTWAIT, MT_HEADER);
1824924Swnj 		struct ip *mhip;
1834924Swnj 
1846505Ssam 		if (mh == 0) {
1856505Ssam 			error = ENOBUFS;
1864924Swnj 			goto bad;
1876505Ssam 		}
1884924Swnj 		mh->m_off = MMAXOFF - hlen;
1894924Swnj 		mhip = mtod(mh, struct ip *);
1904924Swnj 		*mhip = *ip;
1914952Swnj 		if (hlen > sizeof (struct ip)) {
1924924Swnj 			int olen = ip_optcopy(ip, mhip, off);
1934924Swnj 			mh->m_len = sizeof (struct ip) + olen;
1944924Swnj 		} else
1954924Swnj 			mh->m_len = sizeof (struct ip);
1965770Swnj 		mhip->ip_off = off >> 3;
19716545Skarels 		if (ip->ip_off & IP_MF)
19816545Skarels 			mhip->ip_off |= IP_MF;
1995892Sroot 		if (off + len >= ip->ip_len-hlen)
2005892Sroot 			len = mhip->ip_len = ip->ip_len - hlen - off;
2014924Swnj 		else {
2024924Swnj 			mhip->ip_len = len;
2034924Swnj 			mhip->ip_off |= IP_MF;
2044496Swnj 		}
2055770Swnj 		mhip->ip_len += sizeof (struct ip);
2065770Swnj 		mhip->ip_len = htons((u_short)mhip->ip_len);
2074924Swnj 		mh->m_next = m_copy(m, off, len);
2084924Swnj 		if (mh->m_next == 0) {
2094967Swnj 			(void) m_free(mh);
2106505Ssam 			error = ENOBUFS;	/* ??? */
2114924Swnj 			goto bad;
2124674Swnj 		}
2135892Sroot 		mhip->ip_off = htons((u_short)mhip->ip_off);
2145892Sroot 		mhip->ip_sum = 0;
2155892Sroot 		mhip->ip_sum = in_cksum(mh, hlen);
21616602Ssam 		if (error = (*ifp->if_output)(ifp, mh, (struct sockaddr *)dst))
2176505Ssam 			break;
2184924Swnj 	}
2194924Swnj bad:
2204924Swnj 	m_freem(m);
2217155Swnj done:
22212417Ssam 	if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt)
2237155Swnj 		RTFREE(ro->ro_rt);
2246505Ssam 	return (error);
2254924Swnj }
2264924Swnj 
2274924Swnj /*
22824814Skarels  * Insert IP options into preformed packet.
22924814Skarels  * Adjust IP destination as required for IP source routing,
23024814Skarels  * as indicated by a non-zero in_addr at the start of the options.
23124814Skarels  */
23224814Skarels struct mbuf *
23324814Skarels ip_insertoptions(m, opt, phlen)
23424814Skarels 	register struct mbuf *m;
23524814Skarels 	struct mbuf *opt;
23624814Skarels 	int *phlen;
23724814Skarels {
23824814Skarels 	register struct ipoption *p = mtod(opt, struct ipoption *);
23924814Skarels 	struct mbuf *n;
24024814Skarels 	register struct ip *ip = mtod(m, struct ip *);
24126385Skarels 	unsigned optlen;
24224814Skarels 
24324814Skarels 	optlen = opt->m_len - sizeof(p->ipopt_dst);
24424814Skarels 	if (p->ipopt_dst.s_addr)
24524814Skarels 		ip->ip_dst = p->ipopt_dst;
24624814Skarels 	if (m->m_off >= MMAXOFF || MMINOFF + optlen > m->m_off) {
24724814Skarels 		MGET(n, M_DONTWAIT, MT_HEADER);
24824814Skarels 		if (n == 0)
24924814Skarels 			return (m);
25024814Skarels 		m->m_len -= sizeof(struct ip);
25124814Skarels 		m->m_off += sizeof(struct ip);
25224814Skarels 		n->m_next = m;
25324814Skarels 		m = n;
25424814Skarels 		m->m_off = MMAXOFF - sizeof(struct ip) - optlen;
25524814Skarels 		m->m_len = optlen + sizeof(struct ip);
25624814Skarels 		bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
25724814Skarels 	} else {
25824814Skarels 		m->m_off -= optlen;
25924814Skarels 		m->m_len += optlen;
26024814Skarels 		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
26124814Skarels 	}
26224814Skarels 	ip = mtod(m, struct ip *);
26326385Skarels 	bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
26424814Skarels 	*phlen = sizeof(struct ip) + optlen;
26524814Skarels 	ip->ip_len += optlen;
26624814Skarels 	return (m);
26724814Skarels }
26824814Skarels 
26924814Skarels /*
2704924Swnj  * Copy options from ip to jp.
2714952Swnj  * If off is 0 all options are copied
2724924Swnj  * otherwise copy selectively.
2734924Swnj  */
2744924Swnj ip_optcopy(ip, jp, off)
2754924Swnj 	struct ip *ip, *jp;
2764924Swnj 	int off;
2774924Swnj {
2784924Swnj 	register u_char *cp, *dp;
2794924Swnj 	int opt, optlen, cnt;
2804924Swnj 
2814924Swnj 	cp = (u_char *)(ip + 1);
2824924Swnj 	dp = (u_char *)(jp + 1);
2834924Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
2844924Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2854924Swnj 		opt = cp[0];
2864924Swnj 		if (opt == IPOPT_EOL)
2874924Swnj 			break;
2884924Swnj 		if (opt == IPOPT_NOP)
2894924Swnj 			optlen = 1;
2904924Swnj 		else
29124814Skarels 			optlen = cp[IPOPT_OLEN];
2924924Swnj 		if (optlen > cnt)			/* XXX */
2934924Swnj 			optlen = cnt;			/* XXX */
2944924Swnj 		if (off == 0 || IPOPT_COPIED(opt)) {
2954952Swnj 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
2964924Swnj 			dp += optlen;
2974674Swnj 		}
2984545Swnj 	}
2994924Swnj 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
3004924Swnj 		*dp++ = IPOPT_EOL;
3014924Swnj 	return (optlen);
3024496Swnj }
30324814Skarels 
30424814Skarels /*
30524814Skarels  * IP socket option processing.
30624814Skarels  */
30724814Skarels ip_ctloutput(op, so, level, optname, m)
30824814Skarels 	int op;
30924814Skarels 	struct socket *so;
31024814Skarels 	int level, optname;
31124814Skarels 	struct mbuf **m;
31224814Skarels {
31324814Skarels 	int error = 0;
31424814Skarels 	struct inpcb *inp = sotoinpcb(so);
31524814Skarels 
31624814Skarels 	if (level != IPPROTO_IP)
31724814Skarels 		error = EINVAL;
31824814Skarels 	else switch (op) {
31924814Skarels 
32024814Skarels 	case PRCO_SETOPT:
32124814Skarels 		switch (optname) {
32224814Skarels 		case IP_OPTIONS:
32326036Skarels 			return (ip_pcbopts(&inp->inp_options, *m));
32424814Skarels 
32524814Skarels 		default:
32624814Skarels 			error = EINVAL;
32724814Skarels 			break;
32824814Skarels 		}
32924814Skarels 		break;
33024814Skarels 
33124814Skarels 	case PRCO_GETOPT:
33224814Skarels 		switch (optname) {
33324814Skarels 		case IP_OPTIONS:
33424814Skarels 			*m = m_get(M_WAIT, MT_SOOPTS);
33524814Skarels 			if (inp->inp_options) {
33624814Skarels 				(*m)->m_off = inp->inp_options->m_off;
33724814Skarels 				(*m)->m_len = inp->inp_options->m_len;
33824814Skarels 				bcopy(mtod(inp->inp_options, caddr_t),
33926385Skarels 				    mtod(*m, caddr_t), (unsigned)(*m)->m_len);
34024814Skarels 			} else
34124814Skarels 				(*m)->m_len = 0;
34224814Skarels 			break;
34324814Skarels 		default:
34424814Skarels 			error = EINVAL;
34524814Skarels 			break;
34624814Skarels 		}
34724814Skarels 		break;
34824814Skarels 	}
34924814Skarels 	if (op == PRCO_SETOPT)
35026385Skarels 		(void)m_free(*m);
35124814Skarels 	return (error);
35224814Skarels }
35324814Skarels 
35424814Skarels /*
35526036Skarels  * Set up IP options in pcb for insertion in output packets.
35626036Skarels  * Store in mbuf with pointer in pcbopt, adding pseudo-option
35726036Skarels  * with destination address if source routed.
35824814Skarels  */
35926036Skarels ip_pcbopts(pcbopt, m)
36026036Skarels 	struct mbuf **pcbopt;
36126036Skarels 	register struct mbuf *m;
36224814Skarels {
36324814Skarels 	register cnt, optlen;
36424814Skarels 	register u_char *cp;
36524814Skarels 	u_char opt;
36624814Skarels 
36724814Skarels 	/* turn off any old options */
36826036Skarels 	if (*pcbopt)
36926385Skarels 		(void)m_free(*pcbopt);
37026036Skarels 	*pcbopt = 0;
37124814Skarels 	if (m == (struct mbuf *)0 || m->m_len == 0) {
37224814Skarels 		/*
37324814Skarels 		 * Only turning off any previous options.
37424814Skarels 		 */
37524814Skarels 		if (m)
37626385Skarels 			(void)m_free(m);
37724814Skarels 		return (0);
37824814Skarels 	}
37924814Skarels 
38024814Skarels #ifndef	vax
38124814Skarels 	if (m->m_len % sizeof(long))
38224814Skarels 		goto bad;
38324814Skarels #endif
38424814Skarels 	/*
38524814Skarels 	 * IP first-hop destination address will be stored before
38624814Skarels 	 * actual options; move other options back
38724814Skarels 	 * and clear it when none present.
38824814Skarels 	 */
38924814Skarels #if	MAX_IPOPTLEN >= MMAXOFF - MMINOFF
39024814Skarels 	if (m->m_off + m->m_len + sizeof(struct in_addr) > MAX_IPOPTLEN)
39124814Skarels 		goto bad;
39224814Skarels #else
39324814Skarels 	if (m->m_off + m->m_len + sizeof(struct in_addr) > MMAXOFF)
39424814Skarels 		goto bad;
39524814Skarels #endif
39624814Skarels 	cnt = m->m_len;
39724814Skarels 	m->m_len += sizeof(struct in_addr);
39824814Skarels 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
39926385Skarels 	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
40024814Skarels 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
40124814Skarels 
40224814Skarels 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
40324814Skarels 		opt = cp[IPOPT_OPTVAL];
40424814Skarels 		if (opt == IPOPT_EOL)
40524814Skarels 			break;
40624814Skarels 		if (opt == IPOPT_NOP)
40724814Skarels 			optlen = 1;
40824814Skarels 		else {
40924814Skarels 			optlen = cp[IPOPT_OLEN];
41024814Skarels 			if (optlen <= IPOPT_OLEN || optlen > cnt)
41124814Skarels 				goto bad;
41224814Skarels 		}
41324814Skarels 		switch (opt) {
41424814Skarels 
41524814Skarels 		default:
41624814Skarels 			break;
41724814Skarels 
41824814Skarels 		case IPOPT_LSRR:
41924814Skarels 		case IPOPT_SSRR:
42024814Skarels 			/*
42124814Skarels 			 * user process specifies route as:
42224814Skarels 			 *	->A->B->C->D
42324814Skarels 			 * D must be our final destination (but we can't
42424814Skarels 			 * check that since we may not have connected yet).
42524814Skarels 			 * A is first hop destination, which doesn't appear in
42624814Skarels 			 * actual IP option, but is stored before the options.
42724814Skarels 			 */
42824814Skarels 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
42924814Skarels 				goto bad;
43024814Skarels 			m->m_len -= sizeof(struct in_addr);
43124814Skarels 			cnt -= sizeof(struct in_addr);
43224814Skarels 			optlen -= sizeof(struct in_addr);
43324814Skarels 			cp[IPOPT_OLEN] = optlen;
43424814Skarels 			/*
43524814Skarels 			 * Move first hop before start of options.
43624814Skarels 			 */
43726385Skarels 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
43824814Skarels 			    sizeof(struct in_addr));
43924814Skarels 			/*
44024814Skarels 			 * Then copy rest of options back
44124814Skarels 			 * to close up the deleted entry.
44224814Skarels 			 */
44326385Skarels 			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
44426385Skarels 			    sizeof(struct in_addr)),
44526385Skarels 			    (caddr_t)&cp[IPOPT_OFFSET+1],
44626385Skarels 			    (unsigned)cnt + sizeof(struct in_addr));
44724814Skarels 			break;
44824814Skarels 		}
44924814Skarels 	}
45026036Skarels 	*pcbopt = m;
45124814Skarels 	return (0);
45224814Skarels 
45324814Skarels bad:
45426385Skarels 	(void)m_free(m);
45524814Skarels 	return (EINVAL);
45624814Skarels }
457