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