source: rtems/cpukit/libnetworking/netinet/ip_output.c @ f94e799e

4.104.114.84.95
Last change on this file since f94e799e was f94e799e, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/29/07 at 07:50:03

Preps to eliminate _IP_VHL (Abandoned in FreeBSD). Misc mergers from upstream FreeBSD.

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