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