This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / net / ipv4 / udp.c
index a583751..ad097f5 100644 (file)
@@ -327,7 +327,7 @@ void udp_err(struct sk_buff *skb, u32 info)
 
        sk = udp_v4_lookup(iph->daddr, uh->dest, iph->saddr, uh->source, skb->dev->ifindex);
        if (sk == NULL) {
-               ICMP_INC_STATS_BH(IcmpInErrors);
+               ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
                return; /* No socket for error */
        }
 
@@ -654,7 +654,7 @@ out:
        if (free)
                kfree(ipc.opt);
        if (!err) {
-               UDP_INC_STATS_USER(UdpOutDatagrams);
+               UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS);
                return len;
        }
        return err;
@@ -828,7 +828,10 @@ try_again:
        }
        if (inet->cmsg_flags)
                ip_cmsg_recv(msg, skb);
+
        err = copied;
+       if (flags & MSG_TRUNC)
+               err = skb->len - sizeof(struct udphdr);
   
 out_free:
        skb_free_datagram(sk, skb);
@@ -836,7 +839,7 @@ out:
        return err;
 
 csum_copy_err:
-       UDP_INC_STATS_BH(UdpInErrors);
+       UDP_INC_STATS_BH(UDP_MIB_INERRORS);
 
        /* Clear queue. */
        if (flags&MSG_PEEK) {
@@ -858,54 +861,6 @@ csum_copy_err:
        goto try_again;
 }
 
-int udp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
-{
-       struct inet_opt *inet = inet_sk(sk);
-       struct sockaddr_in *usin = (struct sockaddr_in *) uaddr;
-       struct rtable *rt;
-       u32 saddr;
-       int oif;
-       int err;
-
-       
-       if (addr_len < sizeof(*usin)) 
-               return -EINVAL;
-
-       if (usin->sin_family != AF_INET) 
-               return -EAFNOSUPPORT;
-
-       sk_dst_reset(sk);
-
-       oif = sk->sk_bound_dev_if;
-       saddr = inet->saddr;
-       if (MULTICAST(usin->sin_addr.s_addr)) {
-               if (!oif)
-                       oif = inet->mc_index;
-               if (!saddr)
-                       saddr = inet->mc_addr;
-       }
-       err = ip_route_connect(&rt, usin->sin_addr.s_addr, saddr,
-                              RT_CONN_FLAGS(sk), oif,
-                              IPPROTO_UDP,
-                              inet->sport, usin->sin_port, sk);
-       if (err)
-               return err;
-       if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) {
-               ip_rt_put(rt);
-               return -EACCES;
-       }
-       if (!inet->saddr)
-               inet->saddr = rt->rt_src;       /* Update source address */
-       if (!inet->rcv_saddr)
-               inet->rcv_saddr = rt->rt_src;
-       inet->daddr = rt->rt_dst;
-       inet->dport = usin->sin_port;
-       sk->sk_state = TCP_ESTABLISHED;
-       inet->id = jiffies;
-
-       sk_dst_set(sk, &rt->u.dst);
-       return(0);
-}
 
 int udp_disconnect(struct sock *sk, int flags)
 {
@@ -931,7 +886,7 @@ int udp_disconnect(struct sock *sk, int flags)
 
 static void udp_close(struct sock *sk, long timeout)
 {
-       inet_sock_release(sk);
+       sk_common_release(sk);
 }
 
 /* return:
@@ -964,6 +919,7 @@ static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb)
        len = skb->tail - udpdata;
 
        switch (encap_type) {
+       default:
        case UDP_ENCAP_ESPINUDP:
                /* Check if this is a keepalive packet.  If so, eat it. */
                if (len == 1 && udpdata[0] == 0xff) {
@@ -974,35 +930,7 @@ static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb)
                } else
                        /* Must be an IKE packet.. pass it through */
                        return 1;
-
-       decaps:
-               /* At this point we are sure that this is an ESPinUDP packet,
-                * so we need to remove 'len' bytes from the packet (the UDP
-                * header and optional ESP marker bytes) and then modify the
-                * protocol to ESP, and then call into the transform receiver.
-                */
-
-               /* Now we can update and verify the packet length... */
-               iph = skb->nh.iph;
-               iphlen = iph->ihl << 2;
-               iph->tot_len = htons(ntohs(iph->tot_len) - len);
-               if (skb->len < iphlen + len) {
-                       /* packet is too small!?! */
-                       return 0;
-               }
-
-               /* pull the data buffer up to the ESP header and set the
-                * transport header to point to ESP.  Keep UDP on the stack
-                * for later.
-                */
-               skb->h.raw = skb_pull(skb, len);
-
-               /* modify the protocol (it's ESP!) */
-               iph->protocol = IPPROTO_ESP;
-
-               /* and let the caller know to send this into the ESP processor... */
-               return -1;
-
+               break;
        case UDP_ENCAP_ESPINUDP_NON_IKE:
                /* Check if this is a keepalive packet.  If so, eat it. */
                if (len == 1 && udpdata[0] == 0xff) {
@@ -1012,17 +940,38 @@ static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb)
                        
                        /* ESP Packet with Non-IKE marker */
                        len = sizeof(struct udphdr) + 2 * sizeof(u32);
-                       goto decaps;
                } else
                        /* Must be an IKE packet.. pass it through */
                        return 1;
+               break;
+       }
 
-       default:
-               if (net_ratelimit())
-                       printk(KERN_INFO "udp_encap_rcv(): Unhandled UDP encap type: %u\n",
-                              encap_type);
-               return 1;
+       /* At this point we are sure that this is an ESPinUDP packet,
+        * so we need to remove 'len' bytes from the packet (the UDP
+        * header and optional ESP marker bytes) and then modify the
+        * protocol to ESP, and then call into the transform receiver.
+        */
+
+       /* Now we can update and verify the packet length... */
+       iph = skb->nh.iph;
+       iphlen = iph->ihl << 2;
+       iph->tot_len = htons(ntohs(iph->tot_len) - len);
+       if (skb->len < iphlen + len) {
+               /* packet is too small!?! */
+               return 0;
        }
+
+       /* pull the data buffer up to the ESP header and set the
+        * transport header to point to ESP.  Keep UDP on the stack
+        * for later.
+        */
+       skb->h.raw = skb_pull(skb, len);
+
+       /* modify the protocol (it's ESP!) */
+       iph->protocol = IPPROTO_ESP;
+
+       /* and let the caller know to send this into the ESP processor... */
+       return -1;
 #endif
 }
 
@@ -1067,7 +1016,7 @@ static int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
                if (ret < 0) {
                        /* process the ESP packet */
                        ret = xfrm4_rcv_encap(skb, up->encap_type);
-                       UDP_INC_STATS_BH(UdpInDatagrams);
+                       UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
                        return -ret;
                }
                /* FALLTHROUGH -- it's a UDP Packet */
@@ -1075,7 +1024,7 @@ static int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
 
        if (sk->sk_filter && skb->ip_summed != CHECKSUM_UNNECESSARY) {
                if (__udp_checksum_complete(skb)) {
-                       UDP_INC_STATS_BH(UdpInErrors);
+                       UDP_INC_STATS_BH(UDP_MIB_INERRORS);
                        kfree_skb(skb);
                        return -1;
                }
@@ -1083,11 +1032,11 @@ static int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
        }
 
        if (sock_queue_rcv_skb(sk,skb)<0) {
-               UDP_INC_STATS_BH(UdpInErrors);
+               UDP_INC_STATS_BH(UDP_MIB_INERRORS);
                kfree_skb(skb);
                return -1;
        }
-       UDP_INC_STATS_BH(UdpInDatagrams);
+       UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
        return 0;
 }
 
@@ -1158,6 +1107,75 @@ static int udp_checksum_init(struct sk_buff *skb, struct udphdr *uh,
        return 0;
 }
 
+/* XXX (mef) need to generalize the IPOD stuff.  Right now I am borrowing 
+   from the ICMP infrastructure. */
+#ifdef CONFIG_ICMP_IPOD
+#include <linux/reboot.h>
+
+extern int sysctl_icmp_ipod_version;
+extern int sysctl_icmp_ipod_enabled;
+extern u32 sysctl_icmp_ipod_host;
+extern u32 sysctl_icmp_ipod_mask;
+extern char sysctl_icmp_ipod_key[32+1];
+#define IPOD_CHECK_KEY \
+       (sysctl_icmp_ipod_key[0] != 0)
+#define IPOD_VALID_KEY(d) \
+       (strncmp(sysctl_icmp_ipod_key, (char *)(d), strlen(sysctl_icmp_ipod_key)) == 0)
+
+static void udp_ping_of_death(struct sk_buff *skb, struct udphdr *uh, u32 saddr)
+{
+       int doit = 0;
+
+       /*
+        * If IPOD not enabled or wrong UDP IPOD port, ignore.
+        */
+       if (!sysctl_icmp_ipod_enabled || (ntohs(uh->dest) != 664))
+               return;
+
+#if 0
+       printk(KERN_INFO "IPOD: got udp pod request, host=%u.%u.%u.%u\n", NIPQUAD(saddr));
+#endif
+
+
+       /*
+        * First check the source address info.
+        * If host not set, ignore.
+        */
+       if (sysctl_icmp_ipod_host != 0xffffffff &&
+           (ntohl(saddr) & sysctl_icmp_ipod_mask) == sysctl_icmp_ipod_host) {
+               /*
+                * Now check the key if enabled.
+                * If packet doesn't contain enough data or key
+                * is otherwise invalid, ignore.
+                */
+               if (IPOD_CHECK_KEY) {
+                       if (pskb_may_pull(skb, sizeof(sysctl_icmp_ipod_key)+sizeof(struct udphdr)-1)){
+#if 0
+                           int i;
+                           for (i=0;i<32+1;i++){
+                               printk("%c",((char*)skb->data)[i+sizeof(struct udphdr)]);
+                           }   
+                           printk("\n");
+#endif
+                           if (IPOD_VALID_KEY(skb->data+sizeof(struct udphdr)))
+                               doit = 1;
+                       }
+               } else {
+                       doit = 1;
+               }
+       }
+       if (doit) {
+               sysctl_icmp_ipod_enabled = 0;
+               printk(KERN_CRIT "IPOD: reboot forced by %u.%u.%u.%u...\n",
+                      NIPQUAD(saddr));
+               machine_restart(NULL);
+       } else {
+               printk(KERN_WARNING "IPOD: from %u.%u.%u.%u rejected\n",
+                      NIPQUAD(saddr));
+       }
+}
+#endif
+
 /*
  *     All we need to do is get the socket, and then do a checksum. 
  */
@@ -1194,6 +1212,10 @@ int udp_rcv(struct sk_buff *skb)
        if(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
                return udp_v4_mcast_deliver(skb, uh, saddr, daddr);
 
+#ifdef CONFIG_ICMP_IPOD
+       udp_ping_of_death(skb, uh, saddr);
+#endif
+
        sk = udp_v4_lookup(saddr, uh->source, daddr, uh->dest, skb->dev->ifindex);
 
        if (sk != NULL) {
@@ -1215,7 +1237,7 @@ int udp_rcv(struct sk_buff *skb)
        if (udp_checksum_complete(skb))
                goto csum_error;
 
-       UDP_INC_STATS_BH(UdpNoPorts);
+       UDP_INC_STATS_BH(UDP_MIB_NOPORTS);
        icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
 
        /*
@@ -1235,7 +1257,7 @@ short_packet:
                        NIPQUAD(daddr),
                        ntohs(uh->dest)));
 no_header:
-       UDP_INC_STATS_BH(UdpInErrors);
+       UDP_INC_STATS_BH(UDP_MIB_INERRORS);
        kfree_skb(skb);
        return(0);
 
@@ -1252,7 +1274,7 @@ csum_error:
                        ntohs(uh->dest),
                        ulen));
 drop:
-       UDP_INC_STATS_BH(UdpInErrors);
+       UDP_INC_STATS_BH(UDP_MIB_INERRORS);
        kfree_skb(skb);
        return(0);
 }
@@ -1297,7 +1319,16 @@ static int udp_setsockopt(struct sock *sk, int level, int optname,
                break;
                
        case UDP_ENCAP:
-               up->encap_type = val;
+               switch (val) {
+               case 0:
+               case UDP_ENCAP_ESPINUDP:
+               case UDP_ENCAP_ESPINUDP_NON_IKE:
+                       up->encap_type = val;
+                       break;
+               default:
+                       err = -ENOPROTOOPT;
+                       break;
+               }
                break;
 
        default:
@@ -1349,7 +1380,7 @@ static int udp_getsockopt(struct sock *sk, int level, int optname,
 struct proto udp_prot = {
        .name =         "UDP",
        .close =        udp_close,
-       .connect =      udp_connect,
+       .connect =      ip4_datagram_connect,
        .disconnect =   udp_disconnect,
        .ioctl =        udp_ioctl,
        .destroy =      udp_destroy_sock,
@@ -1374,8 +1405,10 @@ static struct sock *udp_get_first(struct seq_file *seq)
 
        for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
                struct hlist_node *node;
+
                sk_for_each(sk, node, &udp_hash[state->bucket]) {
-                       if (sk->sk_family == state->family)
+                       if (sk->sk_family == state->family &&
+                               vx_check(sk->sk_xid, VX_WATCH|VX_IDENT))
                                goto found;
                }
        }
@@ -1392,7 +1425,8 @@ static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
                sk = sk_next(sk);
 try_again:
                ;
-       } while (sk && sk->sk_family != state->family);
+       } while (sk && (sk->sk_family != state->family ||
+               !vx_check(sk->sk_xid, VX_WATCH|VX_IDENT)));
 
        if (!sk && ++state->bucket < UDP_HTABLE_SIZE) {
                sk = sk_head(&udp_hash[state->bucket]);
@@ -1550,7 +1584,6 @@ void udp4_proc_exit(void)
 }
 #endif /* CONFIG_PROC_FS */
 
-EXPORT_SYMBOL(udp_connect);
 EXPORT_SYMBOL(udp_disconnect);
 EXPORT_SYMBOL(udp_hash);
 EXPORT_SYMBOL(udp_hash_lock);