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