blob: 0ce7d0fe5ee7e5ab041d94019c683418570f2b3d [file] [log] [blame]
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001/*
2 * net/dccp/ipv4.c
3 *
4 * An implementation of the DCCP protocol
5 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13#include <linux/config.h>
14#include <linux/dccp.h>
15#include <linux/icmp.h>
16#include <linux/module.h>
17#include <linux/skbuff.h>
18#include <linux/random.h>
19
20#include <net/icmp.h>
21#include <net/inet_hashtables.h>
22#include <net/sock.h>
23#include <net/tcp_states.h>
24#include <net/xfrm.h>
25
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -070026#include "ackvec.h"
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070027#include "ccid.h"
28#include "dccp.h"
29
30struct inet_hashinfo __cacheline_aligned dccp_hashinfo = {
31 .lhash_lock = RW_LOCK_UNLOCKED,
32 .lhash_users = ATOMIC_INIT(0),
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -030033 .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait),
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070034};
35
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -030036EXPORT_SYMBOL_GPL(dccp_hashinfo);
37
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070038static int dccp_v4_get_port(struct sock *sk, const unsigned short snum)
39{
Arnaldo Carvalho de Melo971af182005-12-13 23:14:47 -080040 return inet_csk_get_port(&dccp_hashinfo, sk, snum,
41 inet_csk_bind_conflict);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070042}
43
44static void dccp_v4_hash(struct sock *sk)
45{
46 inet_hash(&dccp_hashinfo, sk);
47}
48
49static void dccp_v4_unhash(struct sock *sk)
50{
51 inet_unhash(&dccp_hashinfo, sk);
52}
53
54/* called with local bh disabled */
55static int __dccp_v4_check_established(struct sock *sk, const __u16 lport,
56 struct inet_timewait_sock **twp)
57{
58 struct inet_sock *inet = inet_sk(sk);
59 const u32 daddr = inet->rcv_saddr;
60 const u32 saddr = inet->daddr;
61 const int dif = sk->sk_bound_dev_if;
62 INET_ADDR_COOKIE(acookie, saddr, daddr)
63 const __u32 ports = INET_COMBINED_PORTS(inet->dport, lport);
Eric Dumazet81c3d542005-10-03 14:13:38 -070064 unsigned int hash = inet_ehashfn(daddr, lport, saddr, inet->dport);
65 struct inet_ehash_bucket *head = inet_ehash_bucket(&dccp_hashinfo, hash);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070066 const struct sock *sk2;
67 const struct hlist_node *node;
68 struct inet_timewait_sock *tw;
69
Eric Dumazet81c3d542005-10-03 14:13:38 -070070 prefetch(head->chain.first);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070071 write_lock(&head->lock);
72
73 /* Check TIME-WAIT sockets first. */
74 sk_for_each(sk2, node, &(head + dccp_hashinfo.ehash_size)->chain) {
75 tw = inet_twsk(sk2);
76
Eric Dumazet81c3d542005-10-03 14:13:38 -070077 if (INET_TW_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070078 goto not_unique;
79 }
80 tw = NULL;
81
82 /* And established part... */
83 sk_for_each(sk2, node, &head->chain) {
Eric Dumazet81c3d542005-10-03 14:13:38 -070084 if (INET_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070085 goto not_unique;
86 }
87
88 /* Must record num and sport now. Otherwise we will see
89 * in hash table socket with a funny identity. */
90 inet->num = lport;
91 inet->sport = htons(lport);
Eric Dumazet81c3d542005-10-03 14:13:38 -070092 sk->sk_hash = hash;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070093 BUG_TRAP(sk_unhashed(sk));
94 __sk_add_node(sk, &head->chain);
95 sock_prot_inc_use(sk->sk_prot);
96 write_unlock(&head->lock);
97
98 if (twp != NULL) {
99 *twp = tw;
100 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
101 } else if (tw != NULL) {
102 /* Silly. Should hash-dance instead... */
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -0700103 inet_twsk_deschedule(tw, &dccp_death_row);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700104 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
105
106 inet_twsk_put(tw);
107 }
108
109 return 0;
110
111not_unique:
112 write_unlock(&head->lock);
113 return -EADDRNOTAVAIL;
114}
115
116/*
117 * Bind a port for a connect operation and hash it.
118 */
119static int dccp_v4_hash_connect(struct sock *sk)
120{
121 const unsigned short snum = inet_sk(sk)->num;
122 struct inet_bind_hashbucket *head;
123 struct inet_bind_bucket *tb;
124 int ret;
125
126 if (snum == 0) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700127 int low = sysctl_local_port_range[0];
128 int high = sysctl_local_port_range[1];
129 int remaining = (high - low) + 1;
Stephen Hemminger6df71632005-11-03 16:33:23 -0800130 int rover = net_random() % (high - low) + low;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700131 struct hlist_node *node;
132 struct inet_timewait_sock *tw = NULL;
133
134 local_bh_disable();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700135 do {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300136 head = &dccp_hashinfo.bhash[inet_bhashfn(rover,
137 dccp_hashinfo.bhash_size)];
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700138 spin_lock(&head->lock);
139
140 /* Does not bother with rcv_saddr checks,
141 * because the established check is already
142 * unique enough.
143 */
144 inet_bind_bucket_for_each(tb, node, &head->chain) {
145 if (tb->port == rover) {
146 BUG_TRAP(!hlist_empty(&tb->owners));
147 if (tb->fastreuse >= 0)
148 goto next_port;
149 if (!__dccp_v4_check_established(sk,
150 rover,
151 &tw))
152 goto ok;
153 goto next_port;
154 }
155 }
156
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300157 tb = inet_bind_bucket_create(dccp_hashinfo.bind_bucket_cachep,
158 head, rover);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700159 if (tb == NULL) {
160 spin_unlock(&head->lock);
161 break;
162 }
163 tb->fastreuse = -1;
164 goto ok;
165
166 next_port:
167 spin_unlock(&head->lock);
Stephen Hemminger6df71632005-11-03 16:33:23 -0800168 if (++rover > high)
169 rover = low;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700170 } while (--remaining > 0);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700171
172 local_bh_enable();
173
174 return -EADDRNOTAVAIL;
175
176ok:
177 /* All locks still held and bhs disabled */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700178 inet_bind_hash(sk, tb, rover);
179 if (sk_unhashed(sk)) {
180 inet_sk(sk)->sport = htons(rover);
181 __inet_hash(&dccp_hashinfo, sk, 0);
182 }
183 spin_unlock(&head->lock);
184
185 if (tw != NULL) {
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -0700186 inet_twsk_deschedule(tw, &dccp_death_row);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700187 inet_twsk_put(tw);
188 }
189
190 ret = 0;
191 goto out;
192 }
193
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300194 head = &dccp_hashinfo.bhash[inet_bhashfn(snum,
195 dccp_hashinfo.bhash_size)];
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700196 tb = inet_csk(sk)->icsk_bind_hash;
197 spin_lock_bh(&head->lock);
198 if (sk_head(&tb->owners) == sk && sk->sk_bind_node.next == NULL) {
199 __inet_hash(&dccp_hashinfo, sk, 0);
200 spin_unlock_bh(&head->lock);
201 return 0;
202 } else {
203 spin_unlock(&head->lock);
204 /* No definite answer... Walk to established hash table */
205 ret = __dccp_v4_check_established(sk, snum, NULL);
206out:
207 local_bh_enable();
208 return ret;
209 }
210}
211
212static int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr,
213 int addr_len)
214{
215 struct inet_sock *inet = inet_sk(sk);
216 struct dccp_sock *dp = dccp_sk(sk);
217 const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
218 struct rtable *rt;
219 u32 daddr, nexthop;
220 int tmp;
221 int err;
222
223 dp->dccps_role = DCCP_ROLE_CLIENT;
224
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700225 if (dccp_service_not_initialized(sk))
226 return -EPROTO;
227
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700228 if (addr_len < sizeof(struct sockaddr_in))
229 return -EINVAL;
230
231 if (usin->sin_family != AF_INET)
232 return -EAFNOSUPPORT;
233
234 nexthop = daddr = usin->sin_addr.s_addr;
235 if (inet->opt != NULL && inet->opt->srr) {
236 if (daddr == 0)
237 return -EINVAL;
238 nexthop = inet->opt->faddr;
239 }
240
241 tmp = ip_route_connect(&rt, nexthop, inet->saddr,
242 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
243 IPPROTO_DCCP,
244 inet->sport, usin->sin_port, sk);
245 if (tmp < 0)
246 return tmp;
247
248 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
249 ip_rt_put(rt);
250 return -ENETUNREACH;
251 }
252
253 if (inet->opt == NULL || !inet->opt->srr)
254 daddr = rt->rt_dst;
255
256 if (inet->saddr == 0)
257 inet->saddr = rt->rt_src;
258 inet->rcv_saddr = inet->saddr;
259
260 inet->dport = usin->sin_port;
261 inet->daddr = daddr;
262
263 dp->dccps_ext_header_len = 0;
264 if (inet->opt != NULL)
265 dp->dccps_ext_header_len = inet->opt->optlen;
266 /*
267 * Socket identity is still unknown (sport may be zero).
268 * However we set state to DCCP_REQUESTING and not releasing socket
269 * lock select source port, enter ourselves into the hash tables and
270 * complete initialization after this.
271 */
272 dccp_set_state(sk, DCCP_REQUESTING);
273 err = dccp_v4_hash_connect(sk);
274 if (err != 0)
275 goto failure;
276
277 err = ip_route_newports(&rt, inet->sport, inet->dport, sk);
278 if (err != 0)
279 goto failure;
280
281 /* OK, now commit destination to socket. */
282 sk_setup_caps(sk, &rt->u.dst);
283
284 dp->dccps_gar =
285 dp->dccps_iss = secure_dccp_sequence_number(inet->saddr,
286 inet->daddr,
287 inet->sport,
288 usin->sin_port);
289 dccp_update_gss(sk, dp->dccps_iss);
290
Arnaldo Carvalho de Melo03ace392005-08-21 05:36:45 -0300291 /*
292 * SWL and AWL are initially adjusted so that they are not less than
293 * the initial Sequence Numbers received and sent, respectively:
294 * SWL := max(GSR + 1 - floor(W/4), ISR),
295 * AWL := max(GSS - W' + 1, ISS).
296 * These adjustments MUST be applied only at the beginning of the
297 * connection.
298 */
299 dccp_set_seqno(&dp->dccps_awl, max48(dp->dccps_awl, dp->dccps_iss));
300
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700301 inet->id = dp->dccps_iss ^ jiffies;
302
303 err = dccp_connect(sk);
304 rt = NULL;
305 if (err != 0)
306 goto failure;
307out:
308 return err;
309failure:
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300310 /*
311 * This unhashes the socket and releases the local port, if necessary.
312 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700313 dccp_set_state(sk, DCCP_CLOSED);
314 ip_rt_put(rt);
315 sk->sk_route_caps = 0;
316 inet->dport = 0;
317 goto out;
318}
319
320/*
321 * This routine does path mtu discovery as defined in RFC1191.
322 */
323static inline void dccp_do_pmtu_discovery(struct sock *sk,
324 const struct iphdr *iph,
325 u32 mtu)
326{
327 struct dst_entry *dst;
328 const struct inet_sock *inet = inet_sk(sk);
329 const struct dccp_sock *dp = dccp_sk(sk);
330
331 /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
332 * send out by Linux are always < 576bytes so they should go through
333 * unfragmented).
334 */
335 if (sk->sk_state == DCCP_LISTEN)
336 return;
337
338 /* We don't check in the destentry if pmtu discovery is forbidden
339 * on this route. We just assume that no packet_to_big packets
340 * are send back when pmtu discovery is not active.
341 * There is a small race when the user changes this flag in the
342 * route, but I think that's acceptable.
343 */
344 if ((dst = __sk_dst_check(sk, 0)) == NULL)
345 return;
346
347 dst->ops->update_pmtu(dst, mtu);
348
349 /* Something is about to be wrong... Remember soft error
350 * for the case, if this connection will not able to recover.
351 */
352 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
353 sk->sk_err_soft = EMSGSIZE;
354
355 mtu = dst_mtu(dst);
356
357 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
358 dp->dccps_pmtu_cookie > mtu) {
359 dccp_sync_mss(sk, mtu);
360
361 /*
362 * From: draft-ietf-dccp-spec-11.txt
363 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300364 * DCCP-Sync packets are the best choice for upward
365 * probing, since DCCP-Sync probes do not risk application
366 * data loss.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700367 */
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300368 dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700369 } /* else let the usual retransmit timer handle it */
370}
371
372static void dccp_v4_ctl_send_ack(struct sk_buff *rxskb)
373{
374 int err;
375 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
376 const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) +
377 sizeof(struct dccp_hdr_ext) +
378 sizeof(struct dccp_hdr_ack_bits);
379 struct sk_buff *skb;
380
381 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
382 return;
383
384 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
385 if (skb == NULL)
386 return;
387
388 /* Reserve space for headers. */
389 skb_reserve(skb, MAX_DCCP_HEADER);
390
391 skb->dst = dst_clone(rxskb->dst);
392
393 skb->h.raw = skb_push(skb, dccp_hdr_ack_len);
394 dh = dccp_hdr(skb);
395 memset(dh, 0, dccp_hdr_ack_len);
396
397 /* Build DCCP header and checksum it. */
398 dh->dccph_type = DCCP_PKT_ACK;
399 dh->dccph_sport = rxdh->dccph_dport;
400 dh->dccph_dport = rxdh->dccph_sport;
401 dh->dccph_doff = dccp_hdr_ack_len / 4;
402 dh->dccph_x = 1;
403
404 dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300405 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
406 DCCP_SKB_CB(rxskb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700407
408 bh_lock_sock(dccp_ctl_socket->sk);
409 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300410 rxskb->nh.iph->daddr,
411 rxskb->nh.iph->saddr, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700412 bh_unlock_sock(dccp_ctl_socket->sk);
413
414 if (err == NET_XMIT_CN || err == 0) {
415 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
416 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
417 }
418}
419
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300420static void dccp_v4_reqsk_send_ack(struct sk_buff *skb,
421 struct request_sock *req)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700422{
423 dccp_v4_ctl_send_ack(skb);
424}
425
426static int dccp_v4_send_response(struct sock *sk, struct request_sock *req,
427 struct dst_entry *dst)
428{
429 int err = -1;
430 struct sk_buff *skb;
431
432 /* First, grab a route. */
433
434 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
435 goto out;
436
437 skb = dccp_make_response(sk, dst, req);
438 if (skb != NULL) {
439 const struct inet_request_sock *ireq = inet_rsk(req);
440
Herbert Xu49c5bfa2005-10-18 12:03:28 +1000441 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700442 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
443 ireq->rmt_addr,
444 ireq->opt);
445 if (err == NET_XMIT_CN)
446 err = 0;
447 }
448
449out:
450 dst_release(dst);
451 return err;
452}
453
454/*
455 * This routine is called by the ICMP module when it gets some sort of error
456 * condition. If err < 0 then the socket should be closed and the error
457 * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
458 * After adjustment header points to the first 8 bytes of the tcp header. We
459 * need to find the appropriate port.
460 *
461 * The locking strategy used here is very "optimistic". When someone else
462 * accesses the socket the ICMP is just dropped and for some paths there is no
463 * check at all. A more general error queue to queue errors for later handling
464 * is probably better.
465 */
466void dccp_v4_err(struct sk_buff *skb, u32 info)
467{
468 const struct iphdr *iph = (struct iphdr *)skb->data;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300469 const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data +
470 (iph->ihl << 2));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700471 struct dccp_sock *dp;
472 struct inet_sock *inet;
473 const int type = skb->h.icmph->type;
474 const int code = skb->h.icmph->code;
475 struct sock *sk;
476 __u64 seq;
477 int err;
478
479 if (skb->len < (iph->ihl << 2) + 8) {
480 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
481 return;
482 }
483
484 sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport,
485 iph->saddr, dh->dccph_sport, inet_iif(skb));
486 if (sk == NULL) {
487 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
488 return;
489 }
490
491 if (sk->sk_state == DCCP_TIME_WAIT) {
492 inet_twsk_put((struct inet_timewait_sock *)sk);
493 return;
494 }
495
496 bh_lock_sock(sk);
497 /* If too many ICMPs get dropped on busy
498 * servers this needs to be solved differently.
499 */
500 if (sock_owned_by_user(sk))
501 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
502
503 if (sk->sk_state == DCCP_CLOSED)
504 goto out;
505
506 dp = dccp_sk(sk);
507 seq = dccp_hdr_seq(skb);
508 if (sk->sk_state != DCCP_LISTEN &&
509 !between48(seq, dp->dccps_swl, dp->dccps_swh)) {
510 NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS);
511 goto out;
512 }
513
514 switch (type) {
515 case ICMP_SOURCE_QUENCH:
516 /* Just silently ignore these. */
517 goto out;
518 case ICMP_PARAMETERPROB:
519 err = EPROTO;
520 break;
521 case ICMP_DEST_UNREACH:
522 if (code > NR_ICMP_UNREACH)
523 goto out;
524
525 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
526 if (!sock_owned_by_user(sk))
527 dccp_do_pmtu_discovery(sk, iph, info);
528 goto out;
529 }
530
531 err = icmp_err_convert[code].errno;
532 break;
533 case ICMP_TIME_EXCEEDED:
534 err = EHOSTUNREACH;
535 break;
536 default:
537 goto out;
538 }
539
540 switch (sk->sk_state) {
541 struct request_sock *req , **prev;
542 case DCCP_LISTEN:
543 if (sock_owned_by_user(sk))
544 goto out;
545 req = inet_csk_search_req(sk, &prev, dh->dccph_dport,
546 iph->daddr, iph->saddr);
547 if (!req)
548 goto out;
549
550 /*
551 * ICMPs are not backlogged, hence we cannot get an established
552 * socket here.
553 */
554 BUG_TRAP(!req->sk);
555
556 if (seq != dccp_rsk(req)->dreq_iss) {
557 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
558 goto out;
559 }
560 /*
561 * Still in RESPOND, just remove it silently.
562 * There is no good way to pass the error to the newly
563 * created socket, and POSIX does not want network
564 * errors returned from accept().
565 */
566 inet_csk_reqsk_queue_drop(sk, req, prev);
567 goto out;
568
569 case DCCP_REQUESTING:
570 case DCCP_RESPOND:
571 if (!sock_owned_by_user(sk)) {
572 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
573 sk->sk_err = err;
574
575 sk->sk_error_report(sk);
576
577 dccp_done(sk);
578 } else
579 sk->sk_err_soft = err;
580 goto out;
581 }
582
583 /* If we've already connected we will keep trying
584 * until we time out, or the user gives up.
585 *
586 * rfc1122 4.2.3.9 allows to consider as hard errors
587 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
588 * but it is obsoleted by pmtu discovery).
589 *
590 * Note, that in modern internet, where routing is unreliable
591 * and in each dark corner broken firewalls sit, sending random
592 * errors ordered by their masters even this two messages finally lose
593 * their original sense (even Linux sends invalid PORT_UNREACHs)
594 *
595 * Now we are in compliance with RFCs.
596 * --ANK (980905)
597 */
598
599 inet = inet_sk(sk);
600 if (!sock_owned_by_user(sk) && inet->recverr) {
601 sk->sk_err = err;
602 sk->sk_error_report(sk);
603 } else /* Only an error on timeout */
604 sk->sk_err_soft = err;
605out:
606 bh_unlock_sock(sk);
607 sock_put(sk);
608}
609
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800610/* This routine computes an IPv4 DCCP checksum. */
611static void dccp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
612{
613 const struct inet_sock *inet = inet_sk(sk);
614 struct dccp_hdr *dh = dccp_hdr(skb);
615
616 dh->dccph_checksum = dccp_v4_checksum(skb, inet->saddr, inet->daddr);
617}
618
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700619int dccp_v4_send_reset(struct sock *sk, enum dccp_reset_codes code)
620{
621 struct sk_buff *skb;
622 /*
623 * FIXME: what if rebuild_header fails?
624 * Should we be doing a rebuild_header here?
625 */
626 int err = inet_sk_rebuild_header(sk);
627
628 if (err != 0)
629 return err;
630
631 skb = dccp_make_reset(sk, sk->sk_dst_cache, code);
632 if (skb != NULL) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700633 const struct inet_sock *inet = inet_sk(sk);
634
Herbert Xu49c5bfa2005-10-18 12:03:28 +1000635 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700636 err = ip_build_and_send_pkt(skb, sk,
637 inet->saddr, inet->daddr, NULL);
638 if (err == NET_XMIT_CN)
639 err = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700640 }
641
642 return err;
643}
644
645static inline u64 dccp_v4_init_sequence(const struct sock *sk,
646 const struct sk_buff *skb)
647{
648 return secure_dccp_sequence_number(skb->nh.iph->daddr,
649 skb->nh.iph->saddr,
650 dccp_hdr(skb)->dccph_dport,
651 dccp_hdr(skb)->dccph_sport);
652}
653
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700654static inline int dccp_bad_service_code(const struct sock *sk,
655 const __u32 service)
656{
657 const struct dccp_sock *dp = dccp_sk(sk);
658
659 if (dp->dccps_service == service)
660 return 0;
661 return !dccp_list_has_service(dp->dccps_service_list, service);
662}
663
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700664int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
665{
666 struct inet_request_sock *ireq;
667 struct dccp_sock dp;
668 struct request_sock *req;
669 struct dccp_request_sock *dreq;
670 const __u32 saddr = skb->nh.iph->saddr;
671 const __u32 daddr = skb->nh.iph->daddr;
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700672 const __u32 service = dccp_hdr_request(skb)->dccph_req_service;
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700673 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
674 __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700675 struct dst_entry *dst = NULL;
676
677 /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
678 if (((struct rtable *)skb->dst)->rt_flags &
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700679 (RTCF_BROADCAST | RTCF_MULTICAST)) {
680 reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700681 goto drop;
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700682 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700683
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700684 if (dccp_bad_service_code(sk, service)) {
685 reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
686 goto drop;
687 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700688 /*
689 * TW buckets are converted to open requests without
690 * limitations, they conserve resources and peer is
691 * evidently real one.
692 */
693 if (inet_csk_reqsk_queue_is_full(sk))
694 goto drop;
695
696 /*
697 * Accept backlog is full. If we have already queued enough
698 * of warm entries in syn queue, drop request. It is better than
699 * clogging syn queue with openreqs with exponentially increasing
700 * timeout.
701 */
702 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
703 goto drop;
704
705 req = reqsk_alloc(sk->sk_prot->rsk_prot);
706 if (req == NULL)
707 goto drop;
708
709 /* FIXME: process options */
710
711 dccp_openreq_init(req, &dp, skb);
712
713 ireq = inet_rsk(req);
714 ireq->loc_addr = daddr;
715 ireq->rmt_addr = saddr;
716 /* FIXME: Merge Aristeu's option parsing code when ready */
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300717 req->rcv_wnd = 100; /* Fake, option parsing will get the
718 right value */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700719 ireq->opt = NULL;
720
721 /*
722 * Step 3: Process LISTEN state
723 *
724 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
725 *
726 * In fact we defer setting S.GSR, S.SWL, S.SWH to
727 * dccp_create_openreq_child.
728 */
729 dreq = dccp_rsk(req);
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700730 dreq->dreq_isr = dcb->dccpd_seq;
731 dreq->dreq_iss = dccp_v4_init_sequence(sk, skb);
732 dreq->dreq_service = service;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700733
734 if (dccp_v4_send_response(sk, req, dst))
735 goto drop_and_free;
736
737 inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
738 return 0;
739
740drop_and_free:
741 /*
742 * FIXME: should be reqsk_free after implementing req->rsk_ops
743 */
744 __reqsk_free(req);
745drop:
746 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700747 dcb->dccpd_reset_code = reset_code;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700748 return -1;
749}
750
751/*
752 * The three way handshake has completed - we got a valid ACK or DATAACK -
753 * now create the new socket.
754 *
755 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
756 */
757struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
758 struct request_sock *req,
759 struct dst_entry *dst)
760{
761 struct inet_request_sock *ireq;
762 struct inet_sock *newinet;
763 struct dccp_sock *newdp;
764 struct sock *newsk;
765
766 if (sk_acceptq_is_full(sk))
767 goto exit_overflow;
768
769 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
770 goto exit;
771
772 newsk = dccp_create_openreq_child(sk, req, skb);
773 if (newsk == NULL)
774 goto exit;
775
776 sk_setup_caps(newsk, dst);
777
778 newdp = dccp_sk(newsk);
779 newinet = inet_sk(newsk);
780 ireq = inet_rsk(req);
781 newinet->daddr = ireq->rmt_addr;
782 newinet->rcv_saddr = ireq->loc_addr;
783 newinet->saddr = ireq->loc_addr;
784 newinet->opt = ireq->opt;
785 ireq->opt = NULL;
786 newinet->mc_index = inet_iif(skb);
787 newinet->mc_ttl = skb->nh.iph->ttl;
788 newinet->id = jiffies;
789
790 dccp_sync_mss(newsk, dst_mtu(dst));
791
792 __inet_hash(&dccp_hashinfo, newsk, 0);
793 __inet_inherit_port(&dccp_hashinfo, sk, newsk);
794
795 return newsk;
796
797exit_overflow:
798 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
799exit:
800 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
801 dst_release(dst);
802 return NULL;
803}
804
805static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
806{
807 const struct dccp_hdr *dh = dccp_hdr(skb);
808 const struct iphdr *iph = skb->nh.iph;
809 struct sock *nsk;
810 struct request_sock **prev;
811 /* Find possible connection requests. */
812 struct request_sock *req = inet_csk_search_req(sk, &prev,
813 dh->dccph_sport,
814 iph->saddr, iph->daddr);
815 if (req != NULL)
816 return dccp_check_req(sk, skb, req, prev);
817
818 nsk = __inet_lookup_established(&dccp_hashinfo,
819 iph->saddr, dh->dccph_sport,
820 iph->daddr, ntohs(dh->dccph_dport),
821 inet_iif(skb));
822 if (nsk != NULL) {
823 if (nsk->sk_state != DCCP_TIME_WAIT) {
824 bh_lock_sock(nsk);
825 return nsk;
826 }
827 inet_twsk_put((struct inet_timewait_sock *)nsk);
828 return NULL;
829 }
830
831 return sk;
832}
833
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300834int dccp_v4_checksum(const struct sk_buff *skb, const u32 saddr,
835 const u32 daddr)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700836{
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700837 const struct dccp_hdr* dh = dccp_hdr(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700838 int checksum_len;
839 u32 tmp;
840
841 if (dh->dccph_cscov == 0)
842 checksum_len = skb->len;
843 else {
844 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300845 checksum_len = checksum_len < skb->len ? checksum_len :
846 skb->len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700847 }
848
849 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300850 return csum_tcpudp_magic(saddr, daddr, checksum_len,
851 IPPROTO_DCCP, tmp);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700852}
853
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700854static int dccp_v4_verify_checksum(struct sk_buff *skb,
855 const u32 saddr, const u32 daddr)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700856{
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700857 struct dccp_hdr *dh = dccp_hdr(skb);
858 int checksum_len;
859 u32 tmp;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700860
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700861 if (dh->dccph_cscov == 0)
862 checksum_len = skb->len;
863 else {
864 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300865 checksum_len = checksum_len < skb->len ? checksum_len :
866 skb->len;
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700867 }
868 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300869 return csum_tcpudp_magic(saddr, daddr, checksum_len,
870 IPPROTO_DCCP, tmp) == 0 ? 0 : -1;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700871}
872
873static struct dst_entry* dccp_v4_route_skb(struct sock *sk,
874 struct sk_buff *skb)
875{
876 struct rtable *rt;
877 struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif,
878 .nl_u = { .ip4_u =
879 { .daddr = skb->nh.iph->saddr,
880 .saddr = skb->nh.iph->daddr,
881 .tos = RT_CONN_FLAGS(sk) } },
882 .proto = sk->sk_protocol,
883 .uli_u = { .ports =
884 { .sport = dccp_hdr(skb)->dccph_dport,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300885 .dport = dccp_hdr(skb)->dccph_sport }
886 }
887 };
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700888
889 if (ip_route_output_flow(&rt, &fl, sk, 0)) {
890 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
891 return NULL;
892 }
893
894 return &rt->u.dst;
895}
896
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300897static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700898{
899 int err;
900 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
901 const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
902 sizeof(struct dccp_hdr_ext) +
903 sizeof(struct dccp_hdr_reset);
904 struct sk_buff *skb;
905 struct dst_entry *dst;
Arnaldo Carvalho de Melo2807d4f2005-08-21 05:33:48 -0300906 u64 seqno;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700907
908 /* Never send a reset in response to a reset. */
909 if (rxdh->dccph_type == DCCP_PKT_RESET)
910 return;
911
912 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
913 return;
914
915 dst = dccp_v4_route_skb(dccp_ctl_socket->sk, rxskb);
916 if (dst == NULL)
917 return;
918
919 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
920 if (skb == NULL)
921 goto out;
922
923 /* Reserve space for headers. */
924 skb_reserve(skb, MAX_DCCP_HEADER);
925 skb->dst = dst_clone(dst);
926
927 skb->h.raw = skb_push(skb, dccp_hdr_reset_len);
928 dh = dccp_hdr(skb);
929 memset(dh, 0, dccp_hdr_reset_len);
930
931 /* Build DCCP header and checksum it. */
932 dh->dccph_type = DCCP_PKT_RESET;
933 dh->dccph_sport = rxdh->dccph_dport;
934 dh->dccph_dport = rxdh->dccph_sport;
935 dh->dccph_doff = dccp_hdr_reset_len / 4;
936 dh->dccph_x = 1;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300937 dccp_hdr_reset(skb)->dccph_reset_code =
938 DCCP_SKB_CB(rxskb)->dccpd_reset_code;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700939
Arnaldo Carvalho de Melo2807d4f2005-08-21 05:33:48 -0300940 /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
941 seqno = 0;
942 if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
943 dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
944
945 dccp_hdr_set_seq(dh, seqno);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300946 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
947 DCCP_SKB_CB(rxskb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700948
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700949 dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr,
950 rxskb->nh.iph->daddr);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700951
952 bh_lock_sock(dccp_ctl_socket->sk);
953 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300954 rxskb->nh.iph->daddr,
955 rxskb->nh.iph->saddr, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700956 bh_unlock_sock(dccp_ctl_socket->sk);
957
958 if (err == NET_XMIT_CN || err == 0) {
959 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
960 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
961 }
962out:
963 dst_release(dst);
964}
965
966int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
967{
968 struct dccp_hdr *dh = dccp_hdr(skb);
969
970 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
971 if (dccp_rcv_established(sk, skb, dh, skb->len))
972 goto reset;
973 return 0;
974 }
975
976 /*
977 * Step 3: Process LISTEN state
978 * If S.state == LISTEN,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300979 * If P.type == Request or P contains a valid Init Cookie
980 * option,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700981 * * Must scan the packet's options to check for an Init
982 * Cookie. Only the Init Cookie is processed here,
983 * however; other options are processed in Step 8. This
984 * scan need only be performed if the endpoint uses Init
985 * Cookies *
986 * * Generate a new socket and switch to that socket *
987 * Set S := new socket for this port pair
988 * S.state = RESPOND
989 * Choose S.ISS (initial seqno) or set from Init Cookie
990 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
991 * Continue with S.state == RESPOND
992 * * A Response packet will be generated in Step 11 *
993 * Otherwise,
994 * Generate Reset(No Connection) unless P.type == Reset
995 * Drop packet and return
996 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300997 * NOTE: the check for the packet types is done in
998 * dccp_rcv_state_process
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700999 */
1000 if (sk->sk_state == DCCP_LISTEN) {
1001 struct sock *nsk = dccp_v4_hnd_req(sk, skb);
1002
1003 if (nsk == NULL)
1004 goto discard;
1005
1006 if (nsk != sk) {
1007 if (dccp_child_process(sk, nsk, skb))
1008 goto reset;
1009 return 0;
1010 }
1011 }
1012
1013 if (dccp_rcv_state_process(sk, skb, dh, skb->len))
1014 goto reset;
1015 return 0;
1016
1017reset:
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001018 dccp_v4_ctl_send_reset(skb);
1019discard:
1020 kfree_skb(skb);
1021 return 0;
1022}
1023
1024static inline int dccp_invalid_packet(struct sk_buff *skb)
1025{
1026 const struct dccp_hdr *dh;
1027
1028 if (skb->pkt_type != PACKET_HOST)
1029 return 1;
1030
1031 if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001032 LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001033 return 1;
1034 }
1035
1036 dh = dccp_hdr(skb);
1037
1038 /* If the packet type is not understood, drop packet and return */
1039 if (dh->dccph_type >= DCCP_PKT_INVALID) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001040 LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001041 return 1;
1042 }
1043
1044 /*
1045 * If P.Data Offset is too small for packet type, or too large for
1046 * packet, drop packet and return
1047 */
1048 if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001049 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
1050 "too small 1\n",
1051 dh->dccph_doff);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001052 return 1;
1053 }
1054
1055 if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001056 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
1057 "too small 2\n",
1058 dh->dccph_doff);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001059 return 1;
1060 }
1061
1062 dh = dccp_hdr(skb);
1063
1064 /*
1065 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
1066 * has short sequence numbers), drop packet and return
1067 */
1068 if (dh->dccph_x == 0 &&
1069 dh->dccph_type != DCCP_PKT_DATA &&
1070 dh->dccph_type != DCCP_PKT_ACK &&
1071 dh->dccph_type != DCCP_PKT_DATAACK) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001072 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack "
1073 "nor DataAck and P.X == 0\n",
1074 dccp_packet_name(dh->dccph_type));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001075 return 1;
1076 }
1077
1078 /* If the header checksum is incorrect, drop packet and return */
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -07001079 if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr,
1080 skb->nh.iph->daddr) < 0) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001081 LIMIT_NETDEBUG(KERN_WARNING "DCCP: header checksum is "
1082 "incorrect\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001083 return 1;
1084 }
1085
1086 return 0;
1087}
1088
1089/* this is called when real data arrives */
1090int dccp_v4_rcv(struct sk_buff *skb)
1091{
1092 const struct dccp_hdr *dh;
1093 struct sock *sk;
1094 int rc;
1095
1096 /* Step 1: Check header basics: */
1097
1098 if (dccp_invalid_packet(skb))
1099 goto discard_it;
1100
1101 dh = dccp_hdr(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001102
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001103 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
1104 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
1105
1106 dccp_pr_debug("%8.8s "
1107 "src=%u.%u.%u.%u@%-5d "
1108 "dst=%u.%u.%u.%u@%-5d seq=%llu",
1109 dccp_packet_name(dh->dccph_type),
1110 NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport),
1111 NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport),
David S. Millerf6ccf552005-08-09 20:27:14 -07001112 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001113
1114 if (dccp_packet_without_ack(skb)) {
1115 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
1116 dccp_pr_debug_cat("\n");
1117 } else {
1118 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
David S. Millerf6ccf552005-08-09 20:27:14 -07001119 dccp_pr_debug_cat(", ack=%llu\n",
1120 (unsigned long long)
1121 DCCP_SKB_CB(skb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001122 }
1123
1124 /* Step 2:
1125 * Look up flow ID in table and get corresponding socket */
1126 sk = __inet_lookup(&dccp_hashinfo,
1127 skb->nh.iph->saddr, dh->dccph_sport,
1128 skb->nh.iph->daddr, ntohs(dh->dccph_dport),
1129 inet_iif(skb));
1130
1131 /*
1132 * Step 2:
1133 * If no socket ...
1134 * Generate Reset(No Connection) unless P.type == Reset
1135 * Drop packet and return
1136 */
1137 if (sk == NULL) {
1138 dccp_pr_debug("failed to look up flow ID in table and "
1139 "get corresponding socket\n");
1140 goto no_dccp_socket;
1141 }
1142
1143 /*
1144 * Step 2:
1145 * ... or S.state == TIMEWAIT,
1146 * Generate Reset(No Connection) unless P.type == Reset
1147 * Drop packet and return
1148 */
1149
1150 if (sk->sk_state == DCCP_TIME_WAIT) {
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -07001151 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
1152 "do_time_wait\n");
1153 goto do_time_wait;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001154 }
1155
1156 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
1157 dccp_pr_debug("xfrm4_policy_check failed\n");
1158 goto discard_and_relse;
1159 }
1160
1161 if (sk_filter(sk, skb, 0)) {
1162 dccp_pr_debug("sk_filter failed\n");
1163 goto discard_and_relse;
1164 }
1165
1166 skb->dev = NULL;
1167
1168 bh_lock_sock(sk);
1169 rc = 0;
1170 if (!sock_owned_by_user(sk))
1171 rc = dccp_v4_do_rcv(sk, skb);
1172 else
1173 sk_add_backlog(sk, skb);
1174 bh_unlock_sock(sk);
1175
1176 sock_put(sk);
1177 return rc;
1178
1179no_dccp_socket:
1180 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1181 goto discard_it;
1182 /*
1183 * Step 2:
1184 * Generate Reset(No Connection) unless P.type == Reset
1185 * Drop packet and return
1186 */
1187 if (dh->dccph_type != DCCP_PKT_RESET) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001188 DCCP_SKB_CB(skb)->dccpd_reset_code =
1189 DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001190 dccp_v4_ctl_send_reset(skb);
1191 }
1192
1193discard_it:
1194 /* Discard frame. */
1195 kfree_skb(skb);
1196 return 0;
1197
1198discard_and_relse:
1199 sock_put(sk);
1200 goto discard_it;
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -07001201
1202do_time_wait:
1203 inet_twsk_put((struct inet_timewait_sock *)sk);
1204 goto no_dccp_socket;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001205}
1206
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -08001207struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
1208 .queue_xmit = ip_queue_xmit,
1209 .send_check = dccp_v4_send_check,
1210 .rebuild_header = inet_sk_rebuild_header,
1211 .conn_request = dccp_v4_conn_request,
1212 .syn_recv_sock = dccp_v4_request_recv_sock,
1213 .net_header_len = sizeof(struct iphdr),
1214 .setsockopt = ip_setsockopt,
1215 .getsockopt = ip_getsockopt,
1216 .addr2sockaddr = inet_csk_addr2sockaddr,
1217 .sockaddr_len = sizeof(struct sockaddr_in),
1218};
1219
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001220static int dccp_v4_init_sock(struct sock *sk)
1221{
1222 struct dccp_sock *dp = dccp_sk(sk);
1223 static int dccp_ctl_socket_init = 1;
1224
1225 dccp_options_init(&dp->dccps_options);
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -03001226 do_gettimeofday(&dp->dccps_epoch);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001227
1228 if (dp->dccps_options.dccpo_send_ack_vector) {
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001229 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(DCCP_MAX_ACKVEC_LEN,
1230 GFP_KERNEL);
1231 if (dp->dccps_hc_rx_ackvec == NULL)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001232 return -ENOMEM;
1233 }
1234
1235 /*
1236 * FIXME: We're hardcoding the CCID, and doing this at this point makes
1237 * the listening (master) sock get CCID control blocks, which is not
1238 * necessary, but for now, to not mess with the test userspace apps,
1239 * lets leave it here, later the real solution is to do this in a
1240 * setsockopt(CCIDs-I-want/accept). -acme
1241 */
1242 if (likely(!dccp_ctl_socket_init)) {
Arnaldo Carvalho de Melo561713c2005-09-18 00:18:52 -07001243 dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_rx_ccid,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001244 sk);
Arnaldo Carvalho de Melo561713c2005-09-18 00:18:52 -07001245 dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_tx_ccid,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001246 sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001247 if (dp->dccps_hc_rx_ccid == NULL ||
1248 dp->dccps_hc_tx_ccid == NULL) {
1249 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1250 ccid_exit(dp->dccps_hc_tx_ccid, sk);
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001251 if (dp->dccps_options.dccpo_send_ack_vector) {
1252 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1253 dp->dccps_hc_rx_ackvec = NULL;
1254 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001255 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1256 return -ENOMEM;
1257 }
1258 } else
1259 dccp_ctl_socket_init = 0;
1260
1261 dccp_init_xmit_timers(sk);
Arnaldo Carvalho de Melo0b4e03b2005-08-09 20:31:11 -07001262 inet_csk(sk)->icsk_rto = DCCP_TIMEOUT_INIT;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001263 sk->sk_state = DCCP_CLOSED;
Arnaldo Carvalho de Meloc530cfb2005-08-29 02:15:54 -03001264 sk->sk_write_space = dccp_write_space;
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -08001265 inet_csk(sk)->icsk_af_ops = &dccp_ipv4_af_ops;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001266 dp->dccps_mss_cache = 536;
1267 dp->dccps_role = DCCP_ROLE_UNDEFINED;
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -07001268 dp->dccps_service = DCCP_SERVICE_INVALID_VALUE;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001269
1270 return 0;
1271}
1272
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -03001273static int dccp_v4_destroy_sock(struct sock *sk)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001274{
1275 struct dccp_sock *dp = dccp_sk(sk);
1276
1277 /*
Ian McDonald4c7e6892005-12-21 19:02:39 -08001278 * DCCP doesn't use sk_write_queue, just sk_send_head
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001279 * for retransmissions
1280 */
1281 if (sk->sk_send_head != NULL) {
1282 kfree_skb(sk->sk_send_head);
1283 sk->sk_send_head = NULL;
1284 }
1285
1286 /* Clean up a referenced DCCP bind bucket. */
1287 if (inet_csk(sk)->icsk_bind_hash != NULL)
1288 inet_put_port(&dccp_hashinfo, sk);
1289
Jesper Juhla51482b2005-11-08 09:41:34 -08001290 kfree(dp->dccps_service_list);
1291 dp->dccps_service_list = NULL;
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -07001292
Arnaldo Carvalho de Melo8efa5442005-08-23 21:54:00 -07001293 ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
1294 ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001295 if (dp->dccps_options.dccpo_send_ack_vector) {
1296 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1297 dp->dccps_hc_rx_ackvec = NULL;
1298 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001299 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1300 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1301 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1302
1303 return 0;
1304}
1305
1306static void dccp_v4_reqsk_destructor(struct request_sock *req)
1307{
1308 kfree(inet_rsk(req)->opt);
1309}
1310
1311static struct request_sock_ops dccp_request_sock_ops = {
1312 .family = PF_INET,
1313 .obj_size = sizeof(struct dccp_request_sock),
1314 .rtx_syn_ack = dccp_v4_send_response,
1315 .send_ack = dccp_v4_reqsk_send_ack,
1316 .destructor = dccp_v4_reqsk_destructor,
1317 .send_reset = dccp_v4_ctl_send_reset,
1318};
1319
1320struct proto dccp_v4_prot = {
1321 .name = "DCCP",
1322 .owner = THIS_MODULE,
1323 .close = dccp_close,
1324 .connect = dccp_v4_connect,
1325 .disconnect = dccp_disconnect,
1326 .ioctl = dccp_ioctl,
1327 .init = dccp_v4_init_sock,
1328 .setsockopt = dccp_setsockopt,
1329 .getsockopt = dccp_getsockopt,
1330 .sendmsg = dccp_sendmsg,
1331 .recvmsg = dccp_recvmsg,
1332 .backlog_rcv = dccp_v4_do_rcv,
1333 .hash = dccp_v4_hash,
1334 .unhash = dccp_v4_unhash,
1335 .accept = inet_csk_accept,
1336 .get_port = dccp_v4_get_port,
1337 .shutdown = dccp_shutdown,
1338 .destroy = dccp_v4_destroy_sock,
1339 .orphan_count = &dccp_orphan_count,
1340 .max_header = MAX_DCCP_HEADER,
1341 .obj_size = sizeof(struct dccp_sock),
1342 .rsk_prot = &dccp_request_sock_ops,
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -07001343 .twsk_obj_size = sizeof(struct inet_timewait_sock),
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001344};