blob: 3f244670764ae94fad1826439a748f860d1b808c [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>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020022#include <net/inet_sock.h>
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070023#include <net/sock.h>
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -080024#include <net/timewait_sock.h>
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070025#include <net/tcp_states.h>
26#include <net/xfrm.h>
27
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -070028#include "ackvec.h"
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070029#include "ccid.h"
30#include "dccp.h"
31
32struct inet_hashinfo __cacheline_aligned dccp_hashinfo = {
33 .lhash_lock = RW_LOCK_UNLOCKED,
34 .lhash_users = ATOMIC_INIT(0),
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -030035 .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait),
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070036};
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{
Arnaldo Carvalho de Melo971af182005-12-13 23:14:47 -080042 return inet_csk_get_port(&dccp_hashinfo, sk, snum,
43 inet_csk_bind_conflict);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070044}
45
46static void dccp_v4_hash(struct sock *sk)
47{
48 inet_hash(&dccp_hashinfo, sk);
49}
50
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -080051void dccp_unhash(struct sock *sk)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070052{
53 inet_unhash(&dccp_hashinfo, sk);
54}
55
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -080056EXPORT_SYMBOL_GPL(dccp_unhash);
57
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -080058int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070059{
60 struct inet_sock *inet = inet_sk(sk);
61 struct dccp_sock *dp = dccp_sk(sk);
62 const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
63 struct rtable *rt;
64 u32 daddr, nexthop;
65 int tmp;
66 int err;
67
68 dp->dccps_role = DCCP_ROLE_CLIENT;
69
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -070070 if (dccp_service_not_initialized(sk))
71 return -EPROTO;
72
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070073 if (addr_len < sizeof(struct sockaddr_in))
74 return -EINVAL;
75
76 if (usin->sin_family != AF_INET)
77 return -EAFNOSUPPORT;
78
79 nexthop = daddr = usin->sin_addr.s_addr;
80 if (inet->opt != NULL && inet->opt->srr) {
81 if (daddr == 0)
82 return -EINVAL;
83 nexthop = inet->opt->faddr;
84 }
85
86 tmp = ip_route_connect(&rt, nexthop, inet->saddr,
87 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
88 IPPROTO_DCCP,
89 inet->sport, usin->sin_port, sk);
90 if (tmp < 0)
91 return tmp;
92
93 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
94 ip_rt_put(rt);
95 return -ENETUNREACH;
96 }
97
98 if (inet->opt == NULL || !inet->opt->srr)
99 daddr = rt->rt_dst;
100
101 if (inet->saddr == 0)
102 inet->saddr = rt->rt_src;
103 inet->rcv_saddr = inet->saddr;
104
105 inet->dport = usin->sin_port;
106 inet->daddr = daddr;
107
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800108 inet_csk(sk)->icsk_ext_hdr_len = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700109 if (inet->opt != NULL)
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800110 inet_csk(sk)->icsk_ext_hdr_len = inet->opt->optlen;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700111 /*
112 * Socket identity is still unknown (sport may be zero).
113 * However we set state to DCCP_REQUESTING and not releasing socket
114 * lock select source port, enter ourselves into the hash tables and
115 * complete initialization after this.
116 */
117 dccp_set_state(sk, DCCP_REQUESTING);
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800118 err = inet_hash_connect(&dccp_death_row, sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700119 if (err != 0)
120 goto failure;
121
122 err = ip_route_newports(&rt, inet->sport, inet->dport, sk);
123 if (err != 0)
124 goto failure;
125
126 /* OK, now commit destination to socket. */
127 sk_setup_caps(sk, &rt->u.dst);
128
129 dp->dccps_gar =
130 dp->dccps_iss = secure_dccp_sequence_number(inet->saddr,
131 inet->daddr,
132 inet->sport,
133 usin->sin_port);
134 dccp_update_gss(sk, dp->dccps_iss);
135
136 inet->id = dp->dccps_iss ^ jiffies;
137
138 err = dccp_connect(sk);
139 rt = NULL;
140 if (err != 0)
141 goto failure;
142out:
143 return err;
144failure:
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300145 /*
146 * This unhashes the socket and releases the local port, if necessary.
147 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700148 dccp_set_state(sk, DCCP_CLOSED);
149 ip_rt_put(rt);
150 sk->sk_route_caps = 0;
151 inet->dport = 0;
152 goto out;
153}
154
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800155EXPORT_SYMBOL_GPL(dccp_v4_connect);
156
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700157/*
158 * This routine does path mtu discovery as defined in RFC1191.
159 */
160static inline void dccp_do_pmtu_discovery(struct sock *sk,
161 const struct iphdr *iph,
162 u32 mtu)
163{
164 struct dst_entry *dst;
165 const struct inet_sock *inet = inet_sk(sk);
166 const struct dccp_sock *dp = dccp_sk(sk);
167
168 /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
169 * send out by Linux are always < 576bytes so they should go through
170 * unfragmented).
171 */
172 if (sk->sk_state == DCCP_LISTEN)
173 return;
174
175 /* We don't check in the destentry if pmtu discovery is forbidden
176 * on this route. We just assume that no packet_to_big packets
177 * are send back when pmtu discovery is not active.
178 * There is a small race when the user changes this flag in the
179 * route, but I think that's acceptable.
180 */
181 if ((dst = __sk_dst_check(sk, 0)) == NULL)
182 return;
183
184 dst->ops->update_pmtu(dst, mtu);
185
186 /* Something is about to be wrong... Remember soft error
187 * for the case, if this connection will not able to recover.
188 */
189 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
190 sk->sk_err_soft = EMSGSIZE;
191
192 mtu = dst_mtu(dst);
193
194 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800195 inet_csk(sk)->icsk_pmtu_cookie > mtu) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700196 dccp_sync_mss(sk, mtu);
197
198 /*
199 * From: draft-ietf-dccp-spec-11.txt
200 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300201 * DCCP-Sync packets are the best choice for upward
202 * probing, since DCCP-Sync probes do not risk application
203 * data loss.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700204 */
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300205 dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700206 } /* else let the usual retransmit timer handle it */
207}
208
209static void dccp_v4_ctl_send_ack(struct sk_buff *rxskb)
210{
211 int err;
212 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
213 const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) +
214 sizeof(struct dccp_hdr_ext) +
215 sizeof(struct dccp_hdr_ack_bits);
216 struct sk_buff *skb;
217
218 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
219 return;
220
221 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
222 if (skb == NULL)
223 return;
224
225 /* Reserve space for headers. */
226 skb_reserve(skb, MAX_DCCP_HEADER);
227
228 skb->dst = dst_clone(rxskb->dst);
229
230 skb->h.raw = skb_push(skb, dccp_hdr_ack_len);
231 dh = dccp_hdr(skb);
232 memset(dh, 0, dccp_hdr_ack_len);
233
234 /* Build DCCP header and checksum it. */
235 dh->dccph_type = DCCP_PKT_ACK;
236 dh->dccph_sport = rxdh->dccph_dport;
237 dh->dccph_dport = rxdh->dccph_sport;
238 dh->dccph_doff = dccp_hdr_ack_len / 4;
239 dh->dccph_x = 1;
240
241 dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300242 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
243 DCCP_SKB_CB(rxskb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700244
245 bh_lock_sock(dccp_ctl_socket->sk);
246 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300247 rxskb->nh.iph->daddr,
248 rxskb->nh.iph->saddr, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700249 bh_unlock_sock(dccp_ctl_socket->sk);
250
251 if (err == NET_XMIT_CN || err == 0) {
252 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
253 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
254 }
255}
256
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300257static void dccp_v4_reqsk_send_ack(struct sk_buff *skb,
258 struct request_sock *req)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700259{
260 dccp_v4_ctl_send_ack(skb);
261}
262
263static int dccp_v4_send_response(struct sock *sk, struct request_sock *req,
264 struct dst_entry *dst)
265{
266 int err = -1;
267 struct sk_buff *skb;
268
269 /* First, grab a route. */
270
271 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
272 goto out;
273
274 skb = dccp_make_response(sk, dst, req);
275 if (skb != NULL) {
276 const struct inet_request_sock *ireq = inet_rsk(req);
277
Herbert Xu49c5bfa2005-10-18 12:03:28 +1000278 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700279 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
280 ireq->rmt_addr,
281 ireq->opt);
282 if (err == NET_XMIT_CN)
283 err = 0;
284 }
285
286out:
287 dst_release(dst);
288 return err;
289}
290
291/*
292 * This routine is called by the ICMP module when it gets some sort of error
293 * condition. If err < 0 then the socket should be closed and the error
294 * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
295 * After adjustment header points to the first 8 bytes of the tcp header. We
296 * need to find the appropriate port.
297 *
298 * The locking strategy used here is very "optimistic". When someone else
299 * accesses the socket the ICMP is just dropped and for some paths there is no
300 * check at all. A more general error queue to queue errors for later handling
301 * is probably better.
302 */
303void dccp_v4_err(struct sk_buff *skb, u32 info)
304{
305 const struct iphdr *iph = (struct iphdr *)skb->data;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300306 const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data +
307 (iph->ihl << 2));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700308 struct dccp_sock *dp;
309 struct inet_sock *inet;
310 const int type = skb->h.icmph->type;
311 const int code = skb->h.icmph->code;
312 struct sock *sk;
313 __u64 seq;
314 int err;
315
316 if (skb->len < (iph->ihl << 2) + 8) {
317 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
318 return;
319 }
320
321 sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport,
322 iph->saddr, dh->dccph_sport, inet_iif(skb));
323 if (sk == NULL) {
324 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
325 return;
326 }
327
328 if (sk->sk_state == DCCP_TIME_WAIT) {
329 inet_twsk_put((struct inet_timewait_sock *)sk);
330 return;
331 }
332
333 bh_lock_sock(sk);
334 /* If too many ICMPs get dropped on busy
335 * servers this needs to be solved differently.
336 */
337 if (sock_owned_by_user(sk))
338 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
339
340 if (sk->sk_state == DCCP_CLOSED)
341 goto out;
342
343 dp = dccp_sk(sk);
344 seq = dccp_hdr_seq(skb);
345 if (sk->sk_state != DCCP_LISTEN &&
346 !between48(seq, dp->dccps_swl, dp->dccps_swh)) {
347 NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS);
348 goto out;
349 }
350
351 switch (type) {
352 case ICMP_SOURCE_QUENCH:
353 /* Just silently ignore these. */
354 goto out;
355 case ICMP_PARAMETERPROB:
356 err = EPROTO;
357 break;
358 case ICMP_DEST_UNREACH:
359 if (code > NR_ICMP_UNREACH)
360 goto out;
361
362 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
363 if (!sock_owned_by_user(sk))
364 dccp_do_pmtu_discovery(sk, iph, info);
365 goto out;
366 }
367
368 err = icmp_err_convert[code].errno;
369 break;
370 case ICMP_TIME_EXCEEDED:
371 err = EHOSTUNREACH;
372 break;
373 default:
374 goto out;
375 }
376
377 switch (sk->sk_state) {
378 struct request_sock *req , **prev;
379 case DCCP_LISTEN:
380 if (sock_owned_by_user(sk))
381 goto out;
382 req = inet_csk_search_req(sk, &prev, dh->dccph_dport,
383 iph->daddr, iph->saddr);
384 if (!req)
385 goto out;
386
387 /*
388 * ICMPs are not backlogged, hence we cannot get an established
389 * socket here.
390 */
391 BUG_TRAP(!req->sk);
392
393 if (seq != dccp_rsk(req)->dreq_iss) {
394 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
395 goto out;
396 }
397 /*
398 * Still in RESPOND, just remove it silently.
399 * There is no good way to pass the error to the newly
400 * created socket, and POSIX does not want network
401 * errors returned from accept().
402 */
403 inet_csk_reqsk_queue_drop(sk, req, prev);
404 goto out;
405
406 case DCCP_REQUESTING:
407 case DCCP_RESPOND:
408 if (!sock_owned_by_user(sk)) {
409 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
410 sk->sk_err = err;
411
412 sk->sk_error_report(sk);
413
414 dccp_done(sk);
415 } else
416 sk->sk_err_soft = err;
417 goto out;
418 }
419
420 /* If we've already connected we will keep trying
421 * until we time out, or the user gives up.
422 *
423 * rfc1122 4.2.3.9 allows to consider as hard errors
424 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
425 * but it is obsoleted by pmtu discovery).
426 *
427 * Note, that in modern internet, where routing is unreliable
428 * and in each dark corner broken firewalls sit, sending random
429 * errors ordered by their masters even this two messages finally lose
430 * their original sense (even Linux sends invalid PORT_UNREACHs)
431 *
432 * Now we are in compliance with RFCs.
433 * --ANK (980905)
434 */
435
436 inet = inet_sk(sk);
437 if (!sock_owned_by_user(sk) && inet->recverr) {
438 sk->sk_err = err;
439 sk->sk_error_report(sk);
440 } else /* Only an error on timeout */
441 sk->sk_err_soft = err;
442out:
443 bh_unlock_sock(sk);
444 sock_put(sk);
445}
446
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800447/* This routine computes an IPv4 DCCP checksum. */
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800448void dccp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800449{
450 const struct inet_sock *inet = inet_sk(sk);
451 struct dccp_hdr *dh = dccp_hdr(skb);
452
453 dh->dccph_checksum = dccp_v4_checksum(skb, inet->saddr, inet->daddr);
454}
455
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800456EXPORT_SYMBOL_GPL(dccp_v4_send_check);
457
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700458int dccp_v4_send_reset(struct sock *sk, enum dccp_reset_codes code)
459{
460 struct sk_buff *skb;
461 /*
462 * FIXME: what if rebuild_header fails?
463 * Should we be doing a rebuild_header here?
464 */
465 int err = inet_sk_rebuild_header(sk);
466
467 if (err != 0)
468 return err;
469
470 skb = dccp_make_reset(sk, sk->sk_dst_cache, code);
471 if (skb != NULL) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700472 const struct inet_sock *inet = inet_sk(sk);
473
Herbert Xu49c5bfa2005-10-18 12:03:28 +1000474 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700475 err = ip_build_and_send_pkt(skb, sk,
476 inet->saddr, inet->daddr, NULL);
477 if (err == NET_XMIT_CN)
478 err = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700479 }
480
481 return err;
482}
483
484static inline u64 dccp_v4_init_sequence(const struct sock *sk,
485 const struct sk_buff *skb)
486{
487 return secure_dccp_sequence_number(skb->nh.iph->daddr,
488 skb->nh.iph->saddr,
489 dccp_hdr(skb)->dccph_dport,
490 dccp_hdr(skb)->dccph_sport);
491}
492
493int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
494{
495 struct inet_request_sock *ireq;
496 struct dccp_sock dp;
497 struct request_sock *req;
498 struct dccp_request_sock *dreq;
499 const __u32 saddr = skb->nh.iph->saddr;
500 const __u32 daddr = skb->nh.iph->daddr;
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700501 const __u32 service = dccp_hdr_request(skb)->dccph_req_service;
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700502 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
503 __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700504
505 /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
506 if (((struct rtable *)skb->dst)->rt_flags &
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700507 (RTCF_BROADCAST | RTCF_MULTICAST)) {
508 reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700509 goto drop;
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700510 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700511
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700512 if (dccp_bad_service_code(sk, service)) {
513 reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
514 goto drop;
515 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700516 /*
517 * TW buckets are converted to open requests without
518 * limitations, they conserve resources and peer is
519 * evidently real one.
520 */
521 if (inet_csk_reqsk_queue_is_full(sk))
522 goto drop;
523
524 /*
525 * Accept backlog is full. If we have already queued enough
526 * of warm entries in syn queue, drop request. It is better than
527 * clogging syn queue with openreqs with exponentially increasing
528 * timeout.
529 */
530 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
531 goto drop;
532
533 req = reqsk_alloc(sk->sk_prot->rsk_prot);
534 if (req == NULL)
535 goto drop;
536
537 /* FIXME: process options */
538
539 dccp_openreq_init(req, &dp, skb);
540
541 ireq = inet_rsk(req);
542 ireq->loc_addr = daddr;
543 ireq->rmt_addr = saddr;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300544 req->rcv_wnd = 100; /* Fake, option parsing will get the
545 right value */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700546 ireq->opt = NULL;
547
548 /*
549 * Step 3: Process LISTEN state
550 *
551 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
552 *
553 * In fact we defer setting S.GSR, S.SWL, S.SWH to
554 * dccp_create_openreq_child.
555 */
556 dreq = dccp_rsk(req);
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700557 dreq->dreq_isr = dcb->dccpd_seq;
558 dreq->dreq_iss = dccp_v4_init_sequence(sk, skb);
559 dreq->dreq_service = service;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700560
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800561 if (dccp_v4_send_response(sk, req, NULL))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700562 goto drop_and_free;
563
564 inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
565 return 0;
566
567drop_and_free:
Arnaldo Carvalho de Melofc44b982005-12-13 23:25:06 -0800568 reqsk_free(req);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700569drop:
570 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700571 dcb->dccpd_reset_code = reset_code;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700572 return -1;
573}
574
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800575EXPORT_SYMBOL_GPL(dccp_v4_conn_request);
576
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700577/*
578 * The three way handshake has completed - we got a valid ACK or DATAACK -
579 * now create the new socket.
580 *
581 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
582 */
583struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
584 struct request_sock *req,
585 struct dst_entry *dst)
586{
587 struct inet_request_sock *ireq;
588 struct inet_sock *newinet;
589 struct dccp_sock *newdp;
590 struct sock *newsk;
591
592 if (sk_acceptq_is_full(sk))
593 goto exit_overflow;
594
595 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
596 goto exit;
597
598 newsk = dccp_create_openreq_child(sk, req, skb);
599 if (newsk == NULL)
600 goto exit;
601
602 sk_setup_caps(newsk, dst);
603
604 newdp = dccp_sk(newsk);
605 newinet = inet_sk(newsk);
606 ireq = inet_rsk(req);
607 newinet->daddr = ireq->rmt_addr;
608 newinet->rcv_saddr = ireq->loc_addr;
609 newinet->saddr = ireq->loc_addr;
610 newinet->opt = ireq->opt;
611 ireq->opt = NULL;
612 newinet->mc_index = inet_iif(skb);
613 newinet->mc_ttl = skb->nh.iph->ttl;
614 newinet->id = jiffies;
615
616 dccp_sync_mss(newsk, dst_mtu(dst));
617
618 __inet_hash(&dccp_hashinfo, newsk, 0);
619 __inet_inherit_port(&dccp_hashinfo, sk, newsk);
620
621 return newsk;
622
623exit_overflow:
624 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
625exit:
626 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
627 dst_release(dst);
628 return NULL;
629}
630
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800631EXPORT_SYMBOL_GPL(dccp_v4_request_recv_sock);
632
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700633static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
634{
635 const struct dccp_hdr *dh = dccp_hdr(skb);
636 const struct iphdr *iph = skb->nh.iph;
637 struct sock *nsk;
638 struct request_sock **prev;
639 /* Find possible connection requests. */
640 struct request_sock *req = inet_csk_search_req(sk, &prev,
641 dh->dccph_sport,
642 iph->saddr, iph->daddr);
643 if (req != NULL)
644 return dccp_check_req(sk, skb, req, prev);
645
646 nsk = __inet_lookup_established(&dccp_hashinfo,
647 iph->saddr, dh->dccph_sport,
648 iph->daddr, ntohs(dh->dccph_dport),
649 inet_iif(skb));
650 if (nsk != NULL) {
651 if (nsk->sk_state != DCCP_TIME_WAIT) {
652 bh_lock_sock(nsk);
653 return nsk;
654 }
655 inet_twsk_put((struct inet_timewait_sock *)nsk);
656 return NULL;
657 }
658
659 return sk;
660}
661
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300662int dccp_v4_checksum(const struct sk_buff *skb, const u32 saddr,
663 const u32 daddr)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700664{
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700665 const struct dccp_hdr* dh = dccp_hdr(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700666 int checksum_len;
667 u32 tmp;
668
669 if (dh->dccph_cscov == 0)
670 checksum_len = skb->len;
671 else {
672 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300673 checksum_len = checksum_len < skb->len ? checksum_len :
674 skb->len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700675 }
676
677 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300678 return csum_tcpudp_magic(saddr, daddr, checksum_len,
679 IPPROTO_DCCP, tmp);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700680}
681
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700682static int dccp_v4_verify_checksum(struct sk_buff *skb,
683 const u32 saddr, const u32 daddr)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700684{
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700685 struct dccp_hdr *dh = dccp_hdr(skb);
686 int checksum_len;
687 u32 tmp;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700688
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700689 if (dh->dccph_cscov == 0)
690 checksum_len = skb->len;
691 else {
692 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300693 checksum_len = checksum_len < skb->len ? checksum_len :
694 skb->len;
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700695 }
696 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300697 return csum_tcpudp_magic(saddr, daddr, checksum_len,
698 IPPROTO_DCCP, tmp) == 0 ? 0 : -1;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700699}
700
701static struct dst_entry* dccp_v4_route_skb(struct sock *sk,
702 struct sk_buff *skb)
703{
704 struct rtable *rt;
705 struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif,
706 .nl_u = { .ip4_u =
707 { .daddr = skb->nh.iph->saddr,
708 .saddr = skb->nh.iph->daddr,
709 .tos = RT_CONN_FLAGS(sk) } },
710 .proto = sk->sk_protocol,
711 .uli_u = { .ports =
712 { .sport = dccp_hdr(skb)->dccph_dport,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300713 .dport = dccp_hdr(skb)->dccph_sport }
714 }
715 };
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700716
717 if (ip_route_output_flow(&rt, &fl, sk, 0)) {
718 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
719 return NULL;
720 }
721
722 return &rt->u.dst;
723}
724
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300725static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700726{
727 int err;
728 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
729 const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
730 sizeof(struct dccp_hdr_ext) +
731 sizeof(struct dccp_hdr_reset);
732 struct sk_buff *skb;
733 struct dst_entry *dst;
Arnaldo Carvalho de Melo2807d4f2005-08-21 05:33:48 -0300734 u64 seqno;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700735
736 /* Never send a reset in response to a reset. */
737 if (rxdh->dccph_type == DCCP_PKT_RESET)
738 return;
739
740 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
741 return;
742
743 dst = dccp_v4_route_skb(dccp_ctl_socket->sk, rxskb);
744 if (dst == NULL)
745 return;
746
747 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
748 if (skb == NULL)
749 goto out;
750
751 /* Reserve space for headers. */
752 skb_reserve(skb, MAX_DCCP_HEADER);
753 skb->dst = dst_clone(dst);
754
755 skb->h.raw = skb_push(skb, dccp_hdr_reset_len);
756 dh = dccp_hdr(skb);
757 memset(dh, 0, dccp_hdr_reset_len);
758
759 /* Build DCCP header and checksum it. */
760 dh->dccph_type = DCCP_PKT_RESET;
761 dh->dccph_sport = rxdh->dccph_dport;
762 dh->dccph_dport = rxdh->dccph_sport;
763 dh->dccph_doff = dccp_hdr_reset_len / 4;
764 dh->dccph_x = 1;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300765 dccp_hdr_reset(skb)->dccph_reset_code =
766 DCCP_SKB_CB(rxskb)->dccpd_reset_code;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700767
Arnaldo Carvalho de Melo2807d4f2005-08-21 05:33:48 -0300768 /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
769 seqno = 0;
770 if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
771 dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
772
773 dccp_hdr_set_seq(dh, seqno);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300774 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
775 DCCP_SKB_CB(rxskb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700776
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700777 dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr,
778 rxskb->nh.iph->daddr);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700779
780 bh_lock_sock(dccp_ctl_socket->sk);
781 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300782 rxskb->nh.iph->daddr,
783 rxskb->nh.iph->saddr, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700784 bh_unlock_sock(dccp_ctl_socket->sk);
785
786 if (err == NET_XMIT_CN || err == 0) {
787 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
788 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
789 }
790out:
791 dst_release(dst);
792}
793
794int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
795{
796 struct dccp_hdr *dh = dccp_hdr(skb);
797
798 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
799 if (dccp_rcv_established(sk, skb, dh, skb->len))
800 goto reset;
801 return 0;
802 }
803
804 /*
805 * Step 3: Process LISTEN state
806 * If S.state == LISTEN,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300807 * If P.type == Request or P contains a valid Init Cookie
808 * option,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700809 * * Must scan the packet's options to check for an Init
810 * Cookie. Only the Init Cookie is processed here,
811 * however; other options are processed in Step 8. This
812 * scan need only be performed if the endpoint uses Init
813 * Cookies *
814 * * Generate a new socket and switch to that socket *
815 * Set S := new socket for this port pair
816 * S.state = RESPOND
817 * Choose S.ISS (initial seqno) or set from Init Cookie
818 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
819 * Continue with S.state == RESPOND
820 * * A Response packet will be generated in Step 11 *
821 * Otherwise,
822 * Generate Reset(No Connection) unless P.type == Reset
823 * Drop packet and return
824 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300825 * NOTE: the check for the packet types is done in
826 * dccp_rcv_state_process
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700827 */
828 if (sk->sk_state == DCCP_LISTEN) {
829 struct sock *nsk = dccp_v4_hnd_req(sk, skb);
830
831 if (nsk == NULL)
832 goto discard;
833
834 if (nsk != sk) {
835 if (dccp_child_process(sk, nsk, skb))
836 goto reset;
837 return 0;
838 }
839 }
840
841 if (dccp_rcv_state_process(sk, skb, dh, skb->len))
842 goto reset;
843 return 0;
844
845reset:
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700846 dccp_v4_ctl_send_reset(skb);
847discard:
848 kfree_skb(skb);
849 return 0;
850}
851
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800852EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
853
854int dccp_invalid_packet(struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700855{
856 const struct dccp_hdr *dh;
857
858 if (skb->pkt_type != PACKET_HOST)
859 return 1;
860
861 if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -0300862 LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700863 return 1;
864 }
865
866 dh = dccp_hdr(skb);
867
868 /* If the packet type is not understood, drop packet and return */
869 if (dh->dccph_type >= DCCP_PKT_INVALID) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -0300870 LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700871 return 1;
872 }
873
874 /*
875 * If P.Data Offset is too small for packet type, or too large for
876 * packet, drop packet and return
877 */
878 if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -0300879 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
880 "too small 1\n",
881 dh->dccph_doff);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700882 return 1;
883 }
884
885 if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -0300886 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
887 "too small 2\n",
888 dh->dccph_doff);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700889 return 1;
890 }
891
892 dh = dccp_hdr(skb);
893
894 /*
895 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
896 * has short sequence numbers), drop packet and return
897 */
898 if (dh->dccph_x == 0 &&
899 dh->dccph_type != DCCP_PKT_DATA &&
900 dh->dccph_type != DCCP_PKT_ACK &&
901 dh->dccph_type != DCCP_PKT_DATAACK) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -0300902 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack "
903 "nor DataAck and P.X == 0\n",
904 dccp_packet_name(dh->dccph_type));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700905 return 1;
906 }
907
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700908 return 0;
909}
910
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800911EXPORT_SYMBOL_GPL(dccp_invalid_packet);
912
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700913/* this is called when real data arrives */
914int dccp_v4_rcv(struct sk_buff *skb)
915{
916 const struct dccp_hdr *dh;
917 struct sock *sk;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700918
919 /* Step 1: Check header basics: */
920
921 if (dccp_invalid_packet(skb))
922 goto discard_it;
923
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800924 /* If the header checksum is incorrect, drop packet and return */
925 if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr,
926 skb->nh.iph->daddr) < 0) {
927 LIMIT_NETDEBUG(KERN_WARNING "%s: incorrect header checksum\n",
928 __FUNCTION__);
929 goto discard_it;
930 }
931
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700932 dh = dccp_hdr(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700933
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700934 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
935 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
936
937 dccp_pr_debug("%8.8s "
938 "src=%u.%u.%u.%u@%-5d "
939 "dst=%u.%u.%u.%u@%-5d seq=%llu",
940 dccp_packet_name(dh->dccph_type),
941 NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport),
942 NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport),
David S. Millerf6ccf552005-08-09 20:27:14 -0700943 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700944
945 if (dccp_packet_without_ack(skb)) {
946 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
947 dccp_pr_debug_cat("\n");
948 } else {
949 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
David S. Millerf6ccf552005-08-09 20:27:14 -0700950 dccp_pr_debug_cat(", ack=%llu\n",
951 (unsigned long long)
952 DCCP_SKB_CB(skb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700953 }
954
955 /* Step 2:
956 * Look up flow ID in table and get corresponding socket */
957 sk = __inet_lookup(&dccp_hashinfo,
958 skb->nh.iph->saddr, dh->dccph_sport,
959 skb->nh.iph->daddr, ntohs(dh->dccph_dport),
960 inet_iif(skb));
961
962 /*
963 * Step 2:
964 * If no socket ...
965 * Generate Reset(No Connection) unless P.type == Reset
966 * Drop packet and return
967 */
968 if (sk == NULL) {
969 dccp_pr_debug("failed to look up flow ID in table and "
970 "get corresponding socket\n");
971 goto no_dccp_socket;
972 }
973
974 /*
975 * Step 2:
976 * ... or S.state == TIMEWAIT,
977 * Generate Reset(No Connection) unless P.type == Reset
978 * Drop packet and return
979 */
980
981 if (sk->sk_state == DCCP_TIME_WAIT) {
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -0700982 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
983 "do_time_wait\n");
984 goto do_time_wait;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700985 }
986
Arnaldo Carvalho de Melo25995ff2005-12-27 02:42:22 -0200987 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700988 goto discard_and_relse;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700989
Arnaldo Carvalho de Melo25995ff2005-12-27 02:42:22 -0200990 return sk_receive_skb(sk, skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700991
992no_dccp_socket:
993 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
994 goto discard_it;
995 /*
996 * Step 2:
997 * Generate Reset(No Connection) unless P.type == Reset
998 * Drop packet and return
999 */
1000 if (dh->dccph_type != DCCP_PKT_RESET) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001001 DCCP_SKB_CB(skb)->dccpd_reset_code =
1002 DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001003 dccp_v4_ctl_send_reset(skb);
1004 }
1005
1006discard_it:
1007 /* Discard frame. */
1008 kfree_skb(skb);
1009 return 0;
1010
1011discard_and_relse:
1012 sock_put(sk);
1013 goto discard_it;
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -07001014
1015do_time_wait:
1016 inet_twsk_put((struct inet_timewait_sock *)sk);
1017 goto no_dccp_socket;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001018}
1019
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -08001020struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
1021 .queue_xmit = ip_queue_xmit,
1022 .send_check = dccp_v4_send_check,
1023 .rebuild_header = inet_sk_rebuild_header,
1024 .conn_request = dccp_v4_conn_request,
1025 .syn_recv_sock = dccp_v4_request_recv_sock,
1026 .net_header_len = sizeof(struct iphdr),
1027 .setsockopt = ip_setsockopt,
1028 .getsockopt = ip_getsockopt,
1029 .addr2sockaddr = inet_csk_addr2sockaddr,
1030 .sockaddr_len = sizeof(struct sockaddr_in),
1031};
1032
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001033int dccp_v4_init_sock(struct sock *sk)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001034{
1035 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001036 struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001037 static int dccp_ctl_socket_init = 1;
1038
1039 dccp_options_init(&dp->dccps_options);
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -03001040 do_gettimeofday(&dp->dccps_epoch);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001041
1042 if (dp->dccps_options.dccpo_send_ack_vector) {
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001043 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(DCCP_MAX_ACKVEC_LEN,
1044 GFP_KERNEL);
1045 if (dp->dccps_hc_rx_ackvec == NULL)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001046 return -ENOMEM;
1047 }
1048
1049 /*
1050 * FIXME: We're hardcoding the CCID, and doing this at this point makes
1051 * the listening (master) sock get CCID control blocks, which is not
1052 * necessary, but for now, to not mess with the test userspace apps,
1053 * lets leave it here, later the real solution is to do this in a
1054 * setsockopt(CCIDs-I-want/accept). -acme
1055 */
1056 if (likely(!dccp_ctl_socket_init)) {
Arnaldo Carvalho de Melo561713c2005-09-18 00:18:52 -07001057 dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_rx_ccid,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001058 sk);
Arnaldo Carvalho de Melo561713c2005-09-18 00:18:52 -07001059 dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_tx_ccid,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001060 sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001061 if (dp->dccps_hc_rx_ccid == NULL ||
1062 dp->dccps_hc_tx_ccid == NULL) {
1063 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1064 ccid_exit(dp->dccps_hc_tx_ccid, sk);
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001065 if (dp->dccps_options.dccpo_send_ack_vector) {
1066 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1067 dp->dccps_hc_rx_ackvec = NULL;
1068 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001069 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1070 return -ENOMEM;
1071 }
1072 } else
1073 dccp_ctl_socket_init = 0;
1074
1075 dccp_init_xmit_timers(sk);
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001076 icsk->icsk_rto = DCCP_TIMEOUT_INIT;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001077 sk->sk_state = DCCP_CLOSED;
Arnaldo Carvalho de Meloc530cfb2005-08-29 02:15:54 -03001078 sk->sk_write_space = dccp_write_space;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001079 icsk->icsk_af_ops = &dccp_ipv4_af_ops;
1080 icsk->icsk_sync_mss = dccp_sync_mss;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001081 dp->dccps_mss_cache = 536;
1082 dp->dccps_role = DCCP_ROLE_UNDEFINED;
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -07001083 dp->dccps_service = DCCP_SERVICE_INVALID_VALUE;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001084
1085 return 0;
1086}
1087
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001088EXPORT_SYMBOL_GPL(dccp_v4_init_sock);
1089
1090int dccp_v4_destroy_sock(struct sock *sk)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001091{
1092 struct dccp_sock *dp = dccp_sk(sk);
1093
1094 /*
Ian McDonald4c7e6892005-12-21 19:02:39 -08001095 * DCCP doesn't use sk_write_queue, just sk_send_head
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001096 * for retransmissions
1097 */
1098 if (sk->sk_send_head != NULL) {
1099 kfree_skb(sk->sk_send_head);
1100 sk->sk_send_head = NULL;
1101 }
1102
1103 /* Clean up a referenced DCCP bind bucket. */
1104 if (inet_csk(sk)->icsk_bind_hash != NULL)
1105 inet_put_port(&dccp_hashinfo, sk);
1106
Jesper Juhla51482b2005-11-08 09:41:34 -08001107 kfree(dp->dccps_service_list);
1108 dp->dccps_service_list = NULL;
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -07001109
Arnaldo Carvalho de Melo8efa5442005-08-23 21:54:00 -07001110 ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
1111 ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001112 if (dp->dccps_options.dccpo_send_ack_vector) {
1113 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1114 dp->dccps_hc_rx_ackvec = NULL;
1115 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001116 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1117 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1118 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1119
1120 return 0;
1121}
1122
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001123EXPORT_SYMBOL_GPL(dccp_v4_destroy_sock);
1124
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001125static void dccp_v4_reqsk_destructor(struct request_sock *req)
1126{
1127 kfree(inet_rsk(req)->opt);
1128}
1129
1130static struct request_sock_ops dccp_request_sock_ops = {
1131 .family = PF_INET,
1132 .obj_size = sizeof(struct dccp_request_sock),
1133 .rtx_syn_ack = dccp_v4_send_response,
1134 .send_ack = dccp_v4_reqsk_send_ack,
1135 .destructor = dccp_v4_reqsk_destructor,
1136 .send_reset = dccp_v4_ctl_send_reset,
1137};
1138
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08001139static struct timewait_sock_ops dccp_timewait_sock_ops = {
1140 .twsk_obj_size = sizeof(struct inet_timewait_sock),
1141};
1142
Arnaldo Carvalho de Melo34ca6862005-12-13 23:23:32 -08001143struct proto dccp_prot = {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001144 .name = "DCCP",
1145 .owner = THIS_MODULE,
1146 .close = dccp_close,
1147 .connect = dccp_v4_connect,
1148 .disconnect = dccp_disconnect,
1149 .ioctl = dccp_ioctl,
1150 .init = dccp_v4_init_sock,
1151 .setsockopt = dccp_setsockopt,
1152 .getsockopt = dccp_getsockopt,
1153 .sendmsg = dccp_sendmsg,
1154 .recvmsg = dccp_recvmsg,
1155 .backlog_rcv = dccp_v4_do_rcv,
1156 .hash = dccp_v4_hash,
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001157 .unhash = dccp_unhash,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001158 .accept = inet_csk_accept,
1159 .get_port = dccp_v4_get_port,
1160 .shutdown = dccp_shutdown,
1161 .destroy = dccp_v4_destroy_sock,
1162 .orphan_count = &dccp_orphan_count,
1163 .max_header = MAX_DCCP_HEADER,
1164 .obj_size = sizeof(struct dccp_sock),
1165 .rsk_prot = &dccp_request_sock_ops,
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08001166 .twsk_prot = &dccp_timewait_sock_ops,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001167};
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08001168
1169EXPORT_SYMBOL_GPL(dccp_prot);