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