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

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