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