Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/netinet - Make ip_setmoptions(), ip_getmoptions() and ip...



details:   https://anonhg.NetBSD.org/src/rev/362367b850a8
branches:  trunk
changeset: 329395:362367b850a8
user:      rmind <rmind%NetBSD.org@localhost>
date:      Thu May 22 23:42:53 2014 +0000

description:
- Make ip_setmoptions(), ip_getmoptions() and ip_pcbopts() static.
- ip_output: eliminate 7th variadic argument; IP_RETURNMTU is flag
  always used to store MTU size into struct inpcb::inp_errormtu.
- Clean up these routines: reduce #ifdefs, variable scopes, etc.

diffstat:

 sys/netinet/ip_output.c |  207 +++++++++++++++++++++--------------------------
 sys/netinet/ip_var.h    |    5 +-
 sys/netinet/raw_ip.c    |   13 ++-
 3 files changed, 104 insertions(+), 121 deletions(-)

diffs (truncated from 606 to 300 lines):

diff -r c8992b008bfe -r 362367b850a8 sys/netinet/ip_output.c
--- a/sys/netinet/ip_output.c   Thu May 22 22:56:53 2014 +0000
+++ b/sys/netinet/ip_output.c   Thu May 22 23:42:53 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ip_output.c,v 1.225 2014/05/17 21:26:20 rmind Exp $    */
+/*     $NetBSD: ip_output.c,v 1.226 2014/05/22 23:42:53 rmind Exp $    */
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -91,17 +91,15 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ip_output.c,v 1.225 2014/05/17 21:26:20 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ip_output.c,v 1.226 2014/05/22 23:42:53 rmind Exp $");
 
 #include "opt_inet.h"
 #include "opt_ipsec.h"
 #include "opt_mrouting.h"
 
 #include <sys/param.h>
-#include <sys/malloc.h>
 #include <sys/kmem.h>
 #include <sys/mbuf.h>
-#include <sys/errno.h>
 #include <sys/protosw.h>
 #include <sys/socket.h>
 #include <sys/socketvar.h>
@@ -110,7 +108,6 @@
 #include <sys/domain.h>
 #endif
 #include <sys/systm.h>
-#include <sys/proc.h>
 
 #include <net/if.h>
 #include <net/route.h>
@@ -134,10 +131,13 @@
 #include <netipsec/ipsec.h>
 #include <netipsec/key.h>
 
+static int ip_pcbopts(struct inpcb *, const struct sockopt *);
 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
 static void ip_mloopback(struct ifnet *, struct mbuf *,
     const struct sockaddr_in *);
+static int ip_setmoptions(struct inpcb *, const struct sockopt *);
+static int ip_getmoptions(struct inpcb *, struct sockopt *);
 
 extern pfil_head_t *inet_pfil_hook;                    /* XXX */
 
@@ -164,7 +164,7 @@
        struct ifaddr *xifa;
        struct mbuf *opt;
        struct route *ro;
-       int flags, sw_csum, *mtu_p;
+       int flags, sw_csum;
        u_long mtu;
        struct ip_moptions *imo;
        struct socket *so;
@@ -187,35 +187,23 @@
        flags = va_arg(ap, int);
        imo = va_arg(ap, struct ip_moptions *);
        so = va_arg(ap, struct socket *);
-       if (flags & IP_RETURNMTU)
-               mtu_p = va_arg(ap, int *);
-       else
-               mtu_p = NULL;
        va_end(ap);
 
        MCLAIM(m, &ip_tx_mowner);
 
-#ifdef DIAGNOSTIC
-       if ((m->m_flags & M_PKTHDR) == 0)
-               panic("ip_output: no HDR");
+       KASSERT(solocked(so));
+       KASSERT((m->m_flags & M_PKTHDR) != 0);
+       KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) == 0);
+       KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) !=
+           (M_CSUM_TCPv4|M_CSUM_UDPv4));
 
-       if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) != 0) {
-               panic("ip_output: IPv6 checksum offload flags: %d",
-                   m->m_pkthdr.csum_flags);
-       }
-
-       if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) ==
-           (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
-               panic("ip_output: conflicting checksum offload flags: %d",
-                   m->m_pkthdr.csum_flags);
-       }
-#endif
        if (opt) {
                m = ip_insertoptions(m, opt, &len);
                if (len >= sizeof(struct ip))
                        hlen = len;
        }
        ip = mtod(m, struct ip *);
+
        /*
         * Fill in IP header.
         */
@@ -228,6 +216,7 @@
        } else {
                hlen = ip->ip_hl << 2;
        }
+
        /*
         * Route packet.
         */
@@ -236,17 +225,15 @@
                ro = &iproute;
        sockaddr_in_init(&u.dst4, &ip->ip_dst, 0);
        dst = satocsin(rtcache_getdst(ro));
+
        /*
-        * If there is a cached route,
-        * check that it is to the same destination
-        * and is still up.  If not, free it and try again.
-        * The address family should also be checked in case of sharing the
-        * cache with IPv6.
+        * If there is a cached route, check that it is to the same
+        * destination and is still up.  If not, free it and try again.
+        * The address family should also be checked in case of sharing
+        * the cache with IPv6.
         */
-       if (dst == NULL)
-               ;
-       else if (dst->sin_family != AF_INET ||
-                !in_hosteq(dst->sin_addr, ip->ip_dst))
+       if (dst && (dst->sin_family != AF_INET ||
+           !in_hosteq(dst->sin_addr, ip->ip_dst)))
                rtcache_free(ro);
 
        if ((rt = rtcache_validate(ro)) == NULL &&
@@ -254,9 +241,9 @@
                dst = &u.dst4;
                rtcache_setdst(ro, &u.dst);
        }
+
        /*
-        * If routing to interface only,
-        * short circuit routing lookup.
+        * If routing to interface only, short circuit routing lookup.
         */
        if (flags & IP_ROUTETOIF) {
                if ((ia = ifatoia(ifa_ifwithladdr(sintocsa(dst)))) == NULL) {
@@ -289,6 +276,7 @@
                if (rt->rt_flags & RTF_GATEWAY)
                        dst = satosin(rt->rt_gateway);
        }
+
        if (IN_MULTICAST(ip->ip_dst.s_addr) ||
            (ip->ip_dst.s_addr == INADDR_BROADCAST)) {
                struct in_multi *inm;
@@ -582,8 +570,10 @@
         * Must be able to put at least 8 bytes per fragment.
         */
        if (ntohs(ip->ip_off) & IP_DF) {
-               if (flags & IP_RETURNMTU)
-                       *mtu_p = mtu;
+               if (flags & IP_RETURNMTU) {
+                       struct inpcb *inp = sotoinpcb(so);
+                       inp->inp_errormtu = mtu;
+               }
                error = EMSGSIZE;
                IP_STATINC(IP_STAT_CANTFRAG);
                goto bad;
@@ -613,15 +603,14 @@
                        if (natt_frag) {
                                error = ip_output(m, opt, ro,
                                    flags | IP_RAWOUTPUT | IP_NOIPNEWID,
-                                   imo, so, mtu_p);
+                                   imo, so);
                        } else {
                                KASSERT((m->m_pkthdr.csum_flags &
                                    (M_CSUM_UDPv4 | M_CSUM_TCPv4)) == 0);
                                KERNEL_LOCK(1, NULL);
                                error = (*ifp->if_output)(ifp, m,
                                    (m->m_flags & M_MCAST) ?
-                                       sintocsa(rdst) : sintocsa(dst),
-                                   rt);
+                                   sintocsa(rdst) : sintocsa(dst), rt);
                                KERNEL_UNLOCK_ONE(NULL);
                        }
                } else
@@ -816,13 +805,12 @@
 {
        struct mbuf *m = inp->inp_options;
 
-       if (m && m->m_len > offsetof(struct ipoption, ipopt_dst))
+       if (m && m->m_len > offsetof(struct ipoption, ipopt_dst)) {
                return (m->m_len - offsetof(struct ipoption, ipopt_dst));
-       else
-               return 0;
+       }
+       return 0;
 }
 
-
 /*
  * Insert IP options into preformed packet.
  * Adjust IP destination as required for IP source routing,
@@ -890,16 +878,12 @@
                        optlen = 1;
                        continue;
                }
-#ifdef DIAGNOSTIC
-               if (cnt < IPOPT_OLEN + sizeof(*cp))
-                       panic("malformed IPv4 option passed to ip_optcopy");
-#endif
+
+               KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp));
                optlen = cp[IPOPT_OLEN];
-#ifdef DIAGNOSTIC
-               if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
-                       panic("malformed IPv4 option passed to ip_optcopy");
-#endif
-               /* bogus lengths should have been caught by ip_dooptions */
+               KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen < cnt);
+
+               /* Invalid lengths should have been caught by ip_dooptions. */
                if (optlen > cnt)
                        optlen = cnt;
                if (IPOPT_COPIED(opt)) {
@@ -919,11 +903,9 @@
 ip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
 {
        struct inpcb *inp = sotoinpcb(so);
-       int optval = 0;
-       int error = 0;
-#if defined(IPSEC)
-       struct lwp *l = curlwp; /*XXX*/
-#endif
+       struct ip *ip = &inp->inp_ip;
+       int inpflags = inp->inp_flags;
+       int optval = 0, error = 0;
 
        if (sopt->sopt_level != IPPROTO_IP) {
                if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER)
@@ -938,7 +920,7 @@
 #ifdef notyet
                case IP_RETOPTS:
 #endif
-                       error = ip_pcbopts(&inp->inp_options, sopt);
+                       error = ip_pcbopts(inp, sopt);
                        break;
 
                case IP_TOS:
@@ -957,11 +939,11 @@
 
                        switch (sopt->sopt_name) {
                        case IP_TOS:
-                               inp->inp_ip.ip_tos = optval;
+                               ip->ip_tos = optval;
                                break;
 
                        case IP_TTL:
-                               inp->inp_ip.ip_ttl = optval;
+                               ip->ip_ttl = optval;
                                break;
 
                        case IP_MINTTL:
@@ -972,9 +954,9 @@
                                break;
 #define        OPTSET(bit) \
        if (optval) \
-               inp->inp_flags |= bit; \
+               inpflags |= bit; \
        else \
-               inp->inp_flags &= ~bit;
+               inpflags &= ~bit;
 
                        case IP_PKTINFO:
                                OPTSET(INP_PKTINFO);
@@ -1012,7 +994,7 @@
                case IP_MULTICAST_LOOP:
                case IP_ADD_MEMBERSHIP:
                case IP_DROP_MEMBERSHIP:
-                       error = ip_setmoptions(&inp->inp_moptions, sopt);
+                       error = ip_setmoptions(inp, sopt);
                        break;
 
                case IP_PORTRANGE:
@@ -1020,22 +1002,20 @@
                        if (error)
                                break;
 
-                       /* INP_LOCK(inp); */
                        switch (optval) {
                        case IP_PORTRANGE_DEFAULT:
                        case IP_PORTRANGE_HIGH:
-                               inp->inp_flags &= ~(INP_LOWPORT);
+                               inpflags &= ~(INP_LOWPORT);
                                break;
 
                        case IP_PORTRANGE_LOW:
-                               inp->inp_flags |= INP_LOWPORT;
+                               inpflags |= INP_LOWPORT;
                                break;
 



Home | Main Index | Thread Index | Old Index