blob: 82434e4a42df1d8a031e0c98675f2d7e51089efe [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 .portalloc_lock = SPIN_LOCK_UNLOCKED,
35 .port_rover = 1024 - 1,
36};
37
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -030038EXPORT_SYMBOL_GPL(dccp_hashinfo);
39
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070040static int dccp_v4_get_port(struct sock *sk, const unsigned short snum)
41{
42 return inet_csk_get_port(&dccp_hashinfo, sk, snum);
43}
44
45static void dccp_v4_hash(struct sock *sk)
46{
47 inet_hash(&dccp_hashinfo, sk);
48}
49
50static void dccp_v4_unhash(struct sock *sk)
51{
52 inet_unhash(&dccp_hashinfo, sk);
53}
54
55/* called with local bh disabled */
56static int __dccp_v4_check_established(struct sock *sk, const __u16 lport,
57 struct inet_timewait_sock **twp)
58{
59 struct inet_sock *inet = inet_sk(sk);
60 const u32 daddr = inet->rcv_saddr;
61 const u32 saddr = inet->daddr;
62 const int dif = sk->sk_bound_dev_if;
63 INET_ADDR_COOKIE(acookie, saddr, daddr)
64 const __u32 ports = INET_COMBINED_PORTS(inet->dport, lport);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -030065 const int hash = inet_ehashfn(daddr, lport, saddr, inet->dport,
66 dccp_hashinfo.ehash_size);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070067 struct inet_ehash_bucket *head = &dccp_hashinfo.ehash[hash];
68 const struct sock *sk2;
69 const struct hlist_node *node;
70 struct inet_timewait_sock *tw;
71
72 write_lock(&head->lock);
73
74 /* Check TIME-WAIT sockets first. */
75 sk_for_each(sk2, node, &(head + dccp_hashinfo.ehash_size)->chain) {
76 tw = inet_twsk(sk2);
77
78 if (INET_TW_MATCH(sk2, acookie, saddr, daddr, ports, dif))
79 goto not_unique;
80 }
81 tw = NULL;
82
83 /* And established part... */
84 sk_for_each(sk2, node, &head->chain) {
85 if (INET_MATCH(sk2, acookie, saddr, daddr, ports, dif))
86 goto not_unique;
87 }
88
89 /* Must record num and sport now. Otherwise we will see
90 * in hash table socket with a funny identity. */
91 inet->num = lport;
92 inet->sport = htons(lport);
93 sk->sk_hashent = hash;
94 BUG_TRAP(sk_unhashed(sk));
95 __sk_add_node(sk, &head->chain);
96 sock_prot_inc_use(sk->sk_prot);
97 write_unlock(&head->lock);
98
99 if (twp != NULL) {
100 *twp = tw;
101 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
102 } else if (tw != NULL) {
103 /* Silly. Should hash-dance instead... */
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -0700104 inet_twsk_deschedule(tw, &dccp_death_row);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700105 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
106
107 inet_twsk_put(tw);
108 }
109
110 return 0;
111
112not_unique:
113 write_unlock(&head->lock);
114 return -EADDRNOTAVAIL;
115}
116
117/*
118 * Bind a port for a connect operation and hash it.
119 */
120static int dccp_v4_hash_connect(struct sock *sk)
121{
122 const unsigned short snum = inet_sk(sk)->num;
123 struct inet_bind_hashbucket *head;
124 struct inet_bind_bucket *tb;
125 int ret;
126
127 if (snum == 0) {
128 int rover;
129 int low = sysctl_local_port_range[0];
130 int high = sysctl_local_port_range[1];
131 int remaining = (high - low) + 1;
132 struct hlist_node *node;
133 struct inet_timewait_sock *tw = NULL;
134
135 local_bh_disable();
136
137 /* TODO. Actually it is not so bad idea to remove
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300138 * dccp_hashinfo.portalloc_lock before next submission to
139 * Linus.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700140 * As soon as we touch this place at all it is time to think.
141 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300142 * Now it protects single _advisory_ variable
143 * dccp_hashinfo.port_rover, hence it is mostly useless.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700144 * Code will work nicely if we just delete it, but
145 * I am afraid in contented case it will work not better or
146 * even worse: another cpu just will hit the same bucket
147 * and spin there.
148 * So some cpu salt could remove both contention and
149 * memory pingpong. Any ideas how to do this in a nice way?
150 */
151 spin_lock(&dccp_hashinfo.portalloc_lock);
152 rover = dccp_hashinfo.port_rover;
153
154 do {
155 rover++;
156 if ((rover < low) || (rover > high))
157 rover = low;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300158 head = &dccp_hashinfo.bhash[inet_bhashfn(rover,
159 dccp_hashinfo.bhash_size)];
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700160 spin_lock(&head->lock);
161
162 /* Does not bother with rcv_saddr checks,
163 * because the established check is already
164 * unique enough.
165 */
166 inet_bind_bucket_for_each(tb, node, &head->chain) {
167 if (tb->port == rover) {
168 BUG_TRAP(!hlist_empty(&tb->owners));
169 if (tb->fastreuse >= 0)
170 goto next_port;
171 if (!__dccp_v4_check_established(sk,
172 rover,
173 &tw))
174 goto ok;
175 goto next_port;
176 }
177 }
178
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300179 tb = inet_bind_bucket_create(dccp_hashinfo.bind_bucket_cachep,
180 head, rover);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700181 if (tb == NULL) {
182 spin_unlock(&head->lock);
183 break;
184 }
185 tb->fastreuse = -1;
186 goto ok;
187
188 next_port:
189 spin_unlock(&head->lock);
190 } while (--remaining > 0);
191 dccp_hashinfo.port_rover = rover;
192 spin_unlock(&dccp_hashinfo.portalloc_lock);
193
194 local_bh_enable();
195
196 return -EADDRNOTAVAIL;
197
198ok:
199 /* All locks still held and bhs disabled */
200 dccp_hashinfo.port_rover = rover;
201 spin_unlock(&dccp_hashinfo.portalloc_lock);
202
203 inet_bind_hash(sk, tb, rover);
204 if (sk_unhashed(sk)) {
205 inet_sk(sk)->sport = htons(rover);
206 __inet_hash(&dccp_hashinfo, sk, 0);
207 }
208 spin_unlock(&head->lock);
209
210 if (tw != NULL) {
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -0700211 inet_twsk_deschedule(tw, &dccp_death_row);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700212 inet_twsk_put(tw);
213 }
214
215 ret = 0;
216 goto out;
217 }
218
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300219 head = &dccp_hashinfo.bhash[inet_bhashfn(snum,
220 dccp_hashinfo.bhash_size)];
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700221 tb = inet_csk(sk)->icsk_bind_hash;
222 spin_lock_bh(&head->lock);
223 if (sk_head(&tb->owners) == sk && sk->sk_bind_node.next == NULL) {
224 __inet_hash(&dccp_hashinfo, sk, 0);
225 spin_unlock_bh(&head->lock);
226 return 0;
227 } else {
228 spin_unlock(&head->lock);
229 /* No definite answer... Walk to established hash table */
230 ret = __dccp_v4_check_established(sk, snum, NULL);
231out:
232 local_bh_enable();
233 return ret;
234 }
235}
236
237static int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr,
238 int addr_len)
239{
240 struct inet_sock *inet = inet_sk(sk);
241 struct dccp_sock *dp = dccp_sk(sk);
242 const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
243 struct rtable *rt;
244 u32 daddr, nexthop;
245 int tmp;
246 int err;
247
248 dp->dccps_role = DCCP_ROLE_CLIENT;
249
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700250 if (dccp_service_not_initialized(sk))
251 return -EPROTO;
252
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700253 if (addr_len < sizeof(struct sockaddr_in))
254 return -EINVAL;
255
256 if (usin->sin_family != AF_INET)
257 return -EAFNOSUPPORT;
258
259 nexthop = daddr = usin->sin_addr.s_addr;
260 if (inet->opt != NULL && inet->opt->srr) {
261 if (daddr == 0)
262 return -EINVAL;
263 nexthop = inet->opt->faddr;
264 }
265
266 tmp = ip_route_connect(&rt, nexthop, inet->saddr,
267 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
268 IPPROTO_DCCP,
269 inet->sport, usin->sin_port, sk);
270 if (tmp < 0)
271 return tmp;
272
273 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
274 ip_rt_put(rt);
275 return -ENETUNREACH;
276 }
277
278 if (inet->opt == NULL || !inet->opt->srr)
279 daddr = rt->rt_dst;
280
281 if (inet->saddr == 0)
282 inet->saddr = rt->rt_src;
283 inet->rcv_saddr = inet->saddr;
284
285 inet->dport = usin->sin_port;
286 inet->daddr = daddr;
287
288 dp->dccps_ext_header_len = 0;
289 if (inet->opt != NULL)
290 dp->dccps_ext_header_len = inet->opt->optlen;
291 /*
292 * Socket identity is still unknown (sport may be zero).
293 * However we set state to DCCP_REQUESTING and not releasing socket
294 * lock select source port, enter ourselves into the hash tables and
295 * complete initialization after this.
296 */
297 dccp_set_state(sk, DCCP_REQUESTING);
298 err = dccp_v4_hash_connect(sk);
299 if (err != 0)
300 goto failure;
301
302 err = ip_route_newports(&rt, inet->sport, inet->dport, sk);
303 if (err != 0)
304 goto failure;
305
306 /* OK, now commit destination to socket. */
307 sk_setup_caps(sk, &rt->u.dst);
308
309 dp->dccps_gar =
310 dp->dccps_iss = secure_dccp_sequence_number(inet->saddr,
311 inet->daddr,
312 inet->sport,
313 usin->sin_port);
314 dccp_update_gss(sk, dp->dccps_iss);
315
Arnaldo Carvalho de Melo03ace392005-08-21 05:36:45 -0300316 /*
317 * SWL and AWL are initially adjusted so that they are not less than
318 * the initial Sequence Numbers received and sent, respectively:
319 * SWL := max(GSR + 1 - floor(W/4), ISR),
320 * AWL := max(GSS - W' + 1, ISS).
321 * These adjustments MUST be applied only at the beginning of the
322 * connection.
323 */
324 dccp_set_seqno(&dp->dccps_awl, max48(dp->dccps_awl, dp->dccps_iss));
325
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700326 inet->id = dp->dccps_iss ^ jiffies;
327
328 err = dccp_connect(sk);
329 rt = NULL;
330 if (err != 0)
331 goto failure;
332out:
333 return err;
334failure:
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300335 /*
336 * This unhashes the socket and releases the local port, if necessary.
337 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700338 dccp_set_state(sk, DCCP_CLOSED);
339 ip_rt_put(rt);
340 sk->sk_route_caps = 0;
341 inet->dport = 0;
342 goto out;
343}
344
345/*
346 * This routine does path mtu discovery as defined in RFC1191.
347 */
348static inline void dccp_do_pmtu_discovery(struct sock *sk,
349 const struct iphdr *iph,
350 u32 mtu)
351{
352 struct dst_entry *dst;
353 const struct inet_sock *inet = inet_sk(sk);
354 const struct dccp_sock *dp = dccp_sk(sk);
355
356 /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
357 * send out by Linux are always < 576bytes so they should go through
358 * unfragmented).
359 */
360 if (sk->sk_state == DCCP_LISTEN)
361 return;
362
363 /* We don't check in the destentry if pmtu discovery is forbidden
364 * on this route. We just assume that no packet_to_big packets
365 * are send back when pmtu discovery is not active.
366 * There is a small race when the user changes this flag in the
367 * route, but I think that's acceptable.
368 */
369 if ((dst = __sk_dst_check(sk, 0)) == NULL)
370 return;
371
372 dst->ops->update_pmtu(dst, mtu);
373
374 /* Something is about to be wrong... Remember soft error
375 * for the case, if this connection will not able to recover.
376 */
377 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
378 sk->sk_err_soft = EMSGSIZE;
379
380 mtu = dst_mtu(dst);
381
382 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
383 dp->dccps_pmtu_cookie > mtu) {
384 dccp_sync_mss(sk, mtu);
385
386 /*
387 * From: draft-ietf-dccp-spec-11.txt
388 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300389 * DCCP-Sync packets are the best choice for upward
390 * probing, since DCCP-Sync probes do not risk application
391 * data loss.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700392 */
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300393 dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700394 } /* else let the usual retransmit timer handle it */
395}
396
397static void dccp_v4_ctl_send_ack(struct sk_buff *rxskb)
398{
399 int err;
400 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
401 const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) +
402 sizeof(struct dccp_hdr_ext) +
403 sizeof(struct dccp_hdr_ack_bits);
404 struct sk_buff *skb;
405
406 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
407 return;
408
409 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
410 if (skb == NULL)
411 return;
412
413 /* Reserve space for headers. */
414 skb_reserve(skb, MAX_DCCP_HEADER);
415
416 skb->dst = dst_clone(rxskb->dst);
417
418 skb->h.raw = skb_push(skb, dccp_hdr_ack_len);
419 dh = dccp_hdr(skb);
420 memset(dh, 0, dccp_hdr_ack_len);
421
422 /* Build DCCP header and checksum it. */
423 dh->dccph_type = DCCP_PKT_ACK;
424 dh->dccph_sport = rxdh->dccph_dport;
425 dh->dccph_dport = rxdh->dccph_sport;
426 dh->dccph_doff = dccp_hdr_ack_len / 4;
427 dh->dccph_x = 1;
428
429 dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300430 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
431 DCCP_SKB_CB(rxskb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700432
433 bh_lock_sock(dccp_ctl_socket->sk);
434 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300435 rxskb->nh.iph->daddr,
436 rxskb->nh.iph->saddr, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700437 bh_unlock_sock(dccp_ctl_socket->sk);
438
439 if (err == NET_XMIT_CN || err == 0) {
440 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
441 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
442 }
443}
444
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300445static void dccp_v4_reqsk_send_ack(struct sk_buff *skb,
446 struct request_sock *req)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700447{
448 dccp_v4_ctl_send_ack(skb);
449}
450
451static int dccp_v4_send_response(struct sock *sk, struct request_sock *req,
452 struct dst_entry *dst)
453{
454 int err = -1;
455 struct sk_buff *skb;
456
457 /* First, grab a route. */
458
459 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
460 goto out;
461
462 skb = dccp_make_response(sk, dst, req);
463 if (skb != NULL) {
464 const struct inet_request_sock *ireq = inet_rsk(req);
465
466 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
467 ireq->rmt_addr,
468 ireq->opt);
469 if (err == NET_XMIT_CN)
470 err = 0;
471 }
472
473out:
474 dst_release(dst);
475 return err;
476}
477
478/*
479 * This routine is called by the ICMP module when it gets some sort of error
480 * condition. If err < 0 then the socket should be closed and the error
481 * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
482 * After adjustment header points to the first 8 bytes of the tcp header. We
483 * need to find the appropriate port.
484 *
485 * The locking strategy used here is very "optimistic". When someone else
486 * accesses the socket the ICMP is just dropped and for some paths there is no
487 * check at all. A more general error queue to queue errors for later handling
488 * is probably better.
489 */
490void dccp_v4_err(struct sk_buff *skb, u32 info)
491{
492 const struct iphdr *iph = (struct iphdr *)skb->data;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300493 const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data +
494 (iph->ihl << 2));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700495 struct dccp_sock *dp;
496 struct inet_sock *inet;
497 const int type = skb->h.icmph->type;
498 const int code = skb->h.icmph->code;
499 struct sock *sk;
500 __u64 seq;
501 int err;
502
503 if (skb->len < (iph->ihl << 2) + 8) {
504 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
505 return;
506 }
507
508 sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport,
509 iph->saddr, dh->dccph_sport, inet_iif(skb));
510 if (sk == NULL) {
511 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
512 return;
513 }
514
515 if (sk->sk_state == DCCP_TIME_WAIT) {
516 inet_twsk_put((struct inet_timewait_sock *)sk);
517 return;
518 }
519
520 bh_lock_sock(sk);
521 /* If too many ICMPs get dropped on busy
522 * servers this needs to be solved differently.
523 */
524 if (sock_owned_by_user(sk))
525 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
526
527 if (sk->sk_state == DCCP_CLOSED)
528 goto out;
529
530 dp = dccp_sk(sk);
531 seq = dccp_hdr_seq(skb);
532 if (sk->sk_state != DCCP_LISTEN &&
533 !between48(seq, dp->dccps_swl, dp->dccps_swh)) {
534 NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS);
535 goto out;
536 }
537
538 switch (type) {
539 case ICMP_SOURCE_QUENCH:
540 /* Just silently ignore these. */
541 goto out;
542 case ICMP_PARAMETERPROB:
543 err = EPROTO;
544 break;
545 case ICMP_DEST_UNREACH:
546 if (code > NR_ICMP_UNREACH)
547 goto out;
548
549 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
550 if (!sock_owned_by_user(sk))
551 dccp_do_pmtu_discovery(sk, iph, info);
552 goto out;
553 }
554
555 err = icmp_err_convert[code].errno;
556 break;
557 case ICMP_TIME_EXCEEDED:
558 err = EHOSTUNREACH;
559 break;
560 default:
561 goto out;
562 }
563
564 switch (sk->sk_state) {
565 struct request_sock *req , **prev;
566 case DCCP_LISTEN:
567 if (sock_owned_by_user(sk))
568 goto out;
569 req = inet_csk_search_req(sk, &prev, dh->dccph_dport,
570 iph->daddr, iph->saddr);
571 if (!req)
572 goto out;
573
574 /*
575 * ICMPs are not backlogged, hence we cannot get an established
576 * socket here.
577 */
578 BUG_TRAP(!req->sk);
579
580 if (seq != dccp_rsk(req)->dreq_iss) {
581 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
582 goto out;
583 }
584 /*
585 * Still in RESPOND, just remove it silently.
586 * There is no good way to pass the error to the newly
587 * created socket, and POSIX does not want network
588 * errors returned from accept().
589 */
590 inet_csk_reqsk_queue_drop(sk, req, prev);
591 goto out;
592
593 case DCCP_REQUESTING:
594 case DCCP_RESPOND:
595 if (!sock_owned_by_user(sk)) {
596 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
597 sk->sk_err = err;
598
599 sk->sk_error_report(sk);
600
601 dccp_done(sk);
602 } else
603 sk->sk_err_soft = err;
604 goto out;
605 }
606
607 /* If we've already connected we will keep trying
608 * until we time out, or the user gives up.
609 *
610 * rfc1122 4.2.3.9 allows to consider as hard errors
611 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
612 * but it is obsoleted by pmtu discovery).
613 *
614 * Note, that in modern internet, where routing is unreliable
615 * and in each dark corner broken firewalls sit, sending random
616 * errors ordered by their masters even this two messages finally lose
617 * their original sense (even Linux sends invalid PORT_UNREACHs)
618 *
619 * Now we are in compliance with RFCs.
620 * --ANK (980905)
621 */
622
623 inet = inet_sk(sk);
624 if (!sock_owned_by_user(sk) && inet->recverr) {
625 sk->sk_err = err;
626 sk->sk_error_report(sk);
627 } else /* Only an error on timeout */
628 sk->sk_err_soft = err;
629out:
630 bh_unlock_sock(sk);
631 sock_put(sk);
632}
633
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700634int dccp_v4_send_reset(struct sock *sk, enum dccp_reset_codes code)
635{
636 struct sk_buff *skb;
637 /*
638 * FIXME: what if rebuild_header fails?
639 * Should we be doing a rebuild_header here?
640 */
641 int err = inet_sk_rebuild_header(sk);
642
643 if (err != 0)
644 return err;
645
646 skb = dccp_make_reset(sk, sk->sk_dst_cache, code);
647 if (skb != NULL) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700648 const struct inet_sock *inet = inet_sk(sk);
649
650 err = ip_build_and_send_pkt(skb, sk,
651 inet->saddr, inet->daddr, NULL);
652 if (err == NET_XMIT_CN)
653 err = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700654 }
655
656 return err;
657}
658
659static inline u64 dccp_v4_init_sequence(const struct sock *sk,
660 const struct sk_buff *skb)
661{
662 return secure_dccp_sequence_number(skb->nh.iph->daddr,
663 skb->nh.iph->saddr,
664 dccp_hdr(skb)->dccph_dport,
665 dccp_hdr(skb)->dccph_sport);
666}
667
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700668static inline int dccp_bad_service_code(const struct sock *sk,
669 const __u32 service)
670{
671 const struct dccp_sock *dp = dccp_sk(sk);
672
673 if (dp->dccps_service == service)
674 return 0;
675 return !dccp_list_has_service(dp->dccps_service_list, service);
676}
677
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700678int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
679{
680 struct inet_request_sock *ireq;
681 struct dccp_sock dp;
682 struct request_sock *req;
683 struct dccp_request_sock *dreq;
684 const __u32 saddr = skb->nh.iph->saddr;
685 const __u32 daddr = skb->nh.iph->daddr;
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700686 const __u32 service = dccp_hdr_request(skb)->dccph_req_service;
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700687 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
688 __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700689 struct dst_entry *dst = NULL;
690
691 /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
692 if (((struct rtable *)skb->dst)->rt_flags &
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700693 (RTCF_BROADCAST | RTCF_MULTICAST)) {
694 reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700695 goto drop;
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700696 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700697
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700698 if (dccp_bad_service_code(sk, service)) {
699 reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
700 goto drop;
701 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700702 /*
703 * TW buckets are converted to open requests without
704 * limitations, they conserve resources and peer is
705 * evidently real one.
706 */
707 if (inet_csk_reqsk_queue_is_full(sk))
708 goto drop;
709
710 /*
711 * Accept backlog is full. If we have already queued enough
712 * of warm entries in syn queue, drop request. It is better than
713 * clogging syn queue with openreqs with exponentially increasing
714 * timeout.
715 */
716 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
717 goto drop;
718
719 req = reqsk_alloc(sk->sk_prot->rsk_prot);
720 if (req == NULL)
721 goto drop;
722
723 /* FIXME: process options */
724
725 dccp_openreq_init(req, &dp, skb);
726
727 ireq = inet_rsk(req);
728 ireq->loc_addr = daddr;
729 ireq->rmt_addr = saddr;
730 /* FIXME: Merge Aristeu's option parsing code when ready */
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300731 req->rcv_wnd = 100; /* Fake, option parsing will get the
732 right value */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700733 ireq->opt = NULL;
734
735 /*
736 * Step 3: Process LISTEN state
737 *
738 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
739 *
740 * In fact we defer setting S.GSR, S.SWL, S.SWH to
741 * dccp_create_openreq_child.
742 */
743 dreq = dccp_rsk(req);
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700744 dreq->dreq_isr = dcb->dccpd_seq;
745 dreq->dreq_iss = dccp_v4_init_sequence(sk, skb);
746 dreq->dreq_service = service;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700747
748 if (dccp_v4_send_response(sk, req, dst))
749 goto drop_and_free;
750
751 inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
752 return 0;
753
754drop_and_free:
755 /*
756 * FIXME: should be reqsk_free after implementing req->rsk_ops
757 */
758 __reqsk_free(req);
759drop:
760 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700761 dcb->dccpd_reset_code = reset_code;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700762 return -1;
763}
764
765/*
766 * The three way handshake has completed - we got a valid ACK or DATAACK -
767 * now create the new socket.
768 *
769 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
770 */
771struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
772 struct request_sock *req,
773 struct dst_entry *dst)
774{
775 struct inet_request_sock *ireq;
776 struct inet_sock *newinet;
777 struct dccp_sock *newdp;
778 struct sock *newsk;
779
780 if (sk_acceptq_is_full(sk))
781 goto exit_overflow;
782
783 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
784 goto exit;
785
786 newsk = dccp_create_openreq_child(sk, req, skb);
787 if (newsk == NULL)
788 goto exit;
789
790 sk_setup_caps(newsk, dst);
791
792 newdp = dccp_sk(newsk);
793 newinet = inet_sk(newsk);
794 ireq = inet_rsk(req);
795 newinet->daddr = ireq->rmt_addr;
796 newinet->rcv_saddr = ireq->loc_addr;
797 newinet->saddr = ireq->loc_addr;
798 newinet->opt = ireq->opt;
799 ireq->opt = NULL;
800 newinet->mc_index = inet_iif(skb);
801 newinet->mc_ttl = skb->nh.iph->ttl;
802 newinet->id = jiffies;
803
804 dccp_sync_mss(newsk, dst_mtu(dst));
805
806 __inet_hash(&dccp_hashinfo, newsk, 0);
807 __inet_inherit_port(&dccp_hashinfo, sk, newsk);
808
809 return newsk;
810
811exit_overflow:
812 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
813exit:
814 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
815 dst_release(dst);
816 return NULL;
817}
818
819static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
820{
821 const struct dccp_hdr *dh = dccp_hdr(skb);
822 const struct iphdr *iph = skb->nh.iph;
823 struct sock *nsk;
824 struct request_sock **prev;
825 /* Find possible connection requests. */
826 struct request_sock *req = inet_csk_search_req(sk, &prev,
827 dh->dccph_sport,
828 iph->saddr, iph->daddr);
829 if (req != NULL)
830 return dccp_check_req(sk, skb, req, prev);
831
832 nsk = __inet_lookup_established(&dccp_hashinfo,
833 iph->saddr, dh->dccph_sport,
834 iph->daddr, ntohs(dh->dccph_dport),
835 inet_iif(skb));
836 if (nsk != NULL) {
837 if (nsk->sk_state != DCCP_TIME_WAIT) {
838 bh_lock_sock(nsk);
839 return nsk;
840 }
841 inet_twsk_put((struct inet_timewait_sock *)nsk);
842 return NULL;
843 }
844
845 return sk;
846}
847
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300848int dccp_v4_checksum(const struct sk_buff *skb, const u32 saddr,
849 const u32 daddr)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700850{
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700851 const struct dccp_hdr* dh = dccp_hdr(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700852 int checksum_len;
853 u32 tmp;
854
855 if (dh->dccph_cscov == 0)
856 checksum_len = skb->len;
857 else {
858 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300859 checksum_len = checksum_len < skb->len ? checksum_len :
860 skb->len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700861 }
862
863 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300864 return csum_tcpudp_magic(saddr, daddr, checksum_len,
865 IPPROTO_DCCP, tmp);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700866}
867
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700868static int dccp_v4_verify_checksum(struct sk_buff *skb,
869 const u32 saddr, const u32 daddr)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700870{
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700871 struct dccp_hdr *dh = dccp_hdr(skb);
872 int checksum_len;
873 u32 tmp;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700874
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700875 if (dh->dccph_cscov == 0)
876 checksum_len = skb->len;
877 else {
878 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300879 checksum_len = checksum_len < skb->len ? checksum_len :
880 skb->len;
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700881 }
882 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300883 return csum_tcpudp_magic(saddr, daddr, checksum_len,
884 IPPROTO_DCCP, tmp) == 0 ? 0 : -1;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700885}
886
887static struct dst_entry* dccp_v4_route_skb(struct sock *sk,
888 struct sk_buff *skb)
889{
890 struct rtable *rt;
891 struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif,
892 .nl_u = { .ip4_u =
893 { .daddr = skb->nh.iph->saddr,
894 .saddr = skb->nh.iph->daddr,
895 .tos = RT_CONN_FLAGS(sk) } },
896 .proto = sk->sk_protocol,
897 .uli_u = { .ports =
898 { .sport = dccp_hdr(skb)->dccph_dport,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300899 .dport = dccp_hdr(skb)->dccph_sport }
900 }
901 };
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700902
903 if (ip_route_output_flow(&rt, &fl, sk, 0)) {
904 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
905 return NULL;
906 }
907
908 return &rt->u.dst;
909}
910
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300911static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700912{
913 int err;
914 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
915 const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
916 sizeof(struct dccp_hdr_ext) +
917 sizeof(struct dccp_hdr_reset);
918 struct sk_buff *skb;
919 struct dst_entry *dst;
Arnaldo Carvalho de Melo2807d4f2005-08-21 05:33:48 -0300920 u64 seqno;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700921
922 /* Never send a reset in response to a reset. */
923 if (rxdh->dccph_type == DCCP_PKT_RESET)
924 return;
925
926 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
927 return;
928
929 dst = dccp_v4_route_skb(dccp_ctl_socket->sk, rxskb);
930 if (dst == NULL)
931 return;
932
933 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
934 if (skb == NULL)
935 goto out;
936
937 /* Reserve space for headers. */
938 skb_reserve(skb, MAX_DCCP_HEADER);
939 skb->dst = dst_clone(dst);
940
941 skb->h.raw = skb_push(skb, dccp_hdr_reset_len);
942 dh = dccp_hdr(skb);
943 memset(dh, 0, dccp_hdr_reset_len);
944
945 /* Build DCCP header and checksum it. */
946 dh->dccph_type = DCCP_PKT_RESET;
947 dh->dccph_sport = rxdh->dccph_dport;
948 dh->dccph_dport = rxdh->dccph_sport;
949 dh->dccph_doff = dccp_hdr_reset_len / 4;
950 dh->dccph_x = 1;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300951 dccp_hdr_reset(skb)->dccph_reset_code =
952 DCCP_SKB_CB(rxskb)->dccpd_reset_code;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700953
Arnaldo Carvalho de Melo2807d4f2005-08-21 05:33:48 -0300954 /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
955 seqno = 0;
956 if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
957 dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
958
959 dccp_hdr_set_seq(dh, seqno);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300960 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
961 DCCP_SKB_CB(rxskb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700962
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700963 dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr,
964 rxskb->nh.iph->daddr);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700965
966 bh_lock_sock(dccp_ctl_socket->sk);
967 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300968 rxskb->nh.iph->daddr,
969 rxskb->nh.iph->saddr, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700970 bh_unlock_sock(dccp_ctl_socket->sk);
971
972 if (err == NET_XMIT_CN || err == 0) {
973 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
974 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
975 }
976out:
977 dst_release(dst);
978}
979
980int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
981{
982 struct dccp_hdr *dh = dccp_hdr(skb);
983
984 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
985 if (dccp_rcv_established(sk, skb, dh, skb->len))
986 goto reset;
987 return 0;
988 }
989
990 /*
991 * Step 3: Process LISTEN state
992 * If S.state == LISTEN,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300993 * If P.type == Request or P contains a valid Init Cookie
994 * option,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700995 * * Must scan the packet's options to check for an Init
996 * Cookie. Only the Init Cookie is processed here,
997 * however; other options are processed in Step 8. This
998 * scan need only be performed if the endpoint uses Init
999 * Cookies *
1000 * * Generate a new socket and switch to that socket *
1001 * Set S := new socket for this port pair
1002 * S.state = RESPOND
1003 * Choose S.ISS (initial seqno) or set from Init Cookie
1004 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
1005 * Continue with S.state == RESPOND
1006 * * A Response packet will be generated in Step 11 *
1007 * Otherwise,
1008 * Generate Reset(No Connection) unless P.type == Reset
1009 * Drop packet and return
1010 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001011 * NOTE: the check for the packet types is done in
1012 * dccp_rcv_state_process
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001013 */
1014 if (sk->sk_state == DCCP_LISTEN) {
1015 struct sock *nsk = dccp_v4_hnd_req(sk, skb);
1016
1017 if (nsk == NULL)
1018 goto discard;
1019
1020 if (nsk != sk) {
1021 if (dccp_child_process(sk, nsk, skb))
1022 goto reset;
1023 return 0;
1024 }
1025 }
1026
1027 if (dccp_rcv_state_process(sk, skb, dh, skb->len))
1028 goto reset;
1029 return 0;
1030
1031reset:
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001032 dccp_v4_ctl_send_reset(skb);
1033discard:
1034 kfree_skb(skb);
1035 return 0;
1036}
1037
1038static inline int dccp_invalid_packet(struct sk_buff *skb)
1039{
1040 const struct dccp_hdr *dh;
1041
1042 if (skb->pkt_type != PACKET_HOST)
1043 return 1;
1044
1045 if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001046 LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001047 return 1;
1048 }
1049
1050 dh = dccp_hdr(skb);
1051
1052 /* If the packet type is not understood, drop packet and return */
1053 if (dh->dccph_type >= DCCP_PKT_INVALID) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001054 LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001055 return 1;
1056 }
1057
1058 /*
1059 * If P.Data Offset is too small for packet type, or too large for
1060 * packet, drop packet and return
1061 */
1062 if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001063 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
1064 "too small 1\n",
1065 dh->dccph_doff);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001066 return 1;
1067 }
1068
1069 if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001070 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
1071 "too small 2\n",
1072 dh->dccph_doff);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001073 return 1;
1074 }
1075
1076 dh = dccp_hdr(skb);
1077
1078 /*
1079 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
1080 * has short sequence numbers), drop packet and return
1081 */
1082 if (dh->dccph_x == 0 &&
1083 dh->dccph_type != DCCP_PKT_DATA &&
1084 dh->dccph_type != DCCP_PKT_ACK &&
1085 dh->dccph_type != DCCP_PKT_DATAACK) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001086 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack "
1087 "nor DataAck and P.X == 0\n",
1088 dccp_packet_name(dh->dccph_type));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001089 return 1;
1090 }
1091
1092 /* If the header checksum is incorrect, drop packet and return */
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -07001093 if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr,
1094 skb->nh.iph->daddr) < 0) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001095 LIMIT_NETDEBUG(KERN_WARNING "DCCP: header checksum is "
1096 "incorrect\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001097 return 1;
1098 }
1099
1100 return 0;
1101}
1102
1103/* this is called when real data arrives */
1104int dccp_v4_rcv(struct sk_buff *skb)
1105{
1106 const struct dccp_hdr *dh;
1107 struct sock *sk;
1108 int rc;
1109
1110 /* Step 1: Check header basics: */
1111
1112 if (dccp_invalid_packet(skb))
1113 goto discard_it;
1114
1115 dh = dccp_hdr(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001116
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001117 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
1118 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
1119
1120 dccp_pr_debug("%8.8s "
1121 "src=%u.%u.%u.%u@%-5d "
1122 "dst=%u.%u.%u.%u@%-5d seq=%llu",
1123 dccp_packet_name(dh->dccph_type),
1124 NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport),
1125 NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport),
David S. Millerf6ccf552005-08-09 20:27:14 -07001126 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001127
1128 if (dccp_packet_without_ack(skb)) {
1129 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
1130 dccp_pr_debug_cat("\n");
1131 } else {
1132 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
David S. Millerf6ccf552005-08-09 20:27:14 -07001133 dccp_pr_debug_cat(", ack=%llu\n",
1134 (unsigned long long)
1135 DCCP_SKB_CB(skb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001136 }
1137
1138 /* Step 2:
1139 * Look up flow ID in table and get corresponding socket */
1140 sk = __inet_lookup(&dccp_hashinfo,
1141 skb->nh.iph->saddr, dh->dccph_sport,
1142 skb->nh.iph->daddr, ntohs(dh->dccph_dport),
1143 inet_iif(skb));
1144
1145 /*
1146 * Step 2:
1147 * If no socket ...
1148 * Generate Reset(No Connection) unless P.type == Reset
1149 * Drop packet and return
1150 */
1151 if (sk == NULL) {
1152 dccp_pr_debug("failed to look up flow ID in table and "
1153 "get corresponding socket\n");
1154 goto no_dccp_socket;
1155 }
1156
1157 /*
1158 * Step 2:
1159 * ... or S.state == TIMEWAIT,
1160 * Generate Reset(No Connection) unless P.type == Reset
1161 * Drop packet and return
1162 */
1163
1164 if (sk->sk_state == DCCP_TIME_WAIT) {
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -07001165 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
1166 "do_time_wait\n");
1167 goto do_time_wait;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001168 }
1169
1170 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
1171 dccp_pr_debug("xfrm4_policy_check failed\n");
1172 goto discard_and_relse;
1173 }
1174
1175 if (sk_filter(sk, skb, 0)) {
1176 dccp_pr_debug("sk_filter failed\n");
1177 goto discard_and_relse;
1178 }
1179
1180 skb->dev = NULL;
1181
1182 bh_lock_sock(sk);
1183 rc = 0;
1184 if (!sock_owned_by_user(sk))
1185 rc = dccp_v4_do_rcv(sk, skb);
1186 else
1187 sk_add_backlog(sk, skb);
1188 bh_unlock_sock(sk);
1189
1190 sock_put(sk);
1191 return rc;
1192
1193no_dccp_socket:
1194 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1195 goto discard_it;
1196 /*
1197 * Step 2:
1198 * Generate Reset(No Connection) unless P.type == Reset
1199 * Drop packet and return
1200 */
1201 if (dh->dccph_type != DCCP_PKT_RESET) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001202 DCCP_SKB_CB(skb)->dccpd_reset_code =
1203 DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001204 dccp_v4_ctl_send_reset(skb);
1205 }
1206
1207discard_it:
1208 /* Discard frame. */
1209 kfree_skb(skb);
1210 return 0;
1211
1212discard_and_relse:
1213 sock_put(sk);
1214 goto discard_it;
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -07001215
1216do_time_wait:
1217 inet_twsk_put((struct inet_timewait_sock *)sk);
1218 goto no_dccp_socket;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001219}
1220
1221static int dccp_v4_init_sock(struct sock *sk)
1222{
1223 struct dccp_sock *dp = dccp_sk(sk);
1224 static int dccp_ctl_socket_init = 1;
1225
1226 dccp_options_init(&dp->dccps_options);
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -03001227 do_gettimeofday(&dp->dccps_epoch);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001228
1229 if (dp->dccps_options.dccpo_send_ack_vector) {
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001230 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(DCCP_MAX_ACKVEC_LEN,
1231 GFP_KERNEL);
1232 if (dp->dccps_hc_rx_ackvec == NULL)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001233 return -ENOMEM;
1234 }
1235
1236 /*
1237 * FIXME: We're hardcoding the CCID, and doing this at this point makes
1238 * the listening (master) sock get CCID control blocks, which is not
1239 * necessary, but for now, to not mess with the test userspace apps,
1240 * lets leave it here, later the real solution is to do this in a
1241 * setsockopt(CCIDs-I-want/accept). -acme
1242 */
1243 if (likely(!dccp_ctl_socket_init)) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001244 dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_ccid,
1245 sk);
1246 dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_ccid,
1247 sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001248 if (dp->dccps_hc_rx_ccid == NULL ||
1249 dp->dccps_hc_tx_ccid == NULL) {
1250 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1251 ccid_exit(dp->dccps_hc_tx_ccid, sk);
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001252 if (dp->dccps_options.dccpo_send_ack_vector) {
1253 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1254 dp->dccps_hc_rx_ackvec = NULL;
1255 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001256 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1257 return -ENOMEM;
1258 }
1259 } else
1260 dccp_ctl_socket_init = 0;
1261
1262 dccp_init_xmit_timers(sk);
Arnaldo Carvalho de Melo0b4e03b2005-08-09 20:31:11 -07001263 inet_csk(sk)->icsk_rto = DCCP_TIMEOUT_INIT;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001264 sk->sk_state = DCCP_CLOSED;
Arnaldo Carvalho de Meloc530cfb2005-08-29 02:15:54 -03001265 sk->sk_write_space = dccp_write_space;
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 /*
1278 * DCCP doesn't use sk_qrite_queue, just sk_send_head
1279 * 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
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -07001290 if (dp->dccps_service_list != NULL) {
1291 kfree(dp->dccps_service_list);
1292 dp->dccps_service_list = NULL;
1293 }
1294
Arnaldo Carvalho de Melo8efa5442005-08-23 21:54:00 -07001295 ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
1296 ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001297 if (dp->dccps_options.dccpo_send_ack_vector) {
1298 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1299 dp->dccps_hc_rx_ackvec = NULL;
1300 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001301 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1302 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1303 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1304
1305 return 0;
1306}
1307
1308static void dccp_v4_reqsk_destructor(struct request_sock *req)
1309{
1310 kfree(inet_rsk(req)->opt);
1311}
1312
1313static struct request_sock_ops dccp_request_sock_ops = {
1314 .family = PF_INET,
1315 .obj_size = sizeof(struct dccp_request_sock),
1316 .rtx_syn_ack = dccp_v4_send_response,
1317 .send_ack = dccp_v4_reqsk_send_ack,
1318 .destructor = dccp_v4_reqsk_destructor,
1319 .send_reset = dccp_v4_ctl_send_reset,
1320};
1321
1322struct proto dccp_v4_prot = {
1323 .name = "DCCP",
1324 .owner = THIS_MODULE,
1325 .close = dccp_close,
1326 .connect = dccp_v4_connect,
1327 .disconnect = dccp_disconnect,
1328 .ioctl = dccp_ioctl,
1329 .init = dccp_v4_init_sock,
1330 .setsockopt = dccp_setsockopt,
1331 .getsockopt = dccp_getsockopt,
1332 .sendmsg = dccp_sendmsg,
1333 .recvmsg = dccp_recvmsg,
1334 .backlog_rcv = dccp_v4_do_rcv,
1335 .hash = dccp_v4_hash,
1336 .unhash = dccp_v4_unhash,
1337 .accept = inet_csk_accept,
1338 .get_port = dccp_v4_get_port,
1339 .shutdown = dccp_shutdown,
1340 .destroy = dccp_v4_destroy_sock,
1341 .orphan_count = &dccp_orphan_count,
1342 .max_header = MAX_DCCP_HEADER,
1343 .obj_size = sizeof(struct dccp_sock),
1344 .rsk_prot = &dccp_request_sock_ops,
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -07001345 .twsk_obj_size = sizeof(struct inet_timewait_sock),
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001346};