ipv4.c 35 KB

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