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