ipv4.c 34 KB

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