ip_output.c revision 1.58 1 /* $NetBSD: ip_output.c,v 1.58 1999/03/27 01:24:50 aidan Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Public Access Networks Corporation ("Panix"). It was developed under
9 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1982, 1986, 1988, 1990, 1993
42 * The Regents of the University of California. All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
73 */
74
75 #include "opt_pfil_hooks.h"
76 #include "opt_mrouting.h"
77
78 #include <sys/param.h>
79 #include <sys/malloc.h>
80 #include <sys/mbuf.h>
81 #include <sys/errno.h>
82 #include <sys/protosw.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/systm.h>
86
87 #include <net/if.h>
88 #include <net/route.h>
89 #include <net/pfil.h>
90
91 #include <netinet/in.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip_var.h>
97
98 #ifdef __vax__
99 #include <machine/mtpr.h>
100 #endif
101
102 #include <machine/stdarg.h>
103
104 static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
105 static void ip_mloopback
106 __P((struct ifnet *, struct mbuf *, struct sockaddr_in *));
107
108 /*
109 * IP output. The packet in mbuf chain m contains a skeletal IP
110 * header (with len, off, ttl, proto, tos, src, dst).
111 * The mbuf chain containing the packet will be freed.
112 * The mbuf opt, if present, will not be freed.
113 */
114 int
115 #if __STDC__
116 ip_output(struct mbuf *m0, ...)
117 #else
118 ip_output(m0, va_alist)
119 struct mbuf *m0;
120 va_dcl
121 #endif
122 {
123 register struct ip *ip, *mhip;
124 register struct ifnet *ifp;
125 register struct mbuf *m = m0;
126 register int hlen = sizeof (struct ip);
127 int len, off, error = 0;
128 struct route iproute;
129 struct sockaddr_in *dst;
130 #if IFA_STATS
131 struct sockaddr_in src;
132 #endif
133 struct in_ifaddr *ia;
134 struct mbuf *opt;
135 struct route *ro;
136 int flags;
137 int *mtu_p;
138 int mtu;
139 struct ip_moptions *imo;
140 va_list ap;
141 #ifdef PFIL_HOOKS
142 struct packet_filter_hook *pfh;
143 struct mbuf *m1;
144 int rv;
145 #endif /* PFIL_HOOKS */
146
147 va_start(ap, m0);
148 opt = va_arg(ap, struct mbuf *);
149 ro = va_arg(ap, struct route *);
150 flags = va_arg(ap, int);
151 imo = va_arg(ap, struct ip_moptions *);
152 if (flags & IP_RETURNMTU)
153 mtu_p = va_arg(ap, int *);
154 else
155 mtu_p = NULL;
156 va_end(ap);
157
158 #ifdef DIAGNOSTIC
159 if ((m->m_flags & M_PKTHDR) == 0)
160 panic("ip_output no HDR");
161 #endif
162 if (opt) {
163 m = ip_insertoptions(m, opt, &len);
164 hlen = len;
165 }
166 ip = mtod(m, struct ip *);
167 /*
168 * Fill in IP header.
169 */
170 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
171 ip->ip_v = IPVERSION;
172 ip->ip_off &= IP_DF;
173 ip->ip_id = htons(ip_id++);
174 ip->ip_hl = hlen >> 2;
175 ipstat.ips_localout++;
176 } else {
177 hlen = ip->ip_hl << 2;
178 }
179 /*
180 * Route packet.
181 */
182 if (ro == 0) {
183 ro = &iproute;
184 bzero((caddr_t)ro, sizeof (*ro));
185 }
186 dst = satosin(&ro->ro_dst);
187 /*
188 * If there is a cached route,
189 * check that it is to the same destination
190 * and is still up. If not, free it and try again.
191 */
192 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
193 !in_hosteq(dst->sin_addr, ip->ip_dst))) {
194 RTFREE(ro->ro_rt);
195 ro->ro_rt = (struct rtentry *)0;
196 }
197 if (ro->ro_rt == 0) {
198 dst->sin_family = AF_INET;
199 dst->sin_len = sizeof(*dst);
200 dst->sin_addr = ip->ip_dst;
201 }
202 /*
203 * If routing to interface only,
204 * short circuit routing lookup.
205 */
206 if (flags & IP_ROUTETOIF) {
207 if ((ia = ifatoia(ifa_ifwithladdr(sintosa(dst)))) == 0) {
208 ipstat.ips_noroute++;
209 error = ENETUNREACH;
210 goto bad;
211 }
212 ifp = ia->ia_ifp;
213 mtu = ifp->if_mtu;
214 ip->ip_ttl = 1;
215 } else {
216 if (ro->ro_rt == 0)
217 rtalloc(ro);
218 if (ro->ro_rt == 0) {
219 ipstat.ips_noroute++;
220 error = EHOSTUNREACH;
221 goto bad;
222 }
223 ia = ifatoia(ro->ro_rt->rt_ifa);
224 ifp = ro->ro_rt->rt_ifp;
225 if ((mtu = ro->ro_rt->rt_rmx.rmx_mtu) == 0)
226 mtu = ifp->if_mtu;
227 ro->ro_rt->rt_use++;
228 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
229 dst = satosin(ro->ro_rt->rt_gateway);
230 }
231 if (IN_MULTICAST(ip->ip_dst.s_addr)) {
232 struct in_multi *inm;
233
234 m->m_flags |= M_MCAST;
235 /*
236 * IP destination address is multicast. Make sure "dst"
237 * still points to the address in "ro". (It may have been
238 * changed to point to a gateway address, above.)
239 */
240 dst = satosin(&ro->ro_dst);
241 /*
242 * See if the caller provided any multicast options
243 */
244 if (imo != NULL) {
245 ip->ip_ttl = imo->imo_multicast_ttl;
246 if (imo->imo_multicast_ifp != NULL) {
247 ifp = imo->imo_multicast_ifp;
248 mtu = ifp->if_mtu;
249 }
250 } else
251 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
252 /*
253 * Confirm that the outgoing interface supports multicast.
254 */
255 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
256 ipstat.ips_noroute++;
257 error = ENETUNREACH;
258 goto bad;
259 }
260 /*
261 * If source address not specified yet, use an address
262 * of outgoing interface.
263 */
264 if (in_nullhost(ip->ip_src)) {
265 register struct in_ifaddr *ia;
266
267 IFP_TO_IA(ifp, ia);
268 ip->ip_src = ia->ia_addr.sin_addr;
269 }
270
271 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
272 if (inm != NULL &&
273 (imo == NULL || imo->imo_multicast_loop)) {
274 /*
275 * If we belong to the destination multicast group
276 * on the outgoing interface, and the caller did not
277 * forbid loopback, loop back a copy.
278 */
279 ip_mloopback(ifp, m, dst);
280 }
281 #ifdef MROUTING
282 else {
283 /*
284 * If we are acting as a multicast router, perform
285 * multicast forwarding as if the packet had just
286 * arrived on the interface to which we are about
287 * to send. The multicast forwarding function
288 * recursively calls this function, using the
289 * IP_FORWARDING flag to prevent infinite recursion.
290 *
291 * Multicasts that are looped back by ip_mloopback(),
292 * above, will be forwarded by the ip_input() routine,
293 * if necessary.
294 */
295 extern struct socket *ip_mrouter;
296
297 if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
298 if (ip_mforward(m, ifp) != 0) {
299 m_freem(m);
300 goto done;
301 }
302 }
303 }
304 #endif
305 /*
306 * Multicasts with a time-to-live of zero may be looped-
307 * back, above, but must not be transmitted on a network.
308 * Also, multicasts addressed to the loopback interface
309 * are not sent -- the above call to ip_mloopback() will
310 * loop back a copy if this host actually belongs to the
311 * destination group on the loopback interface.
312 */
313 if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) {
314 m_freem(m);
315 goto done;
316 }
317
318 goto sendit;
319 }
320 #ifndef notdef
321 /*
322 * If source address not specified yet, use address
323 * of outgoing interface.
324 */
325 if (in_nullhost(ip->ip_src))
326 ip->ip_src = ia->ia_addr.sin_addr;
327 #endif
328 /*
329 * Look for broadcast address and
330 * and verify user is allowed to send
331 * such a packet.
332 */
333 if (in_broadcast(dst->sin_addr, ifp)) {
334 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
335 error = EADDRNOTAVAIL;
336 goto bad;
337 }
338 if ((flags & IP_ALLOWBROADCAST) == 0) {
339 error = EACCES;
340 goto bad;
341 }
342 /* don't allow broadcast messages to be fragmented */
343 if ((u_int16_t)ip->ip_len > ifp->if_mtu) {
344 error = EMSGSIZE;
345 goto bad;
346 }
347 m->m_flags |= M_BCAST;
348 } else
349 m->m_flags &= ~M_BCAST;
350
351 #ifdef PFIL_HOOKS
352 /*
353 * Run through list of hooks for output packets.
354 */
355 m1 = m;
356 for (pfh = pfil_hook_get(PFIL_OUT); pfh; pfh = pfh->pfil_link.tqe_next)
357 if (pfh->pfil_func) {
358 rv = pfh->pfil_func(ip, hlen, ifp, 1, &m1);
359 if (rv) {
360 error = EHOSTUNREACH;
361 goto done;
362 }
363 m = m1;
364 if (m == NULL)
365 goto done;
366 ip = mtod(m, struct ip *);
367 }
368 #endif /* PFIL_HOOKS */
369 sendit:
370 /*
371 * If small enough for mtu of path, can just send directly.
372 */
373 if ((u_int16_t)ip->ip_len <= mtu) {
374 HTONS(ip->ip_len);
375 HTONS(ip->ip_off);
376 ip->ip_sum = 0;
377 ip->ip_sum = in_cksum(m, hlen);
378 error = (*ifp->if_output)(ifp, m, sintosa(dst), ro->ro_rt);
379 goto done;
380 }
381 /*
382 * Too large for interface; fragment if possible.
383 * Must be able to put at least 8 bytes per fragment.
384 */
385 if (ip->ip_off & IP_DF) {
386 if (flags & IP_RETURNMTU)
387 *mtu_p = mtu;
388 error = EMSGSIZE;
389 ipstat.ips_cantfrag++;
390 goto bad;
391 }
392 len = (mtu - hlen) &~ 7;
393 if (len < 8) {
394 error = EMSGSIZE;
395 goto bad;
396 }
397
398 {
399 int mhlen, firstlen = len;
400 struct mbuf **mnext = &m->m_nextpkt;
401 int fragments = 0;
402 int s;
403
404 /*
405 * Loop through length of segment after first fragment,
406 * make new header and copy data of each part and link onto chain.
407 */
408 m0 = m;
409 mhlen = sizeof (struct ip);
410 for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len) {
411 MGETHDR(m, M_DONTWAIT, MT_HEADER);
412 if (m == 0) {
413 error = ENOBUFS;
414 ipstat.ips_odropped++;
415 goto sendorfree;
416 }
417 *mnext = m;
418 mnext = &m->m_nextpkt;
419 m->m_data += max_linkhdr;
420 mhip = mtod(m, struct ip *);
421 *mhip = *ip;
422 if (hlen > sizeof (struct ip)) {
423 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
424 mhip->ip_hl = mhlen >> 2;
425 }
426 m->m_len = mhlen;
427 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
428 if (ip->ip_off & IP_MF)
429 mhip->ip_off |= IP_MF;
430 if (off + len >= (u_int16_t)ip->ip_len)
431 len = (u_int16_t)ip->ip_len - off;
432 else
433 mhip->ip_off |= IP_MF;
434 mhip->ip_len = htons((u_int16_t)(len + mhlen));
435 m->m_next = m_copy(m0, off, len);
436 if (m->m_next == 0) {
437 error = ENOBUFS; /* ??? */
438 ipstat.ips_odropped++;
439 goto sendorfree;
440 }
441 m->m_pkthdr.len = mhlen + len;
442 m->m_pkthdr.rcvif = (struct ifnet *)0;
443 HTONS(mhip->ip_off);
444 mhip->ip_sum = 0;
445 mhip->ip_sum = in_cksum(m, mhlen);
446 ipstat.ips_ofragments++;
447 fragments++;
448 }
449 /*
450 * Update first fragment by trimming what's been copied out
451 * and updating header, then send each fragment (in order).
452 */
453 m = m0;
454 m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len);
455 m->m_pkthdr.len = hlen + firstlen;
456 ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
457 ip->ip_off |= IP_MF;
458 HTONS(ip->ip_off);
459 ip->ip_sum = 0;
460 ip->ip_sum = in_cksum(m, hlen);
461 sendorfree:
462 /*
463 * If there is no room for all the fragments, don't queue
464 * any of them.
465 */
466 s = splimp();
467 if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments)
468 error = ENOBUFS;
469 splx(s);
470 for (m = m0; m; m = m0) {
471 m0 = m->m_nextpkt;
472 m->m_nextpkt = 0;
473 if (error == 0)
474 error = (*ifp->if_output)(ifp, m, sintosa(dst),
475 ro->ro_rt);
476 else
477 m_freem(m);
478 }
479
480 if (error == 0)
481 ipstat.ips_fragmented++;
482 }
483 done:
484 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) {
485 RTFREE(ro->ro_rt);
486 ro->ro_rt = 0;
487 }
488 #if IFA_STATS
489 if (error == 0) {
490 /* search for the source address structure to maintain output
491 * statistics. */
492 bzero((caddr_t*) &src, sizeof(src));
493 src.sin_family = AF_INET;
494 src.sin_addr.s_addr = ip->ip_src.s_addr;
495 src.sin_len = sizeof(src);
496 ia = ifatoia(ifa_ifwithladdr(sintosa(&src)));
497 if (ia)
498 ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len);
499 }
500 #endif
501 return (error);
502 bad:
503 m_freem(m);
504 goto done;
505 }
506
507 /*
508 * Determine the maximum length of the options to be inserted;
509 * we would far rather allocate too much space rather than too little.
510 */
511
512 u_int
513 ip_optlen(inp)
514 struct inpcb *inp;
515 {
516 struct mbuf *m = inp->inp_options;
517
518 if (m && m->m_len > offsetof(struct ipoption, ipopt_dst))
519 return(m->m_len - offsetof(struct ipoption, ipopt_dst));
520 else
521 return 0;
522 }
523
524
525 /*
526 * Insert IP options into preformed packet.
527 * Adjust IP destination as required for IP source routing,
528 * as indicated by a non-zero in_addr at the start of the options.
529 */
530 static struct mbuf *
531 ip_insertoptions(m, opt, phlen)
532 register struct mbuf *m;
533 struct mbuf *opt;
534 int *phlen;
535 {
536 register struct ipoption *p = mtod(opt, struct ipoption *);
537 struct mbuf *n;
538 register struct ip *ip = mtod(m, struct ip *);
539 unsigned optlen;
540
541 optlen = opt->m_len - sizeof(p->ipopt_dst);
542 if (optlen + (u_int16_t)ip->ip_len > IP_MAXPACKET)
543 return (m); /* XXX should fail */
544 if (!in_nullhost(p->ipopt_dst))
545 ip->ip_dst = p->ipopt_dst;
546 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
547 MGETHDR(n, M_DONTWAIT, MT_HEADER);
548 if (n == 0)
549 return (m);
550 n->m_pkthdr.len = m->m_pkthdr.len + optlen;
551 m->m_len -= sizeof(struct ip);
552 m->m_data += sizeof(struct ip);
553 n->m_next = m;
554 m = n;
555 m->m_len = optlen + sizeof(struct ip);
556 m->m_data += max_linkhdr;
557 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
558 } else {
559 m->m_data -= optlen;
560 m->m_len += optlen;
561 m->m_pkthdr.len += optlen;
562 memmove(mtod(m, caddr_t), ip, sizeof(struct ip));
563 }
564 ip = mtod(m, struct ip *);
565 bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
566 *phlen = sizeof(struct ip) + optlen;
567 ip->ip_len += optlen;
568 return (m);
569 }
570
571 /*
572 * Copy options from ip to jp,
573 * omitting those not copied during fragmentation.
574 */
575 int
576 ip_optcopy(ip, jp)
577 struct ip *ip, *jp;
578 {
579 register u_char *cp, *dp;
580 int opt, optlen, cnt;
581
582 cp = (u_char *)(ip + 1);
583 dp = (u_char *)(jp + 1);
584 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
585 for (; cnt > 0; cnt -= optlen, cp += optlen) {
586 opt = cp[0];
587 if (opt == IPOPT_EOL)
588 break;
589 if (opt == IPOPT_NOP) {
590 /* Preserve for IP mcast tunnel's LSRR alignment. */
591 *dp++ = IPOPT_NOP;
592 optlen = 1;
593 continue;
594 } else
595 optlen = cp[IPOPT_OLEN];
596 /* bogus lengths should have been caught by ip_dooptions */
597 if (optlen > cnt)
598 optlen = cnt;
599 if (IPOPT_COPIED(opt)) {
600 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
601 dp += optlen;
602 }
603 }
604 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
605 *dp++ = IPOPT_EOL;
606 return (optlen);
607 }
608
609 /*
610 * IP socket option processing.
611 */
612 int
613 ip_ctloutput(op, so, level, optname, mp)
614 int op;
615 struct socket *so;
616 int level, optname;
617 struct mbuf **mp;
618 {
619 register struct inpcb *inp = sotoinpcb(so);
620 register struct mbuf *m = *mp;
621 register int optval = 0;
622 int error = 0;
623
624 if (level != IPPROTO_IP) {
625 error = EINVAL;
626 if (op == PRCO_SETOPT && *mp)
627 (void) m_free(*mp);
628 } else switch (op) {
629
630 case PRCO_SETOPT:
631 switch (optname) {
632 case IP_OPTIONS:
633 #ifdef notyet
634 case IP_RETOPTS:
635 return (ip_pcbopts(optname, &inp->inp_options, m));
636 #else
637 return (ip_pcbopts(&inp->inp_options, m));
638 #endif
639
640 case IP_TOS:
641 case IP_TTL:
642 case IP_RECVOPTS:
643 case IP_RECVRETOPTS:
644 case IP_RECVDSTADDR:
645 case IP_RECVIF:
646 if (m == NULL || m->m_len != sizeof(int))
647 error = EINVAL;
648 else {
649 optval = *mtod(m, int *);
650 switch (optname) {
651
652 case IP_TOS:
653 inp->inp_ip.ip_tos = optval;
654 break;
655
656 case IP_TTL:
657 inp->inp_ip.ip_ttl = optval;
658 break;
659 #define OPTSET(bit) \
660 if (optval) \
661 inp->inp_flags |= bit; \
662 else \
663 inp->inp_flags &= ~bit;
664
665 case IP_RECVOPTS:
666 OPTSET(INP_RECVOPTS);
667 break;
668
669 case IP_RECVRETOPTS:
670 OPTSET(INP_RECVRETOPTS);
671 break;
672
673 case IP_RECVDSTADDR:
674 OPTSET(INP_RECVDSTADDR);
675 break;
676
677 case IP_RECVIF:
678 OPTSET(INP_RECVIF);
679 break;
680 }
681 }
682 break;
683 #undef OPTSET
684
685 case IP_MULTICAST_IF:
686 case IP_MULTICAST_TTL:
687 case IP_MULTICAST_LOOP:
688 case IP_ADD_MEMBERSHIP:
689 case IP_DROP_MEMBERSHIP:
690 error = ip_setmoptions(optname, &inp->inp_moptions, m);
691 break;
692
693 case IP_PORTRANGE:
694 if (m == 0 || m->m_len != sizeof(int))
695 error = EINVAL;
696 else {
697 optval = *mtod(m, int *);
698
699 switch (optval) {
700
701 case IP_PORTRANGE_DEFAULT:
702 case IP_PORTRANGE_HIGH:
703 inp->inp_flags &= ~(INP_LOWPORT);
704 break;
705
706 case IP_PORTRANGE_LOW:
707 inp->inp_flags |= INP_LOWPORT;
708 break;
709
710 default:
711 error = EINVAL;
712 break;
713 }
714 }
715 break;
716
717 default:
718 error = ENOPROTOOPT;
719 break;
720 }
721 if (m)
722 (void)m_free(m);
723 break;
724
725 case PRCO_GETOPT:
726 switch (optname) {
727 case IP_OPTIONS:
728 case IP_RETOPTS:
729 *mp = m = m_get(M_WAIT, MT_SOOPTS);
730 if (inp->inp_options) {
731 m->m_len = inp->inp_options->m_len;
732 bcopy(mtod(inp->inp_options, caddr_t),
733 mtod(m, caddr_t), (unsigned)m->m_len);
734 } else
735 m->m_len = 0;
736 break;
737
738 case IP_TOS:
739 case IP_TTL:
740 case IP_RECVOPTS:
741 case IP_RECVRETOPTS:
742 case IP_RECVDSTADDR:
743 case IP_RECVIF:
744 case IP_ERRORMTU:
745 *mp = m = m_get(M_WAIT, MT_SOOPTS);
746 m->m_len = sizeof(int);
747 switch (optname) {
748
749 case IP_TOS:
750 optval = inp->inp_ip.ip_tos;
751 break;
752
753 case IP_TTL:
754 optval = inp->inp_ip.ip_ttl;
755 break;
756
757 case IP_ERRORMTU:
758 optval = inp->inp_errormtu;
759 break;
760
761 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
762
763 case IP_RECVOPTS:
764 optval = OPTBIT(INP_RECVOPTS);
765 break;
766
767 case IP_RECVRETOPTS:
768 optval = OPTBIT(INP_RECVRETOPTS);
769 break;
770
771 case IP_RECVDSTADDR:
772 optval = OPTBIT(INP_RECVDSTADDR);
773 break;
774
775 case IP_RECVIF:
776 optval = OPTBIT(INP_RECVIF);
777 break;
778 }
779 *mtod(m, int *) = optval;
780 break;
781
782 case IP_MULTICAST_IF:
783 case IP_MULTICAST_TTL:
784 case IP_MULTICAST_LOOP:
785 case IP_ADD_MEMBERSHIP:
786 case IP_DROP_MEMBERSHIP:
787 error = ip_getmoptions(optname, inp->inp_moptions, mp);
788 break;
789
790 case IP_PORTRANGE:
791 *mp = m = m_get(M_WAIT, MT_SOOPTS);
792 m->m_len = sizeof(int);
793
794 if (inp->inp_flags & INP_LOWPORT)
795 optval = IP_PORTRANGE_LOW;
796 else
797 optval = IP_PORTRANGE_DEFAULT;
798
799 *mtod(m, int *) = optval;
800 break;
801
802 default:
803 error = ENOPROTOOPT;
804 break;
805 }
806 break;
807 }
808 return (error);
809 }
810
811 /*
812 * Set up IP options in pcb for insertion in output packets.
813 * Store in mbuf with pointer in pcbopt, adding pseudo-option
814 * with destination address if source routed.
815 */
816 int
817 #ifdef notyet
818 ip_pcbopts(optname, pcbopt, m)
819 int optname;
820 #else
821 ip_pcbopts(pcbopt, m)
822 #endif
823 struct mbuf **pcbopt;
824 register struct mbuf *m;
825 {
826 register int cnt, optlen;
827 register u_char *cp;
828 u_char opt;
829
830 /* turn off any old options */
831 if (*pcbopt)
832 (void)m_free(*pcbopt);
833 *pcbopt = 0;
834 if (m == (struct mbuf *)0 || m->m_len == 0) {
835 /*
836 * Only turning off any previous options.
837 */
838 if (m)
839 (void)m_free(m);
840 return (0);
841 }
842
843 #ifndef vax
844 if (m->m_len % sizeof(int32_t))
845 goto bad;
846 #endif
847 /*
848 * IP first-hop destination address will be stored before
849 * actual options; move other options back
850 * and clear it when none present.
851 */
852 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
853 goto bad;
854 cnt = m->m_len;
855 m->m_len += sizeof(struct in_addr);
856 cp = mtod(m, u_char *) + sizeof(struct in_addr);
857 memmove(cp, mtod(m, caddr_t), (unsigned)cnt);
858 bzero(mtod(m, caddr_t), sizeof(struct in_addr));
859
860 for (; cnt > 0; cnt -= optlen, cp += optlen) {
861 opt = cp[IPOPT_OPTVAL];
862 if (opt == IPOPT_EOL)
863 break;
864 if (opt == IPOPT_NOP)
865 optlen = 1;
866 else {
867 optlen = cp[IPOPT_OLEN];
868 if (optlen <= IPOPT_OLEN || optlen > cnt)
869 goto bad;
870 }
871 switch (opt) {
872
873 default:
874 break;
875
876 case IPOPT_LSRR:
877 case IPOPT_SSRR:
878 /*
879 * user process specifies route as:
880 * ->A->B->C->D
881 * D must be our final destination (but we can't
882 * check that since we may not have connected yet).
883 * A is first hop destination, which doesn't appear in
884 * actual IP option, but is stored before the options.
885 */
886 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
887 goto bad;
888 m->m_len -= sizeof(struct in_addr);
889 cnt -= sizeof(struct in_addr);
890 optlen -= sizeof(struct in_addr);
891 cp[IPOPT_OLEN] = optlen;
892 /*
893 * Move first hop before start of options.
894 */
895 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
896 sizeof(struct in_addr));
897 /*
898 * Then copy rest of options back
899 * to close up the deleted entry.
900 */
901 memmove(&cp[IPOPT_OFFSET+1],
902 (caddr_t)(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
903 (unsigned)cnt + sizeof(struct in_addr));
904 break;
905 }
906 }
907 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
908 goto bad;
909 *pcbopt = m;
910 return (0);
911
912 bad:
913 (void)m_free(m);
914 return (EINVAL);
915 }
916
917 /*
918 * Set the IP multicast options in response to user setsockopt().
919 */
920 int
921 ip_setmoptions(optname, imop, m)
922 int optname;
923 struct ip_moptions **imop;
924 struct mbuf *m;
925 {
926 register int error = 0;
927 u_char loop;
928 register int i;
929 struct in_addr addr;
930 register struct ip_mreq *mreq;
931 register struct ifnet *ifp;
932 register struct ip_moptions *imo = *imop;
933 struct route ro;
934 register struct sockaddr_in *dst;
935
936 if (imo == NULL) {
937 /*
938 * No multicast option buffer attached to the pcb;
939 * allocate one and initialize to default values.
940 */
941 imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS,
942 M_WAITOK);
943
944 if (imo == NULL)
945 return (ENOBUFS);
946 *imop = imo;
947 imo->imo_multicast_ifp = NULL;
948 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
949 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
950 imo->imo_num_memberships = 0;
951 }
952
953 switch (optname) {
954
955 case IP_MULTICAST_IF:
956 /*
957 * Select the interface for outgoing multicast packets.
958 */
959 if (m == NULL || m->m_len != sizeof(struct in_addr)) {
960 error = EINVAL;
961 break;
962 }
963 addr = *(mtod(m, struct in_addr *));
964 /*
965 * INADDR_ANY is used to remove a previous selection.
966 * When no interface is selected, a default one is
967 * chosen every time a multicast packet is sent.
968 */
969 if (in_nullhost(addr)) {
970 imo->imo_multicast_ifp = NULL;
971 break;
972 }
973 /*
974 * The selected interface is identified by its local
975 * IP address. Find the interface and confirm that
976 * it supports multicasting.
977 */
978 INADDR_TO_IFP(addr, ifp);
979 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
980 error = EADDRNOTAVAIL;
981 break;
982 }
983 imo->imo_multicast_ifp = ifp;
984 break;
985
986 case IP_MULTICAST_TTL:
987 /*
988 * Set the IP time-to-live for outgoing multicast packets.
989 */
990 if (m == NULL || m->m_len != 1) {
991 error = EINVAL;
992 break;
993 }
994 imo->imo_multicast_ttl = *(mtod(m, u_char *));
995 break;
996
997 case IP_MULTICAST_LOOP:
998 /*
999 * Set the loopback flag for outgoing multicast packets.
1000 * Must be zero or one.
1001 */
1002 if (m == NULL || m->m_len != 1 ||
1003 (loop = *(mtod(m, u_char *))) > 1) {
1004 error = EINVAL;
1005 break;
1006 }
1007 imo->imo_multicast_loop = loop;
1008 break;
1009
1010 case IP_ADD_MEMBERSHIP:
1011 /*
1012 * Add a multicast group membership.
1013 * Group must be a valid IP multicast address.
1014 */
1015 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1016 error = EINVAL;
1017 break;
1018 }
1019 mreq = mtod(m, struct ip_mreq *);
1020 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1021 error = EINVAL;
1022 break;
1023 }
1024 /*
1025 * If no interface address was provided, use the interface of
1026 * the route to the given multicast address.
1027 */
1028 if (in_nullhost(mreq->imr_interface)) {
1029 bzero((caddr_t)&ro, sizeof(ro));
1030 ro.ro_rt = NULL;
1031 dst = satosin(&ro.ro_dst);
1032 dst->sin_len = sizeof(*dst);
1033 dst->sin_family = AF_INET;
1034 dst->sin_addr = mreq->imr_multiaddr;
1035 rtalloc(&ro);
1036 if (ro.ro_rt == NULL) {
1037 error = EADDRNOTAVAIL;
1038 break;
1039 }
1040 ifp = ro.ro_rt->rt_ifp;
1041 rtfree(ro.ro_rt);
1042 } else {
1043 INADDR_TO_IFP(mreq->imr_interface, ifp);
1044 }
1045 /*
1046 * See if we found an interface, and confirm that it
1047 * supports multicast.
1048 */
1049 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1050 error = EADDRNOTAVAIL;
1051 break;
1052 }
1053 /*
1054 * See if the membership already exists or if all the
1055 * membership slots are full.
1056 */
1057 for (i = 0; i < imo->imo_num_memberships; ++i) {
1058 if (imo->imo_membership[i]->inm_ifp == ifp &&
1059 in_hosteq(imo->imo_membership[i]->inm_addr,
1060 mreq->imr_multiaddr))
1061 break;
1062 }
1063 if (i < imo->imo_num_memberships) {
1064 error = EADDRINUSE;
1065 break;
1066 }
1067 if (i == IP_MAX_MEMBERSHIPS) {
1068 error = ETOOMANYREFS;
1069 break;
1070 }
1071 /*
1072 * Everything looks good; add a new record to the multicast
1073 * address list for the given interface.
1074 */
1075 if ((imo->imo_membership[i] =
1076 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1077 error = ENOBUFS;
1078 break;
1079 }
1080 ++imo->imo_num_memberships;
1081 break;
1082
1083 case IP_DROP_MEMBERSHIP:
1084 /*
1085 * Drop a multicast group membership.
1086 * Group must be a valid IP multicast address.
1087 */
1088 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1089 error = EINVAL;
1090 break;
1091 }
1092 mreq = mtod(m, struct ip_mreq *);
1093 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1094 error = EINVAL;
1095 break;
1096 }
1097 /*
1098 * If an interface address was specified, get a pointer
1099 * to its ifnet structure.
1100 */
1101 if (in_nullhost(mreq->imr_interface))
1102 ifp = NULL;
1103 else {
1104 INADDR_TO_IFP(mreq->imr_interface, ifp);
1105 if (ifp == NULL) {
1106 error = EADDRNOTAVAIL;
1107 break;
1108 }
1109 }
1110 /*
1111 * Find the membership in the membership array.
1112 */
1113 for (i = 0; i < imo->imo_num_memberships; ++i) {
1114 if ((ifp == NULL ||
1115 imo->imo_membership[i]->inm_ifp == ifp) &&
1116 in_hosteq(imo->imo_membership[i]->inm_addr,
1117 mreq->imr_multiaddr))
1118 break;
1119 }
1120 if (i == imo->imo_num_memberships) {
1121 error = EADDRNOTAVAIL;
1122 break;
1123 }
1124 /*
1125 * Give up the multicast address record to which the
1126 * membership points.
1127 */
1128 in_delmulti(imo->imo_membership[i]);
1129 /*
1130 * Remove the gap in the membership array.
1131 */
1132 for (++i; i < imo->imo_num_memberships; ++i)
1133 imo->imo_membership[i-1] = imo->imo_membership[i];
1134 --imo->imo_num_memberships;
1135 break;
1136
1137 default:
1138 error = EOPNOTSUPP;
1139 break;
1140 }
1141
1142 /*
1143 * If all options have default values, no need to keep the mbuf.
1144 */
1145 if (imo->imo_multicast_ifp == NULL &&
1146 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1147 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1148 imo->imo_num_memberships == 0) {
1149 free(*imop, M_IPMOPTS);
1150 *imop = NULL;
1151 }
1152
1153 return (error);
1154 }
1155
1156 /*
1157 * Return the IP multicast options in response to user getsockopt().
1158 */
1159 int
1160 ip_getmoptions(optname, imo, mp)
1161 int optname;
1162 register struct ip_moptions *imo;
1163 register struct mbuf **mp;
1164 {
1165 u_char *ttl;
1166 u_char *loop;
1167 struct in_addr *addr;
1168 struct in_ifaddr *ia;
1169
1170 *mp = m_get(M_WAIT, MT_SOOPTS);
1171
1172 switch (optname) {
1173
1174 case IP_MULTICAST_IF:
1175 addr = mtod(*mp, struct in_addr *);
1176 (*mp)->m_len = sizeof(struct in_addr);
1177 if (imo == NULL || imo->imo_multicast_ifp == NULL)
1178 *addr = zeroin_addr;
1179 else {
1180 IFP_TO_IA(imo->imo_multicast_ifp, ia);
1181 *addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
1182 }
1183 return (0);
1184
1185 case IP_MULTICAST_TTL:
1186 ttl = mtod(*mp, u_char *);
1187 (*mp)->m_len = 1;
1188 *ttl = imo ? imo->imo_multicast_ttl
1189 : IP_DEFAULT_MULTICAST_TTL;
1190 return (0);
1191
1192 case IP_MULTICAST_LOOP:
1193 loop = mtod(*mp, u_char *);
1194 (*mp)->m_len = 1;
1195 *loop = imo ? imo->imo_multicast_loop
1196 : IP_DEFAULT_MULTICAST_LOOP;
1197 return (0);
1198
1199 default:
1200 return (EOPNOTSUPP);
1201 }
1202 }
1203
1204 /*
1205 * Discard the IP multicast options.
1206 */
1207 void
1208 ip_freemoptions(imo)
1209 register struct ip_moptions *imo;
1210 {
1211 register int i;
1212
1213 if (imo != NULL) {
1214 for (i = 0; i < imo->imo_num_memberships; ++i)
1215 in_delmulti(imo->imo_membership[i]);
1216 free(imo, M_IPMOPTS);
1217 }
1218 }
1219
1220 /*
1221 * Routine called from ip_output() to loop back a copy of an IP multicast
1222 * packet to the input queue of a specified interface. Note that this
1223 * calls the output routine of the loopback "driver", but with an interface
1224 * pointer that might NOT be &loif -- easier than replicating that code here.
1225 */
1226 static void
1227 ip_mloopback(ifp, m, dst)
1228 struct ifnet *ifp;
1229 register struct mbuf *m;
1230 register struct sockaddr_in *dst;
1231 {
1232 register struct ip *ip;
1233 struct mbuf *copym;
1234
1235 copym = m_copy(m, 0, M_COPYALL);
1236 if (copym != NULL) {
1237 /*
1238 * We don't bother to fragment if the IP length is greater
1239 * than the interface's MTU. Can this possibly matter?
1240 */
1241 ip = mtod(copym, struct ip *);
1242 HTONS(ip->ip_len);
1243 HTONS(ip->ip_off);
1244 ip->ip_sum = 0;
1245 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
1246 (void) looutput(ifp, copym, sintosa(dst), NULL);
1247 }
1248 }
1249