Commit | Line | Data |
---|---|---|
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> | |
21 | #include <net/inet_hashtables.h> | |
22 | #include <net/sock.h> | |
6d6ee43e | 23 | #include <net/timewait_sock.h> |
7c657876 ACM |
24 | #include <net/tcp_states.h> |
25 | #include <net/xfrm.h> | |
26 | ||
ae31c339 | 27 | #include "ackvec.h" |
7c657876 ACM |
28 | #include "ccid.h" |
29 | #include "dccp.h" | |
30 | ||
31 | struct inet_hashinfo __cacheline_aligned dccp_hashinfo = { | |
32 | .lhash_lock = RW_LOCK_UNLOCKED, | |
33 | .lhash_users = ATOMIC_INIT(0), | |
7690af3f | 34 | .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait), |
7c657876 ACM |
35 | }; |
36 | ||
540722ff ACM |
37 | EXPORT_SYMBOL_GPL(dccp_hashinfo); |
38 | ||
7c657876 ACM |
39 | static int dccp_v4_get_port(struct sock *sk, const unsigned short snum) |
40 | { | |
971af18b ACM |
41 | return inet_csk_get_port(&dccp_hashinfo, sk, snum, |
42 | inet_csk_bind_conflict); | |
7c657876 ACM |
43 | } |
44 | ||
45 | static void dccp_v4_hash(struct sock *sk) | |
46 | { | |
47 | inet_hash(&dccp_hashinfo, sk); | |
48 | } | |
49 | ||
f21e68ca | 50 | void dccp_unhash(struct sock *sk) |
7c657876 ACM |
51 | { |
52 | inet_unhash(&dccp_hashinfo, sk); | |
53 | } | |
54 | ||
f21e68ca ACM |
55 | EXPORT_SYMBOL_GPL(dccp_unhash); |
56 | ||
7c657876 ACM |
57 | /* called with local bh disabled */ |
58 | static int __dccp_v4_check_established(struct sock *sk, const __u16 lport, | |
59 | struct inet_timewait_sock **twp) | |
60 | { | |
61 | struct inet_sock *inet = inet_sk(sk); | |
62 | const u32 daddr = inet->rcv_saddr; | |
63 | const u32 saddr = inet->daddr; | |
64 | const int dif = sk->sk_bound_dev_if; | |
65 | INET_ADDR_COOKIE(acookie, saddr, daddr) | |
66 | const __u32 ports = INET_COMBINED_PORTS(inet->dport, lport); | |
81c3d547 ED |
67 | unsigned int hash = inet_ehashfn(daddr, lport, saddr, inet->dport); |
68 | struct inet_ehash_bucket *head = inet_ehash_bucket(&dccp_hashinfo, hash); | |
7c657876 ACM |
69 | const struct sock *sk2; |
70 | const struct hlist_node *node; | |
71 | struct inet_timewait_sock *tw; | |
72 | ||
81c3d547 | 73 | prefetch(head->chain.first); |
7c657876 ACM |
74 | write_lock(&head->lock); |
75 | ||
76 | /* Check TIME-WAIT sockets first. */ | |
77 | sk_for_each(sk2, node, &(head + dccp_hashinfo.ehash_size)->chain) { | |
78 | tw = inet_twsk(sk2); | |
79 | ||
81c3d547 | 80 | if (INET_TW_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif)) |
7c657876 ACM |
81 | goto not_unique; |
82 | } | |
83 | tw = NULL; | |
84 | ||
85 | /* And established part... */ | |
86 | sk_for_each(sk2, node, &head->chain) { | |
81c3d547 | 87 | if (INET_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif)) |
7c657876 ACM |
88 | goto not_unique; |
89 | } | |
90 | ||
91 | /* Must record num and sport now. Otherwise we will see | |
92 | * in hash table socket with a funny identity. */ | |
93 | inet->num = lport; | |
94 | inet->sport = htons(lport); | |
81c3d547 | 95 | sk->sk_hash = hash; |
7c657876 ACM |
96 | BUG_TRAP(sk_unhashed(sk)); |
97 | __sk_add_node(sk, &head->chain); | |
98 | sock_prot_inc_use(sk->sk_prot); | |
99 | write_unlock(&head->lock); | |
100 | ||
101 | if (twp != NULL) { | |
102 | *twp = tw; | |
103 | NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED); | |
104 | } else if (tw != NULL) { | |
105 | /* Silly. Should hash-dance instead... */ | |
64cf1e5d | 106 | inet_twsk_deschedule(tw, &dccp_death_row); |
7c657876 ACM |
107 | NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED); |
108 | ||
109 | inet_twsk_put(tw); | |
110 | } | |
111 | ||
112 | return 0; | |
113 | ||
114 | not_unique: | |
115 | write_unlock(&head->lock); | |
116 | return -EADDRNOTAVAIL; | |
117 | } | |
118 | ||
119 | /* | |
120 | * Bind a port for a connect operation and hash it. | |
121 | */ | |
122 | static int dccp_v4_hash_connect(struct sock *sk) | |
123 | { | |
124 | const unsigned short snum = inet_sk(sk)->num; | |
125 | struct inet_bind_hashbucket *head; | |
126 | struct inet_bind_bucket *tb; | |
127 | int ret; | |
128 | ||
129 | if (snum == 0) { | |
7c657876 ACM |
130 | int low = sysctl_local_port_range[0]; |
131 | int high = sysctl_local_port_range[1]; | |
132 | int remaining = (high - low) + 1; | |
6df71634 | 133 | int rover = net_random() % (high - low) + low; |
7c657876 ACM |
134 | struct hlist_node *node; |
135 | struct inet_timewait_sock *tw = NULL; | |
136 | ||
137 | local_bh_disable(); | |
7c657876 | 138 | do { |
7690af3f ACM |
139 | head = &dccp_hashinfo.bhash[inet_bhashfn(rover, |
140 | dccp_hashinfo.bhash_size)]; | |
7c657876 ACM |
141 | spin_lock(&head->lock); |
142 | ||
143 | /* Does not bother with rcv_saddr checks, | |
144 | * because the established check is already | |
145 | * unique enough. | |
146 | */ | |
147 | inet_bind_bucket_for_each(tb, node, &head->chain) { | |
148 | if (tb->port == rover) { | |
149 | BUG_TRAP(!hlist_empty(&tb->owners)); | |
150 | if (tb->fastreuse >= 0) | |
151 | goto next_port; | |
152 | if (!__dccp_v4_check_established(sk, | |
153 | rover, | |
154 | &tw)) | |
155 | goto ok; | |
156 | goto next_port; | |
157 | } | |
158 | } | |
159 | ||
7690af3f ACM |
160 | tb = inet_bind_bucket_create(dccp_hashinfo.bind_bucket_cachep, |
161 | head, rover); | |
7c657876 ACM |
162 | if (tb == NULL) { |
163 | spin_unlock(&head->lock); | |
164 | break; | |
165 | } | |
166 | tb->fastreuse = -1; | |
167 | goto ok; | |
168 | ||
169 | next_port: | |
170 | spin_unlock(&head->lock); | |
6df71634 SH |
171 | if (++rover > high) |
172 | rover = low; | |
7c657876 | 173 | } while (--remaining > 0); |
7c657876 ACM |
174 | |
175 | local_bh_enable(); | |
176 | ||
177 | return -EADDRNOTAVAIL; | |
178 | ||
179 | ok: | |
180 | /* All locks still held and bhs disabled */ | |
7c657876 ACM |
181 | inet_bind_hash(sk, tb, rover); |
182 | if (sk_unhashed(sk)) { | |
183 | inet_sk(sk)->sport = htons(rover); | |
184 | __inet_hash(&dccp_hashinfo, sk, 0); | |
185 | } | |
186 | spin_unlock(&head->lock); | |
187 | ||
188 | if (tw != NULL) { | |
64cf1e5d | 189 | inet_twsk_deschedule(tw, &dccp_death_row); |
7c657876 ACM |
190 | inet_twsk_put(tw); |
191 | } | |
192 | ||
193 | ret = 0; | |
194 | goto out; | |
195 | } | |
196 | ||
7690af3f ACM |
197 | head = &dccp_hashinfo.bhash[inet_bhashfn(snum, |
198 | dccp_hashinfo.bhash_size)]; | |
7c657876 ACM |
199 | tb = inet_csk(sk)->icsk_bind_hash; |
200 | spin_lock_bh(&head->lock); | |
201 | if (sk_head(&tb->owners) == sk && sk->sk_bind_node.next == NULL) { | |
202 | __inet_hash(&dccp_hashinfo, sk, 0); | |
203 | spin_unlock_bh(&head->lock); | |
204 | return 0; | |
205 | } else { | |
206 | spin_unlock(&head->lock); | |
207 | /* No definite answer... Walk to established hash table */ | |
208 | ret = __dccp_v4_check_established(sk, snum, NULL); | |
209 | out: | |
210 | local_bh_enable(); | |
211 | return ret; | |
212 | } | |
213 | } | |
214 | ||
f21e68ca | 215 | int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) |
7c657876 ACM |
216 | { |
217 | struct inet_sock *inet = inet_sk(sk); | |
218 | struct dccp_sock *dp = dccp_sk(sk); | |
219 | const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr; | |
220 | struct rtable *rt; | |
221 | u32 daddr, nexthop; | |
222 | int tmp; | |
223 | int err; | |
224 | ||
225 | dp->dccps_role = DCCP_ROLE_CLIENT; | |
226 | ||
67e6b629 ACM |
227 | if (dccp_service_not_initialized(sk)) |
228 | return -EPROTO; | |
229 | ||
7c657876 ACM |
230 | if (addr_len < sizeof(struct sockaddr_in)) |
231 | return -EINVAL; | |
232 | ||
233 | if (usin->sin_family != AF_INET) | |
234 | return -EAFNOSUPPORT; | |
235 | ||
236 | nexthop = daddr = usin->sin_addr.s_addr; | |
237 | if (inet->opt != NULL && inet->opt->srr) { | |
238 | if (daddr == 0) | |
239 | return -EINVAL; | |
240 | nexthop = inet->opt->faddr; | |
241 | } | |
242 | ||
243 | tmp = ip_route_connect(&rt, nexthop, inet->saddr, | |
244 | RT_CONN_FLAGS(sk), sk->sk_bound_dev_if, | |
245 | IPPROTO_DCCP, | |
246 | inet->sport, usin->sin_port, sk); | |
247 | if (tmp < 0) | |
248 | return tmp; | |
249 | ||
250 | if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) { | |
251 | ip_rt_put(rt); | |
252 | return -ENETUNREACH; | |
253 | } | |
254 | ||
255 | if (inet->opt == NULL || !inet->opt->srr) | |
256 | daddr = rt->rt_dst; | |
257 | ||
258 | if (inet->saddr == 0) | |
259 | inet->saddr = rt->rt_src; | |
260 | inet->rcv_saddr = inet->saddr; | |
261 | ||
262 | inet->dport = usin->sin_port; | |
263 | inet->daddr = daddr; | |
264 | ||
265 | dp->dccps_ext_header_len = 0; | |
266 | if (inet->opt != NULL) | |
267 | dp->dccps_ext_header_len = inet->opt->optlen; | |
268 | /* | |
269 | * Socket identity is still unknown (sport may be zero). | |
270 | * However we set state to DCCP_REQUESTING and not releasing socket | |
271 | * lock select source port, enter ourselves into the hash tables and | |
272 | * complete initialization after this. | |
273 | */ | |
274 | dccp_set_state(sk, DCCP_REQUESTING); | |
275 | err = dccp_v4_hash_connect(sk); | |
276 | if (err != 0) | |
277 | goto failure; | |
278 | ||
279 | err = ip_route_newports(&rt, inet->sport, inet->dport, sk); | |
280 | if (err != 0) | |
281 | goto failure; | |
282 | ||
283 | /* OK, now commit destination to socket. */ | |
284 | sk_setup_caps(sk, &rt->u.dst); | |
285 | ||
286 | dp->dccps_gar = | |
287 | dp->dccps_iss = secure_dccp_sequence_number(inet->saddr, | |
288 | inet->daddr, | |
289 | inet->sport, | |
290 | usin->sin_port); | |
291 | dccp_update_gss(sk, dp->dccps_iss); | |
292 | ||
293 | inet->id = dp->dccps_iss ^ jiffies; | |
294 | ||
295 | err = dccp_connect(sk); | |
296 | rt = NULL; | |
297 | if (err != 0) | |
298 | goto failure; | |
299 | out: | |
300 | return err; | |
301 | failure: | |
7690af3f ACM |
302 | /* |
303 | * This unhashes the socket and releases the local port, if necessary. | |
304 | */ | |
7c657876 ACM |
305 | dccp_set_state(sk, DCCP_CLOSED); |
306 | ip_rt_put(rt); | |
307 | sk->sk_route_caps = 0; | |
308 | inet->dport = 0; | |
309 | goto out; | |
310 | } | |
311 | ||
f21e68ca ACM |
312 | EXPORT_SYMBOL_GPL(dccp_v4_connect); |
313 | ||
7c657876 ACM |
314 | /* |
315 | * This routine does path mtu discovery as defined in RFC1191. | |
316 | */ | |
317 | static inline void dccp_do_pmtu_discovery(struct sock *sk, | |
318 | const struct iphdr *iph, | |
319 | u32 mtu) | |
320 | { | |
321 | struct dst_entry *dst; | |
322 | const struct inet_sock *inet = inet_sk(sk); | |
323 | const struct dccp_sock *dp = dccp_sk(sk); | |
324 | ||
325 | /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs | |
326 | * send out by Linux are always < 576bytes so they should go through | |
327 | * unfragmented). | |
328 | */ | |
329 | if (sk->sk_state == DCCP_LISTEN) | |
330 | return; | |
331 | ||
332 | /* We don't check in the destentry if pmtu discovery is forbidden | |
333 | * on this route. We just assume that no packet_to_big packets | |
334 | * are send back when pmtu discovery is not active. | |
335 | * There is a small race when the user changes this flag in the | |
336 | * route, but I think that's acceptable. | |
337 | */ | |
338 | if ((dst = __sk_dst_check(sk, 0)) == NULL) | |
339 | return; | |
340 | ||
341 | dst->ops->update_pmtu(dst, mtu); | |
342 | ||
343 | /* Something is about to be wrong... Remember soft error | |
344 | * for the case, if this connection will not able to recover. | |
345 | */ | |
346 | if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst)) | |
347 | sk->sk_err_soft = EMSGSIZE; | |
348 | ||
349 | mtu = dst_mtu(dst); | |
350 | ||
351 | if (inet->pmtudisc != IP_PMTUDISC_DONT && | |
352 | dp->dccps_pmtu_cookie > mtu) { | |
353 | dccp_sync_mss(sk, mtu); | |
354 | ||
355 | /* | |
356 | * From: draft-ietf-dccp-spec-11.txt | |
357 | * | |
7690af3f ACM |
358 | * DCCP-Sync packets are the best choice for upward |
359 | * probing, since DCCP-Sync probes do not risk application | |
360 | * data loss. | |
7c657876 | 361 | */ |
e92ae93a | 362 | dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC); |
7c657876 ACM |
363 | } /* else let the usual retransmit timer handle it */ |
364 | } | |
365 | ||
366 | static void dccp_v4_ctl_send_ack(struct sk_buff *rxskb) | |
367 | { | |
368 | int err; | |
369 | struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh; | |
370 | const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) + | |
371 | sizeof(struct dccp_hdr_ext) + | |
372 | sizeof(struct dccp_hdr_ack_bits); | |
373 | struct sk_buff *skb; | |
374 | ||
375 | if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL) | |
376 | return; | |
377 | ||
378 | skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC); | |
379 | if (skb == NULL) | |
380 | return; | |
381 | ||
382 | /* Reserve space for headers. */ | |
383 | skb_reserve(skb, MAX_DCCP_HEADER); | |
384 | ||
385 | skb->dst = dst_clone(rxskb->dst); | |
386 | ||
387 | skb->h.raw = skb_push(skb, dccp_hdr_ack_len); | |
388 | dh = dccp_hdr(skb); | |
389 | memset(dh, 0, dccp_hdr_ack_len); | |
390 | ||
391 | /* Build DCCP header and checksum it. */ | |
392 | dh->dccph_type = DCCP_PKT_ACK; | |
393 | dh->dccph_sport = rxdh->dccph_dport; | |
394 | dh->dccph_dport = rxdh->dccph_sport; | |
395 | dh->dccph_doff = dccp_hdr_ack_len / 4; | |
396 | dh->dccph_x = 1; | |
397 | ||
398 | dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq); | |
7690af3f ACM |
399 | dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), |
400 | DCCP_SKB_CB(rxskb)->dccpd_seq); | |
7c657876 ACM |
401 | |
402 | bh_lock_sock(dccp_ctl_socket->sk); | |
403 | err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk, | |
7690af3f ACM |
404 | rxskb->nh.iph->daddr, |
405 | rxskb->nh.iph->saddr, NULL); | |
7c657876 ACM |
406 | bh_unlock_sock(dccp_ctl_socket->sk); |
407 | ||
408 | if (err == NET_XMIT_CN || err == 0) { | |
409 | DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS); | |
410 | DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS); | |
411 | } | |
412 | } | |
413 | ||
7690af3f ACM |
414 | static void dccp_v4_reqsk_send_ack(struct sk_buff *skb, |
415 | struct request_sock *req) | |
7c657876 ACM |
416 | { |
417 | dccp_v4_ctl_send_ack(skb); | |
418 | } | |
419 | ||
420 | static int dccp_v4_send_response(struct sock *sk, struct request_sock *req, | |
421 | struct dst_entry *dst) | |
422 | { | |
423 | int err = -1; | |
424 | struct sk_buff *skb; | |
425 | ||
426 | /* First, grab a route. */ | |
427 | ||
428 | if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL) | |
429 | goto out; | |
430 | ||
431 | skb = dccp_make_response(sk, dst, req); | |
432 | if (skb != NULL) { | |
433 | const struct inet_request_sock *ireq = inet_rsk(req); | |
434 | ||
49c5bfaf | 435 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
7c657876 ACM |
436 | err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr, |
437 | ireq->rmt_addr, | |
438 | ireq->opt); | |
439 | if (err == NET_XMIT_CN) | |
440 | err = 0; | |
441 | } | |
442 | ||
443 | out: | |
444 | dst_release(dst); | |
445 | return err; | |
446 | } | |
447 | ||
448 | /* | |
449 | * This routine is called by the ICMP module when it gets some sort of error | |
450 | * condition. If err < 0 then the socket should be closed and the error | |
451 | * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code. | |
452 | * After adjustment header points to the first 8 bytes of the tcp header. We | |
453 | * need to find the appropriate port. | |
454 | * | |
455 | * The locking strategy used here is very "optimistic". When someone else | |
456 | * accesses the socket the ICMP is just dropped and for some paths there is no | |
457 | * check at all. A more general error queue to queue errors for later handling | |
458 | * is probably better. | |
459 | */ | |
460 | void dccp_v4_err(struct sk_buff *skb, u32 info) | |
461 | { | |
462 | const struct iphdr *iph = (struct iphdr *)skb->data; | |
7690af3f ACM |
463 | const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data + |
464 | (iph->ihl << 2)); | |
7c657876 ACM |
465 | struct dccp_sock *dp; |
466 | struct inet_sock *inet; | |
467 | const int type = skb->h.icmph->type; | |
468 | const int code = skb->h.icmph->code; | |
469 | struct sock *sk; | |
470 | __u64 seq; | |
471 | int err; | |
472 | ||
473 | if (skb->len < (iph->ihl << 2) + 8) { | |
474 | ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); | |
475 | return; | |
476 | } | |
477 | ||
478 | sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport, | |
479 | iph->saddr, dh->dccph_sport, inet_iif(skb)); | |
480 | if (sk == NULL) { | |
481 | ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); | |
482 | return; | |
483 | } | |
484 | ||
485 | if (sk->sk_state == DCCP_TIME_WAIT) { | |
486 | inet_twsk_put((struct inet_timewait_sock *)sk); | |
487 | return; | |
488 | } | |
489 | ||
490 | bh_lock_sock(sk); | |
491 | /* If too many ICMPs get dropped on busy | |
492 | * servers this needs to be solved differently. | |
493 | */ | |
494 | if (sock_owned_by_user(sk)) | |
495 | NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS); | |
496 | ||
497 | if (sk->sk_state == DCCP_CLOSED) | |
498 | goto out; | |
499 | ||
500 | dp = dccp_sk(sk); | |
501 | seq = dccp_hdr_seq(skb); | |
502 | if (sk->sk_state != DCCP_LISTEN && | |
503 | !between48(seq, dp->dccps_swl, dp->dccps_swh)) { | |
504 | NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS); | |
505 | goto out; | |
506 | } | |
507 | ||
508 | switch (type) { | |
509 | case ICMP_SOURCE_QUENCH: | |
510 | /* Just silently ignore these. */ | |
511 | goto out; | |
512 | case ICMP_PARAMETERPROB: | |
513 | err = EPROTO; | |
514 | break; | |
515 | case ICMP_DEST_UNREACH: | |
516 | if (code > NR_ICMP_UNREACH) | |
517 | goto out; | |
518 | ||
519 | if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */ | |
520 | if (!sock_owned_by_user(sk)) | |
521 | dccp_do_pmtu_discovery(sk, iph, info); | |
522 | goto out; | |
523 | } | |
524 | ||
525 | err = icmp_err_convert[code].errno; | |
526 | break; | |
527 | case ICMP_TIME_EXCEEDED: | |
528 | err = EHOSTUNREACH; | |
529 | break; | |
530 | default: | |
531 | goto out; | |
532 | } | |
533 | ||
534 | switch (sk->sk_state) { | |
535 | struct request_sock *req , **prev; | |
536 | case DCCP_LISTEN: | |
537 | if (sock_owned_by_user(sk)) | |
538 | goto out; | |
539 | req = inet_csk_search_req(sk, &prev, dh->dccph_dport, | |
540 | iph->daddr, iph->saddr); | |
541 | if (!req) | |
542 | goto out; | |
543 | ||
544 | /* | |
545 | * ICMPs are not backlogged, hence we cannot get an established | |
546 | * socket here. | |
547 | */ | |
548 | BUG_TRAP(!req->sk); | |
549 | ||
550 | if (seq != dccp_rsk(req)->dreq_iss) { | |
551 | NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS); | |
552 | goto out; | |
553 | } | |
554 | /* | |
555 | * Still in RESPOND, just remove it silently. | |
556 | * There is no good way to pass the error to the newly | |
557 | * created socket, and POSIX does not want network | |
558 | * errors returned from accept(). | |
559 | */ | |
560 | inet_csk_reqsk_queue_drop(sk, req, prev); | |
561 | goto out; | |
562 | ||
563 | case DCCP_REQUESTING: | |
564 | case DCCP_RESPOND: | |
565 | if (!sock_owned_by_user(sk)) { | |
566 | DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS); | |
567 | sk->sk_err = err; | |
568 | ||
569 | sk->sk_error_report(sk); | |
570 | ||
571 | dccp_done(sk); | |
572 | } else | |
573 | sk->sk_err_soft = err; | |
574 | goto out; | |
575 | } | |
576 | ||
577 | /* If we've already connected we will keep trying | |
578 | * until we time out, or the user gives up. | |
579 | * | |
580 | * rfc1122 4.2.3.9 allows to consider as hard errors | |
581 | * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too, | |
582 | * but it is obsoleted by pmtu discovery). | |
583 | * | |
584 | * Note, that in modern internet, where routing is unreliable | |
585 | * and in each dark corner broken firewalls sit, sending random | |
586 | * errors ordered by their masters even this two messages finally lose | |
587 | * their original sense (even Linux sends invalid PORT_UNREACHs) | |
588 | * | |
589 | * Now we are in compliance with RFCs. | |
590 | * --ANK (980905) | |
591 | */ | |
592 | ||
593 | inet = inet_sk(sk); | |
594 | if (!sock_owned_by_user(sk) && inet->recverr) { | |
595 | sk->sk_err = err; | |
596 | sk->sk_error_report(sk); | |
597 | } else /* Only an error on timeout */ | |
598 | sk->sk_err_soft = err; | |
599 | out: | |
600 | bh_unlock_sock(sk); | |
601 | sock_put(sk); | |
602 | } | |
603 | ||
57cca05a | 604 | /* This routine computes an IPv4 DCCP checksum. */ |
f21e68ca | 605 | void dccp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb) |
57cca05a ACM |
606 | { |
607 | const struct inet_sock *inet = inet_sk(sk); | |
608 | struct dccp_hdr *dh = dccp_hdr(skb); | |
609 | ||
610 | dh->dccph_checksum = dccp_v4_checksum(skb, inet->saddr, inet->daddr); | |
611 | } | |
612 | ||
f21e68ca ACM |
613 | EXPORT_SYMBOL_GPL(dccp_v4_send_check); |
614 | ||
7c657876 ACM |
615 | int dccp_v4_send_reset(struct sock *sk, enum dccp_reset_codes code) |
616 | { | |
617 | struct sk_buff *skb; | |
618 | /* | |
619 | * FIXME: what if rebuild_header fails? | |
620 | * Should we be doing a rebuild_header here? | |
621 | */ | |
622 | int err = inet_sk_rebuild_header(sk); | |
623 | ||
624 | if (err != 0) | |
625 | return err; | |
626 | ||
627 | skb = dccp_make_reset(sk, sk->sk_dst_cache, code); | |
628 | if (skb != NULL) { | |
7c657876 ACM |
629 | const struct inet_sock *inet = inet_sk(sk); |
630 | ||
49c5bfaf | 631 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
7c657876 ACM |
632 | err = ip_build_and_send_pkt(skb, sk, |
633 | inet->saddr, inet->daddr, NULL); | |
634 | if (err == NET_XMIT_CN) | |
635 | err = 0; | |
7c657876 ACM |
636 | } |
637 | ||
638 | return err; | |
639 | } | |
640 | ||
641 | static inline u64 dccp_v4_init_sequence(const struct sock *sk, | |
642 | const struct sk_buff *skb) | |
643 | { | |
644 | return secure_dccp_sequence_number(skb->nh.iph->daddr, | |
645 | skb->nh.iph->saddr, | |
646 | dccp_hdr(skb)->dccph_dport, | |
647 | dccp_hdr(skb)->dccph_sport); | |
648 | } | |
649 | ||
650 | int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb) | |
651 | { | |
652 | struct inet_request_sock *ireq; | |
653 | struct dccp_sock dp; | |
654 | struct request_sock *req; | |
655 | struct dccp_request_sock *dreq; | |
656 | const __u32 saddr = skb->nh.iph->saddr; | |
657 | const __u32 daddr = skb->nh.iph->daddr; | |
67e6b629 | 658 | const __u32 service = dccp_hdr_request(skb)->dccph_req_service; |
0c10c5d9 ACM |
659 | struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb); |
660 | __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY; | |
7c657876 ACM |
661 | |
662 | /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */ | |
663 | if (((struct rtable *)skb->dst)->rt_flags & | |
0c10c5d9 ACM |
664 | (RTCF_BROADCAST | RTCF_MULTICAST)) { |
665 | reset_code = DCCP_RESET_CODE_NO_CONNECTION; | |
7c657876 | 666 | goto drop; |
0c10c5d9 | 667 | } |
7c657876 | 668 | |
67e6b629 ACM |
669 | if (dccp_bad_service_code(sk, service)) { |
670 | reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE; | |
671 | goto drop; | |
672 | } | |
7c657876 ACM |
673 | /* |
674 | * TW buckets are converted to open requests without | |
675 | * limitations, they conserve resources and peer is | |
676 | * evidently real one. | |
677 | */ | |
678 | if (inet_csk_reqsk_queue_is_full(sk)) | |
679 | goto drop; | |
680 | ||
681 | /* | |
682 | * Accept backlog is full. If we have already queued enough | |
683 | * of warm entries in syn queue, drop request. It is better than | |
684 | * clogging syn queue with openreqs with exponentially increasing | |
685 | * timeout. | |
686 | */ | |
687 | if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1) | |
688 | goto drop; | |
689 | ||
690 | req = reqsk_alloc(sk->sk_prot->rsk_prot); | |
691 | if (req == NULL) | |
692 | goto drop; | |
693 | ||
694 | /* FIXME: process options */ | |
695 | ||
696 | dccp_openreq_init(req, &dp, skb); | |
697 | ||
698 | ireq = inet_rsk(req); | |
699 | ireq->loc_addr = daddr; | |
700 | ireq->rmt_addr = saddr; | |
7690af3f ACM |
701 | req->rcv_wnd = 100; /* Fake, option parsing will get the |
702 | right value */ | |
7c657876 ACM |
703 | ireq->opt = NULL; |
704 | ||
705 | /* | |
706 | * Step 3: Process LISTEN state | |
707 | * | |
708 | * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie | |
709 | * | |
710 | * In fact we defer setting S.GSR, S.SWL, S.SWH to | |
711 | * dccp_create_openreq_child. | |
712 | */ | |
713 | dreq = dccp_rsk(req); | |
67e6b629 ACM |
714 | dreq->dreq_isr = dcb->dccpd_seq; |
715 | dreq->dreq_iss = dccp_v4_init_sequence(sk, skb); | |
716 | dreq->dreq_service = service; | |
7c657876 | 717 | |
f21e68ca | 718 | if (dccp_v4_send_response(sk, req, NULL)) |
7c657876 ACM |
719 | goto drop_and_free; |
720 | ||
721 | inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT); | |
722 | return 0; | |
723 | ||
724 | drop_and_free: | |
fc44b980 | 725 | reqsk_free(req); |
7c657876 ACM |
726 | drop: |
727 | DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS); | |
0c10c5d9 | 728 | dcb->dccpd_reset_code = reset_code; |
7c657876 ACM |
729 | return -1; |
730 | } | |
731 | ||
f21e68ca ACM |
732 | EXPORT_SYMBOL_GPL(dccp_v4_conn_request); |
733 | ||
7c657876 ACM |
734 | /* |
735 | * The three way handshake has completed - we got a valid ACK or DATAACK - | |
736 | * now create the new socket. | |
737 | * | |
738 | * This is the equivalent of TCP's tcp_v4_syn_recv_sock | |
739 | */ | |
740 | struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb, | |
741 | struct request_sock *req, | |
742 | struct dst_entry *dst) | |
743 | { | |
744 | struct inet_request_sock *ireq; | |
745 | struct inet_sock *newinet; | |
746 | struct dccp_sock *newdp; | |
747 | struct sock *newsk; | |
748 | ||
749 | if (sk_acceptq_is_full(sk)) | |
750 | goto exit_overflow; | |
751 | ||
752 | if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL) | |
753 | goto exit; | |
754 | ||
755 | newsk = dccp_create_openreq_child(sk, req, skb); | |
756 | if (newsk == NULL) | |
757 | goto exit; | |
758 | ||
759 | sk_setup_caps(newsk, dst); | |
760 | ||
761 | newdp = dccp_sk(newsk); | |
762 | newinet = inet_sk(newsk); | |
763 | ireq = inet_rsk(req); | |
764 | newinet->daddr = ireq->rmt_addr; | |
765 | newinet->rcv_saddr = ireq->loc_addr; | |
766 | newinet->saddr = ireq->loc_addr; | |
767 | newinet->opt = ireq->opt; | |
768 | ireq->opt = NULL; | |
769 | newinet->mc_index = inet_iif(skb); | |
770 | newinet->mc_ttl = skb->nh.iph->ttl; | |
771 | newinet->id = jiffies; | |
772 | ||
773 | dccp_sync_mss(newsk, dst_mtu(dst)); | |
774 | ||
775 | __inet_hash(&dccp_hashinfo, newsk, 0); | |
776 | __inet_inherit_port(&dccp_hashinfo, sk, newsk); | |
777 | ||
778 | return newsk; | |
779 | ||
780 | exit_overflow: | |
781 | NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS); | |
782 | exit: | |
783 | NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS); | |
784 | dst_release(dst); | |
785 | return NULL; | |
786 | } | |
787 | ||
f21e68ca ACM |
788 | EXPORT_SYMBOL_GPL(dccp_v4_request_recv_sock); |
789 | ||
7c657876 ACM |
790 | static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb) |
791 | { | |
792 | const struct dccp_hdr *dh = dccp_hdr(skb); | |
793 | const struct iphdr *iph = skb->nh.iph; | |
794 | struct sock *nsk; | |
795 | struct request_sock **prev; | |
796 | /* Find possible connection requests. */ | |
797 | struct request_sock *req = inet_csk_search_req(sk, &prev, | |
798 | dh->dccph_sport, | |
799 | iph->saddr, iph->daddr); | |
800 | if (req != NULL) | |
801 | return dccp_check_req(sk, skb, req, prev); | |
802 | ||
803 | nsk = __inet_lookup_established(&dccp_hashinfo, | |
804 | iph->saddr, dh->dccph_sport, | |
805 | iph->daddr, ntohs(dh->dccph_dport), | |
806 | inet_iif(skb)); | |
807 | if (nsk != NULL) { | |
808 | if (nsk->sk_state != DCCP_TIME_WAIT) { | |
809 | bh_lock_sock(nsk); | |
810 | return nsk; | |
811 | } | |
812 | inet_twsk_put((struct inet_timewait_sock *)nsk); | |
813 | return NULL; | |
814 | } | |
815 | ||
816 | return sk; | |
817 | } | |
818 | ||
7690af3f ACM |
819 | int dccp_v4_checksum(const struct sk_buff *skb, const u32 saddr, |
820 | const u32 daddr) | |
7c657876 | 821 | { |
95b81ef7 | 822 | const struct dccp_hdr* dh = dccp_hdr(skb); |
7c657876 ACM |
823 | int checksum_len; |
824 | u32 tmp; | |
825 | ||
826 | if (dh->dccph_cscov == 0) | |
827 | checksum_len = skb->len; | |
828 | else { | |
829 | checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32); | |
7690af3f ACM |
830 | checksum_len = checksum_len < skb->len ? checksum_len : |
831 | skb->len; | |
7c657876 ACM |
832 | } |
833 | ||
834 | tmp = csum_partial((unsigned char *)dh, checksum_len, 0); | |
7690af3f ACM |
835 | return csum_tcpudp_magic(saddr, daddr, checksum_len, |
836 | IPPROTO_DCCP, tmp); | |
7c657876 ACM |
837 | } |
838 | ||
95b81ef7 YN |
839 | static int dccp_v4_verify_checksum(struct sk_buff *skb, |
840 | const u32 saddr, const u32 daddr) | |
7c657876 | 841 | { |
95b81ef7 YN |
842 | struct dccp_hdr *dh = dccp_hdr(skb); |
843 | int checksum_len; | |
844 | u32 tmp; | |
7c657876 | 845 | |
95b81ef7 YN |
846 | if (dh->dccph_cscov == 0) |
847 | checksum_len = skb->len; | |
848 | else { | |
849 | checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32); | |
7690af3f ACM |
850 | checksum_len = checksum_len < skb->len ? checksum_len : |
851 | skb->len; | |
95b81ef7 YN |
852 | } |
853 | tmp = csum_partial((unsigned char *)dh, checksum_len, 0); | |
7690af3f ACM |
854 | return csum_tcpudp_magic(saddr, daddr, checksum_len, |
855 | IPPROTO_DCCP, tmp) == 0 ? 0 : -1; | |
7c657876 ACM |
856 | } |
857 | ||
858 | static struct dst_entry* dccp_v4_route_skb(struct sock *sk, | |
859 | struct sk_buff *skb) | |
860 | { | |
861 | struct rtable *rt; | |
862 | struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif, | |
863 | .nl_u = { .ip4_u = | |
864 | { .daddr = skb->nh.iph->saddr, | |
865 | .saddr = skb->nh.iph->daddr, | |
866 | .tos = RT_CONN_FLAGS(sk) } }, | |
867 | .proto = sk->sk_protocol, | |
868 | .uli_u = { .ports = | |
869 | { .sport = dccp_hdr(skb)->dccph_dport, | |
7690af3f ACM |
870 | .dport = dccp_hdr(skb)->dccph_sport } |
871 | } | |
872 | }; | |
7c657876 ACM |
873 | |
874 | if (ip_route_output_flow(&rt, &fl, sk, 0)) { | |
875 | IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES); | |
876 | return NULL; | |
877 | } | |
878 | ||
879 | return &rt->u.dst; | |
880 | } | |
881 | ||
a1d3a355 | 882 | static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb) |
7c657876 ACM |
883 | { |
884 | int err; | |
885 | struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh; | |
886 | const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) + | |
887 | sizeof(struct dccp_hdr_ext) + | |
888 | sizeof(struct dccp_hdr_reset); | |
889 | struct sk_buff *skb; | |
890 | struct dst_entry *dst; | |
2807d4ff | 891 | u64 seqno; |
7c657876 ACM |
892 | |
893 | /* Never send a reset in response to a reset. */ | |
894 | if (rxdh->dccph_type == DCCP_PKT_RESET) | |
895 | return; | |
896 | ||
897 | if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL) | |
898 | return; | |
899 | ||
900 | dst = dccp_v4_route_skb(dccp_ctl_socket->sk, rxskb); | |
901 | if (dst == NULL) | |
902 | return; | |
903 | ||
904 | skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC); | |
905 | if (skb == NULL) | |
906 | goto out; | |
907 | ||
908 | /* Reserve space for headers. */ | |
909 | skb_reserve(skb, MAX_DCCP_HEADER); | |
910 | skb->dst = dst_clone(dst); | |
911 | ||
912 | skb->h.raw = skb_push(skb, dccp_hdr_reset_len); | |
913 | dh = dccp_hdr(skb); | |
914 | memset(dh, 0, dccp_hdr_reset_len); | |
915 | ||
916 | /* Build DCCP header and checksum it. */ | |
917 | dh->dccph_type = DCCP_PKT_RESET; | |
918 | dh->dccph_sport = rxdh->dccph_dport; | |
919 | dh->dccph_dport = rxdh->dccph_sport; | |
920 | dh->dccph_doff = dccp_hdr_reset_len / 4; | |
921 | dh->dccph_x = 1; | |
7690af3f ACM |
922 | dccp_hdr_reset(skb)->dccph_reset_code = |
923 | DCCP_SKB_CB(rxskb)->dccpd_reset_code; | |
7c657876 | 924 | |
2807d4ff ACM |
925 | /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */ |
926 | seqno = 0; | |
927 | if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ) | |
928 | dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1); | |
929 | ||
930 | dccp_hdr_set_seq(dh, seqno); | |
7690af3f ACM |
931 | dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), |
932 | DCCP_SKB_CB(rxskb)->dccpd_seq); | |
7c657876 | 933 | |
95b81ef7 YN |
934 | dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr, |
935 | rxskb->nh.iph->daddr); | |
7c657876 ACM |
936 | |
937 | bh_lock_sock(dccp_ctl_socket->sk); | |
938 | err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk, | |
7690af3f ACM |
939 | rxskb->nh.iph->daddr, |
940 | rxskb->nh.iph->saddr, NULL); | |
7c657876 ACM |
941 | bh_unlock_sock(dccp_ctl_socket->sk); |
942 | ||
943 | if (err == NET_XMIT_CN || err == 0) { | |
944 | DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS); | |
945 | DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS); | |
946 | } | |
947 | out: | |
948 | dst_release(dst); | |
949 | } | |
950 | ||
951 | int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb) | |
952 | { | |
953 | struct dccp_hdr *dh = dccp_hdr(skb); | |
954 | ||
955 | if (sk->sk_state == DCCP_OPEN) { /* Fast path */ | |
956 | if (dccp_rcv_established(sk, skb, dh, skb->len)) | |
957 | goto reset; | |
958 | return 0; | |
959 | } | |
960 | ||
961 | /* | |
962 | * Step 3: Process LISTEN state | |
963 | * If S.state == LISTEN, | |
7690af3f ACM |
964 | * If P.type == Request or P contains a valid Init Cookie |
965 | * option, | |
7c657876 ACM |
966 | * * Must scan the packet's options to check for an Init |
967 | * Cookie. Only the Init Cookie is processed here, | |
968 | * however; other options are processed in Step 8. This | |
969 | * scan need only be performed if the endpoint uses Init | |
970 | * Cookies * | |
971 | * * Generate a new socket and switch to that socket * | |
972 | * Set S := new socket for this port pair | |
973 | * S.state = RESPOND | |
974 | * Choose S.ISS (initial seqno) or set from Init Cookie | |
975 | * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie | |
976 | * Continue with S.state == RESPOND | |
977 | * * A Response packet will be generated in Step 11 * | |
978 | * Otherwise, | |
979 | * Generate Reset(No Connection) unless P.type == Reset | |
980 | * Drop packet and return | |
981 | * | |
7690af3f ACM |
982 | * NOTE: the check for the packet types is done in |
983 | * dccp_rcv_state_process | |
7c657876 ACM |
984 | */ |
985 | if (sk->sk_state == DCCP_LISTEN) { | |
986 | struct sock *nsk = dccp_v4_hnd_req(sk, skb); | |
987 | ||
988 | if (nsk == NULL) | |
989 | goto discard; | |
990 | ||
991 | if (nsk != sk) { | |
992 | if (dccp_child_process(sk, nsk, skb)) | |
993 | goto reset; | |
994 | return 0; | |
995 | } | |
996 | } | |
997 | ||
998 | if (dccp_rcv_state_process(sk, skb, dh, skb->len)) | |
999 | goto reset; | |
1000 | return 0; | |
1001 | ||
1002 | reset: | |
7c657876 ACM |
1003 | dccp_v4_ctl_send_reset(skb); |
1004 | discard: | |
1005 | kfree_skb(skb); | |
1006 | return 0; | |
1007 | } | |
1008 | ||
f21e68ca ACM |
1009 | EXPORT_SYMBOL_GPL(dccp_v4_do_rcv); |
1010 | ||
1011 | int dccp_invalid_packet(struct sk_buff *skb) | |
7c657876 ACM |
1012 | { |
1013 | const struct dccp_hdr *dh; | |
1014 | ||
1015 | if (skb->pkt_type != PACKET_HOST) | |
1016 | return 1; | |
1017 | ||
1018 | if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) { | |
c59eab46 | 1019 | LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n"); |
7c657876 ACM |
1020 | return 1; |
1021 | } | |
1022 | ||
1023 | dh = dccp_hdr(skb); | |
1024 | ||
1025 | /* If the packet type is not understood, drop packet and return */ | |
1026 | if (dh->dccph_type >= DCCP_PKT_INVALID) { | |
c59eab46 | 1027 | LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n"); |
7c657876 ACM |
1028 | return 1; |
1029 | } | |
1030 | ||
1031 | /* | |
1032 | * If P.Data Offset is too small for packet type, or too large for | |
1033 | * packet, drop packet and return | |
1034 | */ | |
1035 | if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) { | |
c59eab46 ACM |
1036 | LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) " |
1037 | "too small 1\n", | |
1038 | dh->dccph_doff); | |
7c657876 ACM |
1039 | return 1; |
1040 | } | |
1041 | ||
1042 | if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) { | |
c59eab46 ACM |
1043 | LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) " |
1044 | "too small 2\n", | |
1045 | dh->dccph_doff); | |
7c657876 ACM |
1046 | return 1; |
1047 | } | |
1048 | ||
1049 | dh = dccp_hdr(skb); | |
1050 | ||
1051 | /* | |
1052 | * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet | |
1053 | * has short sequence numbers), drop packet and return | |
1054 | */ | |
1055 | if (dh->dccph_x == 0 && | |
1056 | dh->dccph_type != DCCP_PKT_DATA && | |
1057 | dh->dccph_type != DCCP_PKT_ACK && | |
1058 | dh->dccph_type != DCCP_PKT_DATAACK) { | |
c59eab46 ACM |
1059 | LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack " |
1060 | "nor DataAck and P.X == 0\n", | |
1061 | dccp_packet_name(dh->dccph_type)); | |
7c657876 ACM |
1062 | return 1; |
1063 | } | |
1064 | ||
7c657876 ACM |
1065 | return 0; |
1066 | } | |
1067 | ||
f21e68ca ACM |
1068 | EXPORT_SYMBOL_GPL(dccp_invalid_packet); |
1069 | ||
7c657876 ACM |
1070 | /* this is called when real data arrives */ |
1071 | int dccp_v4_rcv(struct sk_buff *skb) | |
1072 | { | |
1073 | const struct dccp_hdr *dh; | |
1074 | struct sock *sk; | |
1075 | int rc; | |
1076 | ||
1077 | /* Step 1: Check header basics: */ | |
1078 | ||
1079 | if (dccp_invalid_packet(skb)) | |
1080 | goto discard_it; | |
1081 | ||
f21e68ca ACM |
1082 | /* If the header checksum is incorrect, drop packet and return */ |
1083 | if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr, | |
1084 | skb->nh.iph->daddr) < 0) { | |
1085 | LIMIT_NETDEBUG(KERN_WARNING "%s: incorrect header checksum\n", | |
1086 | __FUNCTION__); | |
1087 | goto discard_it; | |
1088 | } | |
1089 | ||
7c657876 | 1090 | dh = dccp_hdr(skb); |
7c657876 | 1091 | |
7c657876 ACM |
1092 | DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb); |
1093 | DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type; | |
1094 | ||
1095 | dccp_pr_debug("%8.8s " | |
1096 | "src=%u.%u.%u.%u@%-5d " | |
1097 | "dst=%u.%u.%u.%u@%-5d seq=%llu", | |
1098 | dccp_packet_name(dh->dccph_type), | |
1099 | NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport), | |
1100 | NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport), | |
f6ccf554 | 1101 | (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq); |
7c657876 ACM |
1102 | |
1103 | if (dccp_packet_without_ack(skb)) { | |
1104 | DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ; | |
1105 | dccp_pr_debug_cat("\n"); | |
1106 | } else { | |
1107 | DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb); | |
f6ccf554 DM |
1108 | dccp_pr_debug_cat(", ack=%llu\n", |
1109 | (unsigned long long) | |
1110 | DCCP_SKB_CB(skb)->dccpd_ack_seq); | |
7c657876 ACM |
1111 | } |
1112 | ||
1113 | /* Step 2: | |
1114 | * Look up flow ID in table and get corresponding socket */ | |
1115 | sk = __inet_lookup(&dccp_hashinfo, | |
1116 | skb->nh.iph->saddr, dh->dccph_sport, | |
1117 | skb->nh.iph->daddr, ntohs(dh->dccph_dport), | |
1118 | inet_iif(skb)); | |
1119 | ||
1120 | /* | |
1121 | * Step 2: | |
1122 | * If no socket ... | |
1123 | * Generate Reset(No Connection) unless P.type == Reset | |
1124 | * Drop packet and return | |
1125 | */ | |
1126 | if (sk == NULL) { | |
1127 | dccp_pr_debug("failed to look up flow ID in table and " | |
1128 | "get corresponding socket\n"); | |
1129 | goto no_dccp_socket; | |
1130 | } | |
1131 | ||
1132 | /* | |
1133 | * Step 2: | |
1134 | * ... or S.state == TIMEWAIT, | |
1135 | * Generate Reset(No Connection) unless P.type == Reset | |
1136 | * Drop packet and return | |
1137 | */ | |
1138 | ||
1139 | if (sk->sk_state == DCCP_TIME_WAIT) { | |
64cf1e5d ACM |
1140 | dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: " |
1141 | "do_time_wait\n"); | |
1142 | goto do_time_wait; | |
7c657876 ACM |
1143 | } |
1144 | ||
1145 | if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) { | |
1146 | dccp_pr_debug("xfrm4_policy_check failed\n"); | |
1147 | goto discard_and_relse; | |
1148 | } | |
1149 | ||
1150 | if (sk_filter(sk, skb, 0)) { | |
1151 | dccp_pr_debug("sk_filter failed\n"); | |
1152 | goto discard_and_relse; | |
1153 | } | |
1154 | ||
1155 | skb->dev = NULL; | |
1156 | ||
1157 | bh_lock_sock(sk); | |
1158 | rc = 0; | |
1159 | if (!sock_owned_by_user(sk)) | |
1160 | rc = dccp_v4_do_rcv(sk, skb); | |
1161 | else | |
1162 | sk_add_backlog(sk, skb); | |
1163 | bh_unlock_sock(sk); | |
1164 | ||
1165 | sock_put(sk); | |
1166 | return rc; | |
1167 | ||
1168 | no_dccp_socket: | |
1169 | if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) | |
1170 | goto discard_it; | |
1171 | /* | |
1172 | * Step 2: | |
1173 | * Generate Reset(No Connection) unless P.type == Reset | |
1174 | * Drop packet and return | |
1175 | */ | |
1176 | if (dh->dccph_type != DCCP_PKT_RESET) { | |
7690af3f ACM |
1177 | DCCP_SKB_CB(skb)->dccpd_reset_code = |
1178 | DCCP_RESET_CODE_NO_CONNECTION; | |
7c657876 ACM |
1179 | dccp_v4_ctl_send_reset(skb); |
1180 | } | |
1181 | ||
1182 | discard_it: | |
1183 | /* Discard frame. */ | |
1184 | kfree_skb(skb); | |
1185 | return 0; | |
1186 | ||
1187 | discard_and_relse: | |
1188 | sock_put(sk); | |
1189 | goto discard_it; | |
64cf1e5d ACM |
1190 | |
1191 | do_time_wait: | |
1192 | inet_twsk_put((struct inet_timewait_sock *)sk); | |
1193 | goto no_dccp_socket; | |
7c657876 ACM |
1194 | } |
1195 | ||
57cca05a ACM |
1196 | struct inet_connection_sock_af_ops dccp_ipv4_af_ops = { |
1197 | .queue_xmit = ip_queue_xmit, | |
1198 | .send_check = dccp_v4_send_check, | |
1199 | .rebuild_header = inet_sk_rebuild_header, | |
1200 | .conn_request = dccp_v4_conn_request, | |
1201 | .syn_recv_sock = dccp_v4_request_recv_sock, | |
1202 | .net_header_len = sizeof(struct iphdr), | |
1203 | .setsockopt = ip_setsockopt, | |
1204 | .getsockopt = ip_getsockopt, | |
1205 | .addr2sockaddr = inet_csk_addr2sockaddr, | |
1206 | .sockaddr_len = sizeof(struct sockaddr_in), | |
1207 | }; | |
1208 | ||
f21e68ca | 1209 | int dccp_v4_init_sock(struct sock *sk) |
7c657876 ACM |
1210 | { |
1211 | struct dccp_sock *dp = dccp_sk(sk); | |
1212 | static int dccp_ctl_socket_init = 1; | |
1213 | ||
1214 | dccp_options_init(&dp->dccps_options); | |
b0e56780 | 1215 | do_gettimeofday(&dp->dccps_epoch); |
7c657876 ACM |
1216 | |
1217 | if (dp->dccps_options.dccpo_send_ack_vector) { | |
ae31c339 ACM |
1218 | dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(DCCP_MAX_ACKVEC_LEN, |
1219 | GFP_KERNEL); | |
1220 | if (dp->dccps_hc_rx_ackvec == NULL) | |
7c657876 ACM |
1221 | return -ENOMEM; |
1222 | } | |
1223 | ||
1224 | /* | |
1225 | * FIXME: We're hardcoding the CCID, and doing this at this point makes | |
1226 | * the listening (master) sock get CCID control blocks, which is not | |
1227 | * necessary, but for now, to not mess with the test userspace apps, | |
1228 | * lets leave it here, later the real solution is to do this in a | |
1229 | * setsockopt(CCIDs-I-want/accept). -acme | |
1230 | */ | |
1231 | if (likely(!dccp_ctl_socket_init)) { | |
561713cf | 1232 | dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_rx_ccid, |
7690af3f | 1233 | sk); |
561713cf | 1234 | dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_tx_ccid, |
7690af3f | 1235 | sk); |
7c657876 ACM |
1236 | if (dp->dccps_hc_rx_ccid == NULL || |
1237 | dp->dccps_hc_tx_ccid == NULL) { | |
1238 | ccid_exit(dp->dccps_hc_rx_ccid, sk); | |
1239 | ccid_exit(dp->dccps_hc_tx_ccid, sk); | |
ae31c339 ACM |
1240 | if (dp->dccps_options.dccpo_send_ack_vector) { |
1241 | dccp_ackvec_free(dp->dccps_hc_rx_ackvec); | |
1242 | dp->dccps_hc_rx_ackvec = NULL; | |
1243 | } | |
7c657876 ACM |
1244 | dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; |
1245 | return -ENOMEM; | |
1246 | } | |
1247 | } else | |
1248 | dccp_ctl_socket_init = 0; | |
1249 | ||
1250 | dccp_init_xmit_timers(sk); | |
0b4e03bf | 1251 | inet_csk(sk)->icsk_rto = DCCP_TIMEOUT_INIT; |
7c657876 | 1252 | sk->sk_state = DCCP_CLOSED; |
c530cfb1 | 1253 | sk->sk_write_space = dccp_write_space; |
57cca05a | 1254 | inet_csk(sk)->icsk_af_ops = &dccp_ipv4_af_ops; |
7c657876 ACM |
1255 | dp->dccps_mss_cache = 536; |
1256 | dp->dccps_role = DCCP_ROLE_UNDEFINED; | |
67e6b629 | 1257 | dp->dccps_service = DCCP_SERVICE_INVALID_VALUE; |
7c657876 ACM |
1258 | |
1259 | return 0; | |
1260 | } | |
1261 | ||
f21e68ca ACM |
1262 | EXPORT_SYMBOL_GPL(dccp_v4_init_sock); |
1263 | ||
1264 | int dccp_v4_destroy_sock(struct sock *sk) | |
7c657876 ACM |
1265 | { |
1266 | struct dccp_sock *dp = dccp_sk(sk); | |
1267 | ||
1268 | /* | |
4c7e6895 | 1269 | * DCCP doesn't use sk_write_queue, just sk_send_head |
7c657876 ACM |
1270 | * for retransmissions |
1271 | */ | |
1272 | if (sk->sk_send_head != NULL) { | |
1273 | kfree_skb(sk->sk_send_head); | |
1274 | sk->sk_send_head = NULL; | |
1275 | } | |
1276 | ||
1277 | /* Clean up a referenced DCCP bind bucket. */ | |
1278 | if (inet_csk(sk)->icsk_bind_hash != NULL) | |
1279 | inet_put_port(&dccp_hashinfo, sk); | |
1280 | ||
a51482bd JJ |
1281 | kfree(dp->dccps_service_list); |
1282 | dp->dccps_service_list = NULL; | |
67e6b629 | 1283 | |
8efa544f ACM |
1284 | ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk); |
1285 | ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk); | |
ae31c339 ACM |
1286 | if (dp->dccps_options.dccpo_send_ack_vector) { |
1287 | dccp_ackvec_free(dp->dccps_hc_rx_ackvec); | |
1288 | dp->dccps_hc_rx_ackvec = NULL; | |
1289 | } | |
7c657876 ACM |
1290 | ccid_exit(dp->dccps_hc_rx_ccid, sk); |
1291 | ccid_exit(dp->dccps_hc_tx_ccid, sk); | |
1292 | dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; | |
1293 | ||
1294 | return 0; | |
1295 | } | |
1296 | ||
f21e68ca ACM |
1297 | EXPORT_SYMBOL_GPL(dccp_v4_destroy_sock); |
1298 | ||
7c657876 ACM |
1299 | static void dccp_v4_reqsk_destructor(struct request_sock *req) |
1300 | { | |
1301 | kfree(inet_rsk(req)->opt); | |
1302 | } | |
1303 | ||
1304 | static struct request_sock_ops dccp_request_sock_ops = { | |
1305 | .family = PF_INET, | |
1306 | .obj_size = sizeof(struct dccp_request_sock), | |
1307 | .rtx_syn_ack = dccp_v4_send_response, | |
1308 | .send_ack = dccp_v4_reqsk_send_ack, | |
1309 | .destructor = dccp_v4_reqsk_destructor, | |
1310 | .send_reset = dccp_v4_ctl_send_reset, | |
1311 | }; | |
1312 | ||
6d6ee43e ACM |
1313 | static struct timewait_sock_ops dccp_timewait_sock_ops = { |
1314 | .twsk_obj_size = sizeof(struct inet_timewait_sock), | |
1315 | }; | |
1316 | ||
34ca6860 | 1317 | struct proto dccp_prot = { |
7c657876 ACM |
1318 | .name = "DCCP", |
1319 | .owner = THIS_MODULE, | |
1320 | .close = dccp_close, | |
1321 | .connect = dccp_v4_connect, | |
1322 | .disconnect = dccp_disconnect, | |
1323 | .ioctl = dccp_ioctl, | |
1324 | .init = dccp_v4_init_sock, | |
1325 | .setsockopt = dccp_setsockopt, | |
1326 | .getsockopt = dccp_getsockopt, | |
1327 | .sendmsg = dccp_sendmsg, | |
1328 | .recvmsg = dccp_recvmsg, | |
1329 | .backlog_rcv = dccp_v4_do_rcv, | |
1330 | .hash = dccp_v4_hash, | |
f21e68ca | 1331 | .unhash = dccp_unhash, |
7c657876 ACM |
1332 | .accept = inet_csk_accept, |
1333 | .get_port = dccp_v4_get_port, | |
1334 | .shutdown = dccp_shutdown, | |
1335 | .destroy = dccp_v4_destroy_sock, | |
1336 | .orphan_count = &dccp_orphan_count, | |
1337 | .max_header = MAX_DCCP_HEADER, | |
1338 | .obj_size = sizeof(struct dccp_sock), | |
1339 | .rsk_prot = &dccp_request_sock_ops, | |
6d6ee43e | 1340 | .twsk_prot = &dccp_timewait_sock_ops, |
7c657876 | 1341 | }; |
6d6ee43e ACM |
1342 | |
1343 | EXPORT_SYMBOL_GPL(dccp_prot); |