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