blob: dc0487b5bacec375dda26bbdc4515b5a5cedc621 [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
Patrick McHardy5d39a792006-01-31 17:35:35 -0800122 err = ip_route_newports(&rt, IPPROTO_DCCP, inet->sport, inet->dport,
123 sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700124 if (err != 0)
125 goto failure;
126
127 /* OK, now commit destination to socket. */
128 sk_setup_caps(sk, &rt->u.dst);
129
130 dp->dccps_gar =
131 dp->dccps_iss = secure_dccp_sequence_number(inet->saddr,
132 inet->daddr,
133 inet->sport,
134 usin->sin_port);
135 dccp_update_gss(sk, dp->dccps_iss);
136
137 inet->id = dp->dccps_iss ^ jiffies;
138
139 err = dccp_connect(sk);
140 rt = NULL;
141 if (err != 0)
142 goto failure;
143out:
144 return err;
145failure:
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300146 /*
147 * This unhashes the socket and releases the local port, if necessary.
148 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700149 dccp_set_state(sk, DCCP_CLOSED);
150 ip_rt_put(rt);
151 sk->sk_route_caps = 0;
152 inet->dport = 0;
153 goto out;
154}
155
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800156EXPORT_SYMBOL_GPL(dccp_v4_connect);
157
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700158/*
159 * This routine does path mtu discovery as defined in RFC1191.
160 */
161static inline void dccp_do_pmtu_discovery(struct sock *sk,
162 const struct iphdr *iph,
163 u32 mtu)
164{
165 struct dst_entry *dst;
166 const struct inet_sock *inet = inet_sk(sk);
167 const struct dccp_sock *dp = dccp_sk(sk);
168
169 /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
170 * send out by Linux are always < 576bytes so they should go through
171 * unfragmented).
172 */
173 if (sk->sk_state == DCCP_LISTEN)
174 return;
175
176 /* We don't check in the destentry if pmtu discovery is forbidden
177 * on this route. We just assume that no packet_to_big packets
178 * are send back when pmtu discovery is not active.
179 * There is a small race when the user changes this flag in the
180 * route, but I think that's acceptable.
181 */
182 if ((dst = __sk_dst_check(sk, 0)) == NULL)
183 return;
184
185 dst->ops->update_pmtu(dst, mtu);
186
187 /* Something is about to be wrong... Remember soft error
188 * for the case, if this connection will not able to recover.
189 */
190 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
191 sk->sk_err_soft = EMSGSIZE;
192
193 mtu = dst_mtu(dst);
194
195 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800196 inet_csk(sk)->icsk_pmtu_cookie > mtu) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700197 dccp_sync_mss(sk, mtu);
198
199 /*
200 * From: draft-ietf-dccp-spec-11.txt
201 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300202 * DCCP-Sync packets are the best choice for upward
203 * probing, since DCCP-Sync probes do not risk application
204 * data loss.
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700205 */
Arnaldo Carvalho de Meloe92ae932005-08-17 03:10:59 -0300206 dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700207 } /* else let the usual retransmit timer handle it */
208}
209
210static void dccp_v4_ctl_send_ack(struct sk_buff *rxskb)
211{
212 int err;
213 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
214 const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) +
215 sizeof(struct dccp_hdr_ext) +
216 sizeof(struct dccp_hdr_ack_bits);
217 struct sk_buff *skb;
218
219 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
220 return;
221
222 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
223 if (skb == NULL)
224 return;
225
226 /* Reserve space for headers. */
227 skb_reserve(skb, MAX_DCCP_HEADER);
228
229 skb->dst = dst_clone(rxskb->dst);
230
231 skb->h.raw = skb_push(skb, dccp_hdr_ack_len);
232 dh = dccp_hdr(skb);
233 memset(dh, 0, dccp_hdr_ack_len);
234
235 /* Build DCCP header and checksum it. */
236 dh->dccph_type = DCCP_PKT_ACK;
237 dh->dccph_sport = rxdh->dccph_dport;
238 dh->dccph_dport = rxdh->dccph_sport;
239 dh->dccph_doff = dccp_hdr_ack_len / 4;
240 dh->dccph_x = 1;
241
242 dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300243 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
244 DCCP_SKB_CB(rxskb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700245
246 bh_lock_sock(dccp_ctl_socket->sk);
247 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300248 rxskb->nh.iph->daddr,
249 rxskb->nh.iph->saddr, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700250 bh_unlock_sock(dccp_ctl_socket->sk);
251
252 if (err == NET_XMIT_CN || err == 0) {
253 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
254 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
255 }
256}
257
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300258static void dccp_v4_reqsk_send_ack(struct sk_buff *skb,
259 struct request_sock *req)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700260{
261 dccp_v4_ctl_send_ack(skb);
262}
263
264static int dccp_v4_send_response(struct sock *sk, struct request_sock *req,
265 struct dst_entry *dst)
266{
267 int err = -1;
268 struct sk_buff *skb;
269
270 /* First, grab a route. */
271
272 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
273 goto out;
274
275 skb = dccp_make_response(sk, dst, req);
276 if (skb != NULL) {
277 const struct inet_request_sock *ireq = inet_rsk(req);
278
Herbert Xu49c5bfa2005-10-18 12:03:28 +1000279 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700280 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
281 ireq->rmt_addr,
282 ireq->opt);
283 if (err == NET_XMIT_CN)
284 err = 0;
285 }
286
287out:
288 dst_release(dst);
289 return err;
290}
291
292/*
293 * This routine is called by the ICMP module when it gets some sort of error
294 * condition. If err < 0 then the socket should be closed and the error
295 * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
296 * After adjustment header points to the first 8 bytes of the tcp header. We
297 * need to find the appropriate port.
298 *
299 * The locking strategy used here is very "optimistic". When someone else
300 * accesses the socket the ICMP is just dropped and for some paths there is no
301 * check at all. A more general error queue to queue errors for later handling
302 * is probably better.
303 */
304void dccp_v4_err(struct sk_buff *skb, u32 info)
305{
306 const struct iphdr *iph = (struct iphdr *)skb->data;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300307 const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data +
308 (iph->ihl << 2));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700309 struct dccp_sock *dp;
310 struct inet_sock *inet;
311 const int type = skb->h.icmph->type;
312 const int code = skb->h.icmph->code;
313 struct sock *sk;
314 __u64 seq;
315 int err;
316
317 if (skb->len < (iph->ihl << 2) + 8) {
318 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
319 return;
320 }
321
322 sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport,
323 iph->saddr, dh->dccph_sport, inet_iif(skb));
324 if (sk == NULL) {
325 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
326 return;
327 }
328
329 if (sk->sk_state == DCCP_TIME_WAIT) {
330 inet_twsk_put((struct inet_timewait_sock *)sk);
331 return;
332 }
333
334 bh_lock_sock(sk);
335 /* If too many ICMPs get dropped on busy
336 * servers this needs to be solved differently.
337 */
338 if (sock_owned_by_user(sk))
339 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
340
341 if (sk->sk_state == DCCP_CLOSED)
342 goto out;
343
344 dp = dccp_sk(sk);
345 seq = dccp_hdr_seq(skb);
346 if (sk->sk_state != DCCP_LISTEN &&
347 !between48(seq, dp->dccps_swl, dp->dccps_swh)) {
348 NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS);
349 goto out;
350 }
351
352 switch (type) {
353 case ICMP_SOURCE_QUENCH:
354 /* Just silently ignore these. */
355 goto out;
356 case ICMP_PARAMETERPROB:
357 err = EPROTO;
358 break;
359 case ICMP_DEST_UNREACH:
360 if (code > NR_ICMP_UNREACH)
361 goto out;
362
363 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
364 if (!sock_owned_by_user(sk))
365 dccp_do_pmtu_discovery(sk, iph, info);
366 goto out;
367 }
368
369 err = icmp_err_convert[code].errno;
370 break;
371 case ICMP_TIME_EXCEEDED:
372 err = EHOSTUNREACH;
373 break;
374 default:
375 goto out;
376 }
377
378 switch (sk->sk_state) {
379 struct request_sock *req , **prev;
380 case DCCP_LISTEN:
381 if (sock_owned_by_user(sk))
382 goto out;
383 req = inet_csk_search_req(sk, &prev, dh->dccph_dport,
384 iph->daddr, iph->saddr);
385 if (!req)
386 goto out;
387
388 /*
389 * ICMPs are not backlogged, hence we cannot get an established
390 * socket here.
391 */
392 BUG_TRAP(!req->sk);
393
394 if (seq != dccp_rsk(req)->dreq_iss) {
395 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
396 goto out;
397 }
398 /*
399 * Still in RESPOND, just remove it silently.
400 * There is no good way to pass the error to the newly
401 * created socket, and POSIX does not want network
402 * errors returned from accept().
403 */
404 inet_csk_reqsk_queue_drop(sk, req, prev);
405 goto out;
406
407 case DCCP_REQUESTING:
408 case DCCP_RESPOND:
409 if (!sock_owned_by_user(sk)) {
410 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
411 sk->sk_err = err;
412
413 sk->sk_error_report(sk);
414
415 dccp_done(sk);
416 } else
417 sk->sk_err_soft = err;
418 goto out;
419 }
420
421 /* If we've already connected we will keep trying
422 * until we time out, or the user gives up.
423 *
424 * rfc1122 4.2.3.9 allows to consider as hard errors
425 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
426 * but it is obsoleted by pmtu discovery).
427 *
428 * Note, that in modern internet, where routing is unreliable
429 * and in each dark corner broken firewalls sit, sending random
430 * errors ordered by their masters even this two messages finally lose
431 * their original sense (even Linux sends invalid PORT_UNREACHs)
432 *
433 * Now we are in compliance with RFCs.
434 * --ANK (980905)
435 */
436
437 inet = inet_sk(sk);
438 if (!sock_owned_by_user(sk) && inet->recverr) {
439 sk->sk_err = err;
440 sk->sk_error_report(sk);
441 } else /* Only an error on timeout */
442 sk->sk_err_soft = err;
443out:
444 bh_unlock_sock(sk);
445 sock_put(sk);
446}
447
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800448/* This routine computes an IPv4 DCCP checksum. */
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800449void dccp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -0800450{
451 const struct inet_sock *inet = inet_sk(sk);
452 struct dccp_hdr *dh = dccp_hdr(skb);
453
454 dh->dccph_checksum = dccp_v4_checksum(skb, inet->saddr, inet->daddr);
455}
456
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800457EXPORT_SYMBOL_GPL(dccp_v4_send_check);
458
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700459int dccp_v4_send_reset(struct sock *sk, enum dccp_reset_codes code)
460{
461 struct sk_buff *skb;
462 /*
463 * FIXME: what if rebuild_header fails?
464 * Should we be doing a rebuild_header here?
465 */
466 int err = inet_sk_rebuild_header(sk);
467
468 if (err != 0)
469 return err;
470
471 skb = dccp_make_reset(sk, sk->sk_dst_cache, code);
472 if (skb != NULL) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700473 const struct inet_sock *inet = inet_sk(sk);
474
Herbert Xu49c5bfa2005-10-18 12:03:28 +1000475 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700476 err = ip_build_and_send_pkt(skb, sk,
477 inet->saddr, inet->daddr, NULL);
478 if (err == NET_XMIT_CN)
479 err = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700480 }
481
482 return err;
483}
484
485static inline u64 dccp_v4_init_sequence(const struct sock *sk,
486 const struct sk_buff *skb)
487{
488 return secure_dccp_sequence_number(skb->nh.iph->daddr,
489 skb->nh.iph->saddr,
490 dccp_hdr(skb)->dccph_dport,
491 dccp_hdr(skb)->dccph_sport);
492}
493
494int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
495{
496 struct inet_request_sock *ireq;
497 struct dccp_sock dp;
498 struct request_sock *req;
499 struct dccp_request_sock *dreq;
500 const __u32 saddr = skb->nh.iph->saddr;
501 const __u32 daddr = skb->nh.iph->daddr;
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700502 const __u32 service = dccp_hdr_request(skb)->dccph_req_service;
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700503 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
504 __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700505
506 /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
507 if (((struct rtable *)skb->dst)->rt_flags &
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700508 (RTCF_BROADCAST | RTCF_MULTICAST)) {
509 reset_code = DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700510 goto drop;
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700511 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700512
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700513 if (dccp_bad_service_code(sk, service)) {
514 reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
515 goto drop;
516 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700517 /*
518 * TW buckets are converted to open requests without
519 * limitations, they conserve resources and peer is
520 * evidently real one.
521 */
522 if (inet_csk_reqsk_queue_is_full(sk))
523 goto drop;
524
525 /*
526 * Accept backlog is full. If we have already queued enough
527 * of warm entries in syn queue, drop request. It is better than
528 * clogging syn queue with openreqs with exponentially increasing
529 * timeout.
530 */
531 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
532 goto drop;
533
534 req = reqsk_alloc(sk->sk_prot->rsk_prot);
535 if (req == NULL)
536 goto drop;
537
538 /* FIXME: process options */
539
540 dccp_openreq_init(req, &dp, skb);
541
542 ireq = inet_rsk(req);
543 ireq->loc_addr = daddr;
544 ireq->rmt_addr = saddr;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300545 req->rcv_wnd = 100; /* Fake, option parsing will get the
546 right value */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700547 ireq->opt = NULL;
548
549 /*
550 * Step 3: Process LISTEN state
551 *
552 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
553 *
554 * In fact we defer setting S.GSR, S.SWL, S.SWH to
555 * dccp_create_openreq_child.
556 */
557 dreq = dccp_rsk(req);
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700558 dreq->dreq_isr = dcb->dccpd_seq;
559 dreq->dreq_iss = dccp_v4_init_sequence(sk, skb);
560 dreq->dreq_service = service;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700561
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800562 if (dccp_v4_send_response(sk, req, NULL))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700563 goto drop_and_free;
564
565 inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
566 return 0;
567
568drop_and_free:
Arnaldo Carvalho de Melofc44b982005-12-13 23:25:06 -0800569 reqsk_free(req);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700570drop:
571 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
Arnaldo Carvalho de Melo0c10c5d2005-09-16 16:58:33 -0700572 dcb->dccpd_reset_code = reset_code;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700573 return -1;
574}
575
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800576EXPORT_SYMBOL_GPL(dccp_v4_conn_request);
577
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700578/*
579 * The three way handshake has completed - we got a valid ACK or DATAACK -
580 * now create the new socket.
581 *
582 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
583 */
584struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
585 struct request_sock *req,
586 struct dst_entry *dst)
587{
588 struct inet_request_sock *ireq;
589 struct inet_sock *newinet;
590 struct dccp_sock *newdp;
591 struct sock *newsk;
592
593 if (sk_acceptq_is_full(sk))
594 goto exit_overflow;
595
596 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
597 goto exit;
598
599 newsk = dccp_create_openreq_child(sk, req, skb);
600 if (newsk == NULL)
601 goto exit;
602
603 sk_setup_caps(newsk, dst);
604
605 newdp = dccp_sk(newsk);
606 newinet = inet_sk(newsk);
607 ireq = inet_rsk(req);
608 newinet->daddr = ireq->rmt_addr;
609 newinet->rcv_saddr = ireq->loc_addr;
610 newinet->saddr = ireq->loc_addr;
611 newinet->opt = ireq->opt;
612 ireq->opt = NULL;
613 newinet->mc_index = inet_iif(skb);
614 newinet->mc_ttl = skb->nh.iph->ttl;
615 newinet->id = jiffies;
616
617 dccp_sync_mss(newsk, dst_mtu(dst));
618
619 __inet_hash(&dccp_hashinfo, newsk, 0);
620 __inet_inherit_port(&dccp_hashinfo, sk, newsk);
621
622 return newsk;
623
624exit_overflow:
625 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
626exit:
627 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
628 dst_release(dst);
629 return NULL;
630}
631
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800632EXPORT_SYMBOL_GPL(dccp_v4_request_recv_sock);
633
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700634static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
635{
636 const struct dccp_hdr *dh = dccp_hdr(skb);
637 const struct iphdr *iph = skb->nh.iph;
638 struct sock *nsk;
639 struct request_sock **prev;
640 /* Find possible connection requests. */
641 struct request_sock *req = inet_csk_search_req(sk, &prev,
642 dh->dccph_sport,
643 iph->saddr, iph->daddr);
644 if (req != NULL)
645 return dccp_check_req(sk, skb, req, prev);
646
647 nsk = __inet_lookup_established(&dccp_hashinfo,
648 iph->saddr, dh->dccph_sport,
649 iph->daddr, ntohs(dh->dccph_dport),
650 inet_iif(skb));
651 if (nsk != NULL) {
652 if (nsk->sk_state != DCCP_TIME_WAIT) {
653 bh_lock_sock(nsk);
654 return nsk;
655 }
656 inet_twsk_put((struct inet_timewait_sock *)nsk);
657 return NULL;
658 }
659
660 return sk;
661}
662
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300663int dccp_v4_checksum(const struct sk_buff *skb, const u32 saddr,
664 const u32 daddr)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700665{
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700666 const struct dccp_hdr* dh = dccp_hdr(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700667 int checksum_len;
668 u32 tmp;
669
670 if (dh->dccph_cscov == 0)
671 checksum_len = skb->len;
672 else {
673 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300674 checksum_len = checksum_len < skb->len ? checksum_len :
675 skb->len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700676 }
677
678 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300679 return csum_tcpudp_magic(saddr, daddr, checksum_len,
680 IPPROTO_DCCP, tmp);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700681}
682
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700683static int dccp_v4_verify_checksum(struct sk_buff *skb,
684 const u32 saddr, const u32 daddr)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700685{
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700686 struct dccp_hdr *dh = dccp_hdr(skb);
687 int checksum_len;
688 u32 tmp;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700689
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700690 if (dh->dccph_cscov == 0)
691 checksum_len = skb->len;
692 else {
693 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300694 checksum_len = checksum_len < skb->len ? checksum_len :
695 skb->len;
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700696 }
697 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300698 return csum_tcpudp_magic(saddr, daddr, checksum_len,
699 IPPROTO_DCCP, tmp) == 0 ? 0 : -1;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700700}
701
702static struct dst_entry* dccp_v4_route_skb(struct sock *sk,
703 struct sk_buff *skb)
704{
705 struct rtable *rt;
706 struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif,
707 .nl_u = { .ip4_u =
708 { .daddr = skb->nh.iph->saddr,
709 .saddr = skb->nh.iph->daddr,
710 .tos = RT_CONN_FLAGS(sk) } },
711 .proto = sk->sk_protocol,
712 .uli_u = { .ports =
713 { .sport = dccp_hdr(skb)->dccph_dport,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300714 .dport = dccp_hdr(skb)->dccph_sport }
715 }
716 };
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700717
718 if (ip_route_output_flow(&rt, &fl, sk, 0)) {
719 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
720 return NULL;
721 }
722
723 return &rt->u.dst;
724}
725
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300726static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700727{
728 int err;
729 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
730 const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
731 sizeof(struct dccp_hdr_ext) +
732 sizeof(struct dccp_hdr_reset);
733 struct sk_buff *skb;
734 struct dst_entry *dst;
Arnaldo Carvalho de Melo2807d4f2005-08-21 05:33:48 -0300735 u64 seqno;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700736
737 /* Never send a reset in response to a reset. */
738 if (rxdh->dccph_type == DCCP_PKT_RESET)
739 return;
740
741 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
742 return;
743
744 dst = dccp_v4_route_skb(dccp_ctl_socket->sk, rxskb);
745 if (dst == NULL)
746 return;
747
748 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
749 if (skb == NULL)
750 goto out;
751
752 /* Reserve space for headers. */
753 skb_reserve(skb, MAX_DCCP_HEADER);
754 skb->dst = dst_clone(dst);
755
756 skb->h.raw = skb_push(skb, dccp_hdr_reset_len);
757 dh = dccp_hdr(skb);
758 memset(dh, 0, dccp_hdr_reset_len);
759
760 /* Build DCCP header and checksum it. */
761 dh->dccph_type = DCCP_PKT_RESET;
762 dh->dccph_sport = rxdh->dccph_dport;
763 dh->dccph_dport = rxdh->dccph_sport;
764 dh->dccph_doff = dccp_hdr_reset_len / 4;
765 dh->dccph_x = 1;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300766 dccp_hdr_reset(skb)->dccph_reset_code =
767 DCCP_SKB_CB(rxskb)->dccpd_reset_code;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700768
Arnaldo Carvalho de Melo2807d4f2005-08-21 05:33:48 -0300769 /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
770 seqno = 0;
771 if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
772 dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
773
774 dccp_hdr_set_seq(dh, seqno);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300775 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
776 DCCP_SKB_CB(rxskb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700777
Yoshifumi Nishida95b81ef2005-08-09 20:15:35 -0700778 dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr,
779 rxskb->nh.iph->daddr);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700780
781 bh_lock_sock(dccp_ctl_socket->sk);
782 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300783 rxskb->nh.iph->daddr,
784 rxskb->nh.iph->saddr, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700785 bh_unlock_sock(dccp_ctl_socket->sk);
786
787 if (err == NET_XMIT_CN || err == 0) {
788 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
789 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
790 }
791out:
792 dst_release(dst);
793}
794
795int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
796{
797 struct dccp_hdr *dh = dccp_hdr(skb);
798
799 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
800 if (dccp_rcv_established(sk, skb, dh, skb->len))
801 goto reset;
802 return 0;
803 }
804
805 /*
806 * Step 3: Process LISTEN state
807 * If S.state == LISTEN,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300808 * If P.type == Request or P contains a valid Init Cookie
809 * option,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700810 * * Must scan the packet's options to check for an Init
811 * Cookie. Only the Init Cookie is processed here,
812 * however; other options are processed in Step 8. This
813 * scan need only be performed if the endpoint uses Init
814 * Cookies *
815 * * Generate a new socket and switch to that socket *
816 * Set S := new socket for this port pair
817 * S.state = RESPOND
818 * Choose S.ISS (initial seqno) or set from Init Cookie
819 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
820 * Continue with S.state == RESPOND
821 * * A Response packet will be generated in Step 11 *
822 * Otherwise,
823 * Generate Reset(No Connection) unless P.type == Reset
824 * Drop packet and return
825 *
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300826 * NOTE: the check for the packet types is done in
827 * dccp_rcv_state_process
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700828 */
829 if (sk->sk_state == DCCP_LISTEN) {
830 struct sock *nsk = dccp_v4_hnd_req(sk, skb);
831
832 if (nsk == NULL)
833 goto discard;
834
835 if (nsk != sk) {
836 if (dccp_child_process(sk, nsk, skb))
837 goto reset;
838 return 0;
839 }
840 }
841
842 if (dccp_rcv_state_process(sk, skb, dh, skb->len))
843 goto reset;
844 return 0;
845
846reset:
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700847 dccp_v4_ctl_send_reset(skb);
848discard:
849 kfree_skb(skb);
850 return 0;
851}
852
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800853EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
854
855int dccp_invalid_packet(struct sk_buff *skb)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700856{
857 const struct dccp_hdr *dh;
858
859 if (skb->pkt_type != PACKET_HOST)
860 return 1;
861
862 if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -0300863 LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700864 return 1;
865 }
866
867 dh = dccp_hdr(skb);
868
869 /* If the packet type is not understood, drop packet and return */
870 if (dh->dccph_type >= DCCP_PKT_INVALID) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -0300871 LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700872 return 1;
873 }
874
875 /*
876 * If P.Data Offset is too small for packet type, or too large for
877 * packet, drop packet and return
878 */
879 if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -0300880 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
881 "too small 1\n",
882 dh->dccph_doff);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700883 return 1;
884 }
885
886 if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -0300887 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
888 "too small 2\n",
889 dh->dccph_doff);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700890 return 1;
891 }
892
893 dh = dccp_hdr(skb);
894
895 /*
896 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
897 * has short sequence numbers), drop packet and return
898 */
899 if (dh->dccph_x == 0 &&
900 dh->dccph_type != DCCP_PKT_DATA &&
901 dh->dccph_type != DCCP_PKT_ACK &&
902 dh->dccph_type != DCCP_PKT_DATAACK) {
Arnaldo Carvalho de Meloc59eab42005-08-18 21:12:02 -0300903 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack "
904 "nor DataAck and P.X == 0\n",
905 dccp_packet_name(dh->dccph_type));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700906 return 1;
907 }
908
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700909 return 0;
910}
911
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800912EXPORT_SYMBOL_GPL(dccp_invalid_packet);
913
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700914/* this is called when real data arrives */
915int dccp_v4_rcv(struct sk_buff *skb)
916{
917 const struct dccp_hdr *dh;
918 struct sock *sk;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700919
920 /* Step 1: Check header basics: */
921
922 if (dccp_invalid_packet(skb))
923 goto discard_it;
924
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800925 /* If the header checksum is incorrect, drop packet and return */
926 if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr,
927 skb->nh.iph->daddr) < 0) {
928 LIMIT_NETDEBUG(KERN_WARNING "%s: incorrect header checksum\n",
929 __FUNCTION__);
930 goto discard_it;
931 }
932
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700933 dh = dccp_hdr(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700934
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700935 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
936 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
937
938 dccp_pr_debug("%8.8s "
939 "src=%u.%u.%u.%u@%-5d "
940 "dst=%u.%u.%u.%u@%-5d seq=%llu",
941 dccp_packet_name(dh->dccph_type),
942 NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport),
943 NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport),
David S. Millerf6ccf552005-08-09 20:27:14 -0700944 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700945
946 if (dccp_packet_without_ack(skb)) {
947 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
948 dccp_pr_debug_cat("\n");
949 } else {
950 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
David S. Millerf6ccf552005-08-09 20:27:14 -0700951 dccp_pr_debug_cat(", ack=%llu\n",
952 (unsigned long long)
953 DCCP_SKB_CB(skb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700954 }
955
956 /* Step 2:
957 * Look up flow ID in table and get corresponding socket */
958 sk = __inet_lookup(&dccp_hashinfo,
959 skb->nh.iph->saddr, dh->dccph_sport,
960 skb->nh.iph->daddr, ntohs(dh->dccph_dport),
961 inet_iif(skb));
962
963 /*
964 * Step 2:
965 * If no socket ...
966 * Generate Reset(No Connection) unless P.type == Reset
967 * Drop packet and return
968 */
969 if (sk == NULL) {
970 dccp_pr_debug("failed to look up flow ID in table and "
971 "get corresponding socket\n");
972 goto no_dccp_socket;
973 }
974
975 /*
976 * Step 2:
977 * ... or S.state == TIMEWAIT,
978 * Generate Reset(No Connection) unless P.type == Reset
979 * Drop packet and return
980 */
981
982 if (sk->sk_state == DCCP_TIME_WAIT) {
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -0700983 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
984 "do_time_wait\n");
985 goto do_time_wait;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700986 }
987
Arnaldo Carvalho de Melo25995ff2005-12-27 02:42:22 -0200988 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700989 goto discard_and_relse;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800990 nf_reset(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700991
Arnaldo Carvalho de Melo25995ff2005-12-27 02:42:22 -0200992 return sk_receive_skb(sk, skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700993
994no_dccp_socket:
995 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
996 goto discard_it;
997 /*
998 * Step 2:
999 * Generate Reset(No Connection) unless P.type == Reset
1000 * Drop packet and return
1001 */
1002 if (dh->dccph_type != DCCP_PKT_RESET) {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001003 DCCP_SKB_CB(skb)->dccpd_reset_code =
1004 DCCP_RESET_CODE_NO_CONNECTION;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001005 dccp_v4_ctl_send_reset(skb);
1006 }
1007
1008discard_it:
1009 /* Discard frame. */
1010 kfree_skb(skb);
1011 return 0;
1012
1013discard_and_relse:
1014 sock_put(sk);
1015 goto discard_it;
Arnaldo Carvalho de Melo64cf1e52005-08-09 20:45:21 -07001016
1017do_time_wait:
1018 inet_twsk_put((struct inet_timewait_sock *)sk);
1019 goto no_dccp_socket;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001020}
1021
Arnaldo Carvalho de Melo57cca052005-12-13 23:16:16 -08001022struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
1023 .queue_xmit = ip_queue_xmit,
1024 .send_check = dccp_v4_send_check,
1025 .rebuild_header = inet_sk_rebuild_header,
1026 .conn_request = dccp_v4_conn_request,
1027 .syn_recv_sock = dccp_v4_request_recv_sock,
1028 .net_header_len = sizeof(struct iphdr),
1029 .setsockopt = ip_setsockopt,
1030 .getsockopt = ip_getsockopt,
1031 .addr2sockaddr = inet_csk_addr2sockaddr,
1032 .sockaddr_len = sizeof(struct sockaddr_in),
1033};
1034
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001035int dccp_v4_init_sock(struct sock *sk)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001036{
1037 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001038 struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001039 static int dccp_ctl_socket_init = 1;
1040
1041 dccp_options_init(&dp->dccps_options);
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -03001042 do_gettimeofday(&dp->dccps_epoch);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001043
1044 if (dp->dccps_options.dccpo_send_ack_vector) {
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001045 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(DCCP_MAX_ACKVEC_LEN,
1046 GFP_KERNEL);
1047 if (dp->dccps_hc_rx_ackvec == NULL)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001048 return -ENOMEM;
1049 }
1050
1051 /*
1052 * FIXME: We're hardcoding the CCID, and doing this at this point makes
1053 * the listening (master) sock get CCID control blocks, which is not
1054 * necessary, but for now, to not mess with the test userspace apps,
1055 * lets leave it here, later the real solution is to do this in a
1056 * setsockopt(CCIDs-I-want/accept). -acme
1057 */
1058 if (likely(!dccp_ctl_socket_init)) {
Arnaldo Carvalho de Melo561713c2005-09-18 00:18:52 -07001059 dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_rx_ccid,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001060 sk);
Arnaldo Carvalho de Melo561713c2005-09-18 00:18:52 -07001061 dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_tx_ccid,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001062 sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001063 if (dp->dccps_hc_rx_ccid == NULL ||
1064 dp->dccps_hc_tx_ccid == NULL) {
1065 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1066 ccid_exit(dp->dccps_hc_tx_ccid, sk);
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001067 if (dp->dccps_options.dccpo_send_ack_vector) {
1068 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1069 dp->dccps_hc_rx_ackvec = NULL;
1070 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001071 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1072 return -ENOMEM;
1073 }
1074 } else
1075 dccp_ctl_socket_init = 0;
1076
1077 dccp_init_xmit_timers(sk);
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001078 icsk->icsk_rto = DCCP_TIMEOUT_INIT;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001079 sk->sk_state = DCCP_CLOSED;
Arnaldo Carvalho de Meloc530cfb2005-08-29 02:15:54 -03001080 sk->sk_write_space = dccp_write_space;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001081 icsk->icsk_af_ops = &dccp_ipv4_af_ops;
1082 icsk->icsk_sync_mss = dccp_sync_mss;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001083 dp->dccps_mss_cache = 536;
1084 dp->dccps_role = DCCP_ROLE_UNDEFINED;
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -07001085 dp->dccps_service = DCCP_SERVICE_INVALID_VALUE;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001086
1087 return 0;
1088}
1089
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001090EXPORT_SYMBOL_GPL(dccp_v4_init_sock);
1091
1092int dccp_v4_destroy_sock(struct sock *sk)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001093{
1094 struct dccp_sock *dp = dccp_sk(sk);
1095
1096 /*
Ian McDonald4c7e6892005-12-21 19:02:39 -08001097 * DCCP doesn't use sk_write_queue, just sk_send_head
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001098 * for retransmissions
1099 */
1100 if (sk->sk_send_head != NULL) {
1101 kfree_skb(sk->sk_send_head);
1102 sk->sk_send_head = NULL;
1103 }
1104
1105 /* Clean up a referenced DCCP bind bucket. */
1106 if (inet_csk(sk)->icsk_bind_hash != NULL)
1107 inet_put_port(&dccp_hashinfo, sk);
1108
Jesper Juhla51482b2005-11-08 09:41:34 -08001109 kfree(dp->dccps_service_list);
1110 dp->dccps_service_list = NULL;
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -07001111
Arnaldo Carvalho de Melo8efa5442005-08-23 21:54:00 -07001112 ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
1113 ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
Arnaldo Carvalho de Meloae31c332005-09-18 00:17:51 -07001114 if (dp->dccps_options.dccpo_send_ack_vector) {
1115 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1116 dp->dccps_hc_rx_ackvec = NULL;
1117 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001118 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1119 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1120 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1121
1122 return 0;
1123}
1124
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001125EXPORT_SYMBOL_GPL(dccp_v4_destroy_sock);
1126
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001127static void dccp_v4_reqsk_destructor(struct request_sock *req)
1128{
1129 kfree(inet_rsk(req)->opt);
1130}
1131
1132static struct request_sock_ops dccp_request_sock_ops = {
1133 .family = PF_INET,
1134 .obj_size = sizeof(struct dccp_request_sock),
1135 .rtx_syn_ack = dccp_v4_send_response,
1136 .send_ack = dccp_v4_reqsk_send_ack,
1137 .destructor = dccp_v4_reqsk_destructor,
1138 .send_reset = dccp_v4_ctl_send_reset,
1139};
1140
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08001141static struct timewait_sock_ops dccp_timewait_sock_ops = {
1142 .twsk_obj_size = sizeof(struct inet_timewait_sock),
1143};
1144
Arnaldo Carvalho de Melo34ca6862005-12-13 23:23:32 -08001145struct proto dccp_prot = {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001146 .name = "DCCP",
1147 .owner = THIS_MODULE,
1148 .close = dccp_close,
1149 .connect = dccp_v4_connect,
1150 .disconnect = dccp_disconnect,
1151 .ioctl = dccp_ioctl,
1152 .init = dccp_v4_init_sock,
1153 .setsockopt = dccp_setsockopt,
1154 .getsockopt = dccp_getsockopt,
1155 .sendmsg = dccp_sendmsg,
1156 .recvmsg = dccp_recvmsg,
1157 .backlog_rcv = dccp_v4_do_rcv,
1158 .hash = dccp_v4_hash,
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001159 .unhash = dccp_unhash,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001160 .accept = inet_csk_accept,
1161 .get_port = dccp_v4_get_port,
1162 .shutdown = dccp_shutdown,
1163 .destroy = dccp_v4_destroy_sock,
1164 .orphan_count = &dccp_orphan_count,
1165 .max_header = MAX_DCCP_HEADER,
1166 .obj_size = sizeof(struct dccp_sock),
1167 .rsk_prot = &dccp_request_sock_ops,
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08001168 .twsk_prot = &dccp_timewait_sock_ops,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001169};
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08001170
1171EXPORT_SYMBOL_GPL(dccp_prot);