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 *
25 * This program is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU General Public License
27 * as published by the Free Software Foundation; either version
28 * 2 of the License, or (at your option) any later version.
29 */
30 #ifndef _SOCK_H
31 #define _SOCK_H
32
33 #include <linux/timer.h>
34 #include <linux/ip.h> /* struct options */
35 #include <linux/in.h> /* struct sockaddr_in */
36 #include <linux/tcp.h> /* struct tcphdr */
37 #include <linux/config.h>
38
39 #include <linux/skbuff.h> /* struct sk_buff */
40 #include <net/protocol.h> /* struct inet_protocol */
41 #ifdef CONFIG_AX25
42 #include <net/ax25.h>
43 #ifdef CONFIG_NETROM
44 #include <net/netrom.h>
45 #endif
46 #endif
47 #ifdef CONFIG_IPX
48 #include <net/ipx.h>
49 #endif
50 #ifdef CONFIG_ATALK
51 #include <linux/atalk.h>
52 #endif
53
54 #include <linux/igmp.h>
55
56 /* Think big (also on some systems a byte is faster) */
57 #define SOCK_ARRAY_SIZE 256
58
59
60 /*
61 * The AF_UNIX specific socket options
62 */
63
64 struct unix_opt
65 {
66 int family;
67 char * name;
68 int locks;
69 struct inode * inode;
70 struct semaphore readsem;
71 struct sock * other;
72 };
73
74
75 /*
76 * This structure really needs to be cleaned up.
77 * Most of it is for TCP, and not used by any of
78 * the other protocols.
79 */
80 struct sock
81 {
82 struct options *opt;
83 volatile unsigned long wmem_alloc;
84 volatile unsigned long rmem_alloc;
85 unsigned long allocation; /* Allocation mode */
86 __u32 write_seq;
87 __u32 sent_seq;
88 __u32 acked_seq;
89 __u32 copied_seq;
90 __u32 rcv_ack_seq;
91 __u32 window_seq;
92 __u32 fin_seq;
93 __u32 urg_seq;
94 __u32 urg_data;
95
96 /*
97 * Not all are volatile, but some are, so we
98 * might as well say they all are.
99 */
100 volatile char inuse,
101 dead,
102 urginline,
103 intr,
104 blog,
105 done,
106 reuse,
107 keepopen,
108 linger,
109 delay_acks,
110 destroy,
111 ack_timed,
112 no_check,
113 zapped, /* In ax25 & ipx means not linked */
114 broadcast,
115 nonagle;
116 unsigned long lingertime;
117 int proc;
118 struct sock *next;
119 struct sock *prev; /* Doubly linked chain.. */
120 struct sock *pair;
121 struct sk_buff * volatile send_head;
122 struct sk_buff * volatile send_tail;
123 struct sk_buff_head back_log;
124 struct sk_buff *partial;
125 struct timer_list partial_timer;
126 long retransmits;
127 struct sk_buff_head write_queue,
128 receive_queue;
129 struct proto *prot;
130 struct wait_queue **sleep;
131 __u32 daddr;
132 __u32 saddr; /* Sending source */
133 __u32 rcv_saddr; /* Bound address */
134 unsigned short max_unacked;
135 unsigned short window;
136 unsigned short bytes_rcv;
137 /*
138 * mss is min(mtu, max_window)
139 */
140 unsigned short mtu; /* mss negotiated in the syn's */
141 volatile unsigned short mss; /* current eff. mss - can change */
142 volatile unsigned short user_mss; /* mss requested by user in ioctl */
143 volatile unsigned short max_window;
144 unsigned long window_clamp;
145 unsigned short num;
146 volatile unsigned short cong_window;
147 volatile unsigned short cong_count;
148 volatile unsigned short ssthresh;
149 volatile unsigned short packets_out;
150 volatile unsigned short shutdown;
151 volatile unsigned long rtt;
152 volatile unsigned long mdev;
153 volatile unsigned long rto;
154
155 /*
156 * currently backoff isn't used, but I'm maintaining it in case
157 * we want to go back to a backoff formula that needs it
158 */
159
160 volatile unsigned short backoff;
161 volatile short err;
162 unsigned char protocol;
163 volatile unsigned char state;
164 volatile unsigned char ack_backlog;
165 unsigned char max_ack_backlog;
166 unsigned char priority;
167 unsigned char debug;
168 unsigned short rcvbuf;
169 unsigned short sndbuf;
170 unsigned short type;
171 unsigned char localroute; /* Route locally only */
172 #ifdef CONFIG_IPX
173 /*
174 * Once the IPX ncpd patches are in these are going into protinfo
175 */
176 ipx_address ipx_dest_addr;
177 ipx_interface *ipx_intrfc;
178 unsigned short ipx_port;
179 unsigned short ipx_type;
180 #endif
181 #ifdef CONFIG_AX25
182 ax25_cb *ax25;
183 #ifdef CONFIG_NETROM
184 nr_cb *nr;
185 #endif
186 #endif
187
188 /*
189 * This is where all the private (optional) areas that don't
190 * overlap will eventually live. For now just AF_UNIX is here.
191 */
192
193 union
194 {
195 struct unix_opt af_unix;
196 #ifdef CONFIG_ATALK
197 struct atalk_sock af_at;
198 #endif
199 } protinfo;
200
201 /*
202 * IP 'private area' or will be eventually
203 */
204 int ip_ttl; /* TTL setting */
205 int ip_tos; /* TOS */
206 struct tcphdr dummy_th;
207 struct timer_list keepalive_timer; /* TCP keepalive hack */
208 struct timer_list retransmit_timer; /* TCP retransmit timer */
209 struct timer_list ack_timer; /* TCP delayed ack timer */
210 int ip_xmit_timeout; /* Why the timeout is running */
211 struct rtable *ip_route_cache; /* Cached output route */
212 unsigned long ip_route_stamp; /* Route cache stamp */
213 unsigned long ip_route_daddr; /* Target address */
214 unsigned long ip_route_saddr; /* Source address */
215 int ip_route_local; /* State of locality flag */
216 unsigned long ip_hcache_stamp; /* Header cache stamp */
217 unsigned long *ip_hcache_ver; /* Pointer to version of cache */
218 char ip_hcache_data[16]; /* Cached header */
219 int ip_hcache_state; /* Have we a cached header */
220 unsigned char ip_option_len; /* Length of IP options */
221 unsigned char ip_option_flen; /* Second fragment option length */
222 unsigned char ip_opt_next_strict; /* Next hop is strict route */
223 unsigned long ip_opt_next_hop; /* Next hop if forced */
224 unsigned char *ip_opt_ptr[2]; /* IP option pointers */
225 unsigned char ip_hdrincl; /* Include headers ? */
226 #ifdef CONFIG_IP_MULTICAST
227 int ip_mc_ttl; /* Multicasting TTL */
228 int ip_mc_loop; /* Loopback */
229 char ip_mc_name[MAX_ADDR_LEN];/* Multicast device name */
230 struct ip_mc_socklist *ip_mc_list; /* Group array */
231 #endif
232
233 /*
234 * This part is used for the timeout functions (timer.c).
235 */
236
237 int timeout; /* What are we waiting for? */
238 struct timer_list timer; /* This is the TIME_WAIT/receive timer
239 * when we are doing IP
240 */
241 struct timeval stamp;
242
243 /*
244 * Identd
245 */
246
247 struct socket *socket;
248
249 /*
250 * Callbacks
251 */
252
253 void (*state_change)(struct sock *sk);
254 void (*data_ready)(struct sock *sk,int bytes);
255 void (*write_space)(struct sock *sk);
256 void (*error_report)(struct sock *sk);
257
258 };
259
260 /*
261 * IP protocol blocks we attach to sockets.
262 */
263
264 struct proto
265 {
266 void (*close)(struct sock *sk, int timeout);
267 int (*read)(struct sock *sk, unsigned char *to,
268 int len, int nonblock, unsigned flags);
269 int (*write)(struct sock *sk, const unsigned char *to,
270 int len, int nonblock, unsigned flags);
271 int (*sendto)(struct sock *sk,
272 const unsigned char *from, int len,
273 int noblock, unsigned flags,
274 struct sockaddr_in *usin, int addr_len);
275 int (*recvfrom)(struct sock *sk,
276 unsigned char *from, int len, int noblock,
277 unsigned flags, struct sockaddr_in *usin,
278 int *addr_len);
279 int (*build_header)(struct sk_buff *skb,
280 __u32 saddr,
281 __u32 daddr,
282 struct device **dev, int type,
283 struct options *opt, int len,
284 int tos, int ttl);
285 int (*connect)(struct sock *sk,
286 struct sockaddr_in *usin, int addr_len);
287 struct sock * (*accept) (struct sock *sk, int flags);
288 void (*queue_xmit)(struct sock *sk,
289 struct device *dev, struct sk_buff *skb,
290 int free);
291 void (*retransmit)(struct sock *sk, int all);
292 void (*write_wakeup)(struct sock *sk);
293 void (*read_wakeup)(struct sock *sk);
294 int (*rcv)(struct sk_buff *buff, struct device *dev,
295 struct options *opt, __u32 daddr,
296 unsigned short len, __u32 saddr,
297 int redo, struct inet_protocol *protocol);
298 int (*select)(struct sock *sk, int which,
299 select_table *wait);
300 int (*ioctl)(struct sock *sk, int cmd,
301 unsigned long arg);
302 int (*init)(struct sock *sk);
303 void (*shutdown)(struct sock *sk, int how);
304 int (*setsockopt)(struct sock *sk, int level, int optname,
305 char *optval, int optlen);
306 int (*getsockopt)(struct sock *sk, int level, int optname,
307 char *optval, int *option);
308 int (*sendmsg)(struct sock *sk, struct msghdr *msg, int len,
309 int noblock, int flags);
310 int (*recvmsg)(struct sock *sk, struct msghdr *msg, int len,
311 int noblock, int flags, int *addr_len);
312 unsigned short max_header;
313 unsigned long retransmits;
314 char name[32];
315 int inuse, highestinuse;
316 struct sock * sock_array[SOCK_ARRAY_SIZE];
317 };
318
319 #define TIME_WRITE 1
320 #define TIME_CLOSE 2
321 #define TIME_KEEPOPEN 3
322 #define TIME_DESTROY 4
323 #define TIME_DONE 5 /* Used to absorb those last few packets */
324 #define TIME_PROBE0 6
325 /*
326 * About 10 seconds
327 */
328 #define SOCK_DESTROY_TIME (10*HZ)
329
330
331 /*
332 * Sockets 0-1023 can't be bound too unless you are superuser
333 */
334
335 #define PROT_SOCK 1024
336
337
338 #define SHUTDOWN_MASK 3
339 #define RCV_SHUTDOWN 1
340 #define SEND_SHUTDOWN 2
341
342
343 extern void destroy_sock(struct sock *sk);
344 extern unsigned short get_new_socknum(struct proto *,
345 unsigned short);
346 extern void put_sock(unsigned short, struct sock *);
347 extern void release_sock(struct sock *sk);
348 extern struct sock *get_sock(struct proto *, unsigned short,
349 unsigned long, unsigned short,
350 unsigned long);
351 extern struct sock *get_sock_mcast(struct sock *, unsigned short,
352 unsigned long, unsigned short,
353 unsigned long);
354 extern struct sock *get_sock_raw(struct sock *, unsigned short,
355 unsigned long, unsigned long);
356
357 extern struct sk_buff *sock_wmalloc(struct sock *sk,
358 unsigned long size, int force,
359 int priority);
360 extern struct sk_buff *sock_rmalloc(struct sock *sk,
361 unsigned long size, int force,
362 int priority);
363 extern void sock_wfree(struct sock *sk,
364 struct sk_buff *skb);
365 extern void sock_rfree(struct sock *sk,
366 struct sk_buff *skb);
367 extern unsigned long sock_rspace(struct sock *sk);
368 extern unsigned long sock_wspace(struct sock *sk);
369
370 extern int sock_setsockopt(struct sock *sk, int level,
371 int op, char *optval,
372 int optlen);
373
374 extern int sock_getsockopt(struct sock *sk, int level,
375 int op, char *optval,
376 int *optlen);
377 extern struct sk_buff *sock_alloc_send_skb(struct sock *skb,
378 unsigned long size,
379 unsigned long fallback,
380 int noblock,
381 int *errcode);
382
383 /*
384 * Queue a received datagram if it will fit. Stream and sequenced
385 * protocols can't normally use this as they need to fit buffers in
386 * and play with them.
387 *
388 * Inlined as its very short and called for pretty much every
389 * packet ever received.
390 */
391
392 extern __inline__ int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
393 {
394 unsigned long flags;
395 if(sk->rmem_alloc + skb->truesize >= sk->rcvbuf)
396 return -ENOMEM;
397 save_flags(flags);
398 cli();
399 sk->rmem_alloc+=skb->truesize;
400 skb->sk=sk;
401 restore_flags(flags);
402 skb_queue_tail(&sk->receive_queue,skb);
403 if(!sk->dead)
404 sk->data_ready(sk,skb->len);
405 return 0;
406 }
407
408 /*
409 * Declarations from timer.c
410 */
411
412 extern struct sock *timer_base;
413
414 extern void delete_timer (struct sock *);
415 extern void reset_timer (struct sock *, int, unsigned long);
416 extern void net_timer (unsigned long);
417
418
419 /*
420 * Enable debug/info messages
421 */
422
423 #define NETDEBUG(x) x
424
425 #endif /* _SOCK_H */