Media table reorganization.
[unix-history] / sys / netinet / ip_output.c
CommitLineData
15637ed4
RG
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 *
38e82238 33 * from: @(#)ip_output.c 7.23 (Berkeley) 11/12/90
4c45483e 34 * $Id: ip_output.c,v 1.3 1993/10/19 09:14:15 davidg Exp $
15637ed4
RG
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
59struct 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 */
4c45483e 67int
15637ed4
RG
68ip_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
b8d8ba78
DG
162
163 /*
164 * Verify that we have any chance at all of being able to queue
165 * the packet or packet fragments
166 */
167 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
168 ifp->if_snd.ifq_maxlen) {
169 error = ENOBUFS;
170 goto bad;
171 }
172
15637ed4
RG
173 /*
174 * Look for broadcast address and
175 * and verify user is allowed to send
176 * such a packet.
177 */
178 if (in_broadcast(dst->sin_addr)) {
179 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
180 error = EADDRNOTAVAIL;
181 goto bad;
182 }
183 if ((flags & IP_ALLOWBROADCAST) == 0) {
184 error = EACCES;
185 goto bad;
186 }
187 /* don't allow broadcast messages to be fragmented */
188 if ((u_short)ip->ip_len > ifp->if_mtu) {
189 error = EMSGSIZE;
190 goto bad;
191 }
192 m->m_flags |= M_BCAST;
193 }
194
195 /*
196 * If small enough for interface, can just send directly.
197 */
198 if ((u_short)ip->ip_len <= ifp->if_mtu) {
199 ip->ip_len = htons((u_short)ip->ip_len);
200 ip->ip_off = htons((u_short)ip->ip_off);
201 ip->ip_sum = 0;
202 ip->ip_sum = in_cksum(m, hlen);
203 error = (*ifp->if_output)(ifp, m,
204 (struct sockaddr *)dst, ro->ro_rt);
205 goto done;
206 }
207 ipstat.ips_fragmented++;
208 /*
209 * Too large for interface; fragment if possible.
210 * Must be able to put at least 8 bytes per fragment.
211 */
212 if (ip->ip_off & IP_DF) {
213 error = EMSGSIZE;
214 goto bad;
215 }
216 len = (ifp->if_mtu - hlen) &~ 7;
217 if (len < 8) {
218 error = EMSGSIZE;
219 goto bad;
220 }
221
222 {
223 int mhlen, firstlen = len;
224 struct mbuf **mnext = &m->m_nextpkt;
225
226 /*
227 * Loop through length of segment after first fragment,
228 * make new header and copy data of each part and link onto chain.
229 */
230 m0 = m;
231 mhlen = sizeof (struct ip);
232 for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
233 MGETHDR(m, M_DONTWAIT, MT_HEADER);
234 if (m == 0) {
235 error = ENOBUFS;
236 goto sendorfree;
237 }
238 m->m_data += max_linkhdr;
239 mhip = mtod(m, struct ip *);
240 *mhip = *ip;
241 if (hlen > sizeof (struct ip)) {
242 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
243 mhip->ip_hl = mhlen >> 2;
244 }
245 m->m_len = mhlen;
246 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
247 if (ip->ip_off & IP_MF)
248 mhip->ip_off |= IP_MF;
249 if (off + len >= (u_short)ip->ip_len)
250 len = (u_short)ip->ip_len - off;
251 else
252 mhip->ip_off |= IP_MF;
253 mhip->ip_len = htons((u_short)(len + mhlen));
254 m->m_next = m_copy(m0, off, len);
255 if (m->m_next == 0) {
256 error = ENOBUFS; /* ??? */
257 goto sendorfree;
258 }
259 m->m_pkthdr.len = mhlen + len;
260 m->m_pkthdr.rcvif = (struct ifnet *)0;
261 mhip->ip_off = htons((u_short)mhip->ip_off);
262 mhip->ip_sum = 0;
263 mhip->ip_sum = in_cksum(m, mhlen);
264 *mnext = m;
265 mnext = &m->m_nextpkt;
266 ipstat.ips_ofragments++;
267 }
268 /*
269 * Update first fragment by trimming what's been copied out
270 * and updating header, then send each fragment (in order).
271 */
272 m = m0;
273 m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
274 m->m_pkthdr.len = hlen + firstlen;
275 ip->ip_len = htons((u_short)m->m_pkthdr.len);
276 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
277 ip->ip_sum = 0;
278 ip->ip_sum = in_cksum(m, hlen);
279sendorfree:
280 for (m = m0; m; m = m0) {
281 m0 = m->m_nextpkt;
282 m->m_nextpkt = 0;
283 if (error == 0)
284 error = (*ifp->if_output)(ifp, m,
285 (struct sockaddr *)dst, ro->ro_rt);
286 else
287 m_freem(m);
288 }
289 }
290done:
291 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt)
292 RTFREE(ro->ro_rt);
293 return (error);
294bad:
295 m_freem(m0);
296 goto done;
297}
298
299/*
300 * Insert IP options into preformed packet.
301 * Adjust IP destination as required for IP source routing,
302 * as indicated by a non-zero in_addr at the start of the options.
303 */
304struct mbuf *
305ip_insertoptions(m, opt, phlen)
306 register struct mbuf *m;
307 struct mbuf *opt;
308 int *phlen;
309{
310 register struct ipoption *p = mtod(opt, struct ipoption *);
311 struct mbuf *n;
312 register struct ip *ip = mtod(m, struct ip *);
313 unsigned optlen;
314
315 optlen = opt->m_len - sizeof(p->ipopt_dst);
316 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
317 return (m); /* XXX should fail */
318 if (p->ipopt_dst.s_addr)
319 ip->ip_dst = p->ipopt_dst;
320 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
321 MGETHDR(n, M_DONTWAIT, MT_HEADER);
322 if (n == 0)
323 return (m);
324 n->m_pkthdr.len = m->m_pkthdr.len + optlen;
325 m->m_len -= sizeof(struct ip);
326 m->m_data += sizeof(struct ip);
327 n->m_next = m;
328 m = n;
329 m->m_len = optlen + sizeof(struct ip);
330 m->m_data += max_linkhdr;
331 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
332 } else {
333 m->m_data -= optlen;
334 m->m_len += optlen;
335 m->m_pkthdr.len += optlen;
336 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
337 }
338 ip = mtod(m, struct ip *);
339 bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
340 *phlen = sizeof(struct ip) + optlen;
341 ip->ip_len += optlen;
342 return (m);
343}
344
345/*
346 * Copy options from ip to jp,
347 * omitting those not copied during fragmentation.
348 */
4c45483e 349int
15637ed4
RG
350ip_optcopy(ip, jp)
351 struct ip *ip, *jp;
352{
353 register u_char *cp, *dp;
354 int opt, optlen, cnt;
355
356 cp = (u_char *)(ip + 1);
357 dp = (u_char *)(jp + 1);
358 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
359 for (; cnt > 0; cnt -= optlen, cp += optlen) {
360 opt = cp[0];
361 if (opt == IPOPT_EOL)
362 break;
363 if (opt == IPOPT_NOP)
364 optlen = 1;
365 else
366 optlen = cp[IPOPT_OLEN];
367 /* bogus lengths should have been caught by ip_dooptions */
368 if (optlen > cnt)
369 optlen = cnt;
370 if (IPOPT_COPIED(opt)) {
371 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
372 dp += optlen;
373 }
374 }
375 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
376 *dp++ = IPOPT_EOL;
377 return (optlen);
378}
379
380/*
381 * IP socket option processing.
382 */
4c45483e 383int
15637ed4
RG
384ip_ctloutput(op, so, level, optname, mp)
385 int op;
386 struct socket *so;
387 int level, optname;
388 struct mbuf **mp;
389{
390 register struct inpcb *inp = sotoinpcb(so);
391 register struct mbuf *m = *mp;
4c45483e 392 register int optval = 0;
15637ed4
RG
393 int error = 0;
394
395 if (level != IPPROTO_IP)
396 error = EINVAL;
397 else switch (op) {
398
399 case PRCO_SETOPT:
400 switch (optname) {
401 case IP_OPTIONS:
402#ifdef notyet
403 case IP_RETOPTS:
404 return (ip_pcbopts(optname, &inp->inp_options, m));
405#else
406 return (ip_pcbopts(&inp->inp_options, m));
407#endif
408
409 case IP_TOS:
410 case IP_TTL:
411 case IP_RECVOPTS:
412 case IP_RECVRETOPTS:
413 case IP_RECVDSTADDR:
414 if (m->m_len != sizeof(int))
415 error = EINVAL;
416 else {
417 optval = *mtod(m, int *);
418 switch (optname) {
419
420 case IP_TOS:
421 inp->inp_ip.ip_tos = optval;
422 break;
423
424 case IP_TTL:
425 inp->inp_ip.ip_ttl = optval;
426 break;
427#define OPTSET(bit) \
428 if (optval) \
429 inp->inp_flags |= bit; \
430 else \
431 inp->inp_flags &= ~bit;
432
433 case IP_RECVOPTS:
434 OPTSET(INP_RECVOPTS);
435 break;
436
437 case IP_RECVRETOPTS:
438 OPTSET(INP_RECVRETOPTS);
439 break;
440
441 case IP_RECVDSTADDR:
442 OPTSET(INP_RECVDSTADDR);
443 break;
444 }
445 }
446 break;
447#undef OPTSET
448
449 default:
450 error = EINVAL;
451 break;
452 }
453 if (m)
454 (void)m_free(m);
455 break;
456
457 case PRCO_GETOPT:
458 switch (optname) {
459 case IP_OPTIONS:
460 case IP_RETOPTS:
461 *mp = m = m_get(M_WAIT, MT_SOOPTS);
462 if (inp->inp_options) {
463 m->m_len = inp->inp_options->m_len;
464 bcopy(mtod(inp->inp_options, caddr_t),
465 mtod(m, caddr_t), (unsigned)m->m_len);
466 } else
467 m->m_len = 0;
468 break;
469
470 case IP_TOS:
471 case IP_TTL:
472 case IP_RECVOPTS:
473 case IP_RECVRETOPTS:
474 case IP_RECVDSTADDR:
475 *mp = m = m_get(M_WAIT, MT_SOOPTS);
476 m->m_len = sizeof(int);
477 switch (optname) {
478
479 case IP_TOS:
480 optval = inp->inp_ip.ip_tos;
481 break;
482
483 case IP_TTL:
484 optval = inp->inp_ip.ip_ttl;
485 break;
486
487#define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
488
489 case IP_RECVOPTS:
490 optval = OPTBIT(INP_RECVOPTS);
491 break;
492
493 case IP_RECVRETOPTS:
494 optval = OPTBIT(INP_RECVRETOPTS);
495 break;
496
497 case IP_RECVDSTADDR:
498 optval = OPTBIT(INP_RECVDSTADDR);
499 break;
500 }
501 *mtod(m, int *) = optval;
502 break;
503
504 default:
505 error = EINVAL;
506 break;
507 }
508 break;
509 }
510 return (error);
511}
512
513/*
514 * Set up IP options in pcb for insertion in output packets.
515 * Store in mbuf with pointer in pcbopt, adding pseudo-option
516 * with destination address if source routed.
517 */
4c45483e 518int
15637ed4
RG
519#ifdef notyet
520ip_pcbopts(optname, pcbopt, m)
521 int optname;
522#else
523ip_pcbopts(pcbopt, m)
524#endif
525 struct mbuf **pcbopt;
526 register struct mbuf *m;
527{
528 register cnt, optlen;
529 register u_char *cp;
530 u_char opt;
531
532 /* turn off any old options */
533 if (*pcbopt)
534 (void)m_free(*pcbopt);
535 *pcbopt = 0;
536 if (m == (struct mbuf *)0 || m->m_len == 0) {
537 /*
538 * Only turning off any previous options.
539 */
540 if (m)
541 (void)m_free(m);
542 return (0);
543 }
544
545#ifndef vax
546 if (m->m_len % sizeof(long))
547 goto bad;
548#endif
549 /*
550 * IP first-hop destination address will be stored before
551 * actual options; move other options back
552 * and clear it when none present.
553 */
554 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
555 goto bad;
556 cnt = m->m_len;
557 m->m_len += sizeof(struct in_addr);
558 cp = mtod(m, u_char *) + sizeof(struct in_addr);
559 ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
560 bzero(mtod(m, caddr_t), sizeof(struct in_addr));
561
562 for (; cnt > 0; cnt -= optlen, cp += optlen) {
563 opt = cp[IPOPT_OPTVAL];
564 if (opt == IPOPT_EOL)
565 break;
566 if (opt == IPOPT_NOP)
567 optlen = 1;
568 else {
569 optlen = cp[IPOPT_OLEN];
570 if (optlen <= IPOPT_OLEN || optlen > cnt)
571 goto bad;
572 }
573 switch (opt) {
574
575 default:
576 break;
577
578 case IPOPT_LSRR:
579 case IPOPT_SSRR:
580 /*
581 * user process specifies route as:
582 * ->A->B->C->D
583 * D must be our final destination (but we can't
584 * check that since we may not have connected yet).
585 * A is first hop destination, which doesn't appear in
586 * actual IP option, but is stored before the options.
587 */
588 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
589 goto bad;
590 m->m_len -= sizeof(struct in_addr);
591 cnt -= sizeof(struct in_addr);
592 optlen -= sizeof(struct in_addr);
593 cp[IPOPT_OLEN] = optlen;
594 /*
595 * Move first hop before start of options.
596 */
597 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
598 sizeof(struct in_addr));
599 /*
600 * Then copy rest of options back
601 * to close up the deleted entry.
602 */
603 ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
604 sizeof(struct in_addr)),
605 (caddr_t)&cp[IPOPT_OFFSET+1],
606 (unsigned)cnt + sizeof(struct in_addr));
607 break;
608 }
609 }
610 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
611 goto bad;
612 *pcbopt = m;
613 return (0);
614
615bad:
616 (void)m_free(m);
617 return (EINVAL);
618}