]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/ip_output.c
Reintegrate head@r272414
[FreeBSD/FreeBSD.git] / sys / netinet / ip_output.c
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  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_inet.h"
36 #include "opt_ipfw.h"
37 #include "opt_ipsec.h"
38 #include "opt_mbuf_stress_test.h"
39 #include "opt_mpath.h"
40 #include "opt_route.h"
41 #include "opt_sctp.h"
42 #include "opt_rss.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/protosw.h>
52 #include <sys/sdt.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/sysctl.h>
56 #include <sys/ucred.h>
57
58 #include <net/if.h>
59 #include <net/if_var.h>
60 #include <net/if_llatbl.h>
61 #include <net/netisr.h>
62 #include <net/pfil.h>
63 #include <net/route.h>
64 #include <net/flowtable.h>
65 #ifdef RADIX_MPATH
66 #include <net/radix_mpath.h>
67 #endif
68 #include <net/vnet.h>
69
70 #include <netinet/in.h>
71 #include <netinet/in_kdtrace.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/in_pcb.h>
75 #include <netinet/in_rss.h>
76 #include <netinet/in_var.h>
77 #include <netinet/ip_var.h>
78 #include <netinet/ip_options.h>
79 #ifdef SCTP
80 #include <netinet/sctp.h>
81 #include <netinet/sctp_crc32.h>
82 #endif
83
84 #ifdef IPSEC
85 #include <netinet/ip_ipsec.h>
86 #include <netipsec/ipsec.h>
87 #endif /* IPSEC*/
88
89 #include <machine/in_cksum.h>
90
91 #include <security/mac/mac_framework.h>
92
93 VNET_DEFINE(u_short, ip_id);
94
95 #ifdef MBUF_STRESS_TEST
96 static int mbuf_frag_size = 0;
97 SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
98         &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
99 #endif
100
101 static void     ip_mloopback
102         (struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
103
104
105 extern int in_mcast_loop;
106 extern  struct protosw inetsw[];
107
108 /*
109  * IP output.  The packet in mbuf chain m contains a skeletal IP
110  * header (with len, off, ttl, proto, tos, src, dst).
111  * The mbuf chain containing the packet will be freed.
112  * The mbuf opt, if present, will not be freed.
113  * If route ro is present and has ro_rt initialized, route lookup would be
114  * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
115  * then result of route lookup is stored in ro->ro_rt.
116  *
117  * In the IP forwarding case, the packet will arrive with options already
118  * inserted, so must have a NULL opt pointer.
119  */
120 int
121 ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
122     struct ip_moptions *imo, struct inpcb *inp)
123 {
124         struct ip *ip;
125         struct ifnet *ifp = NULL;       /* keep compiler happy */
126         struct mbuf *m0;
127         int hlen = sizeof (struct ip);
128         int mtu;
129         int error = 0;
130         struct sockaddr_in *dst;
131         const struct sockaddr_in *gw;
132         struct in_ifaddr *ia;
133         int isbroadcast;
134         uint16_t ip_len, ip_off;
135         struct route iproute;
136         struct rtentry *rte;    /* cache for ro->ro_rt */
137         struct in_addr odst;
138         struct m_tag *fwd_tag = NULL;
139         uint32_t fibnum;
140         int have_ia_ref;
141         int needfiblookup;
142 #ifdef IPSEC
143         int no_route_but_check_spd = 0;
144 #endif
145         M_ASSERTPKTHDR(m);
146
147         if (inp != NULL) {
148                 INP_LOCK_ASSERT(inp);
149                 M_SETFIB(m, inp->inp_inc.inc_fibnum);
150                 if (((flags & IP_NODEFAULTFLOWID) == 0) &&
151                     inp->inp_flags & (INP_HW_FLOWID|INP_SW_FLOWID)) {
152                         m->m_pkthdr.flowid = inp->inp_flowid;
153                         M_HASHTYPE_SET(m, inp->inp_flowtype);
154                         m->m_flags |= M_FLOWID;
155                 }
156         }
157
158         if (ro == NULL) {
159                 ro = &iproute;
160                 bzero(ro, sizeof (*ro));
161         }
162
163 #ifdef FLOWTABLE
164         if (ro->ro_rt == NULL)
165                 (void )flowtable_lookup(AF_INET, m, ro);
166 #endif
167
168         if (opt) {
169                 int len = 0;
170                 m = ip_insertoptions(m, opt, &len);
171                 if (len != 0)
172                         hlen = len; /* ip->ip_hl is updated above */
173         }
174         ip = mtod(m, struct ip *);
175         ip_len = ntohs(ip->ip_len);
176         ip_off = ntohs(ip->ip_off);
177
178         /*
179          * Fill in IP header.  If we are not allowing fragmentation,
180          * then the ip_id field is meaningless, but we don't set it
181          * to zero.  Doing so causes various problems when devices along
182          * the path (routers, load balancers, firewalls, etc.) illegally
183          * disable DF on our packet.  Note that a 16-bit counter
184          * will wrap around in less than 10 seconds at 100 Mbit/s on a
185          * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
186          * for Counting NATted Hosts", Proc. IMW'02, available at
187          * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>.
188          */
189         if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
190                 ip->ip_v = IPVERSION;
191                 ip->ip_hl = hlen >> 2;
192                 ip->ip_id = ip_newid();
193                 IPSTAT_INC(ips_localout);
194         } else {
195                 /* Header already set, fetch hlen from there */
196                 hlen = ip->ip_hl << 2;
197         }
198
199         /*
200          * dst/gw handling:
201          *
202          * dst can be rewritten but always points to &ro->ro_dst.
203          * gw is readonly but can point either to dst OR rt_gateway,
204          * therefore we need restore gw if we're redoing lookup.
205          */
206         gw = dst = (struct sockaddr_in *)&ro->ro_dst;
207         fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
208 again:
209         ia = NULL;
210         have_ia_ref = 0;
211         /*
212          * If there is a cached route, check that it is to the same
213          * destination and is still up.  If not, free it and try again.
214          * The address family should also be checked in case of sharing
215          * the cache with IPv6.
216          */
217         rte = ro->ro_rt;
218         if (rte && ((rte->rt_flags & RTF_UP) == 0 ||
219                     rte->rt_ifp == NULL ||
220                     !RT_LINK_IS_UP(rte->rt_ifp) ||
221                           dst->sin_family != AF_INET ||
222                           dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
223                 RO_RTFREE(ro);
224                 ro->ro_lle = NULL;
225                 rte = NULL;
226                 gw = dst;
227         }
228         if (rte == NULL && fwd_tag == NULL) {
229                 bzero(dst, sizeof(*dst));
230                 dst->sin_family = AF_INET;
231                 dst->sin_len = sizeof(*dst);
232                 dst->sin_addr = ip->ip_dst;
233         }
234         /*
235          * If routing to interface only, short circuit routing lookup.
236          * The use of an all-ones broadcast address implies this; an
237          * interface is specified by the broadcast address of an interface,
238          * or the destination address of a ptp interface.
239          */
240         if (flags & IP_SENDONES) {
241                 if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst),
242                                                       M_GETFIB(m)))) == NULL &&
243                     (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
244                                                     M_GETFIB(m)))) == NULL) {
245                         IPSTAT_INC(ips_noroute);
246                         error = ENETUNREACH;
247                         goto bad;
248                 }
249                 have_ia_ref = 1;
250                 ip->ip_dst.s_addr = INADDR_BROADCAST;
251                 dst->sin_addr = ip->ip_dst;
252                 ifp = ia->ia_ifp;
253                 ip->ip_ttl = 1;
254                 isbroadcast = 1;
255         } else if (flags & IP_ROUTETOIF) {
256                 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
257                                                     M_GETFIB(m)))) == NULL &&
258                     (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0,
259                                                 M_GETFIB(m)))) == NULL) {
260                         IPSTAT_INC(ips_noroute);
261                         error = ENETUNREACH;
262                         goto bad;
263                 }
264                 have_ia_ref = 1;
265                 ifp = ia->ia_ifp;
266                 ip->ip_ttl = 1;
267                 isbroadcast = in_broadcast(dst->sin_addr, ifp);
268         } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
269             imo != NULL && imo->imo_multicast_ifp != NULL) {
270                 /*
271                  * Bypass the normal routing lookup for multicast
272                  * packets if the interface is specified.
273                  */
274                 ifp = imo->imo_multicast_ifp;
275                 IFP_TO_IA(ifp, ia);
276                 if (ia)
277                         have_ia_ref = 1;
278                 isbroadcast = 0;        /* fool gcc */
279         } else {
280                 /*
281                  * We want to do any cloning requested by the link layer,
282                  * as this is probably required in all cases for correct
283                  * operation (as it is for ARP).
284                  */
285                 if (rte == NULL) {
286 #ifdef RADIX_MPATH
287                         rtalloc_mpath_fib(ro,
288                             ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr),
289                             fibnum);
290 #else
291                         in_rtalloc_ign(ro, 0, fibnum);
292 #endif
293                         rte = ro->ro_rt;
294                 }
295                 if (rte == NULL ||
296                     rte->rt_ifp == NULL ||
297                     !RT_LINK_IS_UP(rte->rt_ifp)) {
298 #ifdef IPSEC
299                         /*
300                          * There is no route for this packet, but it is
301                          * possible that a matching SPD entry exists.
302                          */
303                         no_route_but_check_spd = 1;
304                         mtu = 0; /* Silence GCC warning. */
305                         goto sendit;
306 #endif
307                         IPSTAT_INC(ips_noroute);
308                         error = EHOSTUNREACH;
309                         goto bad;
310                 }
311                 ia = ifatoia(rte->rt_ifa);
312                 ifp = rte->rt_ifp;
313                 counter_u64_add(rte->rt_pksent, 1);
314                 if (rte->rt_flags & RTF_GATEWAY)
315                         gw = (struct sockaddr_in *)rte->rt_gateway;
316                 if (rte->rt_flags & RTF_HOST)
317                         isbroadcast = (rte->rt_flags & RTF_BROADCAST);
318                 else
319                         isbroadcast = in_broadcast(gw->sin_addr, ifp);
320         }
321         /*
322          * Calculate MTU.  If we have a route that is up, use that,
323          * otherwise use the interface's MTU.
324          */
325         if (rte != NULL && (rte->rt_flags & (RTF_UP|RTF_HOST))) {
326                 /*
327                  * This case can happen if the user changed the MTU
328                  * of an interface after enabling IP on it.  Because
329                  * most netifs don't keep track of routes pointing to
330                  * them, there is no way for one to update all its
331                  * routes when the MTU is changed.
332                  */
333                 if (rte->rt_mtu > ifp->if_mtu)
334                         rte->rt_mtu = ifp->if_mtu;
335                 mtu = rte->rt_mtu;
336         } else {
337                 mtu = ifp->if_mtu;
338         }
339         /* Catch a possible divide by zero later. */
340         KASSERT(mtu > 0, ("%s: mtu %d <= 0, rte=%p (rt_flags=0x%08x) ifp=%p",
341             __func__, mtu, rte, (rte != NULL) ? rte->rt_flags : 0, ifp));
342         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
343                 m->m_flags |= M_MCAST;
344                 /*
345                  * IP destination address is multicast.  Make sure "gw"
346                  * still points to the address in "ro".  (It may have been
347                  * changed to point to a gateway address, above.)
348                  */
349                 gw = dst;
350                 /*
351                  * See if the caller provided any multicast options
352                  */
353                 if (imo != NULL) {
354                         ip->ip_ttl = imo->imo_multicast_ttl;
355                         if (imo->imo_multicast_vif != -1)
356                                 ip->ip_src.s_addr =
357                                     ip_mcast_src ?
358                                     ip_mcast_src(imo->imo_multicast_vif) :
359                                     INADDR_ANY;
360                 } else
361                         ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
362                 /*
363                  * Confirm that the outgoing interface supports multicast.
364                  */
365                 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
366                         if ((ifp->if_flags & IFF_MULTICAST) == 0) {
367                                 IPSTAT_INC(ips_noroute);
368                                 error = ENETUNREACH;
369                                 goto bad;
370                         }
371                 }
372                 /*
373                  * If source address not specified yet, use address
374                  * of outgoing interface.
375                  */
376                 if (ip->ip_src.s_addr == INADDR_ANY) {
377                         /* Interface may have no addresses. */
378                         if (ia != NULL)
379                                 ip->ip_src = IA_SIN(ia)->sin_addr;
380                 }
381
382                 if ((imo == NULL && in_mcast_loop) ||
383                     (imo && imo->imo_multicast_loop)) {
384                         /*
385                          * Loop back multicast datagram if not expressly
386                          * forbidden to do so, even if we are not a member
387                          * of the group; ip_input() will filter it later,
388                          * thus deferring a hash lookup and mutex acquisition
389                          * at the expense of a cheap copy using m_copym().
390                          */
391                         ip_mloopback(ifp, m, dst, hlen);
392                 } else {
393                         /*
394                          * If we are acting as a multicast router, perform
395                          * multicast forwarding as if the packet had just
396                          * arrived on the interface to which we are about
397                          * to send.  The multicast forwarding function
398                          * recursively calls this function, using the
399                          * IP_FORWARDING flag to prevent infinite recursion.
400                          *
401                          * Multicasts that are looped back by ip_mloopback(),
402                          * above, will be forwarded by the ip_input() routine,
403                          * if necessary.
404                          */
405                         if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) {
406                                 /*
407                                  * If rsvp daemon is not running, do not
408                                  * set ip_moptions. This ensures that the packet
409                                  * is multicast and not just sent down one link
410                                  * as prescribed by rsvpd.
411                                  */
412                                 if (!V_rsvp_on)
413                                         imo = NULL;
414                                 if (ip_mforward &&
415                                     ip_mforward(ip, ifp, m, imo) != 0) {
416                                         m_freem(m);
417                                         goto done;
418                                 }
419                         }
420                 }
421
422                 /*
423                  * Multicasts with a time-to-live of zero may be looped-
424                  * back, above, but must not be transmitted on a network.
425                  * Also, multicasts addressed to the loopback interface
426                  * are not sent -- the above call to ip_mloopback() will
427                  * loop back a copy. ip_input() will drop the copy if
428                  * this host does not belong to the destination group on
429                  * the loopback interface.
430                  */
431                 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
432                         m_freem(m);
433                         goto done;
434                 }
435
436                 goto sendit;
437         }
438
439         /*
440          * If the source address is not specified yet, use the address
441          * of the outoing interface.
442          */
443         if (ip->ip_src.s_addr == INADDR_ANY) {
444                 /* Interface may have no addresses. */
445                 if (ia != NULL) {
446                         ip->ip_src = IA_SIN(ia)->sin_addr;
447                 }
448         }
449
450         /*
451          * Look for broadcast address and
452          * verify user is allowed to send
453          * such a packet.
454          */
455         if (isbroadcast) {
456                 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
457                         error = EADDRNOTAVAIL;
458                         goto bad;
459                 }
460                 if ((flags & IP_ALLOWBROADCAST) == 0) {
461                         error = EACCES;
462                         goto bad;
463                 }
464                 /* don't allow broadcast messages to be fragmented */
465                 if (ip_len > mtu) {
466                         error = EMSGSIZE;
467                         goto bad;
468                 }
469                 m->m_flags |= M_BCAST;
470         } else {
471                 m->m_flags &= ~M_BCAST;
472         }
473
474 sendit:
475 #ifdef IPSEC
476         switch(ip_ipsec_output(&m, inp, &flags, &error)) {
477         case 1:
478                 goto bad;
479         case -1:
480                 goto done;
481         case 0:
482         default:
483                 break;  /* Continue with packet processing. */
484         }
485         /*
486          * Check if there was a route for this packet; return error if not.
487          */
488         if (no_route_but_check_spd) {
489                 IPSTAT_INC(ips_noroute);
490                 error = EHOSTUNREACH;
491                 goto bad;
492         }
493         /* Update variables that are affected by ipsec4_output(). */
494         ip = mtod(m, struct ip *);
495         hlen = ip->ip_hl << 2;
496 #endif /* IPSEC */
497
498         /* Jump over all PFIL processing if hooks are not active. */
499         if (!PFIL_HOOKED(&V_inet_pfil_hook))
500                 goto passout;
501
502         /* Run through list of hooks for output packets. */
503         odst.s_addr = ip->ip_dst.s_addr;
504         error = pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
505         if (error != 0 || m == NULL)
506                 goto done;
507
508         ip = mtod(m, struct ip *);
509         needfiblookup = 0;
510
511         /* See if destination IP address was changed by packet filter. */
512         if (odst.s_addr != ip->ip_dst.s_addr) {
513                 m->m_flags |= M_SKIP_FIREWALL;
514                 /* If destination is now ourself drop to ip_input(). */
515                 if (in_localip(ip->ip_dst)) {
516                         m->m_flags |= M_FASTFWD_OURS;
517                         if (m->m_pkthdr.rcvif == NULL)
518                                 m->m_pkthdr.rcvif = V_loif;
519                         if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
520                                 m->m_pkthdr.csum_flags |=
521                                     CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
522                                 m->m_pkthdr.csum_data = 0xffff;
523                         }
524                         m->m_pkthdr.csum_flags |=
525                             CSUM_IP_CHECKED | CSUM_IP_VALID;
526 #ifdef SCTP
527                         if (m->m_pkthdr.csum_flags & CSUM_SCTP)
528                                 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
529 #endif
530                         error = netisr_queue(NETISR_IP, m);
531                         goto done;
532                 } else {
533                         if (have_ia_ref)
534                                 ifa_free(&ia->ia_ifa);
535                         needfiblookup = 1; /* Redo the routing table lookup. */
536                 }
537         }
538         /* See if fib was changed by packet filter. */
539         if (fibnum != M_GETFIB(m)) {
540                 m->m_flags |= M_SKIP_FIREWALL;
541                 fibnum = M_GETFIB(m);
542                 RO_RTFREE(ro);
543                 needfiblookup = 1;
544         }
545         if (needfiblookup)
546                 goto again;
547
548         /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
549         if (m->m_flags & M_FASTFWD_OURS) {
550                 if (m->m_pkthdr.rcvif == NULL)
551                         m->m_pkthdr.rcvif = V_loif;
552                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
553                         m->m_pkthdr.csum_flags |=
554                             CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
555                         m->m_pkthdr.csum_data = 0xffff;
556                 }
557 #ifdef SCTP
558                 if (m->m_pkthdr.csum_flags & CSUM_SCTP)
559                         m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
560 #endif
561                 m->m_pkthdr.csum_flags |=
562                             CSUM_IP_CHECKED | CSUM_IP_VALID;
563
564                 error = netisr_queue(NETISR_IP, m);
565                 goto done;
566         }
567         /* Or forward to some other address? */
568         if ((m->m_flags & M_IP_NEXTHOP) &&
569             (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
570                 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
571                 m->m_flags |= M_SKIP_FIREWALL;
572                 m->m_flags &= ~M_IP_NEXTHOP;
573                 m_tag_delete(m, fwd_tag);
574                 if (have_ia_ref)
575                         ifa_free(&ia->ia_ifa);
576                 goto again;
577         }
578
579 passout:
580         /* 127/8 must not appear on wire - RFC1122. */
581         if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
582             (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
583                 if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
584                         IPSTAT_INC(ips_badaddr);
585                         error = EADDRNOTAVAIL;
586                         goto bad;
587                 }
588         }
589
590         m->m_pkthdr.csum_flags |= CSUM_IP;
591         if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) {
592                 in_delayed_cksum(m);
593                 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
594         }
595 #ifdef SCTP
596         if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) {
597                 sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
598                 m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
599         }
600 #endif
601
602         /*
603          * If small enough for interface, or the interface will take
604          * care of the fragmentation for us, we can just send directly.
605          */
606         if (ip_len <= mtu ||
607             (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0) {
608                 ip->ip_sum = 0;
609                 if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) {
610                         ip->ip_sum = in_cksum(m, hlen);
611                         m->m_pkthdr.csum_flags &= ~CSUM_IP;
612                 }
613
614                 /*
615                  * Record statistics for this interface address.
616                  * With CSUM_TSO the byte/packet count will be slightly
617                  * incorrect because we count the IP+TCP headers only
618                  * once instead of for every generated packet.
619                  */
620                 if (!(flags & IP_FORWARDING) && ia) {
621                         if (m->m_pkthdr.csum_flags & CSUM_TSO)
622                                 counter_u64_add(ia->ia_ifa.ifa_opackets,
623                                     m->m_pkthdr.len / m->m_pkthdr.tso_segsz);
624                         else
625                                 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
626
627                         counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len);
628                 }
629 #ifdef MBUF_STRESS_TEST
630                 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
631                         m = m_fragment(m, M_NOWAIT, mbuf_frag_size);
632 #endif
633                 /*
634                  * Reset layer specific mbuf flags
635                  * to avoid confusing lower layers.
636                  */
637                 m_clrprotoflags(m);
638                 IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
639                 error = (*ifp->if_output)(ifp, m,
640                     (const struct sockaddr *)gw, ro);
641                 goto done;
642         }
643
644         /* Balk when DF bit is set or the interface didn't support TSO. */
645         if ((ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
646                 error = EMSGSIZE;
647                 IPSTAT_INC(ips_cantfrag);
648                 goto bad;
649         }
650
651         /*
652          * Too large for interface; fragment if possible. If successful,
653          * on return, m will point to a list of packets to be sent.
654          */
655         error = ip_fragment(ip, &m, mtu, ifp->if_hwassist);
656         if (error)
657                 goto bad;
658         for (; m; m = m0) {
659                 m0 = m->m_nextpkt;
660                 m->m_nextpkt = 0;
661                 if (error == 0) {
662                         /* Record statistics for this interface address. */
663                         if (ia != NULL) {
664                                 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
665                                 counter_u64_add(ia->ia_ifa.ifa_obytes,
666                                     m->m_pkthdr.len);
667                         }
668                         /*
669                          * Reset layer specific mbuf flags
670                          * to avoid confusing upper layers.
671                          */
672                         m_clrprotoflags(m);
673
674                         IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
675                         error = (*ifp->if_output)(ifp, m,
676                             (const struct sockaddr *)gw, ro);
677                 } else
678                         m_freem(m);
679         }
680
681         if (error == 0)
682                 IPSTAT_INC(ips_fragmented);
683
684 done:
685         if (ro == &iproute)
686                 RO_RTFREE(ro);
687         if (have_ia_ref)
688                 ifa_free(&ia->ia_ifa);
689         return (error);
690 bad:
691         m_freem(m);
692         goto done;
693 }
694
695 /*
696  * Create a chain of fragments which fit the given mtu. m_frag points to the
697  * mbuf to be fragmented; on return it points to the chain with the fragments.
698  * Return 0 if no error. If error, m_frag may contain a partially built
699  * chain of fragments that should be freed by the caller.
700  *
701  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
702  */
703 int
704 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
705     u_long if_hwassist_flags)
706 {
707         int error = 0;
708         int hlen = ip->ip_hl << 2;
709         int len = (mtu - hlen) & ~7;    /* size of payload in each fragment */
710         int off;
711         struct mbuf *m0 = *m_frag;      /* the original packet          */
712         int firstlen;
713         struct mbuf **mnext;
714         int nfrags;
715         uint16_t ip_len, ip_off;
716
717         ip_len = ntohs(ip->ip_len);
718         ip_off = ntohs(ip->ip_off);
719
720         if (ip_off & IP_DF) {   /* Fragmentation not allowed */
721                 IPSTAT_INC(ips_cantfrag);
722                 return EMSGSIZE;
723         }
724
725         /*
726          * Must be able to put at least 8 bytes per fragment.
727          */
728         if (len < 8)
729                 return EMSGSIZE;
730
731         /*
732          * If the interface will not calculate checksums on
733          * fragmented packets, then do it here.
734          */
735         if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
736                 in_delayed_cksum(m0);
737                 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
738         }
739 #ifdef SCTP
740         if (m0->m_pkthdr.csum_flags & CSUM_SCTP) {
741                 sctp_delayed_cksum(m0, hlen);
742                 m0->m_pkthdr.csum_flags &= ~CSUM_SCTP;
743         }
744 #endif
745         if (len > PAGE_SIZE) {
746                 /*
747                  * Fragment large datagrams such that each segment
748                  * contains a multiple of PAGE_SIZE amount of data,
749                  * plus headers. This enables a receiver to perform
750                  * page-flipping zero-copy optimizations.
751                  *
752                  * XXX When does this help given that sender and receiver
753                  * could have different page sizes, and also mtu could
754                  * be less than the receiver's page size ?
755                  */
756                 int newlen;
757                 struct mbuf *m;
758
759                 for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
760                         off += m->m_len;
761
762                 /*
763                  * firstlen (off - hlen) must be aligned on an
764                  * 8-byte boundary
765                  */
766                 if (off < hlen)
767                         goto smart_frag_failure;
768                 off = ((off - hlen) & ~7) + hlen;
769                 newlen = (~PAGE_MASK) & mtu;
770                 if ((newlen + sizeof (struct ip)) > mtu) {
771                         /* we failed, go back the default */
772 smart_frag_failure:
773                         newlen = len;
774                         off = hlen + len;
775                 }
776                 len = newlen;
777
778         } else {
779                 off = hlen + len;
780         }
781
782         firstlen = off - hlen;
783         mnext = &m0->m_nextpkt;         /* pointer to next packet */
784
785         /*
786          * Loop through length of segment after first fragment,
787          * make new header and copy data of each part and link onto chain.
788          * Here, m0 is the original packet, m is the fragment being created.
789          * The fragments are linked off the m_nextpkt of the original
790          * packet, which after processing serves as the first fragment.
791          */
792         for (nfrags = 1; off < ip_len; off += len, nfrags++) {
793                 struct ip *mhip;        /* ip header on the fragment */
794                 struct mbuf *m;
795                 int mhlen = sizeof (struct ip);
796
797                 m = m_gethdr(M_NOWAIT, MT_DATA);
798                 if (m == NULL) {
799                         error = ENOBUFS;
800                         IPSTAT_INC(ips_odropped);
801                         goto done;
802                 }
803                 m->m_flags |= (m0->m_flags & M_MCAST);
804                 /*
805                  * In the first mbuf, leave room for the link header, then
806                  * copy the original IP header including options. The payload
807                  * goes into an additional mbuf chain returned by m_copym().
808                  */
809                 m->m_data += max_linkhdr;
810                 mhip = mtod(m, struct ip *);
811                 *mhip = *ip;
812                 if (hlen > sizeof (struct ip)) {
813                         mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
814                         mhip->ip_v = IPVERSION;
815                         mhip->ip_hl = mhlen >> 2;
816                 }
817                 m->m_len = mhlen;
818                 /* XXX do we need to add ip_off below ? */
819                 mhip->ip_off = ((off - hlen) >> 3) + ip_off;
820                 if (off + len >= ip_len)
821                         len = ip_len - off;
822                 else
823                         mhip->ip_off |= IP_MF;
824                 mhip->ip_len = htons((u_short)(len + mhlen));
825                 m->m_next = m_copym(m0, off, len, M_NOWAIT);
826                 if (m->m_next == NULL) {        /* copy failed */
827                         m_free(m);
828                         error = ENOBUFS;        /* ??? */
829                         IPSTAT_INC(ips_odropped);
830                         goto done;
831                 }
832                 m->m_pkthdr.len = mhlen + len;
833                 m->m_pkthdr.rcvif = NULL;
834 #ifdef MAC
835                 mac_netinet_fragment(m0, m);
836 #endif
837                 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
838                 mhip->ip_off = htons(mhip->ip_off);
839                 mhip->ip_sum = 0;
840                 if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
841                         mhip->ip_sum = in_cksum(m, mhlen);
842                         m->m_pkthdr.csum_flags &= ~CSUM_IP;
843                 }
844                 *mnext = m;
845                 mnext = &m->m_nextpkt;
846         }
847         IPSTAT_ADD(ips_ofragments, nfrags);
848
849         /*
850          * Update first fragment by trimming what's been copied out
851          * and updating header.
852          */
853         m_adj(m0, hlen + firstlen - ip_len);
854         m0->m_pkthdr.len = hlen + firstlen;
855         ip->ip_len = htons((u_short)m0->m_pkthdr.len);
856         ip->ip_off = htons(ip_off | IP_MF);
857         ip->ip_sum = 0;
858         if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
859                 ip->ip_sum = in_cksum(m0, hlen);
860                 m0->m_pkthdr.csum_flags &= ~CSUM_IP;
861         }
862
863 done:
864         *m_frag = m0;
865         return error;
866 }
867
868 void
869 in_delayed_cksum(struct mbuf *m)
870 {
871         struct ip *ip;
872         uint16_t csum, offset, ip_len;
873
874         ip = mtod(m, struct ip *);
875         offset = ip->ip_hl << 2 ;
876         ip_len = ntohs(ip->ip_len);
877         csum = in_cksum_skip(m, ip_len, offset);
878         if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
879                 csum = 0xffff;
880         offset += m->m_pkthdr.csum_data;        /* checksum offset */
881
882         /* find the mbuf in the chain where the checksum starts*/
883         while ((m != NULL) && (offset >= m->m_len)) {
884                 offset -= m->m_len;
885                 m = m->m_next;
886         }
887         KASSERT(m != NULL, ("in_delayed_cksum: checksum outside mbuf chain."));
888         KASSERT(offset + sizeof(u_short) <= m->m_len, ("in_delayed_cksum: checksum split between mbufs."));
889         *(u_short *)(m->m_data + offset) = csum;
890 }
891
892 /*
893  * IP socket option processing.
894  */
895 int
896 ip_ctloutput(struct socket *so, struct sockopt *sopt)
897 {
898         struct  inpcb *inp = sotoinpcb(so);
899         int     error, optval;
900 #ifdef  RSS
901         uint32_t rss_bucket;
902         int retval;
903 #endif
904
905         error = optval = 0;
906         if (sopt->sopt_level != IPPROTO_IP) {
907                 error = EINVAL;
908
909                 if (sopt->sopt_level == SOL_SOCKET &&
910                     sopt->sopt_dir == SOPT_SET) {
911                         switch (sopt->sopt_name) {
912                         case SO_REUSEADDR:
913                                 INP_WLOCK(inp);
914                                 if ((so->so_options & SO_REUSEADDR) != 0)
915                                         inp->inp_flags2 |= INP_REUSEADDR;
916                                 else
917                                         inp->inp_flags2 &= ~INP_REUSEADDR;
918                                 INP_WUNLOCK(inp);
919                                 error = 0;
920                                 break;
921                         case SO_REUSEPORT:
922                                 INP_WLOCK(inp);
923                                 if ((so->so_options & SO_REUSEPORT) != 0)
924                                         inp->inp_flags2 |= INP_REUSEPORT;
925                                 else
926                                         inp->inp_flags2 &= ~INP_REUSEPORT;
927                                 INP_WUNLOCK(inp);
928                                 error = 0;
929                                 break;
930                         case SO_SETFIB:
931                                 INP_WLOCK(inp);
932                                 inp->inp_inc.inc_fibnum = so->so_fibnum;
933                                 INP_WUNLOCK(inp);
934                                 error = 0;
935                                 break;
936                         default:
937                                 break;
938                         }
939                 }
940                 return (error);
941         }
942
943         switch (sopt->sopt_dir) {
944         case SOPT_SET:
945                 switch (sopt->sopt_name) {
946                 case IP_OPTIONS:
947 #ifdef notyet
948                 case IP_RETOPTS:
949 #endif
950                 {
951                         struct mbuf *m;
952                         if (sopt->sopt_valsize > MLEN) {
953                                 error = EMSGSIZE;
954                                 break;
955                         }
956                         m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
957                         if (m == NULL) {
958                                 error = ENOBUFS;
959                                 break;
960                         }
961                         m->m_len = sopt->sopt_valsize;
962                         error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
963                                             m->m_len);
964                         if (error) {
965                                 m_free(m);
966                                 break;
967                         }
968                         INP_WLOCK(inp);
969                         error = ip_pcbopts(inp, sopt->sopt_name, m);
970                         INP_WUNLOCK(inp);
971                         return (error);
972                 }
973
974                 case IP_BINDANY:
975                         if (sopt->sopt_td != NULL) {
976                                 error = priv_check(sopt->sopt_td,
977                                     PRIV_NETINET_BINDANY);
978                                 if (error)
979                                         break;
980                         }
981                         /* FALLTHROUGH */
982                 case IP_BINDMULTI:
983 #ifdef  RSS
984                 case IP_RSS_LISTEN_BUCKET:
985 #endif
986                 case IP_TOS:
987                 case IP_TTL:
988                 case IP_MINTTL:
989                 case IP_RECVOPTS:
990                 case IP_RECVRETOPTS:
991                 case IP_RECVDSTADDR:
992                 case IP_RECVTTL:
993                 case IP_RECVIF:
994                 case IP_FAITH:
995                 case IP_ONESBCAST:
996                 case IP_DONTFRAG:
997                 case IP_RECVTOS:
998                 case IP_RECVFLOWID:
999 #ifdef  RSS
1000                 case IP_RECVRSSBUCKETID:
1001 #endif
1002                         error = sooptcopyin(sopt, &optval, sizeof optval,
1003                                             sizeof optval);
1004                         if (error)
1005                                 break;
1006
1007                         switch (sopt->sopt_name) {
1008                         case IP_TOS:
1009                                 inp->inp_ip_tos = optval;
1010                                 break;
1011
1012                         case IP_TTL:
1013                                 inp->inp_ip_ttl = optval;
1014                                 break;
1015
1016                         case IP_MINTTL:
1017                                 if (optval >= 0 && optval <= MAXTTL)
1018                                         inp->inp_ip_minttl = optval;
1019                                 else
1020                                         error = EINVAL;
1021                                 break;
1022
1023 #define OPTSET(bit) do {                                                \
1024         INP_WLOCK(inp);                                                 \
1025         if (optval)                                                     \
1026                 inp->inp_flags |= bit;                                  \
1027         else                                                            \
1028                 inp->inp_flags &= ~bit;                                 \
1029         INP_WUNLOCK(inp);                                               \
1030 } while (0)
1031
1032 #define OPTSET2(bit, val) do {                                          \
1033         INP_WLOCK(inp);                                                 \
1034         if (val)                                                        \
1035                 inp->inp_flags2 |= bit;                                 \
1036         else                                                            \
1037                 inp->inp_flags2 &= ~bit;                                \
1038         INP_WUNLOCK(inp);                                               \
1039 } while (0)
1040
1041                         case IP_RECVOPTS:
1042                                 OPTSET(INP_RECVOPTS);
1043                                 break;
1044
1045                         case IP_RECVRETOPTS:
1046                                 OPTSET(INP_RECVRETOPTS);
1047                                 break;
1048
1049                         case IP_RECVDSTADDR:
1050                                 OPTSET(INP_RECVDSTADDR);
1051                                 break;
1052
1053                         case IP_RECVTTL:
1054                                 OPTSET(INP_RECVTTL);
1055                                 break;
1056
1057                         case IP_RECVIF:
1058                                 OPTSET(INP_RECVIF);
1059                                 break;
1060
1061                         case IP_FAITH:
1062                                 OPTSET(INP_FAITH);
1063                                 break;
1064
1065                         case IP_ONESBCAST:
1066                                 OPTSET(INP_ONESBCAST);
1067                                 break;
1068                         case IP_DONTFRAG:
1069                                 OPTSET(INP_DONTFRAG);
1070                                 break;
1071                         case IP_BINDANY:
1072                                 OPTSET(INP_BINDANY);
1073                                 break;
1074                         case IP_RECVTOS:
1075                                 OPTSET(INP_RECVTOS);
1076                                 break;
1077                         case IP_BINDMULTI:
1078                                 OPTSET2(INP_BINDMULTI, optval);
1079                                 break;
1080                         case IP_RECVFLOWID:
1081                                 OPTSET2(INP_RECVFLOWID, optval);
1082                                 break;
1083 #ifdef  RSS
1084                         case IP_RSS_LISTEN_BUCKET:
1085                                 if ((optval >= 0) &&
1086                                     (optval < rss_getnumbuckets())) {
1087                                         inp->inp_rss_listen_bucket = optval;
1088                                         OPTSET2(INP_RSS_BUCKET_SET, 1);
1089                                 } else {
1090                                         error = EINVAL;
1091                                 }
1092                                 break;
1093                         case IP_RECVRSSBUCKETID:
1094                                 OPTSET2(INP_RECVRSSBUCKETID, optval);
1095                                 break;
1096 #endif
1097                         }
1098                         break;
1099 #undef OPTSET
1100 #undef OPTSET2
1101
1102                 /*
1103                  * Multicast socket options are processed by the in_mcast
1104                  * module.
1105                  */
1106                 case IP_MULTICAST_IF:
1107                 case IP_MULTICAST_VIF:
1108                 case IP_MULTICAST_TTL:
1109                 case IP_MULTICAST_LOOP:
1110                 case IP_ADD_MEMBERSHIP:
1111                 case IP_DROP_MEMBERSHIP:
1112                 case IP_ADD_SOURCE_MEMBERSHIP:
1113                 case IP_DROP_SOURCE_MEMBERSHIP:
1114                 case IP_BLOCK_SOURCE:
1115                 case IP_UNBLOCK_SOURCE:
1116                 case IP_MSFILTER:
1117                 case MCAST_JOIN_GROUP:
1118                 case MCAST_LEAVE_GROUP:
1119                 case MCAST_JOIN_SOURCE_GROUP:
1120                 case MCAST_LEAVE_SOURCE_GROUP:
1121                 case MCAST_BLOCK_SOURCE:
1122                 case MCAST_UNBLOCK_SOURCE:
1123                         error = inp_setmoptions(inp, sopt);
1124                         break;
1125
1126                 case IP_PORTRANGE:
1127                         error = sooptcopyin(sopt, &optval, sizeof optval,
1128                                             sizeof optval);
1129                         if (error)
1130                                 break;
1131
1132                         INP_WLOCK(inp);
1133                         switch (optval) {
1134                         case IP_PORTRANGE_DEFAULT:
1135                                 inp->inp_flags &= ~(INP_LOWPORT);
1136                                 inp->inp_flags &= ~(INP_HIGHPORT);
1137                                 break;
1138
1139                         case IP_PORTRANGE_HIGH:
1140                                 inp->inp_flags &= ~(INP_LOWPORT);
1141                                 inp->inp_flags |= INP_HIGHPORT;
1142                                 break;
1143
1144                         case IP_PORTRANGE_LOW:
1145                                 inp->inp_flags &= ~(INP_HIGHPORT);
1146                                 inp->inp_flags |= INP_LOWPORT;
1147                                 break;
1148
1149                         default:
1150                                 error = EINVAL;
1151                                 break;
1152                         }
1153                         INP_WUNLOCK(inp);
1154                         break;
1155
1156 #ifdef IPSEC
1157                 case IP_IPSEC_POLICY:
1158                 {
1159                         caddr_t req;
1160                         struct mbuf *m;
1161
1162                         if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1163                                 break;
1164                         if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1165                                 break;
1166                         req = mtod(m, caddr_t);
1167                         error = ipsec_set_policy(inp, sopt->sopt_name, req,
1168                             m->m_len, (sopt->sopt_td != NULL) ?
1169                             sopt->sopt_td->td_ucred : NULL);
1170                         m_freem(m);
1171                         break;
1172                 }
1173 #endif /* IPSEC */
1174
1175                 default:
1176                         error = ENOPROTOOPT;
1177                         break;
1178                 }
1179                 break;
1180
1181         case SOPT_GET:
1182                 switch (sopt->sopt_name) {
1183                 case IP_OPTIONS:
1184                 case IP_RETOPTS:
1185                         if (inp->inp_options)
1186                                 error = sooptcopyout(sopt,
1187                                                      mtod(inp->inp_options,
1188                                                           char *),
1189                                                      inp->inp_options->m_len);
1190                         else
1191                                 sopt->sopt_valsize = 0;
1192                         break;
1193
1194                 case IP_TOS:
1195                 case IP_TTL:
1196                 case IP_MINTTL:
1197                 case IP_RECVOPTS:
1198                 case IP_RECVRETOPTS:
1199                 case IP_RECVDSTADDR:
1200                 case IP_RECVTTL:
1201                 case IP_RECVIF:
1202                 case IP_PORTRANGE:
1203                 case IP_FAITH:
1204                 case IP_ONESBCAST:
1205                 case IP_DONTFRAG:
1206                 case IP_BINDANY:
1207                 case IP_RECVTOS:
1208                 case IP_BINDMULTI:
1209                 case IP_FLOWID:
1210                 case IP_FLOWTYPE:
1211                 case IP_RECVFLOWID:
1212 #ifdef  RSS
1213                 case IP_RSSBUCKETID:
1214                 case IP_RECVRSSBUCKETID:
1215 #endif
1216                         switch (sopt->sopt_name) {
1217
1218                         case IP_TOS:
1219                                 optval = inp->inp_ip_tos;
1220                                 break;
1221
1222                         case IP_TTL:
1223                                 optval = inp->inp_ip_ttl;
1224                                 break;
1225
1226                         case IP_MINTTL:
1227                                 optval = inp->inp_ip_minttl;
1228                                 break;
1229
1230 #define OPTBIT(bit)     (inp->inp_flags & bit ? 1 : 0)
1231 #define OPTBIT2(bit)    (inp->inp_flags2 & bit ? 1 : 0)
1232
1233                         case IP_RECVOPTS:
1234                                 optval = OPTBIT(INP_RECVOPTS);
1235                                 break;
1236
1237                         case IP_RECVRETOPTS:
1238                                 optval = OPTBIT(INP_RECVRETOPTS);
1239                                 break;
1240
1241                         case IP_RECVDSTADDR:
1242                                 optval = OPTBIT(INP_RECVDSTADDR);
1243                                 break;
1244
1245                         case IP_RECVTTL:
1246                                 optval = OPTBIT(INP_RECVTTL);
1247                                 break;
1248
1249                         case IP_RECVIF:
1250                                 optval = OPTBIT(INP_RECVIF);
1251                                 break;
1252
1253                         case IP_PORTRANGE:
1254                                 if (inp->inp_flags & INP_HIGHPORT)
1255                                         optval = IP_PORTRANGE_HIGH;
1256                                 else if (inp->inp_flags & INP_LOWPORT)
1257                                         optval = IP_PORTRANGE_LOW;
1258                                 else
1259                                         optval = 0;
1260                                 break;
1261
1262                         case IP_FAITH:
1263                                 optval = OPTBIT(INP_FAITH);
1264                                 break;
1265
1266                         case IP_ONESBCAST:
1267                                 optval = OPTBIT(INP_ONESBCAST);
1268                                 break;
1269                         case IP_DONTFRAG:
1270                                 optval = OPTBIT(INP_DONTFRAG);
1271                                 break;
1272                         case IP_BINDANY:
1273                                 optval = OPTBIT(INP_BINDANY);
1274                                 break;
1275                         case IP_RECVTOS:
1276                                 optval = OPTBIT(INP_RECVTOS);
1277                                 break;
1278                         case IP_FLOWID:
1279                                 optval = inp->inp_flowid;
1280                                 break;
1281                         case IP_FLOWTYPE:
1282                                 optval = inp->inp_flowtype;
1283                                 break;
1284                         case IP_RECVFLOWID:
1285                                 optval = OPTBIT2(INP_RECVFLOWID);
1286                                 break;
1287 #ifdef  RSS
1288                         case IP_RSSBUCKETID:
1289                                 retval = rss_hash2bucket(inp->inp_flowid,
1290                                     inp->inp_flowtype,
1291                                     &rss_bucket);
1292                                 if (retval == 0)
1293                                         optval = rss_bucket;
1294                                 else
1295                                         error = EINVAL;
1296                                 break;
1297                         case IP_RECVRSSBUCKETID:
1298                                 optval = OPTBIT2(INP_RECVRSSBUCKETID);
1299                                 break;
1300 #endif
1301                         case IP_BINDMULTI:
1302                                 optval = OPTBIT2(INP_BINDMULTI);
1303                                 break;
1304                         }
1305                         error = sooptcopyout(sopt, &optval, sizeof optval);
1306                         break;
1307
1308                 /*
1309                  * Multicast socket options are processed by the in_mcast
1310                  * module.
1311                  */
1312                 case IP_MULTICAST_IF:
1313                 case IP_MULTICAST_VIF:
1314                 case IP_MULTICAST_TTL:
1315                 case IP_MULTICAST_LOOP:
1316                 case IP_MSFILTER:
1317                         error = inp_getmoptions(inp, sopt);
1318                         break;
1319
1320 #ifdef IPSEC
1321                 case IP_IPSEC_POLICY:
1322                 {
1323                         struct mbuf *m = NULL;
1324                         caddr_t req = NULL;
1325                         size_t len = 0;
1326
1327                         if (m != 0) {
1328                                 req = mtod(m, caddr_t);
1329                                 len = m->m_len;
1330                         }
1331                         error = ipsec_get_policy(sotoinpcb(so), req, len, &m);
1332                         if (error == 0)
1333                                 error = soopt_mcopyout(sopt, m); /* XXX */
1334                         if (error == 0)
1335                                 m_freem(m);
1336                         break;
1337                 }
1338 #endif /* IPSEC */
1339
1340                 default:
1341                         error = ENOPROTOOPT;
1342                         break;
1343                 }
1344                 break;
1345         }
1346         return (error);
1347 }
1348
1349 /*
1350  * Routine called from ip_output() to loop back a copy of an IP multicast
1351  * packet to the input queue of a specified interface.  Note that this
1352  * calls the output routine of the loopback "driver", but with an interface
1353  * pointer that might NOT be a loopback interface -- evil, but easier than
1354  * replicating that code here.
1355  */
1356 static void
1357 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst,
1358     int hlen)
1359 {
1360         register struct ip *ip;
1361         struct mbuf *copym;
1362
1363         /*
1364          * Make a deep copy of the packet because we're going to
1365          * modify the pack in order to generate checksums.
1366          */
1367         copym = m_dup(m, M_NOWAIT);
1368         if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
1369                 copym = m_pullup(copym, hlen);
1370         if (copym != NULL) {
1371                 /* If needed, compute the checksum and mark it as valid. */
1372                 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1373                         in_delayed_cksum(copym);
1374                         copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1375                         copym->m_pkthdr.csum_flags |=
1376                             CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1377                         copym->m_pkthdr.csum_data = 0xffff;
1378                 }
1379                 /*
1380                  * We don't bother to fragment if the IP length is greater
1381                  * than the interface's MTU.  Can this possibly matter?
1382                  */
1383                 ip = mtod(copym, struct ip *);
1384                 ip->ip_sum = 0;
1385                 ip->ip_sum = in_cksum(copym, hlen);
1386 #if 1 /* XXX */
1387                 if (dst->sin_family != AF_INET) {
1388                         printf("ip_mloopback: bad address family %d\n",
1389                                                 dst->sin_family);
1390                         dst->sin_family = AF_INET;
1391                 }
1392 #endif
1393                 if_simloop(ifp, copym, dst->sin_family, 0);
1394         }
1395 }