Cleanup eeprom reading code.
[linux-2.6/linux-mips.git] / include / net / sock.h
blob3d4ba5ff394326d2db1b05932a37a437211b4794
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.
6 * Definitions for the AF_INET socket handler.
8 * Version: @(#)sock.h 1.0.4 05/13/93
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>
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 * David S. Miller : New socket lookup architecture.
26 * Steve Whitehouse: Default routines for sock_ops
27 * Arnaldo C. Melo : removed net_pinfo, tp_pinfo and made
28 * protinfo be just a void pointer, as the
29 * protocol specific parts were moved to
30 * respective headers and ipv4/v6, etc now
31 * use private slabcaches for its socks
32 * Pedro Hortas : New flags field for socket options
35 * This program is free software; you can redistribute it and/or
36 * modify it under the terms of the GNU General Public License
37 * as published by the Free Software Foundation; either version
38 * 2 of the License, or (at your option) any later version.
40 #ifndef _SOCK_H
41 #define _SOCK_H
43 #include <linux/config.h>
44 #include <linux/timer.h>
45 #include <linux/cache.h>
46 #include <linux/module.h>
47 #include <linux/netdevice.h>
48 #include <linux/skbuff.h> /* struct sk_buff */
49 #include <linux/security.h>
51 #include <linux/filter.h>
53 #include <asm/atomic.h>
54 #include <net/dst.h>
57 * This structure really needs to be cleaned up.
58 * Most of it is for TCP, and not used by any of
59 * the other protocols.
62 /* Sock flags */
63 enum {
64 SOCK_DEAD,
65 SOCK_DONE,
66 SOCK_URGINLINE,
67 SOCK_KEEPOPEN,
68 SOCK_LINGER,
69 SOCK_DESTROY,
70 SOCK_BROADCAST,
73 /* Define this to get the sk->debug debugging facility. */
74 #define SOCK_DEBUGGING
75 #ifdef SOCK_DEBUGGING
76 #define SOCK_DEBUG(sk, msg...) do { if((sk) && ((sk)->debug)) printk(KERN_DEBUG msg); } while (0)
77 #else
78 #define SOCK_DEBUG(sk, msg...) do { } while (0)
79 #endif
81 /* This is the per-socket lock. The spinlock provides a synchronization
82 * between user contexts and software interrupt processing, whereas the
83 * mini-semaphore synchronizes multiple users amongst themselves.
85 struct sock_iocb;
86 typedef struct {
87 spinlock_t slock;
88 struct sock_iocb *owner;
89 wait_queue_head_t wq;
90 } socket_lock_t;
92 #define sock_lock_init(__sk) \
93 do { spin_lock_init(&((__sk)->lock.slock)); \
94 (__sk)->lock.owner = NULL; \
95 init_waitqueue_head(&((__sk)->lock.wq)); \
96 } while(0)
98 /**
99 * struct sock - network layer representation of sockets
100 * @state - Connection state
101 * @zapped - ax25 & ipx means !linked
102 * @reuse - %SO_REUSEADDR setting
103 * @shutdown - mask of %SEND_SHUTDOWN and/or %RCV_SHUTDOWN
104 * @bound_dev_if - bound device index if != 0
105 * @next - main hash linkage for various protocol lookup tables
106 * @pprev - main hash linkage for various protocol lookup tables
107 * @bind_next - main hash linkage for various protocol lookup tables
108 * @bind_pprev - main hash linkage for various protocol lookup tables
109 * @refcnt - reference count
110 * @family - network address family
111 * @use_write_queue - wheter to call sk->write_space(sk) in sock_wfree
112 * @userlocks - %SO_SNDBUF and %SO_RCVBUF settings
113 * @lock - synchronizer
114 * @rcvbuf - size of receive buffer in bytes
115 * @sleep - sock wait queue
116 * @dst_cache - destination cache
117 * @dst_lock - destination cache lock
118 * @policy - flow policy
119 * @rmem_alloc - receive queue bytes committed
120 * @receive_queue - incoming packets
121 * @wmem_alloc - transmit queue bytes committed
122 * @write_queue - Packet sending queue
123 * @omem_alloc - "o" is "option" or "other"
124 * @wmem_queued - persistent queue size
125 * @forward_alloc - space allocated forward
126 * @allocation - allocation mode
127 * @sndbuf - size of send buffer in bytes
128 * @prev - pointer to previous sock in the list this sock is in
129 * @flags - %SO_LINGER (l_onoff), %SO_BROADCAST, %SO_KEEPALIVE, %SO_OOBINLINE settings
130 * @no_check - %SO_NO_CHECK setting, wether or not checkup packets
131 * @debug - %SO_DEBUG setting
132 * @rcvtstamp - %SO_TIMESTAMP setting
133 * @no_largesend - whether to sent large segments or not
134 * @route_caps - route capabilities (e.g. %NETIF_F_TSO)
135 * @lingertime - %SO_LINGER l_linger setting
136 * @hashent - hash entry in several tables (e.g. tcp_ehash)
137 * @pair - socket pair (e.g. AF_UNIX/unix_peer)
138 * @backlog - always used with the per-socket spinlock held
139 * @callback_lock - used with the callbacks in the end of this struct
140 * @error_queue - rarely used
141 * @prot - protocol handlers inside a network family
142 * @err - last error
143 * @err_soft - errors that don't cause failure but are the cause of a persistent failure not just 'timed out'
144 * @ack_backlog - current listen backlog
145 * @max_ack_backlog - listen backlog set in listen()
146 * @priority - %SO_PRIORITY setting
147 * @type - socket type (%SOCK_STREAM, etc)
148 * @localroute - route locally only, %SO_DONTROUTE setting
149 * @protocol - which protocol this socket belongs in this network family
150 * @peercred - %SO_PEERCRED setting
151 * @rcvlowat - %SO_RCVLOWAT setting
152 * @rcvtimeo - %SO_RCVTIMEO setting
153 * @sndtimeo - %SO_SNDTIMEO setting
154 * @filter - socket filtering instructions
155 * @protinfo - private area, net family specific, when not using slab
156 * @slab - the slabcache this instance was allocated from
157 * @timer - sock cleanup timer
158 * @stamp - time stamp of last packet received
159 * @socket - Identd and reporting IO signals
160 * @user_data - RPC layer private data
161 * @owner - module that owns this socket
162 * @state_change - callback to indicate change in the state of the sock
163 * @data_ready - callback to indicate there is data to be processed
164 * @write_space - callback to indicate there is bf sending space available
165 * @error_report - callback to indicate errors (e.g. %MSG_ERRQUEUE)
166 * @backlog_rcv - callback to process the backlog
167 * @destruct - called at sock freeing time, i.e. when all refcnt == 0
169 struct sock {
170 /* Begin of struct sock/struct tcp_tw_bucket shared layout */
171 volatile unsigned char state,
172 zapped;
173 unsigned char reuse;
174 unsigned char shutdown;
175 int bound_dev_if;
176 struct sock *next;
177 struct sock **pprev;
178 struct sock *bind_next;
179 struct sock **bind_pprev;
180 atomic_t refcnt;
181 unsigned short family;
182 /* End of struct sock/struct tcp_tw_bucket shared layout */
183 unsigned char use_write_queue;
184 unsigned char userlocks;
185 socket_lock_t lock;
186 int rcvbuf;
187 wait_queue_head_t *sleep;
188 struct dst_entry *dst_cache;
189 rwlock_t dst_lock;
190 struct xfrm_policy *policy[2];
191 atomic_t rmem_alloc;
192 struct sk_buff_head receive_queue;
193 atomic_t wmem_alloc;
194 struct sk_buff_head write_queue;
195 atomic_t omem_alloc;
196 int wmem_queued;
197 int forward_alloc;
198 unsigned int allocation;
199 int sndbuf;
200 struct sock *prev;
201 unsigned long flags;
202 char no_check;
203 unsigned char debug;
204 unsigned char rcvtstamp;
205 unsigned char no_largesend;
206 int route_caps;
207 unsigned long lingertime;
208 int hashent;
209 struct sock *pair;
211 * The backlog queue is special, it is always used with
212 * the per-socket spinlock held and requires low latency
213 * access. Therefore we special case it's implementation.
215 struct {
216 struct sk_buff *head;
217 struct sk_buff *tail;
218 } backlog;
219 rwlock_t callback_lock;
220 struct sk_buff_head error_queue;
221 struct proto *prot;
222 int err,
223 err_soft;
224 unsigned short ack_backlog;
225 unsigned short max_ack_backlog;
226 __u32 priority;
227 unsigned short type;
228 unsigned char localroute;
229 unsigned char protocol;
230 struct ucred peercred;
231 int rcvlowat;
232 long rcvtimeo;
233 long sndtimeo;
234 struct sk_filter *filter;
235 void *protinfo;
236 kmem_cache_t *slab;
237 struct timer_list timer;
238 struct timeval stamp;
239 struct socket *socket;
240 void *user_data;
241 struct module *owner;
242 void (*state_change)(struct sock *sk);
243 void (*data_ready)(struct sock *sk, int bytes);
244 void (*write_space)(struct sock *sk);
245 void (*error_report)(struct sock *sk);
246 int (*backlog_rcv) (struct sock *sk,
247 struct sk_buff *skb);
248 void (*destruct)(struct sock *sk);
251 /* The per-socket spinlock must be held here. */
252 #define sk_add_backlog(__sk, __skb) \
253 do { if((__sk)->backlog.tail == NULL) { \
254 (__sk)->backlog.head = \
255 (__sk)->backlog.tail = (__skb); \
256 } else { \
257 ((__sk)->backlog.tail)->next = (__skb); \
258 (__sk)->backlog.tail = (__skb); \
260 (__skb)->next = NULL; \
261 } while(0)
263 /* IP protocol blocks we attach to sockets.
264 * socket layer -> transport layer interface
265 * transport -> network interface is defined by struct inet_proto
267 struct proto {
268 void (*close)(struct sock *sk,
269 long timeout);
270 int (*connect)(struct sock *sk,
271 struct sockaddr *uaddr,
272 int addr_len);
273 int (*disconnect)(struct sock *sk, int flags);
275 struct sock * (*accept) (struct sock *sk, int flags, int *err);
277 int (*ioctl)(struct sock *sk, int cmd,
278 unsigned long arg);
279 int (*init)(struct sock *sk);
280 int (*destroy)(struct sock *sk);
281 void (*shutdown)(struct sock *sk, int how);
282 int (*setsockopt)(struct sock *sk, int level,
283 int optname, char *optval, int optlen);
284 int (*getsockopt)(struct sock *sk, int level,
285 int optname, char *optval,
286 int *option);
287 int (*sendmsg)(struct kiocb *iocb, struct sock *sk,
288 struct msghdr *msg, int len);
289 int (*recvmsg)(struct kiocb *iocb, struct sock *sk,
290 struct msghdr *msg,
291 int len, int noblock, int flags,
292 int *addr_len);
293 int (*sendpage)(struct sock *sk, struct page *page,
294 int offset, size_t size, int flags);
295 int (*bind)(struct sock *sk,
296 struct sockaddr *uaddr, int addr_len);
298 int (*backlog_rcv) (struct sock *sk,
299 struct sk_buff *skb);
301 /* Keeping track of sk's, looking them up, and port selection methods. */
302 void (*hash)(struct sock *sk);
303 void (*unhash)(struct sock *sk);
304 int (*get_port)(struct sock *sk, unsigned short snum);
306 char name[32];
308 struct {
309 int inuse;
310 u8 __pad[SMP_CACHE_BYTES - sizeof(int)];
311 } stats[NR_CPUS];
314 static __inline__ void sk_set_owner(struct sock *sk, struct module *owner)
317 * One should use sk_set_owner just once, after struct sock creation,
318 * be it shortly after sk_alloc or after a function that returns a new
319 * struct sock (and that down the call chain called sk_alloc), e.g. the
320 * IPv4 and IPv6 modules share tcp_create_openreq_child, so if
321 * tcp_create_openreq_child called sk_set_owner IPv6 would have to
322 * change the ownership of this struct sock, with one not needed
323 * transient sk_set_owner call.
325 if (unlikely(sk->owner != NULL))
326 BUG();
327 sk->owner = owner;
328 __module_get(owner);
331 /* Called with local bh disabled */
332 static __inline__ void sock_prot_inc_use(struct proto *prot)
334 prot->stats[smp_processor_id()].inuse++;
337 static __inline__ void sock_prot_dec_use(struct proto *prot)
339 prot->stats[smp_processor_id()].inuse--;
342 /* About 10 seconds */
343 #define SOCK_DESTROY_TIME (10*HZ)
345 /* Sockets 0-1023 can't be bound to unless you are superuser */
346 #define PROT_SOCK 1024
348 #define SHUTDOWN_MASK 3
349 #define RCV_SHUTDOWN 1
350 #define SEND_SHUTDOWN 2
352 #define SOCK_SNDBUF_LOCK 1
353 #define SOCK_RCVBUF_LOCK 2
354 #define SOCK_BINDADDR_LOCK 4
355 #define SOCK_BINDPORT_LOCK 8
357 /* sock_iocb: used to kick off async processing of socket ios */
358 struct sock_iocb {
359 struct list_head list;
361 int flags;
362 int size;
363 struct socket *sock;
364 struct sock *sk;
365 struct scm_cookie *scm;
366 struct msghdr *msg, async_msg;
367 struct iovec async_iov;
370 static inline struct sock_iocb *kiocb_to_siocb(struct kiocb *iocb)
372 BUG_ON(sizeof(struct sock_iocb) > KIOCB_PRIVATE_SIZE);
373 return (struct sock_iocb *)iocb->private;
376 static inline struct kiocb *siocb_to_kiocb(struct sock_iocb *si)
378 return container_of((void *)si, struct kiocb, private);
381 struct socket_alloc {
382 struct socket socket;
383 struct inode vfs_inode;
386 static inline struct socket *SOCKET_I(struct inode *inode)
388 return &container_of(inode, struct socket_alloc, vfs_inode)->socket;
391 static inline struct inode *SOCK_INODE(struct socket *socket)
393 return &container_of(socket, struct socket_alloc, socket)->vfs_inode;
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.
402 * While locked, BH processing will add new packets to
403 * the backlog queue. This queue is processed by the
404 * owner of the socket lock right before it is released.
406 * Since ~2.3.5 it is also exclusive sleep lock serializing
407 * accesses from user process context.
409 extern void __lock_sock(struct sock *sk);
410 extern void __release_sock(struct sock *sk);
411 #define sock_owned_by_user(sk) (NULL != (sk)->lock.owner)
412 #define lock_sock(__sk) \
413 do { might_sleep(); \
414 spin_lock_bh(&((__sk)->lock.slock)); \
415 if ((__sk)->lock.owner != NULL) \
416 __lock_sock(__sk); \
417 (__sk)->lock.owner = (void *)1; \
418 spin_unlock_bh(&((__sk)->lock.slock)); \
419 } while(0)
421 #define release_sock(__sk) \
422 do { spin_lock_bh(&((__sk)->lock.slock)); \
423 if ((__sk)->backlog.tail != NULL) \
424 __release_sock(__sk); \
425 (__sk)->lock.owner = NULL; \
426 if (waitqueue_active(&((__sk)->lock.wq))) wake_up(&((__sk)->lock.wq)); \
427 spin_unlock_bh(&((__sk)->lock.slock)); \
428 } while(0)
430 /* BH context may only use the following locking interface. */
431 #define bh_lock_sock(__sk) spin_lock(&((__sk)->lock.slock))
432 #define bh_unlock_sock(__sk) spin_unlock(&((__sk)->lock.slock))
434 extern struct sock * sk_alloc(int family, int priority, int zero_it,
435 kmem_cache_t *slab);
436 extern void sk_free(struct sock *sk);
438 extern struct sk_buff *sock_wmalloc(struct sock *sk,
439 unsigned long size, int force,
440 int priority);
441 extern struct sk_buff *sock_rmalloc(struct sock *sk,
442 unsigned long size, int force,
443 int priority);
444 extern void sock_wfree(struct sk_buff *skb);
445 extern void sock_rfree(struct sk_buff *skb);
447 extern int sock_setsockopt(struct socket *sock, int level,
448 int op, char __user *optval,
449 int optlen);
451 extern int sock_getsockopt(struct socket *sock, int level,
452 int op, char __user *optval,
453 int __user *optlen);
454 extern struct sk_buff *sock_alloc_send_skb(struct sock *sk,
455 unsigned long size,
456 int noblock,
457 int *errcode);
458 extern struct sk_buff *sock_alloc_send_pskb(struct sock *sk,
459 unsigned long header_len,
460 unsigned long data_len,
461 int noblock,
462 int *errcode);
463 extern void *sock_kmalloc(struct sock *sk, int size, int priority);
464 extern void sock_kfree_s(struct sock *sk, void *mem, int size);
465 extern void sk_send_sigurg(struct sock *sk);
468 * Functions to fill in entries in struct proto_ops when a protocol
469 * does not implement a particular function.
471 extern int sock_no_release(struct socket *);
472 extern int sock_no_bind(struct socket *,
473 struct sockaddr *, int);
474 extern int sock_no_connect(struct socket *,
475 struct sockaddr *, int, int);
476 extern int sock_no_socketpair(struct socket *,
477 struct socket *);
478 extern int sock_no_accept(struct socket *,
479 struct socket *, int);
480 extern int sock_no_getname(struct socket *,
481 struct sockaddr *, int *, int);
482 extern unsigned int sock_no_poll(struct file *, struct socket *,
483 struct poll_table_struct *);
484 extern int sock_no_ioctl(struct socket *, unsigned int,
485 unsigned long);
486 extern int sock_no_listen(struct socket *, int);
487 extern int sock_no_shutdown(struct socket *, int);
488 extern int sock_no_getsockopt(struct socket *, int , int,
489 char *, int *);
490 extern int sock_no_setsockopt(struct socket *, int, int,
491 char *, int);
492 extern int sock_no_sendmsg(struct kiocb *, struct socket *,
493 struct msghdr *, int);
494 extern int sock_no_recvmsg(struct kiocb *, struct socket *,
495 struct msghdr *, int, int);
496 extern int sock_no_mmap(struct file *file,
497 struct socket *sock,
498 struct vm_area_struct *vma);
499 extern ssize_t sock_no_sendpage(struct socket *sock,
500 struct page *page,
501 int offset, size_t size,
502 int flags);
505 * Default socket callbacks and setup code
508 extern void sock_def_destruct(struct sock *);
510 /* Initialise core socket variables */
511 extern void sock_init_data(struct socket *sock, struct sock *sk);
514 * sk_filter - run a packet through a socket filter
515 * @sk: sock associated with &sk_buff
516 * @skb: buffer to filter
517 * @needlock: set to 1 if the sock is not locked by caller.
519 * Run the filter code and then cut skb->data to correct size returned by
520 * sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller
521 * than pkt_len we keep whole skb->data. This is the socket level
522 * wrapper to sk_run_filter. It returns 0 if the packet should
523 * be accepted or -EPERM if the packet should be tossed.
527 static inline int sk_filter(struct sock *sk, struct sk_buff *skb, int needlock)
529 int err;
531 err = security_sock_rcv_skb(sk, skb);
532 if (err)
533 return err;
535 if (sk->filter) {
536 struct sk_filter *filter;
538 if (needlock)
539 bh_lock_sock(sk);
541 filter = sk->filter;
542 if (filter) {
543 int pkt_len = sk_run_filter(skb, filter->insns,
544 filter->len);
545 if (!pkt_len)
546 err = -EPERM;
547 else
548 skb_trim(skb, pkt_len);
551 if (needlock)
552 bh_unlock_sock(sk);
554 return err;
558 * sk_filter_release: Release a socket filter
559 * @sk: socket
560 * @fp: filter to remove
562 * Remove a filter from a socket and release its resources.
565 static inline void sk_filter_release(struct sock *sk, struct sk_filter *fp)
567 unsigned int size = sk_filter_len(fp);
569 atomic_sub(size, &sk->omem_alloc);
571 if (atomic_dec_and_test(&fp->refcnt))
572 kfree(fp);
575 static inline void sk_filter_charge(struct sock *sk, struct sk_filter *fp)
577 atomic_inc(&fp->refcnt);
578 atomic_add(sk_filter_len(fp), &sk->omem_alloc);
582 * Socket reference counting postulates.
584 * * Each user of socket SHOULD hold a reference count.
585 * * Each access point to socket (an hash table bucket, reference from a list,
586 * running timer, skb in flight MUST hold a reference count.
587 * * When reference count hits 0, it means it will never increase back.
588 * * When reference count hits 0, it means that no references from
589 * outside exist to this socket and current process on current CPU
590 * is last user and may/should destroy this socket.
591 * * sk_free is called from any context: process, BH, IRQ. When
592 * it is called, socket has no references from outside -> sk_free
593 * may release descendant resources allocated by the socket, but
594 * to the time when it is called, socket is NOT referenced by any
595 * hash tables, lists etc.
596 * * Packets, delivered from outside (from network or from another process)
597 * and enqueued on receive/error queues SHOULD NOT grab reference count,
598 * when they sit in queue. Otherwise, packets will leak to hole, when
599 * socket is looked up by one cpu and unhasing is made by another CPU.
600 * It is true for udp/raw, netlink (leak to receive and error queues), tcp
601 * (leak to backlog). Packet socket does all the processing inside
602 * BR_NETPROTO_LOCK, so that it has not this race condition. UNIX sockets
603 * use separate SMP lock, so that they are prone too.
606 /* Grab socket reference count. This operation is valid only
607 when sk is ALREADY grabbed f.e. it is found in hash table
608 or a list and the lookup is made under lock preventing hash table
609 modifications.
612 static inline void sock_hold(struct sock *sk)
614 atomic_inc(&sk->refcnt);
617 /* Ungrab socket in the context, which assumes that socket refcnt
618 cannot hit zero, f.e. it is true in context of any socketcall.
620 static inline void __sock_put(struct sock *sk)
622 atomic_dec(&sk->refcnt);
625 /* Ungrab socket and destroy it, if it was the last reference. */
626 static inline void sock_put(struct sock *sk)
628 if (atomic_dec_and_test(&sk->refcnt))
629 sk_free(sk);
632 /* Detach socket from process context.
633 * Announce socket dead, detach it from wait queue and inode.
634 * Note that parent inode held reference count on this struct sock,
635 * we do not release it in this function, because protocol
636 * probably wants some additional cleanups or even continuing
637 * to work with this socket (TCP).
639 static inline void sock_orphan(struct sock *sk)
641 write_lock_bh(&sk->callback_lock);
642 __set_bit(SOCK_DEAD, &sk->flags);
643 sk->socket = NULL;
644 sk->sleep = NULL;
645 write_unlock_bh(&sk->callback_lock);
648 static inline void sock_graft(struct sock *sk, struct socket *parent)
650 write_lock_bh(&sk->callback_lock);
651 sk->sleep = &parent->wait;
652 parent->sk = sk;
653 sk->socket = parent;
654 write_unlock_bh(&sk->callback_lock);
657 static inline int sock_i_uid(struct sock *sk)
659 int uid;
661 read_lock(&sk->callback_lock);
662 uid = sk->socket ? SOCK_INODE(sk->socket)->i_uid : 0;
663 read_unlock(&sk->callback_lock);
664 return uid;
667 static inline unsigned long sock_i_ino(struct sock *sk)
669 unsigned long ino;
671 read_lock(&sk->callback_lock);
672 ino = sk->socket ? SOCK_INODE(sk->socket)->i_ino : 0;
673 read_unlock(&sk->callback_lock);
674 return ino;
677 static inline struct dst_entry *
678 __sk_dst_get(struct sock *sk)
680 return sk->dst_cache;
683 static inline struct dst_entry *
684 sk_dst_get(struct sock *sk)
686 struct dst_entry *dst;
688 read_lock(&sk->dst_lock);
689 dst = sk->dst_cache;
690 if (dst)
691 dst_hold(dst);
692 read_unlock(&sk->dst_lock);
693 return dst;
696 static inline void
697 __sk_dst_set(struct sock *sk, struct dst_entry *dst)
699 struct dst_entry *old_dst;
701 old_dst = sk->dst_cache;
702 sk->dst_cache = dst;
703 dst_release(old_dst);
706 static inline void
707 sk_dst_set(struct sock *sk, struct dst_entry *dst)
709 write_lock(&sk->dst_lock);
710 __sk_dst_set(sk, dst);
711 write_unlock(&sk->dst_lock);
714 static inline void
715 __sk_dst_reset(struct sock *sk)
717 struct dst_entry *old_dst;
719 old_dst = sk->dst_cache;
720 sk->dst_cache = NULL;
721 dst_release(old_dst);
724 static inline void
725 sk_dst_reset(struct sock *sk)
727 write_lock(&sk->dst_lock);
728 __sk_dst_reset(sk);
729 write_unlock(&sk->dst_lock);
732 static inline struct dst_entry *
733 __sk_dst_check(struct sock *sk, u32 cookie)
735 struct dst_entry *dst = sk->dst_cache;
737 if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
738 sk->dst_cache = NULL;
739 return NULL;
742 return dst;
745 static inline struct dst_entry *
746 sk_dst_check(struct sock *sk, u32 cookie)
748 struct dst_entry *dst = sk_dst_get(sk);
750 if (dst && dst->obsolete && dst->ops->check(dst, cookie) == NULL) {
751 sk_dst_reset(sk);
752 return NULL;
755 return dst;
760 * Queue a received datagram if it will fit. Stream and sequenced
761 * protocols can't normally use this as they need to fit buffers in
762 * and play with them.
764 * Inlined as it's very short and called for pretty much every
765 * packet ever received.
768 static inline void skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
770 sock_hold(sk);
771 skb->sk = sk;
772 skb->destructor = sock_wfree;
773 atomic_add(skb->truesize, &sk->wmem_alloc);
776 static inline void skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
778 skb->sk = sk;
779 skb->destructor = sock_rfree;
780 atomic_add(skb->truesize, &sk->rmem_alloc);
783 static inline int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
785 int err = 0;
787 /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
788 number of warnings when compiling with -W --ANK
790 if (atomic_read(&sk->rmem_alloc) + skb->truesize >= (unsigned)sk->rcvbuf) {
791 err = -ENOMEM;
792 goto out;
795 /* It would be deadlock, if sock_queue_rcv_skb is used
796 with socket lock! We assume that users of this
797 function are lock free.
799 err = sk_filter(sk, skb, 1);
800 if (err)
801 goto out;
803 skb->dev = NULL;
804 skb_set_owner_r(skb, sk);
805 skb_queue_tail(&sk->receive_queue, skb);
806 if (!test_bit(SOCK_DEAD, &sk->flags))
807 sk->data_ready(sk,skb->len);
808 out:
809 return err;
812 static inline int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
814 /* Cast skb->rcvbuf to unsigned... It's pointless, but reduces
815 number of warnings when compiling with -W --ANK
817 if (atomic_read(&sk->rmem_alloc) + skb->truesize >= (unsigned)sk->rcvbuf)
818 return -ENOMEM;
819 skb_set_owner_r(skb, sk);
820 skb_queue_tail(&sk->error_queue,skb);
821 if (!test_bit(SOCK_DEAD, &sk->flags))
822 sk->data_ready(sk,skb->len);
823 return 0;
827 * Recover an error report and clear atomically
830 static inline int sock_error(struct sock *sk)
832 int err=xchg(&sk->err,0);
833 return -err;
836 static inline unsigned long sock_wspace(struct sock *sk)
838 int amt = 0;
840 if (!(sk->shutdown & SEND_SHUTDOWN)) {
841 amt = sk->sndbuf - atomic_read(&sk->wmem_alloc);
842 if (amt < 0)
843 amt = 0;
845 return amt;
848 static inline void sk_wake_async(struct sock *sk, int how, int band)
850 if (sk->socket && sk->socket->fasync_list)
851 sock_wake_async(sk->socket, how, band);
854 #define SOCK_MIN_SNDBUF 2048
855 #define SOCK_MIN_RCVBUF 256
858 * Default write policy as shown to user space via poll/select/SIGIO
860 static inline int sock_writeable(struct sock *sk)
862 return atomic_read(&sk->wmem_alloc) < (sk->sndbuf / 2);
865 static inline int gfp_any(void)
867 return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
870 static inline long sock_rcvtimeo(struct sock *sk, int noblock)
872 return noblock ? 0 : sk->rcvtimeo;
875 static inline long sock_sndtimeo(struct sock *sk, int noblock)
877 return noblock ? 0 : sk->sndtimeo;
880 static inline int sock_rcvlowat(struct sock *sk, int waitall, int len)
882 return (waitall ? len : min_t(int, sk->rcvlowat, len)) ? : 1;
885 /* Alas, with timeout socket operations are not restartable.
886 * Compare this to poll().
888 static inline int sock_intr_errno(long timeo)
890 return timeo == MAX_SCHEDULE_TIMEOUT ? -ERESTARTSYS : -EINTR;
893 static __inline__ void
894 sock_recv_timestamp(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
896 if (sk->rcvtstamp)
897 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP, sizeof(skb->stamp), &skb->stamp);
898 else
899 sk->stamp = skb->stamp;
903 * Enable debug/info messages
906 #if 0
907 #define NETDEBUG(x) do { } while (0)
908 #else
909 #define NETDEBUG(x) do { x; } while (0)
910 #endif
913 * Macros for sleeping on a socket. Use them like this:
915 * SOCK_SLEEP_PRE(sk)
916 * if (condition)
917 * schedule();
918 * SOCK_SLEEP_POST(sk)
920 * N.B. These are now obsolete and were, afaik, only ever used in DECnet
921 * and when the last use of them in DECnet has gone, I'm intending to
922 * remove them.
925 #define SOCK_SLEEP_PRE(sk) { struct task_struct *tsk = current; \
926 DECLARE_WAITQUEUE(wait, tsk); \
927 tsk->state = TASK_INTERRUPTIBLE; \
928 add_wait_queue((sk)->sleep, &wait); \
929 release_sock(sk);
931 #define SOCK_SLEEP_POST(sk) tsk->state = TASK_RUNNING; \
932 remove_wait_queue((sk)->sleep, &wait); \
933 lock_sock(sk); \
936 static inline void sock_valbool_flag(struct sock *sk, int bit, int valbool)
938 if (valbool)
939 __set_bit(bit, &sk->flags);
940 else
941 __clear_bit(bit, &sk->flags);
944 extern __u32 sysctl_wmem_max;
945 extern __u32 sysctl_rmem_max;
947 int siocdevprivate_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg);
949 #endif /* _SOCK_H */