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