source: rtems/cpukit/libnetworking/netinet/ip_output.c @ 657e6c93

5
Last change on this file since 657e6c93 was 657e6c93, checked in by Christian Mauderer <Christian.Mauderer@…>, on 06/24/16 at 05:57:17

libnetworking: Import current <netinet/in.h>

Import the <netinet/in.h> from current FreeBSD. This allows to build
some current software (e.g. libressl).

Add legacy support like

  • prototype for in_cksum(),
  • IPPORT_USERRESERVED,
  • deprecated IPCTL_RT* defines,
  • ip_fw_chk_t and ip_fw_ctl_t,
  • ip_nat_... (IP NAT hooks), and
  • IP_NAT option for get/setsockopt()

to new <rtems/rtems_netinet_in.h>.

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