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