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 inet_sock *inet = inet_sk(sk);
  549. err = ip_build_and_send_pkt(skb, sk,
  550. inet->saddr, inet->daddr, NULL);
  551. if (err == NET_XMIT_CN)
  552. err = 0;
  553. }
  554. return err;
  555. }
  556. static inline u64 dccp_v4_init_sequence(const struct sock *sk,
  557. const struct sk_buff *skb)
  558. {
  559. return secure_dccp_sequence_number(skb->nh.iph->daddr,
  560. skb->nh.iph->saddr,
  561. dccp_hdr(skb)->dccph_dport,
  562. dccp_hdr(skb)->dccph_sport);
  563. }
  564. int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
  565. {
  566. struct inet_request_sock *ireq;
  567. struct dccp_sock dp;
  568. struct request_sock *req;
  569. struct dccp_request_sock *dreq;
  570. const __u32 saddr = skb->nh.iph->saddr;
  571. const __u32 daddr = skb->nh.iph->daddr;
  572. struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
  573. __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
  574. struct dst_entry *dst = NULL;
  575. /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
  576. if (((struct rtable *)skb->dst)->rt_flags &
  577. (RTCF_BROADCAST | RTCF_MULTICAST)) {
  578. reset_code = DCCP_RESET_CODE_NO_CONNECTION;
  579. goto drop;
  580. }
  581. /*
  582. * TW buckets are converted to open requests without
  583. * limitations, they conserve resources and peer is
  584. * evidently real one.
  585. */
  586. if (inet_csk_reqsk_queue_is_full(sk))
  587. goto drop;
  588. /*
  589. * Accept backlog is full. If we have already queued enough
  590. * of warm entries in syn queue, drop request. It is better than
  591. * clogging syn queue with openreqs with exponentially increasing
  592. * timeout.
  593. */
  594. if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
  595. goto drop;
  596. req = reqsk_alloc(sk->sk_prot->rsk_prot);
  597. if (req == NULL)
  598. goto drop;
  599. /* FIXME: process options */
  600. dccp_openreq_init(req, &dp, skb);
  601. ireq = inet_rsk(req);
  602. ireq->loc_addr = daddr;
  603. ireq->rmt_addr = saddr;
  604. /* FIXME: Merge Aristeu's option parsing code when ready */
  605. req->rcv_wnd = 100; /* Fake, option parsing will get the
  606. right value */
  607. ireq->opt = NULL;
  608. /*
  609. * Step 3: Process LISTEN state
  610. *
  611. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
  612. *
  613. * In fact we defer setting S.GSR, S.SWL, S.SWH to
  614. * dccp_create_openreq_child.
  615. */
  616. dreq = dccp_rsk(req);
  617. dreq->dreq_isr = dcb->dccpd_seq;
  618. dreq->dreq_iss = dccp_v4_init_sequence(sk, skb);
  619. dreq->dreq_service = dccp_hdr_request(skb)->dccph_req_service;
  620. if (dccp_v4_send_response(sk, req, dst))
  621. goto drop_and_free;
  622. inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
  623. return 0;
  624. drop_and_free:
  625. /*
  626. * FIXME: should be reqsk_free after implementing req->rsk_ops
  627. */
  628. __reqsk_free(req);
  629. drop:
  630. DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
  631. dcb->dccpd_reset_code = reset_code;
  632. return -1;
  633. }
  634. /*
  635. * The three way handshake has completed - we got a valid ACK or DATAACK -
  636. * now create the new socket.
  637. *
  638. * This is the equivalent of TCP's tcp_v4_syn_recv_sock
  639. */
  640. struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
  641. struct request_sock *req,
  642. struct dst_entry *dst)
  643. {
  644. struct inet_request_sock *ireq;
  645. struct inet_sock *newinet;
  646. struct dccp_sock *newdp;
  647. struct sock *newsk;
  648. if (sk_acceptq_is_full(sk))
  649. goto exit_overflow;
  650. if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
  651. goto exit;
  652. newsk = dccp_create_openreq_child(sk, req, skb);
  653. if (newsk == NULL)
  654. goto exit;
  655. sk_setup_caps(newsk, dst);
  656. newdp = dccp_sk(newsk);
  657. newinet = inet_sk(newsk);
  658. ireq = inet_rsk(req);
  659. newinet->daddr = ireq->rmt_addr;
  660. newinet->rcv_saddr = ireq->loc_addr;
  661. newinet->saddr = ireq->loc_addr;
  662. newinet->opt = ireq->opt;
  663. ireq->opt = NULL;
  664. newinet->mc_index = inet_iif(skb);
  665. newinet->mc_ttl = skb->nh.iph->ttl;
  666. newinet->id = jiffies;
  667. dccp_sync_mss(newsk, dst_mtu(dst));
  668. __inet_hash(&dccp_hashinfo, newsk, 0);
  669. __inet_inherit_port(&dccp_hashinfo, sk, newsk);
  670. return newsk;
  671. exit_overflow:
  672. NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
  673. exit:
  674. NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
  675. dst_release(dst);
  676. return NULL;
  677. }
  678. static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
  679. {
  680. const struct dccp_hdr *dh = dccp_hdr(skb);
  681. const struct iphdr *iph = skb->nh.iph;
  682. struct sock *nsk;
  683. struct request_sock **prev;
  684. /* Find possible connection requests. */
  685. struct request_sock *req = inet_csk_search_req(sk, &prev,
  686. dh->dccph_sport,
  687. iph->saddr, iph->daddr);
  688. if (req != NULL)
  689. return dccp_check_req(sk, skb, req, prev);
  690. nsk = __inet_lookup_established(&dccp_hashinfo,
  691. iph->saddr, dh->dccph_sport,
  692. iph->daddr, ntohs(dh->dccph_dport),
  693. inet_iif(skb));
  694. if (nsk != NULL) {
  695. if (nsk->sk_state != DCCP_TIME_WAIT) {
  696. bh_lock_sock(nsk);
  697. return nsk;
  698. }
  699. inet_twsk_put((struct inet_timewait_sock *)nsk);
  700. return NULL;
  701. }
  702. return sk;
  703. }
  704. int dccp_v4_checksum(const struct sk_buff *skb, const u32 saddr,
  705. const u32 daddr)
  706. {
  707. const struct dccp_hdr* dh = dccp_hdr(skb);
  708. int checksum_len;
  709. u32 tmp;
  710. if (dh->dccph_cscov == 0)
  711. checksum_len = skb->len;
  712. else {
  713. checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
  714. checksum_len = checksum_len < skb->len ? checksum_len :
  715. skb->len;
  716. }
  717. tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
  718. return csum_tcpudp_magic(saddr, daddr, checksum_len,
  719. IPPROTO_DCCP, tmp);
  720. }
  721. static int dccp_v4_verify_checksum(struct sk_buff *skb,
  722. const u32 saddr, const u32 daddr)
  723. {
  724. struct dccp_hdr *dh = dccp_hdr(skb);
  725. int checksum_len;
  726. u32 tmp;
  727. if (dh->dccph_cscov == 0)
  728. checksum_len = skb->len;
  729. else {
  730. checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
  731. checksum_len = checksum_len < skb->len ? checksum_len :
  732. skb->len;
  733. }
  734. tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
  735. return csum_tcpudp_magic(saddr, daddr, checksum_len,
  736. IPPROTO_DCCP, tmp) == 0 ? 0 : -1;
  737. }
  738. static struct dst_entry* dccp_v4_route_skb(struct sock *sk,
  739. struct sk_buff *skb)
  740. {
  741. struct rtable *rt;
  742. struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif,
  743. .nl_u = { .ip4_u =
  744. { .daddr = skb->nh.iph->saddr,
  745. .saddr = skb->nh.iph->daddr,
  746. .tos = RT_CONN_FLAGS(sk) } },
  747. .proto = sk->sk_protocol,
  748. .uli_u = { .ports =
  749. { .sport = dccp_hdr(skb)->dccph_dport,
  750. .dport = dccp_hdr(skb)->dccph_sport }
  751. }
  752. };
  753. if (ip_route_output_flow(&rt, &fl, sk, 0)) {
  754. IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
  755. return NULL;
  756. }
  757. return &rt->u.dst;
  758. }
  759. static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
  760. {
  761. int err;
  762. struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
  763. const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
  764. sizeof(struct dccp_hdr_ext) +
  765. sizeof(struct dccp_hdr_reset);
  766. struct sk_buff *skb;
  767. struct dst_entry *dst;
  768. u64 seqno;
  769. /* Never send a reset in response to a reset. */
  770. if (rxdh->dccph_type == DCCP_PKT_RESET)
  771. return;
  772. if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
  773. return;
  774. dst = dccp_v4_route_skb(dccp_ctl_socket->sk, rxskb);
  775. if (dst == NULL)
  776. return;
  777. skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
  778. if (skb == NULL)
  779. goto out;
  780. /* Reserve space for headers. */
  781. skb_reserve(skb, MAX_DCCP_HEADER);
  782. skb->dst = dst_clone(dst);
  783. skb->h.raw = skb_push(skb, dccp_hdr_reset_len);
  784. dh = dccp_hdr(skb);
  785. memset(dh, 0, dccp_hdr_reset_len);
  786. /* Build DCCP header and checksum it. */
  787. dh->dccph_type = DCCP_PKT_RESET;
  788. dh->dccph_sport = rxdh->dccph_dport;
  789. dh->dccph_dport = rxdh->dccph_sport;
  790. dh->dccph_doff = dccp_hdr_reset_len / 4;
  791. dh->dccph_x = 1;
  792. dccp_hdr_reset(skb)->dccph_reset_code =
  793. DCCP_SKB_CB(rxskb)->dccpd_reset_code;
  794. /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
  795. seqno = 0;
  796. if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
  797. dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
  798. dccp_hdr_set_seq(dh, seqno);
  799. dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
  800. DCCP_SKB_CB(rxskb)->dccpd_seq);
  801. dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr,
  802. rxskb->nh.iph->daddr);
  803. bh_lock_sock(dccp_ctl_socket->sk);
  804. err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
  805. rxskb->nh.iph->daddr,
  806. rxskb->nh.iph->saddr, NULL);
  807. bh_unlock_sock(dccp_ctl_socket->sk);
  808. if (err == NET_XMIT_CN || err == 0) {
  809. DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
  810. DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
  811. }
  812. out:
  813. dst_release(dst);
  814. }
  815. int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
  816. {
  817. struct dccp_hdr *dh = dccp_hdr(skb);
  818. if (sk->sk_state == DCCP_OPEN) { /* Fast path */
  819. if (dccp_rcv_established(sk, skb, dh, skb->len))
  820. goto reset;
  821. return 0;
  822. }
  823. /*
  824. * Step 3: Process LISTEN state
  825. * If S.state == LISTEN,
  826. * If P.type == Request or P contains a valid Init Cookie
  827. * option,
  828. * * Must scan the packet's options to check for an Init
  829. * Cookie. Only the Init Cookie is processed here,
  830. * however; other options are processed in Step 8. This
  831. * scan need only be performed if the endpoint uses Init
  832. * Cookies *
  833. * * Generate a new socket and switch to that socket *
  834. * Set S := new socket for this port pair
  835. * S.state = RESPOND
  836. * Choose S.ISS (initial seqno) or set from Init Cookie
  837. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
  838. * Continue with S.state == RESPOND
  839. * * A Response packet will be generated in Step 11 *
  840. * Otherwise,
  841. * Generate Reset(No Connection) unless P.type == Reset
  842. * Drop packet and return
  843. *
  844. * NOTE: the check for the packet types is done in
  845. * dccp_rcv_state_process
  846. */
  847. if (sk->sk_state == DCCP_LISTEN) {
  848. struct sock *nsk = dccp_v4_hnd_req(sk, skb);
  849. if (nsk == NULL)
  850. goto discard;
  851. if (nsk != sk) {
  852. if (dccp_child_process(sk, nsk, skb))
  853. goto reset;
  854. return 0;
  855. }
  856. }
  857. if (dccp_rcv_state_process(sk, skb, dh, skb->len))
  858. goto reset;
  859. return 0;
  860. reset:
  861. dccp_v4_ctl_send_reset(skb);
  862. discard:
  863. kfree_skb(skb);
  864. return 0;
  865. }
  866. static inline int dccp_invalid_packet(struct sk_buff *skb)
  867. {
  868. const struct dccp_hdr *dh;
  869. if (skb->pkt_type != PACKET_HOST)
  870. return 1;
  871. if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
  872. LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n");
  873. return 1;
  874. }
  875. dh = dccp_hdr(skb);
  876. /* If the packet type is not understood, drop packet and return */
  877. if (dh->dccph_type >= DCCP_PKT_INVALID) {
  878. LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n");
  879. return 1;
  880. }
  881. /*
  882. * If P.Data Offset is too small for packet type, or too large for
  883. * packet, drop packet and return
  884. */
  885. if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
  886. LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
  887. "too small 1\n",
  888. dh->dccph_doff);
  889. return 1;
  890. }
  891. if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
  892. LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
  893. "too small 2\n",
  894. dh->dccph_doff);
  895. return 1;
  896. }
  897. dh = dccp_hdr(skb);
  898. /*
  899. * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
  900. * has short sequence numbers), drop packet and return
  901. */
  902. if (dh->dccph_x == 0 &&
  903. dh->dccph_type != DCCP_PKT_DATA &&
  904. dh->dccph_type != DCCP_PKT_ACK &&
  905. dh->dccph_type != DCCP_PKT_DATAACK) {
  906. LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack "
  907. "nor DataAck and P.X == 0\n",
  908. dccp_packet_name(dh->dccph_type));
  909. return 1;
  910. }
  911. /* If the header checksum is incorrect, drop packet and return */
  912. if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr,
  913. skb->nh.iph->daddr) < 0) {
  914. LIMIT_NETDEBUG(KERN_WARNING "DCCP: header checksum is "
  915. "incorrect\n");
  916. return 1;
  917. }
  918. return 0;
  919. }
  920. /* this is called when real data arrives */
  921. int dccp_v4_rcv(struct sk_buff *skb)
  922. {
  923. const struct dccp_hdr *dh;
  924. struct sock *sk;
  925. int rc;
  926. /* Step 1: Check header basics: */
  927. if (dccp_invalid_packet(skb))
  928. goto discard_it;
  929. dh = dccp_hdr(skb);
  930. #if 0
  931. /*
  932. * Use something like this to simulate some DATA/DATAACK loss to test
  933. * dccp_ackpkts_add, you'll get something like this on a session that
  934. * sends 10 DATA/DATAACK packets:
  935. *
  936. * ackpkts_print: 281473596467422 |0,0|3,0|0,0|3,0|0,0|3,0|0,0|3,0|0,1|
  937. *
  938. * 0, 0 means: DCCP_ACKPKTS_STATE_RECEIVED, RLE == just this packet
  939. * 0, 1 means: DCCP_ACKPKTS_STATE_RECEIVED, RLE == two adjacent packets
  940. * with the same state
  941. * 3, 0 means: DCCP_ACKPKTS_STATE_NOT_RECEIVED, RLE == just this packet
  942. *
  943. * So...
  944. *
  945. * 281473596467422 was received
  946. * 281473596467421 was not received
  947. * 281473596467420 was received
  948. * 281473596467419 was not received
  949. * 281473596467418 was received
  950. * 281473596467417 was not received
  951. * 281473596467416 was received
  952. * 281473596467415 was not received
  953. * 281473596467414 was received
  954. * 281473596467413 was received (this one was the 3way handshake
  955. * RESPONSE)
  956. *
  957. */
  958. if (dh->dccph_type == DCCP_PKT_DATA ||
  959. dh->dccph_type == DCCP_PKT_DATAACK) {
  960. static int discard = 0;
  961. if (discard) {
  962. discard = 0;
  963. goto discard_it;
  964. }
  965. discard = 1;
  966. }
  967. #endif
  968. DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
  969. DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
  970. dccp_pr_debug("%8.8s "
  971. "src=%u.%u.%u.%u@%-5d "
  972. "dst=%u.%u.%u.%u@%-5d seq=%llu",
  973. dccp_packet_name(dh->dccph_type),
  974. NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport),
  975. NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport),
  976. (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
  977. if (dccp_packet_without_ack(skb)) {
  978. DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
  979. dccp_pr_debug_cat("\n");
  980. } else {
  981. DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
  982. dccp_pr_debug_cat(", ack=%llu\n",
  983. (unsigned long long)
  984. DCCP_SKB_CB(skb)->dccpd_ack_seq);
  985. }
  986. /* Step 2:
  987. * Look up flow ID in table and get corresponding socket */
  988. sk = __inet_lookup(&dccp_hashinfo,
  989. skb->nh.iph->saddr, dh->dccph_sport,
  990. skb->nh.iph->daddr, ntohs(dh->dccph_dport),
  991. inet_iif(skb));
  992. /*
  993. * Step 2:
  994. * If no socket ...
  995. * Generate Reset(No Connection) unless P.type == Reset
  996. * Drop packet and return
  997. */
  998. if (sk == NULL) {
  999. dccp_pr_debug("failed to look up flow ID in table and "
  1000. "get corresponding socket\n");
  1001. goto no_dccp_socket;
  1002. }
  1003. /*
  1004. * Step 2:
  1005. * ... or S.state == TIMEWAIT,
  1006. * Generate Reset(No Connection) unless P.type == Reset
  1007. * Drop packet and return
  1008. */
  1009. if (sk->sk_state == DCCP_TIME_WAIT) {
  1010. dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
  1011. "do_time_wait\n");
  1012. goto do_time_wait;
  1013. }
  1014. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
  1015. dccp_pr_debug("xfrm4_policy_check failed\n");
  1016. goto discard_and_relse;
  1017. }
  1018. if (sk_filter(sk, skb, 0)) {
  1019. dccp_pr_debug("sk_filter failed\n");
  1020. goto discard_and_relse;
  1021. }
  1022. skb->dev = NULL;
  1023. bh_lock_sock(sk);
  1024. rc = 0;
  1025. if (!sock_owned_by_user(sk))
  1026. rc = dccp_v4_do_rcv(sk, skb);
  1027. else
  1028. sk_add_backlog(sk, skb);
  1029. bh_unlock_sock(sk);
  1030. sock_put(sk);
  1031. return rc;
  1032. no_dccp_socket:
  1033. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1034. goto discard_it;
  1035. /*
  1036. * Step 2:
  1037. * Generate Reset(No Connection) unless P.type == Reset
  1038. * Drop packet and return
  1039. */
  1040. if (dh->dccph_type != DCCP_PKT_RESET) {
  1041. DCCP_SKB_CB(skb)->dccpd_reset_code =
  1042. DCCP_RESET_CODE_NO_CONNECTION;
  1043. dccp_v4_ctl_send_reset(skb);
  1044. }
  1045. discard_it:
  1046. /* Discard frame. */
  1047. kfree_skb(skb);
  1048. return 0;
  1049. discard_and_relse:
  1050. sock_put(sk);
  1051. goto discard_it;
  1052. do_time_wait:
  1053. inet_twsk_put((struct inet_timewait_sock *)sk);
  1054. goto no_dccp_socket;
  1055. }
  1056. static int dccp_v4_init_sock(struct sock *sk)
  1057. {
  1058. struct dccp_sock *dp = dccp_sk(sk);
  1059. static int dccp_ctl_socket_init = 1;
  1060. dccp_options_init(&dp->dccps_options);
  1061. do_gettimeofday(&dp->dccps_epoch);
  1062. if (dp->dccps_options.dccpo_send_ack_vector) {
  1063. dp->dccps_hc_rx_ackpkts =
  1064. dccp_ackpkts_alloc(DCCP_MAX_ACK_VECTOR_LEN,
  1065. GFP_KERNEL);
  1066. if (dp->dccps_hc_rx_ackpkts == NULL)
  1067. return -ENOMEM;
  1068. }
  1069. /*
  1070. * FIXME: We're hardcoding the CCID, and doing this at this point makes
  1071. * the listening (master) sock get CCID control blocks, which is not
  1072. * necessary, but for now, to not mess with the test userspace apps,
  1073. * lets leave it here, later the real solution is to do this in a
  1074. * setsockopt(CCIDs-I-want/accept). -acme
  1075. */
  1076. if (likely(!dccp_ctl_socket_init)) {
  1077. dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_ccid,
  1078. sk);
  1079. dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_ccid,
  1080. sk);
  1081. if (dp->dccps_hc_rx_ccid == NULL ||
  1082. dp->dccps_hc_tx_ccid == NULL) {
  1083. ccid_exit(dp->dccps_hc_rx_ccid, sk);
  1084. ccid_exit(dp->dccps_hc_tx_ccid, sk);
  1085. dccp_ackpkts_free(dp->dccps_hc_rx_ackpkts);
  1086. dp->dccps_hc_rx_ackpkts = NULL;
  1087. dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
  1088. return -ENOMEM;
  1089. }
  1090. } else
  1091. dccp_ctl_socket_init = 0;
  1092. dccp_init_xmit_timers(sk);
  1093. inet_csk(sk)->icsk_rto = DCCP_TIMEOUT_INIT;
  1094. sk->sk_state = DCCP_CLOSED;
  1095. sk->sk_write_space = dccp_write_space;
  1096. dp->dccps_mss_cache = 536;
  1097. dp->dccps_role = DCCP_ROLE_UNDEFINED;
  1098. return 0;
  1099. }
  1100. static int dccp_v4_destroy_sock(struct sock *sk)
  1101. {
  1102. struct dccp_sock *dp = dccp_sk(sk);
  1103. /*
  1104. * DCCP doesn't use sk_qrite_queue, just sk_send_head
  1105. * for retransmissions
  1106. */
  1107. if (sk->sk_send_head != NULL) {
  1108. kfree_skb(sk->sk_send_head);
  1109. sk->sk_send_head = NULL;
  1110. }
  1111. /* Clean up a referenced DCCP bind bucket. */
  1112. if (inet_csk(sk)->icsk_bind_hash != NULL)
  1113. inet_put_port(&dccp_hashinfo, sk);
  1114. ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
  1115. ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
  1116. dccp_ackpkts_free(dp->dccps_hc_rx_ackpkts);
  1117. dp->dccps_hc_rx_ackpkts = NULL;
  1118. ccid_exit(dp->dccps_hc_rx_ccid, sk);
  1119. ccid_exit(dp->dccps_hc_tx_ccid, sk);
  1120. dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
  1121. return 0;
  1122. }
  1123. static void dccp_v4_reqsk_destructor(struct request_sock *req)
  1124. {
  1125. kfree(inet_rsk(req)->opt);
  1126. }
  1127. static struct request_sock_ops dccp_request_sock_ops = {
  1128. .family = PF_INET,
  1129. .obj_size = sizeof(struct dccp_request_sock),
  1130. .rtx_syn_ack = dccp_v4_send_response,
  1131. .send_ack = dccp_v4_reqsk_send_ack,
  1132. .destructor = dccp_v4_reqsk_destructor,
  1133. .send_reset = dccp_v4_ctl_send_reset,
  1134. };
  1135. struct proto dccp_v4_prot = {
  1136. .name = "DCCP",
  1137. .owner = THIS_MODULE,
  1138. .close = dccp_close,
  1139. .connect = dccp_v4_connect,
  1140. .disconnect = dccp_disconnect,
  1141. .ioctl = dccp_ioctl,
  1142. .init = dccp_v4_init_sock,
  1143. .setsockopt = dccp_setsockopt,
  1144. .getsockopt = dccp_getsockopt,
  1145. .sendmsg = dccp_sendmsg,
  1146. .recvmsg = dccp_recvmsg,
  1147. .backlog_rcv = dccp_v4_do_rcv,
  1148. .hash = dccp_v4_hash,
  1149. .unhash = dccp_v4_unhash,
  1150. .accept = inet_csk_accept,
  1151. .get_port = dccp_v4_get_port,
  1152. .shutdown = dccp_shutdown,
  1153. .destroy = dccp_v4_destroy_sock,
  1154. .orphan_count = &dccp_orphan_count,
  1155. .max_header = MAX_DCCP_HEADER,
  1156. .obj_size = sizeof(struct dccp_sock),
  1157. .rsk_prot = &dccp_request_sock_ops,
  1158. .twsk_obj_size = sizeof(struct inet_timewait_sock),
  1159. };