ip_output.c revision 1.114 1 /* $NetBSD: ip_output.c,v 1.114 2003/08/19 08:00:54 itojun Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1998 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Public Access Networks Corporation ("Panix"). It was developed under
38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the NetBSD
51 * Foundation, Inc. and its contributors.
52 * 4. Neither the name of The NetBSD Foundation nor the names of its
53 * contributors may be used to endorse or promote products derived
54 * from this software without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 * POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 /*
70 * Copyright (c) 1982, 1986, 1988, 1990, 1993
71 * The Regents of the University of California. All rights reserved.
72 *
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. Neither the name of the University nor the names of its contributors
82 * may be used to endorse or promote products derived from this software
83 * without specific prior written permission.
84 *
85 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
86 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
89 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95 * SUCH DAMAGE.
96 *
97 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
98 */
99
100 #include <sys/cdefs.h>
101 __KERNEL_RCSID(0, "$NetBSD: ip_output.c,v 1.114 2003/08/19 08:00:54 itojun Exp $");
102
103 #include "opt_pfil_hooks.h"
104 #include "opt_ipsec.h"
105 #include "opt_mrouting.h"
106
107 #include <sys/param.h>
108 #include <sys/malloc.h>
109 #include <sys/mbuf.h>
110 #include <sys/errno.h>
111 #include <sys/protosw.h>
112 #include <sys/socket.h>
113 #include <sys/socketvar.h>
114 #include <sys/systm.h>
115 #include <sys/proc.h>
116
117 #include <net/if.h>
118 #include <net/route.h>
119 #include <net/pfil.h>
120
121 #include <netinet/in.h>
122 #include <netinet/in_systm.h>
123 #include <netinet/ip.h>
124 #include <netinet/in_pcb.h>
125 #include <netinet/in_var.h>
126 #include <netinet/ip_var.h>
127
128 #ifdef MROUTING
129 #include <netinet/ip_mroute.h>
130 #endif
131
132 #include <machine/stdarg.h>
133
134 #ifdef IPSEC
135 #include <netinet6/ipsec.h>
136 #include <netkey/key.h>
137 #include <netkey/key_debug.h>
138 #endif /*IPSEC*/
139
140 #ifdef FAST_IPSEC
141 #include <netipsec/ipsec.h>
142 #include <netipsec/key.h>
143 #include <netipsec/xform.h>
144 #endif /* FAST_IPSEC*/
145
146 static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
147 static struct ifnet *ip_multicast_if __P((struct in_addr *, int *));
148 static void ip_mloopback
149 __P((struct ifnet *, struct mbuf *, struct sockaddr_in *));
150
151 #ifdef PFIL_HOOKS
152 extern struct pfil_head inet_pfil_hook; /* XXX */
153 #endif
154
155 /*
156 * IP output. The packet in mbuf chain m contains a skeletal IP
157 * header (with len, off, ttl, proto, tos, src, dst).
158 * The mbuf chain containing the packet will be freed.
159 * The mbuf opt, if present, will not be freed.
160 */
161 int
162 #if __STDC__
163 ip_output(struct mbuf *m0, ...)
164 #else
165 ip_output(m0, va_alist)
166 struct mbuf *m0;
167 va_dcl
168 #endif
169 {
170 struct ip *ip;
171 struct ifnet *ifp;
172 struct mbuf *m = m0;
173 int hlen = sizeof (struct ip);
174 int len, error = 0;
175 struct route iproute;
176 struct sockaddr_in *dst;
177 struct in_ifaddr *ia;
178 struct mbuf *opt;
179 struct route *ro;
180 int flags, sw_csum;
181 int *mtu_p;
182 u_long mtu;
183 struct ip_moptions *imo;
184 struct inpcb *inp;
185 va_list ap;
186 #ifdef IPSEC
187 struct socket *so;
188 struct secpolicy *sp = NULL;
189 #endif /*IPSEC*/
190 #ifdef FAST_IPSEC
191 struct m_tag *mtag;
192 struct secpolicy *sp = NULL;
193 struct tdb_ident *tdbi;
194 int s;
195 #endif
196 u_int16_t ip_len;
197
198 len = 0;
199 va_start(ap, m0);
200 opt = va_arg(ap, struct mbuf *);
201 ro = va_arg(ap, struct route *);
202 flags = va_arg(ap, int);
203 imo = va_arg(ap, struct ip_moptions *);
204 inp = va_arg(ap, struct inpcb *);
205 if (flags & IP_RETURNMTU)
206 mtu_p = va_arg(ap, int *);
207 else
208 mtu_p = NULL;
209 va_end(ap);
210
211 MCLAIM(m, &ip_tx_mowner);
212 #ifdef IPSEC /* XXX so = ((inp == NULL) ? NULL : inp->inp_socket; */
213 so = ipsec_getsocket(m);
214 (void)ipsec_setsocket(m, NULL);
215 #endif /*IPSEC*/
216
217 #ifdef DIAGNOSTIC
218 if ((m->m_flags & M_PKTHDR) == 0)
219 panic("ip_output no HDR");
220 #endif
221 if (opt) {
222 m = ip_insertoptions(m, opt, &len);
223 if (len >= sizeof(struct ip))
224 hlen = len;
225 }
226 ip = mtod(m, struct ip *);
227 /*
228 * Fill in IP header.
229 */
230 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
231 ip->ip_v = IPVERSION;
232 ip->ip_off = htons(0);
233 ip->ip_id = htons(ip_id++);
234 ip->ip_hl = hlen >> 2;
235 ipstat.ips_localout++;
236 } else {
237 hlen = ip->ip_hl << 2;
238 }
239 /*
240 * Route packet.
241 */
242 if (ro == 0) {
243 ro = &iproute;
244 bzero((caddr_t)ro, sizeof (*ro));
245 }
246 dst = satosin(&ro->ro_dst);
247 /*
248 * If there is a cached route,
249 * check that it is to the same destination
250 * and is still up. If not, free it and try again.
251 * The address family should also be checked in case of sharing the
252 * cache with IPv6.
253 */
254 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
255 dst->sin_family != AF_INET ||
256 !in_hosteq(dst->sin_addr, ip->ip_dst))) {
257 RTFREE(ro->ro_rt);
258 ro->ro_rt = (struct rtentry *)0;
259 }
260 if (ro->ro_rt == 0) {
261 bzero(dst, sizeof(*dst));
262 dst->sin_family = AF_INET;
263 dst->sin_len = sizeof(*dst);
264 dst->sin_addr = ip->ip_dst;
265 }
266 /*
267 * If routing to interface only,
268 * short circuit routing lookup.
269 */
270 if (flags & IP_ROUTETOIF) {
271 if ((ia = ifatoia(ifa_ifwithladdr(sintosa(dst)))) == 0) {
272 ipstat.ips_noroute++;
273 error = ENETUNREACH;
274 goto bad;
275 }
276 ifp = ia->ia_ifp;
277 mtu = ifp->if_mtu;
278 ip->ip_ttl = 1;
279 } else if ((IN_MULTICAST(ip->ip_dst.s_addr) ||
280 ip->ip_dst.s_addr == INADDR_BROADCAST) &&
281 imo != NULL && imo->imo_multicast_ifp != NULL) {
282 ifp = imo->imo_multicast_ifp;
283 mtu = ifp->if_mtu;
284 IFP_TO_IA(ifp, ia);
285 } else {
286 if (ro->ro_rt == 0)
287 rtalloc(ro);
288 if (ro->ro_rt == 0) {
289 ipstat.ips_noroute++;
290 error = EHOSTUNREACH;
291 goto bad;
292 }
293 ia = ifatoia(ro->ro_rt->rt_ifa);
294 ifp = ro->ro_rt->rt_ifp;
295 if ((mtu = ro->ro_rt->rt_rmx.rmx_mtu) == 0)
296 mtu = ifp->if_mtu;
297 ro->ro_rt->rt_use++;
298 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
299 dst = satosin(ro->ro_rt->rt_gateway);
300 }
301 if (IN_MULTICAST(ip->ip_dst.s_addr) ||
302 (ip->ip_dst.s_addr == INADDR_BROADCAST)) {
303 struct in_multi *inm;
304
305 m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ?
306 M_BCAST : M_MCAST;
307 /*
308 * IP destination address is multicast. Make sure "dst"
309 * still points to the address in "ro". (It may have been
310 * changed to point to a gateway address, above.)
311 */
312 dst = satosin(&ro->ro_dst);
313 /*
314 * See if the caller provided any multicast options
315 */
316 if (imo != NULL)
317 ip->ip_ttl = imo->imo_multicast_ttl;
318 else
319 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
320
321 /*
322 * if we don't know the outgoing ifp yet, we can't generate
323 * output
324 */
325 if (!ifp) {
326 ipstat.ips_noroute++;
327 error = ENETUNREACH;
328 goto bad;
329 }
330
331 /*
332 * If the packet is multicast or broadcast, confirm that
333 * the outgoing interface can transmit it.
334 */
335 if (((m->m_flags & M_MCAST) &&
336 (ifp->if_flags & IFF_MULTICAST) == 0) ||
337 ((m->m_flags & M_BCAST) &&
338 (ifp->if_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)) {
339 ipstat.ips_noroute++;
340 error = ENETUNREACH;
341 goto bad;
342 }
343 /*
344 * If source address not specified yet, use an address
345 * of outgoing interface.
346 */
347 if (in_nullhost(ip->ip_src)) {
348 struct in_ifaddr *ia;
349
350 IFP_TO_IA(ifp, ia);
351 if (!ia) {
352 error = EADDRNOTAVAIL;
353 goto bad;
354 }
355 ip->ip_src = ia->ia_addr.sin_addr;
356 }
357
358 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
359 if (inm != NULL &&
360 (imo == NULL || imo->imo_multicast_loop)) {
361 /*
362 * If we belong to the destination multicast group
363 * on the outgoing interface, and the caller did not
364 * forbid loopback, loop back a copy.
365 */
366 ip_mloopback(ifp, m, dst);
367 }
368 #ifdef MROUTING
369 else {
370 /*
371 * If we are acting as a multicast router, perform
372 * multicast forwarding as if the packet had just
373 * arrived on the interface to which we are about
374 * to send. The multicast forwarding function
375 * recursively calls this function, using the
376 * IP_FORWARDING flag to prevent infinite recursion.
377 *
378 * Multicasts that are looped back by ip_mloopback(),
379 * above, will be forwarded by the ip_input() routine,
380 * if necessary.
381 */
382 extern struct socket *ip_mrouter;
383
384 if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
385 if (ip_mforward(m, ifp) != 0) {
386 m_freem(m);
387 goto done;
388 }
389 }
390 }
391 #endif
392 /*
393 * Multicasts with a time-to-live of zero may be looped-
394 * back, above, but must not be transmitted on a network.
395 * Also, multicasts addressed to the loopback interface
396 * are not sent -- the above call to ip_mloopback() will
397 * loop back a copy if this host actually belongs to the
398 * destination group on the loopback interface.
399 */
400 if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) {
401 m_freem(m);
402 goto done;
403 }
404
405 goto sendit;
406 }
407 #ifndef notdef
408 /*
409 * If source address not specified yet, use address
410 * of outgoing interface.
411 */
412 if (in_nullhost(ip->ip_src))
413 ip->ip_src = ia->ia_addr.sin_addr;
414 #endif
415
416 /*
417 * packets with Class-D address as source are not valid per
418 * RFC 1112
419 */
420 if (IN_MULTICAST(ip->ip_src.s_addr)) {
421 ipstat.ips_odropped++;
422 error = EADDRNOTAVAIL;
423 goto bad;
424 }
425
426 /*
427 * Look for broadcast address and
428 * and verify user is allowed to send
429 * such a packet.
430 */
431 if (in_broadcast(dst->sin_addr, ifp)) {
432 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
433 error = EADDRNOTAVAIL;
434 goto bad;
435 }
436 if ((flags & IP_ALLOWBROADCAST) == 0) {
437 error = EACCES;
438 goto bad;
439 }
440 /* don't allow broadcast messages to be fragmented */
441 if (ntohs(ip->ip_len) > ifp->if_mtu) {
442 error = EMSGSIZE;
443 goto bad;
444 }
445 m->m_flags |= M_BCAST;
446 } else
447 m->m_flags &= ~M_BCAST;
448
449 sendit:
450 /*
451 * If we're doing Path MTU Discovery, we need to set DF unless
452 * the route's MTU is locked.
453 */
454 if ((flags & IP_MTUDISC) != 0 && ro->ro_rt != NULL &&
455 (ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
456 ip->ip_off |= htons(IP_DF);
457
458 /* Remember the current ip_len */
459 ip_len = ntohs(ip->ip_len);
460
461 #ifdef IPSEC
462 /* get SP for this packet */
463 if (so == NULL)
464 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
465 flags, &error);
466 else
467 sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
468
469 if (sp == NULL) {
470 ipsecstat.out_inval++;
471 goto bad;
472 }
473
474 error = 0;
475
476 /* check policy */
477 switch (sp->policy) {
478 case IPSEC_POLICY_DISCARD:
479 /*
480 * This packet is just discarded.
481 */
482 ipsecstat.out_polvio++;
483 goto bad;
484
485 case IPSEC_POLICY_BYPASS:
486 case IPSEC_POLICY_NONE:
487 /* no need to do IPsec. */
488 goto skip_ipsec;
489
490 case IPSEC_POLICY_IPSEC:
491 if (sp->req == NULL) {
492 /* XXX should be panic ? */
493 printf("ip_output: No IPsec request specified.\n");
494 error = EINVAL;
495 goto bad;
496 }
497 break;
498
499 case IPSEC_POLICY_ENTRUST:
500 default:
501 printf("ip_output: Invalid policy found. %d\n", sp->policy);
502 }
503
504 /*
505 * ipsec4_output() expects ip_len and ip_off in network
506 * order. They have been set to network order above.
507 */
508
509 {
510 struct ipsec_output_state state;
511 bzero(&state, sizeof(state));
512 state.m = m;
513 if (flags & IP_ROUTETOIF) {
514 state.ro = &iproute;
515 bzero(&iproute, sizeof(iproute));
516 } else
517 state.ro = ro;
518 state.dst = (struct sockaddr *)dst;
519
520 /*
521 * We can't defer the checksum of payload data if
522 * we're about to encrypt/authenticate it.
523 *
524 * XXX When we support crypto offloading functions of
525 * XXX network interfaces, we need to reconsider this,
526 * XXX since it's likely that they'll support checksumming,
527 * XXX as well.
528 */
529 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
530 in_delayed_cksum(m);
531 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
532 }
533
534 error = ipsec4_output(&state, sp, flags);
535
536 m = state.m;
537 if (flags & IP_ROUTETOIF) {
538 /*
539 * if we have tunnel mode SA, we may need to ignore
540 * IP_ROUTETOIF.
541 */
542 if (state.ro != &iproute || state.ro->ro_rt != NULL) {
543 flags &= ~IP_ROUTETOIF;
544 ro = state.ro;
545 }
546 } else
547 ro = state.ro;
548 dst = (struct sockaddr_in *)state.dst;
549 if (error) {
550 /* mbuf is already reclaimed in ipsec4_output. */
551 m0 = NULL;
552 switch (error) {
553 case EHOSTUNREACH:
554 case ENETUNREACH:
555 case EMSGSIZE:
556 case ENOBUFS:
557 case ENOMEM:
558 break;
559 default:
560 printf("ip4_output (ipsec): error code %d\n", error);
561 /*fall through*/
562 case ENOENT:
563 /* don't show these error codes to the user */
564 error = 0;
565 break;
566 }
567 goto bad;
568 }
569
570 /* be sure to update variables that are affected by ipsec4_output() */
571 ip = mtod(m, struct ip *);
572 hlen = ip->ip_hl << 2;
573 ip_len = ntohs(ip->ip_len);
574
575 if (ro->ro_rt == NULL) {
576 if ((flags & IP_ROUTETOIF) == 0) {
577 printf("ip_output: "
578 "can't update route after IPsec processing\n");
579 error = EHOSTUNREACH; /*XXX*/
580 goto bad;
581 }
582 } else {
583 /* nobody uses ia beyond here */
584 if (state.encap)
585 ifp = ro->ro_rt->rt_ifp;
586 }
587 }
588 skip_ipsec:
589 #endif /*IPSEC*/
590 #ifdef FAST_IPSEC
591 /*
592 * Check the security policy (SP) for the packet and, if
593 * required, do IPsec-related processing. There are two
594 * cases here; the first time a packet is sent through
595 * it will be untagged and handled by ipsec4_checkpolicy.
596 * If the packet is resubmitted to ip_output (e.g. after
597 * AH, ESP, etc. processing), there will be a tag to bypass
598 * the lookup and related policy checking.
599 */
600 mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
601 s = splsoftnet();
602 if (mtag != NULL) {
603 tdbi = (struct tdb_ident *)(mtag + 1);
604 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
605 if (sp == NULL)
606 error = -EINVAL; /* force silent drop */
607 m_tag_delete(m, mtag);
608 } else {
609 sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
610 &error, inp);
611 }
612 /*
613 * There are four return cases:
614 * sp != NULL apply IPsec policy
615 * sp == NULL, error == 0 no IPsec handling needed
616 * sp == NULL, error == -EINVAL discard packet w/o error
617 * sp == NULL, error != 0 discard packet, report error
618 */
619 if (sp != NULL) {
620 /* Loop detection, check if ipsec processing already done */
621 IPSEC_ASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
622 for (mtag = m_tag_first(m); mtag != NULL;
623 mtag = m_tag_next(m, mtag)) {
624 #ifdef MTAG_ABI_COMPAT
625 if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
626 continue;
627 #endif
628 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
629 mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
630 continue;
631 /*
632 * Check if policy has an SA associated with it.
633 * This can happen when an SP has yet to acquire
634 * an SA; e.g. on first reference. If it occurs,
635 * then we let ipsec4_process_packet do its thing.
636 */
637 if (sp->req->sav == NULL)
638 break;
639 tdbi = (struct tdb_ident *)(mtag + 1);
640 if (tdbi->spi == sp->req->sav->spi &&
641 tdbi->proto == sp->req->sav->sah->saidx.proto &&
642 bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
643 sizeof (union sockaddr_union)) == 0) {
644 /*
645 * No IPsec processing is needed, free
646 * reference to SP.
647 *
648 * NB: null pointer to avoid free at
649 * done: below.
650 */
651 KEY_FREESP(&sp), sp = NULL;
652 splx(s);
653 goto spd_done;
654 }
655 }
656
657 /*
658 * Do delayed checksums now because we send before
659 * this is done in the normal processing path.
660 */
661 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
662 in_delayed_cksum(m);
663 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
664 }
665
666 #ifdef __FreeBSD__
667 ip->ip_len = htons(ip->ip_len);
668 ip->ip_off = htons(ip->ip_off);
669 #endif
670
671 /* NB: callee frees mbuf */
672 error = ipsec4_process_packet(m, sp->req, flags, 0);
673 /*
674 * Preserve KAME behaviour: ENOENT can be returned
675 * when an SA acquire is in progress. Don't propagate
676 * this to user-level; it confuses applications.
677 *
678 * XXX this will go away when the SADB is redone.
679 */
680 if (error == ENOENT)
681 error = 0;
682 splx(s);
683 goto done;
684 } else {
685 splx(s);
686
687 if (error != 0) {
688 /*
689 * Hack: -EINVAL is used to signal that a packet
690 * should be silently discarded. This is typically
691 * because we asked key management for an SA and
692 * it was delayed (e.g. kicked up to IKE).
693 */
694 if (error == -EINVAL)
695 error = 0;
696 goto bad;
697 } else {
698 /* No IPsec processing for this packet. */
699 }
700 #ifdef notyet
701 /*
702 * If deferred crypto processing is needed, check that
703 * the interface supports it.
704 */
705 mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
706 if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) {
707 /* notify IPsec to do its own crypto */
708 ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
709 error = EHOSTUNREACH;
710 goto bad;
711 }
712 #endif
713 }
714 spd_done:
715 #endif /* FAST_IPSEC */
716
717 #ifdef PFIL_HOOKS
718 /*
719 * Run through list of hooks for output packets.
720 */
721 if ((error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT)) != 0)
722 goto done;
723 if (m == NULL)
724 goto done;
725
726 ip = mtod(m, struct ip *);
727 hlen = ip->ip_hl << 2;
728 #endif /* PFIL_HOOKS */
729
730 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
731 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
732 /*
733 * If small enough for mtu of path, can just send directly.
734 */
735 if (ip_len <= mtu) {
736 #if IFA_STATS
737 /*
738 * search for the source address structure to
739 * maintain output statistics.
740 */
741 INADDR_TO_IA(ip->ip_src, ia);
742 if (ia)
743 ia->ia_ifa.ifa_data.ifad_outbytes += ip_len;
744 #endif
745 /*
746 * Always initialize the sum to 0! Some HW assisted
747 * checksumming requires this.
748 */
749 ip->ip_sum = 0;
750
751 /*
752 * Perform any checksums that the hardware can't do
753 * for us.
754 *
755 * XXX Does any hardware require the {th,uh}_sum
756 * XXX fields to be 0?
757 */
758 if (sw_csum & M_CSUM_IPv4) {
759 ip->ip_sum = in_cksum(m, hlen);
760 m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
761 }
762 if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
763 in_delayed_cksum(m);
764 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
765 }
766
767 #ifdef IPSEC
768 /* clean ipsec history once it goes out of the node */
769 ipsec_delaux(m);
770 #endif
771 error = (*ifp->if_output)(ifp, m, sintosa(dst), ro->ro_rt);
772 goto done;
773 }
774
775 /*
776 * We can't use HW checksumming if we're about to
777 * to fragment the packet.
778 *
779 * XXX Some hardware can do this.
780 */
781 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
782 in_delayed_cksum(m);
783 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
784 }
785
786 /*
787 * Too large for interface; fragment if possible.
788 * Must be able to put at least 8 bytes per fragment.
789 */
790 if (ntohs(ip->ip_off) & IP_DF) {
791 if (flags & IP_RETURNMTU)
792 *mtu_p = mtu;
793 error = EMSGSIZE;
794 ipstat.ips_cantfrag++;
795 goto bad;
796 }
797
798 error = ip_fragment(m, ifp, mtu);
799 if (error == EMSGSIZE)
800 goto bad;
801
802 for (m = m0; m; m = m0) {
803 m0 = m->m_nextpkt;
804 m->m_nextpkt = 0;
805 if (error == 0) {
806 #if IFA_STATS
807 /*
808 * search for the source address structure to
809 * maintain output statistics.
810 */
811 INADDR_TO_IA(ip->ip_src, ia);
812 if (ia) {
813 ia->ia_ifa.ifa_data.ifad_outbytes +=
814 ntohs(ip->ip_len);
815 }
816 #endif
817 #ifdef IPSEC
818 /* clean ipsec history once it goes out of the node */
819 ipsec_delaux(m);
820 #endif
821 KASSERT((m->m_pkthdr.csum_flags &
822 (M_CSUM_UDPv4 | M_CSUM_TCPv4)) == 0);
823 error = (*ifp->if_output)(ifp, m, sintosa(dst),
824 ro->ro_rt);
825 } else
826 m_freem(m);
827 }
828
829 if (error == 0)
830 ipstat.ips_fragmented++;
831 done:
832 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) {
833 RTFREE(ro->ro_rt);
834 ro->ro_rt = 0;
835 }
836
837 #ifdef IPSEC
838 if (sp != NULL) {
839 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
840 printf("DP ip_output call free SP:%p\n", sp));
841 key_freesp(sp);
842 }
843 #endif /* IPSEC */
844 #ifdef FAST_IPSEC
845 if (sp != NULL)
846 KEY_FREESP(&sp);
847 #endif /* FAST_IPSEC */
848
849 return (error);
850 bad:
851 m_freem(m);
852 goto done;
853 }
854
855 int
856 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu)
857 {
858 struct ip *ip, *mhip;
859 struct mbuf *m0;
860 int len, hlen, off;
861 int mhlen, firstlen;
862 struct mbuf **mnext;
863 int sw_csum;
864 int fragments = 0;
865 int s;
866 int error = 0;
867
868 ip = mtod(m, struct ip *);
869 hlen = ip->ip_hl << 2;
870 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
871
872 len = (mtu - hlen) &~ 7;
873 if (len < 8)
874 return (EMSGSIZE);
875
876 firstlen = len;
877 mnext = &m->m_nextpkt;
878
879 /*
880 * Loop through length of segment after first fragment,
881 * make new header and copy data of each part and link onto chain.
882 */
883 m0 = m;
884 mhlen = sizeof (struct ip);
885 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
886 MGETHDR(m, M_DONTWAIT, MT_HEADER);
887 if (m == 0) {
888 error = ENOBUFS;
889 ipstat.ips_odropped++;
890 goto sendorfree;
891 }
892 MCLAIM(m, m0->m_owner);
893 *mnext = m;
894 mnext = &m->m_nextpkt;
895 m->m_data += max_linkhdr;
896 mhip = mtod(m, struct ip *);
897 *mhip = *ip;
898 /* we must inherit MCAST and BCAST flags */
899 m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST);
900 if (hlen > sizeof (struct ip)) {
901 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
902 mhip->ip_hl = mhlen >> 2;
903 }
904 m->m_len = mhlen;
905 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
906 if (ip->ip_off & IP_MF)
907 mhip->ip_off |= IP_MF;
908 if (off + len >= ntohs(ip->ip_len))
909 len = ntohs(ip->ip_len) - off;
910 else
911 mhip->ip_off |= IP_MF;
912 HTONS(mhip->ip_off);
913 mhip->ip_len = htons((u_int16_t)(len + mhlen));
914 m->m_next = m_copy(m0, off, len);
915 if (m->m_next == 0) {
916 error = ENOBUFS; /* ??? */
917 ipstat.ips_odropped++;
918 goto sendorfree;
919 }
920 m->m_pkthdr.len = mhlen + len;
921 m->m_pkthdr.rcvif = (struct ifnet *)0;
922 mhip->ip_sum = 0;
923 if (sw_csum & M_CSUM_IPv4) {
924 mhip->ip_sum = in_cksum(m, mhlen);
925 KASSERT((m->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0);
926 } else {
927 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
928 }
929 ipstat.ips_ofragments++;
930 fragments++;
931 }
932 /*
933 * Update first fragment by trimming what's been copied out
934 * and updating header, then send each fragment (in order).
935 */
936 m = m0;
937 m_adj(m, hlen + firstlen - ntohs(ip->ip_len));
938 m->m_pkthdr.len = hlen + firstlen;
939 ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
940 ip->ip_off |= htons(IP_MF);
941 ip->ip_sum = 0;
942 if (sw_csum & M_CSUM_IPv4) {
943 ip->ip_sum = in_cksum(m, hlen);
944 m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
945 } else {
946 KASSERT(m->m_pkthdr.csum_flags & M_CSUM_IPv4);
947 }
948 sendorfree:
949 /*
950 * If there is no room for all the fragments, don't queue
951 * any of them.
952 */
953 s = splnet();
954 if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments)
955 error = ENOBUFS;
956 splx(s);
957 return (error);
958 }
959
960 /*
961 * Process a delayed payload checksum calculation.
962 */
963 void
964 in_delayed_cksum(struct mbuf *m)
965 {
966 struct ip *ip;
967 u_int16_t csum, offset;
968
969 ip = mtod(m, struct ip *);
970 offset = ip->ip_hl << 2;
971 csum = in4_cksum(m, 0, offset, ntohs(ip->ip_len) - offset);
972 if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv4) != 0)
973 csum = 0xffff;
974
975 offset += m->m_pkthdr.csum_data; /* checksum offset */
976
977 if ((offset + sizeof(u_int16_t)) > m->m_len) {
978 /* This happen when ip options were inserted
979 printf("in_delayed_cksum: pullup len %d off %d proto %d\n",
980 m->m_len, offset, ip->ip_p);
981 */
982 m_copyback(m, offset, sizeof(csum), (caddr_t) &csum);
983 } else
984 *(u_int16_t *)(mtod(m, caddr_t) + offset) = csum;
985 }
986
987 /*
988 * Determine the maximum length of the options to be inserted;
989 * we would far rather allocate too much space rather than too little.
990 */
991
992 u_int
993 ip_optlen(inp)
994 struct inpcb *inp;
995 {
996 struct mbuf *m = inp->inp_options;
997
998 if (m && m->m_len > offsetof(struct ipoption, ipopt_dst))
999 return (m->m_len - offsetof(struct ipoption, ipopt_dst));
1000 else
1001 return 0;
1002 }
1003
1004
1005 /*
1006 * Insert IP options into preformed packet.
1007 * Adjust IP destination as required for IP source routing,
1008 * as indicated by a non-zero in_addr at the start of the options.
1009 */
1010 static struct mbuf *
1011 ip_insertoptions(m, opt, phlen)
1012 struct mbuf *m;
1013 struct mbuf *opt;
1014 int *phlen;
1015 {
1016 struct ipoption *p = mtod(opt, struct ipoption *);
1017 struct mbuf *n;
1018 struct ip *ip = mtod(m, struct ip *);
1019 unsigned optlen;
1020
1021 optlen = opt->m_len - sizeof(p->ipopt_dst);
1022 if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET)
1023 return (m); /* XXX should fail */
1024 if (!in_nullhost(p->ipopt_dst))
1025 ip->ip_dst = p->ipopt_dst;
1026 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1027 MGETHDR(n, M_DONTWAIT, MT_HEADER);
1028 if (n == 0)
1029 return (m);
1030 MCLAIM(n, m->m_owner);
1031 M_COPY_PKTHDR(n, m);
1032 m->m_flags &= ~M_PKTHDR;
1033 m->m_len -= sizeof(struct ip);
1034 m->m_data += sizeof(struct ip);
1035 n->m_next = m;
1036 m = n;
1037 m->m_len = optlen + sizeof(struct ip);
1038 m->m_data += max_linkhdr;
1039 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
1040 } else {
1041 m->m_data -= optlen;
1042 m->m_len += optlen;
1043 memmove(mtod(m, caddr_t), ip, sizeof(struct ip));
1044 }
1045 m->m_pkthdr.len += optlen;
1046 ip = mtod(m, struct ip *);
1047 bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
1048 *phlen = sizeof(struct ip) + optlen;
1049 ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
1050 return (m);
1051 }
1052
1053 /*
1054 * Copy options from ip to jp,
1055 * omitting those not copied during fragmentation.
1056 */
1057 int
1058 ip_optcopy(ip, jp)
1059 struct ip *ip, *jp;
1060 {
1061 u_char *cp, *dp;
1062 int opt, optlen, cnt;
1063
1064 cp = (u_char *)(ip + 1);
1065 dp = (u_char *)(jp + 1);
1066 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1067 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1068 opt = cp[0];
1069 if (opt == IPOPT_EOL)
1070 break;
1071 if (opt == IPOPT_NOP) {
1072 /* Preserve for IP mcast tunnel's LSRR alignment. */
1073 *dp++ = IPOPT_NOP;
1074 optlen = 1;
1075 continue;
1076 }
1077 #ifdef DIAGNOSTIC
1078 if (cnt < IPOPT_OLEN + sizeof(*cp))
1079 panic("malformed IPv4 option passed to ip_optcopy");
1080 #endif
1081 optlen = cp[IPOPT_OLEN];
1082 #ifdef DIAGNOSTIC
1083 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1084 panic("malformed IPv4 option passed to ip_optcopy");
1085 #endif
1086 /* bogus lengths should have been caught by ip_dooptions */
1087 if (optlen > cnt)
1088 optlen = cnt;
1089 if (IPOPT_COPIED(opt)) {
1090 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
1091 dp += optlen;
1092 }
1093 }
1094 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1095 *dp++ = IPOPT_EOL;
1096 return (optlen);
1097 }
1098
1099 /*
1100 * IP socket option processing.
1101 */
1102 int
1103 ip_ctloutput(op, so, level, optname, mp)
1104 int op;
1105 struct socket *so;
1106 int level, optname;
1107 struct mbuf **mp;
1108 {
1109 struct inpcb *inp = sotoinpcb(so);
1110 struct mbuf *m = *mp;
1111 int optval = 0;
1112 int error = 0;
1113 #if defined(IPSEC) || defined(FAST_IPSEC)
1114 struct proc *p = curproc; /*XXX*/
1115 #endif
1116
1117 if (level != IPPROTO_IP) {
1118 error = EINVAL;
1119 if (op == PRCO_SETOPT && *mp)
1120 (void) m_free(*mp);
1121 } else switch (op) {
1122
1123 case PRCO_SETOPT:
1124 switch (optname) {
1125 case IP_OPTIONS:
1126 #ifdef notyet
1127 case IP_RETOPTS:
1128 return (ip_pcbopts(optname, &inp->inp_options, m));
1129 #else
1130 return (ip_pcbopts(&inp->inp_options, m));
1131 #endif
1132
1133 case IP_TOS:
1134 case IP_TTL:
1135 case IP_RECVOPTS:
1136 case IP_RECVRETOPTS:
1137 case IP_RECVDSTADDR:
1138 case IP_RECVIF:
1139 if (m == NULL || m->m_len != sizeof(int))
1140 error = EINVAL;
1141 else {
1142 optval = *mtod(m, int *);
1143 switch (optname) {
1144
1145 case IP_TOS:
1146 inp->inp_ip.ip_tos = optval;
1147 break;
1148
1149 case IP_TTL:
1150 inp->inp_ip.ip_ttl = optval;
1151 break;
1152 #define OPTSET(bit) \
1153 if (optval) \
1154 inp->inp_flags |= bit; \
1155 else \
1156 inp->inp_flags &= ~bit;
1157
1158 case IP_RECVOPTS:
1159 OPTSET(INP_RECVOPTS);
1160 break;
1161
1162 case IP_RECVRETOPTS:
1163 OPTSET(INP_RECVRETOPTS);
1164 break;
1165
1166 case IP_RECVDSTADDR:
1167 OPTSET(INP_RECVDSTADDR);
1168 break;
1169
1170 case IP_RECVIF:
1171 OPTSET(INP_RECVIF);
1172 break;
1173 }
1174 }
1175 break;
1176 #undef OPTSET
1177
1178 case IP_MULTICAST_IF:
1179 case IP_MULTICAST_TTL:
1180 case IP_MULTICAST_LOOP:
1181 case IP_ADD_MEMBERSHIP:
1182 case IP_DROP_MEMBERSHIP:
1183 error = ip_setmoptions(optname, &inp->inp_moptions, m);
1184 break;
1185
1186 case IP_PORTRANGE:
1187 if (m == 0 || m->m_len != sizeof(int))
1188 error = EINVAL;
1189 else {
1190 optval = *mtod(m, int *);
1191
1192 switch (optval) {
1193
1194 case IP_PORTRANGE_DEFAULT:
1195 case IP_PORTRANGE_HIGH:
1196 inp->inp_flags &= ~(INP_LOWPORT);
1197 break;
1198
1199 case IP_PORTRANGE_LOW:
1200 inp->inp_flags |= INP_LOWPORT;
1201 break;
1202
1203 default:
1204 error = EINVAL;
1205 break;
1206 }
1207 }
1208 break;
1209
1210 #if defined(IPSEC) || defined(FAST_IPSEC)
1211 case IP_IPSEC_POLICY:
1212 {
1213 caddr_t req = NULL;
1214 size_t len = 0;
1215 int priv = 0;
1216
1217 #ifdef __NetBSD__
1218 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
1219 priv = 0;
1220 else
1221 priv = 1;
1222 #else
1223 priv = (in6p->in6p_socket->so_state & SS_PRIV);
1224 #endif
1225 if (m) {
1226 req = mtod(m, caddr_t);
1227 len = m->m_len;
1228 }
1229 error = ipsec4_set_policy(inp, optname, req, len, priv);
1230 break;
1231 }
1232 #endif /*IPSEC*/
1233
1234 default:
1235 error = ENOPROTOOPT;
1236 break;
1237 }
1238 if (m)
1239 (void)m_free(m);
1240 break;
1241
1242 case PRCO_GETOPT:
1243 switch (optname) {
1244 case IP_OPTIONS:
1245 case IP_RETOPTS:
1246 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1247 MCLAIM(m, so->so_mowner);
1248 if (inp->inp_options) {
1249 m->m_len = inp->inp_options->m_len;
1250 bcopy(mtod(inp->inp_options, caddr_t),
1251 mtod(m, caddr_t), (unsigned)m->m_len);
1252 } else
1253 m->m_len = 0;
1254 break;
1255
1256 case IP_TOS:
1257 case IP_TTL:
1258 case IP_RECVOPTS:
1259 case IP_RECVRETOPTS:
1260 case IP_RECVDSTADDR:
1261 case IP_RECVIF:
1262 case IP_ERRORMTU:
1263 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1264 MCLAIM(m, so->so_mowner);
1265 m->m_len = sizeof(int);
1266 switch (optname) {
1267
1268 case IP_TOS:
1269 optval = inp->inp_ip.ip_tos;
1270 break;
1271
1272 case IP_TTL:
1273 optval = inp->inp_ip.ip_ttl;
1274 break;
1275
1276 case IP_ERRORMTU:
1277 optval = inp->inp_errormtu;
1278 break;
1279
1280 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1281
1282 case IP_RECVOPTS:
1283 optval = OPTBIT(INP_RECVOPTS);
1284 break;
1285
1286 case IP_RECVRETOPTS:
1287 optval = OPTBIT(INP_RECVRETOPTS);
1288 break;
1289
1290 case IP_RECVDSTADDR:
1291 optval = OPTBIT(INP_RECVDSTADDR);
1292 break;
1293
1294 case IP_RECVIF:
1295 optval = OPTBIT(INP_RECVIF);
1296 break;
1297 }
1298 *mtod(m, int *) = optval;
1299 break;
1300
1301 #if defined(IPSEC) || defined(FAST_IPSEC)
1302 case IP_IPSEC_POLICY:
1303 {
1304 caddr_t req = NULL;
1305 size_t len = 0;
1306
1307 if (m) {
1308 req = mtod(m, caddr_t);
1309 len = m->m_len;
1310 }
1311 error = ipsec4_get_policy(inp, req, len, mp);
1312 break;
1313 }
1314 #endif /*IPSEC*/
1315
1316 case IP_MULTICAST_IF:
1317 case IP_MULTICAST_TTL:
1318 case IP_MULTICAST_LOOP:
1319 case IP_ADD_MEMBERSHIP:
1320 case IP_DROP_MEMBERSHIP:
1321 error = ip_getmoptions(optname, inp->inp_moptions, mp);
1322 if (*mp)
1323 MCLAIM(*mp, so->so_mowner);
1324 break;
1325
1326 case IP_PORTRANGE:
1327 *mp = m = m_get(M_WAIT, MT_SOOPTS);
1328 MCLAIM(m, so->so_mowner);
1329 m->m_len = sizeof(int);
1330
1331 if (inp->inp_flags & INP_LOWPORT)
1332 optval = IP_PORTRANGE_LOW;
1333 else
1334 optval = IP_PORTRANGE_DEFAULT;
1335
1336 *mtod(m, int *) = optval;
1337 break;
1338
1339 default:
1340 error = ENOPROTOOPT;
1341 break;
1342 }
1343 break;
1344 }
1345 return (error);
1346 }
1347
1348 /*
1349 * Set up IP options in pcb for insertion in output packets.
1350 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1351 * with destination address if source routed.
1352 */
1353 int
1354 #ifdef notyet
1355 ip_pcbopts(optname, pcbopt, m)
1356 int optname;
1357 #else
1358 ip_pcbopts(pcbopt, m)
1359 #endif
1360 struct mbuf **pcbopt;
1361 struct mbuf *m;
1362 {
1363 int cnt, optlen;
1364 u_char *cp;
1365 u_char opt;
1366
1367 /* turn off any old options */
1368 if (*pcbopt)
1369 (void)m_free(*pcbopt);
1370 *pcbopt = 0;
1371 if (m == (struct mbuf *)0 || m->m_len == 0) {
1372 /*
1373 * Only turning off any previous options.
1374 */
1375 if (m)
1376 (void)m_free(m);
1377 return (0);
1378 }
1379
1380 #ifndef __vax__
1381 if (m->m_len % sizeof(int32_t))
1382 goto bad;
1383 #endif
1384 /*
1385 * IP first-hop destination address will be stored before
1386 * actual options; move other options back
1387 * and clear it when none present.
1388 */
1389 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1390 goto bad;
1391 cnt = m->m_len;
1392 m->m_len += sizeof(struct in_addr);
1393 cp = mtod(m, u_char *) + sizeof(struct in_addr);
1394 memmove(cp, mtod(m, caddr_t), (unsigned)cnt);
1395 bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1396
1397 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1398 opt = cp[IPOPT_OPTVAL];
1399 if (opt == IPOPT_EOL)
1400 break;
1401 if (opt == IPOPT_NOP)
1402 optlen = 1;
1403 else {
1404 if (cnt < IPOPT_OLEN + sizeof(*cp))
1405 goto bad;
1406 optlen = cp[IPOPT_OLEN];
1407 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1408 goto bad;
1409 }
1410 switch (opt) {
1411
1412 default:
1413 break;
1414
1415 case IPOPT_LSRR:
1416 case IPOPT_SSRR:
1417 /*
1418 * user process specifies route as:
1419 * ->A->B->C->D
1420 * D must be our final destination (but we can't
1421 * check that since we may not have connected yet).
1422 * A is first hop destination, which doesn't appear in
1423 * actual IP option, but is stored before the options.
1424 */
1425 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1426 goto bad;
1427 m->m_len -= sizeof(struct in_addr);
1428 cnt -= sizeof(struct in_addr);
1429 optlen -= sizeof(struct in_addr);
1430 cp[IPOPT_OLEN] = optlen;
1431 /*
1432 * Move first hop before start of options.
1433 */
1434 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1435 sizeof(struct in_addr));
1436 /*
1437 * Then copy rest of options back
1438 * to close up the deleted entry.
1439 */
1440 memmove(&cp[IPOPT_OFFSET+1],
1441 (caddr_t)(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
1442 (unsigned)cnt + sizeof(struct in_addr));
1443 break;
1444 }
1445 }
1446 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1447 goto bad;
1448 *pcbopt = m;
1449 return (0);
1450
1451 bad:
1452 (void)m_free(m);
1453 return (EINVAL);
1454 }
1455
1456 /*
1457 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1458 */
1459 static struct ifnet *
1460 ip_multicast_if(a, ifindexp)
1461 struct in_addr *a;
1462 int *ifindexp;
1463 {
1464 int ifindex;
1465 struct ifnet *ifp = NULL;
1466 struct in_ifaddr *ia;
1467
1468 if (ifindexp)
1469 *ifindexp = 0;
1470 if (ntohl(a->s_addr) >> 24 == 0) {
1471 ifindex = ntohl(a->s_addr) & 0xffffff;
1472 if (ifindex < 0 || if_index < ifindex)
1473 return NULL;
1474 ifp = ifindex2ifnet[ifindex];
1475 if (ifindexp)
1476 *ifindexp = ifindex;
1477 } else {
1478 LIST_FOREACH(ia, &IN_IFADDR_HASH(a->s_addr), ia_hash) {
1479 if (in_hosteq(ia->ia_addr.sin_addr, *a) &&
1480 (ia->ia_ifp->if_flags & IFF_MULTICAST) != 0) {
1481 ifp = ia->ia_ifp;
1482 break;
1483 }
1484 }
1485 }
1486 return ifp;
1487 }
1488
1489 /*
1490 * Set the IP multicast options in response to user setsockopt().
1491 */
1492 int
1493 ip_setmoptions(optname, imop, m)
1494 int optname;
1495 struct ip_moptions **imop;
1496 struct mbuf *m;
1497 {
1498 int error = 0;
1499 u_char loop;
1500 int i;
1501 struct in_addr addr;
1502 struct ip_mreq *mreq;
1503 struct ifnet *ifp;
1504 struct ip_moptions *imo = *imop;
1505 struct route ro;
1506 struct sockaddr_in *dst;
1507 int ifindex;
1508
1509 if (imo == NULL) {
1510 /*
1511 * No multicast option buffer attached to the pcb;
1512 * allocate one and initialize to default values.
1513 */
1514 imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS,
1515 M_WAITOK);
1516
1517 if (imo == NULL)
1518 return (ENOBUFS);
1519 *imop = imo;
1520 imo->imo_multicast_ifp = NULL;
1521 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1522 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1523 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1524 imo->imo_num_memberships = 0;
1525 }
1526
1527 switch (optname) {
1528
1529 case IP_MULTICAST_IF:
1530 /*
1531 * Select the interface for outgoing multicast packets.
1532 */
1533 if (m == NULL || m->m_len != sizeof(struct in_addr)) {
1534 error = EINVAL;
1535 break;
1536 }
1537 addr = *(mtod(m, struct in_addr *));
1538 /*
1539 * INADDR_ANY is used to remove a previous selection.
1540 * When no interface is selected, a default one is
1541 * chosen every time a multicast packet is sent.
1542 */
1543 if (in_nullhost(addr)) {
1544 imo->imo_multicast_ifp = NULL;
1545 break;
1546 }
1547 /*
1548 * The selected interface is identified by its local
1549 * IP address. Find the interface and confirm that
1550 * it supports multicasting.
1551 */
1552 ifp = ip_multicast_if(&addr, &ifindex);
1553 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1554 error = EADDRNOTAVAIL;
1555 break;
1556 }
1557 imo->imo_multicast_ifp = ifp;
1558 if (ifindex)
1559 imo->imo_multicast_addr = addr;
1560 else
1561 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1562 break;
1563
1564 case IP_MULTICAST_TTL:
1565 /*
1566 * Set the IP time-to-live for outgoing multicast packets.
1567 */
1568 if (m == NULL || m->m_len != 1) {
1569 error = EINVAL;
1570 break;
1571 }
1572 imo->imo_multicast_ttl = *(mtod(m, u_char *));
1573 break;
1574
1575 case IP_MULTICAST_LOOP:
1576 /*
1577 * Set the loopback flag for outgoing multicast packets.
1578 * Must be zero or one.
1579 */
1580 if (m == NULL || m->m_len != 1 ||
1581 (loop = *(mtod(m, u_char *))) > 1) {
1582 error = EINVAL;
1583 break;
1584 }
1585 imo->imo_multicast_loop = loop;
1586 break;
1587
1588 case IP_ADD_MEMBERSHIP:
1589 /*
1590 * Add a multicast group membership.
1591 * Group must be a valid IP multicast address.
1592 */
1593 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1594 error = EINVAL;
1595 break;
1596 }
1597 mreq = mtod(m, struct ip_mreq *);
1598 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1599 error = EINVAL;
1600 break;
1601 }
1602 /*
1603 * If no interface address was provided, use the interface of
1604 * the route to the given multicast address.
1605 */
1606 if (in_nullhost(mreq->imr_interface)) {
1607 bzero((caddr_t)&ro, sizeof(ro));
1608 ro.ro_rt = NULL;
1609 dst = satosin(&ro.ro_dst);
1610 dst->sin_len = sizeof(*dst);
1611 dst->sin_family = AF_INET;
1612 dst->sin_addr = mreq->imr_multiaddr;
1613 rtalloc(&ro);
1614 if (ro.ro_rt == NULL) {
1615 error = EADDRNOTAVAIL;
1616 break;
1617 }
1618 ifp = ro.ro_rt->rt_ifp;
1619 rtfree(ro.ro_rt);
1620 } else {
1621 ifp = ip_multicast_if(&mreq->imr_interface, NULL);
1622 }
1623 /*
1624 * See if we found an interface, and confirm that it
1625 * supports multicast.
1626 */
1627 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1628 error = EADDRNOTAVAIL;
1629 break;
1630 }
1631 /*
1632 * See if the membership already exists or if all the
1633 * membership slots are full.
1634 */
1635 for (i = 0; i < imo->imo_num_memberships; ++i) {
1636 if (imo->imo_membership[i]->inm_ifp == ifp &&
1637 in_hosteq(imo->imo_membership[i]->inm_addr,
1638 mreq->imr_multiaddr))
1639 break;
1640 }
1641 if (i < imo->imo_num_memberships) {
1642 error = EADDRINUSE;
1643 break;
1644 }
1645 if (i == IP_MAX_MEMBERSHIPS) {
1646 error = ETOOMANYREFS;
1647 break;
1648 }
1649 /*
1650 * Everything looks good; add a new record to the multicast
1651 * address list for the given interface.
1652 */
1653 if ((imo->imo_membership[i] =
1654 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1655 error = ENOBUFS;
1656 break;
1657 }
1658 ++imo->imo_num_memberships;
1659 break;
1660
1661 case IP_DROP_MEMBERSHIP:
1662 /*
1663 * Drop a multicast group membership.
1664 * Group must be a valid IP multicast address.
1665 */
1666 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1667 error = EINVAL;
1668 break;
1669 }
1670 mreq = mtod(m, struct ip_mreq *);
1671 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1672 error = EINVAL;
1673 break;
1674 }
1675 /*
1676 * If an interface address was specified, get a pointer
1677 * to its ifnet structure.
1678 */
1679 if (in_nullhost(mreq->imr_interface))
1680 ifp = NULL;
1681 else {
1682 ifp = ip_multicast_if(&mreq->imr_interface, NULL);
1683 if (ifp == NULL) {
1684 error = EADDRNOTAVAIL;
1685 break;
1686 }
1687 }
1688 /*
1689 * Find the membership in the membership array.
1690 */
1691 for (i = 0; i < imo->imo_num_memberships; ++i) {
1692 if ((ifp == NULL ||
1693 imo->imo_membership[i]->inm_ifp == ifp) &&
1694 in_hosteq(imo->imo_membership[i]->inm_addr,
1695 mreq->imr_multiaddr))
1696 break;
1697 }
1698 if (i == imo->imo_num_memberships) {
1699 error = EADDRNOTAVAIL;
1700 break;
1701 }
1702 /*
1703 * Give up the multicast address record to which the
1704 * membership points.
1705 */
1706 in_delmulti(imo->imo_membership[i]);
1707 /*
1708 * Remove the gap in the membership array.
1709 */
1710 for (++i; i < imo->imo_num_memberships; ++i)
1711 imo->imo_membership[i-1] = imo->imo_membership[i];
1712 --imo->imo_num_memberships;
1713 break;
1714
1715 default:
1716 error = EOPNOTSUPP;
1717 break;
1718 }
1719
1720 /*
1721 * If all options have default values, no need to keep the mbuf.
1722 */
1723 if (imo->imo_multicast_ifp == NULL &&
1724 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1725 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1726 imo->imo_num_memberships == 0) {
1727 free(*imop, M_IPMOPTS);
1728 *imop = NULL;
1729 }
1730
1731 return (error);
1732 }
1733
1734 /*
1735 * Return the IP multicast options in response to user getsockopt().
1736 */
1737 int
1738 ip_getmoptions(optname, imo, mp)
1739 int optname;
1740 struct ip_moptions *imo;
1741 struct mbuf **mp;
1742 {
1743 u_char *ttl;
1744 u_char *loop;
1745 struct in_addr *addr;
1746 struct in_ifaddr *ia;
1747
1748 *mp = m_get(M_WAIT, MT_SOOPTS);
1749
1750 switch (optname) {
1751
1752 case IP_MULTICAST_IF:
1753 addr = mtod(*mp, struct in_addr *);
1754 (*mp)->m_len = sizeof(struct in_addr);
1755 if (imo == NULL || imo->imo_multicast_ifp == NULL)
1756 *addr = zeroin_addr;
1757 else if (imo->imo_multicast_addr.s_addr) {
1758 /* return the value user has set */
1759 *addr = imo->imo_multicast_addr;
1760 } else {
1761 IFP_TO_IA(imo->imo_multicast_ifp, ia);
1762 *addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
1763 }
1764 return (0);
1765
1766 case IP_MULTICAST_TTL:
1767 ttl = mtod(*mp, u_char *);
1768 (*mp)->m_len = 1;
1769 *ttl = imo ? imo->imo_multicast_ttl
1770 : IP_DEFAULT_MULTICAST_TTL;
1771 return (0);
1772
1773 case IP_MULTICAST_LOOP:
1774 loop = mtod(*mp, u_char *);
1775 (*mp)->m_len = 1;
1776 *loop = imo ? imo->imo_multicast_loop
1777 : IP_DEFAULT_MULTICAST_LOOP;
1778 return (0);
1779
1780 default:
1781 return (EOPNOTSUPP);
1782 }
1783 }
1784
1785 /*
1786 * Discard the IP multicast options.
1787 */
1788 void
1789 ip_freemoptions(imo)
1790 struct ip_moptions *imo;
1791 {
1792 int i;
1793
1794 if (imo != NULL) {
1795 for (i = 0; i < imo->imo_num_memberships; ++i)
1796 in_delmulti(imo->imo_membership[i]);
1797 free(imo, M_IPMOPTS);
1798 }
1799 }
1800
1801 /*
1802 * Routine called from ip_output() to loop back a copy of an IP multicast
1803 * packet to the input queue of a specified interface. Note that this
1804 * calls the output routine of the loopback "driver", but with an interface
1805 * pointer that might NOT be &loif -- easier than replicating that code here.
1806 */
1807 static void
1808 ip_mloopback(ifp, m, dst)
1809 struct ifnet *ifp;
1810 struct mbuf *m;
1811 struct sockaddr_in *dst;
1812 {
1813 struct ip *ip;
1814 struct mbuf *copym;
1815
1816 copym = m_copy(m, 0, M_COPYALL);
1817 if (copym != NULL
1818 && (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip)))
1819 copym = m_pullup(copym, sizeof(struct ip));
1820 if (copym != NULL) {
1821 /*
1822 * We don't bother to fragment if the IP length is greater
1823 * than the interface's MTU. Can this possibly matter?
1824 */
1825 ip = mtod(copym, struct ip *);
1826
1827 if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
1828 in_delayed_cksum(copym);
1829 copym->m_pkthdr.csum_flags &=
1830 ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
1831 }
1832
1833 ip->ip_sum = 0;
1834 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
1835 (void) looutput(ifp, copym, sintosa(dst), NULL);
1836 }
1837 }
1838