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