root/include/net/sock.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. lock_sock
  2. release_sock
  3. sock_queue_rcv_skb
  4. sock_error

   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  *              Definitions for the AF_INET socket handler.
   7  *
   8  * Version:     @(#)sock.h      1.0.4   05/13/93
   9  *
  10  * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  11  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12  *              Corey Minyard <wf-rch!minyard@relay.EU.net>
  13  *              Florian La Roche <flla@stud.uni-sb.de>
  14  *
  15  * Fixes:
  16  *              Alan Cox        :       Volatiles in skbuff pointers. See
  17  *                                      skbuff comments. May be overdone,
  18  *                                      better to prove they can be removed
  19  *                                      than the reverse.
  20  *              Alan Cox        :       Added a zapped field for tcp to note
  21  *                                      a socket is reset and must stay shut up
  22  *              Alan Cox        :       New fields for options
  23  *      Pauline Middelink       :       identd support
  24  *              Alan Cox        :       Eliminate low level recv/recvfrom
  25  *
  26  *              This program is free software; you can redistribute it and/or
  27  *              modify it under the terms of the GNU General Public License
  28  *              as published by the Free Software Foundation; either version
  29  *              2 of the License, or (at your option) any later version.
  30  */
  31 #ifndef _SOCK_H
  32 #define _SOCK_H
  33 
  34 #include <linux/timer.h>
  35 #include <linux/ip.h>           /* struct options */
  36 #include <linux/in.h>           /* struct sockaddr_in */
  37 #include <linux/tcp.h>          /* struct tcphdr */
  38 #include <linux/config.h>
  39 
  40 #include <linux/netdevice.h>
  41 #include <linux/skbuff.h>       /* struct sk_buff */
  42 #include <net/protocol.h>               /* struct inet_protocol */
  43 #ifdef CONFIG_AX25
  44 #include <net/ax25.h>
  45 #ifdef CONFIG_NETROM
  46 #include <net/netrom.h>
  47 #endif
  48 #endif
  49 #ifdef CONFIG_IPX
  50 #include <net/ipx.h>
  51 #endif
  52 #ifdef CONFIG_ATALK
  53 #include <linux/atalk.h>
  54 #endif
  55 
  56 #include <linux/igmp.h>
  57 
  58 #include <asm/atomic.h>
  59 
  60 /* Think big (also on some systems a byte is faster) */
  61 #define SOCK_ARRAY_SIZE 256
  62 
  63 
  64 /*
  65  *      The AF_UNIX specific socket options
  66  */
  67  
  68 struct unix_opt
  69 {
  70         int                     family;
  71         char *                  name;
  72         int                     locks;
  73         struct inode *          inode;
  74         struct semaphore        readsem;
  75         struct sock *           other;
  76 };
  77 
  78 /*
  79  *      IP packet socket options
  80  */
  81 
  82 struct inet_packet_opt
  83 {
  84         struct notifier_block   notifier;               /* Used when bound */
  85         struct device           *bound_dev;
  86         unsigned long           dev_stamp;
  87         struct packet_type      *prot_hook;
  88         char                    device_name[15];
  89 };
  90 
  91 /*
  92  *      Once the IPX ncpd patches are in these are going into protinfo
  93  */
  94 
  95 #ifdef CONFIG_IPX 
  96 struct ipx_opt
  97 {
  98         ipx_address             dest_addr;
  99         ipx_interface           *intrfc;
 100         unsigned short          port;
 101 #ifdef CONFIG_IPX_INTERN
 102         unsigned char           node[IPX_NODE_LEN];
 103 #endif
 104         unsigned short          type;
 105 /* 
 106  * To handle asynchronous messages from the NetWare server, we have to
 107  * know the connection this socket belongs to. 
 108  */
 109         struct ncp_server       *ncp_server;
 110         
 111 };
 112 #endif
 113 
 114 #ifdef CONFIG_NUTCP
 115 struct tcp_opt
 116 {
 117 /*
 118  *      RFC793 variables by their proper names. This means you can
 119  *      read the code and the spec side by side (and laugh ...)
 120  *      See RFC793 and RFC1122. The RFC writes these in capitals.
 121  */
 122         __u32   rcv_nxt;        /* What we want to receive next         */
 123         __u32   rcv_up;         /* The urgent point (may not be valid)  */
 124         __u32   rcv_wnd;        /* Current receiver window              */
 125         __u32   snd_nxt;        /* Next sequence we send                */
 126         __u32   snd_una;        /* First byte we want an ack for        */
 127         __u32   snd_up;         /* Outgoing urgent pointer              */
 128         __u32   snd_wl1;        /* Sequence for window update           */
 129         __u32   snd_wl2;        /* Ack sequence for update              */
 130 /*
 131  *      Slow start and congestion control (see also Nagle, and Karn & Partridge)
 132  */
 133         __u32   snd_cwnd;       /* Sending congestion window            */
 134         __u32   snd_ssthresh;   /* Slow start size threshold            */
 135 /*
 136  *      Timers used by the TCP protocol layer
 137  */
 138         struct timer_list       delack_timer;           /* Ack delay    */
 139         struct timer_list       idle_timer;             /* Idle watch   */
 140         struct timer_list       completion_timer;       /* Up/Down timer */
 141         struct timer_list       probe_timer;            /* Probes       */
 142         struct timer_list       retransmit_timer;       /* Resend (no ack) */
 143 };
 144 #endif
 145         
 146 /*
 147  * This structure really needs to be cleaned up.
 148  * Most of it is for TCP, and not used by any of
 149  * the other protocols.
 150  */
 151 struct sock 
 152 {
 153         struct options          *opt;
 154         atomic_t                wmem_alloc;
 155         atomic_t                rmem_alloc;
 156         unsigned long           allocation;             /* Allocation mode */
 157         __u32                   write_seq;
 158         __u32                   sent_seq;
 159         __u32                   acked_seq;
 160         __u32                   copied_seq;
 161         __u32                   rcv_ack_seq;
 162         __u32                   window_seq;
 163         __u32                   fin_seq;
 164         __u32                   urg_seq;
 165         __u32                   urg_data;
 166         int                     users;                  /* user count */
 167   /*
 168    *    Not all are volatile, but some are, so we
 169    *    might as well say they all are.
 170    */
 171         volatile char           dead,
 172                                 urginline,
 173                                 intr,
 174                                 blog,
 175                                 done,
 176                                 reuse,
 177                                 keepopen,
 178                                 linger,
 179                                 delay_acks,
 180                                 destroy,
 181                                 ack_timed,
 182                                 no_check,
 183                                 zapped, /* In ax25 & ipx means not linked */
 184                                 broadcast,
 185                                 nonagle,
 186                                 bsdism;
 187         unsigned long           lingertime;
 188         int                     proc;
 189         struct sock             *next;
 190         struct sock             *prev; /* Doubly linked chain.. */
 191         struct sock             *pair;
 192         struct sk_buff          * volatile send_head;
 193         struct sk_buff          * volatile send_tail;
 194         struct sk_buff_head     back_log;
 195         struct sk_buff          *partial;
 196         struct timer_list       partial_timer;
 197         long                    retransmits;
 198         struct sk_buff_head     write_queue,
 199                                 receive_queue;
 200         struct proto            *prot;
 201         struct wait_queue       **sleep;
 202         __u32                   daddr;
 203         __u32                   saddr;          /* Sending source */
 204         __u32                   rcv_saddr;      /* Bound address */
 205         unsigned short          max_unacked;
 206         unsigned short          window;
 207         __u32                   lastwin_seq;    /* sequence number when we last updated the window we offer */
 208         volatile unsigned long  ato;            /* ack timeout */
 209         volatile unsigned long  lrcvtime;       /* jiffies at last rcv */
 210         unsigned short          bytes_rcv;
 211 /*
 212  *      mss is min(mtu, max_window) 
 213  */
 214         unsigned short          mtu;       /* mss negotiated in the syn's */
 215         volatile unsigned short mss;       /* current eff. mss - can change */
 216         volatile unsigned short user_mss;  /* mss requested by user in ioctl */
 217         volatile unsigned short max_window;
 218         unsigned long           window_clamp;
 219         unsigned short          num;
 220         volatile unsigned short cong_window;
 221         volatile unsigned short cong_count;
 222         volatile unsigned short ssthresh;
 223         volatile unsigned short packets_out;
 224         volatile unsigned short shutdown;
 225         volatile unsigned long  rtt;
 226         volatile unsigned long  mdev;
 227         volatile unsigned long  rto;
 228 
 229 /*
 230  *      currently backoff isn't used, but I'm maintaining it in case
 231  *      we want to go back to a backoff formula that needs it
 232  */
 233  
 234         volatile unsigned short backoff;
 235         volatile int            err, err_soft;  /* Soft holds errors that don't
 236                                                    cause failure but are the cause
 237                                                    of a persistent failure not just
 238                                                    'timed out' */
 239         unsigned char           protocol;
 240         volatile unsigned char  state;
 241         unsigned char           ack_backlog;
 242         unsigned char           max_ack_backlog;
 243         unsigned char           priority;
 244         unsigned char           debug;
 245         unsigned short          rcvbuf;
 246         unsigned short          sndbuf;
 247         unsigned short          type;
 248         unsigned char           localroute;     /* Route locally only */
 249 #ifdef CONFIG_AX25
 250         ax25_cb                 *ax25;
 251 #ifdef CONFIG_NETROM
 252         nr_cb                   *nr;
 253 #endif
 254 #endif
 255   
 256 /*
 257  *      This is where all the private (optional) areas that don't
 258  *      overlap will eventually live. 
 259  */
 260 
 261         union
 262         {
 263                 struct unix_opt af_unix;
 264 #ifdef CONFIG_ATALK
 265                 struct atalk_sock       af_at;
 266 #endif
 267 #ifdef CONFIG_IPX
 268                 struct ipx_opt          af_ipx;
 269 #endif          
 270 #ifdef CONFIG_INET
 271                 struct inet_packet_opt  af_packet;
 272 #ifdef CONFIG_NUTCP             
 273                 struct tcp_opt          af_tcp;
 274 #endif          
 275 #endif
 276         } protinfo;             
 277 
 278 /* 
 279  *      IP 'private area' or will be eventually 
 280  */
 281         int                     ip_ttl;                 /* TTL setting */
 282         int                     ip_tos;                 /* TOS */
 283         struct tcphdr           dummy_th;
 284         struct timer_list       keepalive_timer;        /* TCP keepalive hack */
 285         struct timer_list       retransmit_timer;       /* TCP retransmit timer */
 286         struct timer_list       ack_timer;              /* TCP delayed ack timer */
 287         int                     ip_xmit_timeout;        /* Why the timeout is running */
 288         struct rtable           *ip_route_cache;        /* Cached output route */
 289         unsigned char           ip_hdrincl;             /* Include headers ? */
 290 #ifdef CONFIG_IP_MULTICAST  
 291         int                     ip_mc_ttl;              /* Multicasting TTL */
 292         int                     ip_mc_loop;             /* Loopback */
 293         char                    ip_mc_name[MAX_ADDR_LEN];/* Multicast device name */
 294         struct ip_mc_socklist   *ip_mc_list;            /* Group array */
 295 #endif  
 296 
 297 /*
 298  *      This part is used for the timeout functions (timer.c). 
 299  */
 300  
 301         int                     timeout;        /* What are we waiting for? */
 302         struct timer_list       timer;          /* This is the TIME_WAIT/receive timer
 303                                          * when we are doing IP
 304                                          */
 305         struct timeval          stamp;
 306 
 307  /*
 308   *     Identd 
 309   */
 310   
 311         struct socket           *socket;
 312   
 313   /*
 314    *    Callbacks 
 315    */
 316    
 317         void                    (*state_change)(struct sock *sk);
 318         void                    (*data_ready)(struct sock *sk,int bytes);
 319         void                    (*write_space)(struct sock *sk);
 320         void                    (*error_report)(struct sock *sk);
 321   
 322 };
 323 
 324 /*
 325  *      IP protocol blocks we attach to sockets.
 326  */
 327  
 328 struct proto 
 329 {
 330         void                    (*close)(struct sock *sk, unsigned long timeout);
 331         int                     (*build_header)(struct sk_buff *skb,
 332                                         __u32 saddr,
 333                                         __u32 daddr,
 334                                         struct device **dev, int type,
 335                                         struct options *opt, int len,
 336                                         int tos, int ttl, struct rtable ** rp);
 337         int                     (*connect)(struct sock *sk,
 338                                         struct sockaddr_in *usin, int addr_len);
 339         struct sock *           (*accept) (struct sock *sk, int flags);
 340         void                    (*queue_xmit)(struct sock *sk,
 341                                         struct device *dev, struct sk_buff *skb,
 342                                         int free);
 343         void                    (*retransmit)(struct sock *sk, int all);
 344         void                    (*write_wakeup)(struct sock *sk);
 345         void                    (*read_wakeup)(struct sock *sk);
 346         int                     (*rcv)(struct sk_buff *buff, struct device *dev,
 347                                         struct options *opt, __u32 daddr,
 348                                         unsigned short len, __u32 saddr,
 349                                         int redo, struct inet_protocol *protocol);
 350         int                     (*select)(struct sock *sk, int which,
 351                                         select_table *wait);
 352         int                     (*ioctl)(struct sock *sk, int cmd,
 353                                         unsigned long arg);
 354         int                     (*init)(struct sock *sk);
 355         void                    (*shutdown)(struct sock *sk, int how);
 356         int                     (*setsockopt)(struct sock *sk, int level, int optname,
 357                                         char *optval, int optlen);
 358         int                     (*getsockopt)(struct sock *sk, int level, int optname,
 359                                         char *optval, int *option);      
 360         int                     (*sendmsg)(struct sock *sk, struct msghdr *msg, int len,
 361                                         int noblock, int flags);
 362         int                     (*recvmsg)(struct sock *sk, struct msghdr *msg, int len,
 363                                         int noblock, int flags, int *addr_len);
 364         int                     (*bind)(struct sock *sk, struct sockaddr *uaddr, int addr_len);
 365         unsigned short          max_header;
 366         unsigned long           retransmits;
 367         char                    name[32];
 368         int                     inuse, highestinuse;
 369         struct sock *           sock_array[SOCK_ARRAY_SIZE];
 370 };
 371 
 372 #define TIME_WRITE      1
 373 #define TIME_CLOSE      2
 374 #define TIME_KEEPOPEN   3
 375 #define TIME_DESTROY    4
 376 #define TIME_DONE       5       /* Used to absorb those last few packets */
 377 #define TIME_PROBE0     6
 378 
 379 /*
 380  *      About 10 seconds 
 381  */
 382 
 383 #define SOCK_DESTROY_TIME (10*HZ)
 384 
 385 /*
 386  *      Sockets 0-1023 can't be bound too unless you are superuser 
 387  */
 388  
 389 #define PROT_SOCK       1024
 390 
 391 #define SHUTDOWN_MASK   3
 392 #define RCV_SHUTDOWN    1
 393 #define SEND_SHUTDOWN   2
 394 
 395 /*
 396  * Used by processes to "lock" a socket state, so that
 397  * interrupts and bottom half handlers won't change it
 398  * from under us. It essentially blocks any incoming
 399  * packets, so that we won't get any new data or any
 400  * packets that change the state of the socket.
 401  *
 402  * Note the 'barrier()' calls: gcc may not move a lock
 403  * "downwards" or a unlock "upwards" when optimizing.
 404  */
 405 extern void __release_sock(struct sock *sk);
 406 
 407 static inline void lock_sock(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 408 {
 409 #if 1
 410 /* debugging code: the test isn't even 100% correct, but it can catch bugs */
 411 /* Note that a double lock is ok in theory - it's just _usually_ a bug */
 412         if (sk->users) {
 413                 __label__ here;
 414                 printk("double lock on socket at %p\n", &&here);
 415 here:
 416         }
 417 #endif
 418         sk->users++;
 419         barrier();
 420 }
 421 
 422 static inline void release_sock(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 423 {
 424         barrier();
 425 #if 1
 426 /* debugging code: remove me when ok */
 427         if (sk->users == 0) {
 428                 __label__ here;
 429                 sk->users = 1;
 430                 printk("trying to unlock unlocked socket at %p\n", &&here);
 431 here:
 432         }
 433 #endif
 434         if (!--sk->users)
 435                 __release_sock(sk);
 436 }
 437 
 438 
 439 extern void                     destroy_sock(struct sock *sk);
 440 extern unsigned short           get_new_socknum(struct proto *,
 441                                                 unsigned short);
 442 extern void                     put_sock(unsigned short, struct sock *); 
 443 extern struct sock              *get_sock(struct proto *, unsigned short,
 444                                           unsigned long, unsigned short,
 445                                           unsigned long);
 446 extern struct sock              *get_sock_mcast(struct sock *, unsigned short,
 447                                           unsigned long, unsigned short,
 448                                           unsigned long);
 449 extern struct sock              *get_sock_raw(struct sock *, unsigned short,
 450                                           unsigned long, unsigned long);
 451 
 452 extern struct sk_buff           *sock_wmalloc(struct sock *sk,
 453                                               unsigned long size, int force,
 454                                               int priority);
 455 extern struct sk_buff           *sock_rmalloc(struct sock *sk,
 456                                               unsigned long size, int force,
 457                                               int priority);
 458 extern void                     sock_wfree(struct sock *sk,
 459                                            struct sk_buff *skb);
 460 extern void                     sock_rfree(struct sock *sk,
 461                                            struct sk_buff *skb);
 462 extern unsigned long            sock_rspace(struct sock *sk);
 463 extern unsigned long            sock_wspace(struct sock *sk);
 464 
 465 extern int                      sock_setsockopt(struct sock *sk, int level,
 466                                                 int op, char *optval,
 467                                                 int optlen);
 468 
 469 extern int                      sock_getsockopt(struct sock *sk, int level,
 470                                                 int op, char *optval, 
 471                                                 int *optlen);
 472 extern struct sk_buff           *sock_alloc_send_skb(struct sock *skb,
 473                                                      unsigned long size,
 474                                                      unsigned long fallback,
 475                                                      int noblock,
 476                                                      int *errcode);
 477 
 478 /*
 479  *      Queue a received datagram if it will fit. Stream and sequenced
 480  *      protocols can't normally use this as they need to fit buffers in
 481  *      and play with them.
 482  *
 483  *      Inlined as its very short and called for pretty much every
 484  *      packet ever received.
 485  */
 486 
 487 extern __inline__ int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
 488 {
 489         if (sk->rmem_alloc + skb->truesize >= sk->rcvbuf)
 490                 return -ENOMEM;
 491         atomic_add(skb->truesize, &sk->rmem_alloc);
 492         skb->sk=sk;
 493         skb_queue_tail(&sk->receive_queue,skb);
 494         if(!sk->dead)
 495                 sk->data_ready(sk,skb->len);
 496         return 0;
 497 }
 498 
 499 /*
 500  *      Recover an error report and clear atomically
 501  */
 502  
 503 extern __inline__ int sock_error(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 504 {
 505         int err=xchg(&sk->err,0);
 506         return -err;
 507 }
 508 
 509 /* 
 510  *      Declarations from timer.c 
 511  */
 512  
 513 extern struct sock *timer_base;
 514 
 515 extern void delete_timer (struct sock *);
 516 extern void reset_timer (struct sock *, int, unsigned long);
 517 extern void net_timer (unsigned long);
 518 
 519 
 520 /* 
 521  *      Enable debug/info messages 
 522  */
 523 
 524 #define NETDEBUG(x)             x
 525 
 526 #endif  /* _SOCK_H */

/* [previous][next][first][last][top][bottom][index][help] */