blob: 656e13e38cfb69622e02df1d54aadd08e334bdfc [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{
40 return inet_csk_get_port(&dccp_hashinfo, sk, snum);
41}
42
43static void dccp_v4_hash(struct sock *sk)
44{
45 inet_hash(&dccp_hashinfo, sk);
46}
47
48static void dccp_v4_unhash(struct sock *sk)
49{
50 inet_unhash(&dccp_hashinfo, sk);
51}
52
53/* called with local bh disabled */
54static int __dccp_v4_check_established(struct sock *sk, const __u16 lport,
55 struct inet_timewait_sock **twp)
56{
57 struct inet_sock *inet = inet_sk(sk);
58 const u32 daddr = inet->rcv_saddr;
59 const u32 saddr = inet->daddr;
60 const int dif = sk->sk_bound_dev_if;
61 INET_ADDR_COOKIE(acookie, saddr, daddr)
62 const __u32 ports = INET_COMBINED_PORTS(inet->dport, lport);
Eric Dumazet81c3d542005-10-03 14:13:38 -070063 unsigned int hash = inet_ehashfn(daddr, lport, saddr, inet->dport);
64 struct inet_ehash_bucket *head = inet_ehash_bucket(&dccp_hashinfo, hash);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070065 const struct sock *sk2;
66 const struct hlist_node *node;
67 struct inet_timewait_sock *tw;
68
Eric Dumazet81c3d542005-10-03 14:13:38 -070069 prefetch(head->chain.first);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070070 write_lock(&head->lock);
71
72 /* Check TIME-WAIT sockets first. */
73 sk_for_each(sk2, node, &(head + dccp_hashinfo.ehash_size)->chain) {
74 tw = inet_twsk(sk2);
75
Eric Dumazet81c3d542005-10-03 14:13:38 -070076 if (INET_TW_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070077 goto not_unique;
78 }
79 tw = NULL;
80
81 /* And established part... */
82 sk_for_each(sk2, node, &head->chain) {
Eric Dumazet81c3d542005-10-03 14:13:38 -070083 if (INET_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070084 goto not_unique;
85 }
86
87 /* Must record num and sport now. Otherwise we will see
88 * in hash table socket with a funny identity. */
89 inet->num = lport;
90 inet->sport = htons(lport);
Eric Dumazet81c3d542005-10-03 14:13:38 -070091 sk->sk_hash = hash;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070092 BUG_TRAP(sk_unhashed(sk));
93 __sk_add_node(sk, &head->chain);
94 sock_prot_inc_use(sk->sk_prot);
95 write_unlock(&head->lock);
96
97 if (twp != NULL) {
98 *twp = tw;
99 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
100 } else if (tw != NULL) {
101 /* Silly. Should hash-dance instead... */
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -0700102 inet_twsk_deschedule(tw, &dccp_death_row);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700103 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
104
105 inet_twsk_put(tw);
106 }
107
108 return 0;
109
110not_unique:
111 write_unlock(&head->lock);
112 return -EADDRNOTAVAIL;
113}
114
115/*
116 * Bind a port for a connect operation and hash it.
117 */
118static int dccp_v4_hash_connect(struct sock *sk)
119{
120 const unsigned short snum = inet_sk(sk)->num;
121 struct inet_bind_hashbucket *head;
122 struct inet_bind_bucket *tb;
123 int ret;
124
125 if (snum == 0) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700126 int low = sysctl_local_port_range[0];
127 int high = sysctl_local_port_range[1];
128 int remaining = (high - low) + 1;
Stephen Hemminger6df71632005-11-03 16:33:23 -0800129 int rover = net_random() % (high - low) + low;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700130 struct hlist_node *node;
131 struct inet_timewait_sock *tw = NULL;
132
133 local_bh_disable();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700134 do {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300135 head = &dccp_hashinfo.bhash[inet_bhashfn(rover,
136 dccp_hashinfo.bhash_size)];
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700137 spin_lock(&head->lock);
138
139 /* Does not bother with rcv_saddr checks,
140 * because the established check is already
141 * unique enough.
142 */
143 inet_bind_bucket_for_each(tb, node, &head->chain) {
144 if (tb->port == rover) {
145 BUG_TRAP(!hlist_empty(&tb->owners));
146 if (tb->fastreuse >= 0)
147 goto next_port;
148 if (!__dccp_v4_check_established(sk,
149 rover,
150 &tw))
151 goto ok;
152 goto next_port;
153 }
154 }
155
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300156 tb = inet_bind_bucket_create(dccp_hashinfo.bind_bucket_cachep,
157 head, rover);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700158 if (tb == NULL) {
159 spin_unlock(&head->lock);
160 break;
161 }
162 tb->fastreuse = -1;
163 goto ok;
164
165 next_port:
166 spin_unlock(&head->lock);
Stephen Hemminger6df71632005-11-03 16:33:23 -0800167 if (++rover > high)
168 rover = low;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700169 } while (--remaining > 0);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700170
171 local_bh_enable();
172
173 return -EADDRNOTAVAIL;
174
175ok:
176 /* All locks still held and bhs disabled */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700177 inet_bind_hash(sk, tb, rover);
178 if (sk_unhashed(sk)) {
179 inet_sk(sk)->sport = htons(rover);
180 __inet_hash(&dccp_hashinfo, sk, 0);
181 }
182 spin_unlock(&head->lock);
183
184 if (tw != NULL) {
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -0700185 inet_twsk_deschedule(tw, &dccp_death_row);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700186 inet_twsk_put(tw);
187 }
188
189 ret = 0;
190 goto out;
191 }
192
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300193 head = &dccp_hashinfo.bhash[inet_bhashfn(snum,
194 dccp_hashinfo.bhash_size)];
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700195 tb = inet_csk(sk)->icsk_bind_hash;
196 spin_lock_bh(&head->lock);
197 if (sk_head(&tb->owners) == sk && sk->sk_bind_node.next == NULL) {
198 __inet_hash(&dccp_hashinfo, sk, 0);
199 spin_unlock_bh(&head->lock);
200 return 0;
201 } else {
202 spin_unlock(&head->lock);
203 /* No definite answer... Walk to established hash table */
204 ret = __dccp_v4_check_established(sk, snum, NULL);
205out:
206 local_bh_enable();
207 return ret;
208 }
209}
210
211static int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr,
212 int addr_len)
213{
214 struct inet_sock *inet = inet_sk(sk);
215 struct dccp_sock *dp = dccp_sk(sk);
216 const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
217 struct rtable *rt;
218 u32 daddr, nexthop;
219 int tmp;
220 int err;
221
222 dp->dccps_role = DCCP_ROLE_CLIENT;
223
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700224 if (dccp_service_not_initialized(sk))
225 return -EPROTO;
226
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700227 if (addr_len < sizeof(struct sockaddr_in))
228 return -EINVAL;
229
230 if (usin->sin_family != AF_INET)
231 return -EAFNOSUPPORT;
232
233 nexthop = daddr = usin->sin_addr.s_addr;
234 if (inet->opt != NULL && inet->opt->srr) {
235 if (daddr == 0)
236 return -EINVAL;
237 nexthop = inet->opt->faddr;
238 }
239
240 tmp = ip_route_connect(&rt, nexthop, inet->saddr,
241 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
242 IPPROTO_DCCP,
243 inet->sport, usin->sin_port, sk);
244 if (tmp < 0)
245 return tmp;
246
247 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
248 ip_rt_put(rt);
249 return -ENETUNREACH;
250 }
251
252 if (inet->opt == NULL || !inet->opt->srr)
253 daddr = rt->rt_dst;
254
255 if (inet->saddr == 0)
256 inet->saddr = rt->rt_src;
257 inet->rcv_saddr = inet->saddr;
258
259 inet->dport = usin->sin_port;
260 inet->daddr = daddr;
261
262 dp->dccps_ext_header_len = 0;
263 if (inet->opt != NULL)
264 dp->dccps_ext_header_len = inet->opt->optlen;
265 /*
266 * Socket identity is still unknown (sport may be zero).
267 * However we set state to DCCP_REQUESTING and not releasing socket
268 * lock select source port, enter ourselves into the hash tables and
269 * complete initialization after this.
270 */
271 dccp_set_state(sk, DCCP_REQUESTING);
272 err = dccp_v4_hash_connect(sk);
273 if (err != 0)
274 goto failure;
275
276 err = ip_route_newports(&rt, inet->sport, inet->dport, sk);
277 if (err != 0)
278 goto failure;
279
280 /* OK, now commit destination to socket. */
281 sk_setup_caps(sk, &rt->u.dst);
282
283 dp->dccps_gar =
284 dp->dccps_iss = secure_dccp_sequence_number(inet->saddr,
285 inet->daddr,
286 inet->sport,
287 usin->sin_port);
288 dccp_update_gss(sk, dp->dccps_iss);
289
Arnaldo Carvalho de Melo03ace392005-08-21 05:36:45 -0300290 /*
291 * SWL and AWL are initially adjusted so that they are not less than
292 * the initial Sequence Numbers received and sent, respectively:
293 * SWL := max(GSR + 1 - floor(W/4), ISR),
294 * AWL := max(GSS - W' + 1, ISS).
295 * These adjustments MUST be applied only at the beginning of the
296 * connection.
297 */
298 dccp_set_seqno(&dp->dccps_awl, max48(dp->dccps_awl, dp->dccps_iss));
299
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700300 inet->id = dp->dccps_iss ^ jiffies;
301
302 err = dccp_connect(sk);
303 rt = NULL;
304 if (err != 0)
305 goto failure;
306out:
307 return err;
308failure:
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300309 /*
310 * This unhashes the socket and releases the local port, if necessary.
311 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700312 dccp_set_state(sk, DCCP_CLOSED);
313 ip_rt_put(rt);
314 sk->sk_route_caps = 0;
315 inet->dport = 0;
316 goto out;
317}
318
319/*
320 * This routine does path mtu discovery as defined in RFC1191.
321 */
322static inline void dccp_do_pmtu_discovery(struct sock *sk,
323 const struct iphdr *iph,
324 u32 mtu)
325{
326 struct dst_entry *dst;
327 const struct inet_sock *inet = inet_sk(sk);
328 const struct dccp_sock *dp = dccp_sk(sk);
329
330 /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
331 * send out by Linux are always < 576bytes so they should go through
332 * unfragmented).
333 */
334 if (sk->sk_state == DCCP_LISTEN)
335 return;
336
337 /* We don't check in the destentry if pmtu discovery is forbidden
338 * on this route. We just assume that no packet_to_big packets
339 * are send back when pmtu discovery is not active.
340 * There is a small race when the user changes this flag in the
341 * route, but I think that's acceptable.
342 */
343 if ((dst = __sk_dst_check(sk, 0)) == NULL)
344 return;
345
346 dst->ops->update_pmtu(dst, mtu);
347
348 /* Something is about to be wrong... Remember soft error
349 * for the case, if this connection will not able to recover.
350 */
351 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
352 sk->sk_err_soft = EMSGSIZE;
353
354 mtu = dst_mtu(dst);
355
356 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
357 dp->dccps_pmtu_cookie > mtu) {
358 dccp_sync_mss(sk, mtu);
359
360 /*
361 * From: draft-ietf-dccp-spec-11.txt
362 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300363 * DCCP-Sync packets are the best choice for upward
364 * probing, since DCCP-Sync probes do not risk application
365 * data loss.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700366 */
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300367 dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700368 } /* else let the usual retransmit timer handle it */
369}
370
371static void dccp_v4_ctl_send_ack(struct sk_buff *rxskb)
372{
373 int err;
374 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
375 const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) +
376 sizeof(struct dccp_hdr_ext) +
377 sizeof(struct dccp_hdr_ack_bits);
378 struct sk_buff *skb;
379
380 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
381 return;
382
383 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
384 if (skb == NULL)
385 return;
386
387 /* Reserve space for headers. */
388 skb_reserve(skb, MAX_DCCP_HEADER);
389
390 skb->dst = dst_clone(rxskb->dst);
391
392 skb->h.raw = skb_push(skb, dccp_hdr_ack_len);
393 dh = dccp_hdr(skb);
394 memset(dh, 0, dccp_hdr_ack_len);
395
396 /* Build DCCP header and checksum it. */
397 dh->dccph_type = DCCP_PKT_ACK;
398 dh->dccph_sport = rxdh->dccph_dport;
399 dh->dccph_dport = rxdh->dccph_sport;
400 dh->dccph_doff = dccp_hdr_ack_len / 4;
401 dh->dccph_x = 1;
402
403 dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300404 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
405 DCCP_SKB_CB(rxskb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700406
407 bh_lock_sock(dccp_ctl_socket->sk);
408 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300409 rxskb->nh.iph->daddr,
410 rxskb->nh.iph->saddr, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700411 bh_unlock_sock(dccp_ctl_socket->sk);
412
413 if (err == NET_XMIT_CN || err == 0) {
414 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
415 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
416 }
417}
418
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300419static void dccp_v4_reqsk_send_ack(struct sk_buff *skb,
420 struct request_sock *req)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700421{
422 dccp_v4_ctl_send_ack(skb);
423}
424
425static int dccp_v4_send_response(struct sock *sk, struct request_sock *req,
426 struct dst_entry *dst)
427{
428 int err = -1;
429 struct sk_buff *skb;
430
431 /* First, grab a route. */
432
433 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
434 goto out;
435
436 skb = dccp_make_response(sk, dst, req);
437 if (skb != NULL) {
438 const struct inet_request_sock *ireq = inet_rsk(req);
439
Herbert Xu49c5bfa2005-10-18 12:03:28 +1000440 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700441 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
442 ireq->rmt_addr,
443 ireq->opt);
444 if (err == NET_XMIT_CN)
445 err = 0;
446 }
447
448out:
449 dst_release(dst);
450 return err;
451}
452
453/*
454 * This routine is called by the ICMP module when it gets some sort of error
455 * condition. If err < 0 then the socket should be closed and the error
456 * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
457 * After adjustment header points to the first 8 bytes of the tcp header. We
458 * need to find the appropriate port.
459 *
460 * The locking strategy used here is very "optimistic". When someone else
461 * accesses the socket the ICMP is just dropped and for some paths there is no
462 * check at all. A more general error queue to queue errors for later handling
463 * is probably better.
464 */
465void dccp_v4_err(struct sk_buff *skb, u32 info)
466{
467 const struct iphdr *iph = (struct iphdr *)skb->data;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300468 const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data +
469 (iph->ihl << 2));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700470 struct dccp_sock *dp;
471 struct inet_sock *inet;
472 const int type = skb->h.icmph->type;
473 const int code = skb->h.icmph->code;
474 struct sock *sk;
475 __u64 seq;
476 int err;
477
478 if (skb->len < (iph->ihl << 2) + 8) {
479 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
480 return;
481 }
482
483 sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport,
484 iph->saddr, dh->dccph_sport, inet_iif(skb));
485 if (sk == NULL) {
486 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
487 return;
488 }
489
490 if (sk->sk_state == DCCP_TIME_WAIT) {
491 inet_twsk_put((struct inet_timewait_sock *)sk);
492 return;
493 }
494
495 bh_lock_sock(sk);
496 /* If too many ICMPs get dropped on busy
497 * servers this needs to be solved differently.
498 */
499 if (sock_owned_by_user(sk))
500 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
501
502 if (sk->sk_state == DCCP_CLOSED)
503 goto out;
504
505 dp = dccp_sk(sk);
506 seq = dccp_hdr_seq(skb);
507 if (sk->sk_state != DCCP_LISTEN &&
508 !between48(seq, dp->dccps_swl, dp->dccps_swh)) {
509 NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS);
510 goto out;
511 }
512
513 switch (type) {
514 case ICMP_SOURCE_QUENCH:
515 /* Just silently ignore these. */
516 goto out;
517 case ICMP_PARAMETERPROB:
518 err = EPROTO;
519 break;
520 case ICMP_DEST_UNREACH:
521 if (code > NR_ICMP_UNREACH)
522 goto out;
523
524 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
525 if (!sock_owned_by_user(sk))
526 dccp_do_pmtu_discovery(sk, iph, info);
527 goto out;
528 }
529
530 err = icmp_err_convert[code].errno;
531 break;
532 case ICMP_TIME_EXCEEDED:
533 err = EHOSTUNREACH;
534 break;
535 default:
536 goto out;
537 }
538
539 switch (sk->sk_state) {
540 struct request_sock *req , **prev;
541 case DCCP_LISTEN:
542 if (sock_owned_by_user(sk))
543 goto out;
544 req = inet_csk_search_req(sk, &prev, dh->dccph_dport,
545 iph->daddr, iph->saddr);
546 if (!req)
547 goto out;
548
549 /*
550 * ICMPs are not backlogged, hence we cannot get an established
551 * socket here.
552 */
553 BUG_TRAP(!req->sk);
554
555 if (seq != dccp_rsk(req)->dreq_iss) {
556 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
557 goto out;
558 }
559 /*
560 * Still in RESPOND, just remove it silently.
561 * There is no good way to pass the error to the newly
562 * created socket, and POSIX does not want network
563 * errors returned from accept().
564 */
565 inet_csk_reqsk_queue_drop(sk, req, prev);
566 goto out;
567
568 case DCCP_REQUESTING:
569 case DCCP_RESPOND:
570 if (!sock_owned_by_user(sk)) {
571 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
572 sk->sk_err = err;
573
574 sk->sk_error_report(sk);
575
576 dccp_done(sk);
577 } else
578 sk->sk_err_soft = err;
579 goto out;
580 }
581
582 /* If we've already connected we will keep trying
583 * until we time out, or the user gives up.
584 *
585 * rfc1122 4.2.3.9 allows to consider as hard errors
586 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
587 * but it is obsoleted by pmtu discovery).
588 *
589 * Note, that in modern internet, where routing is unreliable
590 * and in each dark corner broken firewalls sit, sending random
591 * errors ordered by their masters even this two messages finally lose
592 * their original sense (even Linux sends invalid PORT_UNREACHs)
593 *
594 * Now we are in compliance with RFCs.
595 * --ANK (980905)
596 */
597
598 inet = inet_sk(sk);
599 if (!sock_owned_by_user(sk) && inet->recverr) {
600 sk->sk_err = err;
601 sk->sk_error_report(sk);
602 } else /* Only an error on timeout */
603 sk->sk_err_soft = err;
604out:
605 bh_unlock_sock(sk);
606 sock_put(sk);
607}
608
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700609int dccp_v4_send_reset(struct sock *sk, enum dccp_reset_codes code)
610{
611 struct sk_buff *skb;
612 /*
613 * FIXME: what if rebuild_header fails?
614 * Should we be doing a rebuild_header here?
615 */
616 int err = inet_sk_rebuild_header(sk);
617
618 if (err != 0)
619 return err;
620
621 skb = dccp_make_reset(sk, sk->sk_dst_cache, code);
622 if (skb != NULL) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700623 const struct inet_sock *inet = inet_sk(sk);
624
Herbert Xu49c5bfa2005-10-18 12:03:28 +1000625 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700626 err = ip_build_and_send_pkt(skb, sk,
627 inet->saddr, inet->daddr, NULL);
628 if (err == NET_XMIT_CN)
629 err = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700630 }
631
632 return err;
633}
634
635static inline u64 dccp_v4_init_sequence(const struct sock *sk,
636 const struct sk_buff *skb)
637{
638 return secure_dccp_sequence_number(skb->nh.iph->daddr,
639 skb->nh.iph->saddr,
640 dccp_hdr(skb)->dccph_dport,
641 dccp_hdr(skb)->dccph_sport);
642}
643
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700644static inline int dccp_bad_service_code(const struct sock *sk,
645 const __u32 service)
646{
647 const struct dccp_sock *dp = dccp_sk(sk);
648
649 if (dp->dccps_service == service)
650 return 0;
651 return !dccp_list_has_service(dp->dccps_service_list, service);
652}
653
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700654int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
655{
656 struct inet_request_sock *ireq;
657 struct dccp_sock dp;
658 struct request_sock *req;
659 struct dccp_request_sock *dreq;
660 const __u32 saddr = skb->nh.iph->saddr;
661 const __u32 daddr = skb->nh.iph->daddr;
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700662 const __u32 service = dccp_hdr_request(skb)->dccph_req_service;
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700663 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
664 __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700665 struct dst_entry *dst = NULL;
666
667 /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
668 if (((struct rtable *)skb->dst)->rt_flags &
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700669 (RTCF_BROADCAST | RTCF_MULTICAST)) {
670 reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700671 goto drop;
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700672 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700673
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700674 if (dccp_bad_service_code(sk, service)) {
675 reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
676 goto drop;
677 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700678 /*
679 * TW buckets are converted to open requests without
680 * limitations, they conserve resources and peer is
681 * evidently real one.
682 */
683 if (inet_csk_reqsk_queue_is_full(sk))
684 goto drop;
685
686 /*
687 * Accept backlog is full. If we have already queued enough
688 * of warm entries in syn queue, drop request. It is better than
689 * clogging syn queue with openreqs with exponentially increasing
690 * timeout.
691 */
692 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
693 goto drop;
694
695 req = reqsk_alloc(sk->sk_prot->rsk_prot);
696 if (req == NULL)
697 goto drop;
698
699 /* FIXME: process options */
700
701 dccp_openreq_init(req, &dp, skb);
702
703 ireq = inet_rsk(req);
704 ireq->loc_addr = daddr;
705 ireq->rmt_addr = saddr;
706 /* FIXME: Merge Aristeu's option parsing code when ready */
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300707 req->rcv_wnd = 100; /* Fake, option parsing will get the
708 right value */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700709 ireq->opt = NULL;
710
711 /*
712 * Step 3: Process LISTEN state
713 *
714 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
715 *
716 * In fact we defer setting S.GSR, S.SWL, S.SWH to
717 * dccp_create_openreq_child.
718 */
719 dreq = dccp_rsk(req);
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700720 dreq->dreq_isr = dcb->dccpd_seq;
721 dreq->dreq_iss = dccp_v4_init_sequence(sk, skb);
722 dreq->dreq_service = service;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700723
724 if (dccp_v4_send_response(sk, req, dst))
725 goto drop_and_free;
726
727 inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
728 return 0;
729
730drop_and_free:
731 /*
732 * FIXME: should be reqsk_free after implementing req->rsk_ops
733 */
734 __reqsk_free(req);
735drop:
736 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700737 dcb->dccpd_reset_code = reset_code;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700738 return -1;
739}
740
741/*
742 * The three way handshake has completed - we got a valid ACK or DATAACK -
743 * now create the new socket.
744 *
745 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
746 */
747struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
748 struct request_sock *req,
749 struct dst_entry *dst)
750{
751 struct inet_request_sock *ireq;
752 struct inet_sock *newinet;
753 struct dccp_sock *newdp;
754 struct sock *newsk;
755
756 if (sk_acceptq_is_full(sk))
757 goto exit_overflow;
758
759 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
760 goto exit;
761
762 newsk = dccp_create_openreq_child(sk, req, skb);
763 if (newsk == NULL)
764 goto exit;
765
766 sk_setup_caps(newsk, dst);
767
768 newdp = dccp_sk(newsk);
769 newinet = inet_sk(newsk);
770 ireq = inet_rsk(req);
771 newinet->daddr = ireq->rmt_addr;
772 newinet->rcv_saddr = ireq->loc_addr;
773 newinet->saddr = ireq->loc_addr;
774 newinet->opt = ireq->opt;
775 ireq->opt = NULL;
776 newinet->mc_index = inet_iif(skb);
777 newinet->mc_ttl = skb->nh.iph->ttl;
778 newinet->id = jiffies;
779
780 dccp_sync_mss(newsk, dst_mtu(dst));
781
782 __inet_hash(&dccp_hashinfo, newsk, 0);
783 __inet_inherit_port(&dccp_hashinfo, sk, newsk);
784
785 return newsk;
786
787exit_overflow:
788 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
789exit:
790 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
791 dst_release(dst);
792 return NULL;
793}
794
795static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
796{
797 const struct dccp_hdr *dh = dccp_hdr(skb);
798 const struct iphdr *iph = skb->nh.iph;
799 struct sock *nsk;
800 struct request_sock **prev;
801 /* Find possible connection requests. */
802 struct request_sock *req = inet_csk_search_req(sk, &prev,
803 dh->dccph_sport,
804 iph->saddr, iph->daddr);
805 if (req != NULL)
806 return dccp_check_req(sk, skb, req, prev);
807
808 nsk = __inet_lookup_established(&dccp_hashinfo,
809 iph->saddr, dh->dccph_sport,
810 iph->daddr, ntohs(dh->dccph_dport),
811 inet_iif(skb));
812 if (nsk != NULL) {
813 if (nsk->sk_state != DCCP_TIME_WAIT) {
814 bh_lock_sock(nsk);
815 return nsk;
816 }
817 inet_twsk_put((struct inet_timewait_sock *)nsk);
818 return NULL;
819 }
820
821 return sk;
822}
823
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300824int dccp_v4_checksum(const struct sk_buff *skb, const u32 saddr,
825 const u32 daddr)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700826{
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700827 const struct dccp_hdr* dh = dccp_hdr(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700828 int checksum_len;
829 u32 tmp;
830
831 if (dh->dccph_cscov == 0)
832 checksum_len = skb->len;
833 else {
834 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300835 checksum_len = checksum_len < skb->len ? checksum_len :
836 skb->len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700837 }
838
839 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300840 return csum_tcpudp_magic(saddr, daddr, checksum_len,
841 IPPROTO_DCCP, tmp);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700842}
843
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700844static int dccp_v4_verify_checksum(struct sk_buff *skb,
845 const u32 saddr, const u32 daddr)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700846{
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700847 struct dccp_hdr *dh = dccp_hdr(skb);
848 int checksum_len;
849 u32 tmp;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700850
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700851 if (dh->dccph_cscov == 0)
852 checksum_len = skb->len;
853 else {
854 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300855 checksum_len = checksum_len < skb->len ? checksum_len :
856 skb->len;
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700857 }
858 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300859 return csum_tcpudp_magic(saddr, daddr, checksum_len,
860 IPPROTO_DCCP, tmp) == 0 ? 0 : -1;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700861}
862
863static struct dst_entry* dccp_v4_route_skb(struct sock *sk,
864 struct sk_buff *skb)
865{
866 struct rtable *rt;
867 struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif,
868 .nl_u = { .ip4_u =
869 { .daddr = skb->nh.iph->saddr,
870 .saddr = skb->nh.iph->daddr,
871 .tos = RT_CONN_FLAGS(sk) } },
872 .proto = sk->sk_protocol,
873 .uli_u = { .ports =
874 { .sport = dccp_hdr(skb)->dccph_dport,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300875 .dport = dccp_hdr(skb)->dccph_sport }
876 }
877 };
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700878
879 if (ip_route_output_flow(&rt, &fl, sk, 0)) {
880 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
881 return NULL;
882 }
883
884 return &rt->u.dst;
885}
886
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300887static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700888{
889 int err;
890 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
891 const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
892 sizeof(struct dccp_hdr_ext) +
893 sizeof(struct dccp_hdr_reset);
894 struct sk_buff *skb;
895 struct dst_entry *dst;
Arnaldo Carvalho de Melo2807d4f2005-08-21 05:33:48 -0300896 u64 seqno;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700897
898 /* Never send a reset in response to a reset. */
899 if (rxdh->dccph_type == DCCP_PKT_RESET)
900 return;
901
902 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
903 return;
904
905 dst = dccp_v4_route_skb(dccp_ctl_socket->sk, rxskb);
906 if (dst == NULL)
907 return;
908
909 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
910 if (skb == NULL)
911 goto out;
912
913 /* Reserve space for headers. */
914 skb_reserve(skb, MAX_DCCP_HEADER);
915 skb->dst = dst_clone(dst);
916
917 skb->h.raw = skb_push(skb, dccp_hdr_reset_len);
918 dh = dccp_hdr(skb);
919 memset(dh, 0, dccp_hdr_reset_len);
920
921 /* Build DCCP header and checksum it. */
922 dh->dccph_type = DCCP_PKT_RESET;
923 dh->dccph_sport = rxdh->dccph_dport;
924 dh->dccph_dport = rxdh->dccph_sport;
925 dh->dccph_doff = dccp_hdr_reset_len / 4;
926 dh->dccph_x = 1;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300927 dccp_hdr_reset(skb)->dccph_reset_code =
928 DCCP_SKB_CB(rxskb)->dccpd_reset_code;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700929
Arnaldo Carvalho de Melo2807d4f2005-08-21 05:33:48 -0300930 /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
931 seqno = 0;
932 if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
933 dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
934
935 dccp_hdr_set_seq(dh, seqno);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300936 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
937 DCCP_SKB_CB(rxskb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700938
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700939 dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr,
940 rxskb->nh.iph->daddr);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700941
942 bh_lock_sock(dccp_ctl_socket->sk);
943 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300944 rxskb->nh.iph->daddr,
945 rxskb->nh.iph->saddr, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700946 bh_unlock_sock(dccp_ctl_socket->sk);
947
948 if (err == NET_XMIT_CN || err == 0) {
949 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
950 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
951 }
952out:
953 dst_release(dst);
954}
955
956int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
957{
958 struct dccp_hdr *dh = dccp_hdr(skb);
959
960 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
961 if (dccp_rcv_established(sk, skb, dh, skb->len))
962 goto reset;
963 return 0;
964 }
965
966 /*
967 * Step 3: Process LISTEN state
968 * If S.state == LISTEN,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300969 * If P.type == Request or P contains a valid Init Cookie
970 * option,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700971 * * Must scan the packet's options to check for an Init
972 * Cookie. Only the Init Cookie is processed here,
973 * however; other options are processed in Step 8. This
974 * scan need only be performed if the endpoint uses Init
975 * Cookies *
976 * * Generate a new socket and switch to that socket *
977 * Set S := new socket for this port pair
978 * S.state = RESPOND
979 * Choose S.ISS (initial seqno) or set from Init Cookie
980 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
981 * Continue with S.state == RESPOND
982 * * A Response packet will be generated in Step 11 *
983 * Otherwise,
984 * Generate Reset(No Connection) unless P.type == Reset
985 * Drop packet and return
986 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300987 * NOTE: the check for the packet types is done in
988 * dccp_rcv_state_process
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700989 */
990 if (sk->sk_state == DCCP_LISTEN) {
991 struct sock *nsk = dccp_v4_hnd_req(sk, skb);
992
993 if (nsk == NULL)
994 goto discard;
995
996 if (nsk != sk) {
997 if (dccp_child_process(sk, nsk, skb))
998 goto reset;
999 return 0;
1000 }
1001 }
1002
1003 if (dccp_rcv_state_process(sk, skb, dh, skb->len))
1004 goto reset;
1005 return 0;
1006
1007reset:
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001008 dccp_v4_ctl_send_reset(skb);
1009discard:
1010 kfree_skb(skb);
1011 return 0;
1012}
1013
1014static inline int dccp_invalid_packet(struct sk_buff *skb)
1015{
1016 const struct dccp_hdr *dh;
1017
1018 if (skb->pkt_type != PACKET_HOST)
1019 return 1;
1020
1021 if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001022 LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001023 return 1;
1024 }
1025
1026 dh = dccp_hdr(skb);
1027
1028 /* If the packet type is not understood, drop packet and return */
1029 if (dh->dccph_type >= DCCP_PKT_INVALID) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001030 LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001031 return 1;
1032 }
1033
1034 /*
1035 * If P.Data Offset is too small for packet type, or too large for
1036 * packet, drop packet and return
1037 */
1038 if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001039 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
1040 "too small 1\n",
1041 dh->dccph_doff);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001042 return 1;
1043 }
1044
1045 if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001046 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
1047 "too small 2\n",
1048 dh->dccph_doff);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001049 return 1;
1050 }
1051
1052 dh = dccp_hdr(skb);
1053
1054 /*
1055 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
1056 * has short sequence numbers), drop packet and return
1057 */
1058 if (dh->dccph_x == 0 &&
1059 dh->dccph_type != DCCP_PKT_DATA &&
1060 dh->dccph_type != DCCP_PKT_ACK &&
1061 dh->dccph_type != DCCP_PKT_DATAACK) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001062 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack "
1063 "nor DataAck and P.X == 0\n",
1064 dccp_packet_name(dh->dccph_type));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001065 return 1;
1066 }
1067
1068 /* If the header checksum is incorrect, drop packet and return */
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -07001069 if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr,
1070 skb->nh.iph->daddr) < 0) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001071 LIMIT_NETDEBUG(KERN_WARNING "DCCP: header checksum is "
1072 "incorrect\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001073 return 1;
1074 }
1075
1076 return 0;
1077}
1078
1079/* this is called when real data arrives */
1080int dccp_v4_rcv(struct sk_buff *skb)
1081{
1082 const struct dccp_hdr *dh;
1083 struct sock *sk;
1084 int rc;
1085
1086 /* Step 1: Check header basics: */
1087
1088 if (dccp_invalid_packet(skb))
1089 goto discard_it;
1090
1091 dh = dccp_hdr(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001092
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001093 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
1094 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
1095
1096 dccp_pr_debug("%8.8s "
1097 "src=%u.%u.%u.%u@%-5d "
1098 "dst=%u.%u.%u.%u@%-5d seq=%llu",
1099 dccp_packet_name(dh->dccph_type),
1100 NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport),
1101 NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport),
David S. Millerf6ccf552005-08-09 20:27:14 -07001102 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001103
1104 if (dccp_packet_without_ack(skb)) {
1105 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
1106 dccp_pr_debug_cat("\n");
1107 } else {
1108 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
David S. Millerf6ccf552005-08-09 20:27:14 -07001109 dccp_pr_debug_cat(", ack=%llu\n",
1110 (unsigned long long)
1111 DCCP_SKB_CB(skb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001112 }
1113
1114 /* Step 2:
1115 * Look up flow ID in table and get corresponding socket */
1116 sk = __inet_lookup(&dccp_hashinfo,
1117 skb->nh.iph->saddr, dh->dccph_sport,
1118 skb->nh.iph->daddr, ntohs(dh->dccph_dport),
1119 inet_iif(skb));
1120
1121 /*
1122 * Step 2:
1123 * If no socket ...
1124 * Generate Reset(No Connection) unless P.type == Reset
1125 * Drop packet and return
1126 */
1127 if (sk == NULL) {
1128 dccp_pr_debug("failed to look up flow ID in table and "
1129 "get corresponding socket\n");
1130 goto no_dccp_socket;
1131 }
1132
1133 /*
1134 * Step 2:
1135 * ... or S.state == TIMEWAIT,
1136 * Generate Reset(No Connection) unless P.type == Reset
1137 * Drop packet and return
1138 */
1139
1140 if (sk->sk_state == DCCP_TIME_WAIT) {
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -07001141 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
1142 "do_time_wait\n");
1143 goto do_time_wait;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001144 }
1145
1146 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
1147 dccp_pr_debug("xfrm4_policy_check failed\n");
1148 goto discard_and_relse;
1149 }
1150
1151 if (sk_filter(sk, skb, 0)) {
1152 dccp_pr_debug("sk_filter failed\n");
1153 goto discard_and_relse;
1154 }
1155
1156 skb->dev = NULL;
1157
1158 bh_lock_sock(sk);
1159 rc = 0;
1160 if (!sock_owned_by_user(sk))
1161 rc = dccp_v4_do_rcv(sk, skb);
1162 else
1163 sk_add_backlog(sk, skb);
1164 bh_unlock_sock(sk);
1165
1166 sock_put(sk);
1167 return rc;
1168
1169no_dccp_socket:
1170 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1171 goto discard_it;
1172 /*
1173 * Step 2:
1174 * Generate Reset(No Connection) unless P.type == Reset
1175 * Drop packet and return
1176 */
1177 if (dh->dccph_type != DCCP_PKT_RESET) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001178 DCCP_SKB_CB(skb)->dccpd_reset_code =
1179 DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001180 dccp_v4_ctl_send_reset(skb);
1181 }
1182
1183discard_it:
1184 /* Discard frame. */
1185 kfree_skb(skb);
1186 return 0;
1187
1188discard_and_relse:
1189 sock_put(sk);
1190 goto discard_it;
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -07001191
1192do_time_wait:
1193 inet_twsk_put((struct inet_timewait_sock *)sk);
1194 goto no_dccp_socket;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001195}
1196
1197static int dccp_v4_init_sock(struct sock *sk)
1198{
1199 struct dccp_sock *dp = dccp_sk(sk);
1200 static int dccp_ctl_socket_init = 1;
1201
1202 dccp_options_init(&dp->dccps_options);
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -03001203 do_gettimeofday(&dp->dccps_epoch);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001204
1205 if (dp->dccps_options.dccpo_send_ack_vector) {
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001206 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(DCCP_MAX_ACKVEC_LEN,
1207 GFP_KERNEL);
1208 if (dp->dccps_hc_rx_ackvec == NULL)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001209 return -ENOMEM;
1210 }
1211
1212 /*
1213 * FIXME: We're hardcoding the CCID, and doing this at this point makes
1214 * the listening (master) sock get CCID control blocks, which is not
1215 * necessary, but for now, to not mess with the test userspace apps,
1216 * lets leave it here, later the real solution is to do this in a
1217 * setsockopt(CCIDs-I-want/accept). -acme
1218 */
1219 if (likely(!dccp_ctl_socket_init)) {
Arnaldo Carvalho de Melo561713c2005-09-18 00:18:52 -07001220 dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_rx_ccid,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001221 sk);
Arnaldo Carvalho de Melo561713c2005-09-18 00:18:52 -07001222 dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_tx_ccid,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001223 sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001224 if (dp->dccps_hc_rx_ccid == NULL ||
1225 dp->dccps_hc_tx_ccid == NULL) {
1226 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1227 ccid_exit(dp->dccps_hc_tx_ccid, sk);
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001228 if (dp->dccps_options.dccpo_send_ack_vector) {
1229 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1230 dp->dccps_hc_rx_ackvec = NULL;
1231 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001232 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1233 return -ENOMEM;
1234 }
1235 } else
1236 dccp_ctl_socket_init = 0;
1237
1238 dccp_init_xmit_timers(sk);
Arnaldo Carvalho de Melo0b4e03b2005-08-09 20:31:11 -07001239 inet_csk(sk)->icsk_rto = DCCP_TIMEOUT_INIT;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001240 sk->sk_state = DCCP_CLOSED;
Arnaldo Carvalho de Meloc530cfb2005-08-29 02:15:54 -03001241 sk->sk_write_space = dccp_write_space;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001242 dp->dccps_mss_cache = 536;
1243 dp->dccps_role = DCCP_ROLE_UNDEFINED;
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -07001244 dp->dccps_service = DCCP_SERVICE_INVALID_VALUE;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001245
1246 return 0;
1247}
1248
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -03001249static int dccp_v4_destroy_sock(struct sock *sk)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001250{
1251 struct dccp_sock *dp = dccp_sk(sk);
1252
1253 /*
Ian McDonald4c7e6892005-12-21 19:02:39 -08001254 * DCCP doesn't use sk_write_queue, just sk_send_head
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001255 * for retransmissions
1256 */
1257 if (sk->sk_send_head != NULL) {
1258 kfree_skb(sk->sk_send_head);
1259 sk->sk_send_head = NULL;
1260 }
1261
1262 /* Clean up a referenced DCCP bind bucket. */
1263 if (inet_csk(sk)->icsk_bind_hash != NULL)
1264 inet_put_port(&dccp_hashinfo, sk);
1265
Jesper Juhla51482b2005-11-08 09:41:34 -08001266 kfree(dp->dccps_service_list);
1267 dp->dccps_service_list = NULL;
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -07001268
Arnaldo Carvalho de Melo8efa5442005-08-23 21:54:00 -07001269 ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
1270 ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001271 if (dp->dccps_options.dccpo_send_ack_vector) {
1272 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1273 dp->dccps_hc_rx_ackvec = NULL;
1274 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001275 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1276 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1277 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1278
1279 return 0;
1280}
1281
1282static void dccp_v4_reqsk_destructor(struct request_sock *req)
1283{
1284 kfree(inet_rsk(req)->opt);
1285}
1286
1287static struct request_sock_ops dccp_request_sock_ops = {
1288 .family = PF_INET,
1289 .obj_size = sizeof(struct dccp_request_sock),
1290 .rtx_syn_ack = dccp_v4_send_response,
1291 .send_ack = dccp_v4_reqsk_send_ack,
1292 .destructor = dccp_v4_reqsk_destructor,
1293 .send_reset = dccp_v4_ctl_send_reset,
1294};
1295
1296struct proto dccp_v4_prot = {
1297 .name = "DCCP",
1298 .owner = THIS_MODULE,
1299 .close = dccp_close,
1300 .connect = dccp_v4_connect,
1301 .disconnect = dccp_disconnect,
1302 .ioctl = dccp_ioctl,
1303 .init = dccp_v4_init_sock,
1304 .setsockopt = dccp_setsockopt,
1305 .getsockopt = dccp_getsockopt,
1306 .sendmsg = dccp_sendmsg,
1307 .recvmsg = dccp_recvmsg,
1308 .backlog_rcv = dccp_v4_do_rcv,
1309 .hash = dccp_v4_hash,
1310 .unhash = dccp_v4_unhash,
1311 .accept = inet_csk_accept,
1312 .get_port = dccp_v4_get_port,
1313 .shutdown = dccp_shutdown,
1314 .destroy = dccp_v4_destroy_sock,
1315 .orphan_count = &dccp_orphan_count,
1316 .max_header = MAX_DCCP_HEADER,
1317 .obj_size = sizeof(struct dccp_sock),
1318 .rsk_prot = &dccp_request_sock_ops,
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -07001319 .twsk_obj_size = sizeof(struct inet_timewait_sock),
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001320};