Home | History | Annotate | Line # | Download | only in netinet
ip_output.c revision 1.3.4.1
      1 /*
      2  * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	from: @(#)ip_output.c	7.23 (Berkeley) 11/12/90
     34  *	$Id: ip_output.c,v 1.3.4.1 1993/10/16 10:51:46 mycroft Exp $
     35  */
     36 
     37 #include "param.h"
     38 #include "malloc.h"
     39 #include "mbuf.h"
     40 #include "errno.h"
     41 #include "protosw.h"
     42 #include "socket.h"
     43 #include "socketvar.h"
     44 
     45 #include "../net/if.h"
     46 #include "../net/route.h"
     47 
     48 #include "in.h"
     49 #include "in_systm.h"
     50 #include "ip.h"
     51 #include "in_pcb.h"
     52 #include "in_var.h"
     53 #include "ip_var.h"
     54 
     55 struct mbuf *ip_insertoptions();
     56 
     57 /*
     58  * IP output.  The packet in mbuf chain m contains a skeletal IP
     59  * header (with len, off, ttl, proto, tos, src, dst).
     60  * The mbuf chain containing the packet will be freed.
     61  * The mbuf opt, if present, will not be freed.
     62  */
     63 ip_output(m0, opt, ro, flags)
     64 	struct mbuf *m0;
     65 	struct mbuf *opt;
     66 	struct route *ro;
     67 	int flags;
     68 {
     69 	register struct ip *ip, *mhip;
     70 	register struct ifnet *ifp;
     71 	register struct mbuf *m = m0;
     72 	register int hlen = sizeof (struct ip);
     73 	int len, off, error = 0;
     74 	struct route iproute;
     75 	struct sockaddr_in *dst;
     76 	struct in_ifaddr *ia;
     77 
     78 #ifdef	DIAGNOSTIC
     79 	if ((m->m_flags & M_PKTHDR) == 0)
     80 		panic("ip_output no HDR");
     81 #endif
     82 	if (opt) {
     83 		m = ip_insertoptions(m, opt, &len);
     84 		hlen = len;
     85 	}
     86 	ip = mtod(m, struct ip *);
     87 	/*
     88 	 * Fill in IP header.
     89 	 */
     90 	if ((flags & IP_FORWARDING) == 0) {
     91 		ip->ip_v = IPVERSION;
     92 		ip->ip_off &= IP_DF;
     93 		ip->ip_id = htons(ip_id++);
     94 		ip->ip_hl = hlen >> 2;
     95 	} else {
     96 		hlen = ip->ip_hl << 2;
     97 		ipstat.ips_localout++;
     98 	}
     99 	/*
    100 	 * Route packet.
    101 	 */
    102 	if (ro == 0) {
    103 		ro = &iproute;
    104 		bzero((caddr_t)ro, sizeof (*ro));
    105 	}
    106 	dst = (struct sockaddr_in *)&ro->ro_dst;
    107 	/*
    108 	 * If there is a cached route,
    109 	 * check that it is to the same destination
    110 	 * and is still up.  If not, free it and try again.
    111 	 */
    112 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
    113 	   dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
    114 		RTFREE(ro->ro_rt);
    115 		ro->ro_rt = (struct rtentry *)0;
    116 	}
    117 	if (ro->ro_rt == 0) {
    118 		dst->sin_family = AF_INET;
    119 		dst->sin_len = sizeof(*dst);
    120 		dst->sin_addr = ip->ip_dst;
    121 	}
    122 	/*
    123 	 * If routing to interface only,
    124 	 * short circuit routing lookup.
    125 	 */
    126 	if (flags & IP_ROUTETOIF) {
    127 
    128 		ia = (struct in_ifaddr *)ifa_ifwithdstaddr((struct sockaddr *)dst);
    129 		if (ia == 0)
    130 			ia = in_iaonnetof(in_netof(ip->ip_dst));
    131 		if (ia == 0) {
    132 			error = ENETUNREACH;
    133 			goto bad;
    134 		}
    135 		ifp = ia->ia_ifp;
    136 	} else {
    137 		if (ro->ro_rt == 0)
    138 			rtalloc(ro);
    139 		if (ro->ro_rt == 0) {
    140 			error = EHOSTUNREACH;
    141 			goto bad;
    142 		}
    143 		ia = (struct in_ifaddr *)ro->ro_rt->rt_ifa;
    144 		ifp = ro->ro_rt->rt_ifp;
    145 		ro->ro_rt->rt_use++;
    146 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
    147 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
    148 	}
    149 #ifndef notdef
    150 	/*
    151 	 * If source address not specified yet, use address
    152 	 * of outgoing interface.
    153 	 */
    154 	if (ip->ip_src.s_addr == INADDR_ANY)
    155 		ip->ip_src = IA_SIN(ia)->sin_addr;
    156 #endif
    157 	/*
    158 	 * Look for broadcast address and
    159 	 * and verify user is allowed to send
    160 	 * such a packet.
    161 	 */
    162 	if (in_broadcast(dst->sin_addr)) {
    163 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
    164 			error = EADDRNOTAVAIL;
    165 			goto bad;
    166 		}
    167 		if ((flags & IP_ALLOWBROADCAST) == 0) {
    168 			error = EACCES;
    169 			goto bad;
    170 		}
    171 		/* don't allow broadcast messages to be fragmented */
    172 		if ((u_short)ip->ip_len > ifp->if_mtu) {
    173 			error = EMSGSIZE;
    174 			goto bad;
    175 		}
    176 		m->m_flags |= M_BCAST;
    177 	}
    178 
    179 	/*
    180 	 * If small enough for interface, can just send directly.
    181 	 */
    182 	if ((u_short)ip->ip_len <= ifp->if_mtu) {
    183 		ip->ip_len = htons((u_short)ip->ip_len);
    184 		ip->ip_off = htons((u_short)ip->ip_off);
    185 		ip->ip_sum = 0;
    186 		ip->ip_sum = in_cksum(m, hlen);
    187 		error = (*ifp->if_output)(ifp, m,
    188 				(struct sockaddr *)dst, ro->ro_rt);
    189 		goto done;
    190 	}
    191 	ipstat.ips_fragmented++;
    192 	/*
    193 	 * Too large for interface; fragment if possible.
    194 	 * Must be able to put at least 8 bytes per fragment.
    195 	 */
    196 	if (ip->ip_off & IP_DF) {
    197 		error = EMSGSIZE;
    198 		goto bad;
    199 	}
    200 	len = (ifp->if_mtu - hlen) &~ 7;
    201 	if (len < 8) {
    202 		error = EMSGSIZE;
    203 		goto bad;
    204 	}
    205 
    206     {
    207 	int mhlen, firstlen = len;
    208 	struct mbuf **mnext = &m->m_nextpkt;
    209 
    210 	/*
    211 	 * Loop through length of segment after first fragment,
    212 	 * make new header and copy data of each part and link onto chain.
    213 	 */
    214 	m0 = m;
    215 	mhlen = sizeof (struct ip);
    216 	for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
    217 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    218 		if (m == 0) {
    219 			error = ENOBUFS;
    220 			goto sendorfree;
    221 		}
    222 		m->m_data += max_linkhdr;
    223 		mhip = mtod(m, struct ip *);
    224 		*mhip = *ip;
    225 		if (hlen > sizeof (struct ip)) {
    226 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
    227 			mhip->ip_hl = mhlen >> 2;
    228 		}
    229 		m->m_len = mhlen;
    230 		mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
    231 		if (ip->ip_off & IP_MF)
    232 			mhip->ip_off |= IP_MF;
    233 		if (off + len >= (u_short)ip->ip_len)
    234 			len = (u_short)ip->ip_len - off;
    235 		else
    236 			mhip->ip_off |= IP_MF;
    237 		mhip->ip_len = htons((u_short)(len + mhlen));
    238 		m->m_next = m_copy(m0, off, len);
    239 		if (m->m_next == 0) {
    240 			error = ENOBUFS;	/* ??? */
    241 			goto sendorfree;
    242 		}
    243 		m->m_pkthdr.len = mhlen + len;
    244 		m->m_pkthdr.rcvif = (struct ifnet *)0;
    245 		mhip->ip_off = htons((u_short)mhip->ip_off);
    246 		mhip->ip_sum = 0;
    247 		mhip->ip_sum = in_cksum(m, mhlen);
    248 		*mnext = m;
    249 		mnext = &m->m_nextpkt;
    250 		ipstat.ips_ofragments++;
    251 	}
    252 	/*
    253 	 * Update first fragment by trimming what's been copied out
    254 	 * and updating header, then send each fragment (in order).
    255 	 */
    256 	m = m0;
    257 	m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
    258 	m->m_pkthdr.len = hlen + firstlen;
    259 	ip->ip_len = htons((u_short)m->m_pkthdr.len);
    260 	ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
    261 	ip->ip_sum = 0;
    262 	ip->ip_sum = in_cksum(m, hlen);
    263 sendorfree:
    264 	for (m = m0; m; m = m0) {
    265 		m0 = m->m_nextpkt;
    266 		m->m_nextpkt = 0;
    267 		if (error == 0)
    268 			error = (*ifp->if_output)(ifp, m,
    269 			    (struct sockaddr *)dst, ro->ro_rt);
    270 		else
    271 			m_freem(m);
    272 	}
    273     }
    274 done:
    275 	if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt)
    276 		RTFREE(ro->ro_rt);
    277 	return (error);
    278 bad:
    279 	m_freem(m0);
    280 	goto done;
    281 }
    282 
    283 /*
    284  * Insert IP options into preformed packet.
    285  * Adjust IP destination as required for IP source routing,
    286  * as indicated by a non-zero in_addr at the start of the options.
    287  */
    288 struct mbuf *
    289 ip_insertoptions(m, opt, phlen)
    290 	register struct mbuf *m;
    291 	struct mbuf *opt;
    292 	int *phlen;
    293 {
    294 	register struct ipoption *p = mtod(opt, struct ipoption *);
    295 	struct mbuf *n;
    296 	register struct ip *ip = mtod(m, struct ip *);
    297 	unsigned optlen;
    298 
    299 	optlen = opt->m_len - sizeof(p->ipopt_dst);
    300 	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
    301 		return (m);		/* XXX should fail */
    302 	if (p->ipopt_dst.s_addr)
    303 		ip->ip_dst = p->ipopt_dst;
    304 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
    305 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
    306 		if (n == 0)
    307 			return (m);
    308 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
    309 		m->m_len -= sizeof(struct ip);
    310 		m->m_data += sizeof(struct ip);
    311 		n->m_next = m;
    312 		m = n;
    313 		m->m_len = optlen + sizeof(struct ip);
    314 		m->m_data += max_linkhdr;
    315 		bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
    316 	} else {
    317 		m->m_data -= optlen;
    318 		m->m_len += optlen;
    319 		m->m_pkthdr.len += optlen;
    320 		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
    321 	}
    322 	ip = mtod(m, struct ip *);
    323 	bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
    324 	*phlen = sizeof(struct ip) + optlen;
    325 	ip->ip_len += optlen;
    326 	return (m);
    327 }
    328 
    329 /*
    330  * Copy options from ip to jp,
    331  * omitting those not copied during fragmentation.
    332  */
    333 ip_optcopy(ip, jp)
    334 	struct ip *ip, *jp;
    335 {
    336 	register u_char *cp, *dp;
    337 	int opt, optlen, cnt;
    338 
    339 	cp = (u_char *)(ip + 1);
    340 	dp = (u_char *)(jp + 1);
    341 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
    342 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
    343 		opt = cp[0];
    344 		if (opt == IPOPT_EOL)
    345 			break;
    346 		if (opt == IPOPT_NOP)
    347 			optlen = 1;
    348 		else
    349 			optlen = cp[IPOPT_OLEN];
    350 		/* bogus lengths should have been caught by ip_dooptions */
    351 		if (optlen > cnt)
    352 			optlen = cnt;
    353 		if (IPOPT_COPIED(opt)) {
    354 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
    355 			dp += optlen;
    356 		}
    357 	}
    358 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
    359 		*dp++ = IPOPT_EOL;
    360 	return (optlen);
    361 }
    362 
    363 /*
    364  * IP socket option processing.
    365  */
    366 ip_ctloutput(op, so, level, optname, mp)
    367 	int op;
    368 	struct socket *so;
    369 	int level, optname;
    370 	struct mbuf **mp;
    371 {
    372 	register struct inpcb *inp = sotoinpcb(so);
    373 	register struct mbuf *m = *mp;
    374 	register int optval;
    375 	int error = 0;
    376 
    377 	if (level != IPPROTO_IP)
    378 		error = EINVAL;
    379 	else switch (op) {
    380 
    381 	case PRCO_SETOPT:
    382 		switch (optname) {
    383 		case IP_OPTIONS:
    384 #ifdef notyet
    385 		case IP_RETOPTS:
    386 			return (ip_pcbopts(optname, &inp->inp_options, m));
    387 #else
    388 			return (ip_pcbopts(&inp->inp_options, m));
    389 #endif
    390 
    391 		case IP_TOS:
    392 		case IP_TTL:
    393 		case IP_RECVOPTS:
    394 		case IP_RECVRETOPTS:
    395 		case IP_RECVDSTADDR:
    396 			if (m->m_len != sizeof(int))
    397 				error = EINVAL;
    398 			else {
    399 				optval = *mtod(m, int *);
    400 				switch (optname) {
    401 
    402 				case IP_TOS:
    403 					inp->inp_ip.ip_tos = optval;
    404 					break;
    405 
    406 				case IP_TTL:
    407 					inp->inp_ip.ip_ttl = optval;
    408 					break;
    409 #define	OPTSET(bit) \
    410 	if (optval) \
    411 		inp->inp_flags |= bit; \
    412 	else \
    413 		inp->inp_flags &= ~bit;
    414 
    415 				case IP_RECVOPTS:
    416 					OPTSET(INP_RECVOPTS);
    417 					break;
    418 
    419 				case IP_RECVRETOPTS:
    420 					OPTSET(INP_RECVRETOPTS);
    421 					break;
    422 
    423 				case IP_RECVDSTADDR:
    424 					OPTSET(INP_RECVDSTADDR);
    425 					break;
    426 				}
    427 			}
    428 			break;
    429 #undef OPTSET
    430 
    431 		default:
    432 			error = EINVAL;
    433 			break;
    434 		}
    435 		if (m)
    436 			(void)m_free(m);
    437 		break;
    438 
    439 	case PRCO_GETOPT:
    440 		switch (optname) {
    441 		case IP_OPTIONS:
    442 		case IP_RETOPTS:
    443 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
    444 			if (inp->inp_options) {
    445 				m->m_len = inp->inp_options->m_len;
    446 				bcopy(mtod(inp->inp_options, caddr_t),
    447 				    mtod(m, caddr_t), (unsigned)m->m_len);
    448 			} else
    449 				m->m_len = 0;
    450 			break;
    451 
    452 		case IP_TOS:
    453 		case IP_TTL:
    454 		case IP_RECVOPTS:
    455 		case IP_RECVRETOPTS:
    456 		case IP_RECVDSTADDR:
    457 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
    458 			m->m_len = sizeof(int);
    459 			switch (optname) {
    460 
    461 			case IP_TOS:
    462 				optval = inp->inp_ip.ip_tos;
    463 				break;
    464 
    465 			case IP_TTL:
    466 				optval = inp->inp_ip.ip_ttl;
    467 				break;
    468 
    469 #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
    470 
    471 			case IP_RECVOPTS:
    472 				optval = OPTBIT(INP_RECVOPTS);
    473 				break;
    474 
    475 			case IP_RECVRETOPTS:
    476 				optval = OPTBIT(INP_RECVRETOPTS);
    477 				break;
    478 
    479 			case IP_RECVDSTADDR:
    480 				optval = OPTBIT(INP_RECVDSTADDR);
    481 				break;
    482 			}
    483 			*mtod(m, int *) = optval;
    484 			break;
    485 
    486 		default:
    487 			error = EINVAL;
    488 			break;
    489 		}
    490 		break;
    491 	}
    492 	return (error);
    493 }
    494 
    495 /*
    496  * Set up IP options in pcb for insertion in output packets.
    497  * Store in mbuf with pointer in pcbopt, adding pseudo-option
    498  * with destination address if source routed.
    499  */
    500 #ifdef notyet
    501 ip_pcbopts(optname, pcbopt, m)
    502 	int optname;
    503 #else
    504 ip_pcbopts(pcbopt, m)
    505 #endif
    506 	struct mbuf **pcbopt;
    507 	register struct mbuf *m;
    508 {
    509 	register cnt, optlen;
    510 	register u_char *cp;
    511 	u_char opt;
    512 
    513 	/* turn off any old options */
    514 	if (*pcbopt)
    515 		(void)m_free(*pcbopt);
    516 	*pcbopt = 0;
    517 	if (m == (struct mbuf *)0 || m->m_len == 0) {
    518 		/*
    519 		 * Only turning off any previous options.
    520 		 */
    521 		if (m)
    522 			(void)m_free(m);
    523 		return (0);
    524 	}
    525 
    526 #ifndef	vax
    527 	if (m->m_len % sizeof(long))
    528 		goto bad;
    529 #endif
    530 	/*
    531 	 * IP first-hop destination address will be stored before
    532 	 * actual options; move other options back
    533 	 * and clear it when none present.
    534 	 */
    535 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
    536 		goto bad;
    537 	cnt = m->m_len;
    538 	m->m_len += sizeof(struct in_addr);
    539 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
    540 	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
    541 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
    542 
    543 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
    544 		opt = cp[IPOPT_OPTVAL];
    545 		if (opt == IPOPT_EOL)
    546 			break;
    547 		if (opt == IPOPT_NOP)
    548 			optlen = 1;
    549 		else {
    550 			optlen = cp[IPOPT_OLEN];
    551 			if (optlen <= IPOPT_OLEN || optlen > cnt)
    552 				goto bad;
    553 		}
    554 		switch (opt) {
    555 
    556 		default:
    557 			break;
    558 
    559 		case IPOPT_LSRR:
    560 		case IPOPT_SSRR:
    561 			/*
    562 			 * user process specifies route as:
    563 			 *	->A->B->C->D
    564 			 * D must be our final destination (but we can't
    565 			 * check that since we may not have connected yet).
    566 			 * A is first hop destination, which doesn't appear in
    567 			 * actual IP option, but is stored before the options.
    568 			 */
    569 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
    570 				goto bad;
    571 			m->m_len -= sizeof(struct in_addr);
    572 			cnt -= sizeof(struct in_addr);
    573 			optlen -= sizeof(struct in_addr);
    574 			cp[IPOPT_OLEN] = optlen;
    575 			/*
    576 			 * Move first hop before start of options.
    577 			 */
    578 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
    579 			    sizeof(struct in_addr));
    580 			/*
    581 			 * Then copy rest of options back
    582 			 * to close up the deleted entry.
    583 			 */
    584 			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
    585 			    sizeof(struct in_addr)),
    586 			    (caddr_t)&cp[IPOPT_OFFSET+1],
    587 			    (unsigned)cnt + sizeof(struct in_addr));
    588 			break;
    589 		}
    590 	}
    591 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
    592 		goto bad;
    593 	*pcbopt = m;
    594 	return (0);
    595 
    596 bad:
    597 	(void)m_free(m);
    598 	return (EINVAL);
    599 }
    600