nfsd: nfsd should drop CAP_MKNOD for non-root
[safe/jmp/linux-2.6] / net / ipv4 / af_inet.c
index ea58448..743f554 100644 (file)
@@ -5,8 +5,6 @@
  *
  *             PF_INET protocol family socket handler.
  *
- * Version:    $Id: af_inet.c,v 1.137 2002/02/01 22:01:03 davem Exp $
- *
  * Authors:    Ross Biro
  *             Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  *             Florian La Roche, <flla@stud.uni-sb.de>
 #include <linux/init.h>
 #include <linux/poll.h>
 #include <linux/netfilter_ipv4.h>
+#include <linux/random.h>
 
 #include <asm/uaccess.h>
 #include <asm/system.h>
 
-#include <linux/smp_lock.h>
 #include <linux/inet.h>
 #include <linux/igmp.h>
 #include <linux/inetdevice.h>
 #include <linux/netdevice.h>
+#include <net/checksum.h>
 #include <net/ip.h>
 #include <net/protocol.h>
 #include <net/arp.h>
 #include <net/inet_connection_sock.h>
 #include <net/tcp.h>
 #include <net/udp.h>
+#include <net/udplite.h>
 #include <linux/skbuff.h>
 #include <net/sock.h>
 #include <net/raw.h>
 #include <net/ipip.h>
 #include <net/inet_common.h>
 #include <net/xfrm.h>
+#include <net/net_namespace.h>
 #ifdef CONFIG_IP_MROUTE
 #include <linux/mroute.h>
 #endif
 
-DEFINE_SNMP_STAT(struct linux_mib, net_statistics) __read_mostly;
-
 extern void ip_mc_drop_socket(struct sock *sk);
 
 /* The inetsw table contains everything that inet_create needs to
@@ -125,6 +124,10 @@ extern void ip_mc_drop_socket(struct sock *sk);
 static struct list_head inetsw[SOCK_MAX];
 static DEFINE_SPINLOCK(inetsw_lock);
 
+struct ipv4_config ipv4_config;
+
+EXPORT_SYMBOL(ipv4_config);
+
 /* New destruction routine */
 
 void inet_sock_destruct(struct sock *sk)
@@ -134,6 +137,8 @@ void inet_sock_destruct(struct sock *sk)
        __skb_queue_purge(&sk->sk_receive_queue);
        __skb_queue_purge(&sk->sk_error_queue);
 
+       sk_mem_reclaim(sk);
+
        if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
                printk("Attempt to release TCP socket in state %d %p\n",
                       sk->sk_state, sk);
@@ -144,10 +149,10 @@ void inet_sock_destruct(struct sock *sk)
                return;
        }
 
-       BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
-       BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
-       BUG_TRAP(!sk->sk_wmem_queued);
-       BUG_TRAP(!sk->sk_forward_alloc);
+       WARN_ON(atomic_read(&sk->sk_rmem_alloc));
+       WARN_ON(atomic_read(&sk->sk_wmem_alloc));
+       WARN_ON(sk->sk_wmem_queued);
+       WARN_ON(sk->sk_forward_alloc);
 
        kfree(inet->opt);
        dst_release(sk->sk_dst_cache);
@@ -204,7 +209,7 @@ int inet_listen(struct socket *sock, int backlog)
         * we can only allow the backlog to be adjusted.
         */
        if (old_state != TCP_LISTEN) {
-               err = inet_csk_listen_start(sk, TCP_SYNQ_HSIZE);
+               err = inet_csk_listen_start(sk, backlog);
                if (err)
                        goto out;
        }
@@ -216,14 +221,50 @@ out:
        return err;
 }
 
+u32 inet_ehash_secret __read_mostly;
+EXPORT_SYMBOL(inet_ehash_secret);
+
+/*
+ * inet_ehash_secret must be set exactly once
+ * Instead of using a dedicated spinlock, we (ab)use inetsw_lock
+ */
+void build_ehash_secret(void)
+{
+       u32 rnd;
+       do {
+               get_random_bytes(&rnd, sizeof(rnd));
+       } while (rnd == 0);
+       spin_lock_bh(&inetsw_lock);
+       if (!inet_ehash_secret)
+               inet_ehash_secret = rnd;
+       spin_unlock_bh(&inetsw_lock);
+}
+EXPORT_SYMBOL(build_ehash_secret);
+
+static inline int inet_netns_ok(struct net *net, int protocol)
+{
+       int hash;
+       struct net_protocol *ipprot;
+
+       if (net_eq(net, &init_net))
+               return 1;
+
+       hash = protocol & (MAX_INET_PROTOS - 1);
+       ipprot = rcu_dereference(inet_protos[hash]);
+
+       if (ipprot == NULL)
+               /* raw IP is OK */
+               return 1;
+       return ipprot->netns_ok;
+}
+
 /*
  *     Create an inet socket.
  */
 
-static int inet_create(struct socket *sock, int protocol)
+static int inet_create(struct net *net, struct socket *sock, int protocol)
 {
        struct sock *sk;
-       struct list_head *p;
        struct inet_protosw *answer;
        struct inet_sock *inet;
        struct proto *answer_prot;
@@ -232,16 +273,19 @@ static int inet_create(struct socket *sock, int protocol)
        int try_loading_module = 0;
        int err;
 
+       if (unlikely(!inet_ehash_secret))
+               if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
+                       build_ehash_secret();
+
        sock->state = SS_UNCONNECTED;
 
        /* Look for the requested type/protocol pair. */
-       answer = NULL;
 lookup_protocol:
        err = -ESOCKTNOSUPPORT;
        rcu_read_lock();
-       list_for_each_rcu(p, &inetsw[sock->type]) {
-               answer = list_entry(p, struct inet_protosw, list);
+       list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {
 
+               err = 0;
                /* Check the non-wild match. */
                if (protocol == answer->protocol) {
                        if (protocol != IPPROTO_IP)
@@ -256,10 +300,9 @@ lookup_protocol:
                                break;
                }
                err = -EPROTONOSUPPORT;
-               answer = NULL;
        }
 
-       if (unlikely(answer == NULL)) {
+       if (unlikely(err)) {
                if (try_loading_module < 2) {
                        rcu_read_unlock();
                        /*
@@ -285,16 +328,20 @@ lookup_protocol:
        if (answer->capability > 0 && !capable(answer->capability))
                goto out_rcu_unlock;
 
+       err = -EAFNOSUPPORT;
+       if (!inet_netns_ok(net, protocol))
+               goto out_rcu_unlock;
+
        sock->ops = answer->ops;
        answer_prot = answer->prot;
        answer_no_check = answer->no_check;
        answer_flags = answer->flags;
        rcu_read_unlock();
 
-       BUG_TRAP(answer_prot->slab != NULL);
+       WARN_ON(answer_prot->slab == NULL);
 
        err = -ENOBUFS;
-       sk = sk_alloc(PF_INET, GFP_KERNEL, answer_prot, 1);
+       sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot);
        if (sk == NULL)
                goto out;
 
@@ -304,7 +351,7 @@ lookup_protocol:
                sk->sk_reuse = 1;
 
        inet = inet_sk(sk);
-       inet->is_icsk = INET_PROTOSW_ICSK & answer_flags;
+       inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
 
        if (SOCK_RAW == sock->type) {
                inet->num = protocol;
@@ -411,7 +458,7 @@ int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
        if (addr_len < sizeof(struct sockaddr_in))
                goto out;
 
-       chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
+       chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
 
        /* Not specified by any standard per-se, however it breaks too
         * many applications when removed.  It is unfortunate since
@@ -422,8 +469,8 @@ int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
         */
        err = -EADDRNOTAVAIL;
        if (!sysctl_ip_nonlocal_bind &&
-           !inet->freebind &&
-           addr->sin_addr.s_addr != INADDR_ANY &&
+           !(inet->freebind || inet->transparent) &&
+           addr->sin_addr.s_addr != htonl(INADDR_ANY) &&
            chk_addr_ret != RTN_LOCAL &&
            chk_addr_ret != RTN_MULTICAST &&
            chk_addr_ret != RTN_BROADCAST)
@@ -549,7 +596,7 @@ int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
                if (err < 0)
                        goto out;
 
-               sock->state = SS_CONNECTING;
+               sock->state = SS_CONNECTING;
 
                /* Just entered SS_CONNECTING state; the only
                 * difference is that return value in non-blocking
@@ -611,8 +658,8 @@ int inet_accept(struct socket *sock, struct socket *newsock, int flags)
 
        lock_sock(sk2);
 
-       BUG_TRAP((1 << sk2->sk_state) &
-                (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE));
+       WARN_ON(!((1 << sk2->sk_state) &
+                 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE)));
 
        sock_graft(sk2, newsock);
 
@@ -643,7 +690,7 @@ int inet_getname(struct socket *sock, struct sockaddr *uaddr,
                sin->sin_port = inet->dport;
                sin->sin_addr.s_addr = inet->daddr;
        } else {
-               __u32 addr = inet->rcv_saddr;
+               __be32 addr = inet->rcv_saddr;
                if (!addr)
                        addr = inet->saddr;
                sin->sin_port = inet->sport;
@@ -749,20 +796,24 @@ int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 {
        struct sock *sk = sock->sk;
        int err = 0;
+       struct net *net = sock_net(sk);
 
        switch (cmd) {
                case SIOCGSTAMP:
                        err = sock_get_timestamp(sk, (struct timeval __user *)arg);
                        break;
+               case SIOCGSTAMPNS:
+                       err = sock_get_timestampns(sk, (struct timespec __user *)arg);
+                       break;
                case SIOCADDRT:
                case SIOCDELRT:
                case SIOCRTMSG:
-                       err = ip_rt_ioctl(cmd, (void __user *)arg);
+                       err = ip_rt_ioctl(net, cmd, (void __user *)arg);
                        break;
                case SIOCDARP:
                case SIOCGARP:
                case SIOCSARP:
-                       err = arp_ioctl(cmd, (void __user *)arg);
+                       err = arp_ioctl(net, cmd, (void __user *)arg);
                        break;
                case SIOCGIFADDR:
                case SIOCSIFADDR:
@@ -775,7 +826,7 @@ int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
                case SIOCSIFPFLAGS:
                case SIOCGIFPFLAGS:
                case SIOCSIFFLAGS:
-                       err = devinet_ioctl(cmd, (void __user *)arg);
+                       err = devinet_ioctl(net, cmd, (void __user *)arg);
                        break;
                default:
                        if (sk->sk_prot->ioctl)
@@ -802,10 +853,11 @@ const struct proto_ops inet_stream_ops = {
        .shutdown          = inet_shutdown,
        .setsockopt        = sock_common_setsockopt,
        .getsockopt        = sock_common_getsockopt,
-       .sendmsg           = inet_sendmsg,
+       .sendmsg           = tcp_sendmsg,
        .recvmsg           = sock_common_recvmsg,
        .mmap              = sock_no_mmap,
        .sendpage          = tcp_sendpage,
+       .splice_read       = tcp_splice_read,
 #ifdef CONFIG_COMPAT
        .compat_setsockopt = compat_sock_common_setsockopt,
        .compat_getsockopt = compat_sock_common_getsockopt,
@@ -877,40 +929,40 @@ static struct net_proto_family inet_family_ops = {
  */
 static struct inet_protosw inetsw_array[] =
 {
-        {
-                .type =       SOCK_STREAM,
-                .protocol =   IPPROTO_TCP,
-                .prot =       &tcp_prot,
-                .ops =        &inet_stream_ops,
-                .capability = -1,
-                .no_check =   0,
-                .flags =      INET_PROTOSW_PERMANENT |
+       {
+               .type =       SOCK_STREAM,
+               .protocol =   IPPROTO_TCP,
+               .prot =       &tcp_prot,
+               .ops =        &inet_stream_ops,
+               .capability = -1,
+               .no_check =   0,
+               .flags =      INET_PROTOSW_PERMANENT |
                              INET_PROTOSW_ICSK,
-        },
-
-        {
-                .type =       SOCK_DGRAM,
-                .protocol =   IPPROTO_UDP,
-                .prot =       &udp_prot,
-                .ops =        &inet_dgram_ops,
-                .capability = -1,
-                .no_check =   UDP_CSUM_DEFAULT,
-                .flags =      INET_PROTOSW_PERMANENT,
+       },
+
+       {
+               .type =       SOCK_DGRAM,
+               .protocol =   IPPROTO_UDP,
+               .prot =       &udp_prot,
+               .ops =        &inet_dgram_ops,
+               .capability = -1,
+               .no_check =   UDP_CSUM_DEFAULT,
+               .flags =      INET_PROTOSW_PERMANENT,
        },
-        
+
 
        {
-               .type =       SOCK_RAW,
-               .protocol =   IPPROTO_IP,       /* wild card */
-               .prot =       &raw_prot,
-               .ops =        &inet_sockraw_ops,
-               .capability = CAP_NET_RAW,
-               .no_check =   UDP_CSUM_DEFAULT,
-               .flags =      INET_PROTOSW_REUSE,
+              .type =       SOCK_RAW,
+              .protocol =   IPPROTO_IP,        /* wild card */
+              .prot =       &raw_prot,
+              .ops =        &inet_sockraw_ops,
+              .capability = CAP_NET_RAW,
+              .no_check =   UDP_CSUM_DEFAULT,
+              .flags =      INET_PROTOSW_REUSE,
        }
 };
 
-#define INETSW_ARRAY_LEN (sizeof(inetsw_array) / sizeof(struct inet_protosw))
+#define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array)
 
 void inet_register_protosw(struct inet_protosw *p)
 {
@@ -945,7 +997,7 @@ void inet_register_protosw(struct inet_protosw *p)
        /* Add the new entry after the last permanent entry if any, so that
         * the new entry does not override a permanent entry when matched with
         * a wild-card protocol. But it is allowed to override any existing
-        * non-permanent entry.  This means that when we remove this entry, the 
+        * non-permanent entry.  This means that when we remove this entry, the
         * system automatically returns to the old behavior.
         */
        list_add_rcu(&p->list, last_perm);
@@ -994,8 +1046,8 @@ static int inet_sk_reselect_saddr(struct sock *sk)
        struct inet_sock *inet = inet_sk(sk);
        int err;
        struct rtable *rt;
-       __u32 old_saddr = inet->saddr;
-       __u32 new_saddr;
+       __be32 old_saddr = inet->saddr;
+       __be32 new_saddr;
        __be32 daddr = inet->daddr;
 
        if (inet->opt && inet->opt->srr)
@@ -1006,7 +1058,7 @@ static int inet_sk_reselect_saddr(struct sock *sk)
                               RT_CONN_FLAGS(sk),
                               sk->sk_bound_dev_if,
                               sk->sk_protocol,
-                              inet->sport, inet->dport, sk);
+                              inet->sport, inet->dport, sk, 0);
        if (err)
                return err;
 
@@ -1018,11 +1070,8 @@ static int inet_sk_reselect_saddr(struct sock *sk)
                return 0;
 
        if (sysctl_ip_dynaddr > 1) {
-               printk(KERN_INFO "%s(): shifting inet->"
-                                "saddr from %d.%d.%d.%d to %d.%d.%d.%d\n",
-                      __FUNCTION__,
-                      NIPQUAD(old_saddr),
-                      NIPQUAD(new_saddr));
+               printk(KERN_INFO "%s(): shifting inet->saddr from %pI4 to %pI4\n",
+                      __func__, &old_saddr, &new_saddr);
        }
 
        inet->saddr = inet->rcv_saddr = new_saddr;
@@ -1043,7 +1092,7 @@ int inet_sk_rebuild_header(struct sock *sk)
 {
        struct inet_sock *inet = inet_sk(sk);
        struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
-       u32 daddr;
+       __be32 daddr;
        int err;
 
        /* Route is OK, nothing to do. */
@@ -1065,6 +1114,7 @@ int inet_sk_rebuild_header(struct sock *sk)
                        },
                },
                .proto = sk->sk_protocol,
+               .flags = inet_sk_flowi_flags(sk),
                .uli_u = {
                        .ports = {
                                .sport = inet->sport,
@@ -1072,9 +1122,9 @@ int inet_sk_rebuild_header(struct sock *sk)
                        },
                },
        };
-                                               
+
        security_sk_classify_flow(sk, &fl);
-       err = ip_route_output_flow(&rt, &fl, sk, 0);
+       err = ip_route_output_flow(sock_net(sk), &rt, &fl, sk, 0);
 }
        if (!err)
                sk_setup_caps(sk, &rt->u.dst);
@@ -1108,7 +1158,7 @@ static int inet_gso_send_check(struct sk_buff *skb)
        if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
                goto out;
 
-       iph = skb->nh.iph;
+       iph = ip_hdr(skb);
        ihl = iph->ihl * 4;
        if (ihl < sizeof(*iph))
                goto out;
@@ -1116,8 +1166,9 @@ static int inet_gso_send_check(struct sk_buff *skb)
        if (unlikely(!pskb_may_pull(skb, ihl)))
                goto out;
 
-       skb->h.raw = __skb_pull(skb, ihl);
-       iph = skb->nh.iph;
+       __skb_pull(skb, ihl);
+       skb_reset_transport_header(skb);
+       iph = ip_hdr(skb);
        proto = iph->protocol & (MAX_INET_PROTOS - 1);
        err = -EPROTONOSUPPORT;
 
@@ -1140,6 +1191,9 @@ static struct sk_buff *inet_gso_segment(struct sk_buff *skb, int features)
        int ihl;
        int id;
 
+       if (!(features & NETIF_F_V4_CSUM))
+               features &= ~NETIF_F_SG;
+
        if (unlikely(skb_shinfo(skb)->gso_type &
                     ~(SKB_GSO_TCPV4 |
                       SKB_GSO_UDP |
@@ -1151,7 +1205,7 @@ static struct sk_buff *inet_gso_segment(struct sk_buff *skb, int features)
        if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
                goto out;
 
-       iph = skb->nh.iph;
+       iph = ip_hdr(skb);
        ihl = iph->ihl * 4;
        if (ihl < sizeof(*iph))
                goto out;
@@ -1159,8 +1213,9 @@ static struct sk_buff *inet_gso_segment(struct sk_buff *skb, int features)
        if (unlikely(!pskb_may_pull(skb, ihl)))
                goto out;
 
-       skb->h.raw = __skb_pull(skb, ihl);
-       iph = skb->nh.iph;
+       __skb_pull(skb, ihl);
+       skb_reset_transport_header(skb);
+       iph = ip_hdr(skb);
        id = ntohs(iph->id);
        proto = iph->protocol & (MAX_INET_PROTOS - 1);
        segs = ERR_PTR(-EPROTONOSUPPORT);
@@ -1171,25 +1226,183 @@ static struct sk_buff *inet_gso_segment(struct sk_buff *skb, int features)
                segs = ops->gso_segment(skb, features);
        rcu_read_unlock();
 
-       if (!segs || unlikely(IS_ERR(segs)))
+       if (!segs || IS_ERR(segs))
                goto out;
 
        skb = segs;
        do {
-               iph = skb->nh.iph;
+               iph = ip_hdr(skb);
                iph->id = htons(id++);
                iph->tot_len = htons(skb->len - skb->mac_len);
                iph->check = 0;
-               iph->check = ip_fast_csum(skb->nh.raw, iph->ihl);
+               iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
        } while ((skb = skb->next));
 
 out:
        return segs;
 }
 
+static struct sk_buff **inet_gro_receive(struct sk_buff **head,
+                                        struct sk_buff *skb)
+{
+       struct net_protocol *ops;
+       struct sk_buff **pp = NULL;
+       struct sk_buff *p;
+       struct iphdr *iph;
+       int flush = 1;
+       int proto;
+       int id;
+
+       if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
+               goto out;
+
+       iph = ip_hdr(skb);
+       proto = iph->protocol & (MAX_INET_PROTOS - 1);
+
+       rcu_read_lock();
+       ops = rcu_dereference(inet_protos[proto]);
+       if (!ops || !ops->gro_receive)
+               goto out_unlock;
+
+       if (iph->version != 4 || iph->ihl != 5)
+               goto out_unlock;
+
+       if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
+               goto out_unlock;
+
+       flush = ntohs(iph->tot_len) != skb->len ||
+               iph->frag_off != htons(IP_DF);
+       id = ntohs(iph->id);
+
+       for (p = *head; p; p = p->next) {
+               struct iphdr *iph2;
+
+               if (!NAPI_GRO_CB(p)->same_flow)
+                       continue;
+
+               iph2 = ip_hdr(p);
+
+               if (iph->protocol != iph2->protocol ||
+                   iph->tos != iph2->tos ||
+                   memcmp(&iph->saddr, &iph2->saddr, 8)) {
+                       NAPI_GRO_CB(p)->same_flow = 0;
+                       continue;
+               }
+
+               /* All fields must match except length and checksum. */
+               NAPI_GRO_CB(p)->flush |=
+                       memcmp(&iph->frag_off, &iph2->frag_off, 4) ||
+                       (u16)(ntohs(iph2->id) + NAPI_GRO_CB(p)->count) != id;
+
+               NAPI_GRO_CB(p)->flush |= flush;
+       }
+
+       NAPI_GRO_CB(skb)->flush |= flush;
+       __skb_pull(skb, sizeof(*iph));
+       skb_reset_transport_header(skb);
+
+       pp = ops->gro_receive(head, skb);
+
+out_unlock:
+       rcu_read_unlock();
+
+out:
+       NAPI_GRO_CB(skb)->flush |= flush;
+
+       return pp;
+}
+
+static int inet_gro_complete(struct sk_buff *skb)
+{
+       struct net_protocol *ops;
+       struct iphdr *iph = ip_hdr(skb);
+       int proto = iph->protocol & (MAX_INET_PROTOS - 1);
+       int err = -ENOSYS;
+       __be16 newlen = htons(skb->len - skb_network_offset(skb));
+
+       csum_replace2(&iph->check, iph->tot_len, newlen);
+       iph->tot_len = newlen;
+
+       rcu_read_lock();
+       ops = rcu_dereference(inet_protos[proto]);
+       if (WARN_ON(!ops || !ops->gro_complete))
+               goto out_unlock;
+
+       err = ops->gro_complete(skb);
+
+out_unlock:
+       rcu_read_unlock();
+
+       return err;
+}
+
+int inet_ctl_sock_create(struct sock **sk, unsigned short family,
+                        unsigned short type, unsigned char protocol,
+                        struct net *net)
+{
+       struct socket *sock;
+       int rc = sock_create_kern(family, type, protocol, &sock);
+
+       if (rc == 0) {
+               *sk = sock->sk;
+               (*sk)->sk_allocation = GFP_ATOMIC;
+               /*
+                * Unhash it so that IP input processing does not even see it,
+                * we do not wish this socket to see incoming packets.
+                */
+               (*sk)->sk_prot->unhash(*sk);
+
+               sk_change_net(*sk, net);
+       }
+       return rc;
+}
+
+EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
+
+unsigned long snmp_fold_field(void *mib[], int offt)
+{
+       unsigned long res = 0;
+       int i;
+
+       for_each_possible_cpu(i) {
+               res += *(((unsigned long *) per_cpu_ptr(mib[0], i)) + offt);
+               res += *(((unsigned long *) per_cpu_ptr(mib[1], i)) + offt);
+       }
+       return res;
+}
+EXPORT_SYMBOL_GPL(snmp_fold_field);
+
+int snmp_mib_init(void *ptr[2], size_t mibsize)
+{
+       BUG_ON(ptr == NULL);
+       ptr[0] = __alloc_percpu(mibsize);
+       if (!ptr[0])
+               goto err0;
+       ptr[1] = __alloc_percpu(mibsize);
+       if (!ptr[1])
+               goto err1;
+       return 0;
+err1:
+       free_percpu(ptr[0]);
+       ptr[0] = NULL;
+err0:
+       return -ENOMEM;
+}
+EXPORT_SYMBOL_GPL(snmp_mib_init);
+
+void snmp_mib_free(void *ptr[2])
+{
+       BUG_ON(ptr == NULL);
+       free_percpu(ptr[0]);
+       free_percpu(ptr[1]);
+       ptr[0] = ptr[1] = NULL;
+}
+EXPORT_SYMBOL_GPL(snmp_mib_free);
+
 #ifdef CONFIG_IP_MULTICAST
 static struct net_protocol igmp_protocol = {
        .handler =      igmp_rcv,
+       .netns_ok =     1,
 };
 #endif
 
@@ -1198,40 +1411,87 @@ static struct net_protocol tcp_protocol = {
        .err_handler =  tcp_v4_err,
        .gso_send_check = tcp_v4_gso_send_check,
        .gso_segment =  tcp_tso_segment,
+       .gro_receive =  tcp4_gro_receive,
+       .gro_complete = tcp4_gro_complete,
        .no_policy =    1,
+       .netns_ok =     1,
 };
 
 static struct net_protocol udp_protocol = {
        .handler =      udp_rcv,
        .err_handler =  udp_err,
        .no_policy =    1,
+       .netns_ok =     1,
 };
 
 static struct net_protocol icmp_protocol = {
        .handler =      icmp_rcv,
+       .no_policy =    1,
+       .netns_ok =     1,
 };
 
-static int __init init_ipv4_mibs(void)
+static __net_init int ipv4_mib_init_net(struct net *net)
 {
-       net_statistics[0] = alloc_percpu(struct linux_mib);
-       net_statistics[1] = alloc_percpu(struct linux_mib);
-       ip_statistics[0] = alloc_percpu(struct ipstats_mib);
-       ip_statistics[1] = alloc_percpu(struct ipstats_mib);
-       icmp_statistics[0] = alloc_percpu(struct icmp_mib);
-       icmp_statistics[1] = alloc_percpu(struct icmp_mib);
-       tcp_statistics[0] = alloc_percpu(struct tcp_mib);
-       tcp_statistics[1] = alloc_percpu(struct tcp_mib);
-       udp_statistics[0] = alloc_percpu(struct udp_mib);
-       udp_statistics[1] = alloc_percpu(struct udp_mib);
-       if (!
-           (net_statistics[0] && net_statistics[1] && ip_statistics[0]
-            && ip_statistics[1] && tcp_statistics[0] && tcp_statistics[1]
-            && udp_statistics[0] && udp_statistics[1]))
-               return -ENOMEM;
-
-       (void) tcp_mib_init();
-
+       if (snmp_mib_init((void **)net->mib.tcp_statistics,
+                         sizeof(struct tcp_mib)) < 0)
+               goto err_tcp_mib;
+       if (snmp_mib_init((void **)net->mib.ip_statistics,
+                         sizeof(struct ipstats_mib)) < 0)
+               goto err_ip_mib;
+       if (snmp_mib_init((void **)net->mib.net_statistics,
+                         sizeof(struct linux_mib)) < 0)
+               goto err_net_mib;
+       if (snmp_mib_init((void **)net->mib.udp_statistics,
+                         sizeof(struct udp_mib)) < 0)
+               goto err_udp_mib;
+       if (snmp_mib_init((void **)net->mib.udplite_statistics,
+                         sizeof(struct udp_mib)) < 0)
+               goto err_udplite_mib;
+       if (snmp_mib_init((void **)net->mib.icmp_statistics,
+                         sizeof(struct icmp_mib)) < 0)
+               goto err_icmp_mib;
+       if (snmp_mib_init((void **)net->mib.icmpmsg_statistics,
+                         sizeof(struct icmpmsg_mib)) < 0)
+               goto err_icmpmsg_mib;
+
+       tcp_mib_init(net);
        return 0;
+
+err_icmpmsg_mib:
+       snmp_mib_free((void **)net->mib.icmp_statistics);
+err_icmp_mib:
+       snmp_mib_free((void **)net->mib.udplite_statistics);
+err_udplite_mib:
+       snmp_mib_free((void **)net->mib.udp_statistics);
+err_udp_mib:
+       snmp_mib_free((void **)net->mib.net_statistics);
+err_net_mib:
+       snmp_mib_free((void **)net->mib.ip_statistics);
+err_ip_mib:
+       snmp_mib_free((void **)net->mib.tcp_statistics);
+err_tcp_mib:
+       return -ENOMEM;
+}
+
+static __net_exit void ipv4_mib_exit_net(struct net *net)
+{
+       snmp_mib_free((void **)net->mib.icmpmsg_statistics);
+       snmp_mib_free((void **)net->mib.icmp_statistics);
+       snmp_mib_free((void **)net->mib.udplite_statistics);
+       snmp_mib_free((void **)net->mib.udp_statistics);
+       snmp_mib_free((void **)net->mib.net_statistics);
+       snmp_mib_free((void **)net->mib.ip_statistics);
+       snmp_mib_free((void **)net->mib.tcp_statistics);
+}
+
+static __net_initdata struct pernet_operations ipv4_mib_ops = {
+       .init = ipv4_mib_init_net,
+       .exit = ipv4_mib_exit_net,
+};
+
+static int __init init_ipv4_mibs(void)
+{
+       return register_pernet_subsys(&ipv4_mib_ops);
 }
 
 static int ipv4_proc_init(void);
@@ -1245,6 +1505,8 @@ static struct packet_type ip_packet_type = {
        .func = ip_rcv,
        .gso_send_check = inet_gso_send_check,
        .gso_segment = inet_gso_segment,
+       .gro_receive = inet_gro_receive,
+       .gro_complete = inet_gro_complete,
 };
 
 static int __init inet_init(void)
@@ -1269,10 +1531,14 @@ static int __init inet_init(void)
                goto out_unregister_udp_proto;
 
        /*
-        *      Tell SOCKET that we are alive... 
+        *      Tell SOCKET that we are alive...
         */
 
-       (void)sock_register(&inet_family_ops);
+       (void)sock_register(&inet_family_ops);
+
+#ifdef CONFIG_SYSCTL
+       ip_static_sysctl_init();
+#endif
 
        /*
         *      Add all the base protocols.
@@ -1302,37 +1568,44 @@ static int __init inet_init(void)
 
        arp_init();
 
-       /*
-        *      Set the IP module up
-        */
+       /*
+        *      Set the IP module up
+        */
 
        ip_init();
 
-       tcp_v4_init(&inet_family_ops);
+       tcp_v4_init();
 
        /* Setup TCP slab cache for open requests. */
        tcp_init();
 
+       /* Setup UDP memory threshold */
+       udp_init();
+
+       /* Add UDP-Lite (RFC 3828) */
+       udplite4_register();
 
        /*
         *      Set the ICMP layer up
         */
 
-       icmp_init(&inet_family_ops);
+       if (icmp_init() < 0)
+               panic("Failed to create the ICMP control socket.\n");
 
        /*
         *      Initialise the multicast router
         */
 #if defined(CONFIG_IP_MROUTE)
-       ip_mr_init();
+       if (ip_mr_init())
+               printk(KERN_CRIT "inet_init: Cannot init ipv4 mroute\n");
 #endif
        /*
         *      Initialise per-cpu ipv4 mibs
-        */ 
+        */
+
+       if (init_ipv4_mibs())
+               printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n");
 
-       if(init_ipv4_mibs())
-               printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n"); ;
-       
        ipv4_proc_init();
 
        ipfrag_init();
@@ -1364,15 +1637,11 @@ static int __init ipv4_proc_init(void)
                goto out_tcp;
        if (udp4_proc_init())
                goto out_udp;
-       if (fib_proc_init())
-               goto out_fib;
        if (ip_misc_proc_init())
                goto out_misc;
 out:
        return rc;
 out_misc:
-       fib_proc_exit();
-out_fib:
        udp4_proc_exit();
 out_udp:
        tcp4_proc_exit();
@@ -1407,5 +1676,4 @@ EXPORT_SYMBOL(inet_sock_destruct);
 EXPORT_SYMBOL(inet_stream_connect);
 EXPORT_SYMBOL(inet_stream_ops);
 EXPORT_SYMBOL(inet_unregister_protosw);
-EXPORT_SYMBOL(net_statistics);
 EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);