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