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