source: rtems/cpukit/libnetworking/netinet/ip_output.c @ 084492f

4.104.114.95
Last change on this file since 084492f was 793249a, checked in by Till Straumann <strauman@…>, on 05/23/08 at 21:48:06

2008-05-23 Till Straumann <strauman@…>

  • libnetworking/netinet/ip_output.c: when fragmenting multicast packets M_MCAST must be set on all fragments. This was fixed in FreeBSD ip_output.c 1.82 on 1998/8/23 ! (see my email to rtems-users from 2008/5/15).
  • Property mode set to 100644
File size: 31.8 KB
Line 
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 *      The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *      @(#)ip_output.c 8.3 (Berkeley) 1/21/94
30 * $FreeBSD: src/sys/netinet/ip_output.c,v 1.271 2007/03/23 09:43:36 bms Exp $
31 */
32
33/*
34 *      $Id$
35 */
36
37#define _IP_VHL
38
39#include <sys/param.h>
40#include <rtems/bsd/sys/queue.h>
41#include <sys/systm.h>
42#include <sys/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/errno.h>
45#include <sys/protosw.h>
46#include <sys/socket.h>
47#include <sys/socketvar.h>
48
49#include <net/if.h>
50#include <net/route.h>
51
52#include <netinet/in.h>
53#include <netinet/in_systm.h>
54#include <netinet/ip.h>
55#include <netinet/in_pcb.h>
56#include <netinet/in_var.h>
57#include <netinet/ip_var.h>
58
59#include <machine/in_cksum.h>
60
61#if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1
62#undef COMPAT_IPFW
63#define COMPAT_IPFW 1
64#else
65#undef COMPAT_IPFW
66#endif
67
68u_short ip_id;
69
70static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
71static void     ip_mloopback
72        (struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
73static int      ip_getmoptions
74        (int, struct ip_moptions *, struct mbuf **);
75static int      ip_optcopy(struct ip *, struct ip *);
76static int      ip_pcbopts(struct mbuf **, struct mbuf *);
77static int      ip_setmoptions
78        (int, struct ip_moptions **, struct mbuf *);
79
80extern  struct protosw inetsw[];
81
82/*
83 * IP output.  The packet in mbuf chain m contains a skeletal IP
84 * header (with len, off, ttl, proto, tos, src, dst).
85 * The mbuf chain containing the packet will be freed.
86 * The mbuf opt, if present, will not be freed.
87 */
88int
89ip_output(m0, opt, ro, flags, imo)
90        struct mbuf *m0;
91        struct mbuf *opt;
92        struct route *ro;
93        int flags;
94        struct ip_moptions *imo;
95{
96        struct ip *ip, *mhip;
97        struct ifnet *ifp;
98        struct mbuf *m = m0;
99        int hlen = sizeof (struct ip);
100        int len = 0, off, error = 0;
101        struct sockaddr_in *dst;
102        struct in_ifaddr *ia;
103        int isbroadcast;
104
105#ifdef  DIAGNOSTIC
106        if ((m->m_flags & M_PKTHDR) == 0)
107                panic("ip_output no HDR");
108        if (!ro)
109                panic("ip_output no route, proto = %d",
110                      mtod(m, struct ip *)->ip_p);
111#endif
112        if (opt) {
113                m = ip_insertoptions(m, opt, &len);
114                hlen = len;
115        }
116        ip = mtod(m, struct ip *);
117        /*
118         * Fill in IP header.
119         */
120        if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
121#ifdef _IP_VHL
122                ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
123#else
124                ip->ip_v = IPVERSION;
125                ip->ip_hl = hlen >> 2;
126#endif
127                ip->ip_off &= IP_DF;
128                ip->ip_id = htons(ip_id++);
129                ipstat.ips_localout++;
130        } else {
131#ifdef _IP_VHL
132                hlen = IP_VHL_HL(ip->ip_vhl) << 2;
133#else
134                hlen = ip->ip_hl << 2;
135#endif
136        }
137
138        dst = (struct sockaddr_in *)&ro->ro_dst;
139        /*
140         * If there is a cached route,
141         * check that it is to the same destination
142         * and is still up.  If not, free it and try again.
143         */
144        if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
145           dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
146                RTFREE(ro->ro_rt);
147                ro->ro_rt = (struct rtentry *)0;
148        }
149        if (ro->ro_rt == NULL) {
150                dst->sin_family = AF_INET;
151                dst->sin_len = sizeof(*dst);
152                dst->sin_addr = ip->ip_dst;
153        }
154        /*
155         * If routing to interface only,
156         * short circuit routing lookup.
157         */
158#define ifatoia(ifa)    ((struct in_ifaddr *)(ifa))
159#define sintosa(sin)    ((struct sockaddr *)(sin))
160        if (flags & IP_ROUTETOIF) {
161                if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
162                    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
163                        ipstat.ips_noroute++;
164                        error = ENETUNREACH;
165                        goto bad;
166                }
167                ifp = ia->ia_ifp;
168                ip->ip_ttl = 1;
169                isbroadcast = in_broadcast(dst->sin_addr, ifp);
170        } else {
171                /*
172                 * If this is the case, we probably don't want to allocate
173                 * a protocol-cloned route since we didn't get one from the
174                 * ULP.  This lets TCP do its thing, while not burdening
175                 * forwarding or ICMP with the overhead of cloning a route.
176                 * Of course, we still want to do any cloning requested by
177                 * the link layer, as this is probably required in all cases
178                 * for correct operation (as it is for ARP).
179                 */
180                if (ro->ro_rt == 0)
181                        rtalloc_ign(ro, RTF_PRCLONING);
182                if (ro->ro_rt == 0) {
183                        ipstat.ips_noroute++;
184                        error = EHOSTUNREACH;
185                        goto bad;
186                }
187                ia = ifatoia(ro->ro_rt->rt_ifa);
188                ifp = ro->ro_rt->rt_ifp;
189                ro->ro_rt->rt_use++;
190                if (ro->ro_rt->rt_flags & RTF_GATEWAY)
191                        dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
192                if (ro->ro_rt->rt_flags & RTF_HOST)
193                        isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
194                else
195                        isbroadcast = in_broadcast(dst->sin_addr, ifp);
196        }
197        if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
198                struct in_multi *inm;
199
200                m->m_flags |= M_MCAST;
201                /*
202                 * IP destination address is multicast.  Make sure "dst"
203                 * still points to the address in "ro".  (It may have been
204                 * changed to point to a gateway address, above.)
205                 */
206                dst = (struct sockaddr_in *)&ro->ro_dst;
207                /*
208                 * See if the caller provided any multicast options
209                 */
210                if (imo != NULL) {
211                        ip->ip_ttl = imo->imo_multicast_ttl;
212                        if (imo->imo_multicast_ifp != NULL)
213                                ifp = imo->imo_multicast_ifp;
214                        if (imo->imo_multicast_vif != -1)
215                                ip->ip_src.s_addr =
216                                    ip_mcast_src(imo->imo_multicast_vif);
217                } else
218                        ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
219                /*
220                 * Confirm that the outgoing interface supports multicast.
221                 */
222                if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
223                        if ((ifp->if_flags & IFF_MULTICAST) == 0) {
224                                ipstat.ips_noroute++;
225                                error = ENETUNREACH;
226                                goto bad;
227                        }
228                }
229                /*
230                 * If source address not specified yet, use address
231                 * of outgoing interface.
232                 */
233                if (ip->ip_src.s_addr == INADDR_ANY) {
234                        register struct in_ifaddr *ia;
235
236                        for (ia = in_ifaddr; ia; ia = ia->ia_next)
237                                if (ia->ia_ifp == ifp) {
238                                        ip->ip_src = IA_SIN(ia)->sin_addr;
239                                        break;
240                                }
241                }
242
243                IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
244                if (inm != NULL &&
245                   (imo == NULL || imo->imo_multicast_loop)) {
246                        /*
247                         * If we belong to the destination multicast group
248                         * on the outgoing interface, and the caller did not
249                         * forbid loopback, loop back a copy.
250                         */
251                        ip_mloopback(ifp, m, dst, hlen);
252                }
253                else {
254                        /*
255                         * If we are acting as a multicast router, perform
256                         * multicast forwarding as if the packet had just
257                         * arrived on the interface to which we are about
258                         * to send.  The multicast forwarding function
259                         * recursively calls this function, using the
260                         * IP_FORWARDING flag to prevent infinite recursion.
261                         *
262                         * Multicasts that are looped back by ip_mloopback(),
263                         * above, will be forwarded by the ip_input() routine,
264                         * if necessary.
265                         */
266                        if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
267                                /*
268                                 * Check if rsvp daemon is running. If not, don't
269                                 * set ip_moptions. This ensures that the packet
270                                 * is multicast and not just sent down one link
271                                 * as prescribed by rsvpd.
272                                 */
273                                if (!rsvp_on)
274                                  imo = NULL;
275                                if (ip_mforward(ip, ifp, m, imo) != 0) {
276                                        m_freem(m);
277                                        goto done;
278                                }
279                        }
280                }
281
282                /*
283                 * Multicasts with a time-to-live of zero may be looped-
284                 * back, above, but must not be transmitted on a network.
285                 * Also, multicasts addressed to the loopback interface
286                 * are not sent -- the above call to ip_mloopback() will
287                 * loop back a copy if this host actually belongs to the
288                 * destination group on the loopback interface.
289                 */
290                if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
291                        m_freem(m);
292                        goto done;
293                }
294
295                goto sendit;
296        }
297#ifndef notdef
298        /*
299         * If source address not specified yet, use address
300         * of outgoing interface.
301         */
302        if (ip->ip_src.s_addr == INADDR_ANY)
303                ip->ip_src = IA_SIN(ia)->sin_addr;
304#endif
305        /*
306         * Verify that we have any chance at all of being able to queue
307         *      the packet or packet fragments
308         */
309        if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
310                ifp->if_snd.ifq_maxlen) {
311                        error = ENOBUFS;
312                        goto bad;
313        }
314
315        /*
316         * Look for broadcast address and
317         * verify user is allowed to send
318         * such a packet.
319         */
320        if (isbroadcast) {
321                if ((ifp->if_flags & IFF_BROADCAST) == 0) {
322                        error = EADDRNOTAVAIL;
323                        goto bad;
324                }
325                if ((flags & IP_ALLOWBROADCAST) == 0) {
326                        error = EACCES;
327                        goto bad;
328                }
329                /* don't allow broadcast messages to be fragmented */
330                if (ip->ip_len > ifp->if_mtu) {
331                        error = EMSGSIZE;
332                        goto bad;
333                }
334                m->m_flags |= M_BCAST;
335        } else {
336                m->m_flags &= ~M_BCAST;
337        }
338
339sendit:
340        /*
341         * IpHack's section.
342         * - Xlate: translate packet's addr/port (NAT).
343         * - Firewall: deny/allow/etc.
344         * - Wrap: fake packet's addr/port <unimpl.>
345         * - Encapsulate: put it in another IP and send out. <unimp.>
346         */
347
348#ifdef COMPAT_IPFW
349        if (ip_nat_ptr && !(*ip_nat_ptr)(&ip, &m, ifp, IP_NAT_OUT)) {
350                error = EACCES;
351                goto done;
352        }
353
354        /*
355         * Check with the firewall...
356         */
357        if (ip_fw_chk_ptr) {
358#ifdef IPDIVERT
359                ip_divert_port = (*ip_fw_chk_ptr)(&ip,
360                    hlen, ifp, ip_divert_ignore, &m);
361                ip_divert_ignore = 0;
362                if (ip_divert_port) {           /* Divert packet */
363                        (*inetsw[ip_protox[IPPROTO_DIVERT]].pr_input)(m, 0);
364                        goto done;
365                }
366#else
367                /* If ipfw says divert, we have to just drop packet */
368                if ((*ip_fw_chk_ptr)(&ip, hlen, ifp, 0, &m)) {
369                        m_freem(m);
370                        goto done;
371                }
372#endif
373                if (!m) {
374                        error = EACCES;
375                        goto done;
376                }
377        }
378#endif /* COMPAT_IPFW */
379
380        /*
381         * If small enough for interface, or the interface will take
382         * care of the fragmentation for us, we can just send directly.
383         */
384        if ((u_short)ip->ip_len <= ifp->if_mtu) {
385                ip->ip_len = htons(ip->ip_len);
386                ip->ip_off = htons(ip->ip_off);
387                ip->ip_sum = 0;
388#ifdef _IP_VHL
389                if (ip->ip_vhl == IP_VHL_BORING) {
390#else
391                if ((ip->ip_hl == 5) && (ip->ip_v = IPVERSION)) {
392#endif
393                        ip->ip_sum = in_cksum_hdr(ip);
394                } else {
395                        ip->ip_sum = in_cksum(m, hlen);
396                }
397                error = (*ifp->if_output)(ifp, m,
398                                (struct sockaddr *)dst, ro->ro_rt);
399                goto done;
400        }
401        /*
402         * Too large for interface; fragment if possible.
403         * Must be able to put at least 8 bytes per fragment.
404         */
405        if (ip->ip_off & IP_DF) {
406                error = EMSGSIZE;
407                /*
408                 * This case can happen if the user changed the MTU
409                 * of an interface after enabling IP on it.  Because
410                 * most netifs don't keep track of routes pointing to
411                 * them, there is no way for one to update all its
412                 * routes when the MTU is changed.
413                 */
414                if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST))
415                    && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU)
416                    && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
417                        ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
418                }
419                ipstat.ips_cantfrag++;
420                goto bad;
421        }
422        len = (ifp->if_mtu - hlen) &~ 7;
423        if (len < 8) {
424                error = EMSGSIZE;
425                goto bad;
426        }
427
428    {
429        int mhlen, firstlen = len;
430        struct mbuf **mnext = &m->m_nextpkt;
431
432        /*
433         * Loop through length of segment after first fragment,
434         * make new header and copy data of each part and link onto chain.
435         */
436        m0 = m;
437        mhlen = sizeof (struct ip);
438        for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
439                MGETHDR(m, M_DONTWAIT, MT_HEADER);
440                if (m == 0) {
441                        error = ENOBUFS;
442                        ipstat.ips_odropped++;
443                        goto sendorfree;
444                }
445                m->m_flags |= (m0->m_flags & M_MCAST);
446                m->m_data += max_linkhdr;
447                mhip = mtod(m, struct ip *);
448                *mhip = *ip;
449                if (hlen > sizeof (struct ip)) {
450                        mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
451#ifdef _IP_VHL
452                        mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
453#else
454                        mhip->ip_v = IPVERSION;
455                        mhip->ip_hl = mhlen >> 2;
456#endif
457                }
458                m->m_len = mhlen;
459                mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
460                if (ip->ip_off & IP_MF)
461                        mhip->ip_off |= IP_MF;
462                if (off + len >= (u_short)ip->ip_len)
463                        len = (u_short)ip->ip_len - off;
464                else
465                        mhip->ip_off |= IP_MF;
466                mhip->ip_len = htons((u_short)(len + mhlen));
467                m->m_next = m_copy(m0, off, len);
468                if (m->m_next == 0) {
469                        (void) m_free(m);
470                        error = ENOBUFS;        /* ??? */
471                        ipstat.ips_odropped++;
472                        goto sendorfree;
473                }
474                m->m_pkthdr.len = mhlen + len;
475                m->m_pkthdr.rcvif = NULL;
476                mhip->ip_off = htons(mhip->ip_off);
477                mhip->ip_sum = 0;
478#ifdef _IP_VHL
479                if (mhip->ip_vhl == IP_VHL_BORING) {
480#else
481                if ((mhip->ip_hl == 5) && (mhip->ip_v == IPVERSION) ) {
482#endif
483                        mhip->ip_sum = in_cksum_hdr(mhip);
484                } else {
485                        mhip->ip_sum = in_cksum(m, mhlen);
486                }
487                *mnext = m;
488                mnext = &m->m_nextpkt;
489                ipstat.ips_ofragments++;
490        }
491        /*
492         * Update first fragment by trimming what's been copied out
493         * and updating header, then send each fragment (in order).
494         */
495        m = m0;
496        m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
497        m->m_pkthdr.len = hlen + firstlen;
498        ip->ip_len = htons((u_short)m->m_pkthdr.len);
499        ip->ip_off |= IP_MF;
500        ip->ip_off = htons(ip->ip_off);
501        ip->ip_sum = 0;
502#ifdef _IP_VHL
503        if (ip->ip_vhl == IP_VHL_BORING) {
504#else
505        if ((ip->ip_hl == 5) && (ip->ip_v == IPVERSION) ) {
506#endif
507                ip->ip_sum = in_cksum_hdr(ip);
508        } else {
509                ip->ip_sum = in_cksum(m, hlen);
510        }
511sendorfree:
512        for (m = m0; m; m = m0) {
513                m0 = m->m_nextpkt;
514                m->m_nextpkt = 0;
515                if (error == 0)
516                        error = (*ifp->if_output)(ifp, m,
517                            (struct sockaddr *)dst, ro->ro_rt);
518                else
519                        m_freem(m);
520        }
521
522        if (error == 0)
523                ipstat.ips_fragmented++;
524    }
525done:
526        return (error);
527bad:
528        m_freem(m0);
529        goto done;
530}
531
532/*
533 * Insert IP options into preformed packet.
534 * Adjust IP destination as required for IP source routing,
535 * as indicated by a non-zero in_addr at the start of the options.
536 *
537 * XXX This routine assumes that the packet has no options in place.
538 */
539static struct mbuf *
540ip_insertoptions(m, opt, phlen)
541        register struct mbuf *m;
542        struct mbuf *opt;
543        int *phlen;
544{
545        register struct ipoption *p = mtod(opt, struct ipoption *);
546        struct mbuf *n;
547        register struct ip *ip = mtod(m, struct ip *);
548        uint32_t optlen;
549
550        optlen = opt->m_len - sizeof(p->ipopt_dst);
551        if (optlen + ip->ip_len > IP_MAXPACKET)
552                return (m);             /* XXX should fail */
553        if (p->ipopt_dst.s_addr)
554                ip->ip_dst = p->ipopt_dst;
555        if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
556                MGETHDR(n, M_DONTWAIT, MT_HEADER);
557                if (n == 0)
558                        return (m);
559                n->m_pkthdr.len = m->m_pkthdr.len + optlen;
560                m->m_len -= sizeof(struct ip);
561                m->m_data += sizeof(struct ip);
562                n->m_next = m;
563                m = n;
564                m->m_len = optlen + sizeof(struct ip);
565                m->m_data += max_linkhdr;
566                (void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
567        } else {
568                m->m_data -= optlen;
569                m->m_len += optlen;
570                m->m_pkthdr.len += optlen;
571                ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
572        }
573        ip = mtod(m, struct ip *);
574        bcopy(p->ipopt_list, ip + 1, optlen);
575        *phlen = sizeof(struct ip) + optlen;
576#ifdef _IP_VHL
577        ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
578#else
579        ip->ip_v = IPVERSION;
580        ip->ip_hl = *phlen >> 2;
581#endif
582        ip->ip_len += optlen;
583        return (m);
584}
585
586/*
587 * Copy options from ip to jp,
588 * omitting those not copied during fragmentation.
589 */
590static int
591ip_optcopy(ip, jp)
592        struct ip *ip, *jp;
593{
594        register u_char *cp, *dp;
595        int opt, optlen, cnt;
596
597        cp = (u_char *)(ip + 1);
598        dp = (u_char *)(jp + 1);
599#ifdef _IP_VHL
600        cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
601#else
602        cnt = (ip->ip_hl << 2) - sizeof (struct ip);
603#endif
604        for (; cnt > 0; cnt -= optlen, cp += optlen) {
605                opt = cp[0];
606                if (opt == IPOPT_EOL)
607                        break;
608                if (opt == IPOPT_NOP) {
609                        /* Preserve for IP mcast tunnel's LSRR alignment. */
610                        *dp++ = IPOPT_NOP;
611                        optlen = 1;
612                        continue;
613                } else
614                        optlen = cp[IPOPT_OLEN];
615                /* bogus lengths should have been caught by ip_dooptions */
616                if (optlen > cnt)
617                        optlen = cnt;
618                if (IPOPT_COPIED(opt)) {
619                        bcopy(cp, dp, optlen);
620                        dp += optlen;
621                }
622        }
623        for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
624                *dp++ = IPOPT_EOL;
625        return (optlen);
626}
627
628/*
629 * IP socket option processing.
630 */
631int
632ip_ctloutput(op, so, level, optname, mp)
633        int op;
634        struct socket *so;
635        int level, optname;
636        struct mbuf **mp;
637{
638        struct  inpcb *inp = sotoinpcb(so);
639        register struct mbuf *m = *mp;
640        register int optval = 0;
641        int error = 0;
642
643        if (level != IPPROTO_IP) {
644                error = EINVAL;
645                if (op == PRCO_SETOPT && *mp)
646                        (void) m_free(*mp);
647        } else switch (op) {
648
649        case PRCO_SETOPT:
650                switch (optname) {
651                case IP_OPTIONS:
652#ifdef notyet
653                case IP_RETOPTS:
654                        return (ip_pcbopts(optname, &inp->inp_options, m));
655#else
656                        return (ip_pcbopts(&inp->inp_options, m));
657#endif
658
659                case IP_TOS:
660                case IP_TTL:
661                case IP_RECVOPTS:
662                case IP_RECVRETOPTS:
663                case IP_RECVDSTADDR:
664                case IP_RECVIF:
665                        if (m == 0 || m->m_len != sizeof(int))
666                                error = EINVAL;
667                        else {
668                                optval = *mtod(m, int *);
669                                switch (optname) {
670
671                                case IP_TOS:
672                                        inp->inp_ip_tos = optval;
673                                        break;
674
675                                case IP_TTL:
676                                        inp->inp_ip_ttl = optval;
677                                        break;
678#define OPTSET(bit) \
679        if (optval) \
680                inp->inp_flags |= bit; \
681        else \
682                inp->inp_flags &= ~bit;
683
684                                case IP_RECVOPTS:
685                                        OPTSET(INP_RECVOPTS);
686                                        break;
687
688                                case IP_RECVRETOPTS:
689                                        OPTSET(INP_RECVRETOPTS);
690                                        break;
691
692                                case IP_RECVDSTADDR:
693                                        OPTSET(INP_RECVDSTADDR);
694                                        break;
695
696                                case IP_RECVIF:
697                                        OPTSET(INP_RECVIF);
698                                        break;
699                                }
700                        }
701                        break;
702#undef OPTSET
703
704                case IP_MULTICAST_IF:
705                case IP_MULTICAST_VIF:
706                case IP_MULTICAST_TTL:
707                case IP_MULTICAST_LOOP:
708                case IP_ADD_MEMBERSHIP:
709                case IP_DROP_MEMBERSHIP:
710                        error = ip_setmoptions(optname, &inp->inp_moptions, m);
711                        break;
712
713                case IP_PORTRANGE:
714                        if (m == 0 || m->m_len != sizeof(int))
715                                error = EINVAL;
716                        else {
717                                optval = *mtod(m, int *);
718
719                                switch (optval) {
720
721                                case IP_PORTRANGE_DEFAULT:
722                                        inp->inp_flags &= ~(INP_LOWPORT);
723                                        inp->inp_flags &= ~(INP_HIGHPORT);
724                                        break;
725
726                                case IP_PORTRANGE_HIGH:
727                                        inp->inp_flags &= ~(INP_LOWPORT);
728                                        inp->inp_flags |= INP_HIGHPORT;
729                                        break;
730
731                                case IP_PORTRANGE_LOW:
732                                        inp->inp_flags &= ~(INP_HIGHPORT);
733                                        inp->inp_flags |= INP_LOWPORT;
734                                        break;
735
736                                default:
737                                        error = EINVAL;
738                                        break;
739                                }
740                        }
741                        break;
742
743                default:
744                        error = ENOPROTOOPT;
745                        break;
746                }
747                if (m)
748                        (void)m_free(m);
749                break;
750
751        case PRCO_GETOPT:
752                switch (optname) {
753                case IP_OPTIONS:
754                case IP_RETOPTS:
755                        *mp = m = m_get(M_WAIT, MT_SOOPTS);
756                        if (inp->inp_options) {
757                                m->m_len = inp->inp_options->m_len;
758                                bcopy(mtod(inp->inp_options, void *),
759                                    mtod(m, void *), m->m_len);
760                        } else
761                                m->m_len = 0;
762                        break;
763
764                case IP_TOS:
765                case IP_TTL:
766                case IP_RECVOPTS:
767                case IP_RECVRETOPTS:
768                case IP_RECVDSTADDR:
769                case IP_RECVIF:
770                        *mp = m = m_get(M_WAIT, MT_SOOPTS);
771                        m->m_len = sizeof(int);
772                        switch (optname) {
773
774                        case IP_TOS:
775                                optval = inp->inp_ip_tos;
776                                break;
777
778                        case IP_TTL:
779                                optval = inp->inp_ip_ttl;
780                                break;
781
782#define OPTBIT(bit)     (inp->inp_flags & bit ? 1 : 0)
783
784                        case IP_RECVOPTS:
785                                optval = OPTBIT(INP_RECVOPTS);
786                                break;
787
788                        case IP_RECVRETOPTS:
789                                optval = OPTBIT(INP_RECVRETOPTS);
790                                break;
791
792                        case IP_RECVDSTADDR:
793                                optval = OPTBIT(INP_RECVDSTADDR);
794                                break;
795
796                        case IP_RECVIF:
797                                optval = OPTBIT(INP_RECVIF);
798                                break;
799                        }
800                        *mtod(m, int *) = optval;
801                        break;
802
803                case IP_MULTICAST_IF:
804                case IP_MULTICAST_VIF:
805                case IP_MULTICAST_TTL:
806                case IP_MULTICAST_LOOP:
807                case IP_ADD_MEMBERSHIP:
808                case IP_DROP_MEMBERSHIP:
809                        error = ip_getmoptions(optname, inp->inp_moptions, mp);
810                        break;
811
812                case IP_PORTRANGE:
813                        *mp = m = m_get(M_WAIT, MT_SOOPTS);
814                        m->m_len = sizeof(int);
815
816                        if (inp->inp_flags & INP_HIGHPORT)
817                                optval = IP_PORTRANGE_HIGH;
818                        else if (inp->inp_flags & INP_LOWPORT)
819                                optval = IP_PORTRANGE_LOW;
820                        else
821                                optval = 0;
822
823                        *mtod(m, int *) = optval;
824                        break;
825
826                default:
827                        error = ENOPROTOOPT;
828                        break;
829                }
830                break;
831        }
832        return (error);
833}
834
835/*
836 * Set up IP options in pcb for insertion in output packets.
837 * Store in mbuf with pointer in pcbopt, adding pseudo-option
838 * with destination address if source routed.
839 */
840static int
841#ifdef notyet
842ip_pcbopts(optname, pcbopt, m)
843        int optname;
844#else
845ip_pcbopts(pcbopt, m)
846#endif
847        struct mbuf **pcbopt;
848        register struct mbuf *m;
849{
850        register int cnt, optlen;
851        register u_char *cp;
852        u_char opt;
853
854        /* turn off any old options */
855        if (*pcbopt)
856                (void)m_free(*pcbopt);
857        *pcbopt = 0;
858        if (m == (struct mbuf *)0 || m->m_len == 0) {
859                /*
860                 * Only turning off any previous options.
861                 */
862                if (m)
863                        (void)m_free(m);
864                return (0);
865        }
866
867#ifndef vax
868        if (m->m_len % sizeof(long))
869                goto bad;
870#endif
871        /*
872         * IP first-hop destination address will be stored before
873         * actual options; move other options back
874         * and clear it when none present.
875         */
876        if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
877                goto bad;
878        cnt = m->m_len;
879        m->m_len += sizeof(struct in_addr);
880        cp = mtod(m, u_char *) + sizeof(struct in_addr);
881        ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
882        bzero(mtod(m, caddr_t), sizeof(struct in_addr));
883
884        for (; cnt > 0; cnt -= optlen, cp += optlen) {
885                opt = cp[IPOPT_OPTVAL];
886                if (opt == IPOPT_EOL)
887                        break;
888                if (opt == IPOPT_NOP)
889                        optlen = 1;
890                else {
891                        optlen = cp[IPOPT_OLEN];
892                        if (optlen <= IPOPT_OLEN || optlen > cnt)
893                                goto bad;
894                }
895                switch (opt) {
896
897                default:
898                        break;
899
900                case IPOPT_LSRR:
901                case IPOPT_SSRR:
902                        /*
903                         * user process specifies route as:
904                         *      ->A->B->C->D
905                         * D must be our final destination (but we can't
906                         * check that since we may not have connected yet).
907                         * A is first hop destination, which doesn't appear in
908                         * actual IP option, but is stored before the options.
909                         */
910                        if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
911                                goto bad;
912                        m->m_len -= sizeof(struct in_addr);
913                        cnt -= sizeof(struct in_addr);
914                        optlen -= sizeof(struct in_addr);
915                        cp[IPOPT_OLEN] = optlen;
916                        /*
917                         * Move first hop before start of options.
918                         */
919                        bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
920                            sizeof(struct in_addr));
921                        /*
922                         * Then copy rest of options back
923                         * to close up the deleted entry.
924                         */
925                        ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
926                            sizeof(struct in_addr)),
927                            (caddr_t)&cp[IPOPT_OFFSET+1],
928                            (unsigned)cnt + sizeof(struct in_addr));
929                        break;
930                }
931        }
932        if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
933                goto bad;
934        *pcbopt = m;
935        return (0);
936
937bad:
938        (void)m_free(m);
939        return (EINVAL);
940}
941
942/*
943 * Set the IP multicast options in response to user setsockopt().
944 */
945static int
946ip_setmoptions(optname, imop, m)
947        int optname;
948        struct ip_moptions **imop;
949        struct mbuf *m;
950{
951        int error = 0;
952        u_char loop;
953        int i;
954        struct in_addr addr;
955        struct ip_mreq *mreq;
956        struct ifnet *ifp;
957        struct ip_moptions *imo = *imop;
958        struct route ro;
959        register struct sockaddr_in *dst;
960        int s;
961
962        if (imo == NULL) {
963                /*
964                 * No multicast option buffer attached to the pcb;
965                 * allocate one and initialize to default values.
966                 */
967                imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
968                    M_WAITOK);
969
970                if (imo == NULL)
971                        return (ENOBUFS);
972                *imop = imo;
973                imo->imo_multicast_ifp = NULL;
974                imo->imo_multicast_vif = -1;
975                imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
976                imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
977                imo->imo_num_memberships = 0;
978        }
979
980        switch (optname) {
981        /* store an index number for the vif you wanna use in the send */
982        case IP_MULTICAST_VIF:
983                if (!legal_vif_num) {
984                        error = EOPNOTSUPP;
985                        break;
986                }
987                if (m == NULL || m->m_len != sizeof(int)) {
988                        error = EINVAL;
989                        break;
990                }
991                i = *(mtod(m, int *));
992                if (!legal_vif_num(i) && (i != -1)) {
993                        error = EINVAL;
994                        break;
995                }
996                imo->imo_multicast_vif = i;
997                break;
998
999        case IP_MULTICAST_IF:
1000                /*
1001                 * Select the interface for outgoing multicast packets.
1002                 */
1003                if (m == NULL || m->m_len != sizeof(struct in_addr)) {
1004                        error = EINVAL;
1005                        break;
1006                }
1007                addr = *(mtod(m, struct in_addr *));
1008                /*
1009                 * INADDR_ANY is used to remove a previous selection.
1010                 * When no interface is selected, a default one is
1011                 * chosen every time a multicast packet is sent.
1012                 */
1013                if (addr.s_addr == INADDR_ANY) {
1014                        imo->imo_multicast_ifp = NULL;
1015                        break;
1016                }
1017                /*
1018                 * The selected interface is identified by its local
1019                 * IP address.  Find the interface and confirm that
1020                 * it supports multicasting.
1021                 */
1022                s = splimp();
1023                INADDR_TO_IFP(addr, ifp);
1024                if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1025                        splx(s);
1026                        error = EADDRNOTAVAIL;
1027                        break;
1028                }
1029                imo->imo_multicast_ifp = ifp;
1030                splx(s);
1031                break;
1032
1033        case IP_MULTICAST_TTL:
1034                /*
1035                 * Set the IP time-to-live for outgoing multicast packets.
1036                 */
1037                if (m == NULL || m->m_len != 1) {
1038                        error = EINVAL;
1039                        break;
1040                }
1041                imo->imo_multicast_ttl = *(mtod(m, u_char *));
1042                break;
1043
1044        case IP_MULTICAST_LOOP:
1045                /*
1046                 * Set the loopback flag for outgoing multicast packets.
1047                 * Must be zero or one.
1048                 */
1049                if (m == NULL || m->m_len != 1 ||
1050                   (loop = *(mtod(m, u_char *))) > 1) {
1051                        error = EINVAL;
1052                        break;
1053                }
1054                imo->imo_multicast_loop = loop;
1055                break;
1056
1057        case IP_ADD_MEMBERSHIP:
1058                /*
1059                 * Add a multicast group membership.
1060                 * Group must be a valid IP multicast address.
1061                 */
1062                if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1063                        error = EINVAL;
1064                        break;
1065                }
1066                mreq = mtod(m, struct ip_mreq *);
1067                if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
1068                        error = EINVAL;
1069                        break;
1070                }
1071                s = splimp();
1072                /*
1073                 * If no interface address was provided, use the interface of
1074                 * the route to the given multicast address.
1075                 */
1076                if (mreq->imr_interface.s_addr == INADDR_ANY) {
1077                        bzero((caddr_t)&ro, sizeof(ro));
1078                        dst = (struct sockaddr_in *)&ro.ro_dst;
1079                        dst->sin_len = sizeof(*dst);
1080                        dst->sin_family = AF_INET;
1081                        dst->sin_addr = mreq->imr_multiaddr;
1082                        rtalloc(&ro);
1083                        if (ro.ro_rt == NULL) {
1084                                error = EADDRNOTAVAIL;
1085                                splx(s);
1086                                break;
1087                        }
1088                        ifp = ro.ro_rt->rt_ifp;
1089                        rtfree(ro.ro_rt);
1090                }
1091                else {
1092                        INADDR_TO_IFP(mreq->imr_interface, ifp);
1093                }
1094
1095                /*
1096                 * See if we found an interface, and confirm that it
1097                 * supports multicast.
1098                 */
1099                if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1100                        error = EADDRNOTAVAIL;
1101                        splx(s);
1102                        break;
1103                }
1104                /*
1105                 * See if the membership already exists or if all the
1106                 * membership slots are full.
1107                 */
1108                for (i = 0; i < imo->imo_num_memberships; ++i) {
1109                        if (imo->imo_membership[i]->inm_ifp == ifp &&
1110                            imo->imo_membership[i]->inm_addr.s_addr
1111                                                == mreq->imr_multiaddr.s_addr)
1112                                break;
1113                }
1114                if (i < imo->imo_num_memberships) {
1115                        error = EADDRINUSE;
1116                        splx(s);
1117                        break;
1118                }
1119                if (i == IP_MAX_MEMBERSHIPS) {
1120                        error = ETOOMANYREFS;
1121                        splx(s);
1122                        break;
1123                }
1124                /*
1125                 * Everything looks good; add a new record to the multicast
1126                 * address list for the given interface.
1127                 */
1128                if ((imo->imo_membership[i] =
1129                    in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1130                        error = ENOBUFS;
1131                        splx(s);
1132                        break;
1133                }
1134                ++imo->imo_num_memberships;
1135                splx(s);
1136                break;
1137
1138        case IP_DROP_MEMBERSHIP:
1139                /*
1140                 * Drop a multicast group membership.
1141                 * Group must be a valid IP multicast address.
1142                 */
1143                if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1144                        error = EINVAL;
1145                        break;
1146                }
1147                mreq = mtod(m, struct ip_mreq *);
1148                if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
1149                        error = EINVAL;
1150                        break;
1151                }
1152
1153                s = splimp();
1154                /*
1155                 * If an interface address was specified, get a pointer
1156                 * to its ifnet structure.
1157                 */
1158                if (mreq->imr_interface.s_addr == INADDR_ANY)
1159                        ifp = NULL;
1160                else {
1161                        INADDR_TO_IFP(mreq->imr_interface, ifp);
1162                        if (ifp == NULL) {
1163                                error = EADDRNOTAVAIL;
1164                                splx(s);
1165                                break;
1166                        }
1167                }
1168                /*
1169                 * Find the membership in the membership array.
1170                 */
1171                for (i = 0; i < imo->imo_num_memberships; ++i) {
1172                        if ((ifp == NULL ||
1173                             imo->imo_membership[i]->inm_ifp == ifp) &&
1174                             imo->imo_membership[i]->inm_addr.s_addr ==
1175                             mreq->imr_multiaddr.s_addr)
1176                                break;
1177                }
1178                if (i == imo->imo_num_memberships) {
1179                        error = EADDRNOTAVAIL;
1180                        splx(s);
1181                        break;
1182                }
1183                /*
1184                 * Give up the multicast address record to which the
1185                 * membership points.
1186                 */
1187                in_delmulti(imo->imo_membership[i]);
1188                /*
1189                 * Remove the gap in the membership array.
1190                 */
1191                for (++i; i < imo->imo_num_memberships; ++i)
1192                        imo->imo_membership[i-1] = imo->imo_membership[i];
1193                --imo->imo_num_memberships;
1194                splx(s);
1195                break;
1196
1197        default:
1198                error = EOPNOTSUPP;
1199                break;
1200        }
1201
1202        /*
1203         * If all options have default values, no need to keep the mbuf.
1204         */
1205        if (imo->imo_multicast_ifp == NULL &&
1206            imo->imo_multicast_vif == -1 &&
1207            imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1208            imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1209            imo->imo_num_memberships == 0) {
1210                free(*imop, M_IPMOPTS);
1211                *imop = NULL;
1212        }
1213
1214        return (error);
1215}
1216
1217/*
1218 * Return the IP multicast options in response to user getsockopt().
1219 */
1220static int
1221ip_getmoptions(optname, imo, mp)
1222        int optname;
1223        register struct ip_moptions *imo;
1224        register struct mbuf **mp;
1225{
1226        u_char *ttl;
1227        u_char *loop;
1228        struct in_addr *addr;
1229        struct in_ifaddr *ia;
1230
1231        *mp = m_get(M_WAIT, MT_SOOPTS);
1232
1233        switch (optname) {
1234
1235        case IP_MULTICAST_VIF:
1236                if (imo != NULL)
1237                        *(mtod(*mp, int *)) = imo->imo_multicast_vif;
1238                else
1239                        *(mtod(*mp, int *)) = -1;
1240                (*mp)->m_len = sizeof(int);
1241                return(0);
1242
1243        case IP_MULTICAST_IF:
1244                addr = mtod(*mp, struct in_addr *);
1245                (*mp)->m_len = sizeof(struct in_addr);
1246                if (imo == NULL || imo->imo_multicast_ifp == NULL)
1247                        addr->s_addr = INADDR_ANY;
1248                else {
1249                        IFP_TO_IA(imo->imo_multicast_ifp, ia);
1250                        addr->s_addr = (ia == NULL) ? INADDR_ANY
1251                                        : IA_SIN(ia)->sin_addr.s_addr;
1252                }
1253                return (0);
1254
1255        case IP_MULTICAST_TTL:
1256                ttl = mtod(*mp, u_char *);
1257                (*mp)->m_len = 1;
1258                *ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL
1259                                     : imo->imo_multicast_ttl;
1260                return (0);
1261
1262        case IP_MULTICAST_LOOP:
1263                loop = mtod(*mp, u_char *);
1264                (*mp)->m_len = 1;
1265                *loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP
1266                                      : imo->imo_multicast_loop;
1267                return (0);
1268
1269        default:
1270                return (EOPNOTSUPP);
1271        }
1272}
1273
1274/*
1275 * Discard the IP multicast options.
1276 */
1277void
1278ip_freemoptions(imo)
1279        register struct ip_moptions *imo;
1280{
1281        register int i;
1282
1283        if (imo != NULL) {
1284                for (i = 0; i < imo->imo_num_memberships; ++i)
1285                        in_delmulti(imo->imo_membership[i]);
1286                free(imo, M_IPMOPTS);
1287        }
1288}
1289
1290/*
1291 * Routine called from ip_output() to loop back a copy of an IP multicast
1292 * packet to the input queue of a specified interface.  Note that this
1293 * calls the output routine of the loopback "driver", but with an interface
1294 * pointer that might NOT be a loopback interface -- evil, but easier than
1295 * replicating that code here.
1296 */
1297static void
1298ip_mloopback(ifp, m, dst, hlen)
1299        struct ifnet *ifp;
1300        register struct mbuf *m;
1301        register struct sockaddr_in *dst;
1302        int hlen;
1303{
1304        register struct ip *ip;
1305        struct mbuf *copym;
1306
1307        copym = m_copy(m, 0, M_COPYALL);
1308        if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
1309                copym = m_pullup(copym, hlen);
1310        if (copym != NULL) {
1311                /*
1312                 * We don't bother to fragment if the IP length is greater
1313                 * than the interface's MTU.  Can this possibly matter?
1314                 */
1315                ip = mtod(copym, struct ip *);
1316                ip->ip_len = htons(ip->ip_len);
1317                ip->ip_off = htons(ip->ip_off);
1318                ip->ip_sum = 0;
1319#ifdef _IP_VHL
1320                if (ip->ip_vhl == IP_VHL_BORING) {
1321                        ip->ip_sum = in_cksum_hdr(ip);
1322                } else {
1323                        ip->ip_sum = in_cksum(copym, hlen);
1324                }
1325#else
1326                ip->ip_sum = in_cksum(copym, hlen);
1327#endif
1328                /*
1329                 * NB:
1330                 * It's not clear whether there are any lingering
1331                 * reentrancy problems in other areas which might
1332                 * be exposed by using ip_input directly (in
1333                 * particular, everything which modifies the packet
1334                 * in-place).  Yet another option is using the
1335                 * protosw directly to deliver the looped back
1336                 * packet.  For the moment, we'll err on the side
1337                 * of safety by continuing to abuse looutput().
1338                 */
1339#ifdef notdef
1340                copym->m_pkthdr.rcvif = ifp;
1341                ip_input(copym);
1342#else
1343                (void) looutput(ifp, copym, (struct sockaddr *)dst, NULL);
1344#endif
1345        }
1346}
Note: See TracBrowser for help on using the repository browser.