ipv4.c 35 KB

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