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