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