ipv4.c 34 KB

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