blob: 3108c9d464f778081b48003b1e34e9f0a0b01f52 [file] [log] [blame]
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001/*
2 * net/dccp/ipv4.c
3 *
4 * An implementation of the DCCP protocol
5 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13#include <linux/config.h>
14#include <linux/dccp.h>
15#include <linux/icmp.h>
16#include <linux/module.h>
17#include <linux/skbuff.h>
18#include <linux/random.h>
19
20#include <net/icmp.h>
21#include <net/inet_hashtables.h>
22#include <net/sock.h>
23#include <net/tcp_states.h>
24#include <net/xfrm.h>
25
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -070026#include "ackvec.h"
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070027#include "ccid.h"
28#include "dccp.h"
29
30struct inet_hashinfo __cacheline_aligned dccp_hashinfo = {
31 .lhash_lock = RW_LOCK_UNLOCKED,
32 .lhash_users = ATOMIC_INIT(0),
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -030033 .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait),
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070034};
35
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -030036EXPORT_SYMBOL_GPL(dccp_hashinfo);
37
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070038static int dccp_v4_get_port(struct sock *sk, const unsigned short snum)
39{
Arnaldo Carvalho de Melo971af182005-12-13 23:14:47 -080040 return inet_csk_get_port(&dccp_hashinfo, sk, snum,
41 inet_csk_bind_conflict);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070042}
43
44static void dccp_v4_hash(struct sock *sk)
45{
46 inet_hash(&dccp_hashinfo, sk);
47}
48
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -080049void dccp_unhash(struct sock *sk)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070050{
51 inet_unhash(&dccp_hashinfo, sk);
52}
53
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -080054EXPORT_SYMBOL_GPL(dccp_unhash);
55
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070056/* called with local bh disabled */
57static int __dccp_v4_check_established(struct sock *sk, const __u16 lport,
58 struct inet_timewait_sock **twp)
59{
60 struct inet_sock *inet = inet_sk(sk);
61 const u32 daddr = inet->rcv_saddr;
62 const u32 saddr = inet->daddr;
63 const int dif = sk->sk_bound_dev_if;
64 INET_ADDR_COOKIE(acookie, saddr, daddr)
65 const __u32 ports = INET_COMBINED_PORTS(inet->dport, lport);
Eric Dumazet81c3d542005-10-03 14:13:38 -070066 unsigned int hash = inet_ehashfn(daddr, lport, saddr, inet->dport);
67 struct inet_ehash_bucket *head = inet_ehash_bucket(&dccp_hashinfo, hash);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070068 const struct sock *sk2;
69 const struct hlist_node *node;
70 struct inet_timewait_sock *tw;
71
Eric Dumazet81c3d542005-10-03 14:13:38 -070072 prefetch(head->chain.first);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070073 write_lock(&head->lock);
74
75 /* Check TIME-WAIT sockets first. */
76 sk_for_each(sk2, node, &(head + dccp_hashinfo.ehash_size)->chain) {
77 tw = inet_twsk(sk2);
78
Eric Dumazet81c3d542005-10-03 14:13:38 -070079 if (INET_TW_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070080 goto not_unique;
81 }
82 tw = NULL;
83
84 /* And established part... */
85 sk_for_each(sk2, node, &head->chain) {
Eric Dumazet81c3d542005-10-03 14:13:38 -070086 if (INET_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070087 goto not_unique;
88 }
89
90 /* Must record num and sport now. Otherwise we will see
91 * in hash table socket with a funny identity. */
92 inet->num = lport;
93 inet->sport = htons(lport);
Eric Dumazet81c3d542005-10-03 14:13:38 -070094 sk->sk_hash = hash;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070095 BUG_TRAP(sk_unhashed(sk));
96 __sk_add_node(sk, &head->chain);
97 sock_prot_inc_use(sk->sk_prot);
98 write_unlock(&head->lock);
99
100 if (twp != NULL) {
101 *twp = tw;
102 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
103 } else if (tw != NULL) {
104 /* Silly. Should hash-dance instead... */
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -0700105 inet_twsk_deschedule(tw, &dccp_death_row);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700106 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
107
108 inet_twsk_put(tw);
109 }
110
111 return 0;
112
113not_unique:
114 write_unlock(&head->lock);
115 return -EADDRNOTAVAIL;
116}
117
118/*
119 * Bind a port for a connect operation and hash it.
120 */
121static int dccp_v4_hash_connect(struct sock *sk)
122{
123 const unsigned short snum = inet_sk(sk)->num;
124 struct inet_bind_hashbucket *head;
125 struct inet_bind_bucket *tb;
126 int ret;
127
128 if (snum == 0) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700129 int low = sysctl_local_port_range[0];
130 int high = sysctl_local_port_range[1];
131 int remaining = (high - low) + 1;
Stephen Hemminger6df71632005-11-03 16:33:23 -0800132 int rover = net_random() % (high - low) + low;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700133 struct hlist_node *node;
134 struct inet_timewait_sock *tw = NULL;
135
136 local_bh_disable();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700137 do {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300138 head = &dccp_hashinfo.bhash[inet_bhashfn(rover,
139 dccp_hashinfo.bhash_size)];
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700140 spin_lock(&head->lock);
141
142 /* Does not bother with rcv_saddr checks,
143 * because the established check is already
144 * unique enough.
145 */
146 inet_bind_bucket_for_each(tb, node, &head->chain) {
147 if (tb->port == rover) {
148 BUG_TRAP(!hlist_empty(&tb->owners));
149 if (tb->fastreuse >= 0)
150 goto next_port;
151 if (!__dccp_v4_check_established(sk,
152 rover,
153 &tw))
154 goto ok;
155 goto next_port;
156 }
157 }
158
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300159 tb = inet_bind_bucket_create(dccp_hashinfo.bind_bucket_cachep,
160 head, rover);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700161 if (tb == NULL) {
162 spin_unlock(&head->lock);
163 break;
164 }
165 tb->fastreuse = -1;
166 goto ok;
167
168 next_port:
169 spin_unlock(&head->lock);
Stephen Hemminger6df71632005-11-03 16:33:23 -0800170 if (++rover > high)
171 rover = low;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700172 } while (--remaining > 0);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700173
174 local_bh_enable();
175
176 return -EADDRNOTAVAIL;
177
178ok:
179 /* All locks still held and bhs disabled */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700180 inet_bind_hash(sk, tb, rover);
181 if (sk_unhashed(sk)) {
182 inet_sk(sk)->sport = htons(rover);
183 __inet_hash(&dccp_hashinfo, sk, 0);
184 }
185 spin_unlock(&head->lock);
186
187 if (tw != NULL) {
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -0700188 inet_twsk_deschedule(tw, &dccp_death_row);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700189 inet_twsk_put(tw);
190 }
191
192 ret = 0;
193 goto out;
194 }
195
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300196 head = &dccp_hashinfo.bhash[inet_bhashfn(snum,
197 dccp_hashinfo.bhash_size)];
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700198 tb = inet_csk(sk)->icsk_bind_hash;
199 spin_lock_bh(&head->lock);
200 if (sk_head(&tb->owners) == sk && sk->sk_bind_node.next == NULL) {
201 __inet_hash(&dccp_hashinfo, sk, 0);
202 spin_unlock_bh(&head->lock);
203 return 0;
204 } else {
205 spin_unlock(&head->lock);
206 /* No definite answer... Walk to established hash table */
207 ret = __dccp_v4_check_established(sk, snum, NULL);
208out:
209 local_bh_enable();
210 return ret;
211 }
212}
213
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800214int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700215{
216 struct inet_sock *inet = inet_sk(sk);
217 struct dccp_sock *dp = dccp_sk(sk);
218 const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
219 struct rtable *rt;
220 u32 daddr, nexthop;
221 int tmp;
222 int err;
223
224 dp->dccps_role = DCCP_ROLE_CLIENT;
225
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700226 if (dccp_service_not_initialized(sk))
227 return -EPROTO;
228
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700229 if (addr_len < sizeof(struct sockaddr_in))
230 return -EINVAL;
231
232 if (usin->sin_family != AF_INET)
233 return -EAFNOSUPPORT;
234
235 nexthop = daddr = usin->sin_addr.s_addr;
236 if (inet->opt != NULL && inet->opt->srr) {
237 if (daddr == 0)
238 return -EINVAL;
239 nexthop = inet->opt->faddr;
240 }
241
242 tmp = ip_route_connect(&rt, nexthop, inet->saddr,
243 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
244 IPPROTO_DCCP,
245 inet->sport, usin->sin_port, sk);
246 if (tmp < 0)
247 return tmp;
248
249 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
250 ip_rt_put(rt);
251 return -ENETUNREACH;
252 }
253
254 if (inet->opt == NULL || !inet->opt->srr)
255 daddr = rt->rt_dst;
256
257 if (inet->saddr == 0)
258 inet->saddr = rt->rt_src;
259 inet->rcv_saddr = inet->saddr;
260
261 inet->dport = usin->sin_port;
262 inet->daddr = daddr;
263
264 dp->dccps_ext_header_len = 0;
265 if (inet->opt != NULL)
266 dp->dccps_ext_header_len = inet->opt->optlen;
267 /*
268 * Socket identity is still unknown (sport may be zero).
269 * However we set state to DCCP_REQUESTING and not releasing socket
270 * lock select source port, enter ourselves into the hash tables and
271 * complete initialization after this.
272 */
273 dccp_set_state(sk, DCCP_REQUESTING);
274 err = dccp_v4_hash_connect(sk);
275 if (err != 0)
276 goto failure;
277
278 err = ip_route_newports(&rt, inet->sport, inet->dport, sk);
279 if (err != 0)
280 goto failure;
281
282 /* OK, now commit destination to socket. */
283 sk_setup_caps(sk, &rt->u.dst);
284
285 dp->dccps_gar =
286 dp->dccps_iss = secure_dccp_sequence_number(inet->saddr,
287 inet->daddr,
288 inet->sport,
289 usin->sin_port);
290 dccp_update_gss(sk, dp->dccps_iss);
291
292 inet->id = dp->dccps_iss ^ jiffies;
293
294 err = dccp_connect(sk);
295 rt = NULL;
296 if (err != 0)
297 goto failure;
298out:
299 return err;
300failure:
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300301 /*
302 * This unhashes the socket and releases the local port, if necessary.
303 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700304 dccp_set_state(sk, DCCP_CLOSED);
305 ip_rt_put(rt);
306 sk->sk_route_caps = 0;
307 inet->dport = 0;
308 goto out;
309}
310
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800311EXPORT_SYMBOL_GPL(dccp_v4_connect);
312
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700313/*
314 * This routine does path mtu discovery as defined in RFC1191.
315 */
316static inline void dccp_do_pmtu_discovery(struct sock *sk,
317 const struct iphdr *iph,
318 u32 mtu)
319{
320 struct dst_entry *dst;
321 const struct inet_sock *inet = inet_sk(sk);
322 const struct dccp_sock *dp = dccp_sk(sk);
323
324 /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
325 * send out by Linux are always < 576bytes so they should go through
326 * unfragmented).
327 */
328 if (sk->sk_state == DCCP_LISTEN)
329 return;
330
331 /* We don't check in the destentry if pmtu discovery is forbidden
332 * on this route. We just assume that no packet_to_big packets
333 * are send back when pmtu discovery is not active.
334 * There is a small race when the user changes this flag in the
335 * route, but I think that's acceptable.
336 */
337 if ((dst = __sk_dst_check(sk, 0)) == NULL)
338 return;
339
340 dst->ops->update_pmtu(dst, mtu);
341
342 /* Something is about to be wrong... Remember soft error
343 * for the case, if this connection will not able to recover.
344 */
345 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
346 sk->sk_err_soft = EMSGSIZE;
347
348 mtu = dst_mtu(dst);
349
350 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
351 dp->dccps_pmtu_cookie > mtu) {
352 dccp_sync_mss(sk, mtu);
353
354 /*
355 * From: draft-ietf-dccp-spec-11.txt
356 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300357 * DCCP-Sync packets are the best choice for upward
358 * probing, since DCCP-Sync probes do not risk application
359 * data loss.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700360 */
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300361 dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700362 } /* else let the usual retransmit timer handle it */
363}
364
365static void dccp_v4_ctl_send_ack(struct sk_buff *rxskb)
366{
367 int err;
368 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
369 const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) +
370 sizeof(struct dccp_hdr_ext) +
371 sizeof(struct dccp_hdr_ack_bits);
372 struct sk_buff *skb;
373
374 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
375 return;
376
377 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
378 if (skb == NULL)
379 return;
380
381 /* Reserve space for headers. */
382 skb_reserve(skb, MAX_DCCP_HEADER);
383
384 skb->dst = dst_clone(rxskb->dst);
385
386 skb->h.raw = skb_push(skb, dccp_hdr_ack_len);
387 dh = dccp_hdr(skb);
388 memset(dh, 0, dccp_hdr_ack_len);
389
390 /* Build DCCP header and checksum it. */
391 dh->dccph_type = DCCP_PKT_ACK;
392 dh->dccph_sport = rxdh->dccph_dport;
393 dh->dccph_dport = rxdh->dccph_sport;
394 dh->dccph_doff = dccp_hdr_ack_len / 4;
395 dh->dccph_x = 1;
396
397 dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300398 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
399 DCCP_SKB_CB(rxskb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700400
401 bh_lock_sock(dccp_ctl_socket->sk);
402 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300403 rxskb->nh.iph->daddr,
404 rxskb->nh.iph->saddr, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700405 bh_unlock_sock(dccp_ctl_socket->sk);
406
407 if (err == NET_XMIT_CN || err == 0) {
408 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
409 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
410 }
411}
412
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300413static void dccp_v4_reqsk_send_ack(struct sk_buff *skb,
414 struct request_sock *req)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700415{
416 dccp_v4_ctl_send_ack(skb);
417}
418
419static int dccp_v4_send_response(struct sock *sk, struct request_sock *req,
420 struct dst_entry *dst)
421{
422 int err = -1;
423 struct sk_buff *skb;
424
425 /* First, grab a route. */
426
427 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
428 goto out;
429
430 skb = dccp_make_response(sk, dst, req);
431 if (skb != NULL) {
432 const struct inet_request_sock *ireq = inet_rsk(req);
433
Herbert Xu49c5bfa2005-10-18 12:03:28 +1000434 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700435 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
436 ireq->rmt_addr,
437 ireq->opt);
438 if (err == NET_XMIT_CN)
439 err = 0;
440 }
441
442out:
443 dst_release(dst);
444 return err;
445}
446
447/*
448 * This routine is called by the ICMP module when it gets some sort of error
449 * condition. If err < 0 then the socket should be closed and the error
450 * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
451 * After adjustment header points to the first 8 bytes of the tcp header. We
452 * need to find the appropriate port.
453 *
454 * The locking strategy used here is very "optimistic". When someone else
455 * accesses the socket the ICMP is just dropped and for some paths there is no
456 * check at all. A more general error queue to queue errors for later handling
457 * is probably better.
458 */
459void dccp_v4_err(struct sk_buff *skb, u32 info)
460{
461 const struct iphdr *iph = (struct iphdr *)skb->data;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300462 const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data +
463 (iph->ihl << 2));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700464 struct dccp_sock *dp;
465 struct inet_sock *inet;
466 const int type = skb->h.icmph->type;
467 const int code = skb->h.icmph->code;
468 struct sock *sk;
469 __u64 seq;
470 int err;
471
472 if (skb->len < (iph->ihl << 2) + 8) {
473 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
474 return;
475 }
476
477 sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport,
478 iph->saddr, dh->dccph_sport, inet_iif(skb));
479 if (sk == NULL) {
480 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
481 return;
482 }
483
484 if (sk->sk_state == DCCP_TIME_WAIT) {
485 inet_twsk_put((struct inet_timewait_sock *)sk);
486 return;
487 }
488
489 bh_lock_sock(sk);
490 /* If too many ICMPs get dropped on busy
491 * servers this needs to be solved differently.
492 */
493 if (sock_owned_by_user(sk))
494 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
495
496 if (sk->sk_state == DCCP_CLOSED)
497 goto out;
498
499 dp = dccp_sk(sk);
500 seq = dccp_hdr_seq(skb);
501 if (sk->sk_state != DCCP_LISTEN &&
502 !between48(seq, dp->dccps_swl, dp->dccps_swh)) {
503 NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS);
504 goto out;
505 }
506
507 switch (type) {
508 case ICMP_SOURCE_QUENCH:
509 /* Just silently ignore these. */
510 goto out;
511 case ICMP_PARAMETERPROB:
512 err = EPROTO;
513 break;
514 case ICMP_DEST_UNREACH:
515 if (code > NR_ICMP_UNREACH)
516 goto out;
517
518 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
519 if (!sock_owned_by_user(sk))
520 dccp_do_pmtu_discovery(sk, iph, info);
521 goto out;
522 }
523
524 err = icmp_err_convert[code].errno;
525 break;
526 case ICMP_TIME_EXCEEDED:
527 err = EHOSTUNREACH;
528 break;
529 default:
530 goto out;
531 }
532
533 switch (sk->sk_state) {
534 struct request_sock *req , **prev;
535 case DCCP_LISTEN:
536 if (sock_owned_by_user(sk))
537 goto out;
538 req = inet_csk_search_req(sk, &prev, dh->dccph_dport,
539 iph->daddr, iph->saddr);
540 if (!req)
541 goto out;
542
543 /*
544 * ICMPs are not backlogged, hence we cannot get an established
545 * socket here.
546 */
547 BUG_TRAP(!req->sk);
548
549 if (seq != dccp_rsk(req)->dreq_iss) {
550 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
551 goto out;
552 }
553 /*
554 * Still in RESPOND, just remove it silently.
555 * There is no good way to pass the error to the newly
556 * created socket, and POSIX does not want network
557 * errors returned from accept().
558 */
559 inet_csk_reqsk_queue_drop(sk, req, prev);
560 goto out;
561
562 case DCCP_REQUESTING:
563 case DCCP_RESPOND:
564 if (!sock_owned_by_user(sk)) {
565 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
566 sk->sk_err = err;
567
568 sk->sk_error_report(sk);
569
570 dccp_done(sk);
571 } else
572 sk->sk_err_soft = err;
573 goto out;
574 }
575
576 /* If we've already connected we will keep trying
577 * until we time out, or the user gives up.
578 *
579 * rfc1122 4.2.3.9 allows to consider as hard errors
580 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
581 * but it is obsoleted by pmtu discovery).
582 *
583 * Note, that in modern internet, where routing is unreliable
584 * and in each dark corner broken firewalls sit, sending random
585 * errors ordered by their masters even this two messages finally lose
586 * their original sense (even Linux sends invalid PORT_UNREACHs)
587 *
588 * Now we are in compliance with RFCs.
589 * --ANK (980905)
590 */
591
592 inet = inet_sk(sk);
593 if (!sock_owned_by_user(sk) && inet->recverr) {
594 sk->sk_err = err;
595 sk->sk_error_report(sk);
596 } else /* Only an error on timeout */
597 sk->sk_err_soft = err;
598out:
599 bh_unlock_sock(sk);
600 sock_put(sk);
601}
602
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800603/* This routine computes an IPv4 DCCP checksum. */
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800604void dccp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800605{
606 const struct inet_sock *inet = inet_sk(sk);
607 struct dccp_hdr *dh = dccp_hdr(skb);
608
609 dh->dccph_checksum = dccp_v4_checksum(skb, inet->saddr, inet->daddr);
610}
611
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800612EXPORT_SYMBOL_GPL(dccp_v4_send_check);
613
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700614int dccp_v4_send_reset(struct sock *sk, enum dccp_reset_codes code)
615{
616 struct sk_buff *skb;
617 /*
618 * FIXME: what if rebuild_header fails?
619 * Should we be doing a rebuild_header here?
620 */
621 int err = inet_sk_rebuild_header(sk);
622
623 if (err != 0)
624 return err;
625
626 skb = dccp_make_reset(sk, sk->sk_dst_cache, code);
627 if (skb != NULL) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700628 const struct inet_sock *inet = inet_sk(sk);
629
Herbert Xu49c5bfa2005-10-18 12:03:28 +1000630 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700631 err = ip_build_and_send_pkt(skb, sk,
632 inet->saddr, inet->daddr, NULL);
633 if (err == NET_XMIT_CN)
634 err = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700635 }
636
637 return err;
638}
639
640static inline u64 dccp_v4_init_sequence(const struct sock *sk,
641 const struct sk_buff *skb)
642{
643 return secure_dccp_sequence_number(skb->nh.iph->daddr,
644 skb->nh.iph->saddr,
645 dccp_hdr(skb)->dccph_dport,
646 dccp_hdr(skb)->dccph_sport);
647}
648
649int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
650{
651 struct inet_request_sock *ireq;
652 struct dccp_sock dp;
653 struct request_sock *req;
654 struct dccp_request_sock *dreq;
655 const __u32 saddr = skb->nh.iph->saddr;
656 const __u32 daddr = skb->nh.iph->daddr;
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700657 const __u32 service = dccp_hdr_request(skb)->dccph_req_service;
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700658 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
659 __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700660
661 /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
662 if (((struct rtable *)skb->dst)->rt_flags &
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700663 (RTCF_BROADCAST | RTCF_MULTICAST)) {
664 reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700665 goto drop;
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700666 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700667
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700668 if (dccp_bad_service_code(sk, service)) {
669 reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
670 goto drop;
671 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700672 /*
673 * TW buckets are converted to open requests without
674 * limitations, they conserve resources and peer is
675 * evidently real one.
676 */
677 if (inet_csk_reqsk_queue_is_full(sk))
678 goto drop;
679
680 /*
681 * Accept backlog is full. If we have already queued enough
682 * of warm entries in syn queue, drop request. It is better than
683 * clogging syn queue with openreqs with exponentially increasing
684 * timeout.
685 */
686 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
687 goto drop;
688
689 req = reqsk_alloc(sk->sk_prot->rsk_prot);
690 if (req == NULL)
691 goto drop;
692
693 /* FIXME: process options */
694
695 dccp_openreq_init(req, &dp, skb);
696
697 ireq = inet_rsk(req);
698 ireq->loc_addr = daddr;
699 ireq->rmt_addr = saddr;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300700 req->rcv_wnd = 100; /* Fake, option parsing will get the
701 right value */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700702 ireq->opt = NULL;
703
704 /*
705 * Step 3: Process LISTEN state
706 *
707 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
708 *
709 * In fact we defer setting S.GSR, S.SWL, S.SWH to
710 * dccp_create_openreq_child.
711 */
712 dreq = dccp_rsk(req);
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700713 dreq->dreq_isr = dcb->dccpd_seq;
714 dreq->dreq_iss = dccp_v4_init_sequence(sk, skb);
715 dreq->dreq_service = service;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700716
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800717 if (dccp_v4_send_response(sk, req, NULL))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700718 goto drop_and_free;
719
720 inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
721 return 0;
722
723drop_and_free:
724 /*
725 * FIXME: should be reqsk_free after implementing req->rsk_ops
726 */
727 __reqsk_free(req);
728drop:
729 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700730 dcb->dccpd_reset_code = reset_code;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700731 return -1;
732}
733
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800734EXPORT_SYMBOL_GPL(dccp_v4_conn_request);
735
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700736/*
737 * The three way handshake has completed - we got a valid ACK or DATAACK -
738 * now create the new socket.
739 *
740 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
741 */
742struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
743 struct request_sock *req,
744 struct dst_entry *dst)
745{
746 struct inet_request_sock *ireq;
747 struct inet_sock *newinet;
748 struct dccp_sock *newdp;
749 struct sock *newsk;
750
751 if (sk_acceptq_is_full(sk))
752 goto exit_overflow;
753
754 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
755 goto exit;
756
757 newsk = dccp_create_openreq_child(sk, req, skb);
758 if (newsk == NULL)
759 goto exit;
760
761 sk_setup_caps(newsk, dst);
762
763 newdp = dccp_sk(newsk);
764 newinet = inet_sk(newsk);
765 ireq = inet_rsk(req);
766 newinet->daddr = ireq->rmt_addr;
767 newinet->rcv_saddr = ireq->loc_addr;
768 newinet->saddr = ireq->loc_addr;
769 newinet->opt = ireq->opt;
770 ireq->opt = NULL;
771 newinet->mc_index = inet_iif(skb);
772 newinet->mc_ttl = skb->nh.iph->ttl;
773 newinet->id = jiffies;
774
775 dccp_sync_mss(newsk, dst_mtu(dst));
776
777 __inet_hash(&dccp_hashinfo, newsk, 0);
778 __inet_inherit_port(&dccp_hashinfo, sk, newsk);
779
780 return newsk;
781
782exit_overflow:
783 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
784exit:
785 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
786 dst_release(dst);
787 return NULL;
788}
789
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800790EXPORT_SYMBOL_GPL(dccp_v4_request_recv_sock);
791
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700792static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
793{
794 const struct dccp_hdr *dh = dccp_hdr(skb);
795 const struct iphdr *iph = skb->nh.iph;
796 struct sock *nsk;
797 struct request_sock **prev;
798 /* Find possible connection requests. */
799 struct request_sock *req = inet_csk_search_req(sk, &prev,
800 dh->dccph_sport,
801 iph->saddr, iph->daddr);
802 if (req != NULL)
803 return dccp_check_req(sk, skb, req, prev);
804
805 nsk = __inet_lookup_established(&dccp_hashinfo,
806 iph->saddr, dh->dccph_sport,
807 iph->daddr, ntohs(dh->dccph_dport),
808 inet_iif(skb));
809 if (nsk != NULL) {
810 if (nsk->sk_state != DCCP_TIME_WAIT) {
811 bh_lock_sock(nsk);
812 return nsk;
813 }
814 inet_twsk_put((struct inet_timewait_sock *)nsk);
815 return NULL;
816 }
817
818 return sk;
819}
820
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300821int dccp_v4_checksum(const struct sk_buff *skb, const u32 saddr,
822 const u32 daddr)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700823{
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700824 const struct dccp_hdr* dh = dccp_hdr(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700825 int checksum_len;
826 u32 tmp;
827
828 if (dh->dccph_cscov == 0)
829 checksum_len = skb->len;
830 else {
831 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300832 checksum_len = checksum_len < skb->len ? checksum_len :
833 skb->len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700834 }
835
836 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300837 return csum_tcpudp_magic(saddr, daddr, checksum_len,
838 IPPROTO_DCCP, tmp);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700839}
840
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700841static int dccp_v4_verify_checksum(struct sk_buff *skb,
842 const u32 saddr, const u32 daddr)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700843{
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700844 struct dccp_hdr *dh = dccp_hdr(skb);
845 int checksum_len;
846 u32 tmp;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700847
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700848 if (dh->dccph_cscov == 0)
849 checksum_len = skb->len;
850 else {
851 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300852 checksum_len = checksum_len < skb->len ? checksum_len :
853 skb->len;
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700854 }
855 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300856 return csum_tcpudp_magic(saddr, daddr, checksum_len,
857 IPPROTO_DCCP, tmp) == 0 ? 0 : -1;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700858}
859
860static struct dst_entry* dccp_v4_route_skb(struct sock *sk,
861 struct sk_buff *skb)
862{
863 struct rtable *rt;
864 struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif,
865 .nl_u = { .ip4_u =
866 { .daddr = skb->nh.iph->saddr,
867 .saddr = skb->nh.iph->daddr,
868 .tos = RT_CONN_FLAGS(sk) } },
869 .proto = sk->sk_protocol,
870 .uli_u = { .ports =
871 { .sport = dccp_hdr(skb)->dccph_dport,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300872 .dport = dccp_hdr(skb)->dccph_sport }
873 }
874 };
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700875
876 if (ip_route_output_flow(&rt, &fl, sk, 0)) {
877 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
878 return NULL;
879 }
880
881 return &rt->u.dst;
882}
883
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300884static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700885{
886 int err;
887 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
888 const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
889 sizeof(struct dccp_hdr_ext) +
890 sizeof(struct dccp_hdr_reset);
891 struct sk_buff *skb;
892 struct dst_entry *dst;
Arnaldo Carvalho de Melo2807d4f2005-08-21 05:33:48 -0300893 u64 seqno;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700894
895 /* Never send a reset in response to a reset. */
896 if (rxdh->dccph_type == DCCP_PKT_RESET)
897 return;
898
899 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
900 return;
901
902 dst = dccp_v4_route_skb(dccp_ctl_socket->sk, rxskb);
903 if (dst == NULL)
904 return;
905
906 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
907 if (skb == NULL)
908 goto out;
909
910 /* Reserve space for headers. */
911 skb_reserve(skb, MAX_DCCP_HEADER);
912 skb->dst = dst_clone(dst);
913
914 skb->h.raw = skb_push(skb, dccp_hdr_reset_len);
915 dh = dccp_hdr(skb);
916 memset(dh, 0, dccp_hdr_reset_len);
917
918 /* Build DCCP header and checksum it. */
919 dh->dccph_type = DCCP_PKT_RESET;
920 dh->dccph_sport = rxdh->dccph_dport;
921 dh->dccph_dport = rxdh->dccph_sport;
922 dh->dccph_doff = dccp_hdr_reset_len / 4;
923 dh->dccph_x = 1;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300924 dccp_hdr_reset(skb)->dccph_reset_code =
925 DCCP_SKB_CB(rxskb)->dccpd_reset_code;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700926
Arnaldo Carvalho de Melo2807d4f2005-08-21 05:33:48 -0300927 /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
928 seqno = 0;
929 if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
930 dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
931
932 dccp_hdr_set_seq(dh, seqno);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300933 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
934 DCCP_SKB_CB(rxskb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700935
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700936 dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr,
937 rxskb->nh.iph->daddr);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700938
939 bh_lock_sock(dccp_ctl_socket->sk);
940 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300941 rxskb->nh.iph->daddr,
942 rxskb->nh.iph->saddr, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700943 bh_unlock_sock(dccp_ctl_socket->sk);
944
945 if (err == NET_XMIT_CN || err == 0) {
946 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
947 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
948 }
949out:
950 dst_release(dst);
951}
952
953int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
954{
955 struct dccp_hdr *dh = dccp_hdr(skb);
956
957 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
958 if (dccp_rcv_established(sk, skb, dh, skb->len))
959 goto reset;
960 return 0;
961 }
962
963 /*
964 * Step 3: Process LISTEN state
965 * If S.state == LISTEN,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300966 * If P.type == Request or P contains a valid Init Cookie
967 * option,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700968 * * Must scan the packet's options to check for an Init
969 * Cookie. Only the Init Cookie is processed here,
970 * however; other options are processed in Step 8. This
971 * scan need only be performed if the endpoint uses Init
972 * Cookies *
973 * * Generate a new socket and switch to that socket *
974 * Set S := new socket for this port pair
975 * S.state = RESPOND
976 * Choose S.ISS (initial seqno) or set from Init Cookie
977 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
978 * Continue with S.state == RESPOND
979 * * A Response packet will be generated in Step 11 *
980 * Otherwise,
981 * Generate Reset(No Connection) unless P.type == Reset
982 * Drop packet and return
983 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300984 * NOTE: the check for the packet types is done in
985 * dccp_rcv_state_process
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700986 */
987 if (sk->sk_state == DCCP_LISTEN) {
988 struct sock *nsk = dccp_v4_hnd_req(sk, skb);
989
990 if (nsk == NULL)
991 goto discard;
992
993 if (nsk != sk) {
994 if (dccp_child_process(sk, nsk, skb))
995 goto reset;
996 return 0;
997 }
998 }
999
1000 if (dccp_rcv_state_process(sk, skb, dh, skb->len))
1001 goto reset;
1002 return 0;
1003
1004reset:
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001005 dccp_v4_ctl_send_reset(skb);
1006discard:
1007 kfree_skb(skb);
1008 return 0;
1009}
1010
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001011EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
1012
1013int dccp_invalid_packet(struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001014{
1015 const struct dccp_hdr *dh;
1016
1017 if (skb->pkt_type != PACKET_HOST)
1018 return 1;
1019
1020 if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001021 LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001022 return 1;
1023 }
1024
1025 dh = dccp_hdr(skb);
1026
1027 /* If the packet type is not understood, drop packet and return */
1028 if (dh->dccph_type >= DCCP_PKT_INVALID) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001029 LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001030 return 1;
1031 }
1032
1033 /*
1034 * If P.Data Offset is too small for packet type, or too large for
1035 * packet, drop packet and return
1036 */
1037 if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001038 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
1039 "too small 1\n",
1040 dh->dccph_doff);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001041 return 1;
1042 }
1043
1044 if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001045 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
1046 "too small 2\n",
1047 dh->dccph_doff);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001048 return 1;
1049 }
1050
1051 dh = dccp_hdr(skb);
1052
1053 /*
1054 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
1055 * has short sequence numbers), drop packet and return
1056 */
1057 if (dh->dccph_x == 0 &&
1058 dh->dccph_type != DCCP_PKT_DATA &&
1059 dh->dccph_type != DCCP_PKT_ACK &&
1060 dh->dccph_type != DCCP_PKT_DATAACK) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -03001061 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack "
1062 "nor DataAck and P.X == 0\n",
1063 dccp_packet_name(dh->dccph_type));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001064 return 1;
1065 }
1066
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001067 return 0;
1068}
1069
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001070EXPORT_SYMBOL_GPL(dccp_invalid_packet);
1071
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001072/* this is called when real data arrives */
1073int dccp_v4_rcv(struct sk_buff *skb)
1074{
1075 const struct dccp_hdr *dh;
1076 struct sock *sk;
1077 int rc;
1078
1079 /* Step 1: Check header basics: */
1080
1081 if (dccp_invalid_packet(skb))
1082 goto discard_it;
1083
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001084 /* If the header checksum is incorrect, drop packet and return */
1085 if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr,
1086 skb->nh.iph->daddr) < 0) {
1087 LIMIT_NETDEBUG(KERN_WARNING "%s: incorrect header checksum\n",
1088 __FUNCTION__);
1089 goto discard_it;
1090 }
1091
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001092 dh = dccp_hdr(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001093
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001094 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
1095 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
1096
1097 dccp_pr_debug("%8.8s "
1098 "src=%u.%u.%u.%u@%-5d "
1099 "dst=%u.%u.%u.%u@%-5d seq=%llu",
1100 dccp_packet_name(dh->dccph_type),
1101 NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport),
1102 NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport),
David S. Millerf6ccf552005-08-09 20:27:14 -07001103 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001104
1105 if (dccp_packet_without_ack(skb)) {
1106 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
1107 dccp_pr_debug_cat("\n");
1108 } else {
1109 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
David S. Millerf6ccf552005-08-09 20:27:14 -07001110 dccp_pr_debug_cat(", ack=%llu\n",
1111 (unsigned long long)
1112 DCCP_SKB_CB(skb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001113 }
1114
1115 /* Step 2:
1116 * Look up flow ID in table and get corresponding socket */
1117 sk = __inet_lookup(&dccp_hashinfo,
1118 skb->nh.iph->saddr, dh->dccph_sport,
1119 skb->nh.iph->daddr, ntohs(dh->dccph_dport),
1120 inet_iif(skb));
1121
1122 /*
1123 * Step 2:
1124 * If no socket ...
1125 * Generate Reset(No Connection) unless P.type == Reset
1126 * Drop packet and return
1127 */
1128 if (sk == NULL) {
1129 dccp_pr_debug("failed to look up flow ID in table and "
1130 "get corresponding socket\n");
1131 goto no_dccp_socket;
1132 }
1133
1134 /*
1135 * Step 2:
1136 * ... or S.state == TIMEWAIT,
1137 * Generate Reset(No Connection) unless P.type == Reset
1138 * Drop packet and return
1139 */
1140
1141 if (sk->sk_state == DCCP_TIME_WAIT) {
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -07001142 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
1143 "do_time_wait\n");
1144 goto do_time_wait;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001145 }
1146
1147 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
1148 dccp_pr_debug("xfrm4_policy_check failed\n");
1149 goto discard_and_relse;
1150 }
1151
1152 if (sk_filter(sk, skb, 0)) {
1153 dccp_pr_debug("sk_filter failed\n");
1154 goto discard_and_relse;
1155 }
1156
1157 skb->dev = NULL;
1158
1159 bh_lock_sock(sk);
1160 rc = 0;
1161 if (!sock_owned_by_user(sk))
1162 rc = dccp_v4_do_rcv(sk, skb);
1163 else
1164 sk_add_backlog(sk, skb);
1165 bh_unlock_sock(sk);
1166
1167 sock_put(sk);
1168 return rc;
1169
1170no_dccp_socket:
1171 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1172 goto discard_it;
1173 /*
1174 * Step 2:
1175 * Generate Reset(No Connection) unless P.type == Reset
1176 * Drop packet and return
1177 */
1178 if (dh->dccph_type != DCCP_PKT_RESET) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001179 DCCP_SKB_CB(skb)->dccpd_reset_code =
1180 DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001181 dccp_v4_ctl_send_reset(skb);
1182 }
1183
1184discard_it:
1185 /* Discard frame. */
1186 kfree_skb(skb);
1187 return 0;
1188
1189discard_and_relse:
1190 sock_put(sk);
1191 goto discard_it;
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -07001192
1193do_time_wait:
1194 inet_twsk_put((struct inet_timewait_sock *)sk);
1195 goto no_dccp_socket;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001196}
1197
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -08001198struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
1199 .queue_xmit = ip_queue_xmit,
1200 .send_check = dccp_v4_send_check,
1201 .rebuild_header = inet_sk_rebuild_header,
1202 .conn_request = dccp_v4_conn_request,
1203 .syn_recv_sock = dccp_v4_request_recv_sock,
1204 .net_header_len = sizeof(struct iphdr),
1205 .setsockopt = ip_setsockopt,
1206 .getsockopt = ip_getsockopt,
1207 .addr2sockaddr = inet_csk_addr2sockaddr,
1208 .sockaddr_len = sizeof(struct sockaddr_in),
1209};
1210
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001211int dccp_v4_init_sock(struct sock *sk)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001212{
1213 struct dccp_sock *dp = dccp_sk(sk);
1214 static int dccp_ctl_socket_init = 1;
1215
1216 dccp_options_init(&dp->dccps_options);
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -03001217 do_gettimeofday(&dp->dccps_epoch);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001218
1219 if (dp->dccps_options.dccpo_send_ack_vector) {
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001220 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(DCCP_MAX_ACKVEC_LEN,
1221 GFP_KERNEL);
1222 if (dp->dccps_hc_rx_ackvec == NULL)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001223 return -ENOMEM;
1224 }
1225
1226 /*
1227 * FIXME: We're hardcoding the CCID, and doing this at this point makes
1228 * the listening (master) sock get CCID control blocks, which is not
1229 * necessary, but for now, to not mess with the test userspace apps,
1230 * lets leave it here, later the real solution is to do this in a
1231 * setsockopt(CCIDs-I-want/accept). -acme
1232 */
1233 if (likely(!dccp_ctl_socket_init)) {
Arnaldo Carvalho de Melo561713c2005-09-18 00:18:52 -07001234 dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_rx_ccid,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001235 sk);
Arnaldo Carvalho de Melo561713c2005-09-18 00:18:52 -07001236 dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_tx_ccid,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001237 sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001238 if (dp->dccps_hc_rx_ccid == NULL ||
1239 dp->dccps_hc_tx_ccid == NULL) {
1240 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1241 ccid_exit(dp->dccps_hc_tx_ccid, sk);
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001242 if (dp->dccps_options.dccpo_send_ack_vector) {
1243 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1244 dp->dccps_hc_rx_ackvec = NULL;
1245 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001246 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1247 return -ENOMEM;
1248 }
1249 } else
1250 dccp_ctl_socket_init = 0;
1251
1252 dccp_init_xmit_timers(sk);
Arnaldo Carvalho de Melo0b4e03b2005-08-09 20:31:11 -07001253 inet_csk(sk)->icsk_rto = DCCP_TIMEOUT_INIT;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001254 sk->sk_state = DCCP_CLOSED;
Arnaldo Carvalho de Meloc530cfb2005-08-29 02:15:54 -03001255 sk->sk_write_space = dccp_write_space;
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -08001256 inet_csk(sk)->icsk_af_ops = &dccp_ipv4_af_ops;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001257 dp->dccps_mss_cache = 536;
1258 dp->dccps_role = DCCP_ROLE_UNDEFINED;
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -07001259 dp->dccps_service = DCCP_SERVICE_INVALID_VALUE;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001260
1261 return 0;
1262}
1263
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001264EXPORT_SYMBOL_GPL(dccp_v4_init_sock);
1265
1266int dccp_v4_destroy_sock(struct sock *sk)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001267{
1268 struct dccp_sock *dp = dccp_sk(sk);
1269
1270 /*
Ian McDonald4c7e6892005-12-21 19:02:39 -08001271 * DCCP doesn't use sk_write_queue, just sk_send_head
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001272 * for retransmissions
1273 */
1274 if (sk->sk_send_head != NULL) {
1275 kfree_skb(sk->sk_send_head);
1276 sk->sk_send_head = NULL;
1277 }
1278
1279 /* Clean up a referenced DCCP bind bucket. */
1280 if (inet_csk(sk)->icsk_bind_hash != NULL)
1281 inet_put_port(&dccp_hashinfo, sk);
1282
Jesper Juhla51482b2005-11-08 09:41:34 -08001283 kfree(dp->dccps_service_list);
1284 dp->dccps_service_list = NULL;
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -07001285
Arnaldo Carvalho de Melo8efa5442005-08-23 21:54:00 -07001286 ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
1287 ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001288 if (dp->dccps_options.dccpo_send_ack_vector) {
1289 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1290 dp->dccps_hc_rx_ackvec = NULL;
1291 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001292 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1293 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1294 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1295
1296 return 0;
1297}
1298
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001299EXPORT_SYMBOL_GPL(dccp_v4_destroy_sock);
1300
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001301static void dccp_v4_reqsk_destructor(struct request_sock *req)
1302{
1303 kfree(inet_rsk(req)->opt);
1304}
1305
1306static struct request_sock_ops dccp_request_sock_ops = {
1307 .family = PF_INET,
1308 .obj_size = sizeof(struct dccp_request_sock),
1309 .rtx_syn_ack = dccp_v4_send_response,
1310 .send_ack = dccp_v4_reqsk_send_ack,
1311 .destructor = dccp_v4_reqsk_destructor,
1312 .send_reset = dccp_v4_ctl_send_reset,
1313};
1314
Arnaldo Carvalho de Melo34ca6862005-12-13 23:23:32 -08001315struct proto dccp_prot = {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001316 .name = "DCCP",
1317 .owner = THIS_MODULE,
1318 .close = dccp_close,
1319 .connect = dccp_v4_connect,
1320 .disconnect = dccp_disconnect,
1321 .ioctl = dccp_ioctl,
1322 .init = dccp_v4_init_sock,
1323 .setsockopt = dccp_setsockopt,
1324 .getsockopt = dccp_getsockopt,
1325 .sendmsg = dccp_sendmsg,
1326 .recvmsg = dccp_recvmsg,
1327 .backlog_rcv = dccp_v4_do_rcv,
1328 .hash = dccp_v4_hash,
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001329 .unhash = dccp_unhash,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001330 .accept = inet_csk_accept,
1331 .get_port = dccp_v4_get_port,
1332 .shutdown = dccp_shutdown,
1333 .destroy = dccp_v4_destroy_sock,
1334 .orphan_count = &dccp_orphan_count,
1335 .max_header = MAX_DCCP_HEADER,
1336 .obj_size = sizeof(struct dccp_sock),
1337 .rsk_prot = &dccp_request_sock_ops,
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -07001338 .twsk_obj_size = sizeof(struct inet_timewait_sock),
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001339};