]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/dccp/ipv4.c
[ICSK]: Introduce inet_csk_ctl_sock_create
[mirror_ubuntu-bionic-kernel.git] / net / dccp / ipv4.c
CommitLineData
7c657876
ACM
1/*
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>
b61fafc4 21#include <net/inet_common.h>
7c657876 22#include <net/inet_hashtables.h>
14c85021 23#include <net/inet_sock.h>
b61fafc4 24#include <net/protocol.h>
7c657876 25#include <net/sock.h>
6d6ee43e 26#include <net/timewait_sock.h>
7c657876
ACM
27#include <net/tcp_states.h>
28#include <net/xfrm.h>
29
ae31c339 30#include "ackvec.h"
7c657876
ACM
31#include "ccid.h"
32#include "dccp.h"
afe00251 33#include "feat.h"
7c657876 34
72478873
ACM
35/*
36 * This is the global socket data structure used for responding to
37 * the Out-of-the-blue (OOTB) packets. A control sock will be created
38 * for this socket at the initialization time.
39 */
40static struct socket *dccp_v4_ctl_socket;
41
7c657876
ACM
42static int dccp_v4_get_port(struct sock *sk, const unsigned short snum)
43{
971af18b
ACM
44 return inet_csk_get_port(&dccp_hashinfo, sk, snum,
45 inet_csk_bind_conflict);
7c657876
ACM
46}
47
f21e68ca 48int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
7c657876
ACM
49{
50 struct inet_sock *inet = inet_sk(sk);
51 struct dccp_sock *dp = dccp_sk(sk);
52 const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
53 struct rtable *rt;
54 u32 daddr, nexthop;
55 int tmp;
56 int err;
57
58 dp->dccps_role = DCCP_ROLE_CLIENT;
59
67e6b629
ACM
60 if (dccp_service_not_initialized(sk))
61 return -EPROTO;
62
7c657876
ACM
63 if (addr_len < sizeof(struct sockaddr_in))
64 return -EINVAL;
65
66 if (usin->sin_family != AF_INET)
67 return -EAFNOSUPPORT;
68
69 nexthop = daddr = usin->sin_addr.s_addr;
70 if (inet->opt != NULL && inet->opt->srr) {
71 if (daddr == 0)
72 return -EINVAL;
73 nexthop = inet->opt->faddr;
74 }
75
76 tmp = ip_route_connect(&rt, nexthop, inet->saddr,
77 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
78 IPPROTO_DCCP,
79 inet->sport, usin->sin_port, sk);
80 if (tmp < 0)
81 return tmp;
82
83 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
84 ip_rt_put(rt);
85 return -ENETUNREACH;
86 }
87
88 if (inet->opt == NULL || !inet->opt->srr)
89 daddr = rt->rt_dst;
90
91 if (inet->saddr == 0)
92 inet->saddr = rt->rt_src;
93 inet->rcv_saddr = inet->saddr;
94
95 inet->dport = usin->sin_port;
96 inet->daddr = daddr;
97
d83d8461 98 inet_csk(sk)->icsk_ext_hdr_len = 0;
7c657876 99 if (inet->opt != NULL)
d83d8461 100 inet_csk(sk)->icsk_ext_hdr_len = inet->opt->optlen;
7c657876
ACM
101 /*
102 * Socket identity is still unknown (sport may be zero).
103 * However we set state to DCCP_REQUESTING and not releasing socket
104 * lock select source port, enter ourselves into the hash tables and
105 * complete initialization after this.
106 */
107 dccp_set_state(sk, DCCP_REQUESTING);
a7f5e7f1 108 err = inet_hash_connect(&dccp_death_row, sk);
7c657876
ACM
109 if (err != 0)
110 goto failure;
111
5d39a795
PM
112 err = ip_route_newports(&rt, IPPROTO_DCCP, inet->sport, inet->dport,
113 sk);
7c657876
ACM
114 if (err != 0)
115 goto failure;
116
117 /* OK, now commit destination to socket. */
118 sk_setup_caps(sk, &rt->u.dst);
119
120 dp->dccps_gar =
121 dp->dccps_iss = secure_dccp_sequence_number(inet->saddr,
122 inet->daddr,
123 inet->sport,
124 usin->sin_port);
125 dccp_update_gss(sk, dp->dccps_iss);
126
127 inet->id = dp->dccps_iss ^ jiffies;
128
129 err = dccp_connect(sk);
130 rt = NULL;
131 if (err != 0)
132 goto failure;
133out:
134 return err;
135failure:
7690af3f
ACM
136 /*
137 * This unhashes the socket and releases the local port, if necessary.
138 */
7c657876
ACM
139 dccp_set_state(sk, DCCP_CLOSED);
140 ip_rt_put(rt);
141 sk->sk_route_caps = 0;
142 inet->dport = 0;
143 goto out;
144}
145
f21e68ca
ACM
146EXPORT_SYMBOL_GPL(dccp_v4_connect);
147
7c657876
ACM
148/*
149 * This routine does path mtu discovery as defined in RFC1191.
150 */
151static inline void dccp_do_pmtu_discovery(struct sock *sk,
152 const struct iphdr *iph,
153 u32 mtu)
154{
155 struct dst_entry *dst;
156 const struct inet_sock *inet = inet_sk(sk);
157 const struct dccp_sock *dp = dccp_sk(sk);
158
159 /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
160 * send out by Linux are always < 576bytes so they should go through
161 * unfragmented).
162 */
163 if (sk->sk_state == DCCP_LISTEN)
164 return;
165
166 /* We don't check in the destentry if pmtu discovery is forbidden
167 * on this route. We just assume that no packet_to_big packets
168 * are send back when pmtu discovery is not active.
169 * There is a small race when the user changes this flag in the
170 * route, but I think that's acceptable.
171 */
172 if ((dst = __sk_dst_check(sk, 0)) == NULL)
173 return;
174
175 dst->ops->update_pmtu(dst, mtu);
176
177 /* Something is about to be wrong... Remember soft error
178 * for the case, if this connection will not able to recover.
179 */
180 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
181 sk->sk_err_soft = EMSGSIZE;
182
183 mtu = dst_mtu(dst);
184
185 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
d83d8461 186 inet_csk(sk)->icsk_pmtu_cookie > mtu) {
7c657876
ACM
187 dccp_sync_mss(sk, mtu);
188
189 /*
190 * From: draft-ietf-dccp-spec-11.txt
191 *
7690af3f
ACM
192 * DCCP-Sync packets are the best choice for upward
193 * probing, since DCCP-Sync probes do not risk application
194 * data loss.
7c657876 195 */
e92ae93a 196 dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
7c657876
ACM
197 } /* else let the usual retransmit timer handle it */
198}
199
200static void dccp_v4_ctl_send_ack(struct sk_buff *rxskb)
201{
202 int err;
203 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
204 const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) +
205 sizeof(struct dccp_hdr_ext) +
206 sizeof(struct dccp_hdr_ack_bits);
207 struct sk_buff *skb;
208
209 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
210 return;
211
212 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
213 if (skb == NULL)
214 return;
215
216 /* Reserve space for headers. */
217 skb_reserve(skb, MAX_DCCP_HEADER);
218
219 skb->dst = dst_clone(rxskb->dst);
220
221 skb->h.raw = skb_push(skb, dccp_hdr_ack_len);
222 dh = dccp_hdr(skb);
223 memset(dh, 0, dccp_hdr_ack_len);
224
225 /* Build DCCP header and checksum it. */
226 dh->dccph_type = DCCP_PKT_ACK;
227 dh->dccph_sport = rxdh->dccph_dport;
228 dh->dccph_dport = rxdh->dccph_sport;
229 dh->dccph_doff = dccp_hdr_ack_len / 4;
230 dh->dccph_x = 1;
231
232 dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq);
7690af3f
ACM
233 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
234 DCCP_SKB_CB(rxskb)->dccpd_seq);
7c657876 235
72478873
ACM
236 bh_lock_sock(dccp_v4_ctl_socket->sk);
237 err = ip_build_and_send_pkt(skb, dccp_v4_ctl_socket->sk,
7690af3f
ACM
238 rxskb->nh.iph->daddr,
239 rxskb->nh.iph->saddr, NULL);
72478873 240 bh_unlock_sock(dccp_v4_ctl_socket->sk);
7c657876
ACM
241
242 if (err == NET_XMIT_CN || err == 0) {
243 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
244 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
245 }
246}
247
7690af3f
ACM
248static void dccp_v4_reqsk_send_ack(struct sk_buff *skb,
249 struct request_sock *req)
7c657876
ACM
250{
251 dccp_v4_ctl_send_ack(skb);
252}
253
254static int dccp_v4_send_response(struct sock *sk, struct request_sock *req,
255 struct dst_entry *dst)
256{
257 int err = -1;
258 struct sk_buff *skb;
259
260 /* First, grab a route. */
261
262 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
263 goto out;
264
265 skb = dccp_make_response(sk, dst, req);
266 if (skb != NULL) {
267 const struct inet_request_sock *ireq = inet_rsk(req);
0a1ec676 268 struct dccp_hdr *dh = dccp_hdr(skb);
7c657876 269
0a1ec676
ACM
270 dh->dccph_checksum = dccp_v4_checksum(skb, ireq->loc_addr,
271 ireq->rmt_addr);
49c5bfaf 272 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
7c657876
ACM
273 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
274 ireq->rmt_addr,
275 ireq->opt);
276 if (err == NET_XMIT_CN)
277 err = 0;
278 }
279
280out:
281 dst_release(dst);
282 return err;
283}
284
285/*
286 * This routine is called by the ICMP module when it gets some sort of error
287 * condition. If err < 0 then the socket should be closed and the error
288 * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
289 * After adjustment header points to the first 8 bytes of the tcp header. We
290 * need to find the appropriate port.
291 *
292 * The locking strategy used here is very "optimistic". When someone else
293 * accesses the socket the ICMP is just dropped and for some paths there is no
294 * check at all. A more general error queue to queue errors for later handling
295 * is probably better.
296 */
b61fafc4 297static void dccp_v4_err(struct sk_buff *skb, u32 info)
7c657876
ACM
298{
299 const struct iphdr *iph = (struct iphdr *)skb->data;
7690af3f
ACM
300 const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data +
301 (iph->ihl << 2));
7c657876
ACM
302 struct dccp_sock *dp;
303 struct inet_sock *inet;
304 const int type = skb->h.icmph->type;
305 const int code = skb->h.icmph->code;
306 struct sock *sk;
307 __u64 seq;
308 int err;
309
310 if (skb->len < (iph->ihl << 2) + 8) {
311 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
312 return;
313 }
314
315 sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport,
316 iph->saddr, dh->dccph_sport, inet_iif(skb));
317 if (sk == NULL) {
318 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
319 return;
320 }
321
322 if (sk->sk_state == DCCP_TIME_WAIT) {
323 inet_twsk_put((struct inet_timewait_sock *)sk);
324 return;
325 }
326
327 bh_lock_sock(sk);
328 /* If too many ICMPs get dropped on busy
329 * servers this needs to be solved differently.
330 */
331 if (sock_owned_by_user(sk))
332 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
333
334 if (sk->sk_state == DCCP_CLOSED)
335 goto out;
336
337 dp = dccp_sk(sk);
338 seq = dccp_hdr_seq(skb);
339 if (sk->sk_state != DCCP_LISTEN &&
340 !between48(seq, dp->dccps_swl, dp->dccps_swh)) {
341 NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS);
342 goto out;
343 }
344
345 switch (type) {
346 case ICMP_SOURCE_QUENCH:
347 /* Just silently ignore these. */
348 goto out;
349 case ICMP_PARAMETERPROB:
350 err = EPROTO;
351 break;
352 case ICMP_DEST_UNREACH:
353 if (code > NR_ICMP_UNREACH)
354 goto out;
355
356 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
357 if (!sock_owned_by_user(sk))
358 dccp_do_pmtu_discovery(sk, iph, info);
359 goto out;
360 }
361
362 err = icmp_err_convert[code].errno;
363 break;
364 case ICMP_TIME_EXCEEDED:
365 err = EHOSTUNREACH;
366 break;
367 default:
368 goto out;
369 }
370
371 switch (sk->sk_state) {
372 struct request_sock *req , **prev;
373 case DCCP_LISTEN:
374 if (sock_owned_by_user(sk))
375 goto out;
376 req = inet_csk_search_req(sk, &prev, dh->dccph_dport,
377 iph->daddr, iph->saddr);
378 if (!req)
379 goto out;
380
381 /*
382 * ICMPs are not backlogged, hence we cannot get an established
383 * socket here.
384 */
385 BUG_TRAP(!req->sk);
386
387 if (seq != dccp_rsk(req)->dreq_iss) {
388 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
389 goto out;
390 }
391 /*
392 * Still in RESPOND, just remove it silently.
393 * There is no good way to pass the error to the newly
394 * created socket, and POSIX does not want network
395 * errors returned from accept().
396 */
397 inet_csk_reqsk_queue_drop(sk, req, prev);
398 goto out;
399
400 case DCCP_REQUESTING:
401 case DCCP_RESPOND:
402 if (!sock_owned_by_user(sk)) {
403 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
404 sk->sk_err = err;
405
406 sk->sk_error_report(sk);
407
408 dccp_done(sk);
409 } else
410 sk->sk_err_soft = err;
411 goto out;
412 }
413
414 /* If we've already connected we will keep trying
415 * until we time out, or the user gives up.
416 *
417 * rfc1122 4.2.3.9 allows to consider as hard errors
418 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
419 * but it is obsoleted by pmtu discovery).
420 *
421 * Note, that in modern internet, where routing is unreliable
422 * and in each dark corner broken firewalls sit, sending random
423 * errors ordered by their masters even this two messages finally lose
424 * their original sense (even Linux sends invalid PORT_UNREACHs)
425 *
426 * Now we are in compliance with RFCs.
427 * --ANK (980905)
428 */
429
430 inet = inet_sk(sk);
431 if (!sock_owned_by_user(sk) && inet->recverr) {
432 sk->sk_err = err;
433 sk->sk_error_report(sk);
434 } else /* Only an error on timeout */
435 sk->sk_err_soft = err;
436out:
437 bh_unlock_sock(sk);
438 sock_put(sk);
439}
440
57cca05a 441/* This routine computes an IPv4 DCCP checksum. */
f21e68ca 442void dccp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
57cca05a
ACM
443{
444 const struct inet_sock *inet = inet_sk(sk);
445 struct dccp_hdr *dh = dccp_hdr(skb);
446
447 dh->dccph_checksum = dccp_v4_checksum(skb, inet->saddr, inet->daddr);
448}
449
f21e68ca
ACM
450EXPORT_SYMBOL_GPL(dccp_v4_send_check);
451
7c657876
ACM
452static inline u64 dccp_v4_init_sequence(const struct sock *sk,
453 const struct sk_buff *skb)
454{
455 return secure_dccp_sequence_number(skb->nh.iph->daddr,
456 skb->nh.iph->saddr,
457 dccp_hdr(skb)->dccph_dport,
458 dccp_hdr(skb)->dccph_sport);
459}
460
461int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
462{
463 struct inet_request_sock *ireq;
464 struct dccp_sock dp;
465 struct request_sock *req;
466 struct dccp_request_sock *dreq;
60fe62e7
AB
467 const __be32 saddr = skb->nh.iph->saddr;
468 const __be32 daddr = skb->nh.iph->daddr;
469 const __be32 service = dccp_hdr_request(skb)->dccph_req_service;
0c10c5d9
ACM
470 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
471 __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
7c657876
ACM
472
473 /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
474 if (((struct rtable *)skb->dst)->rt_flags &
0c10c5d9
ACM
475 (RTCF_BROADCAST | RTCF_MULTICAST)) {
476 reset_code = DCCP_RESET_CODE_NO_CONNECTION;
7c657876 477 goto drop;
0c10c5d9 478 }
7c657876 479
67e6b629
ACM
480 if (dccp_bad_service_code(sk, service)) {
481 reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
482 goto drop;
483 }
7c657876
ACM
484 /*
485 * TW buckets are converted to open requests without
486 * limitations, they conserve resources and peer is
487 * evidently real one.
488 */
489 if (inet_csk_reqsk_queue_is_full(sk))
490 goto drop;
491
492 /*
493 * Accept backlog is full. If we have already queued enough
494 * of warm entries in syn queue, drop request. It is better than
495 * clogging syn queue with openreqs with exponentially increasing
496 * timeout.
497 */
498 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
499 goto drop;
500
501 req = reqsk_alloc(sk->sk_prot->rsk_prot);
502 if (req == NULL)
503 goto drop;
504
afe00251
AB
505 if (dccp_parse_options(sk, skb))
506 goto drop;
7c657876
ACM
507
508 dccp_openreq_init(req, &dp, skb);
509
510 ireq = inet_rsk(req);
511 ireq->loc_addr = daddr;
512 ireq->rmt_addr = saddr;
7690af3f
ACM
513 req->rcv_wnd = 100; /* Fake, option parsing will get the
514 right value */
7c657876
ACM
515 ireq->opt = NULL;
516
517 /*
518 * Step 3: Process LISTEN state
519 *
520 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
521 *
522 * In fact we defer setting S.GSR, S.SWL, S.SWH to
523 * dccp_create_openreq_child.
524 */
525 dreq = dccp_rsk(req);
67e6b629
ACM
526 dreq->dreq_isr = dcb->dccpd_seq;
527 dreq->dreq_iss = dccp_v4_init_sequence(sk, skb);
528 dreq->dreq_service = service;
7c657876 529
f21e68ca 530 if (dccp_v4_send_response(sk, req, NULL))
7c657876
ACM
531 goto drop_and_free;
532
533 inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
534 return 0;
535
536drop_and_free:
fc44b980 537 reqsk_free(req);
7c657876
ACM
538drop:
539 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
0c10c5d9 540 dcb->dccpd_reset_code = reset_code;
7c657876
ACM
541 return -1;
542}
543
f21e68ca
ACM
544EXPORT_SYMBOL_GPL(dccp_v4_conn_request);
545
7c657876
ACM
546/*
547 * The three way handshake has completed - we got a valid ACK or DATAACK -
548 * now create the new socket.
549 *
550 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
551 */
552struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
553 struct request_sock *req,
554 struct dst_entry *dst)
555{
556 struct inet_request_sock *ireq;
557 struct inet_sock *newinet;
558 struct dccp_sock *newdp;
559 struct sock *newsk;
560
561 if (sk_acceptq_is_full(sk))
562 goto exit_overflow;
563
564 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
565 goto exit;
566
567 newsk = dccp_create_openreq_child(sk, req, skb);
568 if (newsk == NULL)
569 goto exit;
570
571 sk_setup_caps(newsk, dst);
572
573 newdp = dccp_sk(newsk);
574 newinet = inet_sk(newsk);
575 ireq = inet_rsk(req);
576 newinet->daddr = ireq->rmt_addr;
577 newinet->rcv_saddr = ireq->loc_addr;
578 newinet->saddr = ireq->loc_addr;
579 newinet->opt = ireq->opt;
580 ireq->opt = NULL;
581 newinet->mc_index = inet_iif(skb);
582 newinet->mc_ttl = skb->nh.iph->ttl;
583 newinet->id = jiffies;
584
585 dccp_sync_mss(newsk, dst_mtu(dst));
586
587 __inet_hash(&dccp_hashinfo, newsk, 0);
588 __inet_inherit_port(&dccp_hashinfo, sk, newsk);
589
590 return newsk;
591
592exit_overflow:
593 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
594exit:
595 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
596 dst_release(dst);
597 return NULL;
598}
599
f21e68ca
ACM
600EXPORT_SYMBOL_GPL(dccp_v4_request_recv_sock);
601
7c657876
ACM
602static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
603{
604 const struct dccp_hdr *dh = dccp_hdr(skb);
605 const struct iphdr *iph = skb->nh.iph;
606 struct sock *nsk;
607 struct request_sock **prev;
608 /* Find possible connection requests. */
609 struct request_sock *req = inet_csk_search_req(sk, &prev,
610 dh->dccph_sport,
611 iph->saddr, iph->daddr);
612 if (req != NULL)
613 return dccp_check_req(sk, skb, req, prev);
614
615 nsk = __inet_lookup_established(&dccp_hashinfo,
616 iph->saddr, dh->dccph_sport,
617 iph->daddr, ntohs(dh->dccph_dport),
618 inet_iif(skb));
619 if (nsk != NULL) {
620 if (nsk->sk_state != DCCP_TIME_WAIT) {
621 bh_lock_sock(nsk);
622 return nsk;
623 }
624 inet_twsk_put((struct inet_timewait_sock *)nsk);
625 return NULL;
626 }
627
628 return sk;
629}
630
60fe62e7
AB
631int dccp_v4_checksum(const struct sk_buff *skb, const __be32 saddr,
632 const __be32 daddr)
7c657876 633{
95b81ef7 634 const struct dccp_hdr* dh = dccp_hdr(skb);
7c657876
ACM
635 int checksum_len;
636 u32 tmp;
637
638 if (dh->dccph_cscov == 0)
639 checksum_len = skb->len;
640 else {
641 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
7690af3f
ACM
642 checksum_len = checksum_len < skb->len ? checksum_len :
643 skb->len;
7c657876
ACM
644 }
645
646 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
7690af3f
ACM
647 return csum_tcpudp_magic(saddr, daddr, checksum_len,
648 IPPROTO_DCCP, tmp);
7c657876
ACM
649}
650
b61fafc4
ACM
651EXPORT_SYMBOL_GPL(dccp_v4_checksum);
652
95b81ef7 653static int dccp_v4_verify_checksum(struct sk_buff *skb,
60fe62e7 654 const __be32 saddr, const __be32 daddr)
7c657876 655{
95b81ef7
YN
656 struct dccp_hdr *dh = dccp_hdr(skb);
657 int checksum_len;
658 u32 tmp;
7c657876 659
95b81ef7
YN
660 if (dh->dccph_cscov == 0)
661 checksum_len = skb->len;
662 else {
663 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
7690af3f
ACM
664 checksum_len = checksum_len < skb->len ? checksum_len :
665 skb->len;
95b81ef7
YN
666 }
667 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
7690af3f
ACM
668 return csum_tcpudp_magic(saddr, daddr, checksum_len,
669 IPPROTO_DCCP, tmp) == 0 ? 0 : -1;
7c657876
ACM
670}
671
672static struct dst_entry* dccp_v4_route_skb(struct sock *sk,
673 struct sk_buff *skb)
674{
675 struct rtable *rt;
676 struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif,
677 .nl_u = { .ip4_u =
678 { .daddr = skb->nh.iph->saddr,
679 .saddr = skb->nh.iph->daddr,
680 .tos = RT_CONN_FLAGS(sk) } },
681 .proto = sk->sk_protocol,
682 .uli_u = { .ports =
683 { .sport = dccp_hdr(skb)->dccph_dport,
7690af3f
ACM
684 .dport = dccp_hdr(skb)->dccph_sport }
685 }
686 };
7c657876
ACM
687
688 if (ip_route_output_flow(&rt, &fl, sk, 0)) {
689 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
690 return NULL;
691 }
692
693 return &rt->u.dst;
694}
695
a1d3a355 696static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
7c657876
ACM
697{
698 int err;
699 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
700 const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
701 sizeof(struct dccp_hdr_ext) +
702 sizeof(struct dccp_hdr_reset);
703 struct sk_buff *skb;
704 struct dst_entry *dst;
2807d4ff 705 u64 seqno;
7c657876
ACM
706
707 /* Never send a reset in response to a reset. */
708 if (rxdh->dccph_type == DCCP_PKT_RESET)
709 return;
710
711 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
712 return;
713
72478873 714 dst = dccp_v4_route_skb(dccp_v4_ctl_socket->sk, rxskb);
7c657876
ACM
715 if (dst == NULL)
716 return;
717
718 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
719 if (skb == NULL)
720 goto out;
721
722 /* Reserve space for headers. */
723 skb_reserve(skb, MAX_DCCP_HEADER);
724 skb->dst = dst_clone(dst);
725
726 skb->h.raw = skb_push(skb, dccp_hdr_reset_len);
727 dh = dccp_hdr(skb);
728 memset(dh, 0, dccp_hdr_reset_len);
729
730 /* Build DCCP header and checksum it. */
731 dh->dccph_type = DCCP_PKT_RESET;
732 dh->dccph_sport = rxdh->dccph_dport;
733 dh->dccph_dport = rxdh->dccph_sport;
734 dh->dccph_doff = dccp_hdr_reset_len / 4;
735 dh->dccph_x = 1;
7690af3f
ACM
736 dccp_hdr_reset(skb)->dccph_reset_code =
737 DCCP_SKB_CB(rxskb)->dccpd_reset_code;
7c657876 738
2807d4ff
ACM
739 /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
740 seqno = 0;
741 if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
742 dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
743
744 dccp_hdr_set_seq(dh, seqno);
7690af3f
ACM
745 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
746 DCCP_SKB_CB(rxskb)->dccpd_seq);
7c657876 747
95b81ef7
YN
748 dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr,
749 rxskb->nh.iph->daddr);
7c657876 750
72478873
ACM
751 bh_lock_sock(dccp_v4_ctl_socket->sk);
752 err = ip_build_and_send_pkt(skb, dccp_v4_ctl_socket->sk,
7690af3f
ACM
753 rxskb->nh.iph->daddr,
754 rxskb->nh.iph->saddr, NULL);
72478873 755 bh_unlock_sock(dccp_v4_ctl_socket->sk);
7c657876
ACM
756
757 if (err == NET_XMIT_CN || err == 0) {
758 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
759 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
760 }
761out:
762 dst_release(dst);
763}
764
765int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
766{
767 struct dccp_hdr *dh = dccp_hdr(skb);
768
769 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
770 if (dccp_rcv_established(sk, skb, dh, skb->len))
771 goto reset;
772 return 0;
773 }
774
775 /*
776 * Step 3: Process LISTEN state
777 * If S.state == LISTEN,
7690af3f
ACM
778 * If P.type == Request or P contains a valid Init Cookie
779 * option,
7c657876
ACM
780 * * Must scan the packet's options to check for an Init
781 * Cookie. Only the Init Cookie is processed here,
782 * however; other options are processed in Step 8. This
783 * scan need only be performed if the endpoint uses Init
784 * Cookies *
785 * * Generate a new socket and switch to that socket *
786 * Set S := new socket for this port pair
787 * S.state = RESPOND
788 * Choose S.ISS (initial seqno) or set from Init Cookie
789 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
790 * Continue with S.state == RESPOND
791 * * A Response packet will be generated in Step 11 *
792 * Otherwise,
793 * Generate Reset(No Connection) unless P.type == Reset
794 * Drop packet and return
795 *
7690af3f
ACM
796 * NOTE: the check for the packet types is done in
797 * dccp_rcv_state_process
7c657876
ACM
798 */
799 if (sk->sk_state == DCCP_LISTEN) {
800 struct sock *nsk = dccp_v4_hnd_req(sk, skb);
801
802 if (nsk == NULL)
803 goto discard;
804
805 if (nsk != sk) {
806 if (dccp_child_process(sk, nsk, skb))
807 goto reset;
808 return 0;
809 }
810 }
811
812 if (dccp_rcv_state_process(sk, skb, dh, skb->len))
813 goto reset;
814 return 0;
815
816reset:
7c657876
ACM
817 dccp_v4_ctl_send_reset(skb);
818discard:
819 kfree_skb(skb);
820 return 0;
821}
822
f21e68ca
ACM
823EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
824
825int dccp_invalid_packet(struct sk_buff *skb)
7c657876
ACM
826{
827 const struct dccp_hdr *dh;
828
829 if (skb->pkt_type != PACKET_HOST)
830 return 1;
831
832 if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
c59eab46 833 LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n");
7c657876
ACM
834 return 1;
835 }
836
837 dh = dccp_hdr(skb);
838
839 /* If the packet type is not understood, drop packet and return */
840 if (dh->dccph_type >= DCCP_PKT_INVALID) {
c59eab46 841 LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n");
7c657876
ACM
842 return 1;
843 }
844
845 /*
846 * If P.Data Offset is too small for packet type, or too large for
847 * packet, drop packet and return
848 */
849 if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
c59eab46
ACM
850 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
851 "too small 1\n",
852 dh->dccph_doff);
7c657876
ACM
853 return 1;
854 }
855
856 if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
c59eab46
ACM
857 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
858 "too small 2\n",
859 dh->dccph_doff);
7c657876
ACM
860 return 1;
861 }
862
863 dh = dccp_hdr(skb);
864
865 /*
866 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
867 * has short sequence numbers), drop packet and return
868 */
869 if (dh->dccph_x == 0 &&
870 dh->dccph_type != DCCP_PKT_DATA &&
871 dh->dccph_type != DCCP_PKT_ACK &&
872 dh->dccph_type != DCCP_PKT_DATAACK) {
c59eab46
ACM
873 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack "
874 "nor DataAck and P.X == 0\n",
875 dccp_packet_name(dh->dccph_type));
7c657876
ACM
876 return 1;
877 }
878
7c657876
ACM
879 return 0;
880}
881
f21e68ca
ACM
882EXPORT_SYMBOL_GPL(dccp_invalid_packet);
883
7c657876 884/* this is called when real data arrives */
b61fafc4 885static int dccp_v4_rcv(struct sk_buff *skb)
7c657876
ACM
886{
887 const struct dccp_hdr *dh;
888 struct sock *sk;
7c657876
ACM
889
890 /* Step 1: Check header basics: */
891
892 if (dccp_invalid_packet(skb))
893 goto discard_it;
894
f21e68ca
ACM
895 /* If the header checksum is incorrect, drop packet and return */
896 if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr,
897 skb->nh.iph->daddr) < 0) {
898 LIMIT_NETDEBUG(KERN_WARNING "%s: incorrect header checksum\n",
899 __FUNCTION__);
900 goto discard_it;
901 }
902
7c657876 903 dh = dccp_hdr(skb);
7c657876 904
7c657876
ACM
905 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
906 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
907
908 dccp_pr_debug("%8.8s "
909 "src=%u.%u.%u.%u@%-5d "
910 "dst=%u.%u.%u.%u@%-5d seq=%llu",
911 dccp_packet_name(dh->dccph_type),
912 NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport),
913 NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport),
f6ccf554 914 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
7c657876
ACM
915
916 if (dccp_packet_without_ack(skb)) {
917 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
918 dccp_pr_debug_cat("\n");
919 } else {
920 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
f6ccf554
DM
921 dccp_pr_debug_cat(", ack=%llu\n",
922 (unsigned long long)
923 DCCP_SKB_CB(skb)->dccpd_ack_seq);
7c657876
ACM
924 }
925
926 /* Step 2:
927 * Look up flow ID in table and get corresponding socket */
928 sk = __inet_lookup(&dccp_hashinfo,
929 skb->nh.iph->saddr, dh->dccph_sport,
930 skb->nh.iph->daddr, ntohs(dh->dccph_dport),
931 inet_iif(skb));
932
933 /*
934 * Step 2:
935 * If no socket ...
936 * Generate Reset(No Connection) unless P.type == Reset
937 * Drop packet and return
938 */
939 if (sk == NULL) {
940 dccp_pr_debug("failed to look up flow ID in table and "
941 "get corresponding socket\n");
942 goto no_dccp_socket;
943 }
944
945 /*
946 * Step 2:
947 * ... or S.state == TIMEWAIT,
948 * Generate Reset(No Connection) unless P.type == Reset
949 * Drop packet and return
950 */
951
952 if (sk->sk_state == DCCP_TIME_WAIT) {
64cf1e5d
ACM
953 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
954 "do_time_wait\n");
955 goto do_time_wait;
7c657876
ACM
956 }
957
25995ff5 958 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
7c657876 959 goto discard_and_relse;
eb9c7ebe 960 nf_reset(skb);
7c657876 961
25995ff5 962 return sk_receive_skb(sk, skb);
7c657876
ACM
963
964no_dccp_socket:
965 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
966 goto discard_it;
967 /*
968 * Step 2:
969 * Generate Reset(No Connection) unless P.type == Reset
970 * Drop packet and return
971 */
972 if (dh->dccph_type != DCCP_PKT_RESET) {
7690af3f
ACM
973 DCCP_SKB_CB(skb)->dccpd_reset_code =
974 DCCP_RESET_CODE_NO_CONNECTION;
7c657876
ACM
975 dccp_v4_ctl_send_reset(skb);
976 }
977
978discard_it:
979 /* Discard frame. */
980 kfree_skb(skb);
981 return 0;
982
983discard_and_relse:
984 sock_put(sk);
985 goto discard_it;
64cf1e5d
ACM
986
987do_time_wait:
988 inet_twsk_put((struct inet_timewait_sock *)sk);
989 goto no_dccp_socket;
7c657876
ACM
990}
991
b61fafc4 992static struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
57cca05a
ACM
993 .queue_xmit = ip_queue_xmit,
994 .send_check = dccp_v4_send_check,
995 .rebuild_header = inet_sk_rebuild_header,
996 .conn_request = dccp_v4_conn_request,
997 .syn_recv_sock = dccp_v4_request_recv_sock,
998 .net_header_len = sizeof(struct iphdr),
999 .setsockopt = ip_setsockopt,
1000 .getsockopt = ip_getsockopt,
1001 .addr2sockaddr = inet_csk_addr2sockaddr,
1002 .sockaddr_len = sizeof(struct sockaddr_in),
1003};
1004
3e0fadc5 1005static int dccp_v4_init_sock(struct sock *sk)
7c657876 1006{
72478873
ACM
1007 static __u8 dccp_v4_ctl_sock_initialized;
1008 int err = dccp_init_sock(sk, dccp_v4_ctl_sock_initialized);
f21e68ca 1009
72478873
ACM
1010 if (err == 0) {
1011 if (unlikely(!dccp_v4_ctl_sock_initialized))
1012 dccp_v4_ctl_sock_initialized = 1;
3e0fadc5 1013 inet_csk(sk)->icsk_af_ops = &dccp_ipv4_af_ops;
72478873
ACM
1014 }
1015
3e0fadc5 1016 return err;
7c657876
ACM
1017}
1018
1019static void dccp_v4_reqsk_destructor(struct request_sock *req)
1020{
1021 kfree(inet_rsk(req)->opt);
1022}
1023
1024static struct request_sock_ops dccp_request_sock_ops = {
1025 .family = PF_INET,
1026 .obj_size = sizeof(struct dccp_request_sock),
1027 .rtx_syn_ack = dccp_v4_send_response,
1028 .send_ack = dccp_v4_reqsk_send_ack,
1029 .destructor = dccp_v4_reqsk_destructor,
1030 .send_reset = dccp_v4_ctl_send_reset,
1031};
1032
6d6ee43e
ACM
1033static struct timewait_sock_ops dccp_timewait_sock_ops = {
1034 .twsk_obj_size = sizeof(struct inet_timewait_sock),
1035};
1036
5e0817f8 1037static struct proto dccp_v4_prot = {
7c657876
ACM
1038 .name = "DCCP",
1039 .owner = THIS_MODULE,
1040 .close = dccp_close,
1041 .connect = dccp_v4_connect,
1042 .disconnect = dccp_disconnect,
1043 .ioctl = dccp_ioctl,
1044 .init = dccp_v4_init_sock,
1045 .setsockopt = dccp_setsockopt,
1046 .getsockopt = dccp_getsockopt,
1047 .sendmsg = dccp_sendmsg,
1048 .recvmsg = dccp_recvmsg,
1049 .backlog_rcv = dccp_v4_do_rcv,
c985ed70 1050 .hash = dccp_hash,
f21e68ca 1051 .unhash = dccp_unhash,
7c657876
ACM
1052 .accept = inet_csk_accept,
1053 .get_port = dccp_v4_get_port,
1054 .shutdown = dccp_shutdown,
3e0fadc5 1055 .destroy = dccp_destroy_sock,
7c657876
ACM
1056 .orphan_count = &dccp_orphan_count,
1057 .max_header = MAX_DCCP_HEADER,
1058 .obj_size = sizeof(struct dccp_sock),
1059 .rsk_prot = &dccp_request_sock_ops,
6d6ee43e 1060 .twsk_prot = &dccp_timewait_sock_ops,
7c657876 1061};
6d6ee43e 1062
b61fafc4
ACM
1063static struct net_protocol dccp_v4_protocol = {
1064 .handler = dccp_v4_rcv,
1065 .err_handler = dccp_v4_err,
1066 .no_policy = 1,
1067};
1068
1069static const struct proto_ops inet_dccp_ops = {
1070 .family = PF_INET,
1071 .owner = THIS_MODULE,
1072 .release = inet_release,
1073 .bind = inet_bind,
1074 .connect = inet_stream_connect,
1075 .socketpair = sock_no_socketpair,
1076 .accept = inet_accept,
1077 .getname = inet_getname,
1078 /* FIXME: work on tcp_poll to rename it to inet_csk_poll */
1079 .poll = dccp_poll,
1080 .ioctl = inet_ioctl,
1081 /* FIXME: work on inet_listen to rename it to sock_common_listen */
1082 .listen = inet_dccp_listen,
1083 .shutdown = inet_shutdown,
1084 .setsockopt = sock_common_setsockopt,
1085 .getsockopt = sock_common_getsockopt,
1086 .sendmsg = inet_sendmsg,
1087 .recvmsg = sock_common_recvmsg,
1088 .mmap = sock_no_mmap,
1089 .sendpage = sock_no_sendpage,
1090};
1091
1092static struct inet_protosw dccp_v4_protosw = {
1093 .type = SOCK_DCCP,
1094 .protocol = IPPROTO_DCCP,
1095 .prot = &dccp_v4_prot,
1096 .ops = &inet_dccp_ops,
1097 .capability = -1,
1098 .no_check = 0,
1099 .flags = INET_PROTOSW_ICSK,
1100};
1101
b61fafc4
ACM
1102static int __init dccp_v4_init(void)
1103{
1104 int err = proto_register(&dccp_v4_prot, 1);
1105
1106 if (err != 0)
1107 goto out;
1108
1109 err = inet_add_protocol(&dccp_v4_protocol, IPPROTO_DCCP);
1110 if (err != 0)
1111 goto out_proto_unregister;
1112
1113 inet_register_protosw(&dccp_v4_protosw);
1114
c4d93909
ACM
1115 err = inet_csk_ctl_sock_create(&dccp_v4_ctl_socket, PF_INET,
1116 SOCK_DCCP, IPPROTO_DCCP);
b61fafc4
ACM
1117 if (err)
1118 goto out_unregister_protosw;
1119out:
1120 return err;
1121out_unregister_protosw:
1122 inet_unregister_protosw(&dccp_v4_protosw);
1123 inet_del_protocol(&dccp_v4_protocol, IPPROTO_DCCP);
1124out_proto_unregister:
1125 proto_unregister(&dccp_v4_prot);
1126 goto out;
1127}
1128
1129static void __exit dccp_v4_exit(void)
1130{
1131 inet_unregister_protosw(&dccp_v4_protosw);
1132 inet_del_protocol(&dccp_v4_protocol, IPPROTO_DCCP);
1133 proto_unregister(&dccp_v4_prot);
1134}
1135
1136module_init(dccp_v4_init);
1137module_exit(dccp_v4_exit);
1138
1139/*
1140 * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33)
1141 * values directly, Also cover the case where the protocol is not specified,
1142 * i.e. net-pf-PF_INET-proto-0-type-SOCK_DCCP
1143 */
1144MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-33-type-6");
1145MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-0-type-6");
1146MODULE_LICENSE("GPL");
1147MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@mandriva.com>");
1148MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");