1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * net/dccp/ipv4.c
4 *
5 * An implementation of the DCCP protocol
6 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
7 */
8
9 #include <linux/dccp.h>
10 #include <linux/icmp.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/random.h>
15
16 #include <net/icmp.h>
17 #include <net/inet_common.h>
18 #include <net/inet_hashtables.h>
19 #include <net/inet_sock.h>
20 #include <net/protocol.h>
21 #include <net/sock.h>
22 #include <net/timewait_sock.h>
23 #include <net/tcp_states.h>
24 #include <net/xfrm.h>
25 #include <net/secure_seq.h>
26 #include <net/netns/generic.h>
27
28 #include "ackvec.h"
29 #include "ccid.h"
30 #include "dccp.h"
31 #include "feat.h"
32
33 struct dccp_v4_pernet {
34 struct sock *v4_ctl_sk;
35 };
36
37 static unsigned int dccp_v4_pernet_id __read_mostly;
38
39 /*
40 * The per-net v4_ctl_sk socket is used for responding to
41 * the Out-of-the-blue (OOTB) packets. A control sock will be created
42 * for this socket at the initialization time.
43 */
44
dccp_v4_connect(struct sock * sk,struct sockaddr * uaddr,int addr_len)45 int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
46 {
47 const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
48 struct inet_sock *inet = inet_sk(sk);
49 struct dccp_sock *dp = dccp_sk(sk);
50 __be16 orig_sport, orig_dport;
51 __be32 daddr, nexthop;
52 struct flowi4 *fl4;
53 struct rtable *rt;
54 int err;
55 struct ip_options_rcu *inet_opt;
56
57 dp->dccps_role = DCCP_ROLE_CLIENT;
58
59 if (addr_len < sizeof(struct sockaddr_in))
60 return -EINVAL;
61
62 if (usin->sin_family != AF_INET)
63 return -EAFNOSUPPORT;
64
65 nexthop = daddr = usin->sin_addr.s_addr;
66
67 inet_opt = rcu_dereference_protected(inet->inet_opt,
68 lockdep_sock_is_held(sk));
69 if (inet_opt != NULL && inet_opt->opt.srr) {
70 if (daddr == 0)
71 return -EINVAL;
72 nexthop = inet_opt->opt.faddr;
73 }
74
75 orig_sport = inet->inet_sport;
76 orig_dport = usin->sin_port;
77 fl4 = &inet->cork.fl.u.ip4;
78 rt = ip_route_connect(fl4, nexthop, inet->inet_saddr,
79 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
80 IPPROTO_DCCP,
81 orig_sport, orig_dport, sk);
82 if (IS_ERR(rt))
83 return PTR_ERR(rt);
84
85 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
86 ip_rt_put(rt);
87 return -ENETUNREACH;
88 }
89
90 if (inet_opt == NULL || !inet_opt->opt.srr)
91 daddr = fl4->daddr;
92
93 if (inet->inet_saddr == 0)
94 inet->inet_saddr = fl4->saddr;
95 sk_rcv_saddr_set(sk, inet->inet_saddr);
96 inet->inet_dport = usin->sin_port;
97 sk_daddr_set(sk, daddr);
98
99 inet_csk(sk)->icsk_ext_hdr_len = 0;
100 if (inet_opt)
101 inet_csk(sk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
102 /*
103 * Socket identity is still unknown (sport may be zero).
104 * However we set state to DCCP_REQUESTING and not releasing socket
105 * lock select source port, enter ourselves into the hash tables and
106 * complete initialization after this.
107 */
108 dccp_set_state(sk, DCCP_REQUESTING);
109 err = inet_hash_connect(&dccp_death_row, sk);
110 if (err != 0)
111 goto failure;
112
113 rt = ip_route_newports(fl4, rt, orig_sport, orig_dport,
114 inet->inet_sport, inet->inet_dport, sk);
115 if (IS_ERR(rt)) {
116 err = PTR_ERR(rt);
117 rt = NULL;
118 goto failure;
119 }
120 /* OK, now commit destination to socket. */
121 sk_setup_caps(sk, &rt->dst);
122
123 dp->dccps_iss = secure_dccp_sequence_number(inet->inet_saddr,
124 inet->inet_daddr,
125 inet->inet_sport,
126 inet->inet_dport);
127 inet->inet_id = prandom_u32();
128
129 err = dccp_connect(sk);
130 rt = NULL;
131 if (err != 0)
132 goto failure;
133 out:
134 return err;
135 failure:
136 /*
137 * This unhashes the socket and releases the local port, if necessary.
138 */
139 dccp_set_state(sk, DCCP_CLOSED);
140 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
141 inet_reset_saddr(sk);
142 ip_rt_put(rt);
143 sk->sk_route_caps = 0;
144 inet->inet_dport = 0;
145 goto out;
146 }
147 EXPORT_SYMBOL_GPL(dccp_v4_connect);
148
149 /*
150 * This routine does path mtu discovery as defined in RFC1191.
151 */
dccp_do_pmtu_discovery(struct sock * sk,const struct iphdr * iph,u32 mtu)152 static inline void dccp_do_pmtu_discovery(struct sock *sk,
153 const struct iphdr *iph,
154 u32 mtu)
155 {
156 struct dst_entry *dst;
157 const struct inet_sock *inet = inet_sk(sk);
158 const struct dccp_sock *dp = dccp_sk(sk);
159
160 /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
161 * send out by Linux are always < 576bytes so they should go through
162 * unfragmented).
163 */
164 if (sk->sk_state == DCCP_LISTEN)
165 return;
166
167 dst = inet_csk_update_pmtu(sk, mtu);
168 if (!dst)
169 return;
170
171 /* Something is about to be wrong... Remember soft error
172 * for the case, if this connection will not able to recover.
173 */
174 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
175 sk->sk_err_soft = EMSGSIZE;
176
177 mtu = dst_mtu(dst);
178
179 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
180 ip_sk_accept_pmtu(sk) &&
181 inet_csk(sk)->icsk_pmtu_cookie > mtu) {
182 dccp_sync_mss(sk, mtu);
183
184 /*
185 * From RFC 4340, sec. 14.1:
186 *
187 * DCCP-Sync packets are the best choice for upward
188 * probing, since DCCP-Sync probes do not risk application
189 * data loss.
190 */
191 dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
192 } /* else let the usual retransmit timer handle it */
193 }
194
dccp_do_redirect(struct sk_buff * skb,struct sock * sk)195 static void dccp_do_redirect(struct sk_buff *skb, struct sock *sk)
196 {
197 struct dst_entry *dst = __sk_dst_check(sk, 0);
198
199 if (dst)
200 dst->ops->redirect(dst, sk, skb);
201 }
202
dccp_req_err(struct sock * sk,u64 seq)203 void dccp_req_err(struct sock *sk, u64 seq)
204 {
205 struct request_sock *req = inet_reqsk(sk);
206 struct net *net = sock_net(sk);
207
208 /*
209 * ICMPs are not backlogged, hence we cannot get an established
210 * socket here.
211 */
212 if (!between48(seq, dccp_rsk(req)->dreq_iss, dccp_rsk(req)->dreq_gss)) {
213 __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
214 } else {
215 /*
216 * Still in RESPOND, just remove it silently.
217 * There is no good way to pass the error to the newly
218 * created socket, and POSIX does not want network
219 * errors returned from accept().
220 */
221 inet_csk_reqsk_queue_drop(req->rsk_listener, req);
222 }
223 reqsk_put(req);
224 }
225 EXPORT_SYMBOL(dccp_req_err);
226
227 /*
228 * This routine is called by the ICMP module when it gets some sort of error
229 * condition. If err < 0 then the socket should be closed and the error
230 * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
231 * After adjustment header points to the first 8 bytes of the tcp header. We
232 * need to find the appropriate port.
233 *
234 * The locking strategy used here is very "optimistic". When someone else
235 * accesses the socket the ICMP is just dropped and for some paths there is no
236 * check at all. A more general error queue to queue errors for later handling
237 * is probably better.
238 */
dccp_v4_err(struct sk_buff * skb,u32 info)239 static int dccp_v4_err(struct sk_buff *skb, u32 info)
240 {
241 const struct iphdr *iph = (struct iphdr *)skb->data;
242 const u8 offset = iph->ihl << 2;
243 const struct dccp_hdr *dh;
244 struct dccp_sock *dp;
245 struct inet_sock *inet;
246 const int type = icmp_hdr(skb)->type;
247 const int code = icmp_hdr(skb)->code;
248 struct sock *sk;
249 __u64 seq;
250 int err;
251 struct net *net = dev_net(skb->dev);
252
253 if (!pskb_may_pull(skb, offset + sizeof(*dh)))
254 return -EINVAL;
255 dh = (struct dccp_hdr *)(skb->data + offset);
256 if (!pskb_may_pull(skb, offset + __dccp_basic_hdr_len(dh)))
257 return -EINVAL;
258 iph = (struct iphdr *)skb->data;
259 dh = (struct dccp_hdr *)(skb->data + offset);
260
261 sk = __inet_lookup_established(net, &dccp_hashinfo,
262 iph->daddr, dh->dccph_dport,
263 iph->saddr, ntohs(dh->dccph_sport),
264 inet_iif(skb), 0);
265 if (!sk) {
266 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
267 return -ENOENT;
268 }
269
270 if (sk->sk_state == DCCP_TIME_WAIT) {
271 inet_twsk_put(inet_twsk(sk));
272 return 0;
273 }
274 seq = dccp_hdr_seq(dh);
275 if (sk->sk_state == DCCP_NEW_SYN_RECV) {
276 dccp_req_err(sk, seq);
277 return 0;
278 }
279
280 bh_lock_sock(sk);
281 /* If too many ICMPs get dropped on busy
282 * servers this needs to be solved differently.
283 */
284 if (sock_owned_by_user(sk))
285 __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
286
287 if (sk->sk_state == DCCP_CLOSED)
288 goto out;
289
290 dp = dccp_sk(sk);
291 if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_LISTEN) &&
292 !between48(seq, dp->dccps_awl, dp->dccps_awh)) {
293 __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
294 goto out;
295 }
296
297 switch (type) {
298 case ICMP_REDIRECT:
299 if (!sock_owned_by_user(sk))
300 dccp_do_redirect(skb, sk);
301 goto out;
302 case ICMP_SOURCE_QUENCH:
303 /* Just silently ignore these. */
304 goto out;
305 case ICMP_PARAMETERPROB:
306 err = EPROTO;
307 break;
308 case ICMP_DEST_UNREACH:
309 if (code > NR_ICMP_UNREACH)
310 goto out;
311
312 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
313 if (!sock_owned_by_user(sk))
314 dccp_do_pmtu_discovery(sk, iph, info);
315 goto out;
316 }
317
318 err = icmp_err_convert[code].errno;
319 break;
320 case ICMP_TIME_EXCEEDED:
321 err = EHOSTUNREACH;
322 break;
323 default:
324 goto out;
325 }
326
327 switch (sk->sk_state) {
328 case DCCP_REQUESTING:
329 case DCCP_RESPOND:
330 if (!sock_owned_by_user(sk)) {
331 __DCCP_INC_STATS(DCCP_MIB_ATTEMPTFAILS);
332 sk->sk_err = err;
333
334 sk_error_report(sk);
335
336 dccp_done(sk);
337 } else
338 sk->sk_err_soft = err;
339 goto out;
340 }
341
342 /* If we've already connected we will keep trying
343 * until we time out, or the user gives up.
344 *
345 * rfc1122 4.2.3.9 allows to consider as hard errors
346 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
347 * but it is obsoleted by pmtu discovery).
348 *
349 * Note, that in modern internet, where routing is unreliable
350 * and in each dark corner broken firewalls sit, sending random
351 * errors ordered by their masters even this two messages finally lose
352 * their original sense (even Linux sends invalid PORT_UNREACHs)
353 *
354 * Now we are in compliance with RFCs.
355 * --ANK (980905)
356 */
357
358 inet = inet_sk(sk);
359 if (!sock_owned_by_user(sk) && inet->recverr) {
360 sk->sk_err = err;
361 sk_error_report(sk);
362 } else /* Only an error on timeout */
363 sk->sk_err_soft = err;
364 out:
365 bh_unlock_sock(sk);
366 sock_put(sk);
367 return 0;
368 }
369
dccp_v4_csum_finish(struct sk_buff * skb,__be32 src,__be32 dst)370 static inline __sum16 dccp_v4_csum_finish(struct sk_buff *skb,
371 __be32 src, __be32 dst)
372 {
373 return csum_tcpudp_magic(src, dst, skb->len, IPPROTO_DCCP, skb->csum);
374 }
375
dccp_v4_send_check(struct sock * sk,struct sk_buff * skb)376 void dccp_v4_send_check(struct sock *sk, struct sk_buff *skb)
377 {
378 const struct inet_sock *inet = inet_sk(sk);
379 struct dccp_hdr *dh = dccp_hdr(skb);
380
381 dccp_csum_outgoing(skb);
382 dh->dccph_checksum = dccp_v4_csum_finish(skb,
383 inet->inet_saddr,
384 inet->inet_daddr);
385 }
386 EXPORT_SYMBOL_GPL(dccp_v4_send_check);
387
dccp_v4_init_sequence(const struct sk_buff * skb)388 static inline u64 dccp_v4_init_sequence(const struct sk_buff *skb)
389 {
390 return secure_dccp_sequence_number(ip_hdr(skb)->daddr,
391 ip_hdr(skb)->saddr,
392 dccp_hdr(skb)->dccph_dport,
393 dccp_hdr(skb)->dccph_sport);
394 }
395
396 /*
397 * The three way handshake has completed - we got a valid ACK or DATAACK -
398 * now create the new socket.
399 *
400 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
401 */
dccp_v4_request_recv_sock(const struct sock * sk,struct sk_buff * skb,struct request_sock * req,struct dst_entry * dst,struct request_sock * req_unhash,bool * own_req)402 struct sock *dccp_v4_request_recv_sock(const struct sock *sk,
403 struct sk_buff *skb,
404 struct request_sock *req,
405 struct dst_entry *dst,
406 struct request_sock *req_unhash,
407 bool *own_req)
408 {
409 struct inet_request_sock *ireq;
410 struct inet_sock *newinet;
411 struct sock *newsk;
412
413 if (sk_acceptq_is_full(sk))
414 goto exit_overflow;
415
416 newsk = dccp_create_openreq_child(sk, req, skb);
417 if (newsk == NULL)
418 goto exit_nonewsk;
419
420 newinet = inet_sk(newsk);
421 ireq = inet_rsk(req);
422 sk_daddr_set(newsk, ireq->ir_rmt_addr);
423 sk_rcv_saddr_set(newsk, ireq->ir_loc_addr);
424 newinet->inet_saddr = ireq->ir_loc_addr;
425 RCU_INIT_POINTER(newinet->inet_opt, rcu_dereference(ireq->ireq_opt));
426 newinet->mc_index = inet_iif(skb);
427 newinet->mc_ttl = ip_hdr(skb)->ttl;
428 newinet->inet_id = prandom_u32();
429
430 if (dst == NULL && (dst = inet_csk_route_child_sock(sk, newsk, req)) == NULL)
431 goto put_and_exit;
432
433 sk_setup_caps(newsk, dst);
434
435 dccp_sync_mss(newsk, dst_mtu(dst));
436
437 if (__inet_inherit_port(sk, newsk) < 0)
438 goto put_and_exit;
439 *own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash), NULL);
440 if (*own_req)
441 ireq->ireq_opt = NULL;
442 else
443 newinet->inet_opt = NULL;
444 return newsk;
445
446 exit_overflow:
447 __NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
448 exit_nonewsk:
449 dst_release(dst);
450 exit:
451 __NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENDROPS);
452 return NULL;
453 put_and_exit:
454 newinet->inet_opt = NULL;
455 inet_csk_prepare_forced_close(newsk);
456 dccp_done(newsk);
457 goto exit;
458 }
459 EXPORT_SYMBOL_GPL(dccp_v4_request_recv_sock);
460
dccp_v4_route_skb(struct net * net,struct sock * sk,struct sk_buff * skb)461 static struct dst_entry* dccp_v4_route_skb(struct net *net, struct sock *sk,
462 struct sk_buff *skb)
463 {
464 struct rtable *rt;
465 const struct iphdr *iph = ip_hdr(skb);
466 struct flowi4 fl4 = {
467 .flowi4_oif = inet_iif(skb),
468 .daddr = iph->saddr,
469 .saddr = iph->daddr,
470 .flowi4_tos = RT_CONN_FLAGS(sk),
471 .flowi4_proto = sk->sk_protocol,
472 .fl4_sport = dccp_hdr(skb)->dccph_dport,
473 .fl4_dport = dccp_hdr(skb)->dccph_sport,
474 };
475
476 security_skb_classify_flow(skb, flowi4_to_flowi_common(&fl4));
477 rt = ip_route_output_flow(net, &fl4, sk);
478 if (IS_ERR(rt)) {
479 IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
480 return NULL;
481 }
482
483 return &rt->dst;
484 }
485
dccp_v4_send_response(const struct sock * sk,struct request_sock * req)486 static int dccp_v4_send_response(const struct sock *sk, struct request_sock *req)
487 {
488 int err = -1;
489 struct sk_buff *skb;
490 struct dst_entry *dst;
491 struct flowi4 fl4;
492
493 dst = inet_csk_route_req(sk, &fl4, req);
494 if (dst == NULL)
495 goto out;
496
497 skb = dccp_make_response(sk, dst, req);
498 if (skb != NULL) {
499 const struct inet_request_sock *ireq = inet_rsk(req);
500 struct dccp_hdr *dh = dccp_hdr(skb);
501
502 dh->dccph_checksum = dccp_v4_csum_finish(skb, ireq->ir_loc_addr,
503 ireq->ir_rmt_addr);
504 rcu_read_lock();
505 err = ip_build_and_send_pkt(skb, sk, ireq->ir_loc_addr,
506 ireq->ir_rmt_addr,
507 rcu_dereference(ireq->ireq_opt),
508 inet_sk(sk)->tos);
509 rcu_read_unlock();
510 err = net_xmit_eval(err);
511 }
512
513 out:
514 dst_release(dst);
515 return err;
516 }
517
dccp_v4_ctl_send_reset(const struct sock * sk,struct sk_buff * rxskb)518 static void dccp_v4_ctl_send_reset(const struct sock *sk, struct sk_buff *rxskb)
519 {
520 int err;
521 const struct iphdr *rxiph;
522 struct sk_buff *skb;
523 struct dst_entry *dst;
524 struct net *net = dev_net(skb_dst(rxskb)->dev);
525 struct dccp_v4_pernet *pn;
526 struct sock *ctl_sk;
527
528 /* Never send a reset in response to a reset. */
529 if (dccp_hdr(rxskb)->dccph_type == DCCP_PKT_RESET)
530 return;
531
532 if (skb_rtable(rxskb)->rt_type != RTN_LOCAL)
533 return;
534
535 pn = net_generic(net, dccp_v4_pernet_id);
536 ctl_sk = pn->v4_ctl_sk;
537 dst = dccp_v4_route_skb(net, ctl_sk, rxskb);
538 if (dst == NULL)
539 return;
540
541 skb = dccp_ctl_make_reset(ctl_sk, rxskb);
542 if (skb == NULL)
543 goto out;
544
545 rxiph = ip_hdr(rxskb);
546 dccp_hdr(skb)->dccph_checksum = dccp_v4_csum_finish(skb, rxiph->saddr,
547 rxiph->daddr);
548 skb_dst_set(skb, dst_clone(dst));
549
550 local_bh_disable();
551 bh_lock_sock(ctl_sk);
552 err = ip_build_and_send_pkt(skb, ctl_sk,
553 rxiph->daddr, rxiph->saddr, NULL,
554 inet_sk(ctl_sk)->tos);
555 bh_unlock_sock(ctl_sk);
556
557 if (net_xmit_eval(err) == 0) {
558 __DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
559 __DCCP_INC_STATS(DCCP_MIB_OUTRSTS);
560 }
561 local_bh_enable();
562 out:
563 dst_release(dst);
564 }
565
dccp_v4_reqsk_destructor(struct request_sock * req)566 static void dccp_v4_reqsk_destructor(struct request_sock *req)
567 {
568 dccp_feat_list_purge(&dccp_rsk(req)->dreq_featneg);
569 kfree(rcu_dereference_protected(inet_rsk(req)->ireq_opt, 1));
570 }
571
dccp_syn_ack_timeout(const struct request_sock * req)572 void dccp_syn_ack_timeout(const struct request_sock *req)
573 {
574 }
575 EXPORT_SYMBOL(dccp_syn_ack_timeout);
576
577 static struct request_sock_ops dccp_request_sock_ops __read_mostly = {
578 .family = PF_INET,
579 .obj_size = sizeof(struct dccp_request_sock),
580 .rtx_syn_ack = dccp_v4_send_response,
581 .send_ack = dccp_reqsk_send_ack,
582 .destructor = dccp_v4_reqsk_destructor,
583 .send_reset = dccp_v4_ctl_send_reset,
584 .syn_ack_timeout = dccp_syn_ack_timeout,
585 };
586
dccp_v4_conn_request(struct sock * sk,struct sk_buff * skb)587 int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
588 {
589 struct inet_request_sock *ireq;
590 struct request_sock *req;
591 struct dccp_request_sock *dreq;
592 const __be32 service = dccp_hdr_request(skb)->dccph_req_service;
593 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
594
595 /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
596 if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
597 return 0; /* discard, don't send a reset here */
598
599 if (dccp_bad_service_code(sk, service)) {
600 dcb->dccpd_reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
601 goto drop;
602 }
603 /*
604 * TW buckets are converted to open requests without
605 * limitations, they conserve resources and peer is
606 * evidently real one.
607 */
608 dcb->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
609 if (inet_csk_reqsk_queue_is_full(sk))
610 goto drop;
611
612 if (sk_acceptq_is_full(sk))
613 goto drop;
614
615 req = inet_reqsk_alloc(&dccp_request_sock_ops, sk, true);
616 if (req == NULL)
617 goto drop;
618
619 if (dccp_reqsk_init(req, dccp_sk(sk), skb))
620 goto drop_and_free;
621
622 dreq = dccp_rsk(req);
623 if (dccp_parse_options(sk, dreq, skb))
624 goto drop_and_free;
625
626 ireq = inet_rsk(req);
627 sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr);
628 sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr);
629 ireq->ir_mark = inet_request_mark(sk, skb);
630 ireq->ireq_family = AF_INET;
631 ireq->ir_iif = sk->sk_bound_dev_if;
632
633 if (security_inet_conn_request(sk, skb, req))
634 goto drop_and_free;
635
636 /*
637 * Step 3: Process LISTEN state
638 *
639 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
640 *
641 * Setting S.SWL/S.SWH to is deferred to dccp_create_openreq_child().
642 */
643 dreq->dreq_isr = dcb->dccpd_seq;
644 dreq->dreq_gsr = dreq->dreq_isr;
645 dreq->dreq_iss = dccp_v4_init_sequence(skb);
646 dreq->dreq_gss = dreq->dreq_iss;
647 dreq->dreq_service = service;
648
649 if (dccp_v4_send_response(sk, req))
650 goto drop_and_free;
651
652 inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
653 reqsk_put(req);
654 return 0;
655
656 drop_and_free:
657 reqsk_free(req);
658 drop:
659 __DCCP_INC_STATS(DCCP_MIB_ATTEMPTFAILS);
660 return -1;
661 }
662 EXPORT_SYMBOL_GPL(dccp_v4_conn_request);
663
dccp_v4_do_rcv(struct sock * sk,struct sk_buff * skb)664 int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
665 {
666 struct dccp_hdr *dh = dccp_hdr(skb);
667
668 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
669 if (dccp_rcv_established(sk, skb, dh, skb->len))
670 goto reset;
671 return 0;
672 }
673
674 /*
675 * Step 3: Process LISTEN state
676 * If P.type == Request or P contains a valid Init Cookie option,
677 * (* Must scan the packet's options to check for Init
678 * Cookies. Only Init Cookies are processed here,
679 * however; other options are processed in Step 8. This
680 * scan need only be performed if the endpoint uses Init
681 * Cookies *)
682 * (* Generate a new socket and switch to that socket *)
683 * Set S := new socket for this port pair
684 * S.state = RESPOND
685 * Choose S.ISS (initial seqno) or set from Init Cookies
686 * Initialize S.GAR := S.ISS
687 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookies
688 * Continue with S.state == RESPOND
689 * (* A Response packet will be generated in Step 11 *)
690 * Otherwise,
691 * Generate Reset(No Connection) unless P.type == Reset
692 * Drop packet and return
693 *
694 * NOTE: the check for the packet types is done in
695 * dccp_rcv_state_process
696 */
697
698 if (dccp_rcv_state_process(sk, skb, dh, skb->len))
699 goto reset;
700 return 0;
701
702 reset:
703 dccp_v4_ctl_send_reset(sk, skb);
704 kfree_skb(skb);
705 return 0;
706 }
707 EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
708
709 /**
710 * dccp_invalid_packet - check for malformed packets
711 * @skb: Packet to validate
712 *
713 * Implements RFC 4340, 8.5: Step 1: Check header basics
714 * Packets that fail these checks are ignored and do not receive Resets.
715 */
dccp_invalid_packet(struct sk_buff * skb)716 int dccp_invalid_packet(struct sk_buff *skb)
717 {
718 const struct dccp_hdr *dh;
719 unsigned int cscov;
720 u8 dccph_doff;
721
722 if (skb->pkt_type != PACKET_HOST)
723 return 1;
724
725 /* If the packet is shorter than 12 bytes, drop packet and return */
726 if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
727 DCCP_WARN("pskb_may_pull failed\n");
728 return 1;
729 }
730
731 dh = dccp_hdr(skb);
732
733 /* If P.type is not understood, drop packet and return */
734 if (dh->dccph_type >= DCCP_PKT_INVALID) {
735 DCCP_WARN("invalid packet type\n");
736 return 1;
737 }
738
739 /*
740 * If P.Data Offset is too small for packet type, drop packet and return
741 */
742 dccph_doff = dh->dccph_doff;
743 if (dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
744 DCCP_WARN("P.Data Offset(%u) too small\n", dccph_doff);
745 return 1;
746 }
747 /*
748 * If P.Data Offset is too large for packet, drop packet and return
749 */
750 if (!pskb_may_pull(skb, dccph_doff * sizeof(u32))) {
751 DCCP_WARN("P.Data Offset(%u) too large\n", dccph_doff);
752 return 1;
753 }
754 dh = dccp_hdr(skb);
755 /*
756 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
757 * has short sequence numbers), drop packet and return
758 */
759 if ((dh->dccph_type < DCCP_PKT_DATA ||
760 dh->dccph_type > DCCP_PKT_DATAACK) && dh->dccph_x == 0) {
761 DCCP_WARN("P.type (%s) not Data || [Data]Ack, while P.X == 0\n",
762 dccp_packet_name(dh->dccph_type));
763 return 1;
764 }
765
766 /*
767 * If P.CsCov is too large for the packet size, drop packet and return.
768 * This must come _before_ checksumming (not as RFC 4340 suggests).
769 */
770 cscov = dccp_csum_coverage(skb);
771 if (cscov > skb->len) {
772 DCCP_WARN("P.CsCov %u exceeds packet length %d\n",
773 dh->dccph_cscov, skb->len);
774 return 1;
775 }
776
777 /* If header checksum is incorrect, drop packet and return.
778 * (This step is completed in the AF-dependent functions.) */
779 skb->csum = skb_checksum(skb, 0, cscov, 0);
780
781 return 0;
782 }
783 EXPORT_SYMBOL_GPL(dccp_invalid_packet);
784
785 /* this is called when real data arrives */
dccp_v4_rcv(struct sk_buff * skb)786 static int dccp_v4_rcv(struct sk_buff *skb)
787 {
788 const struct dccp_hdr *dh;
789 const struct iphdr *iph;
790 bool refcounted;
791 struct sock *sk;
792 int min_cov;
793
794 /* Step 1: Check header basics */
795
796 if (dccp_invalid_packet(skb))
797 goto discard_it;
798
799 iph = ip_hdr(skb);
800 /* Step 1: If header checksum is incorrect, drop packet and return */
801 if (dccp_v4_csum_finish(skb, iph->saddr, iph->daddr)) {
802 DCCP_WARN("dropped packet with invalid checksum\n");
803 goto discard_it;
804 }
805
806 dh = dccp_hdr(skb);
807
808 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(dh);
809 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
810
811 dccp_pr_debug("%8.8s src=%pI4@%-5d dst=%pI4@%-5d seq=%llu",
812 dccp_packet_name(dh->dccph_type),
813 &iph->saddr, ntohs(dh->dccph_sport),
814 &iph->daddr, ntohs(dh->dccph_dport),
815 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
816
817 if (dccp_packet_without_ack(skb)) {
818 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
819 dccp_pr_debug_cat("\n");
820 } else {
821 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
822 dccp_pr_debug_cat(", ack=%llu\n", (unsigned long long)
823 DCCP_SKB_CB(skb)->dccpd_ack_seq);
824 }
825
826 lookup:
827 sk = __inet_lookup_skb(&dccp_hashinfo, skb, __dccp_hdr_len(dh),
828 dh->dccph_sport, dh->dccph_dport, 0, &refcounted);
829 if (!sk) {
830 dccp_pr_debug("failed to look up flow ID in table and "
831 "get corresponding socket\n");
832 goto no_dccp_socket;
833 }
834
835 /*
836 * Step 2:
837 * ... or S.state == TIMEWAIT,
838 * Generate Reset(No Connection) unless P.type == Reset
839 * Drop packet and return
840 */
841 if (sk->sk_state == DCCP_TIME_WAIT) {
842 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: do_time_wait\n");
843 inet_twsk_put(inet_twsk(sk));
844 goto no_dccp_socket;
845 }
846
847 if (sk->sk_state == DCCP_NEW_SYN_RECV) {
848 struct request_sock *req = inet_reqsk(sk);
849 struct sock *nsk;
850
851 sk = req->rsk_listener;
852 if (unlikely(sk->sk_state != DCCP_LISTEN)) {
853 inet_csk_reqsk_queue_drop_and_put(sk, req);
854 goto lookup;
855 }
856 sock_hold(sk);
857 refcounted = true;
858 nsk = dccp_check_req(sk, skb, req);
859 if (!nsk) {
860 reqsk_put(req);
861 goto discard_and_relse;
862 }
863 if (nsk == sk) {
864 reqsk_put(req);
865 } else if (dccp_child_process(sk, nsk, skb)) {
866 dccp_v4_ctl_send_reset(sk, skb);
867 goto discard_and_relse;
868 } else {
869 sock_put(sk);
870 return 0;
871 }
872 }
873 /*
874 * RFC 4340, sec. 9.2.1: Minimum Checksum Coverage
875 * o if MinCsCov = 0, only packets with CsCov = 0 are accepted
876 * o if MinCsCov > 0, also accept packets with CsCov >= MinCsCov
877 */
878 min_cov = dccp_sk(sk)->dccps_pcrlen;
879 if (dh->dccph_cscov && (min_cov == 0 || dh->dccph_cscov < min_cov)) {
880 dccp_pr_debug("Packet CsCov %d does not satisfy MinCsCov %d\n",
881 dh->dccph_cscov, min_cov);
882 /* FIXME: "Such packets SHOULD be reported using Data Dropped
883 * options (Section 11.7) with Drop Code 0, Protocol
884 * Constraints." */
885 goto discard_and_relse;
886 }
887
888 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
889 goto discard_and_relse;
890 nf_reset_ct(skb);
891
892 return __sk_receive_skb(sk, skb, 1, dh->dccph_doff * 4, refcounted);
893
894 no_dccp_socket:
895 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
896 goto discard_it;
897 /*
898 * Step 2:
899 * If no socket ...
900 * Generate Reset(No Connection) unless P.type == Reset
901 * Drop packet and return
902 */
903 if (dh->dccph_type != DCCP_PKT_RESET) {
904 DCCP_SKB_CB(skb)->dccpd_reset_code =
905 DCCP_RESET_CODE_NO_CONNECTION;
906 dccp_v4_ctl_send_reset(sk, skb);
907 }
908
909 discard_it:
910 kfree_skb(skb);
911 return 0;
912
913 discard_and_relse:
914 if (refcounted)
915 sock_put(sk);
916 goto discard_it;
917 }
918
919 static const struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
920 .queue_xmit = ip_queue_xmit,
921 .send_check = dccp_v4_send_check,
922 .rebuild_header = inet_sk_rebuild_header,
923 .conn_request = dccp_v4_conn_request,
924 .syn_recv_sock = dccp_v4_request_recv_sock,
925 .net_header_len = sizeof(struct iphdr),
926 .setsockopt = ip_setsockopt,
927 .getsockopt = ip_getsockopt,
928 .addr2sockaddr = inet_csk_addr2sockaddr,
929 .sockaddr_len = sizeof(struct sockaddr_in),
930 };
931
dccp_v4_init_sock(struct sock * sk)932 static int dccp_v4_init_sock(struct sock *sk)
933 {
934 static __u8 dccp_v4_ctl_sock_initialized;
935 int err = dccp_init_sock(sk, dccp_v4_ctl_sock_initialized);
936
937 if (err == 0) {
938 if (unlikely(!dccp_v4_ctl_sock_initialized))
939 dccp_v4_ctl_sock_initialized = 1;
940 inet_csk(sk)->icsk_af_ops = &dccp_ipv4_af_ops;
941 }
942
943 return err;
944 }
945
946 static struct timewait_sock_ops dccp_timewait_sock_ops = {
947 .twsk_obj_size = sizeof(struct inet_timewait_sock),
948 };
949
950 static struct proto dccp_v4_prot = {
951 .name = "DCCP",
952 .owner = THIS_MODULE,
953 .close = dccp_close,
954 .connect = dccp_v4_connect,
955 .disconnect = dccp_disconnect,
956 .ioctl = dccp_ioctl,
957 .init = dccp_v4_init_sock,
958 .setsockopt = dccp_setsockopt,
959 .getsockopt = dccp_getsockopt,
960 .sendmsg = dccp_sendmsg,
961 .recvmsg = dccp_recvmsg,
962 .backlog_rcv = dccp_v4_do_rcv,
963 .hash = inet_hash,
964 .unhash = inet_unhash,
965 .accept = inet_csk_accept,
966 .get_port = inet_csk_get_port,
967 .shutdown = dccp_shutdown,
968 .destroy = dccp_destroy_sock,
969 .orphan_count = &dccp_orphan_count,
970 .max_header = MAX_DCCP_HEADER,
971 .obj_size = sizeof(struct dccp_sock),
972 .slab_flags = SLAB_TYPESAFE_BY_RCU,
973 .rsk_prot = &dccp_request_sock_ops,
974 .twsk_prot = &dccp_timewait_sock_ops,
975 .h.hashinfo = &dccp_hashinfo,
976 };
977
978 static const struct net_protocol dccp_v4_protocol = {
979 .handler = dccp_v4_rcv,
980 .err_handler = dccp_v4_err,
981 .no_policy = 1,
982 .icmp_strict_tag_validation = 1,
983 };
984
985 static const struct proto_ops inet_dccp_ops = {
986 .family = PF_INET,
987 .owner = THIS_MODULE,
988 .release = inet_release,
989 .bind = inet_bind,
990 .connect = inet_stream_connect,
991 .socketpair = sock_no_socketpair,
992 .accept = inet_accept,
993 .getname = inet_getname,
994 /* FIXME: work on tcp_poll to rename it to inet_csk_poll */
995 .poll = dccp_poll,
996 .ioctl = inet_ioctl,
997 .gettstamp = sock_gettstamp,
998 /* FIXME: work on inet_listen to rename it to sock_common_listen */
999 .listen = inet_dccp_listen,
1000 .shutdown = inet_shutdown,
1001 .setsockopt = sock_common_setsockopt,
1002 .getsockopt = sock_common_getsockopt,
1003 .sendmsg = inet_sendmsg,
1004 .recvmsg = sock_common_recvmsg,
1005 .mmap = sock_no_mmap,
1006 .sendpage = sock_no_sendpage,
1007 };
1008
1009 static struct inet_protosw dccp_v4_protosw = {
1010 .type = SOCK_DCCP,
1011 .protocol = IPPROTO_DCCP,
1012 .prot = &dccp_v4_prot,
1013 .ops = &inet_dccp_ops,
1014 .flags = INET_PROTOSW_ICSK,
1015 };
1016
dccp_v4_init_net(struct net * net)1017 static int __net_init dccp_v4_init_net(struct net *net)
1018 {
1019 struct dccp_v4_pernet *pn = net_generic(net, dccp_v4_pernet_id);
1020
1021 if (dccp_hashinfo.bhash == NULL)
1022 return -ESOCKTNOSUPPORT;
1023
1024 return inet_ctl_sock_create(&pn->v4_ctl_sk, PF_INET,
1025 SOCK_DCCP, IPPROTO_DCCP, net);
1026 }
1027
dccp_v4_exit_net(struct net * net)1028 static void __net_exit dccp_v4_exit_net(struct net *net)
1029 {
1030 struct dccp_v4_pernet *pn = net_generic(net, dccp_v4_pernet_id);
1031
1032 inet_ctl_sock_destroy(pn->v4_ctl_sk);
1033 }
1034
dccp_v4_exit_batch(struct list_head * net_exit_list)1035 static void __net_exit dccp_v4_exit_batch(struct list_head *net_exit_list)
1036 {
1037 inet_twsk_purge(&dccp_hashinfo, AF_INET);
1038 }
1039
1040 static struct pernet_operations dccp_v4_ops = {
1041 .init = dccp_v4_init_net,
1042 .exit = dccp_v4_exit_net,
1043 .exit_batch = dccp_v4_exit_batch,
1044 .id = &dccp_v4_pernet_id,
1045 .size = sizeof(struct dccp_v4_pernet),
1046 };
1047
dccp_v4_init(void)1048 static int __init dccp_v4_init(void)
1049 {
1050 int err = proto_register(&dccp_v4_prot, 1);
1051
1052 if (err)
1053 goto out;
1054
1055 inet_register_protosw(&dccp_v4_protosw);
1056
1057 err = register_pernet_subsys(&dccp_v4_ops);
1058 if (err)
1059 goto out_destroy_ctl_sock;
1060
1061 err = inet_add_protocol(&dccp_v4_protocol, IPPROTO_DCCP);
1062 if (err)
1063 goto out_proto_unregister;
1064
1065 out:
1066 return err;
1067 out_proto_unregister:
1068 unregister_pernet_subsys(&dccp_v4_ops);
1069 out_destroy_ctl_sock:
1070 inet_unregister_protosw(&dccp_v4_protosw);
1071 proto_unregister(&dccp_v4_prot);
1072 goto out;
1073 }
1074
dccp_v4_exit(void)1075 static void __exit dccp_v4_exit(void)
1076 {
1077 inet_del_protocol(&dccp_v4_protocol, IPPROTO_DCCP);
1078 unregister_pernet_subsys(&dccp_v4_ops);
1079 inet_unregister_protosw(&dccp_v4_protosw);
1080 proto_unregister(&dccp_v4_prot);
1081 }
1082
1083 module_init(dccp_v4_init);
1084 module_exit(dccp_v4_exit);
1085
1086 /*
1087 * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33)
1088 * values directly, Also cover the case where the protocol is not specified,
1089 * i.e. net-pf-PF_INET-proto-0-type-SOCK_DCCP
1090 */
1091 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 33, 6);
1092 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 0, 6);
1093 MODULE_LICENSE("GPL");
1094 MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@mandriva.com>");
1095 MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");
1096