]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/ip_output.c
xnu-517.12.7.tar.gz
[apple/xnu.git] / bsd / netinet / ip_output.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
1c79356b 11 *
e5568f75
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * Copyright (c) 1982, 1986, 1988, 1990, 1993
24 * The Regents of the University of California. All rights reserved.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 *
54 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
9bccf70c 55 * $FreeBSD: src/sys/netinet/ip_output.c,v 1.99.2.16 2001/07/19 06:37:26 kris Exp $
1c79356b
A
56 */
57
58#define _IP_VHL
59
1c79356b
A
60#include <sys/param.h>
61#include <sys/systm.h>
62#include <sys/kernel.h>
63#include <sys/malloc.h>
64#include <sys/mbuf.h>
65#include <sys/protosw.h>
66#include <sys/socket.h>
67#include <sys/socketvar.h>
68
69#include <net/if.h>
70#include <net/route.h>
71
72#include <netinet/in.h>
73#include <netinet/in_systm.h>
74#include <netinet/ip.h>
1c79356b
A
75#include <netinet/in_pcb.h>
76#include <netinet/in_var.h>
77#include <netinet/ip_var.h>
1c79356b 78
9bccf70c
A
79#include "faith.h"
80
81#include <net/dlil.h>
1c79356b
A
82#include <sys/kdebug.h>
83
84#define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 1)
85#define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 3)
86#define DBG_FNC_IP_OUTPUT NETDBG_CODE(DBG_NETIP, (1 << 8) | 1)
55e303ae 87#define DBG_FNC_IPSEC4_OUTPUT NETDBG_CODE(DBG_NETIP, (2 << 8) | 1)
1c79356b
A
88
89
9bccf70c 90#if vax
1c79356b
A
91#include <machine/mtpr.h>
92#endif
93
9bccf70c 94#if __FreeBSD__
1c79356b
A
95#include <machine/in_cksum.h>
96
97static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
98#endif
99
1c79356b
A
100#if IPSEC
101#include <netinet6/ipsec.h>
102#include <netkey/key.h>
9bccf70c 103#if IPSEC_DEBUG
1c79356b 104#include <netkey/key_debug.h>
1c79356b 105#else
9bccf70c 106#define KEYDEBUG(lev,arg)
1c79356b 107#endif
9bccf70c 108#endif /*IPSEC*/
1c79356b 109
1c79356b 110#include <netinet/ip_fw.h>
1c79356b
A
111
112#if DUMMYNET
113#include <netinet/ip_dummynet.h>
114#endif
115
116#if IPFIREWALL_FORWARD_DEBUG
117#define print_ip(a) printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\
118 (ntohl(a.s_addr)>>16)&0xFF,\
119 (ntohl(a.s_addr)>>8)&0xFF,\
120 (ntohl(a.s_addr))&0xFF);
121#endif
122
123u_short ip_id;
124
125static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
9bccf70c 126static struct ifnet *ip_multicast_if __P((struct in_addr *, int *));
1c79356b
A
127static void ip_mloopback
128 __P((struct ifnet *, struct mbuf *, struct sockaddr_in *, int));
129static int ip_getmoptions
130 __P((struct sockopt *, struct ip_moptions *));
131static int ip_pcbopts __P((int, struct mbuf **, struct mbuf *));
132static int ip_setmoptions
133 __P((struct sockopt *, struct ip_moptions **));
9bccf70c 134
55e303ae
A
135int ip_createmoptions(struct ip_moptions **imop);
136int ip_addmembership(struct ip_moptions *imo, struct ip_mreq *mreq);
137int ip_dropmembership(struct ip_moptions *imo, struct ip_mreq *mreq);
9bccf70c
A
138int ip_optcopy __P((struct ip *, struct ip *));
139extern int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **));
140#ifdef __APPLE__
141extern struct mbuf* m_dup(register struct mbuf *m, int how);
142#endif
143
1c79356b 144static u_long lo_dl_tag = 0;
1c79356b 145
0b4e3aa0
A
146void in_delayed_cksum(struct mbuf *m);
147extern int apple_hwcksum_tx;
55e303ae 148extern u_long route_generation;
1c79356b
A
149
150extern struct protosw inetsw[];
151
9bccf70c
A
152extern struct ip_linklocal_stat ip_linklocal_stat;
153
154/* temporary: for testing */
155#if IPSEC
156extern int ipsec_bypass;
157#endif
158
1c79356b
A
159/*
160 * IP output. The packet in mbuf chain m contains a skeletal IP
161 * header (with len, off, ttl, proto, tos, src, dst).
162 * The mbuf chain containing the packet will be freed.
163 * The mbuf opt, if present, will not be freed.
164 */
165int
166ip_output(m0, opt, ro, flags, imo)
167 struct mbuf *m0;
168 struct mbuf *opt;
169 struct route *ro;
170 int flags;
171 struct ip_moptions *imo;
172{
173 struct ip *ip, *mhip;
55e303ae 174 struct ifnet *ifp = NULL;
1c79356b
A
175 struct mbuf *m = m0;
176 int hlen = sizeof (struct ip);
177 int len, off, error = 0;
55e303ae 178 struct sockaddr_in *dst = NULL;
9bccf70c 179 struct in_ifaddr *ia = NULL;
0b4e3aa0 180 int isbroadcast, sw_csum;
1c79356b
A
181#if IPSEC
182 struct route iproute;
9bccf70c 183 struct socket *so = NULL;
1c79356b
A
184 struct secpolicy *sp = NULL;
185#endif
9bccf70c 186 u_int16_t divert_cookie; /* firewall cookie */
1c79356b
A
187#if IPFIREWALL_FORWARD
188 int fwd_rewrite_src = 0;
189#endif
9bccf70c
A
190 struct ip_fw_chain *rule = NULL;
191
192#if IPDIVERT
193 /* Get and reset firewall cookie */
194 divert_cookie = ip_divert_cookie;
195 ip_divert_cookie = 0;
196#else
197 divert_cookie = 0;
1c79356b
A
198#endif
199
200 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT | DBG_FUNC_START, 0,0,0,0,0);
201
1c79356b
A
202#if IPFIREWALL && DUMMYNET
203 /*
204 * dummynet packet are prepended a vestigial mbuf with
205 * m_type = MT_DUMMYNET and m_data pointing to the matching
206 * rule.
207 */
208 if (m->m_type == MT_DUMMYNET) {
1c79356b
A
209 /*
210 * the packet was already tagged, so part of the
211 * processing was already done, and we need to go down.
9bccf70c 212 * Get parameters from the header.
1c79356b 213 */
9bccf70c
A
214 rule = (struct ip_fw_chain *)(m->m_data) ;
215 opt = NULL ;
216 ro = & ( ((struct dn_pkt *)m)->ro ) ;
217 imo = NULL ;
218 dst = ((struct dn_pkt *)m)->dn_dst ;
219 ifp = ((struct dn_pkt *)m)->ifp ;
55e303ae 220 flags = ((struct dn_pkt *)m)->flags;
9bccf70c
A
221 m0 = m = m->m_next ;
222#if IPSEC
55e303ae 223 if (ipsec_bypass == 0 && (flags & IP_NOIPSEC) == 0) {
9bccf70c
A
224 so = ipsec_getsocket(m);
225 (void)ipsec_setsocket(m, NULL);
226 }
227#endif
1c79356b 228 ip = mtod(m, struct ip *);
1c79356b 229 hlen = IP_VHL_HL(ip->ip_vhl) << 2 ;
9bccf70c
A
230 if (ro->ro_rt != NULL)
231 ia = (struct in_ifaddr *)ro->ro_rt->rt_ifa;
1c79356b
A
232 goto sendit;
233 } else
234 rule = NULL ;
235#endif
9bccf70c 236#if IPSEC
55e303ae 237 if (ipsec_bypass == 0 && (flags & IP_NOIPSEC) == 0) {
9bccf70c
A
238 so = ipsec_getsocket(m);
239 (void)ipsec_setsocket(m, NULL);
240 }
241#endif
1c79356b
A
242
243#if DIAGNOSTIC
244 if ((m->m_flags & M_PKTHDR) == 0)
245 panic("ip_output no HDR");
246 if (!ro)
247 panic("ip_output no route, proto = %d",
248 mtod(m, struct ip *)->ip_p);
249#endif
250 if (opt) {
251 m = ip_insertoptions(m, opt, &len);
252 hlen = len;
253 }
254 ip = mtod(m, struct ip *);
255 /*
256 * Fill in IP header.
257 */
258 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
259 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
260 ip->ip_off &= IP_DF;
9bccf70c
A
261#if RANDOM_IP_ID
262 ip->ip_id = ip_randomid();
263#else
1c79356b 264 ip->ip_id = htons(ip_id++);
9bccf70c 265#endif
1c79356b
A
266 ipstat.ips_localout++;
267 } else {
268 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
269 }
270
271 KERNEL_DEBUG(DBG_LAYER_BEG, ip->ip_dst.s_addr,
272 ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len);
273
274 dst = (struct sockaddr_in *)&ro->ro_dst;
55e303ae 275
1c79356b
A
276 /*
277 * If there is a cached route,
278 * check that it is to the same destination
279 * and is still up. If not, free it and try again.
55e303ae
A
280 * The address family should also be checked in case of sharing the
281 * cache with IPv6.
1c79356b 282 */
55e303ae 283
ab86ba33
A
284 if (ro->ro_rt && (ro->ro_rt->generation_id != route_generation) &&
285 ((flags & (IP_ROUTETOIF | IP_FORWARDING)) == 0) && (ip->ip_src.s_addr != INADDR_ANY) &&
286 (ifa_foraddr(ip->ip_src.s_addr) == NULL)) {
287 error = EADDRNOTAVAIL;
288 goto bad;
289 }
1c79356b 290 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
55e303ae 291 dst->sin_family != AF_INET ||
ab86ba33 292 dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
9bccf70c 293 rtfree(ro->ro_rt);
1c79356b
A
294 ro->ro_rt = (struct rtentry *)0;
295 }
296 if (ro->ro_rt == 0) {
55e303ae 297 bzero(dst, sizeof(*dst));
1c79356b
A
298 dst->sin_family = AF_INET;
299 dst->sin_len = sizeof(*dst);
300 dst->sin_addr = ip->ip_dst;
301 }
302 /*
303 * If routing to interface only,
304 * short circuit routing lookup.
305 */
306#define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
307#define sintosa(sin) ((struct sockaddr *)(sin))
308 if (flags & IP_ROUTETOIF) {
309 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
310 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
311 ipstat.ips_noroute++;
312 error = ENETUNREACH;
313 goto bad;
314 }
315 ifp = ia->ia_ifp;
1c79356b
A
316 ip->ip_ttl = 1;
317 isbroadcast = in_broadcast(dst->sin_addr, ifp);
318 } else {
319 /*
320 * If this is the case, we probably don't want to allocate
321 * a protocol-cloned route since we didn't get one from the
322 * ULP. This lets TCP do its thing, while not burdening
323 * forwarding or ICMP with the overhead of cloning a route.
324 * Of course, we still want to do any cloning requested by
325 * the link layer, as this is probably required in all cases
326 * for correct operation (as it is for ARP).
327 */
328 if (ro->ro_rt == 0)
329 rtalloc_ign(ro, RTF_PRCLONING);
330 if (ro->ro_rt == 0) {
331 ipstat.ips_noroute++;
332 error = EHOSTUNREACH;
333 goto bad;
334 }
335 ia = ifatoia(ro->ro_rt->rt_ifa);
336 ifp = ro->ro_rt->rt_ifp;
1c79356b
A
337 ro->ro_rt->rt_use++;
338 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
339 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
340 if (ro->ro_rt->rt_flags & RTF_HOST)
341 isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
342 else
343 isbroadcast = in_broadcast(dst->sin_addr, ifp);
344 }
345 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
346 struct in_multi *inm;
347
348 m->m_flags |= M_MCAST;
349 /*
350 * IP destination address is multicast. Make sure "dst"
351 * still points to the address in "ro". (It may have been
352 * changed to point to a gateway address, above.)
353 */
354 dst = (struct sockaddr_in *)&ro->ro_dst;
355 /*
356 * See if the caller provided any multicast options
357 */
358 if (imo != NULL) {
55e303ae 359 if ((flags & IP_RAWOUTPUT) == 0) ip->ip_ttl = imo->imo_multicast_ttl;
fa4905b1 360 if (imo->imo_multicast_ifp != NULL) {
1c79356b 361 ifp = imo->imo_multicast_ifp;
fa4905b1 362 }
55e303ae
A
363 if (imo->imo_multicast_vif != -1 &&
364 ((flags & IP_RAWOUTPUT) == 0 || ip->ip_src.s_addr == INADDR_ANY))
1c79356b 365 ip->ip_src.s_addr =
55e303ae 366 ip_mcast_src(imo->imo_multicast_vif);
1c79356b 367 } else
55e303ae 368 if ((flags & IP_RAWOUTPUT) == 0) ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
1c79356b
A
369 /*
370 * Confirm that the outgoing interface supports multicast.
371 */
372 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
373 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
374 ipstat.ips_noroute++;
375 error = ENETUNREACH;
376 goto bad;
377 }
378 }
379 /*
380 * If source address not specified yet, use address
381 * of outgoing interface.
382 */
383 if (ip->ip_src.s_addr == INADDR_ANY) {
384 register struct in_ifaddr *ia1;
385
9bccf70c 386 TAILQ_FOREACH(ia1, &in_ifaddrhead, ia_link)
1c79356b
A
387 if (ia1->ia_ifp == ifp) {
388 ip->ip_src = IA_SIN(ia1)->sin_addr;
55e303ae 389
1c79356b
A
390 break;
391 }
55e303ae
A
392 if (ip->ip_src.s_addr == INADDR_ANY) {
393 error = ENETUNREACH;
394 goto bad;
395 }
1c79356b
A
396 }
397
398 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
399 if (inm != NULL &&
400 (imo == NULL || imo->imo_multicast_loop)) {
401 /*
402 * If we belong to the destination multicast group
403 * on the outgoing interface, and the caller did not
404 * forbid loopback, loop back a copy.
405 */
406 ip_mloopback(ifp, m, dst, hlen);
407 }
408 else {
409 /*
410 * If we are acting as a multicast router, perform
411 * multicast forwarding as if the packet had just
412 * arrived on the interface to which we are about
413 * to send. The multicast forwarding function
414 * recursively calls this function, using the
415 * IP_FORWARDING flag to prevent infinite recursion.
416 *
417 * Multicasts that are looped back by ip_mloopback(),
418 * above, will be forwarded by the ip_input() routine,
419 * if necessary.
420 */
421 if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
422 /*
423 * Check if rsvp daemon is running. If not, don't
424 * set ip_moptions. This ensures that the packet
425 * is multicast and not just sent down one link
426 * as prescribed by rsvpd.
427 */
428 if (!rsvp_on)
429 imo = NULL;
430 if (ip_mforward(ip, ifp, m, imo) != 0) {
431 m_freem(m);
432 goto done;
433 }
434 }
435 }
436
437 /*
438 * Multicasts with a time-to-live of zero may be looped-
439 * back, above, but must not be transmitted on a network.
440 * Also, multicasts addressed to the loopback interface
441 * are not sent -- the above call to ip_mloopback() will
442 * loop back a copy if this host actually belongs to the
443 * destination group on the loopback interface.
444 */
445 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
446 m_freem(m);
447 goto done;
448 }
449
450 goto sendit;
451 }
452#ifndef notdef
453 /*
454 * If source address not specified yet, use address
455 * of outgoing interface.
456 */
457 if (ip->ip_src.s_addr == INADDR_ANY) {
458 ip->ip_src = IA_SIN(ia)->sin_addr;
459#if IPFIREWALL_FORWARD
460 /* Keep note that we did this - if the firewall changes
461 * the next-hop, our interface may change, changing the
462 * default source IP. It's a shame so much effort happens
463 * twice. Oh well.
464 */
465 fwd_rewrite_src++;
466#endif /* IPFIREWALL_FORWARD */
467 }
468#endif /* notdef */
469 /*
470 * Verify that we have any chance at all of being able to queue
471 * the packet or packet fragments
472 */
473 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
474 ifp->if_snd.ifq_maxlen) {
475 error = ENOBUFS;
476 goto bad;
477 }
478
479 /*
480 * Look for broadcast address and
481 * and verify user is allowed to send
482 * such a packet.
483 */
484 if (isbroadcast) {
485 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
486 error = EADDRNOTAVAIL;
487 goto bad;
488 }
489 if ((flags & IP_ALLOWBROADCAST) == 0) {
490 error = EACCES;
491 goto bad;
492 }
493 /* don't allow broadcast messages to be fragmented */
494 if ((u_short)ip->ip_len > ifp->if_mtu) {
495 error = EMSGSIZE;
496 goto bad;
497 }
498 m->m_flags |= M_BCAST;
499 } else {
500 m->m_flags &= ~M_BCAST;
501 }
502
503sendit:
9bccf70c
A
504 /*
505 * Force IP TTL to 255 following draft-ietf-zeroconf-ipv4-linklocal.txt
506 */
507 if (IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) || IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
508 ip_linklocal_stat.iplls_out_total++;
509 if (ip->ip_ttl != MAXTTL) {
510 ip_linklocal_stat.iplls_out_badttl++;
511 ip->ip_ttl = MAXTTL;
512 }
513 }
514
515#if IPSEC
516 /* temporary for testing only: bypass ipsec alltogether */
517
55e303ae 518 if (ipsec_bypass != 0 || (flags & IP_NOIPSEC) != 0)
9bccf70c
A
519 goto skip_ipsec;
520
55e303ae
A
521 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_START, 0,0,0,0,0);
522
9bccf70c
A
523 /* get SP for this packet */
524 if (so == NULL)
525 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error);
526 else
527 sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
528
529 if (sp == NULL) {
530 ipsecstat.out_inval++;
55e303ae 531 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END, 0,0,0,0,0);
9bccf70c
A
532 goto bad;
533 }
534
535 error = 0;
536
537 /* check policy */
538 switch (sp->policy) {
539 case IPSEC_POLICY_DISCARD:
540 /*
541 * This packet is just discarded.
542 */
543 ipsecstat.out_polvio++;
55e303ae 544 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END, 1,0,0,0,0);
9bccf70c
A
545 goto bad;
546
547 case IPSEC_POLICY_BYPASS:
548 case IPSEC_POLICY_NONE:
549 /* no need to do IPsec. */
55e303ae 550 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END, 2,0,0,0,0);
9bccf70c
A
551 goto skip_ipsec;
552
553 case IPSEC_POLICY_IPSEC:
554 if (sp->req == NULL) {
555 /* acquire a policy */
556 error = key_spdacquire(sp);
55e303ae 557 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END, 3,0,0,0,0);
9bccf70c
A
558 goto bad;
559 }
560 break;
561
562 case IPSEC_POLICY_ENTRUST:
563 default:
564 printf("ip_output: Invalid policy found. %d\n", sp->policy);
565 }
566 {
567 struct ipsec_output_state state;
568 bzero(&state, sizeof(state));
569 state.m = m;
570 if (flags & IP_ROUTETOIF) {
571 state.ro = &iproute;
572 bzero(&iproute, sizeof(iproute));
573 } else
574 state.ro = ro;
575 state.dst = (struct sockaddr *)dst;
576
577 ip->ip_sum = 0;
578
579 /*
580 * XXX
581 * delayed checksums are not currently compatible with IPsec
582 */
583 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
584 in_delayed_cksum(m);
585 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
586 }
587
588 HTONS(ip->ip_len);
589 HTONS(ip->ip_off);
590
591 error = ipsec4_output(&state, sp, flags);
592
55e303ae
A
593 m0 = m = state.m;
594
9bccf70c
A
595 if (flags & IP_ROUTETOIF) {
596 /*
597 * if we have tunnel mode SA, we may need to ignore
598 * IP_ROUTETOIF.
599 */
600 if (state.ro != &iproute || state.ro->ro_rt != NULL) {
601 flags &= ~IP_ROUTETOIF;
602 ro = state.ro;
603 }
604 } else
605 ro = state.ro;
55e303ae 606
9bccf70c
A
607 dst = (struct sockaddr_in *)state.dst;
608 if (error) {
609 /* mbuf is already reclaimed in ipsec4_output. */
610 m0 = NULL;
611 switch (error) {
612 case EHOSTUNREACH:
613 case ENETUNREACH:
614 case EMSGSIZE:
615 case ENOBUFS:
616 case ENOMEM:
617 break;
618 default:
619 printf("ip4_output (ipsec): error code %d\n", error);
620 /*fall through*/
621 case ENOENT:
622 /* don't show these error codes to the user */
623 error = 0;
624 break;
625 }
55e303ae 626 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END, 4,0,0,0,0);
9bccf70c
A
627 goto bad;
628 }
629 }
630
631 /* be sure to update variables that are affected by ipsec4_output() */
632 ip = mtod(m, struct ip *);
55e303ae 633
9bccf70c
A
634#ifdef _IP_VHL
635 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
636#else
637 hlen = ip->ip_hl << 2;
638#endif
55e303ae
A
639 /* Check that there wasn't a route change and src is still valid */
640
641 if (ro->ro_rt->generation_id != route_generation) {
642 if (ifa_foraddr(ip->ip_src.s_addr) == NULL && ((flags & (IP_ROUTETOIF | IP_FORWARDING)) == 0)) {
643 error = EADDRNOTAVAIL;
644 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END, 5,0,0,0,0);
645 goto bad;
646 }
647 rtfree(ro->ro_rt);
648 ro->ro_rt = NULL;
649 }
650
9bccf70c
A
651 if (ro->ro_rt == NULL) {
652 if ((flags & IP_ROUTETOIF) == 0) {
653 printf("ip_output: "
654 "can't update route after IPsec processing\n");
55e303ae
A
655 error = EHOSTUNREACH; /*XXX*/
656 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END, 6,0,0,0,0);
9bccf70c
A
657 goto bad;
658 }
659 } else {
660 ia = ifatoia(ro->ro_rt->rt_ifa);
661 ifp = ro->ro_rt->rt_ifp;
9bccf70c
A
662 }
663
664 /* make it flipped, again. */
665 NTOHS(ip->ip_len);
666 NTOHS(ip->ip_off);
55e303ae 667 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END, 7,0xff,0xff,0xff,0xff);
9bccf70c
A
668skip_ipsec:
669#endif /*IPSEC*/
670
1c79356b
A
671 /*
672 * IpHack's section.
673 * - Xlate: translate packet's addr/port (NAT).
674 * - Firewall: deny/allow/etc.
675 * - Wrap: fake packet's addr/port <unimpl.>
676 * - Encapsulate: put it in another IP and send out. <unimp.>
677 */
9bccf70c
A
678 if (fr_checkp) {
679 struct mbuf *m1 = m;
680
681 if ((error = (*fr_checkp)(ip, hlen, ifp, 1, &m1)) || !m1)
682 goto done;
55e303ae 683 ip = mtod(m0 = m = m1, struct ip *);
1c79356b
A
684 }
685
686 /*
687 * Check with the firewall...
688 */
9bccf70c 689 if (fw_enable && ip_fw_chk_ptr) {
1c79356b
A
690 struct sockaddr_in *old = dst;
691
692 off = (*ip_fw_chk_ptr)(&ip,
9bccf70c 693 hlen, ifp, &divert_cookie, &m, &rule, &dst);
1c79356b
A
694 /*
695 * On return we must do the following:
9bccf70c 696 * IP_FW_PORT_DENY_FLAG -> drop the pkt (XXX new)
1c79356b 697 * 1<=off<= 0xffff -> DIVERT
9bccf70c
A
698 * (off & IP_FW_PORT_DYNT_FLAG) -> send to a DUMMYNET pipe
699 * (off & IP_FW_PORT_TEE_FLAG) -> TEE the packet
1c79356b
A
700 * dst != old -> IPFIREWALL_FORWARD
701 * off==0, dst==old -> accept
702 * If some of the above modules is not compiled in, then
703 * we should't have to check the corresponding condition
704 * (because the ipfw control socket should not accept
705 * unsupported rules), but better play safe and drop
706 * packets in case of doubt.
707 */
55e303ae 708 m0 = m;
9bccf70c
A
709 if ( (off & IP_FW_PORT_DENY_FLAG) || m == NULL) {
710 if (m)
711 m_freem(m);
712 error = EACCES ;
713 goto done ;
1c79356b 714 }
9bccf70c 715 ip = mtod(m, struct ip *);
1c79356b
A
716 if (off == 0 && dst == old) /* common case */
717 goto pass ;
718#if DUMMYNET
9bccf70c 719 if ((off & IP_FW_PORT_DYNT_FLAG) != 0) {
1c79356b
A
720 /*
721 * pass the pkt to dummynet. Need to include
9bccf70c 722 * pipe number, m, ifp, ro, dst because these are
1c79356b
A
723 * not recomputed in the next pass.
724 * All other parameters have been already used and
725 * so they are not needed anymore.
726 * XXX note: if the ifp or ro entry are deleted
727 * while a pkt is in dummynet, we are in trouble!
728 */
9bccf70c
A
729 error = dummynet_io(off & 0xffff, DN_TO_IP_OUT, m,
730 ifp,ro,dst,rule, flags);
731 goto done;
1c79356b
A
732 }
733#endif
734#if IPDIVERT
9bccf70c
A
735 if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) {
736 struct mbuf *clone = NULL;
737
738 /* Clone packet if we're doing a 'tee' */
739 if ((off & IP_FW_PORT_TEE_FLAG) != 0)
740 clone = m_dup(m, M_DONTWAIT);
741 /*
742 * XXX
743 * delayed checksums are not currently compatible
744 * with divert sockets.
745 */
746 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
747 in_delayed_cksum(m);
748 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
749 }
750
751 /* Restore packet header fields to original values */
752 HTONS(ip->ip_len);
753 HTONS(ip->ip_off);
754
755 /* Deliver packet to divert input routine */
756 ip_divert_cookie = divert_cookie;
757 divert_packet(m, 0, off & 0xffff);
758
759 /* If 'tee', continue with original packet */
760 if (clone != NULL) {
55e303ae 761 m0 = m = clone;
9bccf70c
A
762 ip = mtod(m, struct ip *);
763 goto pass;
764 }
1c79356b
A
765 goto done;
766 }
767#endif
768
769#if IPFIREWALL_FORWARD
770 /* Here we check dst to make sure it's directly reachable on the
771 * interface we previously thought it was.
772 * If it isn't (which may be likely in some situations) we have
773 * to re-route it (ie, find a route for the next-hop and the
774 * associated interface) and set them here. This is nested
775 * forwarding which in most cases is undesirable, except where
776 * such control is nigh impossible. So we do it here.
777 * And I'm babbling.
778 */
779 if (off == 0 && old != dst) {
780 struct in_ifaddr *ia;
781
782 /* It's changed... */
783 /* There must be a better way to do this next line... */
784 static struct route sro_fwd, *ro_fwd = &sro_fwd;
785#if IPFIREWALL_FORWARD_DEBUG
786 printf("IPFIREWALL_FORWARD: New dst ip: ");
787 print_ip(dst->sin_addr);
788 printf("\n");
789#endif
790 /*
791 * We need to figure out if we have been forwarded
792 * to a local socket. If so then we should somehow
793 * "loop back" to ip_input, and get directed to the
794 * PCB as if we had received this packet. This is
795 * because it may be dificult to identify the packets
796 * you want to forward until they are being output
797 * and have selected an interface. (e.g. locally
798 * initiated packets) If we used the loopback inteface,
799 * we would not be able to control what happens
800 * as the packet runs through ip_input() as
801 * it is done through a ISR.
802 */
9bccf70c 803 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
1c79356b
A
804 /*
805 * If the addr to forward to is one
806 * of ours, we pretend to
807 * be the destination for this packet.
808 */
809 if (IA_SIN(ia)->sin_addr.s_addr ==
810 dst->sin_addr.s_addr)
811 break;
812 }
813 if (ia) {
814 /* tell ip_input "dont filter" */
815 ip_fw_fwd_addr = dst;
816 if (m->m_pkthdr.rcvif == NULL)
817 m->m_pkthdr.rcvif = ifunit("lo0");
9bccf70c
A
818 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
819 m->m_pkthdr.csum_flags |=
820 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
55e303ae 821 m->m_pkthdr.csum_data = 0xffff;
9bccf70c
A
822 }
823 m->m_pkthdr.csum_flags |=
824 CSUM_IP_CHECKED | CSUM_IP_VALID;
825 HTONS(ip->ip_len);
826 HTONS(ip->ip_off);
1c79356b
A
827 ip_input(m);
828 goto done;
829 }
830 /* Some of the logic for this was
831 * nicked from above.
832 *
833 * This rewrites the cached route in a local PCB.
834 * Is this what we want to do?
835 */
836 bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst));
837
838 ro_fwd->ro_rt = 0;
839 rtalloc_ign(ro_fwd, RTF_PRCLONING);
840
841 if (ro_fwd->ro_rt == 0) {
842 ipstat.ips_noroute++;
843 error = EHOSTUNREACH;
844 goto bad;
845 }
846
847 ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
848 ifp = ro_fwd->ro_rt->rt_ifp;
1c79356b
A
849 ro_fwd->ro_rt->rt_use++;
850 if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
851 dst = (struct sockaddr_in *)ro_fwd->ro_rt->rt_gateway;
852 if (ro_fwd->ro_rt->rt_flags & RTF_HOST)
853 isbroadcast =
854 (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
855 else
856 isbroadcast = in_broadcast(dst->sin_addr, ifp);
9bccf70c 857 rtfree(ro->ro_rt);
1c79356b
A
858 ro->ro_rt = ro_fwd->ro_rt;
859 dst = (struct sockaddr_in *)&ro_fwd->ro_dst;
860
861 /*
862 * If we added a default src ip earlier,
863 * which would have been gotten from the-then
864 * interface, do it again, from the new one.
865 */
866 if (fwd_rewrite_src)
867 ip->ip_src = IA_SIN(ia)->sin_addr;
868 goto pass ;
869 }
870#endif /* IPFIREWALL_FORWARD */
871 /*
872 * if we get here, none of the above matches, and
873 * we have to drop the pkt
874 */
875 m_freem(m);
876 error = EACCES; /* not sure this is the right error msg */
877 goto done;
878 }
1c79356b
A
879
880pass:
e5568f75
A
881#if __APPLE__
882 /* Do not allow loopback address to wind up on a wire */
883 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
884 ((ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
885 (ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)) {
886 ipstat.ips_badaddr++;
887 m_freem(m);
888 /*
889 * Simply drop the packet just like a firewall -- we do not want the
890 * the application to feel the pain, not yet...
891 * Returning ENETUNREACH like ip6_output does in some similar cases
892 * could startle the otherwise clueless process that specifies
893 * loopback as the source address.
894 */
895 goto done;
896 }
897#endif
9bccf70c 898 m->m_pkthdr.csum_flags |= CSUM_IP;
4a249263
A
899 sw_csum = m->m_pkthdr.csum_flags
900 & ~IF_HWASSIST_CSUM_FLAGS(ifp->if_hwassist);
1c79356b 901
9bccf70c 902 if ((ifp->if_hwassist & CSUM_TCP_SUM16) != 0) {
1c79356b 903 /*
9bccf70c
A
904 * Special case code for GMACE
905 * frames that can be checksumed by GMACE SUM16 HW:
906 * frame >64, no fragments, no UDP
1c79356b 907 */
9bccf70c
A
908 if (apple_hwcksum_tx && (m->m_pkthdr.csum_flags & CSUM_TCP)
909 && (ip->ip_len > 50) && (ip->ip_len <= ifp->if_mtu)) {
910 /* Apple GMAC HW, expects STUFF_OFFSET << 16 | START_OFFSET */
911 u_short offset = (IP_VHL_HL(ip->ip_vhl) << 2) +14 ; /* IP+Enet header length */
912 u_short csumprev= m->m_pkthdr.csum_data & 0xFFFF;
913 m->m_pkthdr.csum_flags = CSUM_DATA_VALID | CSUM_TCP_SUM16; /* for GMAC */
914 m->m_pkthdr.csum_data = (csumprev + offset) << 16 ;
915 m->m_pkthdr.csum_data += offset;
916 sw_csum = CSUM_DELAY_IP; /* do IP hdr chksum in software */
1c79356b 917 }
9bccf70c
A
918 else {
919 /* let the software handle any UDP or TCP checksums */
920 sw_csum |= (CSUM_DELAY_DATA & m->m_pkthdr.csum_flags);
1c79356b 921 }
1c79356b 922 }
9bccf70c
A
923
924 if (sw_csum & CSUM_DELAY_DATA) {
925 in_delayed_cksum(m);
926 sw_csum &= ~CSUM_DELAY_DATA;
927 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
0b4e3aa0 928 }
9bccf70c 929
4a249263 930 m->m_pkthdr.csum_flags &= IF_HWASSIST_CSUM_FLAGS(ifp->if_hwassist);
0b4e3aa0 931
1c79356b 932 /*
9bccf70c 933 * If small enough for interface, or the interface will take
0b4e3aa0 934 * care of the fragmentation for us, can just send directly.
1c79356b 935 */
9bccf70c
A
936 if ((u_short)ip->ip_len <= ifp->if_mtu ||
937 ifp->if_hwassist & CSUM_FRAGMENT) {
938 HTONS(ip->ip_len);
939 HTONS(ip->ip_off);
1c79356b 940 ip->ip_sum = 0;
9bccf70c 941 if (sw_csum & CSUM_DELAY_IP) {
0b4e3aa0 942 ip->ip_sum = in_cksum(m, hlen);
9bccf70c
A
943 }
944
945#ifndef __APPLE__
946 /* Record statistics for this interface address. */
947 if (!(flags & IP_FORWARDING) && ia != NULL) {
948 ia->ia_ifa.if_opackets++;
949 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
950 }
951#endif
952
953#if IPSEC
954 /* clean ipsec history once it goes out of the node */
55e303ae 955 if (ipsec_bypass == 0 && (flags & IP_NOIPSEC) == 0)
9bccf70c
A
956 ipsec_delaux(m);
957#endif
958#if __APPLE__
55e303ae 959 error = dlil_output(ifptodlt(ifp, PF_INET), m, (void *) ro->ro_rt,
1c79356b 960 (struct sockaddr *)dst, 0);
9bccf70c
A
961#else
962 error = (*ifp->if_output)(ifp, m,
963 (struct sockaddr *)dst, ro->ro_rt);
964#endif
1c79356b
A
965 goto done;
966 }
967 /*
968 * Too large for interface; fragment if possible.
969 * Must be able to put at least 8 bytes per fragment.
970 */
971 if (ip->ip_off & IP_DF) {
972 error = EMSGSIZE;
973 /*
974 * This case can happen if the user changed the MTU
975 * of an interface after enabling IP on it. Because
976 * most netifs don't keep track of routes pointing to
977 * them, there is no way for one to update all its
978 * routes when the MTU is changed.
979 */
980 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST))
981 && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU)
982 && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
983 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
984 }
985 ipstat.ips_cantfrag++;
986 goto bad;
987 }
988 len = (ifp->if_mtu - hlen) &~ 7;
989 if (len < 8) {
990 error = EMSGSIZE;
991 goto bad;
992 }
993
9bccf70c
A
994 /*
995 * if the interface will not calculate checksums on
996 * fragmented packets, then do it here.
997 */
998 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
999 (ifp->if_hwassist & CSUM_IP_FRAGS) == 0) {
1000 in_delayed_cksum(m);
0b4e3aa0
A
1001 if (m == NULL)
1002 return(ENOMEM);
9bccf70c
A
1003 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1004 }
0b4e3aa0
A
1005
1006
1c79356b
A
1007 {
1008 int mhlen, firstlen = len;
1009 struct mbuf **mnext = &m->m_nextpkt;
9bccf70c 1010 int nfrags = 1;
1c79356b
A
1011
1012 /*
1013 * Loop through length of segment after first fragment,
1014 * make new header and copy data of each part and link onto chain.
1015 */
1016 m0 = m;
1017 mhlen = sizeof (struct ip);
1018 for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
1019 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1020 if (m == 0) {
1021 error = ENOBUFS;
1022 ipstat.ips_odropped++;
1023 goto sendorfree;
1024 }
0b4e3aa0 1025 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
1c79356b
A
1026 m->m_data += max_linkhdr;
1027 mhip = mtod(m, struct ip *);
1028 *mhip = *ip;
1029 if (hlen > sizeof (struct ip)) {
1030 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
1031 mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
1032 }
1033 m->m_len = mhlen;
1034 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
1035 if (ip->ip_off & IP_MF)
1036 mhip->ip_off |= IP_MF;
1037 if (off + len >= (u_short)ip->ip_len)
1038 len = (u_short)ip->ip_len - off;
1039 else
1040 mhip->ip_off |= IP_MF;
1041 mhip->ip_len = htons((u_short)(len + mhlen));
1042 m->m_next = m_copy(m0, off, len);
1043 if (m->m_next == 0) {
1044 (void) m_free(m);
1045 error = ENOBUFS; /* ??? */
1046 ipstat.ips_odropped++;
1047 goto sendorfree;
1048 }
1049 m->m_pkthdr.len = mhlen + len;
1050 m->m_pkthdr.rcvif = (struct ifnet *)0;
9bccf70c
A
1051 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
1052 HTONS(mhip->ip_off);
1c79356b 1053 mhip->ip_sum = 0;
9bccf70c 1054 if (sw_csum & CSUM_DELAY_IP) {
0b4e3aa0 1055 mhip->ip_sum = in_cksum(m, mhlen);
9bccf70c 1056 }
1c79356b
A
1057 *mnext = m;
1058 mnext = &m->m_nextpkt;
0b4e3aa0 1059 nfrags++;
1c79356b 1060 }
0b4e3aa0
A
1061 ipstat.ips_ofragments += nfrags;
1062
1063 /* set first/last markers for fragment chain */
9bccf70c
A
1064 m->m_flags |= M_LASTFRAG;
1065 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
0b4e3aa0
A
1066 m0->m_pkthdr.csum_data = nfrags;
1067
1c79356b
A
1068 /*
1069 * Update first fragment by trimming what's been copied out
1070 * and updating header, then send each fragment (in order).
1071 */
1072 m = m0;
1073 m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
1074 m->m_pkthdr.len = hlen + firstlen;
1075 ip->ip_len = htons((u_short)m->m_pkthdr.len);
9bccf70c
A
1076 ip->ip_off |= IP_MF;
1077 HTONS(ip->ip_off);
1c79356b 1078 ip->ip_sum = 0;
9bccf70c 1079 if (sw_csum & CSUM_DELAY_IP) {
0b4e3aa0 1080 ip->ip_sum = in_cksum(m, hlen);
9bccf70c 1081 }
1c79356b
A
1082sendorfree:
1083
1084 KERNEL_DEBUG(DBG_LAYER_END, ip->ip_dst.s_addr,
1085 ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len);
1086
1087 for (m = m0; m; m = m0) {
1088 m0 = m->m_nextpkt;
1089 m->m_nextpkt = 0;
9bccf70c
A
1090#if IPSEC
1091 /* clean ipsec history once it goes out of the node */
55e303ae 1092 if (ipsec_bypass == 0 && (flags & IP_NOIPSEC) == 0)
9bccf70c
A
1093 ipsec_delaux(m);
1094#endif
1095 if (error == 0) {
1096#ifndef __APPLE__
1097 /* Record statistics for this interface address. */
1098 if (ia != NULL) {
1099 ia->ia_ifa.if_opackets++;
1100 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1101 }
1102#endif
1103
1104#if __APPLE__
55e303ae 1105 error = dlil_output(ifptodlt(ifp, PF_INET), m, (void *) ro->ro_rt,
1c79356b 1106 (struct sockaddr *)dst, 0);
9bccf70c
A
1107#else
1108 error = (*ifp->if_output)(ifp, m,
1109 (struct sockaddr *)dst, ro->ro_rt);
1110#endif
1111 } else
1c79356b
A
1112 m_freem(m);
1113 }
1114
1115 if (error == 0)
1116 ipstat.ips_fragmented++;
1117 }
1118done:
1119#if IPSEC
55e303ae 1120 if (ipsec_bypass == 0 && (flags & IP_NOIPSEC) == 0) {
1c79356b 1121 if (ro == &iproute && ro->ro_rt) {
9bccf70c 1122 rtfree(ro->ro_rt);
1c79356b
A
1123 ro->ro_rt = NULL;
1124 }
1125 if (sp != NULL) {
1126 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1127 printf("DP ip_output call free SP:%x\n", sp));
1128 key_freesp(sp);
1129 }
9bccf70c 1130 }
1c79356b
A
1131#endif /* IPSEC */
1132
1133 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT | DBG_FUNC_END, error,0,0,0,0);
1134 return (error);
1135bad:
1136 m_freem(m0);
1137 goto done;
1138}
1139
0b4e3aa0
A
1140void
1141in_delayed_cksum(struct mbuf *m)
1142{
9bccf70c
A
1143 struct ip *ip;
1144 u_short csum, offset;
1145 ip = mtod(m, struct ip *);
1146 offset = IP_VHL_HL(ip->ip_vhl) << 2 ;
1147 csum = in_cksum_skip(m, ip->ip_len, offset);
1148 if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
0b4e3aa0 1149 csum = 0xffff;
9bccf70c 1150 offset += m->m_pkthdr.csum_data & 0xFFFF; /* checksum offset */
0b4e3aa0
A
1151
1152 if (offset > ip->ip_len) /* bogus offset */
1153 return;
1154
9bccf70c
A
1155 if (offset + sizeof(u_short) > m->m_len) {
1156 printf("delayed m_pullup, m->len: %d off: %d p: %d\n",
1157 m->m_len, offset, ip->ip_p);
1158 /*
1159 * XXX
1160 * this shouldn't happen, but if it does, the
1161 * correct behavior may be to insert the checksum
1162 * in the existing chain instead of rearranging it.
1163 */
1164 m = m_pullup(m, offset + sizeof(u_short));
1165 }
1166 *(u_short *)(m->m_data + offset) = csum;
0b4e3aa0
A
1167}
1168
1c79356b
A
1169/*
1170 * Insert IP options into preformed packet.
1171 * Adjust IP destination as required for IP source routing,
1172 * as indicated by a non-zero in_addr at the start of the options.
1173 *
1174 * XXX This routine assumes that the packet has no options in place.
1175 */
1176static struct mbuf *
1177ip_insertoptions(m, opt, phlen)
1178 register struct mbuf *m;
1179 struct mbuf *opt;
1180 int *phlen;
1181{
1182 register struct ipoption *p = mtod(opt, struct ipoption *);
1183 struct mbuf *n;
1184 register struct ip *ip = mtod(m, struct ip *);
1185 unsigned optlen;
1186
1187 optlen = opt->m_len - sizeof(p->ipopt_dst);
1188 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
1189 return (m); /* XXX should fail */
1190 if (p->ipopt_dst.s_addr)
1191 ip->ip_dst = p->ipopt_dst;
1192 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1193 MGETHDR(n, M_DONTWAIT, MT_HEADER);
1194 if (n == 0)
1195 return (m);
9bccf70c 1196 n->m_pkthdr.rcvif = (struct ifnet *)0;
1c79356b
A
1197 n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1198 m->m_len -= sizeof(struct ip);
1199 m->m_data += sizeof(struct ip);
1200 n->m_next = m;
1201 m = n;
1202 m->m_len = optlen + sizeof(struct ip);
1203 m->m_data += max_linkhdr;
1204 (void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
1205 } else {
1206 m->m_data -= optlen;
1207 m->m_len += optlen;
1208 m->m_pkthdr.len += optlen;
1209 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
1210 }
1211 ip = mtod(m, struct ip *);
1212 bcopy(p->ipopt_list, ip + 1, optlen);
1213 *phlen = sizeof(struct ip) + optlen;
1214 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
1215 ip->ip_len += optlen;
1216 return (m);
1217}
1218
1219/*
1220 * Copy options from ip to jp,
1221 * omitting those not copied during fragmentation.
1222 */
1c79356b
A
1223int
1224ip_optcopy(ip, jp)
1225 struct ip *ip, *jp;
1226{
1227 register u_char *cp, *dp;
1228 int opt, optlen, cnt;
1229
1230 cp = (u_char *)(ip + 1);
1231 dp = (u_char *)(jp + 1);
1232 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1233 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1234 opt = cp[0];
1235 if (opt == IPOPT_EOL)
1236 break;
1237 if (opt == IPOPT_NOP) {
1238 /* Preserve for IP mcast tunnel's LSRR alignment. */
1239 *dp++ = IPOPT_NOP;
1240 optlen = 1;
1241 continue;
9bccf70c
A
1242 }
1243#if DIAGNOSTIC
1244 if (cnt < IPOPT_OLEN + sizeof(*cp))
1245 panic("malformed IPv4 option passed to ip_optcopy");
1246#endif
1247 optlen = cp[IPOPT_OLEN];
1248#if DIAGNOSTIC
1249 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1250 panic("malformed IPv4 option passed to ip_optcopy");
1251#endif
1c79356b
A
1252 /* bogus lengths should have been caught by ip_dooptions */
1253 if (optlen > cnt)
1254 optlen = cnt;
1255 if (IPOPT_COPIED(opt)) {
1256 bcopy(cp, dp, optlen);
1257 dp += optlen;
1258 }
1259 }
1260 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1261 *dp++ = IPOPT_EOL;
1262 return (optlen);
1263}
1264
1265/*
1266 * IP socket option processing.
1267 */
1268int
1269ip_ctloutput(so, sopt)
1270 struct socket *so;
1271 struct sockopt *sopt;
1272{
1273 struct inpcb *inp = sotoinpcb(so);
1274 int error, optval;
1275
1276 error = optval = 0;
1277 if (sopt->sopt_level != IPPROTO_IP) {
1278 return (EINVAL);
1279 }
1280
1281 switch (sopt->sopt_dir) {
1282 case SOPT_SET:
1283 switch (sopt->sopt_name) {
1284 case IP_OPTIONS:
1285#ifdef notyet
1286 case IP_RETOPTS:
1287#endif
1288 {
1289 struct mbuf *m;
1290 if (sopt->sopt_valsize > MLEN) {
1291 error = EMSGSIZE;
1292 break;
1293 }
1294 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_HEADER);
1295 if (m == 0) {
1296 error = ENOBUFS;
1297 break;
1298 }
1299 m->m_len = sopt->sopt_valsize;
1300 error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1301 m->m_len);
1302 if (error)
1303 break;
1304
1305 return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
1306 m));
1307 }
1308
1309 case IP_TOS:
1310 case IP_TTL:
1311 case IP_RECVOPTS:
1312 case IP_RECVRETOPTS:
1313 case IP_RECVDSTADDR:
1314 case IP_RECVIF:
55e303ae 1315 case IP_RECVTTL:
9bccf70c 1316#if defined(NFAITH) && NFAITH > 0
1c79356b 1317 case IP_FAITH:
9bccf70c 1318#endif
1c79356b
A
1319 error = sooptcopyin(sopt, &optval, sizeof optval,
1320 sizeof optval);
1321 if (error)
1322 break;
1323
1324 switch (sopt->sopt_name) {
1325 case IP_TOS:
1326 inp->inp_ip_tos = optval;
1327 break;
1328
1329 case IP_TTL:
1330 inp->inp_ip_ttl = optval;
1331 break;
1332#define OPTSET(bit) \
1333 if (optval) \
1334 inp->inp_flags |= bit; \
1335 else \
1336 inp->inp_flags &= ~bit;
1337
1338 case IP_RECVOPTS:
1339 OPTSET(INP_RECVOPTS);
1340 break;
1341
1342 case IP_RECVRETOPTS:
1343 OPTSET(INP_RECVRETOPTS);
1344 break;
1345
1346 case IP_RECVDSTADDR:
1347 OPTSET(INP_RECVDSTADDR);
1348 break;
1349
1350 case IP_RECVIF:
1351 OPTSET(INP_RECVIF);
1352 break;
1353
55e303ae
A
1354 case IP_RECVTTL:
1355 OPTSET(INP_RECVTTL);
1356 break;
1357
9bccf70c 1358#if defined(NFAITH) && NFAITH > 0
1c79356b
A
1359 case IP_FAITH:
1360 OPTSET(INP_FAITH);
1361 break;
9bccf70c 1362#endif
1c79356b
A
1363 }
1364 break;
1365#undef OPTSET
1366
1367 case IP_MULTICAST_IF:
1368 case IP_MULTICAST_VIF:
1369 case IP_MULTICAST_TTL:
1370 case IP_MULTICAST_LOOP:
1371 case IP_ADD_MEMBERSHIP:
1372 case IP_DROP_MEMBERSHIP:
1373 error = ip_setmoptions(sopt, &inp->inp_moptions);
1374 break;
1375
1376 case IP_PORTRANGE:
1377 error = sooptcopyin(sopt, &optval, sizeof optval,
1378 sizeof optval);
1379 if (error)
1380 break;
1381
1382 switch (optval) {
1383 case IP_PORTRANGE_DEFAULT:
1384 inp->inp_flags &= ~(INP_LOWPORT);
1385 inp->inp_flags &= ~(INP_HIGHPORT);
1386 break;
1387
1388 case IP_PORTRANGE_HIGH:
1389 inp->inp_flags &= ~(INP_LOWPORT);
1390 inp->inp_flags |= INP_HIGHPORT;
1391 break;
1392
1393 case IP_PORTRANGE_LOW:
1394 inp->inp_flags &= ~(INP_HIGHPORT);
1395 inp->inp_flags |= INP_LOWPORT;
1396 break;
1397
1398 default:
1399 error = EINVAL;
1400 break;
1401 }
1402 break;
1403
1404#if IPSEC
1405 case IP_IPSEC_POLICY:
1406 {
1407 caddr_t req = NULL;
1408 size_t len = 0;
1409 int priv;
1410 struct mbuf *m;
1411 int optname;
1412
a3d08fcd
A
1413 if (sopt->sopt_valsize > MCLBYTES) {
1414 error = EMSGSIZE;
1415 break;
1416 }
1417
9bccf70c 1418 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1c79356b 1419 break;
9bccf70c 1420 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1c79356b
A
1421 break;
1422 priv = (sopt->sopt_p != NULL &&
1423 suser(sopt->sopt_p->p_ucred,
1424 &sopt->sopt_p->p_acflag) != 0) ? 0 : 1;
1425 if (m) {
1426 req = mtod(m, caddr_t);
1427 len = m->m_len;
1428 }
1429 optname = sopt->sopt_name;
1430 error = ipsec4_set_policy(inp, optname, req, len, priv);
1431 m_freem(m);
1432 break;
1433 }
1434#endif /*IPSEC*/
1435
1436 default:
1437 error = ENOPROTOOPT;
1438 break;
1439 }
1440 break;
1441
1442 case SOPT_GET:
1443 switch (sopt->sopt_name) {
1444 case IP_OPTIONS:
1445 case IP_RETOPTS:
1446 if (inp->inp_options)
1447 error = sooptcopyout(sopt,
1448 mtod(inp->inp_options,
1449 char *),
1450 inp->inp_options->m_len);
1451 else
1452 sopt->sopt_valsize = 0;
1453 break;
1454
1455 case IP_TOS:
1456 case IP_TTL:
1457 case IP_RECVOPTS:
1458 case IP_RECVRETOPTS:
1459 case IP_RECVDSTADDR:
1460 case IP_RECVIF:
55e303ae 1461 case IP_RECVTTL:
1c79356b 1462 case IP_PORTRANGE:
9bccf70c 1463#if defined(NFAITH) && NFAITH > 0
1c79356b 1464 case IP_FAITH:
9bccf70c 1465#endif
1c79356b
A
1466 switch (sopt->sopt_name) {
1467
1468 case IP_TOS:
1469 optval = inp->inp_ip_tos;
1470 break;
1471
1472 case IP_TTL:
1473 optval = inp->inp_ip_ttl;
1474 break;
1475
1476#define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1477
1478 case IP_RECVOPTS:
1479 optval = OPTBIT(INP_RECVOPTS);
1480 break;
1481
1482 case IP_RECVRETOPTS:
1483 optval = OPTBIT(INP_RECVRETOPTS);
1484 break;
1485
1486 case IP_RECVDSTADDR:
1487 optval = OPTBIT(INP_RECVDSTADDR);
1488 break;
1489
1490 case IP_RECVIF:
1491 optval = OPTBIT(INP_RECVIF);
1492 break;
1493
55e303ae
A
1494 case IP_RECVTTL:
1495 optval = OPTBIT(INP_RECVTTL);
1496 break;
1497
1c79356b
A
1498 case IP_PORTRANGE:
1499 if (inp->inp_flags & INP_HIGHPORT)
1500 optval = IP_PORTRANGE_HIGH;
1501 else if (inp->inp_flags & INP_LOWPORT)
1502 optval = IP_PORTRANGE_LOW;
1503 else
1504 optval = 0;
1505 break;
1506
9bccf70c 1507#if defined(NFAITH) && NFAITH > 0
1c79356b
A
1508 case IP_FAITH:
1509 optval = OPTBIT(INP_FAITH);
1510 break;
9bccf70c 1511#endif
1c79356b
A
1512 }
1513 error = sooptcopyout(sopt, &optval, sizeof optval);
1514 break;
1515
1516 case IP_MULTICAST_IF:
1517 case IP_MULTICAST_VIF:
1518 case IP_MULTICAST_TTL:
1519 case IP_MULTICAST_LOOP:
1520 case IP_ADD_MEMBERSHIP:
1521 case IP_DROP_MEMBERSHIP:
1522 error = ip_getmoptions(sopt, inp->inp_moptions);
1523 break;
1524
1525#if IPSEC
1526 case IP_IPSEC_POLICY:
1527 {
1528 struct mbuf *m = NULL;
1c79356b 1529 caddr_t req = NULL;
9bccf70c 1530 size_t len = 0;
1c79356b 1531
9bccf70c 1532 if (m != 0) {
1c79356b
A
1533 req = mtod(m, caddr_t);
1534 len = m->m_len;
1535 }
1c79356b
A
1536 error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
1537 if (error == 0)
9bccf70c 1538 error = soopt_mcopyout(sopt, m); /* XXX */
1c79356b
A
1539 if (error == 0)
1540 m_freem(m);
1541 break;
1542 }
1543#endif /*IPSEC*/
1544
1545 default:
1546 error = ENOPROTOOPT;
1547 break;
1548 }
1549 break;
1550 }
1551 return (error);
1552}
1553
1554/*
1555 * Set up IP options in pcb for insertion in output packets.
1556 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1557 * with destination address if source routed.
1558 */
1559static int
1560ip_pcbopts(optname, pcbopt, m)
1561 int optname;
1562 struct mbuf **pcbopt;
1563 register struct mbuf *m;
1564{
1565 register int cnt, optlen;
1566 register u_char *cp;
1567 u_char opt;
1568
1569 /* turn off any old options */
1570 if (*pcbopt)
1571 (void)m_free(*pcbopt);
1572 *pcbopt = 0;
1573 if (m == (struct mbuf *)0 || m->m_len == 0) {
1574 /*
1575 * Only turning off any previous options.
1576 */
1577 if (m)
1578 (void)m_free(m);
1579 return (0);
1580 }
1581
1582#ifndef vax
1583 if (m->m_len % sizeof(int32_t))
1584 goto bad;
1585#endif
1586 /*
1587 * IP first-hop destination address will be stored before
1588 * actual options; move other options back
1589 * and clear it when none present.
1590 */
1591 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1592 goto bad;
1593 cnt = m->m_len;
1594 m->m_len += sizeof(struct in_addr);
1595 cp = mtod(m, u_char *) + sizeof(struct in_addr);
1596 ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
1597 bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1598
1599 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1600 opt = cp[IPOPT_OPTVAL];
1601 if (opt == IPOPT_EOL)
1602 break;
1603 if (opt == IPOPT_NOP)
1604 optlen = 1;
1605 else {
1606 if (cnt < IPOPT_OLEN + sizeof(*cp))
1607 goto bad;
1608 optlen = cp[IPOPT_OLEN];
1609 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1610 goto bad;
1611 }
1612 switch (opt) {
1613
1614 default:
1615 break;
1616
1617 case IPOPT_LSRR:
1618 case IPOPT_SSRR:
1619 /*
1620 * user process specifies route as:
1621 * ->A->B->C->D
1622 * D must be our final destination (but we can't
1623 * check that since we may not have connected yet).
1624 * A is first hop destination, which doesn't appear in
1625 * actual IP option, but is stored before the options.
1626 */
1627 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1628 goto bad;
1629 m->m_len -= sizeof(struct in_addr);
1630 cnt -= sizeof(struct in_addr);
1631 optlen -= sizeof(struct in_addr);
1632 cp[IPOPT_OLEN] = optlen;
1633 /*
1634 * Move first hop before start of options.
1635 */
1636 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1637 sizeof(struct in_addr));
1638 /*
1639 * Then copy rest of options back
1640 * to close up the deleted entry.
1641 */
1642 ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
1643 sizeof(struct in_addr)),
1644 (caddr_t)&cp[IPOPT_OFFSET+1],
1645 (unsigned)cnt + sizeof(struct in_addr));
1646 break;
1647 }
1648 }
1649 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1650 goto bad;
1651 *pcbopt = m;
1652 return (0);
1653
1654bad:
1655 (void)m_free(m);
1656 return (EINVAL);
1657}
1658
1659/*
1660 * XXX
1661 * The whole multicast option thing needs to be re-thought.
1662 * Several of these options are equally applicable to non-multicast
1663 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1664 * standard option (IP_TTL).
1665 */
9bccf70c
A
1666
1667/*
1668 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1669 */
1670static struct ifnet *
1671ip_multicast_if(a, ifindexp)
1672 struct in_addr *a;
1673 int *ifindexp;
1674{
1675 int ifindex;
1676 struct ifnet *ifp;
1677
1678 if (ifindexp)
1679 *ifindexp = 0;
1680 if (ntohl(a->s_addr) >> 24 == 0) {
1681 ifindex = ntohl(a->s_addr) & 0xffffff;
1682 if (ifindex < 0 || if_index < ifindex)
1683 return NULL;
1684 ifp = ifindex2ifnet[ifindex];
1685 if (ifindexp)
1686 *ifindexp = ifindex;
1687 } else {
1688 INADDR_TO_IFP(*a, ifp);
1689 }
1690 return ifp;
1691}
1692
1c79356b
A
1693/*
1694 * Set the IP multicast options in response to user setsockopt().
1695 */
1696static int
1697ip_setmoptions(sopt, imop)
1698 struct sockopt *sopt;
1699 struct ip_moptions **imop;
1700{
1701 int error = 0;
1702 int i;
1703 struct in_addr addr;
1704 struct ip_mreq mreq;
9bccf70c 1705 struct ifnet *ifp = NULL;
1c79356b 1706 struct ip_moptions *imo = *imop;
9bccf70c 1707 int ifindex;
1c79356b
A
1708 int s;
1709
1710 if (imo == NULL) {
1711 /*
1712 * No multicast option buffer attached to the pcb;
1713 * allocate one and initialize to default values.
1714 */
55e303ae
A
1715 error = ip_createmoptions(imop);
1716 if (error != 0)
1717 return error;
1718 imo = *imop;
1c79356b
A
1719 }
1720
1721 switch (sopt->sopt_name) {
1722 /* store an index number for the vif you wanna use in the send */
1723 case IP_MULTICAST_VIF:
1724 if (legal_vif_num == 0) {
1725 error = EOPNOTSUPP;
1726 break;
1727 }
1728 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1729 if (error)
1730 break;
1731 if (!legal_vif_num(i) && (i != -1)) {
1732 error = EINVAL;
1733 break;
1734 }
1735 imo->imo_multicast_vif = i;
1736 break;
1737
1738 case IP_MULTICAST_IF:
1739 /*
1740 * Select the interface for outgoing multicast packets.
1741 */
1742 error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1743 if (error)
1744 break;
1745 /*
1746 * INADDR_ANY is used to remove a previous selection.
1747 * When no interface is selected, a default one is
1748 * chosen every time a multicast packet is sent.
1749 */
1750 if (addr.s_addr == INADDR_ANY) {
1751 imo->imo_multicast_ifp = NULL;
1752 break;
1753 }
1754 /*
1755 * The selected interface is identified by its local
1756 * IP address. Find the interface and confirm that
1757 * it supports multicasting.
1758 */
1759 s = splimp();
9bccf70c 1760 ifp = ip_multicast_if(&addr, &ifindex);
1c79356b
A
1761 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1762 splx(s);
1763 error = EADDRNOTAVAIL;
1764 break;
1765 }
1766 imo->imo_multicast_ifp = ifp;
9bccf70c
A
1767 if (ifindex)
1768 imo->imo_multicast_addr = addr;
1769 else
1770 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1c79356b
A
1771 splx(s);
1772 break;
1773
1774 case IP_MULTICAST_TTL:
1775 /*
1776 * Set the IP time-to-live for outgoing multicast packets.
1777 * The original multicast API required a char argument,
1778 * which is inconsistent with the rest of the socket API.
1779 * We allow either a char or an int.
1780 */
1781 if (sopt->sopt_valsize == 1) {
1782 u_char ttl;
1783 error = sooptcopyin(sopt, &ttl, 1, 1);
1784 if (error)
1785 break;
1786 imo->imo_multicast_ttl = ttl;
1787 } else {
1788 u_int ttl;
1789 error = sooptcopyin(sopt, &ttl, sizeof ttl,
1790 sizeof ttl);
1791 if (error)
1792 break;
1793 if (ttl > 255)
1794 error = EINVAL;
1795 else
1796 imo->imo_multicast_ttl = ttl;
1797 }
1798 break;
1799
1800 case IP_MULTICAST_LOOP:
1801 /*
1802 * Set the loopback flag for outgoing multicast packets.
1803 * Must be zero or one. The original multicast API required a
1804 * char argument, which is inconsistent with the rest
1805 * of the socket API. We allow either a char or an int.
1806 */
1807 if (sopt->sopt_valsize == 1) {
1808 u_char loop;
1809 error = sooptcopyin(sopt, &loop, 1, 1);
1810 if (error)
1811 break;
1812 imo->imo_multicast_loop = !!loop;
1813 } else {
1814 u_int loop;
1815 error = sooptcopyin(sopt, &loop, sizeof loop,
1816 sizeof loop);
1817 if (error)
1818 break;
1819 imo->imo_multicast_loop = !!loop;
1820 }
1821 break;
1822
1823 case IP_ADD_MEMBERSHIP:
1824 /*
1825 * Add a multicast group membership.
1826 * Group must be a valid IP multicast address.
1827 */
1828 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1829 if (error)
1830 break;
9bccf70c 1831
55e303ae 1832 error = ip_addmembership(imo, &mreq);
1c79356b
A
1833 break;
1834
1835 case IP_DROP_MEMBERSHIP:
1836 /*
1837 * Drop a multicast group membership.
1838 * Group must be a valid IP multicast address.
1839 */
1840 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1841 if (error)
1842 break;
55e303ae
A
1843
1844 error = ip_dropmembership(imo, &mreq);
1c79356b
A
1845 break;
1846
1847 default:
1848 error = EOPNOTSUPP;
1849 break;
1850 }
1851
1852 /*
1853 * If all options have default values, no need to keep the mbuf.
1854 */
1855 if (imo->imo_multicast_ifp == NULL &&
1856 imo->imo_multicast_vif == -1 &&
1857 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1858 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1859 imo->imo_num_memberships == 0) {
1860 FREE(*imop, M_IPMOPTS);
1861 *imop = NULL;
1862 }
1863
1864 return (error);
1865}
1866
55e303ae
A
1867/*
1868 * Set the IP multicast options in response to user setsockopt().
1869 */
1870__private_extern__ int
1871ip_createmoptions(
1872 struct ip_moptions **imop)
1873{
1874 struct ip_moptions *imo;
1875 imo = (struct ip_moptions*) _MALLOC(sizeof(*imo), M_IPMOPTS,
1876 M_WAITOK);
1877
1878 if (imo == NULL)
1879 return (ENOBUFS);
1880 *imop = imo;
1881 imo->imo_multicast_ifp = NULL;
1882 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1883 imo->imo_multicast_vif = -1;
1884 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1885 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1886 imo->imo_num_memberships = 0;
1887
1888 return 0;
1889}
1890
1891/*
1892 * Add membership to an IPv4 multicast.
1893 */
1894__private_extern__ int
1895ip_addmembership(
1896 struct ip_moptions *imo,
1897 struct ip_mreq *mreq)
1898{
1899 struct route ro;
1900 struct sockaddr_in *dst;
1901 struct ifnet *ifp = NULL;
1902 int error = 0;
1903 int s = 0;
1904 int i;
1905
1906 if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
1907 error = EINVAL;
1908 return error;
1909 }
1910 s = splimp();
1911 /*
1912 * If no interface address was provided, use the interface of
1913 * the route to the given multicast address.
1914 */
1915 if (mreq->imr_interface.s_addr == INADDR_ANY) {
1916 bzero((caddr_t)&ro, sizeof(ro));
1917 dst = (struct sockaddr_in *)&ro.ro_dst;
1918 dst->sin_len = sizeof(*dst);
1919 dst->sin_family = AF_INET;
1920 dst->sin_addr = mreq->imr_multiaddr;
1921 rtalloc(&ro);
1922 if (ro.ro_rt != NULL) {
1923 ifp = ro.ro_rt->rt_ifp;
1924 rtfree(ro.ro_rt);
1925 }
1926 else {
1927 /* If there's no default route, try using loopback */
1928 mreq->imr_interface.s_addr = INADDR_LOOPBACK;
1929 }
1930 }
1931
1932 if (ifp == NULL) {
1933 ifp = ip_multicast_if(&mreq->imr_interface, NULL);
1934 }
1935
1936 /*
1937 * See if we found an interface, and confirm that it
1938 * supports multicast.
1939 */
1940 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1941 error = EADDRNOTAVAIL;
1942 splx(s);
1943 return error;
1944 }
1945 /*
1946 * See if the membership already exists or if all the
1947 * membership slots are full.
1948 */
1949 for (i = 0; i < imo->imo_num_memberships; ++i) {
1950 if (imo->imo_membership[i]->inm_ifp == ifp &&
1951 imo->imo_membership[i]->inm_addr.s_addr
1952 == mreq->imr_multiaddr.s_addr)
1953 break;
1954 }
1955 if (i < imo->imo_num_memberships) {
1956 error = EADDRINUSE;
1957 splx(s);
1958 return error;
1959 }
1960 if (i == IP_MAX_MEMBERSHIPS) {
1961 error = ETOOMANYREFS;
1962 splx(s);
1963 return error;
1964 }
1965 /*
1966 * Everything looks good; add a new record to the multicast
1967 * address list for the given interface.
1968 */
1969 if ((imo->imo_membership[i] =
1970 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1971 error = ENOBUFS;
1972 splx(s);
1973 return error;
1974 }
1975 ++imo->imo_num_memberships;
1976 splx(s);
1977
1978 return error;
1979}
1980
1981/*
1982 * Drop membership of an IPv4 multicast.
1983 */
1984__private_extern__ int
1985ip_dropmembership(
1986 struct ip_moptions *imo,
1987 struct ip_mreq *mreq)
1988{
1989 int error = 0;
1990 int s = 0;
1991 struct ifnet* ifp = NULL;
1992 int i;
1993
1994 if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
1995 error = EINVAL;
1996 return error;
1997 }
1998
1999 s = splimp();
2000 /*
2001 * If an interface address was specified, get a pointer
2002 * to its ifnet structure.
2003 */
2004 if (mreq->imr_interface.s_addr == INADDR_ANY)
2005 ifp = NULL;
2006 else {
2007 ifp = ip_multicast_if(&mreq->imr_interface, NULL);
2008 if (ifp == NULL) {
2009 error = EADDRNOTAVAIL;
2010 splx(s);
2011 return error;
2012 }
2013 }
2014 /*
2015 * Find the membership in the membership array.
2016 */
2017 for (i = 0; i < imo->imo_num_memberships; ++i) {
2018 if ((ifp == NULL ||
2019 imo->imo_membership[i]->inm_ifp == ifp) &&
2020 imo->imo_membership[i]->inm_addr.s_addr ==
2021 mreq->imr_multiaddr.s_addr)
2022 break;
2023 }
2024 if (i == imo->imo_num_memberships) {
2025 error = EADDRNOTAVAIL;
2026 splx(s);
2027 return error;
2028 }
2029 /*
2030 * Give up the multicast address record to which the
2031 * membership points.
2032 */
2033 in_delmulti(imo->imo_membership[i]);
2034 /*
2035 * Remove the gap in the membership array.
2036 */
2037 for (++i; i < imo->imo_num_memberships; ++i)
2038 imo->imo_membership[i-1] = imo->imo_membership[i];
2039 --imo->imo_num_memberships;
2040 splx(s);
2041
2042 return error;
2043}
2044
1c79356b
A
2045/*
2046 * Return the IP multicast options in response to user getsockopt().
2047 */
2048static int
2049ip_getmoptions(sopt, imo)
2050 struct sockopt *sopt;
2051 register struct ip_moptions *imo;
2052{
2053 struct in_addr addr;
2054 struct in_ifaddr *ia;
2055 int error, optval;
2056 u_char coptval;
2057
2058 error = 0;
2059 switch (sopt->sopt_name) {
2060 case IP_MULTICAST_VIF:
2061 if (imo != NULL)
2062 optval = imo->imo_multicast_vif;
2063 else
2064 optval = -1;
2065 error = sooptcopyout(sopt, &optval, sizeof optval);
2066 break;
2067
2068 case IP_MULTICAST_IF:
2069 if (imo == NULL || imo->imo_multicast_ifp == NULL)
2070 addr.s_addr = INADDR_ANY;
9bccf70c
A
2071 else if (imo->imo_multicast_addr.s_addr) {
2072 /* return the value user has set */
2073 addr = imo->imo_multicast_addr;
2074 } else {
1c79356b
A
2075 IFP_TO_IA(imo->imo_multicast_ifp, ia);
2076 addr.s_addr = (ia == NULL) ? INADDR_ANY
2077 : IA_SIN(ia)->sin_addr.s_addr;
2078 }
2079 error = sooptcopyout(sopt, &addr, sizeof addr);
2080 break;
2081
2082 case IP_MULTICAST_TTL:
2083 if (imo == 0)
2084 optval = coptval = IP_DEFAULT_MULTICAST_TTL;
2085 else
2086 optval = coptval = imo->imo_multicast_ttl;
2087 if (sopt->sopt_valsize == 1)
2088 error = sooptcopyout(sopt, &coptval, 1);
2089 else
2090 error = sooptcopyout(sopt, &optval, sizeof optval);
2091 break;
2092
2093 case IP_MULTICAST_LOOP:
2094 if (imo == 0)
2095 optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
2096 else
2097 optval = coptval = imo->imo_multicast_loop;
2098 if (sopt->sopt_valsize == 1)
2099 error = sooptcopyout(sopt, &coptval, 1);
2100 else
2101 error = sooptcopyout(sopt, &optval, sizeof optval);
2102 break;
2103
2104 default:
2105 error = ENOPROTOOPT;
2106 break;
2107 }
2108 return (error);
2109}
2110
2111/*
2112 * Discard the IP multicast options.
2113 */
2114void
2115ip_freemoptions(imo)
2116 register struct ip_moptions *imo;
2117{
2118 register int i;
2119
2120 if (imo != NULL) {
2121 for (i = 0; i < imo->imo_num_memberships; ++i)
9bccf70c
A
2122 if (imo->imo_membership[i] != NULL)
2123 in_delmulti(imo->imo_membership[i]);
1c79356b
A
2124 FREE(imo, M_IPMOPTS);
2125 }
2126}
2127
2128/*
2129 * Routine called from ip_output() to loop back a copy of an IP multicast
2130 * packet to the input queue of a specified interface. Note that this
2131 * calls the output routine of the loopback "driver", but with an interface
2132 * pointer that might NOT be a loopback interface -- evil, but easier than
2133 * replicating that code here.
2134 */
2135static void
2136ip_mloopback(ifp, m, dst, hlen)
2137 struct ifnet *ifp;
2138 register struct mbuf *m;
2139 register struct sockaddr_in *dst;
2140 int hlen;
2141{
2142 register struct ip *ip;
2143 struct mbuf *copym;
2144
2145 copym = m_copy(m, 0, M_COPYALL);
2146 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
2147 copym = m_pullup(copym, hlen);
2148 if (copym != NULL) {
2149 /*
2150 * We don't bother to fragment if the IP length is greater
2151 * than the interface's MTU. Can this possibly matter?
2152 */
2153 ip = mtod(copym, struct ip *);
9bccf70c
A
2154 HTONS(ip->ip_len);
2155 HTONS(ip->ip_off);
1c79356b
A
2156 ip->ip_sum = 0;
2157 ip->ip_sum = in_cksum(copym, hlen);
1c79356b
A
2158 /*
2159 * NB:
2160 * It's not clear whether there are any lingering
2161 * reentrancy problems in other areas which might
2162 * be exposed by using ip_input directly (in
2163 * particular, everything which modifies the packet
2164 * in-place). Yet another option is using the
2165 * protosw directly to deliver the looped back
2166 * packet. For the moment, we'll err on the side
2167 * of safety by using if_simloop().
2168 */
2169#if 1 /* XXX */
2170 if (dst->sin_family != AF_INET) {
2171 printf("ip_mloopback: bad address family %d\n",
2172 dst->sin_family);
2173 dst->sin_family = AF_INET;
2174 }
2175#endif
2176
0b4e3aa0 2177
9bccf70c
A
2178 /*
2179 * Mark checksum as valid or calculate checksum for loopback.
2180 *
2181 * This is done this way because we have to embed the ifp of
2182 * the interface we will send the original copy of the packet
2183 * out on in the mbuf. ip_input will check if_hwassist of the
2184 * embedded ifp and ignore all csum_flags if if_hwassist is 0.
2185 * The UDP checksum has not been calculated yet.
2186 */
2187 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
4a249263 2188 if (IF_HWASSIST_CSUM_FLAGS(ifp->if_hwassist)) {
9bccf70c
A
2189 copym->m_pkthdr.csum_flags |=
2190 CSUM_DATA_VALID | CSUM_PSEUDO_HDR |
2191 CSUM_IP_CHECKED | CSUM_IP_VALID;
2192 copym->m_pkthdr.csum_data = 0xffff;
2193 } else {
2194 NTOHS(ip->ip_len);
2195 in_delayed_cksum(copym);
2196 HTONS(ip->ip_len);
2197 }
2198 }
0b4e3aa0
A
2199
2200
1c79356b
A
2201 /*
2202 * TedW:
2203 * We need to send all loopback traffic down to dlil in case
2204 * a filter has tapped-in.
2205 */
2206
2207 if (lo_dl_tag == 0)
2208 dlil_find_dltag(APPLE_IF_FAM_LOOPBACK, 0, PF_INET, &lo_dl_tag);
2209
2210 /*
2211 * Stuff the 'real' ifp into the pkthdr, to be used in matching
2212 * in ip_input(); we need the loopback ifp/dl_tag passed as args
2213 * to make the loopback driver compliant with the data link
2214 * requirements.
2215 */
9bccf70c
A
2216 if (lo_dl_tag) {
2217 copym->m_pkthdr.rcvif = ifp;
1c79356b
A
2218 dlil_output(lo_dl_tag, copym, 0, (struct sockaddr *) dst, 0);
2219 } else {
2220 printf("Warning: ip_output call to dlil_find_dltag failed!\n");
2221 m_freem(copym);
2222 }
2223
2224/* if_simloop(ifp, copym, (struct sockaddr *)dst, 0);*/
2225 }
2226}