2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
34 #include "opt_ipsec.h"
36 #include "opt_mbuf_stress_test.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/sysctl.h>
50 #include <net/netisr.h>
52 #include <net/route.h>
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 #include <netinet/ip_options.h>
62 #if defined(IPSEC) || defined(FAST_IPSEC)
63 #include <netinet/ip_ipsec.h>
65 #include <netinet6/ipsec.h>
68 #include <netipsec/ipsec.h>
72 #include <machine/in_cksum.h>
74 #include <security/mac/mac_framework.h>
76 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
78 #define print_ip(x, a, y) printf("%s %d.%d.%d.%d%s",\
79 x, (ntohl(a.s_addr)>>24)&0xFF,\
80 (ntohl(a.s_addr)>>16)&0xFF,\
81 (ntohl(a.s_addr)>>8)&0xFF,\
82 (ntohl(a.s_addr))&0xFF, y);
86 #ifdef MBUF_STRESS_TEST
87 int mbuf_frag_size = 0;
88 SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
89 &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
92 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
93 static void ip_mloopback
94 (struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
95 static int ip_getmoptions(struct inpcb *, struct sockopt *);
96 static int ip_setmoptions(struct inpcb *, struct sockopt *);
99 extern struct protosw inetsw[];
102 * IP output. The packet in mbuf chain m contains a skeletal IP
103 * header (with len, off, ttl, proto, tos, src, dst).
104 * The mbuf chain containing the packet will be freed.
105 * The mbuf opt, if present, will not be freed.
106 * In the IP forwarding case, the packet will arrive with options already
107 * inserted, so must have a NULL opt pointer.
110 ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro,
111 int flags, struct ip_moptions *imo, struct inpcb *inp)
114 struct ifnet *ifp = NULL; /* keep compiler happy */
116 int hlen = sizeof (struct ip);
119 struct sockaddr_in *dst = NULL; /* keep compiler happy */
120 struct in_ifaddr *ia = NULL;
121 int isbroadcast, sw_csum;
122 struct route iproute;
124 #ifdef IPFIREWALL_FORWARD
125 struct m_tag *fwd_tag = NULL;
131 bzero(ro, sizeof (*ro));
135 INP_LOCK_ASSERT(inp);
139 m = ip_insertoptions(m, opt, &len);
143 ip = mtod(m, struct ip *);
146 * Fill in IP header. If we are not allowing fragmentation,
147 * then the ip_id field is meaningless, but we don't set it
148 * to zero. Doing so causes various problems when devices along
149 * the path (routers, load balancers, firewalls, etc.) illegally
150 * disable DF on our packet. Note that a 16-bit counter
151 * will wrap around in less than 10 seconds at 100 Mbit/s on a
152 * medium with MTU 1500. See Steven M. Bellovin, "A Technique
153 * for Counting NATted Hosts", Proc. IMW'02, available at
154 * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>.
156 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
157 ip->ip_v = IPVERSION;
158 ip->ip_hl = hlen >> 2;
159 ip->ip_id = ip_newid();
160 ipstat.ips_localout++;
162 hlen = ip->ip_hl << 2;
165 dst = (struct sockaddr_in *)&ro->ro_dst;
168 * If there is a cached route,
169 * check that it is to the same destination
170 * and is still up. If not, free it and try again.
171 * The address family should also be checked in case of sharing the
174 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
175 dst->sin_family != AF_INET ||
176 dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
178 ro->ro_rt = (struct rtentry *)NULL;
180 #ifdef IPFIREWALL_FORWARD
181 if (ro->ro_rt == NULL && fwd_tag == NULL) {
183 if (ro->ro_rt == NULL) {
185 bzero(dst, sizeof(*dst));
186 dst->sin_family = AF_INET;
187 dst->sin_len = sizeof(*dst);
188 dst->sin_addr = ip->ip_dst;
191 * If routing to interface only, short circuit routing lookup.
192 * The use of an all-ones broadcast address implies this; an
193 * interface is specified by the broadcast address of an interface,
194 * or the destination address of a ptp interface.
196 if (flags & IP_SENDONES) {
197 if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL &&
198 (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) {
199 ipstat.ips_noroute++;
203 ip->ip_dst.s_addr = INADDR_BROADCAST;
204 dst->sin_addr = ip->ip_dst;
208 } else if (flags & IP_ROUTETOIF) {
209 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
210 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
211 ipstat.ips_noroute++;
217 isbroadcast = in_broadcast(dst->sin_addr, ifp);
218 } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
219 imo != NULL && imo->imo_multicast_ifp != NULL) {
221 * Bypass the normal routing lookup for multicast
222 * packets if the interface is specified.
224 ifp = imo->imo_multicast_ifp;
226 isbroadcast = 0; /* fool gcc */
229 * We want to do any cloning requested by the link layer,
230 * as this is probably required in all cases for correct
231 * operation (as it is for ARP).
233 if (ro->ro_rt == NULL)
235 if (ro->ro_rt == NULL) {
236 ipstat.ips_noroute++;
237 error = EHOSTUNREACH;
240 ia = ifatoia(ro->ro_rt->rt_ifa);
241 ifp = ro->ro_rt->rt_ifp;
242 ro->ro_rt->rt_rmx.rmx_pksent++;
243 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
244 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
245 if (ro->ro_rt->rt_flags & RTF_HOST)
246 isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
248 isbroadcast = in_broadcast(dst->sin_addr, ifp);
251 * Calculate MTU. If we have a route that is up, use that,
252 * otherwise use the interface's MTU.
254 if (ro->ro_rt != NULL && (ro->ro_rt->rt_flags & (RTF_UP|RTF_HOST))) {
256 * This case can happen if the user changed the MTU
257 * of an interface after enabling IP on it. Because
258 * most netifs don't keep track of routes pointing to
259 * them, there is no way for one to update all its
260 * routes when the MTU is changed.
262 if (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)
263 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
264 mtu = ro->ro_rt->rt_rmx.rmx_mtu;
268 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
269 struct in_multi *inm;
271 m->m_flags |= M_MCAST;
273 * IP destination address is multicast. Make sure "dst"
274 * still points to the address in "ro". (It may have been
275 * changed to point to a gateway address, above.)
277 dst = (struct sockaddr_in *)&ro->ro_dst;
279 * See if the caller provided any multicast options
282 ip->ip_ttl = imo->imo_multicast_ttl;
283 if (imo->imo_multicast_vif != -1)
286 ip_mcast_src(imo->imo_multicast_vif) :
289 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
291 * Confirm that the outgoing interface supports multicast.
293 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
294 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
295 ipstat.ips_noroute++;
301 * If source address not specified yet, use address
302 * of outgoing interface.
304 if (ip->ip_src.s_addr == INADDR_ANY) {
305 /* Interface may have no addresses. */
307 ip->ip_src = IA_SIN(ia)->sin_addr;
311 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
313 (imo == NULL || imo->imo_multicast_loop)) {
316 * If we belong to the destination multicast group
317 * on the outgoing interface, and the caller did not
318 * forbid loopback, loop back a copy.
320 ip_mloopback(ifp, m, dst, hlen);
325 * If we are acting as a multicast router, perform
326 * multicast forwarding as if the packet had just
327 * arrived on the interface to which we are about
328 * to send. The multicast forwarding function
329 * recursively calls this function, using the
330 * IP_FORWARDING flag to prevent infinite recursion.
332 * Multicasts that are looped back by ip_mloopback(),
333 * above, will be forwarded by the ip_input() routine,
336 if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
338 * If rsvp daemon is not running, do not
339 * set ip_moptions. This ensures that the packet
340 * is multicast and not just sent down one link
341 * as prescribed by rsvpd.
346 ip_mforward(ip, ifp, m, imo) != 0) {
354 * Multicasts with a time-to-live of zero may be looped-
355 * back, above, but must not be transmitted on a network.
356 * Also, multicasts addressed to the loopback interface
357 * are not sent -- the above call to ip_mloopback() will
358 * loop back a copy if this host actually belongs to the
359 * destination group on the loopback interface.
361 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
370 * If the source address is not specified yet, use the address
371 * of the outoing interface.
373 if (ip->ip_src.s_addr == INADDR_ANY) {
374 /* Interface may have no addresses. */
376 ip->ip_src = IA_SIN(ia)->sin_addr;
381 * Verify that we have any chance at all of being able to queue the
382 * packet or packet fragments, unless ALTQ is enabled on the given
383 * interface in which case packetdrop should be done by queueing.
386 if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) &&
387 ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >=
388 ifp->if_snd.ifq_maxlen))
390 if ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >=
391 ifp->if_snd.ifq_maxlen)
395 ipstat.ips_odropped++;
396 ifp->if_snd.ifq_drops += (ip->ip_len / ifp->if_mtu + 1);
401 * Look for broadcast address and
402 * verify user is allowed to send
406 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
407 error = EADDRNOTAVAIL;
410 if ((flags & IP_ALLOWBROADCAST) == 0) {
414 /* don't allow broadcast messages to be fragmented */
415 if (ip->ip_len > mtu) {
419 m->m_flags |= M_BCAST;
421 m->m_flags &= ~M_BCAST;
425 #if defined(IPSEC) || defined(FAST_IPSEC)
426 switch(ip_ipsec_output(&m, inp, &flags, &error, &ro, &iproute, &dst, &ia, &ifp)) {
433 break; /* Continue with packet processing. */
435 /* Update variables that are affected by ipsec4_output(). */
436 ip = mtod(m, struct ip *);
437 hlen = ip->ip_hl << 2;
440 /* Jump over all PFIL processing if hooks are not active. */
441 if (!PFIL_HOOKED(&inet_pfil_hook))
444 /* Run through list of hooks for output packets. */
445 odst.s_addr = ip->ip_dst.s_addr;
446 error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
447 if (error != 0 || m == NULL)
450 ip = mtod(m, struct ip *);
452 /* See if destination IP address was changed by packet filter. */
453 if (odst.s_addr != ip->ip_dst.s_addr) {
454 m->m_flags |= M_SKIP_FIREWALL;
455 /* If destination is now ourself drop to ip_input(). */
456 if (in_localip(ip->ip_dst)) {
457 m->m_flags |= M_FASTFWD_OURS;
458 if (m->m_pkthdr.rcvif == NULL)
459 m->m_pkthdr.rcvif = loif;
460 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
461 m->m_pkthdr.csum_flags |=
462 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
463 m->m_pkthdr.csum_data = 0xffff;
465 m->m_pkthdr.csum_flags |=
466 CSUM_IP_CHECKED | CSUM_IP_VALID;
468 error = netisr_queue(NETISR_IP, m);
471 goto again; /* Redo the routing table lookup. */
474 #ifdef IPFIREWALL_FORWARD
475 /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
476 if (m->m_flags & M_FASTFWD_OURS) {
477 if (m->m_pkthdr.rcvif == NULL)
478 m->m_pkthdr.rcvif = loif;
479 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
480 m->m_pkthdr.csum_flags |=
481 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
482 m->m_pkthdr.csum_data = 0xffff;
484 m->m_pkthdr.csum_flags |=
485 CSUM_IP_CHECKED | CSUM_IP_VALID;
487 error = netisr_queue(NETISR_IP, m);
490 /* Or forward to some other address? */
491 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
493 dst = (struct sockaddr_in *)&ro->ro_dst;
494 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
495 m->m_flags |= M_SKIP_FIREWALL;
496 m_tag_delete(m, fwd_tag);
499 #endif /* IPFIREWALL_FORWARD */
502 /* 127/8 must not appear on wire - RFC1122. */
503 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
504 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
505 if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
506 ipstat.ips_badaddr++;
507 error = EADDRNOTAVAIL;
512 m->m_pkthdr.csum_flags |= CSUM_IP;
513 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
514 if (sw_csum & CSUM_DELAY_DATA) {
516 sw_csum &= ~CSUM_DELAY_DATA;
518 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
521 * If small enough for interface, or the interface will take
522 * care of the fragmentation for us, we can just send directly.
524 if (ip->ip_len <= mtu ||
525 (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 ||
526 ((ip->ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) {
527 ip->ip_len = htons(ip->ip_len);
528 ip->ip_off = htons(ip->ip_off);
530 if (sw_csum & CSUM_DELAY_IP)
531 ip->ip_sum = in_cksum(m, hlen);
534 * Record statistics for this interface address.
535 * With CSUM_TSO the byte/packet count will be slightly
536 * incorrect because we count the IP+TCP headers only
537 * once instead of for every generated packet.
539 if (!(flags & IP_FORWARDING) && ia) {
540 if (m->m_pkthdr.csum_flags & CSUM_TSO)
541 ia->ia_ifa.if_opackets +=
542 m->m_pkthdr.len / m->m_pkthdr.tso_segsz;
544 ia->ia_ifa.if_opackets++;
545 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
548 /* clean ipsec history once it goes out of the node */
551 #ifdef MBUF_STRESS_TEST
552 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
553 m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
556 * Reset layer specific mbuf flags
557 * to avoid confusing lower layers.
559 m->m_flags &= ~(M_PROTOFLAGS);
561 error = (*ifp->if_output)(ifp, m,
562 (struct sockaddr *)dst, ro->ro_rt);
566 /* Balk when DF bit is set or the interface didn't support TSO. */
567 if ((ip->ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
569 ipstat.ips_cantfrag++;
574 * Too large for interface; fragment if possible. If successful,
575 * on return, m will point to a list of packets to be sent.
577 error = ip_fragment(ip, &m, mtu, ifp->if_hwassist, sw_csum);
584 /* clean ipsec history once it goes out of the node */
588 /* Record statistics for this interface address. */
590 ia->ia_ifa.if_opackets++;
591 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
594 * Reset layer specific mbuf flags
595 * to avoid confusing upper layers.
597 m->m_flags &= ~(M_PROTOFLAGS);
599 error = (*ifp->if_output)(ifp, m,
600 (struct sockaddr *)dst, ro->ro_rt);
606 ipstat.ips_fragmented++;
609 if (ro == &iproute && ro->ro_rt) {
619 * Create a chain of fragments which fit the given mtu. m_frag points to the
620 * mbuf to be fragmented; on return it points to the chain with the fragments.
621 * Return 0 if no error. If error, m_frag may contain a partially built
622 * chain of fragments that should be freed by the caller.
624 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
625 * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
628 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
629 u_long if_hwassist_flags, int sw_csum)
632 int hlen = ip->ip_hl << 2;
633 int len = (mtu - hlen) & ~7; /* size of payload in each fragment */
635 struct mbuf *m0 = *m_frag; /* the original packet */
640 if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */
641 ipstat.ips_cantfrag++;
646 * Must be able to put at least 8 bytes per fragment.
652 * If the interface will not calculate checksums on
653 * fragmented packets, then do it here.
655 if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
656 (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
657 in_delayed_cksum(m0);
658 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
661 if (len > PAGE_SIZE) {
663 * Fragment large datagrams such that each segment
664 * contains a multiple of PAGE_SIZE amount of data,
665 * plus headers. This enables a receiver to perform
666 * page-flipping zero-copy optimizations.
668 * XXX When does this help given that sender and receiver
669 * could have different page sizes, and also mtu could
670 * be less than the receiver's page size ?
675 for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
679 * firstlen (off - hlen) must be aligned on an
683 goto smart_frag_failure;
684 off = ((off - hlen) & ~7) + hlen;
685 newlen = (~PAGE_MASK) & mtu;
686 if ((newlen + sizeof (struct ip)) > mtu) {
687 /* we failed, go back the default */
698 firstlen = off - hlen;
699 mnext = &m0->m_nextpkt; /* pointer to next packet */
702 * Loop through length of segment after first fragment,
703 * make new header and copy data of each part and link onto chain.
704 * Here, m0 is the original packet, m is the fragment being created.
705 * The fragments are linked off the m_nextpkt of the original
706 * packet, which after processing serves as the first fragment.
708 for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
709 struct ip *mhip; /* ip header on the fragment */
711 int mhlen = sizeof (struct ip);
713 MGETHDR(m, M_DONTWAIT, MT_DATA);
716 ipstat.ips_odropped++;
719 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
721 * In the first mbuf, leave room for the link header, then
722 * copy the original IP header including options. The payload
723 * goes into an additional mbuf chain returned by m_copy().
725 m->m_data += max_linkhdr;
726 mhip = mtod(m, struct ip *);
728 if (hlen > sizeof (struct ip)) {
729 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
730 mhip->ip_v = IPVERSION;
731 mhip->ip_hl = mhlen >> 2;
734 /* XXX do we need to add ip->ip_off below ? */
735 mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
736 if (off + len >= ip->ip_len) { /* last fragment */
737 len = ip->ip_len - off;
738 m->m_flags |= M_LASTFRAG;
740 mhip->ip_off |= IP_MF;
741 mhip->ip_len = htons((u_short)(len + mhlen));
742 m->m_next = m_copy(m0, off, len);
743 if (m->m_next == NULL) { /* copy failed */
745 error = ENOBUFS; /* ??? */
746 ipstat.ips_odropped++;
749 m->m_pkthdr.len = mhlen + len;
750 m->m_pkthdr.rcvif = NULL;
752 mac_create_fragment(m0, m);
754 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
755 mhip->ip_off = htons(mhip->ip_off);
757 if (sw_csum & CSUM_DELAY_IP)
758 mhip->ip_sum = in_cksum(m, mhlen);
760 mnext = &m->m_nextpkt;
762 ipstat.ips_ofragments += nfrags;
764 /* set first marker for fragment chain */
765 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
766 m0->m_pkthdr.csum_data = nfrags;
769 * Update first fragment by trimming what's been copied out
770 * and updating header.
772 m_adj(m0, hlen + firstlen - ip->ip_len);
773 m0->m_pkthdr.len = hlen + firstlen;
774 ip->ip_len = htons((u_short)m0->m_pkthdr.len);
776 ip->ip_off = htons(ip->ip_off);
778 if (sw_csum & CSUM_DELAY_IP)
779 ip->ip_sum = in_cksum(m0, hlen);
787 in_delayed_cksum(struct mbuf *m)
790 u_short csum, offset;
792 ip = mtod(m, struct ip *);
793 offset = ip->ip_hl << 2 ;
794 csum = in_cksum_skip(m, ip->ip_len, offset);
795 if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
797 offset += m->m_pkthdr.csum_data; /* checksum offset */
799 if (offset + sizeof(u_short) > m->m_len) {
800 printf("delayed m_pullup, m->len: %d off: %d p: %d\n",
801 m->m_len, offset, ip->ip_p);
804 * this shouldn't happen, but if it does, the
805 * correct behavior may be to insert the checksum
806 * in the appropriate next mbuf in the chain.
810 *(u_short *)(m->m_data + offset) = csum;
814 * IP socket option processing.
817 ip_ctloutput(so, sopt)
819 struct sockopt *sopt;
821 struct inpcb *inp = sotoinpcb(so);
825 if (sopt->sopt_level != IPPROTO_IP) {
829 switch (sopt->sopt_dir) {
831 switch (sopt->sopt_name) {
838 if (sopt->sopt_valsize > MLEN) {
842 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
847 m->m_len = sopt->sopt_valsize;
848 error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
855 error = ip_pcbopts(inp, sopt->sopt_name, m);
871 error = sooptcopyin(sopt, &optval, sizeof optval,
876 switch (sopt->sopt_name) {
878 inp->inp_ip_tos = optval;
882 inp->inp_ip_ttl = optval;
886 if (optval > 0 && optval <= MAXTTL)
887 inp->inp_ip_minttl = optval;
892 #define OPTSET(bit) do { \
895 inp->inp_flags |= bit; \
897 inp->inp_flags &= ~bit; \
902 OPTSET(INP_RECVOPTS);
906 OPTSET(INP_RECVRETOPTS);
910 OPTSET(INP_RECVDSTADDR);
926 OPTSET(INP_ONESBCAST);
929 OPTSET(INP_DONTFRAG);
935 case IP_MULTICAST_IF:
936 case IP_MULTICAST_VIF:
937 case IP_MULTICAST_TTL:
938 case IP_MULTICAST_LOOP:
939 case IP_ADD_MEMBERSHIP:
940 case IP_DROP_MEMBERSHIP:
941 error = ip_setmoptions(inp, sopt);
945 error = sooptcopyin(sopt, &optval, sizeof optval,
952 case IP_PORTRANGE_DEFAULT:
953 inp->inp_flags &= ~(INP_LOWPORT);
954 inp->inp_flags &= ~(INP_HIGHPORT);
957 case IP_PORTRANGE_HIGH:
958 inp->inp_flags &= ~(INP_LOWPORT);
959 inp->inp_flags |= INP_HIGHPORT;
962 case IP_PORTRANGE_LOW:
963 inp->inp_flags &= ~(INP_HIGHPORT);
964 inp->inp_flags |= INP_LOWPORT;
974 #if defined(IPSEC) || defined(FAST_IPSEC)
975 case IP_IPSEC_POLICY:
983 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
985 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
987 if (sopt->sopt_td != NULL) {
989 * XXXRW: Would be more desirable to do this
990 * one layer down so that we only exercise
991 * privilege if it is needed.
993 error = priv_check(sopt->sopt_td,
1001 req = mtod(m, caddr_t);
1003 optname = sopt->sopt_name;
1004 error = ipsec4_set_policy(inp, optname, req, len, priv);
1011 error = ENOPROTOOPT;
1017 switch (sopt->sopt_name) {
1020 if (inp->inp_options)
1021 error = sooptcopyout(sopt,
1022 mtod(inp->inp_options,
1024 inp->inp_options->m_len);
1026 sopt->sopt_valsize = 0;
1033 case IP_RECVRETOPTS:
1034 case IP_RECVDSTADDR:
1041 switch (sopt->sopt_name) {
1044 optval = inp->inp_ip_tos;
1048 optval = inp->inp_ip_ttl;
1052 optval = inp->inp_ip_minttl;
1055 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1058 optval = OPTBIT(INP_RECVOPTS);
1061 case IP_RECVRETOPTS:
1062 optval = OPTBIT(INP_RECVRETOPTS);
1065 case IP_RECVDSTADDR:
1066 optval = OPTBIT(INP_RECVDSTADDR);
1070 optval = OPTBIT(INP_RECVTTL);
1074 optval = OPTBIT(INP_RECVIF);
1078 if (inp->inp_flags & INP_HIGHPORT)
1079 optval = IP_PORTRANGE_HIGH;
1080 else if (inp->inp_flags & INP_LOWPORT)
1081 optval = IP_PORTRANGE_LOW;
1087 optval = OPTBIT(INP_FAITH);
1091 optval = OPTBIT(INP_ONESBCAST);
1094 optval = OPTBIT(INP_DONTFRAG);
1097 error = sooptcopyout(sopt, &optval, sizeof optval);
1100 case IP_MULTICAST_IF:
1101 case IP_MULTICAST_VIF:
1102 case IP_MULTICAST_TTL:
1103 case IP_MULTICAST_LOOP:
1104 error = ip_getmoptions(inp, sopt);
1107 #if defined(IPSEC) || defined(FAST_IPSEC)
1108 case IP_IPSEC_POLICY:
1110 struct mbuf *m = NULL;
1115 req = mtod(m, caddr_t);
1118 error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
1120 error = soopt_mcopyout(sopt, m); /* XXX */
1128 error = ENOPROTOOPT;
1138 * The whole multicast option thing needs to be re-thought.
1139 * Several of these options are equally applicable to non-multicast
1140 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1141 * standard option (IP_TTL).
1145 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1147 static struct ifnet *
1148 ip_multicast_if(a, ifindexp)
1157 if (ntohl(a->s_addr) >> 24 == 0) {
1158 ifindex = ntohl(a->s_addr) & 0xffffff;
1159 if (ifindex < 0 || if_index < ifindex)
1161 ifp = ifnet_byindex(ifindex);
1163 *ifindexp = ifindex;
1165 INADDR_TO_IFP(*a, ifp);
1171 * Given an inpcb, return its multicast options structure pointer. Accepts
1172 * an unlocked inpcb pointer, but will return it locked. May sleep.
1174 static struct ip_moptions *
1175 ip_findmoptions(struct inpcb *inp)
1177 struct ip_moptions *imo;
1178 struct in_multi **immp;
1181 if (inp->inp_moptions != NULL)
1182 return (inp->inp_moptions);
1186 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK);
1187 immp = (struct in_multi **)malloc((sizeof(*immp) * IP_MIN_MEMBERSHIPS),
1188 M_IPMOPTS, M_WAITOK);
1190 imo->imo_multicast_ifp = NULL;
1191 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1192 imo->imo_multicast_vif = -1;
1193 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1194 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1195 imo->imo_num_memberships = 0;
1196 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1197 imo->imo_membership = immp;
1200 if (inp->inp_moptions != NULL) {
1201 free(immp, M_IPMOPTS);
1202 free(imo, M_IPMOPTS);
1203 return (inp->inp_moptions);
1205 inp->inp_moptions = imo;
1210 * Set the IP multicast options in response to user setsockopt().
1213 ip_setmoptions(struct inpcb *inp, struct sockopt *sopt)
1217 struct in_addr addr;
1218 struct ip_mreq mreq;
1220 struct ip_moptions *imo;
1222 struct sockaddr_in *dst;
1226 switch (sopt->sopt_name) {
1227 /* store an index number for the vif you wanna use in the send */
1228 case IP_MULTICAST_VIF:
1229 if (legal_vif_num == 0) {
1233 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1236 if (!legal_vif_num(i) && (i != -1)) {
1240 imo = ip_findmoptions(inp);
1241 imo->imo_multicast_vif = i;
1245 case IP_MULTICAST_IF:
1247 * Select the interface for outgoing multicast packets.
1249 error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1253 * INADDR_ANY is used to remove a previous selection.
1254 * When no interface is selected, a default one is
1255 * chosen every time a multicast packet is sent.
1257 imo = ip_findmoptions(inp);
1258 if (addr.s_addr == INADDR_ANY) {
1259 imo->imo_multicast_ifp = NULL;
1264 * The selected interface is identified by its local
1265 * IP address. Find the interface and confirm that
1266 * it supports multicasting.
1269 ifp = ip_multicast_if(&addr, &ifindex);
1270 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1273 error = EADDRNOTAVAIL;
1276 imo->imo_multicast_ifp = ifp;
1278 imo->imo_multicast_addr = addr;
1280 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1285 case IP_MULTICAST_TTL:
1287 * Set the IP time-to-live for outgoing multicast packets.
1288 * The original multicast API required a char argument,
1289 * which is inconsistent with the rest of the socket API.
1290 * We allow either a char or an int.
1292 if (sopt->sopt_valsize == 1) {
1294 error = sooptcopyin(sopt, &ttl, 1, 1);
1297 imo = ip_findmoptions(inp);
1298 imo->imo_multicast_ttl = ttl;
1302 error = sooptcopyin(sopt, &ttl, sizeof ttl,
1309 imo = ip_findmoptions(inp);
1310 imo->imo_multicast_ttl = ttl;
1316 case IP_MULTICAST_LOOP:
1318 * Set the loopback flag for outgoing multicast packets.
1319 * Must be zero or one. The original multicast API required a
1320 * char argument, which is inconsistent with the rest
1321 * of the socket API. We allow either a char or an int.
1323 if (sopt->sopt_valsize == 1) {
1325 error = sooptcopyin(sopt, &loop, 1, 1);
1328 imo = ip_findmoptions(inp);
1329 imo->imo_multicast_loop = !!loop;
1333 error = sooptcopyin(sopt, &loop, sizeof loop,
1337 imo = ip_findmoptions(inp);
1338 imo->imo_multicast_loop = !!loop;
1343 case IP_ADD_MEMBERSHIP:
1345 * Add a multicast group membership.
1346 * Group must be a valid IP multicast address.
1348 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1352 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1358 * If no interface address was provided, use the interface of
1359 * the route to the given multicast address.
1361 if (mreq.imr_interface.s_addr == INADDR_ANY) {
1362 bzero((caddr_t)&ro, sizeof(ro));
1363 dst = (struct sockaddr_in *)&ro.ro_dst;
1364 dst->sin_len = sizeof(*dst);
1365 dst->sin_family = AF_INET;
1366 dst->sin_addr = mreq.imr_multiaddr;
1367 rtalloc_ign(&ro, RTF_CLONING);
1368 if (ro.ro_rt == NULL) {
1369 error = EADDRNOTAVAIL;
1373 ifp = ro.ro_rt->rt_ifp;
1377 ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1381 * See if we found an interface, and confirm that it
1382 * supports multicast.
1384 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1385 error = EADDRNOTAVAIL;
1390 * See if the membership already exists or if all the
1391 * membership slots are full.
1393 imo = ip_findmoptions(inp);
1394 for (i = 0; i < imo->imo_num_memberships; ++i) {
1395 if (imo->imo_membership[i]->inm_ifp == ifp &&
1396 imo->imo_membership[i]->inm_addr.s_addr
1397 == mreq.imr_multiaddr.s_addr)
1400 if (i < imo->imo_num_memberships) {
1406 if (imo->imo_num_memberships == imo->imo_max_memberships) {
1407 struct in_multi **nmships, **omships;
1410 * Resize the vector to next power-of-two minus 1. If the
1411 * size would exceed the maximum then we know we've really
1412 * run out of entries. Otherwise, we realloc() the vector
1413 * with the INP lock held to avoid introducing a race.
1416 omships = imo->imo_membership;
1417 newmax = ((imo->imo_max_memberships + 1) * 2) - 1;
1418 if (newmax <= IP_MAX_MEMBERSHIPS) {
1419 nmships = (struct in_multi **)realloc(omships,
1420 sizeof(*nmships) * newmax, M_IPMOPTS, M_NOWAIT);
1421 if (nmships != NULL) {
1422 imo->imo_membership = nmships;
1423 imo->imo_max_memberships = newmax;
1426 if (nmships == NULL) {
1428 error = ETOOMANYREFS;
1434 * Everything looks good; add a new record to the multicast
1435 * address list for the given interface.
1437 if ((imo->imo_membership[i] =
1438 in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1444 ++imo->imo_num_memberships;
1449 case IP_DROP_MEMBERSHIP:
1451 * Drop a multicast group membership.
1452 * Group must be a valid IP multicast address.
1454 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1458 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1465 * If an interface address was specified, get a pointer
1466 * to its ifnet structure.
1468 if (mreq.imr_interface.s_addr == INADDR_ANY)
1471 ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1473 error = EADDRNOTAVAIL;
1479 * Find the membership in the membership array.
1481 imo = ip_findmoptions(inp);
1482 for (i = 0; i < imo->imo_num_memberships; ++i) {
1484 imo->imo_membership[i]->inm_ifp == ifp) &&
1485 imo->imo_membership[i]->inm_addr.s_addr ==
1486 mreq.imr_multiaddr.s_addr)
1489 if (i == imo->imo_num_memberships) {
1491 error = EADDRNOTAVAIL;
1496 * Give up the multicast address record to which the
1497 * membership points.
1499 in_delmulti(imo->imo_membership[i]);
1501 * Remove the gap in the membership array.
1503 for (++i; i < imo->imo_num_memberships; ++i)
1504 imo->imo_membership[i-1] = imo->imo_membership[i];
1505 --imo->imo_num_memberships;
1519 * Return the IP multicast options in response to user getsockopt().
1522 ip_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1524 struct ip_moptions *imo;
1525 struct in_addr addr;
1526 struct in_ifaddr *ia;
1531 imo = inp->inp_moptions;
1534 switch (sopt->sopt_name) {
1535 case IP_MULTICAST_VIF:
1537 optval = imo->imo_multicast_vif;
1541 error = sooptcopyout(sopt, &optval, sizeof optval);
1544 case IP_MULTICAST_IF:
1545 if (imo == NULL || imo->imo_multicast_ifp == NULL)
1546 addr.s_addr = INADDR_ANY;
1547 else if (imo->imo_multicast_addr.s_addr) {
1548 /* return the value user has set */
1549 addr = imo->imo_multicast_addr;
1551 IFP_TO_IA(imo->imo_multicast_ifp, ia);
1552 addr.s_addr = (ia == NULL) ? INADDR_ANY
1553 : IA_SIN(ia)->sin_addr.s_addr;
1556 error = sooptcopyout(sopt, &addr, sizeof addr);
1559 case IP_MULTICAST_TTL:
1561 optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1563 optval = coptval = imo->imo_multicast_ttl;
1565 if (sopt->sopt_valsize == 1)
1566 error = sooptcopyout(sopt, &coptval, 1);
1568 error = sooptcopyout(sopt, &optval, sizeof optval);
1571 case IP_MULTICAST_LOOP:
1573 optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1575 optval = coptval = imo->imo_multicast_loop;
1577 if (sopt->sopt_valsize == 1)
1578 error = sooptcopyout(sopt, &coptval, 1);
1580 error = sooptcopyout(sopt, &optval, sizeof optval);
1585 error = ENOPROTOOPT;
1588 INP_UNLOCK_ASSERT(inp);
1594 * Discard the IP multicast options.
1597 ip_freemoptions(imo)
1598 register struct ip_moptions *imo;
1603 for (i = 0; i < imo->imo_num_memberships; ++i)
1604 in_delmulti(imo->imo_membership[i]);
1605 free(imo->imo_membership, M_IPMOPTS);
1606 free(imo, M_IPMOPTS);
1611 * Routine called from ip_output() to loop back a copy of an IP multicast
1612 * packet to the input queue of a specified interface. Note that this
1613 * calls the output routine of the loopback "driver", but with an interface
1614 * pointer that might NOT be a loopback interface -- evil, but easier than
1615 * replicating that code here.
1618 ip_mloopback(ifp, m, dst, hlen)
1620 register struct mbuf *m;
1621 register struct sockaddr_in *dst;
1624 register struct ip *ip;
1627 copym = m_copy(m, 0, M_COPYALL);
1628 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
1629 copym = m_pullup(copym, hlen);
1630 if (copym != NULL) {
1631 /* If needed, compute the checksum and mark it as valid. */
1632 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1633 in_delayed_cksum(copym);
1634 copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1635 copym->m_pkthdr.csum_flags |=
1636 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1637 copym->m_pkthdr.csum_data = 0xffff;
1640 * We don't bother to fragment if the IP length is greater
1641 * than the interface's MTU. Can this possibly matter?
1643 ip = mtod(copym, struct ip *);
1644 ip->ip_len = htons(ip->ip_len);
1645 ip->ip_off = htons(ip->ip_off);
1647 ip->ip_sum = in_cksum(copym, hlen);
1650 * It's not clear whether there are any lingering
1651 * reentrancy problems in other areas which might
1652 * be exposed by using ip_input directly (in
1653 * particular, everything which modifies the packet
1654 * in-place). Yet another option is using the
1655 * protosw directly to deliver the looped back
1656 * packet. For the moment, we'll err on the side
1657 * of safety by using if_simloop().
1660 if (dst->sin_family != AF_INET) {
1661 printf("ip_mloopback: bad address family %d\n",
1663 dst->sin_family = AF_INET;
1668 copym->m_pkthdr.rcvif = ifp;
1671 if_simloop(ifp, copym, dst->sin_family, 0);