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