ipv4.c 34 KB

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