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