linux-bk/net/ipv4/udp.c
<<
>>
Prefs
   1/*
   2 * INET         An implementation of the TCP/IP protocol suite for the LINUX
   3 *              operating system.  INET is implemented using the  BSD Socket
   4 *              interface as the means of communication with the user level.
   5 *
   6 *              The User Datagram Protocol (UDP).
   7 *
   8 * Version:     $Id: udp.c,v 1.102 2002/02/01 22:01:04 davem Exp $
   9 *
  10 * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  11 *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12 *              Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  13 *              Alan Cox, <Alan.Cox@linux.org>
  14 *              Hirokazu Takahashi, <taka@valinux.co.jp>
  15 *
  16 * Fixes:
  17 *              Alan Cox        :       verify_area() calls
  18 *              Alan Cox        :       stopped close while in use off icmp
  19 *                                      messages. Not a fix but a botch that
  20 *                                      for udp at least is 'valid'.
  21 *              Alan Cox        :       Fixed icmp handling properly
  22 *              Alan Cox        :       Correct error for oversized datagrams
  23 *              Alan Cox        :       Tidied select() semantics. 
  24 *              Alan Cox        :       udp_err() fixed properly, also now 
  25 *                                      select and read wake correctly on errors
  26 *              Alan Cox        :       udp_send verify_area moved to avoid mem leak
  27 *              Alan Cox        :       UDP can count its memory
  28 *              Alan Cox        :       send to an unknown connection causes
  29 *                                      an ECONNREFUSED off the icmp, but
  30 *                                      does NOT close.
  31 *              Alan Cox        :       Switched to new sk_buff handlers. No more backlog!
  32 *              Alan Cox        :       Using generic datagram code. Even smaller and the PEEK
  33 *                                      bug no longer crashes it.
  34 *              Fred Van Kempen :       Net2e support for sk->broadcast.
  35 *              Alan Cox        :       Uses skb_free_datagram
  36 *              Alan Cox        :       Added get/set sockopt support.
  37 *              Alan Cox        :       Broadcasting without option set returns EACCES.
  38 *              Alan Cox        :       No wakeup calls. Instead we now use the callbacks.
  39 *              Alan Cox        :       Use ip_tos and ip_ttl
  40 *              Alan Cox        :       SNMP Mibs
  41 *              Alan Cox        :       MSG_DONTROUTE, and 0.0.0.0 support.
  42 *              Matt Dillon     :       UDP length checks.
  43 *              Alan Cox        :       Smarter af_inet used properly.
  44 *              Alan Cox        :       Use new kernel side addressing.
  45 *              Alan Cox        :       Incorrect return on truncated datagram receive.
  46 *      Arnt Gulbrandsen        :       New udp_send and stuff
  47 *              Alan Cox        :       Cache last socket
  48 *              Alan Cox        :       Route cache
  49 *              Jon Peatfield   :       Minor efficiency fix to sendto().
  50 *              Mike Shaver     :       RFC1122 checks.
  51 *              Alan Cox        :       Nonblocking error fix.
  52 *      Willy Konynenberg       :       Transparent proxying support.
  53 *              Mike McLagan    :       Routing by source
  54 *              David S. Miller :       New socket lookup architecture.
  55 *                                      Last socket cache retained as it
  56 *                                      does have a high hit rate.
  57 *              Olaf Kirch      :       Don't linearise iovec on sendmsg.
  58 *              Andi Kleen      :       Some cleanups, cache destination entry
  59 *                                      for connect. 
  60 *      Vitaly E. Lavrov        :       Transparent proxy revived after year coma.
  61 *              Melvin Smith    :       Check msg_name not msg_namelen in sendto(),
  62 *                                      return ENOTCONN for unconnected sockets (POSIX)
  63 *              Janos Farkas    :       don't deliver multi/broadcasts to a different
  64 *                                      bound-to-device socket
  65 *      Hirokazu Takahashi      :       HW checksumming for outgoing UDP
  66 *                                      datagrams.
  67 *      Hirokazu Takahashi      :       sendfile() on UDP works now.
  68 *              Arnaldo C. Melo :       convert /proc/net/udp to seq_file
  69 *      YOSHIFUJI Hideaki @USAGI and:   Support IPV6_V6ONLY socket option, which
  70 *      Alexey Kuznetsov:               allow both IPv4 and IPv6 sockets to bind
  71 *                                      a single port at the same time.
  72 *      Derek Atkins <derek@ihtfp.com>: Add Encapulation Support
  73 *
  74 *
  75 *              This program is free software; you can redistribute it and/or
  76 *              modify it under the terms of the GNU General Public License
  77 *              as published by the Free Software Foundation; either version
  78 *              2 of the License, or (at your option) any later version.
  79 */
  80 
  81#include <asm/system.h>
  82#include <asm/uaccess.h>
  83#include <asm/ioctls.h>
  84#include <linux/types.h>
  85#include <linux/fcntl.h>
  86#include <linux/module.h>
  87#include <linux/socket.h>
  88#include <linux/sockios.h>
  89#include <linux/in.h>
  90#include <linux/errno.h>
  91#include <linux/timer.h>
  92#include <linux/mm.h>
  93#include <linux/config.h>
  94#include <linux/inet.h>
  95#include <linux/ipv6.h>
  96#include <linux/netdevice.h>
  97#include <net/snmp.h>
  98#include <net/tcp.h>
  99#include <net/protocol.h>
 100#include <linux/skbuff.h>
 101#include <linux/proc_fs.h>
 102#include <linux/seq_file.h>
 103#include <net/sock.h>
 104#include <net/udp.h>
 105#include <net/icmp.h>
 106#include <net/route.h>
 107#include <net/inet_common.h>
 108#include <net/checksum.h>
 109#include <net/xfrm.h>
 110
 111/*
 112 *      Snmp MIB for the UDP layer
 113 */
 114
 115DEFINE_SNMP_STAT(struct udp_mib, udp_statistics);
 116
 117struct hlist_head udp_hash[UDP_HTABLE_SIZE];
 118rwlock_t udp_hash_lock = RW_LOCK_UNLOCKED;
 119
 120/* Shared by v4/v6 udp. */
 121int udp_port_rover;
 122
 123static int udp_v4_get_port(struct sock *sk, unsigned short snum)
 124{
 125        struct hlist_node *node;
 126        struct sock *sk2;
 127        struct inet_opt *inet = inet_sk(sk);
 128
 129        write_lock_bh(&udp_hash_lock);
 130        if (snum == 0) {
 131                int best_size_so_far, best, result, i;
 132
 133                if (udp_port_rover > sysctl_local_port_range[1] ||
 134                    udp_port_rover < sysctl_local_port_range[0])
 135                        udp_port_rover = sysctl_local_port_range[0];
 136                best_size_so_far = 32767;
 137                best = result = udp_port_rover;
 138                for (i = 0; i < UDP_HTABLE_SIZE; i++, result++) {
 139                        struct hlist_head *list;
 140                        int size;
 141
 142                        list = &udp_hash[result & (UDP_HTABLE_SIZE - 1)];
 143                        if (hlist_empty(list)) {
 144                                if (result > sysctl_local_port_range[1])
 145                                        result = sysctl_local_port_range[0] +
 146                                                ((result - sysctl_local_port_range[0]) &
 147                                                 (UDP_HTABLE_SIZE - 1));
 148                                goto gotit;
 149                        }
 150                        size = 0;
 151                        sk_for_each(sk2, node, list)
 152                                if (++size >= best_size_so_far)
 153                                        goto next;
 154                        best_size_so_far = size;
 155                        best = result;
 156                next:;
 157                }
 158                result = best;
 159                for(i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++, result += UDP_HTABLE_SIZE) {
 160                        if (result > sysctl_local_port_range[1])
 161                                result = sysctl_local_port_range[0]
 162                                        + ((result - sysctl_local_port_range[0]) &
 163                                           (UDP_HTABLE_SIZE - 1));
 164                        if (!udp_lport_inuse(result))
 165                                break;
 166                }
 167                if (i >= (1 << 16) / UDP_HTABLE_SIZE)
 168                        goto fail;
 169gotit:
 170                udp_port_rover = snum = result;
 171        } else {
 172                sk_for_each(sk2, node,
 173                            &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]) {
 174                        struct inet_opt *inet2 = inet_sk(sk2);
 175
 176                        if (inet2->num == snum &&
 177                            sk2 != sk &&
 178                            !ipv6_only_sock(sk2) &&
 179                            (!sk2->sk_bound_dev_if ||
 180                             !sk->sk_bound_dev_if ||
 181                             sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
 182                            (!inet2->rcv_saddr ||
 183                             !inet->rcv_saddr ||
 184                             inet2->rcv_saddr == inet->rcv_saddr) &&
 185                            (!sk2->sk_reuse || !sk->sk_reuse))
 186                                goto fail;
 187                }
 188        }
 189        inet->num = snum;
 190        if (sk_unhashed(sk)) {
 191                struct hlist_head *h = &udp_hash[snum & (UDP_HTABLE_SIZE - 1)];
 192
 193                sk_add_node(sk, h);
 194                sock_prot_inc_use(sk->sk_prot);
 195        }
 196        write_unlock_bh(&udp_hash_lock);
 197        return 0;
 198
 199fail:
 200        write_unlock_bh(&udp_hash_lock);
 201        return 1;
 202}
 203
 204static void udp_v4_hash(struct sock *sk)
 205{
 206        BUG();
 207}
 208
 209static void udp_v4_unhash(struct sock *sk)
 210{
 211        write_lock_bh(&udp_hash_lock);
 212        if (sk_del_node_init(sk)) {
 213                inet_sk(sk)->num = 0;
 214                sock_prot_dec_use(sk->sk_prot);
 215        }
 216        write_unlock_bh(&udp_hash_lock);
 217}
 218
 219/* UDP is nearly always wildcards out the wazoo, it makes no sense to try
 220 * harder than this. -DaveM
 221 */
 222struct sock *udp_v4_lookup_longway(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif)
 223{
 224        struct sock *sk, *result = NULL;
 225        struct hlist_node *node;
 226        unsigned short hnum = ntohs(dport);
 227        int badness = -1;
 228
 229        sk_for_each(sk, node, &udp_hash[hnum & (UDP_HTABLE_SIZE - 1)]) {
 230                struct inet_opt *inet = inet_sk(sk);
 231
 232                if (inet->num == hnum && !ipv6_only_sock(sk)) {
 233                        int score = (sk->sk_family == PF_INET ? 1 : 0);
 234                        if (inet->rcv_saddr) {
 235                                if (inet->rcv_saddr != daddr)
 236                                        continue;
 237                                score+=2;
 238                        }
 239                        if (inet->daddr) {
 240                                if (inet->daddr != saddr)
 241                                        continue;
 242                                score+=2;
 243                        }
 244                        if (inet->dport) {
 245                                if (inet->dport != sport)
 246                                        continue;
 247                                score+=2;
 248                        }
 249                        if (sk->sk_bound_dev_if) {
 250                                if (sk->sk_bound_dev_if != dif)
 251                                        continue;
 252                                score+=2;
 253                        }
 254                        if(score == 9) {
 255                                result = sk;
 256                                break;
 257                        } else if(score > badness) {
 258                                result = sk;
 259                                badness = score;
 260                        }
 261                }
 262        }
 263        return result;
 264}
 265
 266__inline__ struct sock *udp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif)
 267{
 268        struct sock *sk;
 269
 270        read_lock(&udp_hash_lock);
 271        sk = udp_v4_lookup_longway(saddr, sport, daddr, dport, dif);
 272        if (sk)
 273                sock_hold(sk);
 274        read_unlock(&udp_hash_lock);
 275        return sk;
 276}
 277
 278static inline struct sock *udp_v4_mcast_next(struct sock *sk,
 279                                             u16 loc_port, u32 loc_addr,
 280                                             u16 rmt_port, u32 rmt_addr,
 281                                             int dif)
 282{
 283        struct hlist_node *node;
 284        struct sock *s = sk;
 285        unsigned short hnum = ntohs(loc_port);
 286
 287        sk_for_each_from(s, node) {
 288                struct inet_opt *inet = inet_sk(s);
 289
 290                if (inet->num != hnum                                   ||
 291                    (inet->daddr && inet->daddr != rmt_addr)            ||
 292                    (inet->dport != rmt_port && inet->dport)            ||
 293                    (inet->rcv_saddr && inet->rcv_saddr != loc_addr)    ||
 294                    ipv6_only_sock(s)                                   ||
 295                    (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
 296                        continue;
 297                if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif))
 298                        continue;
 299                goto found;
 300        }
 301        s = NULL;
 302found:
 303        return s;
 304}
 305
 306/*
 307 * This routine is called by the ICMP module when it gets some
 308 * sort of error condition.  If err < 0 then the socket should
 309 * be closed and the error returned to the user.  If err > 0
 310 * it's just the icmp type << 8 | icmp code.  
 311 * Header points to the ip header of the error packet. We move
 312 * on past this. Then (as it used to claim before adjustment)
 313 * header points to the first 8 bytes of the udp header.  We need
 314 * to find the appropriate port.
 315 */
 316
 317void udp_err(struct sk_buff *skb, u32 info)
 318{
 319        struct inet_opt *inet;
 320        struct iphdr *iph = (struct iphdr*)skb->data;
 321        struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2));
 322        int type = skb->h.icmph->type;
 323        int code = skb->h.icmph->code;
 324        struct sock *sk;
 325        int harderr;
 326        int err;
 327
 328        sk = udp_v4_lookup(iph->daddr, uh->dest, iph->saddr, uh->source, skb->dev->ifindex);
 329        if (sk == NULL) {
 330                ICMP_INC_STATS_BH(IcmpInErrors);
 331                return; /* No socket for error */
 332        }
 333
 334        err = 0;
 335        harderr = 0;
 336        inet = inet_sk(sk);
 337
 338        switch (type) {
 339        default:
 340        case ICMP_TIME_EXCEEDED:
 341                err = EHOSTUNREACH;
 342                break;
 343        case ICMP_SOURCE_QUENCH:
 344                goto out;
 345        case ICMP_PARAMETERPROB:
 346                err = EPROTO;
 347                harderr = 1;
 348                break;
 349        case ICMP_DEST_UNREACH:
 350                if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
 351                        if (inet->pmtudisc != IP_PMTUDISC_DONT) {
 352                                err = EMSGSIZE;
 353                                harderr = 1;
 354                                break;
 355                        }
 356                        goto out;
 357                }
 358                err = EHOSTUNREACH;
 359                if (code <= NR_ICMP_UNREACH) {
 360                        harderr = icmp_err_convert[code].fatal;
 361                        err = icmp_err_convert[code].errno;
 362                }
 363                break;
 364        }
 365
 366        /*
 367         *      RFC1122: OK.  Passes ICMP errors back to application, as per 
 368         *      4.1.3.3.
 369         */
 370        if (!inet->recverr) {
 371                if (!harderr || sk->sk_state != TCP_ESTABLISHED)
 372                        goto out;
 373        } else {
 374                ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1));
 375        }
 376        sk->sk_err = err;
 377        sk->sk_error_report(sk);
 378out:
 379        sock_put(sk);
 380}
 381
 382/*
 383 * Throw away all pending data and cancel the corking. Socket is locked.
 384 */
 385static void udp_flush_pending_frames(struct sock *sk)
 386{
 387        struct udp_opt *up = udp_sk(sk);
 388
 389        if (up->pending) {
 390                up->len = 0;
 391                up->pending = 0;
 392                ip_flush_pending_frames(sk);
 393        }
 394}
 395
 396/*
 397 * Push out all pending data as one UDP datagram. Socket is locked.
 398 */
 399static int udp_push_pending_frames(struct sock *sk, struct udp_opt *up)
 400{
 401        struct inet_opt *inet = inet_sk(sk);
 402        struct flowi *fl = &inet->cork.fl;
 403        struct sk_buff *skb;
 404        struct udphdr *uh;
 405        int err = 0;
 406
 407        /* Grab the skbuff where UDP header space exists. */
 408        if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
 409                goto out;
 410
 411        /*
 412         * Create a UDP header
 413         */
 414        uh = skb->h.uh;
 415        uh->source = fl->fl_ip_sport;
 416        uh->dest = fl->fl_ip_dport;
 417        uh->len = htons(up->len);
 418        uh->check = 0;
 419
 420        if (sk->sk_no_check == UDP_CSUM_NOXMIT) {
 421                skb->ip_summed = CHECKSUM_NONE;
 422                goto send;
 423        }
 424
 425        if (skb_queue_len(&sk->sk_write_queue) == 1) {
 426                /*
 427                 * Only one fragment on the socket.
 428                 */
 429                if (skb->ip_summed == CHECKSUM_HW) {
 430                        skb->csum = offsetof(struct udphdr, check);
 431                        uh->check = ~csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
 432                                        up->len, IPPROTO_UDP, 0);
 433                } else {
 434                        skb->csum = csum_partial((char *)uh,
 435                                        sizeof(struct udphdr), skb->csum);
 436                        uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
 437                                        up->len, IPPROTO_UDP, skb->csum);
 438                        if (uh->check == 0)
 439                                uh->check = -1;
 440                }
 441        } else {
 442                unsigned int csum = 0;
 443                /*
 444                 * HW-checksum won't work as there are two or more 
 445                 * fragments on the socket so that all csums of sk_buffs
 446                 * should be together.
 447                 */
 448                if (skb->ip_summed == CHECKSUM_HW) {
 449                        int offset = (unsigned char *)uh - skb->data;
 450                        skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
 451
 452                        skb->ip_summed = CHECKSUM_NONE;
 453                } else {
 454                        skb->csum = csum_partial((char *)uh,
 455                                        sizeof(struct udphdr), skb->csum);
 456                }
 457
 458                skb_queue_walk(&sk->sk_write_queue, skb) {
 459                        csum = csum_add(csum, skb->csum);
 460                }
 461                uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
 462                                up->len, IPPROTO_UDP, csum);
 463                if (uh->check == 0)
 464                        uh->check = -1;
 465        }
 466send:
 467        err = ip_push_pending_frames(sk);
 468out:
 469        up->len = 0;
 470        up->pending = 0;
 471        return err;
 472}
 473
 474
 475static unsigned short udp_check(struct udphdr *uh, int len, unsigned long saddr, unsigned long daddr, unsigned long base)
 476{
 477        return(csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base));
 478}
 479
 480int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
 481                int len)
 482{
 483        struct inet_opt *inet = inet_sk(sk);
 484        struct udp_opt *up = udp_sk(sk);
 485        int ulen = len;
 486        struct ipcm_cookie ipc;
 487        struct rtable *rt = NULL;
 488        int free = 0;
 489        int connected = 0;
 490        u32 daddr, faddr, saddr;
 491        u16 dport;
 492        u8  tos;
 493        int err;
 494        int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
 495
 496        /* This check is ONLY to check for arithmetic overflow
 497           on integer(!) len. Not more! Real check will be made
 498           in ip_append_* --ANK
 499
 500           BTW socket.c -> af_*.c -> ... make multiple
 501           invalid conversions size_t -> int. We MUST repair it f.e.
 502           by replacing all of them with size_t and revise all
 503           the places sort of len += sizeof(struct iphdr)
 504           If len was ULONG_MAX-10 it would be cathastrophe  --ANK
 505         */
 506
 507        if (len < 0 || len > 0xFFFF)
 508                return -EMSGSIZE;
 509
 510        /* 
 511         *      Check the flags.
 512         */
 513
 514        if (msg->msg_flags&MSG_OOB)     /* Mirror BSD error message compatibility */
 515                return -EOPNOTSUPP;
 516
 517        ipc.opt = NULL;
 518
 519        if (up->pending) {
 520                /*
 521                 * There are pending frames.
 522                 * The socket lock must be held while it's corked.
 523                 */
 524                lock_sock(sk);
 525                if (likely(up->pending)) {
 526                        if (unlikely(up->pending != AF_INET)) {
 527                                release_sock(sk);
 528                                return -EINVAL;
 529                        }
 530                        goto do_append_data;
 531                }
 532                release_sock(sk);
 533        }
 534        ulen += sizeof(struct udphdr);
 535
 536        /*
 537         *      Get and verify the address. 
 538         */
 539        if (msg->msg_name) {
 540                struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name;
 541                if (msg->msg_namelen < sizeof(*usin))
 542                        return -EINVAL;
 543                if (usin->sin_family != AF_INET) {
 544                        if (usin->sin_family != AF_UNSPEC)
 545                                return -EINVAL;
 546                }
 547
 548                daddr = usin->sin_addr.s_addr;
 549                dport = usin->sin_port;
 550                if (dport == 0)
 551                        return -EINVAL;
 552        } else {
 553                if (sk->sk_state != TCP_ESTABLISHED)
 554                        return -EDESTADDRREQ;
 555                daddr = inet->daddr;
 556                dport = inet->dport;
 557                /* Open fast path for connected socket.
 558                   Route will not be used, if at least one option is set.
 559                 */
 560                connected = 1;
 561        }
 562        ipc.addr = inet->saddr;
 563
 564        ipc.oif = sk->sk_bound_dev_if;
 565        if (msg->msg_controllen) {
 566                err = ip_cmsg_send(msg, &ipc);
 567                if (err)
 568                        return err;
 569                if (ipc.opt)
 570                        free = 1;
 571                connected = 0;
 572        }
 573        if (!ipc.opt)
 574                ipc.opt = inet->opt;
 575
 576        saddr = ipc.addr;
 577        ipc.addr = faddr = daddr;
 578
 579        if (ipc.opt && ipc.opt->srr) {
 580                if (!daddr)
 581                        return -EINVAL;
 582                faddr = ipc.opt->faddr;
 583                connected = 0;
 584        }
 585        tos = RT_TOS(inet->tos);
 586        if (sk->sk_localroute || (msg->msg_flags & MSG_DONTROUTE) || 
 587            (ipc.opt && ipc.opt->is_strictroute)) {
 588                tos |= RTO_ONLINK;
 589                connected = 0;
 590        }
 591
 592        if (MULTICAST(daddr)) {
 593                if (!ipc.oif)
 594                        ipc.oif = inet->mc_index;
 595                if (!saddr)
 596                        saddr = inet->mc_addr;
 597                connected = 0;
 598        }
 599
 600        if (connected)
 601                rt = (struct rtable*)sk_dst_check(sk, 0);
 602
 603        if (rt == NULL) {
 604                struct flowi fl = { .oif = ipc.oif,
 605                                    .nl_u = { .ip4_u =
 606                                              { .daddr = faddr,
 607                                                .saddr = saddr,
 608                                                .tos = tos } },
 609                                    .proto = IPPROTO_UDP,
 610                                    .uli_u = { .ports =
 611                                               { .sport = inet->sport,
 612                                                 .dport = dport } } };
 613                err = ip_route_output_flow(&rt, &fl, sk, !(msg->msg_flags&MSG_DONTWAIT));
 614                if (err)
 615                        goto out;
 616
 617                err = -EACCES;
 618                if ((rt->rt_flags & RTCF_BROADCAST) &&
 619                    !sock_flag(sk, SOCK_BROADCAST))
 620                        goto out;
 621                if (connected)
 622                        sk_dst_set(sk, dst_clone(&rt->u.dst));
 623        }
 624
 625        if (msg->msg_flags&MSG_CONFIRM)
 626                goto do_confirm;
 627back_from_confirm:
 628
 629        saddr = rt->rt_src;
 630        if (!ipc.addr)
 631                daddr = ipc.addr = rt->rt_dst;
 632
 633        lock_sock(sk);
 634        if (unlikely(up->pending)) {
 635                /* The socket is already corked while preparing it. */
 636                /* ... which is an evident application bug. --ANK */
 637                release_sock(sk);
 638
 639                NETDEBUG(if (net_ratelimit()) printk(KERN_DEBUG "udp cork app bug 2\n"));
 640                err = -EINVAL;
 641                goto out;
 642        }
 643        /*
 644         *      Now cork the socket to pend data.
 645         */
 646        inet->cork.fl.fl4_dst = daddr;
 647        inet->cork.fl.fl_ip_dport = dport;
 648        inet->cork.fl.fl4_src = saddr;
 649        inet->cork.fl.fl_ip_sport = inet->sport;
 650        up->pending = AF_INET;
 651
 652do_append_data:
 653        up->len += ulen;
 654        err = ip_append_data(sk, ip_generic_getfrag, msg->msg_iov, ulen, 
 655                        sizeof(struct udphdr), &ipc, rt, 
 656                        corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
 657        if (err)
 658                udp_flush_pending_frames(sk);
 659        else if (!corkreq)
 660                err = udp_push_pending_frames(sk, up);
 661        release_sock(sk);
 662
 663out:
 664        ip_rt_put(rt);
 665        if (free)
 666                kfree(ipc.opt);
 667        if (!err) {
 668                UDP_INC_STATS_USER(UdpOutDatagrams);
 669                return len;
 670        }
 671        return err;
 672
 673do_confirm:
 674        dst_confirm(&rt->u.dst);
 675        if (!(msg->msg_flags&MSG_PROBE) || len)
 676                goto back_from_confirm;
 677        err = 0;
 678        goto out;
 679}
 680
 681int udp_sendpage(struct sock *sk, struct page *page, int offset, size_t size, int flags)
 682{
 683        struct udp_opt *up = udp_sk(sk);
 684        int ret;
 685
 686        if (!up->pending) {
 687                struct msghdr msg = {   .msg_flags = flags|MSG_MORE };
 688
 689                /* Call udp_sendmsg to specify destination address which
 690                 * sendpage interface can't pass.
 691                 * This will succeed only when the socket is connected.
 692                 */
 693                ret = udp_sendmsg(NULL, sk, &msg, 0);
 694                if (ret < 0)
 695                        return ret;
 696        }
 697
 698        lock_sock(sk);
 699
 700        if (unlikely(!up->pending)) {
 701                release_sock(sk);
 702
 703                NETDEBUG(if (net_ratelimit()) printk(KERN_DEBUG "udp cork app bug 3\n"));
 704                return -EINVAL;
 705        }
 706
 707        ret = ip_append_page(sk, page, offset, size, flags);
 708        if (ret == -EOPNOTSUPP) {
 709                release_sock(sk);
 710                return sock_no_sendpage(sk->sk_socket, page, offset,
 711                                        size, flags);
 712        }
 713        if (ret < 0) {
 714                udp_flush_pending_frames(sk);
 715                goto out;
 716        }
 717
 718        up->len += size;
 719        if (!(up->corkflag || (flags&MSG_MORE)))
 720                ret = udp_push_pending_frames(sk, up);
 721        if (!ret)
 722                ret = size;
 723out:
 724        release_sock(sk);
 725        return ret;
 726}
 727
 728/*
 729 *      IOCTL requests applicable to the UDP protocol
 730 */
 731 
 732int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
 733{
 734        switch(cmd) 
 735        {
 736                case SIOCOUTQ:
 737                {
 738                        int amount = atomic_read(&sk->sk_wmem_alloc);
 739                        return put_user(amount, (int *)arg);
 740                }
 741
 742                case SIOCINQ:
 743                {
 744                        struct sk_buff *skb;
 745                        unsigned long amount;
 746
 747                        amount = 0;
 748                        spin_lock_irq(&sk->sk_receive_queue.lock);
 749                        skb = skb_peek(&sk->sk_receive_queue);
 750                        if (skb != NULL) {
 751                                /*
 752                                 * We will only return the amount
 753                                 * of this packet since that is all
 754                                 * that will be read.
 755                                 */
 756                                amount = skb->len - sizeof(struct udphdr);
 757                        }
 758                        spin_unlock_irq(&sk->sk_receive_queue.lock);
 759                        return put_user(amount, (int *)arg);
 760                }
 761
 762                default:
 763                        return -ENOIOCTLCMD;
 764        }
 765        return(0);
 766}
 767
 768static __inline__ int __udp_checksum_complete(struct sk_buff *skb)
 769{
 770        return (unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum));
 771}
 772
 773static __inline__ int udp_checksum_complete(struct sk_buff *skb)
 774{
 775        return skb->ip_summed != CHECKSUM_UNNECESSARY &&
 776                __udp_checksum_complete(skb);
 777}
 778
 779/*
 780 *      This should be easy, if there is something there we
 781 *      return it, otherwise we block.
 782 */
 783
 784int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
 785                int len, int noblock, int flags, int *addr_len)
 786{
 787        struct inet_opt *inet = inet_sk(sk);
 788        struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
 789        struct sk_buff *skb;
 790        int copied, err;
 791
 792        /*
 793         *      Check any passed addresses
 794         */
 795        if (addr_len)
 796                *addr_len=sizeof(*sin);
 797
 798        if (flags & MSG_ERRQUEUE)
 799                return ip_recv_error(sk, msg, len);
 800
 801        skb = skb_recv_datagram(sk, flags, noblock, &err);
 802        if (!skb)
 803                goto out;
 804  
 805        copied = skb->len - sizeof(struct udphdr);
 806        if (copied > len) {
 807                copied = len;
 808                msg->msg_flags |= MSG_TRUNC;
 809        }
 810
 811        if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
 812                err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
 813                                              copied);
 814        } else if (msg->msg_flags&MSG_TRUNC) {
 815                if (__udp_checksum_complete(skb))
 816                        goto csum_copy_err;
 817                err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
 818                                              copied);
 819        } else {
 820                err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
 821
 822                if (err == -EINVAL)
 823                        goto csum_copy_err;
 824        }
 825
 826        if (err)
 827                goto out_free;
 828
 829        sock_recv_timestamp(msg, sk, skb);
 830
 831        /* Copy the address. */
 832        if (sin)
 833        {
 834                sin->sin_family = AF_INET;
 835                sin->sin_port = skb->h.uh->source;
 836                sin->sin_addr.s_addr = skb->nh.iph->saddr;
 837                memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
 838        }
 839        if (inet->cmsg_flags)
 840                ip_cmsg_recv(msg, skb);
 841        err = copied;
 842  
 843out_free:
 844        skb_free_datagram(sk, skb);
 845out:
 846        return err;
 847
 848csum_copy_err:
 849        UDP_INC_STATS_BH(UdpInErrors);
 850
 851        /* Clear queue. */
 852        if (flags&MSG_PEEK) {
 853                int clear = 0;
 854                spin_lock_irq(&sk->sk_receive_queue.lock);
 855                if (skb == skb_peek(&sk->sk_receive_queue)) {
 856                        __skb_unlink(skb, &sk->sk_receive_queue);
 857                        clear = 1;
 858                }
 859                spin_unlock_irq(&sk->sk_receive_queue.lock);
 860                if (clear)
 861                        kfree_skb(skb);
 862        }
 863
 864        skb_free_datagram(sk, skb);
 865
 866        return -EAGAIN; 
 867}
 868
 869int udp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
 870{
 871        struct inet_opt *inet = inet_sk(sk);
 872        struct sockaddr_in *usin = (struct sockaddr_in *) uaddr;
 873        struct rtable *rt;
 874        u32 saddr;
 875        int oif;
 876        int err;
 877
 878        
 879        if (addr_len < sizeof(*usin)) 
 880                return -EINVAL;
 881
 882        if (usin->sin_family != AF_INET) 
 883                return -EAFNOSUPPORT;
 884
 885        sk_dst_reset(sk);
 886
 887        oif = sk->sk_bound_dev_if;
 888        saddr = inet->saddr;
 889        if (MULTICAST(usin->sin_addr.s_addr)) {
 890                if (!oif)
 891                        oif = inet->mc_index;
 892                if (!saddr)
 893                        saddr = inet->mc_addr;
 894        }
 895        err = ip_route_connect(&rt, usin->sin_addr.s_addr, saddr,
 896                               RT_CONN_FLAGS(sk), oif,
 897                               IPPROTO_UDP,
 898                               inet->sport, usin->sin_port, sk);
 899        if (err)
 900                return err;
 901        if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) {
 902                ip_rt_put(rt);
 903                return -EACCES;
 904        }
 905        if (!inet->saddr)
 906                inet->saddr = rt->rt_src;       /* Update source address */
 907        if (!inet->rcv_saddr)
 908                inet->rcv_saddr = rt->rt_src;
 909        inet->daddr = rt->rt_dst;
 910        inet->dport = usin->sin_port;
 911        sk->sk_state = TCP_ESTABLISHED;
 912        inet->id = jiffies;
 913
 914        sk_dst_set(sk, &rt->u.dst);
 915        return(0);
 916}
 917
 918int udp_disconnect(struct sock *sk, int flags)
 919{
 920        struct inet_opt *inet = inet_sk(sk);
 921        /*
 922         *      1003.1g - break association.
 923         */
 924         
 925        sk->sk_state = TCP_CLOSE;
 926        inet->daddr = 0;
 927        inet->dport = 0;
 928        sk->sk_bound_dev_if = 0;
 929        if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
 930                inet_reset_saddr(sk);
 931
 932        if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
 933                sk->sk_prot->unhash(sk);
 934                inet->sport = 0;
 935        }
 936        sk_dst_reset(sk);
 937        return 0;
 938}
 939
 940static void udp_close(struct sock *sk, long timeout)
 941{
 942        inet_sock_release(sk);
 943}
 944
 945/* return:
 946 *      1  if the the UDP system should process it
 947 *      0  if we should drop this packet
 948 *      -1 if it should get processed by xfrm4_rcv_encap
 949 */
 950static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb)
 951{
 952#ifndef CONFIG_XFRM
 953        return 1; 
 954#else
 955        struct udp_opt *up = udp_sk(sk);
 956        struct udphdr *uh = skb->h.uh;
 957        struct iphdr *iph;
 958        int iphlen, len;
 959  
 960        __u8 *udpdata = (__u8 *)uh + sizeof(struct udphdr);
 961        __u32 *udpdata32 = (__u32 *)udpdata;
 962        __u16 encap_type = up->encap_type;
 963
 964        /* if we're overly short, let UDP handle it */
 965        if (udpdata > skb->tail)
 966                return 1;
 967
 968        /* if this is not encapsulated socket, then just return now */
 969        if (!encap_type)
 970                return 1;
 971
 972        len = skb->tail - udpdata;
 973
 974        switch (encap_type) {
 975        case UDP_ENCAP_ESPINUDP:
 976                /* Check if this is a keepalive packet.  If so, eat it. */
 977                if (len == 1 && udpdata[0] == 0xff) {
 978                        return 0;
 979                } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0 ) {
 980                        /* ESP Packet without Non-ESP header */
 981                        len = sizeof(struct udphdr);
 982                } else
 983                        /* Must be an IKE packet.. pass it through */
 984                        return 1;
 985
 986                /* At this point we are sure that this is an ESPinUDP packet,
 987                 * so we need to remove 'len' bytes from the packet (the UDP
 988                 * header and optional ESP marker bytes) and then modify the
 989                 * protocol to ESP, and then call into the transform receiver.
 990                 */
 991
 992                /* Now we can update and verify the packet length... */
 993                iph = skb->nh.iph;
 994                iphlen = iph->ihl << 2;
 995                iph->tot_len = htons(ntohs(iph->tot_len) - len);
 996                if (skb->len < iphlen + len) {
 997                        /* packet is too small!?! */
 998                        return 0;
 999                }
1000
1001                /* pull the data buffer up to the ESP header and set the
1002                 * transport header to point to ESP.  Keep UDP on the stack
1003                 * for later.
1004                 */
1005                skb->h.raw = skb_pull(skb, len);
1006
1007                /* modify the protocol (it's ESP!) */
1008                iph->protocol = IPPROTO_ESP;
1009
1010                /* and let the caller know to send this into the ESP processor... */
1011                return -1;
1012
1013        default:
1014                if (net_ratelimit())
1015                        printk(KERN_INFO "udp_encap_rcv(): Unhandled UDP encap type: %u\n",
1016                               encap_type);
1017                return 1;
1018        }
1019#endif
1020}
1021
1022/* returns:
1023 *  -1: error
1024 *   0: success
1025 *  >0: "udp encap" protocol resubmission
1026 *
1027 * Note that in the success and error cases, the skb is assumed to
1028 * have either been requeued or freed.
1029 */
1030static int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
1031{
1032        struct udp_opt *up = udp_sk(sk);
1033
1034        /*
1035         *      Charge it to the socket, dropping if the queue is full.
1036         */
1037        if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
1038                kfree_skb(skb);
1039                return -1;
1040        }
1041
1042        if (up->encap_type) {
1043                /*
1044                 * This is an encapsulation socket, so let's see if this is
1045                 * an encapsulated packet.
1046                 * If it's a keepalive packet, then just eat it.
1047                 * If it's an encapsulateed packet, then pass it to the
1048                 * IPsec xfrm input and return the response
1049                 * appropriately.  Otherwise, just fall through and
1050                 * pass this up the UDP socket.
1051                 */
1052                int ret;
1053
1054                ret = udp_encap_rcv(sk, skb);
1055                if (ret == 0) {
1056                        /* Eat the packet .. */
1057                        kfree_skb(skb);
1058                        return 0;
1059                }
1060                if (ret < 0) {
1061                        /* process the ESP packet */
1062                        ret = xfrm4_rcv_encap(skb, up->encap_type);
1063                        UDP_INC_STATS_BH(UdpInDatagrams);
1064                        return -ret;
1065                }
1066                /* FALLTHROUGH -- it's a UDP Packet */
1067        }
1068
1069        if (sk->sk_filter && skb->ip_summed != CHECKSUM_UNNECESSARY) {
1070                if (__udp_checksum_complete(skb)) {
1071                        UDP_INC_STATS_BH(UdpInErrors);
1072                        kfree_skb(skb);
1073                        return -1;
1074                }
1075                skb->ip_summed = CHECKSUM_UNNECESSARY;
1076        }
1077
1078        if (sock_queue_rcv_skb(sk,skb)<0) {
1079                UDP_INC_STATS_BH(UdpInErrors);
1080                kfree_skb(skb);
1081                return -1;
1082        }
1083        UDP_INC_STATS_BH(UdpInDatagrams);
1084        return 0;
1085}
1086
1087/*
1088 *      Multicasts and broadcasts go to each listener.
1089 *
1090 *      Note: called only from the BH handler context,
1091 *      so we don't need to lock the hashes.
1092 */
1093static int udp_v4_mcast_deliver(struct sk_buff *skb, struct udphdr *uh,
1094                                 u32 saddr, u32 daddr)
1095{
1096        struct sock *sk;
1097        int dif;
1098
1099        read_lock(&udp_hash_lock);
1100        sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
1101        dif = skb->dev->ifindex;
1102        sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
1103        if (sk) {
1104                struct sock *sknext = NULL;
1105
1106                do {
1107                        struct sk_buff *skb1 = skb;
1108
1109                        sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr,
1110                                                   uh->source, saddr, dif);
1111                        if(sknext)
1112                                skb1 = skb_clone(skb, GFP_ATOMIC);
1113
1114                        if(skb1) {
1115                                int ret = udp_queue_rcv_skb(sk, skb1);
1116                                if (ret > 0)
1117                                        /* we should probably re-process instead
1118                                         * of dropping packets here. */
1119                                        kfree_skb(skb1);
1120                        }
1121                        sk = sknext;
1122                } while(sknext);
1123        } else
1124                kfree_skb(skb);
1125        read_unlock(&udp_hash_lock);
1126        return 0;
1127}
1128
1129/* Initialize UDP checksum. If exited with zero value (success),
1130 * CHECKSUM_UNNECESSARY means, that no more checks are required.
1131 * Otherwise, csum completion requires chacksumming packet body,
1132 * including udp header and folding it to skb->csum.
1133 */
1134static int udp_checksum_init(struct sk_buff *skb, struct udphdr *uh,
1135                             unsigned short ulen, u32 saddr, u32 daddr)
1136{
1137        if (uh->check == 0) {
1138                skb->ip_summed = CHECKSUM_UNNECESSARY;
1139        } else if (skb->ip_summed == CHECKSUM_HW) {
1140                skb->ip_summed = CHECKSUM_UNNECESSARY;
1141                if (!udp_check(uh, ulen, saddr, daddr, skb->csum))
1142                        return 0;
1143                NETDEBUG(if (net_ratelimit()) printk(KERN_DEBUG "udp v4 hw csum failure.\n"));
1144                skb->ip_summed = CHECKSUM_NONE;
1145        }
1146        if (skb->ip_summed != CHECKSUM_UNNECESSARY)
1147                skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
1148        /* Probably, we should checksum udp header (it should be in cache
1149         * in any case) and data in tiny packets (< rx copybreak).
1150         */
1151        return 0;
1152}
1153
1154/*
1155 *      All we need to do is get the socket, and then do a checksum. 
1156 */
1157 
1158int udp_rcv(struct sk_buff *skb)
1159{
1160        struct sock *sk;
1161        struct udphdr *uh;
1162        unsigned short ulen;
1163        struct rtable *rt = (struct rtable*)skb->dst;
1164        u32 saddr = skb->nh.iph->saddr;
1165        u32 daddr = skb->nh.iph->daddr;
1166        int len = skb->len;
1167
1168        /*
1169         *      Validate the packet and the UDP length.
1170         */
1171        if (!pskb_may_pull(skb, sizeof(struct udphdr)))
1172                goto no_header;
1173
1174        uh = skb->h.uh;
1175
1176        ulen = ntohs(uh->len);
1177
1178        if (ulen > len || ulen < sizeof(*uh))
1179                goto short_packet;
1180
1181        if (pskb_trim(skb, ulen))
1182                goto short_packet;
1183
1184        if (udp_checksum_init(skb, uh, ulen, saddr, daddr) < 0)
1185                goto csum_error;
1186
1187        if(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
1188                return udp_v4_mcast_deliver(skb, uh, saddr, daddr);
1189
1190        sk = udp_v4_lookup(saddr, uh->source, daddr, uh->dest, skb->dev->ifindex);
1191
1192        if (sk != NULL) {
1193                int ret = udp_queue_rcv_skb(sk, skb);
1194                sock_put(sk);
1195
1196                /* a return value > 0 means to resubmit the input, but
1197                 * it it wants the return to be -protocol, or 0
1198                 */
1199                if (ret > 0)
1200                        return -ret;
1201                return 0;
1202        }
1203
1204        if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1205                goto drop;
1206
1207        /* No socket. Drop packet silently, if checksum is wrong */
1208        if (udp_checksum_complete(skb))
1209                goto csum_error;
1210
1211        UDP_INC_STATS_BH(UdpNoPorts);
1212        icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1213
1214        /*
1215         * Hmm.  We got an UDP packet to a port to which we
1216         * don't wanna listen.  Ignore it.
1217         */
1218        kfree_skb(skb);
1219        return(0);
1220
1221short_packet:
1222        NETDEBUG(if (net_ratelimit())
1223                printk(KERN_DEBUG "UDP: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n",
1224                        NIPQUAD(saddr),
1225                        ntohs(uh->source),
1226                        ulen,
1227                        len,
1228                        NIPQUAD(daddr),
1229                        ntohs(uh->dest)));
1230no_header:
1231        UDP_INC_STATS_BH(UdpInErrors);
1232        kfree_skb(skb);
1233        return(0);
1234
1235csum_error:
1236        /* 
1237         * RFC1122: OK.  Discards the bad packet silently (as far as 
1238         * the network is concerned, anyway) as per 4.1.3.4 (MUST). 
1239         */
1240        NETDEBUG(if (net_ratelimit())
1241                 printk(KERN_DEBUG "UDP: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
1242                        NIPQUAD(saddr),
1243                        ntohs(uh->source),
1244                        NIPQUAD(daddr),
1245                        ntohs(uh->dest),
1246                        ulen));
1247drop:
1248        UDP_INC_STATS_BH(UdpInErrors);
1249        kfree_skb(skb);
1250        return(0);
1251}
1252
1253static int udp_destroy_sock(struct sock *sk)
1254{
1255        lock_sock(sk);
1256        udp_flush_pending_frames(sk);
1257        release_sock(sk);
1258        return 0;
1259}
1260
1261/*
1262 *      Socket option code for UDP
1263 */
1264static int udp_setsockopt(struct sock *sk, int level, int optname, 
1265                          char *optval, int optlen)
1266{
1267        struct udp_opt *up = udp_sk(sk);
1268        int val;
1269        int err = 0;
1270
1271        if (level != SOL_UDP)
1272                return ip_setsockopt(sk, level, optname, optval, optlen);
1273
1274        if(optlen<sizeof(int))
1275                return -EINVAL;
1276
1277        if (get_user(val, (int *)optval))
1278                return -EFAULT;
1279
1280        switch(optname) {
1281        case UDP_CORK:
1282                if (val != 0) {
1283                        up->corkflag = 1;
1284                } else {
1285                        up->corkflag = 0;
1286                        lock_sock(sk);
1287                        udp_push_pending_frames(sk, up);
1288                        release_sock(sk);
1289                }
1290                break;
1291                
1292        case UDP_ENCAP:
1293                up->encap_type = val;
1294                break;
1295
1296        default:
1297                err = -ENOPROTOOPT;
1298                break;
1299        };
1300
1301        return err;
1302}
1303
1304static int udp_getsockopt(struct sock *sk, int level, int optname, 
1305                          char *optval, int *optlen)
1306{
1307        struct udp_opt *up = udp_sk(sk);
1308        int val, len;
1309
1310        if (level != SOL_UDP)
1311                return ip_getsockopt(sk, level, optname, optval, optlen);
1312
1313        if(get_user(len,optlen))
1314                return -EFAULT;
1315
1316        len = min_t(unsigned int, len, sizeof(int));
1317        
1318        if(len < 0)
1319                return -EINVAL;
1320
1321        switch(optname) {
1322        case UDP_CORK:
1323                val = up->corkflag;
1324                break;
1325
1326        case UDP_ENCAP:
1327                val = up->encap_type;
1328                break;
1329
1330        default:
1331                return -ENOPROTOOPT;
1332        };
1333
1334        if(put_user(len, optlen))
1335                return -EFAULT;
1336        if(copy_to_user(optval, &val,len))
1337                return -EFAULT;
1338        return 0;
1339}
1340
1341
1342struct proto udp_prot = {
1343        .name =         "UDP",
1344        .close =        udp_close,
1345        .connect =      udp_connect,
1346        .disconnect =   udp_disconnect,
1347        .ioctl =        udp_ioctl,
1348        .destroy =      udp_destroy_sock,
1349        .setsockopt =   udp_setsockopt,
1350        .getsockopt =   udp_getsockopt,
1351        .sendmsg =      udp_sendmsg,
1352        .recvmsg =      udp_recvmsg,
1353        .sendpage =     udp_sendpage,
1354        .backlog_rcv =  udp_queue_rcv_skb,
1355        .hash =         udp_v4_hash,
1356        .unhash =       udp_v4_unhash,
1357        .get_port =     udp_v4_get_port,
1358};
1359
1360/* ------------------------------------------------------------------------ */
1361#ifdef CONFIG_PROC_FS
1362
1363static struct sock *udp_get_first(struct seq_file *seq)
1364{
1365        struct sock *sk;
1366        struct udp_iter_state *state = seq->private;
1367
1368        for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
1369                struct hlist_node *node;
1370                sk_for_each(sk, node, &udp_hash[state->bucket]) {
1371                        if (sk->sk_family == state->family)
1372                                goto found;
1373                }
1374        }
1375        sk = NULL;
1376found:
1377        return sk;
1378}
1379
1380static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
1381{
1382        struct udp_iter_state *state = seq->private;
1383
1384        do {
1385                sk = sk_next(sk);
1386try_again:
1387                ;
1388        } while (sk && sk->sk_family != state->family);
1389
1390        if (!sk && ++state->bucket < UDP_HTABLE_SIZE) {
1391                sk = sk_head(&udp_hash[state->bucket]);
1392                goto try_again;
1393        }
1394        return sk;
1395}
1396
1397static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
1398{
1399        struct sock *sk = udp_get_first(seq);
1400
1401        if (sk)
1402                while(pos && (sk = udp_get_next(seq, sk)) != NULL)
1403                        --pos;
1404        return pos ? NULL : sk;
1405}
1406
1407static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
1408{
1409        read_lock(&udp_hash_lock);
1410        return *pos ? udp_get_idx(seq, *pos-1) : (void *)1;
1411}
1412
1413static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1414{
1415        struct sock *sk;
1416
1417        if (v == (void *)1)
1418                sk = udp_get_idx(seq, 0);
1419        else
1420                sk = udp_get_next(seq, v);
1421
1422        ++*pos;
1423        return sk;
1424}
1425
1426static void udp_seq_stop(struct seq_file *seq, void *v)
1427{
1428        read_unlock(&udp_hash_lock);
1429}
1430
1431static int udp_seq_open(struct inode *inode, struct file *file)
1432{
1433        struct udp_seq_afinfo *afinfo = PDE(inode)->data;
1434        struct seq_file *seq;
1435        int rc = -ENOMEM;
1436        struct udp_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
1437
1438        if (!s)
1439                goto out;
1440        memset(s, 0, sizeof(*s));
1441        s->family               = afinfo->family;
1442        s->seq_ops.start        = udp_seq_start;
1443        s->seq_ops.next         = udp_seq_next;
1444        s->seq_ops.show         = afinfo->seq_show;
1445        s->seq_ops.stop         = udp_seq_stop;
1446
1447        rc = seq_open(file, &s->seq_ops);
1448        if (rc)
1449                goto out_kfree;
1450
1451        seq          = file->private_data;
1452        seq->private = s;
1453out:
1454        return rc;
1455out_kfree:
1456        kfree(s);
1457        goto out;
1458}
1459
1460/* ------------------------------------------------------------------------ */
1461int udp_proc_register(struct udp_seq_afinfo *afinfo)
1462{
1463        struct proc_dir_entry *p;
1464        int rc = 0;
1465
1466        if (!afinfo)
1467                return -EINVAL;
1468        afinfo->seq_fops->owner         = afinfo->owner;
1469        afinfo->seq_fops->open          = udp_seq_open;
1470        afinfo->seq_fops->read          = seq_read;
1471        afinfo->seq_fops->llseek        = seq_lseek;
1472        afinfo->seq_fops->release       = seq_release_private;
1473
1474        p = proc_net_fops_create(afinfo->name, S_IRUGO, afinfo->seq_fops);
1475        if (p)
1476                p->data = afinfo;
1477        else
1478                rc = -ENOMEM;
1479        return rc;
1480}
1481
1482void udp_proc_unregister(struct udp_seq_afinfo *afinfo)
1483{
1484        if (!afinfo)
1485                return;
1486        proc_net_remove(afinfo->name);
1487        memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
1488}
1489
1490/* ------------------------------------------------------------------------ */
1491static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket)
1492{
1493        struct inet_opt *inet = inet_sk(sp);
1494        unsigned int dest = inet->daddr;
1495        unsigned int src  = inet->rcv_saddr;
1496        __u16 destp       = ntohs(inet->dport);
1497        __u16 srcp        = ntohs(inet->sport);
1498
1499        sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
1500                " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
1501                bucket, src, srcp, dest, destp, sp->sk_state, 
1502                atomic_read(&sp->sk_wmem_alloc),
1503                atomic_read(&sp->sk_rmem_alloc),
1504                0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
1505                atomic_read(&sp->sk_refcnt), sp);
1506}
1507
1508static int udp4_seq_show(struct seq_file *seq, void *v)
1509{
1510        if (v == SEQ_START_TOKEN)
1511                seq_printf(seq, "%-127s\n",
1512                           "  sl  local_address rem_address   st tx_queue "
1513                           "rx_queue tr tm->when retrnsmt   uid  timeout "
1514                           "inode");
1515        else {
1516                char tmpbuf[129];
1517                struct udp_iter_state *state = seq->private;
1518
1519                udp4_format_sock(v, tmpbuf, state->bucket);
1520                seq_printf(seq, "%-127s\n", tmpbuf);
1521        }
1522        return 0;
1523}
1524
1525/* ------------------------------------------------------------------------ */
1526static struct file_operations udp4_seq_fops;
1527static struct udp_seq_afinfo udp4_seq_afinfo = {
1528        .owner          = THIS_MODULE,
1529        .name           = "udp",
1530        .family         = AF_INET,
1531        .seq_show       = udp4_seq_show,
1532        .seq_fops       = &udp4_seq_fops,
1533};
1534
1535int __init udp4_proc_init(void)
1536{
1537        return udp_proc_register(&udp4_seq_afinfo);
1538}
1539
1540void udp4_proc_exit(void)
1541{
1542        udp_proc_unregister(&udp4_seq_afinfo);
1543}
1544#endif /* CONFIG_PROC_FS */
1545
1546EXPORT_SYMBOL(udp_connect);
1547EXPORT_SYMBOL(udp_disconnect);
1548EXPORT_SYMBOL(udp_hash);
1549EXPORT_SYMBOL(udp_hash_lock);
1550EXPORT_SYMBOL(udp_ioctl);
1551EXPORT_SYMBOL(udp_port_rover);
1552EXPORT_SYMBOL(udp_prot);
1553EXPORT_SYMBOL(udp_sendmsg);
1554
1555#ifdef CONFIG_PROC_FS
1556EXPORT_SYMBOL(udp_proc_register);
1557EXPORT_SYMBOL(udp_proc_unregister);
1558#endif
1559
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.