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