1 /*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
30 * $FreeBSD: releng/5.3/sys/netinet/ip_output.c 136588 2004-10-16 08:43:07Z cvs2svn $
31 */
32
33 #include "opt_ipfw.h"
34 #include "opt_ipsec.h"
35 #include "opt_mac.h"
36 #include "opt_mbuf_stress_test.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/mac.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/sysctl.h>
48
49 #include <net/if.h>
50 #include <net/netisr.h>
51 #include <net/pfil.h>
52 #include <net/route.h>
53
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip_var.h>
60
61 #include <machine/in_cksum.h>
62
63 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
64
65 #ifdef IPSEC
66 #include <netinet6/ipsec.h>
67 #include <netkey/key.h>
68 #ifdef IPSEC_DEBUG
69 #include <netkey/key_debug.h>
70 #else
71 #define KEYDEBUG(lev,arg)
72 #endif
73 #endif /*IPSEC*/
74
75 #ifdef FAST_IPSEC
76 #include <netipsec/ipsec.h>
77 #include <netipsec/xform.h>
78 #include <netipsec/key.h>
79 #endif /*FAST_IPSEC*/
80
81 #define print_ip(x, a, y) printf("%s %d.%d.%d.%d%s",\
82 x, (ntohl(a.s_addr)>>24)&0xFF,\
83 (ntohl(a.s_addr)>>16)&0xFF,\
84 (ntohl(a.s_addr)>>8)&0xFF,\
85 (ntohl(a.s_addr))&0xFF, y);
86
87 u_short ip_id;
88
89 #ifdef MBUF_STRESS_TEST
90 int mbuf_frag_size = 0;
91 SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
92 &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
93 #endif
94
95 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
96 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
97 static void ip_mloopback
98 (struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
99 static int ip_getmoptions
100 (struct sockopt *, struct ip_moptions *);
101 static int ip_pcbopts(int, struct mbuf **, struct mbuf *);
102 static int ip_setmoptions
103 (struct sockopt *, struct ip_moptions **);
104
105 int ip_optcopy(struct ip *, struct ip *);
106
107
108 extern struct protosw inetsw[];
109
110 /*
111 * IP output. The packet in mbuf chain m contains a skeletal IP
112 * header (with len, off, ttl, proto, tos, src, dst).
113 * The mbuf chain containing the packet will be freed.
114 * The mbuf opt, if present, will not be freed.
115 * In the IP forwarding case, the packet will arrive with options already
116 * inserted, so must have a NULL opt pointer.
117 */
118 int
119 ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro,
120 int flags, struct ip_moptions *imo, struct inpcb *inp)
121 {
122 struct ip *ip;
123 struct ifnet *ifp = NULL; /* keep compiler happy */
124 struct mbuf *m0;
125 int hlen = sizeof (struct ip);
126 int len, error = 0;
127 struct sockaddr_in *dst = NULL; /* keep compiler happy */
128 struct in_ifaddr *ia = NULL;
129 int isbroadcast, sw_csum;
130 struct route iproute;
131 struct in_addr odst;
132 #ifdef IPFIREWALL_FORWARD
133 struct m_tag *fwd_tag = NULL;
134 #endif
135 #ifdef IPSEC
136 struct secpolicy *sp = NULL;
137 #endif
138 #ifdef FAST_IPSEC
139 struct secpolicy *sp = NULL;
140 struct tdb_ident *tdbi;
141 struct m_tag *mtag;
142 int s;
143 #endif /* FAST_IPSEC */
144
145 M_ASSERTPKTHDR(m);
146
147 if (ro == NULL) {
148 ro = &iproute;
149 bzero(ro, sizeof (*ro));
150 }
151
152 if (inp != NULL)
153 INP_LOCK_ASSERT(inp);
154
155 if (opt) {
156 len = 0;
157 m = ip_insertoptions(m, opt, &len);
158 if (len != 0)
159 hlen = len;
160 }
161 ip = mtod(m, struct ip *);
162
163 /*
164 * Fill in IP header. If we are not allowing fragmentation,
165 * then the ip_id field is meaningless, but we don't set it
166 * to zero. Doing so causes various problems when devices along
167 * the path (routers, load balancers, firewalls, etc.) illegally
168 * disable DF on our packet. Note that a 16-bit counter
169 * will wrap around in less than 10 seconds at 100 Mbit/s on a
170 * medium with MTU 1500. See Steven M. Bellovin, "A Technique
171 * for Counting NATted Hosts", Proc. IMW'02, available at
172 * <http://www.research.att.com/~smb/papers/fnat.pdf>.
173 */
174 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
175 ip->ip_v = IPVERSION;
176 ip->ip_hl = hlen >> 2;
177 ip->ip_id = ip_newid();
178 ipstat.ips_localout++;
179 } else {
180 hlen = ip->ip_hl << 2;
181 }
182
183 dst = (struct sockaddr_in *)&ro->ro_dst;
184 again:
185 /*
186 * If there is a cached route,
187 * check that it is to the same destination
188 * and is still up. If not, free it and try again.
189 * The address family should also be checked in case of sharing the
190 * cache with IPv6.
191 */
192 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
193 dst->sin_family != AF_INET ||
194 dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
195 RTFREE(ro->ro_rt);
196 ro->ro_rt = (struct rtentry *)0;
197 }
198 #ifdef IPFIREWALL_FORWARD
199 if (ro->ro_rt == NULL && fwd_tag == NULL) {
200 #else
201 if (ro->ro_rt == NULL) {
202 #endif
203 bzero(dst, sizeof(*dst));
204 dst->sin_family = AF_INET;
205 dst->sin_len = sizeof(*dst);
206 dst->sin_addr = ip->ip_dst;
207 }
208 /*
209 * If routing to interface only,
210 * short circuit routing lookup.
211 */
212 if (flags & IP_ROUTETOIF) {
213 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
214 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
215 ipstat.ips_noroute++;
216 error = ENETUNREACH;
217 goto bad;
218 }
219 ifp = ia->ia_ifp;
220 ip->ip_ttl = 1;
221 isbroadcast = in_broadcast(dst->sin_addr, ifp);
222 } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
223 imo != NULL && imo->imo_multicast_ifp != NULL) {
224 /*
225 * Bypass the normal routing lookup for multicast
226 * packets if the interface is specified.
227 */
228 ifp = imo->imo_multicast_ifp;
229 IFP_TO_IA(ifp, ia);
230 isbroadcast = 0; /* fool gcc */
231 } else {
232 /*
233 * We want to do any cloning requested by the link layer,
234 * as this is probably required in all cases for correct
235 * operation (as it is for ARP).
236 */
237 if (ro->ro_rt == NULL)
238 rtalloc_ign(ro, 0);
239 if (ro->ro_rt == NULL) {
240 ipstat.ips_noroute++;
241 error = EHOSTUNREACH;
242 goto bad;
243 }
244 ia = ifatoia(ro->ro_rt->rt_ifa);
245 ifp = ro->ro_rt->rt_ifp;
246 ro->ro_rt->rt_rmx.rmx_pksent++;
247 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
248 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
249 if (ro->ro_rt->rt_flags & RTF_HOST)
250 isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
251 else
252 isbroadcast = in_broadcast(dst->sin_addr, ifp);
253 }
254 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
255 struct in_multi *inm;
256
257 m->m_flags |= M_MCAST;
258 /*
259 * IP destination address is multicast. Make sure "dst"
260 * still points to the address in "ro". (It may have been
261 * changed to point to a gateway address, above.)
262 */
263 dst = (struct sockaddr_in *)&ro->ro_dst;
264 /*
265 * See if the caller provided any multicast options
266 */
267 if (imo != NULL) {
268 ip->ip_ttl = imo->imo_multicast_ttl;
269 if (imo->imo_multicast_vif != -1)
270 ip->ip_src.s_addr =
271 ip_mcast_src ?
272 ip_mcast_src(imo->imo_multicast_vif) :
273 INADDR_ANY;
274 } else
275 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
276 /*
277 * Confirm that the outgoing interface supports multicast.
278 */
279 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
280 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
281 ipstat.ips_noroute++;
282 error = ENETUNREACH;
283 goto bad;
284 }
285 }
286 /*
287 * If source address not specified yet, use address
288 * of outgoing interface.
289 */
290 if (ip->ip_src.s_addr == INADDR_ANY) {
291 /* Interface may have no addresses. */
292 if (ia != NULL)
293 ip->ip_src = IA_SIN(ia)->sin_addr;
294 }
295
296 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
297 if (inm != NULL &&
298 (imo == NULL || imo->imo_multicast_loop)) {
299 /*
300 * If we belong to the destination multicast group
301 * on the outgoing interface, and the caller did not
302 * forbid loopback, loop back a copy.
303 */
304 ip_mloopback(ifp, m, dst, hlen);
305 }
306 else {
307 /*
308 * If we are acting as a multicast router, perform
309 * multicast forwarding as if the packet had just
310 * arrived on the interface to which we are about
311 * to send. The multicast forwarding function
312 * recursively calls this function, using the
313 * IP_FORWARDING flag to prevent infinite recursion.
314 *
315 * Multicasts that are looped back by ip_mloopback(),
316 * above, will be forwarded by the ip_input() routine,
317 * if necessary.
318 */
319 if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
320 /*
321 * If rsvp daemon is not running, do not
322 * set ip_moptions. This ensures that the packet
323 * is multicast and not just sent down one link
324 * as prescribed by rsvpd.
325 */
326 if (!rsvp_on)
327 imo = NULL;
328 if (ip_mforward &&
329 ip_mforward(ip, ifp, m, imo) != 0) {
330 m_freem(m);
331 goto done;
332 }
333 }
334 }
335
336 /*
337 * Multicasts with a time-to-live of zero may be looped-
338 * back, above, but must not be transmitted on a network.
339 * Also, multicasts addressed to the loopback interface
340 * are not sent -- the above call to ip_mloopback() will
341 * loop back a copy if this host actually belongs to the
342 * destination group on the loopback interface.
343 */
344 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
345 m_freem(m);
346 goto done;
347 }
348
349 goto sendit;
350 }
351 #ifndef notdef
352 /*
353 * If the source address is not specified yet, use the address
354 * of the outoing interface.
355 */
356 if (ip->ip_src.s_addr == INADDR_ANY) {
357 /* Interface may have no addresses. */
358 if (ia != NULL) {
359 ip->ip_src = IA_SIN(ia)->sin_addr;
360 }
361 }
362 #endif /* notdef */
363 #ifdef ALTQ
364 /*
365 * disable packet drop hack.
366 * packetdrop should be done by queueing.
367 */
368 #else /* !ALTQ */
369 /*
370 * Verify that we have any chance at all of being able to queue
371 * the packet or packet fragments
372 */
373 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
374 ifp->if_snd.ifq_maxlen) {
375 error = ENOBUFS;
376 ipstat.ips_odropped++;
377 goto bad;
378 }
379 #endif /* !ALTQ */
380
381 /*
382 * Look for broadcast address and
383 * verify user is allowed to send
384 * such a packet.
385 */
386 if (isbroadcast) {
387 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
388 error = EADDRNOTAVAIL;
389 goto bad;
390 }
391 if ((flags & IP_ALLOWBROADCAST) == 0) {
392 error = EACCES;
393 goto bad;
394 }
395 /* don't allow broadcast messages to be fragmented */
396 if (ip->ip_len > ifp->if_mtu) {
397 error = EMSGSIZE;
398 goto bad;
399 }
400 if (flags & IP_SENDONES)
401 ip->ip_dst.s_addr = INADDR_BROADCAST;
402 m->m_flags |= M_BCAST;
403 } else {
404 m->m_flags &= ~M_BCAST;
405 }
406
407 sendit:
408 #ifdef IPSEC
409 /* get SP for this packet */
410 if (inp == NULL)
411 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
412 flags, &error);
413 else
414 sp = ipsec4_getpolicybypcb(m, IPSEC_DIR_OUTBOUND, inp, &error);
415
416 if (sp == NULL) {
417 ipsecstat.out_inval++;
418 goto bad;
419 }
420
421 error = 0;
422
423 /* check policy */
424 switch (sp->policy) {
425 case IPSEC_POLICY_DISCARD:
426 /*
427 * This packet is just discarded.
428 */
429 ipsecstat.out_polvio++;
430 goto bad;
431
432 case IPSEC_POLICY_BYPASS:
433 case IPSEC_POLICY_NONE:
434 case IPSEC_POLICY_TCP:
435 /* no need to do IPsec. */
436 goto skip_ipsec;
437
438 case IPSEC_POLICY_IPSEC:
439 if (sp->req == NULL) {
440 /* acquire a policy */
441 error = key_spdacquire(sp);
442 goto bad;
443 }
444 break;
445
446 case IPSEC_POLICY_ENTRUST:
447 default:
448 printf("ip_output: Invalid policy found. %d\n", sp->policy);
449 }
450 {
451 struct ipsec_output_state state;
452 bzero(&state, sizeof(state));
453 state.m = m;
454 if (flags & IP_ROUTETOIF) {
455 state.ro = &iproute;
456 bzero(&iproute, sizeof(iproute));
457 } else
458 state.ro = ro;
459 state.dst = (struct sockaddr *)dst;
460
461 ip->ip_sum = 0;
462
463 /*
464 * XXX
465 * delayed checksums are not currently compatible with IPsec
466 */
467 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
468 in_delayed_cksum(m);
469 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
470 }
471
472 ip->ip_len = htons(ip->ip_len);
473 ip->ip_off = htons(ip->ip_off);
474
475 error = ipsec4_output(&state, sp, flags);
476
477 m = state.m;
478 if (flags & IP_ROUTETOIF) {
479 /*
480 * if we have tunnel mode SA, we may need to ignore
481 * IP_ROUTETOIF.
482 */
483 if (state.ro != &iproute || state.ro->ro_rt != NULL) {
484 flags &= ~IP_ROUTETOIF;
485 ro = state.ro;
486 }
487 } else
488 ro = state.ro;
489 dst = (struct sockaddr_in *)state.dst;
490 if (error) {
491 /* mbuf is already reclaimed in ipsec4_output. */
492 m = NULL;
493 switch (error) {
494 case EHOSTUNREACH:
495 case ENETUNREACH:
496 case EMSGSIZE:
497 case ENOBUFS:
498 case ENOMEM:
499 break;
500 default:
501 printf("ip4_output (ipsec): error code %d\n", error);
502 /*fall through*/
503 case ENOENT:
504 /* don't show these error codes to the user */
505 error = 0;
506 break;
507 }
508 goto bad;
509 }
510
511 /* be sure to update variables that are affected by ipsec4_output() */
512 ip = mtod(m, struct ip *);
513 hlen = ip->ip_hl << 2;
514 if (ro->ro_rt == NULL) {
515 if ((flags & IP_ROUTETOIF) == 0) {
516 printf("ip_output: "
517 "can't update route after IPsec processing\n");
518 error = EHOSTUNREACH; /*XXX*/
519 goto bad;
520 }
521 } else {
522 if (state.encap) {
523 ia = ifatoia(ro->ro_rt->rt_ifa);
524 ifp = ro->ro_rt->rt_ifp;
525 }
526 }
527 }
528
529 /* make it flipped, again. */
530 ip->ip_len = ntohs(ip->ip_len);
531 ip->ip_off = ntohs(ip->ip_off);
532 skip_ipsec:
533 #endif /*IPSEC*/
534 #ifdef FAST_IPSEC
535 /*
536 * Check the security policy (SP) for the packet and, if
537 * required, do IPsec-related processing. There are two
538 * cases here; the first time a packet is sent through
539 * it will be untagged and handled by ipsec4_checkpolicy.
540 * If the packet is resubmitted to ip_output (e.g. after
541 * AH, ESP, etc. processing), there will be a tag to bypass
542 * the lookup and related policy checking.
543 */
544 mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
545 s = splnet();
546 if (mtag != NULL) {
547 tdbi = (struct tdb_ident *)(mtag + 1);
548 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
549 if (sp == NULL)
550 error = -EINVAL; /* force silent drop */
551 m_tag_delete(m, mtag);
552 } else {
553 sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
554 &error, inp);
555 }
556 /*
557 * There are four return cases:
558 * sp != NULL apply IPsec policy
559 * sp == NULL, error == 0 no IPsec handling needed
560 * sp == NULL, error == -EINVAL discard packet w/o error
561 * sp == NULL, error != 0 discard packet, report error
562 */
563 if (sp != NULL) {
564 /* Loop detection, check if ipsec processing already done */
565 KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
566 for (mtag = m_tag_first(m); mtag != NULL;
567 mtag = m_tag_next(m, mtag)) {
568 if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
569 continue;
570 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
571 mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
572 continue;
573 /*
574 * Check if policy has an SA associated with it.
575 * This can happen when an SP has yet to acquire
576 * an SA; e.g. on first reference. If it occurs,
577 * then we let ipsec4_process_packet do its thing.
578 */
579 if (sp->req->sav == NULL)
580 break;
581 tdbi = (struct tdb_ident *)(mtag + 1);
582 if (tdbi->spi == sp->req->sav->spi &&
583 tdbi->proto == sp->req->sav->sah->saidx.proto &&
584 bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
585 sizeof (union sockaddr_union)) == 0) {
586 /*
587 * No IPsec processing is needed, free
588 * reference to SP.
589 *
590 * NB: null pointer to avoid free at
591 * done: below.
592 */
593 KEY_FREESP(&sp), sp = NULL;
594 splx(s);
595 goto spd_done;
596 }
597 }
598
599 /*
600 * Do delayed checksums now because we send before
601 * this is done in the normal processing path.
602 */
603 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
604 in_delayed_cksum(m);
605 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
606 }
607
608 ip->ip_len = htons(ip->ip_len);
609 ip->ip_off = htons(ip->ip_off);
610
611 /* NB: callee frees mbuf */
612 error = ipsec4_process_packet(m, sp->req, flags, 0);
613 /*
614 * Preserve KAME behaviour: ENOENT can be returned
615 * when an SA acquire is in progress. Don't propagate
616 * this to user-level; it confuses applications.
617 *
618 * XXX this will go away when the SADB is redone.
619 */
620 if (error == ENOENT)
621 error = 0;
622 splx(s);
623 goto done;
624 } else {
625 splx(s);
626
627 if (error != 0) {
628 /*
629 * Hack: -EINVAL is used to signal that a packet
630 * should be silently discarded. This is typically
631 * because we asked key management for an SA and
632 * it was delayed (e.g. kicked up to IKE).
633 */
634 if (error == -EINVAL)
635 error = 0;
636 goto bad;
637 } else {
638 /* No IPsec processing for this packet. */
639 }
640 #ifdef notyet
641 /*
642 * If deferred crypto processing is needed, check that
643 * the interface supports it.
644 */
645 mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
646 if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) {
647 /* notify IPsec to do its own crypto */
648 ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
649 error = EHOSTUNREACH;
650 goto bad;
651 }
652 #endif
653 }
654 spd_done:
655 #endif /* FAST_IPSEC */
656
657 /* Jump over all PFIL processing if hooks are not active. */
658 if (inet_pfil_hook.ph_busy_count == -1)
659 goto passout;
660
661 /* Run through list of hooks for output packets. */
662 odst.s_addr = ip->ip_dst.s_addr;
663 error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
664 if (error != 0 || m == NULL)
665 goto done;
666
667 ip = mtod(m, struct ip *);
668
669 /* See if destination IP address was changed by packet filter. */
670 if (odst.s_addr != ip->ip_dst.s_addr) {
671 m->m_flags |= M_SKIP_FIREWALL;
672 if (in_localip(ip->ip_dst)) {
673 m->m_flags |= M_FASTFWD_OURS;
674 if (m->m_pkthdr.rcvif == NULL)
675 m->m_pkthdr.rcvif = loif;
676 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
677 m->m_pkthdr.csum_flags |=
678 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
679 m->m_pkthdr.csum_data = 0xffff;
680 }
681 m->m_pkthdr.csum_flags |=
682 CSUM_IP_CHECKED | CSUM_IP_VALID;
683
684 error = netisr_queue(NETISR_IP, m);
685 goto done;
686 } else
687 goto again;
688 }
689
690 #ifdef IPFIREWALL_FORWARD
691 /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
692 if (m->m_flags & M_FASTFWD_OURS) {
693 if (m->m_pkthdr.rcvif == NULL)
694 m->m_pkthdr.rcvif = loif;
695 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
696 m->m_pkthdr.csum_flags |=
697 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
698 m->m_pkthdr.csum_data = 0xffff;
699 }
700 m->m_pkthdr.csum_flags |=
701 CSUM_IP_CHECKED | CSUM_IP_VALID;
702
703 error = netisr_queue(NETISR_IP, m);
704 goto done;
705 }
706 /* Or forward to some other address? */
707 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
708 if (fwd_tag) {
709 if (!in_localip(ip->ip_src) && !in_localaddr(ip->ip_dst)) {
710 dst = (struct sockaddr_in *)&ro->ro_dst;
711 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
712 m->m_flags |= M_SKIP_FIREWALL;
713 m_tag_delete(m, fwd_tag);
714 goto again;
715 } else {
716 m_tag_delete(m, fwd_tag);
717 /* Continue. */
718 }
719 }
720 #endif
721
722 passout:
723 /* 127/8 must not appear on wire - RFC1122. */
724 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
725 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
726 if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
727 ipstat.ips_badaddr++;
728 error = EADDRNOTAVAIL;
729 goto bad;
730 }
731 }
732
733 m->m_pkthdr.csum_flags |= CSUM_IP;
734 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
735 if (sw_csum & CSUM_DELAY_DATA) {
736 in_delayed_cksum(m);
737 sw_csum &= ~CSUM_DELAY_DATA;
738 }
739 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
740
741 /*
742 * If small enough for interface, or the interface will take
743 * care of the fragmentation for us, can just send directly.
744 */
745 if (ip->ip_len <= ifp->if_mtu || (ifp->if_hwassist & CSUM_FRAGMENT &&
746 ((ip->ip_off & IP_DF) == 0))) {
747 ip->ip_len = htons(ip->ip_len);
748 ip->ip_off = htons(ip->ip_off);
749 ip->ip_sum = 0;
750 if (sw_csum & CSUM_DELAY_IP)
751 ip->ip_sum = in_cksum(m, hlen);
752
753 /* Record statistics for this interface address. */
754 if (!(flags & IP_FORWARDING) && ia) {
755 ia->ia_ifa.if_opackets++;
756 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
757 }
758
759 #ifdef IPSEC
760 /* clean ipsec history once it goes out of the node */
761 ipsec_delaux(m);
762 #endif
763
764 #ifdef MBUF_STRESS_TEST
765 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
766 m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
767 #endif
768 error = (*ifp->if_output)(ifp, m,
769 (struct sockaddr *)dst, ro->ro_rt);
770 goto done;
771 }
772
773 if (ip->ip_off & IP_DF) {
774 error = EMSGSIZE;
775 /*
776 * This case can happen if the user changed the MTU
777 * of an interface after enabling IP on it. Because
778 * most netifs don't keep track of routes pointing to
779 * them, there is no way for one to update all its
780 * routes when the MTU is changed.
781 */
782 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) &&
783 (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
784 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
785 }
786 ipstat.ips_cantfrag++;
787 goto bad;
788 }
789
790 /*
791 * Too large for interface; fragment if possible. If successful,
792 * on return, m will point to a list of packets to be sent.
793 */
794 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum);
795 if (error)
796 goto bad;
797 for (; m; m = m0) {
798 m0 = m->m_nextpkt;
799 m->m_nextpkt = 0;
800 #ifdef IPSEC
801 /* clean ipsec history once it goes out of the node */
802 ipsec_delaux(m);
803 #endif
804 if (error == 0) {
805 /* Record statistics for this interface address. */
806 if (ia != NULL) {
807 ia->ia_ifa.if_opackets++;
808 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
809 }
810
811 error = (*ifp->if_output)(ifp, m,
812 (struct sockaddr *)dst, ro->ro_rt);
813 } else
814 m_freem(m);
815 }
816
817 if (error == 0)
818 ipstat.ips_fragmented++;
819
820 done:
821 if (ro == &iproute && ro->ro_rt) {
822 RTFREE(ro->ro_rt);
823 }
824 #ifdef IPSEC
825 if (sp != NULL) {
826 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
827 printf("DP ip_output call free SP:%p\n", sp));
828 key_freesp(sp);
829 }
830 #endif
831 #ifdef FAST_IPSEC
832 if (sp != NULL)
833 KEY_FREESP(&sp);
834 #endif
835 return (error);
836 bad:
837 m_freem(m);
838 goto done;
839 }
840
841 /*
842 * Create a chain of fragments which fit the given mtu. m_frag points to the
843 * mbuf to be fragmented; on return it points to the chain with the fragments.
844 * Return 0 if no error. If error, m_frag may contain a partially built
845 * chain of fragments that should be freed by the caller.
846 *
847 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
848 * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
849 */
850 int
851 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
852 u_long if_hwassist_flags, int sw_csum)
853 {
854 int error = 0;
855 int hlen = ip->ip_hl << 2;
856 int len = (mtu - hlen) & ~7; /* size of payload in each fragment */
857 int off;
858 struct mbuf *m0 = *m_frag; /* the original packet */
859 int firstlen;
860 struct mbuf **mnext;
861 int nfrags;
862
863 if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */
864 ipstat.ips_cantfrag++;
865 return EMSGSIZE;
866 }
867
868 /*
869 * Must be able to put at least 8 bytes per fragment.
870 */
871 if (len < 8)
872 return EMSGSIZE;
873
874 /*
875 * If the interface will not calculate checksums on
876 * fragmented packets, then do it here.
877 */
878 if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
879 (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
880 in_delayed_cksum(m0);
881 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
882 }
883
884 if (len > PAGE_SIZE) {
885 /*
886 * Fragment large datagrams such that each segment
887 * contains a multiple of PAGE_SIZE amount of data,
888 * plus headers. This enables a receiver to perform
889 * page-flipping zero-copy optimizations.
890 *
891 * XXX When does this help given that sender and receiver
892 * could have different page sizes, and also mtu could
893 * be less than the receiver's page size ?
894 */
895 int newlen;
896 struct mbuf *m;
897
898 for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
899 off += m->m_len;
900
901 /*
902 * firstlen (off - hlen) must be aligned on an
903 * 8-byte boundary
904 */
905 if (off < hlen)
906 goto smart_frag_failure;
907 off = ((off - hlen) & ~7) + hlen;
908 newlen = (~PAGE_MASK) & mtu;
909 if ((newlen + sizeof (struct ip)) > mtu) {
910 /* we failed, go back the default */
911 smart_frag_failure:
912 newlen = len;
913 off = hlen + len;
914 }
915 len = newlen;
916
917 } else {
918 off = hlen + len;
919 }
920
921 firstlen = off - hlen;
922 mnext = &m0->m_nextpkt; /* pointer to next packet */
923
924 /*
925 * Loop through length of segment after first fragment,
926 * make new header and copy data of each part and link onto chain.
927 * Here, m0 is the original packet, m is the fragment being created.
928 * The fragments are linked off the m_nextpkt of the original
929 * packet, which after processing serves as the first fragment.
930 */
931 for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
932 struct ip *mhip; /* ip header on the fragment */
933 struct mbuf *m;
934 int mhlen = sizeof (struct ip);
935
936 MGETHDR(m, M_DONTWAIT, MT_HEADER);
937 if (m == NULL) {
938 error = ENOBUFS;
939 ipstat.ips_odropped++;
940 goto done;
941 }
942 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
943 /*
944 * In the first mbuf, leave room for the link header, then
945 * copy the original IP header including options. The payload
946 * goes into an additional mbuf chain returned by m_copy().
947 */
948 m->m_data += max_linkhdr;
949 mhip = mtod(m, struct ip *);
950 *mhip = *ip;
951 if (hlen > sizeof (struct ip)) {
952 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
953 mhip->ip_v = IPVERSION;
954 mhip->ip_hl = mhlen >> 2;
955 }
956 m->m_len = mhlen;
957 /* XXX do we need to add ip->ip_off below ? */
958 mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
959 if (off + len >= ip->ip_len) { /* last fragment */
960 len = ip->ip_len - off;
961 m->m_flags |= M_LASTFRAG;
962 } else
963 mhip->ip_off |= IP_MF;
964 mhip->ip_len = htons((u_short)(len + mhlen));
965 m->m_next = m_copy(m0, off, len);
966 if (m->m_next == NULL) { /* copy failed */
967 m_free(m);
968 error = ENOBUFS; /* ??? */
969 ipstat.ips_odropped++;
970 goto done;
971 }
972 m->m_pkthdr.len = mhlen + len;
973 m->m_pkthdr.rcvif = (struct ifnet *)0;
974 #ifdef MAC
975 mac_create_fragment(m0, m);
976 #endif
977 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
978 mhip->ip_off = htons(mhip->ip_off);
979 mhip->ip_sum = 0;
980 if (sw_csum & CSUM_DELAY_IP)
981 mhip->ip_sum = in_cksum(m, mhlen);
982 *mnext = m;
983 mnext = &m->m_nextpkt;
984 }
985 ipstat.ips_ofragments += nfrags;
986
987 /* set first marker for fragment chain */
988 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
989 m0->m_pkthdr.csum_data = nfrags;
990
991 /*
992 * Update first fragment by trimming what's been copied out
993 * and updating header.
994 */
995 m_adj(m0, hlen + firstlen - ip->ip_len);
996 m0->m_pkthdr.len = hlen + firstlen;
997 ip->ip_len = htons((u_short)m0->m_pkthdr.len);
998 ip->ip_off |= IP_MF;
999 ip->ip_off = htons(ip->ip_off);
1000 ip->ip_sum = 0;
1001 if (sw_csum & CSUM_DELAY_IP)
1002 ip->ip_sum = in_cksum(m0, hlen);
1003
1004 done:
1005 *m_frag = m0;
1006 return error;
1007 }
1008
1009 void
1010 in_delayed_cksum(struct mbuf *m)
1011 {
1012 struct ip *ip;
1013 u_short csum, offset;
1014
1015 ip = mtod(m, struct ip *);
1016 offset = ip->ip_hl << 2 ;
1017 csum = in_cksum_skip(m, ip->ip_len, offset);
1018 if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1019 csum = 0xffff;
1020 offset += m->m_pkthdr.csum_data; /* checksum offset */
1021
1022 if (offset + sizeof(u_short) > m->m_len) {
1023 printf("delayed m_pullup, m->len: %d off: %d p: %d\n",
1024 m->m_len, offset, ip->ip_p);
1025 /*
1026 * XXX
1027 * this shouldn't happen, but if it does, the
1028 * correct behavior may be to insert the checksum
1029 * in the existing chain instead of rearranging it.
1030 */
1031 m = m_pullup(m, offset + sizeof(u_short));
1032 }
1033 *(u_short *)(m->m_data + offset) = csum;
1034 }
1035
1036 /*
1037 * Insert IP options into preformed packet.
1038 * Adjust IP destination as required for IP source routing,
1039 * as indicated by a non-zero in_addr at the start of the options.
1040 *
1041 * XXX This routine assumes that the packet has no options in place.
1042 */
1043 static struct mbuf *
1044 ip_insertoptions(m, opt, phlen)
1045 register struct mbuf *m;
1046 struct mbuf *opt;
1047 int *phlen;
1048 {
1049 register struct ipoption *p = mtod(opt, struct ipoption *);
1050 struct mbuf *n;
1051 register struct ip *ip = mtod(m, struct ip *);
1052 unsigned optlen;
1053
1054 optlen = opt->m_len - sizeof(p->ipopt_dst);
1055 if (optlen + ip->ip_len > IP_MAXPACKET) {
1056 *phlen = 0;
1057 return (m); /* XXX should fail */
1058 }
1059 if (p->ipopt_dst.s_addr)
1060 ip->ip_dst = p->ipopt_dst;
1061 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1062 MGETHDR(n, M_DONTWAIT, MT_HEADER);
1063 if (n == NULL) {
1064 *phlen = 0;
1065 return (m);
1066 }
1067 n->m_pkthdr.rcvif = (struct ifnet *)0;
1068 #ifdef MAC
1069 mac_create_mbuf_from_mbuf(m, n);
1070 #endif
1071 n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1072 m->m_len -= sizeof(struct ip);
1073 m->m_data += sizeof(struct ip);
1074 n->m_next = m;
1075 m = n;
1076 m->m_len = optlen + sizeof(struct ip);
1077 m->m_data += max_linkhdr;
1078 bcopy(ip, mtod(m, void *), sizeof(struct ip));
1079 } else {
1080 m->m_data -= optlen;
1081 m->m_len += optlen;
1082 m->m_pkthdr.len += optlen;
1083 bcopy(ip, mtod(m, void *), sizeof(struct ip));
1084 }
1085 ip = mtod(m, struct ip *);
1086 bcopy(p->ipopt_list, ip + 1, optlen);
1087 *phlen = sizeof(struct ip) + optlen;
1088 ip->ip_v = IPVERSION;
1089 ip->ip_hl = *phlen >> 2;
1090 ip->ip_len += optlen;
1091 return (m);
1092 }
1093
1094 /*
1095 * Copy options from ip to jp,
1096 * omitting those not copied during fragmentation.
1097 */
1098 int
1099 ip_optcopy(ip, jp)
1100 struct ip *ip, *jp;
1101 {
1102 register u_char *cp, *dp;
1103 int opt, optlen, cnt;
1104
1105 cp = (u_char *)(ip + 1);
1106 dp = (u_char *)(jp + 1);
1107 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1108 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1109 opt = cp[0];
1110 if (opt == IPOPT_EOL)
1111 break;
1112 if (opt == IPOPT_NOP) {
1113 /* Preserve for IP mcast tunnel's LSRR alignment. */
1114 *dp++ = IPOPT_NOP;
1115 optlen = 1;
1116 continue;
1117 }
1118
1119 KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
1120 ("ip_optcopy: malformed ipv4 option"));
1121 optlen = cp[IPOPT_OLEN];
1122 KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
1123 ("ip_optcopy: malformed ipv4 option"));
1124
1125 /* bogus lengths should have been caught by ip_dooptions */
1126 if (optlen > cnt)
1127 optlen = cnt;
1128 if (IPOPT_COPIED(opt)) {
1129 bcopy(cp, dp, optlen);
1130 dp += optlen;
1131 }
1132 }
1133 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1134 *dp++ = IPOPT_EOL;
1135 return (optlen);
1136 }
1137
1138 /*
1139 * IP socket option processing.
1140 */
1141 int
1142 ip_ctloutput(so, sopt)
1143 struct socket *so;
1144 struct sockopt *sopt;
1145 {
1146 struct inpcb *inp = sotoinpcb(so);
1147 int error, optval;
1148
1149 error = optval = 0;
1150 if (sopt->sopt_level != IPPROTO_IP) {
1151 return (EINVAL);
1152 }
1153
1154 switch (sopt->sopt_dir) {
1155 case SOPT_SET:
1156 switch (sopt->sopt_name) {
1157 case IP_OPTIONS:
1158 #ifdef notyet
1159 case IP_RETOPTS:
1160 #endif
1161 {
1162 struct mbuf *m;
1163 if (sopt->sopt_valsize > MLEN) {
1164 error = EMSGSIZE;
1165 break;
1166 }
1167 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
1168 if (m == NULL) {
1169 error = ENOBUFS;
1170 break;
1171 }
1172 m->m_len = sopt->sopt_valsize;
1173 error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1174 m->m_len);
1175
1176 return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
1177 m));
1178 }
1179
1180 case IP_TOS:
1181 case IP_TTL:
1182 case IP_RECVOPTS:
1183 case IP_RECVRETOPTS:
1184 case IP_RECVDSTADDR:
1185 case IP_RECVTTL:
1186 case IP_RECVIF:
1187 case IP_FAITH:
1188 case IP_ONESBCAST:
1189 error = sooptcopyin(sopt, &optval, sizeof optval,
1190 sizeof optval);
1191 if (error)
1192 break;
1193
1194 switch (sopt->sopt_name) {
1195 case IP_TOS:
1196 inp->inp_ip_tos = optval;
1197 break;
1198
1199 case IP_TTL:
1200 inp->inp_ip_ttl = optval;
1201 break;
1202 #define OPTSET(bit) do { \
1203 INP_LOCK(inp); \
1204 if (optval) \
1205 inp->inp_flags |= bit; \
1206 else \
1207 inp->inp_flags &= ~bit; \
1208 INP_UNLOCK(inp); \
1209 } while (0)
1210
1211 case IP_RECVOPTS:
1212 OPTSET(INP_RECVOPTS);
1213 break;
1214
1215 case IP_RECVRETOPTS:
1216 OPTSET(INP_RECVRETOPTS);
1217 break;
1218
1219 case IP_RECVDSTADDR:
1220 OPTSET(INP_RECVDSTADDR);
1221 break;
1222
1223 case IP_RECVTTL:
1224 OPTSET(INP_RECVTTL);
1225 break;
1226
1227 case IP_RECVIF:
1228 OPTSET(INP_RECVIF);
1229 break;
1230
1231 case IP_FAITH:
1232 OPTSET(INP_FAITH);
1233 break;
1234
1235 case IP_ONESBCAST:
1236 OPTSET(INP_ONESBCAST);
1237 break;
1238 }
1239 break;
1240 #undef OPTSET
1241
1242 case IP_MULTICAST_IF:
1243 case IP_MULTICAST_VIF:
1244 case IP_MULTICAST_TTL:
1245 case IP_MULTICAST_LOOP:
1246 case IP_ADD_MEMBERSHIP:
1247 case IP_DROP_MEMBERSHIP:
1248 error = ip_setmoptions(sopt, &inp->inp_moptions);
1249 break;
1250
1251 case IP_PORTRANGE:
1252 error = sooptcopyin(sopt, &optval, sizeof optval,
1253 sizeof optval);
1254 if (error)
1255 break;
1256
1257 INP_LOCK(inp);
1258 switch (optval) {
1259 case IP_PORTRANGE_DEFAULT:
1260 inp->inp_flags &= ~(INP_LOWPORT);
1261 inp->inp_flags &= ~(INP_HIGHPORT);
1262 break;
1263
1264 case IP_PORTRANGE_HIGH:
1265 inp->inp_flags &= ~(INP_LOWPORT);
1266 inp->inp_flags |= INP_HIGHPORT;
1267 break;
1268
1269 case IP_PORTRANGE_LOW:
1270 inp->inp_flags &= ~(INP_HIGHPORT);
1271 inp->inp_flags |= INP_LOWPORT;
1272 break;
1273
1274 default:
1275 error = EINVAL;
1276 break;
1277 }
1278 INP_UNLOCK(inp);
1279 break;
1280
1281 #if defined(IPSEC) || defined(FAST_IPSEC)
1282 case IP_IPSEC_POLICY:
1283 {
1284 caddr_t req;
1285 size_t len = 0;
1286 int priv;
1287 struct mbuf *m;
1288 int optname;
1289
1290 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1291 break;
1292 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1293 break;
1294 priv = (sopt->sopt_td != NULL &&
1295 suser(sopt->sopt_td) != 0) ? 0 : 1;
1296 req = mtod(m, caddr_t);
1297 len = m->m_len;
1298 optname = sopt->sopt_name;
1299 error = ipsec4_set_policy(inp, optname, req, len, priv);
1300 m_freem(m);
1301 break;
1302 }
1303 #endif /*IPSEC*/
1304
1305 default:
1306 error = ENOPROTOOPT;
1307 break;
1308 }
1309 break;
1310
1311 case SOPT_GET:
1312 switch (sopt->sopt_name) {
1313 case IP_OPTIONS:
1314 case IP_RETOPTS:
1315 if (inp->inp_options)
1316 error = sooptcopyout(sopt,
1317 mtod(inp->inp_options,
1318 char *),
1319 inp->inp_options->m_len);
1320 else
1321 sopt->sopt_valsize = 0;
1322 break;
1323
1324 case IP_TOS:
1325 case IP_TTL:
1326 case IP_RECVOPTS:
1327 case IP_RECVRETOPTS:
1328 case IP_RECVDSTADDR:
1329 case IP_RECVTTL:
1330 case IP_RECVIF:
1331 case IP_PORTRANGE:
1332 case IP_FAITH:
1333 case IP_ONESBCAST:
1334 switch (sopt->sopt_name) {
1335
1336 case IP_TOS:
1337 optval = inp->inp_ip_tos;
1338 break;
1339
1340 case IP_TTL:
1341 optval = inp->inp_ip_ttl;
1342 break;
1343
1344 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1345
1346 case IP_RECVOPTS:
1347 optval = OPTBIT(INP_RECVOPTS);
1348 break;
1349
1350 case IP_RECVRETOPTS:
1351 optval = OPTBIT(INP_RECVRETOPTS);
1352 break;
1353
1354 case IP_RECVDSTADDR:
1355 optval = OPTBIT(INP_RECVDSTADDR);
1356 break;
1357
1358 case IP_RECVTTL:
1359 optval = OPTBIT(INP_RECVTTL);
1360 break;
1361
1362 case IP_RECVIF:
1363 optval = OPTBIT(INP_RECVIF);
1364 break;
1365
1366 case IP_PORTRANGE:
1367 if (inp->inp_flags & INP_HIGHPORT)
1368 optval = IP_PORTRANGE_HIGH;
1369 else if (inp->inp_flags & INP_LOWPORT)
1370 optval = IP_PORTRANGE_LOW;
1371 else
1372 optval = 0;
1373 break;
1374
1375 case IP_FAITH:
1376 optval = OPTBIT(INP_FAITH);
1377 break;
1378
1379 case IP_ONESBCAST:
1380 optval = OPTBIT(INP_ONESBCAST);
1381 break;
1382 }
1383 error = sooptcopyout(sopt, &optval, sizeof optval);
1384 break;
1385
1386 case IP_MULTICAST_IF:
1387 case IP_MULTICAST_VIF:
1388 case IP_MULTICAST_TTL:
1389 case IP_MULTICAST_LOOP:
1390 case IP_ADD_MEMBERSHIP:
1391 case IP_DROP_MEMBERSHIP:
1392 error = ip_getmoptions(sopt, inp->inp_moptions);
1393 break;
1394
1395 #if defined(IPSEC) || defined(FAST_IPSEC)
1396 case IP_IPSEC_POLICY:
1397 {
1398 struct mbuf *m = NULL;
1399 caddr_t req = NULL;
1400 size_t len = 0;
1401
1402 if (m != 0) {
1403 req = mtod(m, caddr_t);
1404 len = m->m_len;
1405 }
1406 error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
1407 if (error == 0)
1408 error = soopt_mcopyout(sopt, m); /* XXX */
1409 if (error == 0)
1410 m_freem(m);
1411 break;
1412 }
1413 #endif /*IPSEC*/
1414
1415 default:
1416 error = ENOPROTOOPT;
1417 break;
1418 }
1419 break;
1420 }
1421 return (error);
1422 }
1423
1424 /*
1425 * Set up IP options in pcb for insertion in output packets.
1426 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1427 * with destination address if source routed.
1428 */
1429 static int
1430 ip_pcbopts(optname, pcbopt, m)
1431 int optname;
1432 struct mbuf **pcbopt;
1433 register struct mbuf *m;
1434 {
1435 register int cnt, optlen;
1436 register u_char *cp;
1437 u_char opt;
1438
1439 /* turn off any old options */
1440 if (*pcbopt)
1441 (void)m_free(*pcbopt);
1442 *pcbopt = 0;
1443 if (m == (struct mbuf *)0 || m->m_len == 0) {
1444 /*
1445 * Only turning off any previous options.
1446 */
1447 if (m)
1448 (void)m_free(m);
1449 return (0);
1450 }
1451
1452 if (m->m_len % sizeof(int32_t))
1453 goto bad;
1454 /*
1455 * IP first-hop destination address will be stored before
1456 * actual options; move other options back
1457 * and clear it when none present.
1458 */
1459 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1460 goto bad;
1461 cnt = m->m_len;
1462 m->m_len += sizeof(struct in_addr);
1463 cp = mtod(m, u_char *) + sizeof(struct in_addr);
1464 bcopy(mtod(m, void *), cp, (unsigned)cnt);
1465 bzero(mtod(m, void *), sizeof(struct in_addr));
1466
1467 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1468 opt = cp[IPOPT_OPTVAL];
1469 if (opt == IPOPT_EOL)
1470 break;
1471 if (opt == IPOPT_NOP)
1472 optlen = 1;
1473 else {
1474 if (cnt < IPOPT_OLEN + sizeof(*cp))
1475 goto bad;
1476 optlen = cp[IPOPT_OLEN];
1477 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1478 goto bad;
1479 }
1480 switch (opt) {
1481
1482 default:
1483 break;
1484
1485 case IPOPT_LSRR:
1486 case IPOPT_SSRR:
1487 /*
1488 * user process specifies route as:
1489 * ->A->B->C->D
1490 * D must be our final destination (but we can't
1491 * check that since we may not have connected yet).
1492 * A is first hop destination, which doesn't appear in
1493 * actual IP option, but is stored before the options.
1494 */
1495 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1496 goto bad;
1497 m->m_len -= sizeof(struct in_addr);
1498 cnt -= sizeof(struct in_addr);
1499 optlen -= sizeof(struct in_addr);
1500 cp[IPOPT_OLEN] = optlen;
1501 /*
1502 * Move first hop before start of options.
1503 */
1504 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1505 sizeof(struct in_addr));
1506 /*
1507 * Then copy rest of options back
1508 * to close up the deleted entry.
1509 */
1510 bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
1511 &cp[IPOPT_OFFSET+1],
1512 (unsigned)cnt - (IPOPT_MINOFF - 1));
1513 break;
1514 }
1515 }
1516 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1517 goto bad;
1518 *pcbopt = m;
1519 return (0);
1520
1521 bad:
1522 (void)m_free(m);
1523 return (EINVAL);
1524 }
1525
1526 /*
1527 * XXX
1528 * The whole multicast option thing needs to be re-thought.
1529 * Several of these options are equally applicable to non-multicast
1530 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1531 * standard option (IP_TTL).
1532 */
1533
1534 /*
1535 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1536 */
1537 static struct ifnet *
1538 ip_multicast_if(a, ifindexp)
1539 struct in_addr *a;
1540 int *ifindexp;
1541 {
1542 int ifindex;
1543 struct ifnet *ifp;
1544
1545 if (ifindexp)
1546 *ifindexp = 0;
1547 if (ntohl(a->s_addr) >> 24 == 0) {
1548 ifindex = ntohl(a->s_addr) & 0xffffff;
1549 if (ifindex < 0 || if_index < ifindex)
1550 return NULL;
1551 ifp = ifnet_byindex(ifindex);
1552 if (ifindexp)
1553 *ifindexp = ifindex;
1554 } else {
1555 INADDR_TO_IFP(*a, ifp);
1556 }
1557 return ifp;
1558 }
1559
1560 /*
1561 * Set the IP multicast options in response to user setsockopt().
1562 */
1563 static int
1564 ip_setmoptions(sopt, imop)
1565 struct sockopt *sopt;
1566 struct ip_moptions **imop;
1567 {
1568 int error = 0;
1569 int i;
1570 struct in_addr addr;
1571 struct ip_mreq mreq;
1572 struct ifnet *ifp;
1573 struct ip_moptions *imo = *imop;
1574 struct route ro;
1575 struct sockaddr_in *dst;
1576 int ifindex;
1577 int s;
1578
1579 if (imo == NULL) {
1580 /*
1581 * No multicast option buffer attached to the pcb;
1582 * allocate one and initialize to default values.
1583 */
1584 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1585 M_WAITOK);
1586
1587 if (imo == NULL)
1588 return (ENOBUFS);
1589 *imop = imo;
1590 imo->imo_multicast_ifp = NULL;
1591 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1592 imo->imo_multicast_vif = -1;
1593 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1594 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1595 imo->imo_num_memberships = 0;
1596 }
1597
1598 switch (sopt->sopt_name) {
1599 /* store an index number for the vif you wanna use in the send */
1600 case IP_MULTICAST_VIF:
1601 if (legal_vif_num == 0) {
1602 error = EOPNOTSUPP;
1603 break;
1604 }
1605 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1606 if (error)
1607 break;
1608 if (!legal_vif_num(i) && (i != -1)) {
1609 error = EINVAL;
1610 break;
1611 }
1612 imo->imo_multicast_vif = i;
1613 break;
1614
1615 case IP_MULTICAST_IF:
1616 /*
1617 * Select the interface for outgoing multicast packets.
1618 */
1619 error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1620 if (error)
1621 break;
1622 /*
1623 * INADDR_ANY is used to remove a previous selection.
1624 * When no interface is selected, a default one is
1625 * chosen every time a multicast packet is sent.
1626 */
1627 if (addr.s_addr == INADDR_ANY) {
1628 imo->imo_multicast_ifp = NULL;
1629 break;
1630 }
1631 /*
1632 * The selected interface is identified by its local
1633 * IP address. Find the interface and confirm that
1634 * it supports multicasting.
1635 */
1636 s = splimp();
1637 ifp = ip_multicast_if(&addr, &ifindex);
1638 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1639 splx(s);
1640 error = EADDRNOTAVAIL;
1641 break;
1642 }
1643 imo->imo_multicast_ifp = ifp;
1644 if (ifindex)
1645 imo->imo_multicast_addr = addr;
1646 else
1647 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1648 splx(s);
1649 break;
1650
1651 case IP_MULTICAST_TTL:
1652 /*
1653 * Set the IP time-to-live for outgoing multicast packets.
1654 * The original multicast API required a char argument,
1655 * which is inconsistent with the rest of the socket API.
1656 * We allow either a char or an int.
1657 */
1658 if (sopt->sopt_valsize == 1) {
1659 u_char ttl;
1660 error = sooptcopyin(sopt, &ttl, 1, 1);
1661 if (error)
1662 break;
1663 imo->imo_multicast_ttl = ttl;
1664 } else {
1665 u_int ttl;
1666 error = sooptcopyin(sopt, &ttl, sizeof ttl,
1667 sizeof ttl);
1668 if (error)
1669 break;
1670 if (ttl > 255)
1671 error = EINVAL;
1672 else
1673 imo->imo_multicast_ttl = ttl;
1674 }
1675 break;
1676
1677 case IP_MULTICAST_LOOP:
1678 /*
1679 * Set the loopback flag for outgoing multicast packets.
1680 * Must be zero or one. The original multicast API required a
1681 * char argument, which is inconsistent with the rest
1682 * of the socket API. We allow either a char or an int.
1683 */
1684 if (sopt->sopt_valsize == 1) {
1685 u_char loop;
1686 error = sooptcopyin(sopt, &loop, 1, 1);
1687 if (error)
1688 break;
1689 imo->imo_multicast_loop = !!loop;
1690 } else {
1691 u_int loop;
1692 error = sooptcopyin(sopt, &loop, sizeof loop,
1693 sizeof loop);
1694 if (error)
1695 break;
1696 imo->imo_multicast_loop = !!loop;
1697 }
1698 break;
1699
1700 case IP_ADD_MEMBERSHIP:
1701 /*
1702 * Add a multicast group membership.
1703 * Group must be a valid IP multicast address.
1704 */
1705 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1706 if (error)
1707 break;
1708
1709 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1710 error = EINVAL;
1711 break;
1712 }
1713 s = splimp();
1714 /*
1715 * If no interface address was provided, use the interface of
1716 * the route to the given multicast address.
1717 */
1718 if (mreq.imr_interface.s_addr == INADDR_ANY) {
1719 bzero((caddr_t)&ro, sizeof(ro));
1720 dst = (struct sockaddr_in *)&ro.ro_dst;
1721 dst->sin_len = sizeof(*dst);
1722 dst->sin_family = AF_INET;
1723 dst->sin_addr = mreq.imr_multiaddr;
1724 rtalloc_ign(&ro, RTF_CLONING);
1725 if (ro.ro_rt == NULL) {
1726 error = EADDRNOTAVAIL;
1727 splx(s);
1728 break;
1729 }
1730 ifp = ro.ro_rt->rt_ifp;
1731 RTFREE(ro.ro_rt);
1732 }
1733 else {
1734 ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1735 }
1736
1737 /*
1738 * See if we found an interface, and confirm that it
1739 * supports multicast.
1740 */
1741 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1742 error = EADDRNOTAVAIL;
1743 splx(s);
1744 break;
1745 }
1746 /*
1747 * See if the membership already exists or if all the
1748 * membership slots are full.
1749 */
1750 for (i = 0; i < imo->imo_num_memberships; ++i) {
1751 if (imo->imo_membership[i]->inm_ifp == ifp &&
1752 imo->imo_membership[i]->inm_addr.s_addr
1753 == mreq.imr_multiaddr.s_addr)
1754 break;
1755 }
1756 if (i < imo->imo_num_memberships) {
1757 error = EADDRINUSE;
1758 splx(s);
1759 break;
1760 }
1761 if (i == IP_MAX_MEMBERSHIPS) {
1762 error = ETOOMANYREFS;
1763 splx(s);
1764 break;
1765 }
1766 /*
1767 * Everything looks good; add a new record to the multicast
1768 * address list for the given interface.
1769 */
1770 if ((imo->imo_membership[i] =
1771 in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1772 error = ENOBUFS;
1773 splx(s);
1774 break;
1775 }
1776 ++imo->imo_num_memberships;
1777 splx(s);
1778 break;
1779
1780 case IP_DROP_MEMBERSHIP:
1781 /*
1782 * Drop a multicast group membership.
1783 * Group must be a valid IP multicast address.
1784 */
1785 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1786 if (error)
1787 break;
1788
1789 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1790 error = EINVAL;
1791 break;
1792 }
1793
1794 s = splimp();
1795 /*
1796 * If an interface address was specified, get a pointer
1797 * to its ifnet structure.
1798 */
1799 if (mreq.imr_interface.s_addr == INADDR_ANY)
1800 ifp = NULL;
1801 else {
1802 ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1803 if (ifp == NULL) {
1804 error = EADDRNOTAVAIL;
1805 splx(s);
1806 break;
1807 }
1808 }
1809 /*
1810 * Find the membership in the membership array.
1811 */
1812 for (i = 0; i < imo->imo_num_memberships; ++i) {
1813 if ((ifp == NULL ||
1814 imo->imo_membership[i]->inm_ifp == ifp) &&
1815 imo->imo_membership[i]->inm_addr.s_addr ==
1816 mreq.imr_multiaddr.s_addr)
1817 break;
1818 }
1819 if (i == imo->imo_num_memberships) {
1820 error = EADDRNOTAVAIL;
1821 splx(s);
1822 break;
1823 }
1824 /*
1825 * Give up the multicast address record to which the
1826 * membership points.
1827 */
1828 in_delmulti(imo->imo_membership[i]);
1829 /*
1830 * Remove the gap in the membership array.
1831 */
1832 for (++i; i < imo->imo_num_memberships; ++i)
1833 imo->imo_membership[i-1] = imo->imo_membership[i];
1834 --imo->imo_num_memberships;
1835 splx(s);
1836 break;
1837
1838 default:
1839 error = EOPNOTSUPP;
1840 break;
1841 }
1842
1843 /*
1844 * If all options have default values, no need to keep the mbuf.
1845 */
1846 if (imo->imo_multicast_ifp == NULL &&
1847 imo->imo_multicast_vif == -1 &&
1848 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1849 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1850 imo->imo_num_memberships == 0) {
1851 free(*imop, M_IPMOPTS);
1852 *imop = NULL;
1853 }
1854
1855 return (error);
1856 }
1857
1858 /*
1859 * Return the IP multicast options in response to user getsockopt().
1860 */
1861 static int
1862 ip_getmoptions(sopt, imo)
1863 struct sockopt *sopt;
1864 register struct ip_moptions *imo;
1865 {
1866 struct in_addr addr;
1867 struct in_ifaddr *ia;
1868 int error, optval;
1869 u_char coptval;
1870
1871 error = 0;
1872 switch (sopt->sopt_name) {
1873 case IP_MULTICAST_VIF:
1874 if (imo != NULL)
1875 optval = imo->imo_multicast_vif;
1876 else
1877 optval = -1;
1878 error = sooptcopyout(sopt, &optval, sizeof optval);
1879 break;
1880
1881 case IP_MULTICAST_IF:
1882 if (imo == NULL || imo->imo_multicast_ifp == NULL)
1883 addr.s_addr = INADDR_ANY;
1884 else if (imo->imo_multicast_addr.s_addr) {
1885 /* return the value user has set */
1886 addr = imo->imo_multicast_addr;
1887 } else {
1888 IFP_TO_IA(imo->imo_multicast_ifp, ia);
1889 addr.s_addr = (ia == NULL) ? INADDR_ANY
1890 : IA_SIN(ia)->sin_addr.s_addr;
1891 }
1892 error = sooptcopyout(sopt, &addr, sizeof addr);
1893 break;
1894
1895 case IP_MULTICAST_TTL:
1896 if (imo == 0)
1897 optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1898 else
1899 optval = coptval = imo->imo_multicast_ttl;
1900 if (sopt->sopt_valsize == 1)
1901 error = sooptcopyout(sopt, &coptval, 1);
1902 else
1903 error = sooptcopyout(sopt, &optval, sizeof optval);
1904 break;
1905
1906 case IP_MULTICAST_LOOP:
1907 if (imo == 0)
1908 optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1909 else
1910 optval = coptval = imo->imo_multicast_loop;
1911 if (sopt->sopt_valsize == 1)
1912 error = sooptcopyout(sopt, &coptval, 1);
1913 else
1914 error = sooptcopyout(sopt, &optval, sizeof optval);
1915 break;
1916
1917 default:
1918 error = ENOPROTOOPT;
1919 break;
1920 }
1921 return (error);
1922 }
1923
1924 /*
1925 * Discard the IP multicast options.
1926 */
1927 void
1928 ip_freemoptions(imo)
1929 register struct ip_moptions *imo;
1930 {
1931 register int i;
1932
1933 if (imo != NULL) {
1934 for (i = 0; i < imo->imo_num_memberships; ++i)
1935 in_delmulti(imo->imo_membership[i]);
1936 free(imo, M_IPMOPTS);
1937 }
1938 }
1939
1940 /*
1941 * Routine called from ip_output() to loop back a copy of an IP multicast
1942 * packet to the input queue of a specified interface. Note that this
1943 * calls the output routine of the loopback "driver", but with an interface
1944 * pointer that might NOT be a loopback interface -- evil, but easier than
1945 * replicating that code here.
1946 */
1947 static void
1948 ip_mloopback(ifp, m, dst, hlen)
1949 struct ifnet *ifp;
1950 register struct mbuf *m;
1951 register struct sockaddr_in *dst;
1952 int hlen;
1953 {
1954 register struct ip *ip;
1955 struct mbuf *copym;
1956
1957 copym = m_copy(m, 0, M_COPYALL);
1958 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
1959 copym = m_pullup(copym, hlen);
1960 if (copym != NULL) {
1961 /* If needed, compute the checksum and mark it as valid. */
1962 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1963 in_delayed_cksum(copym);
1964 copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1965 copym->m_pkthdr.csum_flags |=
1966 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1967 copym->m_pkthdr.csum_data = 0xffff;
1968 }
1969 /*
1970 * We don't bother to fragment if the IP length is greater
1971 * than the interface's MTU. Can this possibly matter?
1972 */
1973 ip = mtod(copym, struct ip *);
1974 ip->ip_len = htons(ip->ip_len);
1975 ip->ip_off = htons(ip->ip_off);
1976 ip->ip_sum = 0;
1977 ip->ip_sum = in_cksum(copym, hlen);
1978 /*
1979 * NB:
1980 * It's not clear whether there are any lingering
1981 * reentrancy problems in other areas which might
1982 * be exposed by using ip_input directly (in
1983 * particular, everything which modifies the packet
1984 * in-place). Yet another option is using the
1985 * protosw directly to deliver the looped back
1986 * packet. For the moment, we'll err on the side
1987 * of safety by using if_simloop().
1988 */
1989 #if 1 /* XXX */
1990 if (dst->sin_family != AF_INET) {
1991 printf("ip_mloopback: bad address family %d\n",
1992 dst->sin_family);
1993 dst->sin_family = AF_INET;
1994 }
1995 #endif
1996
1997 #ifdef notdef
1998 copym->m_pkthdr.rcvif = ifp;
1999 ip_input(copym);
2000 #else
2001 if_simloop(ifp, copym, dst->sin_family, 0);
2002 #endif
2003 }
2004 }
Cache object: 122f7629503508de377f2fdc14e0d5c3
|