ip_output.c revision 1.58.2.3 1 /* $NetBSD: ip_output.c,v 1.58.2.3 2000/04/30 10:34:33 he 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 sendit:
352 #ifdef PFIL_HOOKS
353 /*
354 * Run through list of hooks for output packets.
355 */
356 m1 = m;
357 for (pfh = pfil_hook_get(PFIL_OUT); pfh; pfh = pfh->pfil_link.tqe_next)
358 if (pfh->pfil_func) {
359 rv = pfh->pfil_func(ip, hlen, ifp, 1, &m1);
360 if (rv) {
361 error = EHOSTUNREACH;
362 goto done;
363 }
364 m = m1;
365 if (m == NULL)
366 goto done;
367 ip = mtod(m, struct ip *);
368 }
369 #endif /* PFIL_HOOKS */
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 /* we must inherit MCAST and BCAST flags */
423 m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST);
424 if (hlen > sizeof (struct ip)) {
425 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
426 mhip->ip_hl = mhlen >> 2;
427 }
428 m->m_len = mhlen;
429 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
430 if (ip->ip_off & IP_MF)
431 mhip->ip_off |= IP_MF;
432 if (off + len >= (u_int16_t)ip->ip_len)
433 len = (u_int16_t)ip->ip_len - off;
434 else
435 mhip->ip_off |= IP_MF;
436 mhip->ip_len = htons((u_int16_t)(len + mhlen));
437 m->m_next = m_copy(m0, off, len);
438 if (m->m_next == 0) {
439 error = ENOBUFS; /* ??? */
440 ipstat.ips_odropped++;
441 goto sendorfree;
442 }
443 m->m_pkthdr.len = mhlen + len;
444 m->m_pkthdr.rcvif = (struct ifnet *)0;
445 HTONS(mhip->ip_off);
446 mhip->ip_sum = 0;
447 mhip->ip_sum = in_cksum(m, mhlen);
448 ipstat.ips_ofragments++;
449 fragments++;
450 }
451 /*
452 * Update first fragment by trimming what's been copied out
453 * and updating header, then send each fragment (in order).
454 */
455 m = m0;
456 m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len);
457 m->m_pkthdr.len = hlen + firstlen;
458 ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
459 ip->ip_off |= IP_MF;
460 HTONS(ip->ip_off);
461 ip->ip_sum = 0;
462 ip->ip_sum = in_cksum(m, hlen);
463 sendorfree:
464 /*
465 * If there is no room for all the fragments, don't queue
466 * any of them.
467 */
468 s = splimp();
469 if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments)
470 error = ENOBUFS;
471 splx(s);
472 for (m = m0; m; m = m0) {
473 m0 = m->m_nextpkt;
474 m->m_nextpkt = 0;
475 if (error == 0)
476 error = (*ifp->if_output)(ifp, m, sintosa(dst),
477 ro->ro_rt);
478 else
479 m_freem(m);
480 }
481
482 if (error == 0)
483 ipstat.ips_fragmented++;
484 }
485 done:
486 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) {
487 RTFREE(ro->ro_rt);
488 ro->ro_rt = 0;
489 }
490 #if IFA_STATS
491 if (error == 0) {
492 /* search for the source address structure to maintain output
493 * statistics. */
494 bzero((caddr_t*) &src, sizeof(src));
495 src.sin_family = AF_INET;
496 src.sin_addr.s_addr = ip->ip_src.s_addr;
497 src.sin_len = sizeof(src);
498 ia = ifatoia(ifa_ifwithladdr(sintosa(&src)));
499 if (ia)
500 ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len);
501 }
502 #endif
503 return (error);
504 bad:
505 m_freem(m);
506 goto done;
507 }
508
509 /*
510 * Determine the maximum length of the options to be inserted;
511 * we would far rather allocate too much space rather than too little.
512 */
513
514 u_int
515 ip_optlen(inp)
516 struct inpcb *inp;
517 {
518 struct mbuf *m = inp->inp_options;
519
520 if (m && m->m_len > offsetof(struct ipoption, ipopt_dst))
521 return(m->m_len - offsetof(struct ipoption, ipopt_dst));
522 else
523 return 0;
524 }
525
526
527 /*
528 * Insert IP options into preformed packet.
529 * Adjust IP destination as required for IP source routing,
530 * as indicated by a non-zero in_addr at the start of the options.
531 */
532 static struct mbuf *
533 ip_insertoptions(m, opt, phlen)
534 register struct mbuf *m;
535 struct mbuf *opt;
536 int *phlen;
537 {
538 register struct ipoption *p = mtod(opt, struct ipoption *);
539 struct mbuf *n;
540 register struct ip *ip = mtod(m, struct ip *);
541 unsigned optlen;
542
543 optlen = opt->m_len - sizeof(p->ipopt_dst);
544 if (optlen + (u_int16_t)ip->ip_len > IP_MAXPACKET)
545 return (m); /* XXX should fail */
546 if (!in_nullhost(p->ipopt_dst))
547 ip->ip_dst = p->ipopt_dst;
548 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
549 MGETHDR(n, M_DONTWAIT, MT_HEADER);
550 if (n == 0)
551 return (m);
552 n->m_pkthdr.len = m->m_pkthdr.len + optlen;
553 m->m_len -= sizeof(struct ip);
554 m->m_data += sizeof(struct ip);
555 n->m_next = m;
556 m = n;
557 m->m_len = optlen + sizeof(struct ip);
558 m->m_data += max_linkhdr;
559 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
560 } else {
561 m->m_data -= optlen;
562 m->m_len += optlen;
563 m->m_pkthdr.len += optlen;
564 memmove(mtod(m, caddr_t), ip, sizeof(struct ip));
565 }
566 ip = mtod(m, struct ip *);
567 bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
568 *phlen = sizeof(struct ip) + optlen;
569 ip->ip_len += optlen;
570 return (m);
571 }
572
573 /*
574 * Copy options from ip to jp,
575 * omitting those not copied during fragmentation.
576 */
577 int
578 ip_optcopy(ip, jp)
579 struct ip *ip, *jp;
580 {
581 register u_char *cp, *dp;
582 int opt, optlen, cnt;
583
584 cp = (u_char *)(ip + 1);
585 dp = (u_char *)(jp + 1);
586 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
587 for (; cnt > 0; cnt -= optlen, cp += optlen) {
588 opt = cp[0];
589 if (opt == IPOPT_EOL)
590 break;
591 if (opt == IPOPT_NOP) {
592 /* Preserve for IP mcast tunnel's LSRR alignment. */
593 *dp++ = IPOPT_NOP;
594 optlen = 1;
595 continue;
596 } else
597 optlen = cp[IPOPT_OLEN];
598 /* bogus lengths should have been caught by ip_dooptions */
599 if (optlen > cnt)
600 optlen = cnt;
601 if (IPOPT_COPIED(opt)) {
602 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
603 dp += optlen;
604 }
605 }
606 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
607 *dp++ = IPOPT_EOL;
608 return (optlen);
609 }
610
611 /*
612 * IP socket option processing.
613 */
614 int
615 ip_ctloutput(op, so, level, optname, mp)
616 int op;
617 struct socket *so;
618 int level, optname;
619 struct mbuf **mp;
620 {
621 register struct inpcb *inp = sotoinpcb(so);
622 register struct mbuf *m = *mp;
623 register int optval = 0;
624 int error = 0;
625
626 if (level != IPPROTO_IP) {
627 error = EINVAL;
628 if (op == PRCO_SETOPT && *mp)
629 (void) m_free(*mp);
630 } else switch (op) {
631
632 case PRCO_SETOPT:
633 switch (optname) {
634 case IP_OPTIONS:
635 #ifdef notyet
636 case IP_RETOPTS:
637 return (ip_pcbopts(optname, &inp->inp_options, m));
638 #else
639 return (ip_pcbopts(&inp->inp_options, m));
640 #endif
641
642 case IP_TOS:
643 case IP_TTL:
644 case IP_RECVOPTS:
645 case IP_RECVRETOPTS:
646 case IP_RECVDSTADDR:
647 case IP_RECVIF:
648 if (m == NULL || m->m_len != sizeof(int))
649 error = EINVAL;
650 else {
651 optval = *mtod(m, int *);
652 switch (optname) {
653
654 case IP_TOS:
655 inp->inp_ip.ip_tos = optval;
656 break;
657
658 case IP_TTL:
659 inp->inp_ip.ip_ttl = optval;
660 break;
661 #define OPTSET(bit) \
662 if (optval) \
663 inp->inp_flags |= bit; \
664 else \
665 inp->inp_flags &= ~bit;
666
667 case IP_RECVOPTS:
668 OPTSET(INP_RECVOPTS);
669 break;
670
671 case IP_RECVRETOPTS:
672 OPTSET(INP_RECVRETOPTS);
673 break;
674
675 case IP_RECVDSTADDR:
676 OPTSET(INP_RECVDSTADDR);
677 break;
678
679 case IP_RECVIF:
680 OPTSET(INP_RECVIF);
681 break;
682 }
683 }
684 break;
685 #undef OPTSET
686
687 case IP_MULTICAST_IF:
688 case IP_MULTICAST_TTL:
689 case IP_MULTICAST_LOOP:
690 case IP_ADD_MEMBERSHIP:
691 case IP_DROP_MEMBERSHIP:
692 error = ip_setmoptions(optname, &inp->inp_moptions, m);
693 break;
694
695 case IP_PORTRANGE:
696 if (m == 0 || m->m_len != sizeof(int))
697 error = EINVAL;
698 else {
699 optval = *mtod(m, int *);
700
701 switch (optval) {
702
703 case IP_PORTRANGE_DEFAULT:
704 case IP_PORTRANGE_HIGH:
705 inp->inp_flags &= ~(INP_LOWPORT);
706 break;
707
708 case IP_PORTRANGE_LOW:
709 inp->inp_flags |= INP_LOWPORT;
710 break;
711
712 default:
713 error = EINVAL;
714 break;
715 }
716 }
717 break;
718
719 default:
720 error = ENOPROTOOPT;
721 break;
722 }
723 if (m)
724 (void)m_free(m);
725 break;
726
727 case PRCO_GETOPT:
728 switch (optname) {
729 case IP_OPTIONS:
730 case IP_RETOPTS:
731 *mp = m = m_get(M_WAIT, MT_SOOPTS);
732 if (inp->inp_options) {
733 m->m_len = inp->inp_options->m_len;
734 bcopy(mtod(inp->inp_options, caddr_t),
735 mtod(m, caddr_t), (unsigned)m->m_len);
736 } else
737 m->m_len = 0;
738 break;
739
740 case IP_TOS:
741 case IP_TTL:
742 case IP_RECVOPTS:
743 case IP_RECVRETOPTS:
744 case IP_RECVDSTADDR:
745 case IP_RECVIF:
746 case IP_ERRORMTU:
747 *mp = m = m_get(M_WAIT, MT_SOOPTS);
748 m->m_len = sizeof(int);
749 switch (optname) {
750
751 case IP_TOS:
752 optval = inp->inp_ip.ip_tos;
753 break;
754
755 case IP_TTL:
756 optval = inp->inp_ip.ip_ttl;
757 break;
758
759 case IP_ERRORMTU:
760 optval = inp->inp_errormtu;
761 break;
762
763 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
764
765 case IP_RECVOPTS:
766 optval = OPTBIT(INP_RECVOPTS);
767 break;
768
769 case IP_RECVRETOPTS:
770 optval = OPTBIT(INP_RECVRETOPTS);
771 break;
772
773 case IP_RECVDSTADDR:
774 optval = OPTBIT(INP_RECVDSTADDR);
775 break;
776
777 case IP_RECVIF:
778 optval = OPTBIT(INP_RECVIF);
779 break;
780 }
781 *mtod(m, int *) = optval;
782 break;
783
784 case IP_MULTICAST_IF:
785 case IP_MULTICAST_TTL:
786 case IP_MULTICAST_LOOP:
787 case IP_ADD_MEMBERSHIP:
788 case IP_DROP_MEMBERSHIP:
789 error = ip_getmoptions(optname, inp->inp_moptions, mp);
790 break;
791
792 case IP_PORTRANGE:
793 *mp = m = m_get(M_WAIT, MT_SOOPTS);
794 m->m_len = sizeof(int);
795
796 if (inp->inp_flags & INP_LOWPORT)
797 optval = IP_PORTRANGE_LOW;
798 else
799 optval = IP_PORTRANGE_DEFAULT;
800
801 *mtod(m, int *) = optval;
802 break;
803
804 default:
805 error = ENOPROTOOPT;
806 break;
807 }
808 break;
809 }
810 return (error);
811 }
812
813 /*
814 * Set up IP options in pcb for insertion in output packets.
815 * Store in mbuf with pointer in pcbopt, adding pseudo-option
816 * with destination address if source routed.
817 */
818 int
819 #ifdef notyet
820 ip_pcbopts(optname, pcbopt, m)
821 int optname;
822 #else
823 ip_pcbopts(pcbopt, m)
824 #endif
825 struct mbuf **pcbopt;
826 register struct mbuf *m;
827 {
828 register int cnt, optlen;
829 register u_char *cp;
830 u_char opt;
831
832 /* turn off any old options */
833 if (*pcbopt)
834 (void)m_free(*pcbopt);
835 *pcbopt = 0;
836 if (m == (struct mbuf *)0 || m->m_len == 0) {
837 /*
838 * Only turning off any previous options.
839 */
840 if (m)
841 (void)m_free(m);
842 return (0);
843 }
844
845 #ifndef vax
846 if (m->m_len % sizeof(int32_t))
847 goto bad;
848 #endif
849 /*
850 * IP first-hop destination address will be stored before
851 * actual options; move other options back
852 * and clear it when none present.
853 */
854 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
855 goto bad;
856 cnt = m->m_len;
857 m->m_len += sizeof(struct in_addr);
858 cp = mtod(m, u_char *) + sizeof(struct in_addr);
859 memmove(cp, mtod(m, caddr_t), (unsigned)cnt);
860 bzero(mtod(m, caddr_t), sizeof(struct in_addr));
861
862 for (; cnt > 0; cnt -= optlen, cp += optlen) {
863 opt = cp[IPOPT_OPTVAL];
864 if (opt == IPOPT_EOL)
865 break;
866 if (opt == IPOPT_NOP)
867 optlen = 1;
868 else {
869 optlen = cp[IPOPT_OLEN];
870 if (optlen <= IPOPT_OLEN || optlen > cnt)
871 goto bad;
872 }
873 switch (opt) {
874
875 default:
876 break;
877
878 case IPOPT_LSRR:
879 case IPOPT_SSRR:
880 /*
881 * user process specifies route as:
882 * ->A->B->C->D
883 * D must be our final destination (but we can't
884 * check that since we may not have connected yet).
885 * A is first hop destination, which doesn't appear in
886 * actual IP option, but is stored before the options.
887 */
888 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
889 goto bad;
890 m->m_len -= sizeof(struct in_addr);
891 cnt -= sizeof(struct in_addr);
892 optlen -= sizeof(struct in_addr);
893 cp[IPOPT_OLEN] = optlen;
894 /*
895 * Move first hop before start of options.
896 */
897 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
898 sizeof(struct in_addr));
899 /*
900 * Then copy rest of options back
901 * to close up the deleted entry.
902 */
903 memmove(&cp[IPOPT_OFFSET+1],
904 (caddr_t)(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
905 (unsigned)cnt + sizeof(struct in_addr));
906 break;
907 }
908 }
909 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
910 goto bad;
911 *pcbopt = m;
912 return (0);
913
914 bad:
915 (void)m_free(m);
916 return (EINVAL);
917 }
918
919 /*
920 * Set the IP multicast options in response to user setsockopt().
921 */
922 int
923 ip_setmoptions(optname, imop, m)
924 int optname;
925 struct ip_moptions **imop;
926 struct mbuf *m;
927 {
928 register int error = 0;
929 u_char loop;
930 register int i;
931 struct in_addr addr;
932 register struct ip_mreq *mreq;
933 register struct ifnet *ifp;
934 register struct ip_moptions *imo = *imop;
935 struct route ro;
936 register struct sockaddr_in *dst;
937
938 if (imo == NULL) {
939 /*
940 * No multicast option buffer attached to the pcb;
941 * allocate one and initialize to default values.
942 */
943 imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS,
944 M_WAITOK);
945
946 if (imo == NULL)
947 return (ENOBUFS);
948 *imop = imo;
949 imo->imo_multicast_ifp = NULL;
950 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
951 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
952 imo->imo_num_memberships = 0;
953 }
954
955 switch (optname) {
956
957 case IP_MULTICAST_IF:
958 /*
959 * Select the interface for outgoing multicast packets.
960 */
961 if (m == NULL || m->m_len != sizeof(struct in_addr)) {
962 error = EINVAL;
963 break;
964 }
965 addr = *(mtod(m, struct in_addr *));
966 /*
967 * INADDR_ANY is used to remove a previous selection.
968 * When no interface is selected, a default one is
969 * chosen every time a multicast packet is sent.
970 */
971 if (in_nullhost(addr)) {
972 imo->imo_multicast_ifp = NULL;
973 break;
974 }
975 /*
976 * The selected interface is identified by its local
977 * IP address. Find the interface and confirm that
978 * it supports multicasting.
979 */
980 INADDR_TO_IFP(addr, ifp);
981 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
982 error = EADDRNOTAVAIL;
983 break;
984 }
985 imo->imo_multicast_ifp = ifp;
986 break;
987
988 case IP_MULTICAST_TTL:
989 /*
990 * Set the IP time-to-live for outgoing multicast packets.
991 */
992 if (m == NULL || m->m_len != 1) {
993 error = EINVAL;
994 break;
995 }
996 imo->imo_multicast_ttl = *(mtod(m, u_char *));
997 break;
998
999 case IP_MULTICAST_LOOP:
1000 /*
1001 * Set the loopback flag for outgoing multicast packets.
1002 * Must be zero or one.
1003 */
1004 if (m == NULL || m->m_len != 1 ||
1005 (loop = *(mtod(m, u_char *))) > 1) {
1006 error = EINVAL;
1007 break;
1008 }
1009 imo->imo_multicast_loop = loop;
1010 break;
1011
1012 case IP_ADD_MEMBERSHIP:
1013 /*
1014 * Add a multicast group membership.
1015 * Group must be a valid IP multicast address.
1016 */
1017 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1018 error = EINVAL;
1019 break;
1020 }
1021 mreq = mtod(m, struct ip_mreq *);
1022 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1023 error = EINVAL;
1024 break;
1025 }
1026 /*
1027 * If no interface address was provided, use the interface of
1028 * the route to the given multicast address.
1029 */
1030 if (in_nullhost(mreq->imr_interface)) {
1031 bzero((caddr_t)&ro, sizeof(ro));
1032 ro.ro_rt = NULL;
1033 dst = satosin(&ro.ro_dst);
1034 dst->sin_len = sizeof(*dst);
1035 dst->sin_family = AF_INET;
1036 dst->sin_addr = mreq->imr_multiaddr;
1037 rtalloc(&ro);
1038 if (ro.ro_rt == NULL) {
1039 error = EADDRNOTAVAIL;
1040 break;
1041 }
1042 ifp = ro.ro_rt->rt_ifp;
1043 rtfree(ro.ro_rt);
1044 } else {
1045 INADDR_TO_IFP(mreq->imr_interface, ifp);
1046 }
1047 /*
1048 * See if we found an interface, and confirm that it
1049 * supports multicast.
1050 */
1051 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1052 error = EADDRNOTAVAIL;
1053 break;
1054 }
1055 /*
1056 * See if the membership already exists or if all the
1057 * membership slots are full.
1058 */
1059 for (i = 0; i < imo->imo_num_memberships; ++i) {
1060 if (imo->imo_membership[i]->inm_ifp == ifp &&
1061 in_hosteq(imo->imo_membership[i]->inm_addr,
1062 mreq->imr_multiaddr))
1063 break;
1064 }
1065 if (i < imo->imo_num_memberships) {
1066 error = EADDRINUSE;
1067 break;
1068 }
1069 if (i == IP_MAX_MEMBERSHIPS) {
1070 error = ETOOMANYREFS;
1071 break;
1072 }
1073 /*
1074 * Everything looks good; add a new record to the multicast
1075 * address list for the given interface.
1076 */
1077 if ((imo->imo_membership[i] =
1078 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1079 error = ENOBUFS;
1080 break;
1081 }
1082 ++imo->imo_num_memberships;
1083 break;
1084
1085 case IP_DROP_MEMBERSHIP:
1086 /*
1087 * Drop a multicast group membership.
1088 * Group must be a valid IP multicast address.
1089 */
1090 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1091 error = EINVAL;
1092 break;
1093 }
1094 mreq = mtod(m, struct ip_mreq *);
1095 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1096 error = EINVAL;
1097 break;
1098 }
1099 /*
1100 * If an interface address was specified, get a pointer
1101 * to its ifnet structure.
1102 */
1103 if (in_nullhost(mreq->imr_interface))
1104 ifp = NULL;
1105 else {
1106 INADDR_TO_IFP(mreq->imr_interface, ifp);
1107 if (ifp == NULL) {
1108 error = EADDRNOTAVAIL;
1109 break;
1110 }
1111 }
1112 /*
1113 * Find the membership in the membership array.
1114 */
1115 for (i = 0; i < imo->imo_num_memberships; ++i) {
1116 if ((ifp == NULL ||
1117 imo->imo_membership[i]->inm_ifp == ifp) &&
1118 in_hosteq(imo->imo_membership[i]->inm_addr,
1119 mreq->imr_multiaddr))
1120 break;
1121 }
1122 if (i == imo->imo_num_memberships) {
1123 error = EADDRNOTAVAIL;
1124 break;
1125 }
1126 /*
1127 * Give up the multicast address record to which the
1128 * membership points.
1129 */
1130 in_delmulti(imo->imo_membership[i]);
1131 /*
1132 * Remove the gap in the membership array.
1133 */
1134 for (++i; i < imo->imo_num_memberships; ++i)
1135 imo->imo_membership[i-1] = imo->imo_membership[i];
1136 --imo->imo_num_memberships;
1137 break;
1138
1139 default:
1140 error = EOPNOTSUPP;
1141 break;
1142 }
1143
1144 /*
1145 * If all options have default values, no need to keep the mbuf.
1146 */
1147 if (imo->imo_multicast_ifp == NULL &&
1148 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1149 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1150 imo->imo_num_memberships == 0) {
1151 free(*imop, M_IPMOPTS);
1152 *imop = NULL;
1153 }
1154
1155 return (error);
1156 }
1157
1158 /*
1159 * Return the IP multicast options in response to user getsockopt().
1160 */
1161 int
1162 ip_getmoptions(optname, imo, mp)
1163 int optname;
1164 register struct ip_moptions *imo;
1165 register struct mbuf **mp;
1166 {
1167 u_char *ttl;
1168 u_char *loop;
1169 struct in_addr *addr;
1170 struct in_ifaddr *ia;
1171
1172 *mp = m_get(M_WAIT, MT_SOOPTS);
1173
1174 switch (optname) {
1175
1176 case IP_MULTICAST_IF:
1177 addr = mtod(*mp, struct in_addr *);
1178 (*mp)->m_len = sizeof(struct in_addr);
1179 if (imo == NULL || imo->imo_multicast_ifp == NULL)
1180 *addr = zeroin_addr;
1181 else {
1182 IFP_TO_IA(imo->imo_multicast_ifp, ia);
1183 *addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
1184 }
1185 return (0);
1186
1187 case IP_MULTICAST_TTL:
1188 ttl = mtod(*mp, u_char *);
1189 (*mp)->m_len = 1;
1190 *ttl = imo ? imo->imo_multicast_ttl
1191 : IP_DEFAULT_MULTICAST_TTL;
1192 return (0);
1193
1194 case IP_MULTICAST_LOOP:
1195 loop = mtod(*mp, u_char *);
1196 (*mp)->m_len = 1;
1197 *loop = imo ? imo->imo_multicast_loop
1198 : IP_DEFAULT_MULTICAST_LOOP;
1199 return (0);
1200
1201 default:
1202 return (EOPNOTSUPP);
1203 }
1204 }
1205
1206 /*
1207 * Discard the IP multicast options.
1208 */
1209 void
1210 ip_freemoptions(imo)
1211 register struct ip_moptions *imo;
1212 {
1213 register int i;
1214
1215 if (imo != NULL) {
1216 for (i = 0; i < imo->imo_num_memberships; ++i)
1217 in_delmulti(imo->imo_membership[i]);
1218 free(imo, M_IPMOPTS);
1219 }
1220 }
1221
1222 /*
1223 * Routine called from ip_output() to loop back a copy of an IP multicast
1224 * packet to the input queue of a specified interface. Note that this
1225 * calls the output routine of the loopback "driver", but with an interface
1226 * pointer that might NOT be &loif -- easier than replicating that code here.
1227 */
1228 static void
1229 ip_mloopback(ifp, m, dst)
1230 struct ifnet *ifp;
1231 register struct mbuf *m;
1232 register struct sockaddr_in *dst;
1233 {
1234 register struct ip *ip;
1235 struct mbuf *copym;
1236
1237 copym = m_copy(m, 0, M_COPYALL);
1238 if (copym != NULL
1239 && (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip)))
1240 copym = m_pullup(copym, sizeof(struct ip));
1241 if (copym != NULL) {
1242 /*
1243 * We don't bother to fragment if the IP length is greater
1244 * than the interface's MTU. Can this possibly matter?
1245 */
1246 ip = mtod(copym, struct ip *);
1247 HTONS(ip->ip_len);
1248 HTONS(ip->ip_off);
1249 ip->ip_sum = 0;
1250 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
1251 (void) looutput(ifp, copym, sintosa(dst), NULL);
1252 }
1253 }
1254