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